0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the test suite of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
|
|
43 |
#include <QtTest/QtTest>
|
|
44 |
|
|
45 |
class tst_QCryptographicHash : public QObject
|
|
46 |
{
|
|
47 |
Q_OBJECT
|
|
48 |
private slots:
|
|
49 |
void repeated_result_data();
|
|
50 |
void repeated_result();
|
|
51 |
void intermediary_result_data();
|
|
52 |
void intermediary_result();
|
|
53 |
void sha1();
|
|
54 |
};
|
|
55 |
#include <QtCore>
|
|
56 |
|
|
57 |
void tst_QCryptographicHash::repeated_result_data()
|
|
58 |
{
|
|
59 |
intermediary_result_data();
|
|
60 |
}
|
|
61 |
|
|
62 |
void tst_QCryptographicHash::repeated_result()
|
|
63 |
{
|
|
64 |
QFETCH(int, algo);
|
|
65 |
QCryptographicHash::Algorithm _algo = QCryptographicHash::Algorithm(algo);
|
|
66 |
QCryptographicHash hash(_algo);
|
|
67 |
|
|
68 |
QFETCH(QByteArray, first);
|
|
69 |
hash.addData(first);
|
|
70 |
|
|
71 |
QFETCH(QByteArray, hash_first);
|
|
72 |
QByteArray result = hash.result();
|
|
73 |
QCOMPARE(result, hash_first);
|
|
74 |
QCOMPARE(result, hash.result());
|
|
75 |
|
|
76 |
hash.reset();
|
|
77 |
hash.addData(first);
|
|
78 |
result = hash.result();
|
|
79 |
QCOMPARE(result, hash_first);
|
|
80 |
QCOMPARE(result, hash.result());
|
|
81 |
}
|
|
82 |
|
|
83 |
void tst_QCryptographicHash::intermediary_result_data()
|
|
84 |
{
|
|
85 |
QTest::addColumn<int>("algo");
|
|
86 |
QTest::addColumn<QByteArray>("first");
|
|
87 |
QTest::addColumn<QByteArray>("second");
|
|
88 |
QTest::addColumn<QByteArray>("hash_first");
|
|
89 |
QTest::addColumn<QByteArray>("hash_firstsecond");
|
|
90 |
|
|
91 |
QTest::newRow("md4") << int(QCryptographicHash::Md4)
|
|
92 |
<< QByteArray("abc") << QByteArray("abc")
|
|
93 |
<< QByteArray::fromHex("A448017AAF21D8525FC10AE87AA6729D")
|
|
94 |
<< QByteArray::fromHex("03E5E436DAFAF3B9B3589DB83C417C6B");
|
|
95 |
QTest::newRow("md5") << int(QCryptographicHash::Md5)
|
|
96 |
<< QByteArray("abc") << QByteArray("abc")
|
|
97 |
<< QByteArray::fromHex("900150983CD24FB0D6963F7D28E17F72")
|
|
98 |
<< QByteArray::fromHex("440AC85892CA43AD26D44C7AD9D47D3E");
|
|
99 |
QTest::newRow("sha1") << int(QCryptographicHash::Sha1)
|
|
100 |
<< QByteArray("abc") << QByteArray("abc")
|
|
101 |
<< QByteArray::fromHex("A9993E364706816ABA3E25717850C26C9CD0D89D")
|
|
102 |
<< QByteArray::fromHex("F8C1D87006FBF7E5CC4B026C3138BC046883DC71");
|
|
103 |
}
|
|
104 |
|
|
105 |
void tst_QCryptographicHash::intermediary_result()
|
|
106 |
{
|
|
107 |
QFETCH(int, algo);
|
|
108 |
QCryptographicHash::Algorithm _algo = QCryptographicHash::Algorithm(algo);
|
|
109 |
QCryptographicHash hash(_algo);
|
|
110 |
|
|
111 |
QFETCH(QByteArray, first);
|
|
112 |
hash.addData(first);
|
|
113 |
|
|
114 |
QFETCH(QByteArray, hash_first);
|
|
115 |
QByteArray result = hash.result();
|
|
116 |
QCOMPARE(result, hash_first);
|
|
117 |
|
|
118 |
// don't reset
|
|
119 |
QFETCH(QByteArray, second);
|
|
120 |
QFETCH(QByteArray, hash_firstsecond);
|
|
121 |
hash.addData(second);
|
|
122 |
|
|
123 |
result = hash.result();
|
|
124 |
QCOMPARE(result, hash_firstsecond);
|
|
125 |
|
|
126 |
hash.reset();
|
|
127 |
}
|
|
128 |
|
|
129 |
|
|
130 |
void tst_QCryptographicHash::sha1()
|
|
131 |
{
|
|
132 |
// SHA1("abc") =
|
|
133 |
// A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
|
|
134 |
QCOMPARE(QCryptographicHash::hash("abc", QCryptographicHash::Sha1).toHex().toUpper(),
|
|
135 |
QByteArray("A9993E364706816ABA3E25717850C26C9CD0D89D"));
|
|
136 |
|
|
137 |
// SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
|
|
138 |
// 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
|
|
139 |
QCOMPARE(QCryptographicHash::hash("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
|
|
140 |
QCryptographicHash::Sha1).toHex().toUpper(),
|
|
141 |
QByteArray("84983E441C3BD26EBAAE4AA1F95129E5E54670F1"));
|
|
142 |
|
|
143 |
// SHA1(A million repetitions of "a") =
|
|
144 |
// 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
|
|
145 |
QByteArray as;
|
|
146 |
for (int i = 0; i < 1000000; ++i)
|
|
147 |
as += 'a';
|
|
148 |
QCOMPARE(QCryptographicHash::hash(as, QCryptographicHash::Sha1).toHex().toUpper(),
|
|
149 |
QByteArray("34AA973CD4C4DAA4F61EEB2BDBAD27316534016F"));
|
|
150 |
}
|
|
151 |
|
|
152 |
|
|
153 |
QTEST_MAIN(tst_QCryptographicHash)
|
|
154 |
#include "tst_qcryptographichash.moc"
|