eventapi_tester.c 14.3 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 "linphonecore.h"
#include "private.h"
23
#include "lpconfig.h"
24 25 26 27 28 29 30
#include <event.h>
#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
31 32 33 34 35 36 37 38
const char *liblinphone_tester_get_subscribe_content(void){
	return subscribe_content;
}

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

39
void linphone_notify_received(LinphoneCore *lc, LinphoneEvent *lev, const char *eventname, const LinphoneContent *content){
40
	LinphoneCoreManager *mgr;
41
	BC_ASSERT_PTR_NOT_NULL_FATAL(content);
Ghislain MARY's avatar
Ghislain MARY committed
42 43 44
	if (!linphone_content_is_multipart(content)) {
		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
		case LinphoneSubscriptionOutgoingInit:
75 76 77 78 79 80 81
			counters->number_of_LinphoneSubscriptionOutgoingInit++;
		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
				linphone_event_notify(lev,content);
85
			}
86 87 88
		break;
		case LinphoneSubscriptionTerminated:
			counters->number_of_LinphoneSubscriptionTerminated++;
89
			mgr->lev=NULL;
90 91 92
		break;
		case LinphoneSubscriptionError:
			counters->number_of_LinphoneSubscriptionError++;
93
			mgr->lev=NULL;
94
		break;
95 96 97 98
		case LinphoneSubscriptionExpiring:
			counters->number_of_LinphoneSubscriptionExpiring++;
			mgr->lev=NULL;
		break;
99
	}
100
	linphone_content_unref(content);
101 102
}

103 104
void linphone_publish_state_changed(LinphoneCore *lc, LinphoneEvent *ev, LinphonePublishState state){
	stats* counters = get_stats(lc);
105 106 107 108
	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);
109 110
	switch(state){
		case LinphonePublishProgress: counters->number_of_LinphonePublishProgress++; break;
111
		case LinphonePublishOk:
112
			/*make sure custom header access API is working*/
113 114
			BC_ASSERT_PTR_NOT_NULL(linphone_event_get_custom_header(ev,"From"));
			counters->number_of_LinphonePublishOk++;
115
			break;
116 117 118 119 120 121
		case LinphonePublishError: counters->number_of_LinphonePublishError++; break;
		case LinphonePublishExpiring: counters->number_of_LinphonePublishExpiring++; break;
		case LinphonePublishCleared: counters->number_of_LinphonePublishCleared++;break;
		default:
		break;
	}
122

123 124
}

125
static void subscribe_test_declined(void) {
Simon Morlat's avatar
Simon Morlat committed
126
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
127
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
128
	LinphoneContent* content;
129 130
	LinphoneEvent *lev;
	const LinphoneErrorInfo *ei;
131 132
	MSList* lcs=ms_list_append(NULL,marie->lc);
	lcs=ms_list_append(lcs,pauline->lc);
133

134 135 136
	content = linphone_core_create_content(marie->lc);
	linphone_content_set_type(content,"application");
	linphone_content_set_subtype(content,"somexml");
137
	linphone_content_set_buffer(content,subscribe_content,strlen(subscribe_content));
138

139
	pauline->decline_subscribe=TRUE;
140 141

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

144 145 146
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionOutgoingInit,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_LinphoneSubscriptionError,1,21000));/*yes flexisip may wait 20 secs in case of forking*/
147
	ei=linphone_event_get_error_info(lev);
148
	BC_ASSERT_PTR_NOT_NULL(ei);
149
	if (ei){
150 151
		BC_ASSERT_EQUAL(linphone_error_info_get_protocol_code(ei),603, int, "%d");
		BC_ASSERT_PTR_NOT_NULL(linphone_error_info_get_phrase(ei));
152
	}
153
	BC_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionTerminated,1,1000));
154 155

	linphone_content_unref(content);
156
	linphone_event_unref(lev);
157 158 159
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}
160

161 162 163 164 165
typedef enum RefreshTestType{
	NoRefresh,
	AutoRefresh,
	ManualRefresh
}RefreshTestType;
166

167
static void subscribe_test_with_args(bool_t terminated_by_subscriber, RefreshTestType refresh_type) {
Simon Morlat's avatar
Simon Morlat committed
168
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
169
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
170
	LinphoneContent* content;
171
	LinphoneEvent *lev;
172
	int expires= refresh_type!=NoRefresh ? 4 : 600;
173
	MSList* lcs=ms_list_append(NULL,marie->lc);
174

175 176
	lcs=ms_list_append(lcs,pauline->lc);

177 178 179
	if (refresh_type==ManualRefresh){
		lp_config_set_int(marie->lc->config,"sip","refresh_generic_subscribe",0);
	}
180

181 182 183
	content = linphone_core_create_content(marie->lc);
	linphone_content_set_type(content,"application");
	linphone_content_set_subtype(content,"somexml");
184
	linphone_content_set_buffer(content,subscribe_content,strlen(subscribe_content));
185 186

	lev=linphone_core_subscribe(marie->lc,pauline->identity,"dodo",expires,content);
187 188 189 190 191

	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionOutgoingInit,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,3000));
	BC_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionActive,1,1000));
192

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

196
	if (refresh_type==AutoRefresh){
197
		wait_for_list(lcs,NULL,0,6000);
198
		BC_ASSERT_EQUAL(linphone_event_get_subscription_state(pauline->lev), LinphoneSubscriptionActive, int, "%d");
199
	}else if (refresh_type==ManualRefresh){
200
		BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionExpiring,1,4000));
201
		linphone_event_update_subscribe(lev,NULL);
202
		BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionActive,2,2000));
203
	}
204

205 206 207
	if (terminated_by_subscriber){
		linphone_event_terminate(lev);
	}else{
208
		BC_ASSERT_PTR_NOT_NULL_FATAL(pauline->lev);
209 210
		linphone_event_terminate(pauline->lev);
	}
211

212 213
	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));
214 215

	linphone_content_unref(content);
216 217 218 219
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

220 221
static void subscribe_test_with_args2(bool_t terminated_by_subscriber, RefreshTestType refresh_type) {
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
222
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
223
	LinphoneContent* content;
224 225 226
	LinphoneEvent *lev;
	int expires= refresh_type!=NoRefresh ? 4 : 600;
	MSList* lcs=ms_list_append(NULL,marie->lc);
227

228 229 230 231 232 233
	lcs=ms_list_append(lcs,pauline->lc);

	if (refresh_type==ManualRefresh){
		lp_config_set_int(marie->lc->config,"sip","refresh_generic_subscribe",0);
	}

234 235 236
	content = linphone_core_create_content(marie->lc);
	linphone_content_set_type(content,"application");
	linphone_content_set_subtype(content,"somexml");
237
	linphone_content_set_buffer(content,subscribe_content,strlen(subscribe_content));
238

239 240 241
	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");
242 243
	linphone_event_send_subscribe(lev,content);

244 245
	BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionOutgoingInit,1,1000));
	BC_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionIncomingReceived,1,3000));
246

247 248 249 250 251
	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");
	}
252 253
	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));
254 255

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

258 259
	if (refresh_type==AutoRefresh){
		wait_for_list(lcs,NULL,0,6000);
260
		BC_ASSERT_EQUAL(linphone_event_get_subscription_state(pauline->lev), LinphoneSubscriptionActive, int, "%d");
261
	}else if (refresh_type==ManualRefresh){
262
		BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionExpiring,1,4000));
263
		linphone_event_update_subscribe(lev,NULL);
264
		BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionActive,2,5000));
265 266 267 268 269
	}

	if (terminated_by_subscriber){
		linphone_event_terminate(lev);
	}else{
270
		BC_ASSERT_PTR_NOT_NULL_FATAL(pauline->lev);
271 272
		linphone_event_terminate(pauline->lev);
	}
273

274 275
	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));
276 277

	linphone_content_unref(content);
278 279 280 281
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

282
static void subscribe_test_terminated_by_subscriber(void){
283
	subscribe_test_with_args(TRUE,NoRefresh);
284 285 286
}

static void subscribe_test_terminated_by_notifier(void){
287
	subscribe_test_with_args(FALSE,NoRefresh);
288 289
}

290
/* Caution: this test does not really check that the subscribe are refreshed, because the core is not managing the expiration of
291 292 293
 * unrefreshed subscribe dialogs. So it is just checking that it is not crashing.
 */
static void subscribe_test_refreshed(void){
294 295 296
	subscribe_test_with_args(TRUE,AutoRefresh);
}

297 298 299 300
static void subscribe_test_with_custom_header(void){
	subscribe_test_with_args2(TRUE,NoRefresh);
}

301 302
static void subscribe_test_manually_refreshed(void){
	subscribe_test_with_args(TRUE,ManualRefresh);
303 304
}

Simon Morlat's avatar
Simon Morlat committed
305
static void publish_test_with_args(bool_t refresh, int expires){
306
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
307
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
308
	LinphoneContent* content;
309 310 311 312
	LinphoneEvent *lev;
	MSList* lcs=ms_list_append(NULL,marie->lc);
	lcs=ms_list_append(lcs,pauline->lc);

313 314 315
	content = linphone_core_create_content(marie->lc);
	linphone_content_set_type(content,"application");
	linphone_content_set_subtype(content,"somexml");
316
	linphone_content_set_buffer(content,subscribe_content,strlen(subscribe_content));
317

318
	lp_config_set_int(marie->lc->config,"sip","refresh_generic_publish",refresh);
319

Simon Morlat's avatar
Simon Morlat committed
320
	lev=linphone_core_create_publish(marie->lc,pauline->identity,"dodo",expires);
321
	linphone_event_add_custom_header(lev,"CustomHeader","someValue");
322
	linphone_event_send_publish(lev,content);
323
	linphone_event_ref(lev);
324

325 326
	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));
327

328
	if (!refresh){
329
		BC_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphonePublishExpiring,1,5000));
330
		linphone_event_update_publish(lev,content);
331 332
		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));
333
	}else{
334

335 336 337
	}

	linphone_event_terminate(lev);
338

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

341
	linphone_event_unref(lev);
342 343

	linphone_content_unref(content);
344 345 346 347
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

348
static void publish_test(void){
Simon Morlat's avatar
Simon Morlat committed
349
	publish_test_with_args(TRUE,5);
350 351
}

352
static void publish_no_auto_test(void){
Simon Morlat's avatar
Simon Morlat committed
353 354 355
	publish_test_with_args(FALSE,5);
}

356
static void publish_without_expires(void){
Simon Morlat's avatar
Simon Morlat committed
357
	publish_test_with_args(TRUE,-1);
358 359 360
}

test_t event_tests[] = {
361 362
	{ "Subscribe declined"	,	subscribe_test_declined 	},
	{ "Subscribe terminated by subscriber", subscribe_test_terminated_by_subscriber },
363
	{ "Subscribe with custom headers", subscribe_test_with_custom_header },
364
	{ "Subscribe refreshed", subscribe_test_refreshed },
365
	{ "Subscribe manually refreshed", subscribe_test_manually_refreshed },
366 367
	{ "Subscribe terminated by notifier", subscribe_test_terminated_by_notifier },
	{ "Publish", publish_test },
Simon Morlat's avatar
Simon Morlat committed
368
	{ "Publish without expires", publish_without_expires },
369
	{ "Publish without automatic refresh",publish_no_auto_test }
370 371
};

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