diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,7 @@ set(QT_MIN_VERSION "5.10.0") set(KF5_MIN_VERSION "5.56") set(BLKID_MIN_VERSION "2.33.2") +set(POLKITQT-1_MIN_VERSION "0.112.0") # Qca-qt5 (tested with botan and ossl backends) # Runtime diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -62,3 +62,7 @@ # Test SMART support kpm_test(testsmart testsmart.cpp ${SMARTPARSER}) add_test(NAME testsmart COMMAND testsmart ${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 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 PolkitQt1Backend +{ + Q_OBJECT + +public: + TestPolkitAuthBackend(); + ~TestPolkitAuthBackend(); + +private Q_SLOTS: + void initAgent(const QString &action, QWidget *parent = nullptr) const; + QByteArray callerID() const override; + Authority::Result actionStatus(const QString &action, const QByteArray &callerID) const override; + bool authorizeAction(const QString &action, const QByteArray &callerID) override; + bool stopAction(const QString &action, const QByteArray &callerID) override; +}; + +#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,102 @@ +/************************************************************************* + * Copyright 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 "backend/corebackend.h" +#include "backend/corebackendmanager.h" +#include "util/externalcommand_polkitbackend.h" + +#include +#include + +#include +#include + +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 &callerID) +{ + if (action == QLatin1String("doomed.to.fail")) { + return false; + } else if (action == QLatin1String("requires.auth")) { + emit actionAuthorized(true); + return true; + } else if (action == QLatin1String("generates.error")) { + emit actionAuthorized(false); + return false; + } else if (action == QLatin1String("always.authorized")) { + return true; + } else if (action.startsWith(QLatin1String("org.kde.externalcommand.init"))) { + qDebug() << "Caller ID:" << callerId; + if (callerId == callerID()) { + emit actionAuthorized(true); + return true; + } else { + emit actionAuthorized(false); + return false; + } + } + + return false; +} + +bool TestPolkitAuthBackend::stopAction(const QString &action, const QByteArray &callerID) +{ + Q_UNUSED(action) + + SystemBusNameSubject subject(QString::fromUtf8(callerID)); + + auto authority = Authority::instance(); + + return authority->revokeTemporaryAuthorizationsSync(subject); +} + +QTEST_MAIN(TestPolkitAuthBackend) + +#include "moc_testpolkitauthbackend.cpp"