File indexing completed on 2024-09-08 04:18:35
0001 0002 0003 #include "test.h" 0004 #include <polkitqt1-authority.h> 0005 #include <polkitqt1-agent-session.h> 0006 #include <polkitqt1-details.h> 0007 #include <stdlib.h> 0008 #include <unistd.h> 0009 #include <pwd.h> 0010 #include <QDBusMessage> 0011 #include <QDBusConnection> 0012 #include <QSignalSpy> 0013 0014 using namespace PolkitQt1; 0015 using namespace PolkitQt1::Agent; 0016 0017 void wait() 0018 { 0019 for (int i = 0; i < 100; i++) { 0020 usleep(100); 0021 QCoreApplication::processEvents(); 0022 } 0023 } 0024 0025 void TestAuth::test_Auth_checkAuthorization() 0026 { 0027 // This needs the file org.qt.policykit.examples.policy from examples to be installed 0028 UnixProcessSubject process(QCoreApplication::applicationPid()); 0029 Authority::Result result; 0030 // Check if this method returns good authorization results 0031 Authority *authority = Authority::instance(); 0032 result = authority->checkAuthorizationSync("org.qt.policykit.examples.kick", process, Authority::None); 0033 QCOMPARE(result, Authority::No); 0034 QVERIFY(!authority->hasError()); 0035 result = authority->checkAuthorizationSync("org.qt.policykit.examples.cry", process, Authority::None); 0036 QCOMPARE(result, Authority::Yes); 0037 QVERIFY(!authority->hasError()); 0038 result = authority->checkAuthorizationSync("org.qt.policykit.examples.bleed", process, Authority::None); 0039 QCOMPARE(result, Authority::Challenge); 0040 QVERIFY(!authority->hasError()); 0041 0042 // Now we try async methods 0043 QSignalSpy spy(authority, SIGNAL(checkAuthorizationFinished(PolkitQt1::Authority::Result))); 0044 // Call asynchronous checkAuthorization 0045 authority->checkAuthorization("org.qt.policykit.examples.kick", process, Authority::None); 0046 // Give the polkit time to obtain the result and emit the signal with it 0047 wait(); 0048 // Test if the signal was emitted 0049 QCOMPARE(spy.count(), 1); 0050 // Test the result 0051 result = spy.takeFirst()[0].value<PolkitQt1::Authority::Result>(); 0052 QCOMPARE(result, Authority::No); 0053 QVERIFY(!authority->hasError()); 0054 spy.clear(); 0055 0056 // Let's test the cancellability 0057 authority->checkAuthorization("org.qt.policykit.examples.kick", process, Authority::None); 0058 authority->checkAuthorizationCancel(); 0059 // Wait and check if the signal arrieved 0060 wait(); 0061 QCOMPARE(spy.count(), 0); 0062 0063 // Check if it can cancel user authentication dialog 0064 authority->checkAuthorization("org.qt.policykit.examples.bleed", process, Authority::AllowUserInteraction); 0065 // Show it for second 0066 sleep(1); 0067 // And now kill it 0068 authority->checkAuthorizationCancel(); 0069 QVERIFY(!authority->hasError()); 0070 // But how to test if it was successful? 0071 qWarning() << "You should see an authentication dialog for a short period."; 0072 } 0073 0074 void TestAuth::test_Auth_enumerateActions() 0075 { 0076 // This needs the file org.qt.policykit.examples.policy from examples to be installed 0077 ActionDescription::List list = Authority::instance()->enumerateActionsSync(); 0078 QVERIFY(!Authority::instance()->hasError()); 0079 // Check whether enumerateAction returns at least example actions 0080 int count = 0; 0081 Q_FOREACH(const ActionDescription &ad, list) { 0082 if ((ad.actionId() == "org.qt.policykit.examples.kick") || 0083 (ad.actionId() == "org.qt.policykit.examples.cry") || 0084 (ad.actionId() == "org.qt.policykit.examples.bleed")) 0085 count++; 0086 } 0087 QCOMPARE(count, 3); 0088 0089 0090 // Test asynchronous version as well 0091 list.clear(); 0092 count = 0; 0093 QSignalSpy spy(Authority::instance(), SIGNAL(enumerateActionsFinished(PolkitQt1::ActionDescription::List))); 0094 Authority::instance()->enumerateActions(); 0095 wait(); 0096 QCOMPARE(spy.count(), 1); 0097 list = spy.takeFirst()[0].value<PolkitQt1::ActionDescription::List>(); 0098 QVERIFY(!Authority::instance()->hasError()); 0099 Q_FOREACH(const ActionDescription &ad, list) { 0100 if ((ad.actionId() == "org.qt.policykit.examples.kick") || 0101 (ad.actionId() == "org.qt.policykit.examples.cry") || 0102 (ad.actionId() == "org.qt.policykit.examples.bleed")) 0103 count++; 0104 } 0105 QCOMPARE(count, 3); 0106 0107 // Test cancelling the enumeration 0108 spy.clear(); 0109 Authority::instance()->enumerateActions(); 0110 Authority::instance()->enumerateActionsCancel(); 0111 wait(); 0112 QCOMPARE(spy.count(), 0); 0113 QVERIFY(!Authority::instance()->hasError()); 0114 } 0115 0116 void TestAuth::test_Identity() 0117 { 0118 // Get real name and id of current user and group 0119 struct passwd *userinfo = getpwuid(getuid()); 0120 QString userName = userinfo->pw_name; 0121 unsigned int userId = userinfo->pw_uid; 0122 unsigned int groupId = userinfo->pw_gid; 0123 0124 // Try to create UnixUser from username 0125 UnixUserIdentity user(userName); 0126 QVERIFY(user.identity()); 0127 0128 // Create generic Identity from UnixUser via string representation 0129 Identity id = Identity::fromString(user.toString()); 0130 // Compare obtained uid with real uid 0131 QCOMPARE(id.toUnixUserIdentity().uid(), userId); 0132 0133 // Create generic Identity from UnixGroup via string representation 0134 UnixGroupIdentity group(groupId); 0135 QVERIFY(group.identity()); 0136 id = Identity::fromString(group.toString()); 0137 QCOMPARE(id.toUnixGroupIdentity().gid(), groupId); 0138 0139 // Test setting gid to another value 0140 group.setGid(9999U); 0141 id = Identity::fromString(group.toString()); 0142 QCOMPARE(id.toUnixGroupIdentity().gid(), 9999U); 0143 } 0144 0145 void TestAuth::test_Authority() 0146 { 0147 Authority *authority = Authority::instance(); 0148 QVERIFY(authority); 0149 QVERIFY(!authority->hasError()); 0150 0151 // Verify emitting of the signals 0152 QSignalSpy spy(authority, SIGNAL(consoleKitDBChanged())); 0153 QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.ConsoleKit", 0154 "/org/freedesktop/ConsoleKit/Manager", 0155 "org.freedesktop.ConsoleKit.Manager", 0156 "OpenSession"); 0157 QDBusMessage reply = QDBusConnection::systemBus().call(msg); 0158 QString cookie; 0159 cookie = reply.arguments()[0].value<QString>(); 0160 0161 0162 msg = QDBusMessage::createMethodCall("org.freedesktop.ConsoleKit", 0163 "/org/freedesktop/ConsoleKit/Manager", 0164 "org.freedesktop.ConsoleKit.Manager", 0165 "CloseSession"); 0166 msg.setArguments(QList<QVariant> () << cookie); 0167 QDBusConnection::systemBus().call(msg); 0168 // FIXME: Emitting consoleKitDBChanged is not working now 0169 qWarning() << "Emitting consoleKitDBChanged is not working now, test will be skipped"; 0170 //QVERIFY(spy.count() > 0); 0171 QVERIFY(!authority->hasError()); 0172 0173 // configChanged signal from authority requires changing some policy files 0174 // and it would require user interaction (typing the password) 0175 // so this is not covered by this test 0176 } 0177 0178 void TestAuth::test_Subject() 0179 { 0180 // Get pid of this application 0181 qint64 pid = QCoreApplication::applicationPid(); 0182 // Create unix process for it 0183 UnixProcessSubject *process = new UnixProcessSubject(pid); 0184 // Test if pid doesn't differ 0185 QCOMPARE(process->pid(), pid); 0186 0187 // Serialize and deserialize subject 0188 //Subject *subject = Subject::fromString(process->toString()); 0189 // and try it 0190 //QCOMPARE(((UnixProcess *) subject)->pid(), pid); 0191 delete process; 0192 } 0193 0194 void TestAuth::test_Session() 0195 { 0196 /* 0197 UnixUser user(getuid()); 0198 Session *session = new Session(&user, "/org/freedesktop/ConsoleKit/Session2"); 0199 QSignalSpy spy_completed(session, SIGNAL(completed(bool))); 0200 QSignalSpy spy_request(session, SIGNAL(request(QString,bool))); 0201 QSignalSpy spy_error(session, SIGNAL(showError(QString))); 0202 QSignalSpy spy_info(session, SIGNAL(showInfo(QString))); 0203 session->initiate(); 0204 session->response("aaa"); 0205 // Canceling should emit the "completed" signal 0206 session->cancel(); 0207 QCOMPARE(spy_completed.count(), 1); 0208 0209 //UnixProcess *process = new UnixProcess(QCoreApplication::applicationPid()); 0210 //Authority::instance()->checkAuthorization("org.qt.policykit.examples.kick", process, Authority::None); 0211 0212 qDebug() << "COMPLETED:" << spy_completed.count(); 0213 qDebug() << "REQUEST:" << spy_request.count(); 0214 qDebug() << "ERROR:" << spy_error.count(); 0215 qDebug() << "INFO:" << spy_info.count(); 0216 */ 0217 } 0218 0219 void TestAuth::test_Details() 0220 { 0221 Details details; 0222 details.insert("1", "aaa"); 0223 details.insert("2", "bbb"); 0224 details.insert("3", "ccc"); 0225 details.insert("4", "ddd"); 0226 QCOMPARE(details.lookup("1"), QString("aaa")); 0227 QCOMPARE(details.lookup("2"), QString("bbb")); 0228 QCOMPARE(details.lookup("3"), QString("ccc")); 0229 QCOMPARE(details.lookup("4"), QString("ddd")); 0230 QList<QString> list = details.keys(); 0231 QVERIFY(list.contains("1")); 0232 QVERIFY(list.contains("2")); 0233 QVERIFY(list.contains("3")); 0234 QVERIFY(list.contains("4")); 0235 } 0236 0237 QTEST_MAIN(TestAuth)