File: HelperTest.cpp

package info (click to toggle)
kf6-kauth 6.20.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,468 kB
  • sloc: cpp: 2,944; xml: 24; sh: 14; makefile: 7
file content (242 lines) | stat: -rw-r--r-- 7,333 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
    SPDX-FileCopyrightText: 2012 Dario Freddi <drf@kde.org>

    SPDX-License-Identifier: LGPL-2.1-or-later
*/

#include "BackendsManager.h"
#include "TestHelper.h"

#include <kauth/actionreply.h>
#include <kauth/executejob.h>

#include <QRandomGenerator>
#include <QSignalSpy>
#include <QTest>
#include <QThread>
#include <QTimer>

#include "../src/backends/dbus/DBusHelperProxy.h"

Q_DECLARE_METATYPE(KAuth::Action::AuthStatus)

class HelperHandler : public QObject
{
    Q_OBJECT
public:
    HelperHandler();

private Q_SLOTS:
    void init();

Q_SIGNALS:
    void ready();

private:
    DBusHelperProxy *m_helperProxy;
    TestHelper *m_helper;
    QThread *m_thread;
};

class HelperTest : public QObject
{
    Q_OBJECT

public:
    HelperTest(QObject *parent = nullptr)
        : QObject(parent)
    {
    }

private Q_SLOTS:
    void initTestCase();
    void init()
    {
    }

    void testBasicActionExecution();
    void testExecuteJobSignals();
    void testTwoCalls();
    void testActionData();
    void testHelperFailure();

    void cleanup()
    {
    }
    void cleanupTestCase()
    {
    }

Q_SIGNALS:
    void changeCapabilities(KAuth::AuthBackend::Capabilities capabilities);

private:
    HelperHandler *m_handler;
};

HelperHandler::HelperHandler()
    : QObject(nullptr)
{
    /* Hello adventurer. What you see here might hurt your eyes, but let me explain why you don't want
       to touch this code. We are dealing with same-process async DBus requests, and if this seems obscure
       to you already please give up and forget about this function.
       Qt's local loop optimizations at the moment make it impossible to stream an async request to a process
       living on the same thread. So that's what we do: we instantiate a separate helperProxy and move it to
       a different thread - afterwards we can do everything as if we were in a separate process.

       If you are wondering if this means we'll have two helper proxies, you are right my friend. But please
       remember that helperProxy acts both as a client and as a server, so it makes total sense.

       tl;dr: Don't touch this and forget the weird questions in your head: it actually works.
    */

    m_thread = new QThread(this);
    moveToThread(m_thread);
    connect(m_thread, &QThread::started, this, &HelperHandler::init);
    m_thread->start();
}

void HelperHandler::init()
{
    qDebug() << "Initializing helper handler";
    qputenv("KAUTH_TEST_CALLER_ID", QDBusConnection::sessionBus().baseService().toUtf8());
    // Set up our Helper - of course, it is not in a separate process here so we need to copy what
    // HelperProxy::helperMain() does
    m_helperProxy = new DBusHelperProxy(QDBusConnection::sessionBus());
    m_helper = new TestHelper;
    // The timer is here just to prevent the app from crashing.
    QTimer *timer = new QTimer(nullptr);

    QVERIFY(m_helperProxy->initHelper(QLatin1String("org.kde.kf6auth.autotest")));

    m_helperProxy->setHelperResponder(m_helper);

    m_helper->setProperty("__KAuth_Helper_Shutdown_Timer", QVariant::fromValue(timer));
    timer->setInterval(10000);
    timer->start();

    // Make BackendsManager aware
    BackendsManager::setProxyForThread(m_thread, m_helperProxy);

    Q_EMIT ready();
}

void HelperTest::initTestCase()
{
    connect(this,
            SIGNAL(changeCapabilities(KAuth::AuthBackend::Capabilities)),
            KAuth::BackendsManager::authBackend(),
            SLOT(setNewCapabilities(KAuth::AuthBackend::Capabilities)));

    qRegisterMetaType<KAuth::Action::AuthStatus>();
    qRegisterMetaType<KJob *>();

    // Set up our HelperHandler
    m_handler = new HelperHandler;
    QEventLoop eventLoop;
    connect(m_handler, &HelperHandler::ready, &eventLoop, &QEventLoop::quit);
    qDebug() << "Waiting for HelperHandler to be initialized";
    eventLoop.exec();
}

void HelperTest::testBasicActionExecution()
{
    Q_EMIT changeCapabilities(KAuth::AuthBackend::AuthorizeFromHelperCapability);

    KAuth::Action action(QLatin1String("org.kde.kf6auth.autotest.standardaction"));
    action.setHelperId(QLatin1String("org.kde.kf6auth.autotest"));
    QVERIFY(action.isValid());

    QCOMPARE(action.status(), KAuth::Action::AuthRequiredStatus);
    KAuth::ExecuteJob *job = action.execute();

    QVERIFY(job->exec());

    QVERIFY(!job->error());
    QVERIFY(job->data().isEmpty());
}

void HelperTest::testExecuteJobSignals()
{
    KAuth::Action action(QLatin1String("org.kde.kf6auth.autotest.longaction"));
    action.setHelperId(QLatin1String("org.kde.kf6auth.autotest"));
    QVERIFY(action.isValid());

    QCOMPARE(action.status(), KAuth::Action::AuthRequiredStatus);

    KAuth::ExecuteJob *job = action.execute();

    QSignalSpy finishedSpy(job, &KJob::result);
    QSignalSpy newDataSpy(job, &KAuth::ExecuteJob::newData);
    QSignalSpy percentSpy(job, &KJob::percentChanged);
    QSignalSpy statusChangedSpy(job, &KAuth::ExecuteJob::statusChanged);

    QVERIFY(job->exec());

    QCOMPARE(finishedSpy.size(), 1);
    QCOMPARE(qobject_cast<KAuth::ExecuteJob *>(finishedSpy.first().first().value<KJob *>()), job);
    QCOMPARE(statusChangedSpy.size(), 1);
    QCOMPARE(statusChangedSpy.first().first().value<KAuth::Action::AuthStatus>(), KAuth::Action::AuthorizedStatus);
    QCOMPARE(percentSpy.size(), 100);
    for (ulong i = 1; i <= 100; ++i) {
        QCOMPARE((unsigned long)percentSpy.at(i - 1).last().toLongLong(), i);
        QCOMPARE(qobject_cast<KAuth::ExecuteJob *>(percentSpy.at(i - 1).first().value<KJob *>()), job);
    }
    QCOMPARE(newDataSpy.size(), 1);
    QCOMPARE(newDataSpy.first().first().value<QVariantMap>().value(QLatin1String("Answer")).toInt(), 42);

    QVERIFY(!job->error());
    QVERIFY(job->data().isEmpty());
}

void HelperTest::testTwoCalls()
{
    KAuth::Action action(QLatin1String("org.kde.kf6auth.autotest.standardaction"));
    action.setHelperId(QLatin1String("org.kde.kf6auth.autotest"));
    QVERIFY(action.isValid());

    QCOMPARE(action.status(), KAuth::Action::AuthRequiredStatus);
    KAuth::ExecuteJob *job = action.execute();
    QVERIFY(job->exec());

    job = action.execute();
    QVERIFY(job->exec());
}

void HelperTest::testActionData()
{
    KAuth::Action action(QLatin1String("org.kde.kf6auth.autotest.echoaction"));
    action.setHelperId(QLatin1String("org.kde.kf6auth.autotest"));

    QVariantMap args;
    // Fill with random data (and test heavy structures while we're at it)
    auto *generator = QRandomGenerator::global();
    for (int i = 0; i < 150; ++i) {
        args.insert(QUuid::createUuid().toString(), generator->generate());
    }
    action.setArguments(args);

    QVERIFY(action.isValid());

    QCOMPARE(action.status(), KAuth::Action::AuthRequiredStatus);
    KAuth::ExecuteJob *job = action.execute();

    QVERIFY(job->exec());

    QVERIFY(!job->error());
    QCOMPARE(job->data(), args);
}

void HelperTest::testHelperFailure()
{
    KAuth::Action action(QLatin1String("org.kde.kf6auth.autotest.failingaction"));
    action.setHelperId(QLatin1String("org.kde.kf6auth.autotest"));
    QVERIFY(action.isValid());
    QCOMPARE(action.status(), KAuth::Action::AuthRequiredStatus);
    KAuth::ExecuteJob *job = action.execute();
    QVERIFY(!job->exec());
    QVERIFY(job->error());
}

QTEST_MAIN(HelperTest)
#include "HelperTest.moc"