File: sessionbackendtest.cpp

package info (click to toggle)
lomiri 0.5.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 24,088 kB
  • sloc: cpp: 39,498; python: 2,559; javascript: 1,426; ansic: 1,012; sh: 289; xml: 252; makefile: 69
file content (289 lines) | stat: -rw-r--r-- 11,507 bytes parent folder | download | duplicates (4)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * Copyright 2013-2015 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *      Ying-Chun Liu (PaulLiu) <paul.liu@canonical.com>
 *      Lukáš Tinkl <lukas.tinkl@canonical.com>
 */

#include <QtTest>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusInterface>
#include <QDBusReply>
#include <QDBusVariant>
#include <QDebug>
#include <QDBusObjectPath>

#include <unistd.h>
#include <sys/types.h>

#include "dbuslomirisessionservice.h"

enum class Action : unsigned
{
  LOGOUT = 0,
  SHUTDOWN,
  REBOOT,
  NONE
};

class SessionBackendTest : public QObject
{
    Q_OBJECT

private Q_SLOTS:

    void initTestCase() {
        dbusLomiriSession = new QDBusInterface ("com.lomiri.Shell",
                                               "/com/lomiri/Shell/Session",
                                               "com.lomiri.Shell.Session",
                                               QDBusConnection::sessionBus());
    }

    void init() {
        qputenv("TEST_USER", "testuser");
        qputenv("TEST_NOPASSWD_USERS", "");
    }

    void testLomiriSessionLogoutRequested_data() {
        QTest::addColumn<QString>("method");
        QTest::addColumn<QString>("signal");

        QTest::newRow("Logout") << "RequestLogout" << "LogoutRequested(bool)";
        QTest::newRow("Reboot") << "RequestReboot" << "RebootRequested(bool)";
        QTest::newRow("Shutdown") << "RequestShutdown" << "ShutdownRequested(bool)";
        QTest::newRow("PromptLock") << "PromptLock" << "LockRequested()";
        QTest::newRow("Lock") << "Lock" << "LockRequested()";
        QTest::newRow("LockLightDM") << "Lock" << "Locked()"; // happens when we lock LightDM, only for Lock()
    }

    void testLomiriSessionLogoutRequested() {
        QFETCH(QString, method);
        QFETCH(QString, signal);

        DBusLomiriSessionService dbusLomiriSessionService;
        QCoreApplication::processEvents(); // to let the service register on DBus

        // .. because QSignalSpy checks the signal signature like this: "if (((aSignal[0] - '0') & 0x03) != QSIGNAL_CODE)"
        QSignalSpy spy(&dbusLomiriSessionService, qPrintable(signal.prepend(QSIGNAL_CODE)));

        QDBusReply<void> reply = dbusLomiriSession->call(method);
        QCOMPARE(reply.isValid(), true);

        QTRY_COMPARE(spy.count(), 1);
    }

    void testGnomeSessionWrapperWithoutConfirmation_data() {
        QTest::addColumn<QString>("method");
        QTest::addColumn<QString>("signal");

        QTest::newRow("Reboot") << "RequestReboot" << SIGNAL(RebootCalled(bool));
        QTest::newRow("Shutdown") << "RequestShutdown" << SIGNAL(PowerOffCalled(bool));
    }

    void testGnomeSessionWrapperWithoutConfirmation() {
        QFETCH(QString, method);
        QFETCH(QString, signal);

        DBusLomiriSessionService dbusLomiriSessionService;
        DBusGnomeSessionManagerWrapper dbusGnomeSessionManagerWrapper;
        QCoreApplication::processEvents(); // to let the services register on DBus

        QDBusInterface login1Interface("org.freedesktop.login1",
                                      "/logindsession",
                                      "org.freedesktop.login1.Manager");
        QSignalSpy spy(&login1Interface, signal.toUtf8().data());

        QDBusInterface dbusGnomeSessionWrapper("org.gnome.SessionManager",
                                               "/org/gnome/SessionManager",
                                               "org.gnome.SessionManager",
                                               QDBusConnection::sessionBus());
        QVERIFY(dbusGnomeSessionWrapper.isValid());

        QDBusReply<void> reply = dbusGnomeSessionWrapper.call(method);
        QVERIFY(reply.isValid());
        QTRY_COMPARE(spy.count(), 1);
    }

    void testGnomeSessionWrapperWithConfirmation_data() {
        QTest::addColumn<QString>("method");
        QTest::addColumn<QString>("signal");

        QTest::newRow("Reboot") << "Reboot" << SIGNAL(RebootRequested(bool));
        QTest::newRow("Shutdown") << "Shutdown" << SIGNAL(ShutdownRequested(bool));
    }

    void testGnomeSessionWrapperWithConfirmation() {
        QFETCH(QString, method);
        QFETCH(QString, signal);

        DBusLomiriSessionService dbusLomiriSessionService;
        DBusGnomeSessionManagerWrapper dbusGnomeSessionManagerWrapper;
        QCoreApplication::processEvents(); // to let the services register on DBus

        QSignalSpy spy(&dbusLomiriSessionService, signal.toUtf8().data());

        QDBusInterface dbusGnomeSessionWrapper("org.gnome.SessionManager",
                                               "/org/gnome/SessionManager",
                                               "org.gnome.SessionManager",
                                               QDBusConnection::sessionBus());
        QVERIFY(dbusGnomeSessionWrapper.isValid());

        QDBusReply<void> reply = dbusGnomeSessionWrapper.call(method);
        QVERIFY(reply.isValid());
        QCOMPARE(spy.count(), 1);
    }

    void testGnomeSessionWrapperLogout_data() {
        QTest::addColumn<int>("mode");
        QTest::addColumn<QString>("signal");

        QTest::newRow("Logout") << 0 << SIGNAL(LogoutRequested(bool));
        QTest::newRow("LogoutNoDialog") << 1 << SIGNAL(LogoutReady());
        QTest::newRow("LogoutNoInhibits") << 2 << SIGNAL(LogoutReady());
        QTest::newRow("LogoutNoDialogNoInhibits") << 3 << SIGNAL(LogoutReady());
    }

    void testGnomeSessionWrapperLogout() {
        QFETCH(int, mode);
        QFETCH(QString, signal);

        DBusLomiriSessionService dbusLomiriSessionService;
        DBusGnomeSessionManagerWrapper dbusGnomeSessionManagerWrapper;
        QCoreApplication::processEvents(); // to let the services register on DBus

        QSignalSpy spy(&dbusLomiriSessionService, signal.toUtf8().data());

        QDBusInterface dbusGnomeSessionWrapper("org.gnome.SessionManager",
                                               "/org/gnome/SessionManager",
                                               "org.gnome.SessionManager",
                                               QDBusConnection::sessionBus());
        QVERIFY(dbusGnomeSessionWrapper.isValid());

        QDBusReply<void> reply = dbusGnomeSessionWrapper.call("Logout", (quint32)mode);
        QVERIFY(reply.isValid());
        QCOMPARE(spy.count(), 1);
    }

    void testGnomeSessionDialogWrapper_data() {
        QTest::addColumn<uint>("method");
        QTest::addColumn<QString>("signal");

        QTest::newRow("Logout") << (uint)Action::LOGOUT << "LogoutRequested(bool)";
        QTest::newRow("Shutdown") << (uint)Action::SHUTDOWN << "ShutdownRequested(bool)";
        QTest::newRow("Reboot") << (uint)Action::REBOOT << "RebootRequested(bool)";
    }

    void testGnomeSessionDialogWrapper() {
        QFETCH(uint, method);
        QFETCH(QString, signal);

        DBusLomiriSessionService dbusLomiriSessionService;
        QCoreApplication::processEvents(); // to let the service register on DBus

        // Spy on the given signal on the /com/lomiri/Shell/Session object
        // as proof we are actually calling the actual method.
        // .. because QSignalSpy checks the signal signature like this: "if (((aSignal[0] - '0') & 0x03) != QSIGNAL_CODE)"
        QSignalSpy spy(&dbusLomiriSessionService, qPrintable(signal.prepend(QSIGNAL_CODE)));

        DBusGnomeSessionManagerDialogWrapper dbusGnomeSessionManagerDialogWrapper;
        QCoreApplication::processEvents(); // to let the service register on DBus

        QDBusInterface dbusGnomeSessionWrapper("com.lomiri.Shell",
                                               "/org/gnome/SessionManager/EndSessionDialog",
                                               "org.gnome.SessionManager.EndSessionDialog",
                                               QDBusConnection::sessionBus());

        QCOMPARE(dbusGnomeSessionWrapper.isValid(), true);

        // Set the QVariant as a QList<QDBusObjectPath> type
        QDbusList var;
        QVariant inhibitors;
        inhibitors.setValue(var);

        QDBusReply<void> reply = dbusGnomeSessionWrapper.call("Open", method, (unsigned)0, (unsigned)0, inhibitors);
        QCOMPARE(reply.isValid(), true);

        // Make sure we see the signal being emitted.
        QCOMPARE(spy.count(), 1);
    }

    void testUnlockFromLogind() {
        DBusLomiriSessionService dbusLomiriSessionService;
        QCoreApplication::processEvents(); // to let the service register on DBus

        QDBusInterface iface("org.freedesktop.login1", "/logindsession", "org.freedesktop.login1.Session");
        QVERIFY(iface.isValid());

        QSignalSpy spy(&dbusLomiriSessionService, SIGNAL(Unlocked()));
        QCOMPARE(iface.call("MockEmitUnlock").errorMessage(), QString());
        QTRY_COMPARE(spy.count(), 1);
    }

    void testUserName() {
        DBusLomiriSessionService dbusLomiriSessionService;
        QCOMPARE(dbusLomiriSessionService.UserName(), QString("testuser"));
    }

    void testRealName() {
        DBusLomiriSessionService dbusLomiriSessionService;
        QCoreApplication::processEvents(); // to let the service register on DBus

        QDBusInterface accIface("org.freedesktop.Accounts", "/org/freedesktop/Accounts", "org.freedesktop.Accounts", QDBusConnection::systemBus());
        if (accIface.isValid()) {
            QDBusReply<QDBusObjectPath> userPath = accIface.asyncCall("FindUserById", static_cast<qint64>(geteuid()));
            if (userPath.isValid()) {
                QDBusInterface userAccIface("org.freedesktop.Accounts", userPath.value().path(), "org.freedesktop.Accounts.User", QDBusConnection::systemBus());
                QCOMPARE(dbusLomiriSessionService.RealName(), userAccIface.property("RealName").toString());
            }
        }
    }

    void testCanLock() {
        DBusLomiriSessionService dbusLomiriSessionService;

        qputenv("TEST_USER", "testuser");
        QVERIFY(dbusLomiriSessionService.CanLock());

        qputenv("TEST_USER", "guest-abcdef"); // guest-* can't lock
        QVERIFY(!dbusLomiriSessionService.CanLock());

        qputenv("TEST_USER", "testuser");
        qputenv("TEST_NOPASSWD_USERS", "testuser"); // nopasswdlogin can't lock
        QVERIFY(!dbusLomiriSessionService.CanLock());
    }

    void testPromptLockRespectsCanLock() {
        DBusLomiriSessionService dbusLomiriSessionService;

        QSignalSpy spy(&dbusLomiriSessionService, SIGNAL(lockRequested()));
        QVERIFY(dbusLomiriSessionService.CanLock());
        dbusLomiriSessionService.PromptLock();
        QCOMPARE(spy.count(), 1);

        spy.clear();
        qputenv("TEST_USER", "guest-abcdef");
        QVERIFY(!dbusLomiriSessionService.CanLock());
        dbusLomiriSessionService.PromptLock();
        QCOMPARE(spy.count(), 0);
    }

private:
    QDBusInterface *dbusLomiriSession;
};

QTEST_GUILESS_MAIN(SessionBackendTest)
#include "sessionbackendtest.moc"