Bug Summary

File:bld/../ini/ini_config_ut.c
Location:line 781, column 5
Description:Value stored to 'logical' is never read

Annotated Source Code

1/*
2 INI LIBRARY
3
4 Unit test for the INI library.
5
6 Copyright (C) Dmitri Pal <dpal@redhat.com> 2009
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 <stdlib.h>
24#include <stdio.h>
25#include <string.h>
26#include <errno(*__errno_location ()).h>
27#include <unistd.h>
28#include <fcntl.h>
29#include <sys/stat.h>
30#define TRACE_HOME
31#include "trace.h"
32#include "ini_config.h"
33#include "collection.h"
34#include "collection_tools.h"
35
36
37int verbose = 0;
38
39#define COLOUT(foo)do { if (verbose) foo; } while(0) \
40 do { \
41 if (verbose) foo; \
42 } while(0)
43
44
45int basic_test(void)
46{
47 int error;
48 struct collection_item *ini_config = NULL((void*)0);
49 struct collection_item *error_set = NULL((void*)0);
50
51 error = config_for_app("test", NULL((void*)0), NULL((void*)0),
52 &ini_config, INI_STOP_ON_NONE1, &error_set);
53 if (error != EINVAL22) {
54 printf("Expected error EINVAL got somethign else: %d\n", error);
55 return EINVAL22;
56 }
57
58 error = config_for_app("test", "foo", "bar",
59 &ini_config, INI_STOP_ON_ANY0, &error_set);
60 if (error != ENOENT2) {
61 printf("Expected error ENOENT got somethign else: %d\n", error);
62 return ENOENT2;
63 }
64
65 error = config_for_app("test", "./ini.conf", "./ini.d",
66 &ini_config, INI_STOP_ON_NONE1, &error_set);
67 if (error) {
68 printf("Attempt to read configuration returned error: %d\n", error);
69 return error;
70 }
71
72 COLOUT(col_debug_collection(ini_config,COL_TRAVERSE_DEFAULT))do { if (verbose) col_debug_collection(ini_config,0x00000000)
; } while(0)
;
73 COLOUT(col_print_collection(ini_config))do { if (verbose) col_print_collection(ini_config); } while(0
)
;
74 COLOUT(col_print_collection(error_set))do { if (verbose) col_print_collection(error_set); } while(0);
75
76 COLOUT(printf("\n\n----------------------\n"))do { if (verbose) printf("\n\n----------------------\n"); } while
(0)
;
77 /* Output parsing errors (if any) */
78 COLOUT(print_config_parsing_errors(stdout, error_set))do { if (verbose) print_config_parsing_errors(stdout, error_set
); } while(0)
;
79 COLOUT(printf("----------------------\n\n\n"))do { if (verbose) printf("----------------------\n\n\n"); } while
(0)
;
80
81
82 free_ini_config(ini_config);
83 free_ini_config_errors(error_set);
84 return 0;
85}
86
87int single_file(void)
88{
89 int error;
90 struct collection_item *ini_config = NULL((void*)0);
91 struct collection_item *error_set = NULL((void*)0);
92 struct collection_item *metadata = NULL((void*)0);
93 uint32_t flags;
94
95 error = config_from_file("test", "./not_exist_ini.conf",
96 &ini_config, INI_STOP_ON_NONE1, &error_set);
97 if (error) {
98 COLOUT(printf("Attempt to read configuration returned error: %d."do { if (verbose) printf("Attempt to read configuration returned error: %d."
" EXPECTED.\n\n", error); } while(0)
99 " EXPECTED.\n\n", error))do { if (verbose) printf("Attempt to read configuration returned error: %d."
" EXPECTED.\n\n", error); } while(0)
;
100 if(error != ENOENT2) return error;
101 }
102
103 error = config_from_file("test",
104 "./ini.conf",
105 &ini_config,
106 INI_STOP_ON_NONE1,
107 &error_set);
108 if (error) {
109 printf("Attempt to read configuration returned error: %d\n", error);
110 return error;
111 }
112
113 COLOUT(col_debug_collection(ini_config, COL_TRAVERSE_DEFAULT))do { if (verbose) col_debug_collection(ini_config, 0x00000000
); } while(0)
;
114 COLOUT(col_print_collection(ini_config))do { if (verbose) col_print_collection(ini_config); } while(0
)
;
115 COLOUT(col_print_collection(error_set))do { if (verbose) col_print_collection(error_set); } while(0);
116
117 COLOUT(printf("\n\n----------------------\n"))do { if (verbose) printf("\n\n----------------------\n"); } while
(0)
;
118 /* Output parsing errors (if any) */
119 COLOUT(print_file_parsing_errors(stdout, error_set))do { if (verbose) print_file_parsing_errors(stdout, error_set
); } while(0)
;
120 COLOUT(printf("----------------------\n\n\n"))do { if (verbose) printf("----------------------\n\n\n"); } while
(0)
;
121
122
123 free_ini_config(ini_config);
124 free_ini_config_errors(error_set);
125
126 ini_config = NULL((void*)0);
127 error_set = NULL((void*)0);
128
129 COLOUT(printf("TEST WITH METADATA NO PARSE\n"))do { if (verbose) printf("TEST WITH METADATA NO PARSE\n"); } while
(0)
;
130 flags = INI_META_SEC_ACCESS_FLAG0x00000001 |
131 INI_META_SEC_ERROR_FLAG0x00000002 |
132 INI_META_ACTION_NOPARSE0x10000000;
133
134 error = config_from_file_with_metadata("test", "./ini.conf",
135 &ini_config, INI_STOP_ON_NONE1,
136 NULL((void*)0),
137 flags,
138 &metadata);
139 if (error) {
140 printf("Attempt to read configuration returned error: %d\n",error);
141 if (metadata) {
142 printf("\n\nMeta data\n");
143 col_debug_collection(metadata, COL_TRAVERSE_DEFAULT0x00000000);
144 }
145 free_ini_config_metadata(metadata);
146 return error;
147 }
148
149 if (ini_config) {
150 printf("Expected no config but got some.\n");
151 col_debug_collection(ini_config, COL_TRAVERSE_DEFAULT0x00000000);
152 free_ini_config(ini_config);
153 printf("\n\nMeta data\n");
154 col_debug_collection(metadata, COL_TRAVERSE_DEFAULT0x00000000);
155 free_ini_config_metadata(metadata);
156 return EINVAL22;
157 }
158
159 COLOUT(printf("\n\nMeta data\n"))do { if (verbose) printf("\n\nMeta data\n"); } while(0);
160 COLOUT(col_debug_collection(metadata, COL_TRAVERSE_DEFAULT))do { if (verbose) col_debug_collection(metadata, 0x00000000);
} while(0)
;
161 free_ini_config_metadata(metadata);
162
163 COLOUT(printf("\n\n----------------------\n"))do { if (verbose) printf("\n\n----------------------\n"); } while
(0)
;
164
165 error = config_from_file_with_metadata("test", "./ini.conf",
166 &ini_config, INI_STOP_ON_NONE1,
167 &error_set,
168 0,
169 NULL((void*)0));
170 if (error) {
171 printf("Attempt to read configuration returned error: %d\n",error);
172 print_file_parsing_errors(stdoutstdout, error_set);
173 free_ini_config_errors(error_set);
174 return error;
175 }
176
177 COLOUT(printf("\n\n----------------------\n"))do { if (verbose) printf("\n\n----------------------\n"); } while
(0)
;
178 COLOUT(col_debug_collection(ini_config, COL_TRAVERSE_DEFAULT))do { if (verbose) col_debug_collection(ini_config, 0x00000000
); } while(0)
;
179
180 COLOUT(printf("\n\n----------------------\n"))do { if (verbose) printf("\n\n----------------------\n"); } while
(0)
;
181 /* Output parsing errors (if any) */
182 COLOUT(print_file_parsing_errors(stdout, error_set))do { if (verbose) print_file_parsing_errors(stdout, error_set
); } while(0)
;
183 COLOUT(printf("----------------------\n\n\n"))do { if (verbose) printf("----------------------\n\n\n"); } while
(0)
;
184
185
186 free_ini_config(ini_config);
187 free_ini_config_errors(error_set);
188
189 return 0;
190}
191
192int single_fd(void)
193{
194 int error;
195 struct collection_item *ini_config = NULL((void*)0);
196 struct collection_item *error_set = NULL((void*)0);
197 struct collection_item *metadata = NULL((void*)0);
198 uint32_t flags;
199
200 int fd = open("./ini.conf", O_RDONLY00);
201 if (fd < 0) {
202 error = errno(*__errno_location ());
203 printf("Attempt to read configuration returned error: %d\n", error);
204 return error;
205 }
206
207 error = config_from_fd("test", fd, "./ini.conf", &ini_config,
208 INI_STOP_ON_NONE1, &error_set);
209 if (error) {
210 printf("Attempt to read configuration returned error: %d\n",error);
211 return error;
212 }
213
214 COLOUT(col_debug_collection(ini_config, COL_TRAVERSE_DEFAULT))do { if (verbose) col_debug_collection(ini_config, 0x00000000
); } while(0)
;
215 COLOUT(col_print_collection(ini_config))do { if (verbose) col_print_collection(ini_config); } while(0
)
;
216 COLOUT(col_print_collection(error_set))do { if (verbose) col_print_collection(error_set); } while(0);
217
218 COLOUT(printf("\n\n----------------------\n"))do { if (verbose) printf("\n\n----------------------\n"); } while
(0)
;
219 /* Output parsing errors (if any) */
220 COLOUT(print_file_parsing_errors(stdout, error_set))do { if (verbose) print_file_parsing_errors(stdout, error_set
); } while(0)
;
221 COLOUT(printf("----------------------\n\n\n"))do { if (verbose) printf("----------------------\n\n\n"); } while
(0)
;
222
223
224 free_ini_config(ini_config);
225 free_ini_config_errors(error_set);
226 close(fd);
227
228 ini_config = NULL((void*)0);
229 error_set = NULL((void*)0);
230
231 COLOUT(printf("TEST WITH FILE FD & META DATA\n"))do { if (verbose) printf("TEST WITH FILE FD & META DATA\n"
); } while(0)
;
232
233 fd = open("./ini.conf", O_RDONLY00);
234 if (fd < 0) {
235 error = errno(*__errno_location ());
236 printf("Attempt to read configuration returned error: %d\n", error);
237 return error;
238 }
239
240 flags = INI_META_SEC_ACCESS_FLAG0x00000001 |
241 INI_META_SEC_ERROR_FLAG0x00000002 |
242 INI_META_ACTION_NOPARSE0x10000000;
243
244 error = config_from_fd_with_metadata("test", fd,
245 "./ini.conf",
246 &ini_config,
247 INI_STOP_ON_NONE1,
248 &error_set,
249 flags,
250 &metadata);
251 if (error) {
252 printf("Attempt to read configuration returned error: %d\n",error);
253 printf("\n\nErrors\n");
254 print_file_parsing_errors(stdoutstdout, error_set);
255 free_ini_config_errors(error_set);
256 if (metadata) {
257 printf("\n\nMeta data\n");
258 col_debug_collection(metadata, COL_TRAVERSE_DEFAULT0x00000000);
259 }
260 free_ini_config_metadata(metadata);
261 return error;
262 }
263
264 if (ini_config) {
265 printf("Expected no config but got some.\n");
266 col_debug_collection(ini_config, COL_TRAVERSE_DEFAULT0x00000000);
267 free_ini_config(ini_config);
268 return EINVAL22;
269 }
270
271
272 COLOUT(printf("\n\nMeta data\n"))do { if (verbose) printf("\n\nMeta data\n"); } while(0);
273 COLOUT(col_debug_collection(metadata, COL_TRAVERSE_DEFAULT))do { if (verbose) col_debug_collection(metadata, 0x00000000);
} while(0)
;
274 free_ini_config_metadata(metadata);
275
276
277 error = config_from_fd_with_metadata("test", fd,
278 "./ini.conf",
279 &ini_config,
280 INI_STOP_ON_NONE1,
281 &error_set,
282 0,
283 NULL((void*)0));
284
285 close(fd);
286
287 if (error) {
288 printf("Attempt to read configuration returned error: %d\n",error);
289 printf("\n\nErrors\n");
290 print_file_parsing_errors(stdoutstdout, error_set);
291 free_ini_config_errors(error_set);
292 return error;
293 }
294
295 COLOUT(col_debug_collection(ini_config, COL_TRAVERSE_DEFAULT))do { if (verbose) col_debug_collection(ini_config, 0x00000000
); } while(0)
;
296
297 COLOUT(printf("\n\n----------------------\n"))do { if (verbose) printf("\n\n----------------------\n"); } while
(0)
;
298 /* Output parsing errors (if any) */
299 COLOUT(print_file_parsing_errors(stdout, error_set))do { if (verbose) print_file_parsing_errors(stdout, error_set
); } while(0)
;
300 COLOUT(printf("----------------------\n\n\n"))do { if (verbose) printf("----------------------\n\n\n"); } while
(0)
;
301
302
303 free_ini_config(ini_config);
304 free_ini_config_errors(error_set);
305
306 return 0;
307}
308
309int negative_test(void)
310{
311 int error;
312 unsigned int count;
313 struct collection_item *ini_config = NULL((void*)0);
314
315 /* App name is null - expect failure */
316 error = config_for_app(NULL((void*)0),
317 NULL((void*)0),
318 NULL((void*)0),
319 NULL((void*)0),
320 INI_STOP_ON_NONE1,
321 NULL((void*)0));
322 if (!error) {
323 printf("Expected error: %d got success\n", EINVAL22);
324 return -1;
325 }
326
327 /* Config collection storage is NULL - expect failure */
328 error = config_for_app("real",
329 NULL((void*)0),
330 NULL((void*)0),
331 NULL((void*)0),
332 INI_STOP_ON_NONE1,
333 NULL((void*)0));
334 if (!error) {
335 printf("Expected error: %d got success\n", EINVAL22);
336 return -1;
337 }
338
339 /* Config collection storage is NULL - expect failure */
340 error = config_for_app("real",
341 "real.conf",
342 NULL((void*)0),
343 NULL((void*)0),
344 INI_STOP_ON_NONE1,
345 NULL((void*)0));
346 if (!error) {
347 printf("Expected error: %d got success\n", EINVAL22);
348 return -1;
349 }
350
351 /* Expect success but empty config */
352 error = config_for_app("real",
353 "real.conf",
354 NULL((void*)0),
355 &ini_config,
356 INI_STOP_ON_NONE1,
357 NULL((void*)0));
358 if (error) {
359 printf("Expected success got error: %d\n",error);
360 return error;
361 }
362
363 count = 0;
364 (void)col_get_collection_count(ini_config, &count);
365 if (count > 1) {
366 printf("Expected empty collection but"
367 " got contents with %d elements\n", count);
368 col_print_collection(ini_config);
369 return -1;
370 }
371
372 free_ini_config(ini_config);
373 return 0;
374
375}
376
377int real_test(const char *file)
378{
379 int error;
380 struct collection_item *ini_config = NULL((void*)0);
381 struct collection_item *error_set = NULL((void*)0);
382 struct collection_iterator *iterator = NULL((void*)0);
383 struct collection_item *item = NULL((void*)0);
384 int type;
385
386 COLOUT(printf("\n\n===== REAL TEST START ======\n"))do { if (verbose) printf("\n\n===== REAL TEST START ======\n"
); } while(0)
;
387 COLOUT(printf("Reading collection\n"))do { if (verbose) printf("Reading collection\n"); } while(0);
388 error = config_for_app("real", file, "./ini.d",
389 &ini_config, INI_STOP_ON_NONE1, &error_set);
390 if (error) {
391 printf("Attempt to read configuration returned error: %d\n", error);
392 return error;
393 }
394
395 COLOUT(printf("Debugging the config collection:\n"))do { if (verbose) printf("Debugging the config collection:\n"
); } while(0)
;
396 COLOUT(col_debug_collection(ini_config, COL_TRAVERSE_DEFAULT))do { if (verbose) col_debug_collection(ini_config, 0x00000000
); } while(0)
;
397 COLOUT(printf("Debugging the error collection:\n"))do { if (verbose) printf("Debugging the error collection:\n")
; } while(0)
;
398 COLOUT(col_debug_collection(error_set, COL_TRAVERSE_DEFAULT))do { if (verbose) col_debug_collection(error_set, 0x00000000)
; } while(0)
;
399
400 COLOUT(printf("About to print parsing errors:\n"))do { if (verbose) printf("About to print parsing errors:\n");
} while(0)
;
401 COLOUT(printf("\n\n----------------------\n"))do { if (verbose) printf("\n\n----------------------\n"); } while
(0)
;
402 /* Output parsing errors (if any) */
403 COLOUT(print_config_parsing_errors(stdout, error_set))do { if (verbose) print_config_parsing_errors(stdout, error_set
); } while(0)
;
404 COLOUT(printf("----------------------\n\n\n"))do { if (verbose) printf("----------------------\n\n\n"); } while
(0)
;
405
406 COLOUT(printf("About to bind iterator to print"do { if (verbose) printf("About to bind iterator to print" " the config file contents.\n"
); } while(0)
407 " the config file contents.\n"))do { if (verbose) printf("About to bind iterator to print" " the config file contents.\n"
); } while(0)
;
408 /* Bind iterator */
409 error = col_bind_iterator(&iterator, ini_config,
410 COL_TRAVERSE_DEFAULT0x00000000|COL_TRAVERSE_END0x00000002);
411 if (error) {
412 printf("Failed to bind iterator: %d\n",error);
413 col_destroy_collection(ini_config);
414 col_destroy_collection(error_set);
415 return error;
416 }
417
418 COLOUT(printf("About to start iteration loop.\n"))do { if (verbose) printf("About to start iteration loop.\n");
} while(0)
;
419 do {
420 /* Loop through a collection */
421 error = col_iterate_collection(iterator, &item);
422 if (error) {
423 printf("Error iterating collection: %d", error);
424 col_unbind_iterator(iterator);
425 return error;
426 }
427
428 /* Are we done ? */
429 if (item == NULL((void*)0)) break;
430
431 type = col_get_item_type(item);
432
433 /* Start of the collection */
434 if (type == COL_TYPE_COLLECTION0x00000100)
435 COLOUT(printf("Contents of the application's configuration %s\n",do { if (verbose) printf("Contents of the application's configuration %s\n"
, col_get_item_property(item, ((void*)0))); } while(0)
436 col_get_item_property(item, NULL)))do { if (verbose) printf("Contents of the application's configuration %s\n"
, col_get_item_property(item, ((void*)0))); } while(0)
;
437 /* End of section */
438 else if (type == COL_TYPE_END0x10000000) COLOUT(printf("\n"))do { if (verbose) printf("\n"); } while(0);
439 /* Section header ? */
440 else if (type == COL_TYPE_COLLECTIONREF0x00000200)
441 COLOUT(printf("[%s]\n", col_get_item_property(item, NULL)))do { if (verbose) printf("[%s]\n", col_get_item_property(item
, ((void*)0))); } while(0)
;
442 /* Anything else - we know they are all strings*/
443 else
444 COLOUT(printf("%s = %s\n",do { if (verbose) printf("%s = %s\n", col_get_item_property(item
, ((void*)0)), (char *)col_get_item_data(item)); } while(0)
445 col_get_item_property(item, NULL),do { if (verbose) printf("%s = %s\n", col_get_item_property(item
, ((void*)0)), (char *)col_get_item_data(item)); } while(0)
446 (char *)col_get_item_data(item)))do { if (verbose) printf("%s = %s\n", col_get_item_property(item
, ((void*)0)), (char *)col_get_item_data(item)); } while(0)
;
447 }
448 while(1);
449
450 /* Do not forget to unbind iterator - otherwise there will be a leak */
451 COLOUT(printf("About to clean up.\n"))do { if (verbose) printf("About to clean up.\n"); } while(0);
452 col_unbind_iterator(iterator);
453
454 free_ini_config(ini_config);
455 free_ini_config_errors(error_set);
456 return 0;
457}
458
459int get_test(void)
460{
461
462 int error;
463 struct collection_item *ini_config = NULL((void*)0);
464 struct collection_item *error_set = NULL((void*)0);
465 struct collection_item *item = NULL((void*)0);
466 int number;
467 long number_long;
468 double number_double;
469 unsigned number_unsigned;
470 unsigned long number_ulong;
471 unsigned char logical;
472 char *str;
473 const char *cstr;
474 const char *cstrn;
475 void *binary;
476 int length;
477 int i = 0;
478 char **strarray;
479 char **strptr;
480 int size;
481 long *array;
482 double *darray;
483 char **prop_array;
484 int32_t val_int32;
485 uint32_t val_uint32;
486 int64_t val_int64;
487 uint64_t val_uint64;
488
489
490 COLOUT(printf("\n\n===== GET TEST START ======\n"))do { if (verbose) printf("\n\n===== GET TEST START ======\n")
; } while(0)
;
491 COLOUT(printf("Reading collection\n"))do { if (verbose) printf("Reading collection\n"); } while(0);
492
493 error = config_for_app("real", NULL((void*)0), "./ini.d",
494 &ini_config, INI_STOP_ON_NONE1, &error_set);
495 if (error) {
496 printf("Attempt to read configuration returned error: %d\n", error);
497 return error;
498 }
499
500 COLOUT(printf("Debugging the config collection:\n"))do { if (verbose) printf("Debugging the config collection:\n"
); } while(0)
;
501 COLOUT(col_debug_collection(ini_config, COL_TRAVERSE_DEFAULT))do { if (verbose) col_debug_collection(ini_config, 0x00000000
); } while(0)
;
502 COLOUT(printf("Debugging the error collection:\n"))do { if (verbose) printf("Debugging the error collection:\n")
; } while(0)
;
503 COLOUT(col_debug_collection(error_set, COL_TRAVERSE_DEFAULT))do { if (verbose) col_debug_collection(error_set, 0x00000000)
; } while(0)
;
504 free_ini_config_errors(error_set);
505
506 COLOUT(printf("Negtive test - trying to get non"do { if (verbose) printf("Negtive test - trying to get non" " existing key-value pair.\n"
); } while(0)
507 " existing key-value pair.\n"))do { if (verbose) printf("Negtive test - trying to get non" " existing key-value pair.\n"
); } while(0)
;
508
509 /* Negative test */
510 item = NULL((void*)0);
511 error = get_config_item("monitor1", "description1", ini_config, &item);
512 if (error) {
513 printf("Expected success but got error! %d\n", error);
514 free_ini_config(ini_config);
515 return error;
516 }
517
518 /* Item should not be found */
519 if (item != NULL((void*)0)) {
520 printf("Expected NULL but got something else!\n");
521 free_ini_config(ini_config);
522 return -1;
523 }
524
525 /* Another negative test but section exists this time */
526 item = NULL((void*)0);
527 error = get_config_item("monitor", "description1", ini_config, &item);
528 if (error) {
529 printf("Expected success but got error! %d\n", error);
530 free_ini_config(ini_config);
531 return error;
532 }
533
534 /* Item should not be found */
535 if(item != NULL((void*)0)) {
536 printf("Expected NULL but got something else!\n");
537 free_ini_config(ini_config);
538 return -1;
539 }
540
541 COLOUT(printf("Trying to get an item.\n"))do { if (verbose) printf("Trying to get an item.\n"); } while
(0)
;
542
543 /* Positive test */
544 item = NULL((void*)0);
545 error = get_config_item("monitor", "description", ini_config, &item);
546 if (error) {
547 printf("Expected success but got error! %d\n", error);
548 free_ini_config(ini_config);
549 return error;
550 }
551
552 /* Item should be found */
553 if (item == NULL((void*)0)) {
554 printf("Expected item but got something NULL!\n");
555 free_ini_config(ini_config);
556 return -1;
557 }
558
559 COLOUT(col_debug_item(item))do { if (verbose) col_debug_item(item); } while(0);
560
561 COLOUT(printf("Get item as string without duplication"do { if (verbose) printf("Get item as string without duplication"
" from the NULL item.\n"); } while(0)
562 " from the NULL item.\n"))do { if (verbose) printf("Get item as string without duplication"
" from the NULL item.\n"); } while(0)
;
563
564 /* Get a string without duplicication */
565 /* Negative test */
566 cstrn = get_const_string_config_value(NULL((void*)0), NULL((void*)0));
567 if (cstrn != NULL((void*)0)) {
568 printf("Expected error got success.\n");
569 free_ini_config(ini_config);
570 return -1;
571 }
572
573 COLOUT(printf("Get item as string without duplication"do { if (verbose) printf("Get item as string without duplication"
"from the correct item.\n"); } while(0)
574 "from the correct item.\n"))do { if (verbose) printf("Get item as string without duplication"
"from the correct item.\n"); } while(0)
;
575
576 /* Now get string from the right item */
577 error = 0;
578 cstr = get_const_string_config_value(item, &error);
579 if (error) {
580 printf("Expected success got error %d.\n", error);
581 free_ini_config(ini_config);
582 return error;
583 }
584
585 COLOUT(printf("Value: [%s]\n", cstr))do { if (verbose) printf("Value: [%s]\n", cstr); } while(0);
586
587 /* Same thing but create a dup */
588
589 COLOUT(printf("Get item as string with duplication"do { if (verbose) printf("Get item as string with duplication"
" from correct item.\n"); } while(0)
590 " from correct item.\n"))do { if (verbose) printf("Get item as string with duplication"
" from correct item.\n"); } while(0)
;
591
592 error = 0;
593 str = get_string_config_value(item, &error);
594 if (error) {
595 printf("Expected success got error %d.\n", error);
596 free_ini_config(ini_config);
597 return error;
598 }
599
600 COLOUT(printf("Value: [%s]\n", str))do { if (verbose) printf("Value: [%s]\n", str); } while(0);
601 free(str);
602
603
604 /* Get a badly formated number */
605 COLOUT(printf("Convert item to number with strict conversion.\n"))do { if (verbose) printf("Convert item to number with strict conversion.\n"
); } while(0)
;
606
607 item = NULL((void*)0);
608 error = get_config_item("monitor", "bad_number", ini_config, &item);
609 if (error) {
610 printf("Expected success but got error! %d\n", error);
611 free_ini_config(ini_config);
612 return error;
613 }
614
615 /* Item should be found */
616 if (item == NULL((void*)0)) {
617 printf("Expected item but got something NULL!\n");
618 free_ini_config(ini_config);
619 return -1;
620 }
621
622 COLOUT(col_debug_item(item))do { if (verbose) col_debug_item(item); } while(0);
623
624
625 /* Now try to get value in different ways */
626 error = 0;
627 number = get_int_config_value(item, 1, 10, &error);
628 if (error) {
629 /* We expected error in this case */
630 COLOUT(printf("Expected error.\n"))do { if (verbose) printf("Expected error.\n"); } while(0);
631 if(number != 10) {
632 printf("It failed to set default value.\n");
633 free_ini_config(ini_config);
634 return -1;
635 }
636 }
637
638 COLOUT(printf("Convert item to number without strict conversion.\n"))do { if (verbose) printf("Convert item to number without strict conversion.\n"
); } while(0)
;
639
640 error = 0;
641 number = 1;
642 number = get_int_config_value(item, 0, 10, &error);
643 if (error) {
644 /* We expected error in this case */
645 printf("Did not expect error.\n");
646 free_ini_config(ini_config);
647 return error;
648 }
649
650 if (number != 5) {
651 /* We expected error in this case */
652 printf("We expected that the conversion will return 5.\n");
653 free_ini_config(ini_config);
654 return -1;
655 }
656
657 /* Get real integer */
658
659 COLOUT(printf("Fetch another item from section \"domains/LOCAL\""do { if (verbose) printf("Fetch another item from section \"domains/LOCAL\""
" named \"enumerate\".\n"); } while(0)
660 " named \"enumerate\".\n"))do { if (verbose) printf("Fetch another item from section \"domains/LOCAL\""
" named \"enumerate\".\n"); } while(0)
;
661
662 item = NULL((void*)0);
663 error = get_config_item("domains/LOCAL","enumerate", ini_config, &item);
664 if (error) {
665 printf("Expected success but got error! %d\n", error);
666 free_ini_config(ini_config);
667 return error;
668 }
669
670 /* Item should be found */
671 if (item == NULL((void*)0)) {
672 printf("Expected success but got NULL.\n");
673 free_ini_config(ini_config);
674 return -1;
675 }
676
677 COLOUT(printf("Convert item to integer.\n"))do { if (verbose) printf("Convert item to integer.\n"); } while
(0)
;
678
679 /* Take number out of it */
680 error = 0;
681 number = get_int_config_value(item, 1, 100, &error);
682 if (error) {
683 printf("Did not expect error. Got %d\n", error);
684 free_ini_config(ini_config);
685 return error;
686 }
687
688 /* It is 3 in the file */
689 if (number != 3) {
690 printf("We expected that the conversion will return 3.\n");
691 free_ini_config(ini_config);
692 return -1;
693 }
694
695 COLOUT(printf("Expected 3 got %d\n", number))do { if (verbose) printf("Expected 3 got %d\n", number); } while
(0)
;
696
697 COLOUT(printf("Convert item to long.\n"))do { if (verbose) printf("Convert item to long.\n"); } while(
0)
;
698
699 /* Take number out of it */
700 error = 0;
701 number_long = get_long_config_value(item, 1, 100, &error);
702 if (error) {
703 printf("Did not expect error. Got %d\n", error);
704 free_ini_config(ini_config);
705 return error;
706 }
707
708 /* It is 3 in the file */
709 if (number_long != 3) {
710 printf("We expected that the conversion will return 3.\n");
711 free_ini_config(ini_config);
712 return -1;
713 }
714
715 COLOUT(printf("Expected 3 got %ld\n", number_long))do { if (verbose) printf("Expected 3 got %ld\n", number_long)
; } while(0)
;
716
717 COLOUT(printf("Convert item to unsigned.\n"))do { if (verbose) printf("Convert item to unsigned.\n"); } while
(0)
;
718
719 /* Take number out of it */
720 error = 0;
721 number_unsigned = get_unsigned_config_value(item, 1, 100, &error);
722 if (error) {
723 printf("Did not expect error. Got %d\n", error);
724 free_ini_config(ini_config);
725 return error;
726 }
727
728 /* It is 3 in the file */
729 if(number_unsigned != 3) {
730 printf("We expected that the conversion will return 3.\n");
731 free_ini_config(ini_config);
732 return -1;
733 }
734
735 COLOUT(printf("Expected 3 got %d\n", number_unsigned))do { if (verbose) printf("Expected 3 got %d\n", number_unsigned
); } while(0)
;
736
737 COLOUT(printf("Convert item to unsigned long.\n"))do { if (verbose) printf("Convert item to unsigned long.\n");
} while(0)
;
738
739 /* Take number out of it */
740 error = 0;
741 number_ulong = get_ulong_config_value(item, 1, 100, &error);
742 if (error) {
743 printf("Did not expect error. Got %d\n", error);
744 free_ini_config(ini_config);
745 return error;
746 }
747
748 /* It is 3 in the file */
749 if (number_ulong != 3) {
750 printf("We expected that the conversion will return 3.\n");
751 free_ini_config(ini_config);
752 return -1;
753 }
754
755 COLOUT(printf("Expected 3 got %lu\n", number_ulong))do { if (verbose) printf("Expected 3 got %lu\n", number_ulong
); } while(0)
;
756
757 COLOUT(printf("Convert item to double.\n"))do { if (verbose) printf("Convert item to double.\n"); } while
(0)
;
758
759 /* Take number out of it */
760 error = 0;
761 number_double = get_double_config_value(item, 1, 100., &error);
762 if (error) {
763 printf("Did not expect error. Got %d\n", error);
764 free_ini_config(ini_config);
765 return error;
766 }
767
768 /* It is 3 in the file */
769 if (number_double != 3.) {
770 printf("We expected that the conversion will return 3.\n");
771 free_ini_config(ini_config);
772 return -1;
773 }
774
775 COLOUT(printf("Expected 3 got %e\n", number_double))do { if (verbose) printf("Expected 3 got %e\n", number_double
); } while(0)
;
776
777 COLOUT(printf("Convert item to bool.\n"))do { if (verbose) printf("Convert item to bool.\n"); } while(
0)
;
778
779 /* Take number out of it */
780 error = 0;
781 logical = get_bool_config_value(item, 1, &error);
Value stored to 'logical' is never read
782 if (!error) {
783 printf("Expect error. Got success.\n");
784 free_ini_config(ini_config);
785 return -1;
786 }
787
788 /* Get real bool item and convert it */
789 COLOUT(printf("Get real bool item \"legacy\" and convert it.\n"))do { if (verbose) printf("Get real bool item \"legacy\" and convert it.\n"
); } while(0)
;
790
791 item = NULL((void*)0);
792 error = get_config_item("domains/LOCAL","legacy", ini_config, &item);
793 if (error) {
794 printf("Expected success but got error! %d\n",error);
795 free_ini_config(ini_config);
796 return error;
797 }
798
799 /* Item should be found */
800 if (item == NULL((void*)0)) {
801 printf("Expected success but got NULL.\n");
802 free_ini_config(ini_config);
803 return -1;
804 }
805
806 COLOUT(printf("Convert item to bool.\n"))do { if (verbose) printf("Convert item to bool.\n"); } while(
0)
;
807
808 error = 0;
809 logical = get_bool_config_value(item, 1, &error);
810 if (error) {
811 printf("Expect success got error %d.\n", error);
812 free_ini_config(ini_config);
813 return error;
814 }
815
816 if (logical) {
817 printf("Expected false but got true - bad.\n");
818 return -1;
819 }
820
821 COLOUT(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)
;
822
823 COLOUT(printf("Get binary item\n"))do { if (verbose) printf("Get binary item\n"); } while(0);
824
825 item = NULL((void*)0);
826 error = get_config_item("domains/EXAMPLE.COM",
827 "binary_test",
828 ini_config,
829 &item);
830 if (error) {
831 printf("Expected success but got error! %d\n", error);
832 free_ini_config(ini_config);
833 return error;
834 }
835
836 /* Item should be found */
837 if (item == NULL((void*)0)) {
838 printf("Expected success but got NULL.\n");
839 free_ini_config(ini_config);
840 return -1;
841 }
842
843 COLOUT(col_debug_item(item))do { if (verbose) col_debug_item(item); } while(0);
844
845 error = 0;
846 binary = get_bin_config_value(item, &length, &error);
847 if (error) {
848 printf("Expect success got error %d.\n", error);
849 free_ini_config(ini_config);
850 return error;
851 }
852
853 COLOUT(printf("Binary value (expect 123) = "))do { if (verbose) printf("Binary value (expect 123) = "); } while
(0)
;
854 COLOUT(for (i=0;i<length;i++) {do { if (verbose) for (i=0;i<length;i++) { printf("%d",*((
unsigned char*)(binary)+i)); }; } while(0)
855 printf("%d",*((unsigned char*)(binary)+i));do { if (verbose) for (i=0;i<length;i++) { printf("%d",*((
unsigned char*)(binary)+i)); }; } while(0)
856 })do { if (verbose) for (i=0;i<length;i++) { printf("%d",*((
unsigned char*)(binary)+i)); }; } while(0)
;
857 COLOUT(printf("\n"))do { if (verbose) printf("\n"); } while(0);
858
859 free_bin_config_value(binary);
860
861 COLOUT(printf("Get string array item\n"))do { if (verbose) printf("Get string array item\n"); } while(
0)
;
862
863 item = NULL((void*)0);
864 error = get_config_item("domains", "domainsorder", ini_config, &item);
865 if(error) {
866 printf("Expected success but got error! %d\n",error);
867 free_ini_config(ini_config);
868 return error;
869 }
870
871 /* Item should be found */
872 if (item == NULL((void*)0)) {
873 printf("Expected success but got NULL.\n");
874 free_ini_config(ini_config);
875 return -1;
876 }
877
878 COLOUT(col_debug_item(item))do { if (verbose) col_debug_item(item); } while(0);
879
880 COLOUT(printf("Get str array without size.\n"))do { if (verbose) printf("Get str array without size.\n"); } while
(0)
;
881
882 error = 0;
883 strarray = get_string_config_array(item, ",", NULL((void*)0), &error);
884 if (error) {
885 printf("Expect success got error %d.\n", error);
886 free_ini_config(ini_config);
887 return error;
888 }
889
890 /* Can be used with this cycle */
891 strptr = strarray;
892 while (*strptr != NULL((void*)0)) {
893 COLOUT(printf("[%s]\n",*strptr))do { if (verbose) printf("[%s]\n",*strptr); } while(0);
894 strptr++;
895 }
896
897 free_string_config_array(strarray);
898
899 COLOUT(printf("Get raw str array without size.\n"))do { if (verbose) printf("Get raw str array without size.\n")
; } while(0)
;
900
901 error = 0;
902 strarray = get_raw_string_config_array(item, ",", NULL((void*)0), &error);
903 if (error) {
904 printf("Expect success got error %d.\n", error);
905 free_ini_config(ini_config);
906 return error;
907 }
908
909 /* Can be used with this cycle */
910 strptr = strarray;
911 while (*strptr != NULL((void*)0)) {
912 COLOUT(printf("[%s]\n",*strptr))do { if (verbose) printf("[%s]\n",*strptr); } while(0);
913 strptr++;
914 }
915
916 free_string_config_array(strarray);
917
918 COLOUT(printf("Get str array with size.\n"))do { if (verbose) printf("Get str array with size.\n"); } while
(0)
;
919
920 error = 0;
921 size = 0;
922 strarray = get_string_config_array(item, ",", &size, &error);
923 if (error) {
924 printf("Expect success got error %d.\n", error);
925 free_ini_config(ini_config);
926 return error;
927 }
928
929 /* Can be used with this cycle */
930 COLOUT(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)
;
931
932 free_string_config_array(strarray);
933
934 COLOUT(printf("Get raw str array with size.\n"))do { if (verbose) printf("Get raw str array with size.\n"); }
while(0)
;
935
936 error = 0;
937 size = 0;
938 strarray = get_raw_string_config_array(item, ",", &size, &error);
939 if (error) {
940 printf("Expect success got error %d.\n", error);
941 free_ini_config(ini_config);
942 return error;
943 }
944
945 /* Can be used with this cycle */
946 COLOUT(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)
;
947
948 free_string_config_array(strarray);
949
950 /**********************************************************/
951
952 COLOUT(printf("Get bad string array \n"))do { if (verbose) printf("Get bad string array \n"); } while(
0)
;
953
954 item = NULL((void*)0);
955 error = get_config_item("domains", "badarray", ini_config, &item);
956 if(error) {
957 printf("Expected success but got error! %d\n",error);
958 free_ini_config(ini_config);
959 return error;
960 }
961
962 /* Item should be found */
963 if (item == NULL((void*)0)) {
964 printf("Expected success but got NULL.\n");
965 free_ini_config(ini_config);
966 return -1;
967 }
968
969 COLOUT(col_debug_item(item))do { if (verbose) col_debug_item(item); } while(0);
970
971 COLOUT(printf("Get bad str array without size.\n"))do { if (verbose) printf("Get bad str array without size.\n")
; } while(0)
;
972
973 error = 0;
974 strarray = get_string_config_array(item, ",", NULL((void*)0), &error);
975 if (error) {
976 printf("Expect success got error %d.\n", error);
977 free_ini_config(ini_config);
978 return error;
979 }
980
981 /* Can be used with this cycle */
982 strptr = strarray;
983 while (*strptr != NULL((void*)0)) {
984 COLOUT(printf("[%s]\n",*strptr))do { if (verbose) printf("[%s]\n",*strptr); } while(0);
985 strptr++;
986 }
987
988 free_string_config_array(strarray);
989
990 /**********************************************************/
991
992 COLOUT(printf("Get long array item\n"))do { if (verbose) printf("Get long array item\n"); } while(0);
993
994 item = NULL((void*)0);
995 error = get_config_item("domains/EXAMPLE.COM",
996 "long_array",
997 ini_config,
998 &item);
999 if(error) {
1000 printf("Expected success but got error! %d\n", error);
1001 free_ini_config(ini_config);
1002 return error;
1003 }
1004
1005 /* Item should be found */
1006 if (item == NULL((void*)0)) {
1007 printf("Expected success but got NULL.\n");
1008 free_ini_config(ini_config);
1009 return -1;
1010 }
1011
1012 COLOUT(col_debug_item(item))do { if (verbose) col_debug_item(item); } while(0);
1013
1014 error = 0;
1015 size = 0; /* Here size is not optional!!! */
1016 array = get_long_config_array(item, &size, &error);
1017 if(error) {
1018 printf("Expect success got error %d.\n", error);
1019 free_ini_config(ini_config);
1020 return error;
1021 }
1022
1023 /* Can be used with this cycle */
1024 COLOUT(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)
;
1025
1026 free_long_config_array(array);
1027
1028 COLOUT(printf("Get double array item\n"))do { if (verbose) printf("Get double array item\n"); } while(
0)
;
1029
1030 item = NULL((void*)0);
1031 error = get_config_item("domains/EXAMPLE.COM",
1032 "double_array",
1033 ini_config,
1034 &item);
1035 if (error) {
1036 printf("Expected success but got error! %d\n", error);
1037 free_ini_config(ini_config);
1038 return error;
1039 }
1040
1041 /* Item should be found */
1042 if (item == NULL((void*)0)) {
1043 printf("Expected success but got NULL.\n");
1044 free_ini_config(ini_config);
1045 return -1;
1046 }
1047
1048 COLOUT(col_debug_item(item))do { if (verbose) col_debug_item(item); } while(0);
1049
1050 error = 0;
1051 size = 0; /* Here size is not optional!!! */
1052 darray = get_double_config_array(item, &size, &error);
1053 if (error) {
1054 printf("Expect success got error %d.\n", error);
1055 free_ini_config(ini_config);
1056 return error;
1057 }
1058
1059 /* Can be used with this cycle */
1060 COLOUT(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)
;
1061
1062 free_double_config_array(darray);
1063
1064 COLOUT(printf("\n\nSection list - no size\n"))do { if (verbose) printf("\n\nSection list - no size\n"); } while
(0)
;
1065
1066 /* Do not care about the error or size */
1067 prop_array = get_section_list(ini_config, NULL((void*)0), NULL((void*)0));
1068 if (prop_array == NULL((void*)0)) {
1069 printf("Expect success got error.\n");
1070 free_ini_config(ini_config);
1071 return -1;
1072 }
1073
1074 i = 0;
1075 COLOUT(while (prop_array[i]) {do { if (verbose) while (prop_array[i]) { printf("Section: [%s]\n"
, prop_array[i]); i++; }; } while(0)
1076 printf("Section: [%s]\n", prop_array[i]);do { if (verbose) while (prop_array[i]) { printf("Section: [%s]\n"
, prop_array[i]); i++; }; } while(0)
1077 i++;do { if (verbose) while (prop_array[i]) { printf("Section: [%s]\n"
, prop_array[i]); i++; }; } while(0)
1078 })do { if (verbose) while (prop_array[i]) { printf("Section: [%s]\n"
, prop_array[i]); i++; }; } while(0)
;
1079
1080 free_section_list(prop_array);
1081
1082 COLOUT(printf("\n\nSection list - with size\n"))do { if (verbose) printf("\n\nSection list - with size\n"); }
while(0)
;
1083
1084 /* Do not care about the error or size */
1085 prop_array = get_section_list(ini_config, &size, NULL((void*)0));
1086 if (prop_array == NULL((void*)0)) {
1087 printf("Expect success got error.\n");
1088 free_ini_config(ini_config);
1089 return -1;
1090 }
1091
1092 COLOUT(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)
;
1093 free_section_list(prop_array);
1094
1095 COLOUT(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)
;
1096
1097 /* Do not care about the error or size */
1098 prop_array = get_attribute_list(ini_config,
1099 "domains/EXAMPLE.COM",
1100 &size,
1101 &error);
1102 if (prop_array == NULL((void*)0)) {
1103 printf("Expect success got error.\n");
1104 free_ini_config(ini_config);
1105 return -1;
1106 }
1107
1108 COLOUT(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)
;
1109 free_attribute_list(prop_array);
1110
1111
1112 /***************************************/
1113 /* Test special types */
1114 /***************************************/
1115 COLOUT(printf("Test int32_t\n"))do { if (verbose) printf("Test int32_t\n"); } while(0);
1116
1117 item = NULL((void*)0);
1118 error = get_config_item("domains/EXAMPLE.COM",
1119 "int32_t",
1120 ini_config,
1121 &item);
1122 if (error) {
1123 printf("Expected success but got error! %d\n", error);
1124 free_ini_config(ini_config);
1125 return error;
1126 }
1127
1128 /* Item should be found */
1129 if (item == NULL((void*)0)) {
1130 printf("Expected success but got NULL.\n");
1131 free_ini_config(ini_config);
1132 return -1;
1133 }
1134
1135 COLOUT(col_debug_item(item))do { if (verbose) col_debug_item(item); } while(0);
1136
1137 error = 0;
1138 val_int32 = get_int32_config_value(item, 1, 0, &error);
1139 if (error) {
1140 printf("Expect success got error %d.\n", error);
1141 free_ini_config(ini_config);
1142 return error;
1143 }
1144
1145 COLOUT(printf("Value: %d\n", val_int32))do { if (verbose) printf("Value: %d\n", val_int32); } while(0
)
;
1146
1147 /***************************************/
1148
1149 COLOUT(printf("Test uint32_t\n"))do { if (verbose) printf("Test uint32_t\n"); } while(0);
1150
1151 item = NULL((void*)0);
1152 error = get_config_item("domains/EXAMPLE.COM",
1153 "uint32_t",
1154 ini_config,
1155 &item);
1156 if (error) {
1157 printf("Expected success but got error! %d\n", error);
1158 free_ini_config(ini_config);
1159 return error;
1160 }
1161
1162 /* Item should be found */
1163 if (item == NULL((void*)0)) {
1164 printf("Expected success but got NULL.\n");
1165 free_ini_config(ini_config);
1166 return -1;
1167 }
1168
1169 COLOUT(col_debug_item(item))do { if (verbose) col_debug_item(item); } while(0);
1170
1171 error = 0;
1172 val_uint32 = get_uint32_config_value(item, 1, 0, &error);
1173 if (error) {
1174 printf("Expect success got error %d.\n", error);
1175 free_ini_config(ini_config);
1176 return error;
1177 }
1178
1179 COLOUT(printf("Value: %u\n", val_uint32))do { if (verbose) printf("Value: %u\n", val_uint32); } while(
0)
;
1180
1181 /***************************************/
1182
1183 COLOUT(printf("Test int64_t\n"))do { if (verbose) printf("Test int64_t\n"); } while(0);
1184
1185 item = NULL((void*)0);
1186 error = get_config_item("domains/EXAMPLE.COM",
1187 "int64_t",
1188 ini_config,
1189 &item);
1190 if (error) {
1191 printf("Expected success but got error! %d\n", error);
1192 free_ini_config(ini_config);
1193 return error;
1194 }
1195
1196 /* Item should be found */
1197 if (item == NULL((void*)0)) {
1198 printf("Expected success but got NULL.\n");
1199 free_ini_config(ini_config);
1200 return -1;
1201 }
1202
1203 COLOUT(col_debug_item(item))do { if (verbose) col_debug_item(item); } while(0);
1204
1205 error = 0;
1206 val_int64 = get_int64_config_value(item, 1, 0, &error);
1207 if (error) {
1208 printf("Expect success got error %d.\n", error);
1209 free_ini_config(ini_config);
1210 return error;
1211 }
1212
1213 COLOUT(printf("Value: %lld\n", (long long)val_int64))do { if (verbose) printf("Value: %lld\n", (long long)val_int64
); } while(0)
;
1214
1215 /***************************************/
1216
1217 COLOUT(printf("Test uint32_t\n"))do { if (verbose) printf("Test uint32_t\n"); } while(0);
1218
1219 item = NULL((void*)0);
1220 error = get_config_item("domains/EXAMPLE.COM",
1221 "uint64_t",
1222 ini_config,
1223 &item);
1224 if (error) {
1225 printf("Expected success but got error! %d\n", error);
1226 free_ini_config(ini_config);
1227 return error;
1228 }
1229
1230 /* Item should be found */
1231 if (item == NULL((void*)0)) {
1232 printf("Expected success but got NULL.\n");
1233 free_ini_config(ini_config);
1234 return -1;
1235 }
1236
1237 COLOUT(col_debug_item(item))do { if (verbose) col_debug_item(item); } while(0);
1238
1239 error = 0;
1240 val_uint64 = get_uint64_config_value(item, 1, 0, &error);
1241 if (error) {
1242 printf("Expect success got error %d.\n", error);
1243 free_ini_config(ini_config);
1244 return error;
1245 }
1246
1247 COLOUT(printf("Value: %llu\n", (unsigned long long)val_uint64))do { if (verbose) printf("Value: %llu\n", (unsigned long long
)val_uint64); } while(0)
;
1248
1249 /***************************************/
1250
1251 COLOUT(printf("Get empty array item\n"))do { if (verbose) printf("Get empty array item\n"); } while(0
)
;
1252
1253 item = NULL((void*)0);
1254 error = get_config_item("domains/EXAMPLE.COM",
1255 "empty_value",
1256 ini_config,
1257 &item);
1258 if(error) {
1259 printf("Expected success but got error! %d\n", error);
1260 free_ini_config(ini_config);
1261 return error;
1262 }
1263
1264 /* Item should be found */
1265 if (item == NULL((void*)0)) {
1266 printf("Expected success but got NULL.\n");
1267 free_ini_config(ini_config);
1268 return -1;
1269 }
1270
1271 COLOUT(col_debug_item(item))do { if (verbose) col_debug_item(item); } while(0);
1272
1273 error = 0;
1274 size = 0; /* Here size is not optional!!! */
1275 strarray = get_string_config_array(item, ",", &size, &error);
1276 if(error) {
1277 printf("Expect success got error %d.\n", error);
1278 free_ini_config(ini_config);
1279 return error;
1280 }
1281
1282 if (size != 0) {
1283 for (i=0; i<size; i++) printf("%s\n", *(strarray + i));
1284 printf("Expected size=0, got size=%d\n", size);
1285 free_string_config_array(strarray);
1286 free_ini_config(ini_config);
1287 return -1;
1288 }
1289
1290
1291 free_string_config_array(strarray);
1292
1293 free_ini_config(ini_config);
1294 COLOUT(printf("Done with get test!\n"))do { if (verbose) printf("Done with get test!\n"); } while(0);
1295 return EOK0;
1296}
1297
1298/* This is an emulation of the case when daemon starts
1299 * and one needs to parse the configuration file
1300 * for the first time and load configuration
1301 */
1302int startup_test(void)
1303{
1304 int error;
1305 struct collection_item *ini_config = NULL((void*)0);
1306 struct collection_item *error_set = NULL((void*)0);
1307 struct collection_item *metadata = NULL((void*)0);
1308 uint32_t flags;
1309
1310
1311 /* At startup we can simplify our life by
1312 * parsing configuration and then checking
1313 * the permissions. It is less optimal from
1314 * the performnce point of view but simple to implement.
1315 * Since it is the start of the daemon we can
1316 * hope that parsing the config file would
1317 * usually not a be a wasted effort.
1318 * If permission check fails that means we should
1319 * exit. Ok so we just parse the INI file for nothing.
1320 * Not a big deal, I would say...
1321 */
1322
1323 COLOUT(printf("STARTUP TEST\n"))do { if (verbose) printf("STARTUP TEST\n"); } while(0);
1324
1325 /* Set file permissions to 0664 */
1326 chmod("./ini.conf", 0664);
1327
1328 flags = INI_META_SEC_ACCESS_FLAG0x00000001 |
1329 INI_META_SEC_ERROR_FLAG0x00000002;
1330
1331 error = config_from_file_with_metadata("test", "./ini.conf",
1332 &ini_config, INI_STOP_ON_NONE1,
1333 &error_set,
1334 flags,
1335 &metadata);
1336 /*
1337 * This is just for debugging.
1338 * do not copy into your implementation
1339 */
1340 if (metadata) {
1341 COLOUT(printf("\n\nMeta data\n"))do { if (verbose) printf("\n\nMeta data\n"); } while(0);
1342 COLOUT(col_debug_collection(metadata, COL_TRAVERSE_DEFAULT))do { if (verbose) col_debug_collection(metadata, 0x00000000);
} while(0)
;
1343 }
1344
1345
1346 if (error) {
1347 printf("Attempt to read configuration returned error: %d\n",error);
1348
1349 /* If you want to do any specific error checking, do it here.
1350 * If you want to get the file error code from the
1351 * metadata get it here.
1352 */
1353 free_ini_config_metadata(metadata);
1354
1355 /* Error reporting start ==> */
1356 if (error_set) {
1357 printf("\n\nErrors\n");
1358 col_debug_collection(error_set, COL_TRAVERSE_DEFAULT0x00000000);
1359 }
1360 /* <==== end */
1361 free_ini_config_errors(error_set);
1362 return error;
1363 }
1364
1365 free_ini_config_errors(error_set);
1366
1367 /* So we are here if we successfully got configuration. */
1368 /* You can check ownership and permissions here in one call */
1369 /* We will check just permissions here. */
1370 error = config_access_check(metadata,
1371 INI_ACCESS_CHECK_MODE0x00000001, /* add uid & gui flags
1372 * in real case
1373 */
1374 0, /* <- will be real uid in real case */
1375 0, /* <- will be real gid in real case */
1376 0440, /* Checking for r--r----- */
1377 0);
1378 /* This check is expected to fail since
1379 * the actual permissions on the test file are: rw-rw-r--
1380 */
1381
1382 if (!error) {
1383 printf("Expected error got success!\n");
1384 free_ini_config_metadata(metadata);
1385 free_ini_config(ini_config);
1386 return EACCES13;
1387 }
1388
1389 error = config_access_check(metadata,
1390 INI_ACCESS_CHECK_MODE0x00000001, /* add uid & gui flags
1391 * in real case
1392 */
1393 0, /* <- will be real uid in real case */
1394 0, /* <- will be real gid in real case */
1395 0664, /* Checkling for rw-rw-r-- */
1396 0);
1397
1398 if (error) {
1399 printf("Access check failed %d!\n", error);
1400 free_ini_config_metadata(metadata);
1401 free_ini_config(ini_config);
1402 return EACCES13;
1403 }
1404
1405
1406 /* Use configuration */
1407
1408 COLOUT(printf("\n\nMeta data\n"))do { if (verbose) printf("\n\nMeta data\n"); } while(0);
1409 COLOUT(col_debug_collection(metadata, COL_TRAVERSE_DEFAULT))do { if (verbose) col_debug_collection(metadata, 0x00000000);
} while(0)
;
1410 free_ini_config_metadata(metadata);
1411
1412 COLOUT(printf("\n\n----------------------\n"))do { if (verbose) printf("\n\n----------------------\n"); } while
(0)
;
1413
1414 COLOUT(printf("\n\nConfiguration\n"))do { if (verbose) printf("\n\nConfiguration\n"); } while(0);
1415 COLOUT(col_debug_collection(ini_config, COL_TRAVERSE_DEFAULT))do { if (verbose) col_debug_collection(ini_config, 0x00000000
); } while(0)
;
1416 free_ini_config(ini_config);
1417
1418 return 0;
1419}
1420
1421int reload_test(void)
1422{
1423
1424 int error;
1425 struct collection_item *ini_config = NULL((void*)0);
1426 struct collection_item *metadata = NULL((void*)0);
1427 struct collection_item *saved_metadata = NULL((void*)0);
1428 uint32_t flags;
1429 int changed = 0;
1430 int fd;
1431
1432 COLOUT(printf("RELOAD TEST\n"))do { if (verbose) printf("RELOAD TEST\n"); } while(0);
1433
1434 /* Assume we saved metadata at the beginning
1435 * when we opened the file and read configuration
1436 * for the first time.
1437 * Here we have to emulate it.
1438 */
1439
1440 flags = INI_META_SEC_ACCESS_FLAG0x00000001 |
1441 INI_META_ACTION_NOPARSE0x10000000;
1442
1443 error = config_from_file_with_metadata("test", "./ini.conf",
1444 &ini_config,
1445 0,
1446 NULL((void*)0),
1447 flags,
1448 &saved_metadata);
1449 if (error) {
1450 printf("Attempt to read configuration returned error: %d\n",error);
1451 free_ini_config_metadata(saved_metadata);
1452 return error;
1453 }
1454
1455 /*****************************************/
1456
1457 /* We are reloading so we probably doing it becuase
1458 * we got a signal ot some kind of time out expired
1459 * and it might be time for us to check if we need
1460 * to reload. So assume it is time to check...
1461 */
1462
1463 /* It is safer to open file first */
1464 fd = open("./ini.conf", O_RDONLY00);
1465 if (fd < 0) {
1466 error = errno(*__errno_location ());
1467 printf("Attempt to read configuration returned error: %d\n", error);
1468 free_ini_config_metadata(saved_metadata);
1469 return error;
1470 }
1471
1472 /* You migth be checking pretty frequently, once in 5 min for example
1473 * but the config usually does not change for months
1474 * so you do not want to do any extra processing every time you check.
1475 */
1476
1477 /* Do permission check here right away on the file, or... */
1478
1479
1480 flags = INI_META_SEC_ACCESS_FLAG0x00000001 |
1481 INI_META_ACTION_NOPARSE0x10000000;
1482
1483 error = config_from_fd_with_metadata("test", fd,
1484 "./ini.conf",
1485 &ini_config,
1486 0,
1487 NULL((void*)0),
1488 flags,
1489 &metadata);
1490 if (error) {
1491 printf("Attempt to read configuration returned error: %d\n",error);
1492 if (metadata) {
1493 printf("\n\nMeta data\n");
1494 col_debug_collection(metadata, COL_TRAVERSE_DEFAULT0x00000000);
1495 }
1496 free_ini_config_metadata(metadata);
1497 free_ini_config_metadata(saved_metadata);
1498 close(fd);
1499 return error;
1500 }
1501
1502 /* ...or you can do permission check here using the metadata
1503 * as it is done in the startup test.
1504 * For now we skip this part and move on.
1505 */
1506
1507 error = config_changed(metadata, saved_metadata, &changed);
1508
1509 if (error) {
1510 printf("Internal error: %d\n",error);
1511 printf("\n\nSaved Meta data\n");
1512 col_debug_collection(saved_metadata, COL_TRAVERSE_DEFAULT0x00000000);
1513 printf("\n\nMeta data\n");
1514 col_debug_collection(metadata, COL_TRAVERSE_DEFAULT0x00000000);
1515 free_ini_config_metadata(saved_metadata);
1516 free_ini_config_metadata(metadata);
1517 close(fd);
1518 return error;
1519
1520 }
1521
1522 if (changed) {
1523
1524 /* Read the config from the descriptor and use it.
1525 * Discard old saved meta data and save
1526 * the latest one for future use...
1527 */
1528
1529 /* Here it would be an error if it is different */
1530 printf("Meta data is supposed to be same but different.\n");
1531 printf("\n\nSaved Meta data\n");
1532 col_debug_collection(saved_metadata, COL_TRAVERSE_DEFAULT0x00000000);
1533 printf("\n\nMeta data\n");
1534 col_debug_collection(metadata, COL_TRAVERSE_DEFAULT0x00000000);
1535 }
1536
1537 free_ini_config_metadata(saved_metadata);
1538 free_ini_config_metadata(metadata);
1539 close(fd);
1540
1541 return 0;
1542}
1543
1544
1545int main(int argc, char *argv[])
1546{
1547 int error = EOK0;
1548 char *srcdir = NULL((void*)0);
1549 char *rundir = NULL((void*)0);
1550 const char inidir[] = "/ini";
1551 int len = 0;
1552
1553 if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = 1;
1554
1555 COLOUT(printf("Start\n"))do { if (verbose) printf("Start\n"); } while(0);
1556
1557 srcdir = getenv("srcdir");
1558 if(srcdir) {
1559
1560 len = strlen(srcdir) + sizeof(inidir);
1561 rundir = malloc(len);
1562 if (!rundir) {
1563 printf("Failed to allocate memory to store path"
1564 " to the test files %d.\n", ENOMEM12);
1565 return -1;
1566 }
1567
1568 snprintf(rundir, len, "%s%s", srcdir, inidir);
1569
1570 errno(*__errno_location ()) = 0;
1571 if(chdir(rundir) != 0) {
1572 error = errno(*__errno_location ());
1573 free(rundir);
1574 printf("Failed to change directory, error %d\n", error);
1575 return error;
1576 }
1577 free(rundir);
1578 }
1579
1580 if ((error = basic_test()) ||
1581 (error = single_file()) ||
1582 (error = single_fd()) ||
1583 (error = real_test(NULL((void*)0))) ||
1584 /* This should result in merged configuration */
1585 (error = real_test("./ini.conf")) ||
1586 (error = startup_test()) ||
1587 (error = reload_test()) ||
1588 (error = get_test())) {
1589 printf("Test failed! Error %d.\n", error);
1590 return -1;
1591 }
1592
1593 COLOUT(printf("Success!\n"))do { if (verbose) printf("Success!\n"); } while(0);
1594 return 0;
1595}