Greenbone Vulnerability Management Libraries 22.32.0
prefs_tests.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
6#include "prefs.c"
7
8#include <cgreen/cgreen.h>
9#include <cgreen/mocks.h>
10#include <glib.h>
11#include <glib/gstdio.h>
12#include <stdio.h>
13#include <unistd.h>
14
15Describe (prefs);
16
18{
19 // Reset preferences before each test
20 if (global_prefs)
21 g_hash_table_destroy (global_prefs);
22 global_prefs = NULL;
23}
24
25AfterEach (prefs)
26{
27 // Clean up after each test
28 if (global_prefs)
29 g_hash_table_destroy (global_prefs);
30 global_prefs = NULL;
31}
32
33Ensure (prefs, preferences_get_initializes_prefs)
34{
35 GHashTable *prefs;
36
37 // First call should initialize the preferences
38 prefs = preferences_get ();
39 assert_that (prefs, is_not_null);
40 assert_that (g_hash_table_size (prefs), is_greater_than (0));
41}
42
43Ensure (prefs, prefs_get_returns_null_for_nonexistent_key)
44{
45 const gchar *value;
46
47 // Get value for a key that doesn't exist
48 value = prefs_get ("nonexistent_key");
49 assert_that (value, is_null);
50}
51
52Ensure (prefs, prefs_get_returns_correct_value)
53{
54 const gchar *value;
55
56 // Set a preference
57 prefs_set ("test_key", "test_value");
58
59 // Get the preference back
60 value = prefs_get ("test_key");
61 assert_that (value, is_equal_to_string ("test_value"));
62}
63
64Ensure (prefs, prefs_get_bool_returns_zero_for_nonexistent_key)
65{
66 int result;
67
68 // Get boolean value for a key that doesn't exist
69 result = prefs_get_bool ("nonexistent_key");
70 assert_that (result, is_equal_to (0));
71}
72
73Ensure (prefs, prefs_get_bool_returns_one_for_yes_value)
74{
75 int result;
76
77 // Set a preference to "yes"
78 prefs_set ("test_bool_key", "yes");
79
80 // Get boolean value
81 result = prefs_get_bool ("test_bool_key");
82 assert_that (result, is_equal_to (1));
83}
84
85Ensure (prefs, prefs_get_bool_returns_zero_for_non_yes_value)
86{
87 int result;
88
89 // Set a preference to something other than "yes"
90 prefs_set ("test_bool_key", "no");
91
92 // Get boolean value
93 result = prefs_get_bool ("test_bool_key");
94 assert_that (result, is_equal_to (0));
95}
96
97Ensure (prefs, prefs_set_creates_new_preference)
98{
99 const gchar *value;
100 GHashTable *prefs;
101
102 // Get initial preferences
103 prefs = preferences_get ();
104 int initial_size = g_hash_table_size (prefs);
105
106 // Set a new preference
107 prefs_set ("new_key", "new_value");
108
109 // Check that the preference was set
110 value = prefs_get ("new_key");
111 assert_that (value, is_equal_to_string ("new_value"));
112
113 // Check that the hash table size increased
114 assert_that (g_hash_table_size (prefs), is_equal_to (initial_size + 1));
115}
116
117Ensure (prefs, prefs_set_overwrites_existing_preference)
118{
119 const gchar *value;
120
121 // Set a preference
122 prefs_set ("overwrite_key", "initial_value");
123
124 // Check initial value
125 value = prefs_get ("overwrite_key");
126 assert_that (value, is_equal_to_string ("initial_value"));
127
128 // Overwrite the preference
129 prefs_set ("overwrite_key", "new_value");
130
131 // Check that the preference was overwritten
132 value = prefs_get ("overwrite_key");
133 assert_that (value, is_equal_to_string ("new_value"));
134}
135
136Ensure (prefs, prefs_config_loads_from_file)
137{
138 gchar *config_file = "test_prefs.conf";
139 FILE *file;
140 const gchar *value;
141
142 // Create a temporary configuration file
143 file = fopen (config_file, "w");
144 assert_that (file, is_not_null);
145 fprintf (file, "test_config_key=test_config_value\n");
146 fclose (file);
147
148 // Load preferences from file
149 prefs_config (config_file);
150
151 // Check that the preference from file was loaded
152 value = prefs_get ("test_config_key");
153 assert_that (value, is_equal_to_string ("test_config_value"));
154
155 // Check that config_file preference was set
156 value = prefs_get ("config_file");
157 assert_that (value, is_equal_to_string (config_file));
158
159 // Clean up
160 g_remove (config_file);
161}
162
163int
164main (int argc, char **argv)
165{
166 int ret;
167 TestSuite *suite;
168
169 suite = create_test_suite ();
170
171 add_test_with_context (suite, prefs, preferences_get_initializes_prefs);
172 add_test_with_context (suite, prefs,
173 prefs_get_returns_null_for_nonexistent_key);
174 add_test_with_context (suite, prefs, prefs_get_returns_correct_value);
175 add_test_with_context (suite, prefs,
176 prefs_get_bool_returns_zero_for_nonexistent_key);
177 add_test_with_context (suite, prefs,
178 prefs_get_bool_returns_one_for_yes_value);
179 add_test_with_context (suite, prefs,
180 prefs_get_bool_returns_zero_for_non_yes_value);
181 add_test_with_context (suite, prefs, prefs_set_creates_new_preference);
182 add_test_with_context (suite, prefs,
183 prefs_set_overwrites_existing_preference);
184 add_test_with_context (suite, prefs, prefs_config_loads_from_file);
185
186 if (argc > 1)
187 ret = run_single_test (suite, argv[1], create_text_reporter ());
188 else
189 ret = run_test_suite (suite, create_text_reporter ());
190
191 destroy_test_suite (suite);
192
193 return ret;
194}
Implementation of API to handle globally stored preferences.
void prefs_config(const char *config)
Apply the configs from given file as preferences.
Definition prefs.c:156
int prefs_get_bool(const gchar *key)
Get a boolean expression of a preference value via a key.
Definition prefs.c:119
const gchar * prefs_get(const gchar *key)
Get a string preference value via a key.
Definition prefs.c:99
static GHashTable * global_prefs
Definition prefs.c:29
GHashTable * preferences_get(void)
Get the pointer to the global preferences structure. Eventually this function should not be used anyw...
Definition prefs.c:81
void prefs_set(const gchar *, const gchar *)
Set a string preference value via a key.
Definition prefs.c:142
BeforeEach(prefs)
Definition prefs_tests.c:17
int main(int argc, char **argv)
Ensure(prefs, preferences_get_initializes_prefs)
Definition prefs_tests.c:33
Describe(prefs)
AfterEach(prefs)
Definition prefs_tests.c:25