File indexing completed on 2024-04-28 04:44:11
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 SecureArrayUnitTest : public QObject 0034 { 0035 Q_OBJECT 0036 0037 private Q_SLOTS: 0038 void initTestCase(); 0039 void cleanupTestCase(); 0040 void testAll(); 0041 0042 private: 0043 QCA::Initializer *m_init; 0044 }; 0045 0046 void SecureArrayUnitTest::initTestCase() 0047 { 0048 m_init = new QCA::Initializer; 0049 } 0050 0051 void SecureArrayUnitTest::cleanupTestCase() 0052 { 0053 delete m_init; 0054 } 0055 0056 void SecureArrayUnitTest::testAll() 0057 { 0058 QCA::SecureArray emptyArray; 0059 QCOMPARE(emptyArray.size(), 0); 0060 QVERIFY(emptyArray.isEmpty()); 0061 0062 QCA::SecureArray testArray(10); 0063 QCOMPARE(testArray.size(), 10); 0064 QCOMPARE(testArray.isEmpty(), false); 0065 0066 QCA::SecureArray testArray64(64); 0067 QCOMPARE(testArray64.size(), 64); 0068 QCOMPARE(testArray64.isEmpty(), false); 0069 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 0076 testArray.fill('b'); 0077 testArray[7] = 0x00; 0078 QCOMPARE(QCA::arrayToHex(testArray.toByteArray()), QStringLiteral("62626262626262006262")); 0079 0080 QByteArray byteArray(10, 'c'); 0081 QCA::SecureArray secureArray(byteArray); 0082 QCOMPARE(secureArray.size(), 10); 0083 QCOMPARE(QCA::arrayToHex(secureArray.toByteArray()), QStringLiteral("63636363636363636363")); 0084 byteArray.fill('d'); 0085 // it should be a copy, so no effect 0086 QCOMPARE(QCA::arrayToHex(secureArray.toByteArray()), QStringLiteral("63636363636363636363")); 0087 0088 QCA::SecureArray copyArray(secureArray); 0089 QCOMPARE(QCA::arrayToHex(copyArray.toByteArray()), QStringLiteral("63636363636363636363")); 0090 copyArray.fill(0x64); 0091 QCOMPARE(QCA::arrayToHex(copyArray.toByteArray()), QStringLiteral("64646464646464646464")); 0092 QCOMPARE(QCA::arrayToHex(secureArray.toByteArray()), QStringLiteral("63636363636363636363")); 0093 0094 // test for detaching 0095 QCA::SecureArray detachArray1 = secureArray; // currently the same 0096 QCOMPARE(QCA::arrayToHex(detachArray1.toByteArray()), QStringLiteral("63636363636363636363")); 0097 for (int i = 0; i < detachArray1.size(); i++) { 0098 detachArray1[i] = 0x66; // implicit detach 0099 } 0100 QCOMPARE(QCA::arrayToHex(secureArray.toByteArray()), QStringLiteral("63636363636363636363")); 0101 QCOMPARE(QCA::arrayToHex(detachArray1.toByteArray()), QStringLiteral("66666666666666666666")); 0102 0103 QCA::SecureArray detachArray2 = secureArray; // currently the same 0104 QCOMPARE(QCA::arrayToHex(detachArray2.toByteArray()), QStringLiteral("63636363636363636363")); 0105 // implicit detach 0106 for (int i = 0; i < detachArray2.size(); i++) { 0107 detachArray2.data()[i] = 0x67; 0108 } 0109 QCOMPARE(QCA::arrayToHex(secureArray.toByteArray()), QStringLiteral("63636363636363636363")); 0110 QCOMPARE(QCA::arrayToHex(detachArray2.toByteArray()), QStringLiteral("67676767676767676767")); 0111 0112 QCA::SecureArray detachArray3 = secureArray; // implicitly shared copy 0113 QCOMPARE(QCA::arrayToHex(detachArray3.toByteArray()), QStringLiteral("63636363636363636363")); 0114 for (int i = 0; i < detachArray3.size(); i++) { 0115 detachArray3.data()[i] = 0x68; 0116 } 0117 QCOMPARE(QCA::arrayToHex(secureArray.toByteArray()), QStringLiteral("63636363636363636363")); 0118 QCOMPARE(QCA::arrayToHex(detachArray3.toByteArray()), QStringLiteral("68686868686868686868")); 0119 0120 // test for resizing 0121 QCA::SecureArray resizeArray = emptyArray; 0122 QCOMPARE(resizeArray.size(), 0); 0123 resizeArray.resize(20); 0124 QCOMPARE(resizeArray.size(), 20); 0125 resizeArray.resize(40); 0126 QCOMPARE(resizeArray.size(), 40); 0127 resizeArray.resize(10); 0128 QCOMPARE(resizeArray.size(), 10); 0129 0130 // test for append 0131 QCA::SecureArray appendArray = secureArray; 0132 appendArray.append(QCA::SecureArray()); 0133 QCOMPARE(QCA::arrayToHex(secureArray.toByteArray()), QCA::arrayToHex(appendArray.toByteArray())); 0134 appendArray.append(secureArray); 0135 QCOMPARE(QCA::arrayToHex(secureArray.toByteArray()), QStringLiteral("63636363636363636363")); 0136 QCOMPARE(QCA::arrayToHex(appendArray.toByteArray()), QStringLiteral("6363636363636363636363636363636363636363")); 0137 QCA::SecureArray appendArray2 = secureArray; 0138 QCOMPARE(QCA::arrayToHex(appendArray2.append(secureArray).toByteArray()), 0139 QStringLiteral("6363636363636363636363636363636363636363")); 0140 0141 // test for a possible problem with operator[] 0142 QVERIFY((secureArray[0] == (char)0x63)); 0143 } 0144 0145 QTEST_MAIN(SecureArrayUnitTest) 0146 0147 #include "securearrayunittest.moc"