File indexing completed on 2022-09-20 15:48:23

0001 /*
0002     SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #include <QtTest/QTest>
0007 
0008 #include "mecardparser.h"
0009 
0010 class MeCardParserTest : public QObject
0011 {
0012     Q_OBJECT
0013 
0014 private slots:
0015     void testWifiParse()
0016     {
0017         MeCardParser p;
0018         QVERIFY(p.parse(QStringLiteral("WIFI:S:myssid;T:WPA;P:xxx123;;")));
0019         QCOMPARE(p.header(), QLatin1String("WIFI"));
0020         QCOMPARE(p.value(u"S"), QLatin1String("myssid"));
0021         QCOMPARE(p.value(u"T"), QLatin1String("WPA"));
0022         QCOMPARE(p.value(u"P"), QLatin1String("xxx123"));
0023 
0024         QVERIFY(p.parse(QStringLiteral("MECARD:N:Doe,John;TEL:13035551212;EMAIL:john.doe@example.com;;")));
0025         QCOMPARE(p.header(), QLatin1String("MECARD"));
0026         QCOMPARE(p.value(u"N"), QLatin1String("Doe,John"));
0027         QCOMPARE(p.value(u"TEL"), QLatin1String("13035551212"));
0028         QCOMPARE(p.value(u"EMAIL"), QLatin1String("john.doe@example.com"));
0029 
0030         QVERIFY(p.parse(QStringLiteral("MECARD:N:Doe,John;TEL:13035551212;EMAIL:john.doe@example.com;EMAIL:null@kde.org;;")));
0031         QCOMPARE(p.header(), QLatin1String("MECARD"));
0032         QCOMPARE(p.value(u"N"), QLatin1String("Doe,John"));
0033         QCOMPARE(p.value(u"TEL"), QLatin1String("13035551212"));
0034         QCOMPARE(p.value(u"EMAIL"), QString());
0035         QCOMPARE(p.values(u"EMAIL"), QStringList({QLatin1String("john.doe@example.com"), QLatin1String("null@kde.org")}));
0036 
0037         QVERIFY(p.parse(QStringLiteral("WIFI:S:\\\"foo\\;bar\\\\baz\\\";P:\"ABCD\";;")));
0038         QCOMPARE(p.value(u"P"), QLatin1String("ABCD"));
0039         QCOMPARE(p.value(u"S"), QLatin1String("\"foo;bar\\baz\""));
0040     }
0041 
0042     void testInvalid_data()
0043     {
0044         QTest::addColumn<QString>("input");
0045         QTest::newRow("empty") << QString();
0046         QTest::newRow("no colon") << QStringLiteral("wifi");
0047         QTest::newRow("no content 1") << QStringLiteral("wifi:");
0048         QTest::newRow("no content 2") << QStringLiteral("wifi:;");
0049         QTest::newRow("no content 3") << QStringLiteral("wifi:;;");
0050         QTest::newRow("invalid element") << QStringLiteral("wifi:S:");
0051     }
0052     void testInvalid()
0053     {
0054         QFETCH(QString, input);
0055         MeCardParser p;
0056         QVERIFY(!p.parse(input));
0057     }
0058 };
0059 
0060 QTEST_APPLESS_MAIN(MeCardParserTest)
0061 
0062 #include "mecardparsertest.moc"