belle_sip_message_tester.c 51.1 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
 * Copyright (c) 2012-2019 Belledonne Communications SARL.
 *
 * This file is part of belle-sip.
 *
 * 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/>.
 */
jehan's avatar
jehan committed
19 20

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


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

29 30
		BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Max-Forwards"));
		BC_ASSERT_PTR_NOT_NULL(BELLE_SIP_HEADER_MAX_FORWARDS(belle_sip_message_get_header(message,"Max-Forwards")));
31

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

38 39
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"To"));
	BC_ASSERT_PTR_NOT_NULL(BELLE_SIP_HEADER_TO(belle_sip_message_get_header(message,"To")));
40

41 42
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"CSeq"));
	BC_ASSERT_PTR_NOT_NULL(BELLE_SIP_HEADER_CSEQ(belle_sip_message_get_header(message,"CSeq")));
43

44 45
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Via"));
	BC_ASSERT_PTR_NOT_NULL(BELLE_SIP_HEADER_VIA(belle_sip_message_get_header(message,"Via")));
46

47 48
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Call-ID"));
	BC_ASSERT_PTR_NOT_NULL(BELLE_SIP_HEADER_CALL_ID(belle_sip_message_get_header(message,"Call-ID")));
49 50


51 52
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Content-Length"));
	BC_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);
81 82 83 84 85
	BC_ASSERT_STRING_EQUAL(belle_sip_request_get_method(request),"REGISTER");
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Expires"));
	BC_ASSERT_PTR_NOT_NULL(BELLE_SIP_HEADER_EXPIRES(belle_sip_message_get_header(message,"Expires")));
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Proxy-Authorization"));
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Contact"));
jehan's avatar
jehan committed
86

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
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"\
jehan's avatar
jehan committed
102
							"Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO, PRACK\r\n"\
103
							"c: application/sdp\r\n"\
104
							"Supported: replaces\r\n"\
jehan's avatar
jehan committed
105
							"Supported: 100rel\r\n"\
jehan's avatar
jehan committed
106
							"Authorization: Digest username=\"003332176\", realm=\"sip.ovh.net\", nonce=\"24212965507cde726e8bc37e04686459\", uri=\"sip:sip.ovh.net\", response=\"896e786e9c0525ca3085322c7f1bce7b\", algorithm=MD5, opaque=\"241b9fb347752f2\"\r\n"\
107 108
							"User-Agent: X-Lite 4 release 4.0 stamp 58832\r\n"\
							"Content-Length: 230\r\n\r\n";
109
	belle_sip_request_t* request;
110
	belle_sip_message_t* message = belle_sip_message_parse(raw_message);
jehan's avatar
jehan committed
111 112 113
	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);
114
	request = BELLE_SIP_REQUEST(message);
115 116 117 118
	BC_ASSERT_STRING_EQUAL(belle_sip_request_get_method(request),"INVITE");
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Contact"));
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Authorization"));
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Content-Type"));
jehan's avatar
jehan committed
119 120 121 122 123 124 125 126 127
	
	belle_sip_header_supported_t* header_support;
	BC_ASSERT_PTR_NOT_NULL(header_support=BELLE_SIP_HEADER_SUPPORTED(belle_sip_message_get_header(message,"Supported")));
	if (header_support) {
		belle_sip_list_t* list;
		list = belle_sip_header_supported_get_supported(header_support);
		BC_ASSERT_PTR_NOT_NULL(list);
		const char* value[] = {"replaces", "100rel"};
		int num_supported = 2;
jehan's avatar
jehan committed
128 129
		int i;
		for(i = 0; i < num_supported; i++){
jehan's avatar
jehan committed
130 131 132 133 134 135 136
			if (list) {
				BC_ASSERT_STRING_EQUAL((const char *)(list->data), value[i]);
				list=list->next;
			}
		}
	}
	
jehan's avatar
jehan committed
137
	check_uri_and_headers(message);
jehan's avatar
jehan committed
138 139
	belle_sip_object_unref(message);
	belle_sip_free(encoded_message);
jehan's avatar
jehan committed
140
}
Ghislain MARY's avatar
Ghislain MARY committed
141

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
static void testInviteMessageWithTelUri(void) {
	const char* raw_message = "INVITE tel:11234567888;phone-context=vzims.fr 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: <tel:+3311234567888;tot=titi>\r\n"\
							"From: tel:11234567888;tag=werwrw\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"\
							"c: application/sdp\r\n"\
							"Supported: replaces\r\n"\
							"Authorization: Digest username=\"003332176\", realm=\"sip.ovh.net\", nonce=\"24212965507cde726e8bc37e04686459\", uri=\"sip:sip.ovh.net\", response=\"896e786e9c0525ca3085322c7f1bce7b\", algorithm=MD5, opaque=\"241b9fb347752f2\"\r\n"\
							"User-Agent: X-Lite 4 release 4.0 stamp 58832\r\n"\
							"Content-Length: 230\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);
163 164 165 166
	BC_ASSERT_STRING_EQUAL(belle_sip_request_get_method(request),"INVITE");
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Contact"));
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Authorization"));
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Content-Type"));
167
	check_uri_and_headers(message);
168 169 170
	BC_ASSERT_PTR_NOT_NULL(belle_sip_header_address_get_absolute_uri(BELLE_SIP_HEADER_ADDRESS(belle_sip_message_get_header_by_type(message,belle_sip_header_from_t))));
	BC_ASSERT_PTR_NOT_NULL(belle_sip_header_address_get_absolute_uri(BELLE_SIP_HEADER_ADDRESS(belle_sip_message_get_header_by_type(message,belle_sip_header_to_t))));
	BC_ASSERT_STRING_EQUAL(belle_generic_uri_get_opaque_part(belle_sip_request_get_absolute_uri(request)),"11234567888;phone-context=vzims.fr");
171 172 173 174
	belle_sip_object_unref(message);
	belle_sip_free(encoded_message);
}

jehan's avatar
jehan committed
175 176 177 178 179 180 181 182 183 184
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";
185
	belle_sip_response_t* response;
jehan's avatar
jehan committed
186
	belle_sip_message_t* message = belle_sip_message_parse(raw_message);
jehan's avatar
jehan committed
187 188 189
	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);
190
	response = BELLE_SIP_RESPONSE(message);
191 192 193
	BC_ASSERT_EQUAL(belle_sip_response_get_status_code(response),401,int,"%d");
	BC_ASSERT_STRING_EQUAL(belle_sip_response_get_reason_phrase(response),"Unauthorized");
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"WWW-Authenticate"));
194
	check_uri_and_headers(message);
jehan's avatar
jehan committed
195 196
	belle_sip_object_unref(message);
	belle_sip_free(encoded_message);
197
}
Ghislain MARY's avatar
Ghislain MARY committed
198

199 200 201 202 203 204 205 206
static void test401ResponseWithoutResponsePhrase(void) {
	const char* raw_message = 	"SIP/2.0 401 \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"
207
								"WWW-Authenticate: Digest realm=\"sip.ovh.net\",\r\n   nonce=\"24212965507cde726e8bc37e04686459\",opaque=\"241b9fb347752f2\",stale=false,algorithm=MD5\r\n"
208 209 210 211 212 213 214
								"Content-Length: 0\r\n\r\n";
	belle_sip_response_t* response;
	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);
	response = BELLE_SIP_RESPONSE(message);
215 216 217
	BC_ASSERT_EQUAL(belle_sip_response_get_status_code(response),401,int,"%d");
	BC_ASSERT_PTR_NULL(belle_sip_response_get_reason_phrase(response));
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"WWW-Authenticate"));
218 219 220 221 222
	check_uri_and_headers(message);
	belle_sip_object_unref(message);
	belle_sip_free(encoded_message);
}

223
static void testRegisterRaw(void) {
224 225 226 227 228 229 230 231 232 233
	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"\
234
							"Content-Length: 0\r\n\r\n123456789";
235
	belle_sip_request_t* request;
236 237 238
	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);
239
	BC_ASSERT_EQUAL((unsigned int)raw_message_size,(unsigned int)size+9,unsigned int,"%u");
240
	request = BELLE_SIP_REQUEST(message);
241 242 243
	BC_ASSERT_STRING_EQUAL(belle_sip_request_get_method(request),"REGISTER");
	BC_ASSERT_PTR_NOT_NULL(belle_sip_request_get_uri(request));
	BC_ASSERT_STRING_EQUAL(&raw_message[size],"123456789");
jehan's avatar
jehan committed
244
	belle_sip_object_unref(message);
245
}
246

247 248 249 250 251 252 253 254 255 256 257 258 259 260
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);
261 262
	BC_ASSERT_STRING_EQUAL(belle_sip_request_get_method(request),"REGISTER");
	BC_ASSERT_PTR_NOT_NULL(belle_sip_request_get_uri(request));
jehan's avatar
jehan committed
263
	belle_sip_object_unref(message);
264
}
jehan's avatar
jehan committed
265

266
static void test_extract_source(void) {
jehan's avatar
jehan committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280
	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"
281 282
						"Content-Type: application/sdp\r\n"\
						"Content-Length: 230\r\n\r\n";
jehan's avatar
jehan committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

	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);
303 304 305 306 307
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Content-type"));
	BC_ASSERT_PTR_NOT_NULL(source);
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(source),"37.59.129.73");
	BC_ASSERT_EQUAL(belle_sip_uri_get_port(source),0,int,"%d");
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_transport_param(source),"tcp");
jehan's avatar
jehan committed
308
	belle_sip_object_unref(message);
Simon Morlat's avatar
Simon Morlat committed
309
	belle_sip_object_unref(source);
jehan's avatar
jehan committed
310 311 312 313

	message = belle_sip_message_parse(invite_2);
	request = BELLE_SIP_REQUEST(message);
	source =belle_sip_request_extract_origin(request);
314 315 316 317
	BC_ASSERT_PTR_NOT_NULL(source);
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(source),"81.56.113.2");
	BC_ASSERT_EQUAL(belle_sip_uri_get_port(source),15060,int,"%d");
	BC_ASSERT_STRING_EQUAL(belle_sip_uri_get_transport_param(source),"udp");
jehan's avatar
jehan committed
318
	belle_sip_object_unref(message);
Simon Morlat's avatar
Simon Morlat committed
319
	belle_sip_object_unref(source);
jehan's avatar
jehan committed
320
}
Ghislain MARY's avatar
Ghislain MARY committed
321

322 323 324 325 326
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);
327 328
	BC_ASSERT_EQUAL(belle_sip_response_get_status_code(response),100,int,"%d");
	BC_ASSERT_STRING_EQUAL(belle_sip_response_get_reason_phrase(response),"Trying");
329 330 331
	belle_sip_object_unref(message);
}

jehan's avatar
jehan committed
332 333 334
/*static void test_fix_contact_with_received_rport() {

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

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
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);
354
	BC_ASSERT_FALSE(belle_sip_message_check_headers(message));
Simon Morlat's avatar
Simon Morlat committed
355
	belle_sip_object_unref(message);
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
}

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);
380 381 382 383 384
	BC_ASSERT_STRING_EQUAL(belle_sip_request_get_method(request),"REGISTER");
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Expires"));
	BC_ASSERT_PTR_NOT_NULL(BELLE_SIP_HEADER_EXPIRES(belle_sip_message_get_header(message,"Expires")));
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Proxy-Authorization"));
	BC_ASSERT_PTR_NULL(belle_sip_message_get_header(message,"Contact")); /*contact is optionnal in register*/
385 386 387 388 389 390

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

jehan's avatar
jehan committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
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);
410
	BC_ASSERT_PTR_NULL(message);
jehan's avatar
jehan committed
411
}
jehan's avatar
jehan committed
412

413 414
void channel_parser_tester_recovery_from_error_base (const char* prelude,const char* raw_message) {

jehan's avatar
jehan committed
415 416 417 418 419 420 421 422
	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);

423 424 425 426 427 428 429 430 431 432 433 434 435 436 437

	belle_sip_request_t* request;
	belle_sip_message_t* message;

	if (prelude) {
		channel->input_stream.write_ptr = strcpy(channel->input_stream.write_ptr,prelude);
		channel->input_stream.write_ptr+=strlen(prelude);
		belle_sip_channel_parse_stream(channel,FALSE);
	}

	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,FALSE);

438 439
	BC_ASSERT_PTR_NOT_NULL(channel->incoming_messages);
	BC_ASSERT_PTR_NOT_NULL(channel->incoming_messages->data);
440
	message=BELLE_SIP_MESSAGE(channel->incoming_messages->data);
441
	BC_ASSERT_TRUE(BELLE_SIP_OBJECT_IS_INSTANCE_OF(message,belle_sip_request_t));
442
	request = BELLE_SIP_REQUEST(message);
443 444 445 446
	BC_ASSERT_STRING_EQUAL(belle_sip_request_get_method(request),"REGISTER");
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Expires"));
	BC_ASSERT_PTR_NOT_NULL(BELLE_SIP_HEADER_EXPIRES(belle_sip_message_get_header(message,"Expires")));
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Proxy-Authorization"));
447 448

	check_uri_and_headers(message);
Simon Morlat's avatar
Simon Morlat committed
449
	belle_sip_object_unref(channel);
450 451
	belle_sip_object_unref(stack);
}
452

453
void channel_parser_tester_recovery_from_error(void) {
454
	const char * raw_message=	"debut de stream tout pourri\r\n"
455
			"INVITE je_suis_une_fausse _request_uri_hihihi SIP/2.0\r\n"
jehan's avatar
jehan committed
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 483 484
			"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";
485
	channel_parser_tester_recovery_from_error_base (NULL, raw_message);
jehan's avatar
jehan committed
486
}
487
void channel_parser_malformed_start(void) {
488
	const char * raw_message=	"debut de stream tout pourri\r\n"
jehan's avatar
jehan committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502
			"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";
503 504
	channel_parser_tester_recovery_from_error_base (NULL, raw_message);
}
jehan's avatar
jehan committed
505

506
void channel_parser_truncated_start(void) {
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
	const char * prelude= "R";
	const char * raw_message=	"EGISTER 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";
jehan's avatar
jehan committed
522

523 524
	channel_parser_tester_recovery_from_error_base (prelude, raw_message);
}
jehan's avatar
jehan committed
525

526
void channel_parser_truncated_start_with_garbage(void) {
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
	const char * prelude= "truc tout pourit R";
	const char * raw_message=	"EGISTER 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";
jehan's avatar
jehan committed
542

543
	channel_parser_tester_recovery_from_error_base (prelude, raw_message);
544 545
}

546 547 548 549 550 551 552
static void testMalformedFrom_process_response_cb(void *user_ctx, const belle_sip_response_event_t *event){
	int status = belle_sip_response_get_status_code(belle_sip_response_event_get_response(event));

	belle_sip_message("testMalformedFrom_process_response_cb [%i]",status);

	(*(int*)user_ctx) += 1; // increment the call counter

553
	BC_ASSERT( status == 400 );
554 555
}

556 557 558
#define LISTENING_POINT_PORT 45421
#define LISTENING_POINT_HOSTPORT "127.0.0.1:45421" /* need the same port as above */

559 560 561 562
static void testMalformedFrom(void){
	belle_sip_stack_t*        stack = belle_sip_stack_new(NULL);
	belle_sip_listening_point_t* lp = belle_sip_stack_create_listening_point(stack,
																			 "127.0.0.1",
563
																			 LISTENING_POINT_PORT,
564 565 566 567
																			 "tcp");
	belle_sip_provider_t* provider = belle_sip_provider_new(stack,lp);
	belle_sip_listener_callbacks_t listener_cbs = {0};

568 569
	const char* raw_message = "INVITE sip:us2@172.1.1.1 SIP/2.0\r\n"
			"Via: SIP/2.0/TCP " LISTENING_POINT_HOSTPORT ";branch=z9hG4bK-edx-U_1zoIkaq72GJPqpSmDpJQ-ouBelFuLODzf9oS5J9MeFUA;rport\r\n"
570
			"From: c\x8e test <sip:00_12_34_56_78_90@us2>;tag=klsk+kwDc\r\n" /** 'c? test' should be enclosed in double quotes */
571 572
			"To: <sip:us2@172.1.1.1;transport=tcp>\r\n"
			"Contact: <sip:00_12_34_56_78_90@172.2.2.2>\r\n"
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
			"Call-ID: 2b6fb0320-1384-179494-426025-23b6b0-2e3303331@172.16.42.1\r\n"
			"Content-Type: application/sdp\r\n"
			"Content-Length: 389\r\n"
			"CSeq: 1 INVITE\r\n"
			"Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, INFO, UPDATE, REGISTER, MESSAGE, REFER, SUBSCRIBE, PRACK\r\n"
			"Accept: application/sdp, application/dtmf-relay\r\n"
			"Max-Forwards: 69\r\n"
			"\r\n"
			"v=0\r\n"
			"o=- 1826 1826 IN IP4 172.16.42.1\r\n"
			"s=VeriCall Edge\r\n"
			"c=IN IP4 172.16.42.1\r\n"
			"t=0 0\r\n"
			"m=audio 20506 RTP/AVP 0 8 13 101\r\n"
			"a=rtpmap:0 PCMU/8000\r\n"
			"a=rtpmap:8 PCMA/8000\r\n"
			"a=rtpmap:13 CN/8000\r\n"
			"a=rtpmap:101 telephone-event/8000\r\n"
			"a=fmtp:101 0-15\r\n"
			"m=video 24194 RTP/AVP 105 104\r\n"
			"a=sendonly\r\n"
			"a=rtpmap:105 H264/90000\r\n"
			"a=fmtp:105 packetization-mode=0\r\n"
			"a=rtpmap:104 H263-1998/90000\r\n"
			"a=fmtp:104 CIF=1;J=1\r\n";

	belle_sip_message_t* message = belle_sip_message_parse(raw_message);
	belle_sip_listener_t* listener = NULL;

	int called_times = 0;

	listener_cbs.process_response_event = testMalformedFrom_process_response_cb;
	listener = belle_sip_listener_create_from_callbacks(&listener_cbs, &called_times);
	belle_sip_provider_add_sip_listener(provider, listener);

	belle_sip_object_ref(message);
	belle_sip_object_ref(message); /* double ref: originally the message is created with 0 refcount, and dispatch_message will unref() it.*/
	belle_sip_provider_dispatch_message(provider, message);
	// we expect the stack to send a 400 error
	belle_sip_stack_sleep(stack,1000);

614
	BC_ASSERT_EQUAL(called_times,1,int,"%d");
615 616 617 618 619 620 621 622 623
	belle_sip_provider_remove_sip_listener(provider,listener);

	belle_sip_object_unref(listener);
	belle_sip_object_unref(provider);
	belle_sip_object_unref(stack);
	belle_sip_object_unref(message);

}

624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
static void testMalformedFrom2(void){
	belle_sip_stack_t*        stack = belle_sip_stack_new(NULL);
	belle_sip_listening_point_t* lp = belle_sip_stack_create_listening_point(stack,
																			 "127.0.0.1",
																			 LISTENING_POINT_PORT,
																			 "tcp");
	belle_sip_provider_t* provider = belle_sip_provider_new(stack,lp);
	belle_sip_listener_callbacks_t listener_cbs = {0};

	const char* raw_message = "INVITE sip:us2@172.1.1.1 SIP/2.0\r\n"
			"Via: SIP/2.0/TCP " LISTENING_POINT_HOSTPORT ";branch=z9hG4bK-edx-U_1zoIkaq72GJPqpSmDpJQ-ouBelFuLODzf9oS5J9MeFUA;rport\r\n"
			"From: RottenFrom <sip:@somehost>;tag=klsk+kwDc\r\n" /** The userpart is empty before '@', this is not allowed */
			"To: <sip:us2@172.1.1.1;transport=tcp>\r\n"
			"Contact: <sip:00_12_34_56_78_90@172.2.2.2>\r\n"
			"Call-ID: 2b6fb0320-1384-179494-426025-23b6b0-2e3303331@172.16.42.1\r\n"
			"Content-Type: application/sdp\r\n"
			"Content-Length: 389\r\n"
			"CSeq: 1 INVITE\r\n"
			"Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, INFO, UPDATE, REGISTER, MESSAGE, REFER, SUBSCRIBE, PRACK\r\n"
			"Accept: application/sdp, application/dtmf-relay\r\n"
			"Max-Forwards: 69\r\n"
			"\r\n"
			"v=0\r\n"
			"o=- 1826 1826 IN IP4 172.16.42.1\r\n"
			"s=Pouetpouet\r\n"
			"c=IN IP4 172.16.42.1\r\n"
			"t=0 0\r\n"
			"m=audio 20506 RTP/AVP 0 8 13 101\r\n"
			"a=rtpmap:0 PCMU/8000\r\n"
			"a=rtpmap:8 PCMA/8000\r\n"
			"a=rtpmap:13 CN/8000\r\n"
			"a=rtpmap:101 telephone-event/8000\r\n"
			"a=fmtp:101 0-15\r\n"
			"m=video 24194 RTP/AVP 105 104\r\n"
			"a=sendonly\r\n"
			"a=rtpmap:105 H264/90000\r\n"
			"a=fmtp:105 packetization-mode=0\r\n"
			"a=rtpmap:104 H263-1998/90000\r\n"
			"a=fmtp:104 CIF=1;J=1\r\n";

	belle_sip_message_t* message = belle_sip_message_parse(raw_message);
	belle_sip_listener_t* listener = NULL;

	int called_times = 0;

	listener_cbs.process_response_event = testMalformedFrom_process_response_cb;
	listener = belle_sip_listener_create_from_callbacks(&listener_cbs, &called_times);
	belle_sip_provider_add_sip_listener(provider, listener);

	belle_sip_object_ref(message);
	belle_sip_object_ref(message); /* double ref: originally the message is created with 0 refcount, and dispatch_message will unref() it.*/
	belle_sip_provider_dispatch_message(provider, message);
	// we expect the stack to send a 400 error
	belle_sip_stack_sleep(stack,1000);

	BC_ASSERT_EQUAL(called_times,1,int,"%d");
	belle_sip_provider_remove_sip_listener(provider,listener);

	belle_sip_object_unref(listener);
	belle_sip_object_unref(provider);
	belle_sip_object_unref(stack);
	belle_sip_object_unref(message);

}

jehan's avatar
jehan committed
689

690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
static void testMalformedMandatoryField(void){
	belle_sip_stack_t*        stack = belle_sip_stack_new(NULL);
	belle_sip_listening_point_t* lp = belle_sip_stack_create_listening_point(stack,
																			 "127.0.0.1",
																			 LISTENING_POINT_PORT,
																			 "tcp");
	belle_sip_provider_t* provider = belle_sip_provider_new(stack,lp);
	belle_sip_listener_callbacks_t listener_cbs = {0};

	/* the MESSAGE message has no definition on which fields are required, which means we'll go into
	 *
	 *
	 *
	 */

	const char* raw_message = "MESSAGE sip:lollol.iphone@22.22.222.222:5861;transport=tcp SIP/2.0\r\n"
			"Via: SIP/2.0/TCP " LISTENING_POINT_HOSTPORT ";branch=z9hG4bK5eca096a;rport\r\n"
			"Max-Forwards: 70\r\n"
			"From: \"MS TFT\" <sip:lollol-labo-ms-tft1@11.11.111.111>;tag=as2413a381\r\n"
			"To: <sip:lollol-labo-iphone4s@22.22.22.222:5861;app-id=fr.lollol.phone.prod;pn-type=apple;pn-tok=azertyuiopqsdfghhjjkmlqoijfubieuzhqiluehcpoqidufqsdkjlcnuoishcvs;pn-msg-str=IM_MSG;pn-call-str=IC_MSG;pn-call-snd=ring.caf;pn-msg-snd=msg.caf;transport=tcp>;tag=\r\n"
			"Call-ID: 4070383971a9674201f463af2de1f012@11.11.111.111:5060\r\n"
			"CSeq: 103 MESSAGE\r\n"
			"User-Agent: Sip Server On Host (20130523_12h10)\r\n"
			"Content-Type: text/plain;charset=UTF-8\r\n"
			"Content-Length: 276\r\n"
			"\r\n"
			"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
			"<VDUCMediaConfig GId=\"1234567\" IdPosteType=\"123\"><Label>Salut Bilout TFT MS2</Label><MediaConfig GId=\"456\"><CommandCode Code=\"MediaCommand*\"><Label>Porte ouverte</Label></CommandCode><withVideo FPS=\"0.0\"/></MediaConfig></VDUCMediaConfig>\r\n"
			"\r\n";

	belle_sip_message_t* message = belle_sip_message_parse(raw_message);
	belle_sip_listener_t* listener = NULL;

	int called_times = 0;

	listener_cbs.process_response_event = testMalformedFrom_process_response_cb;
	listener = belle_sip_listener_create_from_callbacks(&listener_cbs, &called_times);

	belle_sip_provider_add_sip_listener(provider, listener);

	belle_sip_object_ref(message);
	belle_sip_object_ref(message); /* double ref: originally the message is created with 0 refcount, and dispatch_message will unref() it.*/

	belle_sip_provider_dispatch_message(provider, message);
	// we expect the stack to send a 400 error
	belle_sip_stack_sleep(stack,1000);

737
	BC_ASSERT_EQUAL(called_times,1,int,"%d");
738 739 740 741 742 743 744 745 746 747
	belle_sip_provider_remove_sip_listener(provider,listener);

	belle_sip_object_unref(listener);
	belle_sip_object_unref(provider);
	belle_sip_object_unref(stack);
	belle_sip_object_unref(message);

}


748 749 750


static void testRFC2543_base(char* branch) {
751
	belle_sip_server_transaction_t *tr;
752 753
	const char* raw_message_base = 	"INVITE sip:me@127.0.0.1 SIP/2.0\r\n"
			"Via: SIP/2.0/UDP 192.168.1.12:15060;%srport=15060;received=81.56.113.2\r\n"
754 755 756 757 758 759 760 761 762 763 764 765
			"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"
766
			"Extended: \r\n" /*fixme lexer*/
767 768
			"\r\n";

769
	char raw_message[2048];
770 771 772
	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);
jehan's avatar
jehan committed
773 774 775
	belle_sip_message_t* message;

	snprintf(raw_message,sizeof(raw_message),raw_message_base,branch);
776

jehan's avatar
jehan committed
777
	message = belle_sip_message_parse(raw_message);
778 779 780 781
	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);
782

783
	BC_ASSERT_PTR_NOT_NULL(request);
784
	tr=belle_sip_provider_create_server_transaction(prov,request);
785 786
	BC_ASSERT_PTR_NOT_NULL(belle_sip_provider_find_matching_server_transaction(prov,request)); /*make sure branch id is properly set*/
	BC_ASSERT_PTR_NOT_NULL(tr);
787 788 789
	belle_sip_object_unref(prov);
	belle_sip_object_unref(stack);
	belle_sip_object_unref(message);
jehan's avatar
jehan committed
790
}
791 792 793
static void testRFC2543Compat(void) {
	testRFC2543_base("");
}
794
static void testRFC2543CompatWithBranch(void) {
795 796
	testRFC2543_base("branch=blablabla;");
}
jehan's avatar
jehan committed
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
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);
828 829
	BC_ASSERT_PTR_NOT_NULL(request);
	BC_ASSERT_PTR_NOT_NULL(raw_header=belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"header1"));
jehan's avatar
jehan committed
830
	if (raw_header) {
831
		BC_ASSERT_STRING_EQUAL(belle_sip_header_extension_get_value(BELLE_SIP_HEADER_EXTENSION(raw_header)),"blabla");
jehan's avatar
jehan committed
832
	}
833
	BC_ASSERT_PTR_NOT_NULL(raw_header=belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"header2"));
jehan's avatar
jehan committed
834
	if (raw_header) {
835
		BC_ASSERT_STRING_EQUAL(belle_sip_header_extension_get_value(BELLE_SIP_HEADER_EXTENSION(raw_header)),"blue;weftutu=bla");
jehan's avatar
jehan committed
836
	}
837
	BC_ASSERT_PTR_NOT_NULL(raw_header=belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"To"));
jehan's avatar
jehan committed
838
	if (raw_header) {
839
		BC_ASSERT_STRING_EQUAL(belle_sip_header_extension_get_value(BELLE_SIP_HEADER_EXTENSION(raw_header)),"sip:toto@titi.com");
jehan's avatar
jehan committed
840
	}
841 842 843 844 845 846 847 848 849 850
	BC_ASSERT_STRING_NOT_EQUAL(belle_sip_header_get_unparsed_value(belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"From")),"toto");
	BC_ASSERT_PTR_NULL(belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"Record-Route"));
	BC_ASSERT_PTR_NULL(belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"Accept"));
	BC_ASSERT_PTR_NULL(belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"Accept-Encoding"));
	BC_ASSERT_PTR_NULL(belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"Accept-Language"));
	BC_ASSERT_PTR_NULL(belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"Allow"));
	BC_ASSERT_PTR_NULL(belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"Contact"));
	BC_ASSERT_PTR_NULL(belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"Organization"));
	BC_ASSERT_PTR_NULL(belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"Supported"));
	BC_ASSERT_PTR_NULL(belle_sip_message_get_header(BELLE_SIP_MESSAGE(request),"User-Agent"));
jehan's avatar
jehan committed
851 852 853


	belle_sip_object_unref(request);
Simon Morlat's avatar
Simon Morlat committed
854 855
	belle_sip_object_unref(prov);
	belle_sip_object_unref(stack);
jehan's avatar
jehan committed
856 857

}
jehan's avatar
jehan committed
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
static void testUrisComponentsForRequest(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);
	belle_sip_client_transaction_t* t;
	const char* raw_uri="sip:toto@titi.com?header1=blabla";

	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")
										,belle_sip_header_to_parse("To: sip:titi@titi.com:5061")
										,belle_sip_header_via_new()
										,70);
873
	BC_ASSERT_PTR_NOT_NULL(request);
jehan's avatar
jehan committed
874
	t=belle_sip_provider_create_client_transaction(prov,request);
875
	BC_ASSERT_NOT_EQUAL(belle_sip_client_transaction_send_request(t),0,int,"%d");
Simon Morlat's avatar
Simon Morlat committed
876 877
	belle_sip_object_unref(prov);
	belle_sip_object_unref(stack);
jehan's avatar
jehan committed
878 879
}

jehan's avatar
jehan committed
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
static void testGenericMessage(void) {
	const char* raw_message = 	"SIP/2.0 180 Ringing\r\n"
			"Via: SIP/2.0/UDP 192.168.1.73:5060;branch=z9hG4bK.hhdJx4~kD;rport\r\n"
			"Record-Route: <sip:91.121.209.194;lr>\r\n"
			"Record-Route: <sip:siproxd@192.168.1.254:5060;lr>\r\n"
			"From: <sip:granny2@sip.linphone.org>;tag=5DuaoDRru\r\n"
			"To: <sip:chmac@sip.linphone.org>;tag=PelIhu0\r\n"
			"Call-ID: e-2Q~fxwNs\r\n"
			"CSeq: 21 INVITE\r\n"
			"user-agent: Linphone/3.6.99 (belle-sip/1.2.4)\r\n"
			"supported: replaces\r\n"
			"supported: outbound\r\n"
			"Content-Length: 0\r\n"
			"\r\n";

	belle_sip_response_t* response;
	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);
	response = BELLE_SIP_RESPONSE(message);
901 902 903
	BC_ASSERT_EQUAL(belle_sip_response_get_status_code(response),180,int,"%d");
/*	BC_ASSERT_STRING_EQUAL(belle_sip_response_get_reason_phrase(response),"Unauthorized");
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"WWW-Authenticate"));
jehan's avatar
jehan committed
904 905 906 907 908
	check_uri_and_headers(message);*/
	belle_sip_object_unref(message);
	belle_sip_free(encoded_message);
}

909 910


jehan's avatar
jehan committed
911 912 913 914 915 916 917 918 919 920 921 922 923 924
static void testHttpGet(void)  {
	const char* raw_message = 	"GET /index.php HTTP/1.1\r\n"
							 	"User-Agent: Wget/1.14 (darwin11.4.2)\r\n"
								"Accept: */*\r\n"
								"Host: www.linphone.org\r\n"
								"Connection: Keep-Alive\r\n"
								"\r\n";
	char* marshaled_msg;
	belle_sip_message_t* msg = belle_sip_message_parse(raw_message);
	belle_http_request_t* http_request;
	belle_generic_uri_t* uri;
	belle_sip_header_extension_t* host_header;
	belle_sip_object_t* tmp;

925
	if (!BC_ASSERT_PTR_NOT_NULL(msg)) return;
jehan's avatar
jehan committed
926 927 928 929 930 931 932 933 934

	marshaled_msg=belle_sip_object_to_string(BELLE_SIP_OBJECT(msg));
	belle_sip_object_unref(msg);
	msg = belle_sip_message_parse(marshaled_msg);
	belle_sip_free(marshaled_msg);
	tmp=belle_sip_object_clone(BELLE_SIP_OBJECT(msg));
	belle_sip_object_unref(msg);
	msg=BELLE_SIP_MESSAGE(tmp);

935
	BC_ASSERT_TRUE(BELLE_SIP_IS_INSTANCE_OF(msg,belle_http_request_t));
jehan's avatar
jehan committed
936
	http_request=BELLE_HTTP_REQUEST(msg);
937
	if (!BC_ASSERT_PTR_NOT_NULL(uri=belle_http_request_get_uri(http_request))) return;
938 939 940 941 942 943 944

	BC_ASSERT_STRING_EQUAL(belle_generic_uri_get_path(uri),"/index.php");
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(msg,"User-Agent"));
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(msg,"Accept"));
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(msg,"Connection"));
	BC_ASSERT_PTR_NOT_NULL(host_header=BELLE_SIP_HEADER_EXTENSION(belle_sip_message_get_header(msg,"Host")));
	BC_ASSERT_STRING_EQUAL(belle_sip_header_extension_get_value(host_header),"www.linphone.org");
jehan's avatar
jehan committed
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
	belle_sip_object_unref(msg);
}

static void testHttp200Ok(void)  {
	const char* raw_message = 	"HTTP/1.1 200 OK\r\n"
								"Date: Tue, 07 Jan 2014 09:28:43 GMT\r\n"
								"Server: Apache\r\n"
								"Last-Modified: Tue, 18 Aug 1998 20:19:11 GMT\r\n"
								"ETag: \"8982a60-14a17-335b3dcdcadc0\"\r\n"
								"Accept-Ranges: bytes\r\n"
								"Vary: Accept-Encoding\r\n"
								"Content-Encoding: gzip\r\n"
								"Content-Length: 6\r\n"
								"Keep-Alive: timeout=15, max=100\r\n"
								"Connection: Keep-Alive\r\n"
								"Content-Type: text/plain\r\n"
								"\r\n"
								"blabla";

	char* marshaled_msg;
	belle_sip_message_t* msg = belle_sip_message_parse(raw_message);
	belle_http_response_t* http_response;
	belle_sip_header_extension_t* host_header;
	belle_sip_object_t* tmp;

970
	if (!BC_ASSERT_PTR_NOT_NULL(msg)) return;
jehan's avatar
jehan committed
971 972 973 974 975 976 977 978 979

	marshaled_msg=belle_sip_object_to_string(BELLE_SIP_OBJECT(msg));
	belle_sip_object_unref(msg);
	msg = belle_sip_message_parse(marshaled_msg);
	belle_sip_free(marshaled_msg);
	tmp=belle_sip_object_clone(BELLE_SIP_OBJECT(msg));
	belle_sip_object_unref(msg);
	msg=BELLE_SIP_MESSAGE(tmp);

980
	BC_ASSERT_TRUE(BELLE_SIP_IS_INSTANCE_OF(msg,belle_http_response_t));
jehan's avatar
jehan committed
981 982
	http_response=BELLE_HTTP_RESPONSE(msg);

983 984
	BC_ASSERT_EQUAL(belle_http_response_get_status_code(http_response),200,int,"%d");
	BC_ASSERT_STRING_EQUAL(belle_http_response_get_reason_phrase(http_response),"OK");
jehan's avatar
jehan committed
985

986 987 988 989 990
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(msg,"Date"));
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(msg,"ETag"));
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(msg,"Connection"));
	BC_ASSERT_PTR_NOT_NULL(host_header=BELLE_SIP_HEADER_EXTENSION(belle_sip_message_get_header(msg,"Server")));
	BC_ASSERT_STRING_EQUAL(belle_sip_header_extension_get_value(host_header),"Apache");
jehan's avatar
jehan committed
991 992 993 994
	belle_sip_object_unref(msg);
}


995
void channel_parser_http_response(void) {
jehan's avatar
jehan committed
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
	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);

	const char * raw_message=	"HTTP/1.1 200 OK\r\n"
								"Cache-Control: private\r\n"
								"Date: Tue, 07 Jan 2014 13:51:57 GMT\r\n"
								"Content-Type: text/html; charset=utf-8\r\n"
								"Server: Microsoft-IIS/6.0\r\n"
								"X-Powered-By: ASP.NET\r\n"
								"Content-Encoding: gzip\r\n"
								"Vary: Accept-Encoding\r\n"
								"Transfer-Encoding: chunked\r\n"
1013 1014
								"\r\n"
								"<html></html>\r\n\r\n";
jehan's avatar
jehan committed
1015 1016 1017 1018 1019
	belle_http_response_t* response;
	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);

1020
	belle_sip_channel_parse_stream(channel,TRUE);
jehan's avatar
jehan committed
1021

1022 1023
	BC_ASSERT_PTR_NOT_NULL(channel->incoming_messages);
	BC_ASSERT_PTR_NOT_NULL(channel->incoming_messages->data);
jehan's avatar
jehan committed
1024
	message=BELLE_SIP_MESSAGE(channel->incoming_messages->data);
1025
	BC_ASSERT_TRUE(BELLE_SIP_OBJECT_IS_INSTANCE_OF(message,belle_http_response_t));
jehan's avatar
jehan committed
1026
	response = BELLE_HTTP_RESPONSE(message);
1027 1028 1029 1030
	BC_ASSERT_STRING_EQUAL(belle_http_response_get_reason_phrase(response),"OK");
	BC_ASSERT_EQUAL(belle_http_response_get_status_code(response),200,int,"%d");
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Cache-Control"));
	BC_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header(message,"Vary"));
jehan's avatar
jehan committed
1031

Simon Morlat's avatar
Simon Morlat committed
1032
	belle_sip_object_unref(channel);
jehan's avatar
jehan committed
1033 1034 1035
	belle_sip_object_unref(stack);
}

jehan's avatar
jehan committed
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
void testGetBody(void) {
	const char* raw_message = "INVITE sip:us2@172.1.1.1 SIP/2.0\r\n"
				"Via: SIP/2.0/TCP " LISTENING_POINT_HOSTPORT ";branch=z9hG4bK-edx-U_1zoIkaq72GJPqpSmDpJQ-ouBelFuLODzf9oS5J9MeFUA;rport\r\n"
				"From: test <sip:00_12_34_56_78_90@us2>;tag=klsk+kwDc\r\n"
				"To: <sip:us2@172.1.1.1;transport=tcp>\r\n"
				"Contact: <sip:00_12_34_56_78_90@172.2.2.2>\r\n"
				"Call-ID: 2b6fb0320-1384-179494-426025-23b6b0-2e3303331@172.16.42.1\r\n"
				"Content-Type: application/sdp\r\n"
				"Content-Length: 389\r\n"
				"CSeq: 1 INVITE\r\n"
				"Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, INFO, UPDATE, REGISTER, MESSAGE, REFER, SUBSCRIBE, PRACK\r\n"
				"Accept: application/sdp, application/dtmf-relay\r\n"
				"Max-Forwards: 69\r\n"
				"\r\n"
				"v=0\r\n"
				"o=- 1826 1826 IN IP4 172.16.42.1\r\n"
				"s=VeriCall Edge\r\n"
				"c=IN IP4 172.16.42.1\r\n"
				"t=0 0\r\n"
				"m=audio 20506 RTP/AVP 0 8 13 101\r\n"
				"a=rtpmap:0 PCMU/8000\r\n"
				"a=rtpmap:8 PCMA/8000\r\n"
				"a=rtpmap:13 CN/8000\r\n"
				"a=rtpmap:101 telephone-event/8000\r\n"
				"a=fmtp:101 0-15\r\n"
				"m=video 24194 RTP/AVP 105 104\r\n"
				"a=sendonly\r\n"
				"a=rtpmap:105 H264/90000\r\n"
				"a=fmtp:105 packetization-mode=0\r\n"
				"a=rtpmap:104 H263-1998/90000\r\n"
				"a=fmtp:104 CIF=1;J=1\r\n"
				"nimportequoi a la fin";
	belle_sip_stack_t* stack = belle_sip_stack_new(NULL);
	belle_sip_channel_t* channel = belle_sip_stream_channel_new_client(stack
																	, NULL
																	, LISTENING_POINT_PORT
																	, NULL
																	, "127.0.0.1"
																	, LISTENING_POINT_PORT);


	belle_sip_message_t* message;
jehan's avatar
jehan committed
1078 1079
	belle_sip_header_content_length_t *ctlt;

jehan's avatar
jehan committed
1080 1081 1082 1083 1084
	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,FALSE);

1085 1086
	BC_ASSERT_PTR_NOT_NULL(channel->incoming_messages);
	BC_ASSERT_PTR_NOT_NULL(channel->incoming_messages->data);
jehan's avatar
jehan committed
1087 1088
	message=BELLE_SIP_MESSAGE(channel->incoming_messages->data);

jehan's avatar
jehan committed
1089
	ctlt = belle_sip_message_get_header_by_type(message,belle_sip_header_content_length_t);
1090
	BC_ASSERT_PTR_NOT_NULL(ctlt);
1091 1092
	BC_ASSERT_EQUAL((unsigned int)belle_sip_header_content_length_get_content_length(ctlt),(unsigned int)strlen(belle_sip_message_get_body(message)),unsigned int,"%u");
	BC_ASSERT_EQUAL((unsigned int)belle_sip_header_content_length_get_content_length(ctlt),(unsigned int)belle_sip_message_get_body_size(message),unsigned int,"%u");
Simon Morlat's avatar
Simon Morlat committed
1093 1094
	belle_sip_object_unref(channel);
	belle_sip_object_unref(stack);
jehan's avatar
jehan committed
1095
}
jehan's avatar
jehan committed
1096

1097 1098 1099 1100 1101 1102 1103 1104 1105
static void testHop(void){
	belle_sip_uri_t * uri = belle_sip_uri_parse("sip:sip.linphone.org;maddr=[2001:41d0:8:6e48::]");
	belle_sip_hop_t *hop = belle_sip_hop_new_from_uri(uri);
	BC_ASSERT_STRING_EQUAL(hop->host, "2001:41d0:8:6e48::");
	belle_sip_object_unref(uri);
	belle_sip_object_unref(hop);
}


Ghislain MARY's avatar
Ghislain MARY committed
1106 1107
/* NOTE - ORDER IS IMPORTANT - MUST TEST fread() AFTER fprintf() */
test_t message_tests[] = {
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
	TEST_NO_TAG("REGISTER", testRegisterMessage),
	TEST_NO_TAG("INVITE", testInviteMessage),
	TEST_NO_TAG("INVITE with tel uri", testInviteMessageWithTelUri),
	TEST_NO_TAG("Option message", testOptionMessage),
	TEST_NO_TAG("REGISTER (Raw)", testRegisterRaw),
	TEST_NO_TAG("401 Response", test401Response),
	TEST_NO_TAG("Response without response phrase",test401ResponseWithoutResponsePhrase),
	TEST_NO_TAG("Origin extraction", test_extract_source),
	TEST_NO_TAG("SIP frag", test_sipfrag),
	TEST_NO_TAG("Malformed invite", testMalformedMessage),
	TEST_NO_TAG("Malformed from", testMalformedFrom),
1119
	TEST_NO_TAG("Malformed from 2", testMalformedFrom2),
1120 1121 1122 1123 1124 1125 1126
	TEST_NO_TAG("Malformed mandatory field", testMalformedMandatoryField),
	TEST_NO_TAG("Malformed invite with bad begin", testMalformedMessageWithWrongStart),
	TEST_NO_TAG("Malformed register", testMalformedOptionnalHeaderInMessage),
	TEST_NO_TAG("Channel parser error recovery", channel_parser_tester_recovery_from_error),
	TEST_NO_TAG("Channel parser malformed start", channel_parser_malformed_start),
	TEST_NO_TAG("Channel parser truncated start", channel_parser_truncated_start),
	TEST_NO_TAG("Channel parser truncated start with garbage",channel_parser_truncated_start_with_garbage),
1127 1128
	TEST_ONE_TAG("RFC2543 compatibility", testRFC2543Compat, "LeaksMemory"),
	TEST_ONE_TAG("RFC2543 compatibility with branch id",testRFC2543CompatWithBranch, "LeaksMemory"),
1129 1130 1131 1132 1133 1134
	TEST_NO_TAG("Uri headers in sip INVITE",testUriHeadersInInvite),
	TEST_NO_TAG("Uris components in request",testUrisComponentsForRequest),
	TEST_NO_TAG("Generic message test",testGenericMessage),
	TEST_NO_TAG("HTTP get",testHttpGet),
	TEST_NO_TAG("HTTP 200 Ok",testHttp200Ok),
	TEST_NO_TAG("Channel parser for HTTP reponse",channel_parser_http_response),
1135 1136
	TEST_NO_TAG("Get body size",testGetBody),
	TEST_NO_TAG("Create hop from uri", testHop)
Ghislain MARY's avatar
Ghislain MARY committed
1137 1138
};

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