Index: autotests/CMakeLists.txt =================================================================== --- autotests/CMakeLists.txt +++ autotests/CMakeLists.txt @@ -49,6 +49,7 @@ ecm_add_tests( klocalsockettest.cpp klocalsocketservertest.cpp + privilegejobtest.cpp NAME_PREFIX "kiocore-" LINK_LIBRARIES KF5::KIOCore KF5::I18n Qt5::Test Qt5::Network ) Index: autotests/privilegejobtest.h =================================================================== --- /dev/null +++ autotests/privilegejobtest.h @@ -0,0 +1,43 @@ +/*** + Copyright (C) 2017 by Chinmoy Ranjan Pradhan + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + + 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 library. If not, see . +***/ + +#ifndef PRIVILEGEJOBTEST_H +#define PRIVILEGEJOBTEST_H + +#include + +class PrivilegeJobTest : public QObject +{ + Q_OBJECT +private Q_SLOTS: + void initTestCase(); + void cleanupTestCase(); + + void privilegeChmod(); + void privilegeCopy(); + void privilegeDelete(); + +private: + QString expectedMetaData() const; + + QString m_testFilePath; +}; + +#endif Index: autotests/privilegejobtest.cpp =================================================================== --- /dev/null +++ autotests/privilegejobtest.cpp @@ -0,0 +1,112 @@ +/*** + Copyright (C) 2017 by Chinmoy Ranjan Pradhan + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + + 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 library. If not, see . +***/ + +#include "privilegejobtest.h" + +#include + +#include +#include +#include + +#include "kiotesthelper.h" + +QTEST_MAIN(PrivilegeJobTest) + +#define R_OWNER QFileDevice::ReadOwner +#define RX_OWNER QFileDevice::ReadOwner | QFileDevice::ExeOwner +#define RWX_OWNER QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner +#define KIO_JOB_FLAGS KIO::HideProgressInfo | KIO::PrivilegeExecution +#define LOCALFILE(x) QUrl::fromLocalFile(x) + +void PrivilegeJobTest::initTestCase() +{ + // To avoid a runtime dependency on klauncher + qputenv("KDE_FORK_SLAVES", "yes"); + + cleanupTestCase(); + homeTmpDir(); + m_testFilePath = homeTmpDir() + "testfile"; + createTestFile(m_testFilePath); + QVERIFY(QFile::exists(m_testFilePath)); + QVERIFY(QFile::setPermissions(homeTmpDir(), RX_OWNER)); +} + +void PrivilegeJobTest::cleanupTestCase() +{ + QFile::setPermissions(homeTmpDir(), RWX_OWNER); + QDir(homeTmpDir()).removeRecursively(); +} + +void PrivilegeJobTest::privilegeChmod() +{ + KFileItem item(LOCALFILE(m_testFilePath)); + const mode_t origPerm = item.permissions(); + mode_t newPerm = origPerm ^ S_IWGRP; + QVERIFY(newPerm != origPerm); + // Remove search permission + QVERIFY(QFile::setPermissions(homeTmpDir(), R_OWNER)); + KFileItemList items; items << item; + KIO::Job *job = KIO::chmod(items, newPerm, S_IWGRP, QString(), QString(), false, KIO_JOB_FLAGS); + job->addMetaData("UnitTesting", "true"); + job->setUiDelegate(nullptr); + QVERIFY(job->exec()); + QCOMPARE(job->queryMetaData("TestData"), expectedMetaData()); + // Bring it back + QVERIFY(QFile::setPermissions(homeTmpDir(), RX_OWNER)); +} + +void PrivilegeJobTest::privilegeCopy() +{ + KIO::CopyJob *copyJob = KIO::copy(LOCALFILE(m_testFilePath), LOCALFILE(homeTmpDir() + "newtestfile"), KIO_JOB_FLAGS); + copyJob->addMetaData("UnitTesting", "true"); + copyJob->setUiDelegate(nullptr); + QSignalSpy spy(copyJob, SIGNAL(result(KJob*))); + QVERIFY(spy.isValid()); + QVERIFY(spy.wait(10000)); + QCOMPARE(copyJob->error(), 0); + + QCOMPARE(copyJob->queryMetaData("TestData"), expectedMetaData()); +} + +void PrivilegeJobTest::privilegeDelete() +{ + // Normal delete + KIO::DeleteJob *job = KIO::del(LOCALFILE(m_testFilePath), KIO_JOB_FLAGS); + //job->addMetaData("recurse", "true"); + job->addMetaData("UnitTesting", "true"); + job->setUiDelegate(nullptr); + QVERIFY(job->exec()); + QCOMPARE(job->queryMetaData("TestData"), expectedMetaData()); + + // Recursive delete + job = KIO::del(LOCALFILE(homeTmpDir()), KIO_JOB_FLAGS); + job->addMetaData("recurse", "true"); + job->addMetaData("UnitTesting", "true"); + job->setUiDelegate(nullptr); + QVERIFY(job->exec()); + QCOMPARE(job->queryMetaData("TestData"), expectedMetaData()); +} + +QString PrivilegeJobTest::expectedMetaData() const +{ + return QString("org.kde.kio.file.exec,true,org.kde.kio.file,true"); +} +