summaryrefslogtreecommitdiff
path: root/tests/writer.c
blob: e5a1d61030e8081c019bcece93e85b6d127fb2b3 (plain)
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
/*
 * Copyright © 2018 Endless Mobile, Inc
 *
 * This library 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 2 of the licence, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Philip Withnall <withnall@endlessm.com>
 */

#include <glib.h>
#include <glib/gstdio.h>
#include <locale.h>

#include "service/dconf-generated.h"
#include "service/dconf-writer.h"

static guint n_warnings = 0;

static GLogWriterOutput
log_writer_cb (GLogLevelFlags   log_level,
               const GLogField *fields,
               gsize            n_fields,
               gpointer         user_data)
{
  if (log_level & G_LOG_LEVEL_WARNING)
    n_warnings++;

  return G_LOG_WRITER_HANDLED;
}

static void
assert_n_warnings (guint expected_n_warnings)
{
  g_assert_cmpuint (n_warnings, ==, expected_n_warnings);
  n_warnings = 0;
}

typedef struct
{
  gchar *dconf_dir;  /* (owned) */
} Fixture;

gchar *config_dir = NULL;

static void
set_up (Fixture       *fixture,
        gconstpointer  test_data)
{
  fixture->dconf_dir = g_build_filename (config_dir, "dconf", NULL);
  g_assert_cmpint (g_mkdir (fixture->dconf_dir, 0755), ==, 0);

  g_test_message ("Using dconf directory: %s", fixture->dconf_dir);
}

static void
tear_down (Fixture       *fixture,
           gconstpointer  test_data)
{
  g_assert_cmpint (g_rmdir (fixture->dconf_dir), ==, 0);
  g_clear_pointer (&fixture->dconf_dir, g_free);

  assert_n_warnings (0);
}

/* Test basic initialisation of a #DConfWriter. This is essentially a smoketest. */
static void
test_writer_basic (Fixture       *fixture,
                   gconstpointer  test_data)
{
  g_autoptr(DConfWriter) writer = NULL;

  writer = DCONF_WRITER (dconf_writer_new (DCONF_TYPE_WRITER, "some-name"));
  g_assert_nonnull (writer);

  g_assert_cmpstr (dconf_writer_get_name (writer), ==, "some-name");
}

/* Test that beginning a write operation when no database exists succeeds. Note
 * that the database will not actually be created until some changes are made
 * and the write is committed. */
static void
test_writer_begin_missing (Fixture       *fixture,
                           gconstpointer  test_data)
{
  g_autoptr(DConfWriter) writer = NULL;
  DConfWriterClass *writer_class;
  gboolean retval;
  g_autoptr(GError) local_error = NULL;
  g_autofree gchar *db_filename = g_build_filename (fixture->dconf_dir, "missing", NULL);

  /* Check the database doesn’t exist. */
  g_assert_false (g_file_test (db_filename, G_FILE_TEST_EXISTS));

  /* Create a writer. */
  writer = DCONF_WRITER (dconf_writer_new (DCONF_TYPE_WRITER, "missing"));
  g_assert_nonnull (writer);

  writer_class = DCONF_WRITER_GET_CLASS (writer);
  retval = writer_class->begin (writer, &local_error);
  g_assert_no_error (local_error);
  g_assert_true (retval);
}

/* Test that beginning a write operation when a corrupt or empty database exists
 * will take a backup of the database and then succeed. Note that a new empty
 * database will not actually be created until some changes are made and the
 * write is committed. */
typedef struct
{
  const gchar *corrupt_db_contents;
  guint n_existing_backups;
} BeginCorruptFileData;

static void
test_writer_begin_corrupt_file (Fixture       *fixture,
                                gconstpointer  test_data)
{
  const BeginCorruptFileData *data = test_data;
  g_autoptr(DConfWriter) writer = NULL;
  DConfWriterClass *writer_class;
  gboolean retval;
  g_autoptr(GError) local_error = NULL;
  g_autofree gchar *db_filename = g_build_filename (fixture->dconf_dir, "corrupt", NULL);
  g_autofree gchar *new_db_filename_backup = NULL;
  g_autofree gchar *backup_file_contents = NULL;
  gsize backup_file_contents_len = 0;
  guint i;

  /* Create a corrupt database. */
  g_file_set_contents (db_filename, data->corrupt_db_contents, -1, &local_error);
  g_assert_no_error (local_error);

  /* Create any existing backups, to test we don’t overwrite them. */
  for (i = 0; i < data->n_existing_backups; i++)
    {
      g_autofree gchar *db_filename_backup = g_strdup_printf ("%s~%u", db_filename, i);
      g_file_set_contents (db_filename_backup, "backup", -1, &local_error);
      g_assert_no_error (local_error);
    }

  new_db_filename_backup = g_strdup_printf ("%s~%u", db_filename, data->n_existing_backups);

  /* Create a writer. */
  writer = DCONF_WRITER (dconf_writer_new (DCONF_TYPE_WRITER, "corrupt"));
  g_assert_nonnull (writer);

  writer_class = DCONF_WRITER_GET_CLASS (writer);
  retval = writer_class->begin (writer, &local_error);
  g_assert_no_error (local_error);
  g_assert_true (retval);

  /* The writer should have printed a warning about the corrupt database. */
  assert_n_warnings (1);

  /* Check a backup file has been created and has the right content. */
  g_file_get_contents (new_db_filename_backup, &backup_file_contents,
                       &backup_file_contents_len, &local_error);
  g_assert_no_error (local_error);
  g_assert_cmpstr (backup_file_contents, ==, data->corrupt_db_contents);
  g_assert_cmpuint (backup_file_contents_len, ==, strlen (data->corrupt_db_contents));

  /* Clean up. */
  g_assert_cmpint (g_unlink (new_db_filename_backup), ==, 0);

  for (i = 0; i < data->n_existing_backups; i++)
    {
      g_autofree gchar *db_filename_backup = g_strdup_printf ("%s~%u", db_filename, i);
      g_assert_cmpint (g_unlink (db_filename_backup), ==, 0);
    }
}

/**
 * Test that committing a write operation when no writes have been queued
 * does not result in a database write.
 */
static void test_writer_commit_no_change (Fixture       *fixture,
                                          gconstpointer  test_data)
{
  const char *db_name = "nonexistent";
  g_autoptr(DConfWriter) writer = NULL;
  DConfWriterClass *writer_class;
  gboolean retval;
  g_autoptr(GError) local_error = NULL;
  g_autofree gchar *db_filename = g_build_filename (fixture->dconf_dir, db_name, NULL);

  /* Create a writer. */
  writer = DCONF_WRITER (dconf_writer_new (DCONF_TYPE_WRITER, db_name));
  g_assert_nonnull (writer);
  writer_class = DCONF_WRITER_GET_CLASS (writer);

  /* Check the database doesn’t exist. */
  g_assert_false (g_file_test (db_filename, G_FILE_TEST_EXISTS));

  /* Begin transaction */
  retval = writer_class->begin (writer, &local_error);
  g_assert_no_error (local_error);
  g_assert_true (retval);

  /* Commit transaction */
  retval = writer_class->commit (writer, &local_error);
  g_assert_no_error (local_error);
  g_assert_true (retval);

  /* Check the database still doesn’t exist. */
  g_assert_false (g_file_test (db_filename, G_FILE_TEST_EXISTS));

  /* End transaction */
  writer_class->end (writer);
}

/**
 * Test that committing a write operation when writes that would not change
 * the database have been queued does not result in a database write.
 */
static void test_writer_commit_empty_changes (Fixture       *fixture,
                                              gconstpointer  test_data)
{
  const char *db_name = "nonexistent";
  g_autoptr(DConfWriter) writer = NULL;
  DConfWriterClass *writer_class;
  gboolean retval;
  g_autoptr(GError) local_error = NULL;
  g_autofree gchar *db_filename = g_build_filename (fixture->dconf_dir, db_name, NULL);

  /* Create a writer. */
  writer = DCONF_WRITER (dconf_writer_new (DCONF_TYPE_WRITER, db_name));
  g_assert_nonnull (writer);
  writer_class = DCONF_WRITER_GET_CLASS (writer);

  /* Check the database doesn’t exist. */
  g_assert_false (g_file_test (db_filename, G_FILE_TEST_EXISTS));

  /* Begin transaction */
  retval = writer_class->begin (writer, &local_error);
  g_assert_no_error (local_error);
  g_assert_true (retval);

  /* Make a redundant/empty change to the database */
  DConfChangeset *changes = dconf_changeset_new();
  writer_class->change (writer, changes, NULL);
  g_assert_no_error (local_error);
  g_assert_true (retval);

  /* Commit transaction */
  retval = writer_class->commit (writer, &local_error);
  g_assert_no_error (local_error);
  g_assert_true (retval);

  /* Check the database still doesn't exist */
  g_assert_false (g_file_test (db_filename, G_FILE_TEST_EXISTS));

  /* End transaction */
  writer_class->end (writer);
}

/**
 * Test that committing a write operation when writes that would change
 * the database have been queued does result in a database write.
 */
static void test_writer_commit_real_changes (Fixture       *fixture,
                                             gconstpointer  test_data)
{
  const char *db_name = "nonexistent";
  g_autoptr(DConfWriter) writer = NULL;
  DConfWriterClass *writer_class;
  gboolean retval;
  g_autoptr(GError) local_error = NULL;
  g_autofree gchar *db_filename = g_build_filename (fixture->dconf_dir, db_name, NULL);

  /* Create a writer. */
  writer = DCONF_WRITER (dconf_writer_new (DCONF_TYPE_WRITER, db_name));
  g_assert_nonnull (writer);
  writer_class = DCONF_WRITER_GET_CLASS (writer);

  /* Check the database doesn’t exist. */
  g_assert_false (g_file_test (db_filename, G_FILE_TEST_EXISTS));

  /* Begin transaction */
  retval = writer_class->begin (writer, &local_error);
  g_assert_no_error (local_error);
  g_assert_true (retval);

  /* Make a real change to the database */
  DConfChangeset *changes = dconf_changeset_new();
  dconf_changeset_set(changes, "/key", g_variant_new ("(s)", "value"));
  writer_class->change (writer, changes, NULL);
  g_assert_no_error (local_error);
  g_assert_true (retval);

  /* Commit transaction */
  retval = writer_class->commit (writer, &local_error);
  g_assert_no_error (local_error);
  g_assert_true (retval);

  /* Check the database now exists */
  g_assert_true (g_file_test (db_filename, G_FILE_TEST_EXISTS));

  /* End transaction */
  writer_class->end (writer);

  /* Clean up. */
  g_assert_cmpint (g_unlink (db_filename), ==, 0);
}

int
main (int argc, char **argv)
{
  g_autoptr(GError) local_error = NULL;
  int retval;
  const BeginCorruptFileData empty_data = { "", 0 };
  const BeginCorruptFileData corrupt_file_data0 = {
    "secretly not a valid GVDB database 😧", 0
  };
  const BeginCorruptFileData corrupt_file_data1 = {
    "secretly not a valid GVDB database 😧", 1
  };
  const BeginCorruptFileData corrupt_file_data2 = {
    "secretly not a valid GVDB database 😧", 2
  };

  setlocale (LC_ALL, "");

  g_test_init (&argc, &argv, NULL);

  /* Set up a fake $XDG_CONFIG_HOME. We can’t do this in the fixture, as
   * g_get_user_config_dir() caches its return value. */
  config_dir = g_dir_make_tmp ("dconf-test-writer_XXXXXX", &local_error);
  g_assert_no_error (local_error);
  g_assert_true (g_setenv ("XDG_CONFIG_HOME", config_dir, TRUE));
  g_test_message ("Using config directory: %s", config_dir);

  /* Log handling so we don’t abort on the first g_warning(). */
  g_log_set_writer_func (log_writer_cb, NULL, NULL);

  g_test_add ("/writer/basic", Fixture, NULL, set_up,
              test_writer_basic, tear_down);
  g_test_add ("/writer/begin/missing", Fixture, NULL, set_up,
              test_writer_begin_missing, tear_down);
  g_test_add ("/writer/begin/empty", Fixture, &empty_data, set_up,
              test_writer_begin_corrupt_file, tear_down);
  g_test_add ("/writer/begin/corrupt-file/0", Fixture, &corrupt_file_data0, set_up,
              test_writer_begin_corrupt_file, tear_down);
  g_test_add ("/writer/begin/corrupt-file/1", Fixture, &corrupt_file_data1, set_up,
              test_writer_begin_corrupt_file, tear_down);
  g_test_add ("/writer/begin/corrupt-file/2", Fixture, &corrupt_file_data2, set_up,
              test_writer_begin_corrupt_file, tear_down);
  g_test_add ("/writer/commit/redundant_change/0", Fixture, NULL, set_up,
              test_writer_commit_no_change, tear_down);
  g_test_add ("/writer/commit/redundant_change/1", Fixture, NULL, set_up,
              test_writer_commit_empty_changes, tear_down);
  g_test_add ("/writer/commit/redundant_change/2", Fixture, NULL, set_up,
              test_writer_commit_real_changes, tear_down);

  retval = g_test_run ();

  /* Clean up the config dir. */
  g_unsetenv ("XDG_CONFIG_HOME");
  g_assert_cmpint (g_rmdir (config_dir), ==, 0);
  g_clear_pointer (&config_dir, g_free);

  return retval;
}