tst_QMessageAuthenticationCode: check that setKey() reset()s

It's documented as such.

Change-Id: I7299d289117e52dcefe3c4ab917d7ecad6dd02be
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
(cherry picked from commit a62aa1817863880fd340aab54d5b86aa4f8b1e53)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2023-02-25 15:01:12 +01:00 committed by Qt Cherry-pick Bot
parent 2ae0c1c390
commit da062aeda4

View File

@ -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");