./0000755000004100000410000000000013033672373011252 5ustar www-datawww-data./CMakeLists.txt0000644000004100000410000000363513033672373014021 0ustar www-datawww-dataproject(libqtdbustest CXX) cmake_minimum_required(VERSION 2.8.9) set(API_VERSION 1) set(ABI_VERSION 1) set(GETTEXT_PACKAGE libqtdbustest) set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake" "${CMAKE_MODULE_PATH}") find_package(PkgConfig REQUIRED) find_package(Qt5Core REQUIRED) include_directories(${Qt5Core_INCLUDE_DIRS}) find_package(Qt5DBus COMPONENTS Qt5DBusMacros REQUIRED) include_directories(${Qt5DBus_INCLUDE_DIRS}) find_package(Qt5Test REQUIRED) include_directories(${Qt5Test_INCLUDE_DIRS}) set(CMAKE_AUTOMOC ON) set(CMAKE_INCLUDE_CURRENT_DIR ON) set(DATA_DIR "${CMAKE_SOURCE_DIR}/data") set(SOURCE_DIR "${CMAKE_SOURCE_DIR}/src") set(SOURCE_BINARY_DIR "${CMAKE_BINARY_DIR}/src") include_directories(${SOURCE_DIR}) include_directories(${SOURCE_BINARY_DIR}) include(GNUInstallDirs) # Workaround for libexecdir on debian if (EXISTS "/etc/debian_version") set(CMAKE_INSTALL_LIBEXECDIR ${CMAKE_INSTALL_LIBDIR}) set(CMAKE_INSTALL_FULL_LIBEXECDIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBEXECDIR}") endif() set(LOCALE_DIR "${CMAKE_INSTALL_FULL_DATADIR}/locale") set(DBUS_SYSTEM_CONFIG_FILE "${CMAKE_INSTALL_FULL_DATADIR}/libqtdbustest/system.conf") set(DBUS_SESSION_CONFIG_FILE "${CMAKE_INSTALL_FULL_DATADIR}/libqtdbustest/session.conf") set(QTDBUSTEST_WATCHDOG_BIN "${CMAKE_INSTALL_FULL_LIBEXECDIR}/libqtdbustest/watchdog") add_definitions( -std=c++11 -fno-permissive -pedantic -Wall -Wextra ) configure_file( "${SOURCE_DIR}/libqtdbustest/config.h.in" "${SOURCE_BINARY_DIR}/libqtdbustest/config.h" ) add_subdirectory("src") add_subdirectory("data") enable_testing() add_subdirectory(tests) ADD_CUSTOM_TARGET( check ${CMAKE_CTEST_COMMAND} --force-new-ctest-process --output-on-failure ) find_package(CoverageReport) enable_coverage_report( TARGETS qtdbustest qdbus-simple-test-runner unit-tests watchdog FILTER ${CMAKE_SOURCE_DIR}/tests/* ${CMAKE_BINARY_DIR}/* TESTS unit-tests ) ./COPYING0000644000004100000410000001672713033672340012314 0ustar www-datawww-data GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. ./src/0000755000004100000410000000000013033672340012033 5ustar www-datawww-data./src/CMakeLists.txt0000644000004100000410000000023113033672340014567 0ustar www-datawww-datainclude(GenerateExportHeader) add_compiler_export_flags() add_subdirectory(watchdog) add_subdirectory(libqtdbustest) add_subdirectory(qdbus-test-runner)./src/watchdog/0000755000004100000410000000000013033672340013633 5ustar www-datawww-data./src/watchdog/CMakeLists.txt0000644000004100000410000000027513033672340016377 0ustar www-datawww-data add_executable( watchdog Watchdog.cpp main.cpp ) qt5_use_modules( watchdog Core ) install( TARGETS watchdog RUNTIME DESTINATION "${CMAKE_INSTALL_FULL_LIBEXECDIR}/libqtdbustest" ) ./src/watchdog/Watchdog.h0000644000004100000410000000221313033672340015542 0ustar www-datawww-data/* * Copyright (C) 2013 Canonical, Ltd. * * This library is free software; you can redistribute it and/or modify it under * the terms of version 3 of the GNU Lesser General Public License as published * by the Free Software Foundation. * * 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 program. If not, see . * * Author: Pete Woods */ #ifndef LIBQTDBUSTEST_WATHDOG_H_ #define LIBQTDBUSTEST_WATHDOG_H_ #include #include #include namespace QtDBusTest { class Q_DECL_EXPORT Watchdog: public QObject { Q_OBJECT public: Watchdog(Q_PID parentPid, Q_PID childPid); virtual ~Watchdog(); protected Q_SLOTS: void timeout(); protected: Q_PID m_parentPid; Q_PID m_childPid; QTimer m_timer; }; } /* namespace QtDBusTest */ #endif /* LIBQTDBUSTEST_WATHDOG_H_ */ ./src/watchdog/Watchdog.cpp0000644000004100000410000000234713033672340016105 0ustar www-datawww-data/* * Copyright (C) 2013 Canonical, Ltd. * * This library is free software; you can redistribute it and/or modify it under * the terms of version 3 of the GNU Lesser General Public License as published * by the Free Software Foundation. * * 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 program. If not, see . * * Author: Pete Woods */ #include #include #include #include namespace QtDBusTest { Watchdog::Watchdog(Q_PID parentPid, Q_PID childPid) : m_parentPid(parentPid), m_childPid(childPid) { m_timer.setInterval(1000); connect(&m_timer, SIGNAL(timeout()), this, SLOT(timeout())); m_timer.start(); } Watchdog::~Watchdog() { } void Watchdog::timeout() { // if the parent process is dead if (kill(m_parentPid, 0) != 0) { kill(m_childPid, SIGKILL); QCoreApplication::exit(0); } } } /* namespace QtDBusTest */ ./src/watchdog/main.cpp0000644000004100000410000000237213033672340015267 0ustar www-datawww-data/* * Copyright (C) 2013 Canonical, Ltd. * * This library is free software; you can redistribute it and/or modify it under * the terms of version 3 of the GNU Lesser General Public License as published * by the Free Software Foundation. * * 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 program. If not, see . * * Author: Pete Woods */ #include #include #include #include using namespace QtDBusTest; static void exitQt(int sig) { Q_UNUSED(sig); QCoreApplication::exit(0); } int main(int argc, char **argv) { QCoreApplication application(argc, argv); signal(SIGINT, &exitQt); signal(SIGTERM, &exitQt); if (argc != 3) { qWarning() << "Usage: " << argv[0] << "PARENT_PID TARGET_ID"; exit(1); } Watchdog watchdog(QString::fromUtf8(argv[1]).toLongLong(), QString::fromUtf8(argv[2]).toLongLong()); return application.exec(); } ./src/qdbus-test-runner/0000755000004100000410000000000013033672340015435 5ustar www-datawww-data./src/qdbus-test-runner/CMakeLists.txt0000644000004100000410000000041013033672340020170 0ustar www-datawww-data add_executable( qdbus-simple-test-runner qdbus-simple-test-runner.cpp ) target_link_libraries( qdbus-simple-test-runner qtdbustest ) qt5_use_modules( qdbus-simple-test-runner Core ) install( TARGETS qdbus-simple-test-runner RUNTIME DESTINATION "bin" ) ./src/qdbus-test-runner/qdbus-simple-test-runner.cpp0000644000004100000410000000377613033672340023047 0ustar www-datawww-data/* * Copyright (C) 2013 Canonical, Ltd. * * This library is free software; you can redistribute it and/or modify it under * the terms of version 3 of the GNU Lesser General Public License as published * by the Free Software Foundation. * * 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 program. If not, see . * * Author: Pete Woods */ #include #include #include #include using namespace QtDBusTest; static void exitQt(int sig) { Q_UNUSED(sig); QCoreApplication::exit(0); } class ExitListener: public QObject { Q_OBJECT public Q_SLOTS: void finished(int exitCode, QProcess::ExitStatus exitStatus) { if (exitStatus == QProcess::CrashExit && exitCode == 0) { exitCode = 1; } QCoreApplication::exit(exitCode); } }; int main(int argc, char **argv) { QCoreApplication application(argc, argv); signal(SIGINT, &exitQt); signal(SIGTERM, &exitQt); DBusTestRunner runner; if (argc < 2) { qWarning() << "Usage:" << argv[0] << "COMMAND "; return 1; } QString command(QString::fromUtf8(argv[1])); QStringList arguments; for (int i(2); i < argc; ++i) { arguments << QString::fromUtf8(argv[i]); } QSharedPointer service( new QProcessDBusService("", QDBusConnection::SessionBus, command, arguments)); runner.registerService(service); runner.startServices(); ExitListener listener; QObject::connect(&service->underlyingProcess(), SIGNAL(finished(int, QProcess::ExitStatus)), &listener, SLOT(finished(int, QProcess::ExitStatus))); return application.exec(); } #include "qdbus-simple-test-runner.moc" ./src/libqtdbustest/0000755000004100000410000000000013033672340014724 5ustar www-datawww-data./src/libqtdbustest/libqtdbustest.pc.in0000644000004100000410000000045513033672340020552 0ustar www-datawww-datalibdir=@CMAKE_INSTALL_FULL_LIBDIR@ includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@ Cflags: -I${includedir}/libqtdbustest-@API_VERSION@ Requires: Qt5Core Libs: -L${libdir} -lqtdbustest Name: libqtdbustest Description: A library to facilitate testing DBus services written in Qt/C++ Version: @ABI_VERSION@ ./src/libqtdbustest/CMakeLists.txt0000644000004100000410000000166413033672340017473 0ustar www-datawww-data set( QTDBUSTEST_SRC DBusTestRunner.cpp DBusService.cpp QProcessDBusService.cpp SuicidalProcess.cpp ) set( QTDBUSTEST_PUBLIC_HEADERS "${CMAKE_CURRENT_BINARY_DIR}/config.h" DBusTestRunner.h DBusService.h QProcessDBusService.h SuicidalProcess.h ) add_library( qtdbustest SHARED ${QTDBUSTEST_SRC} ) qt5_use_modules( qtdbustest Core DBus Test ) set_target_properties( qtdbustest PROPERTIES VERSION ${API_VERSION}.0.0 SOVERSION ${ABI_VERSION} PUBLIC_HEADER "${QTDBUSTEST_PUBLIC_HEADERS}" ) install( TARGETS qtdbustest LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/libqtdbustest-${API_VERSION}/libqtdbustest" ) # Package config set(LIBQTDBUSTEST_PC "${CMAKE_CURRENT_BINARY_DIR}/libqtdbustest-${API_VERSION}.pc") configure_file("libqtdbustest.pc.in" "${LIBQTDBUSTEST_PC}" @ONLY) install( FILES "${LIBQTDBUSTEST_PC}" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig" ) ./src/libqtdbustest/config.h.in0000644000004100000410000000173313033672340016753 0ustar www-datawww-data/* * Copyright (C) 2013 Canonical, Ltd. * * This library is free software; you can redistribute it and/or modify it under * the terms of version 3 of the GNU Lesser General Public License as published * by the Free Software Foundation. * * 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 program. If not, see . * * Author: Pete Woods */ #ifndef LIBQTDBUSTEST_CONFIG_H_ #define LIBQTDBUSTEST_CONFIG_H_ #define DBUS_SYSTEM_CONFIG_FILE "@DBUS_SYSTEM_CONFIG_FILE@" #define DBUS_SESSION_CONFIG_FILE "@DBUS_SESSION_CONFIG_FILE@" #define QTDBUSTEST_WATCHDOG_BIN "@QTDBUSTEST_WATCHDOG_BIN@" #endif // LIBQTDBUSTEST_CONFIG_H_ ./src/libqtdbustest/DBusTestRunner.cpp0000644000004100000410000000746613033672340020334 0ustar www-datawww-data/* * Copyright (C) 2013 Canonical, Ltd. * * This library is free software; you can redistribute it and/or modify it under * the terms of version 3 of the GNU Lesser General Public License as published * by the Free Software Foundation. * * 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 program. If not, see . * * Author: Pete Woods */ #include #include namespace QtDBusTest { class DBusTestRunnerPrivate { public: DBusTestRunnerPrivate() : m_sessionConnection("DBusTestRunner-session"), m_systemConnection( "DBusTestRunner-system") { } QString m_sessionBus; QDBusConnection m_sessionConnection; SuicidalProcess m_sessionDBus; QString m_systemBus; QDBusConnection m_systemConnection; SuicidalProcess m_systemDBus; QList m_services; }; DBusTestRunner::DBusTestRunner(const QString &dbusSessionConfigFile, const QString &dbusSystemConfigFile) : d(new DBusTestRunnerPrivate()) { // If we are already running inside a QDBus test environment if (qEnvironmentVariableIsSet("QDBUS_TEST_RUNNER_PARENT")) { // session bus setup d->m_sessionBus = QString::fromUtf8( qgetenv("DBUS_SESSION_BUS_ADDRESS")); d->m_sessionConnection = QDBusConnection::sessionBus(); // system bus setup d->m_systemBus = QString::fromUtf8(qgetenv("DBUS_SYSTEM_BUS_ADDRESS")); d->m_systemConnection = QDBusConnection::systemBus(); } else { // session bus setup d->m_sessionDBus.setProcessChannelMode(QProcess::MergedChannels); d->m_sessionDBus.start("dbus-daemon", QStringList() << "--config-file" << dbusSessionConfigFile << "--print-address"); Q_ASSERT(d->m_sessionDBus.waitForStarted()); d->m_sessionDBus.waitForReadyRead(); d->m_sessionBus = d->m_sessionDBus.readAll().trimmed(); qputenv("DBUS_SESSION_BUS_ADDRESS", d->m_sessionBus.toUtf8()); qputenv("DBUS_STARTER_ADDRESS", d->m_sessionBus.toUtf8()); qputenv("DBUS_STARTER_BUS_TYPE", "session"); d->m_sessionConnection = QDBusConnection::connectToBus(d->m_sessionBus, d->m_sessionBus); // system bus setup d->m_systemDBus.setProcessChannelMode(QProcess::MergedChannels); d->m_systemDBus.start("dbus-daemon", QStringList() << "--config-file" << dbusSystemConfigFile << "--print-address"); Q_ASSERT(d->m_systemDBus.waitForStarted()); d->m_systemDBus.waitForReadyRead(); d->m_systemBus = d->m_systemDBus.readAll().trimmed(); qputenv("DBUS_SYSTEM_BUS_ADDRESS", d->m_systemBus.toUtf8()); d->m_systemConnection = QDBusConnection::connectToBus(d->m_systemBus, d->m_systemBus); } } DBusTestRunner::~DBusTestRunner() { d->m_services.clear(); } void DBusTestRunner::registerService(DBusServicePtr service) { d->m_services.push_back(service); } void DBusTestRunner::startServices() { for (DBusServicePtr service : d->m_services) { switch (service->busType()) { case QDBusConnection::SystemBus: service->start(d->m_systemConnection); break; case QDBusConnection::SessionBus: service->start(d->m_sessionConnection); break; case QDBusConnection::ActivationBus: qWarning() << "Unknown bus type"; break; } } } const QDBusConnection & DBusTestRunner::sessionConnection() const { return d->m_sessionConnection; } const QString & DBusTestRunner::sessionBus() const { return d->m_sessionBus; } const QDBusConnection & DBusTestRunner::systemConnection() const { return d->m_systemConnection; } const QString & DBusTestRunner::systemBus() const { return d->m_systemBus; } } ./src/libqtdbustest/QProcessDBusService.cpp0000644000004100000410000000567113033672340021277 0ustar www-datawww-data/* * Copyright (C) 2013 Canonical, Ltd. * * This library is free software; you can redistribute it and/or modify it under * the terms of version 3 of the GNU Lesser General Public License as published * by the Free Software Foundation. * * 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 program. If not, see . * * Author: Pete Woods */ #include #include #include #include #include #include namespace QtDBusTest { namespace { int timeoutInSeconds() { const int defaultTimeout = 15; QByteArray timeoutString = qgetenv("QDBUS_DBUS_START_TIMEOUT"); if (!timeoutString.isEmpty()) { bool ok; const int timeout = timeoutString.toInt(&ok); if (ok) { return timeout; } } return defaultTimeout; } } // anonymous namespace class QProcessDBusServicePrivate { public: QProcessDBusServicePrivate(const QString &program, const QStringList &arguments) : m_program(program), m_arguments(arguments) { } QString m_program; QStringList m_arguments; SuicidalProcess m_process; }; QProcessDBusService::QProcessDBusService(const QString &name, QDBusConnection::BusType busType, const QString &program, const QStringList &arguments) : DBusService(name, busType), p( new QProcessDBusServicePrivate(program, arguments)) { } QProcessDBusService::~QProcessDBusService() { } void QProcessDBusService::start(const QDBusConnection &connection) { QDBusServiceWatcher watcher(name(), connection, QDBusServiceWatcher::WatchForRegistration); QSignalSpy spy(&watcher, SIGNAL(serviceOwnerChanged(const QString &,const QString &,const QString &))); QProcessEnvironment environment(QProcessEnvironment::systemEnvironment()); environment.insert("QDBUS_TEST_RUNNER_PARENT", "1"); p->m_process.setProcessEnvironment(environment); p->m_process.setProcessChannelMode(QProcess::ForwardedChannels); p->m_process.start(p->m_program, p->m_arguments); if (name().isEmpty()) { if (!p->m_process.waitForStarted()) { QString error = "Process [" + p->m_program + "] for service [" + name() + "] failed to start"; throw std::logic_error(error.toStdString()); } } else { spy.wait(timeoutInSeconds() * 1000); if (spy.empty()) { QString error = "Process [" + p->m_program + "] for service [" + name() + "] failed to appear on bus"; throw std::logic_error(error.toStdString()); } } } Q_PID QProcessDBusService::pid() const { return p->m_process.pid(); } const QProcess & QProcessDBusService::underlyingProcess() const { return p->m_process; } } ./src/libqtdbustest/DBusService.cpp0000644000004100000410000000241713033672340017612 0ustar www-datawww-data/* * Copyright (C) 2013 Canonical, Ltd. * * This library is free software; you can redistribute it and/or modify it under * the terms of version 3 of the GNU Lesser General Public License as published * by the Free Software Foundation. * * 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 program. If not, see . * * Author: Pete Woods */ #include namespace QtDBusTest { class DBusServicePrivate { public: DBusServicePrivate(const QString &name, QDBusConnection::BusType busType) : m_name(name), m_busType(busType) { } QString m_name; QDBusConnection::BusType m_busType; }; DBusService::DBusService(const QString &name, QDBusConnection::BusType busType) : d(new DBusServicePrivate(name, busType)) { } DBusService::~DBusService() { } const QString & DBusService::name() const { return d->m_name; } QDBusConnection::BusType DBusService::busType() const { return d->m_busType; } } ./src/libqtdbustest/DBusService.h0000644000004100000410000000252213033672340017254 0ustar www-datawww-data/* * Copyright (C) 2013 Canonical, Ltd. * * This library is free software; you can redistribute it and/or modify it under * the terms of version 3 of the GNU Lesser General Public License as published * by the Free Software Foundation. * * 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 program. If not, see . * * Author: Pete Woods */ #ifndef QTDBUSTEST_DBUSSERVICE_H_ #define QTDBUSTEST_DBUSSERVICE_H_ #include #include #include namespace QtDBusTest { class DBusService; class DBusServicePrivate; typedef QSharedPointer DBusServicePtr; class Q_DECL_EXPORT DBusService { public: DBusService(const QString &name, QDBusConnection::BusType busType); virtual ~DBusService(); virtual const QString & name() const; virtual QDBusConnection::BusType busType() const; virtual void start(const QDBusConnection &connection) = 0; private: QScopedPointer d; }; } #endif /* QTDBUSTEST_DBUSSERVICE_H_ */ ./src/libqtdbustest/SuicidalProcess.h0000644000004100000410000000233613033672340020175 0ustar www-datawww-data/* * Copyright (C) 2013 Canonical, Ltd. * * This library is free software; you can redistribute it and/or modify it under * the terms of version 3 of the GNU Lesser General Public License as published * by the Free Software Foundation. * * 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 program. If not, see . * * Author: Pete Woods */ #ifndef SUICIDALPROCESS_H_ #define SUICIDALPROCESS_H_ #include #include namespace QtDBusTest { class SuicidalProcessPrivate; class Q_DECL_EXPORT SuicidalProcess: public QProcess { Q_OBJECT public: explicit SuicidalProcess(QObject *parent = 0); virtual ~SuicidalProcess(); void setWatchdogCommand(const QString &watchdogCommand); protected Q_SLOTS: virtual void setSuicidal(); private: QScopedPointer d; }; } /* namespace QtDBusTest */ #endif /* SUICIDALPROCESS_H_ */ ./src/libqtdbustest/SuicidalProcess.cpp0000644000004100000410000000316113033672340020525 0ustar www-datawww-data/* * Copyright (C) 2013 Canonical, Ltd. * * This library is free software; you can redistribute it and/or modify it under * the terms of version 3 of the GNU Lesser General Public License as published * by the Free Software Foundation. * * 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 program. If not, see . * * Author: Pete Woods */ #include #include namespace QtDBusTest { class SuicidalProcessPrivate { public: SuicidalProcessPrivate() : m_watchdogCommand(QTDBUSTEST_WATCHDOG_BIN) { } ~SuicidalProcessPrivate() { } QString m_watchdogCommand; QProcess m_watchdog; }; SuicidalProcess::SuicidalProcess(QObject *parent) : QProcess(parent), d(new SuicidalProcessPrivate()) { connect(this, SIGNAL(started()), this, SLOT(setSuicidal())); } SuicidalProcess::~SuicidalProcess() { d->m_watchdog.kill(); d->m_watchdog.waitForFinished(); terminate(); waitForFinished(); } void SuicidalProcess::setSuicidal() { d->m_watchdog.start(d->m_watchdogCommand, QStringList() << QString::number(QCoreApplication::applicationPid()) << QString::number(pid())); } void SuicidalProcess::setWatchdogCommand(const QString &watchdogCommand) { d->m_watchdogCommand = watchdogCommand; } } /* namespace QtDBusTest */ ./src/libqtdbustest/QProcessDBusService.h0000644000004100000410000000265613033672340020744 0ustar www-datawww-data/* * Copyright (C) 2013 Canonical, Ltd. * * This library is free software; you can redistribute it and/or modify it under * the terms of version 3 of the GNU Lesser General Public License as published * by the Free Software Foundation. * * 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 program. If not, see . * * Author: Pete Woods */ #ifndef QTDBUSTEST_QPROCESSDBUSSERVICE_H_ #define QTDBUSTEST_QPROCESSDBUSSERVICE_H_ #include #include #include #include namespace QtDBusTest { class QProcessDBusServicePrivate; class Q_DECL_EXPORT QProcessDBusService: public DBusService { public: explicit QProcessDBusService(const QString &name, QDBusConnection::BusType busType, const QString &program, const QStringList &arguments); virtual ~QProcessDBusService(); virtual void start(const QDBusConnection &connection); virtual Q_PID pid() const; virtual const QProcess & underlyingProcess() const; private: QScopedPointer p; }; } #endif /* QTDBUSTEST_QPROCESSDBUSSERVICE_H_ */ ./src/libqtdbustest/DBusTestRunner.h0000644000004100000410000000307113033672340017765 0ustar www-datawww-data/* * Copyright (C) 2013 Canonical, Ltd. * * This library is free software; you can redistribute it and/or modify it under * the terms of version 3 of the GNU Lesser General Public License as published * by the Free Software Foundation. * * 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 program. If not, see . * * Author: Pete Woods */ #include #include #include #include #include #ifndef QTDBUSTEST_DBUSTESTRUNNER_H_ #define QTDBUSTEST_DBUSTESTRUNNER_H_ namespace QtDBusTest { class DBusTestRunnerPrivate; class Q_DECL_EXPORT DBusTestRunner { public: explicit DBusTestRunner(const QString &dbusSessionConfigFile = DBUS_SESSION_CONFIG_FILE, const QString &dbusSystemConfigFile = DBUS_SYSTEM_CONFIG_FILE); virtual ~DBusTestRunner(); virtual void startServices(); virtual const QDBusConnection & sessionConnection() const; virtual const QString & sessionBus() const; virtual const QDBusConnection & systemConnection() const; virtual const QString & systemBus() const; virtual void registerService(DBusServicePtr service); private: QScopedPointer d; }; } #endif // QTDBUSTEST_DBUSTESTRUNNER_H_ ./tests/0000755000004100000410000000000013033672373012414 5ustar www-datawww-data./tests/CMakeLists.txt0000644000004100000410000000147313033672373015161 0ustar www-datawww-data set(CMAKE_AUTOMOC OFF) find_package(GMock) set(CMAKE_AUTOMOC ON) include_directories(${CMAKE_CURRENT_SOURCE_DIR}) include_directories(${GMOCK_INCLUDE_DIRS}) include_directories(${GTEST_INCLUDE_DIRS}) add_definitions(-DTEST_DBUS_SESSION_CONFIG_FILE="${DATA_DIR}/session.conf") add_definitions(-DTEST_DBUS_SYSTEM_CONFIG_FILE="${DATA_DIR}/system.conf") add_definitions(-DTEST_QTDBUSTEST_WATCHDOG_BIN="${CMAKE_BINARY_DIR}/src/watchdog/watchdog") set( UNIT_TESTS_SRC main.cpp libqtdbustest/TestDBusTestRunner.cpp libqtdbustest/TestQProcessDBusService.cpp libqtdbustest/TestSuicidalProcess.cpp ) add_executable( unit-tests ${UNIT_TESTS_SRC} ) qt5_use_modules( unit-tests Core DBus Test ) target_link_libraries( unit-tests qtdbustest ${GTEST_LIBRARIES} ${GMOCK_LIBRARIES} ) add_test( unit-tests unit-tests ) ./tests/main.cpp0000644000004100000410000000217413033672340014042 0ustar www-datawww-data/* * Copyright (C) 2013 Canonical, Ltd. * * This library is free software; you can redistribute it and/or modify it under * the terms of version 3 of the GNU Lesser General Public License as published * by the Free Software Foundation. * * 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 program. If not, see . * * Author: Pete Woods */ #include #include #include int main(int argc, char **argv) { qputenv("LANG", "C.UTF-8"); unsetenv("LC_ALL"); // setlocale(LC_ALL, ""); // bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR); // textdomain(GETTEXT_PACKAGE); QCoreApplication application(argc, argv); ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } ./tests/libqtdbustest/0000755000004100000410000000000013033672340015277 5ustar www-datawww-data./tests/libqtdbustest/TestDBusTestRunner.cpp0000644000004100000410000000552213033672340021536 0ustar www-datawww-data/* * Copyright (C) 2013 Canonical, Ltd. * * This library is free software; you can redistribute it and/or modify it under * the terms of version 3 of the GNU Lesser General Public License as published * by the Free Software Foundation. * * 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 program. If not, see . * * Author: Pete Woods */ #include #include #include #include using namespace std; using namespace testing; using namespace QtDBusTest; namespace { class TestDBusTestRunner: public Test { protected: TestDBusTestRunner() : dbusTestRunner(TEST_DBUS_SESSION_CONFIG_FILE, TEST_DBUS_SYSTEM_CONFIG_FILE) { } virtual ~TestDBusTestRunner() { } DBusTestRunner dbusTestRunner; }; TEST_F(TestDBusTestRunner, StartsSessionService) { QSharedPointer process( new QProcessDBusService("test.session.name", QDBusConnection::SessionBus, "/usr/bin/python3", QStringList() << "-m" << "dbusmock" << "test.session.name" << "/test/object" << "test.Interface")); dbusTestRunner.registerService(process); dbusTestRunner.startServices(); QProcess pgrep; pgrep.start("ps", QStringList() << "--no-headers" << "--pid" << QString::number(process->pid()) << "-o" << "args"); pgrep.waitForFinished(); pgrep.waitForReadyRead(); EXPECT_EQ( "/usr/bin/python3 -m dbusmock test.session.name /test/object test.Interface", QString::fromUtf8(pgrep.readAll().trimmed()).toStdString()); } TEST_F(TestDBusTestRunner, StartsSystemService) { QSharedPointer process( new QProcessDBusService("test.system.name", QDBusConnection::SystemBus, "/usr/bin/python3", QStringList() << "-m" << "dbusmock" << "-s" << "test.system.name" << "/test/object" << "test.Interface")); dbusTestRunner.registerService(process); dbusTestRunner.startServices(); QProcess pgrep; pgrep.start("ps", QStringList() << "--no-headers" << "--pid" << QString::number(process->pid()) << "-o" << "args"); pgrep.waitForFinished(); pgrep.waitForReadyRead(); EXPECT_EQ( "/usr/bin/python3 -m dbusmock -s test.system.name /test/object test.Interface", QString::fromUtf8(pgrep.readAll().trimmed()).toStdString()); } TEST_F(TestDBusTestRunner, SetsEnvironmentVariables) { EXPECT_EQ(dbusTestRunner.sessionBus(), qgetenv("DBUS_SESSION_BUS_ADDRESS")); EXPECT_EQ(dbusTestRunner.systemBus(), qgetenv("DBUS_SYSTEM_BUS_ADDRESS")); } } // namespace ./tests/libqtdbustest/TestSuicidalProcess.cpp0000644000004100000410000000301413033672340021735 0ustar www-datawww-data/* * Copyright (C) 2013 Canonical, Ltd. * * This library is free software; you can redistribute it and/or modify it under * the terms of version 3 of the GNU Lesser General Public License as published * by the Free Software Foundation. * * 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 program. If not, see . * * Author: Pete Woods */ #include #include #include using namespace std; using namespace testing; using namespace QtDBusTest; namespace { class TestSuicidalProcess: public Test { protected: TestSuicidalProcess() { process.setWatchdogCommand(TEST_QTDBUSTEST_WATCHDOG_BIN); } virtual ~TestSuicidalProcess() { process.kill(); process.waitForFinished(); } SuicidalProcess process; }; TEST_F(TestSuicidalProcess, BehavesLikeNormalQProcess) { process.start("sleep", QStringList() << "5"); QProcess pgrep; pgrep.start("ps", QStringList() << "--no-headers" << "--pid" << QString::number(process.pid()) << "-o" << "args"); pgrep.waitForFinished(); pgrep.waitForReadyRead(); EXPECT_EQ("sleep 5", QString::fromUtf8(pgrep.readAll().trimmed()).toStdString()); } } // namespace ./tests/libqtdbustest/TestQProcessDBusService.cpp0000644000004100000410000000426013033672340022503 0ustar www-datawww-data/* * Copyright (C) 2013 Canonical, Ltd. * * This library is free software; you can redistribute it and/or modify it under * the terms of version 3 of the GNU Lesser General Public License as published * by the Free Software Foundation. * * 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 program. If not, see . * * Author: Pete Woods */ #include #include #include #include #include using namespace std; using namespace testing; using namespace QtDBusTest; namespace { class TestQProcessDBusService: public Test { protected: TestQProcessDBusService() : dbusTestRunner(TEST_DBUS_SESSION_CONFIG_FILE, TEST_DBUS_SYSTEM_CONFIG_FILE) { } virtual ~TestQProcessDBusService() { } DBusTestRunner dbusTestRunner; }; TEST_F(TestQProcessDBusService, WaitsForServiceAppeared) { QProcessDBusService process("test.name", QDBusConnection::SessionBus, "/usr/bin/python3", QStringList() << "-m" << "dbusmock" << "test.name" << "/test/object" << "test.Interface"); process.start(dbusTestRunner.sessionConnection()); QProcess pgrep; pgrep.start("ps", QStringList() << "--no-headers" << "--pid" << QString::number(process.pid()) << "-o" << "args"); pgrep.waitForFinished(); pgrep.waitForReadyRead(); EXPECT_EQ( "/usr/bin/python3 -m dbusmock test.name /test/object test.Interface", QString::fromUtf8(pgrep.readAll().trimmed()).toStdString()); } TEST_F(TestQProcessDBusService, ThrowsErrorForFailToStart) { QProcessDBusService process("test.name", QDBusConnection::SessionBus, "/usr/bin/python3", QStringList() << "-m" << "dbusmock" << "not.test.name" << "/test/object" << "test.Interface"); ASSERT_THROW(process.start(dbusTestRunner.sessionConnection()), std::logic_error); } } // namespace ./tests/data/0000755000004100000410000000000013033672340013317 5ustar www-datawww-data./tests/data/valgrind.suppression0000644000004100000410000000154413033672340017445 0ustar www-datawww-data{ Ignore pthreads Memcheck:Leak ... fun:pthread_create@@* } { Glib memalign Memcheck:Leak ... fun:posix_memalign } { Ignore glib mainloop Memcheck:Leak ... fun:g_main_context_push_thread_default } { Ignore glib main context Memcheck:Leak ... fun:g_main_context_new } { False positive leak on strings in i386 Memcheck:Leak fun:_Znwj fun:_ZNSs4_Rep9_S_createEjjRKSaIcE ... fun:_ZN7testing14InitGoogleTestEPiPPc fun:(below main) } { False positive leak on strings on i386 Memcheck:Leak fun:_Znwj fun:_ZNSs4_Rep9_S_createEjjRKSaIcE ... fun:__libc_csu_init fun:(below main) } { Qt apparently has some statically allocated data here Memcheck:Leak fun:calloc fun:allocate_dtv fun:_ZN7QThread5startENS_8PriorityE obj:/usr/lib/*/libQt5Core.so.* ... obj:* } ./build.sh0000755000004100000410000000267413033672340012713 0ustar www-datawww-data#! /bin/bash set -e ECLIPSE=false CLEAN=false SOURCEDIR="$PWD" BRANCHNAME=`basename $SOURCEDIR` BUILDDIR="$SOURCEDIR/../$BRANCHNAME-build" usage() { echo "usage: $0 [OPTIONS] [BUILD_TYPE]\n" >&2 echo "Script to build libqtdbustest. If BUILD_TYPE is not specified, it defaults to \"Debug\".\n" >&2 echo "OPTIONS:" >&2 echo " -e, --eclipse Generate Eclipse projects" >&2 echo " -c, --clean Clean the build tree before building" >&2 echo >&2 exit 1 } ARGS=`getopt -n$0 -u -a --longoptions="eclipse,clean,help" -o "sch" -- "$@"` [ $? -ne 0 ] && usage eval set -- "$ARGS" while [ $# -gt 0 ] do case "$1" in -e|--eclipse) ECLIPSE=true;; -c|--clean) CLEAN=true;; -h|--help) usage;; --) shift;break;; esac shift done [ $# -gt 1 ] && usage BUILD_TYPE="Debug" [ $# -eq 1 ] && BUILD_TYPE="$1" if [ -f "/usr/bin/ninja" ] ; then if $ECLIPSE; then GENERATOR="Eclipse CDT4 - Ninja" else GENERATOR="Ninja" fi BUILD_COMMAND="ninja" else if $ECLIPSE; then GENERATOR="Eclipse CDT4 - Unix Makefiles" else GENERATOR="Unix Makefiles" fi BUILD_COMMAND="make" fi echo "Using $BUILD_COMMAND to build" if $CLEAN; then rm -rf $BUILDDIR fi mkdir -p $BUILDDIR ( cd $BUILDDIR cmake "$SOURCEDIR" -G "$GENERATOR" \ -DCMAKE_BUILD_TYPE=$BUILD_TYPE \ -DENABLE_MEMCHECK_OPTION=YES \ -DCMAKE_INSTALL_PREFIX="$SOURCEDIR/../$BRANCHNAME-install" $BUILD_COMMAND ) ./data/0000755000004100000410000000000013033672340012155 5ustar www-datawww-data./data/CMakeLists.txt0000644000004100000410000000014513033672340014715 0ustar www-datawww-data install( FILES session.conf system.conf DESTINATION "${CMAKE_INSTALL_DATADIR}/libqtdbustest/" )./data/system.conf0000644000004100000410000000123213033672340014346 0ustar www-datawww-data system unix:tmpdir=/tmp ./data/session.conf0000644000004100000410000000315513033672340014513 0ustar www-datawww-data session unix:tmpdir=/tmp 60000 1000000000 1000000000 1000000000 120000 240000 100000 10000 100000 10000 50000 50000 50000 300000