File indexing completed on 2024-03-24 03:56:13

0001 /*
0002     This file is part of the KContacts framework.
0003     SPDX-FileCopyrightText: 2007 Tobias Koenig <tokoe@kde.org>
0004     SPDX-FileCopyrightText: 2016-2019 Laurent Montel <montel@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "keytest.h"
0010 #include "kcontacts/key.h"
0011 #include "vcardtool_p.h"
0012 #include <QTest>
0013 
0014 QTEST_MAIN(KeyTest)
0015 
0016 void KeyTest::storeTest()
0017 {
0018     KContacts::Key key;
0019 
0020     key.setId(QStringLiteral("My Id"));
0021     key.setType(KContacts::Key::Custom);
0022     key.setCustomTypeString(QStringLiteral("GnuPG"));
0023     key.setTextData(QStringLiteral("That's my super secret key"));
0024 
0025     QVERIFY(key.id() == QLatin1String("My Id"));
0026     QVERIFY(key.type() == KContacts::Key::Custom);
0027     QVERIFY(key.customTypeString() == QLatin1String("GnuPG"));
0028     QVERIFY(key.textData() == QLatin1String("That's my super secret key"));
0029     QVERIFY(key.isBinary() == false);
0030 }
0031 
0032 void KeyTest::equalsTest()
0033 {
0034     KContacts::Key key1;
0035     KContacts::Key key2;
0036 
0037     key1.setId(QStringLiteral("My Id"));
0038     key1.setType(KContacts::Key::Custom);
0039     key1.setCustomTypeString(QStringLiteral("GnuPG"));
0040     key1.setTextData(QStringLiteral("That's my super secret key"));
0041 
0042     key2.setId(QStringLiteral("My Id"));
0043     key2.setType(KContacts::Key::Custom);
0044     key2.setCustomTypeString(QStringLiteral("GnuPG"));
0045     key2.setTextData(QStringLiteral("That's my super secret key"));
0046 
0047     QVERIFY(key1 == key2);
0048 }
0049 
0050 void KeyTest::differsTest()
0051 {
0052     KContacts::Key key1(QStringLiteral("TextKey"), KContacts::Key::PGP);
0053     KContacts::Key key2(QStringLiteral("TextKey"), KContacts::Key::Custom);
0054 
0055     QVERIFY(key1 != key2);
0056 }
0057 
0058 void KeyTest::assignmentTest()
0059 {
0060     KContacts::Key key1;
0061     KContacts::Key key2;
0062 
0063     key1.setId(QStringLiteral("My Id"));
0064     key1.setType(KContacts::Key::Custom);
0065     key1.setCustomTypeString(QStringLiteral("GnuPG"));
0066     key1.setTextData(QStringLiteral("That's my super secret key"));
0067 
0068     key2 = key1;
0069 
0070     QVERIFY(key1 == key2);
0071 }
0072 
0073 void KeyTest::serializeTest()
0074 {
0075     KContacts::Key key1;
0076     KContacts::Key key2;
0077 
0078     key1.setId(QStringLiteral("My Id"));
0079     key1.setType(KContacts::Key::Custom);
0080     key1.setCustomTypeString(QStringLiteral("GnuPG"));
0081     key1.setTextData(QStringLiteral("That's my super secret key"));
0082 
0083     QByteArray data;
0084     QDataStream s(&data, QIODevice::WriteOnly);
0085     s << key1;
0086 
0087     QDataStream t(&data, QIODevice::ReadOnly);
0088     t >> key2;
0089 
0090     QVERIFY(key1 == key2);
0091 }
0092 
0093 void KeyTest::shouldExportVCard3()
0094 {
0095     KContacts::AddresseeList lst;
0096     KContacts::Addressee addr;
0097     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0098     addr.setUid(QStringLiteral("testuid"));
0099     KContacts::Key key1(QStringLiteral("https://foo.org/sherlock-holmes.pub.asc"), KContacts::Key::PGP);
0100     addr.setKeys(KContacts::Key::List{key1});
0101     lst << addr;
0102     KContacts::VCardTool vcard;
0103     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v3_0);
0104     QByteArray expected(
0105         "BEGIN:VCARD\r\n"
0106         "VERSION:3.0\r\n"
0107         "EMAIL:foo@kde.org\r\n"
0108         "KEY;TYPE=PGP:https://foo.org/sherlock-holmes.pub.asc\r\n"
0109         "N:;;;;\r\n"
0110         "UID:testuid\r\n"
0111         "END:VCARD\r\n\r\n");
0112 
0113     QCOMPARE(ba, expected);
0114 }
0115 
0116 void KeyTest::shouldExportVCard4()
0117 {
0118     KContacts::AddresseeList lst;
0119     KContacts::Addressee addr;
0120     addr.setEmails(QStringList() << QStringLiteral("foo@kde.org"));
0121     addr.setUid(QStringLiteral("testuid"));
0122     KContacts::Key key1(QStringLiteral("https://foo.org/sherlock-holmes.pub.asc"), KContacts::Key::PGP);
0123     addr.setKeys(KContacts::Key::List{key1});
0124     lst << addr;
0125     KContacts::VCardTool vcard;
0126     const QByteArray ba = vcard.exportVCards(lst, KContacts::VCard::v4_0);
0127     QByteArray expected(
0128         "BEGIN:VCARD\r\n"
0129         "VERSION:4.0\r\n"
0130         "EMAIL:foo@kde.org\r\n"
0131         "KEY;MEDIATYPE=application/pgp-keys:https://foo.org/sherlock-holmes.pub.asc\r\n"
0132         "N:;;;;\r\n"
0133         "UID:testuid\r\n"
0134         "END:VCARD\r\n\r\n");
0135 
0136     QCOMPARE(ba, expected);
0137 }
0138 
0139 void KeyTest::shouldParseVcard3()
0140 {
0141     QByteArray vcarddata(
0142         "BEGIN:VCARD\r\n"
0143         "VERSION:3.0\r\n"
0144         "EMAIL:foo@kde.org\r\n"
0145         "KEY;TYPE=PGP:https://foo.org/sherlock-holmes.pub.asc\r\n"
0146         "N:;;;;\r\n"
0147         "UID:testuid\r\n"
0148         "END:VCARD\r\n\r\n");
0149 
0150     KContacts::VCardTool vcard;
0151     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
0152     QCOMPARE(lst.count(), 1);
0153     QVERIFY(!lst.at(0).keys().isEmpty());
0154     QCOMPARE(lst.at(0).keys().count(), 1);
0155 
0156     KContacts::Key key = lst.at(0).keys().at(0);
0157     QCOMPARE(key.type(), KContacts::Key::PGP);
0158     QCOMPARE(key.textData(), QStringLiteral("https://foo.org/sherlock-holmes.pub.asc"));
0159 }
0160 
0161 void KeyTest::shouldParseVcard4()
0162 {
0163     QByteArray vcarddata(
0164         "BEGIN:VCARD\r\n"
0165         "VERSION:4.0\r\n"
0166         "EMAIL:foo@kde.org\r\n"
0167         "KEY;MEDIATYPE=application/pgp-keys:https://foo.org/sherlock-holmes.pub.asc\r\n"
0168         "N:;;;;\r\n"
0169         "UID:testuid\r\n"
0170         "END:VCARD\r\n\r\n");
0171     KContacts::VCardTool vcard;
0172     const KContacts::AddresseeList lst = vcard.parseVCards(vcarddata);
0173     QCOMPARE(lst.count(), 1);
0174     QVERIFY(!lst.at(0).keys().isEmpty());
0175     QCOMPARE(lst.at(0).keys().count(), 1);
0176 
0177     KContacts::Key key = lst.at(0).keys().at(0);
0178     QCOMPARE(key.type(), KContacts::Key::PGP);
0179     QCOMPARE(key.textData(), QStringLiteral("https://foo.org/sherlock-holmes.pub.asc"));
0180 }
0181 
0182 #include "moc_keytest.cpp"