File indexing completed on 2024-05-05 08:56:05
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 SymmetricKeyUnitTest : public QObject 0034 { 0035 Q_OBJECT 0036 0037 private Q_SLOTS: 0038 void initTestCase(); 0039 void cleanupTestCase(); 0040 void test1(); 0041 void weakKey_data(); 0042 void weakKey(); 0043 0044 private: 0045 QCA::Initializer *m_init; 0046 }; 0047 0048 void SymmetricKeyUnitTest::initTestCase() 0049 { 0050 m_init = new QCA::Initializer; 0051 } 0052 0053 void SymmetricKeyUnitTest::cleanupTestCase() 0054 { 0055 delete m_init; 0056 } 0057 0058 void SymmetricKeyUnitTest::test1() 0059 { 0060 QCA::SymmetricKey emptyKey; 0061 QCOMPARE(emptyKey.size(), 0); 0062 0063 QCA::SymmetricKey randomKey(10); 0064 QCOMPARE(randomKey.size(), 10); 0065 0066 QByteArray byteArray(10, 'c'); 0067 QCA::SecureArray secureArray(byteArray); 0068 QCA::SymmetricKey keyArray = secureArray; 0069 QCOMPARE(secureArray.size(), 10); 0070 QCOMPARE(keyArray.size(), secureArray.size()); 0071 QCOMPARE(QCA::arrayToHex(keyArray.toByteArray()), QStringLiteral("63636363636363636363")); 0072 QCOMPARE(QCA::arrayToHex(secureArray.toByteArray()), QStringLiteral("63636363636363636363")); 0073 keyArray[3] = 0x00; // test keyArray detaches OK 0074 QCOMPARE(QCA::arrayToHex(keyArray.toByteArray()), QStringLiteral("63636300636363636363")); 0075 QCOMPARE(QCA::arrayToHex(secureArray.toByteArray()), QStringLiteral("63636363636363636363")); 0076 0077 QCA::SymmetricKey anotherKey; 0078 anotherKey = keyArray; 0079 QCOMPARE(QCA::arrayToHex(anotherKey.toByteArray()), QStringLiteral("63636300636363636363")); 0080 QCA::SymmetricKey bigKey(100); 0081 anotherKey = bigKey; 0082 QCOMPARE(anotherKey.size(), 100); 0083 anotherKey = secureArray; 0084 QCOMPARE(QCA::arrayToHex(secureArray.toByteArray()), QStringLiteral("63636363636363636363")); 0085 QCOMPARE(anotherKey.size(), 10); 0086 anotherKey = emptyKey; 0087 QCOMPARE(anotherKey.size(), 0); 0088 } 0089 0090 // These are from the Botan test suite 0091 void SymmetricKeyUnitTest::weakKey_data() 0092 { 0093 QTest::addColumn<QByteArray>("keyText"); 0094 QTest::addColumn<bool>("isWeak"); 0095 0096 QTest::newRow("") << QByteArray("ffffffffffffffff") << true; 0097 QTest::newRow("") << QByteArray("0000000000000000") << true; 0098 QTest::newRow("") << QByteArray("d5d44ff720683d0d") << false; 0099 QTest::newRow("") << QByteArray("d5d44ff720683d0d") << false; 0100 QTest::newRow("") << QByteArray("1046913489980131") << false; 0101 QTest::newRow("") << QByteArray("1007103489988020") << false; 0102 QTest::newRow("") << QByteArray("10071034c8980120") << false; 0103 QTest::newRow("") << QByteArray("1046103489988020") << false; 0104 } 0105 0106 void SymmetricKeyUnitTest::weakKey() 0107 { 0108 QFETCH(QByteArray, keyText); 0109 QFETCH(bool, isWeak); 0110 0111 QCA::SymmetricKey key(QCA::hexToArray(QString::fromLatin1(keyText))); 0112 QCOMPARE(key.isWeakDESKey(), isWeak); 0113 } 0114 0115 QTEST_MAIN(SymmetricKeyUnitTest) 0116 0117 #include "symmetrickeyunittest.moc"