eventapi_tester.c 5.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
	belle-sip - SIP (RFC3261) library.
    Copyright (C) 2010  Belledonne Communications SARL

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 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>
#include "CUnit/Basic.h"
#include "linphonecore.h"
#include "private.h"
#include <event.h>
#include "liblinphone_tester.h"


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

void linphone_notify_received(LinphoneCore *lc, LinphoneEvent *lev, const char *eventname, const LinphoneContent *content){
31 32
	CU_ASSERT_PTR_NOT_NULL_FATAL(content);
	CU_ASSERT_TRUE(strcmp(notify_content,(const char*)content->data)==0);
33 34 35
}

void linphone_subscription_state_change(LinphoneCore *lc, LinphoneEvent *lev, LinphoneSubscriptionState state) {
36 37
	stats* counters = get_stats(lc);
	LinphoneCoreManager *mgr=get_manager(lc);
38 39 40 41 42 43 44 45 46 47 48 49
	LinphoneContent content;
	
	content.type="application";
	content.subtype="somexml2";
	content.data=(void*)notify_content;
	content.size=strlen(notify_content);
	
	switch(state){
		case LinphoneSubscriptionNone:
		break;
		case LinphoneSubscriptionIncomingReceived:
			counters->number_of_LinphoneSubscriptionIncomingReceived++;
50 51 52 53
			if (!mgr->decline_subscribe)
				linphone_event_accept_subscription(lev);
			else
				linphone_event_deny_subscription(lev, LinphoneReasonDeclined);
54 55 56 57 58 59 60 61 62
		break;
		case LinphoneSubscriptionOutoingInit:
			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
63
			if (linphone_event_get_subscription_dir(lev)==LinphoneSubscriptionIncoming){
64
				mgr->lev=lev;
65
				linphone_event_notify(lev,&content);
66
			}
67 68 69
		break;
		case LinphoneSubscriptionTerminated:
			counters->number_of_LinphoneSubscriptionTerminated++;
70
			mgr->lev=NULL;
71 72 73
		break;
		case LinphoneSubscriptionError:
			counters->number_of_LinphoneSubscriptionError++;
74
			mgr->lev=NULL;
75 76 77 78
		break;
	}
}

79
static void subscribe_test_declined(void) {
Simon Morlat's avatar
Simon Morlat committed
80 81
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_rc");
82 83 84
	LinphoneContent content;
	MSList* lcs=ms_list_append(NULL,marie->lc);
	lcs=ms_list_append(lcs,pauline->lc);
85 86


87 88 89 90 91 92 93 94 95 96 97
	content.type="application";
	content.subtype="somexml";
	content.data=(char*)subscribe_content;
	content.size=strlen(subscribe_content);
	
	pauline->decline_subscribe=TRUE;
	
	linphone_core_subscribe(marie->lc,pauline->identity,"dodo",600,&content);
	
	CU_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionOutgoingInit,1,1000));
	CU_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionIncomingReceived,1,1000));
98
	CU_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionError,1,21000));/*yes flexisip may wait 20 secs in case of forking*/
99 100 101 102 103
	CU_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionTerminated,1,1000));
	
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}
104 105


106
static void subscribe_test_with_args(bool_t terminated_by_subscriber) {
Simon Morlat's avatar
Simon Morlat committed
107 108
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_rc");
109
	LinphoneContent content;
110
	LinphoneEvent *lev;
111 112 113
	MSList* lcs=ms_list_append(NULL,marie->lc);
	lcs=ms_list_append(lcs,pauline->lc);

114

115 116 117 118 119
	content.type="application";
	content.subtype="somexml";
	content.data=(char*)subscribe_content;
	content.size=strlen(subscribe_content);
	
120
	lev=linphone_core_subscribe(marie->lc,pauline->identity,"dodo",600,&content);
121 122 123 124 125 126
	
	CU_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionOutgoingInit,1,1000));
	CU_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionIncomingReceived,1,1000));
	CU_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionActive,1,1000));
	CU_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionActive,1,1000));

127 128 129
	if (terminated_by_subscriber){
		linphone_event_terminate(lev);
	}else{
130
		CU_ASSERT_PTR_NOT_NULL_FATAL(pauline->lev);
131 132 133 134 135
		linphone_event_terminate(pauline->lev);
	}
	
	CU_ASSERT_TRUE(wait_for_list(lcs,&marie->stat.number_of_LinphoneSubscriptionTerminated,1,1000));
	CU_ASSERT_TRUE(wait_for_list(lcs,&pauline->stat.number_of_LinphoneSubscriptionTerminated,1,1000));
136 137 138 139 140
	
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

141 142 143 144 145 146 147 148
static void subscribe_test_terminated_by_subscriber(void){
	subscribe_test_with_args(TRUE);
}

static void subscribe_test_terminated_by_notifier(void){
	subscribe_test_with_args(FALSE);
}

149
test_t subscribe_tests[] = {
150 151 152
	{ "Subscribe declined"	,	subscribe_test_declined 	},
	{ "Subscribe terminated by subscriber", subscribe_test_terminated_by_subscriber },
	{ "Subscribe terminated by notifier", subscribe_test_terminated_by_notifier }
153 154 155 156 157 158 159 160 161 162
};

test_suite_t subscribe_test_suite = {
	"Subscribe",
	NULL,
	NULL,
	sizeof(subscribe_tests) / sizeof(subscribe_tests[0]),
	subscribe_tests
};