test_remote_access.cpp 14.8 KB
Newer Older
1
2
3
4
5
6
/*
    SPDX-FileCopyrightText: 2016 Oleg Chernovskiy <kanedias@xaker.ru>
    SPDX-FileCopyrightText: 2018 Roman Gilg <subdiff@gmail.com>

    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
7
// Qt
Laurent Montel's avatar
Laurent Montel committed
8
#include <QtTest>
9
// client
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
10
11
12
13
14
#include "KWayland/Client/connection_thread.h"
#include "KWayland/Client/event_queue.h"
#include "KWayland/Client/remote_access.h"
#include "KWayland/Client/registry.h"
#include "KWayland/Client/output.h"
15
16
17
18
19
20
// server
#include "../../src/server/display.h"
#include "../../src/server/output_interface.h"
#include "../../src/server/remote_access_interface.h"

using namespace KWayland::Client;
21
using namespace KWaylandServer;
22
23
24
25
26
27
28
29
30
31
32
33
34
35

Q_DECLARE_METATYPE(const BufferHandle *)
Q_DECLARE_METATYPE(const RemoteBuffer *)
Q_DECLARE_METATYPE(const void *)

class RemoteAccessTest : public QObject
{
    Q_OBJECT
private Q_SLOTS:
    void init();
    void cleanup();

    void testSendReleaseSingle();
    void testSendReleaseMultiple();
36
    void testSendReleaseCrossScreen();
37
38
39
40
41
    void testSendClientGone();
    void testSendReceiveClientGone();

private:
    Display *m_display = nullptr;
42
    OutputInterface *m_outputInterface[2] = {nullptr};
43
    RemoteAccessManagerInterface *m_remoteAccessInterface = nullptr;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
};

class MockupClient : public QObject
{
    Q_OBJECT
public:
    MockupClient(QObject *parent = nullptr);
    ~MockupClient();

    void bindOutput(int index);

    ConnectionThread *connection = nullptr;
    QThread *thread = nullptr;
    EventQueue *queue = nullptr;
    Registry *registry = nullptr;
    RemoteAccessManager *remoteAccess = nullptr;
    Output *outputs[2] = {nullptr};
61
62
63
64
};

static const QString s_socketName = QStringLiteral("kwayland-test-remote-access-0");

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
MockupClient::MockupClient(QObject *parent)
    : QObject(parent)
{
    // setup connection
    connection = new KWayland::Client::ConnectionThread;
    QSignalSpy connectedSpy(connection, &ConnectionThread::connected);
    QVERIFY(connectedSpy.isValid());
    connection->setSocketName(s_socketName);

    thread = new QThread(this);
    connection->moveToThread(thread);
    thread->start();

    connection->initConnection();
    QVERIFY(connectedSpy.wait());

    queue = new EventQueue(this);
    queue->setup(connection);

    registry = new Registry(this);
    QSignalSpy interfacesAnnouncedSpy(registry, &Registry::interfacesAnnounced);
    QVERIFY(interfacesAnnouncedSpy.isValid());
    registry->setEventQueue(queue);
    registry->create(connection);
    QVERIFY(registry->isValid());
    registry->setup();
    QVERIFY(interfacesAnnouncedSpy.wait());

    remoteAccess = registry->createRemoteAccessManager(
                                            registry->interface(Registry::Interface::RemoteAccessManager).name,
                                            registry->interface(Registry::Interface::RemoteAccessManager).version,
                                            this);
    QVERIFY(remoteAccess->isValid());
    connection->flush();
}

MockupClient::~MockupClient()
{
#define CLEANUP(variable) \
    if (variable) { \
        delete variable; \
        variable = nullptr; \
    }
    CLEANUP(outputs[0])
    CLEANUP(outputs[1])
    CLEANUP(remoteAccess)
    CLEANUP(queue)
    CLEANUP(registry)
    if (thread) {
        if (connection) {
            connection->flush();
            connection->deleteLater();
            connection = nullptr;
        }

        thread->quit();
        thread->wait();
        delete thread;
        thread = nullptr;
    }
#undef CLEANUP
}

void MockupClient::bindOutput(int index)
{
    // client-bound output
    outputs[index] = registry->createOutput(registry->interfaces(Registry::Interface::Output)[index].name,
                             registry->interfaces(Registry::Interface::Output)[index].version,
                             this);
    QVERIFY(outputs[index]->isValid());
    connection->flush();
}

138
139
140
141
142
143
144
145
146
147
148
149
void RemoteAccessTest::init()
{
    qRegisterMetaType<const BufferHandle *>();
    qRegisterMetaType<const RemoteBuffer *>();
    qRegisterMetaType<const void *>();

    delete m_display;
    m_display = new Display(this);
    m_display->setSocketName(s_socketName);
    m_display->start();
    QVERIFY(m_display->isRunning());
    m_display->createShm();
150
151
152
153
154
155
156
157

    auto initOutputIface = [this](int i) {
        m_outputInterface[i] = m_display->createOutput();
        m_outputInterface[i]->create();
    };
    initOutputIface(0);
    initOutputIface(1);

158
159
160
161
162
163
164
165
166
167
168
169
170
    m_remoteAccessInterface = m_display->createRemoteAccessManager();
    m_remoteAccessInterface->create();
    QSignalSpy bufferReleasedSpy(m_remoteAccessInterface, &RemoteAccessManagerInterface::bufferReleased);
    QVERIFY(bufferReleasedSpy.isValid());
}

void RemoteAccessTest::cleanup()
{
#define CLEANUP(variable) \
    if (variable) { \
        delete variable; \
        variable = nullptr; \
    }
171
172
    CLEANUP(m_outputInterface[0])
    CLEANUP(m_outputInterface[1])
173
174
175
176
177
178
179
180
181
182
183
    CLEANUP(m_remoteAccessInterface)
    CLEANUP(m_display)
#undef CLEANUP
}

void RemoteAccessTest::testSendReleaseSingle()
{
    // this test verifies that a buffer is sent to client and returned back

    // setup
    QVERIFY(!m_remoteAccessInterface->isBound());
184
185
186
    auto *client = new MockupClient(this);
    client->bindOutput(0);

187
188
189
    m_display->dispatchEvents();

    QVERIFY(m_remoteAccessInterface->isBound()); // we have one client now
190
    QSignalSpy bufferReadySpy(client->remoteAccess, &RemoteAccessManager::bufferReady);
191
192
193
194
195
196
197
198
199
200
    QVERIFY(bufferReadySpy.isValid());

    BufferHandle *buf = new BufferHandle();
    QTemporaryFile *tmpFile = new QTemporaryFile(this);
    tmpFile->open();

    buf->setFd(tmpFile->handle());
    buf->setSize(50, 50);
    buf->setFormat(100500);
    buf->setStride(7800);
201
    m_remoteAccessInterface->sendBufferReady(m_outputInterface[0], buf);
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

    // receive buffer
    QVERIFY(bufferReadySpy.wait());
    auto rbuf = bufferReadySpy.takeFirst()[1].value<const RemoteBuffer *>();
    QSignalSpy paramsObtainedSpy(rbuf, &RemoteBuffer::parametersObtained);
    QVERIFY(paramsObtainedSpy.isValid());

    // wait for params
    QVERIFY(paramsObtainedSpy.wait());
    // client fd is different, not subject to check
    QCOMPARE(rbuf->width(), 50u);
    QCOMPARE(rbuf->height(), 50u);
    QCOMPARE(rbuf->format(), 100500u);
    QCOMPARE(rbuf->stride(), 7800u);

    // release
    QSignalSpy bufferReleasedSpy(m_remoteAccessInterface, &RemoteAccessManagerInterface::bufferReleased);
    QVERIFY(bufferReleasedSpy.isValid());
    delete rbuf;
    QVERIFY(bufferReleasedSpy.wait());

    // cleanup
    delete buf;
    delete client;
    m_display->dispatchEvents();
    QVERIFY(!m_remoteAccessInterface->isBound());
}

void RemoteAccessTest::testSendReleaseMultiple()
{
    // this test verifies that a buffer is sent to 2 clients and returned back

    // setup
    QVERIFY(!m_remoteAccessInterface->isBound());
236
237
238
239
    auto *client1 = new MockupClient(this);
    auto *client2 = new MockupClient(this);
    client1->bindOutput(0);
    client2->bindOutput(0);
240
241
    m_display->dispatchEvents();
    QVERIFY(m_remoteAccessInterface->isBound()); // now we have 2 clients
242

243
244
245
246
247
248
249
250
251
    BufferHandle *buf = new BufferHandle();
    QTemporaryFile *tmpFile = new QTemporaryFile(this);
    tmpFile->open();

    buf->setFd(tmpFile->handle());
    buf->setSize(50, 50);
    buf->setFormat(100500);
    buf->setStride(7800);

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
    bool buffer1ParametersReceived = false;
    bool buffer2ParametersReceived = false;

    const RemoteBuffer *rbuf1 = nullptr;
    const RemoteBuffer *rbuf2 = nullptr;

    client1->connection->flush();
    client2->connection->flush();

    {
        QEventLoop e;
        QTimer::singleShot(1000, &e, &QEventLoop::quit); //safety timer
        connect(client1->remoteAccess, &RemoteAccessManager::bufferReady, [&] (const void*, const RemoteBuffer *buffer) {
                rbuf1 = buffer;
                connect(rbuf1, &RemoteBuffer::parametersObtained, [&]() {
                    buffer1ParametersReceived = true;
                    if (buffer1ParametersReceived && buffer2ParametersReceived) {
                        e.quit();
                    }
                });
        });

        connect(client2->remoteAccess, &RemoteAccessManager::bufferReady, [&] (const void*, const RemoteBuffer *buffer) {
            rbuf2 = buffer;
            connect(rbuf2, &RemoteBuffer::parametersObtained, [&]() {
                buffer2ParametersReceived = true;
                if (buffer1ParametersReceived && buffer2ParametersReceived) {
                    e.quit();
                }
            });
        });
        m_remoteAccessInterface->sendBufferReady(m_outputInterface[0], buf);

        e.exec();
286
    }
287
288
289
290
291
292


    QVERIFY(rbuf1);
    QVERIFY(rbuf2);
    QVERIFY(buffer1ParametersReceived);
    QVERIFY(buffer2ParametersReceived);
293
294
295
296
297
298
299
300
301
302
303
304
305

    // release
    QSignalSpy bufferReleasedSpy(m_remoteAccessInterface, &RemoteAccessManagerInterface::bufferReleased);
    delete rbuf1;
    QVERIFY(!bufferReleasedSpy.wait(1000)); // one client released, second still holds buffer!

    delete rbuf2;
    QVERIFY(bufferReleasedSpy.wait()); // all clients released, buffer should be freed

    // cleanup
    delete buf;
    delete client1;
    delete client2;
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
    m_display->dispatchEvents();
    QVERIFY(!m_remoteAccessInterface->isBound());
}

void RemoteAccessTest::testSendReleaseCrossScreen()
{
    // this test verifies that multiple buffers for multiple screens are sent to
    // multiple clients and returned back

    // setup
    QVERIFY(!m_remoteAccessInterface->isBound());
    auto *client1 = new MockupClient(this);
    auto *client2 = new MockupClient(this);
    client1->bindOutput(1);
    client2->bindOutput(0);
    m_display->dispatchEvents();
    QVERIFY(m_remoteAccessInterface->isBound()); // now we have 2 clients

    QSignalSpy bufferReadySpy1(client1->remoteAccess, &RemoteAccessManager::bufferReady);
    QVERIFY(bufferReadySpy1.isValid());
    QSignalSpy bufferReadySpy2(client2->remoteAccess, &RemoteAccessManager::bufferReady);
    QVERIFY(bufferReadySpy2.isValid());

    BufferHandle *buf1 = new BufferHandle();
    QTemporaryFile *tmpFile1 = new QTemporaryFile(this);
    tmpFile1->open();

    BufferHandle *buf2 = new BufferHandle();
    QTemporaryFile *tmpFile2 = new QTemporaryFile(this);
    tmpFile2->open();

    buf1->setFd(tmpFile1->handle());
    buf1->setSize(50, 50);
    buf1->setFormat(100500);
    buf1->setStride(7800);

    buf2->setFd(tmpFile2->handle());
    buf2->setSize(100, 100);
    buf2->setFormat(100500);
    buf2->setStride(7800);

    m_remoteAccessInterface->sendBufferReady(m_outputInterface[0], buf1);
    m_remoteAccessInterface->sendBufferReady(m_outputInterface[1], buf2);

    // wait for event loop
    QVERIFY(bufferReadySpy1.wait());
    if (bufferReadySpy2.size() == 0) {
        QVERIFY(bufferReadySpy2.wait());
    }

    // receive buffer at client 1
    QCOMPARE(bufferReadySpy1.size(), 1);
    auto rbuf1 = bufferReadySpy1.takeFirst()[1].value<const RemoteBuffer *>();
    QSignalSpy paramsObtainedSpy1(rbuf1, &RemoteBuffer::parametersObtained);
    QVERIFY(paramsObtainedSpy1.isValid());

    // receive buffer at client 2
    QCOMPARE(bufferReadySpy2.size(), 1);
    auto rbuf2 = bufferReadySpy2.takeFirst()[1].value<const RemoteBuffer *>();
    QSignalSpy paramsObtainedSpy2(rbuf2, &RemoteBuffer::parametersObtained);
    QVERIFY(paramsObtainedSpy2.isValid());

    // wait for event loop
369
370
371
372
    if (paramsObtainedSpy1.isEmpty()) {
        QVERIFY(paramsObtainedSpy1.wait());
    }
    if (paramsObtainedSpy2.isEmpty()) {
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
        QVERIFY(paramsObtainedSpy2.wait());
    }
    QCOMPARE(paramsObtainedSpy1.size(), 1);
    QCOMPARE(paramsObtainedSpy2.size(), 1);

    // release
    QSignalSpy bufferReleasedSpy(m_remoteAccessInterface, &RemoteAccessManagerInterface::bufferReleased);
    QVERIFY(bufferReleasedSpy.isValid());
    delete rbuf1;
    QVERIFY(bufferReleasedSpy.wait());

    delete rbuf2;
    QVERIFY(bufferReleasedSpy.wait());

    QCOMPARE(bufferReleasedSpy.size(), 2);

    // cleanup
    delete buf1;
    delete buf2;
    delete client1;
    delete client2;
394
395
396
397
398
399
400
401
    m_display->dispatchEvents();
    QVERIFY(!m_remoteAccessInterface->isBound());
}

void RemoteAccessTest::testSendClientGone()
{
    // this test verifies that when buffer is sent and client is gone, server will release buffer correctly
    QVERIFY(!m_remoteAccessInterface->isBound());
402
403
    auto *client = new MockupClient(this);
    client->bindOutput(0);
404
405
406
    m_display->dispatchEvents();

    QVERIFY(m_remoteAccessInterface->isBound()); // we have one client now
407
    QSignalSpy bufferReadySpy(client->remoteAccess, &RemoteAccessManager::bufferReady);
408
409
410
411
412
413
414
415
416
417
    QVERIFY(bufferReadySpy.isValid());

    BufferHandle *buf = new BufferHandle();
    QTemporaryFile *tmpFile = new QTemporaryFile(this);
    tmpFile->open();

    buf->setFd(tmpFile->handle());
    buf->setSize(50, 50);
    buf->setFormat(100500);
    buf->setStride(7800);
418
    m_remoteAccessInterface->sendBufferReady(m_outputInterface[0], buf);
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433

    // release forcefully
    QSignalSpy bufferReleasedSpy(m_remoteAccessInterface, &RemoteAccessManagerInterface::bufferReleased);
    QVERIFY(bufferReleasedSpy.isValid());
    delete client;
    QVERIFY(bufferReleasedSpy.wait());

    // cleanup
    delete buf;
    m_display->dispatchEvents();
    QVERIFY(!m_remoteAccessInterface->isBound());
}

void RemoteAccessTest::testSendReceiveClientGone()
{
434
    // this test verifies that when buffer is sent, received and client is gone,
435
436
    // both client and server will release buffer correctly
    QVERIFY(!m_remoteAccessInterface->isBound());
437
438
    auto *client = new MockupClient(this);
    client->bindOutput(0);
439
440
441
    m_display->dispatchEvents();

    QVERIFY(m_remoteAccessInterface->isBound()); // we have one client now
442
    QSignalSpy bufferReadySpy(client->remoteAccess, &RemoteAccessManager::bufferReady);
443
444
445
446
447
448
449
450
451
452
    QVERIFY(bufferReadySpy.isValid());

    BufferHandle *buf = new BufferHandle();
    QTemporaryFile *tmpFile = new QTemporaryFile(this);
    tmpFile->open();

    buf->setFd(tmpFile->handle());
    buf->setSize(50, 50);
    buf->setFormat(100500);
    buf->setStride(7800);
453
    m_remoteAccessInterface->sendBufferReady(m_outputInterface[0], buf);
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482

    // receive buffer
    QVERIFY(bufferReadySpy.wait());
    auto rbuf = bufferReadySpy.takeFirst()[1].value<const RemoteBuffer *>();
    QSignalSpy paramsObtainedSpy(rbuf, &RemoteBuffer::parametersObtained);
    QVERIFY(paramsObtainedSpy.isValid());

    // wait for params
    QVERIFY(paramsObtainedSpy.wait());
    // client fd is different, not subject to check
    QCOMPARE(rbuf->width(), 50u);
    QCOMPARE(rbuf->height(), 50u);
    QCOMPARE(rbuf->format(), 100500u);
    QCOMPARE(rbuf->stride(), 7800u);

    // release forcefully
    QSignalSpy bufferReleasedSpy(m_remoteAccessInterface, &RemoteAccessManagerInterface::bufferReleased);
    QVERIFY(bufferReleasedSpy.isValid());
    delete client;
    QVERIFY(bufferReleasedSpy.wait());

    // cleanup
    delete buf;
    m_display->dispatchEvents();
    QVERIFY(!m_remoteAccessInterface->isBound());
}

QTEST_GUILESS_MAIN(RemoteAccessTest)
#include "test_remote_access.moc"