File: commandline-account-job.cpp

package info (click to toggle)
keysmith 25.08.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,276 kB
  • sloc: cpp: 11,649; xml: 352; sh: 121; makefile: 3
file content (110 lines) | stat: -rw-r--r-- 4,362 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * SPDX-License-Identifier: GPL-3.0-or-later
 * SPDX-FileCopyrightText: 2020 Johan Ouwerkerk <jm.ouwerkerk@gmail.com>
 */
#include "app/cli.h"

#include "../test-utils/spy.h"

#include <QSignalSpy>
#include <QTest>
#include <QThreadPool>
#include <QtDebug>

class CommandLineAccountJobTest : public QObject // clazy:exclude=ctor-missing-parent-argument
{
    Q_OBJECT
private Q_SLOTS:
    void initTestCase(void);
    void testValidAccountUri(void);
    void testInvalidAccountUri(void);
    void testExpiredRecipient(void);
};

void CommandLineAccountJobTest::initTestCase(void)
{
    auto threadPool = QThreadPool::globalInstance();
    QVERIFY2(threadPool, "should have a global thread pool by now");
    threadPool->setMaxThreadCount(3);
}

void CommandLineAccountJobTest::testValidAccountUri(void)
{
    model::AccountInput recipient;
    auto uut = new app::CommandLineAccountJob(&recipient);

    QSignalSpy invalid(uut, &app::CommandLineAccountJob::newAccountInvalid);
    QSignalSpy processed(uut, &app::CommandLineAccountJob::newAccountProcessed);
    QSignalSpy cleaned(uut, &QObject::destroyed);

    uut->run(QStringLiteral("otpauth://hotp/issuer:valid?secret=VALUE&digits=8&period=60&issuer=issuer&counter=42&algorithm=sha512"));

    QVERIFY2(test::signal_eventually_emitted_once(processed), "URI should be successfully processed by now");
    QVERIFY2(test::signal_eventually_emitted_once(cleaned), "AccountJob should be disposed of by now");
    QCOMPARE(invalid.count(), 0);

    QCOMPARE(recipient.name(), QStringLiteral("valid"));
    QCOMPARE(recipient.issuer(), QStringLiteral("issuer"));
    QCOMPARE(recipient.counter(), QStringLiteral("42"));
    QCOMPARE(recipient.secret(), QStringLiteral("VALUE==="));
    QCOMPARE(recipient.tokenLength(), 8U);
    QCOMPARE(recipient.timeStep(), 60U);
    QCOMPARE(recipient.type(), model::AccountInput::TokenType::Hotp);
    QCOMPARE(recipient.algorithm(), model::AccountInput::TOTPAlgorithm::Sha512);

    QVERIFY2(QThreadPool::globalInstance()->waitForDone(500), "the global thread pool should be done by now");
}

void CommandLineAccountJobTest::testExpiredRecipient(void)
{
    auto recipient = new model::AccountInput();
    QSignalSpy expired(recipient, &QObject::destroyed);

    auto uut = new app::CommandLineAccountJob(recipient);

    QSignalSpy invalid(uut, &app::CommandLineAccountJob::newAccountInvalid);
    QSignalSpy processed(uut, &app::CommandLineAccountJob::newAccountProcessed);
    QSignalSpy cleaned(uut, &QObject::destroyed);

    recipient->deleteLater();
    QVERIFY2(test::signal_eventually_emitted_once(expired), "AccountInput should have expired by now");

    uut->run(QStringLiteral("otpauth://hotp/issuer:valid?secret=VALUE&digits=8&period=60&issuer=issuer&counter=42&algorithm=sha512"));

    QVERIFY2(test::signal_eventually_emitted_once(cleaned), "AccountJob should be disposed of by now");
    QCOMPARE(processed.count(), 0);
    QCOMPARE(invalid.count(), 0);

    QVERIFY2(QThreadPool::globalInstance()->waitForDone(500), "the global thread pool should be done by now");
}

void CommandLineAccountJobTest::testInvalidAccountUri(void)
{
    model::AccountInput recipient;
    auto uut = new app::CommandLineAccountJob(&recipient);

    QSignalSpy invalid(uut, &app::CommandLineAccountJob::newAccountInvalid);
    QSignalSpy processed(uut, &app::CommandLineAccountJob::newAccountProcessed);
    QSignalSpy cleaned(uut, &QObject::destroyed);

    uut->run(QStringLiteral("not a valid otpauth:// URI"));

    QVERIFY2(test::signal_eventually_emitted_once(invalid), "URI should be rejected by now");
    QVERIFY2(test::signal_eventually_emitted_once(cleaned), "AccountJob should be disposed of by now");
    QCOMPARE(processed.count(), 0);

    QCOMPARE(recipient.name(), QString());
    QCOMPARE(recipient.issuer(), QString());
    QCOMPARE(recipient.counter(), QStringLiteral("0"));
    QCOMPARE(recipient.secret(), QString());
    QCOMPARE(recipient.tokenLength(), 6U);
    QCOMPARE(recipient.timeStep(), 30U);
    QCOMPARE(recipient.type(), model::AccountInput::TokenType::Totp);
    QCOMPARE(recipient.algorithm(), model::AccountInput::TOTPAlgorithm::Sha1);

    QVERIFY2(QThreadPool::globalInstance()->waitForDone(500), "the global thread pool should be done by now");
}

QTEST_GUILESS_MAIN(CommandLineAccountJobTest)

#include "commandline-account-job.moc"