| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2013 Ruslan Nigmatullin <euroelessar@yandex.ru> | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the test suite of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT | 
| 21 | ** included in the packaging of this file. Please review the following | 
| 22 | ** information to ensure the GNU General Public License requirements will | 
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. | 
| 24 | ** | 
| 25 | ** $QT_END_LICENSE$ | 
| 26 | ** | 
| 27 | ****************************************************************************/ | 
| 28 |  | 
| 29 |  | 
| 30 | #include <QtCore/QCoreApplication> | 
| 31 | #include <QtTest/QtTest> | 
| 32 |  | 
| 33 | class tst_QMessageAuthenticationCode : public QObject | 
| 34 | { | 
| 35 |     Q_OBJECT | 
| 36 | private slots: | 
| 37 |     void result_data(); | 
| 38 |     void result(); | 
| 39 |     void result_incremental_data(); | 
| 40 |     void result_incremental(); | 
| 41 | }; | 
| 42 |  | 
| 43 | Q_DECLARE_METATYPE(QCryptographicHash::Algorithm) | 
| 44 |  | 
| 45 | void tst_QMessageAuthenticationCode::result_data() | 
| 46 | { | 
| 47 |     QTest::addColumn<QCryptographicHash::Algorithm>(name: "algo" ); | 
| 48 |     QTest::addColumn<QByteArray>(name: "key" ); | 
| 49 |     QTest::addColumn<QByteArray>(name: "message" ); | 
| 50 |     QTest::addColumn<QByteArray>(name: "code" ); | 
| 51 |  | 
| 52 |     // Empty values | 
| 53 |     QTest::newRow(dataTag: "md5-empty" ) << QCryptographicHash::Md5 | 
| 54 |                                << QByteArray() | 
| 55 |                                << QByteArray() | 
| 56 |                                << QByteArray::fromHex(hexEncoded: "74e6f7298a9c2d168935f58c001bad88" ); | 
| 57 |     QTest::newRow(dataTag: "sha1-empty" ) << QCryptographicHash::Sha1 | 
| 58 |                                 << QByteArray() | 
| 59 |                                 << QByteArray() | 
| 60 |                                 << QByteArray::fromHex(hexEncoded: "fbdb1d1b18aa6c08324b7d64b71fb76370690e1d" ); | 
| 61 |     QTest::newRow(dataTag: "sha256-empty" ) << QCryptographicHash::Sha256 | 
| 62 |                                   << QByteArray() | 
| 63 |                                   << QByteArray() | 
| 64 |                                   << QByteArray::fromHex(hexEncoded: "b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad" ); | 
| 65 |  | 
| 66 |     // Some not-empty | 
| 67 |     QTest::newRow(dataTag: "md5" ) << QCryptographicHash::Md5 | 
| 68 |                          << QByteArray("key" ) | 
| 69 |                          << QByteArray("The quick brown fox jumps over the lazy dog" ) | 
| 70 |                          << QByteArray::fromHex(hexEncoded: "80070713463e7749b90c2dc24911e275" ); | 
| 71 |     QTest::newRow(dataTag: "sha1" ) << QCryptographicHash::Sha1 | 
| 72 |                           << QByteArray("key" ) | 
| 73 |                           << QByteArray("The quick brown fox jumps over the lazy dog" ) | 
| 74 |                           << QByteArray::fromHex(hexEncoded: "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9" ); | 
| 75 |     QTest::newRow(dataTag: "sha256" ) << QCryptographicHash::Sha256 | 
| 76 |                             << QByteArray("key" ) | 
| 77 |                             << QByteArray("The quick brown fox jumps over the lazy dog" ) | 
| 78 |                             << QByteArray::fromHex(hexEncoded: "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8" ); | 
| 79 |  | 
| 80 |     // Some from rfc-2104 | 
| 81 |     QTest::newRow(dataTag: "rfc-md5-1" ) << QCryptographicHash::Md5 | 
| 82 |                                << QByteArray::fromHex(hexEncoded: "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b" ) | 
| 83 |                                << QByteArray("Hi There" ) | 
| 84 |                                << QByteArray::fromHex(hexEncoded: "9294727a3638bb1c13f48ef8158bfc9d" ); | 
| 85 |     QTest::newRow(dataTag: "rfc-md5-2" ) << QCryptographicHash::Md5 | 
| 86 |                                << QByteArray("Jefe" ) | 
| 87 |                                << QByteArray("what do ya want for nothing?" ) | 
| 88 |                                << QByteArray::fromHex(hexEncoded: "750c783e6ab0b503eaa86e310a5db738" ); | 
| 89 |     QTest::newRow(dataTag: "rfc-md5-3" ) << QCryptographicHash::Md5 | 
| 90 |                                << QByteArray::fromHex(hexEncoded: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" ) | 
| 91 |                                << QByteArray(50, char(0xdd)) | 
| 92 |                                << QByteArray::fromHex(hexEncoded: "56be34521d144c88dbb8c733f0e8b3f6" ); | 
| 93 | } | 
| 94 |  | 
| 95 | void tst_QMessageAuthenticationCode::result() | 
| 96 | { | 
| 97 |     QFETCH(QCryptographicHash::Algorithm, algo); | 
| 98 |     QFETCH(QByteArray, key); | 
| 99 |     QFETCH(QByteArray, message); | 
| 100 |     QFETCH(QByteArray, code); | 
| 101 |  | 
| 102 |     QMessageAuthenticationCode mac(algo); | 
| 103 |     mac.setKey(key); | 
| 104 |     mac.addData(data: message); | 
| 105 |     QByteArray result = mac.result(); | 
| 106 |  | 
| 107 |     QCOMPARE(result, code); | 
| 108 | } | 
| 109 |  | 
| 110 | void tst_QMessageAuthenticationCode::result_incremental_data() | 
| 111 | { | 
| 112 |     result_data(); | 
| 113 | } | 
| 114 |  | 
| 115 | void tst_QMessageAuthenticationCode::result_incremental() | 
| 116 | { | 
| 117 |     QFETCH(QCryptographicHash::Algorithm, algo); | 
| 118 |     QFETCH(QByteArray, key); | 
| 119 |     QFETCH(QByteArray, message); | 
| 120 |     QFETCH(QByteArray, code); | 
| 121 |  | 
| 122 |     int index = message.length() / 2; | 
| 123 |     QByteArray leftPart(message.mid(index: 0, len: index)); | 
| 124 |     QByteArray rightPart(message.mid(index)); | 
| 125 |  | 
| 126 |     QCOMPARE(leftPart + rightPart, message); | 
| 127 |  | 
| 128 |     QMessageAuthenticationCode mac(algo); | 
| 129 |     mac.setKey(key); | 
| 130 |     mac.addData(data: leftPart); | 
| 131 |     mac.addData(data: rightPart); | 
| 132 |     QByteArray result = mac.result(); | 
| 133 |  | 
| 134 |     QCOMPARE(result, code); | 
| 135 | } | 
| 136 |  | 
| 137 | QTEST_MAIN(tst_QMessageAuthenticationCode) | 
| 138 | #include "tst_qmessageauthenticationcode.moc" | 
| 139 |  |