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

	BC_ASSERT_EQUAL(linphone_proxy_config_is_registered(proxy_cfg), expected_final_state == LinphoneRegistrationOk, int, "%d");
131 132
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationNone,0, int, "%d");
	BC_ASSERT_TRUE(counters->number_of_LinphoneRegistrationProgress>=1);
133
	if (expected_final_state == LinphoneRegistrationOk) {
134 135
		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");
136 137
	} else
		/*checking to be done outside this functions*/
138
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationCleared,0, int, "%d");
jehan's avatar
jehan committed
139 140

}
Ghislain MARY's avatar
Ghislain MARY committed
141

142 143 144 145 146 147 148 149
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
150
static void register_with_refresh_base(LinphoneCore* lc, bool_t refresh,const char* domain,const char* route) {
151 152
	LCSipTransports transport = {5070,5070,0,5071};
	register_with_refresh_base_2(lc,refresh,domain,route,FALSE,transport);
jehan's avatar
jehan committed
153
}
Ghislain MARY's avatar
Ghislain MARY committed
154

155 156 157 158
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);
159
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationCleared,1, int, "%d");
jehan's avatar
jehan committed
160 161
}

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

171
	register_with_refresh_base(lcm->lc,TRUE,auth_domain,route);
jehan's avatar
jehan committed
172
	/*simultate a network error*/
173
	sal_set_send_error(lcm->lc->sal, -1);
174
	while (counters->number_of_LinphoneRegistrationProgress<2 && retry++ <200) {
175
			linphone_core_iterate(lcm->lc);
176
			ms_usleep(10000);
jehan's avatar
jehan committed
177
	}
178 179
	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
180

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

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

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

194 195 196 197 198
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;
199

200 201 202 203
	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.*/
204

205 206 207
	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");
208 209
	BC_ASSERT_PTR_NOT_NULL(value);
	if (value) BC_ASSERT_TRUE(strstr(value, "Flexisip")!=NULL);
210 211 212
	linphone_core_manager_destroy(marie);
}

213 214 215 216 217 218 219 220 221 222 223 224
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*/
225
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
226 227
	linphone_proxy_config_enable_register(proxy_config,FALSE);
	linphone_proxy_config_done(proxy_config);
228
	BC_ASSERT_TRUE(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1));
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
	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*/
244
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
245 246 247 248

	linphone_proxy_config_set_expires(proxy_config,3);

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


	linphone_core_manager_destroy(lcm);
}

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

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

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

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

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

307

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

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

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

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

345
	lcm = linphone_core_manager_new(NULL);
346

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

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

jehan's avatar
jehan committed
357 358

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

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

366
	lcm =  linphone_core_manager_new(NULL);
367

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

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

	test_password=wrong_passwd;

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

386
	lcm =  linphone_core_manager_new(NULL);
387

388 389
	counters = get_stats(lcm->lc);
	register_with_refresh_base_3(lcm->lc,FALSE,auth_domain,route,TRUE,transport,LinphoneRegistrationFailed);
390 391 392
	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");
393 394
	test_password=saved_test_passwd;

395
	linphone_core_manager_destroy(lcm);
396 397
}

398
static void authenticated_register_with_wrong_credentials_with_params_base(const char* user_agent,LinphoneCoreManager *lcm) {
jehan's avatar
jehan committed
399 400
	stats* counters;
	LCSipTransports transport = {5070,5070,0,5071};
401
	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
402
	char route[256];
403

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

406
	sal_set_refresher_retry_after(lcm->lc->sal,500);
407
	if (user_agent) {
408
		linphone_core_set_user_agent(lcm->lc,user_agent,NULL);
409
	}
410 411 412
	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);
413
	//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
414
	/*wait for retry*/
415
	BC_ASSERT_TRUE(wait_for(lcm->lc,lcm->lc,&counters->number_of_auth_info_requested,4));
416
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationFailed,1, int, "%d");
417

418 419 420
	/*check the detailed error info */
	if (!user_agent || strcmp(user_agent,"tester-no-403")!=0){
		LinphoneProxyConfig *cfg=NULL;
421
		linphone_core_get_default_proxy(lcm->lc,&cfg);
422
		BC_ASSERT_PTR_NOT_NULL(cfg);
423 424 425
		if (cfg){
			const LinphoneErrorInfo *ei=linphone_proxy_config_get_error_info(cfg);
			const char *phrase=linphone_error_info_get_phrase(ei);
426 427 428 429
			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));
430
		}
431

432
	}
433 434
	}
static void authenticated_register_with_wrong_credentials_with_params(const char* user_agent) {
435 436 437
	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
438
}
439 440 441
static void authenticated_register_with_wrong_credentials() {
	authenticated_register_with_wrong_credentials_with_params(NULL);
}
442
static void authenticated_register_with_wrong_credentials_2() {
443 444
	LinphoneCoreManager *lcm = linphone_core_manager_new(NULL);
	stats* counters = get_stats(lcm->lc);
445 446 447
	int current_in_progress;
	LinphoneProxyConfig* proxy;

448
	authenticated_register_with_wrong_credentials_with_params_base(NULL,lcm);
449

450
	linphone_core_get_default_proxy(lcm->lc,&proxy);
451 452 453 454 455
	/*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;
456
	BC_ASSERT_FALSE(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationProgress,current_in_progress+1));
457

458
	linphone_core_manager_destroy(lcm);
459
}
460 461 462
static void authenticated_register_with_wrong_credentials_without_403() {
	authenticated_register_with_wrong_credentials_with_params("tester-no-403");
}
463
static LinphoneCoreManager* configure_lcm(void) {
464 465
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager *lcm=linphone_core_manager_new2( "multi_account_rc", FALSE);
466 467 468 469 470 471
		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
472 473 474
}

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

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

488 489 490 491 492 493 494 495 496 497 498
		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
499
}
jehan's avatar
jehan committed
500 501 502 503 504 505 506 507 508 509 510
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;
}
511
static void transport_change(){
512
	LinphoneCoreManager *lcm;
513 514 515 516 517
	LinphoneCore* lc;
	int register_ok;
	stats* counters ;
	LCSipTransports sip_tr;
	LCSipTransports sip_tr_orig;
jehan's avatar
jehan committed
518
	int number_of_udp_proxy=0;
jehan's avatar
jehan committed
519
	int total_number_of_proxies;
520

521 522 523 524 525 526
	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
527

528 529 530
		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
531

532
		sip_tr.udp_port=sip_tr_orig.udp_port;
533

534 535 536
		/*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));
537

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

540 541
		linphone_core_manager_destroy(lcm);
	}
542 543
}

544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
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);

559
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
560 561 562 563 564 565 566 567 568 569 570
	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);

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

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

599
	BC_ASSERT(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationOk,1));
600
	/*as we change proxy server destination, we should'nt be notified about the clear*/
601 602 603 604
	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");
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625

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

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

630 631 632 633
	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");
634 635 636 637 638

	linphone_core_manager_destroy(lcm);

}

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

646

647 648 649 650 651 652 653
	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
654

655 656
		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");
657

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

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

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

671 672 673 674 675 676 677
	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
678

679 680 681
		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
682

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

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

static void io_recv_error_late_recovery(){
690 691 692 693 694 695 696 697
	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*/
698 699 700 701
		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
702 703


704 705 706 707 708 709
		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
710

711 712
		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
713

714
		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
715

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

719
		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));
720
		linphone_core_manager_destroy(lcm);
721
	}
jehan's avatar
jehan committed
722 723
}

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

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

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

739 740 741 742 743 744 745 746 747
		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*/));
748

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

751 752 753
		/*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
754

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

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

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

jehan's avatar
jehan committed
763

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