3 From 221c26685354dea2b2732df94404e8e5e77a1591 Mon Sep 17 00:00:00 2001
4 From: Philip Withnall <pwithnall@endlessos.org>
5 Date: Wed, 10 Feb 2021 21:21:36 +0000
6 Subject: [PATCH 3/3] tests: Add tests for key name handling in the keyfile
9 This tests the two recent commits.
11 Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
13 gio/tests/gsettings.c | 171 +++++++++++++++++++++++++++++++++++++++++-
14 1 file changed, 170 insertions(+), 1 deletion(-)
16 diff --git a/gio/tests/gsettings.c b/gio/tests/gsettings.c
17 index baadca8f5..afe594a23 100644
18 --- a/gio/tests/gsettings.c
19 +++ b/gio/tests/gsettings.c
25 @@ -1740,6 +1741,14 @@ key_changed_cb (GSettings *settings, const gchar *key, gpointer data)
32 + const gchar *root_group;
33 + const gchar *keyfile_group;
34 + const gchar *root_path;
38 * Test that using a keyfile works
40 @@ -1834,7 +1843,11 @@ test_keyfile (Fixture *fixture,
43 g_settings_set (settings, "farewell", "s", "cheerio");
46 + /* Check that empty keys/groups are not allowed. */
47 + g_assert_false (g_settings_is_writable (settings, ""));
48 + g_assert_false (g_settings_is_writable (settings, "/"));
50 /* When executing as root, changing the mode of the keyfile will have
51 * no effect on the writability of the settings.
53 @@ -1866,6 +1879,149 @@ test_keyfile (Fixture *fixture,
54 g_free (keyfile_path);
58 + * Test that using a keyfile works with a schema with no path set.
61 +test_keyfile_no_path (Fixture *fixture,
62 + gconstpointer user_data)
64 + const KeyfileTestData *test_data = user_data;
65 + GSettingsBackend *kf_backend;
66 + GSettings *settings;
70 + GError *error = NULL;
71 + gchar *keyfile_path = NULL, *store_path = NULL;
73 + keyfile_path = g_build_filename (fixture->tmp_dir, "keyfile", NULL);
74 + store_path = g_build_filename (keyfile_path, "gsettings.store", NULL);
75 + kf_backend = g_keyfile_settings_backend_new (store_path, test_data->root_path, test_data->root_group);
76 + settings = g_settings_new_with_backend_and_path ("org.gtk.test.no-path", kf_backend, test_data->path);
77 + g_object_unref (kf_backend);
79 + g_settings_reset (settings, "test-boolean");
80 + g_assert_true (g_settings_get_boolean (settings, "test-boolean"));
82 + writable = g_settings_is_writable (settings, "test-boolean");
83 + g_assert_true (writable);
84 + g_settings_set (settings, "test-boolean", "b", FALSE);
86 + g_assert_false (g_settings_get_boolean (settings, "test-boolean"));
88 + g_settings_delay (settings);
89 + g_settings_set (settings, "test-boolean", "b", TRUE);
90 + g_settings_apply (settings);
92 + keyfile = g_key_file_new ();
93 + g_assert_true (g_key_file_load_from_file (keyfile, store_path, 0, NULL));
95 + g_assert_true (g_key_file_get_boolean (keyfile, test_data->keyfile_group, "test-boolean", NULL));
97 + g_key_file_free (keyfile);
99 + g_settings_reset (settings, "test-boolean");
100 + g_settings_apply (settings);
101 + keyfile = g_key_file_new ();
102 + g_assert_true (g_key_file_load_from_file (keyfile, store_path, 0, NULL));
104 + g_assert_false (g_key_file_get_string (keyfile, test_data->keyfile_group, "test-boolean", &error));
105 + g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND);
106 + g_clear_error (&error);
108 + /* Check that empty keys/groups are not allowed. */
109 + g_assert_false (g_settings_is_writable (settings, ""));
110 + g_assert_false (g_settings_is_writable (settings, "/"));
112 + /* Keys which ghost the root group name are not allowed. This can only be
113 + * tested when the path is `/` as otherwise it acts as a prefix and prevents
115 + if (g_str_equal (test_data->path, "/"))
117 + key = g_strdup_printf ("%s/%s", test_data->root_group, "");
118 + g_assert_false (g_settings_is_writable (settings, key));
121 + key = g_strdup_printf ("%s/%s", test_data->root_group, "/");
122 + g_assert_false (g_settings_is_writable (settings, key));
125 + key = g_strdup_printf ("%s/%s", test_data->root_group, "test-boolean");
126 + g_assert_false (g_settings_is_writable (settings, key));
130 + g_key_file_free (keyfile);
131 + g_object_unref (settings);
133 + /* Clean up the temporary directory. */
134 + g_assert_cmpint (g_chmod (keyfile_path, 0777) == 0 ? 0 : errno, ==, 0);
135 + g_assert_cmpint (g_remove (store_path) == 0 ? 0 : errno, ==, 0);
136 + g_assert_cmpint (g_rmdir (keyfile_path) == 0 ? 0 : errno, ==, 0);
137 + g_free (store_path);
138 + g_free (keyfile_path);
142 + * Test that a keyfile rejects writes to keys outside its root path.
145 +test_keyfile_outside_root_path (Fixture *fixture,
146 + gconstpointer user_data)
148 + GSettingsBackend *kf_backend;
149 + GSettings *settings;
150 + gchar *keyfile_path = NULL, *store_path = NULL;
152 + keyfile_path = g_build_filename (fixture->tmp_dir, "keyfile", NULL);
153 + store_path = g_build_filename (keyfile_path, "gsettings.store", NULL);
154 + kf_backend = g_keyfile_settings_backend_new (store_path, "/tests/basic-types/", "root");
155 + settings = g_settings_new_with_backend_and_path ("org.gtk.test.no-path", kf_backend, "/tests/");
156 + g_object_unref (kf_backend);
158 + g_assert_false (g_settings_is_writable (settings, "test-boolean"));
160 + g_object_unref (settings);
162 + /* Clean up the temporary directory. The keyfile probably doesn’t exist, so
163 + * don’t error on failure. */
164 + g_remove (store_path);
165 + g_assert_cmpint (g_rmdir (keyfile_path) == 0 ? 0 : errno, ==, 0);
166 + g_free (store_path);
167 + g_free (keyfile_path);
171 + * Test that a keyfile rejects writes to keys in the root if no root group is set.
174 +test_keyfile_no_root_group (Fixture *fixture,
175 + gconstpointer user_data)
177 + GSettingsBackend *kf_backend;
178 + GSettings *settings;
179 + gchar *keyfile_path = NULL, *store_path = NULL;
181 + keyfile_path = g_build_filename (fixture->tmp_dir, "keyfile", NULL);
182 + store_path = g_build_filename (keyfile_path, "gsettings.store", NULL);
183 + kf_backend = g_keyfile_settings_backend_new (store_path, "/", NULL);
184 + settings = g_settings_new_with_backend_and_path ("org.gtk.test.no-path", kf_backend, "/");
185 + g_object_unref (kf_backend);
187 + g_assert_false (g_settings_is_writable (settings, "test-boolean"));
188 + g_assert_true (g_settings_is_writable (settings, "child/test-boolean"));
190 + g_object_unref (settings);
192 + /* Clean up the temporary directory. The keyfile probably doesn’t exist, so
193 + * don’t error on failure. */
194 + g_remove (store_path);
195 + g_assert_cmpint (g_rmdir (keyfile_path) == 0 ? 0 : errno, ==, 0);
196 + g_free (store_path);
197 + g_free (keyfile_path);
200 /* Test that getting child schemas works
203 @@ -2844,6 +3000,14 @@ main (int argc, char *argv[])
204 gchar *override_text;
207 + const KeyfileTestData keyfile_test_data_explicit_path = { "/tests/", "root", "tests", "/" };
208 + const KeyfileTestData keyfile_test_data_empty_path = { "/", "root", "root", "/" };
209 + const KeyfileTestData keyfile_test_data_long_path = {
210 + "/tests/path/is/very/long/and/this/makes/some/comparisons/take/a/different/branch/",
212 + "tests/path/is/very/long/and/this/makes/some/comparisons/take/a/different/branch",
216 /* Meson build sets this */
217 #ifdef TEST_LOCALE_PATH
218 @@ -2967,6 +3131,11 @@ main (int argc, char *argv[])
221 g_test_add ("/gsettings/keyfile", Fixture, NULL, setup, test_keyfile, teardown);
222 + g_test_add ("/gsettings/keyfile/explicit-path", Fixture, &keyfile_test_data_explicit_path, setup, test_keyfile_no_path, teardown);
223 + g_test_add ("/gsettings/keyfile/empty-path", Fixture, &keyfile_test_data_empty_path, setup, test_keyfile_no_path, teardown);
224 + g_test_add ("/gsettings/keyfile/long-path", Fixture, &keyfile_test_data_long_path, setup, test_keyfile_no_path, teardown);
225 + g_test_add ("/gsettings/keyfile/outside-root-path", Fixture, NULL, setup, test_keyfile_outside_root_path, teardown);
226 + g_test_add ("/gsettings/keyfile/no-root-group", Fixture, NULL, setup, test_keyfile_no_root_group, teardown);
227 g_test_add_func ("/gsettings/child-schema", test_child_schema);
228 g_test_add_func ("/gsettings/strinfo", test_strinfo);
229 g_test_add_func ("/gsettings/enums", test_enums);