register_tester.c 47.3 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 72 73
static void register_with_refresh_base_3(LinphoneCore* lc
											, bool_t refresh
											,const char* domain
											,const char* route
											,bool_t late_auth_info
											,LCSipTransports transport
											,LinphoneRegistrationState expected_final_state) {
jehan's avatar
jehan committed
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 87 88 89 90
	reset_counters(counters);
	linphone_core_set_sip_transports(lc,&transport);

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

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

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

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

180
	linphone_core_manager_destroy(lcm);
jehan's avatar
jehan committed
181
}
182

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

191 192 193 194 195
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;
196

197 198 199 200
	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.*/
201

202 203 204
	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");
205
	BC_ASSERT_PTR_NOT_NULL(value);
206
	if (value) BC_ASSERT_PTR_NOT_NULL(strstr(value, "Flexisip"));
207 208 209
	linphone_core_manager_destroy(marie);
}

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

216
	proxy_config = linphone_core_get_default_proxy_config(lcm->lc);
217 218 219 220 221

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

	/*nothing is supposed to arrive until done*/
222
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
223 224
	linphone_proxy_config_enable_register(proxy_config,FALSE);
	linphone_proxy_config_done(proxy_config);
225
	BC_ASSERT_TRUE(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1));
226 227 228
	linphone_core_manager_destroy(lcm);
}

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

235
	proxy_config = linphone_core_get_default_proxy_config(lcm->lc);
236 237 238 239

	linphone_proxy_config_edit(proxy_config);

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

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

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

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

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

281
static void simple_tcp_register_compatibility_mode(void){
282
	char route[256];
283
	LinphoneCoreManager* lcm;
284
	LCSipTransports transport = {0,5070,0,0};
285
	sprintf(route,"sip:%s",test_route);
286 287 288
	lcm = create_lcm();
	register_with_refresh_base_2(lcm->lc,FALSE,test_domain,route,FALSE,transport);
	linphone_core_manager_destroy(lcm);
289 290
}

291
static void simple_tls_register(void){
292 293 294
	if (transport_supported(LinphoneTransportTls)) {
		char route[256];
		LinphoneCoreManager* lcm = create_lcm();
295 296
		sprintf(route,"sip:%s;transport=tls",test_route);
		register_with_refresh(lcm,FALSE,test_domain,route);
297
		linphone_core_manager_destroy(lcm);
298
	}
jehan's avatar
jehan committed
299 300
}

301

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

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

333
static void authenticated_register_with_no_initial_credentials(void){
334
	LinphoneCoreManager *lcm;
335
	LinphoneCoreCbs *cbs = linphone_factory_create_core_cbs(linphone_factory_get());
336
	stats* counters;
337
	char route[256];
338

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

341
	lcm = linphone_core_manager_new(NULL);
342

343 344 345
	linphone_core_cbs_set_authentication_requested(cbs, authentication_requested);
	linphone_core_add_callbacks(lcm->lc, cbs);
	linphone_core_cbs_unref(cbs);
346

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

jehan's avatar
jehan committed
354

355
static void authenticated_register_with_late_credentials(void){
356
	LinphoneCoreManager *lcm;
357
	stats* counters;
358
	char route[256];
359
	LCSipTransports transport = {5070,5070,0,5071};
360

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

363
	lcm =  linphone_core_manager_new(NULL);
364

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

371
static void authenticated_register_with_provided_credentials(void){
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
	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
395
	linphone_address_unref(from);
396

397 398
	ai = linphone_auth_info_new(test_username, NULL, test_password, NULL, NULL, NULL);
	linphone_core_add_auth_info(lcm->lc, ai);
399
	linphone_auth_info_unref(ai);
400
	linphone_core_add_proxy_config(lcm->lc, cfg);
401

402 403
	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");
404

405 406
	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));
407 408

	linphone_proxy_config_destroy(cfg);
409 410 411
	linphone_core_manager_destroy(lcm);
}

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

	test_password=wrong_passwd;

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

424
	lcm =  linphone_core_manager_new(NULL);
425

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

433
	linphone_core_manager_destroy(lcm);
434 435
}

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

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

444
	sal_set_refresher_retry_after(lcm->lc->sal,500);
445
	if (user_agent) {
446
		linphone_core_set_user_agent(lcm->lc,user_agent,NULL);
447
	}
448
	linphone_core_add_auth_info(lcm->lc,info); /*add wrong authentication info to LinphoneCore*/
449
	linphone_auth_info_unref(info);
450 451
	counters = get_stats(lcm->lc);
	register_with_refresh_base_3(lcm->lc,TRUE,auth_domain,route,FALSE,transport,LinphoneRegistrationFailed);
452
	//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
453
	/*wait for retry*/
454
	BC_ASSERT_TRUE(wait_for(lcm->lc,lcm->lc,&counters->number_of_auth_info_requested,4));
455
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationFailed,1, int, "%d");
456

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

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

487
	authenticated_register_with_wrong_credentials_with_params_base(NULL,lcm);
488

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

497
	linphone_core_manager_destroy(lcm);
498
}
499
static void authenticated_register_with_wrong_credentials_without_403(void) {
500 501
	authenticated_register_with_wrong_credentials_with_params("tester-no-403");
}
502
static LinphoneCoreManager* configure_lcm(void) {
503 504
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager *lcm=linphone_core_manager_new2( "multi_account_rc", FALSE);
505
		stats *counters=&lcm->stat;
506
		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))));
507 508 509 510
		BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationFailed,0, int, "%d");
		return lcm;
	}
	return NULL;
jehan's avatar
jehan committed
511 512
}

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

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

527 528 529 530 531 532 533 534 535 536 537
		counters = get_stats(lc);
		register_ok=counters->number_of_LinphoneRegistrationOk;
		linphone_core_set_network_reachable(lc,FALSE);
		BC_ASSERT_TRUE(wait_for(lc,lc,&counters->number_of_NetworkReachableFalse,1));
		BC_ASSERT_TRUE(wait_for(lc,lc,&counters->number_of_LinphoneRegistrationNone,register_ok));
		linphone_core_set_network_reachable(lc,TRUE);
		BC_ASSERT_TRUE(wait_for(lc,lc,&counters->number_of_NetworkReachableTrue,1));
		wait_for(lc,lc,&counters->number_of_LinphoneRegistrationOk,2*register_ok);

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

560 561 562 563 564 565
	lcm=configure_lcm();
	if (lcm) {
		memset(&sip_tr,0,sizeof(sip_tr));
		lc=lcm->lc;
		counters = get_stats(lc);
		register_ok=counters->number_of_LinphoneRegistrationOk;
566

567
		number_of_udp_proxy=get_number_of_udp_proxy(lc);
568
		total_number_of_proxies=(int)bctbx_list_size(linphone_core_get_proxy_config_list(lc));
569
		linphone_core_get_sip_transports(lc,&sip_tr_orig);
570

571
		sip_tr.udp_port=sip_tr_orig.udp_port;
572

573 574 575
		/*keep only udp*/
		linphone_core_set_sip_transports(lc,&sip_tr);
		BC_ASSERT_TRUE(wait_for(lc,lc,&counters->number_of_LinphoneRegistrationOk,register_ok+number_of_udp_proxy));
576

577
		BC_ASSERT_TRUE(wait_for(lc,lc,&counters->number_of_LinphoneRegistrationFailed,total_number_of_proxies-number_of_udp_proxy));
578

579 580
		linphone_core_manager_destroy(lcm);
	}
581 582
}

Simon Morlat's avatar
Simon Morlat committed
583 584 585 586 587 588 589 590 591 592 593
static void transport_dont_bind(void){
	LinphoneCoreManager *pauline = linphone_core_manager_new("pauline_tcp_rc");
	stats* counters = &pauline->stat;
	LCSipTransports tr;
	
	memset(&tr, 0, sizeof(tr));
	tr.udp_port = 0;
	tr.tcp_port = LC_SIP_TRANSPORT_DONTBIND;
	tr.tls_port = LC_SIP_TRANSPORT_DONTBIND;
	
	linphone_core_set_sip_transports(pauline->lc, &tr);
Simon Morlat's avatar
Simon Morlat committed
594
	BC_ASSERT_TRUE(wait_for_until(pauline->lc,pauline->lc,&counters->number_of_LinphoneRegistrationOk,2,15000));
Simon Morlat's avatar
Simon Morlat committed
595 596 597 598 599 600 601 602
	memset(&tr, 0, sizeof(tr));
	linphone_core_get_sip_transports_used(pauline->lc, &tr);
	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_core_manager_destroy(pauline);
}

Simon Morlat's avatar
Simon Morlat committed
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
static void transport_busy(void){
	LinphoneCoreManager *pauline = linphone_core_manager_new("pauline_tcp_rc");
	LCSipTransports tr;
	
	memset(&tr, 0, sizeof(tr));
	tr.udp_port = 5070;
	tr.tcp_port = 5070;
	tr.tls_port = 5071;
	
	linphone_core_set_sip_transports(pauline->lc, &tr);
	
	{
		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);
619
		/*BC_ASSERT_EQUAL(tr.udp_port, 0, int, "%i");
Simon Morlat's avatar
Simon Morlat committed
620
		BC_ASSERT_EQUAL(tr.tcp_port, 0, int, "%i");
621
		BC_ASSERT_EQUAL(tr.tls_port, 0, int, "%i");*/
Simon Morlat's avatar
Simon Morlat committed
622 623 624 625 626 627
		linphone_core_manager_destroy(marie);
	}
	
	linphone_core_manager_destroy(pauline);
}

628
static void proxy_transport_change(void){
629 630 631 632 633 634 635
	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*/
636
	linphone_auth_info_unref(info);
637 638
	register_with_refresh_base(lcm->lc,FALSE,auth_domain,NULL);

639
	proxy_config = linphone_core_get_default_proxy_config(lcm->lc);
640 641 642
	reset_counters(counters); /*clear stats*/
	linphone_proxy_config_edit(proxy_config);

643
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
644 645 646 647 648 649 650 651 652 653 654
	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);

655
	BC_ASSERT(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationOk,1));
656
	/*as we change p[roxy server destination, we should'nt be notified about the clear*/
657
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationCleared,0, int, "%d");
658
	ms_free(addr_as_string);
Simon Morlat's avatar
Simon Morlat committed
659
	linphone_address_unref(addr);
660 661 662
	linphone_core_manager_destroy(lcm);

}
663
static void proxy_transport_change_with_wrong_port(void) {
664 665 666 667 668 669 670 671 672
	LinphoneCoreManager* lcm = create_lcm();
	stats* counters = &lcm->stat;
	LinphoneProxyConfig* proxy_config;
	LinphoneAuthInfo *info=linphone_auth_info_new(test_username,NULL,test_password,NULL,auth_domain,NULL); /*create authentication structure from identity*/
	char route[256];
	LCSipTransports transport= {LC_SIP_TRANSPORT_RANDOM,LC_SIP_TRANSPORT_RANDOM,LC_SIP_TRANSPORT_RANDOM,LC_SIP_TRANSPORT_RANDOM};
	sprintf(route,"sip:%s",test_route);

	linphone_core_add_auth_info(lcm->lc,info); /*add authentication info to LinphoneCore*/
673
	linphone_auth_info_unref(info);
674 675
	register_with_refresh_base_3(lcm->lc, FALSE, auth_domain, "sip2.linphone.org:5987", 0,transport,LinphoneRegistrationProgress);

676
	proxy_config = linphone_core_get_default_proxy_config(lcm->lc);
677 678
	linphone_proxy_config_edit(proxy_config);

679
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
680 681 682
	linphone_proxy_config_set_server_addr(proxy_config,route);
	linphone_proxy_config_done(proxy_config);

683
	BC_ASSERT(wait_for(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationOk,1));
684
	/*as we change proxy server destination, we should'nt be notified about the clear*/
685 686 687 688
	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");
689 690 691 692 693

	linphone_core_manager_destroy(lcm);

}

694
static void proxy_transport_change_with_wrong_port_givin_up(void) {
695 696 697 698 699 700 701 702 703
	LinphoneCoreManager* lcm = create_lcm();
	stats* counters = &lcm->stat;
	LinphoneProxyConfig* proxy_config;
	LinphoneAuthInfo *info=linphone_auth_info_new(test_username,NULL,test_password,NULL,auth_domain,NULL); /*create authentication structure from identity*/
	char route[256];
	LCSipTransports transport= {LC_SIP_TRANSPORT_RANDOM,LC_SIP_TRANSPORT_RANDOM,LC_SIP_TRANSPORT_RANDOM,LC_SIP_TRANSPORT_RANDOM};
	sprintf(route,"sip:%s",test_route);

	linphone_core_add_auth_info(lcm->lc,info); /*add authentication info to LinphoneCore*/
704
	linphone_auth_info_unref(info);
705 706
	register_with_refresh_base_3(lcm->lc, FALSE, auth_domain, "sip2.linphone.org:5987", 0,transport,LinphoneRegistrationProgress);

707
	proxy_config = linphone_core_get_default_proxy_config(lcm->lc);
708 709
	linphone_proxy_config_edit(proxy_config);

710
	BC_ASSERT_FALSE(wait_for_until(lcm->lc,lcm->lc,&counters->number_of_LinphoneRegistrationCleared,1,3000));
711 712 713
	linphone_proxy_config_enableregister(proxy_config,FALSE);
	linphone_proxy_config_done(proxy_config);

714 715 716 717
	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");
718 719 720 721 722

	linphone_core_manager_destroy(lcm);

}

723
static void io_recv_error(void){
724
	LinphoneCoreManager *lcm;
jehan's avatar
jehan committed
725 726 727
	LinphoneCore* lc;
	int register_ok;
	stats* counters ;
728
	int number_of_udp_proxy=0;
jehan's avatar
jehan committed
729

730

731 732 733 734 735 736 737
	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
738

739 740
		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");
741

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

744 745
		linphone_core_manager_destroy(lcm);
	}
jehan's avatar
jehan committed
746 747
}

748
static void io_recv_error_retry_immediatly(void){
749
	LinphoneCoreManager *lcm;
jehan's avatar
jehan committed
750 751 752 753 754
	LinphoneCore* lc;
	int register_ok;
	stats* counters ;
	int number_of_udp_proxy=0;

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

763 764 765
		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
766

767
		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
768

769 770
		linphone_core_manager_destroy(lcm);
	}
jehan's avatar
jehan committed
771 772
}

773
static void io_recv_error_late_recovery(void){
774 775 776 777 778 779
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager *lcm;
		LinphoneCore* lc;
		int register_ok;
		stats* counters ;
		int number_of_udp_proxy=0;
780
		bctbx_list_t* lcs;
781
		lcm=linphone_core_manager_new2( "multi_account_rc",FALSE); /*to make sure iterates are not call yet*/
782 783 784
		lc=lcm->lc;
		sal_set_refresher_retry_after(lc->sal,1000);
		counters=&lcm->stat;
785
		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
786 787


788 789 790 791 792 793
		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
794

795 796
		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
797

798
		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
799

800 801
		sal_set_recv_error(lc->sal, 1); /*reset*/
		sal_set_send_error(lc->sal, 0);
jehan's avatar
jehan committed
802

803
		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));
804
		linphone_core_manager_destroy(lcm);
805
	}
jehan's avatar
jehan committed
806 807
}

808
static void io_recv_error_without_active_register(void){
809
	LinphoneCoreManager *lcm;
jehan's avatar
jehan committed
810 811 812
	LinphoneCore* lc;
	int register_ok;
	stats* counters ;
813
	bctbx_list_t* proxys;
814
	int dummy=0;
jehan's avatar
jehan committed
815

816 817 818 819
	lcm=configure_lcm();
	if (lcm) {
		lc=lcm->lc;
		counters = get_stats(lc);
820

821
		register_ok=counters->number_of_LinphoneRegistrationOk;
jehan's avatar
jehan committed
822

823
		for (proxys=bctbx_list_copy(linphone_core_get_proxy_config_list(lc));proxys!=NULL;proxys=proxys->next) {
824 825 826 827 828
			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);
		}
829
		bctbx_list_free(proxys);
830 831
		/*wait for unregistrations*/
		BC_ASSERT_TRUE(wait_for(lc,lc,&counters->number_of_LinphoneRegistrationCleared,register_ok /*because 1 udp*/));
832

833
		sal_set_recv_error(lc->sal, 0);
jehan's avatar
jehan committed
834

835 836
		/*nothing should happen because no active registration*/
		wait_for_until(lc,lc, &dummy, 1, 3000);
837
		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
838

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

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

843 844
		linphone_core_manager_destroy(lcm);
	}
jehan's avatar
jehan committed
845 846
}

jehan's avatar
jehan committed
847

848
static void tls_certificate_failure(void){
849 850 851
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager* lcm;
		LinphoneCore *lc;
852
		char *rootcapath = bc_tester_res("certificates/cn/agent.pem"); /*bad root ca*/
853

854 855
		lcm=linphone_core_manager_new2("pauline_rc",FALSE);
		lc=lcm->lc;
856 857 858 859 860 861
		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));
862
		bc_free(rootcapath);
863
		rootcapath = bc_tester_res("certificates/cn/cafile.pem"); /*good root ca*/
864 865 866 867
		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");
868
		linphone_core_manager_destroy(lcm);
869
		bc_free(rootcapath);
870
	}
jehan's avatar
jehan committed
871
}
Simon Morlat's avatar
Simon Morlat committed
872

873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
char *read_file(const char *path) {
	long  numbytes = 0;
	size_t readbytes;
	char *buffer = NULL;
	FILE *infile = fopen(path, "rb");
	
	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));
907
		bc_free(rootcapath);
908 909 910 911 912 913 914 915
		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);
916
		bc_free(rootcapath);
917 918 919 920
		ms_free(data);
	}
}

921
/*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*/
922
static void tls_with_non_tls_server(void){
923 924 925 926 927 928 929 930 931
	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;
932
		sal_set_transport_timeout(lc->sal,3000);
933
		proxy_cfg = linphone_core_get_default_proxy_config(lc);
934 935 936 937 938 939
		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
940
		linphone_address_unref(addr);
941
		BC_ASSERT_TRUE(wait_for_until(lc,lc,&lcm->stat.number_of_LinphoneRegistrationFailed,1,10000));
942
		linphone_core_manager_destroy(lcm);
943
	}
Simon Morlat's avatar
Simon Morlat committed
944
}
jehan's avatar
jehan committed
945

946
static void tls_alt_name_register(void){
947 948 949
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager* lcm;
		LinphoneCore *lc;
950
		char *rootcapath = bc_tester_res("certificates/cn/cafile.pem");
951

952 953
		lcm=linphone_core_manager_new2("pauline_alt_rc",FALSE);
		lc=lcm->lc;
954 955 956 957
		linphone_core_set_root_ca(lc,rootcapath);
		linphone_core_refresh_registers(lc);
		BC_ASSERT_TRUE(wait_for(lc,lc,&lcm->stat.number_of_LinphoneRegistrationOk,1));
		BC_ASSERT_EQUAL(lcm->stat.number_of_LinphoneRegistrationFailed,0, int, "%d");
958
		linphone_core_manager_destroy(lcm);
959
		bc_free(rootcapath);
960
	}
961 962
}

963
static void tls_wildcard_register(void){
964 965 966
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager* lcm;
		LinphoneCore *lc;
967
		char *rootcapath = bc_tester_res("certificates/cn/cafile.pem");
968

969 970
		lcm=linphone_core_manager_new2("pauline_wild_rc",FALSE);
		lc=lcm->lc;
971 972 973 974
		linphone_core_set_root_ca(lc,rootcapath);
		linphone_core_refresh_registers(lc);
		BC_ASSERT_TRUE(wait_for(lc,lc,&lcm->stat.number_of_LinphoneRegistrationOk,2));
		BC_ASSERT_EQUAL(lcm->stat.number_of_LinphoneRegistrationFailed,0, int, "%d");
975
		linphone_core_manager_destroy(lcm);
976
		bc_free(rootcapath);
977
	}
978 979
}

980
static void redirect(void){
jehan's avatar
jehan committed
981 982 983 984 985
	char route[256];
	LinphoneCoreManager* lcm;
	LCSipTransports transport = {-1,0,0,0};
	sprintf(route,"sip:%s:5064",test_route);
	lcm = create_lcm();
986 987 988 989 990
	if (lcm) {
		linphone_core_set_user_agent(lcm->lc,"redirect",NULL);
		register_with_refresh_base_2(lcm->lc,FALSE,test_domain,route,FALSE,transport);
		linphone_core_manager_destroy(lcm);
	}
jehan's avatar
jehan committed
991 992
}

993 994
static void tls_auth_global_client_cert(void) {
	if (transport_supported(LinphoneTransportTls)) {
995 996 997 998 999 1000 1001 1002 1003 1004
		LinphoneCoreManager *manager = ms_new0(LinphoneCoreManager, 1);
		LpConfig *lpc = NULL;
		char *cert_path = bc_tester_res("certificates/client/cert.pem");
		char *key_path = bc_tester_res("certificates/client/key.pem");
		linphone_core_manager_init(manager, "pauline_tls_client_rc", NULL);
		lpc = manager->lc->config;
		lp_config_set_string(lpc, "sip", "client_cert_chain", cert_path);
		lp_config_set_string(lpc, "sip", "client_cert_key", key_path);
		linphone_core_manager_start(manager, TRUE);
		linphone_core_manager_destroy(manager);
1005 1006
		bc_free(cert_path);
		bc_free(key_path);
1007 1008 1009 1010 1011
	}
}

static void tls_auth_global_client_cert_api(void) {
	if (transport_supported(LinphoneTransportTls)) {
1012
		LinphoneCoreManager *pauline = linphone_core_manager_new2("pauline_tls_client_rc", FALSE);
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
		char *cert_path = bc_tester_res("certificates/client/cert.pem");
		char *key_path = bc_tester_res("certificates/client/key.pem");
		char *cert = read_file(cert_path);
		char *key = read_file(key_path);
		LinphoneCore *lc = pauline->lc;
		linphone_core_set_tls_cert(lc, cert);
		linphone_core_set_tls_key(lc, key);
		BC_ASSERT_TRUE(wait_for(lc, lc, &pauline->stat.number_of_LinphoneRegistrationOk, 1));
		linphone_core_manager_destroy(pauline);
		ms_free(cert);
		ms_free(key);
1024 1025
		bc_free(cert_path);
		bc_free(key_path);
1026 1027 1028 1029 1030
	}
}

static void tls_auth_global_client_cert_api_path(void) {
	if (transport_supported(LinphoneTransportTls)) {
1031
		LinphoneCoreManager *pauline = linphone_core_manager_new2("pauline_tls_client_rc", FALSE);
1032 1033 1034 1035 1036 1037 1038
		char *cert = bc_tester_res("certificates/client/cert.pem");
		char *key = bc_tester_res("certificates/client/key.pem");
		LinphoneCore *lc = pauline->lc;
		linphone_core_set_tls_cert_path(lc, cert);
		linphone_core_set_tls_key_path(lc, key);
		BC_ASSERT_TRUE(wait_for(lc, lc, &pauline->stat.number_of_LinphoneRegistrationOk, 1));
		linphone_core_manager_destroy(pauline);
1039 1040
		bc_free(cert);
		bc_free(key);
1041 1042 1043 1044 1045
	}
}

static void tls_auth_info_client_cert_api<