From d8579826ed2834ef8f63b39bce7c3869dac733da Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Mon, 23 Nov 2020 12:23:34 +0100 Subject: QMessageAuthenticationCode: extend tests - Add test for static hash() method - Add tests for addData() overloads - Add input data for sha384 and sha512 Task-number: QTBUG-88183 Change-Id: I7e16419b3a582468fd1de15613e1157af428bc4c Reviewed-by: Sona Kurazyan --- .../tst_qmessageauthenticationcode.cpp | 61 ++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'tests/auto/corelib') diff --git a/tests/auto/corelib/tools/qmessageauthenticationcode/tst_qmessageauthenticationcode.cpp b/tests/auto/corelib/tools/qmessageauthenticationcode/tst_qmessageauthenticationcode.cpp index 2f8052fd4a..7ed091e9c2 100644 --- a/tests/auto/corelib/tools/qmessageauthenticationcode/tst_qmessageauthenticationcode.cpp +++ b/tests/auto/corelib/tools/qmessageauthenticationcode/tst_qmessageauthenticationcode.cpp @@ -38,6 +38,8 @@ private slots: void result(); void result_incremental_data(); void result_incremental(); + void addData_overloads_data(); + void addData_overloads(); }; Q_DECLARE_METATYPE(QCryptographicHash::Algorithm) @@ -62,6 +64,15 @@ void tst_QMessageAuthenticationCode::result_data() << QByteArray() << QByteArray() << QByteArray::fromHex("b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad"); + QTest::newRow("sha384-empty") << QCryptographicHash::Sha384 << QByteArray() << QByteArray() + << QByteArray::fromHex( + "6c1f2ee938fad2e24bd91298474382ca218c75db3d83e114b3d43" + "67776d14d3551289e75e8209cd4b792302840234adc"); + QTest::newRow("sha512-empty") + << QCryptographicHash::Sha512 << QByteArray() << QByteArray() + << QByteArray::fromHex( + "b936cee86c9f87aa5d3c6f2e84cb5a4239a5fe50480a6ec66b70ab5b1f4ac6730c6c515421b" + "327ec1d69402e53dfb49ad7381eb067b338fd7b0cb22247225d47"); // Some not-empty QTest::newRow("md5") << QCryptographicHash::Md5 @@ -76,6 +87,17 @@ void tst_QMessageAuthenticationCode::result_data() << QByteArray("key") << QByteArray("The quick brown fox jumps over the lazy dog") << QByteArray::fromHex("f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8"); + QTest::newRow("sha384") << QCryptographicHash::Sha384 << QByteArray("key") + << QByteArray("The quick brown fox jumps over the lazy dog") + << QByteArray::fromHex( + "d7f4727e2c0b39ae0f1e40cc96f60242d5b7801841cea6fc592c5d3e1ae" + "50700582a96cf35e1e554995fe4e03381c237"); + QTest::newRow("sha512") + << QCryptographicHash::Sha512 << QByteArray("key") + << QByteArray("The quick brown fox jumps over the lazy dog") + << QByteArray::fromHex( + "b42af09057bac1e2d41708e48a902e09b5ff7f12ab428a4fe86653c73dd248fb82f948a549f" + "7b791a5b41915ee4d1ec3935357e4e2317250d0372afa2ebeeb3a"); // Some from rfc-2104 QTest::newRow("rfc-md5-1") << QCryptographicHash::Md5 @@ -105,6 +127,9 @@ void tst_QMessageAuthenticationCode::result() QByteArray result = mac.result(); QCOMPARE(result, code); + + result = QMessageAuthenticationCode::hash(message, key, algo); + QCOMPARE(result, code); } void tst_QMessageAuthenticationCode::result_incremental_data() @@ -134,5 +159,41 @@ void tst_QMessageAuthenticationCode::result_incremental() QCOMPARE(result, code); } +void tst_QMessageAuthenticationCode::addData_overloads_data() +{ + result_data(); +} + +void tst_QMessageAuthenticationCode::addData_overloads() +{ + QFETCH(QCryptographicHash::Algorithm, algo); + QFETCH(QByteArray, key); + QFETCH(QByteArray, message); + QFETCH(QByteArray, code); + + // overload using const char* and length + { + QMessageAuthenticationCode mac(algo); + mac.setKey(key); + mac.addData(message.constData(), message.size()); + QByteArray result = mac.result(); + + QCOMPARE(result, code); + } + + // overload using QIODevice + { + QBuffer buffer(&message); + buffer.open(QIODevice::ReadOnly); + QMessageAuthenticationCode mac(algo); + mac.setKey(key); + QVERIFY(mac.addData(&buffer)); + QByteArray result = mac.result(); + buffer.close(); + + QCOMPARE(result, code); + } +} + QTEST_MAIN(tst_QMessageAuthenticationCode) #include "tst_qmessageauthenticationcode.moc" -- cgit v1.2.3