2004-11-05 03:10:59 +00:00
|
|
|
/**
|
2006-10-12 05:40:09 +00:00
|
|
|
* Copyright (C) 2004-2006 Brad Hards <bradh@frogmouth.net>
|
2004-11-05 03:10:59 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2006-10-12 05:40:09 +00:00
|
|
|
|
2005-01-01 05:11:34 +00:00
|
|
|
#include <QtCrypto>
|
2006-10-12 05:40:09 +00:00
|
|
|
#include <QtTest/QtTest>
|
|
|
|
|
2014-01-02 03:50:18 +06:00
|
|
|
#ifdef QT_STATICPLUGIN
|
|
|
|
#include "import_plugins.h"
|
|
|
|
#endif
|
|
|
|
|
2006-10-12 05:40:09 +00:00
|
|
|
class SecureArrayUnitTest : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
2020-01-22 19:31:28 +01:00
|
|
|
private Q_SLOTS:
|
2006-10-12 05:40:09 +00:00
|
|
|
void initTestCase();
|
|
|
|
void cleanupTestCase();
|
|
|
|
void testAll();
|
|
|
|
|
|
|
|
private:
|
|
|
|
QCA::Initializer* m_init;
|
|
|
|
};
|
|
|
|
|
2004-11-05 03:10:59 +00:00
|
|
|
|
2005-12-23 06:05:33 +00:00
|
|
|
void SecureArrayUnitTest::initTestCase()
|
2004-11-05 03:10:59 +00:00
|
|
|
{
|
2005-12-23 06:05:33 +00:00
|
|
|
m_init = new QCA::Initializer;
|
2004-11-05 03:10:59 +00:00
|
|
|
}
|
|
|
|
|
2005-12-23 06:05:33 +00:00
|
|
|
void SecureArrayUnitTest::cleanupTestCase()
|
2004-11-05 03:10:59 +00:00
|
|
|
{
|
2005-12-23 06:05:33 +00:00
|
|
|
delete m_init;
|
|
|
|
}
|
2004-11-05 03:10:59 +00:00
|
|
|
|
2005-12-23 06:05:33 +00:00
|
|
|
|
|
|
|
void SecureArrayUnitTest::testAll()
|
|
|
|
{
|
2007-04-13 19:19:18 +00:00
|
|
|
QCA::SecureArray emptyArray;
|
2005-12-23 06:05:33 +00:00
|
|
|
QCOMPARE( emptyArray.size(), 0 );
|
|
|
|
QVERIFY( emptyArray.isEmpty() );
|
2004-11-05 03:10:59 +00:00
|
|
|
|
2007-04-13 19:19:18 +00:00
|
|
|
QCA::SecureArray testArray(10);
|
2005-12-23 06:05:33 +00:00
|
|
|
QCOMPARE( testArray.size(), 10 );
|
|
|
|
QCOMPARE( testArray.isEmpty(), false );
|
2004-11-05 09:41:32 +00:00
|
|
|
|
2007-04-13 19:19:18 +00:00
|
|
|
QCA::SecureArray testArray64(64);
|
2005-12-23 06:05:33 +00:00
|
|
|
QCOMPARE( testArray64.size(), 64 );
|
|
|
|
QCOMPARE( testArray64.isEmpty(), false );
|
2004-12-19 06:59:24 +00:00
|
|
|
|
2004-11-05 09:41:32 +00:00
|
|
|
//testArray.fill( 'a' );
|
Big fixup of the unit test harness, and each unit test case, to match
the new Qt4 stuff, and the removal of QCString from the API
When I run this, I get:
# Running normal tests... #
Base64UnitTest - 22 tests passed, 0 tests failed
BigIntUnitTest - 252 tests passed, 0 tests failed
CertUnitTest - 0 tests passed, 1 test failed
Unexpected failure:
certunittest.cpp[40]: failed on "QCA::haveSystemStore()"
result = '0', expected = '1'
CipherUnitTest - 2740 tests passed, 0 tests failed
HashUnitTest - 186 tests passed, 0 tests failed
HexUnitTest - 17 tests passed, 0 tests failed
KDFUnitTest - 0 tests passed, 0 tests failed; also 2 skipped
Skipped tests:
kdfunittest.cpp[80]: "PBKDF version 1 with SHA1 not supported"
kdfunittest.cpp[98]: "PBKDF version 2 with SHA1 not supported"
KeyLengthUnitTest - 9 tests passed, 0 tests failed
MACUnitTest - 33 tests passed, 0 tests failed
RandomUnitTest - 67 tests passed, 0 tests failed
SecureArrayUnitTest - 32 tests passed, 0 tests failed
StaticUnitTest - 31 tests passed, 0 tests failed
SymmetricKeyUnitTest - 13 tests passed, 0 tests failed
# Done with normal tests:
Total test cases: 13
Total test steps : 3405
Total passed test steps (including unexpected) : 3402
Total unexpected passed test steps : 0
Total failed test steps (including expected) : 1
Total expected failed test steps : 0
Total skipped test steps : 2
CCMAIL: delta-affinix.com@lists.affinix.com
svn path=/trunk/kdesupport/qca/; revision=394072
2005-03-01 11:15:25 +00:00
|
|
|
for (int i = 0; i < testArray.size(); i++) {
|
2004-11-05 09:41:32 +00:00
|
|
|
testArray[ i ] = 0x61;
|
|
|
|
}
|
2020-02-10 00:29:23 +01:00
|
|
|
QCOMPARE( QCA::arrayToHex( testArray.toByteArray() ), QStringLiteral( "61616161616161616161" ) );
|
2004-12-19 06:59:24 +00:00
|
|
|
|
|
|
|
testArray.fill( 'b' );
|
2004-11-05 09:41:32 +00:00
|
|
|
testArray[7] = 0x00;
|
2020-02-10 00:29:23 +01:00
|
|
|
QCOMPARE( QCA::arrayToHex( testArray.toByteArray() ), QStringLiteral( "62626262626262006262" ) );
|
2004-11-05 09:41:32 +00:00
|
|
|
|
Big fixup of the unit test harness, and each unit test case, to match
the new Qt4 stuff, and the removal of QCString from the API
When I run this, I get:
# Running normal tests... #
Base64UnitTest - 22 tests passed, 0 tests failed
BigIntUnitTest - 252 tests passed, 0 tests failed
CertUnitTest - 0 tests passed, 1 test failed
Unexpected failure:
certunittest.cpp[40]: failed on "QCA::haveSystemStore()"
result = '0', expected = '1'
CipherUnitTest - 2740 tests passed, 0 tests failed
HashUnitTest - 186 tests passed, 0 tests failed
HexUnitTest - 17 tests passed, 0 tests failed
KDFUnitTest - 0 tests passed, 0 tests failed; also 2 skipped
Skipped tests:
kdfunittest.cpp[80]: "PBKDF version 1 with SHA1 not supported"
kdfunittest.cpp[98]: "PBKDF version 2 with SHA1 not supported"
KeyLengthUnitTest - 9 tests passed, 0 tests failed
MACUnitTest - 33 tests passed, 0 tests failed
RandomUnitTest - 67 tests passed, 0 tests failed
SecureArrayUnitTest - 32 tests passed, 0 tests failed
StaticUnitTest - 31 tests passed, 0 tests failed
SymmetricKeyUnitTest - 13 tests passed, 0 tests failed
# Done with normal tests:
Total test cases: 13
Total test steps : 3405
Total passed test steps (including unexpected) : 3402
Total unexpected passed test steps : 0
Total failed test steps (including expected) : 1
Total expected failed test steps : 0
Total skipped test steps : 2
CCMAIL: delta-affinix.com@lists.affinix.com
svn path=/trunk/kdesupport/qca/; revision=394072
2005-03-01 11:15:25 +00:00
|
|
|
QByteArray byteArray(10, 'c');
|
2007-04-13 19:19:18 +00:00
|
|
|
QCA::SecureArray secureArray( byteArray );
|
2005-12-23 06:05:33 +00:00
|
|
|
QCOMPARE( secureArray.size(), 10 );
|
2020-02-10 00:29:23 +01:00
|
|
|
QCOMPARE( QCA::arrayToHex ( secureArray.toByteArray() ), QStringLiteral( "63636363636363636363" ) );
|
2004-11-05 09:41:32 +00:00
|
|
|
byteArray.fill( 'd' );
|
|
|
|
// it should be a copy, so no effect
|
2020-02-10 00:29:23 +01:00
|
|
|
QCOMPARE( QCA::arrayToHex ( secureArray.toByteArray() ), QStringLiteral( "63636363636363636363" ) );
|
2004-11-05 09:41:32 +00:00
|
|
|
|
2007-04-13 19:19:18 +00:00
|
|
|
QCA::SecureArray copyArray( secureArray );
|
2020-02-10 00:29:23 +01:00
|
|
|
QCOMPARE( QCA::arrayToHex ( copyArray.toByteArray() ), QStringLiteral( "63636363636363636363" ) );
|
2004-12-19 06:59:24 +00:00
|
|
|
copyArray.fill(0x64);
|
2020-02-10 00:29:23 +01:00
|
|
|
QCOMPARE( QCA::arrayToHex ( copyArray.toByteArray() ), QStringLiteral( "64646464646464646464" ) );
|
|
|
|
QCOMPARE( QCA::arrayToHex ( secureArray.toByteArray() ), QStringLiteral( "63636363636363636363" ) );
|
2004-11-05 09:41:32 +00:00
|
|
|
|
|
|
|
// test for detaching
|
2007-04-13 19:19:18 +00:00
|
|
|
QCA::SecureArray detachArray1 = secureArray; // currently the same
|
2020-02-10 00:29:23 +01:00
|
|
|
QCOMPARE( QCA::arrayToHex ( detachArray1.toByteArray() ), QStringLiteral( "63636363636363636363" ) );
|
Big fixup of the unit test harness, and each unit test case, to match
the new Qt4 stuff, and the removal of QCString from the API
When I run this, I get:
# Running normal tests... #
Base64UnitTest - 22 tests passed, 0 tests failed
BigIntUnitTest - 252 tests passed, 0 tests failed
CertUnitTest - 0 tests passed, 1 test failed
Unexpected failure:
certunittest.cpp[40]: failed on "QCA::haveSystemStore()"
result = '0', expected = '1'
CipherUnitTest - 2740 tests passed, 0 tests failed
HashUnitTest - 186 tests passed, 0 tests failed
HexUnitTest - 17 tests passed, 0 tests failed
KDFUnitTest - 0 tests passed, 0 tests failed; also 2 skipped
Skipped tests:
kdfunittest.cpp[80]: "PBKDF version 1 with SHA1 not supported"
kdfunittest.cpp[98]: "PBKDF version 2 with SHA1 not supported"
KeyLengthUnitTest - 9 tests passed, 0 tests failed
MACUnitTest - 33 tests passed, 0 tests failed
RandomUnitTest - 67 tests passed, 0 tests failed
SecureArrayUnitTest - 32 tests passed, 0 tests failed
StaticUnitTest - 31 tests passed, 0 tests failed
SymmetricKeyUnitTest - 13 tests passed, 0 tests failed
# Done with normal tests:
Total test cases: 13
Total test steps : 3405
Total passed test steps (including unexpected) : 3402
Total unexpected passed test steps : 0
Total failed test steps (including expected) : 1
Total expected failed test steps : 0
Total skipped test steps : 2
CCMAIL: delta-affinix.com@lists.affinix.com
svn path=/trunk/kdesupport/qca/; revision=394072
2005-03-01 11:15:25 +00:00
|
|
|
for (int i = 0; i < detachArray1.size(); i++) {
|
2004-11-05 09:41:32 +00:00
|
|
|
detachArray1[i] = 0x66; // implicit detach
|
|
|
|
}
|
2020-02-10 00:29:23 +01:00
|
|
|
QCOMPARE( QCA::arrayToHex ( secureArray.toByteArray() ), QStringLiteral( "63636363636363636363" ) );
|
|
|
|
QCOMPARE( QCA::arrayToHex ( detachArray1.toByteArray() ), QStringLiteral( "66666666666666666666" ) );
|
2004-11-05 09:41:32 +00:00
|
|
|
|
2007-04-13 19:19:18 +00:00
|
|
|
QCA::SecureArray detachArray2 = secureArray; // currently the same
|
2020-02-10 00:29:23 +01:00
|
|
|
QCOMPARE( QCA::arrayToHex ( detachArray2.toByteArray() ), QStringLiteral( "63636363636363636363" ) );
|
Big fixup of the unit test harness, and each unit test case, to match
the new Qt4 stuff, and the removal of QCString from the API
When I run this, I get:
# Running normal tests... #
Base64UnitTest - 22 tests passed, 0 tests failed
BigIntUnitTest - 252 tests passed, 0 tests failed
CertUnitTest - 0 tests passed, 1 test failed
Unexpected failure:
certunittest.cpp[40]: failed on "QCA::haveSystemStore()"
result = '0', expected = '1'
CipherUnitTest - 2740 tests passed, 0 tests failed
HashUnitTest - 186 tests passed, 0 tests failed
HexUnitTest - 17 tests passed, 0 tests failed
KDFUnitTest - 0 tests passed, 0 tests failed; also 2 skipped
Skipped tests:
kdfunittest.cpp[80]: "PBKDF version 1 with SHA1 not supported"
kdfunittest.cpp[98]: "PBKDF version 2 with SHA1 not supported"
KeyLengthUnitTest - 9 tests passed, 0 tests failed
MACUnitTest - 33 tests passed, 0 tests failed
RandomUnitTest - 67 tests passed, 0 tests failed
SecureArrayUnitTest - 32 tests passed, 0 tests failed
StaticUnitTest - 31 tests passed, 0 tests failed
SymmetricKeyUnitTest - 13 tests passed, 0 tests failed
# Done with normal tests:
Total test cases: 13
Total test steps : 3405
Total passed test steps (including unexpected) : 3402
Total unexpected passed test steps : 0
Total failed test steps (including expected) : 1
Total expected failed test steps : 0
Total skipped test steps : 2
CCMAIL: delta-affinix.com@lists.affinix.com
svn path=/trunk/kdesupport/qca/; revision=394072
2005-03-01 11:15:25 +00:00
|
|
|
//implicit detach
|
|
|
|
for (int i = 0; i < detachArray2.size(); i++) {
|
2006-10-12 05:40:09 +00:00
|
|
|
detachArray2.data()[i] = 0x67;
|
2004-11-05 09:41:32 +00:00
|
|
|
}
|
2020-02-10 00:29:23 +01:00
|
|
|
QCOMPARE( QCA::arrayToHex ( secureArray.toByteArray() ), QStringLiteral( "63636363636363636363" ) );
|
|
|
|
QCOMPARE( QCA::arrayToHex ( detachArray2.toByteArray() ), QStringLiteral( "67676767676767676767" ) );
|
2004-11-05 09:41:32 +00:00
|
|
|
|
2007-04-13 19:19:18 +00:00
|
|
|
QCA::SecureArray detachArray3 = secureArray; // implicitly shared copy
|
2020-02-10 00:29:23 +01:00
|
|
|
QCOMPARE( QCA::arrayToHex ( detachArray3.toByteArray() ), QStringLiteral( "63636363636363636363" ) );
|
Big fixup of the unit test harness, and each unit test case, to match
the new Qt4 stuff, and the removal of QCString from the API
When I run this, I get:
# Running normal tests... #
Base64UnitTest - 22 tests passed, 0 tests failed
BigIntUnitTest - 252 tests passed, 0 tests failed
CertUnitTest - 0 tests passed, 1 test failed
Unexpected failure:
certunittest.cpp[40]: failed on "QCA::haveSystemStore()"
result = '0', expected = '1'
CipherUnitTest - 2740 tests passed, 0 tests failed
HashUnitTest - 186 tests passed, 0 tests failed
HexUnitTest - 17 tests passed, 0 tests failed
KDFUnitTest - 0 tests passed, 0 tests failed; also 2 skipped
Skipped tests:
kdfunittest.cpp[80]: "PBKDF version 1 with SHA1 not supported"
kdfunittest.cpp[98]: "PBKDF version 2 with SHA1 not supported"
KeyLengthUnitTest - 9 tests passed, 0 tests failed
MACUnitTest - 33 tests passed, 0 tests failed
RandomUnitTest - 67 tests passed, 0 tests failed
SecureArrayUnitTest - 32 tests passed, 0 tests failed
StaticUnitTest - 31 tests passed, 0 tests failed
SymmetricKeyUnitTest - 13 tests passed, 0 tests failed
# Done with normal tests:
Total test cases: 13
Total test steps : 3405
Total passed test steps (including unexpected) : 3402
Total unexpected passed test steps : 0
Total failed test steps (including expected) : 1
Total expected failed test steps : 0
Total skipped test steps : 2
CCMAIL: delta-affinix.com@lists.affinix.com
svn path=/trunk/kdesupport/qca/; revision=394072
2005-03-01 11:15:25 +00:00
|
|
|
for (int i = 0; i < detachArray3.size(); i++) {
|
2006-10-12 05:40:09 +00:00
|
|
|
detachArray3.data()[i] = 0x68;
|
2004-11-05 09:41:32 +00:00
|
|
|
}
|
2020-02-10 00:29:23 +01:00
|
|
|
QCOMPARE( QCA::arrayToHex ( secureArray.toByteArray() ), QStringLiteral( "63636363636363636363" ) );
|
|
|
|
QCOMPARE( QCA::arrayToHex ( detachArray3.toByteArray() ), QStringLiteral( "68686868686868686868" ) );
|
2004-11-05 09:41:32 +00:00
|
|
|
|
2004-11-05 11:50:35 +00:00
|
|
|
|
|
|
|
// test for resizing
|
2007-04-13 19:19:18 +00:00
|
|
|
QCA::SecureArray resizeArray = emptyArray;
|
2005-12-23 06:05:33 +00:00
|
|
|
QCOMPARE( resizeArray.size(), 0 );
|
2004-11-05 11:50:35 +00:00
|
|
|
resizeArray.resize(20);
|
2005-12-23 06:05:33 +00:00
|
|
|
QCOMPARE( resizeArray.size(), 20 );
|
2004-11-05 11:50:35 +00:00
|
|
|
resizeArray.resize(40);
|
2005-12-23 06:05:33 +00:00
|
|
|
QCOMPARE( resizeArray.size(), 40 );
|
2004-11-05 11:50:35 +00:00
|
|
|
resizeArray.resize(10);
|
2005-12-23 06:05:33 +00:00
|
|
|
QCOMPARE( resizeArray.size(), 10 );
|
2004-11-14 06:13:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
// test for append
|
2007-04-13 19:19:18 +00:00
|
|
|
QCA::SecureArray appendArray = secureArray;
|
|
|
|
appendArray.append( QCA::SecureArray() );
|
2007-06-13 02:30:43 +00:00
|
|
|
QCOMPARE( QCA::arrayToHex( secureArray.toByteArray() ), QCA::arrayToHex( appendArray.toByteArray() ) );
|
2004-11-14 06:13:11 +00:00
|
|
|
appendArray.append( secureArray );
|
2020-02-10 00:29:23 +01:00
|
|
|
QCOMPARE( QCA::arrayToHex ( secureArray.toByteArray() ), QStringLiteral( "63636363636363636363" ) );
|
|
|
|
QCOMPARE( QCA::arrayToHex ( appendArray.toByteArray() ), QStringLiteral( "6363636363636363636363636363636363636363" ) );
|
2007-04-13 19:19:18 +00:00
|
|
|
QCA::SecureArray appendArray2 = secureArray;
|
2020-02-10 00:29:23 +01:00
|
|
|
QCOMPARE( QCA::arrayToHex ( appendArray2.append(secureArray).toByteArray() ), QStringLiteral( "6363636363636363636363636363636363636363" ) );
|
2004-12-19 06:59:24 +00:00
|
|
|
|
|
|
|
// test for a possible problem with operator[]
|
2005-12-23 06:05:33 +00:00
|
|
|
QVERIFY( (secureArray[0] == (char)0x63) );
|
2004-11-05 03:10:59 +00:00
|
|
|
}
|
|
|
|
|
2005-12-23 06:05:33 +00:00
|
|
|
QTEST_MAIN(SecureArrayUnitTest)
|
2006-10-12 05:40:09 +00:00
|
|
|
|
|
|
|
#include "securearrayunittest.moc"
|
|
|
|
|