1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
#define _GNU_SOURCE
#include "../engine/dconf-engine.h"
#include "../engine/dconf-engine-profile.h"
#include "dconf-mock.h"
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
/* Interpose to catch fopen("/etc/dconf/profile/user") */
static const gchar *filename_to_replace;
static const gchar *filename_to_replace_it_with;
FILE *
fopen (const char *filename,
const char *mode)
{
static FILE * (*real_fopen) (const char *, const char *);
if (!real_fopen)
real_fopen = dlsym (RTLD_NEXT, "fopen");
if (filename_to_replace && g_str_equal (filename, filename_to_replace))
{
/* Crash if this file was unexpectedly opened */
g_assert (filename_to_replace_it_with != NULL);
filename = filename_to_replace_it_with;
}
return (* real_fopen) (filename, mode);
}
static GThread *main_thread;
void
dconf_engine_change_notify (DConfEngine *engine,
const gchar *prefix,
const gchar * const *changes,
const gchar *tag,
gpointer user_data)
{
/* ignore */
}
static void
verify_and_free (DConfEngineSource **sources,
gint n_sources,
const gchar * const *expected_names,
gint n_expected)
{
gint i;
g_assert_cmpint (n_sources, ==, n_expected);
g_assert ((sources == NULL) == (n_sources == 0));
for (i = 0; i < n_sources; i++)
{
g_assert_cmpstr (sources[i]->name, ==, expected_names[i]);
dconf_engine_source_free (sources[i]);
}
g_free (sources);
}
static void
test_five_times (const gchar *filename,
gint n_expected,
...)
{
const gchar **expected_names;
DConfEngineSource **sources;
gint n_sources;
va_list ap;
gint i;
expected_names = g_new (const gchar *, n_expected);
va_start (ap, n_expected);
for (i = 0; i < n_expected; i++)
expected_names[i] = va_arg (ap, const gchar *);
va_end (ap);
/* first try by supplying the profile filename via the API */
g_assert (g_getenv ("DCONF_PROFILE") == NULL);
g_assert (filename_to_replace == NULL);
sources = dconf_engine_profile_open (filename, &n_sources);
verify_and_free (sources, n_sources, expected_names, n_expected);
/* next try supplying it via the environment */
g_setenv ("DCONF_PROFILE", filename, TRUE);
g_assert (filename_to_replace == NULL);
sources = dconf_engine_profile_open (NULL, &n_sources);
verify_and_free (sources, n_sources, expected_names, n_expected);
g_unsetenv ("DCONF_PROFILE");
/* next try supplying a profile name via API and intercepting fopen */
filename_to_replace = "/etc/dconf/profile/myprofile";
filename_to_replace_it_with = filename;
g_assert (g_getenv ("DCONF_PROFILE") == NULL);
sources = dconf_engine_profile_open ("myprofile", &n_sources);
verify_and_free (sources, n_sources, expected_names, n_expected);
filename_to_replace = NULL;
/* next try the same, via the environment */
g_setenv ("DCONF_PROFILE", "myprofile", TRUE);
filename_to_replace = "/etc/dconf/profile/myprofile";
filename_to_replace_it_with = filename;
sources = dconf_engine_profile_open (NULL, &n_sources);
verify_and_free (sources, n_sources, expected_names, n_expected);
g_unsetenv ("DCONF_PROFILE");
filename_to_replace = NULL;
/* next try to have dconf pick it up as the default user profile */
filename_to_replace = "/etc/dconf/profile/user";
filename_to_replace_it_with = filename;
g_assert (g_getenv ("DCONF_PROFILE") == NULL);
sources = dconf_engine_profile_open (NULL, &n_sources);
verify_and_free (sources, n_sources, expected_names, n_expected);
filename_to_replace = NULL;
filename_to_replace_it_with = NULL;
g_free (expected_names);
}
static void
test_profile_parser (void)
{
DConfEngineSource **sources;
gint n_sources;
if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
{
g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
sources = dconf_engine_profile_open (SRCDIR "/profile/this-file-does-not-exist", &n_sources);
g_assert_cmpint (n_sources, ==, 0);
g_assert (sources == NULL);
exit (0);
}
g_test_trap_assert_passed ();
g_test_trap_assert_stderr ("*WARNING*: unable to open named profile*");
if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
{
g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
sources = dconf_engine_profile_open (SRCDIR "/profile/broken-profile", &n_sources);
g_assert_cmpint (n_sources, ==, 0);
g_assert (sources == NULL);
exit (0);
}
g_test_trap_assert_passed ();
g_test_trap_assert_stderr ("*WARNING*: unknown dconf database*unknown dconf database*");
test_five_times (SRCDIR "/profile/empty-profile", 0);
test_five_times (SRCDIR "/profile/test-profile", 1, "test");
test_five_times (SRCDIR "/profile/colourful", 4,
"user",
"other",
"verylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongnameverylongname",
"nonewline");
test_five_times (SRCDIR "/profile/dos", 2, "user", "site");
test_five_times (SRCDIR "/profile/no-newline-longline", 0);
test_five_times (SRCDIR "/profile/many-sources", 10,
"user", "local", "room", "floor", "building",
"site", "region", "division", "country", "global");
/* finally, test that we get the default profile if the user profile
* file cannot be located and we do not specify another profile.
*/
filename_to_replace = "/etc/dconf/profile/user";
filename_to_replace_it_with = SRCDIR "/profile/this-file-does-not-exist";
g_assert (g_getenv ("DCONF_PROFILE") == NULL);
sources = dconf_engine_profile_open (NULL, &n_sources);
filename_to_replace = NULL;
g_assert_cmpint (n_sources, ==, 1);
g_assert_cmpstr (sources[0]->name, ==, "user");
dconf_engine_source_free (sources[0]);
g_free (sources);
dconf_mock_shm_reset ();
}
static gpointer
test_signal_threadsafety_worker (gpointer user_data)
{
gint *finished = user_data;
gint i;
for (i = 0; i < 20000; i++)
{
DConfEngine *engine;
engine = dconf_engine_new (NULL, NULL);
dconf_engine_unref (engine);
}
g_atomic_int_inc (finished);
return NULL;
}
static void
test_signal_threadsafety (void)
{
#define N_WORKERS 4
GVariant *parameters;
gint finished = 0;
gint i;
parameters = g_variant_new_parsed ("('/test/key', [''], 'tag')");
g_variant_ref_sink (parameters);
for (i = 0; i < N_WORKERS; i++)
g_thread_unref (g_thread_new ("testcase worker", test_signal_threadsafety_worker, &finished));
while (g_atomic_int_get (&finished) < N_WORKERS)
dconf_engine_handle_dbus_signal (G_BUS_TYPE_SESSION,
":1.2.3",
"/ca/desrt/dconf/Writer/user",
"Notify", parameters);
g_variant_unref (parameters);
dconf_mock_shm_reset ();
}
static void
test_user_source (void)
{
DConfEngineSource *source;
GvdbTable *table;
GvdbTable *locks;
gboolean reopened;
/* Create the source from a clean slate */
source = dconf_engine_source_new ("user-db:user");
g_assert (source != NULL);
g_assert (source->values == NULL);
g_assert (source->locks == NULL);
/* Refresh it the first time.
* This should cause it to open the shm.
* FALSE should be returned because there is no database file.
*/
reopened = dconf_engine_source_refresh (source);
g_assert (!reopened);
dconf_mock_shm_assert_log ("open user;");
/* Try to refresh it. There must be no IO at this point. */
reopened = dconf_engine_source_refresh (source);
g_assert (!reopened);
dconf_mock_shm_assert_log ("");
/* Add a real database. */
table = dconf_mock_gvdb_table_new ();
dconf_mock_gvdb_table_insert (table, "/values/int32", g_variant_new_int32 (123456), NULL);
dconf_mock_gvdb_install ("/HOME/.config/dconf/user", table);
/* Try to refresh it again.
* Because we didn't flag the change there must still be no IO.
*/
reopened = dconf_engine_source_refresh (source);
g_assert (!reopened);
g_assert (source->values == NULL);
g_assert (source->locks == NULL);
dconf_mock_shm_assert_log ("");
/* Now flag it and reopen. */
dconf_mock_shm_flag ("user");
reopened = dconf_engine_source_refresh (source);
g_assert (reopened);
g_assert (source->values != NULL);
g_assert (source->locks == NULL);
g_assert (gvdb_table_has_value (source->values, "/values/int32"));
dconf_mock_shm_assert_log ("close;open user;");
/* Do it again -- should get the same result, after some IO */
dconf_mock_shm_flag ("user");
reopened = dconf_engine_source_refresh (source);
g_assert (reopened);
g_assert (source->values != NULL);
g_assert (source->locks == NULL);
dconf_mock_shm_assert_log ("close;open user;");
/* "Delete" the gvdb and make sure dconf notices after a flag */
dconf_mock_gvdb_install ("/HOME/.config/dconf/user", NULL);
dconf_mock_shm_flag ("user");
reopened = dconf_engine_source_refresh (source);
g_assert (reopened);
g_assert (source->values == NULL);
g_assert (source->locks == NULL);
dconf_mock_shm_assert_log ("close;open user;");
/* Add a gvdb with a lock */
table = dconf_mock_gvdb_table_new ();
locks = dconf_mock_gvdb_table_new ();
dconf_mock_gvdb_table_insert (table, "/values/int32", g_variant_new_int32 (123456), NULL);
dconf_mock_gvdb_table_insert (locks, "/values/int32", g_variant_new_boolean (TRUE), NULL);
dconf_mock_gvdb_table_insert (table, ".locks", NULL, locks);
dconf_mock_gvdb_install ("/HOME/.config/dconf/user", table);
/* Reopen and check if we have the lock */
dconf_mock_shm_flag ("user");
reopened = dconf_engine_source_refresh (source);
g_assert (reopened);
g_assert (source->values != NULL);
g_assert (source->locks != NULL);
g_assert (gvdb_table_has_value (source->values, "/values/int32"));
g_assert (gvdb_table_has_value (source->locks, "/values/int32"));
dconf_mock_shm_assert_log ("close;open user;");
/* Reopen one last time */
dconf_mock_shm_flag ("user");
reopened = dconf_engine_source_refresh (source);
g_assert (reopened);
g_assert (source->values != NULL);
g_assert (source->locks != NULL);
dconf_mock_shm_assert_log ("close;open user;");
dconf_engine_source_free (source);
dconf_mock_shm_assert_log ("close;");
dconf_mock_gvdb_install ("/HOME/.config/dconf/user", NULL);
dconf_mock_shm_reset ();
}
static void
test_system_source (void)
{
DConfEngineSource *source;
GvdbTable *table;
gboolean reopened;
source = dconf_engine_source_new ("system-db:site");
g_assert (source != NULL);
/* Check to see that we get the warning about the missing file. */
if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
{
g_log_set_always_fatal (G_LOG_LEVEL_ERROR);
/* Failing to open should return FALSE from refresh */
reopened = dconf_engine_source_refresh (source);
g_assert (!reopened);
g_assert (source->values == NULL);
/* Attempt the reopen to make sure we don't get two warnings.
* We should see FALSE again since we go from NULL to NULL.
*/
reopened = dconf_engine_source_refresh (source);
g_assert (!reopened);
/* Create the file after the fact and make sure it opens properly */
table = dconf_mock_gvdb_table_new ();
dconf_mock_gvdb_install ("/etc/dconf/db/site", table);
reopened = dconf_engine_source_refresh (source);
g_assert (reopened);
g_assert (source->values != NULL);
dconf_engine_source_free (source);
exit (0);
}
g_test_trap_assert_passed ();
/* Check that we only saw the warning, but only one time. */
g_test_trap_assert_stderr ("*this gvdb does not exist; expect degraded performance*");
g_test_trap_assert_stderr_unmatched ("*degraded*degraded*");
/* Create the file before the first refresh attempt */
table = dconf_mock_gvdb_table_new ();
dconf_mock_gvdb_install ("/etc/dconf/db/site", table);
/* See that we get the database. */
reopened = dconf_engine_source_refresh (source);
g_assert (reopened);
g_assert (source->values != NULL);
/* Do a refresh, make sure there is no change. */
reopened = dconf_engine_source_refresh (source);
g_assert (!reopened);
dconf_engine_source_free (source);
}
int
main (int argc, char **argv)
{
g_setenv ("XDG_CONFIG_HOME", "/HOME/.config", TRUE);
g_unsetenv ("DCONF_PROFILE");
main_thread = g_thread_self ();
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/engine/profile-parser", test_profile_parser);
g_test_add_func ("/engine/signal-threadsafety", test_signal_threadsafety);
g_test_add_func ("/engine/sources/user", test_user_source);
g_test_add_func ("/engine/sources/system", test_system_source);
return g_test_run ();
}
|