cpim-tester.cpp 12.5 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 19 20 21 22
 * 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/>.
 */

#include "cpim/cpim.h"

#include "liblinphone_tester.h"

Ronan's avatar
Ronan committed
23
// =============================================================================
Ronan's avatar
Ronan committed
24

Ronan's avatar
Ronan committed
25
using namespace std;
Ronan's avatar
Ronan committed
26

Ronan's avatar
Ronan committed
27
using namespace LINPHONE_NAMESPACE;
Ronan's avatar
Ronan committed
28

Ronan's avatar
Ronan committed
29
static void parse_minimal_message () {
Ronan's avatar
Ronan committed
30 31 32 33 34 35 36 37 38 39 40 41
	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());
}

Ronan's avatar
Ronan committed
42
static void set_generic_header_name () {
Ronan's avatar
Ronan committed
43 44 45 46 47 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
	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
85
static void set_generic_header_value () {
Ronan's avatar
Ronan committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
	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
109
static void check_core_header_names () {
Ronan's avatar
Ronan committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
	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
126
static void set_core_header_values () {
Ronan's avatar
Ronan committed
127 128 129 130 131 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
	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
199
static void check_subject_header_language () {
Ronan's avatar
Ronan committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	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
229
static void parse_rfc_example () {
Ronan's avatar
Ronan committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
	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"
		"Content-type text/xml; charset=utf-8\r\n"
		"Content-ID: <1234567890@foo.com>\r\n"
		"\r\n"
		"<body>"
		"Here is the text of my message."
		"</body>";

	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());
}

Ronan's avatar
Ronan committed
256
static void parse_message_with_generic_header_parameters () {
Ronan's avatar
Ronan committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
	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"
		"Content-type text/xml; charset=utf-8\r\n"
		"Content-ID: <1234567890@foo.com>\r\n"
		"\r\n"
		"<body>"
		"Here is the text of my message."
		"</body>";

	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());
}

Ronan's avatar
Ronan committed
278
static void build_message () {
Ronan's avatar
Ronan committed
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
	Cpim::Message message;
	if (!BC_ASSERT_FALSE(message.isValid()))
		return;

	// Set CPIM headers.
	Cpim::GenericHeader contentTypeHeader;
	if (!BC_ASSERT_TRUE(contentTypeHeader.setName("Content-Type"))) return;
	if (!BC_ASSERT_TRUE(contentTypeHeader.setValue("Message/CPIM"))) return;

	if (!BC_ASSERT_TRUE(message.addCpimHeader(contentTypeHeader))) return;

	// 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;

	const string content = "Content-type text/xml; charset=utf-8\r\n"
		"Content-ID: <1234567890@foo.com>\r\n"
		"\r\n"
		"<body>"
		"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"
		"Content-type text/xml; charset=utf-8\r\n"
		"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());
}

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),
	TEST_NO_TAG("Build Message", build_message)
};

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
};