Greenbone Vulnerability Management Libraries 22.32.0
prefs_tests.c File Reference
#include "prefs.c"
#include <cgreen/cgreen.h>
#include <cgreen/mocks.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <stdio.h>
#include <unistd.h>
Include dependency graph for prefs_tests.c:

Go to the source code of this file.

Functions

 Describe (prefs)
 BeforeEach (prefs)
 AfterEach (prefs)
 Ensure (prefs, preferences_get_initializes_prefs)
 Ensure (prefs, prefs_get_returns_null_for_nonexistent_key)
 Ensure (prefs, prefs_get_returns_correct_value)
 Ensure (prefs, prefs_get_bool_returns_zero_for_nonexistent_key)
 Ensure (prefs, prefs_get_bool_returns_one_for_yes_value)
 Ensure (prefs, prefs_get_bool_returns_zero_for_non_yes_value)
 Ensure (prefs, prefs_set_creates_new_preference)
 Ensure (prefs, prefs_set_overwrites_existing_preference)
 Ensure (prefs, prefs_config_loads_from_file)
int main (int argc, char **argv)

Function Documentation

◆ AfterEach()

AfterEach ( prefs )

Definition at line 25 of file prefs_tests.c.

26{
27 // Clean up after each test
28 if (global_prefs)
29 g_hash_table_destroy (global_prefs);
30 global_prefs = NULL;
31}
static GHashTable * global_prefs
Definition prefs.c:29

References global_prefs.

◆ BeforeEach()

BeforeEach ( prefs )

Definition at line 17 of file prefs_tests.c.

18{
19 // Reset preferences before each test
20 if (global_prefs)
21 g_hash_table_destroy (global_prefs);
22 global_prefs = NULL;
23}

References global_prefs.

◆ Describe()

Describe ( prefs )

◆ Ensure() [1/9]

Ensure ( prefs ,
preferences_get_initializes_prefs  )

Definition at line 33 of file prefs_tests.c.

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}
GHashTable * preferences_get(void)
Get the pointer to the global preferences structure. Eventually this function should not be used anyw...
Definition prefs.c:81

References preferences_get().

Here is the call graph for this function:

◆ Ensure() [2/9]

Ensure ( prefs ,
prefs_config_loads_from_file  )

Definition at line 136 of file prefs_tests.c.

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}
void prefs_config(const char *config)
Apply the configs from given file as preferences.
Definition prefs.c:156
const gchar * prefs_get(const gchar *key)
Get a string preference value via a key.
Definition prefs.c:99

References prefs_config(), and prefs_get().

Here is the call graph for this function:

◆ Ensure() [3/9]

Ensure ( prefs ,
prefs_get_bool_returns_one_for_yes_value  )

Definition at line 73 of file prefs_tests.c.

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}
int prefs_get_bool(const gchar *key)
Get a boolean expression of a preference value via a key.
Definition prefs.c:119
void prefs_set(const gchar *, const gchar *)
Set a string preference value via a key.
Definition prefs.c:142

References prefs_get_bool(), and prefs_set().

Here is the call graph for this function:

◆ Ensure() [4/9]

Ensure ( prefs ,
prefs_get_bool_returns_zero_for_non_yes_value  )

Definition at line 85 of file prefs_tests.c.

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}

References prefs_get_bool(), and prefs_set().

Here is the call graph for this function:

◆ Ensure() [5/9]

Ensure ( prefs ,
prefs_get_bool_returns_zero_for_nonexistent_key  )

Definition at line 64 of file prefs_tests.c.

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}

References prefs_get_bool().

Here is the call graph for this function:

◆ Ensure() [6/9]

Ensure ( prefs ,
prefs_get_returns_correct_value  )

Definition at line 52 of file prefs_tests.c.

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}

References prefs_get(), and prefs_set().

Here is the call graph for this function:

◆ Ensure() [7/9]

Ensure ( prefs ,
prefs_get_returns_null_for_nonexistent_key  )

Definition at line 43 of file prefs_tests.c.

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}

References prefs_get().

Here is the call graph for this function:

◆ Ensure() [8/9]

Ensure ( prefs ,
prefs_set_creates_new_preference  )

Definition at line 97 of file prefs_tests.c.

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}

References preferences_get(), prefs_get(), and prefs_set().

Here is the call graph for this function:

◆ Ensure() [9/9]

Ensure ( prefs ,
prefs_set_overwrites_existing_preference  )

Definition at line 117 of file prefs_tests.c.

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}

References prefs_get(), and prefs_set().

Here is the call graph for this function:

◆ main()

int main ( int argc,
char ** argv )

Definition at line 164 of file prefs_tests.c.

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}