1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include <QtTest/QtTest>
45 class tst_QCryptographicHash : public QObject
49 void repeated_result_data();
50 void repeated_result();
51 void intermediary_result_data();
52 void intermediary_result();
59 void tst_QCryptographicHash::repeated_result_data()
61 intermediary_result_data();
64 void tst_QCryptographicHash::repeated_result()
67 QCryptographicHash::Algorithm _algo = QCryptographicHash::Algorithm(algo);
68 QCryptographicHash hash(_algo);
70 QFETCH(QByteArray, first);
73 QFETCH(QByteArray, hash_first);
74 QByteArray result = hash.result();
75 QCOMPARE(result, hash_first);
76 QCOMPARE(result, hash.result());
80 result = hash.result();
81 QCOMPARE(result, hash_first);
82 QCOMPARE(result, hash.result());
85 void tst_QCryptographicHash::intermediary_result_data()
87 QTest::addColumn<int>("algo");
88 QTest::addColumn<QByteArray>("first");
89 QTest::addColumn<QByteArray>("second");
90 QTest::addColumn<QByteArray>("hash_first");
91 QTest::addColumn<QByteArray>("hash_firstsecond");
93 QTest::newRow("md4") << int(QCryptographicHash::Md4)
94 << QByteArray("abc") << QByteArray("abc")
95 << QByteArray::fromHex("A448017AAF21D8525FC10AE87AA6729D")
96 << QByteArray::fromHex("03E5E436DAFAF3B9B3589DB83C417C6B");
97 QTest::newRow("md5") << int(QCryptographicHash::Md5)
98 << QByteArray("abc") << QByteArray("abc")
99 << QByteArray::fromHex("900150983CD24FB0D6963F7D28E17F72")
100 << QByteArray::fromHex("440AC85892CA43AD26D44C7AD9D47D3E");
101 QTest::newRow("sha1") << int(QCryptographicHash::Sha1)
102 << QByteArray("abc") << QByteArray("abc")
103 << QByteArray::fromHex("A9993E364706816ABA3E25717850C26C9CD0D89D")
104 << QByteArray::fromHex("F8C1D87006FBF7E5CC4B026C3138BC046883DC71");
107 void tst_QCryptographicHash::intermediary_result()
110 QCryptographicHash::Algorithm _algo = QCryptographicHash::Algorithm(algo);
111 QCryptographicHash hash(_algo);
113 QFETCH(QByteArray, first);
116 QFETCH(QByteArray, hash_first);
117 QByteArray result = hash.result();
118 QCOMPARE(result, hash_first);
121 QFETCH(QByteArray, second);
122 QFETCH(QByteArray, hash_firstsecond);
123 hash.addData(second);
125 result = hash.result();
126 QCOMPARE(result, hash_firstsecond);
132 void tst_QCryptographicHash::sha1()
135 // A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
136 QCOMPARE(QCryptographicHash::hash("abc", QCryptographicHash::Sha1).toHex().toUpper(),
137 QByteArray("A9993E364706816ABA3E25717850C26C9CD0D89D"));
139 // SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
140 // 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
141 QCOMPARE(QCryptographicHash::hash("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
142 QCryptographicHash::Sha1).toHex().toUpper(),
143 QByteArray("84983E441C3BD26EBAAE4AA1F95129E5E54670F1"));
145 // SHA1(A million repetitions of "a") =
146 // 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
148 for (int i = 0; i < 1000000; ++i)
150 QCOMPARE(QCryptographicHash::hash(as, QCryptographicHash::Sha1).toHex().toUpper(),
151 QByteArray("34AA973CD4C4DAA4F61EEB2BDBAD27316534016F"));
155 Q_DECLARE_METATYPE(QCryptographicHash::Algorithm);
157 void tst_QCryptographicHash::files_data() {
158 QTest::addColumn<QString>("filename");
159 QTest::addColumn<QCryptographicHash::Algorithm>("algorithm");
160 QTest::addColumn<QByteArray>("md5sum");
161 QTest::newRow("Line") << QString::fromAscii("data/2c1517dad3678f03917f15849b052fd5.md5") << QCryptographicHash::Md5 << QByteArray("2c1517dad3678f03917f15849b052fd5");
162 QTest::newRow("Line") << QString::fromAscii("data/d41d8cd98f00b204e9800998ecf8427e.md5") << QCryptographicHash::Md5 << QByteArray("d41d8cd98f00b204e9800998ecf8427e");
166 void tst_QCryptographicHash::files()
168 QFETCH(QString, filename);
169 QFETCH(QCryptographicHash::Algorithm, algorithm);
170 QFETCH(QByteArray, md5sum);
172 QFile f(QString::fromLocal8Bit(SRCDIR) + filename);
173 QCryptographicHash hash(algorithm);
174 QVERIFY(! hash.addData(&f)); // file is not open for reading;
175 if (f.open(QIODevice::ReadOnly)) {
176 QVERIFY(hash.addData(&f));
177 QCOMPARE(hash.result().toHex(),md5sum);
179 QFAIL("Failed to open file for testing. should not happen");
185 QTEST_MAIN(tst_QCryptographicHash)
186 #include "tst_qcryptographichash.moc"