FOSSology  4.4.0
Open Source License Compliance by Open Source Software
test_fossconfig.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2011 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
12 /* includes for files that will be tested */
13 #include <fossconfig.h>
14 
15 /* library includes */
16 #include <stdio.h>
17 
18 /* cunit includes */
19 #include <libfocunit.h>
20 
21 /* ************************************************************************** */
22 /* *** declaration of private members *************************************** */
23 /* ************************************************************************** */
24 
25 fo_conf* test_data;
26 
27 /* ************************************************************************** */
28 /* * defines for easy of mapping to test configuration file * */
29 /* * these are included so that if changes are made to the configuration * */
30 /* * file, these can be changed and that will propagate through all of * */
31 /* * test cases * */
32 /* * * */
33 /* * for those unfamiliar with the c preprocessor, the ## operator joins * */
34 /* * two strings, so calling "JOIN(GROUP_, 1)" will produce "GROUP_1" * */
35 /* * because of this calling "key(1, 2)" should produce "GROUP_1_KEY_2" * */
36 /* * * */
37 /* * this means that a change to the test configuration file should mean a * */
38 /* * change to only one spot in this file. the group(1) should also be more * */
39 /* * readable in the error output than GROUP_1 or "one" which is the main * */
40 /* * reason why all these #defines were added * */
41 /* ************************************************************************** */
42 
43 #define CONF_FILE "confdata/conftest.conf"
44 #define NONE "none"
45 
46 #define GROUP(g) GROUP_##g
47 #define KEY(g, k) GROUP_##g##_KEY_##k
48 #define VAL(g, v) GROUP_##g##_VALUE_##v
49 #define VAL_IDX(g, v, i) GROUP_##g##_VALUE_##v##_##i
50 
51 #define GROUP_0 "one"
52 #define GROUP_0_KEY_0 "key_a"
53 #define GROUP_0_VALUE_0 "hello"
54 #define GROUP_0_KEY_1 "key_b"
55 #define GROUP_0_VALUE_1 "goodbye"
56 
57 #define GROUP_1 "two"
58 #define GROUP_1_KEY_0 "another"
59 #define GROUP_1_VALUE_0 "value"
60 #define GROUP_1_KEY_1 "names"
61 #define GROUP_1_VALUE_1 "Bob, Marry, Mark, Larry, Vincent, Alex"
62 
63 #define GROUP_2 "three"
64 #define GROUP_2_KEY_0 "this"
65 #define GROUP_2_VALUE_0 "group"
66 #define GROUP_2_KEY_1 "has"
67 #define GROUP_2_VALUE_1 "3"
68 #define GROUP_2_KEY_2 "key"
69 #define GROUP_2_VALUE_2 "literals"
70 
71 #define GROUP_3 "four"
72 #define GROUP_3_KEY_0 "is"
73 #define GROUP_3_VALUE_0_0 "is"
74 #define GROUP_3_VALUE_0_1 "a"
75 #define GROUP_3_VALUE_0_2 "list"
76 #define GROUP_3_VALUE_0_3 "group"
77 #define GROUP_3_KEY_1 "there"
78 #define GROUP_3_VALUE_1_0 "there"
79 #define GROUP_3_VALUE_1_1 "are"
80 #define GROUP_3_VALUE_1_2 "two"
81 #define GROUP_3_VALUE_1_3 "lists"
82 #define GROUP_3_VALUE_1_4 "in"
83 #define GROUP_3_VALUE_1_5 "this"
84 #define GROUP_3_VALUE_1_6 "group"
85 #define GROUP_3_KEY_2 "not"
86 #define GROUP_3_VALUE_2 "list"
87 
88 /* ************************************************************************** */
89 /* *** tests **************************************************************** */
90 /* ************************************************************************** */
91 
106 {
107  GError* error = NULL;
108 
109  test_data = fo_config_load("dummy", &error);
110  FO_ASSERT_PTR_NULL(test_data);
111  FO_ASSERT_PTR_NOT_NULL(error);
112  FO_ASSERT_EQUAL(error->domain, PARSE_ERROR);
113  FO_ASSERT_EQUAL(error->code, fo_missing_file);
114  g_clear_error(&error);
115 
116  test_data = fo_config_load("confdata/invalid_group.conf", &error);
117  FO_ASSERT_PTR_NULL(test_data);
118  FO_ASSERT_PTR_NOT_NULL_FATAL(error);
119  FO_ASSERT_EQUAL(error->domain, PARSE_ERROR);
120  FO_ASSERT_EQUAL(error->code, fo_invalid_file);
121  g_clear_error(&error);
122 
123  test_data = fo_config_load("confdata/no_group.conf", &error);
124  FO_ASSERT_PTR_NULL(test_data);
125  FO_ASSERT_PTR_NOT_NULL_FATAL(error);
126  FO_ASSERT_EQUAL(error->domain, PARSE_ERROR);
127  FO_ASSERT_EQUAL(error->code, fo_invalid_key);
128  g_clear_error(&error);
129 
130  test_data = fo_config_load("confdata/key_value.conf", &error);
131  FO_ASSERT_PTR_NULL(test_data);
132  FO_ASSERT_PTR_NOT_NULL_FATAL(error);
133  FO_ASSERT_EQUAL(error->domain, PARSE_ERROR);
134  FO_ASSERT_EQUAL(error->code, fo_invalid_file);
135  g_clear_error(&error);
136 
137  test_data = fo_config_load("confdata/bad_key.conf", &error);
138  FO_ASSERT_PTR_NULL(test_data);
139  FO_ASSERT_PTR_NOT_NULL_FATAL(error);
140  FO_ASSERT_EQUAL(error->domain, PARSE_ERROR);
141  FO_ASSERT_EQUAL(error->code, fo_invalid_file);
142  g_clear_error(&error);
143 
144  test_data = fo_config_load("confdata/key_name.conf", &error);
145  FO_ASSERT_PTR_NULL(test_data);
146  FO_ASSERT_PTR_NOT_NULL_FATAL(error);
147  FO_ASSERT_EQUAL(error->domain, PARSE_ERROR);
148  FO_ASSERT_EQUAL(error->code, fo_invalid_file);
149  g_clear_error(&error);
150 
151  test_data = fo_config_load(CONF_FILE, &error);
152  if (error)
153  {
154  FO_FAIL_FATAL("can't load test configuration, aborting");
155  }
156 }
157 
173 {
174  int length;
175  char** names = fo_config_group_set(test_data, &length);
176 
177  FO_ASSERT_EQUAL_FATAL(length, 4);
178  FO_ASSERT_STRING_EQUAL(names[0], GROUP(3));
179  FO_ASSERT_STRING_EQUAL(names[1], GROUP(0));
180  FO_ASSERT_STRING_EQUAL(names[2], GROUP(2));
181  FO_ASSERT_STRING_EQUAL(names[3], GROUP(1));
182 }
183 
195 {
196  int length;
197  char** names;
198 
199  names = fo_config_key_set(test_data, GROUP(0), &length);
200  FO_ASSERT_EQUAL_FATAL(length, 2);
201  FO_ASSERT_STRING_EQUAL(names[0], KEY(0, 0));
202  FO_ASSERT_STRING_EQUAL(names[1], KEY(0, 1));
203 
204  names = fo_config_key_set(test_data, GROUP(1), &length);
205  FO_ASSERT_EQUAL_FATAL(length, 2);
206  FO_ASSERT_STRING_EQUAL(names[0], KEY(1, 0));
207  FO_ASSERT_STRING_EQUAL(names[1], KEY(1, 1));
208 
209  names = fo_config_key_set(test_data, GROUP(2), &length);
210  FO_ASSERT_EQUAL_FATAL(length, 3);
211  FO_ASSERT_STRING_EQUAL(names[0], KEY(2, 1));
212  FO_ASSERT_STRING_EQUAL(names[1], KEY(2, 2));
213  FO_ASSERT_STRING_EQUAL(names[2], KEY(2, 0));
214 
215  names = fo_config_key_set(test_data, GROUP(3), &length);
216  FO_ASSERT_EQUAL_FATAL(length, 3);
217  FO_ASSERT_STRING_EQUAL(names[0], KEY(3, 0));
218  FO_ASSERT_STRING_EQUAL(names[1], KEY(3, 2));
219  FO_ASSERT_STRING_EQUAL(names[2], KEY(3, 1));
220 
221  FO_ASSERT_PTR_NULL(fo_config_key_set(test_data, "none", &length));
222 }
223 
231 {
232  FO_ASSERT_TRUE(fo_config_has_group(test_data, GROUP(0)));
233  FO_ASSERT_FALSE(fo_config_has_group(test_data, NONE));
234 }
235 
247 {
248  FO_ASSERT_TRUE(fo_config_has_key(test_data, GROUP(0), KEY(0, 0)));
249  FO_ASSERT_FALSE(fo_config_has_key(test_data, NONE, KEY(0, 0)));
250  FO_ASSERT_FALSE(fo_config_has_key(test_data, GROUP(0), NONE));
251 }
252 
263 {
264  GError* error = NULL;
265 
266  FO_ASSERT_STRING_EQUAL(
267  fo_config_get(test_data, GROUP(0), KEY(0, 0), &error),
268  VAL(0, 0));
269  FO_ASSERT_STRING_EQUAL(
270  fo_config_get(test_data, GROUP(0), KEY(0, 1), &error),
271  VAL(0, 1));
272  FO_ASSERT_STRING_EQUAL(
273  fo_config_get(test_data, GROUP(1), KEY(1, 0), &error),
274  VAL(1, 0));
275  FO_ASSERT_STRING_EQUAL(
276  fo_config_get(test_data, GROUP(1), KEY(1, 1), &error),
277  VAL(1, 1));
278  FO_ASSERT_STRING_EQUAL(
279  fo_config_get(test_data, GROUP(2), KEY(2, 0), &error),
280  VAL(2, 0));
281  FO_ASSERT_STRING_EQUAL(
282  fo_config_get(test_data, GROUP(2), KEY(2, 1), &error),
283  VAL(2, 1));
284  FO_ASSERT_STRING_EQUAL(
285  fo_config_get(test_data, GROUP(2), KEY(2, 2), &error),
286  VAL(2, 2));
287  FO_ASSERT_STRING_EQUAL(
288  fo_config_get(test_data, GROUP(3), KEY(3, 2), &error),
289  VAL(3, 2));
290 
291  FO_ASSERT_PTR_NULL(fo_config_get(test_data, GROUP(0), NONE, &error));
292  FO_ASSERT_EQUAL(error->domain, RETRIEVE_ERROR);
293  FO_ASSERT_EQUAL(error->code, fo_missing_key);
294  FO_ASSERT_STRING_EQUAL(error->message,
295  "ERROR: unknown key=\"none\" for group=\"one\"");
296  g_clear_error(&error);
297 
298  FO_ASSERT_PTR_NULL(fo_config_get(test_data, NONE, KEY(0, 0), &error));
299  FO_ASSERT_EQUAL(error->domain, RETRIEVE_ERROR);
300  FO_ASSERT_EQUAL(error->code, fo_missing_group);
301  FO_ASSERT_STRING_EQUAL(error->message,
302  "ERROR: unknown group \"none\"");
303  g_clear_error(&error);
304 }
305 
316 {
317  GError* error = NULL;
318 
319  FO_ASSERT_FALSE(fo_config_is_list(test_data, GROUP(3), KEY(0, 0), &error));
320  FO_ASSERT_TRUE(fo_config_is_list(test_data, GROUP(3), KEY(3, 0), &error));
321  FO_ASSERT_TRUE(fo_config_is_list(test_data, GROUP(3), KEY(3, 1), &error));
322  FO_ASSERT_FALSE(fo_config_is_list(test_data, GROUP(3), KEY(3, 2), &error));
323 }
324 
334 {
335  GError* error = NULL;
336  int len = 0;
337 
338  len = fo_config_list_length(test_data, GROUP(3), KEY(3, 0), &error);
339  FO_ASSERT_EQUAL(len, 4);
340  FO_ASSERT_PTR_NULL(error);
341  if (error) g_clear_error(&error);
342 
343  len = fo_config_list_length(test_data, GROUP(3), KEY(3, 1), &error);
344  FO_ASSERT_EQUAL(len, 7);
345  FO_ASSERT_PTR_NULL(error);
346  if (error) g_clear_error(&error);
347 
348  len = fo_config_list_length(test_data, GROUP(3), KEY(3, 2), &error);
349  FO_ASSERT_EQUAL(len, 0);
350 
351  FO_ASSERT_PTR_NOT_NULL_FATAL(error);
352  FO_ASSERT_EQUAL(error->domain, RETRIEVE_ERROR);
353  FO_ASSERT_EQUAL(error->code, fo_invalid_group);
354  FO_ASSERT_STRING_EQUAL(error->message,
355  "ERROR: four[not] must be of type list to get length");
356  g_clear_error(&error);
357 }
358 
368 {
369  GError* error = NULL;
370  gchar* tmp;
371 
372 #define CONFIG_GET_LIST_ASSERT(g, k, i) \
373  tmp = fo_config_get_list(test_data, GROUP(g), KEY(g, k), i, &error); \
374  FO_ASSERT_STRING_EQUAL(tmp, VAL_IDX(g, k, i)); \
375  FO_ASSERT_PTR_NULL(error); \
376  if(error) g_clear_error(&error)
377 
378  CONFIG_GET_LIST_ASSERT(3, 0, 0);
379  CONFIG_GET_LIST_ASSERT(3, 0, 1);
380  CONFIG_GET_LIST_ASSERT(3, 0, 2);
381  CONFIG_GET_LIST_ASSERT(3, 0, 3);
382 
383  CONFIG_GET_LIST_ASSERT(3, 1, 0);
384  CONFIG_GET_LIST_ASSERT(3, 1, 1);
385  CONFIG_GET_LIST_ASSERT(3, 1, 2);
386  CONFIG_GET_LIST_ASSERT(3, 1, 3);
387  CONFIG_GET_LIST_ASSERT(3, 1, 4);
388  CONFIG_GET_LIST_ASSERT(3, 1, 5);
389  CONFIG_GET_LIST_ASSERT(3, 1, 6);
390 
391 #undef CONFIG_GET_LIST_ASSERT
392 
393  FO_ASSERT_PTR_NULL(
394  fo_config_get_list(test_data, GROUP(3), KEY(3, 2), 0, &error));
395  FO_ASSERT_EQUAL(error->domain, RETRIEVE_ERROR);
396  FO_ASSERT_EQUAL(error->code, fo_invalid_key);
397  FO_ASSERT_STRING_EQUAL(error->message,
398  "ERROR: four[not] must be of type list to get list element")
399  g_clear_error(&error);
400 
401  FO_ASSERT_PTR_NULL(
402  fo_config_get_list(test_data, GROUP(3), KEY(3, 0), 4, &error));
403  FO_ASSERT_EQUAL(error->domain, RETRIEVE_ERROR);
404  FO_ASSERT_EQUAL(error->code, fo_invalid_key);
405  FO_ASSERT_STRING_EQUAL(error->message,
406  "ERROR: four[is] 4 is out of range");
407  g_clear_error(&error);
408 
409  FO_ASSERT_PTR_NULL(
410  fo_config_get_list(test_data, GROUP(3), KEY(3, 0), -1, &error));
411  FO_ASSERT_EQUAL(error->domain, RETRIEVE_ERROR);
412  FO_ASSERT_EQUAL(error->code, fo_invalid_key);
413  FO_ASSERT_STRING_EQUAL(error->message,
414  "ERROR: four[is] -1 is out of range");
415  g_clear_error(&error);
416 }
417 
427 {
428  fo_config_free(test_data);
429 }
430 
431 /* ************************************************************************** */
432 /* *** cunit test info ****************************************************** */
433 /* ************************************************************************** */
434 
435 CU_TestInfo fossconfig_testcases[] =
436  {
437  {"fo_config_load()", test_fo_config_load},
438  {"fo_config_group_set()", test_fo_config_group_set},
439  {"fo_config_key_set()", test_fo_config_key_set},
440  {"fo_config_has_group()", test_fo_config_has_group},
441  {"fo_config_has_key()", test_fo_config_has_key},
442  {"fo_config_get()", test_fo_config_get},
443  {"fo_config_is_list()", test_fo_config_is_list},
444  {"fo_config_list_length()", test_fo_config_list_length},
445  {"fo_config_get_list()", test_fo_config_get_list},
446  {"fo_config_free()", test_fo_config_free},
447  CU_TEST_INFO_NULL
448  };
char * fo_config_get_list(fo_conf *conf, char *group, char *key, int idx, GError **error)
Definition: fossconfig.c:382
int fo_config_list_length(fo_conf *conf, char *group, char *key, GError **error)
Gets the length of the list associated with a particular list key.
Definition: fossconfig.c:475
void fo_config_free(fo_conf *conf)
Frees the memory associated with the internal configuration data structures.
Definition: fossconfig.c:506
char ** fo_config_group_set(fo_conf *conf, int *length)
Gets the set of group names.
Definition: fossconfig.c:572
fo_conf * fo_config_load(char *rawname, GError **error)
Load the configuration information from the provided file.
Definition: fossconfig.c:275
char * fo_config_get(fo_conf *conf, const char *group, const char *key, GError **error)
Gets an element based on its group name and key name. If the group or key is not found,...
Definition: fossconfig.c:336
int fo_config_has_group(fo_conf *conf, char *group)
Checks if the currently parsed configuration file has a specific group.
Definition: fossconfig.c:651
int fo_config_is_list(fo_conf *conf, char *group, char *key, GError **error)
Checks if a particular value is a list or just a normal value.
Definition: fossconfig.c:439
char ** fo_config_key_set(fo_conf *conf, char *group, int *length)
Gets the set of key names for a particular group.
Definition: fossconfig.c:614
int fo_config_has_key(fo_conf *conf, char *group, char *key)
Checks if the a specific group in the currently parsed configuration file has a specific key.
Definition: fossconfig.c:668
FOSSology library to read config file.
#define PARSE_ERROR
Definition: fossconfig.h:16
@ fo_missing_key
Required key is missing.
Definition: fossconfig.h:24
@ fo_invalid_file
File is invalid.
Definition: fossconfig.h:27
@ fo_missing_file
File is missing.
Definition: fossconfig.h:22
@ fo_invalid_group
Requested group is invalid.
Definition: fossconfig.h:26
@ fo_missing_group
Required group is missing.
Definition: fossconfig.h:23
@ fo_invalid_key
Requested key is invalid.
Definition: fossconfig.h:25
#define GROUP
Default group ID.
Definition: libfossrepo.c:37
void test_fo_config_group_set()
Test the group set function.
void test_fo_config_list_length()
Tests the list length function.
void test_fo_config_free()
Tests the config free function. This makes sure that everything is correctly set to NULL after a free...
void test_fo_config_load()
test the fo_config_load function.
void test_fo_config_get_list()
Tests the get list function.
void test_fo_config_get()
Test the get function. This will also test the error cases of invalid key and invalid group names.
void test_fo_config_has_key()
Test the has key function.
void test_fo_config_has_group()
Tests the has group function.
void test_fo_config_key_set()
Test the key set function. Again, keys are stored in alphabetical order, so the comparison order may ...
void test_fo_config_is_list()
Tests the is list function.