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
33class SecureArrayUnitTest : public QObject
34{
35 Q_OBJECT
36
37private Q_SLOTS:
38 void initTestCase();
39 void cleanupTestCase();
40 void testAll();
41
42private:
43 QCA::Initializer *m_init;
44};
45
46void SecureArrayUnitTest::initTestCase()
47{
48 m_init = new QCA::Initializer;
49}
50
51void SecureArrayUnitTest::cleanupTestCase()
52{
53 delete m_init;
54}
55
56void SecureArrayUnitTest::testAll()
57{
58 QCA::SecureArray emptyArray;
59 QCOMPARE(emptyArray.size(), 0);
60 QVERIFY(emptyArray.isEmpty());
61
62 QCA::SecureArray testArray(10);
63 QCOMPARE(testArray.size(), 10);
64 QCOMPARE(testArray.isEmpty(), false);
65
66 QCA::SecureArray testArray64(64);
67 QCOMPARE(testArray64.size(), 64);
68 QCOMPARE(testArray64.isEmpty(), false);
69
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
76 testArray.fill(fillChar: 'b');
77 testArray[7] = 0x00;
78 QCOMPARE(QCA::arrayToHex(testArray.toByteArray()), QStringLiteral("62626262626262006262"));
79
80 QByteArray byteArray(10, 'c');
81 QCA::SecureArray secureArray(byteArray);
82 QCOMPARE(secureArray.size(), 10);
83 QCOMPARE(QCA::arrayToHex(secureArray.toByteArray()), QStringLiteral("63636363636363636363"));
84 byteArray.fill(c: 'd');
85 // it should be a copy, so no effect
86 QCOMPARE(QCA::arrayToHex(secureArray.toByteArray()), QStringLiteral("63636363636363636363"));
87
88 QCA::SecureArray copyArray(secureArray);
89 QCOMPARE(QCA::arrayToHex(copyArray.toByteArray()), QStringLiteral("63636363636363636363"));
90 copyArray.fill(fillChar: 0x64);
91 QCOMPARE(QCA::arrayToHex(copyArray.toByteArray()), QStringLiteral("64646464646464646464"));
92 QCOMPARE(QCA::arrayToHex(secureArray.toByteArray()), QStringLiteral("63636363636363636363"));
93
94 // test for detaching
95 QCA::SecureArray detachArray1 = secureArray; // currently the same
96 QCOMPARE(QCA::arrayToHex(detachArray1.toByteArray()), QStringLiteral("63636363636363636363"));
97 for (int i = 0; i < detachArray1.size(); i++) {
98 detachArray1[i] = 0x66; // implicit detach
99 }
100 QCOMPARE(QCA::arrayToHex(secureArray.toByteArray()), QStringLiteral("63636363636363636363"));
101 QCOMPARE(QCA::arrayToHex(detachArray1.toByteArray()), QStringLiteral("66666666666666666666"));
102
103 QCA::SecureArray detachArray2 = secureArray; // currently the same
104 QCOMPARE(QCA::arrayToHex(detachArray2.toByteArray()), QStringLiteral("63636363636363636363"));
105 // implicit detach
106 for (int i = 0; i < detachArray2.size(); i++) {
107 detachArray2.data()[i] = 0x67;
108 }
109 QCOMPARE(QCA::arrayToHex(secureArray.toByteArray()), QStringLiteral("63636363636363636363"));
110 QCOMPARE(QCA::arrayToHex(detachArray2.toByteArray()), QStringLiteral("67676767676767676767"));
111
112 QCA::SecureArray detachArray3 = secureArray; // implicitly shared copy
113 QCOMPARE(QCA::arrayToHex(detachArray3.toByteArray()), QStringLiteral("63636363636363636363"));
114 for (int i = 0; i < detachArray3.size(); i++) {
115 detachArray3.data()[i] = 0x68;
116 }
117 QCOMPARE(QCA::arrayToHex(secureArray.toByteArray()), QStringLiteral("63636363636363636363"));
118 QCOMPARE(QCA::arrayToHex(detachArray3.toByteArray()), QStringLiteral("68686868686868686868"));
119
120 // test for resizing
121 QCA::SecureArray resizeArray = emptyArray;
122 QCOMPARE(resizeArray.size(), 0);
123 resizeArray.resize(size: 20);
124 QCOMPARE(resizeArray.size(), 20);
125 resizeArray.resize(size: 40);
126 QCOMPARE(resizeArray.size(), 40);
127 resizeArray.resize(size: 10);
128 QCOMPARE(resizeArray.size(), 10);
129
130 // test for append
131 QCA::SecureArray appendArray = secureArray;
132 appendArray.append(a: QCA::SecureArray());
133 QCOMPARE(QCA::arrayToHex(secureArray.toByteArray()), QCA::arrayToHex(appendArray.toByteArray()));
134 appendArray.append(a: secureArray);
135 QCOMPARE(QCA::arrayToHex(secureArray.toByteArray()), QStringLiteral("63636363636363636363"));
136 QCOMPARE(QCA::arrayToHex(appendArray.toByteArray()), QStringLiteral("6363636363636363636363636363636363636363"));
137 QCA::SecureArray appendArray2 = secureArray;
138 QCOMPARE(QCA::arrayToHex(appendArray2.append(secureArray).toByteArray()),
139 QStringLiteral("6363636363636363636363636363636363636363"));
140
141 // test for a possible problem with operator[]
142 QVERIFY((secureArray[0] == (char)0x63));
143}
144
145QTEST_MAIN(SecureArrayUnitTest)
146
147#include "securearrayunittest.moc"
148

source code of qca/unittest/securearrayunittest/securearrayunittest.cpp