2020-05-04 13:22:35 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import dbus
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import fcntl
|
|
|
|
import subprocess
|
2021-12-10 17:39:50 -05:00
|
|
|
import getpass
|
2021-12-11 05:41:53 -05:00
|
|
|
import argparse
|
2020-05-04 13:22:35 -04:00
|
|
|
from collections import OrderedDict
|
|
|
|
from dbusmock import DBusTestCase
|
|
|
|
from dbus.mainloop.glib import DBusGMainLoop
|
|
|
|
|
|
|
|
DBusGMainLoop(set_as_default=True)
|
|
|
|
|
|
|
|
|
|
|
|
def get_templates_dir():
|
|
|
|
return os.path.join(os.path.dirname(__file__), 'dbusmock-templates')
|
|
|
|
|
|
|
|
def get_template_path(template_name):
|
|
|
|
return os.path.join(get_templates_dir(), template_name + '.py')
|
|
|
|
|
2021-11-30 02:15:26 -05:00
|
|
|
def get_subprocess_stdout():
|
|
|
|
if os.getenv('META_DBUS_RUNNER_VERBOSE') == '1':
|
|
|
|
return sys.stderr
|
|
|
|
else:
|
|
|
|
return subprocess.DEVNULL;
|
|
|
|
|
2020-05-04 13:22:35 -04:00
|
|
|
|
|
|
|
class MutterDBusTestCase(DBusTestCase):
|
|
|
|
@classmethod
|
2021-12-11 05:41:53 -05:00
|
|
|
def setUpClass(klass, enable_kvm):
|
2020-05-04 13:22:35 -04:00
|
|
|
klass.mocks = OrderedDict()
|
|
|
|
|
2021-12-10 17:33:50 -05:00
|
|
|
print('Starting D-Bus daemons (session & system)...', file=sys.stderr)
|
2020-05-04 13:22:35 -04:00
|
|
|
DBusTestCase.setUpClass()
|
|
|
|
klass.start_session_bus()
|
|
|
|
klass.start_system_bus()
|
|
|
|
|
2021-12-10 17:33:50 -05:00
|
|
|
print('Starting mocked services...', file=sys.stderr)
|
2020-05-04 13:22:35 -04:00
|
|
|
(klass.mocks_manager, klass.mock_obj) = klass.start_from_local_template(
|
|
|
|
'meta-mocks-manager', {'templates-dir': get_templates_dir()})
|
|
|
|
|
2021-08-26 09:07:47 -04:00
|
|
|
klass.start_from_local_template('localed')
|
2020-05-04 13:22:35 -04:00
|
|
|
|
|
|
|
klass.system_bus_con = klass.get_dbus(system_bus=True)
|
|
|
|
klass.session_bus_con = klass.get_dbus(system_bus=False)
|
|
|
|
|
2021-12-11 05:41:53 -05:00
|
|
|
klass.init_logind(enable_kvm)
|
2021-12-10 17:39:50 -05:00
|
|
|
|
2020-05-04 13:22:35 -04:00
|
|
|
if klass.session_bus_con.name_has_owner('org.gnome.Mutter.DisplayConfig'):
|
|
|
|
raise Exception(
|
|
|
|
'org.gnome.Mutter.DisplayConfig already has owner on the session bus, bailing')
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(klass):
|
|
|
|
klass.mock_obj.Cleanup()
|
|
|
|
|
|
|
|
for (mock_server, mock_obj) in reversed(klass.mocks.values()):
|
|
|
|
mock_server.terminate()
|
|
|
|
mock_server.wait()
|
|
|
|
|
|
|
|
DBusTestCase.tearDownClass()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def start_from_template(klass, template, params={}):
|
|
|
|
mock_server, mock_obj = \
|
|
|
|
klass.spawn_server_template(template,
|
|
|
|
params,
|
2021-11-30 02:15:26 -05:00
|
|
|
get_subprocess_stdout())
|
2020-05-04 13:22:35 -04:00
|
|
|
|
|
|
|
mocks = (mock_server, mock_obj)
|
|
|
|
assert klass.mocks.setdefault(template, mocks) == mocks
|
|
|
|
return mocks
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def start_from_local_template(klass, template_file_name, params={}):
|
|
|
|
template = get_template_path(template_file_name)
|
|
|
|
return klass.start_from_template(template, params)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def start_from_template_managed(klass, template):
|
|
|
|
klass.mock_obj.StartFromTemplate(template)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def start_from_local_template_managed(klass, template_file_name):
|
|
|
|
template = get_template_path(template_file_name)
|
|
|
|
klass.mock_obj.StartFromLocalTemplate(template)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def start_from_class(klass, mock_class, params={}):
|
|
|
|
mock_server = \
|
|
|
|
klass.spawn_server(mock_class.BUS_NAME,
|
|
|
|
mock_class.MAIN_OBJ,
|
|
|
|
mock_class.MAIN_IFACE,
|
|
|
|
mock_class.SYSTEM_BUS,
|
2021-11-30 02:15:26 -05:00
|
|
|
stdout=get_subprocess_stdout())
|
2020-05-04 13:22:35 -04:00
|
|
|
|
|
|
|
bus = klass.get_dbus(system_bus=mock_class.SYSTEM_BUS)
|
|
|
|
mock_obj = bus.get_object(mock_class.BUS_NAME, mock_class.MAIN_OBJ)
|
|
|
|
mock_class.load(mock_obj, params)
|
|
|
|
|
|
|
|
mocks = (mock_server, mock_obj)
|
|
|
|
assert klass.mocks.setdefault(mock_class, mocks) == mocks
|
|
|
|
return mocks
|
|
|
|
|
2021-12-10 17:39:50 -05:00
|
|
|
@classmethod
|
2021-12-11 05:41:53 -05:00
|
|
|
def init_logind_kvm(klass, session_path):
|
|
|
|
session_obj = klass.system_bus_con.get_object('org.freedesktop.login1', session_path)
|
|
|
|
session_obj.AddMethod('org.freedesktop.login1.Session',
|
|
|
|
'TakeDevice',
|
|
|
|
'uu', 'hb',
|
|
|
|
'''
|
|
|
|
import re
|
|
|
|
|
|
|
|
major = args[0]
|
|
|
|
minor = args[1]
|
|
|
|
|
|
|
|
sysfs_uevent_path = '/sys/dev/char/{}:{}/uevent'.format(major, minor)
|
|
|
|
sysfs_uevent = open(sysfs_uevent_path, 'r')
|
|
|
|
devname = None
|
|
|
|
for line in sysfs_uevent.readlines():
|
|
|
|
match = re.match('DEVNAME=(.*)', line)
|
|
|
|
if match:
|
|
|
|
devname = match[1]
|
|
|
|
break
|
|
|
|
sysfs_uevent.close()
|
|
|
|
if not devname:
|
|
|
|
raise dbus.exceptions.DBusException(f'Device file {major}:{minor} doesn\\\'t exist',
|
|
|
|
major=major, minor=minor)
|
|
|
|
fd = os.open('/dev/' + devname, os.O_RDWR | os.O_CLOEXEC)
|
|
|
|
unix_fd = dbus.types.UnixFd(fd)
|
|
|
|
os.close(fd)
|
|
|
|
ret = (unix_fd, False)
|
|
|
|
''')
|
|
|
|
session_obj.AddMethods('org.freedesktop.login1.Session', [
|
|
|
|
('ReleaseDevice', 'uu', '', ''),
|
|
|
|
('TakeControl', 'b', '', ''),
|
|
|
|
])
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def init_logind(klass, enable_kvm):
|
|
|
|
logind = klass.start_from_template('logind')
|
|
|
|
|
2021-12-10 17:39:50 -05:00
|
|
|
[p_mock, obj] = logind
|
2021-12-11 05:41:53 -05:00
|
|
|
|
2021-12-10 17:39:50 -05:00
|
|
|
mock_iface = 'org.freedesktop.DBus.Mock'
|
|
|
|
obj.AddSeat('seat0', dbus_interface=mock_iface)
|
2021-12-11 05:41:53 -05:00
|
|
|
session_path = obj.AddSession('dummy', 'seat0',
|
|
|
|
dbus.types.UInt32(os.getuid()),
|
|
|
|
getpass.getuser(),
|
|
|
|
True,
|
|
|
|
dbus_interface=mock_iface)
|
|
|
|
|
|
|
|
if enable_kvm:
|
|
|
|
klass.init_logind_kvm(session_path)
|
2021-12-10 17:39:50 -05:00
|
|
|
|
2020-05-04 13:22:35 -04:00
|
|
|
def wrap_call(self, args):
|
|
|
|
env = {}
|
|
|
|
env.update(os.environ)
|
|
|
|
env['NO_AT_BRIDGE'] = '1'
|
|
|
|
env['GSETTINGS_BACKEND'] = 'memory'
|
|
|
|
|
|
|
|
wrapper = env.get('META_DBUS_RUNNER_WRAPPER')
|
|
|
|
if wrapper == 'gdb':
|
|
|
|
args = ['gdb', '-ex', 'r', '-ex', 'bt full', '--args'] + args
|
|
|
|
elif wrapper:
|
|
|
|
args = wrapper.split(' ') + args
|
|
|
|
|
|
|
|
p = subprocess.Popen(args, env=env)
|
2022-08-05 06:16:56 -04:00
|
|
|
return p.wait()
|
2020-05-04 13:22:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-12-11 05:41:53 -05:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('--kvm', action='store_true', default=False)
|
|
|
|
(args, rest) = parser.parse_known_args(sys.argv)
|
|
|
|
|
|
|
|
MutterDBusTestCase.setUpClass(args.kvm)
|
2020-05-04 13:22:35 -04:00
|
|
|
test_case = MutterDBusTestCase()
|
2021-12-11 05:41:53 -05:00
|
|
|
test_case.assertGreater(len(rest), 1)
|
2022-08-05 06:16:56 -04:00
|
|
|
result = 1
|
2021-09-20 07:18:25 -04:00
|
|
|
try:
|
2021-12-10 17:33:50 -05:00
|
|
|
print('Running test case...', file=sys.stderr)
|
2022-08-05 06:16:56 -04:00
|
|
|
result = test_case.wrap_call(rest[1:])
|
2021-09-20 07:18:25 -04:00
|
|
|
finally:
|
|
|
|
MutterDBusTestCase.tearDownClass()
|
2022-08-05 06:16:56 -04:00
|
|
|
sys.exit(result)
|