message_tester.c 110 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

20

21
#include "linphone/core.h"
jehan's avatar
jehan committed
22 23
#include "private.h"
#include "liblinphone_tester.h"
24
#include "lime.h"
jehan's avatar
jehan committed
25

26
#ifdef SQLITE_STORAGE_ENABLED
27 28 29
#include <sqlite3.h>
#endif

30 31 32
#if __clang__ || ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
#pragma GCC diagnostic push
#endif
33 34 35
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#endif
36

37

Simon Morlat's avatar
Simon Morlat committed
38
static char* message_external_body_url=NULL;
Ghislain MARY's avatar
Ghislain MARY committed
39

40 41 42
static const char *marie_zid_cache = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<cache><selfZID>ef7692d0792a67491ae2d44e</selfZID><peer><ZID>005dbe0399643d953a2202dd</ZID><rs1>9b5c8f06f3b6c2c695f2dfc3c26f31f5fef8661f8c5fe7c95aeb5c5b0435b045</rs1><aux>f8324dd18ea905171ec2be89f879d01d5994132048d92ea020778cbdf31c605e</aux><rs2>2fdcef69380937c2cf221f7d11526f286c39f49641452ba9012521c705094899</rs2><uri>%s</uri><sndKey>08df5907d30959b8cb70f6fff2d8febd88fb41b0c8afc39e4b972f86dd5cfe2d</sndKey><rcvKey>60f020a3fe11dc2cc0e1e8ed9341b4cd14944db806ca4fc95456bbe45d95c43a</rcvKey><sndSId>5f9aa1e5e4c7ec88fa389a9f6b8879b42d3c57bb28e62068d2df23e8f9b77193</sndSId><rcvSId>bcffd51e7316a6c6f53a50fcf01b01bf2d3c57bb28e62068d2df23e8f9b77193</rcvSId><sndIndex>00000078</sndIndex><rcvIndex>000001cf</rcvIndex><pvs>01</pvs></peer><peer><ZID>1234567889643d953a2202ee</ZID><rs1>9b5c8f06f3b6c2c695f2dfc3c26f31f5fef8661f8c5fe7c95aeb5c5b0435b045</rs1><aux>f8324dd18ea905171ec2be89f879d01d5994132048d92ea020778cbdf31c605e</aux><rs2>2fdcef69380937c2cf221f7d11526f286c39f49641452ba9012521c705094899</rs2><uri>%s</uri><sndKey>72d80ab1cad243cf45634980c1d02cfb2df81ce0dd5dfcf1ebeacfc5345a9176</sndKey><rcvKey>25d9ac653a83c4559cb0ae7394e7cd3b2d3c57bb28e62068d2df23e8f9b77193</rcvKey><sndSId>f69aa1e5e4c7ec88fa389a9f6b8879b42d3c57bb28e62068d2df23e8f9b77193</sndSId><rcvSId>22ffd51e7316a6c6f53a50fcf01b01bf2d3c57bb28e62068d2df23e8f9b77193</rcvSId><sndIndex>0000000f</sndIndex><rcvIndex>00000000</rcvIndex></peer></cache>";
static const char *pauline_zid_cache = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<cache><selfZID>005dbe0399643d953a2202dd</selfZID><peer><ZID>ef7692d0792a67491ae2d44e</ZID><rs1>9b5c8f06f3b6c2c695f2dfc3c26f31f5fef8661f8c5fe7c95aeb5c5b0435b045</rs1><aux>f8324dd18ea905171ec2be89f879d01d5994132048d92ea020778cbdf31c605e</aux><rs2>2fdcef69380937c2cf221f7d11526f286c39f49641452ba9012521c705094899</rs2><uri>%s</uri><rcvKey>08df5907d30959b8cb70f6fff2d8febd88fb41b0c8afc39e4b972f86dd5cfe2d</rcvKey><sndKey>60f020a3fe11dc2cc0e1e8ed9341b4cd14944db806ca4fc95456bbe45d95c43a</sndKey><rcvSId>5f9aa1e5e4c7ec88fa389a9f6b8879b42d3c57bb28e62068d2df23e8f9b77193</rcvSId><sndSId>bcffd51e7316a6c6f53a50fcf01b01bf2d3c57bb28e62068d2df23e8f9b77193</sndSId><rcvIndex>00000078</rcvIndex><sndIndex>000001cf</sndIndex><pvs>01</pvs></peer><peer><ZID>1234567889643d953a2202ee</ZID><rs1>9b5c8f06f3b6c2c695f2dfc3c26f31f5fef8661f8c5fe7c95aeb5c5b0435b045</rs1><aux>f8324dd18ea905171ec2be89f879d01d5994132048d92ea020778cbdf31c605e</aux><rs2>2fdcef69380937c2cf221f7d11526f286c39f49641452ba9012521c705094899</rs2><uri>%s</uri><sndKey>81e6e6362c34dc974263d1f77cbb9a8d6d6a718330994379099a8fa19fb12faa</sndKey><rcvKey>25d9ac653a83c4559cb0ae7394e7cd3b2d3c57bb28e62068d2df23e8f9b77193</rcvKey><sndSId>f69aa1e5e4c7ec88fa389a9f6b8879b42d3c57bb28e62068d2df23e8f9b77193</sndSId><rcvSId>22ffd51e7316a6c6f53a50fcf01b01bf2d3c57bb28e62068d2df23e8f9b77193</rcvSId><sndIndex>0000002e</sndIndex><rcvIndex>00000000</rcvIndex><pvs>01</pvs></peer></cache>";

43
void text_message_received(LinphoneCore *lc, LinphoneChatRoom *room, const LinphoneAddress *from_address, const char *msg) {
44
	stats* counters = get_stats(lc);
45 46
	counters->number_of_LinphoneMessageReceivedLegacy++;
}
Ghislain MARY's avatar
Ghislain MARY committed
47

48 49
void message_received(LinphoneCore *lc, LinphoneChatRoom *room, LinphoneChatMessage* msg) {
	char* from=linphone_address_as_string(linphone_chat_message_get_from(msg));
50
	stats* counters;
51 52
	const char *text=linphone_chat_message_get_text(msg);
	const char *external_body_url=linphone_chat_message_get_external_body_url(msg);
Ghislain MARY's avatar
Ghislain MARY committed
53 54 55
	ms_message("Message from [%s]  is [%s] , external URL [%s]",from?from:""
																,text?text:""
																,external_body_url?external_body_url:"");
jehan's avatar
jehan committed
56
	ms_free(from);
57
	counters = get_stats(lc);
jehan's avatar
jehan committed
58
	counters->number_of_LinphoneMessageReceived++;
jehan's avatar
jehan committed
59
	if (counters->last_received_chat_message) linphone_chat_message_unref(counters->last_received_chat_message);
60 61
	counters->last_received_chat_message=linphone_chat_message_ref(msg);
	if (linphone_chat_message_get_file_transfer_information(msg)) {
62
		counters->number_of_LinphoneMessageReceivedWithFile++;
63
	} else if (linphone_chat_message_get_external_body_url(msg)) {
64
		counters->number_of_LinphoneMessageExtBodyReceived++;
jehan's avatar
jehan committed
65
		if (message_external_body_url) {
66
			BC_ASSERT_STRING_EQUAL(linphone_chat_message_get_external_body_url(msg),message_external_body_url);
jehan's avatar
jehan committed
67 68
			message_external_body_url=NULL;
		}
69
	}
jehan's avatar
jehan committed
70 71
}

72 73 74
/**
 * function invoked when a file transfer is received.
 * */
75
void file_transfer_received(LinphoneChatMessage *msg, const LinphoneContent* content, const LinphoneBuffer *buffer){
76
	FILE* file=NULL;
77
	char *receive_file = NULL;
78 79
	LinphoneChatRoom *cr = linphone_chat_message_get_chat_room(msg);
	LinphoneCore *lc = linphone_chat_room_get_core(cr);
80 81
	
	if (linphone_chat_message_get_file_transfer_filepath(msg) != NULL) {
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
		if (linphone_buffer_is_empty(buffer)) {
			stats* counters = get_stats(lc);
			counters->number_of_LinphoneFileTransferDownloadSuccessful++;
			return;
		}
	} else {
		receive_file = bc_tester_file("receive_file.dump");
		if (!linphone_chat_message_get_user_data(msg)) {
			/*first chunk, creating file*/
			file = fopen(receive_file,"wb");
			linphone_chat_message_set_user_data(msg,(void*)file); /*store fd for next chunks*/
		}
		bc_free(receive_file);
		file = (FILE*)linphone_chat_message_get_user_data(msg);
		BC_ASSERT_PTR_NOT_NULL(file);
		if (linphone_buffer_is_empty(buffer)) { /* tranfer complete */
			stats* counters = get_stats(lc);
			counters->number_of_LinphoneFileTransferDownloadSuccessful++;
			linphone_chat_message_set_user_data(msg, NULL);
			fclose(file);
		} else { /* store content on a file*/
			if (fwrite(linphone_buffer_get_content(buffer),linphone_buffer_get_size(buffer),1,file)==0){
				ms_error("file_transfer_received(): write() failed: %s",strerror(errno));
			}
106
		}
107 108 109 110 111 112
	}
}

/*
 * function called when the file transfer is initiated. file content should be feed into object LinphoneContent
 * */
113
LinphoneBuffer * tester_file_transfer_send(LinphoneChatMessage *msg, const LinphoneContent* content, size_t offset, size_t size){
114 115 116 117
	LinphoneBuffer *lb;
	size_t file_size;
	size_t size_to_send;
	uint8_t *buf;
118
	FILE *file_to_send = linphone_chat_message_get_user_data(msg);
119

Simon Morlat's avatar
Simon Morlat committed
120 121 122 123
	BC_ASSERT_PTR_NOT_NULL(file_to_send);
	if (file_to_send == NULL){
		return NULL;
	}
124 125
	fseek(file_to_send, 0, SEEK_END);
	file_size = ftell(file_to_send);
126
	fseek(file_to_send, (long)offset, SEEK_SET);
127 128
	size_to_send = MIN(size, file_size - offset);
	buf = ms_malloc(size_to_send);
Simon Morlat's avatar
Simon Morlat committed
129
	if (fread(buf, size_to_send, 1, file_to_send)!=size_to_send){
130 131
		// reaching end of file, close it
		fclose(file_to_send);
Simon Morlat's avatar
Simon Morlat committed
132
		linphone_chat_message_set_user_data(msg, NULL);
Simon Morlat's avatar
Simon Morlat committed
133
	}
134 135 136 137 138
	lb = linphone_buffer_new_from_data(buf, size_to_send);
	ms_free(buf);
	return lb;
}

139 140 141
/**
 * function invoked to report file transfer progress.
 * */
142 143 144
void file_transfer_progress_indication(LinphoneChatMessage *msg, const LinphoneContent* content, size_t offset, size_t total) {
	LinphoneChatRoom *cr = linphone_chat_message_get_chat_room(msg);
	LinphoneCore *lc = linphone_chat_room_get_core(cr);
145 146 147
	const LinphoneAddress* from_address = linphone_chat_message_get_from(msg);
	const LinphoneAddress* to_address = linphone_chat_message_get_to(msg);
	char *address = linphone_chat_message_is_outgoing(msg)?linphone_address_as_string(to_address):linphone_address_as_string(from_address);
148
	stats* counters = get_stats(lc);
François Grisez's avatar
François Grisez committed
149 150
	int progress = (int)((offset * 100)/total);
	ms_message(" File transfer  [%d%%] %s of type [%s/%s] %s [%s] \n", progress
151
																	,(linphone_chat_message_is_outgoing(msg)?"sent":"received")
152 153
																	, linphone_content_get_type(content)
																	, linphone_content_get_subtype(content)
154
																	,(linphone_chat_message_is_outgoing(msg)?"to":"from")
155
																	, address);
johan's avatar
johan committed
156
	counters->progress_of_LinphoneFileTransfer = progress;
157 158 159
	free(address);
}

160 161 162 163 164 165 166 167 168
void is_composing_received(LinphoneCore *lc, LinphoneChatRoom *room) {
	stats *counters = get_stats(lc);
	if (room->remote_is_composing == LinphoneIsComposingActive) {
		counters->number_of_LinphoneIsComposingActiveReceived++;
	} else {
		counters->number_of_LinphoneIsComposingIdleReceived++;
	}
}

Simon Morlat's avatar
Simon Morlat committed
169
void liblinphone_tester_chat_message_state_change(LinphoneChatMessage* msg,LinphoneChatMessageState state,void* ud) {
170
	liblinphone_tester_chat_message_msg_state_changed(msg, state);
171 172
}

173
void liblinphone_tester_chat_message_msg_state_changed(LinphoneChatMessage *msg, LinphoneChatMessageState state) {
174 175
	LinphoneChatRoom *cr = linphone_chat_message_get_chat_room(msg);
	LinphoneCore *lc = linphone_chat_room_get_core(cr);
176
	stats* counters = get_stats(lc);
177
	switch (state) {
Simon Morlat's avatar
Simon Morlat committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
		case LinphoneChatMessageStateIdle:
			return;
		case LinphoneChatMessageStateDelivered:
			counters->number_of_LinphoneMessageDelivered++;
			return;
		case LinphoneChatMessageStateNotDelivered:
			counters->number_of_LinphoneMessageNotDelivered++;
			return;
		case LinphoneChatMessageStateInProgress:
			counters->number_of_LinphoneMessageInProgress++;
			return;
		case LinphoneChatMessageStateFileTransferError:
			counters->number_of_LinphoneMessageNotDelivered++;
			return;
		case LinphoneChatMessageStateFileTransferDone:
			counters->number_of_LinphoneMessageFileTransferDone++;
			return;
Ghislain MARY's avatar
Ghislain MARY committed
195 196 197 198 199 200
		case LinphoneChatMessageStateDeliveredToUser:
			counters->number_of_LinphoneMessageDeliveredToUser++;
			return;
		case LinphoneChatMessageStateDisplayed:
			counters->number_of_LinphoneMessageDisplayed++;
			return;
201
	}
202
	ms_error("Unexpected state [%s] for msg [%p]",linphone_chat_message_state_to_string(state), msg);
203
}
jehan's avatar
jehan committed
204

205 206 207 208 209
void compare_files(const char *path1, const char *path2) {
	size_t size1;
	size_t size2;
	uint8_t *buf1;
	uint8_t *buf2;
jehan's avatar
jehan committed
210

211 212 213 214
	buf1 = (uint8_t*)ms_load_path_content(path1, &size1);
	buf2 = (uint8_t*)ms_load_path_content(path2, &size2);
	BC_ASSERT_PTR_NOT_NULL(buf1);
	BC_ASSERT_PTR_NOT_NULL(buf2);
215
	BC_ASSERT_EQUAL((uint8_t)size2, (uint8_t)size1, uint8_t, "%u");
216 217 218 219 220 221 222
	BC_ASSERT_EQUAL(memcmp(buf1, buf2, size1), 0, int, "%d");
	ms_free(buf1);
	ms_free(buf2);
}

LinphoneChatMessage* create_message_from_nowebcam(LinphoneChatRoom *chat_room) {
	FILE *file_to_send = NULL;
223
	LinphoneChatMessageCbs *cbs;
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
	LinphoneContent* content;
	LinphoneChatMessage* msg;
	size_t file_size;
	char *send_filepath = bc_tester_res("images/nowebcamCIF.jpg");
	file_to_send = fopen(send_filepath, "rb");
	fseek(file_to_send, 0, SEEK_END);
	file_size = ftell(file_to_send);
	fseek(file_to_send, 0, SEEK_SET);

	content = linphone_core_create_content(chat_room->lc);
	belle_sip_object_set_name(&content->base, "nowebcam content");
	linphone_content_set_type(content,"image");
	linphone_content_set_subtype(content,"jpeg");
	linphone_content_set_size(content,file_size); /*total size to be transfered*/
	linphone_content_set_name(content,"nowebcamCIF.jpg");

	msg = linphone_chat_room_create_file_transfer_message(chat_room, content);
241 242 243 244
	cbs = linphone_chat_message_get_callbacks(msg);
	linphone_chat_message_cbs_set_file_transfer_send(cbs, tester_file_transfer_send);
	linphone_chat_message_cbs_set_msg_state_changed(cbs,liblinphone_tester_chat_message_msg_state_changed);
	linphone_chat_message_cbs_set_file_transfer_progress_indication(cbs, file_transfer_progress_indication);
245 246 247 248 249 250 251
	linphone_chat_message_set_user_data(msg, file_to_send);

	linphone_content_unref(content);
	ms_free(send_filepath);
	return msg;
}

252
LinphoneChatMessage* create_file_transfer_message_from_nowebcam(LinphoneChatRoom *chat_room) {
253
	LinphoneChatMessageCbs *cbs;
254 255 256 257 258 259 260 261 262 263 264 265
	LinphoneContent* content;
	LinphoneChatMessage* msg;
	char *send_filepath = bc_tester_res("images/nowebcamCIF.jpg");

	content = linphone_core_create_content(chat_room->lc);
	belle_sip_object_set_name(&content->base, "nowebcam content");
	linphone_content_set_type(content,"image");
	linphone_content_set_subtype(content,"jpeg");
	linphone_content_set_name(content,"nowebcamCIF.jpg");

	msg = linphone_chat_room_create_file_transfer_message(chat_room, content);
	linphone_chat_message_set_file_transfer_filepath(msg, send_filepath);
266 267 268
	cbs = linphone_chat_message_get_callbacks(msg);
	linphone_chat_message_cbs_set_msg_state_changed(cbs,liblinphone_tester_chat_message_msg_state_changed);
	linphone_chat_message_cbs_set_file_transfer_progress_indication(cbs, file_transfer_progress_indication);
269 270 271 272 273 274

	linphone_content_unref(content);
	ms_free(send_filepath);
	return msg;
}

275
void text_message_base(LinphoneCoreManager* marie, LinphoneCoreManager* pauline) {
276 277 278
	LinphoneChatMessage* msg = linphone_chat_room_create_message(linphone_core_get_chat_room(pauline->lc,marie->identity),"Bli bli bli \n blu");
	LinphoneChatMessageCbs *cbs = linphone_chat_message_get_callbacks(msg);
	linphone_chat_message_cbs_set_msg_state_changed(cbs, liblinphone_tester_chat_message_msg_state_changed);
279 280 281
	linphone_chat_room_send_chat_message(msg->chat_room,msg);

	BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&pauline->stat.number_of_LinphoneMessageDelivered,1));
282
	BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneMessageReceived,1));
jehan's avatar
jehan committed
283

284
	BC_ASSERT_PTR_NOT_NULL(linphone_core_get_chat_room(marie->lc,pauline->identity));
285 286 287 288 289 290 291 292 293 294
}

/****************************** Tests starting below ******************************/

static void text_message(void) {
	LinphoneCoreManager* marie = linphone_core_manager_new("marie_rc");
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");

	linphone_chat_room_send_message(linphone_core_get_chat_room(pauline->lc,marie->identity), "hello");
	BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneMessageReceivedLegacy,1));
jehan's avatar
jehan committed
295 296 297 298 299

	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

300
static void text_message_within_call_dialog(void) {
301
	LinphoneCoreManager* marie = linphone_core_manager_new("marie_rc");
302
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
303 304
	lp_config_set_int(pauline->lc->config,"sip","chat_use_call_dialogs",1);

305
	BC_ASSERT_TRUE(call(marie,pauline));
306
	linphone_chat_room_send_message(linphone_core_get_chat_room(pauline->lc, marie->identity),"Bla bla bla bla");
307

308
	BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneMessageReceived,1));
309 310
	// when using call dialogs, we will never receive delivered status
	BC_ASSERT_EQUAL(pauline->stat.number_of_LinphoneMessageDelivered,0,int,"%d");
311

Simon Morlat's avatar
Simon Morlat committed
312
	end_call(marie, pauline);
313 314 315 316
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

317
static LinphoneAuthInfo* text_message_with_credential_from_auth_cb_auth_info;
318
static void text_message_with_credential_from_auth_cb_auth_info_requested(LinphoneCore *lc, const char *realm, const char *username, const char *domain) {
319
	ms_message("text_message_with_credential_from_auth_callback:Auth info requested  for user id [%s] at realm [%s]\n"
320 321 322 323
						,username
						,realm);
	linphone_core_add_auth_info(lc,text_message_with_credential_from_auth_cb_auth_info); /*add stored authentication info to LinphoneCore*/
}
324
static void text_message_with_credential_from_auth_callback(void) {
325
	LinphoneCoreManager* marie = linphone_core_manager_new("marie_rc");
326
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
327
	LinphoneCoreVTable* vtable = linphone_core_v_table_new();
328 329

	/*to force cb to be called*/
330
	text_message_with_credential_from_auth_cb_auth_info=linphone_auth_info_clone((LinphoneAuthInfo*)(linphone_core_get_auth_info_list(pauline->lc)->data));
jehan's avatar
jehan committed
331
	linphone_core_clear_all_auth_info(pauline->lc);
332
	vtable->auth_info_requested=text_message_with_credential_from_auth_cb_auth_info_requested;
jehan's avatar
jehan committed
333
	linphone_core_add_listener(pauline->lc, vtable);
334

335
	text_message_base(marie, pauline);
336 337 338 339 340

	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

jehan's avatar
jehan committed
341
static void text_message_with_privacy(void) {
Simon Morlat's avatar
Simon Morlat committed
342
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
343
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
344
	linphone_proxy_config_set_privacy(linphone_core_get_default_proxy_config(pauline->lc),LinphonePrivacyId);
345

346
	text_message_base(marie, pauline);
347
	BC_ASSERT_EQUAL(marie->stat.number_of_LinphoneMessageReceivedLegacy,1, int, "%d");
jehan's avatar
jehan committed
348

349 350 351
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}
jehan's avatar
jehan committed
352

353
static void text_message_compatibility_mode(void) {
Simon Morlat's avatar
Simon Morlat committed
354
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
355
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
356 357 358
	LinphoneProxyConfig* proxy = linphone_core_get_default_proxy_config(marie->lc);
	LinphoneAddress* proxy_address=linphone_address_new(linphone_proxy_config_get_server_addr(proxy));
	char route[256];
359
	char*tmp;
360 361
	/*only keep tcp*/
	LCSipTransports transport = {0,-1,0,0};
362 363 364
	linphone_address_clean(proxy_address);
	tmp=linphone_address_as_string_uri_only(proxy_address);
	linphone_proxy_config_set_server_addr(proxy,tmp);
365 366
	sprintf(route,"sip:%s",test_route);
	linphone_proxy_config_set_route(proxy,route);
367 368 369 370
	ms_free(tmp);
	linphone_address_destroy(proxy_address);
	linphone_core_set_sip_transports(marie->lc,&transport);
	marie->stat.number_of_LinphoneRegistrationOk=0;
371
	BC_ASSERT_TRUE (wait_for(marie->lc,marie->lc,&marie->stat.number_of_LinphoneRegistrationOk,1));
372

373 374
	text_message_base(marie, pauline);

375 376 377
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}
378

Ghislain MARY's avatar
Ghislain MARY committed
379
static void text_message_with_ack(void) {
380 381
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
382 383 384 385 386 387 388 389 390 391 392

	text_message_base(marie, pauline);

	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

static void text_message_with_send_error(void) {
	LinphoneCoreManager* marie = linphone_core_manager_new("marie_rc");
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
	LinphoneChatRoom* chat_room = linphone_core_get_chat_room(marie->lc, pauline->identity);
393
	LinphoneChatMessage* msg = linphone_chat_room_create_message(chat_room,"Bli bli bli \n blu");
394
	LinphoneChatMessageCbs *cbs = linphone_chat_message_get_callbacks(msg);
395 396 397

	/*simulate a network error*/
	sal_set_send_error(marie->lc->sal, -1);
398
	linphone_chat_message_cbs_set_msg_state_changed(cbs,liblinphone_tester_chat_message_msg_state_changed);
399
	linphone_chat_room_send_chat_message(chat_room,msg);
400 401

	/* check transient msg list: the msg should be in it, and should be the only one */
Ghislain MARY's avatar
Ghislain MARY committed
402
	BC_ASSERT_EQUAL((unsigned int)bctbx_list_size(chat_room->transient_messages), 1, unsigned int, "%u");
403
	BC_ASSERT_PTR_EQUAL(bctbx_list_nth_data(chat_room->transient_messages,0), msg);
404 405 406 407 408 409

	BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneMessageNotDelivered,1));
	/*BC_ASSERT_EQUAL(marie->stat.number_of_LinphoneMessageInProgress,1, int, "%d");*/
	BC_ASSERT_EQUAL(pauline->stat.number_of_LinphoneMessageReceived,0, int, "%d");

	/* the msg should have been discarded from transient list after an error */
Ghislain MARY's avatar
Ghislain MARY committed
410
	BC_ASSERT_EQUAL((unsigned int)bctbx_list_size(chat_room->transient_messages), 0, unsigned int, "%u");
411 412 413 414 415 416 417

	sal_set_send_error(marie->lc->sal, 0);

	/*give a chance to register again to allow linphone_core_manager_destroy to properly unregister*/
	linphone_core_refresh_registers(marie->lc);
	BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneRegistrationOk,marie->stat.number_of_LinphoneRegistrationOk + 1));

418 419
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
420
}
Ghislain MARY's avatar
Ghislain MARY committed
421 422

static void text_message_with_external_body(void) {
Simon Morlat's avatar
Simon Morlat committed
423
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
424
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
Simon Morlat's avatar
Simon Morlat committed
425
	LinphoneChatRoom* chat_room = linphone_core_get_chat_room(pauline->lc, marie->identity);
426
	LinphoneChatMessage* msg = linphone_chat_room_create_message(chat_room,"Bli bli bli \n blu");
427
	LinphoneChatMessageCbs *cbs = linphone_chat_message_get_callbacks(msg);
Simon Morlat's avatar
Simon Morlat committed
428

429 430
	message_external_body_url="http://www.linphone.org";
	linphone_chat_message_set_external_body_url(msg,message_external_body_url);
431
	linphone_chat_message_cbs_set_msg_state_changed(cbs,liblinphone_tester_chat_message_msg_state_changed);
432
	linphone_chat_room_send_chat_message(chat_room,msg);
433

434
	/* check transient msg list: the msg should be in it, and should be the only one */
Ghislain MARY's avatar
Ghislain MARY committed
435
	BC_ASSERT_EQUAL((unsigned int)bctbx_list_size(chat_room->transient_messages), 1, unsigned int, "%u");
436
	BC_ASSERT_PTR_EQUAL(bctbx_list_nth_data(chat_room->transient_messages,0), msg);
437

438 439
	BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneMessageReceived,1));
	BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&pauline->stat.number_of_LinphoneMessageDelivered,1));
440

441 442
	BC_ASSERT_EQUAL(pauline->stat.number_of_LinphoneMessageInProgress,1, int, "%d");
	BC_ASSERT_EQUAL(marie->stat.number_of_LinphoneMessageExtBodyReceived,1, int, "%d");
443

Ghislain MARY's avatar
Ghislain MARY committed
444
	BC_ASSERT_EQUAL((unsigned int)bctbx_list_size(chat_room->transient_messages), 0, unsigned int, "%u");
445

jehan's avatar
jehan committed
446 447 448 449
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

Sylvain Berfini's avatar
Sylvain Berfini committed
450 451
void transfer_message_base2(LinphoneCoreManager* marie, LinphoneCoreManager* pauline, bool_t upload_error, bool_t download_error, 
							bool_t use_file_body_handler_in_upload, bool_t use_file_body_handler_in_download, bool_t download_from_history) {
452 453 454 455
	char *send_filepath = bc_tester_res("images/nowebcamCIF.jpg");
	char *receive_filepath = bc_tester_file("receive_file.dump");
	LinphoneChatRoom* chat_room;
	LinphoneChatMessage* msg;
456
	LinphoneChatMessageCbs *cbs;
Ghislain MARY's avatar
Ghislain MARY committed
457
	bctbx_list_t *msg_list = NULL;
458

459 460 461
	/* Remove any previously downloaded file */
	remove(receive_filepath);
	
462 463
	/* Globally configure an http file transfer server. */
	linphone_core_set_file_transfer_server(pauline->lc,"https://www.linphone.org:444/lft.php");
464

465 466
	/* create a chatroom on pauline's side */
	chat_room = linphone_core_get_chat_room(pauline->lc, marie->identity);
Ghislain MARY's avatar
Ghislain MARY committed
467

468
	/* create a file transfer msg */
469
	if (use_file_body_handler_in_upload) {
470 471 472 473 474
		msg = create_file_transfer_message_from_nowebcam(chat_room);
	} else {
		msg = create_message_from_nowebcam(chat_room);
	}
	
475
	linphone_chat_room_send_chat_message(chat_room,msg);
476

477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
	if (upload_error) {
		/*wait for file to be 25% uploaded and simulate a network error*/
		BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&pauline->stat.progress_of_LinphoneFileTransfer,25));
		sal_set_send_error(pauline->lc->sal, -1);

		BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&pauline->stat.number_of_LinphoneMessageNotDelivered,1));

		BC_ASSERT_EQUAL(pauline->stat.number_of_LinphoneMessageNotDelivered,1, int, "%d");
		BC_ASSERT_EQUAL(marie->stat.number_of_LinphoneFileTransferDownloadSuccessful,0, int, "%d");

		sal_set_send_error(pauline->lc->sal, 0);

		linphone_core_refresh_registers(pauline->lc); /*to make sure registration is back in registered and so it can be later unregistered*/
		BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&pauline->stat.number_of_LinphoneRegistrationOk,pauline->stat.number_of_LinphoneRegistrationOk+1));
	} else {
		BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneMessageReceivedWithFile,1));
Ghislain MARY's avatar
Ghislain MARY committed
493 494 495 496 497 498 499 500 501 502 503 504
		if (marie->stat.last_received_chat_message) {
			LinphoneChatMessage *recv_msg;
			if (download_from_history) {
				LinphoneChatRoom *marie_room = linphone_core_get_chat_room(marie->lc, pauline->identity);
				msg_list = linphone_chat_room_get_history(marie_room,1);
				BC_ASSERT_PTR_NOT_NULL(msg_list);
				if (!msg_list)  goto end;
				recv_msg = (LinphoneChatMessage *)msg_list->data;
			} else {
				recv_msg = marie->stat.last_received_chat_message;
			}
			cbs = linphone_chat_message_get_callbacks(recv_msg);
505 506 507
			linphone_chat_message_cbs_set_msg_state_changed(cbs, liblinphone_tester_chat_message_msg_state_changed);
			linphone_chat_message_cbs_set_file_transfer_recv(cbs, file_transfer_received);
			linphone_chat_message_cbs_set_file_transfer_progress_indication(cbs, file_transfer_progress_indication);
508
			if (use_file_body_handler_in_download) {
Sylvain Berfini's avatar
Sylvain Berfini committed
509
				linphone_chat_message_set_file_transfer_filepath(recv_msg, receive_filepath);
510
			}
Sylvain Berfini's avatar
Sylvain Berfini committed
511
			linphone_chat_message_download_file(recv_msg);
512 513 514 515 516 517 518 519 520

			if (download_error) {
				/* wait for file to be 50% downloaded */
				BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.progress_of_LinphoneFileTransfer, 50));
				/* and simulate network error */
				belle_http_provider_set_recv_error(marie->lc->http_provider, -1);
				BC_ASSERT_TRUE(wait_for_until(marie->lc, pauline->lc, &marie->stat.number_of_LinphoneMessageNotDelivered,1, 10000));
				belle_http_provider_set_recv_error(marie->lc->http_provider, 0);
			} else {
521 522 523 524
				/* wait for a long time in case the DNS SRV resolution takes times - it should be immediate though */
				if (BC_ASSERT_TRUE(wait_for_until(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneFileTransferDownloadSuccessful,1,55000))) {
					compare_files(send_filepath, receive_filepath);
				}
525 526
			}
		}
527 528 529
		BC_ASSERT_EQUAL(pauline->stat.number_of_LinphoneMessageInProgress,2, int, "%d"); //sent twice because of file transfer
		BC_ASSERT_EQUAL(pauline->stat.number_of_LinphoneMessageDelivered,1, int, "%d");
	}
Sylvain Berfini's avatar
Sylvain Berfini committed
530 531
end:
	bctbx_list_free_with_data(msg_list, (bctbx_list_free_func)linphone_chat_message_unref);
532
	remove(receive_filepath);
533
	ms_free(send_filepath);
534
	bc_free(receive_filepath);
535 536
}

Sylvain Berfini's avatar
Sylvain Berfini committed
537 538
void transfer_message_base(bool_t upload_error, bool_t download_error, bool_t use_file_body_handler_in_upload, 
						   bool_t use_file_body_handler_in_download, bool_t download_from_history) {
539 540 541
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
		LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
Sylvain Berfini's avatar
Sylvain Berfini committed
542
		transfer_message_base2(marie,pauline,upload_error,download_error, use_file_body_handler_in_upload, use_file_body_handler_in_download, download_from_history);
543
		linphone_core_manager_destroy(pauline);
544
		linphone_core_manager_destroy(marie);
545
	}
546
}
547
static void transfer_message(void) {
Sylvain Berfini's avatar
Sylvain Berfini committed
548
	transfer_message_base(FALSE, FALSE, FALSE, FALSE, FALSE);
549 550 551
}

static void transfer_message_2(void) {
Sylvain Berfini's avatar
Sylvain Berfini committed
552
	transfer_message_base(FALSE, FALSE, TRUE, FALSE, FALSE);
553 554 555
}

static void transfer_message_3(void) {
Sylvain Berfini's avatar
Sylvain Berfini committed
556
	transfer_message_base(FALSE, FALSE, FALSE, TRUE, FALSE);
557 558 559
}

static void transfer_message_4(void) {
Sylvain Berfini's avatar
Sylvain Berfini committed
560 561 562 563 564
	transfer_message_base(FALSE, FALSE, TRUE, TRUE, FALSE);
}

static void transfer_message_from_history(void) {
	transfer_message_base(FALSE, FALSE, TRUE, TRUE, TRUE);
565 566 567
}

static void transfer_message_with_upload_io_error(void) {
Sylvain Berfini's avatar
Sylvain Berfini committed
568
	transfer_message_base(TRUE, FALSE, FALSE, FALSE, FALSE);
569 570 571
}

static void transfer_message_with_download_io_error(void) {
Sylvain Berfini's avatar
Sylvain Berfini committed
572
	transfer_message_base(FALSE, TRUE, FALSE, FALSE, FALSE);
573 574 575
}

static void transfer_message_upload_cancelled(void) {
576 577
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
578
		LinphoneChatRoom* chat_room;
579
		LinphoneChatMessage* msg;
580
		LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
johan's avatar
johan committed
581

582 583
		/* Globally configure an http file transfer server. */
		linphone_core_set_file_transfer_server(pauline->lc,"https://www.linphone.org:444/lft.php");
johan's avatar
johan committed
584

585
		/* create a chatroom on pauline's side */
Simon Morlat's avatar
Simon Morlat committed
586
		chat_room = linphone_core_get_chat_room(pauline->lc, marie->identity);
587 588

		msg = create_message_from_nowebcam(chat_room);
589
		linphone_chat_room_send_chat_message(chat_room,msg);
johan's avatar
johan committed
590

591 592 593 594 595 596 597 598
		/*wait for file to be 50% uploaded and cancel the transfer */
		BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&pauline->stat.progress_of_LinphoneFileTransfer, 50));
		linphone_chat_message_cancel_file_transfer(msg);

		BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&pauline->stat.number_of_LinphoneMessageNotDelivered,1));

		BC_ASSERT_EQUAL(pauline->stat.number_of_LinphoneMessageNotDelivered,1, int, "%d");
		BC_ASSERT_EQUAL(marie->stat.number_of_LinphoneFileTransferDownloadSuccessful,0, int, "%d");
johan's avatar
johan committed
599

600
		linphone_core_manager_destroy(pauline);
601
		linphone_core_manager_destroy(marie);
602
	}
johan's avatar
johan committed
603 604
}

605 606 607 608 609
static void transfer_message_download_cancelled(void) {
	LinphoneChatRoom* chat_room;
	LinphoneChatMessage* msg;
	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
610

611 612
	/* Globally configure an http file transfer server. */
	linphone_core_set_file_transfer_server(pauline->lc,"https://www.linphone.org:444/lft.php");
613

614 615 616
	/* create a chatroom on pauline's side */
	chat_room = linphone_core_get_chat_room(pauline->lc,marie->identity);
	msg = create_message_from_nowebcam(chat_room);
617
	linphone_chat_room_send_chat_message(chat_room,msg);
618 619 620 621 622

	/* wait for marie to receive pauline's msg */
	BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneMessageReceivedWithFile,1));

	if (marie->stat.last_received_chat_message ) { /* get last msg and use it to download file */
623 624
		LinphoneChatMessageCbs *cbs = linphone_chat_message_get_callbacks(marie->stat.last_received_chat_message);
		linphone_chat_message_cbs_set_file_transfer_progress_indication(cbs, file_transfer_progress_indication);
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
		linphone_chat_message_start_file_download(marie->stat.last_received_chat_message, liblinphone_tester_chat_message_state_change, marie->lc);
		/* wait for file to be 50% downloaded */
		BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.progress_of_LinphoneFileTransfer, 50));
		/* and cancel the transfer */
		linphone_chat_message_cancel_file_transfer(marie->stat.last_received_chat_message);
	}

	BC_ASSERT_EQUAL(pauline->stat.number_of_LinphoneMessageInProgress,2, int, "%d");
	BC_ASSERT_EQUAL(pauline->stat.number_of_LinphoneMessageDelivered,1, int, "%d");
	BC_ASSERT_EQUAL(marie->stat.number_of_LinphoneFileTransferDownloadSuccessful,0, int, "%d");
	BC_ASSERT_EQUAL(marie->stat.number_of_LinphoneMessageNotDelivered,1, int, "%d");

	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

static void file_transfer_using_external_body_url(void) {
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager *marie = linphone_core_manager_new("marie_rc");
		LinphoneChatRoom *chat_room;
		LinphoneChatMessage *msg;
646
		LinphoneChatMessageCbs *cbs;
647 648 649 650 651 652 653 654 655 656 657
		LinphoneCoreManager *pauline = linphone_core_manager_new("pauline_rc");

		/* make sure lime is disabled */
		linphone_core_enable_lime(marie->lc, FALSE);
		linphone_core_enable_lime(pauline->lc, FALSE);

		/* create a chatroom on pauline's side */
		chat_room = linphone_core_get_chat_room(pauline->lc, marie->identity);

		msg = linphone_chat_room_create_message(chat_room, NULL);

658 659 660
		cbs = linphone_chat_message_get_callbacks(msg);
		linphone_chat_message_cbs_set_msg_state_changed(cbs, liblinphone_tester_chat_message_msg_state_changed);

661 662 663 664 665 666 667 668 669 670 671 672 673 674
		linphone_chat_message_set_external_body_url(msg, "https://www.linphone.org:444//tmp/54ec58280ace9_c30709218df8eaba61d1.jpg");
		linphone_chat_room_send_chat_message(chat_room, msg);

		BC_ASSERT_TRUE(wait_for(pauline->lc, marie->lc, &marie->stat.number_of_LinphoneMessageReceived, 1));
		if (marie->stat.last_received_chat_message) {
			linphone_chat_message_download_file(marie->stat.last_received_chat_message);
		}
		BC_ASSERT_TRUE(wait_for(pauline->lc, marie->lc, &marie->stat.number_of_LinphoneMessageExtBodyReceived, 1));
		BC_ASSERT_TRUE(wait_for(pauline->lc, marie->lc, &pauline->stat.number_of_LinphoneMessageInProgress, 1));
		linphone_core_manager_destroy(pauline);
		linphone_core_manager_destroy(marie);
	}
}

675
static void file_transfer_2_messages_simultaneously(void) {
676 677 678 679 680
	if (transport_supported(LinphoneTransportTls)) {
		LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
		LinphoneChatRoom* pauline_room;
		LinphoneChatMessage* msg;
		LinphoneChatMessage* msg2;
681
		LinphoneChatMessageCbs *cbs;
682 683 684 685
		char *send_filepath = bc_tester_res("images/nowebcamCIF.jpg");
		char *receive_filepath = bc_tester_file("receive_file.dump");
		LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");

686 687 688
		/* Remove any previously downloaded file */
		remove(receive_filepath);
	
689 690 691 692 693 694 695 696
		/* Globally configure an http file transfer server. */
		linphone_core_set_file_transfer_server(pauline->lc,"https://www.linphone.org:444/lft.php");

		/* create a chatroom on pauline's side */
		pauline_room = linphone_core_get_chat_room(pauline->lc, marie->identity);
		msg = create_message_from_nowebcam(pauline_room);
		msg2 = create_message_from_nowebcam(pauline_room);

697 698 699
		cbs = linphone_chat_message_get_callbacks(msg2);
		linphone_chat_message_cbs_set_msg_state_changed(cbs,liblinphone_tester_chat_message_msg_state_changed);

Ghislain MARY's avatar
Ghislain MARY committed
700
		BC_ASSERT_EQUAL((unsigned int)bctbx_list_size(linphone_core_get_chat_rooms(marie->lc)), 0, unsigned int, "%u");
701 702 703 704 705 706 707
		if (bctbx_list_size(linphone_core_get_chat_rooms(marie->lc)) == 0) {
			linphone_chat_room_send_chat_message(pauline_room,msg);
			linphone_chat_room_send_chat_message(pauline_room,msg2);
			if (BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneMessageReceivedWithFile,1))) {
				msg = linphone_chat_message_clone(marie->stat.last_received_chat_message);
				BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneMessageReceivedWithFile,2));
				msg2 = marie->stat.last_received_chat_message;
Ghislain MARY's avatar
Ghislain MARY committed
708
				BC_ASSERT_EQUAL((unsigned int)bctbx_list_size(linphone_core_get_chat_rooms(marie->lc)), 1, unsigned int, "%u");
709 710 711 712 713 714 715 716 717
				if (bctbx_list_size(linphone_core_get_chat_rooms(marie->lc)) != 1) {
					char * buf = ms_strdup_printf("Found %d rooms instead of 1: ", bctbx_list_size(linphone_core_get_chat_rooms(marie->lc)));
					const bctbx_list_t *it = linphone_core_get_chat_rooms(marie->lc);
					while (it) {
						const LinphoneAddress * peer = linphone_chat_room_get_peer_address(it->data);
						buf = ms_strcat_printf("%s, ", linphone_address_get_username(peer));
						it = it->next;
					}
					ms_error("%s", buf);
718
				}
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
719

720 721 722
				cbs = linphone_chat_message_get_callbacks(msg);
				linphone_chat_message_cbs_set_msg_state_changed(cbs, liblinphone_tester_chat_message_msg_state_changed);
				linphone_chat_message_cbs_set_file_transfer_recv(cbs, file_transfer_received);
723
				linphone_chat_message_download_file(msg);
724 725 726 727

				cbs = linphone_chat_message_get_callbacks(msg2);
				linphone_chat_message_cbs_set_msg_state_changed(cbs, liblinphone_tester_chat_message_msg_state_changed);
				linphone_chat_message_cbs_set_file_transfer_recv(cbs, file_transfer_received);
728
				linphone_chat_message_download_file(msg2);
729

730
				BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneFileTransferDownloadSuccessful,2));
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
731

732 733 734
				BC_ASSERT_EQUAL(pauline->stat.number_of_LinphoneMessageInProgress,4, int, "%d");
				BC_ASSERT_EQUAL(pauline->stat.number_of_LinphoneMessageDelivered,2, int, "%d");
				compare_files(send_filepath, receive_filepath);
Gautier Pelloux-Prayer's avatar
Gautier Pelloux-Prayer committed
735

736
				linphone_chat_message_unref(msg);
737 738 739
			}
		}
		linphone_core_manager_destroy(pauline);
740
		remove(receive_filepath);
741
		ms_free(send_filepath);
742
		bc_free(receive_filepath);
743 744 745 746 747 748 749 750 751
		linphone_core_manager_destroy(marie);
	}
}

static void text_message_denied(void) {
	LinphoneCoreManager* marie = linphone_core_manager_new("marie_rc");
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
	LinphoneChatRoom* chat_room = linphone_core_get_chat_room(marie->lc, pauline->identity);
	LinphoneChatMessage* msg = linphone_chat_room_create_message(chat_room,"Bli bli bli \n blu");
752
	LinphoneChatMessageCbs *cbs = linphone_chat_message_get_callbacks(msg);
753 754 755

	/*pauline doesn't want to be disturbed*/
	linphone_core_disable_chat(pauline->lc,LinphoneReasonDoNotDisturb);
756
	linphone_chat_message_cbs_set_msg_state_changed(cbs,liblinphone_tester_chat_message_msg_state_changed);
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
	linphone_chat_room_send_chat_message(chat_room,msg);

	BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&marie->stat.number_of_LinphoneMessageNotDelivered,1));
	BC_ASSERT_EQUAL(pauline->stat.number_of_LinphoneMessageReceived,0, int, "%d");
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

static const char *info_content="<somexml>blabla</somexml>";

void info_message_received(LinphoneCore *lc, LinphoneCall* call, const LinphoneInfoMessage *msg){
	stats* counters = get_stats(lc);

	if (counters->last_received_info_message) {
		linphone_info_message_destroy(counters->last_received_info_message);
	}
	counters->last_received_info_message=linphone_info_message_copy(msg);
	counters->number_of_inforeceived++;
}

void info_message_base(bool_t with_content) {
	LinphoneInfoMessage *info;
	const LinphoneContent *content;
	const char *hvalue;

	LinphoneCoreManager* marie = linphone_core_manager_new( "marie_rc");
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");

	BC_ASSERT_TRUE(call(pauline,marie));

	info=linphone_core_create_info_message(marie->lc);
	linphone_info_message_add_header(info,"Weather","still bad");
	if (with_content) {
		LinphoneContent* ct=linphone_core_create_content(marie->lc);
		linphone_content_set_type(ct,"application");
		linphone_content_set_subtype(ct,"somexml");
		linphone_content_set_buffer(ct,info_content,strlen(info_content));
		linphone_info_message_set_content(info,ct);
		linphone_content_unref(ct);
	}
	linphone_call_send_info_message(linphone_core_get_current_call(marie->lc),info);
	linphone_info_message_destroy(info);

	BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&pauline->stat.number_of_inforeceived,1));

	BC_ASSERT_PTR_NOT_NULL(pauline->stat.last_received_info_message);
	hvalue=linphone_info_message_get_header(pauline->stat.last_received_info_message, "Weather");
	content=linphone_info_message_get_content(pauline->stat.last_received_info_message);

	BC_ASSERT_PTR_NOT_NULL(hvalue);
	if (hvalue)
		BC_ASSERT_STRING_EQUAL(hvalue, "still bad");

	if (with_content){
		BC_ASSERT_PTR_NOT_NULL(content);
		if (content) {
			BC_ASSERT_PTR_NOT_NULL(linphone_content_get_buffer(content));
			BC_ASSERT_PTR_NOT_NULL(linphone_content_get_type(content));
			BC_ASSERT_PTR_NOT_NULL(linphone_content_get_subtype(content));
			if (linphone_content_get_type(content)) BC_ASSERT_STRING_EQUAL(linphone_content_get_type(content),"application");
			if (linphone_content_get_subtype(content)) BC_ASSERT_STRING_EQUAL(linphone_content_get_subtype(content),"somexml");
			if (linphone_content_get_buffer(content))BC_ASSERT_STRING_EQUAL((const char*)linphone_content_get_buffer(content),info_content);
819
			BC_ASSERT_EQUAL((int)linphone_content_get_size(content),(int)strlen(info_content), int, "%d");
820 821 822 823 824 825 826
		}
	}
	end_call(marie, pauline);
	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
}

827
static void info_message(void){
828 829 830
	info_message_base(FALSE);
}

831
static void info_message_with_body(void){
832 833 834
	info_message_base(TRUE);
}

835 836 837 838 839 840 841 842
static FILE* fopen_from_write_dir(const char * name, const char * mode) {
	char *filepath = bc_tester_file(name);
	FILE * file = fopen(filepath,mode);
	bc_free(filepath);
	return file;
}

static void _is_composing_notification(bool_t lime_enabled) {
843 844
	LinphoneChatRoom* chat_room;
	int dummy = 0;
845 846
	char* filepath = NULL;
	FILE *ZIDCacheMarieFD = NULL, *ZIDCachePaulineFD = NULL;
847 848 849

	LinphoneCoreManager* marie = linphone_core_manager_new("marie_rc");
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
Ghislain MARY's avatar
Ghislain MARY committed
850

851 852 853 854 855 856 857 858 859 860 861 862
	if (lime_enabled) {
		if (!linphone_core_lime_available(marie->lc)) {
			ms_warning("Lime not available, skiping");
			goto end;
		}
		/* make sure lime is enabled */
		linphone_core_enable_lime(marie->lc, 1);
		linphone_core_enable_lime(pauline->lc, 1);
		
		/* set the zid caches files : create two ZID cache from this valid one inserting the auto-generated sip URI for the peer account as keys in ZID cache are indexed by peer sip uri */
		ZIDCacheMarieFD = fopen_from_write_dir("tmpZIDCacheMarie.xml", "w");
		ZIDCachePaulineFD = fopen_from_write_dir("tmpZIDCachePauline.xml", "w");
863 864
		fprintf(ZIDCacheMarieFD, marie_zid_cache, linphone_address_as_string_uri_only(pauline->identity), linphone_address_as_string_uri_only(pauline->identity));
		fprintf(ZIDCachePaulineFD, pauline_zid_cache, linphone_address_as_string_uri_only(marie->identity), linphone_address_as_string_uri_only(marie->identity));
865 866 867 868 869 870 871 872 873 874 875 876
		fclose(ZIDCacheMarieFD);
		fclose(ZIDCachePaulineFD);

		filepath = bc_tester_file("tmpZIDCacheMarie.xml");
		linphone_core_set_zrtp_secrets_file(marie->lc, filepath);
		bc_free(filepath);

		filepath = bc_tester_file("tmpZIDCachePauline.xml");
		linphone_core_set_zrtp_secrets_file(pauline->lc, filepath);
		bc_free(filepath);
	}
	
877 878 879 880 881 882 883
	chat_room = linphone_core_get_chat_room(pauline->lc, marie->identity);
	linphone_core_get_chat_room(marie->lc, pauline->identity); /*make marie create the chatroom with pauline, which is necessary for receiving the is-composing*/
	linphone_chat_room_compose(chat_room);
	wait_for_until(pauline->lc, marie->lc, &dummy, 1, 1500); /*just to sleep while iterating*/
	linphone_chat_room_send_message(chat_room, "Composing a msg");
	BC_ASSERT_TRUE(wait_for(pauline->lc, marie->lc, &marie->stat.number_of_LinphoneIsComposingActiveReceived, 1));
	BC_ASSERT_TRUE(wait_for(pauline->lc, marie->lc, &marie->stat.number_of_LinphoneIsComposingIdleReceived, 2));
884 885
	
end:
886 887
	remove("tmpZIDCacheMarie.xml");
	remove("tmpZIDCachePauline.xml");