Debian Codegraph @

ini/ini_parse_ut_check.c

raw symbols SCIP
1 /*
2 INI LIBRARY
3
4 Check based unit test for ini parser.
5
6 Copyright (C) Michal Zidek <[email protected]> 2016
7
8 INI Library is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 INI Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with INI Library. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "config.h"
23
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <check.h>
28
29 /* #define TRACE_LEVEL 7 */
30 #define TRACE_HOME
31 #include "trace.h"
32 #include "ini_configobj.h"
33 #include "ini_config_priv.h"
34
35 #define TEST_DIR_PATH ""
36
37 START_TEST(test_ini_parse_non_kvp)
38 {
39 int ret;
40 struct ini_cfgobj *ini_cfg;
41 int value;
42 struct ini_cfgfile *file_ctx;
43 struct value_obj *vo;
44 char non_kvp_cfg[] =
45 "[section_before]\n"
46 "one = 1\n"
47
48 "[section_non_kvp]\n"
49 "two = 2\n"
50 "non_kvp\n"
51 "three = 3\n"
52 "=nonkvp\n"
53
54 "[section_after]\n"
55 "four = 4\n";
56
57 ret = ini_config_file_from_mem(non_kvp_cfg, strlen(non_kvp_cfg),
58 &file_ctx);
59 fail_unless(ret == EOK, "Failed to load config. Error %d.\n", ret);
60
61 /* First try without the INI_PARSE_IGNORE_NON_KVP. This should fail
62 * with error. */
63 ret = ini_config_create(&ini_cfg);
64 fail_unless(ret == EOK, "Failed to create config. Error %d.\n", ret);
65 ret = ini_config_parse(file_ctx, INI_STOP_ON_ERROR, INI_MV1S_ALLOW, 0,
66 ini_cfg);
67 fail_if(ret != 5, "Expected error was not found.\n");
68
69 ini_config_destroy(ini_cfg);
70 ini_config_file_destroy(file_ctx);
71
72 /* Now try with INI_PARSE_IGNORE_NON_KVP. We should have no errors
73 * and all the surounding configuration should be valid */
74 ret = ini_config_file_from_mem(non_kvp_cfg, strlen(non_kvp_cfg),
75 &file_ctx);
76 fail_unless(ret == EOK, "Failed to load config. Error %d.\n", ret);
77 ret = ini_config_create(&ini_cfg);
78 fail_unless(ret == EOK, "Failed to create config. Error %d.\n", ret);
79 ret = ini_config_parse(file_ctx, INI_STOP_ON_ERROR, INI_MV1S_ALLOW,
80 INI_PARSE_IGNORE_NON_KVP,
81 ini_cfg);
82 fail_unless(ret == EOK, "ini_config_parse returned %d\n", ret);
83
84 /* Now check if the surrounding configuration is OK */
85 /* section_before */
86 ret = ini_get_config_valueobj("section_before", "one", ini_cfg,
87 INI_GET_FIRST_VALUE, &vo);
88 fail_unless(ret == EOK, "ini_get_config_valueobj returned %d\n: %s", ret,
89 strerror(ret));
90
91 value = ini_get_int_config_value(vo, 1, -1, &ret);
92 fail_unless(ret == EOK, "ini_get_int_config_value returned %d\n: %s", ret,
93 strerror(ret));
94
95 fail_unless(ret == EOK);
96 fail_if(value != 1, "Expected value 1 got %d\n", value);
97
98 /* section_non_kvp */
99 ret = ini_get_config_valueobj("section_non_kvp", "two", ini_cfg,
100 INI_GET_FIRST_VALUE, &vo);
101 fail_unless(ret == EOK);
102
103 value = ini_get_int_config_value(vo, 1, -1, &ret);
104 fail_unless(ret == EOK);
105 fail_if(value != 2, "Expected value 2 got %d\n", value);
106
107 ret = ini_get_config_valueobj("section_non_kvp", "three", ini_cfg,
108 INI_GET_FIRST_VALUE, &vo);
109 fail_unless(ret == EOK);
110
111 value = ini_get_int_config_value(vo, 1, -1, &ret);
112 fail_unless(ret == EOK);
113 fail_if(value != 3, "Expected value 3 got %d\n", value);
114
115 /* section_after */
116 ret = ini_get_config_valueobj("section_after", "four", ini_cfg,
117 INI_GET_FIRST_VALUE, &vo);
118 fail_unless(ret == EOK);
119
120 value = ini_get_int_config_value(vo, 1, -1, &ret);
121 fail_unless(ret == EOK);
122 fail_if(value != 4, "Expected value 4 got %d\n", value);
123
124 ini_config_destroy(ini_cfg);
125 ini_config_file_destroy(file_ctx);
126 }
127 END_TEST
128
129 START_TEST(test_ini_parse_section_key_conflict)
130 {
131 /*
132 * This tests the behavior of ini_config_parse to ensure correct handling
133 * of conflicts between sections and keys of the same name. There are
134 * three possibilities for conflict:
135 *
136 * 1. Inside a section, between the section name and a key name
137 * 2. Between a default-section key name and a section name
138 * 3. Between a key name in a different section and a section name
139 *
140 * In case (1), parsing finished without an error. However, when
141 * trying to select a value object inside a section, the returned
142 * object was an unchecked cast from the section's data, and not the
143 * attribute's data. In cases (2) and (3), the parser segfaulted while
144 * trying to merge a section with an attribute.
145 */
146
147 char config1[] =
148 "[a]\n"
149 "a=a\n";
150
151 char config2[] =
152 "a=b\n"
153 "[a]\n"
154 "c=d\n";
155
156 char config3[] =
157 "[a]\n"
158 "b=c\n"
159 "[b]\n"
160 "a=d\n";
161
162 char *file_contents[] = {config1, config2, config3, NULL};
163
164 size_t iter;
165
166 struct ini_cfgobj *ini_config = NULL;
167 struct ini_cfgfile *file_ctx = NULL;
168
169 int ret;
170 int i;
171 int j;
172
173 char **sections = NULL;
174 int sections_count = 0;
175 int sections_error = 0;
176
177 char **attributes = NULL;
178 int attributes_count = 0;
179 int attributes_error = 0;
180
181 struct value_obj *val = NULL;
182 char *val_str = NULL;
183
184 for (iter = 0; file_contents[iter] != NULL; iter++) {
185 ret = ini_config_create(&ini_config);
186 fail_unless(ret == EOK, "Failed to create config. Error %d.\n", ret);
187
188 ret = ini_config_file_from_mem(file_contents[iter],
189 strlen(file_contents[iter]),
190 &file_ctx);
191 fail_unless(ret == EOK, "Failed to load file. Error %d.\n", ret);
192
193 ret = ini_config_parse(file_ctx, 1, 0, 0, ini_config);
194 fail_unless(ret == EOK, "Failed to parse file. Error %d.\n", ret);
195
196 sections = ini_get_section_list(ini_config, &sections_count,
197 &sections_error);
198 fail_unless(sections_error == EOK,
199 "Failed to get sections. Error %d.\n",
200 sections_error);
201
202 for (i = 0; i < sections_count; i++) {
203 attributes = ini_get_attribute_list(ini_config,
204 sections[i],
205 &attributes_count,
206 &attributes_error);
207 fail_unless(attributes_error == EOK,
208 "Failed to get attributes. Error %d.\n",
209 attributes_error);
210
211 for (j = 0; j < attributes_count; j++) {
212 ret = ini_get_config_valueobj(sections[i], attributes[j],
213 ini_config, 0, &val);
214 fail_unless(ret == EOK,
215 "Failed to get attribute. Error %d.\n",
216 ret);
217
218 val_str = ini_get_string_config_value(val, &ret);
219 fail_unless(ret == EOK,
220 "Failed to get attribute as string. Error %d.\n",
221 ret);
222 fail_unless(val_str != NULL,
223 "Failed to get attribute as string: was NULL.\n");
224
225 free(val_str);
226 }
227
228 ini_free_attribute_list(attributes);
229 }
230
231 ini_free_section_list(sections);
232 ini_config_file_destroy(file_ctx);
233 ini_config_destroy(ini_config);
234 }
235 }
236 END_TEST
237
238 /* Maybe we should test even bigger values? */
239 #define VALUE_LEN 10000
240 /* The +100 is space for section name and key name. */
241 #define CFGBUF_LEN (VALUE_LEN + 100)
242 START_TEST(test_ini_long_value)
243 {
244 int ret;
245 struct ini_cfgobj *ini_cfg;
246 struct ini_cfgfile *file_ctx;
247 struct value_obj *vo;
248 char big_val_cfg[CFGBUF_LEN] = {0};
249 char value[VALUE_LEN] = {0};
250 char *value_got;
251
252 /* The value is just a lot of As ending with '\0'*/
253 memset(value, 'A', VALUE_LEN - 1);
254
255 /* Create config file */
256 ret = snprintf(big_val_cfg, CFGBUF_LEN, "[section]\nkey=%s", value);
257
258 ret = ini_config_file_from_mem(big_val_cfg, strlen(big_val_cfg),
259 &file_ctx);
260 fail_unless(ret == EOK, "Failed to load config. Error %d.\n", ret);
261
262 ret = ini_config_create(&ini_cfg);
263 fail_unless(ret == EOK, "Failed to create config. Error %d.\n", ret);
264 ret = ini_config_parse(file_ctx, INI_STOP_ON_ERROR, INI_MV1S_ALLOW, 0,
265 ini_cfg);
266 fail_if(ret != 0, "Failed to parse config. Error %d.\n", ret);
267
268 ret = ini_get_config_valueobj("section", "key", ini_cfg,
269 INI_GET_FIRST_VALUE, &vo);
270 fail_unless(ret == EOK, "ini_get_config_valueobj returned %d\n: %s", ret,
271 strerror(ret));
272
273 value_got = ini_get_string_config_value(vo, &ret);
274 fail_unless(ret == EOK, "ini_get_int_config_value returned %d\n: %s", ret,
275 strerror(ret));
276
277 fail_unless(strcmp(value, value_got) == 0, "Expected and found values differ!\n");
278 free(value_got);
279 ini_config_destroy(ini_cfg);
280 ini_config_file_destroy(file_ctx);
281 }
282 END_TEST
283
284 static Suite *ini_parse_suite(void)
285 {
286 Suite *s = suite_create("ini_parse_suite");
287
288 TCase *tc_parse = tcase_create("ini_parse");
289 tcase_add_test(tc_parse, test_ini_parse_non_kvp);
290 tcase_add_test(tc_parse, test_ini_parse_section_key_conflict);
291 tcase_add_test(tc_parse, test_ini_long_value);
292
293 suite_add_tcase(s, tc_parse);
294
295 return s;
296 }
297
298 int main(void)
299 {
300 int number_failed;
301
302 Suite *s = ini_parse_suite();
303 SRunner *sr = srunner_create(s);
304 /* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
305 srunner_run_all(sr, CK_ENV);
306 number_failed = srunner_ntests_failed(sr);
307 srunner_free(sr);
308 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
309 }