register_tester.c 35 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
Simon Morlat's avatar
Simon Morlat committed
7
    the Free Software Foundation, either version 2 of the License, or
jehan's avatar
jehan committed
8 9 10 11 12 13 14 15 16 17
    (at your option) any later version.

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

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

19

jehan's avatar
jehan committed
20 21 22 23
#include "linphonecore.h"
#include "private.h"
#include "liblinphone_tester.h"

24 25 26 27 28 29 30 31 32 33


static void auth_info_requested(LinphoneCore *lc, const char *realm, const char *username, const char *domain) {
	LinphoneAuthInfo *info;
	info=linphone_auth_info_new(test_username,NULL,test_password,NULL,realm,domain); /*create authentication structure from identity*/
	linphone_core_add_auth_info(lc,info); /*add authentication info to LinphoneCore*/
}



34
static LinphoneCoreManager* create_lcm_with_auth(unsigned int with_auth) {
35
	LinphoneCoreManager* lcm=linphone_core_manager_new(NULL);
36

37
	if (with_auth) {
38
		LinphoneCoreVTable* vtable = linphone_core_v_table_new();
39
		vtable->auth_info_requested=auth_info_requested;
40
		linphone_core_add_listener(lcm->lc,vtable);
41
	}
42

43
	/*to allow testing with 127.0.0.1*/
44 45
	linphone_core_set_network_reachable(lcm->lc,TRUE);
	return lcm;
46
}
jehan's avatar
jehan committed
47

48 49
static LinphoneCoreManager* create_lcm() {
	return create_lcm_with_auth(0);
jehan's avatar
jehan committed
50 51 52
}

void registration_state_changed(struct _LinphoneCore *lc, LinphoneProxyConfig *cfg, LinphoneRegistrationState cstate, const char *message){
53
		stats* counters;
jehan's avatar
jehan committed
54 55 56 57
		ms_message("New registration state %s for user id [%s] at proxy [%s]\n"
				,linphone_registration_state_to_string(cstate)
				,linphone_proxy_config_get_identity(cfg)
				,linphone_proxy_config_get_addr(cfg));
58
		counters = get_stats(lc);
jehan's avatar
jehan committed
59 60 61 62 63 64 65
		switch (cstate) {
		case LinphoneRegistrationNone:counters->number_of_LinphoneRegistrationNone++;break;
		case LinphoneRegistrationProgress:counters->number_of_LinphoneRegistrationProgress++;break;
		case LinphoneRegistrationOk:counters->number_of_LinphoneRegistrationOk++;break;
		case LinphoneRegistrationCleared:counters->number_of_LinphoneRegistrationCleared++;break;
		case LinphoneRegistrationFailed:counters->number_of_LinphoneRegistrationFailed++;break;
		default:
66
			BC_FAIL("unexpected event");break;
jehan's avatar
jehan committed
67 68 69
		}

}
Ghislain MARY's avatar
Ghislain MARY committed
70

71 72 73 74 75 76 77
static void register_with_refresh_base_3(LinphoneCore* lc
											, bool_t refresh
											,const char* domain
											,const char* route
											,bool_t late_auth_info
											,LCSipTransports transport
											,LinphoneRegistrationState expected_final_state) {
jehan's avatar
jehan committed
78
	int retry=0;
Simon Morlat's avatar
Simon Morlat committed
79
	char* addr;
80 81 82 83 84 85
	LinphoneProxyConfig* proxy_cfg;
	stats* counters;
	LinphoneAddress *from;
	const char* server_addr;
	LinphoneAuthInfo *info;

86
	BC_ASSERT_PTR_NOT_NULL(lc);
jehan's avatar
jehan committed
87
	if (!lc) return;
88

89
	counters = get_stats(lc);
jehan's avatar
jehan committed
90 91 92 93 94
	reset_counters(counters);
	linphone_core_set_sip_transports(lc,&transport);

	proxy_cfg = linphone_proxy_config_new();

95
	from = create_linphone_address(domain);
jehan's avatar
jehan committed
96

97 98
	linphone_proxy_config_set_identity(proxy_cfg,addr=linphone_address_as_string(from));
	ms_free(addr);
99
	server_addr = linphone_address_get_domain(from);
jehan's avatar
jehan committed
100 101

	linphone_proxy_config_enable_register(proxy_cfg,TRUE);
102
	linphone_proxy_config_set_expires(proxy_cfg,1);
jehan's avatar
jehan committed
103 104 105 106 107 108 109 110 111 112 113
	if (route) {
		linphone_proxy_config_set_route(proxy_cfg,route);
		linphone_proxy_config_set_server_addr(proxy_cfg,route);
	} else {
		linphone_proxy_config_set_server_addr(proxy_cfg,server_addr);
	}
	linphone_address_destroy(from);

	linphone_core_add_proxy_config(lc,proxy_cfg);
	linphone_core_set_default_proxy(lc,proxy_cfg);

114
	while (counters->number_of_LinphoneRegistrationOk<1+(refresh!=0)
115
			&& retry++ <(1100 /*only wait 11 s if final state is progress*/+(expected_final_state==LinphoneRegistrationProgress?0:2000))) {
jehan's avatar
jehan committed
116
		linphone_core_iterate(lc);
jehan's avatar
jehan committed
117
		if (counters->number_of_auth_info_requested>0 && linphone_proxy_config_get_state(proxy_cfg) == LinphoneRegistrationFailed && late_auth_info) {
118
			if (!linphone_core_get_auth_info_list(lc)) {
119
				BC_ASSERT_EQUAL(linphone_proxy_config_get_error(proxy_cfg),LinphoneReasonUnauthorized, int, "%d");
120
				info=linphone_auth_info_new(test_username,NULL,test_password,NULL,auth_domain,NULL); /*create authentication structure from identity*/
121 122
				linphone_core_add_auth_info(lc,info); /*add authentication info to LinphoneCore*/
			}
jehan's avatar
jehan committed
123
		}
124 125
		if (linphone_proxy_config_get_error(proxy_cfg) == LinphoneReasonBadCredentials
				|| (counters->number_of_auth_info_requested>2 &&linphone_proxy_config_get_error(proxy_cfg) == LinphoneReasonUnauthorized)) /*no need to continue if auth cannot be found*/
126
			break; /*no need to continue*/
127
		ms_usleep(10000);
jehan's avatar
jehan committed
128
	}
129 130 131
	BC_ASSERT_EQUAL(linphone_proxy_config_is_registered(proxy_cfg),(expected_final_state == LinphoneRegistrationOk), int, "%d");
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationNone,0, int, "%d");
	BC_ASSERT_TRUE(counters->number_of_LinphoneRegistrationProgress>=1);
132
	if (expected_final_state == LinphoneRegistrationOk) {
133 134
		BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationOk,1+(refresh!=0), int, "%d");
		BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationFailed,late_auth_info?1:0, int, "%d");
135 136
	} else
		/*checking to be done outside this functions*/
137
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationCleared,0, int, "%d");
jehan's avatar
jehan committed
138 139

}
Ghislain MARY's avatar
Ghislain MARY committed
140

141 142 143 144 145 146 147 148
static void register_with_refresh_base_2(LinphoneCore* lc
											, bool_t refresh
											,const char* domain
											,const char* route
											,bool_t late_auth_info
											,LCSipTransports transport) {
	register_with_refresh_base_3(lc, refresh, domain, route, late_auth_info, transport,LinphoneRegistrationOk );
}
jehan's avatar
jehan committed
149
static void register_with_refresh_base(LinphoneCore* lc, bool_t refresh,const char* domain,const char* route) {
150 151
	LCSipTransports transport = {5070,5070,0,5071};
	register_with_refresh_base_2(lc,refresh,domain,route,FALSE,transport);
jehan's avatar
jehan committed
152
}
Ghislain MARY's avatar
Ghislain MARY committed
153

154 155 156 157
static void register_with_refresh(LinphoneCoreManager* lcm, bool_t refresh,const char* domain,const char* route) {
	stats* counters = &lcm->stat;
	register_with_refresh_base(lcm->lc,refresh,domain,route);
	linphone_core_manager_stop(lcm);
158
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationCleared,1, int, "%d");
jehan's avatar
jehan committed
159 160
}

161
static void register_with_refresh_with_send_error() {
jehan's avatar
jehan committed
162
	int retry=0;
163 164
	LinphoneCoreManager* lcm = create_lcm_with_auth(1);
	stats* counters = &lcm->stat;
165
	LinphoneAuthInfo *info=linphone_auth_info_new(test_username,NULL,test_password,NULL,auth_domain,NULL); /*create authentication structure from identity*/
166 167
	char route[256];
	sprintf(route,"sip:%s",test_route);
168
	linphone_core_add_auth_info(lcm->lc,info); /*add authentication info to LinphoneCore*/
jehan's avatar
jehan committed
169

170
	register_with_refresh_base(lcm->lc,TRUE,auth_domain,route);
jehan's avatar
jehan committed
171
	/*simultate a network error*/
172
	sal_set_send_error(lcm->lc->sal, -1);
173
	while (counters->number_of_LinphoneRegistrationProgress<2 && retry++ <200) {
174
			linphone_core_iterate(lcm->lc);
175
			ms_usleep(10000);
jehan's avatar
jehan committed
176
	}
177 178
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationFailed,0, int, "%d");
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationProgress,2, int, "%d");
jehan's avatar
jehan committed
179

180
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationCleared,0, int, "%d");
jehan's avatar
jehan committed
181

182
	linphone_core_manager_destroy(lcm);
jehan's avatar
jehan committed
183
}
Ghislain MARY's avatar
Ghislain MARY committed
184

jehan's avatar
jehan committed
185
static void simple_register(){
186 187 188
	LinphoneCoreManager* lcm = create_lcm();
	stats* counters = &lcm->stat;
	register_with_refresh(lcm,FALSE,NULL,NULL);
189
	BC_ASSERT_EQUAL(counters->number_of_auth_info_requested,0, int, "%d");
190
	linphone_core_manager_destroy(lcm);
jehan's avatar
jehan committed
191 192
}

193 194 195 196 197
static void register_with_custom_headers(void){
	LinphoneCoreManager *marie=linphone_core_manager_new("marie_rc");
	LinphoneProxyConfig *cfg=linphone_core_get_default_proxy_config(marie->lc);
	int initial_register_ok=marie->stat.number_of_LinphoneRegistrationOk;
	const char *value;
198

199 200 201 202
	linphone_core_set_network_reachable(marie->lc, FALSE);
	linphone_proxy_config_set_custom_header(cfg, "ah-bah-ouais", "...mais bon.");
	/*unfortunately it is difficult to programmatically check that sent custom headers are actually sent.
	 * A server development would be required here.*/
203

204 205 206
	linphone_core_set_network_reachable(marie->lc, TRUE);
	wait_for(marie->lc, NULL, &marie->stat.number_of_LinphoneRegistrationOk,initial_register_ok+1);
	value=linphone_proxy_config_get_custom_header(cfg, "Server");
207 208
	BC_ASSERT_PTR_NOT_NULL(value);
	if (value) BC_ASSERT_TRUE(strstr(value, "Flexisip")!=NULL);
209 210 211
	linphone_core_manager_destroy(marie);
}

212 213 214 215 216 217 218 219 220 221 222 223
static void simple_unregister(){
	LinphoneCoreManager* lcm = create_lcm();
	stats* counters = &lcm->stat;
	LinphoneProxyConfig* proxy_config;
	register_with_refresh_base(lcm->lc,FALSE,NULL,NULL);

	linphone_core_get_default_proxy(lcm->lc,&proxy_config);

	linphone_proxy_config_edit(proxy_config);
	reset_counters(counters); /*clear stats*/

	/*nothing is supposed to arrive until done*/
224
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
225 226
	linphone_proxy_config_enable_register(proxy_config,FALSE);
	linphone_proxy_config_done(proxy_config);
227
	BC_ASSERT_TRUE(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1));
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	linphone_core_manager_destroy(lcm);
}

static void change_expires(){
	LinphoneCoreManager* lcm = create_lcm();
	stats* counters = &lcm->stat;
	LinphoneProxyConfig* proxy_config;
	register_with_refresh_base(lcm->lc,FALSE,NULL,NULL);

	linphone_core_get_default_proxy(lcm->lc,&proxy_config);

	linphone_proxy_config_edit(proxy_config);
	reset_counters(counters); /*clear stats*/

	/*nothing is supposed to arrive until done*/
243
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
244 245 246 247

	linphone_proxy_config_set_expires(proxy_config,3);

	linphone_proxy_config_done(proxy_config);
248
	BC_ASSERT_TRUE(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationOk,1));
249
	/*wait 2s without receive refresh*/
250
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationOk,2,2000));
251
	/* now, it should be ok*/
252
	BC_ASSERT_TRUE(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationOk,2));
253 254 255 256 257


	linphone_core_manager_destroy(lcm);
}

jehan's avatar
jehan committed
258 259
/*take care of min expires configuration from server*/
static void simple_register_with_refresh() {
260 261 262
	LinphoneCoreManager* lcm = create_lcm();
	stats* counters = &lcm->stat;
	register_with_refresh(lcm,TRUE,NULL,NULL);
263
	BC_ASSERT_EQUAL(counters->number_of_auth_info_requested,0, int, "%d");
264
	linphone_core_manager_destroy(lcm);
jehan's avatar
jehan committed
265 266 267
}

static void simple_auth_register_with_refresh() {
268 269
	LinphoneCoreManager* lcm = create_lcm_with_auth(1);
	stats* counters = &lcm->stat;
270 271
	char route[256];
	sprintf(route,"sip:%s",test_route);
272
	register_with_refresh(lcm,TRUE,auth_domain,route);
273
	BC_ASSERT_EQUAL(counters->number_of_auth_info_requested,1, int, "%d");
274
	linphone_core_manager_destroy(lcm);
jehan's avatar
jehan committed
275 276 277 278
}

static void simple_tcp_register(){
	char route[256];
279
	LinphoneCoreManager* lcm;
280
	sprintf(route,"sip:%s;transport=tcp",test_route);
281 282 283
	lcm = create_lcm();
	register_with_refresh(lcm,FALSE,test_domain,route);
	linphone_core_manager_destroy(lcm);
jehan's avatar
jehan committed
284
}
Ghislain MARY's avatar
Ghislain MARY committed
285

286
static void simple_tcp_register_compatibility_mode(){
287
	char route[256];
288
	LinphoneCoreManager* lcm;
289
	LCSipTransports transport = {0,5070,0,0};
290
	sprintf(route,"sip:%s",test_route);
291 292 293
	lcm = create_lcm();
	register_with_refresh_base_2(lcm->lc,FALSE,test_domain,route,FALSE,transport);
	linphone_core_manager_destroy(lcm);
294 295
}

jehan's avatar
jehan committed
296
static void simple_tls_register(){
297 298 299
	if (transport_supported(LinphoneTransportTls)) {
		char route[256];
		LinphoneCoreManager* lcm = create_lcm();
300 301
		sprintf(route,"sip:%s;transport=tls",test_route);
		register_with_refresh(lcm,FALSE,test_domain,route);
302
		linphone_core_manager_destroy(lcm);
303
	}
jehan's avatar
jehan committed
304 305
}

306

jehan's avatar
jehan committed
307
static void simple_authenticated_register(){
308
	stats* counters;
309
	LinphoneCoreManager* lcm = create_lcm();
310
	LinphoneAuthInfo *info=linphone_auth_info_new(test_username,NULL,test_password,NULL,auth_domain,NULL); /*create authentication structure from identity*/
311 312
	char route[256];
	sprintf(route,"sip:%s",test_route);
313 314 315
	linphone_core_add_auth_info(lcm->lc,info); /*add authentication info to LinphoneCore*/
	counters = &lcm->stat;
	register_with_refresh(lcm,FALSE,auth_domain,route);
316
	BC_ASSERT_EQUAL(counters->number_of_auth_info_requested,0, int, "%d");
317
	linphone_core_manager_destroy(lcm);
jehan's avatar
jehan committed
318 319
}

320 321
static void ha1_authenticated_register(){
	stats* counters;
322
	LinphoneCoreManager* lcm = create_lcm();
323 324 325 326
	char ha1[33];
	LinphoneAuthInfo *info;
	char route[256];
	sal_auth_compute_ha1(test_username,auth_domain,test_password,ha1);
327
	info=linphone_auth_info_new(test_username,NULL,NULL,ha1,auth_domain,NULL); /*create authentication structure from identity*/
328
	sprintf(route,"sip:%s",test_route);
329 330 331
	linphone_core_add_auth_info(lcm->lc,info); /*add authentication info to LinphoneCore*/
	counters = &lcm->stat;
	register_with_refresh(lcm,FALSE,auth_domain,route);
332
	BC_ASSERT_EQUAL(counters->number_of_auth_info_requested,0, int, "%d");
333
	linphone_core_manager_destroy(lcm);
334 335
}

jehan's avatar
jehan committed
336
static void authenticated_register_with_no_initial_credentials(){
337
	LinphoneCoreManager *lcm;
338
	LinphoneCoreVTable* vtable = linphone_core_v_table_new();
339
	stats* counters;
340
	char route[256];
341

342
	sprintf(route,"sip:%s",test_route);
343

344
	lcm = linphone_core_manager_new(NULL);
345

346
	vtable->auth_info_requested=auth_info_requested;
347
	linphone_core_add_listener(lcm->lc,vtable);
348

349
	counters= get_stats(lcm->lc);
jehan's avatar
jehan committed
350
	counters->number_of_auth_info_requested=0;
351
	register_with_refresh(lcm,FALSE,auth_domain,route);
352
	BC_ASSERT_EQUAL(counters->number_of_auth_info_requested,1, int, "%d");
353
	linphone_core_manager_destroy(lcm);
jehan's avatar
jehan committed
354
}
Ghislain MARY's avatar
Ghislain MARY committed
355

jehan's avatar
jehan committed
356 357

static void authenticated_register_with_late_credentials(){
358
	LinphoneCoreManager *lcm;
359
	stats* counters;
360
	LCSipTransports transport = {5070,5070,0,5071};
361
	char route[256];
362

363
	sprintf(route,"sip:%s",test_route);
364

365
	lcm =  linphone_core_manager_new(NULL);
366

367 368
	counters = get_stats(lcm->lc);
	register_with_refresh_base_2(lcm->lc,FALSE,auth_domain,route,TRUE,transport);
369
	BC_ASSERT_EQUAL(counters->number_of_auth_info_requested,1, int, "%d");
370
	linphone_core_manager_destroy(lcm);
jehan's avatar
jehan committed
371
}
jehan's avatar
jehan committed
372

373
static void authenticated_register_with_wrong_late_credentials(){
374
	LinphoneCoreManager *lcm;
375 376 377 378
	stats* counters;
	LCSipTransports transport = {5070,5070,0,5071};
	char route[256];
	const char* saved_test_passwd=test_password;
379
	char* wrong_passwd="mot de pass tout pourri";
380 381 382 383 384

	test_password=wrong_passwd;

	sprintf(route,"sip:%s",test_route);

385
	lcm =  linphone_core_manager_new(NULL);
386

387 388
	counters = get_stats(lcm->lc);
	register_with_refresh_base_3(lcm->lc,FALSE,auth_domain,route,TRUE,transport,LinphoneRegistrationFailed);
389 390 391
	BC_ASSERT_EQUAL(counters->number_of_auth_info_requested,2, int, "%d");
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationFailed,2, int, "%d");
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationProgress,2, int, "%d");
392 393
	test_password=saved_test_passwd;

394
	linphone_core_manager_destroy(lcm);
395 396
}

397
static void authenticated_register_with_wrong_credentials_with_params_base(const char* user_agent,LinphoneCoreManager *lcm) {
jehan's avatar
jehan committed
398 399
	stats* counters;
	LCSipTransports transport = {5070,5070,0,5071};
400
	LinphoneAuthInfo *info=linphone_auth_info_new(test_username,NULL,"wrong passwd",NULL,auth_domain,NULL); /*create authentication structure from identity*/
jehan's avatar
jehan committed
401
	char route[256];
402

jehan's avatar
jehan committed
403
	sprintf(route,"sip:%s",test_route);
404

405
	sal_set_refresher_retry_after(lcm->lc->sal,500);
406
	if (user_agent) {
407
		linphone_core_set_user_agent(lcm->lc,user_agent,NULL);
408
	}
409 410 411
	linphone_core_add_auth_info(lcm->lc,info); /*add wrong authentication info to LinphoneCore*/
	counters = get_stats(lcm->lc);
	register_with_refresh_base_3(lcm->lc,TRUE,auth_domain,route,FALSE,transport,LinphoneRegistrationFailed);
412
	//BC_ASSERT_EQUAL(counters->number_of_auth_info_requested,3, int, "%d"); register_with_refresh_base_3 does not alow to precisely check number of number_of_auth_info_requested
413
	/*wait for retry*/
414
	BC_ASSERT_TRUE(wait_for(lcm->lc,lcm->lc,&counters->number_of_auth_info_requested,4));
415
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationFailed,1, int, "%d");
416

417 418 419
	/*check the detailed error info */
	if (!user_agent || strcmp(user_agent,"tester-no-403")!=0){
		LinphoneProxyConfig *cfg=NULL;
420
		linphone_core_get_default_proxy(lcm->lc,&cfg);
421
		BC_ASSERT_PTR_NOT_NULL(cfg);
422 423 424
		if (cfg){
			const LinphoneErrorInfo *ei=linphone_proxy_config_get_error_info(cfg);
			const char *phrase=linphone_error_info_get_phrase(ei);
425 426 427 428
			BC_ASSERT_PTR_NOT_NULL(phrase);
			if (phrase) BC_ASSERT_TRUE(strcmp(phrase,"Forbidden")==0);
			BC_ASSERT_EQUAL(linphone_error_info_get_protocol_code(ei),403, int, "%d");
			BC_ASSERT_PTR_NULL(linphone_error_info_get_details(ei));
429
		}
430

431
	}
432 433
	}
static void authenticated_register_with_wrong_credentials_with_params(const char* user_agent) {
434 435 436
	LinphoneCoreManager *lcm = linphone_core_manager_new(NULL);
	authenticated_register_with_wrong_credentials_with_params_base(user_agent,lcm);
	linphone_core_manager_destroy(lcm);
jehan's avatar
jehan committed
437
}
438 439 440
static void authenticated_register_with_wrong_credentials() {
	authenticated_register_with_wrong_credentials_with_params(NULL);
}
441
static void authenticated_register_with_wrong_credentials_2() {
442 443
	LinphoneCoreManager *lcm = linphone_core_manager_new(NULL);
	stats* counters = get_stats(lcm->lc);
444 445 446
	int current_in_progress;
	LinphoneProxyConfig* proxy;

447
	authenticated_register_with_wrong_credentials_with_params_base(NULL,lcm);
448

449
	linphone_core_get_default_proxy(lcm->lc,&proxy);
450 451 452 453 454
	/*Make sure registration attempts are stopped*/
	linphone_proxy_config_edit(proxy);
	linphone_proxy_config_enable_register(proxy,FALSE);
	linphone_proxy_config_done(proxy);
	current_in_progress=counters->number_of_LinphoneRegistrationProgress;
455
	BC_ASSERT_FALSE(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationProgress,current_in_progress+1));
456

457
	linphone_core_manager_destroy(lcm);
458
}
459 460 461
static void authenticated_register_with_wrong_credentials_without_403() {
	authenticated_register_with_wrong_credentials_with_params("tester-no-403");
}
462
static LinphoneCoreManager* configure_lcm(void) {
463 464
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager *lcm=linphone_core_manager_new2( "multi_account_rc", FALSE);
465 466 467 468 469 470
		stats *counters=&lcm->stat;
		BC_ASSERT_TRUE(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationOk,ms_list_size(linphone_core_get_proxy_config_list(lcm->lc))));
		BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationFailed,0, int, "%d");
		return lcm;
	}
	return NULL;
jehan's avatar
jehan committed
471 472 473
}

static void multiple_proxy(){
474 475 476 477
	LinphoneCoreManager *lcm=configure_lcm();
	if (lcm) {
		linphone_core_manager_destroy(lcm);
	}
jehan's avatar
jehan committed
478 479
}

jehan's avatar
jehan committed
480 481
static void network_state_change(){
	int register_ok;
482
	stats *counters;
483 484 485
	LinphoneCoreManager *lcm=configure_lcm();
	if (lcm) {
		LinphoneCore *lc=lcm->lc;
486

487 488 489 490 491 492 493 494 495 496 497
		counters = get_stats(lc);
		register_ok=counters->number_of_LinphoneRegistrationOk;
		linphone_core_set_network_reachable(lc,FALSE);
		BC_ASSERT_TRUE(wait_for(lc,lc,&counters->number_of_NetworkReachableFalse,1));
		BC_ASSERT_TRUE(wait_for(lc,lc,&counters->number_of_LinphoneRegistrationNone,register_ok));
		linphone_core_set_network_reachable(lc,TRUE);
		BC_ASSERT_TRUE(wait_for(lc,lc,&counters->number_of_NetworkReachableTrue,1));
		wait_for(lc,lc,&counters->number_of_LinphoneRegistrationOk,2*register_ok);

		linphone_core_manager_destroy(lcm);
	}
jehan's avatar
jehan committed
498
}
jehan's avatar
jehan committed
499 500 501 502 503 504 505 506 507 508 509
static int get_number_of_udp_proxy(const LinphoneCore* lc) {
	int number_of_udp_proxy=0;
	LinphoneProxyConfig* proxy_cfg;
	MSList* proxys;
	for (proxys=(MSList*)linphone_core_get_proxy_config_list(lc);proxys!=NULL;proxys=proxys->next) {
			proxy_cfg=(LinphoneProxyConfig*)proxys->data;
			if (strcmp("udp",linphone_proxy_config_get_transport(proxy_cfg))==0)
				number_of_udp_proxy++;
	}
	return number_of_udp_proxy;
}
510
static void transport_change(){
511
	LinphoneCoreManager *lcm;
512 513 514 515 516
	LinphoneCore* lc;
	int register_ok;
	stats* counters ;
	LCSipTransports sip_tr;
	LCSipTransports sip_tr_orig;
jehan's avatar
jehan committed
517
	int number_of_udp_proxy=0;
jehan's avatar
jehan committed
518
	int total_number_of_proxies;
519

520 521 522 523 524 525
	lcm=configure_lcm();
	if (lcm) {
		memset(&sip_tr,0,sizeof(sip_tr));
		lc=lcm->lc;
		counters = get_stats(lc);
		register_ok=counters->number_of_LinphoneRegistrationOk;
jehan's avatar
jehan committed
526

527 528 529
		number_of_udp_proxy=get_number_of_udp_proxy(lc);
		total_number_of_proxies=ms_list_size(linphone_core_get_proxy_config_list(lc));
		linphone_core_get_sip_transports(lc,&sip_tr_orig);
jehan's avatar
jehan committed
530

531
		sip_tr.udp_port=sip_tr_orig.udp_port;
532

533 534 535
		/*keep only udp*/
		linphone_core_set_sip_transports(lc,&sip_tr);
		BC_ASSERT_TRUE(wait_for(lc,lc,&counters->number_of_LinphoneRegistrationOk,register_ok+number_of_udp_proxy));
536

537
		BC_ASSERT_TRUE(wait_for(lc,lc,&counters->number_of_LinphoneRegistrationFailed,total_number_of_proxies-number_of_udp_proxy));
538

539 540
		linphone_core_manager_destroy(lcm);
	}
541 542
}

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
static void proxy_transport_change(){
	LinphoneCoreManager* lcm = create_lcm();
	stats* counters = &lcm->stat;
	LinphoneProxyConfig* proxy_config;
	LinphoneAddress* addr;
	char* addr_as_string;
	LinphoneAuthInfo *info=linphone_auth_info_new(test_username,NULL,test_password,NULL,auth_domain,NULL); /*create authentication structure from identity*/
	linphone_core_add_auth_info(lcm->lc,info); /*add authentication info to LinphoneCore*/

	register_with_refresh_base(lcm->lc,FALSE,auth_domain,NULL);

	linphone_core_get_default_proxy(lcm->lc,&proxy_config);
	reset_counters(counters); /*clear stats*/
	linphone_proxy_config_edit(proxy_config);

558
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
559 560 561 562 563 564 565 566 567 568 569
	addr = linphone_address_new(linphone_proxy_config_get_addr(proxy_config));

	if (LinphoneTransportTcp == linphone_address_get_transport(addr)) {
		linphone_address_set_transport(addr,LinphoneTransportUdp);
	} else {
		linphone_address_set_transport(addr,LinphoneTransportTcp);
	}
	linphone_proxy_config_set_server_addr(proxy_config,addr_as_string=linphone_address_as_string(addr));

	linphone_proxy_config_done(proxy_config);

570
	BC_ASSERT(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationOk,1));
571
	/*as we change p[roxy server destination, we should'nt be notified about the clear*/
572
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationCleared,0, int, "%d");
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
	ms_free(addr_as_string);
	linphone_address_destroy(addr);
	linphone_core_manager_destroy(lcm);

}
static void proxy_transport_change_with_wrong_port() {
	LinphoneCoreManager* lcm = create_lcm();
	stats* counters = &lcm->stat;
	LinphoneProxyConfig* proxy_config;
	LinphoneAuthInfo *info=linphone_auth_info_new(test_username,NULL,test_password,NULL,auth_domain,NULL); /*create authentication structure from identity*/
	char route[256];
	LCSipTransports transport= {LC_SIP_TRANSPORT_RANDOM,LC_SIP_TRANSPORT_RANDOM,LC_SIP_TRANSPORT_RANDOM,LC_SIP_TRANSPORT_RANDOM};
	sprintf(route,"sip:%s",test_route);

	linphone_core_add_auth_info(lcm->lc,info); /*add authentication info to LinphoneCore*/

	register_with_refresh_base_3(lcm->lc, FALSE, auth_domain, "sip2.linphone.org:5987", 0,transport,LinphoneRegistrationProgress);

	linphone_core_get_default_proxy(lcm->lc,&proxy_config);
	linphone_proxy_config_edit(proxy_config);

594
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
595 596 597
	linphone_proxy_config_set_server_addr(proxy_config,route);
	linphone_proxy_config_done(proxy_config);

598
	BC_ASSERT(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationOk,1));
599
	/*as we change proxy server destination, we should'nt be notified about the clear*/
600 601 602 603
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationCleared,0, int, "%d");
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationOk,1, int, "%d");
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationProgress,1, int, "%d");
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationFailed,0, int, "%d");
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624

	linphone_core_manager_destroy(lcm);

}

static void proxy_transport_change_with_wrong_port_givin_up() {
	LinphoneCoreManager* lcm = create_lcm();
	stats* counters = &lcm->stat;
	LinphoneProxyConfig* proxy_config;
	LinphoneAuthInfo *info=linphone_auth_info_new(test_username,NULL,test_password,NULL,auth_domain,NULL); /*create authentication structure from identity*/
	char route[256];
	LCSipTransports transport= {LC_SIP_TRANSPORT_RANDOM,LC_SIP_TRANSPORT_RANDOM,LC_SIP_TRANSPORT_RANDOM,LC_SIP_TRANSPORT_RANDOM};
	sprintf(route,"sip:%s",test_route);

	linphone_core_add_auth_info(lcm->lc,info); /*add authentication info to LinphoneCore*/

	register_with_refresh_base_3(lcm->lc, FALSE, auth_domain, "sip2.linphone.org:5987", 0,transport,LinphoneRegistrationProgress);

	linphone_core_get_default_proxy(lcm->lc,&proxy_config);
	linphone_proxy_config_edit(proxy_config);

625
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
626 627 628
	linphone_proxy_config_enableregister(proxy_config,FALSE);
	linphone_proxy_config_done(proxy_config);

629 630 631 632
	BC_ASSERT(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1));
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationOk,0, int, "%d");
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationProgress,1, int, "%d");
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationFailed,0, int, "%d");
633 634 635 636 637

	linphone_core_manager_destroy(lcm);

}

jehan's avatar
jehan committed
638
static void io_recv_error(){
639
	LinphoneCoreManager *lcm;
jehan's avatar
jehan committed
640 641 642
	LinphoneCore* lc;
	int register_ok;
	stats* counters ;
jehan's avatar
jehan committed
643
	int number_of_udp_proxy=0;
jehan's avatar
jehan committed
644

645

646 647 648 649 650 651 652
	lcm=configure_lcm();
	if (lcm) {
		lc=lcm->lc;
		counters = get_stats(lc);
		register_ok=counters->number_of_LinphoneRegistrationOk;
		number_of_udp_proxy=get_number_of_udp_proxy(lc);
		sal_set_recv_error(lc->sal, 0);
jehan's avatar
jehan committed
653

654 655
		BC_ASSERT_TRUE(wait_for(lc,lc,&counters->number_of_LinphoneRegistrationProgress,2*(register_ok-number_of_udp_proxy) /*because 1 udp*/));
		BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationFailed,0,int,"%d");
656

657
		sal_set_recv_error(lc->sal, 1); /*reset*/
jehan's avatar
jehan committed
658

659 660
		linphone_core_manager_destroy(lcm);
	}
jehan's avatar
jehan committed
661 662
}

jehan's avatar
jehan committed
663
static void io_recv_error_retry_immediatly(){
664
	LinphoneCoreManager *lcm;
jehan's avatar
jehan committed
665 666 667 668 669
	LinphoneCore* lc;
	int register_ok;
	stats* counters ;
	int number_of_udp_proxy=0;

670 671 672 673 674 675 676
	lcm=configure_lcm();
	if (lcm) {
		lc=lcm->lc;
		counters = get_stats(lc);
		register_ok=counters->number_of_LinphoneRegistrationOk;
		number_of_udp_proxy=get_number_of_udp_proxy(lc);
		sal_set_recv_error(lc->sal, 0);
jehan's avatar
jehan committed
677

678 679 680
		BC_ASSERT_TRUE(wait_for(lc,NULL,&counters->number_of_LinphoneRegistrationProgress,(register_ok-number_of_udp_proxy)+register_ok /*because 1 udp*/));
		BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationFailed,0,int,"%d");
		sal_set_recv_error(lc->sal, 1); /*reset*/
jehan's avatar
jehan committed
681

682
		BC_ASSERT_TRUE(wait_for(lc,lc,&counters->number_of_LinphoneRegistrationOk,register_ok-number_of_udp_proxy+register_ok));
jehan's avatar
jehan committed
683

684 685
		linphone_core_manager_destroy(lcm);
	}
jehan's avatar
jehan committed
686 687 688
}

static void io_recv_error_late_recovery(){
689 690 691 692 693 694 695 696
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager *lcm;
		LinphoneCore* lc;
		int register_ok;
		stats* counters ;
		int number_of_udp_proxy=0;
		MSList* lcs;
		lcm=linphone_core_manager_new2( "multi_account_rc",FALSE); /*to make sure iterates are not call yet*/
697 698 699 700
		lc=lcm->lc;
		sal_set_refresher_retry_after(lc->sal,1000);
		counters=&lcm->stat;
		BC_ASSERT_TRUE(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationOk,ms_list_size(linphone_core_get_proxy_config_list(lcm->lc))));
jehan's avatar
jehan committed
701 702


703 704 705 706 707 708
		counters = get_stats(lc);
		register_ok=counters->number_of_LinphoneRegistrationOk;
		number_of_udp_proxy=get_number_of_udp_proxy(lc);
		/*simulate a general socket error*/
		sal_set_recv_error(lc->sal, 0);
		sal_set_send_error(lc->sal, -1);
jehan's avatar
jehan committed
709

710 711
		BC_ASSERT_TRUE(wait_for(lc,NULL,&counters->number_of_LinphoneRegistrationProgress,(register_ok-number_of_udp_proxy)+register_ok /*because 1 udp*/));
		BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationFailed,0,int,"%d");
jehan's avatar
jehan committed
712

713
		BC_ASSERT_TRUE(wait_for_list(lcs=ms_list_append(NULL,lc),&counters->number_of_LinphoneRegistrationFailed,(register_ok-number_of_udp_proxy),sal_get_refresher_retry_after(lc->sal)+3000));
jehan's avatar
jehan committed
714

715 716
		sal_set_recv_error(lc->sal, 1); /*reset*/
		sal_set_send_error(lc->sal, 0);
jehan's avatar
jehan committed
717

718
		BC_ASSERT_TRUE(wait_for_list(lcs=ms_list_append(NULL,lc),&counters->number_of_LinphoneRegistrationOk,register_ok-number_of_udp_proxy +register_ok,sal_get_refresher_retry_after(lc->sal)+3000));
719
		linphone_core_manager_destroy(lcm);
720
	}
jehan's avatar
jehan committed
721 722
}

jehan's avatar
jehan committed
723
static void io_recv_error_without_active_register(){
724
	LinphoneCoreManager *lcm;
jehan's avatar
jehan committed
725 726 727 728
	LinphoneCore* lc;
	int register_ok;
	stats* counters ;
	MSList* proxys;
729
	int dummy=0;
jehan's avatar
jehan committed
730

731 732 733 734
	lcm=configure_lcm();
	if (lcm) {
		lc=lcm->lc;
		counters = get_stats(lc);
735

736
		register_ok=counters->number_of_LinphoneRegistrationOk;
jehan's avatar
jehan committed
737

738 739 740 741 742 743 744 745 746
		for (proxys=ms_list_copy(linphone_core_get_proxy_config_list(lc));proxys!=NULL;proxys=proxys->next) {
			LinphoneProxyConfig* proxy_cfg=(LinphoneProxyConfig*)proxys->data;
			linphone_proxy_config_edit(proxy_cfg);
			linphone_proxy_config_enableregister(proxy_cfg,FALSE);
			linphone_proxy_config_done(proxy_cfg);
		}
		ms_list_free(proxys);
		/*wait for unregistrations*/
		BC_ASSERT_TRUE(wait_for(lc,lc,&counters->number_of_LinphoneRegistrationCleared,register_ok /*because 1 udp*/));
747

748
		sal_set_recv_error(lc->sal, 0);
jehan's avatar
jehan committed
749

750 751 752
		/*nothing should happen because no active registration*/
		wait_for_until(lc,lc, &dummy, 1, 3000);
		BC_ASSERT_TRUE(counters->number_of_LinphoneRegistrationProgress == ms_list_size(linphone_core_get_proxy_config_list(lc)));
jehan's avatar
jehan committed
753

754
		BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationFailed,0,int,"%d");
jehan's avatar
jehan committed
755

756
		sal_set_recv_error(lc->sal, 1); /*reset*/
jehan's avatar
jehan committed
757

758 759
		linphone_core_manager_destroy(lcm);
	}
jehan's avatar
jehan committed
760 761
}

jehan's avatar
jehan committed
762

jehan's avatar
jehan committed
763
static void tls_certificate_failure(){
764 765 766
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager* lcm;
		LinphoneCore *lc;
767
		char *rootcapath = bc_tester_res("certificates/cn/agent.pem"); /*bad root ca*/
768

769 770
		lcm=linphone_core_manager_new2("pauline_rc",FALSE);
		lc=lcm->lc;