File indexing completed on 2024-04-28 05:50:04

0001 /*
0002  * SPDX-License-Identifier: GPL-3.0-or-later
0003  * SPDX-FileCopyrightText: 2020 Johan Ouwerkerk <jm.ouwerkerk@gmail.com>
0004  */
0005 #include "hmac/hmac.h"
0006 
0007 #include <QTest>
0008 #include <QtDebug>
0009 
0010 class HMacValidateKeySizeTest: public QObject
0011 {
0012     Q_OBJECT
0013 private Q_SLOTS:
0014     void testValidation(void);
0015     void testValidation_data(void);
0016 };
0017 
0018 static void define_test_data(void)
0019 {
0020     QTest::addColumn<int>("hash");
0021     QTest::addColumn<int>("keySize");
0022     QTest::addColumn<bool>("requireSaneKeySize");
0023     QTest::addColumn<bool>("expected");
0024 }
0025 
0026 static void define_test_case(const char *testCase, QCryptographicHash::Algorithm hash, int keySize, bool requireSaneKeySize, bool expected)
0027 {
0028     QTest::newRow(qPrintable(QLatin1String(testCase))) << ((int) hash) << keySize << requireSaneKeySize << expected;
0029 }
0030 
0031 
0032 void HMacValidateKeySizeTest::testValidation(void)
0033 {
0034     QFETCH(int, hash);
0035     QFETCH(int, keySize);
0036     QFETCH(bool, requireSaneKeySize);
0037 
0038     QTEST(hmac::validateKeySize((QCryptographicHash::Algorithm) hash, keySize, requireSaneKeySize), "expected");
0039 }
0040 
0041 
0042 void HMacValidateKeySizeTest::testValidation_data(void)
0043 {
0044     define_test_data();
0045 
0046     define_test_case("short keys for HMAC-SHA1 permitted", QCryptographicHash::Sha1, 19, false, true);
0047     define_test_case("exact match for HMAC-SHA1 output", QCryptographicHash::Sha1, 20, true, true);
0048     define_test_case("long keys for HMAC-SHA1", QCryptographicHash::Sha1, 500, true, true);
0049     define_test_case("short keys for HMAC-SHA1 disallowed", QCryptographicHash::Sha1, 19, true, false);
0050     define_test_case("invalid key size: -1", QCryptographicHash::Sha1, -1, false, false);
0051     define_test_case("invalid algorithm: -1", (QCryptographicHash::Algorithm) -1, 500, true, false);
0052 }
0053 
0054 QTEST_APPLESS_MAIN(HMacValidateKeySizeTest)
0055 
0056 #include "hmac-validate-keysize.moc"