belle_sip_uri_tester.c 20 KB
Newer Older
jehan's avatar
jehan committed
1 2 3 4 5 6
/*
	belle-sip - SIP (RFC3261) library.
    Copyright (C) 2010  Belledonne Communications SARL

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
7
    the Free Software Foundation, either version 2 of the License, or
jehan's avatar
jehan committed
8 9 10 11 12 13 14 15 16 17
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
jehan's avatar
jehan committed
18

19
#include "belle-sip/belle-sip.h"
Ghislain MARY's avatar
Ghislain MARY committed
20
#include "belle_sip_tester.h"
21
#include "belle_sip_internal.h"
jehan's avatar
jehan committed
22 23


jehan's avatar
jehan committed
24
static void testSIMPLEURI(void) {
25
	belle_sip_uri_t* L_tmp;
26
	belle_sip_uri_t* L_uri = belle_sip_uri_parse("sip:sip.titi.com");
27 28
	char* l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
29 30 31
	L_tmp = belle_sip_uri_parse(l_raw_uri);
	L_uri = BELLE_SIP_URI(belle_sip_object_clone(BELLE_SIP_OBJECT(L_tmp)));
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_tmp));
32
	belle_sip_free(l_raw_uri);
33

34 35 36
	BC_ASSERT_PTR_NULL(belle_sip_uri_get_user(L_uri));
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(L_uri), "sip.titi.com");
	BC_ASSERT_PTR_NULL(belle_sip_uri_get_transport_param(L_uri));
37
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
jehan's avatar
jehan committed
38 39
}

jehan's avatar
jehan committed
40
static void testCOMPLEXURI(void) {
41
	belle_sip_uri_t* L_tmp;
42
	belle_sip_uri_t *  L_uri = belle_sip_uri_parse("sip:toto@titi.com:5060;transport=tcp");
43 44
	char* l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
45 46 47
	L_tmp = belle_sip_uri_parse(l_raw_uri);
	L_uri = BELLE_SIP_URI(belle_sip_object_clone(BELLE_SIP_OBJECT(L_tmp)));
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_tmp));
48
	belle_sip_free(l_raw_uri);
49 50 51 52
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_user(L_uri), "toto");
	BC_ASSERT_EQUAL(belle_sip_uri_get_port(L_uri), 5060, int, "%d");
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(L_uri), "titi.com");
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_transport_param(L_uri), "tcp");
53
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
jehan's avatar
jehan committed
54
}
55 56 57

static void testIPV6URI_base(const char* ip6) {
	belle_sip_uri_t* L_tmp;
58 59
	belle_sip_uri_t * L_uri;
	char* l_raw_uri;
60 61
	char uri[256];
	snprintf(uri,sizeof(uri),"sip:toto@[%s]:5060;transport=tcp",ip6);
62 63
	L_uri = belle_sip_uri_parse(uri);
	l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
64 65 66 67 68
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
	L_tmp = belle_sip_uri_parse(l_raw_uri);
	L_uri = BELLE_SIP_URI(belle_sip_object_clone(BELLE_SIP_OBJECT(L_tmp)));
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_tmp));
	belle_sip_free(l_raw_uri);
69 70 71 72
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_user(L_uri), "toto");
	BC_ASSERT_EQUAL(belle_sip_uri_get_port(L_uri), 5060, int, "%d");
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(L_uri),ip6);
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_transport_param(L_uri), "tcp");
73 74 75 76 77 78
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
}

static void testIPV6URI(void) {
	testIPV6URI_base("fe80::1");
	testIPV6URI_base("2a01:e35:1387:1020:6233:4bff:fe0b:5663");
79 80
	testIPV6URI_base("2a01:e35:1387:1020:6233::5663");
	testIPV6URI_base("::1");
81
}
Ghislain MARY's avatar
Ghislain MARY committed
82

jehan's avatar
jehan committed
83
static void testSIPSURI(void) {
84

85
	belle_sip_uri_t *  L_uri = belle_sip_uri_parse("sips:linphone.org");
86 87 88 89
	char* l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
	L_uri = belle_sip_uri_parse(l_raw_uri);
	belle_sip_free(l_raw_uri);
90
	BC_ASSERT_EQUAL(belle_sip_uri_is_secure(L_uri), 1, int, "%d");
91
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
jehan's avatar
jehan committed
92
	L_uri = belle_sip_uri_parse("sip:linphone.org");
93
	BC_ASSERT_EQUAL(belle_sip_uri_is_secure(L_uri), 0, int, "%d");
94
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
jehan's avatar
jehan committed
95
}
Ghislain MARY's avatar
Ghislain MARY committed
96

jehan's avatar
jehan committed
97
static void test_ip_host(void) {
98
	belle_sip_uri_t *  L_uri = belle_sip_uri_parse("sip:192.168.0.1");
99 100 101 102
	char* l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
	L_uri = belle_sip_uri_parse(l_raw_uri);
	belle_sip_free(l_raw_uri);
103
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(L_uri), "192.168.0.1");
104
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
jehan's avatar
jehan committed
105
}
Ghislain MARY's avatar
Ghislain MARY committed
106

jehan's avatar
jehan committed
107
static void test_lr(void) {
108
	belle_sip_uri_t *  L_uri = belle_sip_uri_parse("sip:192.168.0.1;lr");
109 110 111 112
	char* l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
	L_uri = belle_sip_uri_parse(l_raw_uri);
	belle_sip_free(l_raw_uri);
113 114
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(L_uri), "192.168.0.1");
	BC_ASSERT_EQUAL(belle_sip_uri_has_lr_param(L_uri), 1, int, "%d");
115
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
jehan's avatar
jehan committed
116 117

}
Ghislain MARY's avatar
Ghislain MARY committed
118

jehan's avatar
jehan committed
119
static void test_maddr(void) {
120
	belle_sip_uri_t *  L_uri = belle_sip_uri_parse("sip:192.168.0.1;lr;maddr=linphone.org");
121 122 123 124
	char* l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
	L_uri = belle_sip_uri_parse(l_raw_uri);
	belle_sip_free(l_raw_uri);
125
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_maddr_param(L_uri), "linphone.org");
126
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
jehan's avatar
jehan committed
127 128

}
Ghislain MARY's avatar
Ghislain MARY committed
129

jehan's avatar
jehan committed
130 131 132 133 134 135
static void test_user_passwd(void) {
	belle_sip_uri_t *  L_uri = belle_sip_uri_parse("sip:toto:tata@bla;");
	char* l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
	L_uri = belle_sip_uri_parse(l_raw_uri);
	belle_sip_free(l_raw_uri);
136
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_user_password(L_uri), "tata");
jehan's avatar
jehan committed
137 138 139 140 141
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));

}


142
static void test_uri_parameters (void) {
143
	char* l_raw_uri;
144
	belle_sip_uri_t* L_tmp;
145
	belle_sip_uri_t *  L_uri = belle_sip_uri_parse("sip:192.168.0.1;ttl=12");
146
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
147 148

	L_uri = belle_sip_uri_parse("sip:maddr=@192.168.0.1;lr;maddr=192.168.0.1;user=ip;ttl=140;transport=sctp;method=INVITE;rport=5060");
149
	l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
150

151
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
152 153 154 155
	L_tmp = belle_sip_uri_parse(l_raw_uri);
	L_uri = BELLE_SIP_URI(belle_sip_object_clone(BELLE_SIP_OBJECT(L_tmp)));
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_tmp));

156
	belle_sip_free(l_raw_uri);
157 158 159 160 161
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_maddr_param(L_uri), "192.168.0.1");
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_user_param(L_uri), "ip");
	BC_ASSERT_EQUAL(belle_sip_uri_get_ttl_param(L_uri),140, int, "%d");
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_transport_param(L_uri), "sctp");
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_method_param(L_uri), "INVITE");
162

163
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
164
}
Ghislain MARY's avatar
Ghislain MARY committed
165

jehan's avatar
jehan committed
166
static void test_headers(void) {
167
	belle_sip_uri_t *  L_uri = belle_sip_uri_parse("sip:192.168.0.1?toto=titi");
168 169 170 171
	char* l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
	L_uri = belle_sip_uri_parse(l_raw_uri);
	belle_sip_free(l_raw_uri);
172 173
	BC_ASSERT_PTR_NOT_NULL_FATAL(belle_sip_uri_get_header(L_uri,"toto"));
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_header(L_uri,"toto"), "titi");
174

175
	BC_ASSERT_PTR_NULL(belle_sip_uri_get_header(L_uri,"bla"));
176
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
177
	L_uri = belle_sip_uri_parse("sip:192.168.0.1?toto=titi&header2=popo&header3=");
178 179 180 181
	l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
	L_uri = belle_sip_uri_parse(l_raw_uri);
	belle_sip_free(l_raw_uri);
182

183 184
	BC_ASSERT_PTR_NOT_NULL_FATAL(belle_sip_uri_get_header(L_uri,"toto"));
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_header(L_uri,"header2"), "popo");
jehan's avatar
jehan committed
185
	belle_sip_object_unref(L_uri);
186
}
187
static void test_escaped_headers(void) {
188 189 190 191 192 193
	const char* raw_uri_2=	"sip:eNgwBpkNcH6EdTHlX0cq8@toto.com?"
							"P-Group-Id=Fu0hHIQ23H4hveVT:New%20Group"
							"&P-Expert-Profile-Id=zKQOBOB2jTmUOjkB:New%20Group"
							"&P-Reverse-Charging=0&P-Campaign-Id=none"
							"&P-Embed-Url=https://toto.com/caller/?1.4.0-dev-42-91bdf0c%26id%3DFu0hHIQ23H4hveVT%26CAMPAIGN_ID%3Dnone";

194 195 196 197 198
	belle_sip_uri_t *  L_uri = belle_sip_uri_parse("sip:toto@sip.linhone.org?User-to-User=323a313030363a3230385a48363039313941364b4342463845495936%3Bencoding%3Dhex");
	char* l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
	L_uri = belle_sip_uri_parse(l_raw_uri);
	belle_sip_free(l_raw_uri);
199 200
	BC_ASSERT_PTR_NOT_NULL_FATAL(belle_sip_uri_get_header(L_uri,"User-to-User"));
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_header(L_uri,"User-to-User"), "323a313030363a3230385a48363039313941364b4342463845495936;encoding=hex");
201
	belle_sip_object_unref(L_uri);
202

203 204 205
	L_uri = belle_sip_uri_parse(raw_uri_2);
	l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
	belle_sip_free(l_raw_uri);
206 207
	BC_ASSERT_PTR_NOT_NULL_FATAL(belle_sip_uri_get_header(L_uri,"P-Embed-Url"));
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_header(L_uri,"P-Embed-Url"), "https://toto.com/caller/?1.4.0-dev-42-91bdf0c&id=Fu0hHIQ23H4hveVT&CAMPAIGN_ID=none");
208
	belle_sip_object_unref(L_uri);
209 210


211
}
212

jehan's avatar
jehan committed
213 214
static void testSIMPLEURI_error(void) {
	belle_sip_uri_t* L_uri = belle_sip_uri_parse("siptcom");
215
	BC_ASSERT_PTR_NULL(L_uri);
jehan's avatar
jehan committed
216

jehan's avatar
jehan committed
217
}
jehan's avatar
jehan committed
218

219 220 221 222 223 224 225 226 227
static void test_escaped_username(void) {
	belle_sip_uri_t* L_tmp;
	belle_sip_uri_t *  L_uri = belle_sip_uri_parse("sip:toto%40linphone.org@titi.com");
	char* l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
	L_tmp = belle_sip_uri_parse(l_raw_uri);
	L_uri = BELLE_SIP_URI(belle_sip_object_clone(BELLE_SIP_OBJECT(L_tmp)));
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_tmp));
	belle_sip_free(l_raw_uri);
228 229
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_user(L_uri), "toto@linphone.org");
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(L_uri), "titi.com");
230 231 232
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
}

jehan's avatar
jehan committed
233 234 235 236 237 238 239 240 241
static void test_escaped_passwd(void) {
	belle_sip_uri_t* L_tmp;
	belle_sip_uri_t *  L_uri = belle_sip_uri_parse("sips:%22jehan%22%20%3cjehan%40sip2.linphone.org:544%3e@sip.linphone.org");
	char* l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
	L_tmp = belle_sip_uri_parse(l_raw_uri);
	L_uri = BELLE_SIP_URI(belle_sip_object_clone(BELLE_SIP_OBJECT(L_tmp)));
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_tmp));
	belle_sip_free(l_raw_uri);
242 243 244
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_user(L_uri), "\"jehan\" <jehan@sip2.linphone.org");
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(L_uri), "sip.linphone.org");
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_user_password(L_uri), "544>");
jehan's avatar
jehan committed
245 246 247 248 249

	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));

}

250 251 252

static void test_escaped_parameter(void) {
	belle_sip_uri_t* L_tmp;
jehan's avatar
jehan committed
253
	belle_sip_uri_t *  L_uri = belle_sip_uri_parse("sip:toto@titi.com;pa%3Dram=aa%40bb:5060[];o%40");
254 255
	char* l_raw_uri = belle_sip_object_to_string(BELLE_SIP_OBJECT(L_uri));
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
Guillaume Beraudo's avatar
Guillaume Beraudo committed
256
	L_tmp = belle_sip_uri_parse(l_raw_uri);
257 258 259
	L_uri = BELLE_SIP_URI(belle_sip_object_clone(BELLE_SIP_OBJECT(L_tmp)));
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_tmp));
	belle_sip_free(l_raw_uri);
260 261 262
	BC_ASSERT_STRING_EQUAL(belle_sip_parameters_get_parameter(BELLE_SIP_PARAMETERS(L_uri), "pa=ram"), "aa@bb:5060[]");
	BC_ASSERT_TRUE(belle_sip_parameters_has_parameter(BELLE_SIP_PARAMETERS(L_uri), "o@"));
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(L_uri), "titi.com");
263 264 265
	belle_sip_object_unref(BELLE_SIP_OBJECT(L_uri));
}

jehan's avatar
jehan committed
266 267 268 269 270 271 272 273 274 275
static void test_uri_equals(void) {
	belle_sip_uri_t* a;
	belle_sip_uri_t* b;
/*
	 *    The URIs within each of the following sets are equivalent:

	   sip:%61lice@atlanta.com;transport=TCP
	   sip:alice@AtLanTa.CoM;Transport=tcp
*/
	a = belle_sip_uri_parse("sip:%61lice@atlanta.com;transport=TCP");
276
	BC_ASSERT_PTR_NOT_NULL_FATAL(a);
jehan's avatar
jehan committed
277
	b = belle_sip_uri_parse("sip:alice@AtLanTa.CoM;Transport=tcp");
278 279
	BC_ASSERT_PTR_NOT_NULL_FATAL(b);
	BC_ASSERT_TRUE(belle_sip_uri_equals(a,b));
jehan's avatar
jehan committed
280 281 282 283 284 285 286 287
	belle_sip_object_unref(a);
	belle_sip_object_unref(b);
	/*
	   sip:carol@chicago.com
	   sip:carol@chicago.com;newparam=5
	   sip:carol@chicago.com;security=on
*/
	a = belle_sip_uri_parse("sip:carol@chicago.com");
288
	BC_ASSERT_PTR_NOT_NULL_FATAL(a);
jehan's avatar
jehan committed
289
	b = belle_sip_uri_parse("sip:carol@chicago.com;newparam=5");
290 291
	BC_ASSERT_PTR_NOT_NULL_FATAL(b);
	BC_ASSERT_TRUE(belle_sip_uri_equals(a,b));
jehan's avatar
jehan committed
292 293 294 295 296 297 298 299
	belle_sip_object_unref(a);
	belle_sip_object_unref(b);
/*

	   sip:biloxi.com;transport=tcp;method=REGISTER?to=sip:bob%40biloxi.com
	   sip:biloxi.com;method=REGISTER;transport=tcp?to=sip:bob%40biloxi.com
*/
	a = belle_sip_uri_parse("sip:biloxi.com;transport=tcp;method=REGISTER?to=sip:bob%40biloxi.com");
300
	BC_ASSERT_PTR_NOT_NULL_FATAL(a);
jehan's avatar
jehan committed
301
	b = belle_sip_uri_parse("sip:biloxi.com;method=REGISTER;transport=tcp?to=sip:bob%40biloxi.com");
302 303
	BC_ASSERT_PTR_NOT_NULL_FATAL(b);
	BC_ASSERT_TRUE(belle_sip_uri_equals(a,b));
jehan's avatar
jehan committed
304 305 306 307 308 309 310 311 312 313 314 315
	belle_sip_object_unref(a);
	belle_sip_object_unref(b);
	/*
	sip:alice@atlanta.com?subject=project%20x&priority=urgent
	   sip:alice@atlanta.com?priority=urgent&subject=project%20x

	   The URIs within each of the following sets are not equivalent:

	   SIP:ALICE@AtLanTa.CoM;Transport=udp             (different usernames)
	   sip:alice@AtLanTa.CoM;Transport=UDP
*/
	a = belle_sip_uri_parse("sip:ALICE@AtLanTa.CoM;Transport=udp");
316
	BC_ASSERT_PTR_NOT_NULL_FATAL(a);
jehan's avatar
jehan committed
317
	b = belle_sip_uri_parse("sip:alice@AtLanTa.CoM;Transport=UDP");
318 319
	BC_ASSERT_PTR_NOT_NULL_FATAL(b);
	BC_ASSERT_FALSE(belle_sip_uri_equals(a,b));
jehan's avatar
jehan committed
320 321 322 323 324 325 326
	belle_sip_object_unref(a);
	belle_sip_object_unref(b);
	/*
	   sip:bob@biloxi.com                   (can resolve to different ports)
	   sip:bob@biloxi.com:5060
*/
	a = belle_sip_uri_parse("sip:ALICE@AtLanTa.CoM;Transport=udp");
327
	BC_ASSERT_PTR_NOT_NULL_FATAL(a);
jehan's avatar
jehan committed
328
	b = belle_sip_uri_parse("sip:alice@AtLanTa.CoM;Transport=UDP");
329 330
	BC_ASSERT_PTR_NOT_NULL_FATAL(b);
	BC_ASSERT_FALSE(belle_sip_uri_equals(a,b));
jehan's avatar
jehan committed
331 332 333 334 335 336 337
	belle_sip_object_unref(a);
	belle_sip_object_unref(b);
	/*
	sip:bob@biloxi.com              (can resolve to different transports)
	   sip:bob@biloxi.com;transport=udp
*/
	a = belle_sip_uri_parse("sip:bob@biloxi.com");
338
	BC_ASSERT_PTR_NOT_NULL_FATAL(a);
jehan's avatar
jehan committed
339
	b = belle_sip_uri_parse("sip:bob@biloxi.com;transport=udp");
340 341
	BC_ASSERT_PTR_NOT_NULL_FATAL(b);
	BC_ASSERT_FALSE(belle_sip_uri_equals(a,b));
jehan's avatar
jehan committed
342 343 344 345 346 347
	belle_sip_object_unref(a);
	belle_sip_object_unref(b);
/*	   sip:bob@biloxi.com     (can resolve to different port and transports)
	   sip:bob@biloxi.com:6000;transport=tcp
*/
	a = belle_sip_uri_parse("sip:bob@biloxi.com");
348
	BC_ASSERT_PTR_NOT_NULL_FATAL(a);
jehan's avatar
jehan committed
349
	b = belle_sip_uri_parse("sip:bob@biloxi.com:6000;transport=tcp");
350 351
	BC_ASSERT_PTR_NOT_NULL_FATAL(b);
	BC_ASSERT_FALSE(belle_sip_uri_equals(a,b));
jehan's avatar
jehan committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
	belle_sip_object_unref(a);
	belle_sip_object_unref(b);

	/*	   sip:carol@chicago.com                    (different header component)
	   sip:carol@chicago.com?Subject=next%20meeting

	   sip:bob@phone21.boxesbybob.com   (even though that's what
	   sip:bob@192.0.2.4                 phone21.boxesbybob.com resolves to)

	   Note that equality is not transitive:

	      o  sip:carol@chicago.com and sip:carol@chicago.com;security=on are
	         equivalent

	      o  sip:carol@chicago.com and sip:carol@chicago.com;security=off
	         are equivalent

	      o  sip:carol@chicago.com;security=on and
	         sip:carol@chicago.com;security=off are not equivalent
	Rosenberg, et. al.          Standards Track                   [Page 155]

	RFC 3261            SIP: Session Initiation Protocol           June 2002

	 */


}
Ghislain MARY's avatar
Ghislain MARY committed
379

jehan's avatar
jehan committed
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
/*
 * From 19.1.1 SIP and SIPS URI Components
 * 									   				dialog
										reg./redir. Contact/
			default  Req.-URI  To  From  Contact   R-R/Route  external
user          --          o      o    o       o          o         o
password      --          o      o    o       o          o         o
host          --          m      m    m       m          m         m
port          (1)         o      -    -       o          o         o
user-param    ip          o      o    o       o          o         o
method        INVITE      -      -    -       -          -         o
maddr-param   --          o      -    -       o          o         o
ttl-param     1           o      -    -       o          -         o
transp.-param (2)         o      -    -       o          o         o
lr-param      --          o      -    -       -          o         o
other-param   --          o      o    o       o          o         o
headers       --          -      -    -       o          -         o*/
void testUriComponentsChecker() {
	belle_sip_uri_t* uri = belle_sip_uri_parse("sip:hostonly");
399
	BC_ASSERT_TRUE(belle_sip_uri_check_components_from_request_uri(uri));
jehan's avatar
jehan committed
400 401 402 403
	belle_sip_object_unref(uri);

	{
	belle_sip_header_from_t*	header = belle_sip_header_from_parse("From: sip:linphone.org:5061");
404
	BC_ASSERT_FALSE(belle_sip_uri_check_components_from_context(belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(header)),NULL,"From"));
jehan's avatar
jehan committed
405 406 407 408
	belle_sip_object_unref(header);
	}
	{
	belle_sip_header_to_t*	header = belle_sip_header_to_parse("To: sip:linphone.org?header=interdit");
409
	BC_ASSERT_FALSE(belle_sip_uri_check_components_from_context(belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(header)),NULL,"To"));
jehan's avatar
jehan committed
410 411 412 413
	belle_sip_object_unref(header);
	}
	{
	belle_sip_header_contact_t*	header = belle_sip_header_contact_parse("Contact: <sip:linphone.org;lr>");
414 415
	BC_ASSERT_FALSE(belle_sip_uri_check_components_from_context(belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(header)),"REGISTER","Contact"));
	BC_ASSERT_TRUE(belle_sip_uri_check_components_from_context(belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(header)),NULL,"Contact"));
jehan's avatar
jehan committed
416 417 418 419
	belle_sip_object_unref(header);
	}
	{
	belle_sip_header_record_route_t*	header = belle_sip_header_record_route_parse("Record-Route: <sip:linphone.org;ttl=interdit>");
420
	BC_ASSERT_FALSE(belle_sip_uri_check_components_from_context(belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(header)),NULL,"Record-Route"));
jehan's avatar
jehan committed
421 422 423 424
	belle_sip_object_unref(header);
	}
	{
	belle_sip_uri_t* uri = belle_sip_uri_parse("sip:linphone.org:5061?header=toto");
425
	BC_ASSERT_TRUE(belle_sip_uri_check_components_from_context(uri,NULL,"Any"));
jehan's avatar
jehan committed
426 427 428
	belle_sip_object_unref(uri);
	}
}
Ghislain MARY's avatar
Ghislain MARY committed
429

430
void test_escaping_bad_chars(void){
431
	char bad_uri[13] = { 'h', 'e', 'l', 'l', 'o', (char)0xa0, (char)0xc8, 'w', 'o', 'r', 'l', 'd', 0x0 };
432
	char *escaped = belle_sip_uri_to_escaped_username(bad_uri);
433
	const char *expected="hello%a0%c8world";
434

435
	BC_ASSERT_STRING_EQUAL(escaped, expected);
436 437 438 439

	belle_sip_free(escaped);
}

440 441 442 443 444 445 446

static belle_sip_header_address_t* test_header_address_parsing(const char* address, int expect_fail){
	belle_sip_header_address_t* header_address = belle_sip_header_address_parse(address);
	if( expect_fail == TRUE ){
		BC_ASSERT_PTR_NULL(header_address);
	} else {
		BC_ASSERT_PTR_NOT_NULL(header_address);
447
	}
448 449 450 451 452 453 454
	return header_address;
}

static void test_empty_password(void){

	static const char *address_fail = "sip:France:@+123456789";
	static const char *address_valid = "sip:France:@toto";
455
	const char* passwd;
456 457 458 459 460 461 462 463 464 465 466 467
	belle_sip_header_address_t* headerAddr;
	belle_sip_uri_t* uri;

	(void)test_header_address_parsing(address_fail, TRUE);

	headerAddr = test_header_address_parsing(address_valid, FALSE);

	BC_ASSERT_PTR_NOT_NULL(headerAddr);

	uri = belle_sip_header_address_get_uri(headerAddr);
	BC_ASSERT_PTR_NOT_NULL(uri);

468
	passwd = belle_sip_uri_get_user_password(uri);
469
	BC_ASSERT_PTR_EQUAL(passwd, NULL);
470 471
}

472

473
static test_t uri_tests[] = {
Ghislain MARY's avatar
Ghislain MARY committed
474 475
	{ "Simple URI", testSIMPLEURI },
	{ "Complex URI", testCOMPLEXURI },
476
	{ "Escaped username", test_escaped_username },
477
	{ "Escaped username with bad chars", test_escaping_bad_chars },
478
	{ "Escaped parameter", test_escaped_parameter },
jehan's avatar
jehan committed
479 480
	{ "Escaped passwd", test_escaped_passwd},
	{ "User passwd", test_user_passwd},
Ghislain MARY's avatar
Ghislain MARY committed
481 482 483 484
	{ "IP host", test_ip_host },
	{ "lr", test_lr },
	{ "maddr", test_maddr },
	{ "headers", test_headers },
485
	{ "Escaped headers", test_escaped_headers},
Ghislain MARY's avatar
Ghislain MARY committed
486 487 488 489
	{ "URI parameters", test_uri_parameters },
	{ "SIPS URI", testSIPSURI },
	{ "URI equals", test_uri_equals },
	{ "Simple URI error", testSIMPLEURI_error },
jehan's avatar
jehan committed
490
	{ "IPv6 URI", testIPV6URI },
491
	{ "URI components", testUriComponentsChecker },
492
	{ "Empty password", test_empty_password },
Ghislain MARY's avatar
Ghislain MARY committed
493 494
};

495 496
test_suite_t sip_uri_test_suite = {
	"SIP URI",
Ghislain MARY's avatar
Ghislain MARY committed
497 498 499 500 501 502
	NULL,
	NULL,
	sizeof(uri_tests) / sizeof(uri_tests[0]),
	uri_tests
};