From 0d64a3482cb9b28b61bd724ec8288d222cfaa2d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Mon, 10 Dec 2018 00:00:00 +0000 Subject: tests: Key paths can be locked in system databases --- tests/test-dconf.py | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/tests/test-dconf.py b/tests/test-dconf.py index 40a8737..b5b511d 100755 --- a/tests/test-dconf.py +++ b/tests/test-dconf.py @@ -73,6 +73,10 @@ def dconf_write(key, value): def dconf_list(key): return dconf('list', key).stdout.splitlines() +def dconf_locks(key, **kwargs): + lines = dconf('list-locks', key, **kwargs).stdout.splitlines() + lines.sort() + return lines def dconf_complete(suffix, prefix): lines = dconf('_complete', suffix, prefix).stdout.splitlines() @@ -661,6 +665,85 @@ class DBusTest(unittest.TestCase): self.assertFalse(os.path.exists(path)) self.assertRegex(cm.exception.stderr, name) + def test_locks(self): + """Key paths can be locked in system databases. + + - Update configures locks based on files found in "locks" subdirectory. + - Locks can be listed with list-locks command. + - Locks are enforced during write. + """ + + db = os.path.join(self.temporary_dir.name, 'db') + profile = os.path.join(self.temporary_dir.name, 'profile') + site = os.path.join(db, 'site') + site_d = os.path.join(db, 'site.d') + site_locks = os.path.join(db, site_d, 'locks') + + os.makedirs(site_locks) + + # For meaningful test of locks we need two sources, first of which + # should be writable. We will use user-db and file-db. + with open(profile, 'w') as file: + file.write(dedent('''\ + user-db:user + file-db:{} + '''.format(site))) + + # Environment to use for all dconf client invocations. + env = dict(os.environ) + env['DCONF_PROFILE'] = profile + + # Default settings + with open(os.path.join(site_d, '10-site-defaults'), 'w') as file: + file.write(dedent('''\ + # Some useful default settings for our site + [system/proxy/http] + host='172.16.0.1' + enabled=true + + [org/gnome/desktop/background] + picture-uri='file:///usr/local/rupert-corp/company-wallpaper.jpeg' + ''')) + + # Lock proxy settings. + with open(os.path.join(site_locks, '10-proxy-lock'), 'w') as file: + file.write(dedent('''\ + # Prevent changes to proxy + /system/proxy/http/host + /system/proxy/http/enabled + /system/proxy/ftp/host + /system/proxy/ftp/enabled + ''')) + + # Compile site configuration. + dconf('update', db) + + # Test list-locks: + self.assertEqual(['/system/proxy/ftp/enabled', + '/system/proxy/ftp/host', + '/system/proxy/http/enabled', + '/system/proxy/http/host'], + dconf_locks('/', env=env)) + + self.assertEqual(['/system/proxy/http/enabled', + '/system/proxy/http/host'], + dconf_locks('/system/proxy/http/', env=env)) + + self.assertEqual([], + dconf_locks('/org/gnome/', env=env)) + + # Changing unlocked defaults is fine. + dconf('write', '/org/gnome/desktop/background/picture-uri', + '"file:///usr/share/backgrounds/gnome/ColdWarm.jpg"', + env=env) + + # It is an error to change locked keys. + with self.assertRaises(subprocess.CalledProcessError) as cm: + dconf('write', '/system/proxy/http/enabled', 'false', + env=env, stderr=subprocess.PIPE) + self.assertRegex(cm.exception.stderr, 'non-writable keys') + + if __name__ == '__main__': # Make sure we don't pick up mandatory profile. mandatory_profile = '/run/dconf/user/{}'.format(os.getuid()) -- cgit v1.2.1 From 6ef3d0209dd6e78c1247c1dded0948e0cadf2ffb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Mon, 10 Dec 2018 00:00:00 +0000 Subject: tests: Sanitize additional environment variables used by dconf --- tests/test-dconf.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test-dconf.py b/tests/test-dconf.py index b5b511d..88f6769 100755 --- a/tests/test-dconf.py +++ b/tests/test-dconf.py @@ -749,8 +749,12 @@ if __name__ == '__main__': mandatory_profile = '/run/dconf/user/{}'.format(os.getuid()) assert not os.path.isfile(mandatory_profile) - # Avoid profile sourced from environment + # Avoid profile sourced from environment or system data dirs. os.environ.pop('DCONF_PROFILE', None) + os.environ.pop('XDG_DATA_DIRS', None) + # Avoid interfering with external message buses. + os.environ.pop('DBUS_SYSTEM_BUS_ADDRESS', None) + os.environ.pop('DBUS_SESSION_BUS_ADDRESS', None) if len(sys.argv) < 3: message = 'Usage: {} path-to-dconf path-to-dconf-service'.format( -- cgit v1.2.1