File indexing completed on 2024-04-14 14:18:54

0001 /*
0002     This file is part of the KContacts framework.
0003     SPDX-FileCopyrightText: 2016-2019 Laurent Montel <montel@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "importexportvcardtest.h"
0009 #include "vcardtool_p.h"
0010 #include <QDebug>
0011 #include <QTest>
0012 
0013 ImportExportVCardTest::ImportExportVCardTest(QObject *parent)
0014     : QObject(parent)
0015 {
0016 }
0017 
0018 ImportExportVCardTest::~ImportExportVCardTest()
0019 {
0020 }
0021 
0022 static void compareBuffers(const QByteArray &outputData, const QByteArray &expected)
0023 {
0024     if (outputData != expected) {
0025         qDebug() << " outputData " << outputData;
0026         qDebug() << " expected " << expected;
0027     }
0028     const QList<QByteArray> outputLines = outputData.split('\n');
0029     const QList<QByteArray> outputRefLines = expected.split('\n');
0030     for (int i = 0; i < qMin(outputLines.count(), outputRefLines.count()); ++i) {
0031         const QByteArray &actual = outputLines.at(i);
0032         const QByteArray &expect = outputRefLines.at(i);
0033         if (actual != expect) {
0034             qCritical() << "Mismatch at output line" << (i + 1);
0035             QCOMPARE(actual, expect);
0036             QCOMPARE(actual.count(), expect.count());
0037         }
0038     }
0039     QCOMPARE(outputLines.count(), outputRefLines.count());
0040     QCOMPARE(outputData.size(), expected.size());
0041 }
0042 
0043 void ImportExportVCardTest::shouldExportFullTestVcard4()
0044 {
0045     QByteArray vcarddata(
0046         "BEGIN:VCARD\r\n"
0047         "VERSION:4.0\r\n"
0048         "FN:Sherlock Holmes\r\n"
0049         "TEL;VALUE=uri;PREF=1;TYPE=\"voice,home\":tel:+44-555-555-5555;ext=5555\r\n"
0050         "TEL;VALUE=uri;TYPE=\"voice,cell,text\":tel:+44-555-555-6666\r\n"
0051         "TEL;VALUE=uri;TYPE=\"voice,work\":tel:+44-555-555-7777\r\n"
0052         "N:Holmes;Sherlock;;Mr;;\r\n"
0053         "KIND:individual\r\n"
0054         "NICKNAME:Shirley\r\n"
0055         "PHOTO:\r\n"
0056         "BDAY:19531015T231000Z\r\n"
0057         "ANNIVERSARY:19960415\r\n"
0058         "GENDER:M\r\n"
0059         "ADR;GEO=\"geo:51.5237,0.1585\";LABEL=\"Mr Sherlock Holmes, 221B Baker Street, London NW1, England, United Kingdom\":;;221B Baker "
0060         "Street;London;;NW1;United Kingdom\r\n"
0061         "EMAIL;TYPE=home:sherlock.holmes@gmail.com\r\n"
0062         "EMAIL;PREF=1;TYPE=work:detective@sherlockholmes.com\r\n"
0063         "IMPP;PREF=1:xmpp:detective@sherlockholmes.com\r\n"
0064         "LANG;TYPE=work;PREF=1:en\r\n"
0065         "LANG;TYPE=work;PREF=2:fr\r\n"
0066         "TZ:London/Europe\r\n"
0067         "GEO:geo:51.5237,0.1585\r\n"
0068         "TITLE;ALTID=1;LANGUAGE=fr:Patron\r\n"
0069         "TITLE;ALTID=2;LANGUAGE=en:Boss\r\n"
0070         "ROLE:Detective\r\n"
0071         "UID:urn:uuid:b8767877-b4a1-4c70-9acc-505d3819e519\r\n"
0072         "CATEGORIES:FICTION,LITERATURE\r\n"
0073         "PRODID:-//KADDRESSBOOK//NONSGML Version 1//EN\r\n"
0074         "REV:20140722T222710Z\r\n"
0075         "URL;TYPE=home:https://sherlockholmes.com\r\n"
0076         "KEY;MEDIATYPE=application/pgp-keys:https://sherlockholmes.com/sherlock-holmes.pub.asc\r\n"
0077         "CALURI;PREF=1:https://sherlockholmes.com/calendar/sherlockholmes\r\n"
0078         "FBURL;PREF=1:https://sherlockholmes.com/busy/detective\r\n"
0079         "CALADRURI;PREF=1:mailto:detective@sherlockholmes.com\r\n"
0080         "END:VCARD\r\n\r\n");
0081     QByteArray vcardexpected(
0082         "BEGIN:VCARD\r\n"
0083         "VERSION:4.0\r\n"
0084         "ADR;GEO=\"geo:51.523701,0.158500\";LABEL=\"Mr Sherlock Holmes, 221B Baker Stre\r\n"
0085         " et, London NW1, England, United Kingdom\";TYPE:;;221B Baker Street;London;;\r\n"
0086         " NW1;United Kingdom\r\n"
0087         "ANNIVERSARY:19960415\r\n"
0088         "BDAY:19531015T231000Z\r\n"
0089         "CALADRURI;PREF=1:mailto:detective@sherlockholmes.com\r\n"
0090         "CALURI;PREF=1:https://sherlockholmes.com/calendar/sherlockholmes\r\n"
0091         "CATEGORIES:FICTION,LITERATURE\r\n"
0092         "EMAIL;PREF=1;TYPE=work:detective@sherlockholmes.com\r\n"
0093         "EMAIL;TYPE=home:sherlock.holmes@gmail.com\r\n"
0094         "FBURL;PREF=1:https://sherlockholmes.com/busy/detective\r\n"
0095         "FN:Sherlock Holmes\r\n"
0096         "GENDER:M\r\n"
0097         "GEO:geo:51.523701,0.158500\r\n"
0098         "IMPP;PREF=1:xmpp:detective@sherlockholmes.com\r\n"
0099         "KEY;MEDIATYPE=application/pgp-keys:https://sherlockholmes.com/sherlock-holm\r\n"
0100         " es.pub.asc\r\nKIND:individual\r\n"
0101         "LANG;PREF=1;TYPE=work:en\r\n"
0102         "LANG;PREF=2;TYPE=work:fr\r\n"
0103         "N:Holmes;Sherlock;;Mr;\r\n"
0104         "NICKNAME:Shirley\r\n"
0105         "PRODID:-//KADDRESSBOOK//NONSGML Version 1//EN\r\n"
0106         "REV:20140722T222710Z\r\n"
0107         "ROLE:Detective\r\n"
0108         "TEL;TYPE=\"home,voice\";PREF=1;VALUE=uri:tel:+44-555-555-5555;ext=5555\r\n"
0109         "TEL;TYPE=\"cell,voice\";VALUE=uri:tel:+44-555-555-6666\r\n"
0110         "TEL;TYPE=\"voice,work\";VALUE=uri:tel:+44-555-555-7777\r\n"
0111         "TITLE;ALTID=1;LANGUAGE=fr:Patron\r\n"
0112         "TITLE;ALTID=2;LANGUAGE=en:Boss\r\n"
0113         "TZ:+00:00\r\n"
0114         "UID:urn:uuid:b8767877-b4a1-4c70-9acc-505d3819e519\r\n"
0115         "URL;TYPE=home:https://sherlockholmes.com\r\n"
0116         "END:VCARD\r\n\r\n");
0117 
0118     KContacts::VCardTool vcard;
0119     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
0120 
0121     const QByteArray result = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0122     compareBuffers(result, vcardexpected);
0123 }
0124 
0125 void ImportExportVCardTest::shouldExportMiscElementVcard4()
0126 {
0127     QByteArray vcarddata(
0128         "BEGIN:VCARD\r\n"
0129         "VERSION:4.0\r\n"
0130         "UID:urn:uuid:4fbe8971-0bc3-424c-9c26-36c3e1eff6b1\r\n"
0131         "FN;PID=1.1:J. Doe\r\n"
0132         "N:Doe;J.;;;\r\n"
0133         "EMAIL;PID=1.1:jdoe@example.com\r\n"
0134         "EMAIL;PID=2.1:boss@example.com\r\n"
0135         "TEL;PID=1.1;VALUE=uri:tel:+1-555-555-5555\r\n"
0136         "TEL;PID=2.1;VALUE=uri:tel:+1-666-666-6666\r\n"
0137         "CLIENTPIDMAP:1;urn:uuid:53e374d9-337e-4727-8803-a1e9c14e0556\r\n"
0138         "END:VCARD\r\n\r\n");
0139     QByteArray vcardexpected(
0140         "BEGIN:VCARD\r\n"
0141         "VERSION:4.0\r\n"
0142         "CLIENTPIDMAP:1;urn:uuid:53e374d9-337e-4727-8803-a1e9c14e0556\r\n"
0143         "EMAIL;PID=1.1:jdoe@example.com\r\n"
0144         "EMAIL;PID=2.1:boss@example.com\r\n"
0145         "FN:J. Doe\r\n"
0146         "N:Doe;J.;;;\r\n"
0147         "TEL;PID=1.1;VALUE=uri:tel:+1-555-555-5555\r\n"
0148         "TEL;PID=2.1;VALUE=uri:tel:+1-666-666-6666\r\n"
0149         "UID:urn:uuid:4fbe8971-0bc3-424c-9c26-36c3e1eff6b1\r\n"
0150         "END:VCARD\r\n\r\n");
0151 
0152     KContacts::VCardTool vcard;
0153     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
0154 
0155     const QByteArray result = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0156     compareBuffers(result, vcardexpected);
0157 }
0158 
0159 void ImportExportVCardTest::shouldExportMemberElementVcard4()
0160 {
0161     QByteArray vcarddata(
0162         "BEGIN:VCARD"
0163         "VERSION:4.0\r\n"
0164         "KIND:group\r\n"
0165         "FN:Funky distribution list\r\n"
0166         "MEMBER:mailto:subscriber1@example.com\r\n"
0167         "MEMBER:xmpp:subscriber2@example.com\r\n"
0168         "MEMBER:sip:subscriber3@example.com\r\n"
0169         "MEMBER:tel:+1-418-555-5555\r\n"
0170         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\r\n"
0171         "END:VCARD\r\n");
0172 
0173     QByteArray vcardexpected(
0174         "BEGIN:VCARD\r\n"
0175         "VERSION:4.0\r\n"
0176         "FN:Funky distribution list\r\n"
0177         "KIND:group\r\n"
0178         "MEMBER:mailto:subscriber1@example.com\r\n"
0179         "MEMBER:xmpp:subscriber2@example.com\r\n"
0180         "MEMBER:sip:subscriber3@example.com\r\n"
0181         "MEMBER:tel:+1-418-555-5555\r\n"
0182         "N:;;;;\r\n"
0183         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\r\n"
0184         "END:VCARD\r\n\r\n");
0185 
0186     KContacts::VCardTool vcard;
0187     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
0188 
0189     const QByteArray result = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0190     compareBuffers(result, vcardexpected);
0191 }
0192 
0193 void ImportExportVCardTest::shouldExportMissingNewlineVcard4()
0194 {
0195     QByteArray vcarddata(
0196         "BEGIN:VCARD"
0197         "VERSION:4.0\r\n"
0198         "KIND:group\r\n"
0199         "FN:Funky distribution list\r\n"
0200         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\r\n"
0201         "END:VCARD");
0202 
0203     QByteArray vcardexpected(
0204         "BEGIN:VCARD\r\n"
0205         "VERSION:4.0\r\n"
0206         "FN:Funky distribution list\r\n"
0207         "KIND:group\r\n"
0208         "N:;;;;\r\n"
0209         "UID:c80cf296-0825-4eb0-ab16-1fac1d522a33@xxxxxx.xx\r\n"
0210         "END:VCARD\r\n\r\n");
0211 
0212     KContacts::VCardTool vcard;
0213     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
0214 
0215     const QByteArray result = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0216     compareBuffers(result, vcardexpected);
0217 }
0218 
0219 // TODO please make this data driven before copy/pasting more methods here...
0220 
0221 QTEST_MAIN(ImportExportVCardTest)
0222 
0223 #include "moc_importexportvcardtest.cpp"