forked from NetComposer/nksip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gruu_test.erl
182 lines (147 loc) · 6.52 KB
/
gruu_test.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
%% -------------------------------------------------------------------
%%
%% gruu_test: Gruu (RFC5627) Test Suite
%%
%% Copyright (c) 2013 Carlos Gonzalez Florido. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------
-module(gruu_test).
-include_lib("nklib/include/nklib.hrl").
-include_lib("eunit/include/eunit.hrl").
-include("../include/nksip.hrl").
-compile([export_all]).
gruu_test_() ->
{setup, spawn,
fun() -> start() end,
fun(_) -> stop() end,
[
fun register/0,
fun temp_gruu/0
]
}.
start() ->
tests_util:start_nksip(),
ok = tests_util:start(server1, ?MODULE, [
{sip_from, "sip:server1@nksip"},
{sip_local_host, "localhost"},
{plugins, [nksip_registrar, nksip_gruu]},
{sip_listen, "sip:all:5060, <sip:all:5061;transport=tls>"}
]),
ok = tests_util:start(ua1, ?MODULE, [
{sip_from, "sip:client1@nksip"},
{sip_local_host, "127.0.0.1"},
{plugins, [nksip_gruu]},
{sip_listen, ["<sip:all:5070>", "<sip:all:5071;transport=tls>"]}
]),
ok = tests_util:start(ua2, ?MODULE, [
{sip_from, "sip:client1@nksip"},
{sip_local_host, "127.0.0.1"},
{plugins, [nksip_gruu]},
{sip_listen, "<sip:all:5080>,<sip:all:5081;transport=tls>"}
]),
nksip_registrar_util:clear(),
tests_util:log(),
?debugFmt("Starting ~p", [?MODULE]).
stop() ->
ok = nksip:stop(server1),
ok = nksip:stop(ua1),
ok = nksip:stop(ua2).
register() ->
{ok, 200, []} = nksip_uac:register(ua1, "sip:127.0.0.1", [unregister_all]),
{ok, 200, [{_, [PC1]}]} =
nksip_uac:register(ua1, "sip:127.0.0.1", [contact, {meta, [contacts]}]),
#uri{
user = <<"client1">>,
domain = <<"127.0.0.1">>,
port = 5070,
ext_opts = EOpts1
} = PC1,
Inst1 = list_to_binary(
nklib_util:unquote(nklib_util:get_value(<<"+sip.instance">>, EOpts1))),
[Pub1] = nksip_parse:ruris(nklib_util:unquote(
nklib_util:get_value(<<"pub-gruu">>, EOpts1))),
[Tmp1] = nksip_parse:ruris(nklib_util:unquote(
nklib_util:get_value(<<"temp-gruu">>, EOpts1))),
Inst1 = nksip:get_uuid(ua1),
#uri{user = <<"client1">>, domain = <<"nksip">>, port = 0} = Pub1,
#uri{domain = <<"nksip">>, port=0} = Tmp1,
{ok, Pub1} = nksip_gruu:get_gruu_pub(ua1),
{ok, Tmp1} = nksip_gruu:get_gruu_temp(ua1),
% Now we register a second UA with the same AOR
{ok, 200, [{_, [PC2, PC1]}]} =
nksip_uac:register(ua2, "sip:127.0.0.1", [contact, {meta, [contacts]}]),
#uri{
user = <<"client1">>,
domain = <<"127.0.0.1">>,
port = 5080,
ext_opts = EOpts2
} = PC2,
Inst2 = list_to_binary(
nklib_util:unquote(nklib_util:get_value(<<"+sip.instance">>, EOpts2))),
[Pub2] = nksip_parse:ruris(nklib_util:unquote(
nklib_util:get_value(<<"pub-gruu">>, EOpts2))),
[Tmp2] = nksip_parse:ruris(nklib_util:unquote(
nklib_util:get_value(<<"temp-gruu">>, EOpts2))),
Inst2 = nksip:get_uuid(ua2),
#uri{user = <<"client1">>, domain = <<"nksip">>, port = 0} = Pub2,
#uri{domain = <<"nksip">>, port=0} = Tmp2,
{ok, Pub2} = nksip_gruu:get_gruu_pub(ua2),
{ok, Tmp2} = nksip_gruu:get_gruu_temp(ua2),
% Now we have two contacts stored for this AOR
[PC2, PC1] = nksip_registrar:find(server1, sip, <<"client1">>, <<"nksip">>),
% true = PC2#uri{ext_opts=[]} == PC2a#uri{headers=[]},
% true = PC1#uri{ext_opts=[]} == PC1a#uri{headers=[]},
% But we use the Public or Private GRUUs, only one of each
[PC1a] = nksip_gruu:registrar_find(server1, Pub1),
[PC2a] = nksip_gruu:registrar_find(server1, Pub2),
[PC1a] = nksip_gruu:registrar_find(server1, Tmp1),
[PC2a] = nksip_gruu:registrar_find(server1, Tmp2),
{ok, 403, []} = nksip_uac:register(ua1, "sip:127.0.0.1", [{contact, Pub1}]),
{ok, 403, []} = nksip_uac:register(ua1, "sip:127.0.0.1", [{contact, Tmp1}]),
ok.
temp_gruu() ->
{ok, 200, []} = nksip_uac:register(ua1, "sip:127.0.0.1", [unregister_all]),
{ok, 200, [{_, CallId}, {_, CSeq}, {_, [#uri{ext_opts=EOpts1}]}]} =
nksip_uac:register(ua1, "sip:127.0.0.1",
[contact, {meta, [call_id, cseq_num, contacts]}]),
[Tmp1] = nksip_parse:ruris(nklib_util:unquote(
nklib_util:get_value(<<"temp-gruu">>, EOpts1))),
% We send a new request with the same Call-ID, NkSIP generates a new valid
% and different temporary GRUU, both are valid
{ok, 200, [{_, [#uri{ext_opts=EOpts2}]}]} =
nksip_uac:register(ua1, "sip:127.0.0.1",
[contact, {call_id, CallId}, {cseq_num, CSeq+1},
{meta, [contacts]}]),
[Tmp2] = nksip_parse:ruris(nklib_util:unquote(
nklib_util:get_value(<<"temp-gruu">>, EOpts2))),
true = Tmp1 /= Tmp2,
[#uri{port=5070}] = nksip_gruu:registrar_find(server1, Tmp1),
[#uri{port=5070}] = nksip_gruu:registrar_find(server1, Tmp2),
% Now we change the Call-ID, both are invalidated and only the new one is valid
{ok, 200, [{_, [#uri{ext_opts=EOpts3}]}]} =
nksip_uac:register(ua1, "sip:127.0.0.1", [contact, {meta, [contacts]}]),
[Tmp3] = nksip_parse:ruris(nklib_util:unquote(
nklib_util:get_value(<<"temp-gruu">>, EOpts3))),
true = Tmp1 /= Tmp3 andalso Tmp2 /= Tmp3,
[] = nksip_gruu:registrar_find(server1, Tmp1),
[] = nksip_gruu:registrar_find(server1, Tmp2),
[#uri{port=5070}] = nksip_gruu:registrar_find(server1, Tmp3),
{ok, 200, []} = nksip_uac:register(ua1, "sip:127.0.0.1", [unregister_all]),
[] = nksip_gruu:registrar_find(server1, Tmp3),
ok.
%%%%%%%%%%%%%%%%%%%%%%% CallBacks (servers and clients) %%%%%%%%%%%%%%%%%%%%%
%% (not necessary, using all defaults in nksip_sipapp)