File: | bld/../ini/ini_parse_ut.c |
Location: | line 1737, column 5 |
Description: | Value stored to 'logical' is never read |
1 | /* |
2 | INI LIBRARY |
3 | |
4 | Unit test for the parser object. |
5 | |
6 | Copyright (C) Dmitri Pal <dpal@redhat.com> 2010 |
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 | #include <stdio.h> |
24 | #include <string.h> |
25 | #include <errno(*__errno_location ()).h> |
26 | #include <stdlib.h> |
27 | #include <limits.h> |
28 | #include <sys/stat.h> |
29 | #include "ini_defines.h" |
30 | #include "ini_configobj.h" |
31 | #include "ini_config_priv.h" |
32 | #include "simplebuffer.h" |
33 | #include "path_utils.h" |
34 | #define TRACE_HOME |
35 | #include "trace.h" |
36 | #include "collection_tools.h" |
37 | |
38 | int verbose = 0; |
39 | char *confdir = NULL((void*)0); |
40 | |
41 | #define VAL_SIZE100 100 |
42 | |
43 | #define INIOUT(foo)do { if (verbose) foo; } while(0) \ |
44 | do { \ |
45 | if (verbose) foo; \ |
46 | } while(0) |
47 | |
48 | typedef int (*test_fn)(void); |
49 | |
50 | int test_one_file(const char *in_filename, |
51 | const char *out_filename) |
52 | { |
53 | int error = EOK0; |
54 | struct ini_cfgfile *file_ctx = NULL((void*)0); |
55 | FILE *ff = NULL((void*)0); |
56 | struct ini_cfgobj *ini_config = NULL((void*)0); |
57 | struct ini_cfgobj *ini_copy = NULL((void*)0); |
58 | char **error_list = NULL((void*)0); |
59 | struct simplebuffer *sbobj = NULL((void*)0); |
60 | uint32_t left = 0; |
61 | |
62 | INIOUT(printf("<==== Testing file %s ====>\n", in_filename))do { if (verbose) printf("<==== Testing file %s ====>\n" , in_filename); } while(0); |
63 | |
64 | /* Create config collection */ |
65 | error = ini_config_create(&ini_config); |
66 | if (error) { |
67 | printf("Failed to create collection. Error %d.\n", error); |
68 | return error; |
69 | } |
70 | |
71 | error = ini_config_file_open(in_filename, |
72 | INI_STOP_ON_NONE, |
73 | 0, /* TBD */ |
74 | 0, /* TBD */ |
75 | &file_ctx); |
76 | if (error) { |
77 | printf("Failed to open file %s for reading. Error %d.\n", |
78 | in_filename, error); |
79 | ini_config_destroy(ini_config); |
80 | return error; |
81 | } |
82 | |
83 | error = ini_config_parse(file_ctx, |
84 | ini_config); |
85 | if (error) { |
86 | INIOUT(printf("Failed to parse configuration. Error %d.\n", error))do { if (verbose) printf("Failed to parse configuration. Error %d.\n" , error); } while(0); |
87 | |
88 | if (ini_config_error_count(file_ctx)) { |
89 | INIOUT(printf("Errors detected while parsing: %s\n",do { if (verbose) printf("Errors detected while parsing: %s\n" , ini_config_get_filename(file_ctx)); } while(0) |
90 | ini_config_get_filename(file_ctx)))do { if (verbose) printf("Errors detected while parsing: %s\n" , ini_config_get_filename(file_ctx)); } while(0); |
91 | ini_config_get_errors(file_ctx, &error_list); |
92 | INIOUT(ini_config_print_errors(stdout, error_list))do { if (verbose) ini_config_print_errors(stdout, error_list) ; } while(0); |
93 | ini_config_free_errors(error_list); |
94 | } |
95 | /* We do not return here intentionally */ |
96 | } |
97 | |
98 | ini_config_file_destroy(file_ctx); |
99 | |
100 | INIOUT(col_debug_collection(ini_config->cfg, COL_TRAVERSE_DEFAULT))do { if (verbose) col_debug_collection(ini_config->cfg, 0x00000000 ); } while(0); |
101 | |
102 | /* Copy configuration */ |
103 | error = ini_config_copy(ini_config, &ini_copy); |
104 | if (error) { |
105 | printf("Failed to copy configuration. Error %d.\n", error); |
106 | ini_config_destroy(ini_config); |
107 | return error; |
108 | } |
109 | |
110 | ini_config_destroy(ini_config); |
111 | ini_config = ini_copy; |
112 | |
113 | INIOUT(col_debug_collection(ini_config->cfg, COL_TRAVERSE_DEFAULT))do { if (verbose) col_debug_collection(ini_config->cfg, 0x00000000 ); } while(0); |
114 | |
115 | error = ini_config_set_wrap(ini_config, 5); |
116 | if (error) { |
117 | printf("Failed to set custom wrapper. Error %d.\n", error); |
118 | ini_config_destroy(ini_config); |
119 | return error; |
120 | } |
121 | |
122 | error = simplebuffer_alloc(&sbobj); |
123 | if (error) { |
124 | TRACE_ERROR_NUMBER("Failed to allocate dynamic string.", error); |
125 | ini_config_destroy(ini_config); |
126 | return error; |
127 | } |
128 | |
129 | error = ini_config_serialize(ini_config, sbobj); |
130 | if (error != EOK0) { |
131 | printf("Failed to serialize configuration. Error %d.\n", error); |
132 | ini_config_destroy(ini_config); |
133 | simplebuffer_free(sbobj); |
134 | return error; |
135 | } |
136 | |
137 | errno(*__errno_location ()) = 0; |
138 | ff = fopen(out_filename, "w"); |
139 | if(!ff) { |
140 | error = errno(*__errno_location ()); |
141 | printf("Failed to open file [%s] for writing. Error %d.\n", |
142 | out_filename, error); |
143 | ini_config_destroy(ini_config); |
144 | simplebuffer_free(sbobj); |
145 | return error; |
146 | } |
147 | |
148 | /* Save */ |
149 | left = simplebuffer_get_len(sbobj); |
150 | while (left > 0) { |
151 | error = simplebuffer_write(fileno(ff), sbobj, &left); |
152 | if (error) { |
153 | printf("Failed to write back the configuration %d.\n", error); |
154 | simplebuffer_free(sbobj); |
155 | ini_config_destroy(ini_config); |
156 | fclose(ff); |
157 | return error; |
158 | } |
159 | } |
160 | |
161 | ini_config_destroy(ini_config); |
162 | simplebuffer_free(sbobj); |
163 | fclose(ff); |
164 | |
165 | return EOK0; |
166 | } |
167 | |
168 | |
169 | /* Run tests for multiple files */ |
170 | int read_save_test(void) |
171 | { |
172 | int error = EOK0; |
173 | int i = 0; |
174 | char infile[PATH_MAX4096]; |
175 | char outfile[PATH_MAX4096]; |
176 | char *srcdir = NULL((void*)0); |
177 | const char *files[] = { "real", |
178 | "mysssd", |
179 | "ipa", |
180 | "test", |
181 | "smerge", |
182 | NULL((void*)0) }; |
183 | |
184 | INIOUT(printf("<==== Read save test ====>\n"))do { if (verbose) printf("<==== Read save test ====>\n" ); } while(0); |
185 | |
186 | srcdir = getenv("srcdir"); |
187 | |
188 | while(files[i]) { |
189 | |
190 | snprintf(infile, PATH_MAX4096, "%s/ini/ini.d/%s.conf", |
191 | (srcdir == NULL((void*)0)) ? "." : srcdir, files[i]); |
192 | snprintf(outfile, PATH_MAX4096, "./%s.conf.out", files[i]); |
193 | error = test_one_file(infile, outfile); |
194 | INIOUT(printf("Test for file: %s returned %d\n", files[i], error))do { if (verbose) printf("Test for file: %s returned %d\n", files [i], error); } while(0); |
195 | i++; |
196 | } |
197 | |
198 | INIOUT(printf("<==== Read save test end ====>\n"))do { if (verbose) printf("<==== Read save test end ====>\n" ); } while(0); |
199 | |
200 | return EOK0; |
201 | } |
202 | |
203 | /* Run tests for multiple files */ |
204 | int read_again_test(void) |
205 | { |
206 | int error = EOK0; |
207 | int i = 0; |
208 | char infile[PATH_MAX4096]; |
209 | char outfile[PATH_MAX4096]; |
210 | char command[PATH_MAX4096 * 3]; |
211 | const char *files[] = { "real", |
212 | "mysssd", |
213 | "ipa", |
214 | "test", |
215 | "smerge", |
216 | NULL((void*)0) }; |
217 | |
218 | INIOUT(printf("<==== Read again test ====>\n"))do { if (verbose) printf("<==== Read again test ====>\n" ); } while(0); |
219 | |
220 | while(files[i]) { |
221 | |
222 | snprintf(infile, PATH_MAX4096, "./%s.conf.out", files[i]); |
223 | snprintf(outfile, PATH_MAX4096, "./%s.conf.2.out", files[i]); |
224 | error = test_one_file(infile, outfile); |
225 | INIOUT(printf("Test for file: %s returned %d\n", files[i], error))do { if (verbose) printf("Test for file: %s returned %d\n", files [i], error); } while(0); |
226 | if (error) break; |
227 | snprintf(command, PATH_MAX4096 * 3, "diff -q %s %s", infile, outfile); |
228 | error = system(command); |
229 | INIOUT(printf("Comparison of %s %s returned: %d\n",do { if (verbose) printf("Comparison of %s %s returned: %d\n" , infile, outfile, error); } while(0) |
230 | infile, outfile, error))do { if (verbose) printf("Comparison of %s %s returned: %d\n" , infile, outfile, error); } while(0); |
231 | if ((error) || (WEXITSTATUS(error)((((__extension__ (((union { __typeof(error) __in; int __i; } ) { .__in = (error) }).__i))) & 0xff00) >> 8))) { |
232 | printf("Failed to run copy command %d %d.\n", error, |
233 | WEXITSTATUS(error)((((__extension__ (((union { __typeof(error) __in; int __i; } ) { .__in = (error) }).__i))) & 0xff00) >> 8)); |
234 | error = -1; |
235 | break; |
236 | } |
237 | i++; |
238 | } |
239 | |
240 | INIOUT(printf("<==== Read again test end ====>\n"))do { if (verbose) printf("<==== Read again test end ====>\n" ); } while(0); |
241 | |
242 | return error; |
243 | } |
244 | |
245 | int create_expect(const char *checkname) |
246 | { |
247 | FILE *ff; |
248 | int error = EOK0; |
249 | |
250 | errno(*__errno_location ()) = 0; |
251 | ff = fopen(checkname, "w"); |
252 | if(!ff) { |
253 | error = errno(*__errno_location ()); |
254 | printf("Failed to open file %s for writing. Error %d.\n", |
255 | checkname, error); |
256 | return error; |
257 | } |
258 | |
259 | /* Ovewrite */ |
260 | fprintf(ff,"#Hoho section\n"); |
261 | fprintf(ff,"[hoho]\n"); |
262 | fprintf(ff,"#Hoho value\n"); |
263 | fprintf(ff,"val = hoho\n"); |
264 | fprintf(ff,"#End of hoho\n"); |
265 | fprintf(ff,"#Start of section\n"); |
266 | fprintf(ff,"[foo]\n"); |
267 | fprintf(ff,"#Second value\n"); |
268 | fprintf(ff,"bar = second value\n"); |
269 | fprintf(ff,"#End of section\n"); |
270 | /* Preserve */ |
271 | fprintf(ff,"#Hoho section\n"); |
272 | fprintf(ff,"[hoho]\n"); |
273 | fprintf(ff,"#Hoho value\n"); |
274 | fprintf(ff,"val = hoho\n"); |
275 | fprintf(ff,"#End of hoho\n"); |
276 | fprintf(ff,"#Start of section\n"); |
277 | fprintf(ff,"[foo]\n"); |
278 | fprintf(ff,"#First value\n"); |
279 | fprintf(ff,"bar = first value\n"); |
280 | fprintf(ff,"#End of section\n"); |
281 | /* Allow */ |
282 | fprintf(ff,"#Hoho section\n"); |
283 | fprintf(ff,"[hoho]\n"); |
284 | fprintf(ff,"#Hoho value\n"); |
285 | fprintf(ff,"val = hoho\n"); |
286 | fprintf(ff,"#End of hoho\n"); |
287 | fprintf(ff,"#Start of section\n"); |
288 | fprintf(ff,"[foo]\n"); |
289 | fprintf(ff,"#First value\n"); |
290 | fprintf(ff,"bar = first value\n"); |
291 | fprintf(ff,"#Second value\n"); |
292 | fprintf(ff,"bar = second value\n"); |
293 | fprintf(ff,"#End of section\n"); |
294 | /* Detect */ |
295 | fprintf(ff,"#Hoho section\n"); |
296 | fprintf(ff,"[hoho]\n"); |
297 | fprintf(ff,"#Hoho value\n"); |
298 | fprintf(ff,"val = hoho\n"); |
299 | fprintf(ff,"#End of hoho\n"); |
300 | fprintf(ff,"#Start of section\n"); |
301 | fprintf(ff,"[foo]\n"); |
302 | fprintf(ff,"#First value\n"); |
303 | fprintf(ff,"bar = first value\n"); |
304 | fprintf(ff,"#Second value\n"); |
305 | fprintf(ff,"bar = second value\n"); |
306 | fprintf(ff,"#End of section\n"); |
307 | |
308 | fclose(ff); |
309 | |
310 | return EOK0; |
311 | } |
312 | |
313 | /* Check merge modes */ |
314 | int merge_values_test(void) |
315 | { |
316 | int error = EOK0; |
317 | int i; |
318 | struct ini_cfgfile *file_ctx = NULL((void*)0); |
319 | FILE *ff = NULL((void*)0); |
320 | struct ini_cfgobj *ini_config = NULL((void*)0); |
321 | char **error_list = NULL((void*)0); |
322 | struct simplebuffer *sbobj = NULL((void*)0); |
323 | uint32_t left = 0; |
324 | uint32_t mflags[] = { INI_MV1S_OVERWRITE0x0000, |
325 | INI_MV1S_ERROR0x0001, |
326 | INI_MV1S_PRESERVE0x0002, |
327 | INI_MV1S_ALLOW0x0003, |
328 | INI_MV1S_DETECT0x0004 }; |
329 | |
330 | const char *mstr[] = { "OVERWRITE", |
331 | "ERROR", |
332 | "PRESERVE", |
333 | "ALLOW", |
334 | "DETECT" }; |
335 | |
336 | char filename[PATH_MAX4096]; |
337 | const char *resname = "./merge.conf.out"; |
338 | const char *checkname = "./expect.conf.out"; |
339 | char command[PATH_MAX4096 * 3]; |
340 | char *srcdir = NULL((void*)0); |
341 | |
342 | INIOUT(printf("<==== Merge values test ====>\n"))do { if (verbose) printf("<==== Merge values test ====>\n" ); } while(0); |
343 | |
344 | srcdir = getenv("srcdir"); |
345 | |
346 | snprintf(filename, PATH_MAX4096, "%s/ini/ini.d/foo.conf.in", |
347 | (srcdir == NULL((void*)0)) ? "." : srcdir); |
348 | |
349 | error = simplebuffer_alloc(&sbobj); |
350 | if (error) { |
351 | TRACE_ERROR_NUMBER("Failed to allocate dynamic string.", error); |
352 | return error; |
353 | } |
354 | |
355 | for (i = 0; i < 5; i++) { |
356 | |
357 | INIOUT(printf("<==== Testing mode %s ====>\n", mstr[i]))do { if (verbose) printf("<==== Testing mode %s ====>\n" , mstr[i]); } while(0); |
358 | |
359 | /* Create config collection */ |
360 | ini_config = NULL((void*)0); |
361 | error = ini_config_create(&ini_config); |
362 | if (error) { |
363 | printf("Failed to create collection. Error %d.\n", error); |
364 | simplebuffer_free(sbobj); |
365 | return error; |
366 | } |
367 | |
368 | file_ctx = NULL((void*)0); |
369 | error = ini_config_file_open(filename, |
370 | INI_STOP_ON_ANY, |
371 | mflags[i], |
372 | 0, /* TBD */ |
373 | &file_ctx); |
374 | if (error) { |
375 | printf("Failed to open file %s for reading. Error %d.\n", |
376 | filename, error); |
377 | printf("Src dir is [%s].\n", (srcdir == NULL((void*)0)) ? |
378 | "NOT DEFINED" : srcdir); |
379 | ini_config_destroy(ini_config); |
380 | simplebuffer_free(sbobj); |
381 | return error; |
382 | } |
383 | |
384 | error = ini_config_parse(file_ctx, |
385 | ini_config); |
386 | if (error) { |
387 | INIOUT(printf("Failed to parse configuration. Error %d.\n",do { if (verbose) printf("Failed to parse configuration. Error %d.\n" , error); } while(0) |
388 | error))do { if (verbose) printf("Failed to parse configuration. Error %d.\n" , error); } while(0); |
389 | |
390 | if (ini_config_error_count(file_ctx)) { |
391 | INIOUT(printf("Errors detected while parsing: %s\n",do { if (verbose) printf("Errors detected while parsing: %s\n" , ini_config_get_filename(file_ctx)); } while(0) |
392 | ini_config_get_filename(file_ctx)))do { if (verbose) printf("Errors detected while parsing: %s\n" , ini_config_get_filename(file_ctx)); } while(0); |
393 | ini_config_get_errors(file_ctx, &error_list); |
394 | INIOUT(ini_config_print_errors(stdout, error_list))do { if (verbose) ini_config_print_errors(stdout, error_list) ; } while(0); |
395 | ini_config_free_errors(error_list); |
396 | } |
397 | |
398 | if (((mflags[i] != INI_MV1S_ERROR0x0001) && |
399 | (mflags[i]!= INI_MV1S_DETECT0x0004)) || |
400 | ((mflags[i] = INI_MV1S_ERROR0x0001) && (error != EEXIST17)) || |
401 | ((mflags[i] = INI_MV1S_DETECT0x0004) && (error != EEXIST17))) { |
402 | printf("This is unexpected error %d in mode %d\n", |
403 | error, mflags[i]); |
404 | ini_config_destroy(ini_config); |
405 | simplebuffer_free(sbobj); |
406 | ini_config_file_destroy(file_ctx); |
407 | return error; |
408 | } |
409 | /* We do not return here intentionally */ |
410 | } |
411 | |
412 | ini_config_file_destroy(file_ctx); |
413 | |
414 | INIOUT(col_debug_collection(ini_config->cfg, COL_TRAVERSE_DEFAULT))do { if (verbose) col_debug_collection(ini_config->cfg, 0x00000000 ); } while(0); |
415 | |
416 | error = ini_config_serialize(ini_config, sbobj); |
417 | if (error != EOK0) { |
418 | printf("Failed to serialize configuration. Error %d.\n", error); |
419 | ini_config_destroy(ini_config); |
420 | simplebuffer_free(sbobj); |
421 | return error; |
422 | } |
423 | |
424 | ini_config_destroy(ini_config); |
425 | } |
426 | |
427 | errno(*__errno_location ()) = 0; |
428 | ff = fopen(resname, "w"); |
429 | if(!ff) { |
430 | error = errno(*__errno_location ()); |
431 | printf("Failed to open file for writing. Error %d.\n", error); |
432 | simplebuffer_free(sbobj); |
433 | return error; |
434 | } |
435 | |
436 | /* Save */ |
437 | left = simplebuffer_get_len(sbobj); |
438 | while (left > 0) { |
439 | error = simplebuffer_write(fileno(ff), sbobj, &left); |
440 | if (error) { |
441 | printf("Failed to write back the configuration %d.\n", error); |
442 | simplebuffer_free(sbobj); |
443 | fclose(ff); |
444 | return error; |
445 | } |
446 | } |
447 | |
448 | simplebuffer_free(sbobj); |
449 | fclose(ff); |
450 | |
451 | error = create_expect(checkname); |
452 | if (error) { |
453 | printf("Failed to create file with expected contents %d.\n", error); |
454 | return error; |
455 | } |
456 | |
457 | snprintf(command, PATH_MAX4096 * 3, "diff -q %s %s", resname, checkname); |
458 | error = system(command); |
459 | INIOUT(printf("Comparison of %s %s returned: %d\n",do { if (verbose) printf("Comparison of %s %s returned: %d\n" , resname, checkname, error); } while(0) |
460 | resname, checkname, error))do { if (verbose) printf("Comparison of %s %s returned: %d\n" , resname, checkname, error); } while(0); |
461 | if ((error) || (WEXITSTATUS(error)((((__extension__ (((union { __typeof(error) __in; int __i; } ) { .__in = (error) }).__i))) & 0xff00) >> 8))) { |
462 | printf("Failed to run copy command %d %d.\n", error, |
463 | WEXITSTATUS(error)((((__extension__ (((union { __typeof(error) __in; int __i; } ) { .__in = (error) }).__i))) & 0xff00) >> 8)); |
464 | return -1; |
465 | } |
466 | |
467 | INIOUT(printf("<==== Merge values test end ====>\n"))do { if (verbose) printf("<==== Merge values test end ====>\n" ); } while(0); |
468 | |
469 | return error; |
470 | } |
471 | |
472 | /* Check merge modes */ |
473 | int merge_section_test(void) |
474 | { |
475 | int error = EOK0; |
476 | int i, j; |
477 | struct ini_cfgfile *file_ctx = NULL((void*)0); |
478 | FILE *ff = NULL((void*)0); |
479 | struct ini_cfgobj *ini_config = NULL((void*)0); |
480 | char **error_list = NULL((void*)0); |
481 | struct simplebuffer *sbobj = NULL((void*)0); |
482 | uint32_t left = 0; |
483 | uint32_t msecflags[] = { INI_MS_MERGE0x0000, |
484 | INI_MS_ERROR0x0100, |
485 | INI_MS_OVERWRITE0x0200, |
486 | INI_MS_PRESERVE0x0300, |
487 | INI_MS_DETECT0x0400 }; |
488 | |
489 | uint32_t mflags[] = { INI_MV2S_OVERWRITE0x0000, |
490 | INI_MV2S_ERROR0x0010, |
491 | INI_MV2S_PRESERVE0x0020, |
492 | INI_MV2S_ALLOW0x0030, |
493 | INI_MV2S_DETECT0x0040 }; |
494 | |
495 | const char *secmstr[] = { "MERGE", |
496 | "ERROR", |
497 | "OVERWRITE", |
498 | "PRESERVE", |
499 | "DETECT" }; |
500 | |
501 | const char *mstr[] = { "OVERWRITE", |
502 | "ERROR", |
503 | "PRESERVE", |
504 | "ALLOW", |
505 | "DETECT" }; |
506 | |
507 | char filename[PATH_MAX4096]; |
508 | char checkname[PATH_MAX4096]; |
509 | char resname[PATH_MAX4096]; |
510 | char command[PATH_MAX4096 * 3]; |
511 | char mode[VAL_SIZE100]; |
512 | char *srcdir = NULL((void*)0); |
513 | char *builddir = NULL((void*)0); |
514 | |
515 | |
516 | INIOUT(printf("<==== Merge section test ====>\n"))do { if (verbose) printf("<==== Merge section test ====>\n" ); } while(0); |
517 | |
518 | srcdir = getenv("srcdir"); |
519 | builddir = getenv("builddir"); |
520 | snprintf(filename, PATH_MAX4096, "%s/ini/ini.d/smerge.conf", |
521 | (srcdir == NULL((void*)0)) ? "." : srcdir); |
522 | snprintf(checkname, PATH_MAX4096, "%s/ini/ini.d/sexpect.conf", |
523 | (srcdir == NULL((void*)0)) ? "." : srcdir); |
524 | snprintf(resname, PATH_MAX4096, "%s/smerge.conf.out", |
525 | (builddir == NULL((void*)0)) ? "." : builddir); |
526 | |
527 | error = simplebuffer_alloc(&sbobj); |
528 | if (error) { |
529 | TRACE_ERROR_NUMBER("Failed to allocate dynamic string.", error); |
530 | return error; |
531 | } |
532 | |
533 | for (i = 0; i < 5; i++) { |
534 | for (j = 0; j < 5; j++) { |
535 | |
536 | INIOUT(printf("<==== Testing mode %s + %s ====>\n",do { if (verbose) printf("<==== Testing mode %s + %s ====>\n" , secmstr[i], mstr[j]); } while(0) |
537 | secmstr[i], mstr[j]))do { if (verbose) printf("<==== Testing mode %s + %s ====>\n" , secmstr[i], mstr[j]); } while(0); |
538 | |
539 | snprintf(mode, VAL_SIZE100, "# Section mode: %s, value mode: %s\n", |
540 | secmstr[i], mstr[j]); |
541 | |
542 | error = simplebuffer_add_str(sbobj, |
543 | mode, |
544 | strlen(mode), |
545 | VAL_SIZE100); |
546 | if (error) { |
547 | TRACE_ERROR_NUMBER("Failed to add string.", |
548 | error); |
549 | simplebuffer_free(sbobj); |
550 | return error; |
551 | } |
552 | |
553 | /* Create config collection */ |
554 | ini_config = NULL((void*)0); |
555 | error = ini_config_create(&ini_config); |
556 | if (error) { |
557 | printf("Failed to create collection. " |
558 | "Error %d.\n", error); |
559 | simplebuffer_free(sbobj); |
560 | return error; |
561 | } |
562 | |
563 | file_ctx = NULL((void*)0); |
564 | error = ini_config_file_open(filename, |
565 | INI_STOP_ON_ANY, |
566 | msecflags[i] | mflags[j], |
567 | 0, /* TBD */ |
568 | &file_ctx); |
569 | if (error) { |
570 | printf("Failed to open file %s for reading. " |
571 | "Error %d.\n", filename, error); |
572 | printf("Source is %s.\n", (srcdir == NULL((void*)0)) ? |
573 | "NOT Defined" : srcdir); |
574 | ini_config_destroy(ini_config); |
575 | simplebuffer_free(sbobj); |
576 | return error; |
577 | } |
578 | |
579 | error = ini_config_parse(file_ctx, |
580 | ini_config); |
581 | if (error) { |
582 | INIOUT(printf("Failed to parse configuration. "do { if (verbose) printf("Failed to parse configuration. " "Error %d.\n" , error); } while(0) |
583 | "Error %d.\n", error))do { if (verbose) printf("Failed to parse configuration. " "Error %d.\n" , error); } while(0); |
584 | |
585 | if (ini_config_error_count(file_ctx)) { |
586 | INIOUT(printf("Errors detected while parsing: %s\n",do { if (verbose) printf("Errors detected while parsing: %s\n" , ini_config_get_filename(file_ctx)); } while(0) |
587 | ini_config_get_filename(file_ctx)))do { if (verbose) printf("Errors detected while parsing: %s\n" , ini_config_get_filename(file_ctx)); } while(0); |
588 | ini_config_get_errors(file_ctx, &error_list); |
589 | INIOUT(ini_config_print_errors(stdout, error_list))do { if (verbose) ini_config_print_errors(stdout, error_list) ; } while(0); |
590 | ini_config_free_errors(error_list); |
591 | } |
592 | |
593 | if (((msecflags[i] == INI_MS_ERROR0x0100) && |
594 | (error == EEXIST17)) || |
595 | ((msecflags[i] == INI_MS_DETECT0x0400) && |
596 | (error == EEXIST17)) || |
597 | ((msecflags[i] == INI_MS_MERGE0x0000) && |
598 | ((mflags[j] == INI_MV2S_ERROR0x0010) || |
599 | (mflags[j] == INI_MV2S_DETECT0x0040)) && |
600 | (error == EEXIST17))) { |
601 | INIOUT(printf("This is an expected error "do { if (verbose) printf("This is an expected error " "%d in mode %d + %d\n" , error, msecflags[i], mflags[j]); } while(0) |
602 | "%d in mode %d + %d\n",do { if (verbose) printf("This is an expected error " "%d in mode %d + %d\n" , error, msecflags[i], mflags[j]); } while(0) |
603 | error,do { if (verbose) printf("This is an expected error " "%d in mode %d + %d\n" , error, msecflags[i], mflags[j]); } while(0) |
604 | msecflags[i],do { if (verbose) printf("This is an expected error " "%d in mode %d + %d\n" , error, msecflags[i], mflags[j]); } while(0) |
605 | mflags[j]))do { if (verbose) printf("This is an expected error " "%d in mode %d + %d\n" , error, msecflags[i], mflags[j]); } while(0); |
606 | /* We do not return here intentionally */ |
607 | } |
608 | else { |
609 | printf("This is unexpected error %d in mode %d + %d\n", |
610 | error, msecflags[i], mflags[j]); |
611 | ini_config_destroy(ini_config); |
612 | simplebuffer_free(sbobj); |
613 | ini_config_file_destroy(file_ctx); |
614 | return error; |
615 | } |
616 | } |
617 | |
618 | ini_config_file_destroy(file_ctx); |
619 | |
620 | INIOUT(col_debug_collection(ini_config->cfg,do { if (verbose) col_debug_collection(ini_config->cfg, 0x00000000 ); } while(0) |
621 | COL_TRAVERSE_DEFAULT))do { if (verbose) col_debug_collection(ini_config->cfg, 0x00000000 ); } while(0); |
622 | |
623 | error = ini_config_serialize(ini_config, sbobj); |
624 | if (error != EOK0) { |
625 | printf("Failed to serialize configuration. " |
626 | "Error %d.\n", error); |
627 | ini_config_destroy(ini_config); |
628 | simplebuffer_free(sbobj); |
629 | return error; |
630 | } |
631 | |
632 | ini_config_destroy(ini_config); |
633 | } |
634 | } |
635 | |
636 | errno(*__errno_location ()) = 0; |
637 | ff = fopen(resname, "w"); |
638 | if(!ff) { |
639 | error = errno(*__errno_location ()); |
640 | printf("Failed to open file for writing. Error %d.\n", error); |
641 | simplebuffer_free(sbobj); |
642 | return error; |
643 | } |
644 | |
645 | /* Save */ |
646 | left = simplebuffer_get_len(sbobj); |
647 | while (left > 0) { |
648 | error = simplebuffer_write(fileno(ff), sbobj, &left); |
649 | if (error) { |
650 | printf("Failed to write back the configuration %d.\n", error); |
651 | simplebuffer_free(sbobj); |
652 | fclose(ff); |
653 | return error; |
654 | } |
655 | } |
656 | |
657 | simplebuffer_free(sbobj); |
658 | fclose(ff); |
659 | |
660 | snprintf(command, PATH_MAX4096 * 3, "diff -q %s %s", resname, checkname); |
661 | error = system(command); |
662 | INIOUT(printf("Comparison of %s %s returned: %d\n",do { if (verbose) printf("Comparison of %s %s returned: %d\n" , resname, checkname, error); } while(0) |
663 | resname, checkname, error))do { if (verbose) printf("Comparison of %s %s returned: %d\n" , resname, checkname, error); } while(0); |
664 | |
665 | if ((error) || (WEXITSTATUS(error)((((__extension__ (((union { __typeof(error) __in; int __i; } ) { .__in = (error) }).__i))) & 0xff00) >> 8))) { |
666 | printf("Failed to run diff command %d %d.\n", error, |
667 | WEXITSTATUS(error)((((__extension__ (((union { __typeof(error) __in; int __i; } ) { .__in = (error) }).__i))) & 0xff00) >> 8)); |
668 | return -1; |
669 | } |
670 | |
671 | INIOUT(printf("<==== Merge section test end ====>\n"))do { if (verbose) printf("<==== Merge section test end ====>\n" ); } while(0); |
672 | |
673 | return error; |
674 | } |
675 | |
676 | int read_one_file(char *name, |
677 | struct ini_cfgobj *ini_config, |
678 | uint32_t collision_flags) |
679 | { |
680 | int error = EOK0; |
681 | struct ini_cfgfile *file_ctx = NULL((void*)0); |
682 | char **error_list = NULL((void*)0); |
683 | |
684 | INIOUT(printf("Reading file %s\n", name))do { if (verbose) printf("Reading file %s\n", name); } while( 0); |
685 | |
686 | file_ctx = NULL((void*)0); |
687 | error = ini_config_file_open(name, |
688 | INI_STOP_ON_ANY, |
689 | collision_flags, |
690 | 0, |
691 | &file_ctx); |
692 | if (error) { |
693 | printf("Failed to open file %s for reading. " |
694 | "Error %d.\n", name, error); |
695 | return error; |
696 | } |
697 | |
698 | INIOUT(printf("Parsing file %s\n", name))do { if (verbose) printf("Parsing file %s\n", name); } while( 0); |
699 | |
700 | error = ini_config_parse(file_ctx, ini_config); |
701 | if (error) { |
702 | INIOUT(printf("Failed to parse configuration. "do { if (verbose) printf("Failed to parse configuration. " "Error %d.\n" , error); } while(0) |
703 | "Error %d.\n", error))do { if (verbose) printf("Failed to parse configuration. " "Error %d.\n" , error); } while(0); |
704 | |
705 | if (ini_config_error_count(file_ctx)) { |
706 | INIOUT(printf("Errors detected while parsing: %s\n",do { if (verbose) printf("Errors detected while parsing: %s\n" , ini_config_get_filename(file_ctx)); } while(0) |
707 | ini_config_get_filename(file_ctx)))do { if (verbose) printf("Errors detected while parsing: %s\n" , ini_config_get_filename(file_ctx)); } while(0); |
708 | ini_config_get_errors(file_ctx, &error_list); |
709 | INIOUT(ini_config_print_errors(stdout, error_list))do { if (verbose) ini_config_print_errors(stdout, error_list) ; } while(0); |
710 | ini_config_free_errors(error_list); |
711 | } |
712 | ini_config_file_destroy(file_ctx); |
713 | return error; |
714 | } |
715 | |
716 | ini_config_file_destroy(file_ctx); |
717 | |
718 | INIOUT(printf("Successfully parsed file %s\n", name))do { if (verbose) printf("Successfully parsed file %s\n", name ); } while(0); |
719 | return EOK0; |
720 | } |
721 | |
722 | /* Check merge modes */ |
723 | int merge_file_test(void) |
724 | { |
725 | int error = EOK0; |
726 | int i, j; |
727 | struct ini_cfgobj *ini_config_first = NULL((void*)0); |
728 | struct ini_cfgobj *ini_config_second = NULL((void*)0); |
729 | struct ini_cfgobj *ini_config_result = NULL((void*)0); |
730 | struct simplebuffer *sbobj = NULL((void*)0); |
731 | uint32_t left = 0; |
732 | FILE *ff = NULL((void*)0); |
733 | uint32_t msecflags[] = { INI_MS_MERGE0x0000, |
734 | INI_MS_ERROR0x0100, |
735 | INI_MS_OVERWRITE0x0200, |
736 | INI_MS_PRESERVE0x0300, |
737 | INI_MS_DETECT0x0400 }; |
738 | |
739 | uint32_t m2flags[] = { INI_MV2S_OVERWRITE0x0000, |
740 | INI_MV2S_ERROR0x0010, |
741 | INI_MV2S_PRESERVE0x0020, |
742 | INI_MV2S_ALLOW0x0030, |
743 | INI_MV2S_DETECT0x0040 }; |
744 | |
745 | uint32_t m1flags[] = { INI_MV1S_OVERWRITE0x0000, |
746 | INI_MV1S_ERROR0x0001, |
747 | INI_MV1S_PRESERVE0x0002, |
748 | INI_MV1S_ALLOW0x0003, |
749 | INI_MV1S_DETECT0x0004 }; |
750 | |
751 | const char *secmstr[] = { "MERGE", |
752 | "ERROR", |
753 | "OVERWRITE", |
754 | "PRESERVE", |
755 | "DETECT" }; |
756 | |
757 | const char *mstr[] = { "OVERWRITE", |
758 | "ERROR", |
759 | "PRESERVE", |
760 | "ALLOW", |
761 | "DETECT" }; |
762 | |
763 | char firstname[PATH_MAX4096]; |
764 | char secondname[PATH_MAX4096]; |
765 | char resname[PATH_MAX4096]; |
766 | char checkname[PATH_MAX4096]; |
767 | char command[PATH_MAX4096 * 3]; |
768 | char msg[VAL_SIZE100]; |
769 | char mode[VAL_SIZE100]; |
770 | char *srcdir = NULL((void*)0); |
771 | char *builddir = NULL((void*)0); |
772 | uint32_t collision_flags; |
773 | uint32_t ms_subst; |
774 | uint32_t mv1s_subst; |
775 | uint32_t mv2s_subst; |
776 | |
777 | INIOUT(printf("<==== Merge file test ====>\n"))do { if (verbose) printf("<==== Merge file test ====>\n" ); } while(0); |
778 | |
779 | srcdir = getenv("srcdir"); |
780 | builddir = getenv("builddir"); |
781 | snprintf(firstname, PATH_MAX4096, "%s/ini/ini.d/first.conf", |
782 | (srcdir == NULL((void*)0)) ? "." : srcdir); |
783 | |
784 | snprintf(secondname, PATH_MAX4096, "%s/ini/ini.d/second.conf", |
785 | (srcdir == NULL((void*)0)) ? "." : srcdir); |
786 | |
787 | snprintf(checkname, PATH_MAX4096, "%s/ini/ini.d/mergecheck.conf", |
788 | (srcdir == NULL((void*)0)) ? "." : srcdir); |
789 | |
790 | snprintf(resname, PATH_MAX4096, "%s/mergecheck.conf.out", |
791 | (builddir == NULL((void*)0)) ? "." : builddir); |
792 | |
793 | error = simplebuffer_alloc(&sbobj); |
794 | if (error) { |
795 | TRACE_ERROR_NUMBER("Failed to allocate dynamic string.", error); |
796 | return error; |
797 | } |
798 | |
799 | for (i = 0; i < 5; i++) { |
800 | for (j = 0; j < 5; j++) { |
801 | |
802 | INIOUT(printf("<==== Testing mode %s + %s ====>\n",do { if (verbose) printf("<==== Testing mode %s + %s ====>\n" , secmstr[i], mstr[j]); } while(0) |
803 | secmstr[i], mstr[j]))do { if (verbose) printf("<==== Testing mode %s + %s ====>\n" , secmstr[i], mstr[j]); } while(0); |
804 | |
805 | snprintf(mode, VAL_SIZE100, "# Section mode: %s, value mode: %s\n", |
806 | secmstr[i], mstr[j]); |
807 | |
808 | error = simplebuffer_add_str(sbobj, |
809 | mode, |
810 | strlen(mode), |
811 | VAL_SIZE100); |
812 | if (error) { |
813 | TRACE_ERROR_NUMBER("Failed to add string.", |
814 | error); |
815 | simplebuffer_free(sbobj); |
816 | return error; |
817 | } |
818 | |
819 | /* Create first config collection */ |
820 | ini_config_first = NULL((void*)0); |
821 | error = ini_config_create(&ini_config_first); |
822 | if (error) { |
823 | printf("Failed to create collection. " |
824 | "Error %d.\n", error); |
825 | simplebuffer_free(sbobj); |
826 | return error; |
827 | } |
828 | |
829 | /* Create second config collection */ |
830 | ini_config_second = NULL((void*)0); |
831 | error = ini_config_create(&ini_config_second); |
832 | if (error) { |
833 | printf("Failed to create collection. " |
834 | "Error %d.\n", error); |
835 | ini_config_destroy(ini_config_first); |
836 | simplebuffer_free(sbobj); |
837 | return error; |
838 | } |
839 | |
840 | /* IMPORTANT: Use same collision flags for reading |
841 | * of the files and then merging. |
842 | * Mixing the flags would lead to strange results |
843 | * that would be hard to debug. |
844 | */ |
845 | /* However here for purely testing purposes |
846 | * we will not use error modes in parsing |
847 | * otherwise we will not be able to try to merge. |
848 | * Instead we replace the error and detect modes |
849 | * with allow or merge mode. |
850 | */ |
851 | /* The test actually does not fail in the case of |
852 | * PRESERVE + ERROR becuase it should fail at |
853 | * the stage of reading file but we suppress |
854 | * it so we can try the merge. |
855 | * As a result the mode PRESERVE + ERROR |
856 | * acts as PRESERVE + ALLOW and does not return an error. |
857 | * The same thing happens with PRESERVE + DETECT mode. |
858 | * It might be confusing if someone tries to decipher |
859 | * the tests, so this comment should help. |
860 | */ |
861 | if ((msecflags[i] == INI_MS_ERROR0x0100) || |
862 | (msecflags[i] == INI_MS_DETECT0x0400)) { |
863 | ms_subst = msecflags[i]; |
864 | } |
865 | else { |
866 | ms_subst = INI_MS_MERGE0x0000; |
867 | } |
868 | |
869 | if ((m2flags[j] == INI_MV2S_ERROR0x0010) || |
870 | (m2flags[j] == INI_MV2S_DETECT0x0040)) { |
871 | mv1s_subst = INI_MV1S_ALLOW0x0003; |
872 | mv2s_subst = INI_MV2S_ALLOW0x0030; |
873 | } |
874 | else { |
875 | mv1s_subst = m1flags[j]; |
876 | mv2s_subst = m2flags[j]; |
877 | } |
878 | |
879 | collision_flags = mv1s_subst | mv2s_subst | ms_subst; |
880 | |
881 | error = read_one_file(firstname, |
882 | ini_config_first, |
883 | collision_flags); |
884 | if (error) { |
885 | printf("Failed to read %s. " |
886 | "Error %d.\n", firstname, error); |
887 | printf("Source is %s.\n", (srcdir == NULL((void*)0)) ? |
888 | "NOT Defined" : srcdir); |
889 | ini_config_destroy(ini_config_first); |
890 | ini_config_destroy(ini_config_second); |
891 | simplebuffer_free(sbobj); |
892 | return error; |
893 | } |
894 | |
895 | error = read_one_file(secondname, |
896 | ini_config_second, |
897 | collision_flags); |
898 | if (error) { |
899 | printf("Failed to read %s. " |
900 | "Error %d.\n", secondname, error); |
901 | printf("Source is %s.\n", (srcdir == NULL((void*)0)) ? |
902 | "NOT Defined" : srcdir); |
903 | ini_config_destroy(ini_config_first); |
904 | ini_config_destroy(ini_config_second); |
905 | simplebuffer_free(sbobj); |
906 | return error; |
907 | } |
908 | |
909 | INIOUT(col_debug_collection(ini_config_first->cfg,do { if (verbose) col_debug_collection(ini_config_first->cfg , 0x00000001); } while(0) |
910 | COL_TRAVERSE_ONELEVEL))do { if (verbose) col_debug_collection(ini_config_first->cfg , 0x00000001); } while(0); |
911 | INIOUT(col_debug_collection(ini_config_second->cfg,do { if (verbose) col_debug_collection(ini_config_second-> cfg, 0x00000001); } while(0) |
912 | COL_TRAVERSE_ONELEVEL))do { if (verbose) col_debug_collection(ini_config_second-> cfg, 0x00000001); } while(0); |
913 | |
914 | ini_config_result = NULL((void*)0); |
915 | error = ini_config_merge(ini_config_first, |
916 | ini_config_second, |
917 | msecflags[i] | m2flags[j] | m1flags[j], |
918 | &ini_config_result); |
919 | if (error) { |
920 | if ((error == EEXIST17) && |
921 | ((msecflags[i] == INI_MS_ERROR0x0100) || |
922 | (m2flags[j] == INI_MV2S_ERROR0x0010))) { |
923 | snprintf(msg, sizeof(msg) -1, |
924 | "# This is an expected error " |
925 | "%d in mode %d + %d + %d\n\n", |
926 | error, |
927 | msecflags[i], |
928 | m2flags[j], |
929 | m1flags[j]); |
930 | INIOUT(printf(msg))do { if (verbose) printf(msg); } while(0); |
931 | error = simplebuffer_add_str(sbobj, |
932 | msg, |
933 | strlen(msg), |
934 | VAL_SIZE100); |
935 | if (error) { |
936 | TRACE_ERROR_NUMBER("Failed to add string.", |
937 | error); |
938 | ini_config_destroy(ini_config_first); |
939 | ini_config_destroy(ini_config_second); |
940 | simplebuffer_free(sbobj); |
941 | return error; |
942 | } |
943 | |
944 | ini_config_destroy(ini_config_first); |
945 | ini_config_destroy(ini_config_second); |
946 | continue; |
947 | } |
948 | else if ((error == EEXIST17) && |
949 | ((msecflags[i] == INI_MS_DETECT0x0400) || |
950 | ((msecflags[i] != INI_MS_ERROR0x0100) && |
951 | (m2flags[j] == INI_MV2S_DETECT0x0040)))) { |
952 | snprintf(msg, sizeof(msg) -1, |
953 | "# This is an expected error " |
954 | "%d in mode %d + %d + %d\n\n", |
955 | error, |
956 | msecflags[i], |
957 | m2flags[j], |
958 | m1flags[j]); |
959 | INIOUT(printf(msg))do { if (verbose) printf(msg); } while(0); |
960 | error = simplebuffer_add_str(sbobj, |
961 | msg, |
962 | strlen(msg), |
963 | VAL_SIZE100); |
964 | if (error) { |
965 | TRACE_ERROR_NUMBER("Failed to add string.", |
966 | error); |
967 | ini_config_destroy(ini_config_first); |
968 | ini_config_destroy(ini_config_second); |
969 | simplebuffer_free(sbobj); |
970 | return error; |
971 | } |
972 | /* Falling throught here */ |
973 | } |
974 | else { |
975 | TRACE_ERROR_NUMBER("Failed to merge.", |
976 | error); |
977 | ini_config_destroy(ini_config_first); |
978 | ini_config_destroy(ini_config_second); |
979 | simplebuffer_free(sbobj); |
980 | return error; |
981 | } |
982 | } |
983 | |
984 | INIOUT(col_debug_collection(ini_config_result->cfg,do { if (verbose) col_debug_collection(ini_config_result-> cfg, 0x00000000); } while(0) |
985 | COL_TRAVERSE_DEFAULT))do { if (verbose) col_debug_collection(ini_config_result-> cfg, 0x00000000); } while(0); |
986 | |
987 | error = ini_config_serialize(ini_config_result, sbobj); |
988 | if (error) { |
989 | printf("Failed to serialize configuration. " |
990 | "Error %d.\n", error); |
991 | ini_config_destroy(ini_config_first); |
992 | ini_config_destroy(ini_config_second); |
993 | ini_config_destroy(ini_config_result); |
994 | simplebuffer_free(sbobj); |
995 | return error; |
996 | } |
997 | |
998 | ini_config_destroy(ini_config_first); |
999 | ini_config_destroy(ini_config_second); |
1000 | ini_config_destroy(ini_config_result); |
1001 | } |
1002 | } |
1003 | |
1004 | errno(*__errno_location ()) = 0; |
1005 | ff = fopen(resname, "w"); |
1006 | if(!ff) { |
1007 | error = errno(*__errno_location ()); |
1008 | printf("Failed to open file for writing. Error %d.\n", error); |
1009 | simplebuffer_free(sbobj); |
1010 | return error; |
1011 | } |
1012 | |
1013 | /* Save */ |
1014 | left = simplebuffer_get_len(sbobj); |
1015 | while (left > 0) { |
1016 | error = simplebuffer_write(fileno(ff), sbobj, &left); |
1017 | if (error) { |
1018 | printf("Failed to write back the configuration %d.\n", error); |
1019 | simplebuffer_free(sbobj); |
1020 | fclose(ff); |
1021 | return error; |
1022 | } |
1023 | } |
1024 | |
1025 | simplebuffer_free(sbobj); |
1026 | fclose(ff); |
1027 | |
1028 | snprintf(command,PATH_MAX4096 * 3, "diff -q %s %s", resname, checkname); |
1029 | error = system(command); |
1030 | INIOUT(printf("Comparison of %s %s returned: %d\n",do { if (verbose) printf("Comparison of %s %s returned: %d\n" , resname, checkname, error); } while(0) |
1031 | resname, checkname, error))do { if (verbose) printf("Comparison of %s %s returned: %d\n" , resname, checkname, error); } while(0); |
1032 | |
1033 | if ((error) || (WEXITSTATUS(error)((((__extension__ (((union { __typeof(error) __in; int __i; } ) { .__in = (error) }).__i))) & 0xff00) >> 8))) { |
1034 | printf("Failed to run diff command %d %d.\n", error, |
1035 | WEXITSTATUS(error)((((__extension__ (((union { __typeof(error) __in; int __i; } ) { .__in = (error) }).__i))) & 0xff00) >> 8)); |
1036 | return -1; |
1037 | } |
1038 | |
1039 | INIOUT(printf("<==== Merge section file end ====>\n"))do { if (verbose) printf("<==== Merge section file end ====>\n" ); } while(0); |
1040 | |
1041 | return EOK0; |
1042 | } |
1043 | |
1044 | int startup_test(void) |
1045 | { |
1046 | int error = EOK0; |
1047 | struct ini_cfgfile *file_ctx = NULL((void*)0); |
1048 | struct ini_cfgobj *ini_config = NULL((void*)0); |
1049 | char **error_list = NULL((void*)0); |
1050 | char infile[PATH_MAX4096]; |
1051 | char outfile[PATH_MAX4096]; |
1052 | char command[PATH_MAX4096 * 3]; |
1053 | char *srcdir = NULL((void*)0); |
1054 | char *builddir; |
1055 | |
1056 | INIOUT(printf("<==== Startup test ====>\n"))do { if (verbose) printf("<==== Startup test ====>\n"); } while(0); |
1057 | |
1058 | srcdir = getenv("srcdir"); |
1059 | snprintf(infile, PATH_MAX4096, "%s/ini/ini.d/foo.conf.in", |
1060 | (srcdir == NULL((void*)0)) ? "." : srcdir); |
1061 | builddir = getenv("builddir"); |
1062 | snprintf(outfile, PATH_MAX4096, "%s/foo.conf", |
1063 | (builddir == NULL((void*)0)) ? "." : builddir); |
1064 | |
1065 | snprintf(command, PATH_MAX4096 * 3, "cp %s %s", infile, outfile); |
1066 | INIOUT(printf("Running command '%s'\n", command))do { if (verbose) printf("Running command '%s'\n", command); } while(0); |
1067 | |
1068 | error = system(command); |
1069 | if ((error) || (WEXITSTATUS(error)((((__extension__ (((union { __typeof(error) __in; int __i; } ) { .__in = (error) }).__i))) & 0xff00) >> 8))) { |
1070 | printf("Failed to run copy command %d %d.\n", error, |
1071 | WEXITSTATUS(error)((((__extension__ (((union { __typeof(error) __in; int __i; } ) { .__in = (error) }).__i))) & 0xff00) >> 8)); |
1072 | return -1; |
1073 | } |
1074 | |
1075 | INIOUT(printf("Running chmod 660 on file '%s'\n", outfile))do { if (verbose) printf("Running chmod 660 on file '%s'\n", outfile ); } while(0); |
1076 | error = chmod(outfile, S_IRUSR0400 | S_IWUSR0200); |
1077 | if(error) { |
1078 | error = errno(*__errno_location ()); |
1079 | printf("Failed to run chmod command %d.\n", error); |
1080 | return error; |
1081 | } |
1082 | |
1083 | /* Open config file */ |
1084 | error = ini_config_file_open(outfile, |
1085 | INI_STOP_ON_NONE, |
1086 | 0, |
1087 | INI_META_STATS1, |
1088 | &file_ctx); |
1089 | if (error) { |
1090 | printf("Failed to open file %s for reading. Error %d.\n", |
1091 | outfile, error); |
1092 | return error; |
1093 | } |
1094 | |
1095 | /* We will check just permissions here. */ |
1096 | error = ini_config_access_check(file_ctx, |
1097 | INI_ACCESS_CHECK_MODE0x00000001, /* add uid & gui flags |
1098 | * in real case |
1099 | */ |
1100 | 0, /* <- will be real uid in real case */ |
1101 | 0, /* <- will be real gid in real case */ |
1102 | 0440, /* Checking for r--r----- */ |
1103 | 0); |
1104 | /* This check is expected to fail since |
1105 | * the actual permissions on the test file are: rw------- |
1106 | */ |
1107 | |
1108 | if (!error) { |
1109 | printf("Expected error got success!\n"); |
1110 | ini_config_file_destroy(file_ctx); |
1111 | return EACCES13; |
1112 | } |
1113 | |
1114 | error = ini_config_access_check( |
1115 | file_ctx, |
1116 | INI_ACCESS_CHECK_MODE0x00000001, /* add uid & gui flags |
1117 | * in real case |
1118 | */ |
1119 | 0, /* <- will be real uid in real case */ |
1120 | 0, /* <- will be real gid in real case */ |
1121 | 0600, /* Checkling for rw------- */ |
1122 | 0); |
1123 | |
1124 | if (error) { |
1125 | printf("Access check failed %d!\n", error); |
1126 | ini_config_file_destroy(file_ctx); |
1127 | return EACCES13; |
1128 | } |
1129 | |
1130 | /* Create config object */ |
1131 | error = ini_config_create(&ini_config); |
1132 | if (error) { |
1133 | printf("Failed to create collection. Error %d.\n", error); |
1134 | ini_config_file_destroy(file_ctx); |
1135 | return error; |
1136 | } |
1137 | |
1138 | error = ini_config_parse(file_ctx, |
1139 | ini_config); |
1140 | if (error) { |
1141 | INIOUT(printf("Failed to parse configuration. Error %d.\n", error))do { if (verbose) printf("Failed to parse configuration. Error %d.\n" , error); } while(0); |
1142 | |
1143 | if (ini_config_error_count(file_ctx)) { |
1144 | INIOUT(printf("Errors detected while parsing: %s\n",do { if (verbose) printf("Errors detected while parsing: %s\n" , ini_config_get_filename(file_ctx)); } while(0) |
1145 | ini_config_get_filename(file_ctx)))do { if (verbose) printf("Errors detected while parsing: %s\n" , ini_config_get_filename(file_ctx)); } while(0); |
1146 | ini_config_get_errors(file_ctx, &error_list); |
1147 | INIOUT(ini_config_print_errors(stdout, error_list))do { if (verbose) ini_config_print_errors(stdout, error_list) ; } while(0); |
1148 | ini_config_free_errors(error_list); |
1149 | } |
1150 | /* We do not return here intentionally */ |
1151 | } |
1152 | |
1153 | ini_config_file_destroy(file_ctx); |
1154 | |
1155 | INIOUT(col_debug_collection(ini_config->cfg, COL_TRAVERSE_DEFAULT))do { if (verbose) col_debug_collection(ini_config->cfg, 0x00000000 ); } while(0); |
1156 | |
1157 | ini_config_destroy(ini_config); |
1158 | |
1159 | INIOUT(printf("<==== Startup test end ====>\n"))do { if (verbose) printf("<==== Startup test end ====>\n" ); } while(0); |
1160 | |
1161 | return 0; |
1162 | } |
1163 | |
1164 | int reload_test(void) |
1165 | { |
1166 | int error = EOK0; |
1167 | struct ini_cfgfile *file_ctx = NULL((void*)0); |
1168 | struct ini_cfgfile *file_ctx_new = NULL((void*)0); |
1169 | char infile[PATH_MAX4096]; |
1170 | char outfile[PATH_MAX4096]; |
1171 | char command[PATH_MAX4096 * 3]; |
1172 | char *srcdir; |
1173 | char *builddir; |
1174 | int changed = 0; |
1175 | |
1176 | INIOUT(printf("<==== Reload test ====>\n"))do { if (verbose) printf("<==== Reload test ====>\n"); } while(0); |
1177 | |
1178 | srcdir = getenv("srcdir"); |
1179 | snprintf(infile, PATH_MAX4096, "%s/ini/ini.d/foo.conf.in", |
1180 | (srcdir == NULL((void*)0)) ? "." : srcdir); |
1181 | builddir = getenv("builddir"); |
1182 | snprintf(outfile, PATH_MAX4096, "%s/foo.conf", |
1183 | (builddir == NULL((void*)0)) ? "." : builddir); |
1184 | |
1185 | snprintf(command, PATH_MAX4096 * 3, "cp %s %s", infile, outfile); |
1186 | INIOUT(printf("Running command '%s'\n", command))do { if (verbose) printf("Running command '%s'\n", command); } while(0); |
1187 | |
1188 | error = system(command); |
1189 | if ((error) || (WEXITSTATUS(error)((((__extension__ (((union { __typeof(error) __in; int __i; } ) { .__in = (error) }).__i))) & 0xff00) >> 8))) { |
1190 | printf("Failed to run copy command %d %d.\n", error, |
1191 | WEXITSTATUS(error)((((__extension__ (((union { __typeof(error) __in; int __i; } ) { .__in = (error) }).__i))) & 0xff00) >> 8)); |
1192 | return -1; |
1193 | } |
1194 | |
1195 | INIOUT(printf("Running chmod 660 on file '%s'\n", outfile))do { if (verbose) printf("Running chmod 660 on file '%s'\n", outfile ); } while(0); |
1196 | error = chmod(outfile, S_IRUSR0400 | S_IWUSR0200); |
1197 | if (error) { |
1198 | error = errno(*__errno_location ()); |
1199 | printf("Failed to run chmod command %d.\n", error); |
1200 | return error; |
1201 | } |
1202 | |
1203 | INIOUT(printf("About to open file: %s'\n", outfile))do { if (verbose) printf("About to open file: %s'\n", outfile ); } while(0); |
1204 | |
1205 | /* Open config file */ |
1206 | error = ini_config_file_open(outfile, |
1207 | INI_STOP_ON_NONE, |
1208 | 0, |
1209 | INI_META_STATS1, |
1210 | &file_ctx); |
1211 | if (error) { |
1212 | printf("Failed to open file %s for reading. Error %d.\n", |
1213 | outfile, error); |
1214 | return error; |
1215 | } |
1216 | |
1217 | INIOUT(printf("About to check access to the file.\n"))do { if (verbose) printf("About to check access to the file.\n" ); } while(0); |
1218 | |
1219 | error = ini_config_access_check( |
1220 | file_ctx, |
1221 | INI_ACCESS_CHECK_MODE0x00000001, /* add uid & gui flags |
1222 | * in real case |
1223 | */ |
1224 | 0, /* <- will be real uid in real case */ |
1225 | 0, /* <- will be real gid in real case */ |
1226 | 0600, /* Checkling for rw------- */ |
1227 | 0); |
1228 | |
1229 | if (error) { |
1230 | printf("Access check failed %d!\n", error); |
1231 | ini_config_file_destroy(file_ctx); |
1232 | return EACCES13; |
1233 | } |
1234 | |
1235 | /* ... Create config object and read configuration - not shown here. |
1236 | * See other examples ... */ |
1237 | |
1238 | INIOUT(printf("About to close file.\n"))do { if (verbose) printf("About to close file.\n"); } while(0 ); |
1239 | |
1240 | /* Now close file but leave the context around */ |
1241 | ini_config_file_close(file_ctx); |
1242 | |
1243 | INIOUT(printf("About to reopen file.\n"))do { if (verbose) printf("About to reopen file.\n"); } while( 0); |
1244 | |
1245 | /* Some time passed and we received a signal to reload... */ |
1246 | error = ini_config_file_reopen(file_ctx, &file_ctx_new); |
1247 | if (error) { |
1248 | printf("Failed to re-open file for reading. Error %d.\n", error); |
1249 | ini_config_file_destroy(file_ctx); |
1250 | return error; |
1251 | } |
1252 | |
1253 | INIOUT(printf("About to check if the file changed.\n"))do { if (verbose) printf("About to check if the file changed.\n" ); } while(0); |
1254 | |
1255 | changed = 0; |
1256 | error = ini_config_changed(file_ctx, |
1257 | file_ctx_new, |
1258 | &changed); |
1259 | if (error) { |
1260 | printf("Failed to compare files. Error %d.\n", error); |
1261 | ini_config_file_destroy(file_ctx); |
1262 | ini_config_file_destroy(file_ctx_new); |
1263 | return error; |
1264 | } |
1265 | |
1266 | /* Check if file changed */ |
1267 | if (changed) { |
1268 | printf("File changed when it shouldn't. This is unexpected error.\n"); |
1269 | ini_config_file_print(file_ctx); |
1270 | ini_config_file_print(file_ctx_new); |
1271 | ini_config_file_destroy(file_ctx); |
1272 | ini_config_file_destroy(file_ctx_new); |
1273 | return EINVAL22; |
1274 | } |
1275 | |
1276 | INIOUT(printf("File did not change - expected. Close and force the change!.\n"))do { if (verbose) printf("File did not change - expected. Close and force the change!.\n" ); } while(0); |
1277 | |
1278 | /* Close file */ |
1279 | ini_config_file_destroy(file_ctx_new); |
1280 | |
1281 | INIOUT(printf("To force the change delete the file: %s\n", outfile))do { if (verbose) printf("To force the change delete the file: %s\n" , outfile); } while(0); |
1282 | |
1283 | /* Emulate as if file changed */ |
1284 | errno(*__errno_location ()) = 0; |
1285 | if (unlink(outfile)) { |
1286 | error = errno(*__errno_location ()); |
1287 | printf("Failed to delete file %d.\n", error); |
1288 | ini_config_file_destroy(file_ctx); |
1289 | return error; |
1290 | } |
1291 | |
1292 | sleep(1); |
1293 | |
1294 | snprintf(command, PATH_MAX4096 * 3, "cp %s %s", infile, outfile); |
1295 | INIOUT(printf("Copy file again with command '%s'\n", command))do { if (verbose) printf("Copy file again with command '%s'\n" , command); } while(0); |
1296 | |
1297 | error = system(command); |
1298 | if ((error) || (WEXITSTATUS(error)((((__extension__ (((union { __typeof(error) __in; int __i; } ) { .__in = (error) }).__i))) & 0xff00) >> 8))) { |
1299 | printf("Failed to run copy command %d %d.\n", error, WEXITSTATUS(error)((((__extension__ (((union { __typeof(error) __in; int __i; } ) { .__in = (error) }).__i))) & 0xff00) >> 8)); |
1300 | ini_config_file_destroy(file_ctx); |
1301 | return -1; |
1302 | } |
1303 | |
1304 | INIOUT(printf("Read file again.\n"))do { if (verbose) printf("Read file again.\n"); } while(0); |
1305 | |
1306 | /* Read again */ |
1307 | file_ctx_new = NULL((void*)0); |
1308 | error = ini_config_file_reopen(file_ctx, &file_ctx_new); |
1309 | if (error) { |
1310 | printf("Failed to re-open file for reading. Error %d.\n", error); |
1311 | ini_config_file_destroy(file_ctx); |
1312 | return error; |
1313 | } |
1314 | |
1315 | INIOUT(printf("Check if it changed.\n"))do { if (verbose) printf("Check if it changed.\n"); } while(0 ); |
1316 | |
1317 | changed = 0; |
1318 | error = ini_config_changed(file_ctx, |
1319 | file_ctx_new, |
1320 | &changed); |
1321 | if (error) { |
1322 | printf("Failed to compare files. Error %d.\n", error); |
1323 | ini_config_file_destroy(file_ctx); |
1324 | ini_config_file_destroy(file_ctx_new); |
1325 | return error; |
1326 | } |
1327 | |
1328 | INIOUT(printf("Changed value is %d.\n", changed))do { if (verbose) printf("Changed value is %d.\n", changed); } while(0); |
1329 | |
1330 | /* Check if file changed */ |
1331 | if (!changed) { |
1332 | printf("File did not change when it should. This is an error.\n"); |
1333 | ini_config_file_print(file_ctx); |
1334 | ini_config_file_print(file_ctx_new); |
1335 | ini_config_file_destroy(file_ctx); |
1336 | ini_config_file_destroy(file_ctx_new); |
1337 | return EINVAL22; |
1338 | } |
1339 | |
1340 | INIOUT(printf("File changed!\n"))do { if (verbose) printf("File changed!\n"); } while(0); |
1341 | INIOUT(ini_config_file_print(file_ctx))do { if (verbose) ini_config_file_print(file_ctx); } while(0); |
1342 | INIOUT(ini_config_file_print(file_ctx_new))do { if (verbose) ini_config_file_print(file_ctx_new); } while (0); |
1343 | |
1344 | /* We do not need original context any more. */ |
1345 | ini_config_file_destroy(file_ctx); |
1346 | |
1347 | /* New context is now original context */ |
1348 | file_ctx = file_ctx_new; |
1349 | |
1350 | /* ... Create config object and read configuration - not shown here. |
1351 | * See other examples ... */ |
1352 | |
1353 | ini_config_file_destroy(file_ctx); |
1354 | |
1355 | INIOUT(printf("<==== Reload test end ====>\n"))do { if (verbose) printf("<==== Reload test end ====>\n" ); } while(0); |
1356 | return 0; |
1357 | } |
1358 | |
1359 | int get_test(void) |
1360 | { |
1361 | |
1362 | int error; |
1363 | int number; |
1364 | long number_long; |
1365 | double number_double; |
1366 | unsigned number_unsigned; |
1367 | unsigned long number_ulong; |
1368 | unsigned char logical; |
1369 | char *str; |
1370 | const char *cstr; |
1371 | const char *cstrn; |
1372 | void *binary; |
1373 | int length; |
1374 | int i = 0; |
1375 | char **strarray; |
1376 | char **strptr; |
1377 | int size; |
1378 | long *array; |
1379 | double *darray; |
1380 | char **prop_array; |
1381 | int32_t val_int32; |
1382 | uint32_t val_uint32; |
1383 | int64_t val_int64; |
1384 | uint64_t val_uint64; |
1385 | struct ini_cfgfile *file_ctx = NULL((void*)0); |
1386 | struct ini_cfgobj *ini_config = NULL((void*)0); |
1387 | struct value_obj *vo = NULL((void*)0); |
1388 | char **error_list = NULL((void*)0); |
1389 | char infile[PATH_MAX4096]; |
1390 | char *srcdir = NULL((void*)0); |
1391 | int bad_val = 0; |
1392 | |
1393 | INIOUT(printf("\n\n<==== GET TEST START =====>\n"))do { if (verbose) printf("\n\n<==== GET TEST START =====>\n" ); } while(0); |
1394 | INIOUT(printf("Creating configuration object\n"))do { if (verbose) printf("Creating configuration object\n"); } while(0); |
1395 | |
1396 | /* Create config collection */ |
1397 | error = ini_config_create(&ini_config); |
1398 | if (error) { |
1399 | printf("Failed to create collection. Error %d.\n", error); |
1400 | return error; |
1401 | } |
1402 | |
1403 | srcdir = getenv("srcdir"); |
1404 | snprintf(infile, PATH_MAX4096, "%s/ini/ini.d/real.conf", |
1405 | (srcdir == NULL((void*)0)) ? "." : srcdir); |
1406 | |
1407 | INIOUT(printf("Reading file %s\n", infile))do { if (verbose) printf("Reading file %s\n", infile); } while (0); |
1408 | |
1409 | error = ini_config_file_open(infile, |
1410 | INI_STOP_ON_NONE, |
1411 | /* Merge section but allow duplicates */ |
1412 | INI_MS_MERGE0x0000 | |
1413 | INI_MV1S_ALLOW0x0003 | |
1414 | INI_MV2S_ALLOW0x0030, |
1415 | 0, |
1416 | &file_ctx); |
1417 | if (error) { |
1418 | printf("Failed to open file for reading. Error %d.\n", error); |
1419 | ini_config_destroy(ini_config); |
1420 | return error; |
1421 | } |
1422 | |
1423 | error = ini_config_parse(file_ctx, |
1424 | ini_config); |
1425 | if (error) { |
1426 | INIOUT(printf("Failed to parse configuration. Error %d.\n", error))do { if (verbose) printf("Failed to parse configuration. Error %d.\n" , error); } while(0); |
1427 | |
1428 | if (ini_config_error_count(file_ctx)) { |
1429 | INIOUT(printf("Errors detected while parsing: %s\n",do { if (verbose) printf("Errors detected while parsing: %s\n" , ini_config_get_filename(file_ctx)); } while(0) |
1430 | ini_config_get_filename(file_ctx)))do { if (verbose) printf("Errors detected while parsing: %s\n" , ini_config_get_filename(file_ctx)); } while(0); |
1431 | ini_config_get_errors(file_ctx, &error_list); |
1432 | INIOUT(ini_config_print_errors(stdout, error_list))do { if (verbose) ini_config_print_errors(stdout, error_list) ; } while(0); |
1433 | ini_config_free_errors(error_list); |
1434 | } |
1435 | /* We do not return here intentionally */ |
1436 | } |
1437 | |
1438 | ini_config_file_destroy(file_ctx); |
1439 | |
1440 | INIOUT(printf("Negtive test - trying to get non"do { if (verbose) printf("Negtive test - trying to get non" " existing key-value pair.\n" ); } while(0) |
1441 | " existing key-value pair.\n"))do { if (verbose) printf("Negtive test - trying to get non" " existing key-value pair.\n" ); } while(0); |
1442 | |
1443 | /* Negative test */ |
1444 | vo = NULL((void*)0); |
1445 | error = ini_get_config_valueobj("monitor1", |
1446 | "description1", |
1447 | ini_config, |
1448 | INI_GET_FIRST_VALUE0, |
1449 | &vo); |
1450 | if (error) { |
1451 | printf("Expected success but got error! %d\n", error); |
1452 | ini_config_destroy(ini_config); |
1453 | return error; |
1454 | } |
1455 | |
1456 | /* Values should not be found */ |
1457 | if (vo != NULL((void*)0)) { |
1458 | printf("Expected NULL but got something else!\n"); |
1459 | ini_config_destroy(ini_config); |
1460 | return -1; |
1461 | } |
1462 | |
1463 | /* Another negative test but section exists this time */ |
1464 | vo = NULL((void*)0); |
1465 | error = ini_get_config_valueobj("monitor", |
1466 | "description1", |
1467 | ini_config, |
1468 | INI_GET_FIRST_VALUE0, |
1469 | &vo); |
1470 | if (error) { |
1471 | printf("Expected success but got error! %d\n", error); |
1472 | ini_config_destroy(ini_config); |
1473 | return error; |
1474 | } |
1475 | |
1476 | /* Valueobj should not be found */ |
1477 | if(vo != NULL((void*)0)) { |
1478 | printf("Expected NULL but got something else!\n"); |
1479 | ini_config_destroy(ini_config); |
1480 | return -1; |
1481 | } |
1482 | |
1483 | INIOUT(printf("Trying to get a value.\n"))do { if (verbose) printf("Trying to get a value.\n"); } while (0); |
1484 | |
1485 | /* Positive test */ |
1486 | vo = NULL((void*)0); |
1487 | error = ini_get_config_valueobj("monitor", |
1488 | "description", |
1489 | ini_config, |
1490 | INI_GET_FIRST_VALUE0, |
1491 | &vo); |
1492 | if (error) { |
1493 | printf("Expected success but got error! %d\n", error); |
1494 | ini_config_destroy(ini_config); |
1495 | return error; |
1496 | } |
1497 | |
1498 | /* Value should be found */ |
1499 | if (vo == NULL((void*)0)) { |
1500 | printf("Expected value but got NULL!\n"); |
1501 | ini_config_destroy(ini_config); |
1502 | return -1; |
1503 | } |
1504 | |
1505 | INIOUT(value_print("description", vo))do { if (verbose) value_print("description", vo); } while(0); |
1506 | |
1507 | INIOUT(printf("Get values as string without duplication"do { if (verbose) printf("Get values as string without duplication" " from the NULL valueobj.\n"); } while(0) |
1508 | " from the NULL valueobj.\n"))do { if (verbose) printf("Get values as string without duplication" " from the NULL valueobj.\n"); } while(0); |
1509 | |
1510 | /* Get a string without duplicication */ |
1511 | /* Negative test */ |
1512 | cstrn = ini_get_const_string_config_value(NULL((void*)0), NULL((void*)0)); |
1513 | if (cstrn != NULL((void*)0)) { |
1514 | printf("Expected error got success.\n"); |
1515 | ini_config_destroy(ini_config); |
1516 | return -1; |
1517 | } |
1518 | |
1519 | INIOUT(printf("Get value as string without duplication"do { if (verbose) printf("Get value as string without duplication" "from the correct value object.\n"); } while(0) |
1520 | "from the correct value object.\n"))do { if (verbose) printf("Get value as string without duplication" "from the correct value object.\n"); } while(0); |
1521 | |
1522 | /* Now get string from the right value object */ |
1523 | error = 0; |
1524 | cstr = ini_get_const_string_config_value(vo, &error); |
1525 | if (error) { |
1526 | printf("Expected success got error %d.\n", error); |
1527 | ini_config_destroy(ini_config); |
1528 | return error; |
1529 | } |
1530 | |
1531 | INIOUT(printf("Value: [%s]\n", cstr))do { if (verbose) printf("Value: [%s]\n", cstr); } while(0); |
1532 | |
1533 | /* Same thing but create a dup */ |
1534 | |
1535 | INIOUT(printf("Get value as string with duplication"do { if (verbose) printf("Get value as string with duplication" " from correct value object.\n"); } while(0) |
1536 | " from correct value object.\n"))do { if (verbose) printf("Get value as string with duplication" " from correct value object.\n"); } while(0); |
1537 | |
1538 | error = 0; |
1539 | str = ini_get_string_config_value(vo, &error); |
1540 | if (error) { |
1541 | printf("Expected success got error %d.\n", error); |
1542 | ini_config_destroy(ini_config); |
1543 | return error; |
1544 | } |
1545 | |
1546 | INIOUT(printf("Value: [%s]\n", str))do { if (verbose) printf("Value: [%s]\n", str); } while(0); |
1547 | free(str); |
1548 | |
1549 | |
1550 | /* Get a badly formated number */ |
1551 | INIOUT(printf("Convert value to number with strict conversion.\n"))do { if (verbose) printf("Convert value to number with strict conversion.\n" ); } while(0); |
1552 | |
1553 | vo = NULL((void*)0); |
1554 | error = ini_get_config_valueobj("monitor", |
1555 | "bad_number", |
1556 | ini_config, |
1557 | INI_GET_FIRST_VALUE0, |
1558 | &vo); |
1559 | if (error) { |
1560 | printf("Expected success but got error! %d\n", error); |
1561 | ini_config_destroy(ini_config); |
1562 | return error; |
1563 | } |
1564 | |
1565 | /* Value should be found */ |
1566 | if (vo == NULL((void*)0)) { |
1567 | printf("Expected value but got something NULL!\n"); |
1568 | ini_config_destroy(ini_config); |
1569 | return -1; |
1570 | } |
1571 | |
1572 | INIOUT(value_print("bad_number", vo))do { if (verbose) value_print("bad_number", vo); } while(0); |
1573 | |
1574 | /* Now try to get value in different ways */ |
1575 | error = 0; |
1576 | number = ini_get_int_config_value(vo, 1, 10, &error); |
1577 | if (error) { |
1578 | /* We expected error in this case */ |
1579 | INIOUT(printf("Expected error.\n"))do { if (verbose) printf("Expected error.\n"); } while(0); |
1580 | if(number != 10) { |
1581 | printf("It failed to set default value.\n"); |
1582 | ini_config_destroy(ini_config); |
1583 | return -1; |
1584 | } |
1585 | } |
1586 | else { |
1587 | printf("Expected error got success.\n"); |
1588 | ini_config_destroy(ini_config); |
1589 | return -1; |
1590 | } |
1591 | |
1592 | INIOUT(printf("Convert value to number without strict conversion.\n"))do { if (verbose) printf("Convert value to number without strict conversion.\n" ); } while(0); |
1593 | |
1594 | error = 0; |
1595 | number = 1; |
1596 | number = ini_get_int_config_value(vo, 0, 10, &error); |
1597 | if (error) { |
1598 | printf("Did not expect error.\n"); |
1599 | ini_config_destroy(ini_config); |
1600 | return error; |
1601 | } |
1602 | |
1603 | if (number != 5) { |
1604 | printf("We expected that the conversion will return 5.\n"); |
1605 | ini_config_destroy(ini_config); |
1606 | return -1; |
1607 | } |
1608 | |
1609 | /* Get real integer */ |
1610 | |
1611 | INIOUT(printf("Fetch another value from section \"domains/LOCAL\""do { if (verbose) printf("Fetch another value from section \"domains/LOCAL\"" " named \"enumerate\".\n"); } while(0) |
1612 | " named \"enumerate\".\n"))do { if (verbose) printf("Fetch another value from section \"domains/LOCAL\"" " named \"enumerate\".\n"); } while(0); |
1613 | |
1614 | vo = NULL((void*)0); |
1615 | error = ini_get_config_valueobj("domains/LOCAL", |
1616 | "enumerate", |
1617 | ini_config, |
1618 | INI_GET_FIRST_VALUE0, |
1619 | &vo); |
1620 | if (error) { |
1621 | printf("Expected success but got error! %d\n", error); |
1622 | ini_config_destroy(ini_config); |
1623 | return error; |
1624 | } |
1625 | |
1626 | /* Value should be found */ |
1627 | if (vo == NULL((void*)0)) { |
1628 | printf("Expected success but got NULL.\n"); |
1629 | ini_config_destroy(ini_config); |
1630 | return -1; |
1631 | } |
1632 | |
1633 | INIOUT(printf("Convert value to integer.\n"))do { if (verbose) printf("Convert value to integer.\n"); } while (0); |
1634 | |
1635 | /* Take number out of it */ |
1636 | error = 0; |
1637 | number = ini_get_int_config_value(vo, 1, 100, &error); |
1638 | if (error) { |
1639 | printf("Did not expect error. Got %d\n", error); |
1640 | ini_config_destroy(ini_config); |
1641 | return error; |
1642 | } |
1643 | |
1644 | /* It is 3 in the file */ |
1645 | if (number != 3) { |
1646 | printf("We expected that the conversion will return 3.\n"); |
1647 | ini_config_destroy(ini_config); |
1648 | return -1; |
1649 | } |
1650 | |
1651 | INIOUT(printf("Expected 3 got %d\n", number))do { if (verbose) printf("Expected 3 got %d\n", number); } while (0); |
1652 | |
1653 | INIOUT(printf("Convert value to long.\n"))do { if (verbose) printf("Convert value to long.\n"); } while (0); |
1654 | |
1655 | /* Take number out of it */ |
1656 | error = 0; |
1657 | number_long = ini_get_long_config_value(vo, 1, 100, &error); |
1658 | if (error) { |
1659 | printf("Did not expect error. Got %d\n", error); |
1660 | ini_config_destroy(ini_config); |
1661 | return error; |
1662 | } |
1663 | |
1664 | /* It is 3 in the file */ |
1665 | if (number_long != 3) { |
1666 | printf("We expected that the conversion will return 3.\n"); |
1667 | ini_config_destroy(ini_config); |
1668 | return -1; |
1669 | } |
1670 | |
1671 | INIOUT(printf("Expected 3 got %ld\n", number_long))do { if (verbose) printf("Expected 3 got %ld\n", number_long) ; } while(0); |
1672 | |
1673 | INIOUT(printf("Convert value to unsigned.\n"))do { if (verbose) printf("Convert value to unsigned.\n"); } while (0); |
1674 | |
1675 | /* Take number out of it */ |
1676 | error = 0; |
1677 | number_unsigned = ini_get_unsigned_config_value(vo, 1, 100, &error); |
1678 | if (error) { |
1679 | printf("Did not expect error. Got %d\n", error); |
1680 | ini_config_destroy(ini_config); |
1681 | return error; |
1682 | } |
1683 | |
1684 | /* It is 3 in the file */ |
1685 | if (number_unsigned != 3) { |
1686 | printf("We expected that the conversion will return 3.\n"); |
1687 | ini_config_destroy(ini_config); |
1688 | return -1; |
1689 | } |
1690 | |
1691 | INIOUT(printf("Expected 3 got %d\n", number_unsigned))do { if (verbose) printf("Expected 3 got %d\n", number_unsigned ); } while(0); |
1692 | |
1693 | INIOUT(printf("Convert value to unsigned long.\n"))do { if (verbose) printf("Convert value to unsigned long.\n") ; } while(0); |
1694 | |
1695 | /* Take number out of it */ |
1696 | error = 0; |
1697 | number_ulong = ini_get_ulong_config_value(vo, 1, 100, &error); |
1698 | if (error) { |
1699 | printf("Did not expect error. Got %d\n", error); |
1700 | ini_config_destroy(ini_config); |
1701 | return error; |
1702 | } |
1703 | |
1704 | /* It is 3 in the file */ |
1705 | if (number_ulong != 3) { |
1706 | printf("We expected that the conversion will return 3.\n"); |
1707 | ini_config_destroy(ini_config); |
1708 | return -1; |
1709 | } |
1710 | |
1711 | INIOUT(printf("Expected 3 got %lu\n", number_ulong))do { if (verbose) printf("Expected 3 got %lu\n", number_ulong ); } while(0); |
1712 | |
1713 | INIOUT(printf("Convert value to double.\n"))do { if (verbose) printf("Convert value to double.\n"); } while (0); |
1714 | |
1715 | /* Take number out of it */ |
1716 | error = 0; |
1717 | number_double = ini_get_double_config_value(vo, 1, 100., &error); |
1718 | if (error) { |
1719 | printf("Did not expect error. Got %d\n", error); |
1720 | ini_config_destroy(ini_config); |
1721 | return error; |
1722 | } |
1723 | |
1724 | /* It is 3 in the file */ |
1725 | if (number_double != 3.) { |
1726 | printf("We expected that the conversion will return 3.\n"); |
1727 | ini_config_destroy(ini_config); |
1728 | return -1; |
1729 | } |
1730 | |
1731 | INIOUT(printf("Expected 3 got %e\n", number_double))do { if (verbose) printf("Expected 3 got %e\n", number_double ); } while(0); |
1732 | |
1733 | INIOUT(printf("Convert value to bool.\n"))do { if (verbose) printf("Convert value to bool.\n"); } while (0); |
1734 | |
1735 | /* Take number out of it */ |
1736 | error = 0; |
1737 | logical = ini_get_bool_config_value(vo, 1, &error); |
Value stored to 'logical' is never read | |
1738 | if (!error) { |
1739 | printf("Expect error. Got success.\n"); |
1740 | ini_config_destroy(ini_config); |
1741 | return -1; |
1742 | } |
1743 | |
1744 | /* Get real bool values and convert it */ |
1745 | INIOUT(printf("Get real bool value \"legacy\" and convert it.\n"))do { if (verbose) printf("Get real bool value \"legacy\" and convert it.\n" ); } while(0); |
1746 | |
1747 | vo = NULL((void*)0); |
1748 | error = ini_get_config_valueobj("domains/LOCAL", |
1749 | "legacy", |
1750 | ini_config, |
1751 | INI_GET_FIRST_VALUE0, |
1752 | &vo); |
1753 | if (error) { |
1754 | printf("Expected success but got error! %d\n",error); |
1755 | ini_config_destroy(ini_config); |
1756 | return error; |
1757 | } |
1758 | |
1759 | /* Value should be found */ |
1760 | if (vo == NULL((void*)0)) { |
1761 | printf("Expected success but got NULL.\n"); |
1762 | ini_config_destroy(ini_config); |
1763 | return -1; |
1764 | } |
1765 | |
1766 | INIOUT(printf("Convert values to bool.\n"))do { if (verbose) printf("Convert values to bool.\n"); } while (0); |
1767 | |
1768 | error = 0; |
1769 | logical = ini_get_bool_config_value(vo, 1, &error); |
1770 | if (error) { |
1771 | printf("Expect success got error %d.\n", error); |
1772 | ini_config_destroy(ini_config); |
1773 | return error; |
1774 | } |
1775 | |
1776 | if (logical) { |
1777 | printf("Expected false but got true - bad.\n"); |
1778 | return -1; |
1779 | } |
1780 | |
1781 | INIOUT(printf("In the files it is FALSE so we got false.\n"))do { if (verbose) printf("In the files it is FALSE so we got false.\n" ); } while(0); |
1782 | |
1783 | INIOUT(printf("Get binary value\n"))do { if (verbose) printf("Get binary value\n"); } while(0); |
1784 | |
1785 | vo = NULL((void*)0); |
1786 | error = ini_get_config_valueobj("domains/EXAMPLE.COM", |
1787 | "binary_test", |
1788 | ini_config, |
1789 | INI_GET_FIRST_VALUE0, |
1790 | &vo); |
1791 | if (error) { |
1792 | printf("Expected success but got error! %d\n", error); |
1793 | ini_config_destroy(ini_config); |
1794 | return error; |
1795 | } |
1796 | |
1797 | /* Value should be found */ |
1798 | if (vo == NULL((void*)0)) { |
1799 | printf("Expected success but got NULL.\n"); |
1800 | ini_config_destroy(ini_config); |
1801 | return -1; |
1802 | } |
1803 | |
1804 | INIOUT(value_print("binary_test", vo))do { if (verbose) value_print("binary_test", vo); } while(0); |
1805 | |
1806 | error = 0; |
1807 | binary = ini_get_bin_config_value(vo, &length, &error); |
1808 | if (error) { |
1809 | printf("Expect success got error %d.\n", error); |
1810 | ini_config_destroy(ini_config); |
1811 | return error; |
1812 | } |
1813 | |
1814 | INIOUT(printf("Binary value (expect 123) = "))do { if (verbose) printf("Binary value (expect 123) = "); } while (0); |
1815 | INIOUT(for (i = 0; i < length; i++) {do { if (verbose) for (i = 0; i < length; i++) { printf("%d" ,*((unsigned char*)(binary) + i)); if (*((unsigned char*)(binary ) + i) != (i + 1)) bad_val = 1; }; } while(0) |
1816 | printf("%d",*((unsigned char*)(binary) + i));do { if (verbose) for (i = 0; i < length; i++) { printf("%d" ,*((unsigned char*)(binary) + i)); if (*((unsigned char*)(binary ) + i) != (i + 1)) bad_val = 1; }; } while(0) |
1817 | if (*((unsigned char*)(binary) + i) != (i + 1)) bad_val = 1;do { if (verbose) for (i = 0; i < length; i++) { printf("%d" ,*((unsigned char*)(binary) + i)); if (*((unsigned char*)(binary ) + i) != (i + 1)) bad_val = 1; }; } while(0) |
1818 | })do { if (verbose) for (i = 0; i < length; i++) { printf("%d" ,*((unsigned char*)(binary) + i)); if (*((unsigned char*)(binary ) + i) != (i + 1)) bad_val = 1; }; } while(0); |
1819 | INIOUT(printf("\n"))do { if (verbose) printf("\n"); } while(0); |
1820 | |
1821 | ini_free_bin_config_value(binary); |
1822 | |
1823 | if (bad_val) { |
1824 | printf("Unexpected binary value.\n"); |
1825 | ini_config_destroy(ini_config); |
1826 | return -1; |
1827 | } |
1828 | |
1829 | INIOUT(printf("Get another binary value\n"))do { if (verbose) printf("Get another binary value\n"); } while (0); |
1830 | |
1831 | bad_val = 0; |
1832 | vo = NULL((void*)0); |
1833 | error = ini_get_config_valueobj("domains/EXAMPLE.COM", |
1834 | "binary_test_two", |
1835 | ini_config, |
1836 | INI_GET_FIRST_VALUE0, |
1837 | &vo); |
1838 | if (error) { |
1839 | printf("Expected success but got error! %d\n", error); |
1840 | ini_config_destroy(ini_config); |
1841 | return error; |
1842 | } |
1843 | |
1844 | /* Value should be found */ |
1845 | if (vo == NULL((void*)0)) { |
1846 | printf("Expected success but got NULL.\n"); |
1847 | ini_config_destroy(ini_config); |
1848 | return -1; |
1849 | } |
1850 | |
1851 | INIOUT(value_print("binary_test_two", vo))do { if (verbose) value_print("binary_test_two", vo); } while (0); |
1852 | |
1853 | error = 0; |
1854 | binary = ini_get_bin_config_value(vo, &length, &error); |
1855 | if (error) { |
1856 | printf("Expect success got error %d.\n", error); |
1857 | ini_config_destroy(ini_config); |
1858 | return error; |
1859 | } |
1860 | |
1861 | INIOUT(printf("Binary value (expect abc) = "))do { if (verbose) printf("Binary value (expect abc) = "); } while (0); |
1862 | INIOUT(for (i = 0; i < length; i++) {do { if (verbose) for (i = 0; i < length; i++) { printf("%x" ,*((unsigned char*)(binary) + i)); if (*((unsigned char*)(binary ) + i) - 10 != i) bad_val = 1; }; } while(0) |
1863 | printf("%x",*((unsigned char*)(binary) + i));do { if (verbose) for (i = 0; i < length; i++) { printf("%x" ,*((unsigned char*)(binary) + i)); if (*((unsigned char*)(binary ) + i) - 10 != i) bad_val = 1; }; } while(0) |
1864 | if (*((unsigned char*)(binary) + i) - 10 != i) bad_val = 1;do { if (verbose) for (i = 0; i < length; i++) { printf("%x" ,*((unsigned char*)(binary) + i)); if (*((unsigned char*)(binary ) + i) - 10 != i) bad_val = 1; }; } while(0) |
1865 | })do { if (verbose) for (i = 0; i < length; i++) { printf("%x" ,*((unsigned char*)(binary) + i)); if (*((unsigned char*)(binary ) + i) - 10 != i) bad_val = 1; }; } while(0); |
1866 | INIOUT(printf("\n"))do { if (verbose) printf("\n"); } while(0); |
1867 | |
1868 | ini_free_bin_config_value(binary); |
1869 | |
1870 | if (bad_val) { |
1871 | printf("Unexpected binary value.\n"); |
1872 | ini_config_destroy(ini_config); |
1873 | return -1; |
1874 | } |
1875 | |
1876 | INIOUT(printf("Get string array value\n"))do { if (verbose) printf("Get string array value\n"); } while (0); |
1877 | |
1878 | vo = NULL((void*)0); |
1879 | error = ini_get_config_valueobj("domains", |
1880 | "domainsorder", |
1881 | ini_config, |
1882 | INI_GET_FIRST_VALUE0, |
1883 | &vo); |
1884 | if(error) { |
1885 | printf("Expected success but got error! %d\n",error); |
1886 | ini_config_destroy(ini_config); |
1887 | return error; |
1888 | } |
1889 | |
1890 | /* Value should be found */ |
1891 | if (vo == NULL((void*)0)) { |
1892 | printf("Expected success but got NULL.\n"); |
1893 | ini_config_destroy(ini_config); |
1894 | return -1; |
1895 | } |
1896 | |
1897 | INIOUT(value_print("domainsorder", vo))do { if (verbose) value_print("domainsorder", vo); } while(0); |
1898 | |
1899 | INIOUT(printf("Get str array without size.\n"))do { if (verbose) printf("Get str array without size.\n"); } while (0); |
1900 | |
1901 | error = 0; |
1902 | strarray = ini_get_string_config_array(vo, ",", NULL((void*)0), &error); |
1903 | if (error) { |
1904 | printf("Expect success got error %d.\n", error); |
1905 | ini_config_destroy(ini_config); |
1906 | return error; |
1907 | } |
1908 | |
1909 | /* Can be used with this cycle */ |
1910 | strptr = strarray; |
1911 | while (*strptr != NULL((void*)0)) { |
1912 | INIOUT(printf("[%s]\n",*strptr))do { if (verbose) printf("[%s]\n",*strptr); } while(0); |
1913 | strptr++; |
1914 | } |
1915 | |
1916 | ini_free_string_config_array(strarray); |
1917 | |
1918 | INIOUT(printf("Get raw str array without size.\n"))do { if (verbose) printf("Get raw str array without size.\n") ; } while(0); |
1919 | |
1920 | error = 0; |
1921 | strarray = ini_get_raw_string_config_array(vo, ",", NULL((void*)0), &error); |
1922 | if (error) { |
1923 | printf("Expect success got error %d.\n", error); |
1924 | ini_config_destroy(ini_config); |
1925 | return error; |
1926 | } |
1927 | |
1928 | /* Can be used with this cycle */ |
1929 | strptr = strarray; |
1930 | while (*strptr != NULL((void*)0)) { |
1931 | INIOUT(printf("[%s]\n",*strptr))do { if (verbose) printf("[%s]\n",*strptr); } while(0); |
1932 | strptr++; |
1933 | } |
1934 | |
1935 | ini_free_string_config_array(strarray); |
1936 | |
1937 | INIOUT(printf("Get str array with size.\n"))do { if (verbose) printf("Get str array with size.\n"); } while (0); |
1938 | |
1939 | error = 0; |
1940 | size = 0; |
1941 | strarray = ini_get_string_config_array(vo, ",", &size, &error); |
1942 | if (error) { |
1943 | printf("Expect success got error %d.\n", error); |
1944 | ini_config_destroy(ini_config); |
1945 | return error; |
1946 | } |
1947 | |
1948 | /* Can be used with this cycle */ |
1949 | INIOUT(for (i=0;i<size;i++) printf("[%s]\n",*(strarray + i)))do { if (verbose) for (i=0;i<size;i++) printf("[%s]\n",*(strarray + i)); } while(0); |
1950 | |
1951 | ini_free_string_config_array(strarray); |
1952 | |
1953 | INIOUT(printf("Get raw str array with size.\n"))do { if (verbose) printf("Get raw str array with size.\n"); } while(0); |
1954 | |
1955 | error = 0; |
1956 | size = 0; |
1957 | strarray = ini_get_raw_string_config_array(vo, ",", &size, &error); |
1958 | if (error) { |
1959 | printf("Expect success got error %d.\n", error); |
1960 | ini_config_destroy(ini_config); |
1961 | return error; |
1962 | } |
1963 | |
1964 | /* Can be used with this cycle */ |
1965 | INIOUT(for (i=0;i<size;i++) printf("[%s]\n",*(strarray + i)))do { if (verbose) for (i=0;i<size;i++) printf("[%s]\n",*(strarray + i)); } while(0); |
1966 | |
1967 | ini_free_string_config_array(strarray); |
1968 | |
1969 | /**********************************************************/ |
1970 | |
1971 | INIOUT(printf("Get bad string array \n"))do { if (verbose) printf("Get bad string array \n"); } while( 0); |
1972 | |
1973 | vo = NULL((void*)0); |
1974 | error = ini_get_config_valueobj("domains", |
1975 | "badarray", |
1976 | ini_config, |
1977 | INI_GET_FIRST_VALUE0, |
1978 | &vo); |
1979 | if(error) { |
1980 | printf("Expected success but got error! %d\n",error); |
1981 | ini_config_destroy(ini_config); |
1982 | return error; |
1983 | } |
1984 | |
1985 | /* Value should be found */ |
1986 | if (vo == NULL((void*)0)) { |
1987 | printf("Expected success but got NULL.\n"); |
1988 | ini_config_destroy(ini_config); |
1989 | return -1; |
1990 | } |
1991 | |
1992 | INIOUT(value_print("badarray", vo))do { if (verbose) value_print("badarray", vo); } while(0); |
1993 | |
1994 | INIOUT(printf("Get bad str array without size.\n"))do { if (verbose) printf("Get bad str array without size.\n") ; } while(0); |
1995 | |
1996 | error = 0; |
1997 | strarray = ini_get_string_config_array(vo, ",", NULL((void*)0), &error); |
1998 | if (error) { |
1999 | printf("Expect success got error %d.\n", error); |
2000 | ini_config_destroy(ini_config); |
2001 | return error; |
2002 | } |
2003 | |
2004 | /* Can be used with this cycle */ |
2005 | strptr = strarray; |
2006 | while (*strptr != NULL((void*)0)) { |
2007 | INIOUT(printf("[%s]\n",*strptr))do { if (verbose) printf("[%s]\n",*strptr); } while(0); |
2008 | strptr++; |
2009 | } |
2010 | |
2011 | ini_free_string_config_array(strarray); |
2012 | |
2013 | /**********************************************************/ |
2014 | |
2015 | INIOUT(printf("Get long array value\n"))do { if (verbose) printf("Get long array value\n"); } while(0 ); |
2016 | |
2017 | vo = NULL((void*)0); |
2018 | error = ini_get_config_valueobj("domains/EXAMPLE.COM", |
2019 | "long_array", |
2020 | ini_config, |
2021 | INI_GET_FIRST_VALUE0, |
2022 | &vo); |
2023 | if(error) { |
2024 | printf("Expected success but got error! %d\n", error); |
2025 | ini_config_destroy(ini_config); |
2026 | return error; |
2027 | } |
2028 | |
2029 | /* Value should be found */ |
2030 | if (vo == NULL((void*)0)) { |
2031 | printf("Expected success but got NULL.\n"); |
2032 | ini_config_destroy(ini_config); |
2033 | return -1; |
2034 | } |
2035 | |
2036 | INIOUT(value_print("long_array", vo))do { if (verbose) value_print("long_array", vo); } while(0); |
2037 | |
2038 | error = 0; |
2039 | size = 0; /* Here size is not optional!!! */ |
2040 | array = ini_get_long_config_array(vo, &size, &error); |
2041 | if(error) { |
2042 | printf("Expect success got error %d.\n", error); |
2043 | ini_config_destroy(ini_config); |
2044 | return error; |
2045 | } |
2046 | |
2047 | /* Can be used with this cycle */ |
2048 | INIOUT(for (i=0;i<size;i++) printf("%ld\n", *(array + i)))do { if (verbose) for (i=0;i<size;i++) printf("%ld\n", *(array + i)); } while(0); |
2049 | |
2050 | ini_free_long_config_array(array); |
2051 | |
2052 | INIOUT(printf("Get double array value\n"))do { if (verbose) printf("Get double array value\n"); } while (0); |
2053 | |
2054 | vo = NULL((void*)0); |
2055 | error = ini_get_config_valueobj("domains/EXAMPLE.COM", |
2056 | "double_array", |
2057 | ini_config, |
2058 | INI_GET_FIRST_VALUE0, |
2059 | &vo); |
2060 | if (error) { |
2061 | printf("Expected success but got error! %d\n", error); |
2062 | ini_config_destroy(ini_config); |
2063 | return error; |
2064 | } |
2065 | |
2066 | /* Values should be found */ |
2067 | if (vo == NULL((void*)0)) { |
2068 | printf("Expected success but got NULL.\n"); |
2069 | ini_config_destroy(ini_config); |
2070 | return -1; |
2071 | } |
2072 | |
2073 | INIOUT(value_print("double_array", vo))do { if (verbose) value_print("double_array", vo); } while(0); |
2074 | |
2075 | error = 0; |
2076 | size = 0; /* Here size is not optional!!! */ |
2077 | darray = ini_get_double_config_array(vo, &size, &error); |
2078 | if (error) { |
2079 | printf("Expect success got error %d.\n", error); |
2080 | ini_config_destroy(ini_config); |
2081 | return error; |
2082 | } |
2083 | |
2084 | /* Can be used with this cycle */ |
2085 | INIOUT(for (i=0;i<size;i++) printf("%.4f\n", darray[i]))do { if (verbose) for (i=0;i<size;i++) printf("%.4f\n", darray [i]); } while(0); |
2086 | |
2087 | ini_free_double_config_array(darray); |
2088 | |
2089 | INIOUT(printf("\n\nSection list - no size\n"))do { if (verbose) printf("\n\nSection list - no size\n"); } while (0); |
2090 | |
2091 | /* Do not care about the error or size */ |
2092 | prop_array = ini_get_section_list(ini_config, NULL((void*)0), NULL((void*)0)); |
2093 | if (prop_array == NULL((void*)0)) { |
2094 | printf("Expect success got error.\n"); |
2095 | ini_config_destroy(ini_config); |
2096 | return -1; |
2097 | } |
2098 | |
2099 | i = 0; |
2100 | INIOUT(while (prop_array[i]) {do { if (verbose) while (prop_array[i]) { printf("Section: [%s]\n" , prop_array[i]); i++; }; } while(0) |
2101 | printf("Section: [%s]\n", prop_array[i]);do { if (verbose) while (prop_array[i]) { printf("Section: [%s]\n" , prop_array[i]); i++; }; } while(0) |
2102 | i++;do { if (verbose) while (prop_array[i]) { printf("Section: [%s]\n" , prop_array[i]); i++; }; } while(0) |
2103 | })do { if (verbose) while (prop_array[i]) { printf("Section: [%s]\n" , prop_array[i]); i++; }; } while(0); |
2104 | |
2105 | ini_free_section_list(prop_array); |
2106 | |
2107 | INIOUT(printf("\n\nSection list - with size\n"))do { if (verbose) printf("\n\nSection list - with size\n"); } while(0); |
2108 | |
2109 | /* Do not care about the error or size */ |
2110 | prop_array = ini_get_section_list(ini_config, &size, NULL((void*)0)); |
2111 | if (prop_array == NULL((void*)0)) { |
2112 | printf("Expect success got error.\n"); |
2113 | ini_config_destroy(ini_config); |
2114 | return -1; |
2115 | } |
2116 | |
2117 | INIOUT(for (i=0;i<size;i++) printf("Section: [%s]\n", prop_array[i]))do { if (verbose) for (i=0;i<size;i++) printf("Section: [%s]\n" , prop_array[i]); } while(0); |
2118 | ini_free_section_list(prop_array); |
2119 | |
2120 | INIOUT(printf("\n\nAttributes in the section - with size and error\n"))do { if (verbose) printf("\n\nAttributes in the section - with size and error\n" ); } while(0); |
2121 | |
2122 | /* Do not care about the error or size */ |
2123 | prop_array = ini_get_attribute_list(ini_config, |
2124 | "domains/EXAMPLE.COM", |
2125 | &size, |
2126 | &error); |
2127 | if (prop_array == NULL((void*)0)) { |
2128 | printf("Expect success got error.\n"); |
2129 | ini_config_destroy(ini_config); |
2130 | return -1; |
2131 | } |
2132 | |
2133 | INIOUT(for (i=0;i<size;i++) printf("Attribute: [%s]\n", prop_array[i]))do { if (verbose) for (i=0;i<size;i++) printf("Attribute: [%s]\n" , prop_array[i]); } while(0); |
2134 | ini_free_attribute_list(prop_array); |
2135 | |
2136 | |
2137 | /***************************************/ |
2138 | /* Test special types */ |
2139 | /***************************************/ |
2140 | INIOUT(printf("Test int32_t\n"))do { if (verbose) printf("Test int32_t\n"); } while(0); |
2141 | |
2142 | vo = NULL((void*)0); |
2143 | error = ini_get_config_valueobj("domains/EXAMPLE.COM", |
2144 | "int32_t", |
2145 | ini_config, |
2146 | INI_GET_FIRST_VALUE0, |
2147 | &vo); |
2148 | if (error) { |
2149 | printf("Expected success but got error! %d\n", error); |
2150 | ini_config_destroy(ini_config); |
2151 | return error; |
2152 | } |
2153 | |
2154 | /* Value should be found */ |
2155 | if (vo == NULL((void*)0)) { |
2156 | printf("Expected success but got NULL.\n"); |
2157 | ini_config_destroy(ini_config); |
2158 | return -1; |
2159 | } |
2160 | |
2161 | INIOUT(value_print("int32_t", vo))do { if (verbose) value_print("int32_t", vo); } while(0); |
2162 | |
2163 | error = 0; |
2164 | val_int32 = ini_get_int32_config_value(vo, 1, 0, &error); |
2165 | if (error) { |
2166 | printf("Expect success got error %d.\n", error); |
2167 | ini_config_destroy(ini_config); |
2168 | return error; |
2169 | } |
2170 | |
2171 | INIOUT(printf("Value: %d\n", val_int32))do { if (verbose) printf("Value: %d\n", val_int32); } while(0 ); |
2172 | |
2173 | /***************************************/ |
2174 | |
2175 | INIOUT(printf("Test uint32_t\n"))do { if (verbose) printf("Test uint32_t\n"); } while(0); |
2176 | |
2177 | vo = NULL((void*)0); |
2178 | error = ini_get_config_valueobj("domains/EXAMPLE.COM", |
2179 | "uint32_t", |
2180 | ini_config, |
2181 | INI_GET_FIRST_VALUE0, |
2182 | &vo); |
2183 | if (error) { |
2184 | printf("Expected success but got error! %d\n", error); |
2185 | ini_config_destroy(ini_config); |
2186 | return error; |
2187 | } |
2188 | |
2189 | /* Valu should be found */ |
2190 | if (vo == NULL((void*)0)) { |
2191 | printf("Expected success but got NULL.\n"); |
2192 | ini_config_destroy(ini_config); |
2193 | return -1; |
2194 | } |
2195 | |
2196 | INIOUT(value_print("uint32_t", vo))do { if (verbose) value_print("uint32_t", vo); } while(0); |
2197 | |
2198 | error = 0; |
2199 | val_uint32 = ini_get_uint32_config_value(vo, 1, 0, &error); |
2200 | if (error) { |
2201 | printf("Expect success got error %d.\n", error); |
2202 | ini_config_destroy(ini_config); |
2203 | return error; |
2204 | } |
2205 | |
2206 | INIOUT(printf("Value: %u\n", val_uint32))do { if (verbose) printf("Value: %u\n", val_uint32); } while( 0); |
2207 | |
2208 | /***************************************/ |
2209 | |
2210 | INIOUT(printf("Test int64_t\n"))do { if (verbose) printf("Test int64_t\n"); } while(0); |
2211 | |
2212 | vo = NULL((void*)0); |
2213 | error = ini_get_config_valueobj("domains/EXAMPLE.COM", |
2214 | "int64_t", |
2215 | ini_config, |
2216 | INI_GET_FIRST_VALUE0, |
2217 | &vo); |
2218 | if (error) { |
2219 | printf("Expected success but got error! %d\n", error); |
2220 | ini_config_destroy(ini_config); |
2221 | return error; |
2222 | } |
2223 | |
2224 | /* Value should be found */ |
2225 | if (vo == NULL((void*)0)) { |
2226 | printf("Expected success but got NULL.\n"); |
2227 | ini_config_destroy(ini_config); |
2228 | return -1; |
2229 | } |
2230 | |
2231 | INIOUT(value_print("int64_t", vo))do { if (verbose) value_print("int64_t", vo); } while(0); |
2232 | |
2233 | error = 0; |
2234 | val_int64 = ini_get_int64_config_value(vo, 1, 0, &error); |
2235 | if (error) { |
2236 | printf("Expect success got error %d.\n", error); |
2237 | ini_config_destroy(ini_config); |
2238 | return error; |
2239 | } |
2240 | |
2241 | INIOUT(printf("Value: %lld\n", (long long)val_int64))do { if (verbose) printf("Value: %lld\n", (long long)val_int64 ); } while(0); |
2242 | |
2243 | /***************************************/ |
2244 | |
2245 | INIOUT(printf("Test uint32_t\n"))do { if (verbose) printf("Test uint32_t\n"); } while(0); |
2246 | |
2247 | vo = NULL((void*)0); |
2248 | error = ini_get_config_valueobj("domains/EXAMPLE.COM", |
2249 | "uint64_t", |
2250 | ini_config, |
2251 | INI_GET_FIRST_VALUE0, |
2252 | &vo); |
2253 | if (error) { |
2254 | printf("Expected success but got error! %d\n", error); |
2255 | ini_config_destroy(ini_config); |
2256 | return error; |
2257 | } |
2258 | |
2259 | /* Value should be found */ |
2260 | if (vo == NULL((void*)0)) { |
2261 | printf("Expected success but got NULL.\n"); |
2262 | ini_config_destroy(ini_config); |
2263 | return -1; |
2264 | } |
2265 | |
2266 | INIOUT(value_print("uint64_t", vo))do { if (verbose) value_print("uint64_t", vo); } while(0); |
2267 | |
2268 | error = 0; |
2269 | val_uint64 = ini_get_uint64_config_value(vo, 1, 0, &error); |
2270 | if (error) { |
2271 | printf("Expect success got error %d.\n", error); |
2272 | ini_config_destroy(ini_config); |
2273 | return error; |
2274 | } |
2275 | |
2276 | INIOUT(printf("Value: %llu\n", (unsigned long long)val_uint64))do { if (verbose) printf("Value: %llu\n", (unsigned long long )val_uint64); } while(0); |
2277 | |
2278 | /***************************************/ |
2279 | |
2280 | INIOUT(printf("Get empty array value object\n"))do { if (verbose) printf("Get empty array value object\n"); } while(0); |
2281 | |
2282 | vo = NULL((void*)0); |
2283 | error = ini_get_config_valueobj("domains/EXAMPLE.COM", |
2284 | "empty_value", |
2285 | ini_config, |
2286 | INI_GET_FIRST_VALUE0, |
2287 | &vo); |
2288 | if(error) { |
2289 | printf("Expected success but got error! %d\n", error); |
2290 | ini_config_destroy(ini_config); |
2291 | return error; |
2292 | } |
2293 | |
2294 | /* Value should be found */ |
2295 | if (vo == NULL((void*)0)) { |
2296 | printf("Expected success but got NULL.\n"); |
2297 | ini_config_destroy(ini_config); |
2298 | return -1; |
2299 | } |
2300 | |
2301 | INIOUT(value_print("empty_value", vo))do { if (verbose) value_print("empty_value", vo); } while(0); |
2302 | |
2303 | error = 0; |
2304 | size = 0; /* Here size is not optional!!! */ |
2305 | strarray = ini_get_string_config_array(vo, ",", &size, &error); |
2306 | if(error) { |
2307 | printf("Expect success got error %d.\n", error); |
2308 | ini_config_destroy(ini_config); |
2309 | return error; |
2310 | } |
2311 | |
2312 | if (size != 0) { |
2313 | for (i=0; i<size; i++) printf("%s\n", *(strarray + i)); |
2314 | printf("Expected size=0, got size=%d\n", size); |
2315 | ini_free_string_config_array(strarray); |
2316 | ini_config_destroy(ini_config); |
2317 | return -1; |
2318 | } |
2319 | |
2320 | ini_free_string_config_array(strarray); |
2321 | |
2322 | /***************************************/ |
2323 | |
2324 | INIOUT(printf("\nGet sequence of the multi-value keys\n"))do { if (verbose) printf("\nGet sequence of the multi-value keys\n" ); } while(0); |
2325 | |
2326 | vo = NULL((void*)0); |
2327 | error = ini_get_config_valueobj("domains/EXAMPLE.COM", |
2328 | "server", |
2329 | ini_config, |
2330 | INI_GET_FIRST_VALUE0, |
2331 | &vo); |
2332 | if(error) { |
2333 | printf("Expected success but got error! %d\n", error); |
2334 | ini_config_destroy(ini_config); |
2335 | return error; |
2336 | } |
2337 | |
2338 | /* Value should be found */ |
2339 | if (vo == NULL((void*)0)) { |
2340 | printf("Expected success but got NULL.\n"); |
2341 | ini_config_destroy(ini_config); |
2342 | return -1; |
2343 | } |
2344 | |
2345 | INIOUT(value_print("server", vo))do { if (verbose) value_print("server", vo); } while(0); |
2346 | |
2347 | do { |
2348 | |
2349 | vo = NULL((void*)0); |
2350 | error = ini_get_config_valueobj("domains/EXAMPLE.COM", |
2351 | "server", |
2352 | ini_config, |
2353 | INI_GET_NEXT_VALUE1, |
2354 | &vo); |
2355 | if(error) { |
2356 | printf("Expected success but got error! %d\n", error); |
2357 | ini_config_destroy(ini_config); |
2358 | return error; |
2359 | } |
2360 | |
2361 | if (vo == NULL((void*)0)) break; |
2362 | |
2363 | INIOUT(value_print("server", vo))do { if (verbose) value_print("server", vo); } while(0); |
2364 | } |
2365 | while(1); |
2366 | |
2367 | /***************************************/ |
2368 | |
2369 | INIOUT(printf("\nGet multi-value keys without prefetching\n"))do { if (verbose) printf("\nGet multi-value keys without prefetching\n" ); } while(0); |
2370 | |
2371 | do { |
2372 | |
2373 | vo = NULL((void*)0); |
2374 | error = ini_get_config_valueobj("domains/EXAMPLE.COM", |
2375 | "server", |
2376 | ini_config, |
2377 | INI_GET_NEXT_VALUE1, |
2378 | &vo); |
2379 | if(error) { |
2380 | printf("Expected success but got error! %d\n", error); |
2381 | ini_config_destroy(ini_config); |
2382 | return error; |
2383 | } |
2384 | |
2385 | if (vo == NULL((void*)0)) break; |
2386 | |
2387 | INIOUT(value_print("server", vo))do { if (verbose) value_print("server", vo); } while(0); |
2388 | } |
2389 | while(1); |
2390 | |
2391 | /***************************************/ |
2392 | |
2393 | INIOUT(printf("\nGet multi-value keys with key interrupt\n"))do { if (verbose) printf("\nGet multi-value keys with key interrupt\n" ); } while(0); |
2394 | |
2395 | i = 0; |
2396 | |
2397 | vo = NULL((void*)0); |
2398 | do { |
2399 | |
2400 | vo = NULL((void*)0); |
2401 | error = ini_get_config_valueobj("domains/EXAMPLE.COM", |
2402 | "server", |
2403 | ini_config, |
2404 | INI_GET_NEXT_VALUE1, |
2405 | &vo); |
2406 | if(error) { |
2407 | printf("Expected success but got error! %d\n", error); |
2408 | ini_config_destroy(ini_config); |
2409 | return error; |
2410 | } |
2411 | |
2412 | if (vo == NULL((void*)0)) break; |
2413 | |
2414 | INIOUT(value_print("server", vo))do { if (verbose) value_print("server", vo); } while(0); |
2415 | i++; |
2416 | |
2417 | if (i==2) { |
2418 | vo = NULL((void*)0); |
2419 | error = ini_get_config_valueobj("domains/EXAMPLE.COM", |
2420 | "empty_value", |
2421 | ini_config, |
2422 | INI_GET_NEXT_VALUE1, |
2423 | &vo); |
2424 | if(error) { |
2425 | printf("Expected success but got error! %d\n", error); |
2426 | ini_config_destroy(ini_config); |
2427 | return error; |
2428 | } |
2429 | } |
2430 | } |
2431 | while(1); |
2432 | |
2433 | if (i != 6) { |
2434 | printf("Expected 6 iterations got %d\n", i); |
2435 | ini_config_destroy(ini_config); |
2436 | return -1; |
2437 | } |
2438 | |
2439 | /***************************************/ |
2440 | |
2441 | INIOUT(printf("\nGet multi-value keys with key interrupt\n"))do { if (verbose) printf("\nGet multi-value keys with key interrupt\n" ); } while(0); |
2442 | |
2443 | i = 0; |
2444 | |
2445 | vo = NULL((void*)0); |
2446 | do { |
2447 | |
2448 | vo = NULL((void*)0); |
2449 | error = ini_get_config_valueobj("domains/EXAMPLE.COM", |
2450 | "server", |
2451 | ini_config, |
2452 | INI_GET_NEXT_VALUE1, |
2453 | &vo); |
2454 | if(error) { |
2455 | printf("Expected success but got error! %d\n", error); |
2456 | ini_config_destroy(ini_config); |
2457 | return error; |
2458 | } |
2459 | |
2460 | if (vo == NULL((void*)0)) break; |
2461 | |
2462 | INIOUT(value_print("server", vo))do { if (verbose) value_print("server", vo); } while(0); |
2463 | i++; |
2464 | |
2465 | if (i==2) { |
2466 | vo = NULL((void*)0); |
2467 | error = ini_get_config_valueobj("domains", |
2468 | "badarray", |
2469 | ini_config, |
2470 | INI_GET_NEXT_VALUE1, |
2471 | &vo); |
2472 | if(error) { |
2473 | printf("Expected success but got error! %d\n", error); |
2474 | ini_config_destroy(ini_config); |
2475 | return error; |
2476 | } |
2477 | } |
2478 | } |
2479 | while(1); |
2480 | |
2481 | if (i != 6) { |
2482 | printf("Expected 6 iterations got %d\n", i); |
2483 | ini_config_destroy(ini_config); |
2484 | return -1; |
2485 | } |
2486 | |
2487 | ini_config_destroy(ini_config); |
2488 | |
2489 | INIOUT(printf("\n<==== GET TEST END =====>\n\n"))do { if (verbose) printf("\n<==== GET TEST END =====>\n\n" ); } while(0); |
2490 | return EOK0; |
2491 | } |
2492 | |
2493 | |
2494 | /* Main function of the unit test */ |
2495 | int main(int argc, char *argv[]) |
2496 | { |
2497 | int error = 0; |
2498 | test_fn tests[] = { read_save_test, |
2499 | read_again_test, |
2500 | merge_values_test, |
2501 | merge_section_test, |
2502 | merge_file_test, |
2503 | startup_test, |
2504 | reload_test, |
2505 | get_test, |
2506 | NULL((void*)0) }; |
2507 | test_fn t; |
2508 | int i = 0; |
2509 | char *var; |
2510 | |
2511 | if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = 1; |
2512 | else { |
2513 | var = getenv("COMMON_TEST_VERBOSE"); |
2514 | if (var) verbose = 1; |
2515 | } |
2516 | |
2517 | INIOUT(printf("Start\n"))do { if (verbose) printf("Start\n"); } while(0); |
2518 | |
2519 | while ((t = tests[i++])) { |
2520 | error = t(); |
2521 | fflush(NULL((void*)0)); |
2522 | if (error) { |
2523 | INIOUT(printf("Failed with error %d!\n", error))do { if (verbose) printf("Failed with error %d!\n", error); } while(0); |
2524 | return error; |
2525 | } |
2526 | } |
2527 | |
2528 | INIOUT(printf("Success!\n"))do { if (verbose) printf("Success!\n"); } while(0); |
2529 | |
2530 | return 0; |
2531 | } |