test_remote_access.cpp 15.6 KB
Newer Older
1
2
/********************************************************************
Copyright 2016  Oleg Chernovskiy <kanedias@xaker.ru>
3
Copyright 2018  Roman Gilg <subdiff@gmail.com>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

This library 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; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.

This library 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 library.  If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
// Qt
Laurent Montel's avatar
Laurent Montel committed
22
#include <QtTest>
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
// client
#include "../../src/client/connection_thread.h"
#include "../../src/client/event_queue.h"
#include "../../src/client/remote_access.h"
#include "../../src/client/registry.h"
#include "../../src/client/output.h"
// server
#include "../../src/server/display.h"
#include "../../src/server/output_interface.h"
#include "../../src/server/remote_access_interface.h"

#include <linux/input.h>

using namespace KWayland::Client;
using namespace KWayland::Server;

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();
52
    void testSendReleaseCrossScreen();
53
54
55
56
57
    void testSendClientGone();
    void testSendReceiveClientGone();

private:
    Display *m_display = nullptr;
58
    OutputInterface *m_outputInterface[2] = {nullptr};
59
    RemoteAccessManagerInterface *m_remoteAccessInterface = nullptr;
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
};

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};
77
78
79
80
};

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

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
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();
}

154
155
156
157
158
159
160
161
162
163
164
165
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();
166
167
168
169
170
171
172
173

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

174
175
176
177
178
179
180
181
182
183
184
185
186
    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; \
    }
187
188
    CLEANUP(m_outputInterface[0])
    CLEANUP(m_outputInterface[1])
189
190
191
192
193
194
195
196
197
198
199
    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());
200
201
202
    auto *client = new MockupClient(this);
    client->bindOutput(0);

203
204
205
    m_display->dispatchEvents();

    QVERIFY(m_remoteAccessInterface->isBound()); // we have one client now
206
    QSignalSpy bufferReadySpy(client->remoteAccess, &RemoteAccessManager::bufferReady);
207
208
209
210
211
212
213
214
215
216
    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);
217
    m_remoteAccessInterface->sendBufferReady(m_outputInterface[0], buf);
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

    // 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());
252
253
254
255
    auto *client1 = new MockupClient(this);
    auto *client2 = new MockupClient(this);
    client1->bindOutput(0);
    client2->bindOutput(0);
256
257
    m_display->dispatchEvents();
    QVERIFY(m_remoteAccessInterface->isBound()); // now we have 2 clients
258
259

    QSignalSpy bufferReadySpy1(client1->remoteAccess, &RemoteAccessManager::bufferReady);
260
    QVERIFY(bufferReadySpy1.isValid());
261
    QSignalSpy bufferReadySpy2(client2->remoteAccess, &RemoteAccessManager::bufferReady);
262
263
264
265
266
267
268
269
270
271
    QVERIFY(bufferReadySpy2.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);
272
    m_remoteAccessInterface->sendBufferReady(m_outputInterface[0], buf);
273
274
275
276
277
278
279
280
281

    // wait for event loop
    QVERIFY(bufferReadySpy1.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());

282
283
284
    if (bufferReadySpy2.size() == 0) {
        QVERIFY(bufferReadySpy2.wait());
    }
285
286
287
288
289
290
291
    // 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
292
    QVERIFY(paramsObtainedSpy1.size() == 1 || paramsObtainedSpy1.wait());
293
    QCOMPARE(paramsObtainedSpy1.size(), 1);
294
295
296
    if (paramsObtainedSpy2.size() == 0) {
        QVERIFY(paramsObtainedSpy2.wait());
    }
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
    QCOMPARE(paramsObtainedSpy2.size(), 1);

    // release
    QSignalSpy bufferReleasedSpy(m_remoteAccessInterface, &RemoteAccessManagerInterface::bufferReleased);
    QVERIFY(bufferReleasedSpy.isValid());
    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;
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
    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
    QVERIFY(paramsObtainedSpy1.wait());
    if (paramsObtainedSpy2.size() == 0) {
        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;
398
399
400
401
402
403
404
405
    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());
406
407
    auto *client = new MockupClient(this);
    client->bindOutput(0);
408
409
410
    m_display->dispatchEvents();

    QVERIFY(m_remoteAccessInterface->isBound()); // we have one client now
411
    QSignalSpy bufferReadySpy(client->remoteAccess, &RemoteAccessManager::bufferReady);
412
413
414
415
416
417
418
419
420
421
    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);
422
    m_remoteAccessInterface->sendBufferReady(m_outputInterface[0], buf);
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440

    // 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()
{
    // this test verifies that when buffer is sent, received and client is gone, 
    // both client and server will release buffer correctly
    QVERIFY(!m_remoteAccessInterface->isBound());
441
442
    auto *client = new MockupClient(this);
    client->bindOutput(0);
443
444
445
    m_display->dispatchEvents();

    QVERIFY(m_remoteAccessInterface->isBound()); // we have one client now
446
    QSignalSpy bufferReadySpy(client->remoteAccess, &RemoteAccessManager::bufferReady);
447
448
449
450
451
452
453
454
455
456
    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);
457
    m_remoteAccessInterface->sendBufferReady(m_outputInterface[0], buf);
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
483
484
485
486

    // 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"