Update copyright year in license headers.
[profile/ivi/qtbase.git] / tests / auto / corelib / tools / qcryptographichash / tst_qcryptographichash.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 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 ** 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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
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     void files_data();
55     void files();
56 };
57 #include <QtCore>
58
59 void tst_QCryptographicHash::repeated_result_data()
60 {
61     intermediary_result_data();
62 }
63
64 void tst_QCryptographicHash::repeated_result()
65 {
66     QFETCH(int, algo);
67     QCryptographicHash::Algorithm _algo = QCryptographicHash::Algorithm(algo);
68     QCryptographicHash hash(_algo);
69
70     QFETCH(QByteArray, first);
71     hash.addData(first);
72
73     QFETCH(QByteArray, hash_first);
74     QByteArray result = hash.result();
75     QCOMPARE(result, hash_first);
76     QCOMPARE(result, hash.result());
77
78     hash.reset();
79     hash.addData(first);
80     result = hash.result();
81     QCOMPARE(result, hash_first);
82     QCOMPARE(result, hash.result());
83 }
84
85 void tst_QCryptographicHash::intermediary_result_data()
86 {
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");
92
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");
105 }
106
107 void tst_QCryptographicHash::intermediary_result()
108 {
109     QFETCH(int, algo);
110     QCryptographicHash::Algorithm _algo = QCryptographicHash::Algorithm(algo);
111     QCryptographicHash hash(_algo);
112
113     QFETCH(QByteArray, first);
114     hash.addData(first);
115
116     QFETCH(QByteArray, hash_first);
117     QByteArray result = hash.result();
118     QCOMPARE(result, hash_first);
119
120     // don't reset
121     QFETCH(QByteArray, second);
122     QFETCH(QByteArray, hash_firstsecond);
123     hash.addData(second);
124
125     result = hash.result();
126     QCOMPARE(result, hash_firstsecond);
127
128     hash.reset();
129 }
130
131
132 void tst_QCryptographicHash::sha1()
133 {
134 //  SHA1("abc") =
135 //      A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
136     QCOMPARE(QCryptographicHash::hash("abc", QCryptographicHash::Sha1).toHex().toUpper(),
137              QByteArray("A9993E364706816ABA3E25717850C26C9CD0D89D"));
138              
139 //  SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
140 //      84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
141     QCOMPARE(QCryptographicHash::hash("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
142                                       QCryptographicHash::Sha1).toHex().toUpper(),
143              QByteArray("84983E441C3BD26EBAAE4AA1F95129E5E54670F1"));
144              
145 //  SHA1(A million repetitions of "a") =
146 //      34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
147     QByteArray as;
148     for (int i = 0; i < 1000000; ++i)
149         as += 'a';
150     QCOMPARE(QCryptographicHash::hash(as, QCryptographicHash::Sha1).toHex().toUpper(), 
151              QByteArray("34AA973CD4C4DAA4F61EEB2BDBAD27316534016F"));
152 }
153
154
155 Q_DECLARE_METATYPE(QCryptographicHash::Algorithm);
156
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");
163 }
164
165
166 void tst_QCryptographicHash::files()
167 {
168     QFETCH(QString, filename);
169     QFETCH(QCryptographicHash::Algorithm, algorithm);
170     QFETCH(QByteArray, md5sum);
171     {
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);
178         } else {
179             QFAIL("Failed to open file for testing. should not happen");
180         }
181     }
182 }
183
184
185 QTEST_MAIN(tst_QCryptographicHash)
186 #include "tst_qcryptographichash.moc"