Branch data Line data Source code
1 : : /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 : : *
3 : : * Copyright 2025 GNOME Foundation, Inc.
4 : : *
5 : : * SPDX-License-Identifier: LGPL-2.1-or-later
6 : : *
7 : : * This library is free software; you can redistribute it and/or
8 : : * modify it under the terms of the GNU Lesser General Public
9 : : * License as published by the Free Software Foundation; either
10 : : * version 2.1 of the License, or (at your option) any later version.
11 : : *
12 : : * This library is distributed in the hope that it will be useful,
13 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : : * Lesser General Public License for more details.
16 : : *
17 : : * You should have received a copy of the GNU Lesser General Public
18 : : * License along with this library; if not, write to the Free Software
19 : : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 : : *
21 : : * Authors:
22 : : * - Philip Withnall <pwithnall@gnome.org>
23 : : */
24 : :
25 : : #include "config.h"
26 : :
27 : : #include <glib.h>
28 : : #include <gio/gio.h>
29 : : #include <libmalcontent-timer/timer-store.h>
30 : : #include <locale.h>
31 : :
32 : :
33 : : static GFile *
34 : 1 : create_tmp_store_directory (void)
35 : : {
36 : 1 : g_autoptr(GError) local_error = NULL;
37 : 1 : g_autofree char *store_directory_path = NULL;
38 : :
39 : 1 : store_directory_path = g_dir_make_tmp ("timer-store-test-XXXXXX", &local_error);
40 : 1 : g_assert_no_error (local_error);
41 : 1 : return g_file_new_for_path (store_directory_path);
42 : : }
43 : :
44 : : static void
45 : 1 : assert_rm_rf (GFile *dir)
46 : : {
47 : 1 : g_autoptr(GFileEnumerator) enumerator = NULL;
48 : 1 : g_autoptr(GFileInfo) child_info = NULL;
49 : 1 : g_autoptr(GError) local_error = NULL;
50 : :
51 : 1 : enumerator = g_file_enumerate_children (dir, "standard::type,standard::name",
52 : : G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
53 : : NULL, &local_error);
54 : 1 : g_assert_no_error (local_error);
55 : :
56 [ - + ]: 1 : while ((child_info = g_file_enumerator_next_file (enumerator, NULL, &local_error)))
57 : : {
58 : 0 : const char *name = g_file_info_get_name (child_info);
59 : 0 : g_autoptr(GFile) child = g_file_get_child (dir, name);
60 : :
61 [ # # ]: 0 : if (g_file_info_get_file_type (child_info) == G_FILE_TYPE_DIRECTORY)
62 : : {
63 : 0 : assert_rm_rf (child);
64 : : }
65 : : else
66 : : {
67 : 0 : g_file_delete (child, NULL, &local_error);
68 : 0 : g_assert_no_error (local_error);
69 : : }
70 : :
71 [ # # ]: 0 : g_clear_object (&child_info);
72 : : }
73 : :
74 : 1 : g_file_delete (dir, NULL, &local_error);
75 : 1 : g_assert_no_error (local_error);
76 : 1 : }
77 : :
78 : : static void
79 : 2 : async_result_cb (GObject *object,
80 : : GAsyncResult *result,
81 : : void *user_data)
82 : : {
83 : 2 : GAsyncResult **result_out = user_data;
84 : :
85 : 2 : g_assert (*result_out == NULL);
86 : 2 : *result_out = g_object_ref (result);
87 : 2 : g_main_context_wakeup (NULL);
88 : 2 : }
89 : :
90 : :
91 : : static void
92 : 1 : test_timer_store_types (void)
93 : : {
94 : 1 : g_test_summary ("Test that the GType definitions for various types work");
95 : :
96 : 1 : g_type_ensure (mct_timer_store_get_type ());
97 : 1 : }
98 : :
99 : : static void
100 : 1 : test_timer_store_open_no_database (void)
101 : : {
102 : 1 : g_autoptr(GFile) store_directory = NULL;
103 : 1 : g_autoptr(MctTimerStore) store = NULL;
104 : 1 : g_autoptr(GAsyncResult) result = NULL;
105 : : const MctTimerStoreTransaction *transaction;
106 : 1 : g_autoptr(GError) local_error = NULL;
107 : :
108 : 1 : g_test_summary ("Test that opening a database works when no file exists yet");
109 : :
110 : : /* Try with a directory which exists but contains no files */
111 : 1 : store_directory = create_tmp_store_directory ();
112 : 1 : store = mct_timer_store_new (store_directory);
113 : :
114 : 1 : mct_timer_store_open_username_async (store, "username", NULL, async_result_cb, &result);
115 [ + + ]: 2 : while (result == NULL)
116 : 1 : g_main_context_iteration (NULL, TRUE);
117 : 1 : transaction = mct_timer_store_open_username_finish (store, result, &local_error);
118 : 1 : g_assert_no_error (local_error);
119 : 1 : g_assert_nonnull (transaction);
120 : :
121 : 1 : mct_timer_store_roll_back_transaction (store, g_steal_pointer (&transaction));
122 : :
123 : : /* And again with a directory which doesn’t exist */
124 : 1 : assert_rm_rf (store_directory);
125 [ + - ]: 1 : g_clear_object (&result);
126 : :
127 : 1 : mct_timer_store_open_username_async (store, "username", NULL, async_result_cb, &result);
128 [ + + ]: 2 : while (result == NULL)
129 : 1 : g_main_context_iteration (NULL, TRUE);
130 : 1 : transaction = mct_timer_store_open_username_finish (store, result, &local_error);
131 : 1 : g_assert_no_error (local_error);
132 : 1 : g_assert_nonnull (transaction);
133 : :
134 : 1 : mct_timer_store_roll_back_transaction (store, g_steal_pointer (&transaction));
135 : 1 : }
136 : :
137 : : int
138 : 1 : main (int argc,
139 : : char **argv)
140 : : {
141 : 1 : setlocale (LC_ALL, "");
142 : 1 : g_test_init (&argc, &argv, NULL);
143 : :
144 : 1 : g_test_add_func ("/timer-store/types", test_timer_store_types);
145 : 1 : g_test_add_func ("/timer-store/open-no-database", test_timer_store_open_no_database);
146 : :
147 : 1 : return g_test_run ();
148 : : }
|