register_tester.c 54.8 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/>.
*/
18

19

20
#include "linphone/core.h"
jehan's avatar
jehan committed
21
#include "liblinphone_tester.h"
Benjamin REIS's avatar
Benjamin REIS committed
22
#include "tester_utils.h"
jehan's avatar
jehan committed
23

24

25 26 27 28
static void authentication_requested(LinphoneCore *lc, LinphoneAuthInfo *auth_info, LinphoneAuthMethod method) {
	linphone_auth_info_set_passwd(auth_info, test_password);
	linphone_core_add_auth_info(lc, auth_info); /*add authentication info to LinphoneCore*/
}
29

30
static LinphoneCoreManager* create_lcm_with_auth(unsigned int with_auth) {
31
	LinphoneCoreManager* lcm = linphone_core_manager_new(NULL);
32

33
	if (with_auth) {
34 35 36 37
		LinphoneCoreCbs *cbs = linphone_factory_create_core_cbs(linphone_factory_get());
		linphone_core_cbs_set_authentication_requested(cbs, authentication_requested);
		linphone_core_add_callbacks(lcm->lc, cbs);
		linphone_core_cbs_unref(cbs);
38
	}
39

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

45
static LinphoneCoreManager* create_lcm(void) {
46
	return create_lcm_with_auth(0);
jehan's avatar
jehan committed
47 48 49
}

void registration_state_changed(struct _LinphoneCore *lc, LinphoneProxyConfig *cfg, LinphoneRegistrationState cstate, const char *message){
50
		stats* counters;
jehan's avatar
jehan committed
51 52 53 54
		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));
55
		counters = get_stats(lc);
jehan's avatar
jehan committed
56 57 58 59 60 61 62
		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:
63
			BC_FAIL("unexpected event");break;
jehan's avatar
jehan committed
64 65
		}
}
66

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

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

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

	proxy_cfg = linphone_proxy_config_new();

94
	from = create_linphone_address_for_algo(domain, username);
jehan's avatar
jehan committed
95

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

	linphone_proxy_config_enable_register(proxy_cfg,TRUE);
101
	linphone_proxy_config_set_expires(proxy_cfg,1);
jehan's avatar
jehan committed
102 103 104 105 106 107
	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);
	}
Simon Morlat's avatar
Simon Morlat committed
108
	linphone_address_unref(from);
jehan's avatar
jehan committed
109 110 111 112

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

113
	while (counters->number_of_LinphoneRegistrationOk<1+(refresh!=0)
114
			&& retry++ <(1100 /*only wait 11 s if final state is progress*/+(expected_final_state==LinphoneRegistrationProgress?0:2000))) {
jehan's avatar
jehan committed
115
		linphone_core_iterate(lc);
jehan's avatar
jehan committed
116
		if (counters->number_of_auth_info_requested>0 && linphone_proxy_config_get_state(proxy_cfg) == LinphoneRegistrationFailed && late_auth_info) {
117
			if (!linphone_core_get_auth_info_list(lc)) {
118
				BC_ASSERT_EQUAL(linphone_proxy_config_get_error(proxy_cfg),LinphoneReasonUnauthorized, int, "%d");
119
				info=linphone_auth_info_new(test_username,NULL,test_password,NULL,auth_domain,NULL); /*create authentication structure from identity*/
120
				linphone_core_add_auth_info(lc,info); /*add authentication info to LinphoneCore*/
121
				linphone_auth_info_unref(info);
122
			}
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_unref(proxy_cfg);
jehan's avatar
jehan committed
140
}
141

142 143 144 145 146 147 148 149 150 151
static void register_with_refresh_base_3(
	LinphoneCore* lc,
	bool_t refresh,
	const char *domain,
	const char *route,
	bool_t late_auth_info,
	LinphoneTransports *transport,
	LinphoneRegistrationState expected_final_state
) {
	register_with_refresh_base_3_for_algo(lc, refresh, domain, route, late_auth_info, transport, expected_final_state, NULL);
152 153
}

154 155 156 157 158 159 160 161 162
static void register_with_refresh_base_2(
	LinphoneCore* lc,
	bool_t refresh,
	const char *domain,
	const char *route,
	bool_t late_auth_info,
	LinphoneTransports *transport
) {
	register_with_refresh_base_3(lc, refresh, domain, route, late_auth_info, transport,LinphoneRegistrationOk);
163
}
164

165 166 167 168 169 170 171 172
static void register_with_refresh_base_for_algo(LinphoneCore* lc, bool_t refresh, const char *domain,const char *route, const char *username) {
	LinphoneTransports *transport = linphone_factory_create_transports(linphone_factory_get());
	linphone_transports_set_udp_port(transport, 5070);
	linphone_transports_set_tcp_port(transport, 5070);
	linphone_transports_set_tls_port(transport, 5071);
	linphone_transports_set_dtls_port(transport, 0);
	register_with_refresh_base_3_for_algo(lc,refresh,domain,route,FALSE,transport,LinphoneRegistrationOk,username);
	linphone_transports_unref(transport);
173 174
}

175 176
static void register_with_refresh_base(LinphoneCore *lc, bool_t refresh, const char *domain, const char *route) {
	register_with_refresh_base_for_algo(lc, refresh, domain, route, NULL);
177 178
}

179 180 181 182 183
static void register_with_refresh_for_algo(LinphoneCoreManager *lcm, bool_t refresh, const char *domain, const char*route, const char *username) {
	stats* counters = &lcm->stat;
	register_with_refresh_base_for_algo(lcm->lc,refresh,domain,route,username);
	linphone_core_manager_stop(lcm);
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationCleared,1, int, "%d");
jehan's avatar
jehan committed
184
}
185

186
static void register_with_refresh(LinphoneCoreManager* lcm, bool_t refresh,const char* domain,const char* route) {
187
	register_with_refresh_for_algo(lcm, refresh, domain, route, NULL);
jehan's avatar
jehan committed
188 189
}

190
static void register_with_refresh_with_send_error(void) {
jehan's avatar
jehan committed
191
	int retry=0;
192 193
	LinphoneCoreManager* lcm = create_lcm_with_auth(1);
	stats* counters = &lcm->stat;
194
	LinphoneAuthInfo *info=linphone_auth_info_new(test_username,NULL,test_password,NULL,auth_domain,NULL); /*create authentication structure from identity*/
195 196
	char route[256];
	sprintf(route,"sip:%s",test_route);
197
	linphone_core_add_auth_info(lcm->lc,info); /*add authentication info to LinphoneCore*/
198
	linphone_auth_info_unref(info);
199
	register_with_refresh_base(lcm->lc,TRUE,auth_domain,route);
jehan's avatar
jehan committed
200
	/*simultate a network error*/
201
	sal_set_send_error(linphone_core_get_sal(lcm->lc), -1);
202
	while (counters->number_of_LinphoneRegistrationProgress<2 && retry++ <200) {
203
			linphone_core_iterate(lcm->lc);
204
			ms_usleep(10000);
jehan's avatar
jehan committed
205
	}
206 207
	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
208

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

211
	linphone_core_manager_destroy(lcm);
jehan's avatar
jehan committed
212
}
213

214
static void simple_register(void){
215 216 217
	LinphoneCoreManager* lcm = create_lcm();
	stats* counters = &lcm->stat;
	register_with_refresh(lcm,FALSE,NULL,NULL);
218
	BC_ASSERT_EQUAL(counters->number_of_auth_info_requested,0, int, "%d");
219
	linphone_core_manager_destroy(lcm);
jehan's avatar
jehan committed
220 221
}

222 223 224 225 226
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;
227

228 229 230 231
	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.*/
232

233 234 235
	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");
236
	BC_ASSERT_PTR_NOT_NULL(value);
237
	if (value) BC_ASSERT_PTR_NOT_NULL(strstr(value, "Flexisip"));
238 239 240
	linphone_core_manager_destroy(marie);
}

241
static void simple_unregister(void){
242 243 244 245 246
	LinphoneCoreManager* lcm = create_lcm();
	stats* counters = &lcm->stat;
	LinphoneProxyConfig* proxy_config;
	register_with_refresh_base(lcm->lc,FALSE,NULL,NULL);

247
	proxy_config = linphone_core_get_default_proxy_config(lcm->lc);
248 249 250 251 252

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

	/*nothing is supposed to arrive until done*/
253
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
254 255
	linphone_proxy_config_enable_register(proxy_config,FALSE);
	linphone_proxy_config_done(proxy_config);
256
	BC_ASSERT_TRUE(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1));
257 258 259
	linphone_core_manager_destroy(lcm);
}

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

266
	proxy_config = linphone_core_get_default_proxy_config(lcm->lc);
267 268 269 270

	linphone_proxy_config_edit(proxy_config);

	/*nothing is supposed to arrive until done*/
271
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
272 273

	linphone_proxy_config_set_expires(proxy_config,3);
274
	reset_counters(counters); /*clear stats*/
275
	linphone_proxy_config_done(proxy_config);
276
	BC_ASSERT_TRUE(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationOk,1));
277
	/*wait 2s without receive refresh*/
278
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationOk,2,2000));
279
	/* now, it should be ok*/
280
	BC_ASSERT_TRUE(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationOk,2));
281 282 283
	linphone_core_manager_destroy(lcm);
}

jehan's avatar
jehan committed
284
/*take care of min expires configuration from server*/
285
static void simple_register_with_refresh(void) {
286 287 288
	LinphoneCoreManager* lcm = create_lcm();
	stats* counters = &lcm->stat;
	register_with_refresh(lcm,TRUE,NULL,NULL);
289
	BC_ASSERT_EQUAL(counters->number_of_auth_info_requested,0, int, "%d");
290
	linphone_core_manager_destroy(lcm);
jehan's avatar
jehan committed
291 292
}

293
static void simple_auth_register_with_refresh(void) {
294 295
	LinphoneCoreManager* lcm = create_lcm_with_auth(1);
	stats* counters = &lcm->stat;
296 297
	char route[256];
	sprintf(route,"sip:%s",test_route);
298
	register_with_refresh(lcm,TRUE,auth_domain,route);
299
	BC_ASSERT_EQUAL(counters->number_of_auth_info_requested,1, int, "%d");
300
	linphone_core_manager_destroy(lcm);
jehan's avatar
jehan committed
301 302
}

303
static void simple_tcp_register(void){
jehan's avatar
jehan committed
304
	char route[256];
305
	LinphoneCoreManager* lcm;
306
	sprintf(route,"sip:%s;transport=tcp",test_route);
307 308 309
	lcm = create_lcm();
	register_with_refresh(lcm,FALSE,test_domain,route);
	linphone_core_manager_destroy(lcm);
jehan's avatar
jehan committed
310
}
311

312
static void simple_tcp_register_compatibility_mode(void){
313
	char route[256];
314
	LinphoneCoreManager* lcm;
315
	LinphoneTransports *transport = NULL;
316
	sprintf(route,"sip:%s",test_route);
317
	lcm = create_lcm();
318
	transport = linphone_factory_create_transports(linphone_factory_get());
319
	linphone_transports_set_tcp_port(transport, 5070);
320
	register_with_refresh_base_2(lcm->lc,FALSE,test_domain,route,FALSE,transport);
321
	linphone_transports_unref(transport);
322
	linphone_core_manager_destroy(lcm);
323 324
}

325
static void simple_tls_register(void){
326 327 328
	if (transport_supported(LinphoneTransportTls)) {
		char route[256];
		LinphoneCoreManager* lcm = create_lcm();
329 330
		sprintf(route,"sip:%s;transport=tls",test_route);
		register_with_refresh(lcm,FALSE,test_domain,route);
331
		linphone_core_manager_destroy(lcm);
332
	}
jehan's avatar
jehan committed
333 334
}

335

336
static void simple_authenticated_register(void){
337
	stats* counters;
338
	LinphoneCoreManager* lcm = create_lcm();
339
	LinphoneAuthInfo *info=linphone_auth_info_new_for_algorithm(test_username,NULL,test_password,NULL,auth_domain,NULL,NULL); /*create authentication structure from identity*/
340 341
	char route[256];
	sprintf(route,"sip:%s",test_route);
342
	linphone_core_add_auth_info(lcm->lc,info); /*add authentication info to LinphoneCore*/
343
	linphone_auth_info_unref(info);
344 345
	counters = &lcm->stat;
	register_with_refresh(lcm,FALSE,auth_domain,route);
346
	BC_ASSERT_EQUAL(counters->number_of_auth_info_requested,0, int, "%d");
347
	linphone_core_manager_destroy(lcm);
jehan's avatar
jehan committed
348 349
}

350
static void simple_authenticated_register_for_algorithm(void){
351 352 353 354 355 356 357 358 359 360 361
	stats* counters;
	LinphoneCoreManager* lcm = create_lcm();
	LinphoneAuthInfo *info=linphone_auth_info_new_for_algorithm(test_sha_username,NULL,test_password,NULL,auth_domain,NULL,"SHA-256"); /*create authentication structure from identity*/
	char route[256];
	sprintf(route,"sip:%s",test_route);
	linphone_core_add_auth_info(lcm->lc,info); /*add authentication info to LinphoneCore*/
	linphone_auth_info_unref(info);
	counters = &lcm->stat;
	register_with_refresh_for_algo(lcm,FALSE,auth_domain,route,test_sha_username);
	BC_ASSERT_EQUAL(counters->number_of_auth_info_requested,0, int, "%d");
	linphone_core_manager_destroy(lcm);
362 363
}

364
static void ha1_authenticated_register(void){
365
	stats* counters;
366
	LinphoneCoreManager* lcm = create_lcm();
367 368 369 370
	char ha1[33];
	LinphoneAuthInfo *info;
	char route[256];
	sal_auth_compute_ha1(test_username,auth_domain,test_password,ha1);
371
	info=linphone_auth_info_new_for_algorithm(test_username,NULL,NULL,ha1,auth_domain,NULL,NULL); /*create authentication structure from identity*/
372
	sprintf(route,"sip:%s",test_route);
373
	linphone_core_add_auth_info(lcm->lc,info); /*add authentication info to LinphoneCore*/
374
	linphone_auth_info_unref(info);
375 376
	counters = &lcm->stat;
	register_with_refresh(lcm,FALSE,auth_domain,route);
377
	BC_ASSERT_EQUAL(counters->number_of_auth_info_requested,0, int, "%d");
378
	linphone_core_manager_destroy(lcm);
379 380
}

381
static void ha1_authenticated_register_for_algorithm(void){
382 383 384 385 386 387 388 389 390 391 392 393 394 395
	stats* counters;
	LinphoneCoreManager* lcm = create_lcm();
	char ha1[65];
	LinphoneAuthInfo *info;
	char route[256];
	sal_auth_compute_ha1_for_algorithm(test_sha_username,auth_domain,test_password,ha1,65,"SHA-256");
	info=linphone_auth_info_new_for_algorithm(test_sha_username,NULL,NULL,ha1,auth_domain,NULL,"SHA-256"); /*create authentication structure from identity*/
	sprintf(route,"sip:%s",test_route);
	linphone_core_add_auth_info(lcm->lc,info); /*add authentication info to LinphoneCore*/
	linphone_auth_info_unref(info);
	counters = &lcm->stat;
	register_with_refresh_for_algo(lcm,FALSE,auth_domain,route,test_sha_username);
	BC_ASSERT_EQUAL(counters->number_of_auth_info_requested,0, int, "%d");
	linphone_core_manager_destroy(lcm);
396 397
}

398
static void authenticated_register_with_no_initial_credentials(void){
399
	LinphoneCoreManager *lcm;
400
	LinphoneCoreCbs *cbs = linphone_factory_create_core_cbs(linphone_factory_get());
401
	stats* counters;
402
	char route[256];
403

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

406
	lcm = linphone_core_manager_new(NULL);
407

408 409 410
	linphone_core_cbs_set_authentication_requested(cbs, authentication_requested);
	linphone_core_add_callbacks(lcm->lc, cbs);
	linphone_core_cbs_unref(cbs);
411

412
	counters= get_stats(lcm->lc);
jehan's avatar
jehan committed
413
	counters->number_of_auth_info_requested=0;
414
	register_with_refresh(lcm,FALSE,auth_domain,route);
415
	BC_ASSERT_EQUAL(counters->number_of_auth_info_requested,1, int, "%d");
416
	linphone_core_manager_destroy(lcm);
jehan's avatar
jehan committed
417
}
418

jehan's avatar
jehan committed
419

420
static void authenticated_register_with_late_credentials(void){
421
	LinphoneCoreManager *lcm;
422
	stats* counters;
423
	char route[256];
424
	LinphoneTransports *transport = NULL;
425

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

428
	lcm = linphone_core_manager_new(NULL);
429
	transport = linphone_factory_create_transports(linphone_factory_get());
430 431 432
	linphone_transports_set_udp_port(transport, 5070);
	linphone_transports_set_tcp_port(transport, 5070);
	linphone_transports_set_dtls_port(transport, 5071);
433

434 435
	counters = get_stats(lcm->lc);
	register_with_refresh_base_2(lcm->lc,FALSE,auth_domain,route,TRUE,transport);
436
	linphone_transports_unref(transport);
437
	BC_ASSERT_EQUAL(counters->number_of_auth_info_requested,1, int, "%d");
438
	linphone_core_manager_destroy(lcm);
jehan's avatar
jehan committed
439
}
jehan's avatar
jehan committed
440

441
static void authenticated_register_with_provided_credentials(void){
442 443 444 445 446 447 448 449 450 451
	LinphoneCoreManager *lcm;
	stats* counters;
	LinphoneProxyConfig *cfg;
	char route[256];
	LinphoneAddress *from;
	char *addr;
	LinphoneAuthInfo *ai;

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

452
	lcm = linphone_core_manager_new(NULL);
453 454 455 456 457 458 459 460 461 462 463 464

	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);
Simon Morlat's avatar
Simon Morlat committed
465
	linphone_address_unref(from);
466

467 468
	ai = linphone_auth_info_new(test_username, NULL, test_password, NULL, NULL, NULL);
	linphone_core_add_auth_info(lcm->lc, ai);
469
	linphone_auth_info_unref(ai);
470
	linphone_core_add_proxy_config(lcm->lc, cfg);
471

472 473
	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");
474

475 476
	BC_ASSERT_PTR_NULL(lp_config_get_string(linphone_core_get_config(lcm->lc), "auth_info_0", "passwd", NULL));
	BC_ASSERT_PTR_NOT_NULL(lp_config_get_string(linphone_core_get_config(lcm->lc), "auth_info_0", "ha1", NULL));
477 478

	linphone_proxy_config_destroy(cfg);
479 480 481
	linphone_core_manager_destroy(lcm);
}

482
static void authenticated_register_with_wrong_late_credentials(void){
483
	LinphoneCoreManager *lcm;
484
	stats* counters;
485
	LinphoneTransports *transport = NULL;
486 487
	char route[256];
	const char* saved_test_passwd=test_password;
488
	char* wrong_passwd="mot de pass tout pourri";
489 490 491 492 493

	test_password=wrong_passwd;

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

494
	lcm = linphone_core_manager_new(NULL);
495
	transport = linphone_factory_create_transports(linphone_factory_get());
496 497 498 499
	linphone_transports_set_udp_port(transport, 5070);
	linphone_transports_set_tcp_port(transport, 5070);
	linphone_transports_set_tls_port(transport, 5071);
	linphone_transports_set_dtls_port(transport, 0);
500

501 502
	counters = get_stats(lcm->lc);
	register_with_refresh_base_3(lcm->lc,FALSE,auth_domain,route,TRUE,transport,LinphoneRegistrationFailed);
503
	linphone_transports_unref(transport);
504 505 506
	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");
507 508
	test_password=saved_test_passwd;

509
	linphone_core_manager_destroy(lcm);
510 511
}

512
static void authenticated_register_with_wrong_credentials_with_params_base(const char* user_agent,LinphoneCoreManager *lcm) {
jehan's avatar
jehan committed
513
	stats* counters;
514
	LinphoneTransports *transport = linphone_factory_create_transports(linphone_factory_get());
515
	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
516
	char route[256];
517

jehan's avatar
jehan committed
518
	sprintf(route,"sip:%s",test_route);
519 520 521 522
	linphone_transports_set_udp_port(transport, 5070);
	linphone_transports_set_tcp_port(transport, 5070);
	linphone_transports_set_tls_port(transport, 5071);
	linphone_transports_set_dtls_port(transport, 0);
523

524
	sal_set_refresher_retry_after(linphone_core_get_sal(lcm->lc),500);
525
	if (user_agent) {
526
		linphone_core_set_user_agent(lcm->lc,user_agent,NULL);
527
	}
528
	linphone_core_add_auth_info(lcm->lc,info); /*add wrong authentication info to LinphoneCore*/
529
	linphone_auth_info_unref(info);
530 531
	counters = get_stats(lcm->lc);
	register_with_refresh_base_3(lcm->lc,TRUE,auth_domain,route,FALSE,transport,LinphoneRegistrationFailed);
532
	linphone_transports_unref(transport);
533
	//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
534
	/*wait for retry*/
535
	BC_ASSERT_TRUE(wait_for(lcm->lc,lcm->lc,&counters->number_of_auth_info_requested,4));
536
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationFailed,1, int, "%d");
537

538 539 540
	/*check the detailed error info */
	if (!user_agent || strcmp(user_agent,"tester-no-403")!=0){
		LinphoneProxyConfig *cfg=NULL;
541
		cfg = linphone_core_get_default_proxy_config(lcm->lc);
542
		BC_ASSERT_PTR_NOT_NULL(cfg);
543 544 545
		if (cfg){
			const LinphoneErrorInfo *ei=linphone_proxy_config_get_error_info(cfg);
			const char *phrase=linphone_error_info_get_phrase(ei);
546
			BC_ASSERT_PTR_NOT_NULL(phrase);
547
			if (phrase) BC_ASSERT_STRING_EQUAL(phrase,"Forbidden");
548
			BC_ASSERT_EQUAL(linphone_error_info_get_protocol_code(ei),403, int, "%d");
549
			BC_ASSERT_PTR_NULL(linphone_error_info_get_warnings(ei));
550
		}
551

552
	}
553 554
	}
static void authenticated_register_with_wrong_credentials_with_params(const char* user_agent) {
555 556 557
	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
558
}
559
static void authenticated_register_with_wrong_credentials(void) {
560 561
	authenticated_register_with_wrong_credentials_with_params(NULL);
}
562
static void authenticated_register_with_wrong_credentials_2(void) {
563 564
	LinphoneCoreManager *lcm = linphone_core_manager_new(NULL);
	stats* counters = get_stats(lcm->lc);
565 566 567
	int current_in_progress;
	LinphoneProxyConfig* proxy;

568
	authenticated_register_with_wrong_credentials_with_params_base(NULL,lcm);
569

570
	proxy = linphone_core_get_default_proxy_config(lcm->lc);
571 572 573 574 575
	/*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;
576
	BC_ASSERT_FALSE(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationProgress,current_in_progress+1));
577

578
	linphone_core_manager_destroy(lcm);
579
}
580
static void authenticated_register_with_wrong_credentials_without_403(void) {
581 582
	authenticated_register_with_wrong_credentials_with_params("tester-no-403");
}
583
static LinphoneCoreManager* configure_lcm(void) {
584 585
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager *lcm=linphone_core_manager_new2( "multi_account_rc", FALSE);
586
		stats *counters=&lcm->stat;
587
		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))));
588 589 590 591
		BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationFailed,0, int, "%d");
		return lcm;
	}
	return NULL;
jehan's avatar
jehan committed
592 593
}

594
static void multiple_proxy(void){
595 596 597 598
	LinphoneCoreManager *lcm=configure_lcm();
	if (lcm) {
		linphone_core_manager_destroy(lcm);
	}
jehan's avatar
jehan committed
599 600
}

601
static void network_state_change(void){
jehan's avatar
jehan committed
602
	int register_ok;
603
	stats *counters;
604 605 606
	LinphoneCoreManager *lcm=configure_lcm();
	if (lcm) {
		LinphoneCore *lc=lcm->lc;
607

608 609 610 611 612
		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));
613
		BC_ASSERT_FALSE(wait_for_until(lc,lc,&counters->number_of_LinphoneRegistrationProgress,register_ok+1,1000)); /*make sure no register is tried*/
614 615 616 617 618 619
		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
620
}
621 622 623
static int get_number_of_udp_proxy(const LinphoneCore* lc) {
	int number_of_udp_proxy=0;
	LinphoneProxyConfig* proxy_cfg;
624
	const bctbx_list_t* proxys;
625
	for (proxys=linphone_core_get_proxy_config_list(lc);proxys!=NULL;proxys=proxys->next) {
626 627 628 629 630 631
			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;
}
632
static void transport_change(void){
633
	LinphoneCoreManager *lcm;
634 635 636
	LinphoneCore* lc;
	int register_ok;
	stats* counters ;
637 638
	LinphoneTransports *sip_tr;
	LinphoneTransports *sip_tr_orig;
639
	int number_of_udp_proxy=0;
jehan's avatar
jehan committed
640
	int total_number_of_proxies;
641

642 643 644
	lcm=configure_lcm();
	if (lcm) {
		lc=lcm->lc;
645
		sip_tr = linphone_factory_create_transports(linphone_factory_get());
646 647
		counters = get_stats(lc);
		register_ok=counters->number_of_LinphoneRegistrationOk;
648

649
		number_of_udp_proxy=get_number_of_udp_proxy(lc);
650
		total_number_of_proxies=(int)bctbx_list_size(linphone_core_get_proxy_config_list(lc));
651
		sip_tr_orig = linphone_core_get_transports(lc);
652

653
		linphone_transports_set_udp_port(sip_tr, linphone_transports_get_udp_port(sip_tr_orig));
654

655
		/*keep only udp*/
656
		linphone_core_set_transports(lc, sip_tr);
657
		BC_ASSERT_TRUE(wait_for(lc,lc,&counters->number_of_LinphoneRegistrationOk,register_ok+number_of_udp_proxy));
658

659
		BC_ASSERT_TRUE(wait_for(lc,lc,&counters->number_of_LinphoneRegistrationFailed,total_number_of_proxies-number_of_udp_proxy));
660

661 662
		linphone_transports_unref(sip_tr);
		linphone_transports_unref(sip_tr_orig);
663 664
		linphone_core_manager_destroy(lcm);
	}
665 666
}

Simon Morlat's avatar
Simon Morlat committed
667 668 669
static void transport_dont_bind(void){
	LinphoneCoreManager *pauline = linphone_core_manager_new("pauline_tcp_rc");
	stats* counters = &pauline->stat;
670
	LinphoneTransports *tr = linphone_factory_create_transports(linphone_factory_get());
671 672
	linphone_transports_set_tcp_port(tr, LC_SIP_TRANSPORT_DONTBIND);
	linphone_transports_set_tls_port(tr, LC_SIP_TRANSPORT_DONTBIND);
Ronan's avatar
Ronan committed
673

674
	linphone_core_set_transports(pauline->lc, tr);
Simon Morlat's avatar
Simon Morlat committed
675
	BC_ASSERT_TRUE(wait_for_until(pauline->lc,pauline->lc,&counters->number_of_LinphoneRegistrationOk,2,15000));
676 677
	linphone_transports_unref(tr);
	tr = linphone_core_get_transports_used(pauline->lc);
678 679 680
	BC_ASSERT_EQUAL(linphone_transports_get_udp_port(tr), 0, int, "%i");
	BC_ASSERT_EQUAL(linphone_transports_get_tcp_port(tr), LC_SIP_TRANSPORT_DONTBIND, int, "%i");
	BC_ASSERT_EQUAL(linphone_transports_get_tls_port(tr), LC_SIP_TRANSPORT_DONTBIND, int, "%i");
681
	linphone_transports_unref(tr);
Simon Morlat's avatar
Simon Morlat committed
682 683 684
	linphone_core_manager_destroy(pauline);
}

Simon Morlat's avatar
Simon Morlat committed
685 686 687
static void transport_busy(void){
	LinphoneCoreManager *pauline = linphone_core_manager_new("pauline_tcp_rc");
	LCSipTransports tr;
Ronan's avatar
Ronan committed
688

Simon Morlat's avatar
Simon Morlat committed
689 690 691 692
	memset(&tr, 0, sizeof(tr));
	tr.udp_port = 5070;
	tr.tcp_port = 5070;
	tr.tls_port = 5071;
Ronan's avatar
Ronan committed
693

Simon Morlat's avatar
Simon Morlat committed
694
	linphone_core_set_sip_transports(pauline->lc, &tr);
Ronan's avatar
Ronan committed
695

Simon Morlat's avatar
Simon Morlat committed
696 697 698 699 700
	{
		LinphoneCoreManager *marie = linphone_core_manager_new("marie_rc");
		linphone_core_set_sip_transports(marie->lc, &tr);
		memset(&tr, 0, sizeof(tr));
		linphone_core_get_sip_transports_used(pauline->lc, &tr);
701
		/*BC_ASSERT_EQUAL(tr.udp_port, 0, int, "%i");
Simon Morlat's avatar
Simon Morlat committed
702
		BC_ASSERT_EQUAL(tr.tcp_port, 0, int, "%i");
703
		BC_ASSERT_EQUAL(tr.tls_port, 0, int, "%i");*/
Simon Morlat's avatar
Simon Morlat committed
704 705
		linphone_core_manager_destroy(marie);
	}
Ronan's avatar
Ronan committed
706

Simon Morlat's avatar
Simon Morlat committed
707 708 709
	linphone_core_manager_destroy(pauline);
}

710
static void proxy_transport_change(void){
711 712 713 714 715 716 717
	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*/
718
	linphone_auth_info_unref(info);
719 720
	register_with_refresh_base(lcm->lc,FALSE,auth_domain,NULL);

721
	proxy_config = linphone_core_get_default_proxy_config(lcm->lc);
722 723 724
	reset_counters(counters); /*clear stats*/
	linphone_proxy_config_edit(proxy_config);

725
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
726 727 728 729 730 731 732 733 734 735 736
	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);

737
	BC_ASSERT(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationOk,1));
738
	/*as we change p[roxy server destination, we should'nt be notified about the clear*/
739
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationCleared,0, int, "%d");
740
	ms_free(addr_as_string);
Simon Morlat's avatar
Simon Morlat committed
741
	linphone_address_unref(addr);
742 743 744
	linphone_core_manager_destroy(lcm);

}
745 746
/*
 * On ios, some firewal require to disable flow label (livebox with default firewall level).
747 748
 * sudo sysctl net.inet6.ip6.auto_flowlabel=0
 * It might be possible to found a sockopt for such purpose.
749
 */
750
static void proxy_transport_change_with_wrong_port(void) {
751 752 753 754 755
	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];
756
	LinphoneTransports *transport= linphone_factory_create_transports(linphone_factory_get());
757
	sprintf(route,"sip:%s",test_route);
758 759 760 761
	linphone_transports_set_udp_port(transport, LC_SIP_TRANSPORT_RANDOM);
	linphone_transports_set_tcp_port(transport, LC_SIP_TRANSPORT_RANDOM);
	linphone_transports_set_tls_port(transport, LC_SIP_TRANSPORT_RANDOM);
	linphone_transports_set_dtls_port(transport, LC_SIP_TRANSPORT_RANDOM);
762 763

	linphone_core_add_auth_info(lcm->lc,info); /*add authentication info to LinphoneCore*/
764
	linphone_auth_info_unref(info);
765
	register_with_refresh_base_3(lcm->lc, FALSE, auth_domain, "sip2.linphone.org:5987", 0,transport,LinphoneRegistrationProgress);
766
	linphone_transports_unref(transport);
767

768
	proxy_config = linphone_core_get_default_proxy_config(lcm->lc);
769 770
	linphone_proxy_config_edit(proxy_config);

771
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
772 773 774
	linphone_proxy_config_set_server_addr(proxy_config,route);
	linphone_proxy_config_done(proxy_config);

775
	BC_ASSERT(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationOk,1));
776
	/*as we change proxy server destination, we should'nt be notified about the clear*/
777 778 779 780
	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");
781 782 783 784 785

	linphone_core_manager_destroy(lcm);

}

786
static void proxy_transport_change_with_wrong_port_givin_up(void) {
787 788 789 790 791
	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];
792
	LinphoneTransports *transport = linphone_factory_create_transports(linphone_factory_get());
793
	sprintf(route,"sip:%s",test_route);
794 795 796 797
	linphone_transports_set_udp_port(transport, LC_SIP_TRANSPORT_RANDOM);
	linphone_transports_set_tcp_port(transport, LC_SIP_TRANSPORT_RANDOM);
	linphone_transports_set_tls_port(transport, LC_SIP_TRANSPORT_RANDOM);
	linphone_transports_set_dtls_port(transport, LC_SIP_TRANSPORT_RANDOM);
798 799

	linphone_core_add_auth_info(lcm->lc,info); /*add authentication info to LinphoneCore*/
800
	linphone_auth_info_unref(info);
801
	register_with_refresh_base_3(lcm->lc, FALSE, auth_domain, "sip2.linphone.org:5987", 0,transport,LinphoneRegistrationProgress);
802
	linphone_transports_unref(transport);
803

804
	proxy_config = linphone_core_get_default_proxy_config(lcm->lc);
805 806
	linphone_proxy_config_edit(proxy_config);

807
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
808 809 810
	linphone_proxy_config_enableregister(proxy_config,FALSE);
	linphone_proxy_config_done(proxy_config);

811 812 813 814
	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");
815 816 817 818 819

	linphone_core_manager_destroy(lcm);

}

820
static void io_recv_error(void){
821
	LinphoneCoreManager *lcm;
jehan's avatar
jehan committed
822 823 824
	LinphoneCore* lc;
	int register_ok;
	stats* counters ;
825
	int number_of_udp_proxy=0;
jehan's avatar
jehan committed
826

827

828 829 830 831 832 833
	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);
834
		sal_set_recv_error(linphone_core_get_sal(lc), 0);
jehan's avatar
jehan committed
835

836 837
		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");
838

839
		sal_set_recv_error(linphone_core_get_sal(lc), 1); /*reset*/
jehan's avatar
jehan committed
840

841 842
		linphone_core_manager_destroy(lcm);
	}
jehan's avatar
jehan committed
843 844
}

845
static void io_recv_error_retry_immediatly(void){
846
	LinphoneCoreManager *lcm;
jehan's avatar
jehan committed
847 848 849 850 851
	LinphoneCore* lc;
	int register_ok;
	stats* counters ;
	int number_of_udp_proxy=0;

852 853 854 855 856 857
	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);
858
		sal_set_recv_error(linphone_core_get_sal(lc), 0);
jehan's avatar
jehan committed
859

860 861
		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");
862
		sal_set_recv_error(linphone_core_get_sal(lc), 1); /*reset*/
jehan's avatar
jehan committed
863

864
		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
865

866 867
		linphone_core_manager_destroy(lcm);
	}
jehan's avatar
jehan committed
868 869
}

870
static void io_recv_error_late_recovery(void){
871 872 873 874 875 876
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager *lcm;
		LinphoneCore* lc;
		int register_ok;
		stats* counters ;
		int number_of_udp_proxy=0;
877
		bctbx_list_t* lcs;
878
		lcm=linphone_core_manager_new2( "multi_account_rc",FALSE); /*to make sure iterates are not call yet*/
879
		lc=lcm->lc;
880
		sal_set_refresher_retry_after(linphone_core_get_sal(lc),1000);
881
		counters=&lcm->stat;
882
		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
883 884


885 886 887 888
		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*/
889 890
		sal_set_recv_error(linphone_core_get_sal(lc), 0);
		sal_set_send_error(linphone_core_get_sal(lc), -1);
jehan's avatar
jehan committed
891

892 893
		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
894

895
		BC_ASSERT_TRUE(wait_for_list(lcs=bctbx_list_append(NULL,lc),&counters->number_of_LinphoneRegistrationFailed,(register_ok-number_of_udp_proxy),sal_get_refresher_retry_after(linphone_core_get_sal(lc))+3000));
jehan's avatar
jehan committed
896

897 898
		sal_set_recv_error(linphone_core_get_sal(lc), 1); /*reset*/
		sal_set_send_error(linphone_core_get_sal(lc), 0);
jehan's avatar
jehan committed
899

900
		BC_ASSERT_TRUE(wait_for_list(lcs=bctbx_list_append(NULL,lc),&counters->number_of_LinphoneRegistrationOk,register_ok-number_of_udp_proxy +register_ok,sal_get_refresher_retry_after(linphone_core_get_sal(lc))+3000));
901
		linphone_core_manager_destroy(lcm);
902
	}
jehan's avatar
jehan committed
903 904
}

905
static void io_recv_error_without_active_register(void){
906
	LinphoneCoreManager *lcm;
jehan's avatar
jehan committed
907 908 909
	LinphoneCore* lc;
	int register_ok;
	stats* counters ;
910
	bctbx_list_t* proxys;
911
	int dummy=0;
jehan's avatar
jehan committed
912

913 914 915 916
	lcm=configure_lcm();
	if (lcm) {
		lc=lcm->lc;
		counters = get_stats(lc);
917

918
		register_ok=counters->number_of_LinphoneRegistrationOk;
jehan's avatar
jehan committed
919

920
		for (proxys=bctbx_list_copy(linphone_core_get_proxy_config_list(lc));proxys!=NULL;proxys=proxys->next) {
921 922 923 924 925
			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);
		}
926
		bctbx_list_free(proxys);
927 928
		/*wait for unregistrations*/
		BC_ASSERT_TRUE(wait_for(lc,lc,&counters->number_of_LinphoneRegistrationCleared,register_ok /*because 1 udp*/));
929

930
		sal_set_recv_error(linphone_core_get_sal(lc), 0);
jehan's avatar
jehan committed
931

932 933
		/*nothing should happen because no active registration*/
		wait_for_until(lc,lc, &dummy, 1, 3000);
934
		BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationProgress, (int)bctbx_list_size(linphone_core_get_proxy_config_list(lc)), int, "%d");
jehan's avatar
jehan committed
935

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

938
		sal_set_recv_error(linphone_core_get_sal(lc), 1); /*reset*/
jehan's avatar
jehan committed
939

940 941
		linphone_core_manager_destroy(lcm);
	}
jehan's avatar
jehan committed
942 943
}

jehan's avatar
jehan committed
944

945
static void tls_certificate_failure(void){
946 947 948
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager* lcm;
		LinphoneCore *lc;
949
		char *rootcapath = bc_tester_res("certificates/cn/agent.pem"); /*bad root ca*/
950

951 952
		lcm=linphone_core_manager_new2("pauline_rc",FALSE);
		lc=lcm->lc;
953 954 955 956 957 958
		linphone_core_set_root_ca(lcm->lc,rootcapath);
		linphone_core_set_network_reachable(lc,TRUE);
		BC_ASSERT_TRUE(wait_for(lcm->lc,lcm->lc,&lcm->stat.number_of_LinphoneRegistrationFailed,1));
		linphone_core_set_root_ca(lcm->lc,NULL); /*no root ca*/
		linphone_core_refresh_registers(lcm->lc);
		BC_ASSERT_TRUE(wait_for(lc,lc,&lcm->stat.number_of_LinphoneRegistrationFailed,2));
959
		bc_free(rootcapath);
960
		rootcapath = bc_tester_res("certificates/cn/cafile.pem"); /*good root ca*/
961 962 963 964
		linphone_core_set_root_ca(lcm->lc,rootcapath);
		linphone_core_refresh_registers(lcm->lc);
		BC_ASSERT_TRUE(wait_for(lc,lc,&lcm->stat.number_of_LinphoneRegistrationOk,1));
		BC_ASSERT_EQUAL(lcm->stat.number_of_LinphoneRegistrationFailed,2, int, "%d");
965
		linphone_core_manager_destroy(lcm);
966
		bc_free(rootcapath);
967
	}
jehan's avatar
jehan committed
968
}
Simon Morlat's avatar
Simon Morlat committed
969