register_tester.c 37.1 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
static LinphoneCoreManager* create_lcm(void) {
49
	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");
139
	linphone_proxy_config_destroy(proxy_cfg);
jehan's avatar
jehan committed
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(void) {
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

186
static void simple_register(void){
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
	BC_ASSERT_PTR_NOT_NULL(value);
209
	if (value) BC_ASSERT_PTR_NOT_NULL(strstr(value, "Flexisip"));
210 211 212
	linphone_core_manager_destroy(marie);
}

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

219
	proxy_config = linphone_core_get_default_proxy_config(lcm->lc);
220 221 222 223 224

	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
	linphone_core_manager_destroy(lcm);
}

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

238
	proxy_config = linphone_core_get_default_proxy_config(lcm->lc);
239 240 241 242 243

	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
/*take care of min expires configuration from server*/
260
static void simple_register_with_refresh(void) {
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(void) {
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
static void simple_tcp_register(void){
jehan's avatar
jehan committed
279
	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(void){
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
}

297
static void simple_tls_register(void){
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

308
static void simple_authenticated_register(void){
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
static void ha1_authenticated_register(void){
322
	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
}

337
static void authenticated_register_with_no_initial_credentials(void){
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(void){
359
	LinphoneCoreManager *lcm;
360
	stats* counters;
361
	char route[256];
362
	LCSipTransports transport = {5070,5070,0,5071};
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_provided_credentials(void){
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
	LinphoneCoreManager *lcm;
	stats* counters;
	LinphoneProxyConfig *cfg;
	char route[256];
	LinphoneAddress *from;
	char *addr;
	LinphoneAuthInfo *ai;

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

	lcm =  linphone_core_manager_new(NULL);

	counters = get_stats(lcm->lc);
	cfg = linphone_core_create_proxy_config(lcm->lc);
	from = create_linphone_address(auth_domain);

	linphone_proxy_config_set_identity(cfg, addr=linphone_address_as_string(from));
	ms_free(addr);

	linphone_proxy_config_enable_register(cfg,TRUE);
	linphone_proxy_config_set_expires(cfg,1);
	linphone_proxy_config_set_route(cfg, test_route);
	linphone_proxy_config_set_server_addr(cfg,test_route);
	linphone_address_destroy(from);
399

400 401 402 403
	ai = linphone_auth_info_new(test_username, NULL, test_password, NULL, NULL, NULL);
	linphone_core_add_auth_info(lcm->lc, ai);

	linphone_core_add_proxy_config(lcm->lc, cfg);
404

405 406
	BC_ASSERT_TRUE(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationOk,1));
	BC_ASSERT_EQUAL(counters->number_of_auth_info_requested,0, int, "%d");
407

408 409
	BC_ASSERT_PTR_NULL(lp_config_get_string(lcm->lc->config, "auth_info_0", "passwd", NULL));
	BC_ASSERT_PTR_NOT_NULL(lp_config_get_string(lcm->lc->config, "auth_info_0", "ha1", NULL));
410 411

	linphone_proxy_config_destroy(cfg);
412 413 414
	linphone_core_manager_destroy(lcm);
}

415
static void authenticated_register_with_wrong_late_credentials(void){
416
	LinphoneCoreManager *lcm;
417 418 419 420
	stats* counters;
	LCSipTransports transport = {5070,5070,0,5071};
	char route[256];
	const char* saved_test_passwd=test_password;
421
	char* wrong_passwd="mot de pass tout pourri";
422 423 424 425 426

	test_password=wrong_passwd;

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

427
	lcm =  linphone_core_manager_new(NULL);
428

429 430
	counters = get_stats(lcm->lc);
	register_with_refresh_base_3(lcm->lc,FALSE,auth_domain,route,TRUE,transport,LinphoneRegistrationFailed);
431 432 433
	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");
434 435
	test_password=saved_test_passwd;

436
	linphone_core_manager_destroy(lcm);
437 438
}

439
static void authenticated_register_with_wrong_credentials_with_params_base(const char* user_agent,LinphoneCoreManager *lcm) {
jehan's avatar
jehan committed
440 441
	stats* counters;
	LCSipTransports transport = {5070,5070,0,5071};
442
	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
443
	char route[256];
444

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

447
	sal_set_refresher_retry_after(lcm->lc->sal,500);
448
	if (user_agent) {
449
		linphone_core_set_user_agent(lcm->lc,user_agent,NULL);
450
	}
451 452 453
	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);
454
	//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
455
	/*wait for retry*/
456
	BC_ASSERT_TRUE(wait_for(lcm->lc,lcm->lc,&counters->number_of_auth_info_requested,4));
457
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationFailed,1, int, "%d");
458

459 460 461
	/*check the detailed error info */
	if (!user_agent || strcmp(user_agent,"tester-no-403")!=0){
		LinphoneProxyConfig *cfg=NULL;
462
		cfg = linphone_core_get_default_proxy_config(lcm->lc);
463
		BC_ASSERT_PTR_NOT_NULL(cfg);
464 465 466
		if (cfg){
			const LinphoneErrorInfo *ei=linphone_proxy_config_get_error_info(cfg);
			const char *phrase=linphone_error_info_get_phrase(ei);
467
			BC_ASSERT_PTR_NOT_NULL(phrase);
468
			if (phrase) BC_ASSERT_STRING_EQUAL(phrase,"Forbidden");
469 470
			BC_ASSERT_EQUAL(linphone_error_info_get_protocol_code(ei),403, int, "%d");
			BC_ASSERT_PTR_NULL(linphone_error_info_get_details(ei));
471
		}
472

473
	}
474 475
	}
static void authenticated_register_with_wrong_credentials_with_params(const char* user_agent) {
476 477 478
	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
479
}
480
static void authenticated_register_with_wrong_credentials(void) {
481 482
	authenticated_register_with_wrong_credentials_with_params(NULL);
}
483
static void authenticated_register_with_wrong_credentials_2(void) {
484 485
	LinphoneCoreManager *lcm = linphone_core_manager_new(NULL);
	stats* counters = get_stats(lcm->lc);
486 487 488
	int current_in_progress;
	LinphoneProxyConfig* proxy;

489
	authenticated_register_with_wrong_credentials_with_params_base(NULL,lcm);
490

491
	proxy = linphone_core_get_default_proxy_config(lcm->lc);
492 493 494 495 496
	/*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;
497
	BC_ASSERT_FALSE(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationProgress,current_in_progress+1));
498

499
	linphone_core_manager_destroy(lcm);
500
}
501
static void authenticated_register_with_wrong_credentials_without_403(void) {
502 503
	authenticated_register_with_wrong_credentials_with_params("tester-no-403");
}
504
static LinphoneCoreManager* configure_lcm(void) {
505 506
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager *lcm=linphone_core_manager_new2( "multi_account_rc", FALSE);
507
		stats *counters=&lcm->stat;
Ghislain MARY's avatar
Ghislain MARY committed
508
		BC_ASSERT_TRUE(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationOk,(int)bctbx_list_size(linphone_core_get_proxy_config_list(lcm->lc))));
509 510 511 512
		BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationFailed,0, int, "%d");
		return lcm;
	}
	return NULL;
jehan's avatar
jehan committed
513 514
}

515
static void multiple_proxy(void){
516 517 518 519
	LinphoneCoreManager *lcm=configure_lcm();
	if (lcm) {
		linphone_core_manager_destroy(lcm);
	}
jehan's avatar
jehan committed
520 521
}

522
static void network_state_change(void){
jehan's avatar
jehan committed
523
	int register_ok;
524
	stats *counters;
525 526 527
	LinphoneCoreManager *lcm=configure_lcm();
	if (lcm) {
		LinphoneCore *lc=lcm->lc;
528

529 530 531 532 533 534 535 536 537 538 539
		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
540
}
jehan's avatar
jehan committed
541 542 543
static int get_number_of_udp_proxy(const LinphoneCore* lc) {
	int number_of_udp_proxy=0;
	LinphoneProxyConfig* proxy_cfg;
544
	const bctbx_list_t* proxys;
545
	for (proxys=linphone_core_get_proxy_config_list(lc);proxys!=NULL;proxys=proxys->next) {
jehan's avatar
jehan committed
546 547 548 549 550 551
			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;
}
552
static void transport_change(void){
553
	LinphoneCoreManager *lcm;
554 555 556 557 558
	LinphoneCore* lc;
	int register_ok;
	stats* counters ;
	LCSipTransports sip_tr;
	LCSipTransports sip_tr_orig;
jehan's avatar
jehan committed
559
	int number_of_udp_proxy=0;
jehan's avatar
jehan committed
560
	int total_number_of_proxies;
561

562 563 564 565 566 567
	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
568

569
		number_of_udp_proxy=get_number_of_udp_proxy(lc);
Ghislain MARY's avatar
Ghislain MARY committed
570
		total_number_of_proxies=(int)bctbx_list_size(linphone_core_get_proxy_config_list(lc));
571
		linphone_core_get_sip_transports(lc,&sip_tr_orig);
jehan's avatar
jehan committed
572

573
		sip_tr.udp_port=sip_tr_orig.udp_port;
574

575 576 577
		/*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));
578

579
		BC_ASSERT_TRUE(wait_for(lc,lc,&counters->number_of_LinphoneRegistrationFailed,total_number_of_proxies-number_of_udp_proxy));
580

581 582
		linphone_core_manager_destroy(lcm);
	}
583 584
}

585
static void proxy_transport_change(void){
586 587 588 589 590 591 592 593 594 595
	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);

596
	proxy_config = linphone_core_get_default_proxy_config(lcm->lc);
597 598 599
	reset_counters(counters); /*clear stats*/
	linphone_proxy_config_edit(proxy_config);

600
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
601 602 603 604 605 606 607 608 609 610 611
	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);

612
	BC_ASSERT(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationOk,1));
613
	/*as we change p[roxy server destination, we should'nt be notified about the clear*/
614
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationCleared,0, int, "%d");
615 616 617 618 619
	ms_free(addr_as_string);
	linphone_address_destroy(addr);
	linphone_core_manager_destroy(lcm);

}
620
static void proxy_transport_change_with_wrong_port(void) {
621 622 623 624 625 626 627 628 629 630 631 632
	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);

633
	proxy_config = linphone_core_get_default_proxy_config(lcm->lc);
634 635
	linphone_proxy_config_edit(proxy_config);

636
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
637 638 639
	linphone_proxy_config_set_server_addr(proxy_config,route);
	linphone_proxy_config_done(proxy_config);

640
	BC_ASSERT(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationOk,1));
641
	/*as we change proxy server destination, we should'nt be notified about the clear*/
642 643 644 645
	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");
646 647 648 649 650

	linphone_core_manager_destroy(lcm);

}

651
static void proxy_transport_change_with_wrong_port_givin_up(void) {
652 653 654 655 656 657 658 659 660 661 662 663
	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);

664
	proxy_config = linphone_core_get_default_proxy_config(lcm->lc);
665 666
	linphone_proxy_config_edit(proxy_config);

667
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
668 669 670
	linphone_proxy_config_enableregister(proxy_config,FALSE);
	linphone_proxy_config_done(proxy_config);

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

	linphone_core_manager_destroy(lcm);

}

680
static void io_recv_error(void){
681
	LinphoneCoreManager *lcm;
jehan's avatar
jehan committed
682 683 684
	LinphoneCore* lc;
	int register_ok;
	stats* counters ;
jehan's avatar
jehan committed
685
	int number_of_udp_proxy=0;
jehan's avatar
jehan committed
686

687

688 689 690 691 692 693 694
	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
695

696 697
		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");
698

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

701 702
		linphone_core_manager_destroy(lcm);
	}
jehan's avatar
jehan committed
703 704
}

705
static void io_recv_error_retry_immediatly(void){
706
	LinphoneCoreManager *lcm;
jehan's avatar
jehan committed
707 708 709 710 711
	LinphoneCore* lc;
	int register_ok;
	stats* counters ;
	int number_of_udp_proxy=0;

712 713 714 715 716 717 718
	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
719

720 721 722
		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
723

724
		BC_ASSERT_TRUE(wait_for_until(lc,lc,&counters->number_of_LinphoneRegistrationOk,register_ok-number_of_udp_proxy+register_ok,30000));
jehan's avatar
jehan committed
725

726 727
		linphone_core_manager_destroy(lcm);
	}
jehan's avatar
jehan committed
728 729
}

730
static void io_recv_error_late_recovery(void){
731 732 733 734 735 736
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager *lcm;
		LinphoneCore* lc;
		int register_ok;
		stats* counters ;
		int number_of_udp_proxy=0;
737
		bctbx_list_t* lcs;
738
		lcm=linphone_core_manager_new2( "multi_account_rc",FALSE); /*to make sure iterates are not call yet*/
739 740 741
		lc=lcm->lc;
		sal_set_refresher_retry_after(lc->sal,1000);
		counters=&lcm->stat;
Ghislain MARY's avatar
Ghislain MARY committed
742
		BC_ASSERT_TRUE(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationOk,(int)bctbx_list_size(linphone_core_get_proxy_config_list(lcm->lc))));
jehan's avatar
jehan committed
743 744


745 746 747 748