mirror of
https://gitlab.com/mishakmak/pam-fprint-grosshack.git
synced 2026-04-09 04:13:33 +02:00
tests: Add standalone PolkitD implementation
We need to be able to hack this to be an async daemon to perform some multi-thread tests, so replacing default implementation with a simple one that for now just does same of default
This commit is contained in:
committed by
Benjamin Berg
parent
0904c8a527
commit
5611317c72
74
tests/dbusmock/polkitd.py
Normal file
74
tests/dbusmock/polkitd.py
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
'''polkit mock template
|
||||||
|
|
||||||
|
This creates the basic methods and properties of the
|
||||||
|
org.freedesktop.PolicyKit1.Authority object, so that we can use it async
|
||||||
|
'''
|
||||||
|
|
||||||
|
# This program 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 3 of the License, or (at your option) any
|
||||||
|
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
|
||||||
|
# of the license.
|
||||||
|
|
||||||
|
__author__ = 'Marco Trevisan'
|
||||||
|
__email__ = 'marco.trevisan@canonical.com'
|
||||||
|
__copyright__ = '(c) 2020 Canonical Ltd.'
|
||||||
|
__license__ = 'LGPL 3+'
|
||||||
|
|
||||||
|
import dbus
|
||||||
|
|
||||||
|
from dbusmock import MOCK_IFACE, mockobject
|
||||||
|
|
||||||
|
BUS_NAME = 'org.freedesktop.PolicyKit1'
|
||||||
|
MAIN_OBJ = '/org/freedesktop/PolicyKit1/Authority'
|
||||||
|
MAIN_IFACE = 'org.freedesktop.PolicyKit1.Authority'
|
||||||
|
SYSTEM_BUS = True
|
||||||
|
IS_OBJECT_MANAGER = False
|
||||||
|
|
||||||
|
def load(mock, parameters):
|
||||||
|
polkitd = mockobject.objects[MAIN_OBJ]
|
||||||
|
# default state
|
||||||
|
polkitd.allow_unknown = False
|
||||||
|
polkitd.allowed = []
|
||||||
|
|
||||||
|
mock.AddProperties(MAIN_IFACE,
|
||||||
|
dbus.Dictionary({
|
||||||
|
'BackendName': 'local',
|
||||||
|
'BackendVersion': '0.8.15',
|
||||||
|
'BackendFeatures': dbus.UInt32(1, variant_level=1),
|
||||||
|
}, signature='sv'))
|
||||||
|
|
||||||
|
|
||||||
|
@dbus.service.method(MAIN_IFACE,
|
||||||
|
in_signature='(sa{sv})sa{ss}us', out_signature='(bba{ss})')
|
||||||
|
def CheckAuthorization(self, subject, action_id, details, flags, cancellation_id):
|
||||||
|
return (action_id in self.allowed or self.allow_unknown, False, {'test': 'test'})
|
||||||
|
|
||||||
|
|
||||||
|
@dbus.service.method(MOCK_IFACE, in_signature='b', out_signature='')
|
||||||
|
def AllowUnknown(self, default):
|
||||||
|
'''Control whether unknown actions are allowed
|
||||||
|
|
||||||
|
This controls the return value of CheckAuthorization for actions which were
|
||||||
|
not explicitly allowed by SetAllowed().
|
||||||
|
'''
|
||||||
|
self.allow_unknown = default
|
||||||
|
|
||||||
|
|
||||||
|
@dbus.service.method(MOCK_IFACE, in_signature='as', out_signature='')
|
||||||
|
def SetAllowed(self, actions):
|
||||||
|
'''Set allowed actions'''
|
||||||
|
|
||||||
|
self.allowed = actions
|
||||||
|
|
||||||
|
@dbus.service.method(MAIN_IFACE,
|
||||||
|
in_signature='', out_signature='o')
|
||||||
|
def GetDefaultDevice(self):
|
||||||
|
devices = self.GetDevices()
|
||||||
|
if len(devices) < 1:
|
||||||
|
raise dbus.exceptions.DBusException(
|
||||||
|
'No devices available',
|
||||||
|
name='net.reactivated.Fprint.Error.NoSuchDevice')
|
||||||
|
return devices[0]
|
||||||
@ -136,6 +136,7 @@ class FPrintdTest(dbusmock.DBusTestCase):
|
|||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
super().setUpClass()
|
super().setUpClass()
|
||||||
fprintd = None
|
fprintd = None
|
||||||
|
cls._polkitd = None
|
||||||
|
|
||||||
if 'FPRINT_BUILD_DIR' in os.environ:
|
if 'FPRINT_BUILD_DIR' in os.environ:
|
||||||
print('Testing local build')
|
print('Testing local build')
|
||||||
@ -256,9 +257,19 @@ class FPrintdTest(dbusmock.DBusTestCase):
|
|||||||
self.daemon = None
|
self.daemon = None
|
||||||
|
|
||||||
def polkitd_start(self):
|
def polkitd_start(self):
|
||||||
|
if self._polkitd:
|
||||||
|
return
|
||||||
|
|
||||||
|
if 'POLKITD_MOCK_PATH' in os.environ:
|
||||||
|
polkitd_template = os.path.join(os.getenv('POLKITD_MOCK_PATH'), 'polkitd.py')
|
||||||
|
else:
|
||||||
|
polkitd_template = os.path.join(os.path.dirname(__file__), 'dbusmock/polkitd.py')
|
||||||
|
print ('Using template from %s' % polkitd_template)
|
||||||
|
|
||||||
self._polkitd, self._polkitd_obj = self.spawn_server_template(
|
self._polkitd, self._polkitd_obj = self.spawn_server_template(
|
||||||
'polkitd', {}, stdout=DEVNULL)
|
polkitd_template, {}, stdout=subprocess.PIPE)
|
||||||
self.addCleanup(self.polkitd_stop)
|
|
||||||
|
return self._polkitd
|
||||||
|
|
||||||
def polkitd_stop(self):
|
def polkitd_stop(self):
|
||||||
if self._polkitd is None:
|
if self._polkitd is None:
|
||||||
@ -365,6 +376,7 @@ class FPrintdVirtualDeviceBaseTest(FPrintdTest):
|
|||||||
self.g_signal_id = self.device.connect('g-signal', signal_cb)
|
self.g_signal_id = self.device.connect('g-signal', signal_cb)
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
|
self.polkitd_stop()
|
||||||
self.device.disconnect(self.g_signal_id)
|
self.device.disconnect(self.g_signal_id)
|
||||||
self.device = None
|
self.device = None
|
||||||
self.manager = None
|
self.manager = None
|
||||||
|
|||||||
Reference in New Issue
Block a user