belle_sip_message_tester.c 26.2 KB
Newer Older
jehan's avatar
jehan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
	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
    the Free Software Foundation, either version 3 of the License, or
    (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/>.
*/

#include "belle-sip/belle-sip.h"
Ghislain MARY's avatar
Ghislain MARY committed
20
#include "belle_sip_tester.h"
jehan's avatar
jehan committed
21 22 23 24
#include <stdio.h>
#include "CUnit/Basic.h"


25
static void check_uri_and_headers(belle_sip_message_t* message) {
jehan's avatar
jehan committed
26 27
	if (belle_sip_message_is_request(message)) {
		CU_ASSERT_PTR_NOT_NULL(belle_sip_request_get_uri(BELLE_SIP_REQUEST(message)));
jehan's avatar
jehan committed
28 29

		CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Max-Forwards"));
30
		CU_ASSERT_PTR_NOT_NULL(BELLE_SIP_HEADER_MAX_FORWARDS(belle_sip_message_get_header(message,"Max-Forwards")));
31 32 33

		CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"User-Agent"));
		CU_ASSERT_PTR_NOT_NULL(BELLE_SIP_HEADER_USER_AGENT(belle_sip_message_get_header(message,"User-Agent")));
jehan's avatar
jehan committed
34
	}
35
	CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"From"));
36
	CU_ASSERT_PTR_NOT_NULL(BELLE_SIP_HEADER_FROM(belle_sip_message_get_header(message,"From")));
37 38

	CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"To"));
39
	CU_ASSERT_PTR_NOT_NULL(BELLE_SIP_HEADER_TO(belle_sip_message_get_header(message,"To")));
40 41

	CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"CSeq"));
42
	CU_ASSERT_PTR_NOT_NULL(BELLE_SIP_HEADER_CSEQ(belle_sip_message_get_header(message,"CSeq")));
43 44

	CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Via"));
45
	CU_ASSERT_PTR_NOT_NULL(BELLE_SIP_HEADER_VIA(belle_sip_message_get_header(message,"Via")));
46 47

	CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Call-ID"));
48
	CU_ASSERT_PTR_NOT_NULL(BELLE_SIP_HEADER_CALL_ID(belle_sip_message_get_header(message,"Call-ID")));
49 50 51


	CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Content-Length"));
52
	CU_ASSERT_PTR_NOT_NULL(BELLE_SIP_HEADER_CONTENT_LENGTH(belle_sip_message_get_header(message,"Content-Length")));
53

jehan's avatar
jehan committed
54

jehan's avatar
jehan committed
55 56


57

58 59 60
}

static void testRegisterMessage(void) {
61
	const char* raw_message = "REGISTER sip:192.168.0.20 SIP/2.0\r\n"\
62 63 64 65
							"v: SIP/2.0/UDP 192.168.1.8:5062;rport;branch=z9hG4bK1439638806\r\n"\
							"f: <sip:jehan-mac@sip.linphone.org>;tag=465687829\r\n"\
							"t: <sip:jehan-mac@sip.linphone.org>\r\n"\
							"i: 1053183492\r\n"\
66
							"CSeq: 1 REGISTER\r\n"\
67
							"m: <sip:jehan-mac@192.168.1.8:5062>\r\n"\
68 69 70
							"Max-Forwards: 70\r\n"\
							"User-Agent: Linphone/3.3.99.10 (eXosip2/3.3.0)\r\n"\
							"Expires: 3600\r\n"\
jehan's avatar
jehan committed
71 72
							"Proxy-Authorization: Digest username=\"8117396\", realm=\"Realm\", nonce=\"MTMwNDAwMjIxMjA4NzVkODY4ZmZhODMzMzU4ZDJkOTA1NzM2NTQ2NDZlNmIz"\
							", uri=\"sip:linphone.net\", response=\"eed376ff7c963441255ec66594e470e7\", algorithm=MD5, cnonce=\"0a4f113b\", qop=auth, nc=00000001\r\n"\
73
							"l: 0\r\n\r\n";
74
	belle_sip_request_t* request;
jehan's avatar
jehan committed
75
	belle_sip_message_t* message = belle_sip_message_parse(raw_message);
jehan's avatar
jehan committed
76 77 78 79
	char* encoded_message = belle_sip_object_to_string(BELLE_SIP_OBJECT(message));
	belle_sip_object_unref(BELLE_SIP_OBJECT(message));
	message = belle_sip_message_parse(encoded_message);

80
	request = BELLE_SIP_REQUEST(message);
jehan's avatar
jehan committed
81
	CU_ASSERT_STRING_EQUAL(belle_sip_request_get_method(request),"REGISTER");
82
	CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Expires"));
83
	CU_ASSERT_PTR_NOT_NULL(BELLE_SIP_HEADER_EXPIRES(belle_sip_message_get_header(message,"Expires")));
jehan's avatar
jehan committed
84 85 86
	CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Proxy-Authorization"));
	CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Contact"));

87
	check_uri_and_headers(message);
jehan's avatar
jehan committed
88 89
	belle_sip_free(encoded_message);
	belle_sip_object_unref(message);
jehan's avatar
jehan committed
90

91
}
Ghislain MARY's avatar
Ghislain MARY committed
92

93 94 95 96 97 98 99 100 101 102
static void testInviteMessage(void) {
	const char* raw_message = "INVITE sip:becheong@sip.linphone.org SIP/2.0\r\n"\
							"Via: SIP/2.0/UDP 10.23.17.117:22600;branch=z9hG4bK-d8754z-4d7620d2feccbfac-1---d8754z-;rport=4820;received=202.165.193.129\r\n"\
							"Max-Forwards: 70\r\n"\
							"Contact: <sip:bcheong@202.165.193.129:4820>\r\n"\
							"To: \"becheong\" <sip:becheong@sip.linphone.org>\r\n"\
							"From: \"Benjamin Cheong\" <sip:bcheong@sip.linphone.org>;tag=7326e5f6\r\n"\
							"Call-ID: Y2NlNzg0ODc0ZGIxODU1MWI5MzhkNDVkNDZhOTQ4YWU.\r\n"\
							"CSeq: 1 INVITE\r\n"\
							"Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO\r\n"\
103
							"c: application/sdp\r\n"\
104
							"Supported: replaces\r\n"\
jehan's avatar
jehan committed
105
							"Authorization: Digest username=\"003332176\", realm=\"sip.ovh.net\", nonce=\"24212965507cde726e8bc37e04686459\", uri=\"sip:sip.ovh.net\", response=\"896e786e9c0525ca3085322c7f1bce7b\", algorithm=MD5, opaque=\"241b9fb347752f2\"\r\n"\
106 107
							"User-Agent: X-Lite 4 release 4.0 stamp 58832\r\n"\
							"Content-Length: 230\r\n\r\n";
108
	belle_sip_request_t* request;
109
	belle_sip_message_t* message = belle_sip_message_parse(raw_message);
jehan's avatar
jehan committed
110 111 112
	char* encoded_message = belle_sip_object_to_string(BELLE_SIP_OBJECT(message));
	belle_sip_object_unref(BELLE_SIP_OBJECT(message));
	message = belle_sip_message_parse(encoded_message);
113
	request = BELLE_SIP_REQUEST(message);
114
	CU_ASSERT_STRING_EQUAL(belle_sip_request_get_method(request),"INVITE");
jehan's avatar
jehan committed
115 116
	CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Contact"));
	CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Authorization"));
117
	CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Content-Type"));
jehan's avatar
jehan committed
118
	check_uri_and_headers(message);
jehan's avatar
jehan committed
119 120
	belle_sip_object_unref(message);
	belle_sip_free(encoded_message);
jehan's avatar
jehan committed
121
}
Ghislain MARY's avatar
Ghislain MARY committed
122

jehan's avatar
jehan committed
123 124 125 126 127 128 129 130 131 132
static void test401Response(void) {
	const char* raw_message = 	"SIP/2.0 401 Unauthorized\r\n"
								"Call-ID: 577586163\r\n"
								"CSeq: 21 REGISTER\r\n"
								"From: <sip:0033532176@sip.ovh.net>;tag=1790643209\r\n"
								"Server: Cirpack/v4.42x (gw_sip)\r\n"
								"To: <sip:0033482176@sip.ovh.net>;tag=00-08075-24212984-22e348d97\r\n"
								"Via: SIP/2.0/UDP 192.168.0.18:5062;received=81.56.113.2;rport=5062;branch=z9hG4bK1939354046\r\n"
								"WWW-Authenticate: Digest realm=\"sip.ovh.net\",nonce=\"24212965507cde726e8bc37e04686459\",opaque=\"241b9fb347752f2\",stale=false,algorithm=MD5\r\n"
								"Content-Length: 0\r\n\r\n";
133
	belle_sip_response_t* response;
jehan's avatar
jehan committed
134
	belle_sip_message_t* message = belle_sip_message_parse(raw_message);
jehan's avatar
jehan committed
135 136 137
	char* encoded_message = belle_sip_object_to_string(BELLE_SIP_OBJECT(message));
	belle_sip_object_unref(BELLE_SIP_OBJECT(message));
	message = belle_sip_message_parse(encoded_message);
138
	response = BELLE_SIP_RESPONSE(message);
jehan's avatar
jehan committed
139 140 141
	CU_ASSERT_EQUAL(belle_sip_response_get_status_code(response),401);
	CU_ASSERT_STRING_EQUAL(belle_sip_response_get_reason_phrase(response),"Unauthorized");
	CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"WWW-Authenticate"));
142
	check_uri_and_headers(message);
jehan's avatar
jehan committed
143 144
	belle_sip_object_unref(message);
	belle_sip_free(encoded_message);
145
}
Ghislain MARY's avatar
Ghislain MARY committed
146

147
static void testRegisterRaw(void) {
148 149 150 151 152 153 154 155 156 157
	const char* raw_message = "REGISTER sip:192.168.0.20 SIP/2.0\r\n"\
							"Via: SIP/2.0/UDP 192.168.1.8:5062;rport;branch=z9hG4bK1439638806\r\n"\
							"From: <sip:jehan-mac@sip.linphone.org>;tag=465687829\r\n"\
							"To: <sip:jehan-mac@sip.linphone.org>\r\n"\
							"Call-ID: 1053183492\r\n"\
							"CSeq: 1 REGISTER\r\n"\
							"Contact: <sip:jehan-mac@192.168.1.8:5062>\r\n"\
							"Max-Forwards: 70\r\n"\
							"User-Agent: Linphone/3.3.99.10 (eXosip2/3.3.0)\r\n"\
							"Expires: 3600\r\n"\
158
							"Content-Length: 0\r\n\r\n123456789";
159
	belle_sip_request_t* request;
160 161 162
	size_t size=0;
	size_t raw_message_size= strlen(raw_message);
	belle_sip_message_t* message = belle_sip_message_parse_raw(raw_message,raw_message_size,&size);
163
	CU_ASSERT_EQUAL(raw_message_size,size+9);
164
	request = BELLE_SIP_REQUEST(message);
165 166
	CU_ASSERT_STRING_EQUAL(belle_sip_request_get_method(request),"REGISTER");
	CU_ASSERT_PTR_NOT_NULL(belle_sip_request_get_uri(request));
jehan's avatar
jehan committed
167 168
	CU_ASSERT_STRING_EQUAL(&raw_message[size],"123456789");
	belle_sip_object_unref(message);
169
}
170

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
static void testOptionMessage(void) {
	const char* raw_message = "REGISTER sip:192.168.0.20 SIP/2.0\r\n"\
							"Via: SIP/2.0/UDP 192.168.1.8:5062;rport;branch=z9hG4bK1439638806\r\n"\
							"From: <sip:jehan-mac@sip.linphone.org>;tag=465687829\r\n"\
							"To: <sip:jehan-mac@sip.linphone.org>\r\n"\
							"Call-ID: 1053183492\r\n"\
							"CSeq: 1 REGISTER\r\n"\
							"Contact: <sip:jehan-mac@192.168.1.8:5062>\r\n"\
							"Max-Forwards: 70\r\n"\
							"User-Agent: Linphone/3.3.99.10 (eXosip2/3.3.0)\r\n"\
							"Expires: 3600\r\n"\
							"Content-Length: 0\r\n\r\n";
	belle_sip_message_t* message = belle_sip_message_parse(raw_message);
	belle_sip_request_t* request = BELLE_SIP_REQUEST(message);
	CU_ASSERT_STRING_EQUAL(belle_sip_request_get_method(request),"REGISTER");
	CU_ASSERT_PTR_NOT_NULL(belle_sip_request_get_uri(request));
jehan's avatar
jehan committed
187
	belle_sip_object_unref(message);
188
}
jehan's avatar
jehan committed
189

190
static void test_extract_source(void) {
jehan's avatar
jehan committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204
	const char * invite_1="INVITE sip:jehan@81.56.113.2:50343;transport=tcp;line=f18e0009dd6cc43 SIP/2.0\r\n"
						"Via: SIP/2.0/TCP 37.59.129.73;branch=z9hG4bK.SKvK9U327e8mU68XUv5rt144pg\r\n"
						"Via: SIP/2.0/UDP 192.168.1.12:15060;rport=15060;branch=z9hG4bK1596944937;received=81.56.113.2\r\n"
						"Record-Route: <sip:37.59.129.73;lr;transport=tcp>\r\n"
						"Record-Route: <sip:37.59.129.73;lr>\r\n"
						"Max-Forwards: 70\r\n"
						"From: <sip:jehan@sip.linphone.org>;tag=711138653\r\n"
						"To: <sip:jehan@sip.linphone.org>\r\n"
						"Call-ID: 977107319\r\n"
						"CSeq: 21 INVITE\r\n"
						"Contact: <sip:jehan@81.56.113.2:15060>\r\n"
						"Subject: Phone call\r\n"
						"User-Agent: Linphone/3.5.2 (eXosip2/3.6.0)\r\n"
						"Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO\r\n"
205 206
						"Content-Type: application/sdp\r\n"\
						"Content-Length: 230\r\n\r\n";
jehan's avatar
jehan committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

	const char * invite_2="INVITE sip:jehan@81.56.113.2:50343;transport=tcp;line=f18e0009dd6cc43 SIP/2.0\r\n"
						"Via: SIP/2.0/UDP 192.168.1.12:15060;rport=15060;branch=z9hG4bK1596944937;received=81.56.113.2\r\n"
						"Via: SIP/2.0/TCP 37.59.129.73;branch=z9hG4bK.SKvK9U327e8mU68XUv5rt144pg\r\n"
						"Record-Route: <sip:37.59.129.73;lr;transport=tcp>\r\n"
						"Record-Route: <sip:37.59.129.73;lr>\r\n"
						"Max-Forwards: 70\r\n"
						"From: <sip:jehan@sip.linphone.org>;tag=711138653\r\n"
						"To: <sip:jehan@sip.linphone.org>\r\n"
						"Call-ID: 977107319\r\n"
						"CSeq: 21 INVITE\r\n"
						"Contact: <sip:jehan@81.56.113.2:15060>\r\n"
						"Subject: Phone call\r\n"
						"User-Agent: Linphone/3.5.2 (eXosip2/3.6.0)\r\n"
						"Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO\r\n"
						"Content-Length: 0\r\n\r\n";

	belle_sip_message_t* message = belle_sip_message_parse(invite_1);
	belle_sip_request_t* request = BELLE_SIP_REQUEST(message);
	belle_sip_uri_t* source =belle_sip_request_extract_origin(request);
227
	CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Content-type"));
jehan's avatar
jehan committed
228 229 230
	CU_ASSERT_PTR_NOT_NULL(source);
	CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(source),"37.59.129.73");
	CU_ASSERT_EQUAL(belle_sip_uri_get_port(source),0);
231
	CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_transport_param(source),"tcp");
jehan's avatar
jehan committed
232 233 234 235 236 237 238 239
	belle_sip_object_unref(message);

	message = belle_sip_message_parse(invite_2);
	request = BELLE_SIP_REQUEST(message);
	source =belle_sip_request_extract_origin(request);
	CU_ASSERT_PTR_NOT_NULL(source);
	CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(source),"81.56.113.2");
	CU_ASSERT_EQUAL(belle_sip_uri_get_port(source),15060);
240
	CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_transport_param(source),"udp");
jehan's avatar
jehan committed
241
	belle_sip_object_unref(message);
jehan's avatar
jehan committed
242

jehan's avatar
jehan committed
243
}
Ghislain MARY's avatar
Ghislain MARY committed
244

245 246 247 248 249 250 251 252 253 254
static void test_sipfrag(void) {
	const char* raw_message = 	"SIP/2.0 100 Trying\r\n";
	belle_sip_response_t* response;
	belle_sip_message_t* message = belle_sip_message_parse(raw_message);
	response = BELLE_SIP_RESPONSE(message);
	CU_ASSERT_EQUAL(belle_sip_response_get_status_code(response),100);
	CU_ASSERT_STRING_EQUAL(belle_sip_response_get_reason_phrase(response),"Trying");
	belle_sip_object_unref(message);
}

jehan's avatar
jehan committed
255 256 257
/*static void test_fix_contact_with_received_rport() {

}*/
Ghislain MARY's avatar
Ghislain MARY committed
258

259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
static void testMalformedMessage(void) {
const char * raw_message=	"INVITE sip:jehan@81.56.113.2:50343;transport=tcp;line=f18e0009dd6cc43 SIP/2.0\r\n"
							"Via: SIP/2.0/UDP 192.168.1.12:15060;rport=15060;branch=z9hG4bK1596944937;received=81.56.113.2\r\n"
							"Via: SIP/2.0/TCP 37.59.129.73;branch=z9hG4bK.SKvK9U327e8mU68XUv5rt144pg\r\n"
							"Record-Route: <sip:37.59.129.73;lr;transport=tcp>\r\n"
							"Record-Route: <sip:37.59.129.73;lr>\r\n"
							"Max-Forwards: 70\r\n"
							"From: <sip:jehan@sip.linphone.org>;tag=711138653\r\n"
							"To: <sip:jehan@sip.linphone.org>\r\n"
							"Call-ID: 977107319\r\n"
							"CSeq: 21 INVITE\r\n"
							"Contact: <sip:jehan-mac@192.168.1.8:5062>;pn-tok=/throttledthirdparty\r\n" /*Slash is not allowed for contact params*/\
							"Subject: Phone call\r\n"
							"User-Agent: Linphone/3.5.2 (eXosip2/3.6.0)\r\n"
							"Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO\r\n"
							"Content-Length: 0\r\n\r\n";

	belle_sip_message_t* message = belle_sip_message_parse(raw_message);
277
	CU_ASSERT_FALSE(belle_sip_message_check_headers(message));
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
}

static void testMalformedOptionnalHeaderInMessage(void) {
const char* raw_message = 	"REGISTER sip:192.168.0.20 SIP/2.0\r\n"\
							"Via: SIP/2.0/UDP 192.168.1.8:5062;rport;branch=z9hG4bK1439638806\r\n"\
							"From: <sip:jehan-mac@sip.linphone.org>;tag=465687829\r\n"\
							"To: <sip:jehan-mac@sip.linphone.org>\r\n"\
							"Call-ID: 1053183492\r\n"\
							"CSeq: 1 REGISTER\r\n"\
							"Contact: <sip:jehan-mac@192.168.1.8:5062>;pn-tok=/throttledthirdparty\r\n" /*Slash is not allowed for contact params*/\
							"Max-Forwards: 70\r\n"\
							"User-Agent: Linphone/3.3.99.10 (eXosip2/3.3.0)\r\n"\
							"Expires: 3600\r\n"\
							"Proxy-Authorization: Digest username=\"8117396\", realm=\"Realm\", nonce=\"MTMwNDAwMjIxMjA4NzVkODY4ZmZhODMzMzU4ZDJkOTA1NzM2NTQ2NDZlNmIz"\
							", uri=\"sip:linphone.net\", response=\"eed376ff7c963441255ec66594e470e7\", algorithm=MD5, cnonce=\"0a4f113b\", qop=auth, nc=00000001\r\n"\
							"Content-Length: 0\r\n\r\n";

	belle_sip_request_t* request;
	belle_sip_message_t* message = belle_sip_message_parse(raw_message);
	char* encoded_message = belle_sip_object_to_string(BELLE_SIP_OBJECT(message));
	belle_sip_object_unref(BELLE_SIP_OBJECT(message));
	message = belle_sip_message_parse(encoded_message);

	request = BELLE_SIP_REQUEST(message);
	CU_ASSERT_STRING_EQUAL(belle_sip_request_get_method(request),"REGISTER");
	CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Expires"));
	CU_ASSERT_PTR_NOT_NULL(BELLE_SIP_HEADER_EXPIRES(belle_sip_message_get_header(message,"Expires")));
	CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Proxy-Authorization"));
	CU_ASSERT_PTR_NULL(belle_sip_message_get_header(message,"Contact")); /*contact is optionnal in register*/

	check_uri_and_headers(message);
	belle_sip_free(encoded_message);
	belle_sip_object_unref(message);
}

jehan's avatar
jehan committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
static void testMalformedMessageWithWrongStart(void) {
	const char * raw_message=	"\r\n"
			"INVITE sip:jehan@81.56.113.2:50343;transport=tcp;line=f18e0009dd6cc43 SIP/2.0\r\n"
			"Via: SIP/2.0/UDP 192.168.1.12:15060;rport=15060;branch=z9hG4bK1596944937;received=81.56.113.2\r\n"
			"Via: SIP/2.0/TCP 37.59.129.73;branch=z9hG4bK.SKvK9U327e8mU68XUv5rt144pg\r\n"
			"Record-Route: <sip:37.59.129.73;lr;transport=tcp>\r\n"
			"Record-Route: <sip:37.59.129.73;lr>\r\n"
			"Max-Forwards: 70\r\n"
			"From: <sip:jehan@sip.linphone.org>;tag=711138653\r\n"
			"To: <sip:jehan@sip.linphone.org>\r\n"
			"Call-ID: 977107319\r\n"
			"CSeq: 21 INVITE\r\n"
			"Contact: <sip:jehan-mac@192.168.1.8:5062>\r\n"
			"Subject: Phone call\r\n"
			"User-Agent: Linphone/3.5.2 (eXosip2/3.6.0)\r\n"
			"Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO\r\n"
			"Content-Length: 0\r\n\r\n";

	belle_sip_message_t* message = belle_sip_message_parse(raw_message);
	CU_ASSERT_PTR_NULL(message);
}
jehan's avatar
jehan committed
334 335 336 337 338 339 340 341 342 343 344
#include "belle_sip_internal.h"

void channel_parser_tester_recovery_from_error () {
	belle_sip_stack_t* stack = belle_sip_stack_new(NULL);
	belle_sip_channel_t* channel = belle_sip_stream_channel_new_client(stack
																	, NULL
																	, 45421
																	, NULL
																	, "127.0.0.1"
																	, 45421);

345 346
	const char * raw_message=	"debut de stream tout pourri\r\n"
			"INVITE je_suis_une_fausse_request_uri_hihihi SIP/2.0\r\n"
jehan's avatar
jehan committed
347 348 349 350 351 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 379 380 381 382 383 384 385
			"Via: SIP/2.0/UDP 192.168.1.12:15060;rport=15060;branch=z9hG4bK1596944937;received=81.56.113.2\r\n"
			"Via: SIP/2.0/TCP 37.59.129.73;branch=z9hG4bK.SKvK9U327e8mU68XUv5rt144pg\r\n"
			"Record-Route: <sip:37.59.129.73;lr;transport=tcp>\r\n"
			"Record-Route: <sip:37.59.129.73;lr>\r\n"
			"Max-Forwards: 70\r\n"
			"From: <sip:jehan@sip.linphone.org>;tag=711138653\r\n"
			"To: <sip:jehan@sip.linphone.org>\r\n"
			"Call-ID: 977107319\r\n"
			"CSeq: 21 INVITE\r\n"
			"Contact: <sip:jehan-mac@192.168.1.8:5062>\r\n"
			"Subject: Phone call\r\n"
			"User-Agent: Linphone/3.5.2 (eXosip2/3.6.0)\r\n"
			"Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO\r\n"
			"Content-Length: 0\r\n"
			"\r\n"
			"REGISTER sip:192.168.0.20 SIP/2.0\r\n"\
			"Via: SIP/2.0/UDP 192.168.1.8:5062;rport;branch=z9hG4bK1439638806\r\n"\
			"From: <sip:jehan-mac@sip.linphone.org>;tag=465687829\r\n"\
			"To: <sip:jehan-mac@sip.linphone.org>\r\n"\
			"Call-ID: 1053183492\r\n"\
			"CSeq: 1 REGISTER\r\n"\
			"Contact: <sip:jehan-mac@192.168.1.8:5062>\r\n" \
			"Max-Forwards: 70\r\n"\
			"User-Agent: Linphone/3.3.99.10 (eXosip2/3.3.0)\r\n"\
			"Expires: 3600\r\n"\
			"Proxy-Authorization: Digest username=\"8117396\", realm=\"Realm\", nonce=\"MTMwNDAwMjIxMjA4NzVkODY4ZmZhODMzMzU4ZDJkOTA1NzM2NTQ2NDZlNmIz"\
			", uri=\"sip:linphone.net\", response=\"eed376ff7c963441255ec66594e470e7\", algorithm=MD5, cnonce=\"0a4f113b\", qop=auth, nc=00000001\r\n"\
			"Content-Length: 0\r\n"
			"\r\n";
	belle_sip_request_t* request;
	belle_sip_message_t* message;
	channel->input_stream.write_ptr = strcpy(channel->input_stream.write_ptr,raw_message);
	channel->input_stream.write_ptr+=strlen(raw_message);

	belle_sip_channel_parse_stream(channel);

	CU_ASSERT_PTR_NOT_NULL(channel->incoming_messages);
	CU_ASSERT_PTR_NOT_NULL(channel->incoming_messages->data);
	message=BELLE_SIP_MESSAGE(channel->incoming_messages->data);
386
	CU_ASSERT_TRUE(BELLE_SIP_OBJECT_IS_INSTANCE_OF(message,belle_sip_request_t));
jehan's avatar
jehan committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
	request = BELLE_SIP_REQUEST(message);
	CU_ASSERT_STRING_EQUAL(belle_sip_request_get_method(request),"REGISTER");
	CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Expires"));
	CU_ASSERT_PTR_NOT_NULL(BELLE_SIP_HEADER_EXPIRES(belle_sip_message_get_header(message,"Expires")));
	CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Proxy-Authorization"));

	check_uri_and_headers(message);

	belle_sip_object_unref(BELLE_SIP_OBJECT(message));
	belle_sip_object_unref(stack);

}

void channel_parser_malformed_start () {
	belle_sip_stack_t* stack = belle_sip_stack_new(NULL);
	belle_sip_channel_t* channel = belle_sip_stream_channel_new_client(stack
																	, NULL
																	, 45421
																	, NULL
																	, "127.0.0.1"
																	, 45421);

409
	const char * raw_message=	"debut de stream tout pourri\r\n"
jehan's avatar
jehan committed
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
			"REGISTER sip:192.168.0.20 SIP/2.0\r\n"
			"Via: SIP/2.0/UDP 192.168.1.8:5062;rport;branch=z9hG4bK1439638806\r\n"
			"From: <sip:jehan-mac@sip.linphone.org>;tag=465687829\r\n"
			"To: <sip:jehan-mac@sip.linphone.org>\r\n"
			"Call-ID: 1053183492\r\n"
			"CSeq: 1 REGISTER\r\n"
			"Contact: <sip:jehan-mac@192.168.1.8:5062>\r\n"
			"Max-Forwards: 70\r\n"
			"User-Agent: Linphone/3.3.99.10 (eXosip2/3.3.0)\r\n"
			"Expires: 3600\r\n"
			"Proxy-Authorization: Digest username=\"8117396\", realm=\"Realm\", nonce=\"MTMwNDAwMjIxMjA4NzVkODY4ZmZhODMzMzU4ZDJkOTA1NzM2NTQ2NDZlNmIz"
			", uri=\"sip:linphone.net\", response=\"eed376ff7c963441255ec66594e470e7\", algorithm=MD5, cnonce=\"0a4f113b\", qop=auth, nc=00000001\r\n"
			"Content-Length: 0\r\n"
			"\r\n";
	belle_sip_request_t* request;
	belle_sip_message_t* message;
	channel->input_stream.write_ptr = strcpy(channel->input_stream.write_ptr,raw_message);
	channel->input_stream.write_ptr+=strlen(raw_message);

	belle_sip_channel_parse_stream(channel);

	CU_ASSERT_PTR_NOT_NULL(channel->incoming_messages);
	CU_ASSERT_PTR_NOT_NULL(channel->incoming_messages->data);
	message=BELLE_SIP_MESSAGE(channel->incoming_messages->data);
434
	CU_ASSERT_TRUE(BELLE_SIP_OBJECT_IS_INSTANCE_OF(message,belle_sip_request_t));
jehan's avatar
jehan committed
435 436 437 438 439 440
	request = BELLE_SIP_REQUEST(message);
	CU_ASSERT_STRING_EQUAL(belle_sip_request_get_method(request),"REGISTER");
	CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Expires"));
	CU_ASSERT_PTR_NOT_NULL(BELLE_SIP_HEADER_EXPIRES(belle_sip_message_get_header(message,"Expires")));
	CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Proxy-Authorization"));

jehan's avatar
jehan committed
441

jehan's avatar
jehan committed
442 443 444 445
	check_uri_and_headers(message);

	belle_sip_object_unref(BELLE_SIP_OBJECT(message));
	belle_sip_object_unref(stack);
446 447
}

jehan's avatar
jehan committed
448

449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
static void testRFC2543Compat(void) {
	belle_sip_server_transaction_t *tr;
	const char* raw_message = 	"INVITE sip:me@127.0.0.1 SIP/2.0\r\n"
			"Via: SIP/2.0/UDP 192.168.1.12:15060;rport=15060;received=81.56.113.2\r\n"
			"Record-Route: <sip:37.59.129.73;lr;transport=tcp>\r\n"
			"Record-Route: <sip:37.59.129.73;lr>\r\n"
			"Max-Forwards: 70\r\n"
			"From: <sip:jehan@sip.linphone.org>;tag=711138653\r\n"
			"To: <sip:jehan@sip.linphone.org>\r\n"
			"Call-ID: 977107319\r\n"
			"CSeq: 21 INVITE\r\n"
			"Contact: <sip:jehan-mac@192.168.1.8:5062>\r\n"
			"Subject: Phone call\r\n"
			"User-Agent: Linphone/3.5.2 (eXosip2/3.6.0)\r\n"
			"Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO\r\n"
			"Content-Length: 0\r\n"
			"\r\n";

	belle_sip_request_t* request;
	belle_sip_stack_t *stack=belle_sip_stack_new(NULL);
	belle_sip_provider_t *prov=belle_sip_provider_new(stack,NULL);
	belle_sip_message_t* message = belle_sip_message_parse(raw_message);
	
	belle_sip_object_ref(message);
	belle_sip_object_ref(message); /*yes double ref: originally the message is created with 0 refcount, and dispatch_message will unref() it.*/
	belle_sip_provider_dispatch_message(prov,message);
	request = BELLE_SIP_REQUEST(message);
	
	CU_ASSERT_PTR_NOT_NULL(request);
	tr=belle_sip_provider_create_server_transaction(prov,request);
	CU_ASSERT_PTR_NOT_NULL(tr);
	belle_sip_object_unref(prov);
	belle_sip_object_unref(stack);
	belle_sip_object_unref(message);
jehan's avatar
jehan committed
483
}
Ghislain MARY's avatar
Ghislain MARY committed
484

jehan's avatar
jehan committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
static void testUriHeadersInInvite(void)  {
	belle_sip_request_t* request;
	belle_sip_stack_t *stack=belle_sip_stack_new(NULL);
	belle_sip_provider_t *prov=belle_sip_provider_new(stack,NULL);
	const char* raw_uri="sip:toto@titi.com"
						"?header1=blabla"
						"&header2=blue%3Bweftutu%3Dbla"
						"&From=toto"
						"&To=sip%3Atoto%40titi.com"
						"&Call-ID=asdads"
						"&CSeq=asdasd"
						"&Via=asdasd"
						"&Accept=adsad"
						"&Accept-Encoding=adsad"
						"&Accept-Language=adsad"
						"&Allow=adsad"
						"&Record-Route=adsad"
						"&Contact=adsad"
						"&Organization=adsad"
						"&Supported=adsad"
						"&User-Agent=adsad";

	belle_sip_header_t* raw_header;
	request=belle_sip_request_create(	belle_sip_uri_parse(raw_uri)
										,"INVITE"
										,belle_sip_provider_create_call_id(prov)
										,belle_sip_header_cseq_create(20,"INVITE")
										,belle_sip_header_from_create2("sip:toto@titi.com","4654")
										,NULL
										,belle_sip_header_via_new()
										,70);
	CU_ASSERT_PTR_NOT_NULL(request);
	CU_ASSERT_PTR_NOT_NULL(raw_header=belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"header1"));
	if (raw_header) {
		CU_ASSERT_STRING_EQUAL(belle_sip_header_extension_get_value(BELLE_SIP_HEADER_EXTENSION(raw_header)),"blabla");
	}
	CU_ASSERT_PTR_NOT_NULL(raw_header=belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"header2"));
	if (raw_header) {
		CU_ASSERT_STRING_EQUAL(belle_sip_header_extension_get_value(BELLE_SIP_HEADER_EXTENSION(raw_header)),"blue;weftutu=bla");
	}
	CU_ASSERT_PTR_NOT_NULL(raw_header=belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"To"));
	if (raw_header) {
		CU_ASSERT_STRING_EQUAL(belle_sip_header_extension_get_value(BELLE_SIP_HEADER_EXTENSION(raw_header)),"sip:toto@titi.com");
	}
	CU_ASSERT_STRING_NOT_EQUAL(belle_sip_header_get_unparsed_value(belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"From")),"toto");
	CU_ASSERT_PTR_NULL(belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"Record-Route"));
	CU_ASSERT_PTR_NULL(belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"Accept"));
	CU_ASSERT_PTR_NULL(belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"Accept-Encoding"));
	CU_ASSERT_PTR_NULL(belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"Accept-Language"));
	CU_ASSERT_PTR_NULL(belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"Allow"));
	CU_ASSERT_PTR_NULL(belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"Contact"));
	CU_ASSERT_PTR_NULL(belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"Organization"));
	CU_ASSERT_PTR_NULL(belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"Supported"));
	CU_ASSERT_PTR_NULL(belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"User-Agent"));


	belle_sip_object_unref(request);

}
Ghislain MARY's avatar
Ghislain MARY committed
544 545 546 547 548 549 550 551 552
/* NOTE - ORDER IS IMPORTANT - MUST TEST fread() AFTER fprintf() */
test_t message_tests[] = {
	{ "REGISTER", testRegisterMessage },
	{ "INVITE", testInviteMessage },
	{ "Option message", testOptionMessage },
	{ "REGISTER (Raw)", testRegisterRaw },
	{ "401 Response", test401Response },
	{ "Origin extraction", test_extract_source },
	{ "SIP frag", test_sipfrag },
553
	{ "Malformed invite", testMalformedMessage },
jehan's avatar
jehan committed
554
	{ "Malformed invite with bad begin", testMalformedMessageWithWrongStart },
555
	{ "Malformed register", testMalformedOptionnalHeaderInMessage },
jehan's avatar
jehan committed
556
	{ "Channel parser error recovery", channel_parser_tester_recovery_from_error},
557
	{ "Channel parser malformed start", channel_parser_malformed_start},
jehan's avatar
jehan committed
558 559
	{ "RFC2543 compatibility", testRFC2543Compat},
	{ "Uri headers in sip INVITE",testUriHeadersInInvite}
Ghislain MARY's avatar
Ghislain MARY committed
560 561 562 563 564 565 566 567 568 569
};

test_suite_t message_test_suite = {
	"Message",
	NULL,
	NULL,
	sizeof(message_tests) / sizeof(message_tests[0]),
	message_tests
};