tester.c 16 KB
Newer Older
1
 /*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 tester - liblinphone test suite
 Copyright (C) 2013  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
 the Free Software Foundation, either version 2 of the License, or
 (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/>.
 */

#include <stdio.h>
20
#include "CUnit/TestRun.h"
21
#include "CUnit/Automated.h"
22 23 24 25 26 27
#include "linphonecore.h"
#include "private.h"
#include "liblinphone_tester.h"
#if HAVE_CU_CURSES
#include "CUnit/CUCurses.h"
#endif
28 29 30
#ifdef HAVE_GTK
#include <gtk/gtk.h>
#endif
Ghislain MARY's avatar
Ghislain MARY committed
31 32 33
#if _WIN32
#define unlink _unlink
#endif
34

35 36
static bool_t liblinphone_tester_ipv6_enabled=FALSE;
static int liblinphone_tester_keep_accounts_flag = 0;
37
static int liblinphone_tester_keep_record_files = FALSE;
38
int manager_count = 0;
39

Simon Morlat's avatar
Simon Morlat committed
40 41
const MSAudioDiffParams audio_cmp_params = {10,2000};

42 43 44 45 46 47
const char* test_domain="sipopen.example.org";
const char* auth_domain="sip.example.org";
const char* test_username="liblinphone_tester";
const char* test_password="secret";
const char* test_route="sip2.linphone.org";
const char *userhostsfile = "tester_hosts";
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
48

49 50
const char *liblinphone_tester_mire_id="Mire: Mire (synthetic moving picture)";

51
static void network_reachable(LinphoneCore *lc, bool_t reachable) {
52 53 54 55 56 57 58 59
	stats* counters;
	ms_message("Network reachable [%s]",reachable?"TRUE":"FALSE");
	counters = get_stats(lc);
	if (reachable)
		counters->number_of_NetworkReachableTrue++;
	else
		counters->number_of_NetworkReachableFalse++;
}
60 61 62 63 64 65 66 67 68 69 70 71
void liblinphone_tester_clock_start(MSTimeSpec *start){
	ms_get_cur_time(start);
}

bool_t liblinphone_tester_clock_elapsed(const MSTimeSpec *start, int value_ms){
	MSTimeSpec current;
	ms_get_cur_time(&current);
	if ((((current.tv_sec-start->tv_sec)*1000LL) + ((current.tv_nsec-start->tv_nsec)/1000000LL))>=value_ms)
		return TRUE;
	return FALSE;
}

72 73 74 75
void liblinphone_tester_enable_ipv6(bool_t enabled){
	liblinphone_tester_ipv6_enabled=enabled;
}

76 77
LinphoneAddress * create_linphone_address(const char * domain) {
	LinphoneAddress *addr = linphone_address_new(NULL);
78
	BC_ASSERT_PTR_NOT_NULL_FATAL(addr);
79
	linphone_address_set_username(addr,test_username);
80
	BC_ASSERT_STRING_EQUAL(test_username,linphone_address_get_username(addr));
81 82
	if (!domain) domain= test_route;
	linphone_address_set_domain(addr,domain);
83
	BC_ASSERT_STRING_EQUAL(domain,linphone_address_get_domain(addr));
84 85
	linphone_address_set_display_name(addr, NULL);
	linphone_address_set_display_name(addr, "Mr Tester");
86
	BC_ASSERT_STRING_EQUAL("Mr Tester",linphone_address_get_display_name(addr));
87 88 89 90 91 92
	return addr;
}

static void auth_info_requested(LinphoneCore *lc, const char *realm, const char *username, const char *domain) {
	stats* counters;
	ms_message("Auth info requested  for user id [%s] at realm [%s]\n"
93 94
			   ,username
			   ,realm);
95 96 97 98 99
	counters = get_stats(lc);
	counters->number_of_auth_info_requested++;
}

void reset_counters( stats* counters) {
100
	if (counters->last_received_chat_message) linphone_chat_message_unref(counters->last_received_chat_message);
101 102 103
	memset(counters,0,sizeof(stats));
}

104
LinphoneCore* configure_lc_from(LinphoneCoreVTable* v_table, const char* path, const char* file, void* user_data) {
105
	LinphoneCore* lc;
106 107 108 109 110 111 112
	LpConfig* config = NULL;
	char *filepath         = NULL;
	char *ringpath         = NULL;
	char *ringbackpath     = NULL;
	char *rootcapath       = NULL;
	char *dnsuserhostspath = NULL;
	char *nowebcampath     = NULL;
113 114 115 116

	if (path==NULL) path=".";

	if (file){
117
		filepath = ms_strdup_printf("%s/%s", path, file);
118
		BC_ASSERT_EQUAL_FATAL(ortp_file_exist(filepath),0,int, "%d");
119
		config = lp_config_new_with_factory(NULL,filepath);
120 121 122
	}


123 124 125 126 127 128
	// setup dynamic-path assets
	ringpath         = ms_strdup_printf("%s/sounds/oldphone.wav",path);
	ringbackpath     = ms_strdup_printf("%s/sounds/ringback.wav", path);
	nowebcampath     = ms_strdup_printf("%s/images/nowebcamCIF.jpg", path);
	rootcapath       = ms_strdup_printf("%s/certificates/cn/cafile.pem", path);
	dnsuserhostspath = ms_strdup_printf( "%s/%s", path, userhostsfile);
129 130


131 132 133 134 135 136
	if( config != NULL ) {
		lp_config_set_string(config, "sound", "remote_ring", ringbackpath);
		lp_config_set_string(config, "sound", "local_ring" , ringpath);
		lp_config_set_string(config, "sip",   "root_ca"    , rootcapath);
		lc = linphone_core_new_with_config(v_table, config, user_data);
	} else {
137
		lc = linphone_core_new(v_table,NULL,(filepath!=NULL&&filepath[0]!='\0') ? filepath : NULL, user_data);
138 139 140 141 142

		linphone_core_set_ring(lc, ringpath);
		linphone_core_set_ringback(lc, ringbackpath);
		linphone_core_set_root_ca(lc,rootcapath);
	}
143

144 145
	sal_enable_test_features(lc->sal,TRUE);
	sal_set_dns_user_hosts_file(lc->sal, dnsuserhostspath);
146
	linphone_core_set_static_picture(lc,nowebcampath);
147

148
	linphone_core_enable_ipv6(lc, liblinphone_tester_ipv6_enabled);
149 150 151 152 153 154 155 156 157 158 159

	ms_free(ringpath);
	ms_free(ringbackpath);
	ms_free(nowebcampath);
	ms_free(rootcapath);
	ms_free(dnsuserhostspath);

	if( filepath ) ms_free(filepath);

	if( config ) lp_config_unref(config);

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
	return lc;
}


bool_t wait_for_until(LinphoneCore* lc_1, LinphoneCore* lc_2,int* counter,int value,int timout) {
	MSList* lcs=NULL;
	bool_t result;
	if (lc_1)
		lcs=ms_list_append(lcs,lc_1);
	if (lc_2)
		lcs=ms_list_append(lcs,lc_2);
	result=wait_for_list(lcs,counter,value,timout);
	ms_list_free(lcs);
	return result;
}

bool_t wait_for(LinphoneCore* lc_1, LinphoneCore* lc_2,int* counter,int value) {
177
	return wait_for_until(lc_1, lc_2,counter,value,10000);
178 179 180 181
}

bool_t wait_for_list(MSList* lcs,int* counter,int value,int timeout_ms) {
	MSList* iterator;
182
	MSTimeSpec start;
183

184 185 186
	liblinphone_tester_clock_start(&start);
	while ((counter==NULL || *counter<value) && !liblinphone_tester_clock_elapsed(&start,timeout_ms)) {
		for (iterator=lcs;iterator!=NULL;iterator=iterator->next) {
187 188 189 190 191
#ifdef HAVE_GTK
			gdk_threads_enter();
			gtk_main_iteration_do(FALSE);
			gdk_threads_leave();
#endif
192 193
			linphone_core_iterate((LinphoneCore*)(iterator->data));
		}
Ghislain MARY's avatar
Ghislain MARY committed
194
#ifdef LINPHONE_WINDOWS_DESKTOP
195 196 197 198 199 200 201 202
		{
			MSG msg;
			while (PeekMessage(&msg, NULL, 0, 0,1)){
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
#endif
203
		ms_usleep(20000);
204 205 206 207 208 209 210 211 212 213
	}
	if(counter && *counter<value) return FALSE;
	else return TRUE;
}

static void set_codec_enable(LinphoneCore* lc,const char* type,int rate,bool_t enable) {
	MSList* codecs=ms_list_copy(linphone_core_get_audio_codecs(lc));
	MSList* codecs_it;
	PayloadType* pt;
	for (codecs_it=codecs;codecs_it!=NULL;codecs_it=codecs_it->next) {
214
		linphone_core_enable_payload_type(lc,(PayloadType*)codecs_it->data,0);
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
	}
	if((pt = linphone_core_find_payload_type(lc,type,rate,1))) {
		linphone_core_enable_payload_type(lc,pt, enable);
	}
	ms_list_free(codecs);
}

static void enable_codec(LinphoneCore* lc,const char* type,int rate) {
	set_codec_enable(lc,type,rate,TRUE);
}
stats * get_stats(LinphoneCore *lc){
	LinphoneCoreManager *manager=(LinphoneCoreManager *)linphone_core_get_user_data(lc);
	return &manager->stat;
}

LinphoneCoreManager *get_manager(LinphoneCore *lc){
	LinphoneCoreManager *manager=(LinphoneCoreManager *)linphone_core_get_user_data(lc);
	return manager;
}

235 236 237
bool_t transport_supported(LinphoneTransportType transport) {
	Sal *sal = sal_init();
	bool_t supported = sal_transport_available(sal,(SalTransport)transport);
238
	if (!supported) ms_message("TLS transport not supported, falling back to TCP if possible otherwise skipping test.");
239
	sal_uninit(sal);
240 241 242
	return supported;
}

243

244 245 246 247
static void display_status(LinphoneCore *lc, const char *status){
	ms_message("display_status(): %s",status);
}

248
LinphoneCoreManager* linphone_core_manager_init(const char* rc_file) {
249 250
	LinphoneCoreManager* mgr= ms_new0(LinphoneCoreManager,1);
	char *rc_path = NULL;
jehan's avatar
jehan committed
251
	char *hellopath = bc_tester_res("sounds/hello8000.wav");
jehan's avatar
jehan committed
252
	mgr->number_of_cunit_error_at_creation = CU_get_number_of_failures();
253 254 255 256 257 258 259 260 261 262 263 264 265 266
	mgr->v_table.registration_state_changed=registration_state_changed;
	mgr->v_table.auth_info_requested=auth_info_requested;
	mgr->v_table.call_state_changed=call_state_changed;
	mgr->v_table.text_received=text_message_received;
	mgr->v_table.message_received=message_received;
	mgr->v_table.is_composing_received=is_composing_received;
	mgr->v_table.new_subscription_requested=new_subscription_requested;
	mgr->v_table.notify_presence_received=notify_presence_received;
	mgr->v_table.transfer_state_changed=linphone_transfer_state_changed;
	mgr->v_table.info_received=info_message_received;
	mgr->v_table.subscription_state_changed=linphone_subscription_state_change;
	mgr->v_table.notify_received=linphone_notify_received;
	mgr->v_table.publish_state_changed=linphone_publish_state_changed;
	mgr->v_table.configuring_status=linphone_configuration_status;
jehan's avatar
jehan committed
267
	mgr->v_table.call_encryption_changed=linphone_call_encryption_changed;
268
	mgr->v_table.network_reachable=network_reachable;
269
	mgr->v_table.dtmf_received=dtmf_received;
270
	mgr->v_table.call_stats_updated=call_stats_updated;
271
	mgr->v_table.display_status=display_status;
272 273 274

	reset_counters(&mgr->stat);
	if (rc_file) rc_path = ms_strdup_printf("rcfiles/%s", rc_file);
275
	mgr->lc=configure_lc_from(&mgr->v_table, bc_tester_get_resource_dir_prefix(), rc_path, mgr);
276
	linphone_core_manager_check_accounts(mgr);
277

278 279
	manager_count++;

Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
280
#if TARGET_OS_IPHONE
281 282
	linphone_core_set_ringer_device( mgr->lc, "AQ: Audio Queue Device");
	linphone_core_set_ringback(mgr->lc, NULL);
283 284
#endif

285 286 287 288 289 290 291
#ifdef VIDEO_ENABLED
	{
		MSWebCam *cam;

		cam = ms_web_cam_manager_get_cam(ms_web_cam_manager_get(), "Mire: Mire (synthetic moving picture)");

		if (cam == NULL) {
Simon Morlat's avatar
Simon Morlat committed
292 293 294 295 296
			MSWebCamDesc *desc = ms_mire_webcam_desc_get();
			if (desc){
				cam=ms_web_cam_new(desc);
				ms_web_cam_manager_add_cam(ms_web_cam_manager_get(), cam);
			}
297 298 299 300
		}
	}
#endif

jehan's avatar
jehan committed
301

302 303
	linphone_core_set_play_file(mgr->lc,hellopath); /*is also used when in pause*/
	ms_free(hellopath);
304

305
	if( manager_count >= 2){
306
		char *recordpath = ms_strdup_printf("%s/record_for_lc_%p.wav",bc_tester_get_writable_dir_prefix(),mgr->lc);
307
		ms_message("Manager for '%s' using files", rc_file ? rc_file : "--");
308
		linphone_core_set_use_files(mgr->lc, TRUE);
309 310
		linphone_core_set_record_file(mgr->lc,recordpath);
		ms_free(recordpath);
311
	}
312

313
	linphone_core_set_user_certificates_path(mgr->lc,bc_tester_get_writable_dir_prefix());
314

315
	if (rc_path) ms_free(rc_path);
316

317 318
	return mgr;
}
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
319

320 321 322
void linphone_core_manager_start(LinphoneCoreManager *mgr, const char* rc_file, int check_for_proxies) {
	LinphoneProxyConfig* proxy;
	int proxy_count;
323

324
	/*BC_ASSERT_EQUAL(ms_list_size(linphone_core_get_proxy_config_list(lc)),proxy_count, int, "%d");*/
325 326 327 328
	if (check_for_proxies && rc_file) /**/
		proxy_count=ms_list_size(linphone_core_get_proxy_config_list(mgr->lc));
	else
		proxy_count=0;
329

330 331 332
	if (proxy_count){
#define REGISTER_TIMEOUT 20 /* seconds */
		int success = wait_for_until(mgr->lc,NULL,&mgr->stat.number_of_LinphoneRegistrationOk,
Guillaume BIENKOWSKI's avatar
Typo  
Guillaume BIENKOWSKI committed
333
									proxy_count,(REGISTER_TIMEOUT * 1000 * proxy_count));
334 335 336 337
		if( !success ){
			ms_error("Did not register after %d seconds for %d proxies", REGISTER_TIMEOUT, proxy_count);
		}
	}
338
	BC_ASSERT_EQUAL(mgr->stat.number_of_LinphoneRegistrationOk,proxy_count, int, "%d");
339 340 341 342
	enable_codec(mgr->lc,"PCMU",8000);

	linphone_core_get_default_proxy(mgr->lc,&proxy);
	if (proxy) {
343
		mgr->identity = linphone_address_clone(linphone_proxy_config_get_identity_address(proxy));
344 345 346 347 348
		linphone_address_clean(mgr->identity);
	}
}

LinphoneCoreManager* linphone_core_manager_new( const char* rc_file) {
349 350 351 352 353 354 355 356 357
	LinphoneCoreManager *manager = linphone_core_manager_init(rc_file);
	linphone_core_manager_start(manager, rc_file, TRUE);
	return manager;
}

LinphoneCoreManager* linphone_core_manager_new2( const char* rc_file, int check_for_proxies) {
	LinphoneCoreManager *manager = linphone_core_manager_init(rc_file);
	linphone_core_manager_start(manager, rc_file, check_for_proxies);
	return manager;
358 359 360 361 362 363 364 365 366 367
}

void linphone_core_manager_stop(LinphoneCoreManager *mgr){
	if (mgr->lc) {
		linphone_core_destroy(mgr->lc);
		mgr->lc=NULL;
	}
}

void linphone_core_manager_destroy(LinphoneCoreManager* mgr) {
Simon Morlat's avatar
Simon Morlat committed
368 369
	if (mgr->lc){
		const char *record_file=linphone_core_get_record_file(mgr->lc);
370
		if (!liblinphone_tester_keep_record_files && record_file){
jehan's avatar
jehan committed
371
			if ((CU_get_number_of_failures()-mgr->number_of_cunit_error_at_creation)>0) {
Simon Morlat's avatar
Simon Morlat committed
372 373 374 375
				ms_message ("Test has failed, keeping recorded file [%s]",record_file);
			} else {
				unlink(record_file);
			}
376
		}
Simon Morlat's avatar
Simon Morlat committed
377
		linphone_core_destroy(mgr->lc);
378
	}
379
	if (mgr->identity) linphone_address_destroy(mgr->identity);
Simon Morlat's avatar
Simon Morlat committed
380
	if (mgr->stat.last_received_chat_message) linphone_chat_message_unref(mgr->stat.last_received_chat_message);
381
	manager_count--;
382

383 384 385
	ms_free(mgr);
}

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
int liblinphone_tester_ipv6_available(void){
	struct addrinfo *ai=belle_sip_ip_address_to_addrinfo(AF_INET6,"2a01:e00::2",53);
	if (ai){
		struct sockaddr_storage ss;
		struct addrinfo src;
		socklen_t slen=sizeof(ss);
		char localip[128];
		int port=0;
		belle_sip_get_src_addr_for(ai->ai_addr,ai->ai_addrlen,(struct sockaddr*) &ss,&slen,4444);
		src.ai_addr=(struct sockaddr*) &ss;
		src.ai_addrlen=slen;
		belle_sip_addrinfo_to_ip(&src,localip, sizeof(localip),&port);
		freeaddrinfo(ai);
		return strcmp(localip,"::1")!=0;
	}
	return FALSE;
}

404 405 406 407
void liblinphone_tester_keep_accounts( int keep ){
	liblinphone_tester_keep_accounts_flag = keep;
}

408 409 410 411
void liblinphone_tester_keep_recorded_files(int keep){
	liblinphone_tester_keep_record_files = keep;
}

412 413 414
void liblinphone_tester_clear_accounts(void){
	account_manager_destroy();
}
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432

void liblinphone_tester_add_suites() {
	bc_tester_add_suite(&setup_test_suite);
	bc_tester_add_suite(&register_test_suite);
	bc_tester_add_suite(&offeranswer_test_suite);
	bc_tester_add_suite(&call_test_suite);
	bc_tester_add_suite(&multi_call_test_suite);
	bc_tester_add_suite(&message_test_suite);
	bc_tester_add_suite(&presence_test_suite);
#ifdef UPNP
	bc_tester_add_suite(&upnp_test_suite);
#endif
	bc_tester_add_suite(&stun_test_suite);
	bc_tester_add_suite(&event_test_suite);
	bc_tester_add_suite(&flexisip_test_suite);
	bc_tester_add_suite(&remote_provisioning_test_suite);
	bc_tester_add_suite(&quality_reporting_test_suite);
	bc_tester_add_suite(&log_collection_test_suite);
433
	bc_tester_add_suite(&tunnel_test_suite);
434 435 436 437 438 439
	bc_tester_add_suite(&player_test_suite);
	bc_tester_add_suite(&dtmf_test_suite);
#if defined(VIDEO_ENABLED) && defined(HAVE_GTK)
	bc_tester_add_suite(&video_test_suite);
#endif
	bc_tester_add_suite(&multicast_call_test_suite);
440
	bc_tester_add_suite(&proxy_config_test_suite);
441
}
442

Simon Morlat's avatar
Simon Morlat committed
443
static int linphone_core_manager_get_max_audio_bw_base(const int array[],int array_size) {
444 445 446 447 448 449 450
	int i,result=0;
	for (i=0; i<array_size; i++) {
		result = MAX(result,array[i]);
	}
	return result;
}

Simon Morlat's avatar
Simon Morlat committed
451 452 453 454 455 456 457 458
static int linphone_core_manager_get_mean_audio_bw_base(const int array[],int array_size) {
	int i,result=0;
	for (i=0; i<array_size; i++) {
		result += array[i];
	}
	return result/array_size;
}

459 460 461 462 463 464 465 466
int linphone_core_manager_get_max_audio_down_bw(const LinphoneCoreManager *mgr) {
	return linphone_core_manager_get_max_audio_bw_base(mgr->stat.audio_download_bandwidth
			, sizeof(mgr->stat.audio_download_bandwidth)/sizeof(int));
}
int linphone_core_manager_get_max_audio_up_bw(const LinphoneCoreManager *mgr) {
	return linphone_core_manager_get_max_audio_bw_base(mgr->stat.audio_upload_bandwidth
			, sizeof(mgr->stat.audio_upload_bandwidth)/sizeof(int));
}
467

Simon Morlat's avatar
Simon Morlat committed
468 469 470 471 472 473 474 475 476
int linphone_core_manager_get_mean_audio_down_bw(const LinphoneCoreManager *mgr) {
	return linphone_core_manager_get_mean_audio_bw_base(mgr->stat.audio_download_bandwidth
			, sizeof(mgr->stat.audio_download_bandwidth)/sizeof(int));
}
int linphone_core_manager_get_mean_audio_up_bw(const LinphoneCoreManager *mgr) {
	return linphone_core_manager_get_mean_audio_bw_base(mgr->stat.audio_upload_bandwidth
			, sizeof(mgr->stat.audio_upload_bandwidth)/sizeof(int));
}

477 478
int liblinphone_tester_setup() {
	if (manager_count != 0) {
479 480 481
		// crash in some linphone core have not been destroyed because if we continue
		// it will crash in CUnit AND we should NEVER keep a manager alive
		ms_fatal("%d linphone core manager still alive!", manager_count);
482 483 484 485
		return 1;
	}
	return 0;
}