presence_tester.c 6.94 KB
Newer Older
jehan's avatar
jehan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
	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/>.
*/
Ghislain MARY's avatar
Ghislain MARY committed
18

jehan's avatar
jehan committed
19 20 21 22 23 24
#include <stdio.h>
#include "CUnit/Basic.h"
#include "linphonecore.h"
#include "private.h"
#include "liblinphone_tester.h"

jehan's avatar
jehan committed
25 26 27 28 29 30 31 32 33
static LinphoneCoreManager* presence_linphone_core_manager_new(char* username) {
	LinphoneCoreManager* mgr= linphone_core_manager_new2(liblinphone_tester_file_prefix, "empty_rc", FALSE);
	char* identity_char;
	mgr->identity= linphone_core_get_primary_contact_parsed(mgr->lc);
	linphone_address_set_username(mgr->identity,username);
	identity_char=linphone_address_as_string(mgr->identity);
	linphone_core_set_primary_contact(mgr->lc,identity_char);
	return mgr;
}
jehan's avatar
jehan committed
34 35
void new_subscribtion_request(LinphoneCore *lc, LinphoneFriend *lf, const char *url){
	char* from=linphone_address_as_string(linphone_friend_get_address(lf));
36
	stats* counters;
jehan's avatar
jehan committed
37 38
	ms_message("New subscription request  from [%s]  url [%s]",from,url);
	ms_free(from);
39
	counters = (stats*)linphone_core_get_user_data(lc);
jehan's avatar
jehan committed
40 41 42
	counters->number_of_NewSubscriptionRequest++;
	linphone_core_add_friend(lc,lf); /*accept subscription*/
}
Ghislain MARY's avatar
Ghislain MARY committed
43

jehan's avatar
jehan committed
44
void notify_presence_received(LinphoneCore *lc, LinphoneFriend * lf) {
45
	stats* counters;
jehan's avatar
jehan committed
46 47 48
	char* from=linphone_address_as_string(linphone_friend_get_address(lf));
	ms_message("New Notify request  from [%s] ",from);
	ms_free(from);
49
	counters = (stats*)linphone_core_get_user_data(lc);
jehan's avatar
jehan committed
50
	counters->number_of_NotifyReceived++;
jehan's avatar
jehan committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

	switch(linphone_friend_get_status(lf)) {
		case LinphoneStatusOffline: counters->number_of_LinphoneStatusOffline++; break;
		case LinphoneStatusOnline: counters->number_of_LinphoneStatusOnline++; break;
		case LinphoneStatusBusy: counters->number_of_LinphoneStatusBusy++; break;
		case LinphoneStatusBeRightBack: counters->number_of_LinphoneStatusBeRightBack++; break;
		case LinphoneStatusAway: counters->number_of_LinphoneStatusAway++; break;
		case LinphoneStatusOnThePhone: counters->number_of_LinphoneStatusOnThePhone++; break;
		case LinphoneStatusOutToLunch: counters->number_of_LinphoneStatusOutToLunch++; break;
		case LinphoneStatusDoNotDisturb: counters->number_of_LinphoneStatusDoNotDisturb++; break;
		case LinphoneStatusMoved: counters->number_of_LinphoneStatusMoved++; break;
		case LinphoneStatusAltService: counters->number_of_LinphoneStatusMoved++; break;
		case LinphoneStatusPending: counters->number_of_LinphoneStatusPending++; break;
		case LinphoneStatusEnd: counters->number_of_LinphoneStatusEnd++; break;

	}
jehan's avatar
jehan committed
67 68
}

Ghislain MARY's avatar
Ghislain MARY committed
69
static void simple_publish(void) {
70
	LinphoneCoreManager* marie = linphone_core_manager_new(liblinphone_tester_file_prefix, "marie_rc");
jehan's avatar
jehan committed
71
	LinphoneProxyConfig* proxy;
jehan's avatar
jehan committed
72
	int i=0;
jehan's avatar
jehan committed
73 74 75 76
	linphone_core_get_default_proxy(marie->lc,&proxy);
	linphone_proxy_config_edit(proxy);
	linphone_proxy_config_enable_publish(proxy,TRUE);
	linphone_proxy_config_done(proxy);
jehan's avatar
jehan committed
77 78 79 80
	for (i=0;i<10;i++) {
		linphone_core_iterate(marie->lc);
		ms_usleep(100000);
	}
jehan's avatar
jehan committed
81 82 83 84 85
	linphone_core_set_presence_info(marie->lc,0,NULL,LinphoneStatusOffline);
	for (i=0;i<10;i++) {
		linphone_core_iterate(marie->lc);
		ms_usleep(100000);
	}
jehan's avatar
jehan committed
86 87 88
	linphone_core_manager_destroy(marie);
}

jehan's avatar
jehan committed
89 90 91 92
static bool_t subscribe_to_callee_presence(LinphoneCoreManager* caller_mgr,LinphoneCoreManager* callee_mgr) {
	stats initial_caller=caller_mgr->stat;
	stats initial_callee=callee_mgr->stat;
	bool_t result=FALSE;
jehan's avatar
jehan committed
93
	char* identity=linphone_address_as_string_uri_only(callee_mgr->identity);
jehan's avatar
jehan committed
94 95


jehan's avatar
jehan committed
96
	LinphoneFriend* friend=linphone_friend_new_with_addr(identity);
jehan's avatar
jehan committed
97 98 99 100 101 102 103 104 105 106 107 108
	linphone_friend_edit(friend);
	linphone_friend_enable_subscribes(friend,TRUE);
	linphone_friend_done(friend);

	linphone_core_add_friend(caller_mgr->lc,friend);

	result=wait_for(caller_mgr->lc,callee_mgr->lc,&callee_mgr->stat.number_of_LinphoneStatusOnline,initial_callee.number_of_LinphoneStatusOnline+1);
	result&=wait_for(caller_mgr->lc,callee_mgr->lc,&caller_mgr->stat.number_of_LinphoneStatusOnline,initial_caller.number_of_LinphoneStatusOnline+1);

	CU_ASSERT_EQUAL(callee_mgr->stat.number_of_NewSubscriptionRequest,initial_callee.number_of_NewSubscriptionRequest+1);
	CU_ASSERT_EQUAL(callee_mgr->stat.number_of_NotifyReceived,initial_callee.number_of_NotifyReceived+1);
	CU_ASSERT_EQUAL(caller_mgr->stat.number_of_NotifyReceived,initial_caller.number_of_NotifyReceived+1);
jehan's avatar
jehan committed
109
	ms_free(identity);
jehan's avatar
jehan committed
110 111 112
	return result;

}
Ghislain MARY's avatar
Ghislain MARY committed
113
static void simple_subscribe(void) {
jehan's avatar
jehan committed
114 115
	LinphoneCoreManager* marie = presence_linphone_core_manager_new("marie");
	LinphoneCoreManager* pauline = presence_linphone_core_manager_new("pauline");
jehan's avatar
jehan committed
116

jehan's avatar
jehan committed
117
	CU_ASSERT_TRUE(subscribe_to_callee_presence(marie,pauline));
jehan's avatar
jehan committed
118 119 120 121 122 123

	linphone_core_manager_destroy(marie);
	CU_ASSERT_FALSE(wait_for(NULL,pauline->lc,&pauline->stat.number_of_NewSubscriptionRequest,2)); /*just to wait for unsubscription even if not notified*/

	linphone_core_manager_destroy(pauline);
}
Ghislain MARY's avatar
Ghislain MARY committed
124 125

static void unsubscribe_while_subscribing(void) {
126
	LinphoneCoreManager* marie = linphone_core_manager_new(liblinphone_tester_file_prefix, "marie_rc");
jehan's avatar
jehan committed
127 128 129 130 131 132 133 134 135
	LinphoneFriend* friend = linphone_friend_new_with_addr("sip:toto@git.linphone.org"); /*any unexisting address*/
	linphone_friend_edit(friend);
	linphone_friend_enable_subscribes(friend,TRUE);
	linphone_friend_done(friend);
	linphone_core_add_friend(marie->lc,friend);
	linphone_core_iterate(marie->lc);
	linphone_core_manager_destroy(marie);
}

jehan's avatar
jehan committed
136
static void call_with_presence(void) {
jehan's avatar
jehan committed
137 138
	LinphoneCoreManager* marie = presence_linphone_core_manager_new("marie");
	LinphoneCoreManager* pauline = presence_linphone_core_manager_new("pauline");
jehan's avatar
jehan committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152
	CU_ASSERT_TRUE(subscribe_to_callee_presence(marie,pauline));

	CU_ASSERT_TRUE(call(marie,pauline));
	CU_ASSERT_EQUAL(marie->stat.number_of_LinphoneStatusOnThePhone,1);
	CU_ASSERT_EQUAL(pauline->stat.number_of_LinphoneStatusOnThePhone,1);

	reset_counters(&marie->stat);
	reset_counters(&pauline->stat);
	linphone_core_terminate_all_calls(marie->lc);
	CU_ASSERT_TRUE(wait_for(marie->lc,pauline->lc,&pauline->stat.number_of_LinphoneStatusOnline,1));
	CU_ASSERT_TRUE(wait_for(marie->lc,pauline->lc,&marie->stat.number_of_LinphoneStatusOnline,1));
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}
Ghislain MARY's avatar
Ghislain MARY committed
153 154 155 156

test_t presence_tests[] = {
	{ "Simple Subscribe", simple_subscribe },
	{ "Simple Publish", simple_publish },
jehan's avatar
jehan committed
157
	{ "Call with Presence", call_with_presence },
Ghislain MARY's avatar
Ghislain MARY committed
158 159 160 161 162 163 164 165 166 167 168
	{ "Unsubscribe while subscribing", unsubscribe_while_subscribing },
};

test_suite_t presence_test_suite = {
	"Presence",
	NULL,
	NULL,
	sizeof(presence_tests) / sizeof(presence_tests[0]),
	presence_tests
};