diff --git a/CMakeLists.txt b/CMakeLists.txt index 12970c8..c91a4b3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,129 +1,130 @@ # Copyright (C) 2008 by Volker Lanz # Copyright (C) 2014-2019 by Andrius Štikonas # # 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 . project(kpmcore) cmake_minimum_required(VERSION 3.1 FATAL_ERROR) set(CMAKE_USE_RELATIVE_PATHS OFF) set(CMAKE_BUILD_WITH_INSTALL_RPATH ON) # Dependencies set(QT_MIN_VERSION "5.10.0") set(KF5_MIN_VERSION "5.56") set(BLKID_MIN_VERSION "2.33.2") # Qca-qt5 (tested with botan and ossl backends) # Runtime # smartmontools 7.0 # Qca plugin (botan or ossl) set(VERSION_MAJOR "4") set(VERSION_MINOR "0") set(VERSION_RELEASE "0") set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_RELEASE}) set(SOVERSION "8") add_definitions(-D'VERSION="${VERSION}"') #" set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(ECM ${KF5_MIN_VERSION} REQUIRED NO_MODULE) set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/") find_package(PolkitQt5-1 REQUIRED) include(KDEInstallDirs) include(KDECMakeSettings) include(KDECompilerSettings NO_POLICY_SCOPE) include(FeatureSummary) include(GenerateExportHeader) include(ECMSetupVersion) ecm_setup_version(${VERSION} VARIABLE_PREFIX KPMCORE VERSION_HEADER "${CMAKE_CURRENT_BINARY_DIR}/kpmcore_version.h" PACKAGE_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/KPMcoreConfigVersion.cmake" SOVERSION ${SOVERSION}) find_package(Qt5 ${QT_MIN_VERSION} CONFIG REQUIRED COMPONENTS Core DBus Gui + Test Widgets ) # We do not link to KF5::AuthCore because we just have build-time dependency on it # Load the frameworks we need find_package(KF5 ${KF5_MIN_VERSION} REQUIRED Auth CoreAddons I18n WidgetsAddons ) find_package(Qca-qt5 REQUIRED) # use sane compile flags add_definitions( -DQT_USE_QSTRINGBUILDER -DQT_NO_CAST_TO_ASCII -DQT_NO_CAST_FROM_ASCII -DQT_STRICT_ITERATORS -DQT_NO_URL_CAST_FROM_STRING -DQT_NO_CAST_FROM_BYTEARRAY -DQT_NO_CAST_TO_BYTEARRAY -DQT_NO_SIGNALS_SLOTS_KEYWORDS -DQT_USE_FAST_OPERATOR_PLUS ) kde_enable_exceptions() if(${CMAKE_SYSTEM_NAME} MATCHES "Linux") find_package(PkgConfig REQUIRED) pkg_check_modules(BLKID REQUIRED blkid>=${BLKID_MIN_VERSION}) endif() include_directories(${Qt5Core_INCLUDE_DIRS} ${UUID_INCLUDE_DIRS} ${BLKID_INCLUDE_DIRS} lib/ src/) add_subdirectory(src) # create a Config.cmake and a ConfigVersion.cmake file and install them set(INCLUDE_INSTALL_DIR "include/kpmcore/") set(CMAKECONFIG_INSTALL_DIR "${CMAKECONFIG_INSTALL_PREFIX}/KPMcore") configure_package_config_file("${CMAKE_CURRENT_SOURCE_DIR}/KPMcoreConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/KPMcoreConfig.cmake" INSTALL_DESTINATION ${CMAKECONFIG_INSTALL_DIR} PATH_VARS INCLUDE_INSTALL_DIR ) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/KPMcoreConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/KPMcoreConfigVersion.cmake" DESTINATION "${CMAKECONFIG_INSTALL_DIR}" COMPONENT Devel ) install(EXPORT KPMcoreTargets DESTINATION "${CMAKECONFIG_INSTALL_DIR}" FILE KPMcoreTargets.cmake ) ki18n_install(po) set_target_properties( kpmcore PROPERTIES VERSION ${VERSION} SOVERSION ${SOVERSION} ) message(STATUS "kpmcore ${VERSION} will be built for install into ${CMAKE_INSTALL_PREFIX}") feature_summary(WHAT ALL INCLUDE_QUIET_PACKAGES FATAL_ON_MISSING_REQUIRED_PACKAGES) enable_testing() add_subdirectory(test) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 6d8f0a5..4d5d736 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,68 +1,81 @@ # Tests for KPMcore # # These are not so much "tests" as "small example programs". They illustrate # 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 add_compile_options(-fPIC) ### # # Helper macro to link to the helper (for initialization of kpmcore) # and to add a test with the given name. # add_library(testhelpers STATIC helpers.cpp) target_link_libraries(testhelpers) 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::DBus 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 kpm_test(testinit testinit.cpp) # Default backend if(TARGET pmdummybackendplugin) add_test(NAME testinit-dummy COMMAND testinit $) endif() if(TARGET pmsfdiskbackendplugin) add_test(NAME testinit-sfdisk COMMAND testinit $) else() return() # All the rest really needs a working backend endif() set(BACKEND $) ### # # Listing devices, partitions kpm_test(testlist testlist.cpp) add_test(NAME testlist COMMAND testlist ${BACKEND}) kpm_test(testdevicescanner testdevicescanner.cpp) add_test(NAME testdevicescanner COMMAND testdevicescanner ${BACKEND}) find_package (Threads) ### # # Execute external commands as root kpm_test(testexternalcommand testexternalcommand.cpp) add_test(NAME testexternalcommand COMMAND testexternalcommand ${BACKEND}) # Including SMART files reference set(SMARTPARSER ${CMAKE_SOURCE_DIR}/src/core/smartdiskinformation.cpp ${CMAKE_SOURCE_DIR}/src/core/smartattributeparseddata.cpp ${CMAKE_SOURCE_DIR}/src/core/smartparser.cpp) # Test SMART support kpm_test(testsmart testsmart.cpp ${SMARTPARSER}) add_test(NAME testsmart COMMAND testsmart ${BACKEND}) # Test Device kpm_test(testdevice testdevice.cpp) add_test(NAME testdevice COMMAND testdevice ${BACKEND}) + + +# Include Polkit-Qt1 backend files reference +set(POLKITBACKEND ${CMAKE_SOURCE_DIR}/src/util/externalcommand_polkitbackend.cpp) + +# Test PolkitQt-1 Authorization Backend +kpm_test(testpolkitauthbackend testpolkitauthbackend.cpp ${POLKITBACKEND}) +add_test(NAME testpolkitauthbackend COMMAND testpolkitauthbackend ${BACKEND}) diff --git a/test/testpolkitauthbackend.cpp b/test/testpolkitauthbackend.cpp new file mode 100644 index 0000000..08b469f --- /dev/null +++ b/test/testpolkitauthbackend.cpp @@ -0,0 +1,110 @@ +/************************************************************************* + * 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::startHelper(const QString &action, const QString &helperID) const +{ + Q_UNUSED(action) + + PolkitQt1Backend *m_authJob = new PolkitQt1Backend; + m_authJob->startHelper(action, helperID); +} + +void TestPolkitAuthBackend::initAgent(const QString &action, QWidget *parent/* = nullptr*/) const +{ + Q_UNUSED(action) + Q_UNUSED(parent) + + 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::revokeAuthorization(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) diff --git a/test/testpolkitauthbackend.h b/test/testpolkitauthbackend.h new file mode 100644 index 0000000..e10dba9 --- /dev/null +++ b/test/testpolkitauthbackend.h @@ -0,0 +1,50 @@ +/************************************************************************* + * 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 startHelper(const QString &action, const QString &helperID) const; + 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 revokeAuthorization(const QString &action, const QByteArray &callerID) const; +}; + +#endif // TESTPOLKITAUTHBACKEND_H +