call_tester.c 137 KB
Newer Older
jehan's avatar
jehan committed
1
/*
2 3
	liblinphone_tester - liblinphone test suite
	Copyright (C) 2013  Belledonne Communications SARL
jehan's avatar
jehan committed
4

5 6 7 8
	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.
jehan's avatar
jehan committed
9

10 11 12 13
	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.
jehan's avatar
jehan committed
14

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

Simon Morlat's avatar
Simon Morlat committed
19

jehan's avatar
jehan committed
20
#include <stdio.h>
21 22
#include <sys/types.h>
#include <sys/stat.h>
jehan's avatar
jehan committed
23 24
#include "CUnit/Basic.h"
#include "linphonecore.h"
25
#include "lpconfig.h"
jehan's avatar
jehan committed
26 27
#include "private.h"
#include "liblinphone_tester.h"
Simon Morlat's avatar
Simon Morlat committed
28 29 30 31 32
#include "mediastreamer2/dsptools.h"

#ifdef WIN32
#define unlink _unlink
#endif
jehan's avatar
jehan committed
33

34
static void srtp_call(void);
jehan's avatar
jehan committed
35
static void call_base(LinphoneMediaEncryption mode, bool_t enable_video,bool_t enable_relay,LinphoneFirewallPolicy policy);
36
static void disable_all_audio_codecs_except_one(LinphoneCore *lc, const char *mime, int rate);
Simon Morlat's avatar
Simon Morlat committed
37
static char *create_filepath(const char *dir, const char *filename, const char *ext);
jehan's avatar
jehan committed
38

39 40 41 42 43 44 45
// prototype definition for call_recording()
#ifdef ANDROID
#ifdef HAVE_OPENH264
extern void libmsopenh264_init(void);
#endif
#endif

jehan's avatar
jehan committed
46 47 48
void call_state_changed(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState cstate, const char *msg){
	char* to=linphone_address_as_string(linphone_call_get_call_log(call)->to);
	char* from=linphone_address_as_string(linphone_call_get_call_log(call)->from);
49
	stats* counters;
jehan's avatar
jehan committed
50 51 52 53
	ms_message(" %s call from [%s] to [%s], new state is [%s]"	,linphone_call_get_call_log(call)->dir==LinphoneCallIncoming?"Incoming":"Outgoing"
																,from
																,to
																,linphone_call_state_to_string(cstate));
jehan's avatar
jehan committed
54 55
	ms_free(to);
	ms_free(from);
56
	counters = get_stats(lc);
jehan's avatar
jehan committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	switch (cstate) {
	case LinphoneCallIncomingReceived:counters->number_of_LinphoneCallIncomingReceived++;break;
	case LinphoneCallOutgoingInit :counters->number_of_LinphoneCallOutgoingInit++;break;
	case LinphoneCallOutgoingProgress :counters->number_of_LinphoneCallOutgoingProgress++;break;
	case LinphoneCallOutgoingRinging :counters->number_of_LinphoneCallOutgoingRinging++;break;
	case LinphoneCallOutgoingEarlyMedia :counters->number_of_LinphoneCallOutgoingEarlyMedia++;break;
	case LinphoneCallConnected :counters->number_of_LinphoneCallConnected++;break;
	case LinphoneCallStreamsRunning :counters->number_of_LinphoneCallStreamsRunning++;break;
	case LinphoneCallPausing :counters->number_of_LinphoneCallPausing++;break;
	case LinphoneCallPaused :counters->number_of_LinphoneCallPaused++;break;
	case LinphoneCallResuming :counters->number_of_LinphoneCallResuming++;break;
	case LinphoneCallRefered :counters->number_of_LinphoneCallRefered++;break;
	case LinphoneCallError :counters->number_of_LinphoneCallError++;break;
	case LinphoneCallEnd :counters->number_of_LinphoneCallEnd++;break;
	case LinphoneCallPausedByRemote :counters->number_of_LinphoneCallPausedByRemote++;break;
	case LinphoneCallUpdatedByRemote :counters->number_of_LinphoneCallUpdatedByRemote++;break;
	case LinphoneCallIncomingEarlyMedia :counters->number_of_LinphoneCallIncomingEarlyMedia++;break;
	case LinphoneCallUpdating :counters->number_of_LinphoneCallUpdating++;break;
	case LinphoneCallReleased :counters->number_of_LinphoneCallReleased++;break;
	default:
		CU_FAIL("unexpected event");break;
	}
}
jehan's avatar
jehan committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
void linphone_call_encryption_changed(LinphoneCore *lc, LinphoneCall *call, bool_t on, const char *authentication_token) {
	char* to=linphone_address_as_string(linphone_call_get_call_log(call)->to);
	char* from=linphone_address_as_string(linphone_call_get_call_log(call)->from);
	stats* counters;
	ms_message(" %s call from [%s] to [%s], is now [%s]",linphone_call_get_call_log(call)->dir==LinphoneCallIncoming?"Incoming":"Outgoing"
														,from
														,to
														,(on?"encrypted":"unencrypted"));
	ms_free(to);
	ms_free(from);
	counters = get_stats(lc);
	if (on)
		counters->number_of_LinphoneCallEncryptedOn++;
	else
		counters->number_of_LinphoneCallEncryptedOff++;
}
jehan's avatar
jehan committed
96 97 98
void linphone_transfer_state_changed(LinphoneCore *lc, LinphoneCall *transfered, LinphoneCallState new_call_state) {
	char* to=linphone_address_as_string(linphone_call_get_call_log(transfered)->to);
	char* from=linphone_address_as_string(linphone_call_get_call_log(transfered)->from);
99
	stats* counters;
jehan's avatar
jehan committed
100 101 102 103
	ms_message("Transferred call from [%s] to [%s], new state is [%s]",from,to,linphone_call_state_to_string(new_call_state));
	ms_free(to);
	ms_free(from);

104
	counters = get_stats(lc);
jehan's avatar
jehan committed
105
	switch (new_call_state) {
jehan's avatar
jehan committed
106
	case LinphoneCallOutgoingInit :counters->number_of_LinphoneTransferCallOutgoingInit++;break;
107 108 109 110 111
	case LinphoneCallOutgoingProgress :counters->number_of_LinphoneTransferCallOutgoingProgress++;break;
	case LinphoneCallOutgoingRinging :counters->number_of_LinphoneTransferCallOutgoingRinging++;break;
	case LinphoneCallOutgoingEarlyMedia :counters->number_of_LinphoneTransferCallOutgoingEarlyMedia++;break;
	case LinphoneCallConnected :counters->number_of_LinphoneTransferCallConnected++;break;
	case LinphoneCallStreamsRunning :counters->number_of_LinphoneTransferCallStreamsRunning++;break;
112
	case LinphoneCallError :counters->number_of_LinphoneTransferCallError++;break;
jehan's avatar
jehan committed
113 114 115 116
	default:
		CU_FAIL("unexpected event");break;
	}
}
Ghislain MARY's avatar
Ghislain MARY committed
117

118
#ifdef VIDEO_ENABLED
jehan's avatar
jehan committed
119 120 121
static void linphone_call_cb(LinphoneCall *call,void * user_data) {
	char* to=linphone_address_as_string(linphone_call_get_call_log(call)->to);
	char* from=linphone_address_as_string(linphone_call_get_call_log(call)->from);
122
	stats* counters;
jehan's avatar
jehan committed
123 124 125 126
	LinphoneCore* lc=(LinphoneCore*)user_data;
	ms_message("call from [%s] to [%s] receive iFrame",from,to);
	ms_free(to);
	ms_free(from);
127
	counters = (stats*)get_stats(lc);
jehan's avatar
jehan committed
128 129
	counters->number_of_IframeDecoded++;
}
130
#endif
Simon Morlat's avatar
Simon Morlat committed
131

Simon Morlat's avatar
Simon Morlat committed
132
void liblinphone_tester_check_rtcp(LinphoneCoreManager* caller, LinphoneCoreManager* callee) {
133 134
	LinphoneCall *c1,*c2;
	int dummy=0;
Simon Morlat's avatar
Simon Morlat committed
135
	MSTimeSpec ts;
136 137 138

	c1=linphone_core_get_current_call(caller->lc);
	c2=linphone_core_get_current_call(callee->lc);
139

Simon Morlat's avatar
Simon Morlat committed
140 141
	CU_ASSERT_PTR_NOT_NULL(c1);
	CU_ASSERT_PTR_NOT_NULL(c2);
142

Simon Morlat's avatar
Simon Morlat committed
143
	if (!c1 || !c2) return;
jehan's avatar
jehan committed
144 145
	linphone_call_ref(c1);
	linphone_call_ref(c2);
Simon Morlat's avatar
Simon Morlat committed
146 147 148
	
	liblinphone_tester_clock_start(&ts);
	do {
149 150
		if (linphone_call_get_audio_stats(c1)->round_trip_delay >0.0
				&& linphone_call_get_audio_stats(c2)->round_trip_delay >0.0
jehan's avatar
jehan committed
151 152
				&& (!linphone_call_log_video_enabled(linphone_call_get_call_log(c1)) || linphone_call_get_video_stats(c1)->round_trip_delay>0.0)
				&& (!linphone_call_log_video_enabled(linphone_call_get_call_log(c2))  || linphone_call_get_video_stats(c2)->round_trip_delay>0.0)) {
153
			break;
jehan's avatar
jehan committed
154

155
		}
jehan's avatar
jehan committed
156
		wait_for_until(caller->lc,callee->lc,&dummy,1,500); /*just to sleep while iterating*/
Simon Morlat's avatar
Simon Morlat committed
157
	}while (!liblinphone_tester_clock_elapsed(&ts,12000));
158 159 160 161 162 163 164 165
	CU_ASSERT_TRUE(linphone_call_get_audio_stats(c1)->round_trip_delay>0.0);
	CU_ASSERT_TRUE(linphone_call_get_audio_stats(c2)->round_trip_delay>0.0);
	if (linphone_call_log_video_enabled(linphone_call_get_call_log(c1))) {
		CU_ASSERT_TRUE(linphone_call_get_video_stats(c1)->round_trip_delay>0.0);
	}
	if (linphone_call_log_video_enabled(linphone_call_get_call_log(c2))) {
		CU_ASSERT_TRUE(linphone_call_get_video_stats(c2)->round_trip_delay>0.0);
	}
jehan's avatar
jehan committed
166 167
	linphone_call_unref(c1);
	linphone_call_unref(c2);
168 169
}

170
bool_t call_with_params2(LinphoneCoreManager* caller_mgr
171 172
						,LinphoneCoreManager* callee_mgr
						, const LinphoneCallParams *caller_params
173
						, const LinphoneCallParams *callee_params, bool_t build_callee_params) {
jehan's avatar
jehan committed
174
	int retry=0;
jehan's avatar
jehan committed
175 176
	stats initial_caller=caller_mgr->stat;
	stats initial_callee=callee_mgr->stat;
jehan's avatar
jehan committed
177
	bool_t result=FALSE;
jehan's avatar
jehan committed
178 179 180 181 182
	char hellopath[256];
	/*use playfile for callee to avoid locking on capture card*/
	linphone_core_use_files (callee_mgr->lc,TRUE);
	snprintf(hellopath,sizeof(hellopath), "%s/sounds/hello8000.wav", liblinphone_tester_file_prefix);
	linphone_core_set_play_file(callee_mgr->lc,hellopath);
183
	if (!caller_params){
Simon Morlat's avatar
Simon Morlat committed
184 185
		CU_ASSERT_PTR_NOT_NULL(linphone_core_invite_address(caller_mgr->lc,callee_mgr->identity));
	}else{
186
		CU_ASSERT_PTR_NOT_NULL(linphone_core_invite_address_with_params(caller_mgr->lc,callee_mgr->identity,caller_params));
Simon Morlat's avatar
Simon Morlat committed
187
	}
jehan's avatar
jehan committed
188 189 190

	/*linphone_core_invite(caller_mgr->lc,"pauline");*/

jehan's avatar
jehan committed
191
	CU_ASSERT_TRUE(wait_for(callee_mgr->lc
jehan's avatar
jehan committed
192 193 194
									,caller_mgr->lc
									,&callee_mgr->stat.number_of_LinphoneCallIncomingReceived
									,initial_callee.number_of_LinphoneCallIncomingReceived+1));
jehan's avatar
jehan committed
195
	CU_ASSERT_TRUE(linphone_core_inc_invite_pending(callee_mgr->lc));
jehan's avatar
jehan committed
196 197
	CU_ASSERT_EQUAL(caller_mgr->stat.number_of_LinphoneCallOutgoingProgress,initial_caller.number_of_LinphoneCallOutgoingProgress+1);

jehan's avatar
jehan committed
198

jehan's avatar
jehan committed
199 200 201
	while (caller_mgr->stat.number_of_LinphoneCallOutgoingRinging!=(initial_caller.number_of_LinphoneCallOutgoingRinging + 1)
			&& caller_mgr->stat.number_of_LinphoneCallOutgoingEarlyMedia!=(initial_caller.number_of_LinphoneCallOutgoingEarlyMedia +1)
			&& retry++ <20) {
jehan's avatar
jehan committed
202 203 204 205 206 207
			linphone_core_iterate(caller_mgr->lc);
			linphone_core_iterate(callee_mgr->lc);
			ms_usleep(100000);
	}


jehan's avatar
jehan committed
208
	CU_ASSERT_TRUE((caller_mgr->stat.number_of_LinphoneCallOutgoingRinging==initial_caller.number_of_LinphoneCallOutgoingRinging+1)
jehan's avatar
jehan committed
209
							|(caller_mgr->stat.number_of_LinphoneCallOutgoingEarlyMedia==initial_caller.number_of_LinphoneCallOutgoingEarlyMedia+1));
jehan's avatar
jehan committed
210

jehan's avatar
jehan committed
211

jehan's avatar
jehan committed
212
	CU_ASSERT_PTR_NOT_NULL(linphone_core_get_current_call_remote_address(callee_mgr->lc));
213
	if(!linphone_core_get_current_call(caller_mgr->lc) || !linphone_core_get_current_call(callee_mgr->lc) || !linphone_core_get_current_call_remote_address(callee_mgr->lc))
jehan's avatar
jehan committed
214
		return 0;
jehan's avatar
jehan committed
215 216 217 218 219 220 221 222 223
	else {
		LinphoneAddress* callee_from=linphone_address_clone(caller_mgr->identity);
		linphone_address_set_port(callee_from,0); /*remove port because port is never present in from header*/
		if (linphone_call_params_get_privacy(linphone_call_get_current_params(linphone_core_get_current_call(caller_mgr->lc))) == LinphonePrivacyNone) {
			CU_ASSERT_TRUE(linphone_address_weak_equal(callee_from,linphone_core_get_current_call_remote_address(callee_mgr->lc)));
		} else {
			CU_ASSERT_FALSE(linphone_address_weak_equal(callee_from,linphone_core_get_current_call_remote_address(callee_mgr->lc)));
		}
		linphone_address_destroy(callee_from);
jehan's avatar
jehan committed
224
	}
225
	if (callee_params){
226
		linphone_core_accept_call_with_params(callee_mgr->lc,linphone_core_get_current_call(callee_mgr->lc),callee_params);
227 228 229 230 231 232
	}else if (build_callee_params){
		LinphoneCallParams *default_params=linphone_core_create_call_params(callee_mgr->lc,linphone_core_get_current_call(callee_mgr->lc));
		ms_error("Created default call params with video=%i", linphone_call_params_video_enabled(default_params));
		linphone_core_accept_call_with_params(callee_mgr->lc,linphone_core_get_current_call(callee_mgr->lc),default_params);
		linphone_call_params_destroy(default_params);
	}else{
233
		linphone_core_accept_call(callee_mgr->lc,linphone_core_get_current_call(callee_mgr->lc));
234
	}
jehan's avatar
jehan committed
235

jehan's avatar
jehan committed
236 237
	CU_ASSERT_TRUE(wait_for(callee_mgr->lc,caller_mgr->lc,&callee_mgr->stat.number_of_LinphoneCallConnected,initial_callee.number_of_LinphoneCallConnected+1));
	CU_ASSERT_TRUE(wait_for(callee_mgr->lc,caller_mgr->lc,&caller_mgr->stat.number_of_LinphoneCallConnected,initial_callee.number_of_LinphoneCallConnected+1));
jehan's avatar
jehan committed
238
	/*just to sleep*/
jehan's avatar
jehan committed
239
	result = wait_for(callee_mgr->lc,caller_mgr->lc,&caller_mgr->stat.number_of_LinphoneCallStreamsRunning,initial_caller.number_of_LinphoneCallStreamsRunning+1)
jehan's avatar
jehan committed
240
			&&
jehan's avatar
jehan committed
241
			wait_for(callee_mgr->lc,caller_mgr->lc,&callee_mgr->stat.number_of_LinphoneCallStreamsRunning,initial_callee.number_of_LinphoneCallStreamsRunning+1);
jehan's avatar
jehan committed
242

jehan's avatar
jehan committed
243 244 245 246 247 248 249 250
	if (linphone_core_get_media_encryption(caller_mgr->lc) != LinphoneMediaEncryptionNone
		&& linphone_core_get_media_encryption(callee_mgr->lc) != LinphoneMediaEncryptionNone) {
		/*wait for encryption to be on, in case of zrtp, it can take a few seconds*/
		if (linphone_core_get_media_encryption(caller_mgr->lc) == LinphoneMediaEncryptionZRTP)
			wait_for(callee_mgr->lc,caller_mgr->lc,&caller_mgr->stat.number_of_LinphoneCallEncryptedOn,initial_caller.number_of_LinphoneCallEncryptedOn+1);
		if (linphone_core_get_media_encryption(callee_mgr->lc) == LinphoneMediaEncryptionZRTP)
			wait_for(callee_mgr->lc,caller_mgr->lc,&callee_mgr->stat.number_of_LinphoneCallEncryptedOn,initial_callee.number_of_LinphoneCallEncryptedOn+1);
		{
jehan's avatar
jehan committed
251 252 253
		const LinphoneCallParams* call_param = linphone_call_get_current_params(linphone_core_get_current_call(callee_mgr->lc));
		CU_ASSERT_EQUAL(linphone_call_params_get_media_encryption(call_param),linphone_core_get_media_encryption(caller_mgr->lc));
		call_param = linphone_call_get_current_params(linphone_core_get_current_call(caller_mgr->lc));
jehan's avatar
jehan committed
254
		CU_ASSERT_EQUAL(linphone_call_params_get_media_encryption(call_param),linphone_core_get_media_encryption(callee_mgr->lc));
jehan's avatar
jehan committed
255
		}
jehan's avatar
jehan committed
256 257
	}
	return result;
jehan's avatar
jehan committed
258
}
259 260 261 262 263 264 265 266

bool_t call_with_params(LinphoneCoreManager* caller_mgr
						,LinphoneCoreManager* callee_mgr
						, const LinphoneCallParams *caller_params
						, const LinphoneCallParams *callee_params){
	return call_with_params2(caller_mgr,callee_mgr,caller_params,callee_params,FALSE);
}

267 268 269
bool_t call_with_caller_params(LinphoneCoreManager* caller_mgr,LinphoneCoreManager* callee_mgr, const LinphoneCallParams *params) {
	return call_with_params(caller_mgr,callee_mgr,params,NULL);
}
270

Simon Morlat's avatar
Simon Morlat committed
271
bool_t call(LinphoneCoreManager* caller_mgr,LinphoneCoreManager* callee_mgr){
272
	return call_with_params(caller_mgr,callee_mgr,NULL,NULL);
Simon Morlat's avatar
Simon Morlat committed
273 274
}

275
void end_call(LinphoneCoreManager *m1, LinphoneCoreManager *m2){
Simon Morlat's avatar
Simon Morlat committed
276 277 278
	linphone_core_terminate_all_calls(m1->lc);
	CU_ASSERT_TRUE(wait_for(m1->lc,m2->lc,&m1->stat.number_of_LinphoneCallEnd,1));
	CU_ASSERT_TRUE(wait_for(m1->lc,m2->lc,&m2->stat.number_of_LinphoneCallEnd,1));
279 280
	CU_ASSERT_TRUE(wait_for(m1->lc,m2->lc,&m1->stat.number_of_LinphoneCallReleased,1));
	CU_ASSERT_TRUE(wait_for(m1->lc,m2->lc,&m2->stat.number_of_LinphoneCallReleased,1));
Simon Morlat's avatar
Simon Morlat committed
281 282
}

Ghislain MARY's avatar
Ghislain MARY committed
283
static void simple_call(void) {
284
	int begin;
Simon Morlat's avatar
Simon Morlat committed
285
	int leaked_objects;
286 287 288 289 290 291 292 293 294 295 296 297 298
	LinphoneCoreManager* marie;
	LinphoneCoreManager* pauline;

	belle_sip_object_enable_leak_detector(TRUE);
	begin=belle_sip_object_get_object_count();

	marie = linphone_core_manager_new( "marie_rc");
	pauline = linphone_core_manager_new( "pauline_rc");
	CU_ASSERT_TRUE(call(pauline,marie));
	liblinphone_tester_check_rtcp(marie,pauline);
	end_call(marie,pauline);
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
Simon Morlat's avatar
Simon Morlat committed
299 300 301 302 303 304

	leaked_objects=belle_sip_object_get_object_count()-begin;
	CU_ASSERT_TRUE(leaked_objects==0);
	if (leaked_objects>0){
		belle_sip_object_dump_active_objects();
	}
jehan's avatar
jehan committed
305
}
Simon Morlat's avatar
Simon Morlat committed
306

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
static void call_outbound_with_multiple_proxy() {
	LinphoneCoreManager* pauline = linphone_core_manager_new2( "pauline_rc", FALSE);
	LinphoneCoreManager* marie   = linphone_core_manager_new2( "marie_rc", FALSE);

	LinphoneProxyConfig* lpc = NULL;
	LinphoneProxyConfig* registered_lpc = linphone_proxy_config_new();

	linphone_core_get_default_proxy(marie->lc, &lpc);
	linphone_core_set_default_proxy(marie->lc,NULL);

	CU_ASSERT_FATAL(lpc != NULL);
	CU_ASSERT_FATAL(registered_lpc != NULL);

	// create new LPC that will successfully register
	linphone_proxy_config_set_identity(registered_lpc, linphone_proxy_config_get_identity(lpc));
	linphone_proxy_config_set_server_addr(registered_lpc, linphone_proxy_config_get_addr(lpc));
	linphone_proxy_config_set_route(registered_lpc, linphone_proxy_config_get_route(lpc));
	linphone_proxy_config_enable_register(registered_lpc, TRUE);

	linphone_core_add_proxy_config(marie->lc, registered_lpc);

	// set first LPC to unreacheable proxy addr
	linphone_proxy_config_edit(lpc);
	linphone_proxy_config_set_server_addr(lpc,"12.13.14.15:5223;transport=udp");
	linphone_proxy_config_set_route(lpc, "12.13.14.15:5223;transport=udp;lr");
	linphone_proxy_config_done(lpc);

	CU_ASSERT_TRUE(wait_for_until(pauline->lc, NULL, &pauline->stat.number_of_LinphoneRegistrationOk, 1, 2000));

	CU_ASSERT_TRUE(wait_for_until(marie->lc, NULL, &marie->stat.number_of_LinphoneRegistrationProgress, 2, 200));
	CU_ASSERT_TRUE(wait_for_until(marie->lc, NULL, &marie->stat.number_of_LinphoneRegistrationOk, 1, 2000));

	// calling marie should go through the second proxy config
	CU_ASSERT_TRUE(call(marie, pauline));

342 343
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
344 345
}

346
#if 0 /* TODO: activate test when the implementation is ready */
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
347 348
static void multiple_answers_call() {
	/* Scenario is this: pauline calls marie, which is registered 2 times.
349
	   Both linphones answer at the same time, and only one should get the
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
350
	   call running, the other should be terminated */
351
	char ringbackpath[256];
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
352 353 354
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_rc" );
	LinphoneCoreManager* marie1  = linphone_core_manager_new( "marie_rc" );
	LinphoneCoreManager* marie2  = linphone_core_manager_new( "marie_rc" );
355

Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
356
	LinphoneCall* call1, *call2;
357

Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
358 359 360
	MSList* lcs = ms_list_append(NULL,pauline->lc);
	lcs = ms_list_append(lcs,marie1->lc);
	lcs = ms_list_append(lcs,marie2->lc);
361

362 363 364
	linphone_core_use_files(pauline->lc, TRUE);
	linphone_core_use_files(marie1->lc, TRUE);
	linphone_core_use_files(marie2->lc, TRUE);
365

366
	snprintf(ringbackpath,sizeof(ringbackpath), "%s/sounds/hello8000.wav" /*use hello because rinback is too short*/, liblinphone_tester_file_prefix);
367

368
	CU_ASSERT_TRUE(wait_for_until(pauline->lc, NULL, &pauline->stat.number_of_LinphoneRegistrationOk, 1, 2000));
369

Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
370 371
	CU_ASSERT_PTR_NOT_NULL( linphone_core_invite_address(pauline->lc, marie1->identity ) );

372 373 374
	CU_ASSERT_TRUE(wait_for_list(lcs,&marie1->stat.number_of_LinphoneCallIncomingReceived, 1, 2000));
	CU_ASSERT_TRUE(wait_for_list(lcs,&marie2->stat.number_of_LinphoneCallIncomingReceived, 1, 2000));
	CU_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneCallOutgoingProgress, 1, 2000));
375

Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
376 377 378
	// marie 1 and 2 answer at the same time
	call1 = linphone_core_get_current_call(marie1->lc);
	call2 = linphone_core_get_current_call(marie2->lc);
379

Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
380 381
	CU_ASSERT_PTR_NOT_NULL_FATAL(call1);
	CU_ASSERT_PTR_NOT_NULL_FATAL(call2);
382

Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
383 384
	CU_ASSERT_EQUAL( linphone_core_accept_call(marie1->lc, call1), 0);
	CU_ASSERT_EQUAL( linphone_core_accept_call(marie2->lc, call2), 0);
385

386 387 388
	CU_ASSERT_TRUE( wait_for_list(lcs, &pauline->stat.number_of_LinphoneCallStreamsRunning, 1, 2000) );
	CU_ASSERT_TRUE( wait_for_list(lcs, &marie1->stat.number_of_LinphoneCallStreamsRunning, 1, 2000) );
	CU_ASSERT_TRUE( wait_for_list(lcs, &marie2->stat.number_of_LinphoneCallEnd, 1, 2000) );
389 390


Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
391 392 393
	linphone_core_manager_destroy(pauline);
	linphone_core_manager_destroy(marie1);
	linphone_core_manager_destroy(marie2);
394 395 396 397
}
#endif

static void multiple_answers_call_with_media_relay() {
398

399
	/* Scenario is this: pauline calls marie, which is registered 2 times.
400
	 *   Both linphones answer at the same time, and only one should get the
401 402 403 404 405
	 *   call running, the other should be terminated */
	char ringbackpath[256];
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_rc" );
	LinphoneCoreManager* marie1  = linphone_core_manager_new( "marie_rc" );
	LinphoneCoreManager* marie2  = linphone_core_manager_new( "marie_rc" );
406

407
	LinphoneCall* call1, *call2;
408

409 410 411
	MSList* lcs = ms_list_append(NULL,pauline->lc);
	lcs = ms_list_append(lcs,marie1->lc);
	lcs = ms_list_append(lcs,marie2->lc);
412

413 414 415
	linphone_core_use_files(pauline->lc, TRUE);
	linphone_core_use_files(marie1->lc, TRUE);
	linphone_core_use_files(marie2->lc, TRUE);
416

417 418 419
	linphone_core_set_user_agent(pauline->lc, "Natted Linphone", NULL);
	linphone_core_set_user_agent(marie1->lc, "Natted Linphone", NULL);
	linphone_core_set_user_agent(marie2->lc, "Natted Linphone", NULL);
420

421
	snprintf(ringbackpath,sizeof(ringbackpath), "%s/sounds/hello8000.wav" /*use hello because rinback is too short*/, liblinphone_tester_file_prefix);
422

423
	CU_ASSERT_TRUE(wait_for_until(pauline->lc, NULL, &pauline->stat.number_of_LinphoneRegistrationOk, 1, 2000));
424

425
	CU_ASSERT_PTR_NOT_NULL( linphone_core_invite_address(pauline->lc, marie1->identity ) );
426

427 428 429
	CU_ASSERT_TRUE(wait_for_list(lcs,&marie1->stat.number_of_LinphoneCallIncomingReceived, 1, 2000));
	CU_ASSERT_TRUE(wait_for_list(lcs,&marie2->stat.number_of_LinphoneCallIncomingReceived, 1, 2000));
	CU_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneCallOutgoingProgress, 1, 2000));
430

431 432 433
	// marie 1 and 2 answer at the same time
	call1 = linphone_core_get_current_call(marie1->lc);
	call2 = linphone_core_get_current_call(marie2->lc);
434

435 436
	CU_ASSERT_PTR_NOT_NULL_FATAL(call1);
	CU_ASSERT_PTR_NOT_NULL_FATAL(call2);
437

438 439
	CU_ASSERT_EQUAL( linphone_core_accept_call(marie1->lc, call1), 0);
	CU_ASSERT_EQUAL( linphone_core_accept_call(marie2->lc, call2), 0);
440

441 442 443
	CU_ASSERT_TRUE( wait_for_list(lcs, &pauline->stat.number_of_LinphoneCallStreamsRunning, 1, 2000) );
	CU_ASSERT_TRUE( wait_for_list(lcs, &marie1->stat.number_of_LinphoneCallStreamsRunning, 1, 2000) );
	CU_ASSERT_TRUE( wait_for_list(lcs, &marie2->stat.number_of_LinphoneCallEnd, 1, 2000) );
444 445


446 447 448
	linphone_core_manager_destroy(pauline);
	linphone_core_manager_destroy(marie1);
	linphone_core_manager_destroy(marie2);
Guillaume BIENKOWSKI's avatar
Guillaume BIENKOWSKI committed
449 450
}

451 452 453 454 455
static void call_with_specified_codec_bitrate(void) {
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_rc");
	const LinphoneCallStats *pauline_stats,*marie_stats;
	bool_t call_ok;
456 457
	char * codec = "opus";
	int rate = 48000;
458 459 460
	int min_bw=24;
	int max_bw=40;

461 462
#ifdef __arm__
	if (ms_get_cpu_count() <2) { /*2 opus codec channel + resampler is too much for a single core*/
463
#ifndef ANDROID
464
		codec = "speex";
465 466 467
		rate = 8000;
		min_bw=20;
		max_bw=35;
468 469 470 471
#else
		CU_PASS("Test requires at least a dual core");
		goto end;
#endif
472 473 474 475
	}
#endif

	if (linphone_core_find_payload_type(marie->lc,codec,rate,-1)==NULL){
476 477 478
		ms_warning("opus codec not supported, test skipped.");
		goto end;
	}
479

480 481
	disable_all_audio_codecs_except_one(marie->lc,codec,rate);
	disable_all_audio_codecs_except_one(pauline->lc,codec,rate);
482

483
	linphone_core_set_payload_type_bitrate(marie->lc,
484
		linphone_core_find_payload_type(marie->lc,codec,rate,-1),
485
		max_bw);
486
	linphone_core_set_payload_type_bitrate(pauline->lc,
487
		linphone_core_find_payload_type(pauline->lc,codec,rate,-1),
488
		min_bw);
489

490 491 492 493 494
	CU_ASSERT_TRUE((call_ok=call(pauline,marie)));
	if (!call_ok) goto end;
	liblinphone_tester_check_rtcp(marie,pauline);
	marie_stats=linphone_call_get_audio_stats(linphone_core_get_current_call(marie->lc));
	pauline_stats=linphone_call_get_audio_stats(linphone_core_get_current_call(pauline->lc));
495 496
	CU_ASSERT_TRUE(marie_stats->download_bandwidth<(min_bw+5+min_bw*.1));
	CU_ASSERT_TRUE(pauline_stats->download_bandwidth>(max_bw-5-max_bw*.1));
497 498 499 500 501 502

end:
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

503
static void simple_call_compatibility_mode(void) {
504
	char route[256];
Simon Morlat's avatar
Simon Morlat committed
505 506
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_rc");
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526

	LinphoneCore* lc_marie=marie->lc;
	LinphoneCore* lc_pauline=pauline->lc;
	stats* stat_marie=&marie->stat;
	stats* stat_pauline=&pauline->stat;
	LinphoneProxyConfig* proxy;
	LinphoneAddress* identity;
	LinphoneAddress* proxy_address;
	char*tmp;
	LCSipTransports transport;

	linphone_core_get_default_proxy(lc_marie,&proxy);
	CU_ASSERT_PTR_NOT_NULL (proxy);
	identity = linphone_address_new(linphone_proxy_config_get_identity(proxy));


	proxy_address=linphone_address_new(linphone_proxy_config_get_addr(proxy));
	linphone_address_clean(proxy_address);
	tmp=linphone_address_as_string_uri_only(proxy_address);
	linphone_proxy_config_set_server_addr(proxy,tmp);
527 528
	sprintf(route,"sip:%s",test_route);
	linphone_proxy_config_set_route(proxy,route);
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
	ms_free(tmp);
	linphone_address_destroy(proxy_address);
	linphone_core_get_sip_transports(lc_marie,&transport);
	transport.udp_port=0;
	transport.tls_port=0;
	transport.dtls_port=0;
	/*only keep tcp*/
	linphone_core_set_sip_transports(lc_marie,&transport);
	stat_marie->number_of_LinphoneRegistrationOk=0;

	CU_ASSERT_TRUE (wait_for(lc_marie,lc_marie,&stat_marie->number_of_LinphoneRegistrationOk,1));

	linphone_core_invite(lc_marie,"pauline");

	CU_ASSERT_TRUE (wait_for(lc_pauline,lc_marie,&stat_pauline->number_of_LinphoneCallIncomingReceived,1));
	CU_ASSERT_TRUE(linphone_core_inc_invite_pending(lc_pauline));
	CU_ASSERT_EQUAL(stat_marie->number_of_LinphoneCallOutgoingProgress,1);
	CU_ASSERT_TRUE(wait_for(lc_pauline,lc_marie,&stat_marie->number_of_LinphoneCallOutgoingRinging,1));

	CU_ASSERT_PTR_NOT_NULL(linphone_core_get_current_call_remote_address(lc_pauline));
	if (linphone_core_get_current_call_remote_address(lc_pauline)) {
		CU_ASSERT_TRUE(linphone_address_weak_equal(identity,linphone_core_get_current_call_remote_address(lc_pauline)));
		linphone_address_destroy(identity);

		linphone_core_accept_call(lc_pauline,linphone_core_get_current_call(lc_pauline));

		CU_ASSERT_TRUE(wait_for(lc_pauline,lc_marie,&stat_pauline->number_of_LinphoneCallConnected,1));
		CU_ASSERT_TRUE(wait_for(lc_pauline,lc_marie,&stat_marie->number_of_LinphoneCallConnected,1));
		CU_ASSERT_TRUE(wait_for(lc_pauline,lc_marie,&stat_pauline->number_of_LinphoneCallStreamsRunning,1));
		CU_ASSERT_TRUE(wait_for(lc_pauline,lc_marie,&stat_marie->number_of_LinphoneCallStreamsRunning,1));
		/*just to sleep*/
		wait_for(lc_pauline,lc_marie,&stat_marie->number_of_LinphoneCallStreamsRunning,3);
		linphone_core_terminate_all_calls(lc_pauline);
		CU_ASSERT_TRUE(wait_for(lc_pauline,lc_marie,&stat_pauline->number_of_LinphoneCallEnd,1));
		CU_ASSERT_TRUE(wait_for(lc_pauline,lc_marie,&stat_marie->number_of_LinphoneCallEnd,1));
	}
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

Ghislain MARY's avatar
Ghislain MARY committed
569 570

static void cancelled_call(void) {
Simon Morlat's avatar
Simon Morlat committed
571 572
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_rc");
jehan's avatar
jehan committed
573 574 575

	LinphoneCall* out_call = linphone_core_invite(pauline->lc,"marie");
	linphone_call_ref(out_call);
jehan's avatar
jehan committed
576
	CU_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&pauline->stat.number_of_LinphoneCallOutgoingInit,1));
jehan's avatar
jehan committed
577 578

	linphone_core_terminate_call(pauline->lc,out_call);
jehan's avatar
jehan committed
579
	CU_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&pauline->stat.number_of_LinphoneCallEnd,1));
jehan's avatar
jehan committed
580
	//CU_ASSERT_EQUAL(linphone_call_get_reason(out_call),LinphoneReasonCanceled);
jehan's avatar
jehan committed
581
	CU_ASSERT_EQUAL(pauline->stat.number_of_LinphoneCallEnd,1);
jehan's avatar
jehan committed
582
	CU_ASSERT_EQUAL(marie->stat.number_of_LinphoneCallIncomingReceived,0);
583 584
	CU_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&pauline->stat.number_of_LinphoneCallReleased,1));

jehan's avatar
jehan committed
585 586 587 588
	linphone_call_unref(out_call);
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}
jehan's avatar
jehan committed
589

590
static void disable_all_audio_codecs_except_one(LinphoneCore *lc, const char *mime, int rate){
591 592
	const MSList *elem=linphone_core_get_audio_codecs(lc);
	PayloadType *pt;
593

594 595 596 597
	for(;elem!=NULL;elem=elem->next){
		pt=(PayloadType*)elem->data;
		linphone_core_enable_payload_type(lc,pt,FALSE);
	}
598
	pt=linphone_core_find_payload_type(lc,mime,rate,-1);
599 600 601 602
	CU_ASSERT_PTR_NOT_NULL_FATAL(pt);
	linphone_core_enable_payload_type(lc,pt,TRUE);
}

603
#ifdef VIDEO_ENABLED
604 605 606 607 608 609 610 611 612 613 614
static void disable_all_video_codecs_except_one(LinphoneCore *lc, const char *mime) {
	const MSList *codecs = linphone_core_get_video_codecs(lc);
	const MSList *it = NULL;
	PayloadType *pt = NULL;

	for(it = codecs; it != NULL; it = it->next) {
		linphone_core_enable_payload_type(lc, (PayloadType *)it->data, FALSE);
	}
	CU_ASSERT_PTR_NOT_NULL_FATAL(pt = linphone_core_find_payload_type(lc, mime, -1, -1));
	linphone_core_enable_payload_type(lc, pt, TRUE);
}
615
#endif
616

617 618 619 620 621
static void call_failed_because_of_codecs(void) {
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_rc");
	LinphoneCall* out_call;

622 623
	disable_all_audio_codecs_except_one(marie->lc,"pcmu",-1);
	disable_all_audio_codecs_except_one(pauline->lc,"pcma",-1);
624 625 626 627
	out_call = linphone_core_invite(pauline->lc,"marie");
	linphone_call_ref(out_call);
	CU_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&pauline->stat.number_of_LinphoneCallOutgoingInit,1));

jehan's avatar
jehan committed
628 629
	/*flexisip will retain the 488 until the "urgent reply" timeout (I.E 5s) arrives.*/
	CU_ASSERT_TRUE(wait_for_until(pauline->lc,marie->lc,&pauline->stat.number_of_LinphoneCallError,1,7000));
630
	CU_ASSERT_EQUAL(linphone_call_get_reason(out_call),LinphoneReasonNotAcceptable);
631 632 633 634 635 636 637
	CU_ASSERT_EQUAL(marie->stat.number_of_LinphoneCallIncomingReceived,0);

	linphone_call_unref(out_call);
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

Ghislain MARY's avatar
Ghislain MARY committed
638
static void call_with_dns_time_out(void) {
Simon Morlat's avatar
Simon Morlat committed
639
	LinphoneCoreManager* marie = linphone_core_manager_new2( "empty_rc", FALSE);
jehan's avatar
jehan committed
640
	LCSipTransports transport = {9773,0,0,0};
Simon Morlat's avatar
Simon Morlat committed
641
	int i;
642

jehan's avatar
jehan committed
643 644 645
	linphone_core_set_sip_transports(marie->lc,&transport);
	linphone_core_iterate(marie->lc);
	sal_set_dns_timeout(marie->lc->sal,0);
646
	linphone_core_invite(marie->lc,"\"t\x8et\x8e\" <sip:toto@toto.com>"); /*just to use non ascii values*/
Simon Morlat's avatar
Simon Morlat committed
647 648 649
	for(i=0;i<10;i++){
		ms_usleep(200000);
		linphone_core_iterate(marie->lc);
650
	}
jehan's avatar
jehan committed
651 652 653
	CU_ASSERT_EQUAL(marie->stat.number_of_LinphoneCallOutgoingInit,1);
	CU_ASSERT_EQUAL(marie->stat.number_of_LinphoneCallOutgoingProgress,1);
	CU_ASSERT_EQUAL(marie->stat.number_of_LinphoneCallError,1);
654
	CU_ASSERT_EQUAL(marie->stat.number_of_LinphoneCallReleased,1);
jehan's avatar
jehan committed
655 656 657
	linphone_core_manager_destroy(marie);
}

658 659
static void early_cancelled_call(void) {
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
660
	LinphoneCoreManager* pauline = linphone_core_manager_new2( "empty_rc",FALSE);
661

662
	LinphoneCall* out_call = linphone_core_invite_address(pauline->lc,marie->identity);
663

664 665
	CU_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&pauline->stat.number_of_LinphoneCallOutgoingInit,1));
	linphone_core_terminate_call(pauline->lc,out_call);
666

667 668
	/*since everything is executed in a row, no response can be received from the server, thus the CANCEL cannot be sent.
	 It will ring at Marie's side.*/
669

670
	CU_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&pauline->stat.number_of_LinphoneCallEnd,1));
671

672
	CU_ASSERT_EQUAL(pauline->stat.number_of_LinphoneCallEnd,1);
673

674 675 676
	CU_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneCallIncomingReceived,1));
	/* now the CANCEL should have been sent and the the call at marie's side should terminate*/
	CU_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneCallEnd,1));
677

678 679 680 681 682 683
	CU_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&pauline->stat.number_of_LinphoneCallReleased,1));

	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

Ghislain MARY's avatar
Ghislain MARY committed
684
static void cancelled_ringing_call(void) {
Simon Morlat's avatar
Simon Morlat committed
685 686
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_rc");
jehan's avatar
jehan committed
687 688 689

	LinphoneCall* out_call = linphone_core_invite(pauline->lc,"marie");
	linphone_call_ref(out_call);
jehan's avatar
jehan committed
690
	CU_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneCallIncomingReceived,1));
jehan's avatar
jehan committed
691 692

	linphone_core_terminate_call(pauline->lc,out_call);
693 694 695 696 697
	CU_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneCallReleased,1));
	CU_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&pauline->stat.number_of_LinphoneCallReleased,1));
	CU_ASSERT_EQUAL(marie->stat.number_of_LinphoneCallEnd,1);
	CU_ASSERT_EQUAL(pauline->stat.number_of_LinphoneCallEnd,1);

jehan's avatar
jehan committed
698 699 700 701 702
	linphone_call_unref(out_call);
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

Ghislain MARY's avatar
Ghislain MARY committed
703
static void early_declined_call(void) {
Simon Morlat's avatar
Simon Morlat committed
704 705
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_rc");
706
	LinphoneCallLog* out_call_log;
707 708 709 710
	LinphoneCall* out_call;

	linphone_core_set_max_calls(marie->lc,0);
	out_call = linphone_core_invite(pauline->lc,"marie");
jehan's avatar
jehan committed
711 712
	linphone_call_ref(out_call);

713 714
	/*wait until flexisip transfers the busy...*/
	CU_ASSERT_TRUE(wait_for_until(pauline->lc,marie->lc,&pauline->stat.number_of_LinphoneCallError,1,33000));
jehan's avatar
jehan committed
715
	CU_ASSERT_EQUAL(pauline->stat.number_of_LinphoneCallError,1);
716
	/* FIXME http://git.linphone.org/mantis/view.php?id=757
717

718
	CU_ASSERT_EQUAL(linphone_call_get_reason(out_call),LinphoneReasonBusy);
719
	 */
720 721 722
	if (ms_list_size(linphone_core_get_call_logs(pauline->lc))>0) {
		CU_ASSERT_PTR_NOT_NULL(out_call_log=(LinphoneCallLog*)(linphone_core_get_call_logs(pauline->lc)->data));
		CU_ASSERT_EQUAL(linphone_call_log_get_status(out_call_log),LinphoneCallAborted);
jehan's avatar
jehan committed
723 724 725 726 727 728 729
	}
	linphone_call_unref(out_call);
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

static void call_declined(void) {
Simon Morlat's avatar
Simon Morlat committed
730 731
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_rc");
732

733
	LinphoneCall* in_call;
734
	LinphoneCall* out_call = linphone_core_invite_address(pauline->lc,marie->identity);
jehan's avatar
jehan committed
735
	linphone_call_ref(out_call);
jehan's avatar
jehan committed
736
	CU_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneCallIncomingReceived,1));
jehan's avatar
jehan committed
737 738 739 740
	CU_ASSERT_PTR_NOT_NULL(in_call=linphone_core_get_current_call(marie->lc));
	if (in_call) {
		linphone_call_ref(in_call);
		linphone_core_terminate_call(marie->lc,in_call);
741 742 743 744
		CU_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneCallReleased,1));
		CU_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&pauline->stat.number_of_LinphoneCallReleased,1));
		CU_ASSERT_EQUAL(marie->stat.number_of_LinphoneCallEnd,1);
		CU_ASSERT_EQUAL(pauline->stat.number_of_LinphoneCallEnd,1);
jehan's avatar
jehan committed
745 746 747 748
		CU_ASSERT_EQUAL(linphone_call_get_reason(in_call),LinphoneReasonDeclined);
		CU_ASSERT_EQUAL(linphone_call_get_reason(out_call),LinphoneReasonDeclined);
		linphone_call_unref(in_call);
	}
jehan's avatar
jehan committed
749 750 751 752 753
	linphone_call_unref(out_call);
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

Ghislain MARY's avatar
Ghislain MARY committed
754
static void call_terminated_by_caller(void) {
Simon Morlat's avatar
Simon Morlat committed
755 756
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_rc");
757

758 759 760 761 762 763 764 765 766
	CU_ASSERT_TRUE(call(pauline,marie));
	/*just to sleep*/
	linphone_core_terminate_all_calls(pauline->lc);
	CU_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&pauline->stat.number_of_LinphoneCallEnd,1));
	CU_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneCallEnd,1));

	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}
jehan's avatar
jehan committed
767

Simon Morlat's avatar
Simon Morlat committed
768
static void call_with_no_sdp(void) {
jehan's avatar
jehan committed
769
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
Simon Morlat's avatar
Simon Morlat committed
770
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_rc");
771

jehan's avatar
jehan committed
772 773
	linphone_core_enable_sdp_200_ack(marie->lc,TRUE);

Simon Morlat's avatar
Simon Morlat committed
774 775 776 777 778 779 780 781 782 783
	CU_ASSERT_TRUE(call(marie,pauline));
	/*just to sleep*/
	linphone_core_terminate_all_calls(pauline->lc);
	CU_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&pauline->stat.number_of_LinphoneCallEnd,1));
	CU_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneCallEnd,1));

	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

jehan's avatar
jehan committed
784

jehan's avatar
jehan committed
785
static bool_t check_ice(LinphoneCoreManager* caller, LinphoneCoreManager* callee, LinphoneIceState state) {
786
	LinphoneCall *c1,*c2;
787 788 789
	bool_t audio_success=FALSE;
	bool_t video_success=FALSE;
	bool_t video_enabled;
790
	MSTimeSpec ts;
jehan's avatar
jehan committed
791 792 793

	c1=linphone_core_get_current_call(caller->lc);
	c2=linphone_core_get_current_call(callee->lc);
794

795 796
	CU_ASSERT_PTR_NOT_NULL(c1);
	CU_ASSERT_PTR_NOT_NULL(c2);
jehan's avatar
jehan committed
797 798 799 800
	if (!c1 || !c2) return FALSE;
	linphone_call_ref(c1);
	linphone_call_ref(c2);

801 802
	CU_ASSERT_EQUAL(linphone_call_params_video_enabled(linphone_call_get_current_params(c1)),linphone_call_params_video_enabled(linphone_call_get_current_params(c2)));
	video_enabled=linphone_call_params_video_enabled(linphone_call_get_current_params(c1));
803 804
	liblinphone_tester_clock_start(&ts);
	do{
Ghislain MARY's avatar
Ghislain MARY committed
805
		if ((c1 != NULL) && (c2 != NULL)) {
806 807 808
			if (linphone_call_get_audio_stats(c1)->ice_state==state &&
				linphone_call_get_audio_stats(c2)->ice_state==state ){
				audio_success=TRUE;
Ghislain MARY's avatar
Ghislain MARY committed
809 810 811 812
				break;
			}
			linphone_core_iterate(caller->lc);
			linphone_core_iterate(callee->lc);
813
		}
814
		ms_usleep(20000);
815
	}while(!liblinphone_tester_clock_elapsed(&ts,10000));
816

817
	if (video_enabled){
818 819
		liblinphone_tester_clock_start(&ts);
		do{
820 821 822 823 824 825 826 827 828
			if ((c1 != NULL) && (c2 != NULL)) {
				if (linphone_call_get_video_stats(c1)->ice_state==state &&
					linphone_call_get_video_stats(c2)->ice_state==state ){
					video_success=TRUE;
					break;
				}
				linphone_core_iterate(caller->lc);
				linphone_core_iterate(callee->lc);
			}
829
			ms_usleep(20000);
830
		}while(!liblinphone_tester_clock_elapsed(&ts,5000));
831
	}
jehan's avatar
jehan committed
832 833 834 835 836 837 838 839 840 841

	 /*make sure encryption mode are preserved*/
	if (c1) {
		const LinphoneCallParams* call_param = linphone_call_get_current_params(c1);
		CU_ASSERT_EQUAL(linphone_call_params_get_media_encryption(call_param),linphone_core_get_media_encryption(caller->lc));
	}
	if (c2) {
		const LinphoneCallParams* call_param = linphone_call_get_current_params(c2);
		CU_ASSERT_EQUAL(linphone_call_params_get_media_encryption(call_param),linphone_core_get_media_encryption(callee->lc));
	}
jehan's avatar
jehan committed
842 843
	linphone_call_unref(c1);
	linphone_call_unref(c2);
844
	return video_enabled ? audio_success && video_success : audio_success;
jehan's avatar
jehan committed
845
}
846

jehan's avatar
jehan committed
847
static void _call_with_ice_base(LinphoneCoreManager* pauline,LinphoneCoreManager* marie, bool_t caller_with_ice, bool_t callee_with_ice, bool_t random_ports) {
848 849 850 851 852 853 854 855
	if (callee_with_ice){
		linphone_core_set_firewall_policy(marie->lc,LinphonePolicyUseIce);
		linphone_core_set_stun_server(marie->lc,"stun.linphone.org");
	}
	if (caller_with_ice){
		linphone_core_set_firewall_policy(pauline->lc,LinphonePolicyUseIce);
		linphone_core_set_stun_server(pauline->lc,"stun.linphone.org");
	}
856