~ubuntu-branches/ubuntu/wily/qca2/wily-proposed

« back to all changes in this revision

Viewing changes to qca/unittest/securearrayunittest/securearrayunittest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2007-10-27 18:51:54 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20071027185154-4ir9ys3h2q9fofrw
Tags: 2.0.0-2
Upload to unstable

Show diffs side-by-side

added added

removed removed

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