From a62aa1817863880fd340aab54d5b86aa4f8b1e53 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sat, 25 Feb 2023 15:01:12 +0100 Subject: tst_QMessageAuthenticationCode: check that setKey() reset()s It's documented as such. Pick-to: 6.5 6.4 6.2 5.15 Change-Id: I7299d289117e52dcefe3c4ab917d7ecad6dd02be Reviewed-by: Thiago Macieira Reviewed-by: Qt CI Bot --- .../tst_qmessageauthenticationcode.cpp | 46 ++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'tests/auto/corelib/tools/qmessageauthenticationcode') diff --git a/tests/auto/corelib/tools/qmessageauthenticationcode/tst_qmessageauthenticationcode.cpp b/tests/auto/corelib/tools/qmessageauthenticationcode/tst_qmessageauthenticationcode.cpp index 12fc2f8e43..59fecc1b56 100644 --- a/tests/auto/corelib/tools/qmessageauthenticationcode/tst_qmessageauthenticationcode.cpp +++ b/tests/auto/corelib/tools/qmessageauthenticationcode/tst_qmessageauthenticationcode.cpp @@ -12,6 +12,8 @@ class tst_QMessageAuthenticationCode : public QObject { Q_OBJECT private slots: + void repeated_setKey_data(); + void repeated_setKey(); void result_data(); void result(); void result_incremental_data(); @@ -22,6 +24,50 @@ private slots: Q_DECLARE_METATYPE(QCryptographicHash::Algorithm) +void tst_QMessageAuthenticationCode::repeated_setKey_data() +{ + using A = QCryptographicHash::Algorithm; + QTest::addColumn("algo"); + + const auto me = QMetaEnum::fromType(); + for (int i = 0, value; (value = me.value(i)) != -1; ++i) + QTest::addRow("%s", me.key(i)) << A(value); +} + +void tst_QMessageAuthenticationCode::repeated_setKey() +{ + QFETCH(const QCryptographicHash::Algorithm, algo); + + if (!QCryptographicHash::supportsAlgorithm(algo)) + QSKIP("QCryptographicHash doesn't support this algorithm"); + + // GIVEN: two long keys, so we're sure the key needs to be hashed in order + // to fit into the hash algorithm's block + + static const QByteArray key1(1024, 'a'); + static const QByteArray key2(2048, 'b'); + + // WHEN: processing the same message + + QMessageAuthenticationCode macX(algo); + QMessageAuthenticationCode mac1(algo, key1); + QMessageAuthenticationCode mac2(algo, key2); + + const auto check = [](QMessageAuthenticationCode &mac) { + mac.addData("This is nonsense, ignore it, please."); + return mac.result(); + }; + + macX.setKey(key1); + QCOMPARE(check(macX), check(mac1)); + + // THEN: the result does not depend on whether a new QMAC instance was used + // or an old one re-used (iow: setKey() reset()s) + + macX.setKey(key2); + QCOMPARE(check(macX), check(mac2)); +} + void tst_QMessageAuthenticationCode::result_data() { QTest::addColumn("algo"); -- cgit v1.2.3