Debian Codegraph @

ini/ini_valueobj_ut.c

raw symbols SCIP
1 /*
2 INI LIBRARY
3
4 Unit test for the value object.
5
6 Copyright (C) Dmitri Pal <[email protected]> 2010
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU 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 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "config.h"
21 #include <errno.h> /* for errors */
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <limits.h>
27
28 #include "simplebuffer.h"
29 #include "ini_configobj.h"
30 #include "ini_config_priv.h"
31 #include "ini_valueobj.h"
32 #include "ini_defines.h"
33 #define TRACE_HOME
34 #include "trace.h"
35
36 #define TEST_SIZE 80
37
38 int verbose = 0;
39
40 #define VOOUT(foo) \
41 do { \
42 if (verbose) foo; \
43 } while(0)
44
45
46 typedef int (*test_fn)(void);
47
48 static int create_comment(int i, struct ini_comment **ic)
49 {
50 int error = EOK;
51 const char *template = ";Line 0 of the value %d";
52 char comment[TEST_SIZE];
53 struct ini_comment *new_ic = NULL;
54
55 TRACE_FLOW_ENTRY();
56
57 snprintf(comment, TEST_SIZE, template, i);
58
59
60 if ((error = ini_comment_create(&new_ic)) ||
61 (error = ini_comment_build(new_ic, comment)) ||
62 (error = ini_comment_build(new_ic, NULL)) ||
63 (error = ini_comment_build(new_ic, "#This is the second line")) ||
64 (error = ini_comment_build(new_ic, ";This is the third line")) ||
65 (error = ini_comment_build(new_ic, ""))) {
66 printf("Failed to create comment object. Error %d.\n", error);
67 ini_comment_destroy(new_ic);
68 return -1;
69 }
70
71 *ic = new_ic;
72
73 TRACE_FLOW_EXIT();
74 return EOK;
75 }
76
77 /* Save value to the file */
78 /* NOTE: might be moved into the API in future */
79 static int save_value(FILE *ff, const char *key, struct value_obj *vo)
80 {
81
82 int error = EOK;
83 struct simplebuffer *sbobj = NULL;
84 uint32_t left = 0;
85
86 TRACE_FLOW_ENTRY();
87
88 error = simplebuffer_alloc(&sbobj);
89 if (error) {
90 TRACE_ERROR_NUMBER("Failed to allocate dynamic string.", error);
91 return error;
92 }
93
94 /* Serialize */
95 error = value_serialize(vo, key, sbobj);
96 if (error) {
97 printf("Failed to serialize a value object %d.\n", error);
98 simplebuffer_free(sbobj);
99 return error;
100 }
101
102 /* Add CR */
103 error = simplebuffer_add_cr(sbobj);
104 if (error) {
105 TRACE_ERROR_NUMBER("Failed to add CR", error);
106 simplebuffer_free(sbobj);
107 return error;
108 }
109
110 /* Save */
111 left = simplebuffer_get_len(sbobj);
112 while (left > 0) {
113 error = simplebuffer_write(fileno(ff), sbobj, &left);
114 if (error) {
115 printf("Failed to write value object %d.\n", error);
116 simplebuffer_free(sbobj);
117 return error;
118 }
119 }
120
121 simplebuffer_free(sbobj);
122
123 TRACE_FLOW_EXIT();
124 return EOK;
125 }
126
127 /* Test to create value object using arrays */
128 static int other_create_test(FILE *ff, struct value_obj **vo)
129 {
130 int error = EOK;
131 struct value_obj *new_vo = NULL;
132 struct ref_array *raw_lines;
133 struct ref_array *raw_lengths;
134 struct ini_comment *ic = NULL;
135 struct ini_comment *ic2 = NULL;
136 char *val;
137 const char *vallines[] = { "Domain1,",
138 " Domain2 ,",
139 " Domain3 " };
140 const char *fullstr;
141 const char *expected = "Domain1, Domain2 , Domain3";
142 int i;
143 uint32_t origin = 0;
144 uint32_t line = 0;
145 uint32_t len = 0;
146 uint32_t expected_len = 0;
147
148
149 TRACE_FLOW_ENTRY();
150
151 /* Create a pair of arrays */
152 error = value_create_arrays(&raw_lines,
153 &raw_lengths);
154 if (error) {
155 printf("Failed to create arrays %d.\n", error);
156 return error;
157 }
158
159 for (i=0; i< 3; i++) {
160 errno = 0;
161 val = strdup(vallines[i]);
162 if (val == NULL) {
163 error = errno;
164 printf("Failed to dup memory %d.\n", error);
165 value_destroy_arrays(raw_lines,
166 raw_lengths);
167 return error;
168 }
169
170 /* Add line to the arrays */
171 error = value_add_to_arrays(val,
172 strlen(val),
173 raw_lines,
174 raw_lengths);
175 if (error) {
176 printf("Failed to add to arrays %d.\n", error);
177 value_destroy_arrays(raw_lines,
178 raw_lengths);
179 return error;
180 }
181
182 }
183
184 /* Create a comment */
185 error = create_comment(1000, &ic);
186 if (error) {
187 printf("Failed to create comment %d.\n", error);
188 value_destroy_arrays(raw_lines,
189 raw_lengths);
190 return error;
191 }
192
193 /* Create object */
194 error = value_create_from_refarray(raw_lines,
195 raw_lengths,
196 1,
197 INI_VALUE_READ,
198 3,
199 70,
200 ic,
201 &new_vo);
202
203 if (error) {
204 printf("Failed to create comment %d.\n", error);
205 value_destroy_arrays(raw_lines,
206 raw_lengths);
207 ini_comment_destroy(ic);
208 return error;
209 }
210
211 /* Save value to the file */
212 error = save_value(ff, "baz", new_vo);
213 if (error) {
214 printf("Failed to save value to file %d.\n", error);
215 value_destroy(new_vo);
216 return error;
217 }
218
219 /* Now do assertions and modifications to the object */
220
221 /* NOTE: Below this line do not need to free arrays or comment
222 * they became internal parts of the value object
223 * and will be freed as a part of it.
224 */
225
226 /* Get concatenated value */
227 error = value_get_concatenated(new_vo,
228 &fullstr);
229
230 if (error) {
231 printf("Failed to get the string %d.\n", error);
232 value_destroy(new_vo);
233 return error;
234 }
235
236 /* Get length of the concatenated value */
237 value_get_concatenated_len(new_vo, &len);
238 expected_len = strlen(expected);
239
240 if ((len != expected_len) ||
241 (strncmp(fullstr, expected, expected_len + 1) != 0)) {
242 printf("The expected value is different.\n");
243 printf("The expected value is %s\n", expected);
244 printf("The real value is %s\n", fullstr);
245 printf("The expected len is %d, real %d.\n", expected_len, len);
246 value_destroy(new_vo);
247 return EINVAL;
248 }
249
250 /* Get value's origin */
251 error = value_get_origin(new_vo, &origin);
252 if (error) {
253 printf("Failed to get origin %d.\n", error);
254 value_destroy(new_vo);
255 return error;
256 }
257
258 if (origin != INI_VALUE_READ) {
259 printf("The expected origin is different.\n%d\n", origin);
260 value_destroy(new_vo);
261 return EINVAL;
262 }
263
264 /* Get value's line */
265 error = value_get_line(new_vo, &line);
266 if (error) {
267 printf("Failed to get origin %d.\n", error);
268 value_destroy(new_vo);
269 return error;
270 }
271
272 if (line != 1) {
273 printf("The expected line is different.\n%d\n", origin);
274 value_destroy(new_vo);
275 return EINVAL;
276 }
277
278 /* Get comment from the value */
279 ic = NULL;
280 error = value_extract_comment(new_vo, &ic);
281 if (error) {
282 printf("Failed to extract comment %d.\n", error);
283 value_destroy(new_vo);
284 return error;
285 }
286
287 if (ic == NULL) {
288 printf("The expected comment to be there.\n");
289 value_destroy(new_vo);
290 return error;
291 }
292
293 VOOUT(ini_comment_print(ic, stdout));
294
295 /* Get comment again */
296 ic2 = NULL;
297 error = value_extract_comment(new_vo, &ic2);
298 if (error) {
299 printf("Failed to extract comment %d.\n", error);
300 value_destroy(new_vo);
301 ini_comment_destroy(ic);
302 return error;
303 }
304
305 if (ic2 != NULL) {
306 printf("The expected NO comment to be there.\n");
307 value_destroy(new_vo);
308 ini_comment_destroy(ic);
309 /* No free for ic2 since it is the same object */
310
311 /* But this should not happen anyways -
312 * it will be coding error.
313 */
314 return EINVAL;
315 }
316
317 /* Put comment back */
318 error = value_put_comment(new_vo, ic);
319 if (error) {
320 printf("Failed to put comment back %d.\n", error);
321 value_destroy(new_vo);
322 ini_comment_destroy(ic);
323 return error;
324 }
325
326 /* Save value to the file */
327 error = save_value(ff, "bar", new_vo);
328 if (error) {
329 printf("Failed to save value to file %d.\n", error);
330 value_destroy(new_vo);
331 return error;
332 }
333
334 *vo = new_vo;
335
336 TRACE_FLOW_EXIT();
337 return EOK;
338 }
339
340 /* Modify the value object */
341 static int modify_test(FILE *ff, struct value_obj *vo)
342 {
343 int error = EOK;
344 const char *strval = "Domain100, Domain200, Domain300";
345
346 TRACE_FLOW_ENTRY();
347
348
349 /* Update key length */
350 error = value_set_keylen(vo, strlen("foobar"));
351 if (error) {
352 printf("Failed to change key length %d.\n", error);
353 return error;
354 }
355
356 /* Update value */
357 error = value_update(vo,
358 strval,
359 strlen(strval),
360 INI_VALUE_CREATED,
361 10);
362 if (error) {
363 printf("Failed to update value %d.\n", error);
364 return error;
365 }
366
367
368 /* Save value to the file */
369 error = save_value(ff, "foobar", vo);
370 if (error) {
371 printf("Failed to save value to file %d.\n", error);
372 return error;
373 }
374
375 TRACE_FLOW_EXIT();
376 return EOK;
377 }
378
379
380 static int vo_basic_test(void)
381 {
382 int error = EOK;
383 const char *strvalue = "Test multi_word_value_that_will_"
384 "be_split_between_several_lines_!";
385
386 /* Other testing can be done with the following string:
387 * const char *strvalue = "Test multi word value that "
388 * "will be split between several lines";
389 */
390
391 struct value_obj *vo = NULL;
392 uint32_t wrap = 0;
393 struct ini_comment *ic = NULL;
394 FILE *ff = NULL;
395
396 TRACE_FLOW_ENTRY();
397
398 errno = 0;
399 ff = fopen("test.ini","wt");
400 if (ff == NULL) {
401 error = errno;
402 printf("Failed to open file. Error %d.\n", error);
403 return error;
404 }
405
406
407 for (wrap = 0; wrap < 80; wrap++) {
408
409 ic = NULL;
410 error = create_comment(wrap, &ic);
411 if (error) {
412 printf("Failed to create a new comment object %d.\n", error);
413 fclose(ff);
414 return error;
415 }
416
417 vo = NULL;
418 error = value_create_new(strvalue,
419 strlen(strvalue),
420 INI_VALUE_CREATED,
421 3,
422 wrap,
423 ic,
424 &vo);
425 if (error) {
426 printf("Failed to create a new value object %d.\n", error);
427 ini_comment_destroy(ic);
428 fclose(ff);
429 return error;
430 }
431
432 error = save_value(ff, "key", vo);
433 if (error) {
434 printf("Failed to save value to file %d.\n", error);
435 value_destroy(vo);
436 fclose(ff);
437 return error;
438 }
439
440 value_destroy(vo);
441 }
442
443 vo = NULL;
444
445 /* Run other create test here */
446 error = other_create_test(ff, &vo);
447 if (error) {
448 printf("Create test failed %d.\n", error);
449 fclose(ff);
450 return error;
451 }
452
453 /* Run modify test here */
454 error = modify_test(ff, vo);
455 if (error) {
456 printf("Modify test failed %d.\n", error);
457 fclose(ff);
458 value_destroy(vo);
459 return error;
460 }
461
462 value_destroy(vo);
463
464
465 ic = NULL;
466 error = create_comment(100, &ic);
467 if (error) {
468 printf("Failed to create a new comment object %d.\n", error);
469 fclose(ff);
470 return error;
471 }
472
473 ini_comment_print(ic, ff);
474
475 ini_comment_destroy(ic);
476
477 fclose(ff);
478
479 TRACE_FLOW_EXIT();
480 return EOK;
481 }
482
483 static int vo_copy_test(void)
484 {
485 int error = EOK;
486 const char *strvalue = "Test multi word value that "
487 "will be split between several lines";
488
489 struct value_obj *vo = NULL;
490 struct value_obj *vo_copy = NULL;
491 uint32_t wrap = 0;
492 struct ini_comment *ic = NULL;
493 FILE *ff = NULL;
494 char comment[100];
495
496 TRACE_FLOW_ENTRY();
497
498 VOOUT(printf("Copy test\n"));
499
500 errno = 0;
501 ff = fopen("test.ini","a");
502 if (ff == NULL) {
503 error = errno;
504 printf("Failed to open file. Error %d.\n", error);
505 return error;
506 }
507
508 error = ini_comment_create(&ic);
509 if (error) {
510 printf("Failed to create comment object\n");
511 fclose(ff);
512 return -1;
513 }
514
515 error = ini_comment_append(ic, "#This is a copy test!");
516 if (error) {
517 printf("Failed to add a line to the comment %d.\n", error);
518 ini_comment_destroy(ic);
519 fclose(ff);
520 return error;
521 }
522
523 error = ini_comment_append(ic, "#Replacable comment line");
524 if (error) {
525 printf("Failed to add a line to the comment %d.\n", error);
526 ini_comment_destroy(ic);
527 fclose(ff);
528 return error;
529 }
530
531 error = value_create_new(strvalue,
532 strlen(strvalue),
533 INI_VALUE_CREATED,
534 3,
535 20,
536 ic,
537 &vo);
538 if (error) {
539 printf("Failed to create a new value object %d.\n", error);
540 ini_comment_destroy(ic);
541 fclose(ff);
542 return error;
543 }
544
545 error = save_value(ff, "key", vo);
546 if (error) {
547 printf("Failed to save value to file %d.\n", error);
548 value_destroy(vo);
549 fclose(ff);
550 return error;
551 }
552
553 for (wrap = 0; wrap < 80; wrap++) {
554
555 TRACE_INFO_NUMBER("Iteration:", wrap);
556
557 vo_copy = NULL;
558 error = value_copy(vo, &vo_copy);
559 if (error) {
560 printf("Failed to create a new value object %d.\n", error);
561 value_destroy(vo);
562 fclose(ff);
563 return error;
564 }
565
566 error = value_set_boundary(vo_copy, wrap);
567 if (error) {
568 printf("Failed to set boundary %d.\n", error);
569 value_destroy(vo);
570 value_destroy(vo_copy);
571 fclose(ff);
572 return error;
573 }
574
575 /* Get comment from the value */
576 ic = NULL;
577 error = value_extract_comment(vo_copy, &ic);
578 if (error) {
579 printf("Failed to extract comment %d.\n", error);
580 value_destroy(vo);
581 value_destroy(vo_copy);
582 fclose(ff);
583 return error;
584 }
585
586 /* Replace comment in the value */
587 snprintf(comment, TEST_SIZE, ";This is value with boundary %d", wrap);
588 VOOUT(printf("Comment: %s\n", comment));
589 error = ini_comment_replace(ic, 1, comment);
590 if (error) {
591 printf("Failed to replace comment %d.\n", error);
592 value_destroy(vo);
593 value_destroy(vo_copy);
594 fclose(ff);
595 return error;
596 }
597
598 /* Set comment into the value */
599 error = value_put_comment(vo_copy, ic);
600 if (error) {
601 printf("Failed to set comment %d.\n", error);
602 value_destroy(vo);
603 value_destroy(vo_copy);
604 fclose(ff);
605 return error;
606 }
607
608 error = save_value(ff, "key", vo_copy);
609 if (error) {
610 printf("Failed to save value to file %d.\n", error);
611 value_destroy(vo);
612 value_destroy(vo_copy);
613 fclose(ff);
614 return error;
615 }
616
617 value_destroy(vo_copy);
618 }
619
620 value_destroy(vo);
621 fclose(ff);
622 TRACE_FLOW_EXIT();
623 return EOK;
624 }
625
626 static int vo_show_test(void)
627 {
628 VOOUT(system("cat test.ini"));
629 return EOK;
630 }
631
632 static int vo_mc_test(void)
633 {
634 int error = EOK;
635 struct value_obj *vo1 = NULL;
636 struct value_obj *vo2 = NULL;
637 struct ini_comment *ic = NULL;
638
639 TRACE_FLOW_ENTRY();
640
641 VOOUT(printf("<=== Merge Comment Test ===>\n"));
642
643 error = create_comment(1, &ic);
644 if (error) {
645 printf("Failed to create a new comment object %d.\n", error);
646 return error;
647 }
648
649 error = value_create_new("test1",
650 5,
651 INI_VALUE_CREATED,
652 3,
653 80,
654 ic,
655 &vo1);
656 if (error) {
657 printf("Failed to create the first value object %d.\n", error);
658 ini_comment_destroy(ic);
659 return error;
660 }
661
662 error = create_comment(2, &ic);
663 if (error) {
664 printf("Failed to create a new comment object %d.\n", error);
665 value_destroy(vo1);
666 return error;
667 }
668
669 error = value_create_new("test2",
670 5,
671 INI_VALUE_CREATED,
672 3,
673 80,
674 ic,
675 &vo2);
676 if (error) {
677 printf("Failed to create the second value object %d.\n", error);
678 ini_comment_destroy(ic);
679 value_destroy(vo1);
680 return error;
681 }
682
683 /* Merge comment from one value into another */
684 error = value_merge_comment(vo2, vo1);
685 if (error) {
686 printf("Failed to merge comments %d.\n", error);
687 value_destroy(vo1);
688 value_destroy(vo2);
689 return error;
690 }
691
692 value_destroy(vo2);
693
694 VOOUT(printf("<=== Key ===>\n"));
695 VOOUT(value_print("key", vo1));
696
697 error = value_extract_comment(vo1, &ic);
698 if (error) {
699 printf("Failed to extract comment %d.\n", error);
700 value_destroy(vo1);
701 return error;
702 }
703
704 value_destroy(vo1);
705
706 VOOUT(printf("<=== Comment ===>\n"));
707 VOOUT(ini_comment_print(ic, stdout));
708 ini_comment_destroy(ic);
709
710 TRACE_FLOW_EXIT();
711 return EOK;
712 }
713
714
715 /* Main function of the unit test */
716 int main(int argc, char *argv[])
717 {
718 int error = 0;
719 test_fn tests[] = { vo_basic_test,
720 vo_copy_test,
721 vo_show_test,
722 vo_mc_test,
723 NULL };
724 test_fn t;
725 int i = 0;
726 char *var;
727
728 if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = 1;
729 else {
730 var = getenv("COMMON_TEST_VERBOSE");
731 if (var) verbose = 1;
732 }
733
734 VOOUT(printf("Start\n"));
735
736 while ((t = tests[i++])) {
737 error = t();
738 if (error) {
739 VOOUT(printf("Failed with error %d!\n", error));
740 return error;
741 }
742 }
743
744 VOOUT(printf("Success!\n"));
745 return 0;
746 }