aboutsummaryrefslogtreecommitdiff
path: root/autotests/testkeyverification.cpp
blob: 96aad6c797cbceac4c122c30826c991a452be242 (plain)
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
// SPDX-FileCopyrightText: 2022 Tobias Fella <fella@posteo.de>
//
// SPDX-License-Identifier: LGPL-2.1-or-later


#include <QTest>
#include "testutils.h"
#include <qt_connection_util.h>

class TestKeyVerificationSession : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void testVerification()
    {
        CREATE_CONNECTION(a, "alice1", "secret", "AliceDesktop")
        CREATE_CONNECTION(b, "alice1", "secret", "AlicePhone")

        QPointer<KeyVerificationSession> aSession{};
        connect(a.get(), &Connection::newKeyVerificationSession, this, [&](KeyVerificationSession* session) {
            aSession = session;
            QVERIFY(session->remoteDeviceId() == b->deviceId());
            QVERIFY(session->state() == KeyVerificationSession::WAITINGFORREADY);
            connectSingleShot(session, &KeyVerificationSession::stateChanged, this, [=](){
                QVERIFY(session->state() == KeyVerificationSession::ACCEPTED);
                connectSingleShot(session, &KeyVerificationSession::stateChanged, this, [=](){
                    QVERIFY(session->state() == KeyVerificationSession::WAITINGFORVERIFICATION);
                    session->sendMac();
                });
            });
        });
        a->startKeyVerificationSession(b->deviceId());
        connect(b.get(), &Connection::newKeyVerificationSession, this, [=](KeyVerificationSession* session) {
            QVERIFY(session->remoteDeviceId() == a->deviceId());
            QVERIFY(session->state() == KeyVerificationSession::INCOMING);
            session->sendReady();
            // KeyVerificationSession::READY is skipped because we have only one method
            QVERIFY(session->state() == KeyVerificationSession::WAITINGFORACCEPT);
            connectSingleShot(session, &KeyVerificationSession::stateChanged, this, [=](){
                QVERIFY(session->state() == KeyVerificationSession::WAITINGFORKEY || session->state() == KeyVerificationSession::ACCEPTED);
                connectSingleShot(session, &KeyVerificationSession::stateChanged, this, [=]() {
                    QVERIFY(session->state() == KeyVerificationSession::WAITINGFORVERIFICATION);
                    QVERIFY(aSession);
                    QVERIFY(aSession->sasEmojis() == session->sasEmojis());
                    session->sendMac();
                    QVERIFY(session->state() == KeyVerificationSession::WAITINGFORMAC);
                });
            });

        });
        b->syncLoop();
        a->syncLoop();
        QSignalSpy spy(aSession, &KeyVerificationSession::finished);
        spy.wait(10000);
    }
};
QTEST_GUILESS_MAIN(TestKeyVerificationSession)
#include "testkeyverification.moc"