summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/tools/qmessageauthenticationcode
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@qt.io>2023-02-25 15:01:12 +0100
committerMarc Mutz <marc.mutz@qt.io>2023-02-26 07:51:29 +0100
commita62aa1817863880fd340aab54d5b86aa4f8b1e53 (patch)
tree6a3fdbbf75c42f79b1baf9db32f77fb5c88a663d /tests/auto/corelib/tools/qmessageauthenticationcode
parent54a4d4bf7619c4c6acf9fb02c2297fefff94d6e9 (diff)
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 <thiago.macieira@intel.com> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Diffstat (limited to 'tests/auto/corelib/tools/qmessageauthenticationcode')
-rw-r--r--tests/auto/corelib/tools/qmessageauthenticationcode/tst_qmessageauthenticationcode.cpp46
1 files changed, 46 insertions, 0 deletions
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<A>("algo");
+
+ const auto me = QMetaEnum::fromType<A>();
+ 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<QCryptographicHash::Algorithm>("algo");