1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
/*
This file is part of the KContacts framework.
SPDX-FileCopyrightText: 2016-2019 Laurent Montel <montel@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "importexportvcardtest.h"
#include "vcardtool_p.h"
#include <QDebug>
#include <QTest>
ImportExportVCardTest::ImportExportVCardTest(QObject *parent)
: QObject(parent)
{
}
ImportExportVCardTest::~ImportExportVCardTest()
{
}
static void compareBuffers(const QByteArray &outputData, const QByteArray &expected)
{
if (outputData != expected) {
qDebug() << " outputData " << outputData;
qDebug() << " expected " << expected;
}
const QList<QByteArray> outputLines = outputData.split('\n');
const QList<QByteArray> outputRefLines = expected.split('\n');
for (int i = 0; i < qMin(outputLines.count(), outputRefLines.count()); ++i) {
const QByteArray &actual = outputLines.at(i);
const QByteArray &expect = outputRefLines.at(i);
if (actual != expect) {
qCritical() << "Mismatch at output line" << (i + 1);
QCOMPARE(actual, expect);
QCOMPARE(actual.count(), expect.count());
}
}
QCOMPARE(outputLines.count(), outputRefLines.count());
QCOMPARE(outputData.size(), expected.size());
}
void ImportExportVCardTest::shouldExportFullTestVcard4()
{
QByteArray vcarddata(
"BEGIN:VCARD\r\n"
"VERSION:4.0\r\n"
"FN:Sherlock Holmes\r\n"
"TEL;VALUE=uri;PREF=1;TYPE=\"voice,home\":tel:+44-555-555-5555;ext=5555\r\n"
"TEL;VALUE=uri;TYPE=\"voice,cell,text\":tel:+44-555-555-6666\r\n"
"TEL;VALUE=uri;TYPE=\"voice,work\":tel:+44-555-555-7777\r\n"
"N:Holmes;Sherlock;;Mr;;\r\n"
"KIND:individual\r\n"
"NICKNAME:Shirley\r\n"
"PHOTO:\r\n"
"BDAY:19531015T231000Z\r\n"
"ANNIVERSARY:19960415\r\n"
"GENDER:M\r\n"
"ADR;GEO=\"geo:51.5237,0.1585\";LABEL=\"Mr Sherlock Holmes, 221B Baker Street, London NW1, England, United Kingdom\":;;221B Baker "
"Street;London;;NW1;United Kingdom\r\n"
"EMAIL;TYPE=home:sherlock.holmes@gmail.com\r\n"
"EMAIL;PREF=1;TYPE=work:detective@sherlockholmes.com\r\n"
"IMPP;PREF=1:xmpp:detective@sherlockholmes.com\r\n"
"LANG;TYPE=work;PREF=1:en\r\n"
"LANG;TYPE=work;PREF=2:fr\r\n"
"TZ:London/Europe\r\n"
"GEO:geo:51.5237,0.1585\r\n"
"TITLE;ALTID=1;LANGUAGE=fr:Patron\r\n"
"TITLE;ALTID=2;LANGUAGE=en:Boss\r\n"
"ROLE:Detective\r\n"
"UID:urn:uuid:b8767877-b4a1-4c70-9acc-505d3819e519\r\n"
"CATEGORIES:FICTION,LITERATURE\r\n"
"PRODID:-//KADDRESSBOOK//NONSGML Version 1//EN\r\n"
"REV:20140722T222710Z\r\n"
"URL;TYPE=home:https://sherlockholmes.com\r\n"
"KEY;MEDIATYPE=application/pgp-keys:https://sherlockholmes.com/sherlock-holmes.pub.asc\r\n"
"CALURI;PREF=1:https://sherlockholmes.com/calendar/sherlockholmes\r\n"
"FBURL;PREF=1:https://sherlockholmes.com/busy/detective\r\n"
"CALADRURI;PREF=1:mailto:detective@sherlockholmes.com\r\n"
"END:VCARD\r\n\r\n");
QByteArray vcardexpected(
"BEGIN:VCARD\r\n"
"VERSION:4.0\r\n"
"ADR;GEO=\"geo:51.523701,0.158500\";LABEL=\"Mr Sherlock Holmes, 221B Baker Stre\r\n"
" et, London NW1, England, United Kingdom\";TYPE:;;221B Baker Street;London;;\r\n"
" NW1;United Kingdom\r\n"
"ANNIVERSARY:19960415\r\n"
"BDAY:19531015T231000Z\r\n"
"CALADRURI;PREF=1:mailto:detective@sherlockholmes.com\r\n"
"CALURI;PREF=1:https://sherlockholmes.com/calendar/sherlockholmes\r\n"
"CATEGORIES:FICTION,LITERATURE\r\n"
"EMAIL;PREF=1;TYPE=work:detective@sherlockholmes.com\r\n"
"EMAIL;TYPE=home:sherlock.holmes@gmail.com\r\n"
"FBURL;PREF=1:https://sherlockholmes.com/busy/detective\r\n"
"FN:Sherlock Holmes\r\n"
"GENDER:M\r\n"
"GEO:geo:51.523701,0.158500\r\n"
"IMPP;PREF=1:xmpp:detective@sherlockholmes.com\r\n"
"KEY;MEDIATYPE=application/pgp-keys:https://sherlockholmes.com/sherlock-holm\r\n"
" es.pub.asc\r\nKIND:individual\r\n"
"LANG;PREF=1;TYPE=work:en\r\n"
"LANG;PREF=2;TYPE=work:fr\r\n"
"N:Holmes;Sherlock;;Mr;\r\n"
"NICKNAME:Shirley\r\n"
"PRODID:-//KADDRESSBOOK//NONSGML Version 1//EN\r\n"
"REV:20140722T222710Z\r\n"
"ROLE:Detective\r\n"
"TEL;TYPE=\"home,voice\";PREF=1;VALUE=uri:tel:+44-555-555-5555;ext=5555\r\n"
"TEL;TYPE=\"cell,voice\";VALUE=uri:tel:+44-555-555-6666\r\n"
"TEL;TYPE=\"voice,work\";VALUE=uri:tel:+44-555-555-7777\r\n"
"TITLE;ALTID=1;LANGUAGE=fr:Patron\r\n"
"TITLE;ALTID=2;LANGUAGE=en:Boss\r\n"
"TZ:+00:00\r\n"
"UID:urn:uuid:b8767877-b4a1-4c70-9acc-505d3819e519\r\n"
"URL;TYPE=home:https://sherlockholmes.com\r\n"
"END:VCARD\r\n\r\n");
KContacts::VCardTool vcard;
const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
const QByteArray result = vcard.exportVCards(lst, KContacts::VCard::v4_0);
compareBuffers(result, vcardexpected);
}
void ImportExportVCardTest::shouldExportMiscElementVcard4()
{
QByteArray vcarddata(
"BEGIN:VCARD\r\n"
"VERSION:4.0\r\n"
"UID:urn:uuid:4fbe8971-0bc3-424c-9c26-36c3e1eff6b1\r\n"
"FN;PID=1.1:J. Doe\r\n"
"N:Doe;J.;;;\r\n"
"EMAIL;PID=1.1:jdoe@example.com\r\n"
"EMAIL;PID=2.1:boss@example.com\r\n"
"TEL;PID=1.1;VALUE=uri:tel:+1-555-555-5555\r\n"
"TEL;PID=2.1;VALUE=uri:tel:+1-666-666-6666\r\n"
"CLIENTPIDMAP:1;urn:uuid:53e374d9-337e-4727-8803-a1e9c14e0556\r\n"
"END:VCARD\r\n\r\n");
QByteArray vcardexpected(
"BEGIN:VCARD\r\n"
"VERSION:4.0\r\n"
"CLIENTPIDMAP:1;urn:uuid:53e374d9-337e-4727-8803-a1e9c14e0556\r\n"
"EMAIL;PID=1.1:jdoe@example.com\r\n"
"EMAIL;PID=2.1:boss@example.com\r\n"
"FN:J. Doe\r\n"
"N:Doe;J.;;;\r\n"
"TEL;PID=1.1;VALUE=uri:tel:+1-555-555-5555\r\n"
"TEL;PID=2.1;VALUE=uri:tel:+1-666-666-6666\r\n"
"UID:urn:uuid:4fbe8971-0bc3-424c-9c26-36c3e1eff6b1\r\n"
"END:VCARD\r\n\r\n");
KContacts::VCardTool vcard;
const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
const QByteArray result = vcard.exportVCards(lst, KContacts::VCard::v4_0);
compareBuffers(result, vcardexpected);
}
void ImportExportVCardTest::shouldExportMemberElementVcard4()
{
QByteArray vcarddata(
"BEGIN:VCARD"
"VERSION:4.0\r\n"
"KIND:group\r\n"
"FN:Funky distribution list\r\n"
"MEMBER:mailto:subscriber1@example.com\r\n"
"MEMBER:xmpp:subscriber2@example.com\r\n"
"MEMBER:sip:subscriber3@example.com\r\n"
"MEMBER:tel:+1-418-555-5555\r\n"
"UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\r\n"
"END:VCARD\r\n");
QByteArray vcardexpected(
"BEGIN:VCARD\r\n"
"VERSION:4.0\r\n"
"FN:Funky distribution list\r\n"
"KIND:group\r\n"
"MEMBER:mailto:subscriber1@example.com\r\n"
"MEMBER:xmpp:subscriber2@example.com\r\n"
"MEMBER:sip:subscriber3@example.com\r\n"
"MEMBER:tel:+1-418-555-5555\r\n"
"N:;;;;\r\n"
"UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\r\n"
"END:VCARD\r\n\r\n");
KContacts::VCardTool vcard;
const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
const QByteArray result = vcard.exportVCards(lst, KContacts::VCard::v4_0);
compareBuffers(result, vcardexpected);
}
void ImportExportVCardTest::shouldExportMissingNewlineVcard4()
{
QByteArray vcarddata(
"BEGIN:VCARD"
"VERSION:4.0\r\n"
"KIND:group\r\n"
"FN:Funky distribution list\r\n"
"UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\r\n"
"END:VCARD");
QByteArray vcardexpected(
"BEGIN:VCARD\r\n"
"VERSION:4.0\r\n"
"FN:Funky distribution list\r\n"
"KIND:group\r\n"
"N:;;;;\r\n"
"UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\r\n"
"END:VCARD\r\n\r\n");
KContacts::VCardTool vcard;
const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
const QByteArray result = vcard.exportVCards(lst, KContacts::VCard::v4_0);
compareBuffers(result, vcardexpected);
}
// TODO please make this data driven before copy/pasting more methods here...
QTEST_MAIN(ImportExportVCardTest)
|