| 1 | /** |
| 2 | * Copyright (C) 2004-2006 Brad Hards <bradh@frogmouth.net> |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 16 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 17 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 18 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 19 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 20 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 21 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include <QTest> |
| 27 | #include <QtCrypto> |
| 28 | |
| 29 | #ifdef QT_STATICPLUGIN |
| 30 | #include "import_plugins.h" |
| 31 | #endif |
| 32 | |
| 33 | class HexUnitTest : public QObject |
| 34 | { |
| 35 | Q_OBJECT |
| 36 | |
| 37 | private Q_SLOTS: |
| 38 | void initTestCase(); |
| 39 | void cleanupTestCase(); |
| 40 | void testHexString_data(); |
| 41 | void testHexString(); |
| 42 | void testIncrementalUpdate(); |
| 43 | void testBrokenInput(); |
| 44 | |
| 45 | private: |
| 46 | QCA::Initializer *m_init; |
| 47 | }; |
| 48 | |
| 49 | void HexUnitTest::initTestCase() |
| 50 | { |
| 51 | m_init = new QCA::Initializer; |
| 52 | } |
| 53 | |
| 54 | void HexUnitTest::cleanupTestCase() |
| 55 | { |
| 56 | delete m_init; |
| 57 | } |
| 58 | |
| 59 | void HexUnitTest::testHexString_data() |
| 60 | { |
| 61 | QTest::addColumn<QString>(name: "raw" ); |
| 62 | QTest::addColumn<QString>(name: "encoded" ); |
| 63 | |
| 64 | QTest::newRow(dataTag: "abcd" ) << QStringLiteral("abcd" ) << QStringLiteral("61626364" ); |
| 65 | QTest::newRow(dataTag: "ABCD" ) << QStringLiteral("ABCD" ) << QStringLiteral("41424344" ); |
| 66 | QTest::newRow(dataTag: "empty" ) << QString(QLatin1String("" )) << QString(QLatin1String("" )); |
| 67 | QTest::newRow(dataTag: "abcddef" ) << QStringLiteral("abcddef" ) << QStringLiteral("61626364646566" ); |
| 68 | QTest::newRow(dataTag: "empty too" ) << QString::fromLatin1(ba: "\0" ) // clazy:exclude=qstring-allocations |
| 69 | << QString::fromLatin1(ba: "" ); // Empty QString. clazy:exclude=qstring-allocations |
| 70 | QTest::newRow(dataTag: "BEL" ) << QStringLiteral("\a" ) << QStringLiteral("07" ); // BEL |
| 71 | QTest::newRow(dataTag: "BS" ) << QStringLiteral("\b" ) << QStringLiteral("08" ); // BS |
| 72 | QTest::newRow(dataTag: "HT" ) << QStringLiteral("\t" ) << QStringLiteral("09" ); // HT |
| 73 | QTest::newRow(dataTag: "LF" ) << QStringLiteral("\n" ) << QStringLiteral("0a" ); // LF |
| 74 | QTest::newRow(dataTag: "VT" ) << QStringLiteral("\v" ) << QStringLiteral("0b" ); // VT |
| 75 | QTest::newRow(dataTag: "FF" ) << QStringLiteral("\f" ) << QStringLiteral("0c" ); // FF |
| 76 | QTest::newRow(dataTag: "CR" ) << QStringLiteral("\r" ) << QStringLiteral("0d" ); // CR |
| 77 | QTest::newRow(dataTag: "bug126735" ) << QStringLiteral("@ABCDEFGHIJKLMNO" ) |
| 78 | << QStringLiteral("404142434445464748494a4b4c4d4e4f" ); |
| 79 | } |
| 80 | |
| 81 | void HexUnitTest::testHexString() |
| 82 | { |
| 83 | QCA::Hex hexObject; |
| 84 | QFETCH(QString, raw); |
| 85 | QFETCH(QString, encoded); |
| 86 | QCOMPARE(hexObject.encodeString(raw), encoded); |
| 87 | QCOMPARE(hexObject.decodeString(encoded), raw); |
| 88 | } |
| 89 | |
| 90 | void HexUnitTest::testIncrementalUpdate() |
| 91 | { |
| 92 | QCA::Hex hexObject; |
| 93 | |
| 94 | hexObject.setup(QCA::Encode); |
| 95 | hexObject.clear(); |
| 96 | QCA::SecureArray result1 = hexObject.update(a: QCA::SecureArray("ab" )); |
| 97 | QVERIFY(hexObject.ok()); |
| 98 | QCOMPARE(result1[0], '6'); |
| 99 | QCOMPARE(result1[1], '1'); |
| 100 | QCOMPARE(result1[2], '6'); |
| 101 | QCOMPARE(result1[3], '2'); |
| 102 | QCA::SecureArray result2 = hexObject.update(a: QCA::SecureArray("cd" )); |
| 103 | QCOMPARE(hexObject.ok(), true); |
| 104 | QCOMPARE(result2[0], '6'); |
| 105 | QCOMPARE(result2[1], '3'); |
| 106 | QCOMPARE(result2[2], '6'); |
| 107 | QCOMPARE(result2[3], '4'); |
| 108 | QCOMPARE(QCA::SecureArray(), QCA::SecureArray(hexObject.final())); |
| 109 | QCOMPARE(hexObject.ok(), true); |
| 110 | } |
| 111 | |
| 112 | void HexUnitTest::testBrokenInput() |
| 113 | { |
| 114 | QCA::Hex hexObject; |
| 115 | |
| 116 | hexObject.setup(QCA::Decode); |
| 117 | hexObject.update(a: QCA::SecureArray("-=" )); |
| 118 | QCOMPARE(hexObject.ok(), false); |
| 119 | } |
| 120 | |
| 121 | QTEST_MAIN(HexUnitTest) |
| 122 | |
| 123 | #include "hexunittest.moc" |
| 124 | |