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 <QtCrypto> |
27 | #include <QtTest/QtTest> |
28 | |
29 | #ifdef QT_STATICPLUGIN |
30 | #include "import_plugins.h" |
31 | #endif |
32 | |
33 | class SymmetricKeyUnitTest : public QObject |
34 | { |
35 | Q_OBJECT |
36 | |
37 | private Q_SLOTS: |
38 | void initTestCase(); |
39 | void cleanupTestCase(); |
40 | void test1(); |
41 | void weakKey_data(); |
42 | void weakKey(); |
43 | |
44 | private: |
45 | QCA::Initializer *m_init; |
46 | }; |
47 | |
48 | void SymmetricKeyUnitTest::initTestCase() |
49 | { |
50 | m_init = new QCA::Initializer; |
51 | } |
52 | |
53 | void SymmetricKeyUnitTest::cleanupTestCase() |
54 | { |
55 | delete m_init; |
56 | } |
57 | |
58 | void SymmetricKeyUnitTest::test1() |
59 | { |
60 | QCA::SymmetricKey emptyKey; |
61 | QCOMPARE(emptyKey.size(), 0); |
62 | |
63 | QCA::SymmetricKey randomKey(10); |
64 | QCOMPARE(randomKey.size(), 10); |
65 | |
66 | QByteArray byteArray(10, 'c'); |
67 | QCA::SecureArray secureArray(byteArray); |
68 | QCA::SymmetricKey keyArray = secureArray; |
69 | QCOMPARE(secureArray.size(), 10); |
70 | QCOMPARE(keyArray.size(), secureArray.size()); |
71 | QCOMPARE(QCA::arrayToHex(keyArray.toByteArray()), QStringLiteral("63636363636363636363" )); |
72 | QCOMPARE(QCA::arrayToHex(secureArray.toByteArray()), QStringLiteral("63636363636363636363" )); |
73 | keyArray[3] = 0x00; // test keyArray detaches OK |
74 | QCOMPARE(QCA::arrayToHex(keyArray.toByteArray()), QStringLiteral("63636300636363636363" )); |
75 | QCOMPARE(QCA::arrayToHex(secureArray.toByteArray()), QStringLiteral("63636363636363636363" )); |
76 | |
77 | QCA::SymmetricKey anotherKey; |
78 | anotherKey = keyArray; |
79 | QCOMPARE(QCA::arrayToHex(anotherKey.toByteArray()), QStringLiteral("63636300636363636363" )); |
80 | QCA::SymmetricKey bigKey(100); |
81 | anotherKey = bigKey; |
82 | QCOMPARE(anotherKey.size(), 100); |
83 | anotherKey = secureArray; |
84 | QCOMPARE(QCA::arrayToHex(secureArray.toByteArray()), QStringLiteral("63636363636363636363" )); |
85 | QCOMPARE(anotherKey.size(), 10); |
86 | anotherKey = emptyKey; |
87 | QCOMPARE(anotherKey.size(), 0); |
88 | } |
89 | |
90 | // These are from the Botan test suite |
91 | void SymmetricKeyUnitTest::weakKey_data() |
92 | { |
93 | QTest::addColumn<QByteArray>(name: "keyText" ); |
94 | QTest::addColumn<bool>(name: "isWeak" ); |
95 | |
96 | QTest::newRow(dataTag: "" ) << QByteArray("ffffffffffffffff" ) << true; |
97 | QTest::newRow(dataTag: "" ) << QByteArray("0000000000000000" ) << true; |
98 | QTest::newRow(dataTag: "" ) << QByteArray("d5d44ff720683d0d" ) << false; |
99 | QTest::newRow(dataTag: "" ) << QByteArray("d5d44ff720683d0d" ) << false; |
100 | QTest::newRow(dataTag: "" ) << QByteArray("1046913489980131" ) << false; |
101 | QTest::newRow(dataTag: "" ) << QByteArray("1007103489988020" ) << false; |
102 | QTest::newRow(dataTag: "" ) << QByteArray("10071034c8980120" ) << false; |
103 | QTest::newRow(dataTag: "" ) << QByteArray("1046103489988020" ) << false; |
104 | } |
105 | |
106 | void SymmetricKeyUnitTest::weakKey() |
107 | { |
108 | QFETCH(QByteArray, keyText); |
109 | QFETCH(bool, isWeak); |
110 | |
111 | QCA::SymmetricKey key(QCA::hexToArray(hexString: QString::fromLatin1(ba: keyText))); |
112 | QCOMPARE(key.isWeakDESKey(), isWeak); |
113 | } |
114 | |
115 | QTEST_MAIN(SymmetricKeyUnitTest) |
116 | |
117 | #include "symmetrickeyunittest.moc" |
118 | |