File indexing completed on 2024-05-12 08:45:38
0001 /** 0002 * Copyright (C) 2004-2006 Brad Hards <bradh@frogmouth.net> 0003 * 0004 * Redistribution and use in source and binary forms, with or without 0005 * modification, are permitted provided that the following conditions 0006 * are met: 0007 * 0008 * 1. Redistributions of source code must retain the above copyright 0009 * notice, this list of conditions and the following disclaimer. 0010 * 2. Redistributions in binary form must reproduce the above copyright 0011 * notice, this list of conditions and the following disclaimer in the 0012 * documentation and/or other materials provided with the distribution. 0013 * 0014 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 0015 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 0016 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 0017 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 0018 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 0019 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 0020 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 0021 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 0022 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 0023 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0024 */ 0025 0026 #include <QtCrypto> 0027 #include <QtTest/QtTest> 0028 0029 #ifdef QT_STATICPLUGIN 0030 #include "import_plugins.h" 0031 #endif 0032 0033 class HexUnitTest : public QObject 0034 { 0035 Q_OBJECT 0036 0037 private Q_SLOTS: 0038 void initTestCase(); 0039 void cleanupTestCase(); 0040 void testHexString_data(); 0041 void testHexString(); 0042 void testIncrementalUpdate(); 0043 void testBrokenInput(); 0044 0045 private: 0046 QCA::Initializer *m_init; 0047 }; 0048 0049 void HexUnitTest::initTestCase() 0050 { 0051 m_init = new QCA::Initializer; 0052 } 0053 0054 void HexUnitTest::cleanupTestCase() 0055 { 0056 delete m_init; 0057 } 0058 0059 void HexUnitTest::testHexString_data() 0060 { 0061 QTest::addColumn<QString>("raw"); 0062 QTest::addColumn<QString>("encoded"); 0063 0064 QTest::newRow("abcd") << QStringLiteral("abcd") << QStringLiteral("61626364"); 0065 QTest::newRow("ABCD") << QStringLiteral("ABCD") << QStringLiteral("41424344"); 0066 QTest::newRow("empty") << QString(QLatin1String("")) << QString(QLatin1String("")); 0067 QTest::newRow("abcddef") << QStringLiteral("abcddef") << QStringLiteral("61626364646566"); 0068 QTest::newRow("empty too") << QString::fromLatin1("\0") // clazy:exclude=qstring-allocations 0069 << QString::fromLatin1(""); // Empty QString. clazy:exclude=qstring-allocations 0070 QTest::newRow("BEL") << QStringLiteral("\a") << QStringLiteral("07"); // BEL 0071 QTest::newRow("BS") << QStringLiteral("\b") << QStringLiteral("08"); // BS 0072 QTest::newRow("HT") << QStringLiteral("\t") << QStringLiteral("09"); // HT 0073 QTest::newRow("LF") << QStringLiteral("\n") << QStringLiteral("0a"); // LF 0074 QTest::newRow("VT") << QStringLiteral("\v") << QStringLiteral("0b"); // VT 0075 QTest::newRow("FF") << QStringLiteral("\f") << QStringLiteral("0c"); // FF 0076 QTest::newRow("CR") << QStringLiteral("\r") << QStringLiteral("0d"); // CR 0077 QTest::newRow("bug126735") << QStringLiteral("@ABCDEFGHIJKLMNO") 0078 << QStringLiteral("404142434445464748494a4b4c4d4e4f"); 0079 } 0080 0081 void HexUnitTest::testHexString() 0082 { 0083 QCA::Hex hexObject; 0084 QFETCH(QString, raw); 0085 QFETCH(QString, encoded); 0086 QCOMPARE(hexObject.encodeString(raw), encoded); 0087 QCOMPARE(hexObject.decodeString(encoded), raw); 0088 } 0089 0090 void HexUnitTest::testIncrementalUpdate() 0091 { 0092 QCA::Hex hexObject; 0093 0094 hexObject.setup(QCA::Encode); 0095 hexObject.clear(); 0096 QCA::SecureArray result1 = hexObject.update(QCA::SecureArray("ab")); 0097 QVERIFY(hexObject.ok()); 0098 QCOMPARE(result1[0], '6'); 0099 QCOMPARE(result1[1], '1'); 0100 QCOMPARE(result1[2], '6'); 0101 QCOMPARE(result1[3], '2'); 0102 QCA::SecureArray result2 = hexObject.update(QCA::SecureArray("cd")); 0103 QCOMPARE(hexObject.ok(), true); 0104 QCOMPARE(result2[0], '6'); 0105 QCOMPARE(result2[1], '3'); 0106 QCOMPARE(result2[2], '6'); 0107 QCOMPARE(result2[3], '4'); 0108 QCOMPARE(QCA::SecureArray(), QCA::SecureArray(hexObject.final())); 0109 QCOMPARE(hexObject.ok(), true); 0110 } 0111 0112 void HexUnitTest::testBrokenInput() 0113 { 0114 QCA::Hex hexObject; 0115 0116 hexObject.setup(QCA::Decode); 0117 hexObject.update(QCA::SecureArray("-=")); 0118 QCOMPARE(hexObject.ok(), false); 0119 } 0120 0121 QTEST_MAIN(HexUnitTest) 0122 0123 #include "hexunittest.moc"