qca/unittest/securearrayunittest.cpp

117 lines
4.6 KiB
C++
Raw Normal View History

/**
* securearrayunittest.cpp
*
* Copyright (C) 2004 Brad Hards <bradh@frogmouth.net>
*
* 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.
*/
#include "securearrayunittest.h"
#include "qca.h"
SecureArrayUnitTest::SecureArrayUnitTest()
: Tester()
{
}
void SecureArrayUnitTest::allTests()
{
QCA::Initializer init;
QSecureArray emptyArray;
CHECK( emptyArray.size(), (unsigned int)0 );
CHECK( emptyArray.isEmpty(), true );
QSecureArray testArray(10);
CHECK( testArray.size(), (unsigned int) 10 );
CHECK( testArray.isEmpty(), false );
//testArray.fill( 'a' );
for (unsigned int i = 0; i < testArray.size(); i++) {
testArray[ i ] = 0x61;
}
CHECK( QCA::arrayToHex( testArray ), QString( "61616161616161616161" ) );
//testArray.fill( 'b' );
for (unsigned int i = 0; i < testArray.size(); i++) {
testArray[ i ] = 0x62;
}
testArray[7] = 0x00;
CHECK( QCA::arrayToHex( testArray ), QString( "62626262626262006262" ) );
QByteArray byteArray(10);
byteArray.fill( 'c' );
QSecureArray secureArray( byteArray );
CHECK( secureArray.size(), (unsigned int) 10 );
CHECK( QCA::arrayToHex ( secureArray ), QString( "63636363636363636363" ) );
byteArray.fill( 'd' );
// it should be a copy, so no effect
CHECK( QCA::arrayToHex ( secureArray ), QString( "63636363636363636363" ) );
QSecureArray copyArray( secureArray );
CHECK( QCA::arrayToHex ( copyArray ), QString( "63636363636363636363" ) );
for (unsigned int i = 0; i < copyArray.size(); i++) {
copyArray[ i ] = 0x64;
}
CHECK( QCA::arrayToHex ( copyArray ), QString( "64646464646464646464" ) );
CHECK( QCA::arrayToHex ( secureArray ), QString( "63636363636363636363" ) );
// test for implicit sharing
QSecureArray equalsArray;
equalsArray = secureArray;
CHECK( QCA::arrayToHex ( equalsArray ), QString( "63636363636363636363" ) );
for (unsigned int i = 0; i < equalsArray.size(); i++) {
equalsArray.data()[i] = 0x65;
}
CHECK( QCA::arrayToHex ( equalsArray ), QString( "65656565656565656565" ) );
// its a reference, so should be the same
CHECK( QCA::arrayToHex ( secureArray ), QString( "65656565656565656565" ) );
// test for detaching
QSecureArray detachArray1 = secureArray; // currently the same
CHECK( QCA::arrayToHex ( detachArray1 ), QString( "65656565656565656565" ) );
for (unsigned int i = 0; i < detachArray1.size(); i++) {
detachArray1[i] = 0x66; // implicit detach
}
CHECK( QCA::arrayToHex ( secureArray ), QString( "65656565656565656565" ) );
CHECK( QCA::arrayToHex ( detachArray1 ), QString( "66666666666666666666" ) );
QSecureArray detachArray2 = secureArray; // currently the same
CHECK( QCA::arrayToHex ( detachArray2 ), QString( "65656565656565656565" ) );
detachArray2.detach(); //explicit detach
for (unsigned int i = 0; i < detachArray2.size(); i++) {
detachArray2.data()[i] = 0x67;
}
CHECK( QCA::arrayToHex ( secureArray ), QString( "65656565656565656565" ) );
CHECK( QCA::arrayToHex ( detachArray2 ), QString( "67676767676767676767" ) );
QSecureArray detachArray3 = secureArray.copy(); // assign and detach in one
CHECK( QCA::arrayToHex ( detachArray3 ), QString( "65656565656565656565" ) );
for (unsigned int i = 0; i < detachArray3.size(); i++) {
detachArray3.data()[i] = 0x68;
}
CHECK( QCA::arrayToHex ( secureArray ), QString( "65656565656565656565" ) );
CHECK( QCA::arrayToHex ( detachArray3 ), QString( "68686868686868686868" ) );
}