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 "oath/oath.h" 0006 0007 #include <QTest> 0008 #include <QtDebug> 0009 0010 class HotpAlgorithmTest: public QObject 0011 { 0012 Q_OBJECT 0013 private Q_SLOTS: 0014 void rfcTestVector(void); 0015 void rfcTestVector_data(void); 0016 }; 0017 0018 void define_test_case(quint64 counter, bool checksum, const char * expected) 0019 { 0020 static const QString testCase(QLatin1String("%1 (%2) ... %3")); 0021 0022 QTest::newRow(qPrintable(testCase.arg(counter).arg(checksum ? QLatin1String("with checksum") : QLatin1String("without checksum")).arg(QLatin1String(expected)))) 0023 << counter << checksum << QString(QLatin1String(expected)); 0024 } 0025 0026 0027 /* 0028 * (Static) test vector params obtained from RFC-4226 0029 * https://tools.ietf.org/html/rfc4226#page-32 0030 */ 0031 static QByteArray secret("12345678901234567890"); 0032 static uint tokenLength = 6; 0033 0034 void HotpAlgorithmTest::rfcTestVector(void) 0035 { 0036 QFETCH(quint64, counter); 0037 QFETCH(bool, checksum); 0038 0039 std::optional<oath::Algorithm> uut = oath::Algorithm::hotp(std::nullopt, tokenLength, checksum); 0040 QVERIFY2(uut, "should be able to construct the algorithm using settings of the RFC test vector"); 0041 0042 QByteArray copy(secret); 0043 0044 std::optional<QString> result = uut->compute(counter, copy.data(), copy.size()); 0045 QVERIFY2(result, "token should be computed successfully"); 0046 QTEST(*result, "rfc-test-vector"); 0047 } 0048 0049 void HotpAlgorithmTest::rfcTestVector_data(void) 0050 { 0051 static const char * corpus[20] { 0052 "755224", 0053 "287082", 0054 "359152", 0055 "969429", 0056 "338314", 0057 "254676", 0058 "287922", 0059 "162583", 0060 "399871", 0061 "520489", 0062 0063 /* 0064 * same as the first batch (from RFC test samples), but with manually added Luhn checksum: 0065 */ 0066 "7552243", 0067 "2870822", 0068 "3591526", 0069 "9694290", 0070 "3383148", 0071 "2546760", 0072 "2879229", 0073 "1625839", 0074 "3998713", 0075 "5204896" 0076 }; 0077 0078 QTest::addColumn<quint64>("counter"); 0079 QTest::addColumn<bool>("checksum"); 0080 QTest::addColumn<QString>("rfc-test-vector"); 0081 0082 for (quint64 k = 0; k < 20; ++k) { 0083 define_test_case(k % 10ULL, k >= 10, corpus[k]); 0084 } 0085 } 0086 0087 QTEST_APPLESS_MAIN(HotpAlgorithmTest) 0088 0089 #include "hotp-algorithm.moc" 0090