File indexing completed on 2024-12-01 12:30:19
0001 /* 0002 SPDX-FileCopyrightText: 2012 Dario Freddi <drf@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.1-or-later 0005 */ 0006 0007 #include "BackendsManager.h" 0008 #include <QTest> 0009 #include <kauth/actionreply.h> 0010 #include <kauth/executejob.h> 0011 0012 class SetupActionTest : public QObject 0013 { 0014 Q_OBJECT 0015 0016 public: 0017 SetupActionTest(QObject *parent = nullptr) 0018 : QObject(parent) 0019 { 0020 } 0021 0022 private Q_SLOTS: 0023 void initTestCase(); 0024 void init() 0025 { 0026 } 0027 0028 void testNonExistentAction(); 0029 #if KAUTHCORE_BUILD_DEPRECATED_SINCE(5, 71) 0030 void testBasicActionPropertiesDeprecated(); 0031 #endif 0032 void testBasicActionProperties(); 0033 void testUserAuthorization(); 0034 void testAuthorizationFail(); 0035 0036 void cleanup() 0037 { 0038 } 0039 void cleanupTestCase() 0040 { 0041 } 0042 0043 Q_SIGNALS: 0044 void changeCapabilities(KAuth::AuthBackend::Capabilities capabilities); 0045 0046 private: 0047 }; 0048 0049 void SetupActionTest::initTestCase() 0050 { 0051 connect(this, 0052 SIGNAL(changeCapabilities(KAuth::AuthBackend::Capabilities)), 0053 KAuth::BackendsManager::authBackend(), 0054 SLOT(setNewCapabilities(KAuth::AuthBackend::Capabilities))); 0055 } 0056 0057 void SetupActionTest::testNonExistentAction() 0058 { 0059 Q_EMIT changeCapabilities(KAuth::AuthBackend::AuthorizeFromHelperCapability | KAuth::AuthBackend::CheckActionExistenceCapability); 0060 KAuth::Action action(QLatin1String("i.do.not.exist")); 0061 QVERIFY(!action.isValid()); 0062 0063 action = KAuth::Action(QLatin1String("/safinvalid124%$&")); 0064 QVERIFY(action.isValid()); 0065 0066 // Now with regexp check 0067 Q_EMIT changeCapabilities(KAuth::AuthBackend::NoCapability); 0068 0069 action = KAuth::Action(QLatin1String("/safinvalid124%$&")); 0070 QVERIFY(!action.isValid()); 0071 } 0072 0073 #if KAUTHCORE_BUILD_DEPRECATED_SINCE(5, 71) 0074 void SetupActionTest::testBasicActionPropertiesDeprecated() 0075 { 0076 Q_EMIT changeCapabilities(KAuth::AuthBackend::AuthorizeFromHelperCapability | KAuth::AuthBackend::CheckActionExistenceCapability); 0077 KAuth::Action::DetailsMap detailsMap{{KAuth::Action::AuthDetail::DetailOther, QLatin1String("details")}}; 0078 KAuth::Action action(QLatin1String("always.authorized"), QLatin1String("details")); 0079 QVERIFY(action.isValid()); 0080 0081 QCOMPARE(action.name(), QLatin1String("always.authorized")); 0082 QCOMPARE(action.details(), QLatin1String("details")); 0083 QCOMPARE(action.detailsV2(), detailsMap); 0084 QVERIFY(!action.hasHelper()); 0085 QVERIFY(action.helperId().isEmpty()); 0086 QCOMPARE(action.status(), KAuth::Action::AuthorizedStatus); 0087 0088 QVERIFY(action.arguments().isEmpty()); 0089 QVariantMap args; 0090 args.insert(QLatin1String("akey"), QVariant::fromValue(42)); 0091 action.setArguments(args); 0092 QCOMPARE(action.arguments(), args); 0093 0094 action.setName(QLatin1String("i.do.not.exist")); 0095 QVERIFY(!action.isValid()); 0096 0097 Q_EMIT changeCapabilities(KAuth::AuthBackend::NoCapability); 0098 0099 action = KAuth::Action(QLatin1String("i.do.not.exist"), QLatin1String("details")); 0100 0101 QVERIFY(action.isValid()); 0102 QCOMPARE(action.name(), QLatin1String("i.do.not.exist")); 0103 QCOMPARE(action.details(), QLatin1String("details")); 0104 QCOMPARE(action.detailsV2(), detailsMap); 0105 QVERIFY(!action.hasHelper()); 0106 QVERIFY(action.helperId().isEmpty()); 0107 QCOMPARE(action.status(), KAuth::Action::InvalidStatus); 0108 } 0109 #endif 0110 0111 void SetupActionTest::testBasicActionProperties() 0112 { 0113 Q_EMIT changeCapabilities(KAuth::AuthBackend::AuthorizeFromHelperCapability | KAuth::AuthBackend::CheckActionExistenceCapability); 0114 KAuth::Action::DetailsMap detailsMap{{KAuth::Action::AuthDetail::DetailOther, QLatin1String("details")}}; 0115 KAuth::Action action(QLatin1String("always.authorized"), detailsMap); 0116 QVERIFY(action.isValid()); 0117 0118 QCOMPARE(action.name(), QLatin1String("always.authorized")); 0119 #if KAUTHCORE_BUILD_DEPRECATED_SINCE(5, 71) 0120 QCOMPARE(action.details(), QLatin1String("details")); 0121 #endif 0122 QCOMPARE(action.detailsV2(), detailsMap); 0123 QVERIFY(!action.hasHelper()); 0124 QVERIFY(action.helperId().isEmpty()); 0125 QCOMPARE(action.status(), KAuth::Action::AuthorizedStatus); 0126 0127 QVERIFY(action.arguments().isEmpty()); 0128 QVariantMap args; 0129 args.insert(QLatin1String("akey"), QVariant::fromValue(42)); 0130 action.setArguments(args); 0131 QCOMPARE(action.arguments(), args); 0132 0133 action.setName(QLatin1String("i.do.not.exist")); 0134 QVERIFY(!action.isValid()); 0135 0136 Q_EMIT changeCapabilities(KAuth::AuthBackend::NoCapability); 0137 0138 action = KAuth::Action(QLatin1String("i.do.not.exist"), detailsMap); 0139 0140 QVERIFY(action.isValid()); 0141 QCOMPARE(action.name(), QLatin1String("i.do.not.exist")); 0142 #if KAUTHCORE_BUILD_DEPRECATED_SINCE(5, 71) 0143 QCOMPARE(action.details(), QLatin1String("details")); 0144 #endif 0145 QCOMPARE(action.detailsV2(), detailsMap); 0146 QVERIFY(!action.hasHelper()); 0147 QVERIFY(action.helperId().isEmpty()); 0148 QCOMPARE(action.status(), KAuth::Action::InvalidStatus); 0149 } 0150 0151 void SetupActionTest::testUserAuthorization() 0152 { 0153 Q_EMIT changeCapabilities(KAuth::AuthBackend::CheckActionExistenceCapability); 0154 0155 KAuth::Action::DetailsMap detailsMap{{KAuth::Action::AuthDetail::DetailOther, QLatin1String("details")}}; 0156 KAuth::Action action(QLatin1String("requires.auth"), detailsMap); 0157 QVERIFY(action.isValid()); 0158 0159 QCOMPARE(action.status(), KAuth::Action::AuthRequiredStatus); 0160 KAuth::ExecuteJob *job = action.execute(); 0161 0162 QVERIFY(!job->exec()); 0163 0164 QCOMPARE(job->error(), (int)KAuth::ActionReply::BackendError); 0165 0166 Q_EMIT changeCapabilities(KAuth::AuthBackend::CheckActionExistenceCapability | KAuth::AuthBackend::AuthorizeFromClientCapability); 0167 0168 QVERIFY(action.isValid()); 0169 0170 QCOMPARE(action.status(), KAuth::Action::AuthRequiredStatus); 0171 job = action.execute(); 0172 0173 QVERIFY(job->exec()); 0174 0175 QVERIFY(!job->error()); 0176 QVERIFY(job->data().isEmpty()); 0177 } 0178 0179 void SetupActionTest::testAuthorizationFail() 0180 { 0181 Q_EMIT changeCapabilities(KAuth::AuthBackend::CheckActionExistenceCapability | KAuth::AuthBackend::AuthorizeFromClientCapability); 0182 0183 KAuth::Action::DetailsMap detailsMap{{KAuth::Action::AuthDetail::DetailOther, QLatin1String("details")}}; 0184 KAuth::Action action(QLatin1String("doomed.to.fail"), detailsMap); 0185 QVERIFY(action.isValid()); 0186 0187 QCOMPARE(action.status(), KAuth::Action::DeniedStatus); 0188 KAuth::ExecuteJob *job = action.execute(); 0189 0190 QVERIFY(!job->exec()); 0191 0192 QCOMPARE(job->error(), (int)KAuth::ActionReply::AuthorizationDeniedError); 0193 QVERIFY(job->data().isEmpty()); 0194 } 0195 0196 QTEST_MAIN(SetupActionTest) 0197 #include "SetupActionTest.moc"