21
#include "core/Entry.h"
23
#include "crypto/Crypto.h"
27
QTEST_GUILESS_MAIN(TestTotp)
29
void TestTotp::initTestCase()
31
QVERIFY(Crypto::init());
34
void TestTotp::testParseSecret()
37
QString secret = "otpauth://totp/"
38
"ACME%20Co:john@example.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm="
39
"SHA1&digits=6&period=30";
40
auto settings = Totp::parseSettings(secret);
41
QVERIFY(!settings.isNull());
42
QCOMPARE(settings->key, QString("HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ"));
43
QCOMPARE(settings->custom, false);
44
QCOMPARE(settings->format, Totp::StorageFormat::OTPURL);
45
QCOMPARE(settings->digits, 6u);
46
QCOMPARE(settings->step, 30u);
47
QCOMPARE(settings->algorithm, Totp::Algorithm::Sha1);
50
secret = "otpauth://totp/"
51
"ACME%20Co:john@example.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm="
52
"SHA512&digits=6&period=30";
53
settings = Totp::parseSettings(secret);
54
QVERIFY(!settings.isNull());
55
QCOMPARE(settings->key, QString("HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ"));
56
QCOMPARE(settings->custom, true);
57
QCOMPARE(settings->format, Totp::StorageFormat::OTPURL);
58
QCOMPARE(settings->digits, 6u);
59
QCOMPARE(settings->step, 30u);
60
QCOMPARE(settings->algorithm, Totp::Algorithm::Sha512);
63
secret.replace("period=30", "period=90000");
64
settings = Totp::parseSettings(secret);
65
QVERIFY(!settings.isNull());
66
QCOMPARE(settings->step, 86400u);
69
secret = "key=HXDMVJECJJWSRBY%3d&step=25&size=8&otpHashMode=Sha256";
70
settings = Totp::parseSettings(secret);
71
QVERIFY(!settings.isNull());
72
QCOMPARE(settings->key, QString("HXDMVJECJJWSRBY="));
73
QCOMPARE(settings->custom, true);
74
QCOMPARE(settings->format, Totp::StorageFormat::KEEOTP);
75
QCOMPARE(settings->digits, 8u);
76
QCOMPARE(settings->step, 25u);
77
QCOMPARE(settings->algorithm, Totp::Algorithm::Sha256);
80
secret = "gezdgnbvgy3tqojqgezdgnbvgy3tqojq";
81
settings = Totp::parseSettings("30;8", secret);
82
QVERIFY(!settings.isNull());
83
QCOMPARE(settings->key, QString("gezdgnbvgy3tqojqgezdgnbvgy3tqojq"));
84
QCOMPARE(settings->custom, true);
85
QCOMPARE(settings->format, Totp::StorageFormat::LEGACY);
86
QCOMPARE(settings->digits, 8u);
87
QCOMPARE(settings->step, 30u);
88
QCOMPARE(settings->algorithm, Totp::Algorithm::Sha1);
91
secret = "gezdgnbvgy3tqojqgezdgnbvgy3tqojq";
92
settings = Totp::parseSettings("", secret);
93
QVERIFY(!settings.isNull());
94
QCOMPARE(settings->key, QString("gezdgnbvgy3tqojqgezdgnbvgy3tqojq"));
95
QCOMPARE(settings->custom, false);
96
QCOMPARE(settings->format, Totp::StorageFormat::LEGACY);
97
QCOMPARE(settings->digits, 6u);
98
QCOMPARE(settings->step, 30u);
99
QCOMPARE(settings->algorithm, Totp::Algorithm::Sha1);
102
settings = Totp::parseSettings("", "");
103
QVERIFY(settings.isNull());
106
settings = Totp::parseSettings("30;8", "");
107
QVERIFY(settings.isNull());
110
void TestTotp::testTotpCode()
114
auto settings = Totp::createSettings("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ", Totp::DEFAULT_DIGITS, Totp::DEFAULT_STEP);
117
quint64 time = 1234567890;
118
QCOMPARE(Totp::generateTotp(settings, time), QString("005924"));
121
QCOMPARE(Totp::generateTotp(settings, time), QString("081804"));
124
settings->digits = 8;
125
settings->custom = true;
127
QCOMPARE(Totp::generateTotp(settings, time), QString("14050471"));
130
QCOMPARE(Totp::generateTotp(settings, time), QString("69279037"));
133
void TestTotp::testSteamTotp()
136
QString secret = "otpauth://totp/"
137
"test:test@example.com?secret=63BEDWCQZKTQWPESARIERL5DTTQFCJTK&issuer=Valve&algorithm="
138
"SHA1&digits=5&period=30&encoder=steam";
139
auto settings = Totp::parseSettings(secret);
141
QCOMPARE(settings->key, QString("63BEDWCQZKTQWPESARIERL5DTTQFCJTK"));
142
QCOMPARE(settings->encoder.shortName, Totp::STEAM_SHORTNAME);
143
QCOMPARE(settings->format, Totp::StorageFormat::OTPURL);
144
QCOMPARE(settings->digits, Totp::STEAM_DIGITS);
145
QCOMPARE(settings->step, 30u);
150
quint64 time = 1511200518;
151
QCOMPARE(Totp::generateTotp(settings, time), QString("FR8RV"));
153
QCOMPARE(Totp::generateTotp(settings, time), QString("9P3VP"));
156
void TestTotp::testEntryHistory()
161
auto settings = Totp::createSettings("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ", digits, step);
163
QCOMPARE(entry.historyItems().size(), 0);
164
QVERIFY(!entry.hasTotp());
166
entry.setTotp(settings);
167
QCOMPARE(entry.historyItems().size(), 1);
168
QVERIFY(entry.hasTotp());
169
QCOMPARE(entry.totpSettings()->key, QString("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ"));
171
settings->key = "foo";
172
entry.setTotp(settings);
173
QCOMPARE(entry.historyItems().size(), 2);
174
QCOMPARE(entry.totpSettings()->key, QString("foo"));
176
entry.setTotp(nullptr);
177
QVERIFY(!entry.hasTotp());
178
QCOMPARE(entry.historyItems().size(), 3);