FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
test_fossconfig.c
Go to the documentation of this file.
1 /*********************************************************************
2 Copyright (C) 2011 Hewlett-Packard Development Company, L.P.
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 version 2 as published by the Free Software Foundation.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 *********************************************************************/
17 
23 /* includes for files that will be tested */
24 #include <fossconfig.h>
25 
26 /* library includes */
27 #include <stdio.h>
28 
29 /* cunit includes */
30 #include <libfocunit.h>
31 
32 /* ************************************************************************** */
33 /* *** declaration of private members *************************************** */
34 /* ************************************************************************** */
35 
36 fo_conf* test_data;
37 
38 /* ************************************************************************** */
39 /* * defines for easy of mapping to test configuration file * */
40 /* * these are included so that if changes are made to the configuration * */
41 /* * file, these can be changed and that will propagate through all of * */
42 /* * test cases * */
43 /* * * */
44 /* * for those unfamiliar with the c preprocessor, the ## operator joins * */
45 /* * two strings, so calling "JOIN(GROUP_, 1)" will produce "GROUP_1" * */
46 /* * because of this calling "key(1, 2)" should produce "GROUP_1_KEY_2" * */
47 /* * * */
48 /* * this means that a change to the test configuration file should mean a * */
49 /* * change to only one spot in this file. the group(1) should also be more * */
50 /* * readable in the error output than GROUP_1 or "one" which is the main * */
51 /* * reason why all these #defines were added * */
52 /* ************************************************************************** */
53 
54 #define CONF_FILE "confdata/conftest.conf"
55 #define NONE "none"
56 
57 #define GROUP(g) GROUP_##g
58 #define KEY(g, k) GROUP_##g##_KEY_##k
59 #define VAL(g, v) GROUP_##g##_VALUE_##v
60 #define VAL_IDX(g, v, i) GROUP_##g##_VALUE_##v##_##i
61 
62 #define GROUP_0 "one"
63 #define GROUP_0_KEY_0 "key_a"
64 #define GROUP_0_VALUE_0 "hello"
65 #define GROUP_0_KEY_1 "key_b"
66 #define GROUP_0_VALUE_1 "goodbye"
67 
68 #define GROUP_1 "two"
69 #define GROUP_1_KEY_0 "another"
70 #define GROUP_1_VALUE_0 "value"
71 #define GROUP_1_KEY_1 "names"
72 #define GROUP_1_VALUE_1 "Bob, Marry, Mark, Larry, Vincent, Alex"
73 
74 #define GROUP_2 "three"
75 #define GROUP_2_KEY_0 "this"
76 #define GROUP_2_VALUE_0 "group"
77 #define GROUP_2_KEY_1 "has"
78 #define GROUP_2_VALUE_1 "3"
79 #define GROUP_2_KEY_2 "key"
80 #define GROUP_2_VALUE_2 "literals"
81 
82 #define GROUP_3 "four"
83 #define GROUP_3_KEY_0 "is"
84 #define GROUP_3_VALUE_0_0 "is"
85 #define GROUP_3_VALUE_0_1 "a"
86 #define GROUP_3_VALUE_0_2 "list"
87 #define GROUP_3_VALUE_0_3 "group"
88 #define GROUP_3_KEY_1 "there"
89 #define GROUP_3_VALUE_1_0 "there"
90 #define GROUP_3_VALUE_1_1 "are"
91 #define GROUP_3_VALUE_1_2 "two"
92 #define GROUP_3_VALUE_1_3 "lists"
93 #define GROUP_3_VALUE_1_4 "in"
94 #define GROUP_3_VALUE_1_5 "this"
95 #define GROUP_3_VALUE_1_6 "group"
96 #define GROUP_3_KEY_2 "not"
97 #define GROUP_3_VALUE_2 "list"
98 
99 /* ************************************************************************** */
100 /* *** tests **************************************************************** */
101 /* ************************************************************************** */
102 
117 {
118  GError* error = NULL;
119 
120  test_data = fo_config_load("dummy", &error);
121  FO_ASSERT_PTR_NULL(test_data);
122  FO_ASSERT_PTR_NOT_NULL(error);
123  FO_ASSERT_EQUAL(error->domain, PARSE_ERROR);
124  FO_ASSERT_EQUAL(error->code, fo_missing_file);
125  g_clear_error(&error);
126 
127  test_data = fo_config_load("confdata/invalid_group.conf", &error);
128  FO_ASSERT_PTR_NULL(test_data);
129  FO_ASSERT_PTR_NOT_NULL_FATAL(error);
130  FO_ASSERT_EQUAL(error->domain, PARSE_ERROR);
131  FO_ASSERT_EQUAL(error->code, fo_invalid_file);
132  g_clear_error(&error);
133 
134  test_data = fo_config_load("confdata/no_group.conf", &error);
135  FO_ASSERT_PTR_NULL(test_data);
136  FO_ASSERT_PTR_NOT_NULL_FATAL(error);
137  FO_ASSERT_EQUAL(error->domain, PARSE_ERROR);
138  FO_ASSERT_EQUAL(error->code, fo_invalid_key);
139  g_clear_error(&error);
140 
141  test_data = fo_config_load("confdata/key_value.conf", &error);
142  FO_ASSERT_PTR_NULL(test_data);
143  FO_ASSERT_PTR_NOT_NULL_FATAL(error);
144  FO_ASSERT_EQUAL(error->domain, PARSE_ERROR);
145  FO_ASSERT_EQUAL(error->code, fo_invalid_file);
146  g_clear_error(&error);
147 
148  test_data = fo_config_load("confdata/bad_key.conf", &error);
149  FO_ASSERT_PTR_NULL(test_data);
150  FO_ASSERT_PTR_NOT_NULL_FATAL(error);
151  FO_ASSERT_EQUAL(error->domain, PARSE_ERROR);
152  FO_ASSERT_EQUAL(error->code, fo_invalid_file);
153  g_clear_error(&error);
154 
155  test_data = fo_config_load("confdata/key_name.conf", &error);
156  FO_ASSERT_PTR_NULL(test_data);
157  FO_ASSERT_PTR_NOT_NULL_FATAL(error);
158  FO_ASSERT_EQUAL(error->domain, PARSE_ERROR);
159  FO_ASSERT_EQUAL(error->code, fo_invalid_file);
160  g_clear_error(&error);
161 
162  test_data = fo_config_load(CONF_FILE, &error);
163  if (error)
164  {
165  FO_FAIL_FATAL("can't load test configuration, aborting");
166  }
167 }
168 
184 {
185  int length;
186  char** names = fo_config_group_set(test_data, &length);
187 
188  FO_ASSERT_EQUAL_FATAL(length, 4);
189  FO_ASSERT_STRING_EQUAL(names[0], GROUP(3));
190  FO_ASSERT_STRING_EQUAL(names[1], GROUP(0));
191  FO_ASSERT_STRING_EQUAL(names[2], GROUP(2));
192  FO_ASSERT_STRING_EQUAL(names[3], GROUP(1));
193 }
194 
206 {
207  int length;
208  char** names;
209 
210  names = fo_config_key_set(test_data, GROUP(0), &length);
211  FO_ASSERT_EQUAL_FATAL(length, 2);
212  FO_ASSERT_STRING_EQUAL(names[0], KEY(0, 0));
213  FO_ASSERT_STRING_EQUAL(names[1], KEY(0, 1));
214 
215  names = fo_config_key_set(test_data, GROUP(1), &length);
216  FO_ASSERT_EQUAL_FATAL(length, 2);
217  FO_ASSERT_STRING_EQUAL(names[0], KEY(1, 0));
218  FO_ASSERT_STRING_EQUAL(names[1], KEY(1, 1));
219 
220  names = fo_config_key_set(test_data, GROUP(2), &length);
221  FO_ASSERT_EQUAL_FATAL(length, 3);
222  FO_ASSERT_STRING_EQUAL(names[0], KEY(2, 1));
223  FO_ASSERT_STRING_EQUAL(names[1], KEY(2, 2));
224  FO_ASSERT_STRING_EQUAL(names[2], KEY(2, 0));
225 
226  names = fo_config_key_set(test_data, GROUP(3), &length);
227  FO_ASSERT_EQUAL_FATAL(length, 3);
228  FO_ASSERT_STRING_EQUAL(names[0], KEY(3, 0));
229  FO_ASSERT_STRING_EQUAL(names[1], KEY(3, 2));
230  FO_ASSERT_STRING_EQUAL(names[2], KEY(3, 1));
231 
232  FO_ASSERT_PTR_NULL(fo_config_key_set(test_data, "none", &length));
233 }
234 
242 {
243  FO_ASSERT_TRUE(fo_config_has_group(test_data, GROUP(0)));
244  FO_ASSERT_FALSE(fo_config_has_group(test_data, NONE));
245 }
246 
258 {
259  FO_ASSERT_TRUE(fo_config_has_key(test_data, GROUP(0), KEY(0, 0)));
260  FO_ASSERT_FALSE(fo_config_has_key(test_data, NONE, KEY(0, 0)));
261  FO_ASSERT_FALSE(fo_config_has_key(test_data, GROUP(0), NONE));
262 }
263 
274 {
275  GError* error = NULL;
276 
277  FO_ASSERT_STRING_EQUAL(
278  fo_config_get(test_data, GROUP(0), KEY(0, 0), &error),
279  VAL(0, 0));
280  FO_ASSERT_STRING_EQUAL(
281  fo_config_get(test_data, GROUP(0), KEY(0, 1), &error),
282  VAL(0, 1));
283  FO_ASSERT_STRING_EQUAL(
284  fo_config_get(test_data, GROUP(1), KEY(1, 0), &error),
285  VAL(1, 0));
286  FO_ASSERT_STRING_EQUAL(
287  fo_config_get(test_data, GROUP(1), KEY(1, 1), &error),
288  VAL(1, 1));
289  FO_ASSERT_STRING_EQUAL(
290  fo_config_get(test_data, GROUP(2), KEY(2, 0), &error),
291  VAL(2, 0));
292  FO_ASSERT_STRING_EQUAL(
293  fo_config_get(test_data, GROUP(2), KEY(2, 1), &error),
294  VAL(2, 1));
295  FO_ASSERT_STRING_EQUAL(
296  fo_config_get(test_data, GROUP(2), KEY(2, 2), &error),
297  VAL(2, 2));
298  FO_ASSERT_STRING_EQUAL(
299  fo_config_get(test_data, GROUP(3), KEY(3, 2), &error),
300  VAL(3, 2));
301 
302  FO_ASSERT_PTR_NULL(fo_config_get(test_data, GROUP(0), NONE, &error));
303  FO_ASSERT_EQUAL(error->domain, RETRIEVE_ERROR);
304  FO_ASSERT_EQUAL(error->code, fo_missing_key);
305  FO_ASSERT_STRING_EQUAL(error->message,
306  "ERROR: unknown key=\"none\" for group=\"one\"");
307  g_clear_error(&error);
308 
309  FO_ASSERT_PTR_NULL(fo_config_get(test_data, NONE, KEY(0, 0), &error));
310  FO_ASSERT_EQUAL(error->domain, RETRIEVE_ERROR);
311  FO_ASSERT_EQUAL(error->code, fo_missing_group);
312  FO_ASSERT_STRING_EQUAL(error->message,
313  "ERROR: unknown group \"none\"");
314  g_clear_error(&error);
315 }
316 
327 {
328  GError* error = NULL;
329 
330  FO_ASSERT_FALSE(fo_config_is_list(test_data, GROUP(3), KEY(0, 0), &error));
331  FO_ASSERT_TRUE(fo_config_is_list(test_data, GROUP(3), KEY(3, 0), &error));
332  FO_ASSERT_TRUE(fo_config_is_list(test_data, GROUP(3), KEY(3, 1), &error));
333  FO_ASSERT_FALSE(fo_config_is_list(test_data, GROUP(3), KEY(3, 2), &error));
334 }
335 
345 {
346  GError* error = NULL;
347  int len = 0;
348 
349  len = fo_config_list_length(test_data, GROUP(3), KEY(3, 0), &error);
350  FO_ASSERT_EQUAL(len, 4);
351  FO_ASSERT_PTR_NULL(error);
352  if (error) g_clear_error(&error);
353 
354  len = fo_config_list_length(test_data, GROUP(3), KEY(3, 1), &error);
355  FO_ASSERT_EQUAL(len, 7);
356  FO_ASSERT_PTR_NULL(error);
357  if (error) g_clear_error(&error);
358 
359  len = fo_config_list_length(test_data, GROUP(3), KEY(3, 2), &error);
360  FO_ASSERT_EQUAL(len, 0);
361 
362  FO_ASSERT_PTR_NOT_NULL_FATAL(error);
363  FO_ASSERT_EQUAL(error->domain, RETRIEVE_ERROR);
364  FO_ASSERT_EQUAL(error->code, fo_invalid_group);
365  FO_ASSERT_STRING_EQUAL(error->message,
366  "ERROR: four[not] must be of type list to get length");
367  g_clear_error(&error);
368 }
369 
379 {
380  GError* error = NULL;
381  gchar* tmp;
382 
383 #define CONFIG_GET_LIST_ASSERT(g, k, i) \
384  tmp = fo_config_get_list(test_data, GROUP(g), KEY(g, k), i, &error); \
385  FO_ASSERT_STRING_EQUAL(tmp, VAL_IDX(g, k, i)); \
386  FO_ASSERT_PTR_NULL(error); \
387  if(error) g_clear_error(&error)
388 
389  CONFIG_GET_LIST_ASSERT(3, 0, 0);
390  CONFIG_GET_LIST_ASSERT(3, 0, 1);
391  CONFIG_GET_LIST_ASSERT(3, 0, 2);
392  CONFIG_GET_LIST_ASSERT(3, 0, 3);
393 
394  CONFIG_GET_LIST_ASSERT(3, 1, 0);
395  CONFIG_GET_LIST_ASSERT(3, 1, 1);
396  CONFIG_GET_LIST_ASSERT(3, 1, 2);
397  CONFIG_GET_LIST_ASSERT(3, 1, 3);
398  CONFIG_GET_LIST_ASSERT(3, 1, 4);
399  CONFIG_GET_LIST_ASSERT(3, 1, 5);
400  CONFIG_GET_LIST_ASSERT(3, 1, 6);
401 
402 #undef CONFIG_GET_LIST_ASSERT
403 
404  FO_ASSERT_PTR_NULL(
405  fo_config_get_list(test_data, GROUP(3), KEY(3, 2), 0, &error));
406  FO_ASSERT_EQUAL(error->domain, RETRIEVE_ERROR);
407  FO_ASSERT_EQUAL(error->code, fo_invalid_key);
408  FO_ASSERT_STRING_EQUAL(error->message,
409  "ERROR: four[not] must be of type list to get list element")
410  g_clear_error(&error);
411 
412  FO_ASSERT_PTR_NULL(
413  fo_config_get_list(test_data, GROUP(3), KEY(3, 0), 4, &error));
414  FO_ASSERT_EQUAL(error->domain, RETRIEVE_ERROR);
415  FO_ASSERT_EQUAL(error->code, fo_invalid_key);
416  FO_ASSERT_STRING_EQUAL(error->message,
417  "ERROR: four[is] 4 is out of range");
418  g_clear_error(&error);
419 
420  FO_ASSERT_PTR_NULL(
421  fo_config_get_list(test_data, GROUP(3), KEY(3, 0), -1, &error));
422  FO_ASSERT_EQUAL(error->domain, RETRIEVE_ERROR);
423  FO_ASSERT_EQUAL(error->code, fo_invalid_key);
424  FO_ASSERT_STRING_EQUAL(error->message,
425  "ERROR: four[is] -1 is out of range");
426  g_clear_error(&error);
427 }
428 
438 {
439  fo_config_free(test_data);
440 }
441 
442 /* ************************************************************************** */
443 /* *** cunit test info ****************************************************** */
444 /* ************************************************************************** */
445 
446 CU_TestInfo fossconfig_testcases[] =
447  {
448  {"fo_config_load()", test_fo_config_load},
449  {"fo_config_group_set()", test_fo_config_group_set},
450  {"fo_config_key_set()", test_fo_config_key_set},
451  {"fo_config_has_group()", test_fo_config_has_group},
452  {"fo_config_has_key()", test_fo_config_has_key},
453  {"fo_config_get()", test_fo_config_get},
454  {"fo_config_is_list()", test_fo_config_is_list},
455  {"fo_config_list_length()", test_fo_config_list_length},
456  {"fo_config_get_list()", test_fo_config_get_list},
457  {"fo_config_free()", test_fo_config_free},
458  CU_TEST_INFO_NULL
459  };
int fo_config_has_group(fo_conf *conf, char *group)
Checks if the currently parsed configuration file has a specific group.
Definition: fossconfig.c:656
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:444
FOSSology library to read config file.
#define GROUP
Default group ID.
Definition: libfossrepo.c:49
void test_fo_config_is_list()
Tests the is list function.
File is missing.
Definition: fossconfig.h:34
File is invalid.
Definition: fossconfig.h:39
void test_fo_config_get_list()
Tests the get list function.
void test_fo_config_list_length()
Tests the list length function.
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, the error object is set and NULL is returned.
Definition: fossconfig.c:341
char ** fo_config_group_set(fo_conf *conf, int *length)
Gets the set of group names.
Definition: fossconfig.c:577
void test_fo_config_load()
test the fo_config_load function.
void test_fo_config_key_set()
Test the key set function. Again, keys are stored in alphabetical order, so the comparison order may ...
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:673
void test_fo_config_free()
Tests the config free function. This makes sure that everything is correctly set to NULL after a free...
#define PARSE_ERROR
Definition: fossconfig.h:28
Required group is missing.
Definition: fossconfig.h:35
Requested group is invalid.
Definition: fossconfig.h:38
void test_fo_config_has_group()
Tests the has group function.
Required key is missing.
Definition: fossconfig.h:36
char * fo_config_get_list(fo_conf *conf, char *group, char *key, int idx, GError **error)
Definition: fossconfig.c:387
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:480
Requested key is invalid.
Definition: fossconfig.h:37
void test_fo_config_has_key()
Test the has key function.
void fo_config_free(fo_conf *conf)
Frees the memory associated with the internal configuration data structures.
Definition: fossconfig.c:511
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:619
fo_conf * fo_config_load(char *rawname, GError **error)
Load the configuration information from the provided file.
Definition: fossconfig.c:280
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_group_set()
Test the group set function.