diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,6 +61,7 @@ Core DBus Gui + Test Widgets ) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -4,7 +4,7 @@ # how to use the library, how to perform common tasks. set(CMAKE_SKIP_BUILD_RPATH FALSE) -SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) +set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) include_directories(${CMAKE_SOURCE_DIR}/src) # To get at KPMcore headers @@ -20,9 +20,14 @@ macro (kpm_test name) add_executable(${name} ${ARGN}) - target_link_libraries(${name} testhelpers kpmcore Qt5::Core) + target_link_libraries(${name} testhelpers kpmcore Qt5::Core Qt5::Test ${POLKITQT-1_LIBRARIES}) endmacro() +if(NOT Qt5Test_FOUND) + message(STATUS "Qt5Test not found, testpolkitauthbackend will not be built.") + return() +endif() + ### # # Tests of initialization: try explicitly loading some backends @@ -66,3 +71,7 @@ # Test Device kpm_test(testdevice testdevice.cpp) add_test(NAME testdevice COMMAND testdevice ${BACKEND}) + +# Test PolkitQt-1 Authorization Backend +kpm_test(testpolkitauthbackend testpolkitauthbackend.cpp) +add_test(Name testpolkitauthbackend COMMAND testpolkitauthbackend ${BACKEND}) diff --git a/test/testpolkitauthbackend.h b/test/testpolkitauthbackend.h new file mode 100644 --- /dev/null +++ b/test/testpolkitauthbackend.h @@ -0,0 +1,48 @@ +/************************************************************************* + * Copyright (C) 2019 by Shubham * + * * + * This program is free software; you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation; either version 3 of * + * the License, or (at your option) any later version. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see .* +**************************************************************************/ + +// SPDX-License-Identifier: GPL-3.0+ + +#ifndef TESTPOLKITAUTHBACKEND_H +#define TESTPOLKITAUTHBACKEND_H + +#include + +#include + +class QByteArray; +class QString; + +using namespace PolkitQt1; + +class TestPolkitAuthBackend : public QObject +{ + Q_OBJECT + +public: + TestPolkitAuthBackend(); + ~TestPolkitAuthBackend(); + +private Q_SLOTS: + void initAgent(const QString &action, QWidget *parent = nullptr) const; + QByteArray callerID() const; + Authority::Result actionStatus(const QString &action, const QByteArray &callerID) const; + bool authorizeAction(const QString &action, const QByteArray &caller) const; + bool stopAction(const QString &action, const QByteArray &callerID) const; +}; + +#endif // TESTPOLKITAUTHBACKEND_H diff --git a/test/testpolkitauthbackend.cpp b/test/testpolkitauthbackend.cpp new file mode 100644 --- /dev/null +++ b/test/testpolkitauthbackend.cpp @@ -0,0 +1,98 @@ +/************************************************************************* + * Copyright (C) 2019 by Shubham * + * * + * This program is free software; you can redistribute it and/or * + * modify it under the terms of the GNU General Public License as * + * published by the Free Software Foundation; either version 3 of * + * the License, or (at your option) any later version. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see .* +**************************************************************************/ + +// SPDX-License-Identifier: GPL-3.0+ + +#include "testpolkitauthbackend.h" +#include "util/externalcommand_polkitbackend.h" + +#include +#include +#include +#include +#include + +#include +#include + +using namespace Auth; +using namespace PolkitQt1; + +TestPolkitAuthBackend::TestPolkitAuthBackend() +{ +} + +TestPolkitAuthBackend::~TestPolkitAuthBackend() +{ +} + +void TestPolkitAuthBackend::initAgent(const QString &action, QWidget *parent/* = nullptr*/) const +{ + /*PolkitQt1Backend *m_authJob = new PolkitQt1Backend(); + m_authJob->initPolkitAgent(action, parent);*/ +} + +QByteArray TestPolkitAuthBackend::callerID() const +{ + return QByteArray("Random caller ID"); +} + +Authority::Result TestPolkitAuthBackend::actionStatus(const QString &action, const QByteArray &callerID) const +{ + SystemBusNameSubject subject(QString::fromUtf8(callerID)); + + auto authority = PolkitQt1::Authority::instance(); + auto result = authority->checkAuthorizationSync(action, subject, Authority::None); + + return result; +} + +bool TestPolkitAuthBackend::authorizeAction(const QString &action, const QByteArray &caller) const +{ + if (action == QLatin1String("doomed.to.fail")) { + return false; + } else if (action == QLatin1String("requires.auth")) { + return true; + } else if (action == QLatin1String("generates.error")) { + return false; + } else if (action == QLatin1String("always.authorized")) { + return true; + } else if (action.startsWith(QLatin1String("org.kde.externalcommand.init"))) { + qDebug() << "Caller ID:" << callerID(); + const QByteArray calling = callerID(); + if (caller == calling) { + return true; + } else { + return false; + } + } + + return false; +} + +bool TestPolkitAuthBackend::stopAction(const QString &action, const QByteArray &callerID) const +{ + Q_UNUSED(action) + + SystemBusNameSubject subject(QString::fromUtf8(callerID)); + + auto authority = Authority::instance(); + + return authority->revokeTemporaryAuthorizationsSync(subject); +} + +QTEST_MAIN(TestPolkitAuthBackend)