register_tester.c 61 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
	proxy_cfg = linphone_core_create_proxy_config(lc);
jehan's avatar
jehan committed
93

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_unref(cfg);
479 480 481
	linphone_core_manager_destroy(lcm);
}

482 483 484 485 486 487 488 489 490
static void authenticated_register_with_provided_credentials_and_username_with_space(void) {
	LinphoneCoreManager *lcm = linphone_core_manager_new(NULL);
	stats *counters = get_stats(lcm->lc);
	LinphoneProxyConfig *cfg = linphone_core_create_proxy_config(lcm->lc);
	const char *username = "test username";
	LinphoneAddress *from = create_linphone_address_for_algo(auth_domain, username);

	char *addr = linphone_address_as_string(from);
	linphone_proxy_config_set_identity(cfg, addr);
491
	ms_free(addr);
492 493 494

	linphone_proxy_config_enable_register(cfg, TRUE);
	linphone_proxy_config_set_expires(cfg, 1);
495
	linphone_proxy_config_set_route(cfg, test_route);
496
	linphone_proxy_config_set_server_addr(cfg, test_route);
497
	linphone_address_unref(from);
498 499

	LinphoneAuthInfo *ai = linphone_auth_info_new(username, NULL, test_password, NULL, NULL, test_route);
500 501 502
	linphone_core_add_auth_info(lcm->lc, ai);
	linphone_auth_info_unref(ai);
	linphone_core_add_proxy_config(lcm->lc, cfg);
503 504 505 506

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

507 508
	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));
509 510

	linphone_proxy_config_unref(cfg);
511 512
	linphone_core_manager_destroy(lcm);
}
513

514
static void authenticated_register_with_wrong_late_credentials(void){
515
	LinphoneCoreManager *lcm;
516
	stats* counters;
517
	LinphoneTransports *transport = NULL;
518 519
	char route[256];
	const char* saved_test_passwd=test_password;
520
	char* wrong_passwd="mot de pass tout pourri";
521 522 523 524 525

	test_password=wrong_passwd;

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

526
	lcm = linphone_core_manager_new(NULL);
527
	transport = linphone_factory_create_transports(linphone_factory_get());
528 529 530 531
	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);
532

533 534
	counters = get_stats(lcm->lc);
	register_with_refresh_base_3(lcm->lc,FALSE,auth_domain,route,TRUE,transport,LinphoneRegistrationFailed);
535
	linphone_transports_unref(transport);
536 537 538
	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");
539 540
	test_password=saved_test_passwd;

541
	linphone_core_manager_destroy(lcm);
542 543
}

544
static void authenticated_register_with_wrong_credentials_with_params_base(const char* user_agent,LinphoneCoreManager *lcm) {
jehan's avatar
jehan committed
545
	stats* counters;
546
	LinphoneTransports *transport = linphone_factory_create_transports(linphone_factory_get());
547
	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
548
	char route[256];
549

jehan's avatar
jehan committed
550
	sprintf(route,"sip:%s",test_route);
551 552 553 554
	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);
555

556
	sal_set_refresher_retry_after(linphone_core_get_sal(lcm->lc),500);
557
	if (user_agent) {
558
		linphone_core_set_user_agent(lcm->lc,user_agent,NULL);
559
	}
560
	linphone_core_add_auth_info(lcm->lc,info); /*add wrong authentication info to LinphoneCore*/
561
	linphone_auth_info_unref(info);
562 563
	counters = get_stats(lcm->lc);
	register_with_refresh_base_3(lcm->lc,TRUE,auth_domain,route,FALSE,transport,LinphoneRegistrationFailed);
564
	linphone_transports_unref(transport);
565
	//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
566
	/*wait for retry*/
567
	BC_ASSERT_TRUE(wait_for(lcm->lc,lcm->lc,&counters->number_of_auth_info_requested,4));
568
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationFailed,1, int, "%d");
569

570 571 572
	/*check the detailed error info */
	if (!user_agent || strcmp(user_agent,"tester-no-403")!=0){
		LinphoneProxyConfig *cfg=NULL;
573
		cfg = linphone_core_get_default_proxy_config(lcm->lc);
574
		BC_ASSERT_PTR_NOT_NULL(cfg);
575 576 577
		if (cfg){
			const LinphoneErrorInfo *ei=linphone_proxy_config_get_error_info(cfg);
			const char *phrase=linphone_error_info_get_phrase(ei);
578
			BC_ASSERT_PTR_NOT_NULL(phrase);
579
			if (phrase) BC_ASSERT_STRING_EQUAL(phrase,"Forbidden");
580
			BC_ASSERT_EQUAL(linphone_error_info_get_protocol_code(ei),403, int, "%d");
581
			BC_ASSERT_PTR_NULL(linphone_error_info_get_warnings(ei));
582
		}
583

584
	}
585 586
	}
static void authenticated_register_with_wrong_credentials_with_params(const char* user_agent) {
587 588 589
	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
590
}
591
static void authenticated_register_with_wrong_credentials(void) {
592 593
	authenticated_register_with_wrong_credentials_with_params(NULL);
}
594
static void authenticated_register_with_wrong_credentials_2(void) {
595 596
	LinphoneCoreManager *lcm = linphone_core_manager_new(NULL);
	stats* counters = get_stats(lcm->lc);
597 598 599
	int current_in_progress;
	LinphoneProxyConfig* proxy;

600
	authenticated_register_with_wrong_credentials_with_params_base(NULL,lcm);
601

602
	proxy = linphone_core_get_default_proxy_config(lcm->lc);
603 604 605 606 607
	/*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;
608
	BC_ASSERT_FALSE(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationProgress,current_in_progress+1));
609

610
	linphone_core_manager_destroy(lcm);
611
}
612
static void authenticated_register_with_wrong_credentials_without_403(void) {
613 614
	authenticated_register_with_wrong_credentials_with_params("tester-no-403");
}
615
static LinphoneCoreManager* configure_lcm(void) {
616 617
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager *lcm=linphone_core_manager_new2( "multi_account_rc", FALSE);
618
		stats *counters=&lcm->stat;
619
		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))));
620 621 622 623
		BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationFailed,0, int, "%d");
		return lcm;
	}
	return NULL;
jehan's avatar
jehan committed
624 625
}

626
static void multiple_proxy(void){
627 628 629 630
	LinphoneCoreManager *lcm=configure_lcm();
	if (lcm) {
		linphone_core_manager_destroy(lcm);
	}
jehan's avatar
jehan committed
631 632
}

633
static void network_state_change(void){
jehan's avatar
jehan committed
634
	int register_ok;
635
	stats *counters;
636 637 638
	LinphoneCoreManager *lcm=configure_lcm();
	if (lcm) {
		LinphoneCore *lc=lcm->lc;
639

640 641 642 643 644
		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));
645
		BC_ASSERT_FALSE(wait_for_until(lc,lc,&counters->number_of_LinphoneRegistrationProgress,register_ok+1,1000)); /*make sure no register is tried*/
646 647 648 649 650 651
		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
652
}
653 654 655
static int get_number_of_udp_proxy(const LinphoneCore* lc) {
	int number_of_udp_proxy=0;
	LinphoneProxyConfig* proxy_cfg;
656
	const bctbx_list_t* proxys;
657
	for (proxys=linphone_core_get_proxy_config_list(lc);proxys!=NULL;proxys=proxys->next) {
658 659 660 661 662 663
			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;
}
664
static void transport_change(void){
665
	LinphoneCoreManager *lcm;
666 667 668
	LinphoneCore* lc;
	int register_ok;
	stats* counters ;
669 670
	LinphoneTransports *sip_tr;
	LinphoneTransports *sip_tr_orig;
671
	int number_of_udp_proxy=0;
jehan's avatar
jehan committed
672
	int total_number_of_proxies;
673

674 675 676
	lcm=configure_lcm();
	if (lcm) {
		lc=lcm->lc;
677
		sip_tr = linphone_factory_create_transports(linphone_factory_get());
678 679
		counters = get_stats(lc);
		register_ok=counters->number_of_LinphoneRegistrationOk;
680

681
		number_of_udp_proxy=get_number_of_udp_proxy(lc);
682
		total_number_of_proxies=(int)bctbx_list_size(linphone_core_get_proxy_config_list(lc));
683
		sip_tr_orig = linphone_core_get_transports(lc);
684

685
		linphone_transports_set_udp_port(sip_tr, linphone_transports_get_udp_port(sip_tr_orig));
686

687
		/*keep only udp*/
688
		linphone_core_set_transports(lc, sip_tr);
689
		BC_ASSERT_TRUE(wait_for(lc,lc,&counters->number_of_LinphoneRegistrationOk,register_ok+number_of_udp_proxy));
690

691
		BC_ASSERT_TRUE(wait_for(lc,lc,&counters->number_of_LinphoneRegistrationFailed,total_number_of_proxies-number_of_udp_proxy));
692

693 694
		linphone_transports_unref(sip_tr);
		linphone_transports_unref(sip_tr_orig);
695 696
		linphone_core_manager_destroy(lcm);
	}
697 698
}

Simon Morlat's avatar
Simon Morlat committed
699 700 701
static void transport_dont_bind(void){
	LinphoneCoreManager *pauline = linphone_core_manager_new("pauline_tcp_rc");
	stats* counters = &pauline->stat;
702
	LinphoneTransports *tr = linphone_factory_create_transports(linphone_factory_get());
703 704
	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
705

706
	linphone_core_set_transports(pauline->lc, tr);
Simon Morlat's avatar
Simon Morlat committed
707
	BC_ASSERT_TRUE(wait_for_until(pauline->lc,pauline->lc,&counters->number_of_LinphoneRegistrationOk,2,15000));
708 709
	linphone_transports_unref(tr);
	tr = linphone_core_get_transports_used(pauline->lc);
710 711 712
	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");
713
	linphone_transports_unref(tr);
Simon Morlat's avatar
Simon Morlat committed
714 715 716
	linphone_core_manager_destroy(pauline);
}

Simon Morlat's avatar
Simon Morlat committed
717 718 719
static void transport_busy(void){
	LinphoneCoreManager *pauline = linphone_core_manager_new("pauline_tcp_rc");
	LCSipTransports tr;
Ronan's avatar
Ronan committed
720

Simon Morlat's avatar
Simon Morlat committed
721 722 723 724
	memset(&tr, 0, sizeof(tr));
	tr.udp_port = 5070;
	tr.tcp_port = 5070;
	tr.tls_port = 5071;
Ronan's avatar
Ronan committed
725

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

Simon Morlat's avatar
Simon Morlat committed
728 729 730 731 732
	{
		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);
733
		/*BC_ASSERT_EQUAL(tr.udp_port, 0, int, "%i");
Simon Morlat's avatar
Simon Morlat committed
734
		BC_ASSERT_EQUAL(tr.tcp_port, 0, int, "%i");
735
		BC_ASSERT_EQUAL(tr.tls_port, 0, int, "%i");*/
Simon Morlat's avatar
Simon Morlat committed
736 737
		linphone_core_manager_destroy(marie);
	}
Ronan's avatar
Ronan committed
738

Simon Morlat's avatar
Simon Morlat committed
739 740 741
	linphone_core_manager_destroy(pauline);
}

742
static void proxy_transport_change(void){
743 744 745 746 747 748 749
	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*/
750
	linphone_auth_info_unref(info);
751 752
	register_with_refresh_base(lcm->lc,FALSE,auth_domain,NULL);

753
	proxy_config = linphone_core_get_default_proxy_config(lcm->lc);
754 755 756
	reset_counters(counters); /*clear stats*/
	linphone_proxy_config_edit(proxy_config);

757
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
758 759 760 761 762 763 764 765 766 767 768
	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);

769
	BC_ASSERT(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationOk,1));
770
	/*as we change p[roxy server destination, we should'nt be notified about the clear*/
771
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationCleared,0, int, "%d");
772
	ms_free(addr_as_string);
Simon Morlat's avatar
Simon Morlat committed
773
	linphone_address_unref(addr);
774 775 776
	linphone_core_manager_destroy(lcm);

}
777 778
/*
 * On ios, some firewal require to disable flow label (livebox with default firewall level).
779 780
 * sudo sysctl net.inet6.ip6.auto_flowlabel=0
 * It might be possible to found a sockopt for such purpose.
781
 */
782
static void proxy_transport_change_with_wrong_port(void) {
783 784 785 786 787
	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];
788
	LinphoneTransports *transport= linphone_factory_create_transports(linphone_factory_get());
789
	sprintf(route,"sip:%s",test_route);
790 791 792 793
	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);
794 795

	linphone_core_add_auth_info(lcm->lc,info); /*add authentication info to LinphoneCore*/
796
	linphone_auth_info_unref(info);
797
	register_with_refresh_base_3(lcm->lc, FALSE, auth_domain, "sip2.linphone.org:5987", 0,transport,LinphoneRegistrationProgress);
798
	linphone_transports_unref(transport);
799

800
	proxy_config = linphone_core_get_default_proxy_config(lcm->lc);
801 802
	linphone_proxy_config_edit(proxy_config);

803
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
804 805 806
	linphone_proxy_config_set_server_addr(proxy_config,route);
	linphone_proxy_config_done(proxy_config);

807
	BC_ASSERT(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationOk,1));
808
	/*as we change proxy server destination, we should'nt be notified about the clear*/
809 810 811 812
	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");
813 814 815 816 817

	linphone_core_manager_destroy(lcm);

}

818
static void proxy_transport_change_with_wrong_port_givin_up(void) {
819 820 821 822 823
	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];
824
	LinphoneTransports *transport = linphone_factory_create_transports(linphone_factory_get());
825
	sprintf(route,"sip:%s",test_route);
826 827 828 829
	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);
830 831

	linphone_core_add_auth_info(lcm->lc,info); /*add authentication info to LinphoneCore*/
832
	linphone_auth_info_unref(info);
833
	register_with_refresh_base_3(lcm->lc, FALSE, auth_domain, "sip2.linphone.org:5987", 0,transport,LinphoneRegistrationProgress);
834
	linphone_transports_unref(transport);
835

836
	proxy_config = linphone_core_get_default_proxy_config(lcm->lc);
837 838
	linphone_proxy_config_edit(proxy_config);

839
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
840 841 842
	linphone_proxy_config_enableregister(proxy_config,FALSE);
	linphone_proxy_config_done(proxy_config);

843 844 845 846
	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");
847 848 849 850 851

	linphone_core_manager_destroy(lcm);

}

852
static void io_recv_error(void){
853
	LinphoneCoreManager *lcm;
jehan's avatar
jehan committed
854 855 856
	LinphoneCore* lc;
	int register_ok;
	stats* counters ;
857
	int number_of_udp_proxy=0;
jehan's avatar
jehan committed
858

859

860 861 862 863 864 865
	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);
866
		sal_set_recv_error(linphone_core_get_sal(lc), 0);
jehan's avatar
jehan committed
867

868 869
		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");
870

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

873 874
		linphone_core_manager_destroy(lcm);
	}
jehan's avatar
jehan committed
875 876
}

877
static void io_recv_error_retry_immediatly(void){
878
	LinphoneCoreManager *lcm;
jehan's avatar
jehan committed
879 880 881 882 883
	LinphoneCore* lc;
	int register_ok;
	stats* counters ;
	int number_of_udp_proxy=0;

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

896
		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
897

898 899
		linphone_core_manager_destroy(lcm);
	}
jehan's avatar
jehan committed
900 901
}

902
static void io_recv_error_late_recovery(void){
903 904 905 906 907 908
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager *lcm;
		LinphoneCore* lc;
		int register_ok;
		stats* counters ;
		int number_of_udp_proxy=0;
909
		bctbx_list_t* lcs;
910
		lcm=linphone_core_manager_new2( "multi_account_rc",FALSE); /*to make sure iterates are not call yet*/
911
		lc=lcm->lc;
912
		sal_set_refresher_retry_after(linphone_core_get_sal(lc),1000);
913
		counters=&lcm->stat;
914
		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
915 916


917 918 919 920
		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*/
921 922
		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
923

924 925
		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
926

927
		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
928

929 930
		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
931

932
		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));
933
		linphone_core_manager_destroy(lcm);
934
	}
jehan's avatar
jehan committed
935 936
}

937
static void io_recv_error_without_active_register(void){
938
	LinphoneCoreManager *lcm;
jehan's avatar
jehan committed
939 940 941
	LinphoneCore* lc;
	int register_ok;
	stats* counters ;
942
	bctbx_list_t* proxys;
943
	int dummy=0;
jehan's avatar
jehan committed
944

945 946 947 948
	lcm=configure_lcm();
	if (lcm) {
		lc=lcm->lc;
		counters = get_stats(lc);
949

950
		register_ok=counters->number_of_LinphoneRegistrationOk;
jehan's avatar
jehan committed
951

952
		for (proxys=bctbx_list_copy(linphone_core_get_proxy_config_list(lc));proxys!=NULL;proxys=proxys->next) {
953 954 955 956 957
			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);
		}
958
		bctbx_list_free(proxys);
959 960
		/*wait for unregistrations*/
		BC_ASSERT_TRUE(wait_for(lc,lc,&counters->number_of_LinphoneRegistrationCleared,register_ok /*because 1 udp*/));
961

962
		sal_set_recv_error(linphone_core_get_sal(lc), 0);
jehan's avatar
jehan committed
963

964 965
		/*nothing should happen because no active registration*/
		wait_for_until(lc,lc, &dummy, 1, 3000);
966
		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
967

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

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

972 973
		linphone_core_manager_destroy(lcm);
	}
jehan's avatar
jehan committed