19 #include <libfocunit.h>
43 #define CONF_FILE "confdata/conftest.conf"
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
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"
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"
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"
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"
107 GError* error = NULL;
110 FO_ASSERT_PTR_NULL(test_data);
111 FO_ASSERT_PTR_NOT_NULL(error);
114 g_clear_error(&error);
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);
121 g_clear_error(&error);
124 FO_ASSERT_PTR_NULL(test_data);
125 FO_ASSERT_PTR_NOT_NULL_FATAL(error);
128 g_clear_error(&error);
131 FO_ASSERT_PTR_NULL(test_data);
132 FO_ASSERT_PTR_NOT_NULL_FATAL(error);
135 g_clear_error(&error);
138 FO_ASSERT_PTR_NULL(test_data);
139 FO_ASSERT_PTR_NOT_NULL_FATAL(error);
142 g_clear_error(&error);
145 FO_ASSERT_PTR_NULL(test_data);
146 FO_ASSERT_PTR_NOT_NULL_FATAL(error);
149 g_clear_error(&error);
154 FO_FAIL_FATAL(
"can't load test configuration, aborting");
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));
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));
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));
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));
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));
264 GError* error = NULL;
266 FO_ASSERT_STRING_EQUAL(
269 FO_ASSERT_STRING_EQUAL(
272 FO_ASSERT_STRING_EQUAL(
275 FO_ASSERT_STRING_EQUAL(
278 FO_ASSERT_STRING_EQUAL(
281 FO_ASSERT_STRING_EQUAL(
284 FO_ASSERT_STRING_EQUAL(
287 FO_ASSERT_STRING_EQUAL(
292 FO_ASSERT_EQUAL(error->domain, RETRIEVE_ERROR);
294 FO_ASSERT_STRING_EQUAL(error->message,
295 "ERROR: unknown key=\"none\" for group=\"one\"");
296 g_clear_error(&error);
298 FO_ASSERT_PTR_NULL(
fo_config_get(test_data, NONE, KEY(0, 0), &error));
299 FO_ASSERT_EQUAL(error->domain, RETRIEVE_ERROR);
301 FO_ASSERT_STRING_EQUAL(error->message,
302 "ERROR: unknown group \"none\"");
303 g_clear_error(&error);
317 GError* error = NULL;
335 GError* error = NULL;
339 FO_ASSERT_EQUAL(len, 4);
340 FO_ASSERT_PTR_NULL(error);
341 if (error) g_clear_error(&error);
344 FO_ASSERT_EQUAL(len, 7);
345 FO_ASSERT_PTR_NULL(error);
346 if (error) g_clear_error(&error);
349 FO_ASSERT_EQUAL(len, 0);
351 FO_ASSERT_PTR_NOT_NULL_FATAL(error);
352 FO_ASSERT_EQUAL(error->domain, RETRIEVE_ERROR);
354 FO_ASSERT_STRING_EQUAL(error->message,
355 "ERROR: four[not] must be of type list to get length");
356 g_clear_error(&error);
369 GError* error = NULL;
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)
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);
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);
391 #undef CONFIG_GET_LIST_ASSERT
395 FO_ASSERT_EQUAL(error->domain, RETRIEVE_ERROR);
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);
403 FO_ASSERT_EQUAL(error->domain, RETRIEVE_ERROR);
405 FO_ASSERT_STRING_EQUAL(error->message,
406 "ERROR: four[is] 4 is out of range");
407 g_clear_error(&error);
411 FO_ASSERT_EQUAL(error->domain, RETRIEVE_ERROR);
413 FO_ASSERT_STRING_EQUAL(error->message,
414 "ERROR: four[is] -1 is out of range");
415 g_clear_error(&error);
435 CU_TestInfo fossconfig_testcases[] =
char * fo_config_get_list(fo_conf *conf, char *group, char *key, int idx, GError **error)
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.
void fo_config_free(fo_conf *conf)
Frees the memory associated with the internal configuration data structures.
char ** fo_config_group_set(fo_conf *conf, int *length)
Gets the set of group names.
fo_conf * fo_config_load(char *rawname, GError **error)
Load the configuration information from the provided file.
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,...
int fo_config_has_group(fo_conf *conf, char *group)
Checks if the currently parsed configuration file has a specific group.
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.
char ** fo_config_key_set(fo_conf *conf, char *group, int *length)
Gets the set of key names for a particular group.
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.
FOSSology library to read config file.
@ fo_missing_key
Required key is missing.
@ fo_invalid_file
File is invalid.
@ fo_missing_file
File is missing.
@ fo_invalid_group
Requested group is invalid.
@ fo_missing_group
Required group is missing.
@ fo_invalid_key
Requested key is invalid.
#define GROUP
Default group ID.
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.