belcard-vcard-tester.cpp 5.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
	belcard-vcard-tester.cpp
	Copyright (C) 2015  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/>.
*/

19

Sylvain Berfini's avatar
Sylvain Berfini committed
20
#include <belr/belr.h>
21
#include "belcard/belcard.hpp"
22
#include "belcard/belcard_parser.hpp"
23
#include "belcard/belcard_utils.hpp"
24
#include "belcard-tester.hpp"
25

26
#include <bctoolbox/tester.h>
27 28 29 30 31 32
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace::std;
Sylvain Berfini's avatar
Sylvain Berfini committed
33
using namespace::belr;
34 35 36
using namespace::belcard;

static string openFile(const char *name) {
37
	ifstream istr(bc_tester_res(name), std::ios::binary);
38 39 40
	if (!istr.is_open()) {
		BC_FAIL(name);
	}
41

42 43 44 45 46 47 48 49
	stringstream vcardStream;
	vcardStream << istr.rdbuf();
	string vcard = vcardStream.str();
	return vcard;
}

static void folding(void) {
	string vcard = openFile("vcards/foldtest.vcf");
50
	string folded_vcard = belcard_fold(vcard);
Sylvain Berfini's avatar
Sylvain Berfini committed
51 52
	string unfolded_vcard = openFile("vcards/unfoldtest.vcf");
	BC_ASSERT_EQUAL(unfolded_vcard.compare(folded_vcard), 0, int, "%d");
53 54 55 56
}

static void unfolding(void) {
	string vcard = openFile("vcards/unfoldtest.vcf");
57
	string unfolded_vcard = belcard_unfold(vcard);
Sylvain Berfini's avatar
Sylvain Berfini committed
58 59
	string folded_vcard = openFile("vcards/foldtest.vcf");
	BC_ASSERT_EQUAL(folded_vcard.compare(unfolded_vcard), 0, int, "%d");
60 61 62 63
}

static void vcard_parsing(void) {
	string vcard = openFile("vcards/vcard.vcf");
64

65 66
	BelCardParser *parser = new BelCardParser();
	shared_ptr<BelCard> belCard = parser->parseOne(vcard);
67
	if (!BC_ASSERT_TRUE(belCard!=NULL)) return;
68
	BC_ASSERT_TRUE(belCard->assertRFCCompliance());
69

70 71
	string vcard2 = belCard->toFoldedString();
	BC_ASSERT_EQUAL(vcard2.compare(vcard), 0, int, "%d");
72
	delete(parser);
73 74
}

75 76
static void vcards_parsing(void) {
	string vcards = openFile("vcards/vcards.vcf");
77

78 79
	BelCardParser *parser = new BelCardParser();
	shared_ptr<BelCardList> belCards = parser->parse(vcards);
80
	if (!BC_ASSERT_TRUE(belCards!=NULL)) return;
81
	BC_ASSERT_EQUAL((unsigned int)belCards->getCards().size(), 2, unsigned int, "%u");
82

83 84
	string vcards2 = belCards->toString();
	BC_ASSERT_EQUAL(vcards2.compare(vcards), 0, int, "%d");
85
	delete(parser);
86 87
}

Sylvain Berfini's avatar
Sylvain Berfini committed
88 89
static void create_vcard_from_api(void) {
	shared_ptr<BelCard> belCard = BelCard::create<BelCard>();
90
	if (!BC_ASSERT_TRUE(belCard!=NULL)) return;
Sylvain Berfini's avatar
Sylvain Berfini committed
91
	BC_ASSERT_FALSE(belCard->assertRFCCompliance());
92

Sylvain Berfini's avatar
Sylvain Berfini committed
93 94 95 96
	shared_ptr<BelCardFullName> fn = BelCard::create<BelCardFullName>();
	fn->setValue("Sylvain Berfini");
	belCard->setFullName(fn);
	BC_ASSERT_TRUE(belCard->assertRFCCompliance());
97
	BC_ASSERT_STRING_EQUAL(belCard->getFullName()->toString().c_str(), fn->toString().c_str());
Ben Rejeb Iyed's avatar
Ben Rejeb Iyed committed
98

99 100 101 102
	fn = BelCard::create<BelCardFullName>();
	fn->setValue("Belcard Tester");
	belCard->setFullName(fn);
	BC_ASSERT_STRING_EQUAL(belCard->getFullName()->toString().c_str(), fn->toString().c_str());
103

Sylvain Berfini's avatar
Sylvain Berfini committed
104
	string vcard = belCard->toString();
105 106
	BelCardParser *parser = new BelCardParser();
	shared_ptr<BelCard> belCard2 = parser->parseOne(vcard);
107
	if (!BC_ASSERT_TRUE(belCard2!=NULL)) return;
108
	BC_ASSERT_TRUE(belCard2->assertRFCCompliance());
Sylvain Berfini's avatar
Sylvain Berfini committed
109
	string vcard2 = belCard2->toString();
110
	BC_ASSERT_EQUAL(vcard.compare(vcard2), 0, unsigned, "%u");
111
	delete(parser);
Sylvain Berfini's avatar
Sylvain Berfini committed
112 113
}

114 115
static void property_sort_using_pref_param(void) {
	shared_ptr<BelCard> belCard = BelCard::create<BelCard>();
116
	BC_ASSERT_TRUE(belCard!=NULL);
117

118
	shared_ptr<BelCardImpp> impp1 = BelCardImpp::parse("IMPP;TYPE=home;PREF=2:sip:viish@sip.linphone.org\r\n");
119
	BC_ASSERT_TRUE(impp1!=NULL);
120

121
	shared_ptr<BelCardImpp> impp2 = BelCardImpp::parse("IMPP;PREF=1;TYPE=work:sip:sylvain@sip.linphone.org\r\n");
122
	BC_ASSERT_TRUE(impp2!=NULL);
123

124 125
	belCard->addImpp(impp1);
	belCard->addImpp(impp2);
126

127
	const list<shared_ptr<BelCardImpp>> imppList = belCard->getImpp();
128
	BC_ASSERT_EQUAL((unsigned int)imppList.size(), 2, unsigned int, "%u");
129 130
	BC_ASSERT_TRUE(imppList.front() == impp2);
	BC_ASSERT_TRUE(imppList.back() == impp1);
Ben Rejeb Iyed's avatar
Ben Rejeb Iyed committed
131

132 133
	const list<shared_ptr<BelCardProperty>> propertiesList = belCard->getProperties();
	BC_ASSERT_EQUAL((unsigned int)propertiesList.size(), 2, unsigned int, "%u");
Ben Rejeb Iyed's avatar
Ben Rejeb Iyed committed
134

135 136 137
	belCard->removeImpp(impp1);
	BC_ASSERT_EQUAL((unsigned int)belCard->getImpp().size(), 1, unsigned int, "%u");
	BC_ASSERT_EQUAL((unsigned int)belCard->getProperties().size(), 1, unsigned int, "%u");
138 139
}

140
static test_t tests[] = {
141 142 143 144 145 146
	TEST_NO_TAG("Folding", folding),
	TEST_NO_TAG("Unfolding", unfolding),
	TEST_NO_TAG("VCard parsing", vcard_parsing),
	TEST_NO_TAG("VCards parsing", vcards_parsing),
	TEST_NO_TAG("VCard created from scratch", create_vcard_from_api),
	TEST_NO_TAG("Property sort using pref param", property_sort_using_pref_param),
147 148 149 150 151 152 153 154 155 156
};

test_suite_t vcard_test_suite = {
	"VCard",
	NULL,
	NULL,
	NULL,
	NULL,
	sizeof(tests) / sizeof(tests[0]),
	tests
157
};