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
|
2023-07-03 19:28:19 +00:00
|
|
|
#include <QSignalSpy>
|
|
|
|
#include <QTest>
|
2014-08-26 14:07:39 +00:00
|
|
|
// WaylandServer
|
2022-04-22 09:27:33 +00:00
|
|
|
#include "wayland/clientconnection.h"
|
|
|
|
#include "wayland/display.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
|
|
|
|
2023-09-13 17:59:29 +00:00
|
|
|
using namespace KWin;
|
2014-08-26 14:07:39 +00:00
|
|
|
|
|
|
|
class TestWaylandServerDisplay : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
private Q_SLOTS:
|
|
|
|
void testSocketName();
|
|
|
|
void testStartStop();
|
2014-11-17 15:01:18 +00:00
|
|
|
void testClientConnection();
|
2014-11-27 15:42:16 +00:00
|
|
|
void testConnectNoSocket();
|
2018-11-23 13:42:18 +00:00
|
|
|
void testAutoSocketName();
|
2014-08-26 14:07:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void TestWaylandServerDisplay::testSocketName()
|
|
|
|
{
|
2023-09-13 17:59:29 +00:00
|
|
|
KWin::Display display;
|
|
|
|
QSignalSpy changedSpy(&display, &KWin::Display::socketNamesChanged);
|
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));
|
|
|
|
|
2023-09-13 17:59:29 +00:00
|
|
|
std::unique_ptr<KWin::Display> display(new KWin::Display);
|
|
|
|
QSignalSpy runningSpy(display.get(), &KWin::Display::runningChanged);
|
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-11-17 15:01:18 +00:00
|
|
|
void TestWaylandServerDisplay::testClientConnection()
|
|
|
|
{
|
2023-09-13 17:59:29 +00:00
|
|
|
KWin::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();
|
2023-09-13 17:59:29 +00:00
|
|
|
QSignalSpy connectedSpy(&display, &KWin::Display::clientConnected);
|
|
|
|
QSignalSpy disconnectedSpy(&display, &KWin::Display::clientDisconnected);
|
2014-11-17 15:01:18 +00:00
|
|
|
|
|
|
|
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());
|
|
|
|
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 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 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);
|
2015-11-10 12:27:27 +00:00
|
|
|
client2->destroy();
|
2023-10-06 11:47:54 +00:00
|
|
|
QCOMPARE(clientDestroyedSpy.count(), 1);
|
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-27 15:42:16 +00:00
|
|
|
void TestWaylandServerDisplay::testConnectNoSocket()
|
|
|
|
{
|
2023-09-13 17:59:29 +00:00
|
|
|
KWin::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]);
|
|
|
|
}
|
|
|
|
|
2018-11-23 13:42:18 +00:00
|
|
|
void TestWaylandServerDisplay::testAutoSocketName()
|
|
|
|
{
|
|
|
|
QTemporaryDir runtimeDir;
|
|
|
|
QVERIFY(runtimeDir.isValid());
|
|
|
|
QVERIFY(qputenv("XDG_RUNTIME_DIR", runtimeDir.path().toUtf8()));
|
|
|
|
|
2023-09-13 17:59:29 +00:00
|
|
|
KWin::Display display0;
|
|
|
|
QSignalSpy socketNameChangedSpy0(&display0, &KWin::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
|
|
|
|
2023-09-13 17:59:29 +00:00
|
|
|
KWin::Display display1;
|
|
|
|
QSignalSpy socketNameChangedSpy1(&display1, &KWin::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"
|