register_tester.c 50 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 22 23
#include "private.h"
#include "liblinphone_tester.h"

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
static void register_with_refresh_base_3(LinphoneCore* lc
											, bool_t refresh
											,const char* domain
											,const char* route
											,bool_t late_auth_info
72
											,LinphoneTransports *transport
73
											,LinphoneRegistrationState expected_final_state) {
jehan's avatar
jehan committed
74
	int retry=0;
75
	char* addr;
76 77 78 79 80 81
	LinphoneProxyConfig* proxy_cfg;
	stats* counters;
	LinphoneAddress *from;
	const char* server_addr;
	LinphoneAuthInfo *info;

82
	BC_ASSERT_PTR_NOT_NULL(lc);
jehan's avatar
jehan committed
83
	if (!lc) return;
84

85
	counters = get_stats(lc);
jehan's avatar
jehan committed
86
	reset_counters(counters);
87
	linphone_core_set_transports(lc, transport);
jehan's avatar
jehan committed
88 89 90

	proxy_cfg = linphone_proxy_config_new();

91
	from = create_linphone_address(domain);
jehan's avatar
jehan committed
92

93 94
	linphone_proxy_config_set_identity(proxy_cfg,addr=linphone_address_as_string(from));
	ms_free(addr);
95
	server_addr = linphone_address_get_domain(from);
jehan's avatar
jehan committed
96 97

	linphone_proxy_config_enable_register(proxy_cfg,TRUE);
98
	linphone_proxy_config_set_expires(proxy_cfg,1);
jehan's avatar
jehan committed
99 100 101 102 103 104
	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
105
	linphone_address_unref(from);
jehan's avatar
jehan committed
106 107 108 109

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

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

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

139 140 141 142 143
static void register_with_refresh_base_2(LinphoneCore* lc
											, bool_t refresh
											,const char* domain
											,const char* route
											,bool_t late_auth_info
144
											,LinphoneTransports *transport) {
145 146
	register_with_refresh_base_3(lc, refresh, domain, route, late_auth_info, transport,LinphoneRegistrationOk );
}
jehan's avatar
jehan committed
147
static void register_with_refresh_base(LinphoneCore* lc, bool_t refresh,const char* domain,const char* route) {
148
	LinphoneTransports *transport = linphone_transports_new();
149 150 151 152
	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);
153
	register_with_refresh_base_2(lc,refresh,domain,route,FALSE,transport);
154
	linphone_transports_unref(transport);
jehan's avatar
jehan committed
155
}
156

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

164
static void register_with_refresh_with_send_error(void) {
jehan's avatar
jehan committed
165
	int retry=0;
166 167
	LinphoneCoreManager* lcm = create_lcm_with_auth(1);
	stats* counters = &lcm->stat;
168
	LinphoneAuthInfo *info=linphone_auth_info_new(test_username,NULL,test_password,NULL,auth_domain,NULL); /*create authentication structure from identity*/
169 170
	char route[256];
	sprintf(route,"sip:%s",test_route);
171
	linphone_core_add_auth_info(lcm->lc,info); /*add authentication info to LinphoneCore*/
172
	linphone_auth_info_unref(info);
173
	register_with_refresh_base(lcm->lc,TRUE,auth_domain,route);
jehan's avatar
jehan committed
174
	/*simultate a network error*/
175
	sal_set_send_error(lcm->lc->sal, -1);
176
	while (counters->number_of_LinphoneRegistrationProgress<2 && retry++ <200) {
177
			linphone_core_iterate(lcm->lc);
178
			ms_usleep(10000);
jehan's avatar
jehan committed
179
	}
180 181
	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
182

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

185
	linphone_core_manager_destroy(lcm);
jehan's avatar
jehan committed
186
}
187

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

196 197 198 199 200
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;
201

202 203 204 205
	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.*/
206

207 208 209
	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");
210
	BC_ASSERT_PTR_NOT_NULL(value);
211
	if (value) BC_ASSERT_PTR_NOT_NULL(strstr(value, "Flexisip"));
212 213 214
	linphone_core_manager_destroy(marie);
}

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

221
	proxy_config = linphone_core_get_default_proxy_config(lcm->lc);
222 223 224 225 226

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

	/*nothing is supposed to arrive until done*/
227
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
228 229
	linphone_proxy_config_enable_register(proxy_config,FALSE);
	linphone_proxy_config_done(proxy_config);
230
	BC_ASSERT_TRUE(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1));
231 232 233
	linphone_core_manager_destroy(lcm);
}

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

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

	linphone_proxy_config_edit(proxy_config);

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

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

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

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

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

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

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

309

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

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

341
static void authenticated_register_with_no_initial_credentials(void){
342
	LinphoneCoreManager *lcm;
343
	LinphoneCoreCbs *cbs = linphone_factory_create_core_cbs(linphone_factory_get());
344
	stats* counters;
345
	char route[256];
346

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

349
	lcm = linphone_core_manager_new(NULL);
350

351 352 353
	linphone_core_cbs_set_authentication_requested(cbs, authentication_requested);
	linphone_core_add_callbacks(lcm->lc, cbs);
	linphone_core_cbs_unref(cbs);
354

355
	counters= get_stats(lcm->lc);
jehan's avatar
jehan committed
356
	counters->number_of_auth_info_requested=0;
357
	register_with_refresh(lcm,FALSE,auth_domain,route);
358
	BC_ASSERT_EQUAL(counters->number_of_auth_info_requested,1, int, "%d");
359
	linphone_core_manager_destroy(lcm);
jehan's avatar
jehan committed
360
}
361

jehan's avatar
jehan committed
362

363
static void authenticated_register_with_late_credentials(void){
364
	LinphoneCoreManager *lcm;
365
	stats* counters;
366
	char route[256];
367
	LinphoneTransports *transport = NULL;
368

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

371
	lcm =  linphone_core_manager_new(NULL);
372
	transport = linphone_transports_new();
373 374 375
	linphone_transports_set_udp_port(transport, 5070);
	linphone_transports_set_tcp_port(transport, 5070);
	linphone_transports_set_dtls_port(transport, 5071);
376

377 378
	counters = get_stats(lcm->lc);
	register_with_refresh_base_2(lcm->lc,FALSE,auth_domain,route,TRUE,transport);
379
	linphone_transports_unref(transport);
380
	BC_ASSERT_EQUAL(counters->number_of_auth_info_requested,1, int, "%d");
381
	linphone_core_manager_destroy(lcm);
jehan's avatar
jehan committed
382
}
jehan's avatar
jehan committed
383

384
static void authenticated_register_with_provided_credentials(void){
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
	LinphoneCoreManager *lcm;
	stats* counters;
	LinphoneProxyConfig *cfg;
	char route[256];
	LinphoneAddress *from;
	char *addr;
	LinphoneAuthInfo *ai;

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

	lcm =  linphone_core_manager_new(NULL);

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

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

	linphone_proxy_config_enable_register(cfg,TRUE);
	linphone_proxy_config_set_expires(cfg,1);
	linphone_proxy_config_set_route(cfg, test_route);
	linphone_proxy_config_set_server_addr(cfg,test_route);
Simon Morlat's avatar
Simon Morlat committed
408
	linphone_address_unref(from);
409

410 411
	ai = linphone_auth_info_new(test_username, NULL, test_password, NULL, NULL, NULL);
	linphone_core_add_auth_info(lcm->lc, ai);
412
	linphone_auth_info_unref(ai);
413
	linphone_core_add_proxy_config(lcm->lc, cfg);
414

415 416
	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");
417

418 419
	BC_ASSERT_PTR_NULL(lp_config_get_string(lcm->lc->config, "auth_info_0", "passwd", NULL));
	BC_ASSERT_PTR_NOT_NULL(lp_config_get_string(lcm->lc->config, "auth_info_0", "ha1", NULL));
420 421

	linphone_proxy_config_destroy(cfg);
422 423 424
	linphone_core_manager_destroy(lcm);
}

425
static void authenticated_register_with_wrong_late_credentials(void){
426
	LinphoneCoreManager *lcm;
427
	stats* counters;
428
	LinphoneTransports *transport = NULL;
429 430
	char route[256];
	const char* saved_test_passwd=test_password;
431
	char* wrong_passwd="mot de pass tout pourri";
432 433 434 435 436

	test_password=wrong_passwd;

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

437
	lcm =  linphone_core_manager_new(NULL);
438
	transport = linphone_transports_new();
439 440 441 442
	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);
443

444 445
	counters = get_stats(lcm->lc);
	register_with_refresh_base_3(lcm->lc,FALSE,auth_domain,route,TRUE,transport,LinphoneRegistrationFailed);
446
	linphone_transports_unref(transport);
447 448 449
	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");
450 451
	test_password=saved_test_passwd;

452
	linphone_core_manager_destroy(lcm);
453 454
}

455
static void authenticated_register_with_wrong_credentials_with_params_base(const char* user_agent,LinphoneCoreManager *lcm) {
jehan's avatar
jehan committed
456
	stats* counters;
457
	LinphoneTransports *transport = linphone_transports_new();
458
	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
459
	char route[256];
460

jehan's avatar
jehan committed
461
	sprintf(route,"sip:%s",test_route);
462 463 464 465
	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);
466

467
	sal_set_refresher_retry_after(lcm->lc->sal,500);
468
	if (user_agent) {
469
		linphone_core_set_user_agent(lcm->lc,user_agent,NULL);
470
	}
471
	linphone_core_add_auth_info(lcm->lc,info); /*add wrong authentication info to LinphoneCore*/
472
	linphone_auth_info_unref(info);
473 474
	counters = get_stats(lcm->lc);
	register_with_refresh_base_3(lcm->lc,TRUE,auth_domain,route,FALSE,transport,LinphoneRegistrationFailed);
475
	linphone_transports_unref(transport);
476
	//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
477
	/*wait for retry*/
478
	BC_ASSERT_TRUE(wait_for(lcm->lc,lcm->lc,&counters->number_of_auth_info_requested,4));
479
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationFailed,1, int, "%d");
480

481 482 483
	/*check the detailed error info */
	if (!user_agent || strcmp(user_agent,"tester-no-403")!=0){
		LinphoneProxyConfig *cfg=NULL;
484
		cfg = linphone_core_get_default_proxy_config(lcm->lc);
485
		BC_ASSERT_PTR_NOT_NULL(cfg);
486 487 488
		if (cfg){
			const LinphoneErrorInfo *ei=linphone_proxy_config_get_error_info(cfg);
			const char *phrase=linphone_error_info_get_phrase(ei);
489
			BC_ASSERT_PTR_NOT_NULL(phrase);
490
			if (phrase) BC_ASSERT_STRING_EQUAL(phrase,"Forbidden");
491
			BC_ASSERT_EQUAL(linphone_error_info_get_protocol_code(ei),403, int, "%d");
492
			BC_ASSERT_PTR_NULL(linphone_error_info_get_warnings(ei));
493
		}
494

495
	}
496 497
	}
static void authenticated_register_with_wrong_credentials_with_params(const char* user_agent) {
498 499 500
	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
501
}
502
static void authenticated_register_with_wrong_credentials(void) {
503 504
	authenticated_register_with_wrong_credentials_with_params(NULL);
}
505
static void authenticated_register_with_wrong_credentials_2(void) {
506 507
	LinphoneCoreManager *lcm = linphone_core_manager_new(NULL);
	stats* counters = get_stats(lcm->lc);
508 509 510
	int current_in_progress;
	LinphoneProxyConfig* proxy;

511
	authenticated_register_with_wrong_credentials_with_params_base(NULL,lcm);
512

513
	proxy = linphone_core_get_default_proxy_config(lcm->lc);
514 515 516 517 518
	/*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;
519
	BC_ASSERT_FALSE(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationProgress,current_in_progress+1));
520

521
	linphone_core_manager_destroy(lcm);
522
}
523
static void authenticated_register_with_wrong_credentials_without_403(void) {
524 525
	authenticated_register_with_wrong_credentials_with_params("tester-no-403");
}
526
static LinphoneCoreManager* configure_lcm(void) {
527 528
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager *lcm=linphone_core_manager_new2( "multi_account_rc", FALSE);
529
		stats *counters=&lcm->stat;
530
		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))));
531 532 533 534
		BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationFailed,0, int, "%d");
		return lcm;
	}
	return NULL;
jehan's avatar
jehan committed
535 536
}

537
static void multiple_proxy(void){
538 539 540 541
	LinphoneCoreManager *lcm=configure_lcm();
	if (lcm) {
		linphone_core_manager_destroy(lcm);
	}
jehan's avatar
jehan committed
542 543
}

544
static void network_state_change(void){
jehan's avatar
jehan committed
545
	int register_ok;
546
	stats *counters;
547 548 549
	LinphoneCoreManager *lcm=configure_lcm();
	if (lcm) {
		LinphoneCore *lc=lcm->lc;
550

551 552 553 554 555
		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));
556
		BC_ASSERT_FALSE(wait_for_until(lc,lc,&counters->number_of_LinphoneRegistrationProgress,register_ok+1,1000)); /*make sure no register is tried*/
557 558 559 560 561 562
		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
563
}
564 565 566
static int get_number_of_udp_proxy(const LinphoneCore* lc) {
	int number_of_udp_proxy=0;
	LinphoneProxyConfig* proxy_cfg;
567
	const bctbx_list_t* proxys;
568
	for (proxys=linphone_core_get_proxy_config_list(lc);proxys!=NULL;proxys=proxys->next) {
569 570 571 572 573 574
			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;
}
575
static void transport_change(void){
576
	LinphoneCoreManager *lcm;
577 578 579
	LinphoneCore* lc;
	int register_ok;
	stats* counters ;
580 581
	LinphoneTransports *sip_tr;
	LinphoneTransports *sip_tr_orig;
582
	int number_of_udp_proxy=0;
jehan's avatar
jehan committed
583
	int total_number_of_proxies;
584

585 586 587
	lcm=configure_lcm();
	if (lcm) {
		lc=lcm->lc;
588
		sip_tr = linphone_transports_new();
589 590
		counters = get_stats(lc);
		register_ok=counters->number_of_LinphoneRegistrationOk;
591

592
		number_of_udp_proxy=get_number_of_udp_proxy(lc);
593
		total_number_of_proxies=(int)bctbx_list_size(linphone_core_get_proxy_config_list(lc));
594
		sip_tr_orig = linphone_core_get_transports(lc);
595

596
		sip_tr->udp_port = sip_tr_orig->udp_port;
597

598
		/*keep only udp*/
599
		linphone_core_set_transports(lc, sip_tr);
600
		BC_ASSERT_TRUE(wait_for(lc,lc,&counters->number_of_LinphoneRegistrationOk,register_ok+number_of_udp_proxy));
601

602
		BC_ASSERT_TRUE(wait_for(lc,lc,&counters->number_of_LinphoneRegistrationFailed,total_number_of_proxies-number_of_udp_proxy));
603

604 605
		linphone_transports_unref(sip_tr);
		linphone_transports_unref(sip_tr_orig);
606 607
		linphone_core_manager_destroy(lcm);
	}
608 609
}

Simon Morlat's avatar
Simon Morlat committed
610 611 612
static void transport_dont_bind(void){
	LinphoneCoreManager *pauline = linphone_core_manager_new("pauline_tcp_rc");
	stats* counters = &pauline->stat;
613
	LinphoneTransports *tr = linphone_transports_new();
614 615
	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
616

617
	linphone_core_set_transports(pauline->lc, tr);
Simon Morlat's avatar
Simon Morlat committed
618
	BC_ASSERT_TRUE(wait_for_until(pauline->lc,pauline->lc,&counters->number_of_LinphoneRegistrationOk,2,15000));
619 620 621 622 623 624
	linphone_transports_unref(tr);
	tr = linphone_core_get_transports_used(pauline->lc);
	BC_ASSERT_EQUAL(tr->udp_port, 0, int, "%i");
	BC_ASSERT_EQUAL(tr->tcp_port, LC_SIP_TRANSPORT_DONTBIND, int, "%i");
	BC_ASSERT_EQUAL(tr->tls_port, LC_SIP_TRANSPORT_DONTBIND, int, "%i");
	linphone_transports_unref(tr);
Simon Morlat's avatar
Simon Morlat committed
625 626 627
	linphone_core_manager_destroy(pauline);
}

Simon Morlat's avatar
Simon Morlat committed
628 629 630
static void transport_busy(void){
	LinphoneCoreManager *pauline = linphone_core_manager_new("pauline_tcp_rc");
	LCSipTransports tr;
Ronan's avatar
Ronan committed
631

Simon Morlat's avatar
Simon Morlat committed
632 633 634 635
	memset(&tr, 0, sizeof(tr));
	tr.udp_port = 5070;
	tr.tcp_port = 5070;
	tr.tls_port = 5071;
Ronan's avatar
Ronan committed
636

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

Simon Morlat's avatar
Simon Morlat committed
639 640 641 642 643
	{
		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);
644
		/*BC_ASSERT_EQUAL(tr.udp_port, 0, int, "%i");
Simon Morlat's avatar
Simon Morlat committed
645
		BC_ASSERT_EQUAL(tr.tcp_port, 0, int, "%i");
646
		BC_ASSERT_EQUAL(tr.tls_port, 0, int, "%i");*/
Simon Morlat's avatar
Simon Morlat committed
647 648
		linphone_core_manager_destroy(marie);
	}
Ronan's avatar
Ronan committed
649

Simon Morlat's avatar
Simon Morlat committed
650 651 652
	linphone_core_manager_destroy(pauline);
}

653
static void proxy_transport_change(void){
654 655 656 657 658 659 660
	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*/
661
	linphone_auth_info_unref(info);
662 663
	register_with_refresh_base(lcm->lc,FALSE,auth_domain,NULL);

664
	proxy_config = linphone_core_get_default_proxy_config(lcm->lc);
665 666 667
	reset_counters(counters); /*clear stats*/
	linphone_proxy_config_edit(proxy_config);

668
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
669 670 671 672 673 674 675 676 677 678 679
	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);

680
	BC_ASSERT(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationOk,1));
681
	/*as we change p[roxy server destination, we should'nt be notified about the clear*/
682
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationCleared,0, int, "%d");
683
	ms_free(addr_as_string);
Simon Morlat's avatar
Simon Morlat committed
684
	linphone_address_unref(addr);
685 686 687
	linphone_core_manager_destroy(lcm);

}
688 689 690 691 692
/*
 * On ios, some firewal require to disable flow label (livebox with default firewall level).
 *  sudo sysctl net.inet6.ip6.auto_flowlabel=0
 *  It might be possible to found a sockopt for such purpose.
 */
693
static void proxy_transport_change_with_wrong_port(void) {
694 695 696 697 698
	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];
699
	LinphoneTransports *transport= linphone_transports_new();
700
	sprintf(route,"sip:%s",test_route);
701 702 703 704
	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);
705 706

	linphone_core_add_auth_info(lcm->lc,info); /*add authentication info to LinphoneCore*/
707
	linphone_auth_info_unref(info);
708
	register_with_refresh_base_3(lcm->lc, FALSE, auth_domain, "sip2.linphone.org:5987", 0,transport,LinphoneRegistrationProgress);
709
	linphone_transports_unref(transport);
710

711
	proxy_config = linphone_core_get_default_proxy_config(lcm->lc);
712 713
	linphone_proxy_config_edit(proxy_config);

714
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
715 716 717
	linphone_proxy_config_set_server_addr(proxy_config,route);
	linphone_proxy_config_done(proxy_config);

718
	BC_ASSERT(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationOk,1));
719
	/*as we change proxy server destination, we should'nt be notified about the clear*/
720 721 722 723
	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");
724 725 726 727 728

	linphone_core_manager_destroy(lcm);

}

729
static void proxy_transport_change_with_wrong_port_givin_up(void) {
730 731 732 733 734
	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];
735
	LinphoneTransports *transport = linphone_transports_new();
736
	sprintf(route,"sip:%s",test_route);
737 738 739 740
	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);
741 742

	linphone_core_add_auth_info(lcm->lc,info); /*add authentication info to LinphoneCore*/
743
	linphone_auth_info_unref(info);
744
	register_with_refresh_base_3(lcm->lc, FALSE, auth_domain, "sip2.linphone.org:5987", 0,transport,LinphoneRegistrationProgress);
745
	linphone_transports_unref(transport);
746

747
	proxy_config = linphone_core_get_default_proxy_config(lcm->lc);
748 749
	linphone_proxy_config_edit(proxy_config);

750
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
751 752 753
	linphone_proxy_config_enableregister(proxy_config,FALSE);
	linphone_proxy_config_done(proxy_config);

754 755 756 757
	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");
758 759 760 761 762

	linphone_core_manager_destroy(lcm);

}

763
static void io_recv_error(void){
764
	LinphoneCoreManager *lcm;
jehan's avatar
jehan committed
765 766 767
	LinphoneCore* lc;
	int register_ok;
	stats* counters ;
768
	int number_of_udp_proxy=0;
jehan's avatar
jehan committed
769

770

771 772 773 774 775 776 777
	lcm=configure_lcm();
	if (lcm) {
		lc=lcm->lc;
		counters = get_stats(lc);
		register_ok=counters->number_of_LinphoneRegistrationOk;
		number_of_udp_proxy=get_number_of_udp_proxy(lc);
		sal_set_recv_error(lc->sal, 0);
jehan's avatar
jehan committed
778

779 780
		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");
781

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

784 785
		linphone_core_manager_destroy(lcm);
	}
jehan's avatar
jehan committed
786 787
}

788
static void io_recv_error_retry_immediatly(void){
789
	LinphoneCoreManager *lcm;
jehan's avatar
jehan committed
790 791 792 793 794
	LinphoneCore* lc;
	int register_ok;
	stats* counters ;
	int number_of_udp_proxy=0;

795 796 797 798 799 800 801
	lcm=configure_lcm();
	if (lcm) {
		lc=lcm->lc;
		counters = get_stats(lc);
		register_ok=counters->number_of_LinphoneRegistrationOk;
		number_of_udp_proxy=get_number_of_udp_proxy(lc);
		sal_set_recv_error(lc->sal, 0);
jehan's avatar
jehan committed
802

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

807
		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
808

809 810
		linphone_core_manager_destroy(lcm);
	}
jehan's avatar
jehan committed
811 812
}

813
static void io_recv_error_late_recovery(void){
814 815 816 817 818 819
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager *lcm;
		LinphoneCore* lc;
		int register_ok;
		stats* counters ;
		int number_of_udp_proxy=0;
820
		bctbx_list_t* lcs;
821
		lcm=linphone_core_manager_new2( "multi_account_rc",FALSE); /*to make sure iterates are not call yet*/
822 823 824
		lc=lcm->lc;
		sal_set_refresher_retry_after(lc->sal,1000);
		counters=&lcm->stat;
825
		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
826 827


828 829 830 831 832 833
		counters = get_stats(lc);
		register_ok=counters->number_of_LinphoneRegistrationOk;
		number_of_udp_proxy=get_number_of_udp_proxy(lc);
		/*simulate a general socket error*/
		sal_set_recv_error(lc->sal, 0);
		sal_set_send_error(lc->sal, -1);
jehan's avatar
jehan committed
834

835 836
		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
837

838
		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(lc->sal)+3000));
jehan's avatar
jehan committed
839

840 841
		sal_set_recv_error(lc->sal, 1); /*reset*/
		sal_set_send_error(lc->sal, 0);
jehan's avatar
jehan committed
842

843
		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(lc->sal)+3000));
844
		linphone_core_manager_destroy(lcm);
845
	}
jehan's avatar
jehan committed
846 847
}

848
static void io_recv_error_without_active_register(void){
849
	LinphoneCoreManager *lcm;
jehan's avatar
jehan committed
850 851 852
	LinphoneCore* lc;
	int register_ok;
	stats* counters ;
853
	bctbx_list_t* proxys;
854
	int dummy=0;
jehan's avatar
jehan committed
855

856 857 858 859
	lcm=configure_lcm();
	if (lcm) {
		lc=lcm->lc;
		counters = get_stats(lc);
860

861
		register_ok=counters->number_of_LinphoneRegistrationOk;
jehan's avatar
jehan committed
862

863
		for (proxys=bctbx_list_copy(linphone_core_get_proxy_config_list(lc));proxys!=NULL;proxys=proxys->next) {
864 865 866 867 868
			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);
		}
869
		bctbx_list_free(proxys);
870 871
		/*wait for unregistrations*/
		BC_ASSERT_TRUE(wait_for(lc,lc,&counters->number_of_LinphoneRegistrationCleared,register_ok /*because 1 udp*/));
872

873
		sal_set_recv_error(lc->sal, 0);
jehan's avatar
jehan committed
874

875 876
		/*nothing should happen because no active registration*/
		wait_for_until(lc,lc, &dummy, 1, 3000);
877
		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
878

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

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

883 884
		linphone_core_manager_destroy(lcm);
	}
jehan's avatar
jehan committed
885 886
}

jehan's avatar
jehan committed
887

888
static void tls_certificate_failure(void){
889 890 891
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager* lcm;
		LinphoneCore *lc;
892
		char *rootcapath = bc_tester_res("certificates/cn/agent.pem"); /*bad root ca*/
893

894 895
		lcm=linphone_core_manager_new2("pauline_rc",FALSE);
		lc=lcm->lc;
896 897 898 899 900 901
		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));
902
		bc_free(rootcapath);
903
		rootcapath = bc_tester_res("certificates/cn/cafile.pem"); /*good root ca*/
904 905 906 907
		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");
908
		linphone_core_manager_destroy(lcm);
909
		bc_free(rootcapath);
910
	}
jehan's avatar
jehan committed
911
}
Simon Morlat's avatar
Simon Morlat committed
912

913 914 915 916 917
char *read_file(const char *path) {
	long  numbytes = 0;
	size_t readbytes;
	char *buffer = NULL;
	FILE *infile = fopen(path, "rb");
Ronan's avatar
Ronan committed
918

919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
	BC_ASSERT_PTR_NOT_NULL(infile);
	if (infile) {
		fseek(infile, 0L, SEEK_END);
		numbytes = ftell(infile);
		fseek(infile, 0L, SEEK_SET);
		buffer = (char*)ms_malloc((numbytes + 1) * sizeof(char));
		readbytes = fread(buffer, sizeof(char), numbytes, infile);
		fclose(infile);
		buffer[readbytes] = '\0';
	}
	return buffer;
}

static void tls_certificate_data(void) {
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager* lcm;
		LinphoneCore *lc;
		char *rootcapath = bc_tester_res("certificates/cn/agent.pem"); /*bad root ca*/
		char *data = read_file(rootcapath);

		lcm = linphone_core_manager_new2("pauline_rc",FALSE);
		lc = lcm->lc;
		linphone_core_set_root_ca_data(lcm->lc, data);
		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_data(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));
947
		bc_free(rootcapath);
948 949 950 951 952 953 954 955
		ms_free(data);
		rootcapath = bc_tester_res("certificates/cn/cafile.pem"); /*good root ca*/
		data = read_file(rootcapath);
		linphone_core_set_root_ca_data(lcm->lc, data);
		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");
		linphone_core_manager_destroy(lcm);
956
		bc_free(rootcapath);
957 958 959 960
		ms_free(data);
	}
}

961
/*the purpose of this test is to check that will not block the proxy config during SSL handshake for entire life in case of mistaken configuration*/
962
static void tls_with_non_tls_server(void){
963 964 965 966 967 968 969 970 971
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager *lcm;
		LinphoneProxyConfig* proxy_cfg;
		LinphoneAddress* addr;
		char tmp[256];
		LinphoneCore *lc;

		lcm=linphone_core_manager_new2( "marie_rc", 0);
		lc=lcm->lc;
972
		sal_set_transport_timeout(lc->sal,3000);
973
		proxy_cfg = linphone_core_get_default_proxy_config(lc);
974 975 976 977 978 979
		linphone_proxy_config_edit(proxy_cfg);
		addr=linphone_address_new(linphone_proxy_config_get_addr(proxy_cfg));
		snprintf(tmp,sizeof(tmp),"sip:%s:%i;transport=tls"	,linphone_address_get_domain(addr)
				,(linphone_address_get_port(addr)>0?linphone_address_get_port(addr):5060));
		linphone_proxy_config_set_server_addr(proxy_cfg,tmp);
		linphone_proxy_config_done(proxy_cfg);
Simon Morlat's avatar
Simon Morlat committed
980
		linphone_address_unref(addr);
981
		BC_ASSERT_TRUE(wait_for_until(lc,lc,&lcm->stat.number_of_LinphoneRegistrationFailed,1,10000));