File indexing completed on 2025-04-20 07:24:36
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 StaticUnitTest : public QObject 0034 { 0035 Q_OBJECT 0036 0037 private Q_SLOTS: 0038 void initTestCase(); 0039 void cleanupTestCase(); 0040 void hexConversions(); 0041 void capabilities(); 0042 void secureMemory(); 0043 0044 private: 0045 QCA::Initializer *m_init; 0046 }; 0047 0048 void StaticUnitTest::initTestCase() 0049 { 0050 m_init = new QCA::Initializer; 0051 } 0052 0053 void StaticUnitTest::cleanupTestCase() 0054 { 0055 delete m_init; 0056 } 0057 0058 void StaticUnitTest::hexConversions() 0059 { 0060 QByteArray test(10, 'a'); 0061 0062 QCOMPARE(QCA::arrayToHex(test), QStringLiteral("61616161616161616161")); 0063 0064 test.fill('b'); 0065 test[7] = 0x00; 0066 0067 QCOMPARE(test == QCA::hexToArray(QStringLiteral("62626262626262006262")), true); 0068 0069 QCA::SecureArray testArray(10); 0070 // testArray.fill( 'a' ); 0071 for (int i = 0; i < testArray.size(); i++) { 0072 testArray[i] = 0x61; 0073 } 0074 QCOMPARE(QCA::arrayToHex(testArray.toByteArray()), QStringLiteral("61616161616161616161")); 0075 // testArray.fill( 'b' ); 0076 for (int i = 0; i < testArray.size(); i++) { 0077 testArray[i] = 0x62; 0078 } 0079 testArray[6] = 0x00; 0080 QCOMPARE(testArray == QCA::hexToArray(QStringLiteral("62626262626200626262")), true); 0081 0082 QCOMPARE(testArray == QCA::hexToArray(QCA::arrayToHex(testArray.toByteArray())), true); 0083 0084 testArray[9] = 0x00; 0085 QCOMPARE(testArray == QCA::hexToArray(QCA::arrayToHex(testArray.toByteArray())), true); 0086 } 0087 0088 void StaticUnitTest::capabilities() 0089 { 0090 // capabilities are reported as a list - that is a problem for 0091 // doing a direct comparison, since they change 0092 // We try to work around that using contains() 0093 QStringList defaultCapabilities = QCA::defaultFeatures(); 0094 QVERIFY(defaultCapabilities.contains(QStringLiteral("random"))); 0095 QVERIFY(defaultCapabilities.contains(QStringLiteral("sha1"))); 0096 QVERIFY(defaultCapabilities.contains(QStringLiteral("md5"))); 0097 0098 QStringList capList; 0099 capList << QStringLiteral("random") << QStringLiteral("sha1"); 0100 QCOMPARE(QCA::isSupported(capList), true); 0101 capList.append(QStringLiteral("noSuch")); 0102 QCOMPARE(QCA::isSupported(capList), false); 0103 capList.clear(); 0104 capList.append(QStringLiteral("noSuch")); 0105 QCOMPARE(QCA::isSupported(capList), false); 0106 } 0107 0108 void StaticUnitTest::secureMemory() 0109 { 0110 // this should be reliably true 0111 QCOMPARE(QCA::haveSecureMemory(), true); 0112 } 0113 0114 QTEST_MAIN(StaticUnitTest) 0115 0116 #include "staticunittest.moc"