diff options
Diffstat (limited to 'tests/auto/dbus/qdbusconnection')
5 files changed, 140 insertions, 84 deletions
diff --git a/tests/auto/dbus/qdbusconnection/CMakeLists.txt b/tests/auto/dbus/qdbusconnection/CMakeLists.txt index e40f18387e..56ae21f291 100644 --- a/tests/auto/dbus/qdbusconnection/CMakeLists.txt +++ b/tests/auto/dbus/qdbusconnection/CMakeLists.txt @@ -1,12 +1,21 @@ -# Generated from qdbusconnection.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## tst_qdbusconnection Test: ##################################################################### -qt_add_test(tst_qdbusconnection +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qdbusconnection LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +qt_internal_add_test(tst_qdbusconnection SOURCES tst_qdbusconnection.cpp tst_qdbusconnection.h - PUBLIC_LIBRARIES + LIBRARIES Qt::DBus + TESTDATA + tst_qdbusconnection.conf ) diff --git a/tests/auto/dbus/qdbusconnection/qdbusconnection.pro b/tests/auto/dbus/qdbusconnection/qdbusconnection.pro deleted file mode 100644 index 3fcf3e076b..0000000000 --- a/tests/auto/dbus/qdbusconnection/qdbusconnection.pro +++ /dev/null @@ -1,5 +0,0 @@ -CONFIG += testcase -TARGET = tst_qdbusconnection -QT = core dbus testlib -SOURCES += tst_qdbusconnection.cpp -HEADERS += tst_qdbusconnection.h diff --git a/tests/auto/dbus/qdbusconnection/tst_qdbusconnection.conf b/tests/auto/dbus/qdbusconnection/tst_qdbusconnection.conf new file mode 100644 index 0000000000..7dec73d0d7 --- /dev/null +++ b/tests/auto/dbus/qdbusconnection/tst_qdbusconnection.conf @@ -0,0 +1,23 @@ +<!-- This configuration file for tst_qdbusconnection. +--> + +<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN" + "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd"> +<busconfig> + <type>tst_qdbusconnection</type> + + <listen>unix:tmpdir=/tmp</listen> + + <auth>EXTERNAL</auth> + + <policy context="default"> + <!-- Allow everything to be sent --> + <allow send_destination="*" eavesdrop="true"/> + <!-- Allow everything to be received --> + <allow eavesdrop="true"/> + <!-- Allow anyone to own anything --> + <allow own="*"/> + </policy> + + <limit name="max_completed_connections">0</limit> +</busconfig> diff --git a/tests/auto/dbus/qdbusconnection/tst_qdbusconnection.cpp b/tests/auto/dbus/qdbusconnection/tst_qdbusconnection.cpp index 77ae6f154d..ab750dff33 100644 --- a/tests/auto/dbus/qdbusconnection/tst_qdbusconnection.cpp +++ b/tests/auto/dbus/qdbusconnection/tst_qdbusconnection.cpp @@ -1,39 +1,17 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Copyright (C) 2016 Intel Corporation. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2016 Intel Corporation. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include "tst_qdbusconnection.h" -#include <qcoreapplication.h> -#include <qdebug.h> - -#include <QtTest/QtTest> -#include <QtDBus/QtDBus> +#include <QTest> +#include <QDebug> +#include <QProcess> +#include <QCoreApplication> +#include <QDBusConnection> +#include <QDBusReply> +#include <QDBusInterface> +#include <QDBusConnectionInterface> #ifdef Q_OS_UNIX # include <sys/types.h> @@ -136,7 +114,7 @@ void tst_QDBusConnection::sendSignalToName() QVERIFY(con.send(msg)); - QTRY_COMPARE(spy.args.count(), 1); + QTRY_COMPARE(spy.args.size(), 1); QCOMPARE(spy.args.at(0).toString(), QString("ping")); } @@ -161,7 +139,7 @@ void tst_QDBusConnection::sendSignalToOtherName() QTest::qWait(1000); - QCOMPARE(spy.args.count(), 0); + QCOMPARE(spy.args.size(), 0); } void tst_QDBusConnection::send() @@ -175,7 +153,7 @@ void tst_QDBusConnection::send() QDBusMessage reply = con.call(msg); - QCOMPARE(reply.arguments().count(), 1); + QCOMPARE(reply.arguments().size(), 1); QCOMPARE(reply.arguments().at(0).typeName(), "QStringList"); QVERIFY(reply.arguments().at(0).toStringList().contains(con.baseService())); } @@ -194,7 +172,7 @@ void tst_QDBusConnection::sendWithGui() QDBusMessage reply = con.call(msg, QDBus::BlockWithGui); - QCOMPARE(reply.arguments().count(), 1); + QCOMPARE(reply.arguments().size(), 1); QCOMPARE(reply.arguments().at(0).typeName(), "QStringList"); QVERIFY(reply.arguments().at(0).toStringList().contains(con.baseService())); } @@ -213,7 +191,7 @@ void tst_QDBusConnection::sendAsync() "/org/freedesktop/DBus", "org.freedesktop.DBus", "ListNames"); QVERIFY(con.callWithCallback(msg, &spy, SLOT(asyncReply(QDBusMessage)))); - QTRY_COMPARE(spy.args.count(), 1); + QTRY_COMPARE(spy.args.size(), 1); QCOMPARE(spy.args.value(0).typeName(), "QStringList"); QVERIFY(spy.args.at(0).toStringList().contains(con.baseService())); } @@ -236,7 +214,7 @@ void tst_QDBusConnection::connect() QVERIFY(con.send(msg)); - QTRY_COMPARE(spy.args.count(), 1); + QTRY_COMPARE(spy.args.size(), 1); QCOMPARE(spy.args.at(0).toString(), QString("ping")); } @@ -1048,7 +1026,7 @@ void tst_QDBusConnection::multipleInterfacesInQObject() "local.BaseObject", "anotherMethod"); QDBusMessage reply = con.call(msg, QDBus::Block); QCOMPARE(reply.type(), QDBusMessage::ReplyMessage); - QCOMPARE(reply.arguments().count(), 0); + QCOMPARE(reply.arguments().size(), 0); QVERIFY_HOOKCALLED(); } @@ -1434,6 +1412,67 @@ void tst_QDBusConnection::pendingCallWhenDisconnected() #endif } +void tst_QDBusConnection::connectionLimit() +{ +#if !QT_CONFIG(process) + QSKIP("Test requires QProcess"); +#else + if (!QCoreApplication::instance()) + QSKIP("Test requires a QCoreApplication"); + + QProcess daemon; + daemon.start("dbus-daemon", + QStringList() << "--config-file" << QFINDTESTDATA("../qdbusconnection/tst_qdbusconnection.conf") + << "--nofork" + << "--print-address"); + QVERIFY2(daemon.waitForReadyRead(2000), + "Daemon didn't print its address in time; error: \"" + daemon.errorString().toLocal8Bit() + + "\"; stderr:\n" + daemon.readAllStandardError()); + + QString address = QString::fromLocal8Bit(daemon.readAll().trimmed()); + QDBusConnection con = QDBusConnection::connectToBus(address, "connectionLimit"); + QVERIFY2(!con.isConnected(), "Unexpected successful connection"); + QCOMPARE(con.lastError().type(), QDBusError::LimitsExceeded); + + // kill the bus + daemon.terminate(); + daemon.waitForFinished(); +#endif +} + +void tst_QDBusConnection::emptyServerAddress() +{ + QDBusServer server({}, nullptr); +} + +void tst_QDBusConnection::parentClassSignal() +{ + if (!QCoreApplication::instance()) + QSKIP("Test requires a QCoreApplication"); + + const QString path = "/path"; + + QDBusConnection con = QDBusConnection::sessionBus(); + QVERIFY(con.isConnected()); + + // register one object at root: + MyObject obj; + QVERIFY(con.registerObject(path, &obj, QDBusConnection::ExportAllContents)); + QCOMPARE(con.objectRegisteredAt(path), static_cast<QObject *>(&obj)); + + SignalReceiver recv; + QVERIFY(con.connect(con.baseService(), path, "local.BaseObject", "baseObjectSignal", &recv, + SLOT(oneSlot()))); + QVERIFY(con.connect(con.baseService(), path, "local.MyObject", "myObjectSignal", &recv, + SLOT(oneSlot()))); + + emit obj.baseObjectSignal(); + QTRY_COMPARE(recv.signalsReceived, 1); + + emit obj.myObjectSignal(); + QTRY_COMPARE(recv.signalsReceived, 2); +} + QString MyObject::path; QString MyObjectWithoutInterface::path; QString MyObjectWithoutInterface::interface; diff --git a/tests/auto/dbus/qdbusconnection/tst_qdbusconnection.h b/tests/auto/dbus/qdbusconnection/tst_qdbusconnection.h index af42224b79..4137859414 100644 --- a/tests/auto/dbus/qdbusconnection/tst_qdbusconnection.h +++ b/tests/auto/dbus/qdbusconnection/tst_qdbusconnection.h @@ -1,59 +1,44 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Copyright (C) 2016 Intel Corporation. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2016 Intel Corporation. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #ifndef TST_QDBUSCONNECTION_H #define TST_QDBUSCONNECTION_H #include <QObject> -#include <QtDBus/QtDBus> -#include <QtTest/QtTest> +#include <QTest> +#include <QTestEventLoop> +#include <QDBusMessage> +#include <QDBusConnection> +#include <QDBusServer> +#include <QDBusVirtualObject> class BaseObject: public QObject { Q_OBJECT Q_CLASSINFO("D-Bus Interface", "local.BaseObject") public: - BaseObject(QObject *parent = 0) : QObject(parent) { } + BaseObject(QObject *parent = nullptr) : QObject(parent) { } public slots: void anotherMethod() { } +signals: + void baseObjectSignal(); }; class MyObject: public BaseObject { Q_OBJECT + Q_CLASSINFO("D-Bus Interface", "local.MyObject") public slots: void method(const QDBusMessage &msg); public: static QString path; int callCount; - MyObject(QObject *parent = 0) : BaseObject(parent), callCount(0) {} + MyObject(QObject *parent = nullptr) : BaseObject(parent), callCount(0) {} + +signals: + void myObjectSignal(); }; class MyObjectWithoutInterface: public QObject @@ -66,7 +51,7 @@ public: static QString path; static QString interface; int callCount; - MyObjectWithoutInterface(QObject *parent = 0) : QObject(parent), callCount(0) {} + MyObjectWithoutInterface(QObject *parent = nullptr) : QObject(parent), callCount(0) {} }; class SignalReceiver : public QObject @@ -135,6 +120,11 @@ private slots: void callVirtualObject(); void callVirtualObjectLocal(); void pendingCallWhenDisconnected(); + void connectionLimit(); + + void emptyServerAddress(); + + void parentClassSignal(); public: QString serviceName() const { return "org.qtproject.Qt.Autotests.QDBusConnection"; } @@ -175,7 +165,7 @@ public: bool registerObject() { - Q_FOREACH (const QString &name, m_connections) { + for (const QString &name : std::as_const(m_connections)) { if (!registerObject(QDBusConnection(name))) return false; } @@ -184,7 +174,7 @@ public: void unregisterObject() { - Q_FOREACH (const QString &name, m_connections) { + for (const QString &name : std::as_const(m_connections)) { QDBusConnection c(name); c.unregisterObject(m_path); } @@ -238,7 +228,7 @@ class TestObject : public QObject { Q_OBJECT public: - TestObject(QObject *parent = 0) : QObject(parent) {} + TestObject(QObject *parent = nullptr) : QObject(parent) {} ~TestObject() {} QString func; |