2020-08-02 22:22:19 +00:00
|
|
|
/*
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
2013-01-07 07:07:27 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-FileCopyrightText: 2013 Martin Gräßlin <mgraesslin@kde.org>
|
2013-01-07 07:07:27 +00:00
|
|
|
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2013-01-14 08:20:59 +00:00
|
|
|
#include "testutils.h"
|
2013-01-07 07:07:27 +00:00
|
|
|
// KWin
|
2021-02-09 18:18:36 +00:00
|
|
|
#include "client_machine.h"
|
2022-01-13 13:54:03 +00:00
|
|
|
#include "utils/xcbutils.h"
|
2013-01-07 07:07:27 +00:00
|
|
|
// Qt
|
|
|
|
#include <QApplication>
|
2018-06-05 10:52:57 +00:00
|
|
|
#include <QtTest>
|
2022-03-09 16:25:44 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
#include <private/qtx11extras_p.h>
|
|
|
|
#else
|
2016-11-11 08:59:46 +00:00
|
|
|
#include <QX11Info>
|
2022-03-09 16:25:44 +00:00
|
|
|
#endif
|
2013-01-07 07:07:27 +00:00
|
|
|
// xcb
|
|
|
|
#include <xcb/xcb.h>
|
|
|
|
// system
|
|
|
|
#include <netdb.h>
|
2022-03-23 10:13:38 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2013-01-07 07:07:27 +00:00
|
|
|
|
2014-12-05 10:42:15 +00:00
|
|
|
Q_LOGGING_CATEGORY(KWIN_CORE, "kwin_core")
|
|
|
|
|
2013-01-07 07:07:27 +00:00
|
|
|
using namespace KWin;
|
|
|
|
|
|
|
|
class TestClientMachine : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
2013-07-22 14:07:39 +00:00
|
|
|
private Q_SLOTS:
|
2013-01-07 07:07:27 +00:00
|
|
|
void initTestCase();
|
|
|
|
void cleanupTestCase();
|
|
|
|
void hostName_data();
|
|
|
|
void hostName();
|
|
|
|
void emptyHostName();
|
|
|
|
|
|
|
|
private:
|
2022-10-21 15:24:38 +00:00
|
|
|
void setClientMachineProperty(xcb_window_t window, const QString &hostname);
|
|
|
|
QString m_hostName;
|
|
|
|
QString m_fqdn;
|
2013-01-07 07:07:27 +00:00
|
|
|
};
|
|
|
|
|
2022-10-21 15:24:38 +00:00
|
|
|
void TestClientMachine::setClientMachineProperty(xcb_window_t window, const QString &hostname)
|
2013-01-07 07:07:27 +00:00
|
|
|
{
|
|
|
|
xcb_change_property(connection(), XCB_PROP_MODE_REPLACE, window,
|
|
|
|
XCB_ATOM_WM_CLIENT_MACHINE, XCB_ATOM_STRING, 8,
|
2022-10-21 15:24:38 +00:00
|
|
|
hostname.length(), hostname.toLocal8Bit().constData());
|
2013-01-07 07:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestClientMachine::initTestCase()
|
|
|
|
{
|
|
|
|
#ifdef HOST_NAME_MAX
|
|
|
|
char hostnamebuf[HOST_NAME_MAX];
|
|
|
|
#else
|
|
|
|
char hostnamebuf[256];
|
|
|
|
#endif
|
|
|
|
if (gethostname(hostnamebuf, sizeof hostnamebuf) >= 0) {
|
2022-03-23 10:13:38 +00:00
|
|
|
hostnamebuf[sizeof(hostnamebuf) - 1] = 0;
|
2013-01-07 07:07:27 +00:00
|
|
|
m_hostName = hostnamebuf;
|
|
|
|
}
|
|
|
|
addrinfo *res;
|
|
|
|
addrinfo addressHints;
|
|
|
|
memset(&addressHints, 0, sizeof(addressHints));
|
|
|
|
addressHints.ai_family = PF_UNSPEC;
|
|
|
|
addressHints.ai_socktype = SOCK_STREAM;
|
|
|
|
addressHints.ai_flags |= AI_CANONNAME;
|
2022-10-21 15:24:38 +00:00
|
|
|
if (getaddrinfo(m_hostName.toLocal8Bit().constData(), nullptr, &addressHints, &res) == 0) {
|
2013-01-07 07:07:27 +00:00
|
|
|
if (res->ai_canonname) {
|
2022-10-21 15:24:38 +00:00
|
|
|
m_fqdn = QString::fromLocal8Bit(res->ai_canonname);
|
2013-01-07 07:07:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
freeaddrinfo(res);
|
2015-02-18 07:31:53 +00:00
|
|
|
|
|
|
|
qApp->setProperty("x11RootWindow", QVariant::fromValue<quint32>(QX11Info::appRootWindow()));
|
2022-03-23 10:13:38 +00:00
|
|
|
qApp->setProperty("x11Connection", QVariant::fromValue<void *>(QX11Info::connection()));
|
2013-01-07 07:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestClientMachine::cleanupTestCase()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestClientMachine::hostName_data()
|
|
|
|
{
|
2022-10-21 15:24:38 +00:00
|
|
|
QTest::addColumn<QString>("hostName");
|
|
|
|
QTest::addColumn<QString>("expectedHost");
|
2013-01-07 07:07:27 +00:00
|
|
|
QTest::addColumn<bool>("local");
|
|
|
|
|
2022-10-21 15:24:38 +00:00
|
|
|
QTest::newRow("empty") << QString() << QStringLiteral("localhost") << true;
|
|
|
|
QTest::newRow("localhost") << QStringLiteral("localhost") << QStringLiteral("localhost") << true;
|
2022-03-23 10:13:38 +00:00
|
|
|
QTest::newRow("hostname") << m_hostName << m_hostName << true;
|
|
|
|
QTest::newRow("HOSTNAME") << m_hostName.toUpper() << m_hostName.toUpper() << true;
|
2022-10-21 15:24:38 +00:00
|
|
|
QString cutted(m_hostName);
|
2013-01-07 07:07:27 +00:00
|
|
|
cutted.remove(0, 1);
|
2022-03-23 10:13:38 +00:00
|
|
|
QTest::newRow("ostname") << cutted << cutted << false;
|
2022-10-21 15:24:38 +00:00
|
|
|
QString domain("random.name.not.exist.tld");
|
2022-03-23 10:13:38 +00:00
|
|
|
QTest::newRow("domain") << domain << domain << false;
|
|
|
|
QTest::newRow("fqdn") << m_fqdn << m_fqdn << true;
|
|
|
|
QTest::newRow("FQDN") << m_fqdn.toUpper() << m_fqdn.toUpper() << true;
|
2013-01-07 07:07:27 +00:00
|
|
|
cutted = m_fqdn;
|
|
|
|
cutted.remove(0, 1);
|
2022-03-23 10:13:38 +00:00
|
|
|
QTest::newRow("qdn") << cutted << cutted << false;
|
2013-01-07 07:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestClientMachine::hostName()
|
|
|
|
{
|
2013-01-29 13:03:05 +00:00
|
|
|
const QRect geometry(0, 0, 10, 10);
|
2022-03-23 10:13:38 +00:00
|
|
|
const uint32_t values[] = {true};
|
2013-01-29 13:03:05 +00:00
|
|
|
Xcb::Window window(geometry, XCB_WINDOW_CLASS_INPUT_ONLY, XCB_CW_OVERRIDE_REDIRECT, values);
|
2022-10-21 15:24:38 +00:00
|
|
|
QFETCH(QString, hostName);
|
2013-01-07 07:07:27 +00:00
|
|
|
QFETCH(bool, local);
|
2013-01-29 13:03:05 +00:00
|
|
|
setClientMachineProperty(window, hostName);
|
2013-01-07 07:07:27 +00:00
|
|
|
|
|
|
|
ClientMachine clientMachine;
|
2019-07-29 19:45:19 +00:00
|
|
|
QSignalSpy spy(&clientMachine, &ClientMachine::localhostChanged);
|
2013-01-29 13:03:05 +00:00
|
|
|
clientMachine.resolve(window, XCB_WINDOW_NONE);
|
2013-01-07 07:07:27 +00:00
|
|
|
QTEST(clientMachine.hostName(), "expectedHost");
|
|
|
|
|
2022-03-23 10:13:38 +00:00
|
|
|
int i = 0;
|
2013-01-07 07:07:27 +00:00
|
|
|
while (clientMachine.isResolving() && i++ < 50) {
|
|
|
|
// name is being resolved in an external thread, so let's wait a little bit
|
|
|
|
QTest::qWait(250);
|
|
|
|
}
|
|
|
|
|
|
|
|
QCOMPARE(clientMachine.isLocal(), local);
|
|
|
|
QCOMPARE(spy.isEmpty(), !local);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestClientMachine::emptyHostName()
|
|
|
|
{
|
2013-01-29 13:03:05 +00:00
|
|
|
const QRect geometry(0, 0, 10, 10);
|
2022-03-23 10:13:38 +00:00
|
|
|
const uint32_t values[] = {true};
|
2013-01-29 13:03:05 +00:00
|
|
|
Xcb::Window window(geometry, XCB_WINDOW_CLASS_INPUT_ONLY, XCB_CW_OVERRIDE_REDIRECT, values);
|
2013-01-07 07:07:27 +00:00
|
|
|
ClientMachine clientMachine;
|
2019-07-29 19:45:19 +00:00
|
|
|
QSignalSpy spy(&clientMachine, &ClientMachine::localhostChanged);
|
2013-01-29 13:03:05 +00:00
|
|
|
clientMachine.resolve(window, XCB_WINDOW_NONE);
|
2013-01-07 07:07:27 +00:00
|
|
|
QCOMPARE(clientMachine.hostName(), ClientMachine::localhost());
|
|
|
|
QVERIFY(clientMachine.isLocal());
|
|
|
|
// should be local
|
|
|
|
QCOMPARE(spy.isEmpty(), false);
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:08:45 +00:00
|
|
|
Q_CONSTRUCTOR_FUNCTION(forceXcb)
|
2016-09-08 13:15:41 +00:00
|
|
|
QTEST_MAIN(TestClientMachine)
|
2013-01-07 07:07:27 +00:00
|
|
|
#include "test_client_machine.moc"
|