Changeset View
Changeset View
Standalone View
Standalone View
test/testpolkitauthbackend.cpp
- This file was added.
1 | /************************************************************************* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 by Shubham <aryan100jangid@gmail.com> * | ||||
3 | * * | ||||
4 | * This program is free software; you can redistribute it and/or * | ||||
5 | * modify it under the terms of the GNU General Public License as * | ||||
6 | * published by the Free Software Foundation; either version 3 of * | ||||
7 | * the License, or (at your option) any later version. * | ||||
8 | * * | ||||
9 | * This program is distributed in the hope that it will be useful, * | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||||
12 | * GNU General Public License for more details. * | ||||
13 | * * | ||||
14 | * You should have received a copy of the GNU General Public License * | ||||
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>.* | ||||
16 | **************************************************************************/ | ||||
17 | | ||||
18 | // SPDX-License-Identifier: GPL-3.0+ | ||||
19 | | ||||
20 | #include "testpolkitauthbackend.h" | ||||
21 | #include "util/externalcommand_polkitbackend.h" | ||||
22 | | ||||
23 | #include <QByteArray> | ||||
24 | #include <QCoreApplication> | ||||
25 | #include <QDebug> | ||||
26 | #include <QString> | ||||
27 | #include <QTest> | ||||
28 | | ||||
29 | #include <PolkitQt1/Authority> | ||||
30 | #include <PolkitQt1/Subject> | ||||
31 | | ||||
32 | using namespace Auth; | ||||
33 | using namespace PolkitQt1; | ||||
34 | | ||||
35 | TestPolkitAuthBackend::TestPolkitAuthBackend() | ||||
36 | { | ||||
37 | } | ||||
38 | | ||||
39 | TestPolkitAuthBackend::~TestPolkitAuthBackend() | ||||
40 | { | ||||
41 | } | ||||
42 | | ||||
43 | void TestPolkitAuthBackend::startHelper(const QString &action, const QString &helperID) const | ||||
44 | { | ||||
45 | PolkitQt1Backend *m_authJob = new PolkitQt1Backend; | ||||
46 | m_authJob->startHelper(action, helperID); | ||||
47 | } | ||||
48 | | ||||
49 | void TestPolkitAuthBackend::initAgent(const QString &action, QWidget *parent/* = nullptr*/) const | ||||
50 | { | ||||
51 | PolkitQt1Backend *m_authJob = new PolkitQt1Backend; | ||||
52 | m_authJob->initPolkitAgent(action, parent); | ||||
53 | } | ||||
54 | | ||||
55 | QByteArray TestPolkitAuthBackend::callerID() const | ||||
56 | { | ||||
57 | return QByteArray("Random caller ID"); | ||||
58 | } | ||||
59 | | ||||
60 | Authority::Result TestPolkitAuthBackend::actionStatus(const QString &action, const QByteArray &callerID) const | ||||
61 | { | ||||
62 | SystemBusNameSubject subject(QString::fromUtf8(callerID)); | ||||
63 | | ||||
64 | auto authority = PolkitQt1::Authority::instance(); | ||||
65 | auto result = authority->checkAuthorizationSync(action, subject, Authority::None); | ||||
66 | | ||||
67 | return result; | ||||
68 | } | ||||
69 | | ||||
70 | bool TestPolkitAuthBackend::authorizeAction(const QString &action, const QByteArray &caller) const | ||||
71 | { | ||||
72 | if (action == QLatin1String("doomed.to.fail")) { | ||||
73 | return false; | ||||
74 | } else if (action == QLatin1String("requires.auth")) { | ||||
75 | return true; | ||||
76 | } else if (action == QLatin1String("generates.error")) { | ||||
77 | return false; | ||||
78 | } else if (action == QLatin1String("always.authorized")) { | ||||
79 | return true; | ||||
80 | } else if (action.startsWith(QLatin1String("org.kde.externalcommand.init"))) { | ||||
81 | qDebug() << "Caller ID:" << callerID(); | ||||
82 | const QByteArray calling = callerID(); | ||||
83 | | ||||
84 | if (caller == calling) { | ||||
85 | return true; | ||||
86 | } else { | ||||
87 | return false; | ||||
88 | } | ||||
89 | } | ||||
90 | | ||||
91 | return false; | ||||
92 | } | ||||
93 | | ||||
94 | bool TestPolkitAuthBackend::revokeAuthorization(const QString &action, const QByteArray &callerID) const | ||||
95 | { | ||||
96 | Q_UNUSED(action) | ||||
97 | | ||||
98 | SystemBusNameSubject subject(QString::fromUtf8(callerID)); | ||||
99 | | ||||
100 | auto authority = Authority::instance(); | ||||
101 | | ||||
102 | return authority->revokeTemporaryAuthorizationsSync(subject); | ||||
103 | } | ||||
104 | | ||||
105 | QTEST_MAIN(TestPolkitAuthBackend) |