eventapi_tester.c 24.4 KB
Newer Older
1
/*
Simon Morlat's avatar
Simon Morlat committed
2 3
    liblinphone_tester - liblinphone test suite
    Copyright (C) 2013  Belledonne Communications SARL
4 5 6

    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
8 9 10 11 12 13 14 15 16 17 18
    (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/>.
*/

Simon Morlat's avatar
Simon Morlat committed
19

20

21 22
#include "linphone/core.h"
#include "linphone/lpconfig.h"
Ghislain MARY's avatar
Ghislain MARY committed
23
#include <linphone/event.h>
24 25 26 27 28
#include "liblinphone_tester.h"

static const char *subscribe_content="<somexml>blabla</somexml>";
static const char *notify_content="<somexml2>blabla</somexml2>";

Simon Morlat's avatar
Simon Morlat committed
29 30 31 32 33 34 35 36
const char *liblinphone_tester_get_subscribe_content(void){
	return subscribe_content;
}

const char *liblinphone_tester_get_notify_content(void){
	return notify_content;
}

37
void linphone_notify_received(LinphoneCore *lc, LinphoneEvent *lev, const char *eventname, const LinphoneContent *content){
38
	LinphoneCoreManager *mgr;
39
	const char * ua = linphone_event_get_custom_header(lev, "User-Agent");
40
	if (!BC_ASSERT_PTR_NOT_NULL(content)) return;
41
	if (!linphone_content_is_multipart(content) && (!ua ||  !strstr(ua, "flexisip"))) { /*disable check for full presence server support*/
jehan's avatar
jehan committed
42
		/*hack to disable content checking for list notify */
Ghislain MARY's avatar
Ghislain MARY committed
43 44
		BC_ASSERT_STRING_EQUAL(notify_content,(const char*)linphone_content_get_buffer(content));
	}
45
	mgr=get_manager(lc);
46
	mgr->stat.number_of_NotifyReceived++;
47 48 49
}

void linphone_subscription_state_change(LinphoneCore *lc, LinphoneEvent *lev, LinphoneSubscriptionState state) {
50 51
	stats* counters = get_stats(lc);
	LinphoneCoreManager *mgr=get_manager(lc);
52
	LinphoneContent* content;
53 54
	const LinphoneAddress* from_addr = linphone_event_get_from(lev);
	char* from = linphone_address_as_string(from_addr);
55 56 57
	content = linphone_core_create_content(lc);
	linphone_content_set_type(content,"application");
	linphone_content_set_subtype(content,"somexml2");
58
	linphone_content_set_buffer(content,notify_content,strlen(notify_content));
59

60 61 62
	ms_message("Subscription state [%s] from [%s]",linphone_subscription_state_to_string(state),from);
	ms_free(from);

63 64 65 66 67
	switch(state){
		case LinphoneSubscriptionNone:
		break;
		case LinphoneSubscriptionIncomingReceived:
			counters->number_of_LinphoneSubscriptionIncomingReceived++;
68
			mgr->lev=lev;
69 70 71 72
			if (!mgr->decline_subscribe)
				linphone_event_accept_subscription(lev);
			else
				linphone_event_deny_subscription(lev, LinphoneReasonDeclined);
73
		break;
74 75
		case LinphoneSubscriptionOutgoingProgress:
			counters->number_of_LinphoneSubscriptionOutgoingProgress++;
76 77 78 79 80 81
		break;
		case LinphoneSubscriptionPending:
			counters->number_of_LinphoneSubscriptionPending++;
		break;
		case LinphoneSubscriptionActive:
			counters->number_of_LinphoneSubscriptionActive++;
Simon Morlat's avatar
Simon Morlat committed
82
			if (linphone_event_get_subscription_dir(lev)==LinphoneSubscriptionIncoming){
83
				mgr->lev=lev;
84
				if(strcmp(linphone_event_get_name(lev), "conference") == 0) {
85
					// TODO : Get LocalConfEventHandler and call handler->subscribeReceived(lev)
86 87 88
				} else {
					linphone_event_notify(lev,content);
				}
89
			}
90 91 92
		break;
		case LinphoneSubscriptionTerminated:
			counters->number_of_LinphoneSubscriptionTerminated++;
93
			mgr->lev=NULL;
94 95 96
		break;
		case LinphoneSubscriptionError:
			counters->number_of_LinphoneSubscriptionError++;
97
			mgr->lev=NULL;
98
		break;
99 100 101 102
		case LinphoneSubscriptionExpiring:
			counters->number_of_LinphoneSubscriptionExpiring++;
			mgr->lev=NULL;
		break;
103
	}
104
	linphone_content_unref(content);
105 106
}

107 108
void linphone_publish_state_changed(LinphoneCore *lc, LinphoneEvent *ev, LinphonePublishState state){
	stats* counters = get_stats(lc);
109 110 111 112
	const LinphoneAddress* from_addr = linphone_event_get_from(ev);
	char* from = linphone_address_as_string(from_addr);
	ms_message("Publish state [%s] from [%s]",linphone_publish_state_to_string(state),from);
	ms_free(from);
113 114
	switch(state){
		case LinphonePublishProgress: counters->number_of_LinphonePublishProgress++; break;
115
		case LinphonePublishOk:
116
			/*make sure custom header access API is working*/
117 118
			BC_ASSERT_PTR_NOT_NULL(linphone_event_get_custom_header(ev,"From"));
			counters->number_of_LinphonePublishOk++;
119
			break;
120 121 122 123 124 125
		case LinphonePublishError: counters->number_of_LinphonePublishError++; break;
		case LinphonePublishExpiring: counters->number_of_LinphonePublishExpiring++; break;
		case LinphonePublishCleared: counters->number_of_LinphonePublishCleared++;break;
		default:
		break;
	}
126

127 128
}

129
static void subscribe_test_declined(void) {
Simon Morlat's avatar
Simon Morlat committed
130
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
131
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
132
	LinphoneContent* content;
133 134
	LinphoneEvent *lev;
	const LinphoneErrorInfo *ei;
135 136
	bctbx_list_t* lcs=bctbx_list_append(NULL,marie->lc);
	lcs=bctbx_list_append(lcs,pauline->lc);
137

138 139 140
	content = linphone_core_create_content(marie->lc);
	linphone_content_set_type(content,"application");
	linphone_content_set_subtype(content,"somexml");
141
	linphone_content_set_buffer(content,subscribe_content,strlen(subscribe_content));
142

143
	pauline->decline_subscribe=TRUE;
144 145

	lev=linphone_core_subscribe(marie->lc,pauline->identity,"dodo",600,content);
146
	linphone_event_ref(lev);
147

148
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionOutgoingProgress,1,1000));
149 150
	BC_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionIncomingReceived,1,3000));
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionError,1,21000));/*yes flexisip may wait 20 secs in case of forking*/
151
	ei=linphone_event_get_error_info(lev);
152
	BC_ASSERT_PTR_NOT_NULL(ei);
153
	if (ei){
154 155
		BC_ASSERT_EQUAL(linphone_error_info_get_protocol_code(ei),603, int, "%d");
		BC_ASSERT_PTR_NOT_NULL(linphone_error_info_get_phrase(ei));
156
	}
157
	BC_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionTerminated,1,1000));
158

159
	bctbx_list_free(lcs);
160
	linphone_content_unref(content);
161
	linphone_event_unref(lev);
162 163 164
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}
165

166 167 168 169 170
typedef enum RefreshTestType{
	NoRefresh,
	AutoRefresh,
	ManualRefresh
}RefreshTestType;
171

172
static void subscribe_test_with_args(bool_t terminated_by_subscriber, RefreshTestType refresh_type) {
Simon Morlat's avatar
Simon Morlat committed
173
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
174
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
175
	LinphoneContent* content;
176
	LinphoneEvent *lev;
177
	int expires= refresh_type!=NoRefresh ? 4 : 600;
178
	bctbx_list_t* lcs=bctbx_list_append(NULL,marie->lc);
179

180
	lcs=bctbx_list_append(lcs,pauline->lc);
181

182
	if (refresh_type==ManualRefresh){
183
		lp_config_set_int(linphone_core_get_config(marie->lc),"sip","refresh_generic_subscribe",0);
184
	}
185

186 187 188
	content = linphone_core_create_content(marie->lc);
	linphone_content_set_type(content,"application");
	linphone_content_set_subtype(content,"somexml");
189
	linphone_content_set_buffer(content,subscribe_content,strlen(subscribe_content));
190 191

	lev=linphone_core_subscribe(marie->lc,pauline->identity,"dodo",expires,content);
192
	linphone_event_ref(lev);
193
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionOutgoingProgress,1,1000));
194 195 196
	BC_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionIncomingReceived,1,3000));
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionActive,1,3000));
	BC_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionActive,1,1000));
197

198
	/*make sure marie receives first notification before terminating*/
199
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_NotifyReceived,1,1000));
200

201
	if (refresh_type==AutoRefresh){
202
		wait_for_list(lcs,NULL,0,6000);
203 204 205 206
		BC_ASSERT_PTR_NOT_NULL(pauline->lev);
		if (pauline->lev){
			BC_ASSERT_EQUAL(linphone_event_get_subscription_state(pauline->lev), LinphoneSubscriptionActive, int, "%d");
		}
207
	}else if (refresh_type==ManualRefresh){
208
		BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionExpiring,1,4000));
209
		linphone_event_update_subscribe(lev,NULL);
210
		BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionActive,2,2000));
211
	}
212

213 214 215
	if (terminated_by_subscriber){
		linphone_event_terminate(lev);
	}else{
216
		BC_ASSERT_PTR_NOT_NULL(pauline->lev);
217 218
		linphone_event_terminate(pauline->lev);
	}
219

220 221
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionTerminated,1,1000));
	BC_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionTerminated,1,1000));
222

223
	bctbx_list_free(lcs);
224
	linphone_event_unref(lev);
225
	linphone_content_unref(content);
226 227 228 229
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

230 231
static void subscribe_test_with_args2(bool_t terminated_by_subscriber, RefreshTestType refresh_type) {
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
232
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
233
	LinphoneContent* content;
234 235
	LinphoneEvent *lev;
	int expires= refresh_type!=NoRefresh ? 4 : 600;
236
	bctbx_list_t* lcs=bctbx_list_append(NULL,marie->lc);
237

238
	lcs=bctbx_list_append(lcs,pauline->lc);
239 240

	if (refresh_type==ManualRefresh){
241
		lp_config_set_int(linphone_core_get_config(marie->lc),"sip","refresh_generic_subscribe",0);
242 243
	}

244 245 246
	content = linphone_core_create_content(marie->lc);
	linphone_content_set_type(content,"application");
	linphone_content_set_subtype(content,"somexml");
247
	linphone_content_set_buffer(content,subscribe_content,strlen(subscribe_content));
248

249 250 251
	lev=linphone_core_create_subscribe(marie->lc,pauline->identity,"dodo",expires);
	linphone_event_add_custom_header(lev,"My-Header","pouet");
	linphone_event_add_custom_header(lev,"My-Header2","pimpon");
252 253
	linphone_event_send_subscribe(lev,content);

254
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionOutgoingProgress,1,1000));
255
	BC_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionIncomingReceived,1,3000));
256

257 258 259 260 261
	if (pauline->stat.number_of_LinphoneSubscriptionIncomingReceived == 1) {
		/*check good receipt of custom headers*/
		BC_ASSERT_STRING_EQUAL(linphone_event_get_custom_header(pauline->lev,"My-Header"),"pouet");
		BC_ASSERT_STRING_EQUAL(linphone_event_get_custom_header(pauline->lev,"My-Header2"),"pimpon");
	}
262 263
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionActive,1,5000));
	BC_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionActive,1,5000));
264 265

	/*make sure marie receives first notification before terminating*/
266 267
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_NotifyReceived,1,5000));

268 269
	if (refresh_type==AutoRefresh){
		wait_for_list(lcs,NULL,0,6000);
270
		BC_ASSERT_EQUAL(linphone_event_get_subscription_state(pauline->lev), LinphoneSubscriptionActive, int, "%d");
271
	}else if (refresh_type==ManualRefresh){
272
		BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionExpiring,1,4000));
273
		linphone_event_update_subscribe(lev,NULL);
274
		BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionActive,2,5000));
275 276 277 278 279
	}

	if (terminated_by_subscriber){
		linphone_event_terminate(lev);
	}else{
280
		BC_ASSERT_PTR_NOT_NULL(pauline->lev);
281 282
		linphone_event_terminate(pauline->lev);
	}
283

284 285
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionTerminated,1,5000));
	BC_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionTerminated,1,5000));
286 287

	linphone_content_unref(content);
288 289
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
290
	bctbx_list_free(lcs);
291 292
}

293
static void subscribe_test_terminated_by_subscriber(void){
294
	subscribe_test_with_args(TRUE,NoRefresh);
295 296 297
}

static void subscribe_test_terminated_by_notifier(void){
298
	subscribe_test_with_args(FALSE,NoRefresh);
299 300
}

301
/* Caution: this test does not really check that the subscribe are refreshed, because the core is not managing the expiration of
302 303 304
 * unrefreshed subscribe dialogs. So it is just checking that it is not crashing.
 */
static void subscribe_test_refreshed(void){
305 306 307
	subscribe_test_with_args(TRUE,AutoRefresh);
}

308 309 310 311
static void subscribe_test_with_custom_header(void){
	subscribe_test_with_args2(TRUE,NoRefresh);
}

312 313
static void subscribe_test_manually_refreshed(void){
	subscribe_test_with_args(TRUE,ManualRefresh);
314 315
}

316
static void subscribe_loosing_dialog(void) {
317 318 319 320 321 322
#ifdef WIN32
	/*Unfortunately this test doesn't work on windows due to the way closed TCP ports behave.
	 * Unlike linux and macOS, released TCP port don't send an ICMP error (or maybe at least for a period of time.
	 * This prevents this test from working, see comments below*/
	ms_warning("subscribe_loosing_dialog() skipped on windows.");
#else
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
	LinphoneContent* content;
	LinphoneEvent *lev;
	int expires= 4;
	bctbx_list_t* lcs=bctbx_list_append(NULL,marie->lc);

	lcs=bctbx_list_append(lcs,pauline->lc);

	content = linphone_core_create_content(marie->lc);
	linphone_content_set_type(content,"application");
	linphone_content_set_subtype(content,"somexml");
	linphone_content_set_buffer(content,subscribe_content,strlen(subscribe_content));

	lev=linphone_core_create_subscribe(marie->lc,pauline->identity,"dodo",expires);
	linphone_event_add_custom_header(lev,"My-Header","pouet");
	linphone_event_add_custom_header(lev,"My-Header2","pimpon");
	linphone_event_send_subscribe(lev,content);

	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionOutgoingProgress,1,1000));
	BC_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionIncomingReceived,1,3000));

	
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionActive,1,5000));
	BC_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionActive,1,5000));

	/*make sure marie receives first notification before terminating*/
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_NotifyReceived,1,5000));

	/* now pauline looses internet connection and reboots */
	linphone_core_set_network_reachable(pauline->lc, FALSE);
	/*let expire the incoming subscribe received by pauline */
	BC_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionTerminated,1,5000));
	lcs = bctbx_list_remove(lcs, pauline->lc);
	linphone_core_manager_destroy(pauline);
	pauline = linphone_core_manager_new( "pauline_tcp_rc");
	lcs = bctbx_list_append(lcs, pauline->lc);
	
361 362 363
	/* Marie will retry the subscription.
	 * She will first receive a 503 Service unavailable from flexisip thanks the ICMP error returned by the no longer existing Pauline.
	 * Then she will forge a new SUBSCRIBE in order to restart a new dialog, and this one will reach the new Pauline.*/
364 365 366 367
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionOutgoingProgress,2,8000));
	/*and get it accepted again*/
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionActive,2,5000));
	BC_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionActive,1,5000));
368 369 370
	BC_ASSERT_PTR_NOT_NULL(pauline->lev);
	if (pauline->lev) BC_ASSERT_EQUAL(linphone_event_get_subscription_state(pauline->lev), LinphoneSubscriptionActive, int, "%d");
	
371 372 373 374 375 376 377 378 379 380 381
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_NotifyReceived,2,5000));
	linphone_event_terminate(lev);
	

	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionTerminated,1,5000));
	BC_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionTerminated,1,5000));

	linphone_content_unref(content);
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
	bctbx_list_free(lcs);
382
#endif
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
}

static void subscribe_with_io_error(void) {
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
	LinphoneContent* content;
	LinphoneEvent *lev;
	int expires= 4;
	bctbx_list_t* lcs=bctbx_list_append(NULL,marie->lc);

	lcs=bctbx_list_append(lcs,pauline->lc);

	content = linphone_core_create_content(marie->lc);
	linphone_content_set_type(content,"application");
	linphone_content_set_subtype(content,"somexml");
	linphone_content_set_buffer(content,subscribe_content,strlen(subscribe_content));

	lev=linphone_core_create_subscribe(marie->lc,pauline->identity,"dodo",expires);
	linphone_event_add_custom_header(lev,"My-Header","pouet");
	linphone_event_add_custom_header(lev,"My-Header2","pimpon");
	linphone_event_send_subscribe(lev,content);

	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionOutgoingProgress,1,1000));
	BC_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionIncomingReceived,1,3000));

	
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionActive,1,5000));
	BC_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionActive,1,5000));

	/*make sure marie receives first notification before terminating*/
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_NotifyReceived,1,5000));

	/* now marie gets network errors when refreshing*/
416
	sal_set_send_error(linphone_core_get_sal(marie->lc), -1);
417 418 419
	
	/*marie will retry the subscription*/
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionOutgoingProgress,2,8000));
420
	sal_set_send_error(linphone_core_get_sal(marie->lc), 0);
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
	
	/*and get it accepted again*/
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionActive,2,10000));
	BC_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionActive,2,5000));
	BC_ASSERT_EQUAL(linphone_event_get_subscription_state(pauline->lev), LinphoneSubscriptionActive, int, "%d");
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_NotifyReceived,2,5000));
	linphone_event_terminate(lev);
	

	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionTerminated,1,5000));
	BC_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionTerminated,1,5000));

	linphone_content_unref(content);
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
	bctbx_list_free(lcs);
}

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
static void subscribe_not_timely_responded(void) {
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
	LinphoneContent* content;
	LinphoneEvent *lev;
	int expires= 4;
	bctbx_list_t* lcs=bctbx_list_append(NULL,marie->lc);

	lcs=bctbx_list_append(lcs,pauline->lc);

	content = linphone_core_create_content(marie->lc);
	linphone_content_set_type(content,"application");
	linphone_content_set_subtype(content,"somexml");
	linphone_content_set_buffer(content,subscribe_content,strlen(subscribe_content));

	lev=linphone_core_create_subscribe(marie->lc,pauline->identity,"dodo",expires);
	linphone_event_add_custom_header(lev,"My-Header","pouet");
	linphone_event_add_custom_header(lev,"My-Header2","pimpon");
	linphone_event_send_subscribe(lev,content);

	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionOutgoingProgress,1,1000));
	BC_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionIncomingReceived,1,3000));

	
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionActive,1,5000));
	BC_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionActive,1,5000));

	/*make sure marie receives first notification before terminating*/
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_NotifyReceived,1,5000));

	/* now pauline is no longer scheduled (simulating a very big latency in the network) */
	lcs = bctbx_list_remove(lcs, pauline->lc);
	/*marie's dialog will expire while the SUBSCRIBE refresh is in progress*/
	wait_for_list(lcs, NULL, 0, 8000);
	
	lcs = bctbx_list_append(lcs, pauline->lc);
	wait_for_list(lcs, NULL, 0, 3000);
	linphone_event_terminate(lev);
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionTerminated,1,5000));
	BC_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionTerminated,1,5000));

	linphone_content_unref(content);
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
	bctbx_list_free(lcs);
}

486

Simon Morlat's avatar
Simon Morlat committed
487
static void publish_test_with_args(bool_t refresh, int expires){
488
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
489
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
490
	LinphoneContent* content;
491
	LinphoneEvent *lev;
492 493
	bctbx_list_t* lcs=bctbx_list_append(NULL,marie->lc);
	lcs=bctbx_list_append(lcs,pauline->lc);
494

495 496 497
	content = linphone_core_create_content(marie->lc);
	linphone_content_set_type(content,"application");
	linphone_content_set_subtype(content,"somexml");
498
	linphone_content_set_buffer(content,subscribe_content,strlen(subscribe_content));
499

500
	lp_config_set_int(linphone_core_get_config(marie->lc),"sip","refresh_generic_publish",refresh);
501

Simon Morlat's avatar
Simon Morlat committed
502
	lev=linphone_core_create_publish(marie->lc,pauline->identity,"dodo",expires);
503
	linphone_event_add_custom_header(lev,"CustomHeader","someValue");
504
	linphone_event_ref(lev);
505 506
	linphone_event_send_publish(lev,content);
	
507

508 509
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphonePublishProgress,1,1000));
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphonePublishOk,1,3000));
510

511
	if (!refresh){
512
		BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphonePublishExpiring,1,5000));
513
		linphone_event_update_publish(lev,content);
514 515
		BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphonePublishProgress,1,1000));
		BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphonePublishOk,1,3000));
516
	}else{
517

518 519 520
	}

	linphone_event_terminate(lev);
521

522
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphonePublishCleared,1,3000));
523

524
	linphone_event_unref(lev);
525 526

	linphone_content_unref(content);
527 528 529 530
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

531
static void publish_test(void){
Simon Morlat's avatar
Simon Morlat committed
532
	publish_test_with_args(TRUE,5);
533 534
}

535
static void publish_no_auto_test(void){
Simon Morlat's avatar
Simon Morlat committed
536 537 538
	publish_test_with_args(FALSE,5);
}

539
static void publish_without_expires(void){
Simon Morlat's avatar
Simon Morlat committed
540
	publish_test_with_args(TRUE,-1);
541 542
}

543 544 545 546 547
static void out_of_dialog_notify(void){
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
	LinphoneContent* content;
	LinphoneEvent *lev;
548 549
	bctbx_list_t* lcs=bctbx_list_append(NULL,marie->lc);
	lcs=bctbx_list_append(lcs,pauline->lc);
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571

	content = linphone_core_create_content(marie->lc);
	linphone_content_set_type(content,"application");
	linphone_content_set_subtype(content,"somexml");
	linphone_content_set_buffer(content,notify_content,strlen(notify_content));

	lev = linphone_core_create_notify(marie->lc,pauline->identity,"dodo");
	linphone_event_ref(lev);
	linphone_event_add_custom_header(lev,"CustomHeader","someValue");
	linphone_event_notify(lev,content);
	

	BC_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_NotifyReceived,1,3000));
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionTerminated,1,3000));

	linphone_event_unref(lev);

	linphone_content_unref(content);
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

572
test_t event_tests[] = {
jehan's avatar
jehan committed
573
	TEST_ONE_TAG("Subscribe declined", subscribe_test_declined, "presence"),
jehan's avatar
jehan committed
574
	TEST_ONE_TAG("Subscribe terminated by subscriber", subscribe_test_terminated_by_subscriber, "presence"),
jehan's avatar
jehan committed
575
	TEST_ONE_TAG("Subscribe with custom headers", subscribe_test_with_custom_header, "presence"),
jehan's avatar
jehan committed
576
	TEST_ONE_TAG("Subscribe refreshed", subscribe_test_refreshed, "presence"),
577 578
	TEST_ONE_TAG("Subscribe loosing dialog", subscribe_loosing_dialog, "presence"),
	TEST_ONE_TAG("Subscribe with io error", subscribe_with_io_error, "presence"),
jehan's avatar
jehan committed
579
	TEST_ONE_TAG("Subscribe manually refreshed", subscribe_test_manually_refreshed, "presence"),
580
	TEST_ONE_TAG("Subscribe terminated by notifier", subscribe_test_terminated_by_notifier, "presence"),
581
	TEST_ONE_TAG("Subscribe not timely responded", subscribe_not_timely_responded, "presence"),
jehan's avatar
jehan committed
582 583
	TEST_ONE_TAG("Publish", publish_test, "presence"),
	TEST_ONE_TAG("Publish without expires", publish_without_expires, "presence"),
584 585
	TEST_ONE_TAG("Publish without automatic refresh",publish_no_auto_test, "presence"),
	TEST_ONE_TAG("Out of dialog notify", out_of_dialog_notify, "presence")
586 587
};

588
test_suite_t event_test_suite = {"Event", NULL, NULL, liblinphone_tester_before_each, liblinphone_tester_after_each,
589
								 sizeof(event_tests) / sizeof(event_tests[0]), event_tests};