File: test-journal.c

package info (click to toggle)
bolt 0.7-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,396 kB
  • sloc: ansic: 21,784; python: 1,574; xml: 426; sh: 25; makefile: 13
file content (484 lines) | stat: -rw-r--r-- 12,532 bytes parent folder | download
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
 * Copyright © 2018 Red Hat, Inc
 *
 * This program 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.1 of the License, 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/>.
 *
 * Authors:
 *       Christian J. Kellner <christian@kellner.me>
 */

#include "config.h"

#include "bolt-journal.h"

#include "bolt-fs.h"

#include "bolt-daemon-resource.h"

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

#include <fcntl.h>
#include <locale.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>


typedef struct
{
  char  *path;
  GFile *root;
} TestJournal;


static void
test_journal_setup (TestJournal *tt, gconstpointer user_data)
{
  g_autoptr(GError) error = NULL;

  tt->path = g_dir_make_tmp ("bolt.journal.XXXXXX",
                             &error);

  if (tt->path == NULL)
    {
      g_critical ("Could not create tmp dir: %s",
                  error->message);
      return;
    }

  g_debug ("journal test path at: %s", tt->path);

  tt->root = g_file_new_for_path (tt->path);
}


static void
test_journal_tear_down (TestJournal *tt, gconstpointer user_data)
{
  g_autoptr(GError) error = NULL;
  gboolean ok;

  ok = bolt_fs_cleanup_dir (tt->path, &error);

  if (!ok)
    g_warning ("Could not clean up dir: %s", error->message);

  g_clear_object (&tt->root);
  g_clear_pointer (&tt->path, g_free);
}

GLogWriterOutput
static
nonfatal_logger (GLogLevelFlags   log_level,
                 const GLogField *fields,
                 gsize            n_fields,
                 gpointer         user_data)
{
  return g_log_writer_standard_streams (log_level, fields, n_fields, user_data);
}

static void
test_journal_object (TestJournal *tt, gconstpointer user_data)
{
  g_autoptr(BoltJournal) j = NULL;
  g_autoptr(GError) err = NULL;
  g_autoptr(GFile) root = NULL;
  g_autofree char *name = NULL;
  gboolean fresh;

  if (g_test_subprocess ())
    {
      g_autofree char *path = NULL;
      int r;

      /* we fiddle with the writer, and also want to check
       * stderr, so the easy way is to do from a subprocess */
      g_log_set_writer_func (nonfatal_logger, NULL, NULL);

      /* name but no root */
      j = g_initable_new (BOLT_TYPE_JOURNAL,
                          NULL, &err,
                          "name", "test",
                          NULL);

      g_assert_error (err, BOLT_ERROR, BOLT_ERROR_FAILED);
      g_assert_null (j);
      g_clear_pointer (&err, g_error_free);


      /* root but no name */
      j = g_initable_new (BOLT_TYPE_JOURNAL,
                          NULL, &err,
                          "root", tt->root,
                          NULL);

      g_assert_error (err, BOLT_ERROR, BOLT_ERROR_FAILED);
      g_assert_null (j);
      g_clear_pointer (&err, g_error_free);

      path = g_build_filename (tt->path, "nobody", NULL);
      r = g_mkdir (path, 0755);
      g_assert_cmpint (r, >, -1);

      /* root, name, but name is an existing dir */
      j = g_initable_new (BOLT_TYPE_JOURNAL,
                          NULL, &err,
                          "root", tt->root,
                          "name", "nobody",
                          NULL);

      g_assert_error (err, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
      g_assert_null (j);
      g_clear_pointer (&err, g_error_free);

      return;
    }

  g_test_trap_subprocess (NULL, 0, 0);
  g_test_trap_assert_passed ();
  g_test_trap_assert_stderr ("*invalid*");

  j = bolt_journal_new (tt->root, "test", &err);

  g_assert_no_error (err);
  g_assert_nonnull (j);

  g_object_get (j,
                "name", &name,
                "root", &root,
                "fresh", &fresh,
                NULL);

  g_assert_cmpstr (name, ==, "test");
  g_assert_true (g_file_equal (root, tt->root));
  g_assert_true (fresh);
}

static void
test_journal_create (TestJournal *tt, gconstpointer user_data)
{
  g_autoptr(BoltJournal) j = NULL;
  g_autoptr(GError) err = NULL;

  /* creation and freshness check */
  j = bolt_journal_new (tt->root, "test", &err);

  g_assert_no_error (err);
  g_assert_nonnull (j);

  g_assert_true (bolt_journal_is_fresh (j));

  g_clear_object (&j);

  j = bolt_journal_new (tt->root, "test", &err);

  g_assert_no_error (err);
  g_assert_nonnull (j);

  g_assert_true (bolt_journal_is_fresh (j));
}

static void
test_journal_insert (TestJournal *tt, gconstpointer user_data)
{
  g_autoptr(BoltJournal) j = NULL;
  g_autoptr(GError) err = NULL;
  g_autoptr(GPtrArray) arr = NULL;
  gboolean ok;
  static BoltJournalItem items[] = {
    {(char *) "aaaa", BOLT_JOURNAL_ADDED,   0},
    {(char *) "bbbb", BOLT_JOURNAL_REMOVED, 0},
    {(char *) "cccc", BOLT_JOURNAL_REMOVED, 0},
    {(char *) "dddd", BOLT_JOURNAL_ADDED,   0},
    {NULL,            BOLT_JOURNAL_FAILED,  0},
  };

  j = bolt_journal_new (tt->root, "test", &err);

  g_assert_no_error (err);
  g_assert_nonnull (j);

  arr = bolt_journal_list (j, &err);
  g_assert_cmpuint (arr->len, ==, 0);

  for (BoltJournalItem *i = items; i->id; i++)
    {
      ok = bolt_journal_put (j, i->id, i->op, &err);
      g_assert_no_error (err);
      g_assert_true (ok);
    }
  g_assert_false (bolt_journal_is_fresh (j));

  g_clear_pointer (&arr, g_ptr_array_unref);
  arr = bolt_journal_list (j, &err);

  g_assert_no_error (err);
  g_assert_nonnull (arr);

  g_assert_cmpuint (arr->len, ==, G_N_ELEMENTS (items) - 1);

  for (guint i = 0; i < arr->len; i++)
    {
      BoltJournalItem *ours = items + i;
      BoltJournalItem *theirs = arr->pdata[i];

      g_assert_cmpstr (theirs->id, ==, ours->id);
      g_assert_cmpint (theirs->op, ==, ours->op);
    }

  /* close and re-open and re-do the test */
  g_clear_object (&j);
  g_clear_pointer (&arr, g_ptr_array_unref);

  j = bolt_journal_new (tt->root, "test", &err);

  g_assert_no_error (err);
  g_assert_nonnull (j);

  arr = bolt_journal_list (j, &err);

  g_assert_no_error (err);
  g_assert_nonnull (arr);

  g_assert_cmpuint (arr->len, ==, G_N_ELEMENTS (items) - 1);

  for (guint i = 0; i < arr->len; i++)
    {
      BoltJournalItem *ours = items + i;
      BoltJournalItem *theirs = arr->pdata[i];

      g_assert_cmpstr (theirs->id, ==, ours->id);
      g_assert_cmpint (theirs->op, ==, ours->op);
    }

  /* reset the journal  */
  ok = bolt_journal_reset (j, &err);
  g_assert_no_error (err);
  g_assert_true (ok);

  g_clear_pointer (&arr, g_ptr_array_unref);
  arr = bolt_journal_list (j, &err);
  g_assert_cmpuint (arr->len, ==, 0);
  g_assert_true (bolt_journal_is_fresh (j));
}

static gint
sort_journal_items (gconstpointer a,
                    gconstpointer b)
{
  const BoltJournalItem *ia = *((BoltJournalItem **) a);
  const BoltJournalItem *ib = *((BoltJournalItem **) b);

  return g_strcmp0 (ia->id, ib->id);
}

static void
test_journal_diff (TestJournal *tt, gconstpointer user_data)
{
  g_autoptr(BoltJournal) j = NULL;
  g_autoptr(GError) err = NULL;
  g_autoptr(GPtrArray) arr = NULL;
  g_autoptr(GHashTable) diff = NULL;
  gboolean ok;
  static BoltJournalItem items[] = {
    {(char *) "aaaa", BOLT_JOURNAL_ADDED,   0},
    {(char *) "bbbb", BOLT_JOURNAL_REMOVED, 0},
    {(char *) "cccc", BOLT_JOURNAL_REMOVED, 0},
    {(char *) "dddd", BOLT_JOURNAL_ADDED,   0},
    {(char *) "eeee", BOLT_JOURNAL_ADDED,   0},
    {(char *) "ffff", BOLT_JOURNAL_ADDED,   0},
  };
  guint k;

  j = bolt_journal_new (tt->root, "diff", &err);

  /* the first and the last elements are  added "manually"
   * the rest via _put_diff */
  ok = bolt_journal_put (j, items[0].id, items[0].op, &err);
  g_assert_no_error (err);
  g_assert_true (ok);

  diff = g_hash_table_new (g_str_hash, g_str_equal);

  for (k = 1; k < G_N_ELEMENTS (items) - 2; k++)
    {
      BoltJournalItem *i = items + k;
      int op = (i->op == BOLT_JOURNAL_ADDED) ? '+' : '-';

      g_hash_table_insert (diff, i->id, GINT_TO_POINTER (op));
    }

  ok = bolt_journal_put_diff (j, diff, &err);
  g_assert_no_error (err);
  g_assert_true (ok);

  /* the last element */

  ok = bolt_journal_put (j, items[k].id, items[k].op, &err);
  g_assert_no_error (err);
  g_assert_true (ok);

  arr = bolt_journal_list (j, &err);

  g_assert_no_error (err);
  g_assert_nonnull (arr);

  g_assert_cmpuint (arr->len, ==, G_N_ELEMENTS (items) - 1);

  /* put_diff does not have an order of any of the items
   * within the diff, so they are sorted to the same order
   * as the initial array
   */
  g_ptr_array_sort (arr, sort_journal_items);

  for (guint i = 0; i < arr->len; i++)
    {
      BoltJournalItem *ours = items + i;
      BoltJournalItem *theirs = g_ptr_array_index (arr, i);

      g_assert_cmpstr (theirs->id, ==, ours->id);
      g_assert_cmpint (theirs->op, ==, ours->op);
    }
}

static void
test_journal_invalid_file (TestJournal *tt, gconstpointer user_data)
{
  if (g_test_subprocess ())
    {
      g_autoptr(GError) err = NULL;
      g_autofree char *path = NULL;
      gboolean ok;
      const char * const invalid_data[] = {
        "justonestring\n",
        "invalidop X 0XFF",
        "str str str\n",
        "str str\n",
        "\n",
        NULL,
      };

      /* we fiddle with the writer, and also want to check
       * stderr, so the easy way is to do from a subprocess */
      g_log_set_writer_func (nonfatal_logger, NULL, NULL);

      path = g_build_filename (tt->path, "bootacl", NULL);

      for (const char *const *i = invalid_data; *i; i++)
        {
          g_autoptr(BoltJournal) j = NULL;
          g_autoptr(GPtrArray) arr = NULL;
          const char *data = *i;

          ok = g_file_set_contents (path, data, -1, &err);
          g_assert_no_error (err);
          g_assert_true (ok);

          j = bolt_journal_new (tt->root, "bootacl", &err);
          arr = bolt_journal_list (j, &err);
          g_assert_no_error (err);
          g_assert_nonnull (arr);
        }

      exit (0);
    }

  g_test_trap_subprocess (NULL, 0, 0);
  g_test_trap_assert_passed ();
  g_test_trap_assert_stderr ("*invalid entry*");
}
static void
test_journal_op_stringops (TestJournal *tt, gconstpointer user_data)
{
  g_autoptr(GError) error = NULL;
  BoltJournalOp op;
  char ops[] = "!=+-";

  /* don't include the trailing \0 */
  for (gsize i = 0; i < G_N_ELEMENTS (ops) - 1; i++)
    {
      g_autoptr(GError) err = NULL;
      char str[] = {ops[i], '\0'};
      const char *tst;

      op = bolt_journal_op_from_string (str, &err);
      g_assert_no_error (err);

      tst = bolt_journal_op_to_string (op);
      g_assert_cmpstr (tst, ==, str);
    }

  op = bolt_journal_op_from_string ("XXX", &error);
  g_assert_error (error, BOLT_ERROR, BOLT_ERROR_FAILED);
  g_clear_pointer (&error, g_error_free);

  op = bolt_journal_op_from_string ("", &error);
  g_assert_error (error, BOLT_ERROR, BOLT_ERROR_FAILED);
}

int
main (int argc, char **argv)
{

  setlocale (LC_ALL, "");

  g_test_init (&argc, &argv, NULL);

  g_resources_register (bolt_daemon_get_resource ());

  g_test_add ("/journal/object",
              TestJournal,
              NULL,
              test_journal_setup,
              test_journal_object,
              test_journal_tear_down);

  g_test_add ("/journal/create",
              TestJournal,
              NULL,
              test_journal_setup,
              test_journal_create,
              test_journal_tear_down);

  g_test_add ("/journal/ops",
              TestJournal,
              NULL,
              test_journal_setup,
              test_journal_insert,
              test_journal_tear_down);

  g_test_add ("/journal/diff",
              TestJournal,
              NULL,
              test_journal_setup,
              test_journal_diff,
              test_journal_tear_down);

  g_test_add ("/journal/invalid_file",
              TestJournal,
              NULL,
              test_journal_setup,
              test_journal_invalid_file,
              test_journal_tear_down);

  g_test_add ("/journal/op/string",
              TestJournal,
              NULL,
              NULL,
              test_journal_op_stringops,
              NULL);

  return g_test_run ();
}