2020-03-15 15:19:28 +00:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
|
2014-08-26 14:07:39 +00:00
|
|
|
|
2020-03-15 15:19:28 +00:00
|
|
|
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
|
|
|
|
*/
|
2014-08-26 14:07:39 +00:00
|
|
|
// Qt
|
2018-11-06 06:22:36 +00:00
|
|
|
#include <QtTest>
|
2014-08-26 14:07:39 +00:00
|
|
|
// WaylandServer
|
2014-11-17 15:01:18 +00:00
|
|
|
#include "../../src/server/clientconnection.h"
|
2021-08-29 05:11:06 +00:00
|
|
|
#include "../../src/server/display.h"
|
2014-09-17 12:35:33 +00:00
|
|
|
#include "../../src/server/output_interface.h"
|
2021-07-23 14:12:28 +00:00
|
|
|
#include "../../src/server/outputmanagement_v2_interface.h"
|
2014-11-17 15:01:18 +00:00
|
|
|
// Wayland
|
|
|
|
#include <wayland-server.h>
|
|
|
|
// system
|
|
|
|
#include <sys/socket.h>
|
2021-08-29 05:11:06 +00:00
|
|
|
#include <sys/types.h>
|
2014-11-17 15:01:18 +00:00
|
|
|
#include <unistd.h>
|
2014-08-26 14:07:39 +00:00
|
|
|
|
2020-04-29 14:56:38 +00:00
|
|
|
using namespace KWaylandServer;
|
2014-08-26 14:07:39 +00:00
|
|
|
|
|
|
|
class TestWaylandServerDisplay : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
private Q_SLOTS:
|
|
|
|
void testSocketName();
|
|
|
|
void testStartStop();
|
2014-09-03 18:01:54 +00:00
|
|
|
void testAddRemoveOutput();
|
2014-11-17 15:01:18 +00:00
|
|
|
void testClientConnection();
|
2014-11-27 15:42:16 +00:00
|
|
|
void testConnectNoSocket();
|
2015-11-04 14:38:10 +00:00
|
|
|
void testOutputManagement();
|
2018-11-23 13:42:18 +00:00
|
|
|
void testAutoSocketName();
|
2014-08-26 14:07:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void TestWaylandServerDisplay::testSocketName()
|
|
|
|
{
|
2022-03-05 12:02:08 +00:00
|
|
|
KWaylandServer::Display display;
|
|
|
|
QSignalSpy changedSpy(&display, &KWaylandServer::Display::socketNamesChanged);
|
2014-08-26 14:07:39 +00:00
|
|
|
QVERIFY(changedSpy.isValid());
|
2020-10-19 15:52:56 +00:00
|
|
|
QCOMPARE(display.socketNames(), QStringList());
|
2014-08-26 14:07:39 +00:00
|
|
|
const QString testSName = QStringLiteral("fooBar");
|
2020-10-19 15:52:56 +00:00
|
|
|
display.addSocketName(testSName);
|
2021-08-29 05:11:06 +00:00
|
|
|
QCOMPARE(display.socketNames(), QStringList{testSName});
|
2014-08-26 14:07:39 +00:00
|
|
|
QCOMPARE(changedSpy.count(), 1);
|
|
|
|
|
|
|
|
// changing to same name again should not emit signal
|
2020-10-19 15:52:56 +00:00
|
|
|
display.addSocketName(testSName);
|
2014-08-26 14:07:39 +00:00
|
|
|
QCOMPARE(changedSpy.count(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestWaylandServerDisplay::testStartStop()
|
|
|
|
{
|
|
|
|
const QString testSocketName = QStringLiteral("kwin-wayland-server-display-test-0");
|
|
|
|
QDir runtimeDir(qgetenv("XDG_RUNTIME_DIR"));
|
|
|
|
QVERIFY(runtimeDir.exists());
|
|
|
|
QVERIFY(!runtimeDir.exists(testSocketName));
|
|
|
|
|
2022-03-05 12:02:08 +00:00
|
|
|
QScopedPointer<KWaylandServer::Display> display(new KWaylandServer::Display);
|
2020-07-06 09:36:25 +00:00
|
|
|
QSignalSpy runningSpy(display.data(), SIGNAL(runningChanged(bool)));
|
2014-08-26 14:07:39 +00:00
|
|
|
QVERIFY(runningSpy.isValid());
|
2020-10-19 15:52:56 +00:00
|
|
|
display->addSocketName(testSocketName);
|
2020-07-06 09:36:25 +00:00
|
|
|
QVERIFY(!display->isRunning());
|
|
|
|
display->start();
|
2021-08-29 05:11:06 +00:00
|
|
|
// QVERIFY(runningSpy.wait());
|
2014-08-26 14:07:39 +00:00
|
|
|
QCOMPARE(runningSpy.count(), 1);
|
|
|
|
QVERIFY(runningSpy.first().first().toBool());
|
2020-07-06 09:36:25 +00:00
|
|
|
QVERIFY(display->isRunning());
|
2014-08-26 14:07:39 +00:00
|
|
|
QVERIFY(runtimeDir.exists(testSocketName));
|
|
|
|
|
2020-07-06 09:36:25 +00:00
|
|
|
display.reset();
|
2014-08-26 14:07:39 +00:00
|
|
|
QVERIFY(!runtimeDir.exists(testSocketName));
|
|
|
|
}
|
|
|
|
|
2014-09-03 18:01:54 +00:00
|
|
|
void TestWaylandServerDisplay::testAddRemoveOutput()
|
|
|
|
{
|
2022-03-05 12:02:08 +00:00
|
|
|
KWaylandServer::Display display;
|
2020-10-19 15:52:56 +00:00
|
|
|
display.addSocketName(QStringLiteral("kwin-wayland-server-display-test-output-0"));
|
2014-09-03 18:01:54 +00:00
|
|
|
display.start();
|
|
|
|
|
2020-12-09 20:13:19 +00:00
|
|
|
OutputInterface *output = new OutputInterface(&display);
|
2014-09-03 18:01:54 +00:00
|
|
|
QCOMPARE(display.outputs().size(), 1);
|
|
|
|
QCOMPARE(display.outputs().first(), output);
|
2020-12-09 20:13:19 +00:00
|
|
|
|
|
|
|
delete output;
|
2014-09-03 18:01:54 +00:00
|
|
|
QVERIFY(display.outputs().isEmpty());
|
|
|
|
}
|
|
|
|
|
2014-11-17 15:01:18 +00:00
|
|
|
void TestWaylandServerDisplay::testClientConnection()
|
|
|
|
{
|
2022-03-05 12:02:08 +00:00
|
|
|
KWaylandServer::Display display;
|
2020-10-19 15:52:56 +00:00
|
|
|
display.addSocketName(QStringLiteral("kwin-wayland-server-display-test-client-connection"));
|
2014-11-17 15:01:18 +00:00
|
|
|
display.start();
|
2021-08-29 05:11:06 +00:00
|
|
|
QSignalSpy connectedSpy(&display, SIGNAL(clientConnected(KWaylandServer::ClientConnection *)));
|
2014-11-17 15:01:18 +00:00
|
|
|
QVERIFY(connectedSpy.isValid());
|
2021-08-29 05:11:06 +00:00
|
|
|
QSignalSpy disconnectedSpy(&display, SIGNAL(clientDisconnected(KWaylandServer::ClientConnection *)));
|
2014-11-17 15:01:18 +00:00
|
|
|
QVERIFY(disconnectedSpy.isValid());
|
|
|
|
|
|
|
|
int sv[2];
|
|
|
|
QVERIFY(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) >= 0);
|
|
|
|
|
|
|
|
auto client = wl_client_create(display, sv[0]);
|
|
|
|
QVERIFY(client);
|
|
|
|
|
|
|
|
QVERIFY(connectedSpy.isEmpty());
|
2014-11-17 17:13:28 +00:00
|
|
|
QVERIFY(display.connections().isEmpty());
|
2014-11-17 15:01:18 +00:00
|
|
|
ClientConnection *connection = display.getConnection(client);
|
|
|
|
QVERIFY(connection);
|
|
|
|
QCOMPARE(connection->client(), client);
|
2017-07-01 15:01:18 +00:00
|
|
|
if (getuid() == 0) {
|
|
|
|
QEXPECT_FAIL("", "Please don't run test as root", Continue);
|
|
|
|
}
|
2014-11-17 15:01:18 +00:00
|
|
|
QVERIFY(connection->userId() != 0);
|
2017-07-01 15:01:18 +00:00
|
|
|
if (getgid() == 0) {
|
|
|
|
QEXPECT_FAIL("", "Please don't run test as root", Continue);
|
|
|
|
}
|
2014-11-17 15:01:18 +00:00
|
|
|
QVERIFY(connection->groupId() != 0);
|
|
|
|
QVERIFY(connection->processId() != 0);
|
2014-11-17 15:21:54 +00:00
|
|
|
QCOMPARE(connection->display(), &display);
|
2017-07-29 06:40:11 +00:00
|
|
|
QCOMPARE(connection->executablePath(), QCoreApplication::applicationFilePath());
|
2021-08-29 05:11:06 +00:00
|
|
|
QCOMPARE((wl_client *)*connection, client);
|
2014-11-17 15:01:18 +00:00
|
|
|
const ClientConnection &constRef = *connection;
|
2021-08-29 05:11:06 +00:00
|
|
|
QCOMPARE((wl_client *)constRef, client);
|
2014-11-17 15:01:18 +00:00
|
|
|
QCOMPARE(connectedSpy.count(), 1);
|
2021-08-29 05:11:06 +00:00
|
|
|
QCOMPARE(connectedSpy.first().first().value<ClientConnection *>(), connection);
|
2014-11-17 17:13:28 +00:00
|
|
|
QCOMPARE(display.connections().count(), 1);
|
|
|
|
QCOMPARE(display.connections().first(), connection);
|
2014-11-17 15:01:18 +00:00
|
|
|
|
|
|
|
QCOMPARE(connection, display.getConnection(client));
|
|
|
|
QCOMPARE(connectedSpy.count(), 1);
|
|
|
|
|
|
|
|
// create a second client
|
|
|
|
int sv2[2];
|
|
|
|
QVERIFY(socketpair(AF_UNIX, SOCK_STREAM, 0, sv2) >= 0);
|
2014-11-27 15:16:54 +00:00
|
|
|
auto client2 = display.createClient(sv2[0]);
|
2014-11-17 15:01:18 +00:00
|
|
|
QVERIFY(client2);
|
2014-11-27 15:16:54 +00:00
|
|
|
ClientConnection *connection2 = display.getConnection(client2->client());
|
2014-11-17 15:01:18 +00:00
|
|
|
QVERIFY(connection2);
|
2014-11-27 15:16:54 +00:00
|
|
|
QCOMPARE(connection2, client2);
|
2014-11-17 15:01:18 +00:00
|
|
|
QCOMPARE(connectedSpy.count(), 2);
|
2021-08-29 05:11:06 +00:00
|
|
|
QCOMPARE(connectedSpy.first().first().value<ClientConnection *>(), connection);
|
|
|
|
QCOMPARE(connectedSpy.last().first().value<ClientConnection *>(), connection2);
|
|
|
|
QCOMPARE(connectedSpy.last().first().value<ClientConnection *>(), client2);
|
2014-11-17 17:13:28 +00:00
|
|
|
QCOMPARE(display.connections().count(), 2);
|
|
|
|
QCOMPARE(display.connections().first(), connection);
|
|
|
|
QCOMPARE(display.connections().last(), connection2);
|
2014-11-27 15:16:54 +00:00
|
|
|
QCOMPARE(display.connections().last(), client2);
|
2014-11-17 15:01:18 +00:00
|
|
|
|
|
|
|
// and destroy
|
|
|
|
QVERIFY(disconnectedSpy.isEmpty());
|
|
|
|
wl_client_destroy(client);
|
|
|
|
QCOMPARE(disconnectedSpy.count(), 1);
|
2016-10-10 11:41:42 +00:00
|
|
|
QSignalSpy clientDestroyedSpy(client2, &QObject::destroyed);
|
|
|
|
QVERIFY(clientDestroyedSpy.isValid());
|
2015-11-10 12:27:27 +00:00
|
|
|
client2->destroy();
|
2016-10-10 11:41:42 +00:00
|
|
|
QVERIFY(clientDestroyedSpy.wait());
|
2014-11-17 15:01:18 +00:00
|
|
|
QCOMPARE(disconnectedSpy.count(), 2);
|
|
|
|
close(sv[0]);
|
|
|
|
close(sv[1]);
|
|
|
|
close(sv2[0]);
|
|
|
|
close(sv2[1]);
|
2014-11-17 17:13:28 +00:00
|
|
|
QVERIFY(display.connections().isEmpty());
|
2014-11-17 15:01:18 +00:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:42:16 +00:00
|
|
|
void TestWaylandServerDisplay::testConnectNoSocket()
|
|
|
|
{
|
2022-03-05 12:02:08 +00:00
|
|
|
KWaylandServer::Display display;
|
2020-10-19 15:52:56 +00:00
|
|
|
display.start();
|
2014-11-27 15:42:16 +00:00
|
|
|
QVERIFY(display.isRunning());
|
|
|
|
|
|
|
|
// let's try connecting a client
|
|
|
|
int sv[2];
|
|
|
|
QVERIFY(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) >= 0);
|
|
|
|
auto client = display.createClient(sv[0]);
|
|
|
|
QVERIFY(client);
|
|
|
|
|
|
|
|
wl_client_destroy(client->client());
|
|
|
|
close(sv[0]);
|
|
|
|
close(sv[1]);
|
|
|
|
}
|
|
|
|
|
2015-11-04 14:38:10 +00:00
|
|
|
void TestWaylandServerDisplay::testOutputManagement()
|
|
|
|
{
|
2022-03-05 12:02:08 +00:00
|
|
|
KWaylandServer::Display display;
|
2020-10-19 15:52:56 +00:00
|
|
|
display.addSocketName("kwayland-test-0");
|
2018-06-15 11:23:10 +00:00
|
|
|
display.start();
|
2021-07-23 14:12:28 +00:00
|
|
|
new OutputManagementV2Interface(&display, this);
|
2015-11-04 14:38:10 +00:00
|
|
|
}
|
|
|
|
|
2018-11-23 13:42:18 +00:00
|
|
|
void TestWaylandServerDisplay::testAutoSocketName()
|
|
|
|
{
|
|
|
|
QTemporaryDir runtimeDir;
|
|
|
|
QVERIFY(runtimeDir.isValid());
|
|
|
|
QVERIFY(qputenv("XDG_RUNTIME_DIR", runtimeDir.path().toUtf8()));
|
|
|
|
|
2022-03-05 12:02:08 +00:00
|
|
|
KWaylandServer::Display display0;
|
|
|
|
QSignalSpy socketNameChangedSpy0(&display0, &KWaylandServer::Display::socketNamesChanged);
|
2020-10-19 15:52:56 +00:00
|
|
|
QVERIFY(socketNameChangedSpy0.isValid());
|
|
|
|
QVERIFY(display0.addSocketName());
|
2018-11-23 13:42:18 +00:00
|
|
|
display0.start();
|
|
|
|
QVERIFY(display0.isRunning());
|
2020-10-19 15:52:56 +00:00
|
|
|
QCOMPARE(socketNameChangedSpy0.count(), 1);
|
2018-11-23 13:42:18 +00:00
|
|
|
|
2022-03-05 12:02:08 +00:00
|
|
|
KWaylandServer::Display display1;
|
|
|
|
QSignalSpy socketNameChangedSpy1(&display1, &KWaylandServer::Display::socketNamesChanged);
|
2020-10-19 15:52:56 +00:00
|
|
|
QVERIFY(socketNameChangedSpy1.isValid());
|
|
|
|
QVERIFY(display1.addSocketName());
|
2018-11-23 13:42:18 +00:00
|
|
|
display1.start();
|
|
|
|
QVERIFY(display1.isRunning());
|
|
|
|
QCOMPARE(socketNameChangedSpy1.count(), 1);
|
|
|
|
}
|
|
|
|
|
2014-09-23 10:00:17 +00:00
|
|
|
QTEST_GUILESS_MAIN(TestWaylandServerDisplay)
|
2014-08-26 14:07:39 +00:00
|
|
|
#include "test_display.moc"
|