cpim-tester.cpp 14.7 KB
Newer Older
Ronan's avatar
Ronan committed
1
/*
Ronan's avatar
Ronan committed
2
 * cpim-tester.cpp
Ronan's avatar
Ronan committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * Copyright (C) 2017  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/>.
 */

Ronan's avatar
Ronan committed
19
#include "chat/cpim/cpim.h"
Sylvain Berfini's avatar
Sylvain Berfini committed
20 21 22
#include "address/address.h"
#include "chat/basic-chat-room.h"
#include "chat/chat-message.h"
Ronan's avatar
Ronan committed
23 24 25

#include "liblinphone_tester.h"

Ronan's avatar
Ronan committed
26
// =============================================================================
Ronan's avatar
Ronan committed
27

Ronan's avatar
Ronan committed
28
using namespace std;
Ronan's avatar
Ronan committed
29

30
using namespace LinphonePrivate;
Ronan's avatar
Ronan committed
31

Ronan's avatar
Ronan committed
32
static void parse_minimal_message () {
Ronan's avatar
Ronan committed
33 34 35 36 37 38 39 40 41 42
	const string str = "Content-type: Message/CPIM\r\n"
		"\r\n"
		"Content-Type: text/plain; charset=utf-8\r\n"
		"\r\n";

	shared_ptr<const Cpim::Message> message = Cpim::Message::createFromString(str);
	if (!BC_ASSERT_PTR_NOT_NULL(message)) return;

	const string str2 = message->asString();
	BC_ASSERT_STRING_EQUAL(str2.c_str(), str.c_str());
43 44

	BC_ASSERT_STRING_EQUAL(message->getContent().c_str(), "");
Ronan's avatar
Ronan committed
45 46
}

Ronan's avatar
Ronan committed
47
static void set_generic_header_name () {
Ronan's avatar
Ronan committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	const list<pair<string, bool> > entries = {
		{ "toto", true },
		{ "george.abitbol", true },
		{ "tata/titi", false },
		{ "hey ho", false },
		{ " fail", false },
		{ "fail2 ", false },

		// Reserved.
		{ "From", false },
		{ "To", false },
		{ "cc", false },
		{ "DateTime", false },
		{ "Subject", false },
		{ "NS", false },
		{ "Require", false },

		// Case sensitivity.
		{ "FROM", true },
		{ "to", true },
		{ "cC", true },
		{ "Datetime", true },
		{ "SuBject", true },
		{ "nS", true },
		{ "requirE", true }
	};

	for (const auto &entry : entries) {
		Cpim::GenericHeader genericHeader;

		const bool result = genericHeader.setName(entry.first);
		BC_ASSERT_EQUAL(result, entry.second, bool, "%d");

		const string name = genericHeader.getName();

		if (result)
			BC_ASSERT_STRING_EQUAL(name.c_str(), entry.first.c_str());
		else
			BC_ASSERT_STRING_EQUAL(name.c_str(), "");
	}
}

Ronan's avatar
Ronan committed
90
static void set_generic_header_value () {
Ronan's avatar
Ronan committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	const list<pair<string, bool> > entries = {
		{ "MyFeatures <mid:MessageFeatures@id.foo.com>", true },
		{ "2000-12-13T13:40:00-08:00", true },
		{ "2000-12-13T13:40:00-08:00", true },
		{ "text/xml; charset=utf-8", true },
		{ "text/xml; charset=ut\r\nf-8", false }
	};

	for (const auto &entry : entries) {
		Cpim::GenericHeader genericHeader;

		const bool result = genericHeader.setValue(entry.first);
		BC_ASSERT_EQUAL(result, entry.second, bool, "%d");

		const string value = genericHeader.getValue();

		if (result)
			BC_ASSERT_STRING_EQUAL(value.c_str(), entry.first.c_str());
		else
			BC_ASSERT_STRING_EQUAL(value.c_str(), "");
	}
}

Ronan's avatar
Ronan committed
114
static void check_core_header_names () {
Ronan's avatar
Ronan committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	const list<pair<shared_ptr<Cpim::CoreHeader>, string> > entries = {
		{ make_shared<Cpim::FromHeader>(), "From" },
		{ make_shared<Cpim::ToHeader>(), "To" },
		{ make_shared<Cpim::CcHeader>(), "cc" },
		{ make_shared<Cpim::DateTimeHeader>(), "DateTime" },
		{ make_shared<Cpim::SubjectHeader>(), "Subject" },
		{ make_shared<Cpim::NsHeader>(), "NS" },
		{ make_shared<Cpim::RequireHeader>(), "Require" }
	};

	for (const auto &entry : entries) {
		const string name = entry.first->getName();
		BC_ASSERT_STRING_EQUAL(name.c_str(), entry.second.c_str());
	}
}

Ronan's avatar
Ronan committed
131
static void set_core_header_values () {
Ronan's avatar
Ronan committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
	const list<pair<shared_ptr<Cpim::Header>, list<pair<string, bool> > > > entries = {
		{ make_shared<Cpim::FromHeader>(), {
				{ "Winnie the Pooh <im:pooh@100akerwood.com>", true },
				{ "<im:tigger@100akerwood.com>", true },
				{ "<im:tigger@100akerwood.com", false },
				{ "<im:tigger>", true },
				{ "toto", false }
			} },
		{ make_shared<Cpim::ToHeader>(), {
				{ "<im:tigger@100akerwood.com", false },
				{ "Winnie the Pooh <im:pooh@100akerwood.com>", true },
				{ "toto", false },
				{ "<im:tigger>", true },
				{ "<im:tigger@100akerwood.com>", true }
			} },
		{ make_shared<Cpim::CcHeader>(), {
				{ "<im:tigger@100akerwood.com>", true },
				{ "<im:tigger@100akerwood.com", false },
				{ "Winnie the Pooh <im:pooh@100akerwood.com>", true },
				{ "<im:tigger>", true },
				{ "toto", false }
			} },
		{ make_shared<Cpim::DateTimeHeader>(), {
				{ "abcd", false },
				{ "1985-04-12T23:20:50.52Z", true },
				{ "1996-12-19T16:39:57-08:00", true },
				{ "1990-12-31T23:59:60Z", true },
				{ "1990-12-31T15:59:60-08:00", true },
				{ "2001-02-29T10:10:10Z", false },
				{ "2000-02-29T10:10:10Z", true },
				{ "1937-01-01T12:00:27.87+00:20", true },
				{ "1937-01-01T12:00:27.87Z", true },
				{ "1956", false }
			} },
		{ make_shared<Cpim::SubjectHeader>(), {
				{ "Eeyore's feeling very depressed today", true },
				{ "🤣", true },
				{ "hello", true }
			} },
		{ make_shared<Cpim::NsHeader>(), {
				{ "MyAlias <mid:MessageFeatures@id.foo.com>", true },
				{ "What is this? - Barry Burton", false },
				{ "<mid:MessageFeatures@id.foo.com>", true },
				{ "<mid:MessageFeatures@id.foo.com", false }
			} },
		{ make_shared<Cpim::RequireHeader>(), {
				{ "MyAlias.VitalHeader", true },
				{ "MyAlias.VitalHeader,Test", true },
				{ "MyAlias.VitalHeader,🤣", false }
			} }
	};

	for (const auto &entry : entries) {
		const shared_ptr<Cpim::Header> header = entry.first;
		string previousValue;

		for (const auto &test : entry.second) {
			const bool result = header->setValue(test.first);
			BC_ASSERT_EQUAL(result, test.second, bool, "%d");

			const string value = header->getValue();

			if (result)
				BC_ASSERT_STRING_EQUAL(value.c_str(), test.first.c_str());
			else
				BC_ASSERT_STRING_EQUAL(value.c_str(), previousValue.c_str());

			previousValue = value;
		}
	}
}

Ronan's avatar
Ronan committed
204
static void check_subject_header_language () {
Ronan's avatar
Ronan committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
	Cpim::SubjectHeader subjectHeader;

	// Check for not defined language.
	{
		const string language = subjectHeader.getLanguage();
		BC_ASSERT_STRING_EQUAL(language.c_str(), "");
	}

	// Set valid language.
	{
		const string languageToSet = "fr";

		BC_ASSERT_TRUE(subjectHeader.setLanguage(languageToSet));
		BC_ASSERT_TRUE(languageToSet == subjectHeader.getLanguage());

		const string str = subjectHeader.asString();
		const string expected = "Subject:;lang=" + languageToSet + " \r\n";
		BC_ASSERT_STRING_EQUAL(str.c_str(), expected.c_str());
	}

	// Set invalid language.
	{
		const string languageToSet = "fr--";
		BC_ASSERT_FALSE(subjectHeader.setLanguage(languageToSet));
		BC_ASSERT_FALSE(languageToSet == subjectHeader.getLanguage());
		BC_ASSERT_FALSE(subjectHeader.isValid());
	}
}

Ronan's avatar
Ronan committed
234
static void parse_rfc_example () {
235 236 237 238
	const string body = "<body>"
		"Here is the text of my message."
		"</body>";

Ronan's avatar
Ronan committed
239 240 241 242 243 244 245 246 247 248 249 250
	const string str = "Content-type: Message/CPIM\r\n"
		"\r\n"
		"From: MR SANDERS <im:piglet@100akerwood.com>\r\n"
		"To: Depressed Donkey <im:eeyore@100akerwood.com>\r\n"
		"DateTime: 2000-12-13T13:40:00-08:00\r\n"
		"Subject: the weather will be fine today\r\n"
		"Subject:;lang=fr beau temps prevu pour aujourd'hui\r\n"
		"NS: MyFeatures <mid:MessageFeatures@id.foo.com>\r\n"
		"Require: MyFeatures.VitalMessageOption\r\n"
		"MyFeatures.VitalMessageOption: Confirmation-requested\r\n"
		"MyFeatures.WackyMessageOption: Use-silly-font\r\n"
		"\r\n"
251
		"Content-Type: text/xml; charset=utf-8\r\n"
Ronan's avatar
Ronan committed
252
		"Content-ID: <1234567890@foo.com>\r\n"
253
		"\r\n" + body;
Ronan's avatar
Ronan committed
254 255 256 257 258 259

	shared_ptr<const Cpim::Message> message = Cpim::Message::createFromString(str);
	if (!BC_ASSERT_PTR_NOT_NULL(message)) return;

	const string str2 = message->asString();
	BC_ASSERT_STRING_EQUAL(str2.c_str(), str.c_str());
260 261 262
	
	string content = message->getContent();
	BC_ASSERT_STRING_EQUAL(content.c_str(), body.c_str());
Ronan's avatar
Ronan committed
263 264
}

Ronan's avatar
Ronan committed
265
static void parse_message_with_generic_header_parameters () {
266 267 268 269
	const string body = "<body>"
		"Here is the text of my message."
		"</body>";

Ronan's avatar
Ronan committed
270 271 272 273 274 275 276
	const string str = "Content-type: Message/CPIM\r\n"
		"\r\n"
		"From: MR SANDERS <im:piglet@100akerwood.com>\r\n"
		"Test:;aaa=bbb;yes=no CheckMe\r\n"
		"yaya: coucou\r\n"
		"yepee:;good=bad ugly\r\n"
		"\r\n"
277
		"Content-Type: text/xml; charset=utf-8\r\n"
Ronan's avatar
Ronan committed
278
		"Content-ID: <1234567890@foo.com>\r\n"
279
		"\r\n" + body;
Ronan's avatar
Ronan committed
280 281 282 283 284 285

	shared_ptr<const Cpim::Message> message = Cpim::Message::createFromString(str);
	if (!BC_ASSERT_PTR_NOT_NULL(message)) return;

	const string str2 = message->asString();
	BC_ASSERT_STRING_EQUAL(str2.c_str(), str.c_str());
286 287 288
	
	string content = message->getContent();
	BC_ASSERT_STRING_EQUAL(content.c_str(), body.c_str());
Ronan's avatar
Ronan committed
289 290
}

Ronan's avatar
Ronan committed
291
static void build_message () {
Ronan's avatar
Ronan committed
292 293 294 295 296
	Cpim::Message message;
	if (!BC_ASSERT_FALSE(message.isValid()))
		return;

	// Set CPIM headers.
297 298 299
	Cpim::GenericHeader cpimContentTypeHeader;
	if (!BC_ASSERT_TRUE(cpimContentTypeHeader.setName("Content-Type"))) return;
	if (!BC_ASSERT_TRUE(cpimContentTypeHeader.setValue("Message/CPIM"))) return;
Ronan's avatar
Ronan committed
300

301
	if (!BC_ASSERT_TRUE(message.addCpimHeader(cpimContentTypeHeader))) return;
Ronan's avatar
Ronan committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

	// Set message headers.
	Cpim::FromHeader fromHeader;
	if (!BC_ASSERT_TRUE(fromHeader.setValue("MR SANDERS <im:piglet@100akerwood.com>"))) return;

	Cpim::ToHeader toHeader;
	if (!BC_ASSERT_TRUE(toHeader.setValue("Depressed Donkey <im:eeyore@100akerwood.com>"))) return;

	Cpim::DateTimeHeader dateTimeHeader;
	if (!BC_ASSERT_TRUE(dateTimeHeader.setValue("2000-12-13T13:40:00-08:00"))) return;

	Cpim::SubjectHeader subjectHeader;
	if (!BC_ASSERT_TRUE(subjectHeader.setValue("the weather will be fine today"))) return;

	Cpim::SubjectHeader subjectWithLanguageHeader;
	if (!BC_ASSERT_TRUE(subjectWithLanguageHeader.setValue("beau temps prevu pour aujourd'hui"))) return;
	if (!BC_ASSERT_TRUE(subjectWithLanguageHeader.setLanguage("fr"))) return;

	Cpim::NsHeader nsHeader;
	if (!BC_ASSERT_TRUE(nsHeader.setValue("MyFeatures <mid:MessageFeatures@id.foo.com>"))) return;

	Cpim::RequireHeader requireHeader;
	if (!BC_ASSERT_TRUE(requireHeader.setValue("MyFeatures.VitalMessageOption"))) return;

	Cpim::GenericHeader vitalMessageHeader;
	if (!BC_ASSERT_TRUE(vitalMessageHeader.setName("MyFeatures.VitalMessageOption"))) return;
	if (!BC_ASSERT_TRUE(vitalMessageHeader.setValue("Confirmation-requested"))) return;

	Cpim::GenericHeader wackyMessageHeader;
	if (!BC_ASSERT_TRUE(wackyMessageHeader.setName("MyFeatures.WackyMessageOption"))) return;
	if (!BC_ASSERT_TRUE(wackyMessageHeader.setValue("Use-silly-font"))) return;

	if (!BC_ASSERT_TRUE(message.addMessageHeader(fromHeader))) return;
	if (!BC_ASSERT_TRUE(message.addMessageHeader(toHeader))) return;
	if (!BC_ASSERT_TRUE(message.addMessageHeader(dateTimeHeader))) return;
	if (!BC_ASSERT_TRUE(message.addMessageHeader(subjectHeader))) return;
	if (!BC_ASSERT_TRUE(message.addMessageHeader(subjectWithLanguageHeader))) return;
	if (!BC_ASSERT_TRUE(message.addMessageHeader(nsHeader))) return;
	if (!BC_ASSERT_TRUE(message.addMessageHeader(requireHeader))) return;
	if (!BC_ASSERT_TRUE(message.addMessageHeader(vitalMessageHeader))) return;
	if (!BC_ASSERT_TRUE(message.addMessageHeader(wackyMessageHeader))) return;

344 345 346 347 348 349 350 351 352 353 354 355
	// Set Content headers.
    Cpim::GenericHeader contentTypeHeader;
	if (!BC_ASSERT_TRUE(contentTypeHeader.setName("Content-Type"))) return;
	if (!BC_ASSERT_TRUE( contentTypeHeader.setValue("text/xml; charset=utf-8"))) return;
	if (!BC_ASSERT_TRUE(message.addContentHeader(contentTypeHeader))) return;
	
    Cpim::GenericHeader contentIdHeader;
	if (!BC_ASSERT_TRUE(contentIdHeader.setName("Content-ID"))) return;
	if (!BC_ASSERT_TRUE( contentIdHeader.setValue("<1234567890@foo.com>"))) return;
    if (!BC_ASSERT_TRUE(message.addContentHeader(contentIdHeader))) return;

	const string content = "<body>"
Ronan's avatar
Ronan committed
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
		"Here is the text of my message."
		"</body>";

	if (!BC_ASSERT_TRUE(message.setContent(content))) return;
	if (!BC_ASSERT_TRUE(message.isValid())) return;

	const string strMessage = message.asString();
	const string expectedMessage = "Content-Type: Message/CPIM\r\n"
		"\r\n"
		"From: MR SANDERS <im:piglet@100akerwood.com>\r\n"
		"To: Depressed Donkey <im:eeyore@100akerwood.com>\r\n"
		"DateTime: 2000-12-13T13:40:00-08:00\r\n"
		"Subject: the weather will be fine today\r\n"
		"Subject:;lang=fr beau temps prevu pour aujourd'hui\r\n"
		"NS: MyFeatures <mid:MessageFeatures@id.foo.com>\r\n"
		"Require: MyFeatures.VitalMessageOption\r\n"
		"MyFeatures.VitalMessageOption: Confirmation-requested\r\n"
		"MyFeatures.WackyMessageOption: Use-silly-font\r\n"
		"\r\n"
375
		"Content-Type: text/xml; charset=utf-8\r\n"
Ronan's avatar
Ronan committed
376 377 378 379 380 381 382 383 384
		"Content-ID: <1234567890@foo.com>\r\n"
		"\r\n"
		"<body>"
		"Here is the text of my message."
		"</body>";

	BC_ASSERT_STRING_EQUAL(strMessage.c_str(), expectedMessage.c_str());
}

Sylvain Berfini's avatar
Sylvain Berfini committed
385 386 387 388 389 390 391 392
static void cpim_chat_message_modifier(void) {
	LinphoneCoreManager* marie = linphone_core_manager_new("marie_rc");
	LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc");
	LpConfig *config = linphone_core_get_config(marie->lc);
	lp_config_set_int(config, "sip", "use_cpim", 1);

	Address paulineAddress(linphone_address_as_string_uri_only(pauline->identity));
	shared_ptr<ChatRoom> marieRoom = ObjectFactory::create<BasicChatRoom>(marie->lc, paulineAddress);
393

Sylvain Berfini's avatar
Sylvain Berfini committed
394 395 396 397 398
	shared_ptr<ChatMessage> marieMessage = marieRoom->createMessage("Hello CPIM");
	marieRoom->sendMessage(marieMessage);

	BC_ASSERT_TRUE(wait_for(pauline->lc,marie->lc,&pauline->stat.number_of_LinphoneMessageReceived,1));
	BC_ASSERT_TRUE(ContentType::isCpim(marieMessage->getInternalContent().getContentType().asString()));
399
	
Sylvain Berfini's avatar
Sylvain Berfini committed
400 401 402 403 404 405 406 407
	BC_ASSERT_PTR_NOT_NULL(pauline->stat.last_received_chat_message);
	if (pauline->stat.last_received_chat_message != NULL) {
		BC_ASSERT_STRING_EQUAL(linphone_chat_message_get_text(pauline->stat.last_received_chat_message), "Hello CPIM");
		BC_ASSERT_STRING_EQUAL(linphone_chat_message_get_content_type(pauline->stat.last_received_chat_message), "text/plain");
	}

	linphone_core_manager_destroy(marie);
	linphone_core_manager_destroy(pauline);
408 409
}

Ronan's avatar
Ronan committed
410 411 412 413 414 415 416 417 418
test_t cpim_tests[] = {
	TEST_NO_TAG("Parse minimal CPIM message", parse_minimal_message),
	TEST_NO_TAG("Set generic header name", set_generic_header_name),
	TEST_NO_TAG("Set generic header value", set_generic_header_value),
	TEST_NO_TAG("Check core header names", check_core_header_names),
	TEST_NO_TAG("Set core header values", set_core_header_values),
	TEST_NO_TAG("Check Subject header language", check_subject_header_language),
	TEST_NO_TAG("Parse RFC example", parse_rfc_example),
	TEST_NO_TAG("Parse Message with generic header parameters", parse_message_with_generic_header_parameters),
419
	TEST_NO_TAG("Build Message", build_message),
Sylvain Berfini's avatar
Sylvain Berfini committed
420
	TEST_NO_TAG("CPIM chat message modifier", cpim_chat_message_modifier)
Ronan's avatar
Ronan committed
421 422 423 424 425 426
};

test_suite_t cpim_test_suite = {
	"Cpim", NULL, NULL, liblinphone_tester_before_each, liblinphone_tester_after_each,
	sizeof(cpim_tests) / sizeof(cpim_tests[0]), cpim_tests
};