register_tester.c 50.9 KB
Newer Older
jehan's avatar
jehan committed
1 2 3 4 5 6
/*
	belle-sip - SIP (RFC3261) library.
    Copyright (C) 2010  Belledonne Communications SARL

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
Simon Morlat's avatar
Simon Morlat committed
7
    the Free Software Foundation, either version 2 of the License, or
jehan's avatar
jehan committed
8 9 10 11 12 13 14 15 16 17
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
Ghislain MARY's avatar
Ghislain MARY committed
18

19

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

23

24 25 26 27
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*/
}
28

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

32
	if (with_auth) {
Ghislain MARY's avatar
Ghislain MARY committed
33 34 35 36
		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);
37
	}
38

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

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

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

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

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

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

	proxy_cfg = linphone_proxy_config_new();

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

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

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

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

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

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

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

156 157 158 159
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);
160
	BC_ASSERT_EQUAL(counters->number_of_LinphoneRegistrationCleared,1, int, "%d");
jehan's avatar
jehan committed
161 162
}

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

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

184
	linphone_core_manager_destroy(lcm);
jehan's avatar
jehan committed
185
}
Ghislain MARY's avatar
Ghislain MARY committed
186

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

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

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

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

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

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

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

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

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

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

	linphone_proxy_config_edit(proxy_config);

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

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

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

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

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

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

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

308

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

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

340
static void authenticated_register_with_no_initial_credentials(void){
341
	LinphoneCoreManager *lcm;
Ghislain MARY's avatar
Ghislain MARY committed
342
	LinphoneCoreCbs *cbs = linphone_factory_create_core_cbs(linphone_factory_get());
343
	stats* counters;
344
	char route[256];
345

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

348
	lcm = linphone_core_manager_new(NULL);
349

Ghislain MARY's avatar
Ghislain MARY committed
350 351 352
	linphone_core_cbs_set_authentication_requested(cbs, authentication_requested);
	linphone_core_add_callbacks(lcm->lc, cbs);
	linphone_core_cbs_unref(cbs);
353

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

jehan's avatar
jehan committed
361

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

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

370
	lcm =  linphone_core_manager_new(NULL);
371
	transport = linphone_factory_create_transports(linphone_factory_get());
372 373 374
	linphone_transports_set_udp_port(transport, 5070);
	linphone_transports_set_tcp_port(transport, 5070);
	linphone_transports_set_dtls_port(transport, 5071);
375

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

383
static void authenticated_register_with_provided_credentials(void){
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
	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
407
	linphone_address_unref(from);
408

409 410
	ai = linphone_auth_info_new(test_username, NULL, test_password, NULL, NULL, NULL);
	linphone_core_add_auth_info(lcm->lc, ai);
Ghislain MARY's avatar
Ghislain MARY committed
411
	linphone_auth_info_unref(ai);
412
	linphone_core_add_proxy_config(lcm->lc, cfg);
413

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

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

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

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

	test_password=wrong_passwd;

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

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

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

451
	linphone_core_manager_destroy(lcm);
452 453
}

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

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

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

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

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

510
	authenticated_register_with_wrong_credentials_with_params_base(NULL,lcm);
511

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

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

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

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

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

584 585 586
	lcm=configure_lcm();
	if (lcm) {
		lc=lcm->lc;
587
		sip_tr = linphone_factory_create_transports(linphone_factory_get());
588 589
		counters = get_stats(lc);
		register_ok=counters->number_of_LinphoneRegistrationOk;
jehan's avatar
jehan committed
590

591
		number_of_udp_proxy=get_number_of_udp_proxy(lc);
Ghislain MARY's avatar
Ghislain MARY committed
592
		total_number_of_proxies=(int)bctbx_list_size(linphone_core_get_proxy_config_list(lc));
593
		sip_tr_orig = linphone_core_get_transports(lc);
jehan's avatar
jehan committed
594

595
		linphone_transports_set_udp_port(sip_tr, linphone_transports_get_udp_port(sip_tr_orig));
596

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

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

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

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

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

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

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

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

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

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

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

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

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

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

}
687 688 689 690 691
/*
 * 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.
 */
692
static void proxy_transport_change_with_wrong_port(void) {
693 694 695 696 697
	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];
698
	LinphoneTransports *transport= linphone_factory_create_transports(linphone_factory_get());
699
	sprintf(route,"sip:%s",test_route);
700 701 702 703
	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);
704 705

	linphone_core_add_auth_info(lcm->lc,info); /*add authentication info to LinphoneCore*/
Ghislain MARY's avatar
Ghislain MARY committed
706
	linphone_auth_info_unref(info);