| 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 StaticUnitTest : public QObject |
| 34 | { |
| 35 | Q_OBJECT |
| 36 | |
| 37 | private Q_SLOTS: |
| 38 | void initTestCase(); |
| 39 | void cleanupTestCase(); |
| 40 | void hexConversions(); |
| 41 | void capabilities(); |
| 42 | void secureMemory(); |
| 43 | |
| 44 | private: |
| 45 | QCA::Initializer *m_init; |
| 46 | }; |
| 47 | |
| 48 | void StaticUnitTest::initTestCase() |
| 49 | { |
| 50 | m_init = new QCA::Initializer; |
| 51 | } |
| 52 | |
| 53 | void StaticUnitTest::cleanupTestCase() |
| 54 | { |
| 55 | delete m_init; |
| 56 | } |
| 57 | |
| 58 | void StaticUnitTest::hexConversions() |
| 59 | { |
| 60 | QByteArray test(10, 'a'); |
| 61 | |
| 62 | QCOMPARE(QCA::arrayToHex(test), QStringLiteral("61616161616161616161" )); |
| 63 | |
| 64 | test.fill(c: 'b'); |
| 65 | test[7] = 0x00; |
| 66 | |
| 67 | QCOMPARE(test == QCA::hexToArray(QStringLiteral("62626262626262006262" )), true); |
| 68 | |
| 69 | QCA::SecureArray testArray(10); |
| 70 | // testArray.fill( 'a' ); |
| 71 | for (int i = 0; i < testArray.size(); i++) { |
| 72 | testArray[i] = 0x61; |
| 73 | } |
| 74 | QCOMPARE(QCA::arrayToHex(testArray.toByteArray()), QStringLiteral("61616161616161616161" )); |
| 75 | // testArray.fill( 'b' ); |
| 76 | for (int i = 0; i < testArray.size(); i++) { |
| 77 | testArray[i] = 0x62; |
| 78 | } |
| 79 | testArray[6] = 0x00; |
| 80 | QCOMPARE(testArray == QCA::hexToArray(QStringLiteral("62626262626200626262" )), true); |
| 81 | |
| 82 | QCOMPARE(testArray == QCA::hexToArray(QCA::arrayToHex(testArray.toByteArray())), true); |
| 83 | |
| 84 | testArray[9] = 0x00; |
| 85 | QCOMPARE(testArray == QCA::hexToArray(QCA::arrayToHex(testArray.toByteArray())), true); |
| 86 | } |
| 87 | |
| 88 | void StaticUnitTest::capabilities() |
| 89 | { |
| 90 | // capabilities are reported as a list - that is a problem for |
| 91 | // doing a direct comparison, since they change |
| 92 | // We try to work around that using contains() |
| 93 | QStringList defaultCapabilities = QCA::defaultFeatures(); |
| 94 | QVERIFY(defaultCapabilities.contains(QStringLiteral("random" ))); |
| 95 | QVERIFY(defaultCapabilities.contains(QStringLiteral("sha1" ))); |
| 96 | QVERIFY(defaultCapabilities.contains(QStringLiteral("md5" ))); |
| 97 | |
| 98 | QStringList capList; |
| 99 | capList << QStringLiteral("random" ) << QStringLiteral("sha1" ); |
| 100 | QCOMPARE(QCA::isSupported(capList), true); |
| 101 | capList.append(QStringLiteral("noSuch" )); |
| 102 | QCOMPARE(QCA::isSupported(capList), false); |
| 103 | capList.clear(); |
| 104 | capList.append(QStringLiteral("noSuch" )); |
| 105 | QCOMPARE(QCA::isSupported(capList), false); |
| 106 | } |
| 107 | |
| 108 | void StaticUnitTest::secureMemory() |
| 109 | { |
| 110 | // this should be reliably true |
| 111 | QCOMPARE(QCA::haveSecureMemory(), true); |
| 112 | } |
| 113 | |
| 114 | QTEST_MAIN(StaticUnitTest) |
| 115 | |
| 116 | #include "staticunittest.moc" |
| 117 | |