belle_sip_refresher_tester.c 32.2 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 18 19 20 21 22
    (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 <stdio.h>
#include "CUnit/Basic.h"
#include "belle-sip/belle-sip.h"
#include "belle_sip_internal.h"
Ghislain MARY's avatar
Ghislain MARY committed
23 24
#include "belle_sip_tester.h"

25
#ifndef _MSC_VER
jehan's avatar
jehan committed
26
#include <sys/time.h>
27
#endif
Ghislain MARY's avatar
Ghislain MARY committed
28 29


jehan's avatar
jehan committed
30 31 32
#define USERNAME "toto"
#define SIPDOMAIN "sip.linphone.org"
#define PASSWD "secret"
jehan's avatar
jehan committed
33

Ghislain MARY's avatar
Ghislain MARY committed
34

35 36 37 38 39 40 41 42 43 44 45
static char publish_body[]=
	"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
	"<impp:presence xmlns:impp=\"urn:ietf:params:xml:ns:pidf entity=\"pres:someone@example.com\">\n"
	"	<impp:tuple id=\"sg89ae\">\n"
	"		<impp:status>\n"
	"			<impp:basic>open</impp:basic>\n"
	"		</impp:status>\n"
	"		<impp:contact priority=\"0.8\">tel:+09012345678</impp:contact>\n"
	"	</impp:tuple>\n"
	"</impp:presence>\n";

jehan's avatar
jehan committed
46 47 48 49 50 51
typedef enum auth_mode {
	none
	,digest
	,digest_auth
}auth_mode_t;

52
typedef struct _status {
jehan's avatar
jehan committed
53
	int twoHundredOk;
jehan's avatar
jehan committed
54
	int fourHundredOne;
jehan's avatar
jehan committed
55
	int refreshOk;
jehan's avatar
jehan committed
56
	int refreshKo;
57
}status_t;
Ghislain MARY's avatar
Ghislain MARY committed
58

jehan's avatar
jehan committed
59 60 61 62 63
typedef struct endpoint {
	belle_sip_stack_t* stack;
	belle_sip_listener_callbacks_t* listener_callbacks;
	belle_sip_provider_t* provider;
	belle_sip_listening_point_t *lp;
Simon Morlat's avatar
Simon Morlat committed
64
	belle_sip_listener_t *listener;
jehan's avatar
jehan committed
65
	auth_mode_t auth;
66
	status_t stat;
jehan's avatar
jehan committed
67
	unsigned char expire_in_contact;
jehan's avatar
jehan committed
68 69
	char nonce[32];
	unsigned int nonce_count;
70 71
	const char* received;
	int rport;
72
	unsigned char unreconizable_contact;
73
	int connection_family;
74
	int register_count;
jehan's avatar
jehan committed
75 76
	int transiant_network_failure;
	belle_sip_refresher_t* refresher;
77
	int early_refresher;
jehan's avatar
jehan committed
78 79
} endpoint_t;

Ghislain MARY's avatar
Ghislain MARY committed
80

jehan's avatar
jehan committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
static unsigned int  wait_for(belle_sip_stack_t*s1, belle_sip_stack_t*s2,int* counter,int value,int timeout) {
	int retry=0;
#define ITER 100
	while (*counter<value && retry++ <(timeout/ITER)) {
		if (s1) belle_sip_stack_sleep(s1,ITER/2);
		if (s2) belle_sip_stack_sleep(s2,ITER/2);
	}
	if(*counter<value) return FALSE;
	else return TRUE;
}

//static void process_dialog_terminated(void *obj, const belle_sip_dialog_terminated_event_t *event){
//	belle_sip_message("process_dialog_terminated called");
//}
//static void process_io_error(void *obj, const belle_sip_io_error_event_t *event){
//	belle_sip_warning("process_io_error");
//	/*belle_sip_main_loop_quit(belle_sip_stack_get_main_loop(stack));*/
//	/*CU_ASSERT(CU_FALSE);*/
//}
Ghislain MARY's avatar
Ghislain MARY committed
100

jehan's avatar
jehan committed
101 102 103 104 105 106 107 108 109 110 111 112
static void compute_response(const char* username
									,const char* realm
									,const char* passwd
									,const char* nonce
									,const char* method
									,const char* uri
									,char response[33] ) {
	char ha1[33],ha2[33];
	belle_sip_auth_helper_compute_ha1(username,realm,passwd,ha1);
	belle_sip_auth_helper_compute_ha2(method,uri,ha2);
	belle_sip_auth_helper_compute_response(ha1,nonce,ha2,response);
}
Ghislain MARY's avatar
Ghislain MARY committed
113

jehan's avatar
jehan committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
static void compute_response_auth_qop(const char* username
										,const char* realm
										,const char* passwd
										,const char* nonce
										,unsigned int nonce_count
										,const char* cnonce
										,const char* qop
										,const char* method
										,const char* uri
										,char response[33] ) {
	char ha1[33],ha2[33];
	belle_sip_auth_helper_compute_ha1(username,realm,passwd,ha1);
	belle_sip_auth_helper_compute_ha2(method,uri,ha2);
	belle_sip_auth_helper_compute_response_qop_auth(ha1, nonce,nonce_count, cnonce,qop,ha2,response);
}
Ghislain MARY's avatar
Ghislain MARY committed
129

jehan's avatar
jehan committed
130
#define MAX_NC_COUNT 3
Ghislain MARY's avatar
Ghislain MARY committed
131

jehan's avatar
jehan committed
132 133 134 135 136
static void server_process_request_event(void *obj, const belle_sip_request_event_t *event){
	endpoint_t* endpoint = (endpoint_t*)obj;
	belle_sip_server_transaction_t* server_transaction =belle_sip_provider_create_server_transaction(endpoint->provider,belle_sip_request_event_get_request(event));
	belle_sip_request_t* req = belle_sip_transaction_get_request(BELLE_SIP_TRANSACTION(server_transaction));
	belle_sip_response_t* resp;
jehan's avatar
jehan committed
137 138 139
	belle_sip_header_contact_t* contact;
	belle_sip_header_expires_t* expires;
	belle_sip_header_authorization_t* authorization;
140
	belle_sip_header_via_t* via;
jehan's avatar
jehan committed
141 142 143 144 145 146 147 148 149
	const char* raw_authenticate_digest = "WWW-Authenticate: Digest "
			"algorithm=MD5, realm=\"" SIPDOMAIN "\", opaque=\"1bc7f9097684320\"";

	belle_sip_header_www_authenticate_t* www_authenticate=NULL;
	const char* auth_uri;
	const char* qop;
	unsigned char auth_ok=0;
	char local_resp[33];

jehan's avatar
jehan committed
150 151 152 153
	belle_sip_message("caller_process_request_event received [%s] message",belle_sip_request_get_method(belle_sip_request_event_get_request(event)));

	switch (endpoint->auth) {
	case none: {
jehan's avatar
jehan committed
154
		auth_ok=1;
jehan's avatar
jehan committed
155 156
		break;
	}
jehan's avatar
jehan committed
157 158
	case digest_auth:
	case digest: {
159
		if ((authorization=belle_sip_message_get_header_by_type(req,belle_sip_header_authorization_t)) != NULL){
jehan's avatar
jehan committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
			qop=belle_sip_header_authorization_get_qop(authorization);

			if (qop && strcmp(qop,"auth")==0) {
				compute_response_auth_qop(	belle_sip_header_authorization_get_username(authorization)
											,belle_sip_header_authorization_get_realm(authorization)
											,PASSWD
											,endpoint->nonce
											,endpoint->nonce_count
											,belle_sip_header_authorization_get_cnonce(authorization)
											,belle_sip_header_authorization_get_qop(authorization)
											,belle_sip_request_get_method(req)
											,auth_uri=belle_sip_uri_to_string(belle_sip_header_authorization_get_uri(authorization))
											,local_resp);
			} else {
				/*digest*/
				compute_response(belle_sip_header_authorization_get_username(authorization)
						,belle_sip_header_authorization_get_realm(authorization)
						,PASSWD
						,endpoint->nonce
						,belle_sip_request_get_method(req)
						,auth_uri=belle_sip_uri_to_string(belle_sip_header_authorization_get_uri(authorization))
						,local_resp);

			}
			belle_sip_free((void*)auth_uri);
			auth_ok=strcmp(belle_sip_header_authorization_get_response(authorization),local_resp)==0;
		}
jehan's avatar
jehan committed
187 188 189
		if (auth_ok && endpoint->nonce_count<MAX_NC_COUNT ) {/*revoke nonce after MAX_NC_COUNT uses*/
			if (endpoint->auth == digest ) {
				sprintf(endpoint->nonce,"%p",authorization); //*change the nonce for next auth*/
jehan's avatar
jehan committed
190 191 192 193
			} else {
				endpoint->nonce_count++;
			}
		} else {
jehan's avatar
jehan committed
194
			auth_ok=0;
jehan's avatar
jehan committed
195
			www_authenticate=belle_sip_header_www_authenticate_parse(raw_authenticate_digest);
jehan's avatar
jehan committed
196
			sprintf(endpoint->nonce,"%p",authorization); //*change the nonce for next auth*/
jehan's avatar
jehan committed
197 198 199
			belle_sip_header_www_authenticate_set_nonce(www_authenticate,endpoint->nonce);
			if (endpoint->auth == digest_auth) {
				belle_sip_header_www_authenticate_add_qop(www_authenticate,"auth");
jehan's avatar
jehan committed
200 201 202 203
				if (endpoint->nonce_count>=MAX_NC_COUNT) {
					belle_sip_header_www_authenticate_set_stale(www_authenticate,1);
					endpoint->nonce_count=1;
				}
jehan's avatar
jehan committed
204 205 206 207
			}
		}
	}
	break;
jehan's avatar
jehan committed
208 209 210
	default:
		break;
	}
jehan's avatar
jehan committed
211 212 213 214 215
	if (auth_ok) {
		resp=belle_sip_response_create_from_request(belle_sip_request_event_get_request(event),200);
		if (!endpoint->expire_in_contact) {
			belle_sip_message_add_header(BELLE_SIP_MESSAGE(resp),BELLE_SIP_HEADER(expires=belle_sip_message_get_header_by_type(req,belle_sip_header_expires_t)));
		}
jehan's avatar
jehan committed
216 217 218 219 220
		if (strcmp(belle_sip_request_get_method(req),"REGISTER")==0) {
			contact=belle_sip_message_get_header_by_type(req,belle_sip_header_contact_t);
		} else {
			contact=belle_sip_header_contact_new();
		}
221
		if(endpoint->unreconizable_contact) {
222
			/*put an unexpected address*/
223 224
			belle_sip_uri_set_host(belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(contact)),"nimportequoi.com");
		}
jehan's avatar
jehan committed
225
		belle_sip_message_add_header(BELLE_SIP_MESSAGE(resp),BELLE_SIP_HEADER(contact));
jehan's avatar
jehan committed
226
		if (strcmp(belle_sip_request_get_method(req),"PUBLISH")==0) {
227 228 229

			belle_sip_header_t* sip_if_match=belle_sip_message_get_header(BELLE_SIP_MESSAGE(resp),"SIP-If-Match");
			if (sip_if_match) {
230
				CU_ASSERT_STRING_EQUAL(belle_sip_header_extension_get_value(BELLE_SIP_HEADER_EXTENSION(sip_if_match)),"blablietag");
jehan's avatar
jehan committed
231
			}
232 233 234 235 236 237 238
			/*check for body*/
			CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_body(BELLE_SIP_MESSAGE(req)));
			if (belle_sip_message_get_body(BELLE_SIP_MESSAGE(req))) {
				CU_ASSERT_STRING_EQUAL(belle_sip_message_get_body(BELLE_SIP_MESSAGE(req)),publish_body);
			}
			CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header_by_type(req,belle_sip_header_content_type_t));
			CU_ASSERT_PTR_NOT_NULL(belle_sip_message_get_header_by_type(req,belle_sip_header_content_length_t));
jehan's avatar
jehan committed
239 240
			belle_sip_message_add_header(BELLE_SIP_MESSAGE(resp),belle_sip_header_create("SIP-ETag","blablietag"));
		}
jehan's avatar
jehan committed
241 242 243 244 245
	} else {
		resp=belle_sip_response_create_from_request(belle_sip_request_event_get_request(event),401);
		if (www_authenticate)
			belle_sip_message_add_header(BELLE_SIP_MESSAGE(resp),BELLE_SIP_HEADER(www_authenticate));
	}
246 247 248 249
	if (endpoint->received) {
		via=belle_sip_message_get_header_by_type(req,belle_sip_header_via_t);
		belle_sip_header_via_set_received(via,endpoint->received);
	}
jehan's avatar
jehan committed
250 251
	belle_sip_server_transaction_send_response(server_transaction,resp);
}
Ghislain MARY's avatar
Ghislain MARY committed
252

jehan's avatar
jehan committed
253 254 255 256 257 258
static void client_process_response_event(void *obj, const belle_sip_response_event_t *event){
	//belle_sip_client_transaction_t* client_transaction = belle_sip_response_event_get_client_transaction(event);
	endpoint_t* endpoint = (endpoint_t*)obj;
	int status = belle_sip_response_get_status_code(belle_sip_response_event_get_response(event));
	belle_sip_message("caller_process_response_event [%i]",status);
	switch (status) {
259 260 261 262 263 264 265 266 267 268 269 270 271 272
	case 200:
		endpoint->stat.twoHundredOk++;
		if (endpoint->connection_family!=AF_UNSPEC){
			const char *host;
			int family_found;
			belle_sip_header_contact_t *ct=belle_sip_message_get_header_by_type(
				(belle_sip_message_t*)belle_sip_response_event_get_response(event),belle_sip_header_contact_t);
			CU_ASSERT_PTR_NOT_NULL_FATAL(ct);
			host=belle_sip_uri_get_host(belle_sip_header_address_get_uri((belle_sip_header_address_t*)ct));
			if (strchr(host,':')) family_found=AF_INET6;
			else family_found=AF_INET;
			CU_ASSERT_TRUE(family_found==endpoint->connection_family);
		}
		break;
jehan's avatar
jehan committed
273
	case 401:endpoint->stat.fourHundredOne++; break;
jehan's avatar
jehan committed
274 275 276 277 278
	default: break;
	}


}
Ghislain MARY's avatar
Ghislain MARY committed
279

jehan's avatar
jehan committed
280 281 282 283 284 285
//static void process_timeout(void *obj, const belle_sip_timeout_event_t *event){
//	belle_sip_message("process_timeout");
//}
//static void process_transaction_terminated(void *obj, const belle_sip_transaction_terminated_event_t *event){
//	belle_sip_message("process_transaction_terminated");
//}
Ghislain MARY's avatar
Ghislain MARY committed
286

jehan's avatar
jehan committed
287
static void client_process_auth_requested(void *obj, belle_sip_auth_event_t *event){
288
	BELLESIP_UNUSED(obj);
jehan's avatar
jehan committed
289 290 291 292 293
	belle_sip_message("process_auth_requested requested for [%s@%s]"
			,belle_sip_auth_event_get_username(event)
			,belle_sip_auth_event_get_realm(event));
	belle_sip_auth_event_set_passwd(event,PASSWD);
}
jehan's avatar
jehan committed
294

295
static void belle_sip_refresher_listener (belle_sip_refresher_t* refresher
jehan's avatar
jehan committed
296 297 298 299
		,void* user_pointer
		,unsigned int status_code
		,const char* reason_phrase) {
	endpoint_t* endpoint = (endpoint_t*)user_pointer;
300
	BELLESIP_UNUSED(refresher);
jehan's avatar
jehan committed
301 302 303
	belle_sip_message("belle_sip_refresher_listener [%i] reason [%s]",status_code,reason_phrase);
	switch (status_code) {
		case 200:endpoint->stat.refreshOk++; break;
jehan's avatar
jehan committed
304 305 306 307 308 309 310 311 312 313
		default:
			endpoint->stat.refreshKo++;
			break;
	}
	if (endpoint->stat.refreshKo==1 && endpoint->transiant_network_failure) {
		belle_sip_stack_set_send_error(endpoint->stack,0);
	} else 	if (endpoint->stat.refreshOk==1 && endpoint->transiant_network_failure) {
		/*generate a network failure*/
		belle_sip_refresher_set_retry_after(endpoint->refresher,100);
		belle_sip_stack_set_send_error(endpoint->stack,-1);
jehan's avatar
jehan committed
314 315 316
	}
}

317
static endpoint_t* create_endpoint(const char *ip, int port,const char* transport,belle_sip_listener_callbacks_t* listener_callbacks) {
jehan's avatar
jehan committed
318 319 320
	endpoint_t* endpoint = belle_sip_new0(endpoint_t);
	endpoint->stack=belle_sip_stack_new(NULL);
	endpoint->listener_callbacks=listener_callbacks;
321 322
	endpoint->lp=belle_sip_stack_create_listening_point(endpoint->stack,ip,port,transport);
	endpoint->connection_family=AF_INET;
323

324
	if (endpoint->lp) belle_sip_object_ref(endpoint->lp);
325

jehan's avatar
jehan committed
326
	endpoint->provider=belle_sip_stack_create_provider(endpoint->stack,endpoint->lp);
Simon Morlat's avatar
Simon Morlat committed
327
	belle_sip_provider_add_sip_listener(endpoint->provider,(endpoint->listener=belle_sip_listener_create_from_callbacks(endpoint->listener_callbacks,endpoint)));
jehan's avatar
jehan committed
328 329
	sprintf(endpoint->nonce,"%p",endpoint); /*initial nonce*/
	endpoint->nonce_count=1;
330
	endpoint->register_count=3;
jehan's avatar
jehan committed
331 332
	return endpoint;
}
Simon Morlat's avatar
Simon Morlat committed
333

jehan's avatar
jehan committed
334 335 336 337
static void destroy_endpoint(endpoint_t* endpoint) {
	belle_sip_object_unref(endpoint->lp);
	belle_sip_object_unref(endpoint->provider);
	belle_sip_object_unref(endpoint->stack);
Simon Morlat's avatar
Simon Morlat committed
338
	belle_sip_object_unref(endpoint->listener);
jehan's avatar
jehan committed
339 340
	belle_sip_free(endpoint);
}
Simon Morlat's avatar
Simon Morlat committed
341

jehan's avatar
jehan committed
342
static endpoint_t* create_udp_endpoint(int port,belle_sip_listener_callbacks_t* listener_callbacks) {
343 344 345
	endpoint_t *endpoint=create_endpoint("0.0.0.0",port,"udp",listener_callbacks);
	CU_ASSERT_PTR_NOT_NULL_FATAL(endpoint->lp);
	return endpoint;
jehan's avatar
jehan committed
346
}
Ghislain MARY's avatar
Ghislain MARY committed
347

jehan's avatar
jehan committed
348

349 350 351 352 353
static void refresher_base_with_body(endpoint_t* client
										,endpoint_t *server
										, const char* method
										, belle_sip_header_content_type_t* content_type
										,const char* body) {
jehan's avatar
jehan committed
354 355 356
	belle_sip_request_t* req;
	belle_sip_client_transaction_t* trans;
	belle_sip_header_route_t* destination_route;
357
	belle_sip_refresher_t* refresher;
jehan's avatar
jehan committed
358 359 360
	const char* identity = "sip:" USERNAME "@" SIPDOMAIN ;
	const char* domain="sip:" SIPDOMAIN ;
	belle_sip_header_contact_t* contact=belle_sip_header_contact_new();
361
	belle_sip_uri_t *dest_uri;
Ghislain MARY's avatar
Ghislain MARY committed
362 363
	uint64_t begin;
	uint64_t end;
364
	if (client->expire_in_contact) belle_sip_header_contact_set_expires(contact,1);
365

366

367
	dest_uri=(belle_sip_uri_t*)belle_sip_object_clone((belle_sip_object_t*)belle_sip_listening_point_get_uri(server->lp));
368 369 370 371
	if (client->connection_family==AF_INET6)
		belle_sip_uri_set_host(dest_uri,"::1");
	else
		belle_sip_uri_set_host(dest_uri,"127.0.0.1");
372
	destination_route=belle_sip_header_route_create(belle_sip_header_address_create(NULL,dest_uri));
jehan's avatar
jehan committed
373 374 375 376


	req=belle_sip_request_create(
		                    belle_sip_uri_parse(domain),
jehan's avatar
jehan committed
377
		                    method,
jehan's avatar
jehan committed
378
		                    belle_sip_provider_create_call_id(client->provider),
jehan's avatar
jehan committed
379
		                    belle_sip_header_cseq_create(20,method),
jehan's avatar
jehan committed
380 381 382 383
		                    belle_sip_header_from_create2(identity,BELLE_SIP_RANDOM_TAG),
		                    belle_sip_header_to_create2(identity,NULL),
		                    belle_sip_header_via_new(),
		                    70);
jehan's avatar
jehan committed
384
	belle_sip_message_add_header(BELLE_SIP_MESSAGE(req),BELLE_SIP_HEADER(contact));
385
	if (!client->expire_in_contact)
jehan's avatar
jehan committed
386 387
		belle_sip_message_add_header(BELLE_SIP_MESSAGE(req),BELLE_SIP_HEADER(belle_sip_header_expires_create(1)));

jehan's avatar
jehan committed
388
	belle_sip_message_add_header(BELLE_SIP_MESSAGE(req),BELLE_SIP_HEADER(destination_route));
389 390 391 392 393 394
	if (content_type && body) {
		size_t body_lenth=strlen(body);
		belle_sip_message_add_header(BELLE_SIP_MESSAGE(req),BELLE_SIP_HEADER(content_type));
		belle_sip_message_add_header(BELLE_SIP_MESSAGE(req),BELLE_SIP_HEADER(belle_sip_header_content_length_create(body_lenth)));
		belle_sip_message_set_body(BELLE_SIP_MESSAGE(req),body,body_lenth);
	}
jehan's avatar
jehan committed
395
	trans=belle_sip_provider_create_client_transaction(client->provider,req);
jehan's avatar
jehan committed
396
	belle_sip_object_ref(trans);/*to avoid trans from being deleted before refresher can use it*/
jehan's avatar
jehan committed
397
	belle_sip_client_transaction_send_request(trans);
398 399
	if (client->early_refresher) {
		client->refresher= refresher = belle_sip_client_transaction_create_refresher(trans);
jehan's avatar
jehan committed
400
	} else {
401 402 403 404 405
		if (server->auth == none) {
			CU_ASSERT_TRUE(wait_for(server->stack,client->stack,&client->stat.twoHundredOk,1,1000));
		} else {
			CU_ASSERT_TRUE(wait_for(server->stack,client->stack,&client->stat.fourHundredOne,1,1000));
			/*update cseq*/
406
			req=belle_sip_client_transaction_create_authenticated_request(trans,NULL,NULL);
407 408 409 410 411 412 413
			belle_sip_object_unref(trans);
			trans=belle_sip_provider_create_client_transaction(client->provider,req);
			belle_sip_object_ref(trans);
			belle_sip_client_transaction_send_request(trans);
			CU_ASSERT_TRUE_FATAL(wait_for(server->stack,client->stack,&client->stat.twoHundredOk,1,1000));
		}
		client->refresher= refresher = belle_sip_client_transaction_create_refresher(trans);
jehan's avatar
jehan committed
414
	}
Simon Morlat's avatar
Simon Morlat committed
415
	CU_ASSERT_TRUE_FATAL(refresher!=NULL);
jehan's avatar
jehan committed
416
	belle_sip_object_unref(trans);
jehan's avatar
jehan committed
417 418
	belle_sip_refresher_set_listener(refresher,belle_sip_refresher_listener,client);

Ghislain MARY's avatar
Ghislain MARY committed
419
	begin = belle_sip_time_ms();
420
	CU_ASSERT_TRUE(wait_for(server->stack,client->stack,&client->stat.refreshOk,client->register_count+(client->early_refresher?1:0),client->register_count*1000 + 1000));
Ghislain MARY's avatar
Ghislain MARY committed
421
	end = belle_sip_time_ms();
422 423
	CU_ASSERT_TRUE(end-begin>=client->register_count*1000);
	CU_ASSERT_TRUE(end-begin<(client->register_count*1000 + 2000));
jehan's avatar
jehan committed
424 425
	/*unregister twice to make sure refresh operation can be safely cascaded*/
	belle_sip_refresher_refresh(refresher,0);
426
	belle_sip_refresher_refresh(refresher,0);
427
	CU_ASSERT_TRUE(wait_for(server->stack,client->stack,&client->stat.refreshOk,client->register_count+1,1000));
428
	CU_ASSERT_EQUAL(client->stat.refreshOk,client->register_count+1);
jehan's avatar
jehan committed
429 430
	belle_sip_refresher_stop(refresher);
	belle_sip_object_unref(refresher);
431
}
432 433 434
static void refresher_base(endpoint_t* client,endpoint_t *server, const char* method) {
	refresher_base_with_body(client,server,method,NULL,NULL);
}
jehan's avatar
jehan committed
435 436 437
static void register_base(endpoint_t* client,endpoint_t *server) {
	refresher_base(client,server,"REGISTER");
}
438 439 440 441 442 443
static void refresher_base_with_param_and_body(const char* method
												, unsigned char expire_in_contact
												, auth_mode_t auth_mode
												, int early_refresher
												, belle_sip_header_content_type_t* content_type
												,const char* body){
444 445 446 447 448 449 450 451 452 453 454 455
	belle_sip_listener_callbacks_t client_callbacks;
	belle_sip_listener_callbacks_t server_callbacks;
	endpoint_t* client,*server;
	memset(&client_callbacks,0,sizeof(belle_sip_listener_callbacks_t));
	memset(&server_callbacks,0,sizeof(belle_sip_listener_callbacks_t));
	client_callbacks.process_response_event=client_process_response_event;
	client_callbacks.process_auth_requested=client_process_auth_requested;
	server_callbacks.process_request_event=server_process_request_event;
	client = create_udp_endpoint(3452,&client_callbacks);
	server = create_udp_endpoint(6788,&server_callbacks);
	server->expire_in_contact=client->expire_in_contact=expire_in_contact;
	server->auth=auth_mode;
456 457
	client->early_refresher=early_refresher;
	refresher_base_with_body(client,server,method,content_type,body);
jehan's avatar
jehan committed
458 459
	destroy_endpoint(client);
	destroy_endpoint(server);
jehan's avatar
jehan committed
460
}
461 462 463 464
static void refresher_base_with_param(const char* method, unsigned char expire_in_contact,auth_mode_t auth_mode) {
	refresher_base_with_param_and_body(method,expire_in_contact,auth_mode,FALSE,NULL,NULL);

}
jehan's avatar
jehan committed
465 466 467
static void register_test_with_param(unsigned char expire_in_contact,auth_mode_t auth_mode) {
	refresher_base_with_param("REGISTER",expire_in_contact,auth_mode);
}
468
static void subscribe_test(void) {
jehan's avatar
jehan committed
469 470 471 472 473 474 475
	belle_sip_listener_callbacks_t client_callbacks;
	belle_sip_listener_callbacks_t server_callbacks;
	belle_sip_request_t* req;
	belle_sip_client_transaction_t* trans;
	belle_sip_header_route_t* destination_route;
	const char* identity = "sip:" USERNAME "@" SIPDOMAIN ;
	const char* domain="sip:" SIPDOMAIN ;
476 477
	endpoint_t* client,*server;
	belle_sip_uri_t *dest_uri;
478
	belle_sip_refresher_t* refresher;
jehan's avatar
jehan committed
479
	belle_sip_header_contact_t* contact=belle_sip_header_contact_new();
Ghislain MARY's avatar
Ghislain MARY committed
480 481
	uint64_t begin;
	uint64_t end;
jehan's avatar
jehan committed
482 483 484 485 486 487 488
	memset(&client_callbacks,0,sizeof(belle_sip_listener_callbacks_t));
	memset(&server_callbacks,0,sizeof(belle_sip_listener_callbacks_t));

	client_callbacks.process_response_event=client_process_response_event;
	client_callbacks.process_auth_requested=client_process_auth_requested;
	server_callbacks.process_request_event=server_process_request_event;

489 490
	client = create_udp_endpoint(3452,&client_callbacks);
	server = create_udp_endpoint(6788,&server_callbacks);
jehan's avatar
jehan committed
491 492
	server->expire_in_contact=0;
	server->auth=digest_auth;
493

494 495 496
	dest_uri=(belle_sip_uri_t*)belle_sip_object_clone((belle_sip_object_t*)belle_sip_listening_point_get_uri(server->lp));
	belle_sip_uri_set_host(dest_uri,"127.0.0.1");
	destination_route=belle_sip_header_route_create(belle_sip_header_address_create(NULL,dest_uri));
jehan's avatar
jehan committed
497 498 499 500 501 502 503 504 505 506 507 508 509


	req=belle_sip_request_create(
		                    belle_sip_uri_parse(domain),
		                    "SUBSCRIBE",
		                    belle_sip_provider_create_call_id(client->provider),
		                    belle_sip_header_cseq_create(20,"SUBSCRIBE"),
		                    belle_sip_header_from_create2(identity,BELLE_SIP_RANDOM_TAG),
		                    belle_sip_header_to_create2(identity,NULL),
		                    belle_sip_header_via_new(),
		                    70);
	belle_sip_message_add_header(BELLE_SIP_MESSAGE(req),BELLE_SIP_HEADER(contact));
	belle_sip_message_add_header(BELLE_SIP_MESSAGE(req),BELLE_SIP_HEADER(belle_sip_header_expires_create(1)));
jehan's avatar
jehan committed
510
	belle_sip_message_add_header(BELLE_SIP_MESSAGE(req),BELLE_SIP_HEADER(belle_sip_header_create("Event","Presence")));
jehan's avatar
jehan committed
511 512 513 514 515 516 517 518

	belle_sip_message_add_header(BELLE_SIP_MESSAGE(req),BELLE_SIP_HEADER(destination_route));
	trans=belle_sip_provider_create_client_transaction(client->provider,req);
	belle_sip_object_ref(trans);/*to avoid trans from being deleted before refresher can use it*/
	belle_sip_client_transaction_send_request(trans);

	CU_ASSERT_TRUE(wait_for(server->stack,client->stack,&client->stat.fourHundredOne,1,1000));

519
	req=belle_sip_client_transaction_create_authenticated_request(trans,NULL,NULL);
jehan's avatar
jehan committed
520 521 522 523 524 525 526 527
	belle_sip_object_unref(trans);
	trans=belle_sip_provider_create_client_transaction(client->provider,req);
	belle_sip_object_ref(trans);
	belle_sip_client_transaction_send_request(trans);
	CU_ASSERT_TRUE_FATAL(wait_for(server->stack,client->stack,&client->stat.twoHundredOk,1,1000));
	 /*maybe dialog should be automatically created*/
	CU_ASSERT_PTR_NOT_NULL_FATAL(belle_sip_transaction_get_dialog(BELLE_SIP_TRANSACTION(trans)))

528
	refresher = belle_sip_client_transaction_create_refresher(trans);
jehan's avatar
jehan committed
529 530 531
	belle_sip_object_unref(trans);
	belle_sip_refresher_set_listener(refresher,belle_sip_refresher_listener,client);

Ghislain MARY's avatar
Ghislain MARY committed
532
	begin = belle_sip_time_ms();
jehan's avatar
jehan committed
533
	CU_ASSERT_TRUE(wait_for(server->stack,client->stack,&client->stat.refreshOk,3,4000));
Ghislain MARY's avatar
Ghislain MARY committed
534 535 536
	end = belle_sip_time_ms();
	CU_ASSERT_TRUE(end-begin>=3000);
	CU_ASSERT_TRUE(end-begin<5000);
jehan's avatar
jehan committed
537 538 539 540
	/*unsubscribe twice to make sure refresh operation can be safely cascaded*/
	belle_sip_refresher_refresh(refresher,0);
	belle_sip_refresher_refresh(refresher,0);

jehan's avatar
jehan committed
541 542 543 544 545 546
	belle_sip_refresher_stop(refresher);
	belle_sip_object_unref(refresher);
	destroy_endpoint(client);
	destroy_endpoint(server);
}

547
static void register_expires_header(void) {
jehan's avatar
jehan committed
548 549
	register_test_with_param(0,none);
}
Ghislain MARY's avatar
Ghislain MARY committed
550

551
static void register_expires_in_contact(void) {
jehan's avatar
jehan committed
552 553
	register_test_with_param(1,none);
}
Ghislain MARY's avatar
Ghislain MARY committed
554

555
static void register_expires_header_digest(void) {
jehan's avatar
jehan committed
556 557
	register_test_with_param(0,digest);
}
jehan's avatar
jehan committed
558

559
static void register_expires_in_contact_header_digest_auth(void) {
jehan's avatar
jehan committed
560 561
	register_test_with_param(1,digest_auth);
}
562

jehan's avatar
jehan committed
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
static void register_with_failure(void) {
	belle_sip_listener_callbacks_t client_callbacks;
	belle_sip_listener_callbacks_t server_callbacks;
	endpoint_t* client,*server;
	memset(&client_callbacks,0,sizeof(belle_sip_listener_callbacks_t));
	memset(&server_callbacks,0,sizeof(belle_sip_listener_callbacks_t));
	client_callbacks.process_response_event=client_process_response_event;
	client_callbacks.process_auth_requested=client_process_auth_requested;
	server_callbacks.process_request_event=server_process_request_event;
	client = create_udp_endpoint(3452,&client_callbacks);
	server = create_udp_endpoint(6788,&server_callbacks);
	server->expire_in_contact=1;
	server->auth=digest;
	client->transiant_network_failure=1;
	register_base(client,server);
	CU_ASSERT_EQUAL(client->stat.refreshKo,1);
	destroy_endpoint(client);
	destroy_endpoint(server);
}
Ghislain MARY's avatar
Ghislain MARY committed
582
static void register_with_unrecognizable_contact(void) {
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
	belle_sip_listener_callbacks_t client_callbacks;
	belle_sip_listener_callbacks_t server_callbacks;
	endpoint_t* client,*server;
	memset(&client_callbacks,0,sizeof(belle_sip_listener_callbacks_t));
	memset(&server_callbacks,0,sizeof(belle_sip_listener_callbacks_t));
	client_callbacks.process_response_event=client_process_response_event;
	client_callbacks.process_auth_requested=client_process_auth_requested;
	server_callbacks.process_request_event=server_process_request_event;
	client = create_udp_endpoint(3452,&client_callbacks);
	server = create_udp_endpoint(6788,&server_callbacks);
	server->expire_in_contact=1;
	server->unreconizable_contact=1;
	server->auth=digest;
	register_base(client,server);
	destroy_endpoint(client);
	destroy_endpoint(server);
}
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
static void register_early_refresher(void) {
	belle_sip_listener_callbacks_t client_callbacks;
	belle_sip_listener_callbacks_t server_callbacks;
	endpoint_t* client,*server;
	memset(&client_callbacks,0,sizeof(belle_sip_listener_callbacks_t));
	memset(&server_callbacks,0,sizeof(belle_sip_listener_callbacks_t));
	client_callbacks.process_response_event=client_process_response_event;
	client_callbacks.process_auth_requested=client_process_auth_requested;
	server_callbacks.process_request_event=server_process_request_event;
	client = create_udp_endpoint(3452,&client_callbacks);
	server = create_udp_endpoint(6788,&server_callbacks);
	server->expire_in_contact=1;
	server->auth=digest;
	client->early_refresher=1;
	register_base(client,server);
	destroy_endpoint(client);
	destroy_endpoint(server);
}
618

619 620 621 622 623 624 625 626 627 628 629 630
static int register_test_with_interfaces(const char *transport, const char *client_ip, const char *server_ip, int connection_family) {
	int ret=0;
	belle_sip_listener_callbacks_t client_callbacks;
	belle_sip_listener_callbacks_t server_callbacks;
	endpoint_t* client,*server;
	memset(&client_callbacks,0,sizeof(belle_sip_listener_callbacks_t));
	memset(&server_callbacks,0,sizeof(belle_sip_listener_callbacks_t));
	client_callbacks.process_response_event=client_process_response_event;
	client_callbacks.process_auth_requested=client_process_auth_requested;
	server_callbacks.process_request_event=server_process_request_event;
	client = create_endpoint(client_ip,3452,transport,&client_callbacks);
	client->connection_family=connection_family;
631
	client->register_count=1;
632

633 634 635 636 637 638 639 640 641 642 643 644 645
	server = create_endpoint(server_ip,6788,transport,&server_callbacks);
	server->expire_in_contact=client->expire_in_contact=0;
	server->auth=none;

	if (client->lp==NULL || server->lp==NULL){
		belle_sip_warning("Cannot check ipv6 because host has no ipv6 support.");
		ret=-1;
	}else register_base(client,server);
	destroy_endpoint(client);
	destroy_endpoint(server);
	return ret;
}

646 647 648 649 650 651 652 653 654 655 656 657 658
static int register_test_with_random_port(const char *transport, const char *client_ip, const char *server_ip, int connection_family) {
	int ret=0;
	belle_sip_listener_callbacks_t client_callbacks;
	belle_sip_listener_callbacks_t server_callbacks;
	endpoint_t* client,*server;
	memset(&client_callbacks,0,sizeof(belle_sip_listener_callbacks_t));
	memset(&server_callbacks,0,sizeof(belle_sip_listener_callbacks_t));
	client_callbacks.process_response_event=client_process_response_event;
	client_callbacks.process_auth_requested=client_process_auth_requested;
	server_callbacks.process_request_event=server_process_request_event;
	client = create_endpoint(client_ip,-1,transport,&client_callbacks);
	client->connection_family=connection_family;
	client->register_count=1;
659

660 661 662 663 664 665 666 667 668 669 670 671 672
	server = create_endpoint(server_ip,6788,transport,&server_callbacks);
	server->expire_in_contact=client->expire_in_contact=0;
	server->auth=none;

	if (client->lp==NULL || server->lp==NULL){
		belle_sip_warning("Cannot check ipv6 because host has no ipv6 support.");
		ret=-1;
	}else register_base(client,server);
	destroy_endpoint(client);
	destroy_endpoint(server);
	return ret;
}

673
static void register_test_ipv6_to_ipv4(void){
674 675 676 677
	if (!belle_sip_tester_ipv6_available()){
		belle_sip_warning("Test skipped, IPv6 connectivity not available.");
		return;
	}
678 679 680 681
	register_test_with_interfaces("udp","::0","0.0.0.0",AF_INET);
}

static void register_test_ipv4_to_ipv6(void){
682 683 684 685
	if (!belle_sip_tester_ipv6_available()){
		belle_sip_warning("Test skipped, IPv6 connectivity not available.");
		return;
	}
686 687 688 689
	register_test_with_interfaces("udp","0.0.0.0","::0",AF_INET);
}

static void register_test_ipv6_to_ipv6_with_ipv4(void){
690 691 692 693
	if (!belle_sip_tester_ipv6_available()){
		belle_sip_warning("Test skipped, IPv6 connectivity not available.");
		return;
	}
694 695 696 697
	register_test_with_interfaces("udp","::0","::0",AF_INET);
}

static void register_test_ipv6_to_ipv6_with_ipv6(void){
698 699 700 701
	if (!belle_sip_tester_ipv6_available()){
		belle_sip_warning("Test skipped, IPv6 connectivity not available.");
		return;
	}
702 703 704
	register_test_with_interfaces("udp","::0","::0",AF_INET6);
}

705
static void register_tcp_test_ipv6_to_ipv4(void){
706 707 708 709
	if (!belle_sip_tester_ipv6_available()){
		belle_sip_warning("Test skipped, IPv6 connectivity not available.");
		return;
	}
710 711 712 713
	register_test_with_interfaces("tcp","::0","0.0.0.0",AF_INET);
}

static void register_tcp_test_ipv4_to_ipv6(void){
714 715 716 717
	if (!belle_sip_tester_ipv6_available()){
		belle_sip_warning("Test skipped, IPv6 connectivity not available.");
		return;
	}
718 719 720 721
	register_test_with_interfaces("tcp","0.0.0.0","::0",AF_INET);
}

static void register_tcp_test_ipv6_to_ipv6_with_ipv4(void){
722 723 724 725
	if (!belle_sip_tester_ipv6_available()){
		belle_sip_warning("Test skipped, IPv6 connectivity not available.");
		return;
	}
726 727 728 729
	register_test_with_interfaces("tcp","::0","::0",AF_INET);
}

static void register_tcp_test_ipv6_to_ipv6_with_ipv6(void){
730 731 732 733
	if (!belle_sip_tester_ipv6_available()){
		belle_sip_warning("Test skipped, IPv6 connectivity not available.");
		return;
	}
734 735 736
	register_test_with_interfaces("tcp","::0","::0",AF_INET6);
}

737 738 739 740 741 742 743 744 745 746 747 748
static void register_udp_test_ipv4_random_port(void){
	register_test_with_random_port("udp","0.0.0.0","0.0.0.0",AF_INET);
}

static void register_udp_test_ipv6_random_port(void){
	if (!belle_sip_tester_ipv6_available()){
		belle_sip_warning("Test skipped, IPv6 connectivity not available.");
		return;
	}
	register_test_with_random_port("udp","::0","0.0.0.0",AF_INET);
}

749 750 751 752 753 754 755 756 757 758 759 760
static void register_tcp_test_ipv4_random_port(void){
	register_test_with_random_port("tcp","0.0.0.0","0.0.0.0",AF_INET);
}

static void register_tcp_test_ipv6_random_port(void){
	if (!belle_sip_tester_ipv6_available()){
		belle_sip_warning("Test skipped, IPv6 connectivity not available.");
		return;
	}
	register_test_with_random_port("tcp","::0","0.0.0.0",AF_INET);
}

jehan's avatar
jehan committed
761
static void simple_publish() {
762 763 764 765 766 767 768 769
	belle_sip_header_content_type_t* content_type=belle_sip_header_content_type_create("application","pidf+xml");
	refresher_base_with_param_and_body("PUBLISH",FALSE,TRUE,FALSE, content_type,publish_body);

}
static void simple_publish_with_early_refresher() {
	belle_sip_header_content_type_t* content_type=belle_sip_header_content_type_create("application","pidf+xml");
	refresher_base_with_param_and_body("PUBLISH",FALSE,TRUE,TRUE, content_type,publish_body);

jehan's avatar
jehan committed
770
}
771

Ghislain MARY's avatar
Ghislain MARY committed
772 773 774 775 776
test_t refresher_tests[] = {
	{ "REGISTER Expires header", register_expires_header },
	{ "REGISTER Expires in Contact", register_expires_in_contact },
	{ "REGISTER Expires header digest", register_expires_header_digest },
	{ "REGISTER Expires in Contact digest auth", register_expires_in_contact_header_digest_auth },
jehan's avatar
jehan committed
777
	{ "REGISTER with failure", register_with_failure },
778
	{ "REGISTER with early refresher",register_early_refresher},
Ghislain MARY's avatar
Ghislain MARY committed
779
	{ "SUBSCRIBE", subscribe_test },
jehan's avatar
jehan committed
780
	{ "PUBLISH", simple_publish },
781
	{ "PUBLISH with early refresher", simple_publish_with_early_refresher },
Ghislain MARY's avatar
Ghislain MARY committed
782
	{ "REGISTER with unrecognizable Contact", register_with_unrecognizable_contact },
783 784 785
	{ "REGISTER UDP from ipv6 to ipv4", register_test_ipv6_to_ipv4 },
	{ "REGISTER UDP from ipv4 to ipv6", register_test_ipv4_to_ipv6 },
	{ "REGISTER UDP from ipv6 to ipv6 with ipv4", register_test_ipv6_to_ipv6_with_ipv4 },
786 787 788 789
	{ "REGISTER UDP from ipv6 to ipv6 with ipv6", register_test_ipv6_to_ipv6_with_ipv6 },
	{ "REGISTER TCP from ipv6 to ipv4", register_tcp_test_ipv6_to_ipv4 },
	{ "REGISTER TCP from ipv4 to ipv6", register_tcp_test_ipv4_to_ipv6 },
	{ "REGISTER TCP from ipv6 to ipv6 with ipv4", register_tcp_test_ipv6_to_ipv6_with_ipv4 },
790 791 792
	{ "REGISTER TCP from ipv6 to ipv6 with ipv6", register_tcp_test_ipv6_to_ipv6_with_ipv6 },
	{ "REGISTER UDP from random port using AF_INET", register_udp_test_ipv4_random_port },
	{ "REGISTER UDP from random port using AF_INET6", register_udp_test_ipv6_random_port },
793 794
	{ "REGISTER TCP from random port using AF_INET", register_tcp_test_ipv4_random_port },
	{ "REGISTER TCP from random port using AF_INET6", register_tcp_test_ipv6_random_port },
Ghislain MARY's avatar
Ghislain MARY committed
795 796 797 798 799 800 801 802 803
};

test_suite_t refresher_test_suite = {
	"Refresher",
	NULL,
	NULL,
	sizeof(refresher_tests) / sizeof(refresher_tests[0]),
	refresher_tests
};
jehan's avatar
jehan committed
804