1 #include <stdio.h>
2 #include <dirent.h>
3 #include <unistd.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
6 #include <stdarg.h>
7
8 #include "CuTest.h"
9 #include "dictionary.h"
10
11 /* We need to directly insert the .c file in order to test the */
12 /* static functions as well */
13 #include "iniparser.c"
14
15 #define GOOD_INI_PATH "ressources/good_ini"
16 #define BAD_INI_PATH "ressources/bad_ini"
17
18
19 /* Tool function to create and populate a generic non-empty dictionary */
generate_dictionary(unsigned sections,unsigned entries_per_section)20 static dictionary * generate_dictionary(unsigned sections, unsigned entries_per_section)
21 {
22 unsigned i, j ;
23 dictionary * dic;
24 char sec_name[32];
25 char key_name[64];
26 char key_value[32];
27
28 dic = dictionary_new(sections + sections * entries_per_section);
29 if (dic == NULL)
30 return NULL;
31
32 /* Insert the sections */
33 for (i = 0; i < sections; ++i) {
34 sprintf(sec_name, "sec%d", i);
35 dictionary_set(dic, sec_name, "");
36 for (j = 0; j < entries_per_section; ++j) {
37 /* Populate the section with the entries */
38 sprintf(key_name, "%s:key%d", sec_name, j);
39 sprintf(key_value, "value-%d/%d", i, j);
40 dictionary_set(dic, key_name, key_value);
41 }
42 }
43
44 return dic;
45 }
46
Test_iniparser_strlwc(CuTest * tc)47 void Test_iniparser_strlwc(CuTest *tc)
48 {
49 char out_buffer[128];
50
51 /* NULL ptr as input */
52 CuAssertPtrEquals(tc, NULL, strlwc(NULL, NULL, 0));
53 CuAssertPtrEquals(tc, NULL, strlwc(NULL, out_buffer, sizeof (out_buffer)));
54 CuAssertPtrEquals(tc, NULL, strlwc("", NULL, sizeof (out_buffer)));
55 CuAssertPtrEquals(tc, NULL, strlwc("", out_buffer, 0));
56 CuAssertPtrEquals(tc, NULL, strlwc(NULL, NULL, 0));
57
58 /* empty string */
59 CuAssertStrEquals(tc, "", strlwc("", out_buffer, sizeof (out_buffer)));
60
61 CuAssertStrEquals(tc, " ", strlwc(" ", out_buffer, sizeof (out_buffer)));
62 CuAssertStrEquals(tc, "test", strlwc("test", out_buffer, sizeof (out_buffer)));
63 CuAssertStrEquals(tc, "test", strlwc("TEST", out_buffer, sizeof (out_buffer)));
64 CuAssertStrEquals(tc, "test", strlwc("TeSt", out_buffer, sizeof (out_buffer)));
65 CuAssertStrEquals(tc, "test test",
66 strlwc("TEST TEST", out_buffer, sizeof (out_buffer)));
67 CuAssertStrEquals(tc, "very long string !!!!!!!",
68 strlwc("very long string !!!!!!!", out_buffer, sizeof (out_buffer)));
69 CuAssertStrEquals(tc, "cutted string", strlwc("cutted string<---here", out_buffer, 14));
70
71 /* test using same buffer as input and output */
72 strcpy(out_buffer, "OVERWRITE ME !");
73 CuAssertPtrNotNull(tc, strlwc(out_buffer, out_buffer, sizeof(out_buffer)));
74 CuAssertStrEquals(tc, "overwrite me !", out_buffer);
75 }
76
Test_iniparser_strstrip(CuTest * tc)77 void Test_iniparser_strstrip(CuTest *tc)
78 {
79 /* First element in the array is the expected stripping result */
80 const char *strings_empty[] = {
81 "",
82 " ",
83 "\n\n\n\n",
84 "\t\t\t\t",
85 "\n \t\n\t\n "
86 };
87 const char *strings_test[] = {
88 "test",
89 "test ",
90 "test ",
91 " test",
92 " test ",
93 "\ttest\t",
94 "\ttest\n"
95
96 };
97 const char *test_with_spaces = "I am a test with\tspaces.";
98 char stripped[ASCIILINESZ+1];
99 char error_msg[1060];
100 unsigned i;
101
102 /* NULL ptr as input */
103 strstrip(NULL);
104
105 /* empty string */
106 for (i = 0 ; i < sizeof (strings_empty) / sizeof (char *) ; ++i) {
107 strcpy(stripped, strings_empty[i]);
108 strstrip(stripped);
109 sprintf(error_msg, "Bad stripping : strstrip(\"%s\") ==> \"%s\"",
110 strings_empty[i], stripped);
111 CuAssertStrEquals_Msg(tc, error_msg, stripped, strings_empty[0]);
112 }
113
114 /* test string */
115 for (i = 0 ; i < sizeof (strings_test) / sizeof (char *) ; ++i) {
116 strcpy(stripped, strings_test[i]);
117 strstrip(stripped);
118 sprintf(error_msg, "Bad stripping : strstrip(\"%s\") ==> \"%s\"",
119 strings_test[i], stripped);
120 CuAssertStrEquals_Msg(tc, error_msg, strings_test[0], stripped);
121 }
122 strcpy(stripped, ".");
123 strstrip(stripped);
124 CuAssertStrEquals(tc, ".", stripped);
125
126 /* string containing spaces */
127 strcpy(stripped, test_with_spaces);
128 strstrip(stripped);
129 CuAssertStrEquals(tc, test_with_spaces, stripped);
130 }
131
Test_iniparser_getnsec(CuTest * tc)132 void Test_iniparser_getnsec(CuTest *tc)
133 {
134 int i;
135 char sec_name[32];
136 dictionary *dic;
137
138 /* NULL test */
139 CuAssertIntEquals(tc, -1, iniparser_getnsec(NULL));
140
141 /* Empty dictionary */
142 dic = dictionary_new(10);
143 CuAssertIntEquals(tc, 0, iniparser_getnsec(dic));
144 dictionary_del(dic);
145
146 /* Regular dictionary */
147 dic = generate_dictionary(512, 0);
148 CuAssertIntEquals(tc, 512, iniparser_getnsec(dic));
149
150 /* Check after removing sections */
151 for (i = 1; i < 512; ++i) {
152 sprintf(sec_name, "sec%d", i);
153 dictionary_unset(dic, sec_name);
154 CuAssertIntEquals(tc, 512 - i, iniparser_getnsec(dic));
155 }
156 dictionary_del(dic);
157
158 /* Mix sections and regular keys */
159 dic = generate_dictionary(10, 512);
160 CuAssertIntEquals(tc, 10, iniparser_getnsec(dic));
161 dictionary_del(dic);
162 }
163
Test_iniparser_getsecname(CuTest * tc)164 void Test_iniparser_getsecname(CuTest *tc)
165 {
166 unsigned i;
167 char sec_name[32];
168 dictionary *dic;
169 /* NULL test */
170 CuAssertTrue(tc, iniparser_getsecname(NULL, 0) == NULL);
171
172 /* Empty dictionary */
173 dic = dictionary_new(10);
174 CuAssertPtrEquals(tc, NULL, iniparser_getsecname(dic, 0));
175 dictionary_del(dic);
176
177 /* Sections without entries dictionary */
178 dic = generate_dictionary(100, 0);
179 for (i = 0; i < 100; ++i) {
180 sprintf(sec_name, "sec%d", i);
181 CuAssertStrEquals(tc, sec_name, iniparser_getsecname(dic, i));
182 }
183 dictionary_del(dic);
184
185 /* Generic dictionary */
186 dic = generate_dictionary(10, 100);
187 for (i = 0; i < 10; ++i) {
188 sprintf(sec_name, "sec%d", i);
189 CuAssertStrEquals(tc, sec_name, iniparser_getsecname(dic, i));
190 }
191 dictionary_del(dic);
192 }
193
Test_iniparser_getseckeys(CuTest * tc)194 void Test_iniparser_getseckeys(CuTest *tc)
195 {
196 unsigned i;
197 char key_name[64];
198 dictionary *dic;
199 int nkeys;
200 const char * keys[10]; /* At most 10 elements per section */
201 /* NULL test */
202 CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(NULL, NULL, NULL));
203 CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(NULL, "dummy", NULL));
204 CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(NULL, "dummy", keys));
205
206 /* Empty dictionary */
207 dic = dictionary_new(10);
208 CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(dic, NULL, keys));
209 CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(dic, "dummy", keys));
210 dictionary_del(dic);
211
212 /* Generic dictionary */
213
214 dic = generate_dictionary(100, 10);
215 CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(dic, NULL, keys));
216 CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(dic, "dummy", keys));
217 CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(dic, "sec0", NULL));
218 nkeys = iniparser_getsecnkeys(dic, "sec42");
219 CuAssertIntEquals(tc, nkeys, 10);
220 CuAssertPtrEquals(tc, keys, iniparser_getseckeys(dic, "sec42", keys));
221 for (i = 0; i < 10; ++i) {
222 sprintf(key_name, "sec42:key%d", i);
223 CuAssertStrEquals(tc, key_name, keys[i]);
224 }
225
226 /* Remove some keys to make the dictionary more real */
227 dictionary_unset(dic, "sec42");
228 dictionary_unset(dic, "sec99:key9");
229 dictionary_unset(dic, "sec0:key0");
230 dictionary_unset(dic, "sec0:key1");
231 dictionary_unset(dic, "sec0:key2");
232
233 CuAssertPtrEquals(tc, NULL, iniparser_getseckeys(dic, "sec42", keys));
234 nkeys = iniparser_getsecnkeys(dic, "Sec99");
235 CuAssertIntEquals(tc, nkeys, 9);
236 CuAssertPtrEquals(tc, keys, iniparser_getseckeys(dic, "Sec99", keys));
237 for (i = 0; i < 9; ++i) {
238 sprintf(key_name, "sec99:key%d", i);
239 CuAssertStrEquals(tc, key_name, keys[i]);
240 }
241
242 nkeys = iniparser_getsecnkeys(dic, "sec0");
243 CuAssertIntEquals(tc, nkeys, 7);
244 CuAssertPtrEquals(tc, keys, iniparser_getseckeys(dic, "sec0", keys));
245 for (i = 0; i < 7; ++i) {
246 sprintf(key_name, "sec0:key%d", i + 3);
247 CuAssertStrEquals(tc, key_name, keys[i]);
248 }
249
250 dictionary_del(dic);
251 }
252
Test_iniparser_getstring(CuTest * tc)253 void Test_iniparser_getstring(CuTest *tc)
254 {
255 dictionary *dic;
256 /* NULL test */
257 CuAssertPtrEquals(tc, NULL, iniparser_getstring(NULL, NULL, NULL));
258 CuAssertPtrEquals(tc, NULL, iniparser_getstring(NULL, "dummy", NULL));
259
260 /* Check the def return element */
261 dic = dictionary_new(10);
262 CuAssertPtrEquals(tc, NULL, iniparser_getstring(dic, "dummy", NULL));
263 CuAssertStrEquals(tc, "def", iniparser_getstring(dic, NULL, "def"));
264 CuAssertStrEquals(tc, "def", iniparser_getstring(dic, "dummy", "def"));
265 dictionary_del(dic);
266
267 /* Generic dictionary */
268 dic = generate_dictionary(100, 10);
269 CuAssertStrEquals(tc, "value-0/0",
270 iniparser_getstring(dic, "sec0:key0", NULL));
271 CuAssertStrEquals(tc, "value-42/5",
272 iniparser_getstring(dic, "sec42:key5", NULL));
273 CuAssertStrEquals(tc, "value-99/9",
274 iniparser_getstring(dic, "sec99:key9", NULL));
275 dictionary_del(dic);
276 }
277
Test_iniparser_getint(CuTest * tc)278 void Test_iniparser_getint(CuTest *tc)
279 {
280 unsigned i;
281 char key_name[64];
282 dictionary *dic;
283 const struct { int num; const char *value; } good_val[] = {
284 { 0, "0" },
285 { 1, "1" },
286 { -1, "-1" },
287 { 1000, "1000" },
288 { 077, "077" },
289 { -01000, "-01000" },
290 { 0xFFFF, "0xFFFF" },
291 { -0xFFFF, "-0xFFFF" },
292 { 0x4242, "0x4242" },
293 { 0, NULL} /* must be last */
294 };
295 const char *bad_val[] = {
296 "",
297 "notanumber",
298 "0x",
299 "k2000",
300 " ",
301 "0xG1"
302 };
303 /* NULL test */
304 CuAssertIntEquals(tc, -42, iniparser_getint(NULL, NULL, -42));
305 CuAssertIntEquals(tc, -42, iniparser_getint(NULL, "dummy", -42));
306
307 /* Check the def return element */
308 dic = dictionary_new(10);
309 CuAssertIntEquals(tc, 42, iniparser_getint(dic, "dummy", 42));
310 CuAssertIntEquals(tc, 0xFFFF, iniparser_getint(dic, NULL, 0xFFFF));
311 CuAssertIntEquals(tc, -0xFFFF, iniparser_getint(dic, "dummy", -0xFFFF));
312 dictionary_del(dic);
313
314 /* Generic dictionary */
315 dic = dictionary_new(10);
316 for (i = 0; good_val[i].value != NULL; ++i) {
317 sprintf(key_name, "int:value%d", i);
318 dictionary_set(dic, key_name, good_val[i].value);
319 }
320 for (i = 0; good_val[i].value != NULL; ++i) {
321 sprintf(key_name, "int:value%d", i);
322 CuAssertIntEquals(tc, good_val[i].num,
323 iniparser_getint(dic, key_name, 0));
324 }
325 dictionary_del(dic);
326
327 /* Test bad names */
328 dic = dictionary_new(10);
329 for (i = 0; i < sizeof (bad_val) / sizeof (char *); ++i) {
330 sprintf(key_name, "int:bad%d", i);
331 dictionary_set(dic, key_name, bad_val[i]);
332 }
333 for (i = 0; i < sizeof (bad_val) / sizeof (char *); ++i) {
334 sprintf(key_name, "int:bad%d", i);
335 CuAssertIntEquals(tc, 0,
336 iniparser_getint(dic, key_name, 0));
337 }
338 dictionary_del(dic);
339 }
340
Test_iniparser_getlongint(CuTest * tc)341 void Test_iniparser_getlongint(CuTest *tc)
342 {
343 unsigned i;
344 char key_name[64];
345 dictionary *dic;
346 const struct { long int num; const char *value; } good_val[] = {
347 { 0, "0" },
348 { 1, "1" },
349 { -1, "-1" },
350 { 1000, "1000" },
351 { 077, "077" },
352 { -01000, "-01000" },
353 { 0x7FFFFFFFFFFFFFFF, "0x7FFFFFFFFFFFFFFF" },
354 { -0x7FFFFFFFFFFFFFFF, "-0x7FFFFFFFFFFFFFFF" },
355 { 0x4242, "0x4242" },
356 { 0, NULL} /* must be last */
357 };
358 const char *bad_val[] = {
359 "",
360 "notanumber",
361 "0x",
362 "k2000",
363 " ",
364 "0xG1"
365 };
366 /* NULL test */
367 CuAssertLongIntEquals(tc, -42, iniparser_getlongint(NULL, NULL, -42));
368 CuAssertLongIntEquals(tc, -42, iniparser_getlongint(NULL, "dummy", -42));
369
370 /* Check the def return element */
371 dic = dictionary_new(10);
372 CuAssertLongIntEquals(tc, 42, iniparser_getlongint(dic, "dummy", 42));
373 CuAssertLongIntEquals(tc, 0x7FFFFFFFFFFFFFFF, iniparser_getlongint(dic, NULL, 0x7FFFFFFFFFFFFFFF));
374 CuAssertLongIntEquals(tc, -0x7FFFFFFFFFFFFFFF, iniparser_getlongint(dic, "dummy", -0x7FFFFFFFFFFFFFFF));
375 dictionary_del(dic);
376
377 /* Generic dictionary */
378 dic = dictionary_new(10);
379 for (i = 0; good_val[i].value != NULL; ++i) {
380 sprintf(key_name, "longint:value%d", i);
381 dictionary_set(dic, key_name, good_val[i].value);
382 }
383 for (i = 0; good_val[i].value != NULL; ++i) {
384 sprintf(key_name, "longint:value%d", i);
385 CuAssertLongIntEquals(tc, good_val[i].num,
386 iniparser_getlongint(dic, key_name, 0));
387 }
388 dictionary_del(dic);
389
390 /* Test bad names */
391 dic = dictionary_new(10);
392 for (i = 0; i < sizeof (bad_val) / sizeof (char *); ++i) {
393 sprintf(key_name, "longint:bad%d", i);
394 dictionary_set(dic, key_name, bad_val[i]);
395 }
396 for (i = 0; i < sizeof (bad_val) / sizeof (char *); ++i) {
397 sprintf(key_name, "longint:bad%d", i);
398 CuAssertLongIntEquals(tc, 0,
399 iniparser_getlongint(dic, key_name, 0));
400 }
401 dictionary_del(dic);
402 }
403
Test_iniparser_getdouble(CuTest * tc)404 void Test_iniparser_getdouble(CuTest *tc)
405 {
406 dictionary *dic;
407
408 /* NULL test */
409 CuAssertDblEquals(tc, -42, iniparser_getdouble(NULL, NULL, -42), 0);
410 CuAssertDblEquals(tc, 4.2, iniparser_getdouble(NULL, "dummy", 4.2), 0);
411
412 /* Check the def return element */
413 dic = dictionary_new(10);
414 CuAssertDblEquals(tc, 3.1415, iniparser_getdouble(dic, "dummy", 3.1415), 0);
415 CuAssertDblEquals(tc, 0xFFFFFFFF, iniparser_getdouble(dic, NULL, 0xFFFFFFFF), 0);
416 CuAssertDblEquals(tc, -0xFFFFFFFF, iniparser_getdouble(dic, "dummy", -0xFFFFFFFF), 0);
417
418 /* Insert some values */
419 dictionary_set(dic, "double", "");
420 dictionary_set(dic, "double:good0", "0");
421 dictionary_set(dic, "double:good1", "-0");
422 dictionary_set(dic, "double:good2", "1.0");
423 dictionary_set(dic, "double:good3", "3.1415");
424 dictionary_set(dic, "double:good4", "6.6655957");
425 dictionary_set(dic, "double:good5", "-123456789.123456789");
426
427 /* Add dummy stuff too */
428 dictionary_set(dic, "double:bad0", "foo");
429
430 /* Get back the values */
431 CuAssertDblEquals(tc, 0, iniparser_getdouble(dic, "double:good0", 0xFF), 0);
432 CuAssertDblEquals(tc, 0, iniparser_getdouble(dic, "double:good1", 0xFF), 0);
433 CuAssertDblEquals(tc, 1.0, iniparser_getdouble(dic, "double:good2", 0xFF), 0);
434 CuAssertDblEquals(tc, 3.1415, iniparser_getdouble(dic, "double:good3", 0xFF), 0);
435 CuAssertDblEquals(tc, 6.6655957, iniparser_getdouble(dic, "double:good4", 0xFF), 0);
436 CuAssertDblEquals(tc, -123456789.123456789,
437 iniparser_getdouble(dic, "double:good5", 0xFF), 0);
438
439 CuAssertDblEquals(tc, 0, iniparser_getdouble(dic, "double:bad0", 42.42), 0);
440
441 dictionary_del(dic);
442 }
443
Test_iniparser_getboolean(CuTest * tc)444 void Test_iniparser_getboolean(CuTest *tc)
445 {
446 unsigned i;
447 char key_name[64];
448
449 dictionary *dic;
450 const char *token_true[] = {
451 "1",
452 "true",
453 "t",
454 "TRUE",
455 "T",
456 "yes",
457 "y",
458 "YES"
459 "Y",
460 NULL
461 };
462 const char *token_false[] = {
463 "0",
464 "false",
465 "f",
466 "FALSE",
467 "F",
468 "no",
469 "n",
470 "NO",
471 "N",
472 NULL
473 };
474
475 /* NULL test */
476 CuAssertIntEquals(tc, 1, iniparser_getboolean(NULL, NULL, 1));
477 CuAssertIntEquals(tc, 1, iniparser_getboolean(NULL, "dummy", 1));
478
479 /* Check the def return element */
480 dic = dictionary_new(10);
481 CuAssertIntEquals(tc, 1, iniparser_getboolean(dic, "dummy", 1));
482 CuAssertIntEquals(tc, 0, iniparser_getboolean(dic, NULL, 0));
483 CuAssertIntEquals(tc, 1, iniparser_getboolean(dic, "dummy", 1));
484
485 for (i = 0; token_true[i] != NULL; ++i) {
486 sprintf(key_name, "bool:true%d", i);
487 iniparser_set(dic, key_name, token_true[i]);
488 }
489 for (i = 0; token_false[i] != NULL; ++i) {
490 sprintf(key_name, "bool:false%d", i);
491 iniparser_set(dic, key_name, token_false[i]);
492 }
493
494 for (i = 0; token_true[i] != NULL; ++i) {
495 sprintf(key_name, "bool:true%d", i);
496 CuAssertIntEquals(tc, 1, iniparser_getboolean(dic, key_name, 0));
497 }
498 for (i = 0; token_false[i] != NULL; ++i) {
499 sprintf(key_name, "bool:false%d", i);
500 CuAssertIntEquals(tc, 0, iniparser_getboolean(dic, key_name, 1));
501 }
502
503 /* Test bad boolean */
504 iniparser_set(dic, "bool:bad0", "");
505 iniparser_set(dic, "bool:bad1", "m'kay");
506 iniparser_set(dic, "bool:bad2", "42");
507 iniparser_set(dic, "bool:bad3", "_true");
508 CuAssertIntEquals(tc, 0xFF, iniparser_getboolean(dic, "bool:bad0", 0xFF));
509 CuAssertIntEquals(tc, 0xFF, iniparser_getboolean(dic, "bool:bad1", 0xFF));
510 CuAssertIntEquals(tc, 0xFF, iniparser_getboolean(dic, "bool:bad2", 0xFF));
511 CuAssertIntEquals(tc, 0xFF, iniparser_getboolean(dic, "bool:bad3", 0xFF));
512
513 dictionary_del(dic);
514 }
515
Test_iniparser_line(CuTest * tc)516 void Test_iniparser_line(CuTest *tc)
517 {
518 char section [ASCIILINESZ+1] ;
519 char key [ASCIILINESZ+1] ;
520 char val [ASCIILINESZ+1] ;
521
522 /* Test empty line */
523 CuAssertIntEquals(tc, LINE_EMPTY, iniparser_line("", section, key, val));
524 CuAssertIntEquals(tc, LINE_EMPTY, iniparser_line(" ", section, key, val));
525 CuAssertIntEquals(tc, LINE_EMPTY, iniparser_line("\t", section, key, val));
526
527 /* Test valid syntax */
528 CuAssertIntEquals(tc, LINE_SECTION, iniparser_line("[s]", section, key, val));
529 CuAssertStrEquals(tc, "s", section);
530
531 CuAssertIntEquals(tc, LINE_SECTION, iniparser_line("[ section ]", section, key, val));
532 CuAssertStrEquals(tc, "section", section);
533
534 CuAssertIntEquals(tc, LINE_VALUE, iniparser_line("k=1", section, key, val));
535 CuAssertStrEquals(tc, "k", key);
536 CuAssertStrEquals(tc, "1", val);
537
538 CuAssertIntEquals(tc, LINE_VALUE, iniparser_line("key = 0x42", section, key, val));
539 CuAssertStrEquals(tc, "key", key);
540 CuAssertStrEquals(tc, "0x42", val);
541
542 CuAssertIntEquals(tc, LINE_VALUE, iniparser_line("key= value with spaces", section, key, val));
543 CuAssertStrEquals(tc, "key", key);
544 CuAssertStrEquals(tc, "value with spaces", val);
545
546 CuAssertIntEquals(tc, LINE_VALUE, iniparser_line("k =_!<>''", section, key, val));
547 CuAssertStrEquals(tc, "k", key);
548 CuAssertStrEquals(tc, "_!<>''", val);
549
550 CuAssertIntEquals(tc, LINE_VALUE, iniparser_line("empty_value =", section, key, val));
551 CuAssertStrEquals(tc, "empty_value", key);
552 CuAssertStrEquals(tc, "", val);
553
554 CuAssertIntEquals(tc, LINE_VALUE, iniparser_line("empty_value = \t\n", section, key, val));
555 CuAssertStrEquals(tc, "empty_value", key);
556 CuAssertStrEquals(tc, "", val);
557
558 CuAssertIntEquals(tc, LINE_VALUE, iniparser_line("key =\tval # comment", section, key, val));
559 CuAssertStrEquals(tc, "key", key);
560 CuAssertStrEquals(tc, "val", val);
561
562 CuAssertIntEquals(tc, LINE_VALUE, iniparser_line("key \n\n = \n val", section, key, val));
563 CuAssertStrEquals(tc, "key", key);
564 CuAssertStrEquals(tc, "val", val);
565
566 CuAssertIntEquals(tc, LINE_COMMENT, iniparser_line(";comment", section, key, val));
567 CuAssertIntEquals(tc, LINE_COMMENT, iniparser_line(" # comment", section, key, val));
568
569 CuAssertIntEquals(tc, LINE_VALUE, iniparser_line("key = \" do_not_strip \"", section, key, val));
570 CuAssertStrEquals(tc, "key", key);
571 CuAssertStrEquals(tc, " do_not_strip ", val);
572
573 CuAssertIntEquals(tc, LINE_VALUE, iniparser_line("key = ' '", section, key, val));
574 CuAssertStrEquals(tc, "key", key);
575 CuAssertStrEquals(tc, " ", val);
576
577 CuAssertIntEquals(tc, LINE_VALUE, iniparser_line("key = \"\"", section, key, val));
578 CuAssertStrEquals(tc, "key", key);
579 CuAssertStrEquals(tc, "", val);
580
581 CuAssertIntEquals(tc, LINE_VALUE, iniparser_line("key = ''", section, key, val));
582 CuAssertStrEquals(tc, "key", key);
583 CuAssertStrEquals(tc, "", val);
584
585 /* Test syntax error */
586 CuAssertIntEquals(tc, LINE_ERROR, iniparser_line("empty_value", section, key, val));
587 CuAssertIntEquals(tc, LINE_ERROR, iniparser_line("not finished\\", section, key, val));
588 CuAssertIntEquals(tc, LINE_ERROR, iniparser_line("0x42 / 0b101010", section, key, val));
589
590 }
591
Test_iniparser_load(CuTest * tc)592 void Test_iniparser_load(CuTest *tc)
593 {
594 DIR *dir;
595 struct dirent *curr;
596 struct stat curr_stat;
597 dictionary *dic;
598 char ini_path[276];
599
600 /* Dummy tests */
601 dic = iniparser_load("/you/shall/not/path");
602 CuAssertPtrEquals(tc, NULL, dic);
603
604 /* Test all the good .ini files */
605 dir = opendir(GOOD_INI_PATH);
606 CuAssertPtrNotNullMsg(tc, "Cannot open good .ini conf directory", dir);
607 for (curr = readdir(dir); curr != NULL; curr = readdir(dir)) {
608 sprintf(ini_path, "%s/%s", GOOD_INI_PATH, curr->d_name);
609 stat(ini_path, &curr_stat);
610 if (S_ISREG(curr_stat.st_mode)) {
611 dic = iniparser_load(ini_path);
612 CuAssertPtrNotNullMsg(tc, ini_path, dic);
613 dictionary_del(dic);
614 }
615 }
616 closedir(dir);
617
618 /* Test all the bad .ini files */
619 dir = opendir(BAD_INI_PATH);
620 CuAssertPtrNotNullMsg(tc, "Cannot open bad .ini conf directory", dir);
621 for (curr = readdir(dir); curr != NULL; curr = readdir(dir)) {
622 sprintf(ini_path, "%s/%s", BAD_INI_PATH, curr->d_name);
623 stat(ini_path, &curr_stat);
624 if (S_ISREG(curr_stat.st_mode)) {
625 dic = iniparser_load(ini_path);
626 CuAssertPtrEquals_Msg(tc, ini_path, NULL, dic);
627 dictionary_del(dic);
628 }
629 }
630 closedir(dir);
631 }
632
Test_dictionary_wrapper(CuTest * tc)633 void Test_dictionary_wrapper(CuTest *tc)
634 {
635 dictionary *dic;
636
637 dic = dictionary_new(10);
638
639 CuAssertIntEquals(tc, -1, iniparser_set(dic, NULL, NULL));
640 CuAssertIntEquals(tc, -1, iniparser_set(NULL, "section", "value"));
641
642 CuAssertIntEquals(tc, 0, iniparser_set(dic, "section", NULL));
643 CuAssertIntEquals(tc, 0, iniparser_set(dic, "section:key", "value"));
644
645 CuAssertStrEquals(tc, "value", iniparser_getstring(dic, "section:key", NULL));
646 /* reset the key's value*/
647 CuAssertIntEquals(tc, 0, iniparser_set(dic, "section:key", NULL));
648 CuAssertStrEquals(tc, NULL, iniparser_getstring(dic, "section:key", "dummy"));
649 CuAssertIntEquals(tc, 0, iniparser_set(dic, "section:key", "value"));
650 CuAssertStrEquals(tc, "value", iniparser_getstring(dic, "section:key", NULL));
651
652 iniparser_unset(dic, "section:key");
653 CuAssertStrEquals(tc, "dummy", iniparser_getstring(dic, "section:key", "dummy"));
654 CuAssertStrEquals(tc, NULL, iniparser_getstring(dic, "section", "dummy"));
655
656 CuAssertIntEquals(tc, 0, iniparser_set(dic, "section:key", NULL));
657 CuAssertIntEquals(tc, 0, iniparser_set(dic, "section:key1", NULL));
658 CuAssertIntEquals(tc, 0, iniparser_set(dic, "section:key2", NULL));
659
660 iniparser_unset(dic, "section");
661 CuAssertStrEquals(tc, NULL, iniparser_getstring(dic, "section", NULL));
662
663 iniparser_freedict(dic);
664 }
665
666 static char _last_error[1024];
_error_callback(const char * format,...)667 static int _error_callback(const char *format, ...)
668 {
669 int ret;
670 va_list argptr;
671 va_start(argptr, format);
672 ret = vsprintf(_last_error, format, argptr);
673 va_end(argptr);
674 return ret;
675
676 }
677
Test_iniparser_error_callback(CuTest * tc)678 void Test_iniparser_error_callback(CuTest *tc)
679 {
680 dictionary *dic;
681
682 /* Specify our custom error_callback */
683 iniparser_set_error_callback(_error_callback);
684
685 /* Trigger an error and check it was written on the right output */
686 dic = iniparser_load("/path/to/nowhere.ini");
687 CuAssertPtrEquals(tc, NULL, dic);
688 CuAssertStrEquals(tc, "iniparser: cannot open /path/to/nowhere.ini\n", _last_error);
689
690 /* Reset erro_callback */
691 _last_error[0] = '\0';
692 iniparser_set_error_callback(NULL);
693
694 /* Make sure custom callback is no more called */
695 dic = iniparser_load("/path/to/nowhere.ini");
696 CuAssertPtrEquals(tc, NULL, dic);
697 CuAssertStrEquals(tc, "", _last_error);
698 }
699