| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
'''polkit mock template |
| 4 |
|
| 5 |
This creates the basic methods and properties of the |
| 6 |
org.freedesktop.PolicyKit1.Authority object, so that we can use it async |
| 7 |
''' |
| 8 |
|
| 9 |
# This program is free software; you can redistribute it and/or modify it under |
| 10 |
# the terms of the GNU Lesser General Public License as published by the Free |
| 11 |
# Software Foundation; either version 3 of the License, or (at your option) any |
| 12 |
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text |
| 13 |
# of the license. |
| 14 |
|
| 15 |
__author__ = 'Marco Trevisan' |
| 16 |
__email__ = '[email protected]' |
| 17 |
__copyright__ = '(c) 2020 Canonical Ltd.' |
| 18 |
__license__ = 'LGPL 3+' |
| 19 |
|
| 20 |
import dbus |
| 21 |
import time |
| 22 |
|
| 23 |
from dbusmock import MOCK_IFACE |
| 24 |
|
| 25 |
BUS_NAME = 'org.freedesktop.PolicyKit1' |
| 26 |
MAIN_OBJ = '/org/freedesktop/PolicyKit1/Authority' |
| 27 |
MAIN_IFACE = 'org.freedesktop.PolicyKit1.Authority' |
| 28 |
SYSTEM_BUS = True |
| 29 |
IS_OBJECT_MANAGER = False |
| 30 |
|
| 31 |
def load(mock, parameters): |
| 32 |
mock.allow_unknown = False |
| 33 |
mock.allowed = [] |
| 34 |
mock.delay = 0 |
| 35 |
mock.simulate_hang = False |
| 36 |
mock.hanging_actions = [] |
| 37 |
mock.hanging_calls = [] |
| 38 |
|
| 39 |
mock.AddProperties(MAIN_IFACE, |
| 40 |
dbus.Dictionary({ |
| 41 |
'BackendName': 'local', |
| 42 |
'BackendVersion': '0.8.15', |
| 43 |
'BackendFeatures': dbus.UInt32(1, variant_level=1), |
| 44 |
}, signature='sv')) |
| 45 |
|
| 46 |
|
| 47 |
@dbus.service.method(MAIN_IFACE, |
| 48 |
in_signature='(sa{sv})sa{ss}us', out_signature='(bba{ss})', |
| 49 |
async_callbacks=('ok_cb', 'err_cb')) |
| 50 |
def CheckAuthorization(self, subject, action_id, details, flags, cancellation_id, |
| 51 |
ok_cb, err_cb): |
| 52 |
time.sleep(self.delay) |
| 53 |
allowed = action_id in self.allowed or self.allow_unknown |
| 54 |
ret = (allowed, False, {'test': 'test'}) |
| 55 |
|
| 56 |
if self.simulate_hang or action_id in self.hanging_actions: |
| 57 |
self.hanging_calls.append((ok_cb, ret)) |
| 58 |
else: |
| 59 |
ok_cb(ret) |
| 60 |
|
| 61 |
@dbus.service.method(MOCK_IFACE, in_signature='b', out_signature='') |
| 62 |
def AllowUnknown(self, default): |
| 63 |
'''Control whether unknown actions are allowed |
| 64 |
|
| 65 |
This controls the return value of CheckAuthorization for actions which were |
| 66 |
not explicitly allowed by SetAllowed(). |
| 67 |
''' |
| 68 |
self.allow_unknown = default |
| 69 |
|
| 70 |
@dbus.service.method(MOCK_IFACE, in_signature='d', out_signature='') |
| 71 |
def SetDelay(self, delay): |
| 72 |
'''Makes the CheckAuthorization() method to delay''' |
| 73 |
self.delay = delay |
| 74 |
|
| 75 |
@dbus.service.method(MOCK_IFACE, in_signature='b', out_signature='') |
| 76 |
def SimulateHang(self, hang): |
| 77 |
'''Makes the CheckAuthorization() method to hang''' |
| 78 |
self.simulate_hang = hang |
| 79 |
|
| 80 |
@dbus.service.method(MOCK_IFACE, in_signature='as', out_signature='') |
| 81 |
def SimulateHangActions(self, actions): |
| 82 |
'''Makes the CheckAuthorization() method to hang on such actions''' |
| 83 |
self.hanging_actions = actions |
| 84 |
|
| 85 |
@dbus.service.method(MOCK_IFACE, in_signature='', out_signature='') |
| 86 |
def ReleaseHangingCalls(self): |
| 87 |
'''Calls all the hanging callbacks''' |
| 88 |
for (cb, ret) in self.hanging_calls: |
| 89 |
cb(ret) |
| 90 |
self.hanging_calls = [] |
| 91 |
|
| 92 |
@dbus.service.method(MOCK_IFACE, in_signature='', out_signature='b') |
| 93 |
def HaveHangingCalls(self): |
| 94 |
'''Check if we've hangling calls''' |
| 95 |
return len(self.hanging_calls) |
| 96 |
|
| 97 |
@dbus.service.method(MOCK_IFACE, in_signature='as', out_signature='') |
| 98 |
def SetAllowed(self, actions): |
| 99 |
'''Set allowed actions''' |
| 100 |
|
| 101 |
self.allowed = actions |