Debian Codegraph @

ini/ini_configmod_ut_check.c

raw symbols SCIP
1 /*
2 INI LIBRARY
3
4 Unit test for the configuration object modification API.
5
6 Copyright (C) Lukas Slebodnik <[email protected]> 2015
7
8 INI Library is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 INI Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with INI Library. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "config.h"
23
24 #include <errno.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <check.h>
29
30 /* #define TRACE_LEVEL 7 */
31 #define TRACE_HOME
32 #include "trace.h"
33 #include "ini_configobj.h"
34 #include "ini_config_priv.h"
35 #include "collection_tools.h"
36 #include "ini_configmod.h"
37 #include "path_utils.h"
38 #include "../basicobjects/simplebuffer.h"
39
40 int verbose = 0;
41
42 #define WRAP_SIZE 80
43
44 static void dump_configuration(struct ini_cfgobj *in_cfg,
45 FILE *file)
46 {
47 int ret;
48 struct simplebuffer *sbobj = NULL;
49 void *buff;
50 uint32_t len;
51
52 ret = simplebuffer_alloc(&sbobj);
53 fail_unless(ret == EOK,
54 "Failed to allocate buffer. Error %d.\n", ret);
55
56 ret = ini_config_serialize(in_cfg, sbobj);
57 fail_unless(ret == EOK,
58 "Failed to serialize. Error %d.\n", ret);
59
60 buff = simplebuffer_get_vbuf(sbobj);
61 len = simplebuffer_get_len(sbobj);
62 ret = fwrite(buff, 1, len, file);
63 fail_if(ret == -1,
64 "Failed to write to file. Error: %d %s\n", ret, strerror(ret));
65
66 simplebuffer_free(sbobj);
67 return;
68 }
69
70 static int call_diff(const char *function,
71 const char *expected_cfg,
72 size_t expected_cfg_len,
73 const char *res_cfg,
74 size_t res_cfg_len)
75 {
76 char expected_fn[PATH_MAX];
77 char res_fn[PATH_MAX];
78 char command[PATH_MAX * 3];
79 char *builddir;
80 int ret;
81 int expected_fd;
82 int res_fd;
83
84 builddir = getenv("builddir");
85
86 snprintf(expected_fn, PATH_MAX, "%s/expected.conf_%s_XXXXXX",
87 (builddir == NULL) ? "." : builddir, function);
88 snprintf(res_fn, PATH_MAX, "%s/result.conf_%s_XXXXXX",
89 (builddir == NULL) ? "." : builddir, function);
90
91 expected_fd = mkstemp(expected_fn);
92 fail_if(expected_fd == -1, "mkstemp failed: %s\n", strerror(errno));
93
94 ret = write(expected_fd, expected_cfg, expected_cfg_len);
95 fail_if(ret == -1,
96 "Failed write to %s. Error %s\n",
97 expected_fn, strerror(errno));
98
99 close(expected_fd);
100
101 res_fd = mkstemp(res_fn);
102 fail_if(res_fd == -1, "mkstemp failed: %s\n", strerror(errno));
103
104 ret = write(res_fd, res_cfg, res_cfg_len);
105 fail_if(ret == -1,
106 "Failed write to %s. Error %s\n",
107 expected_fn, strerror(errno));
108 close(res_fd);
109
110 snprintf(command, PATH_MAX * 3, "diff -wi %s %s", expected_fn, res_fn);
111 ret = system(command);
112 fail_if(ret == -1,
113 "Failed to execute command:%s. Erorr %s\n",
114 command, strerror(errno));
115
116 return EOK;
117 }
118
119 #define assert_configuration_equal(expected_cfg, expected_cfg_len, res_cfg) \
120 _assert_configuration_equal(expected_cfg, expected_cfg_len, res_cfg, \
121 __func__, __FILE__, __LINE__)
122 static void _assert_configuration_equal(const char *expected_cfg,
123 size_t expected_cfg_len,
124 struct ini_cfgobj *res_cfg,
125 const char *function,
126 const char *file,
127 int line)
128 {
129 char *res_buffer = NULL;
130 size_t res_buffer_size;
131 FILE *f_memstream;
132 int ret;
133
134 --expected_cfg_len; /* do not use trailing zero */
135
136 f_memstream = open_memstream(&res_buffer, &res_buffer_size);
137 fail_if(f_memstream == NULL,
138 "\n\t[%s:%d] open_memstream failed.", file, line);
139
140 dump_configuration(res_cfg, f_memstream);
141 fclose(f_memstream);
142
143 fail_unless(expected_cfg_len == res_buffer_size,
144 "\n\t[%s:%d] Size of expected config %zu and result config %d "
145 "does not match. Res:%d\n",
146 file, line, expected_cfg_len, res_buffer_size,
147 call_diff(function, expected_cfg, expected_cfg_len,
148 res_buffer, res_buffer_size));
149
150 ret = memcmp(res_buffer, expected_cfg, expected_cfg_len);
151 fail_unless(ret == EOK,
152 "\n\t[%s:%d] Configurations are not identical. Res:%d\n",
153 file, line,
154 call_diff(function, expected_cfg, expected_cfg_len,
155 res_buffer, res_buffer_size));
156
157 free(res_buffer);
158 }
159
160 static struct ini_cfgobj *get_ini_config_from_str(char input_data[],
161 size_t input_data_len)
162 {
163 struct ini_cfgobj *in_cfg;
164 struct ini_cfgfile *file_ctx;
165 int ret;
166
167 ret = ini_config_create(&in_cfg);
168 fail_unless(ret == EOK, "Failed to create config. Error %d.\n", ret);
169
170 ret = ini_config_file_from_mem(input_data, input_data_len, &file_ctx);
171 fail_unless(ret == EOK, "Failed to load config. Error %d.\n", ret);
172
173 ret = ini_config_parse(file_ctx, INI_STOP_ON_NONE, INI_MV1S_ALLOW, 0,
174 in_cfg);
175 fail_unless(ret == EOK, "Failed to parse config. Error %d.\n", ret);
176
177 ini_config_file_destroy(file_ctx);
178
179 return in_cfg;
180 }
181
182 START_TEST(test_delete_value_wrong_arguments)
183 {
184 int ret = EOK;
185 struct ini_cfgobj *in_cfg = NULL;
186
187 char exp_data[] =
188 "[zero]\n"
189 "[one]\n"
190 "key1 = value1a\n";
191
192 in_cfg = get_ini_config_from_str(exp_data, sizeof(exp_data));
193 assert_configuration_equal(exp_data, sizeof(exp_data), in_cfg);
194
195 /* missing ini_config */
196 ret = ini_config_delete_value(NULL, "one", COL_DSP_NDUP, "key1", 0);
197 fail_unless(ret == EINVAL, "delete value should fail. Error: %d", ret);
198 assert_configuration_equal(exp_data, sizeof(exp_data), in_cfg);
199
200 /* missing section */
201 ret = ini_config_delete_value(in_cfg, NULL, COL_DSP_NDUP, "key1", 0);
202 fail_unless(ret == EINVAL, "delete value should fail. Error: %d", ret);
203 assert_configuration_equal(exp_data, sizeof(exp_data), in_cfg);
204
205 /* missing key */
206 ret = ini_config_delete_value(in_cfg, "one", COL_DSP_NDUP, NULL, 0);
207 fail_unless(ret == EINVAL, "delete value should fail. Error: %d", ret);
208 assert_configuration_equal(exp_data, sizeof(exp_data), in_cfg);
209
210 /* value index is too low */
211 ret = ini_config_delete_value(in_cfg, "one", COL_DSP_NDUP, "key1", -1);
212 fail_unless(ret == EINVAL, "delete value should fail. Error: %d", ret);
213 assert_configuration_equal(exp_data, sizeof(exp_data), in_cfg);
214
215 /* value index is too high */
216 ret = ini_config_delete_value(in_cfg, "one", COL_DSP_NDUP, "key1", 1);
217 fail_unless(ret == ENOENT, "delete value should fail. Error: %d", ret);
218 assert_configuration_equal(exp_data, sizeof(exp_data), in_cfg);
219
220 ini_config_destroy(in_cfg);
221 }
222 END_TEST
223
224 START_TEST(test_delete_value)
225 {
226 int ret = EOK;
227 struct ini_cfgobj *in_cfg;
228
229 char input_data[] =
230 "[zero]\n"
231 "[one]\n"
232 "key1 = first\n"
233 "key1 = second\n"
234 "key1 = third\n"
235 "key1 = last\n";
236
237 char delete_first[] =
238 "[zero]\n"
239 "[one]\n"
240 "key1 = first\n"
241 "key1 = second\n"
242 "key1 = third\n";
243
244 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
245 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
246
247 ret = ini_config_delete_value(in_cfg, "one", COL_DSP_NDUP, "key1", 3);
248 fail_unless(ret == EOK, "delete value should fail. Error: %d", ret);
249 assert_configuration_equal(delete_first, sizeof(delete_first), in_cfg);
250
251 ini_config_destroy(in_cfg);
252 }
253 END_TEST
254
255 START_TEST(test_update_comments_wrong_arguments)
256 {
257 int ret = EOK;
258 struct ini_cfgobj *in_cfg;
259
260 char input_data[] =
261 "[one]\n"
262 "key1 = value1\n"
263 "key1 = value1a\n"
264 "key1 = value1a_bis\n"
265 "// This is a test\n"
266 "key1 = value1b\n"
267 "key1 = value1c\n"
268 "key2 = value2\n"
269 "key3 = value3\n";
270
271 const char *comment[] = { "// This is a test", NULL };
272
273 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
274 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
275
276 /* missing ini_config */
277 ret = ini_config_update_comment(NULL, "one", "key1", comment, 1, 3);
278 fail_unless(ret == EINVAL, "update commants should fail. Error: %d", ret);
279 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
280
281 /* missing section */
282 ret = ini_config_update_comment(in_cfg, NULL, "key1", comment, 1, 3);
283 fail_unless(ret == EINVAL, "update commants should fail. Error: %d", ret);
284 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
285
286 /* missing key */
287 ret = ini_config_update_comment(in_cfg, "one", NULL, comment, 1, 3);
288 fail_unless(ret == EINVAL, "update commants should fail. Error: %d", ret);
289 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
290
291 /* wrong section */
292 ret = ini_config_update_comment(in_cfg, "noexist", "key1", comment, 1, 3);
293 fail_unless(ret == ENOENT, "update commants should fail. Error: %d", ret);
294 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
295
296 /* wrong key */
297 ret = ini_config_update_comment(in_cfg, "one", "noexist", comment, 1, 3);
298 fail_unless(ret == ENOENT, "update commants should fail. Error: %d", ret);
299 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
300
301 /* value index is too low */
302 ret = ini_config_update_comment(in_cfg, "one", "key1", comment, 1, -1);
303 fail_unless(ret == EINVAL, "update commants should fail. Error: %d", ret);
304 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
305
306 /* value index is too high */
307 ret = ini_config_update_comment(in_cfg, "one", "key1", comment, 1, 5);
308 fail_unless(ret == ENOENT, "update commants should fail. Error: %d", ret);
309 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
310
311 ini_config_destroy(in_cfg);
312 }
313 END_TEST
314
315 START_TEST(test_update_comments)
316 {
317 int ret = EOK;
318 struct ini_cfgobj *in_cfg;
319
320 char input_data[] =
321 "[one]\n"
322 "key1 = value1\n"
323 "key1 = value1a\n"
324 "key1 = value1a_bis\n"
325 "key1 = value1b\n"
326 "// this is a comment\n"
327 "key1 = value1c\n"
328 "key2 = value2\n"
329 "key3 = value3\n";
330
331 char exp_data_1comment[] =
332 "[one]\n"
333 "// This is a test1\n"
334 "key1 = value1\n"
335 "key1 = value1a\n"
336 "key1 = value1a_bis\n"
337 "key1 = value1b\n"
338 "// this is a comment\n"
339 "key1 = value1c\n"
340 "key2 = value2\n"
341 "key3 = value3\n";
342
343 char exp_data_2comments[] =
344 "[one]\n"
345 "// This is a test1\n"
346 "// This is a test2\n"
347 "key1 = value1\n"
348 "key1 = value1a\n"
349 "key1 = value1a_bis\n"
350 "key1 = value1b\n"
351 "// this is a comment\n"
352 "key1 = value1c\n"
353 "key2 = value2\n"
354 "key3 = value3\n";
355
356 char exp_data_1comment_after2[] =
357 "[one]\n"
358 "key1 = value1\n"
359 "key1 = value1a\n"
360 "// This is a test1\n"
361 "key1 = value1a_bis\n"
362 "key1 = value1b\n"
363 "// this is a comment\n"
364 "key1 = value1c\n"
365 "key2 = value2\n"
366 "key3 = value3\n";
367
368 char exp_replaced[] =
369 "[one]\n"
370 "key1 = value1\n"
371 "key1 = value1a\n"
372 "key1 = value1a_bis\n"
373 "key1 = value1b\n"
374 "// This is a test1\n"
375 "// This is a test2\n"
376 "key1 = value1c\n"
377 "key2 = value2\n"
378 "key3 = value3\n";
379
380 char exp_removed_comment[] =
381 "[one]\n"
382 "key1 = value1\n"
383 "key1 = value1a\n"
384 "key1 = value1a_bis\n"
385 "key1 = value1b\n"
386 "key1 = value1c\n"
387 "key2 = value2\n"
388 "key3 = value3\n";
389
390 const char *comments[] = { "// This is a test1", "// This is a test2",
391 NULL };
392
393 const char *empty_comment[] = { NULL };
394
395 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
396 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
397
398 /* add comments with size */
399 ret = ini_config_update_comment(in_cfg, "one", "key1", comments, 1, 0);
400 fail_unless(ret == EOK, "update commants should fail. Error: %d", ret);
401 assert_configuration_equal(exp_data_1comment, sizeof(exp_data_1comment),
402 in_cfg);
403 ini_config_destroy(in_cfg);
404
405
406 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
407 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
408
409 /* add comments with size */
410 ret = ini_config_update_comment(in_cfg, "one", "key1", comments, 2, 0);
411 fail_unless(ret == EOK, "update commants should fail. Error: %d", ret);
412 assert_configuration_equal(exp_data_2comments, sizeof(exp_data_2comments),
413 in_cfg);
414 ini_config_destroy(in_cfg);
415
416 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
417 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
418
419 /* add comments (NULL terminated array), size is 0 */
420 ret = ini_config_update_comment(in_cfg, "one", "key1", comments, 0, 0);
421 fail_unless(ret == EOK, "update commants should fail. Error: %d", ret);
422 assert_configuration_equal(exp_data_2comments, sizeof(exp_data_2comments),
423 in_cfg);
424 ini_config_destroy(in_cfg);
425
426 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
427 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
428
429 /* add comments (NULL terminated array), size is 0 */
430 ret = ini_config_update_comment(in_cfg, "one", "key1", comments, 1, 2);
431 fail_unless(ret == EOK, "update commants should fail. Error: %d", ret);
432 assert_configuration_equal(exp_data_1comment_after2,
433 sizeof(exp_data_1comment_after2),
434 in_cfg);
435 ini_config_destroy(in_cfg);
436
437 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
438 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
439
440 /* replace comment */
441 ret = ini_config_update_comment(in_cfg, "one", "key1", comments, 0, 4);
442 fail_unless(ret == EOK, "update commants should fail. Error: %d", ret);
443 assert_configuration_equal(exp_replaced, sizeof(exp_replaced),
444 in_cfg);
445 ini_config_destroy(in_cfg);
446
447 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
448 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
449
450 /* remove comment */
451 ret = ini_config_update_comment(in_cfg, "one", "key1",
452 empty_comment, 0, 4);
453 fail_unless(ret == EOK, "update commants should fail. Error: %d", ret);
454 assert_configuration_equal(exp_removed_comment,
455 sizeof(exp_removed_comment),
456 in_cfg);
457 ini_config_destroy(in_cfg);
458
459 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
460 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
461
462 /* remove comment (2nd way; argument is NULL) */
463 ret = ini_config_update_comment(in_cfg, "one", "key1",
464 NULL, 0, 4);
465 fail_unless(ret == EOK, "update commants should fail. Error: %d", ret);
466 assert_configuration_equal(exp_removed_comment,
467 sizeof(exp_removed_comment),
468 in_cfg);
469 ini_config_destroy(in_cfg);
470 }
471 END_TEST
472
473 START_TEST(test_add_str_wrong_arguments)
474 {
475 int ret = EOK;
476 struct ini_cfgobj *in_cfg;
477
478 char input_data[] =
479 "[zero]\n"
480 "[one]\n"
481 "key1 = value1a\n";
482
483 const char *comments[] = { "// This is a test1", "// This is a test2",
484 NULL };
485
486 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
487 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
488
489 /* missing ini_config */
490 ret = ini_config_add_str_value(NULL, "one", "newkey", "newvalue",
491 comments, 2, WRAP_SIZE, COL_DSP_END, "key1",
492 0, INI_VA_NOCHECK);
493 fail_unless(ret == EINVAL, "Add str should fail. Error: %d", ret);
494 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
495
496 /* missing section */
497 ret = ini_config_add_str_value(in_cfg, NULL, "newkey", "newvalue",
498 comments, 2, WRAP_SIZE, COL_DSP_END, "key1",
499 0, INI_VA_NOCHECK);
500 fail_unless(ret == EINVAL, "Add str should fail. Error: %d", ret);
501 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
502
503 /* missing key */
504 ret = ini_config_add_str_value(in_cfg, "one", NULL, "newvalue",
505 comments, 2, WRAP_SIZE, COL_DSP_END, "key1",
506 0, INI_VA_NOCHECK);
507 fail_unless(ret == EINVAL, "Add str should fail. Error: %d", ret);
508 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
509
510 /* missing value */
511 ret = ini_config_add_str_value(in_cfg, "one", "newkey", NULL,
512 comments, 2, WRAP_SIZE, COL_DSP_END, "key1",
513 0, INI_VA_NOCHECK);
514 fail_unless(ret == EINVAL, "Add str should fail. Error: %d", ret);
515 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
516
517 /* wrong index */
518 ret = ini_config_add_str_value(in_cfg, "one", "newkey", "newvalue",
519 comments, 2, WRAP_SIZE, COL_DSP_END, "key1",
520 -1, INI_VA_NOCHECK);
521 fail_unless(ret == EINVAL, "Add str should fail. Error: %d", ret);
522 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
523
524 /* wrong flag */
525 ret = ini_config_add_str_value(in_cfg, "one", "newkey", "newvalue",
526 comments, 2, WRAP_SIZE, COL_DSP_END, "key1",
527 0, 0xff);
528 fail_unless(ret == ENOSYS, "Add str should fail. Error: %d", ret);
529 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
530
531 /* add duplicate for missing key */
532 ret = ini_config_add_str_value(in_cfg, "one", "newkey", "newvalue",
533 comments, 2, WRAP_SIZE, COL_DSP_NDUP,
534 "key1", 0, INI_VA_NOCHECK);
535 fail_unless(ret == ENOENT, "Add str should fail. Error: %d", ret);
536 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
537
538 ini_config_destroy(in_cfg);
539 }
540 END_TEST
541
542 START_TEST(test_add_str_simple)
543 {
544 int ret = EOK;
545 struct ini_cfgobj *in_cfg;
546
547 char input_data[] =
548 "[zero]\n"
549 "[one]\n"
550 "key1 = value1a\n"
551 "key1 = value1b\n"
552 "key2 = value2a\n";
553
554 const char *comments[] = { "// This is a test1", "// This is a test2",
555 NULL };
556
557 char add_new_value_to_end[] =
558 "[zero]\n"
559 "[one]\n"
560 "key1 = value1a\n"
561 "key1 = value1b\n"
562 "key2 = value2a\n"
563 "newkey = newvalue\n";
564
565 char add_new_value_to_end_with_comment[] =
566 "[zero]\n"
567 "[one]\n"
568 "key1 = value1a\n"
569 "key1 = value1b\n"
570 "key2 = value2a\n"
571 "// This is a test1\n"
572 "// This is a test2\n"
573 "newkey = newvalue\n";
574
575 char add_new_value_to_front[] =
576 "[zero]\n"
577 "[one]\n"
578 "newkey = newvalue\n"
579 "key1 = value1a\n"
580 "key1 = value1b\n"
581 "key2 = value2a\n";
582
583 char add_new_value_to_front_with_comment[] =
584 "[zero]\n"
585 "[one]\n"
586 "// This is a test1\n"
587 "// This is a test2\n"
588 "newkey = newvalue\n"
589 "key1 = value1a\n"
590 "key1 = value1b\n"
591 "key2 = value2a\n";
592
593 char add_new_value_after_key1[] =
594 "[zero]\n"
595 "[one]\n"
596 "key1 = value1a\n"
597 "newkey = newvalue\n"
598 "key1 = value1b\n"
599 "key2 = value2a\n";
600
601 char add_new_value_before_key2[] =
602 "[zero]\n"
603 "[one]\n"
604 "key1 = value1a\n"
605 "key1 = value1b\n"
606 "newkey = newvalue\n"
607 "key2 = value2a\n";
608
609 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
610 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
611
612 /* Simple add new value to end of section */
613 ret = ini_config_add_str_value(in_cfg, "one", "newkey", "newvalue",
614 NULL, 0, WRAP_SIZE, COL_DSP_END, NULL,
615 0, INI_VA_NOCHECK);
616 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
617 assert_configuration_equal(add_new_value_to_end,
618 sizeof(add_new_value_to_end), in_cfg);
619 ini_config_destroy(in_cfg);
620
621 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
622 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
623
624 /* add new value with comment to end of section */
625 ret = ini_config_add_str_value(in_cfg, "one", "newkey", "newvalue",
626 comments, 2, WRAP_SIZE, COL_DSP_END, NULL,
627 0, INI_VA_NOCHECK);
628 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
629 assert_configuration_equal(add_new_value_to_end_with_comment,
630 sizeof(add_new_value_to_end_with_comment),
631 in_cfg);
632 ini_config_destroy(in_cfg);
633
634
635 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
636 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
637
638 /* Simple add new value to the begin of section */
639 ret = ini_config_add_str_value(in_cfg, "one", "newkey", "newvalue",
640 NULL, 0, WRAP_SIZE, COL_DSP_FRONT, NULL,
641 0, INI_VA_NOCHECK);
642 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
643 assert_configuration_equal(add_new_value_to_front,
644 sizeof(add_new_value_to_front), in_cfg);
645 ini_config_destroy(in_cfg);
646
647 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
648 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
649
650 /* add new value with comment to the begin of section */
651 ret = ini_config_add_str_value(in_cfg, "one", "newkey", "newvalue",
652 comments, 2, WRAP_SIZE, COL_DSP_FRONT, NULL,
653 0, INI_VA_NOCHECK);
654 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
655 assert_configuration_equal(add_new_value_to_front_with_comment,
656 sizeof(add_new_value_to_front_with_comment),
657 in_cfg);
658 ini_config_destroy(in_cfg);
659
660 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
661 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
662
663 /* add new value after "key1" with index 0 */
664 ret = ini_config_add_str_value(in_cfg, "one", "newkey", "newvalue",
665 NULL, 0, WRAP_SIZE, COL_DSP_AFTER, "key1",
666 0, INI_VA_NOCHECK);
667 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
668 assert_configuration_equal(add_new_value_after_key1,
669 sizeof(add_new_value_after_key1),
670 in_cfg);
671 ini_config_destroy(in_cfg);
672
673 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
674 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
675
676 /* add new value after "key1" with index 1 (index ignored) */
677 ret = ini_config_add_str_value(in_cfg, "one", "newkey", "newvalue",
678 NULL, 0, WRAP_SIZE, COL_DSP_AFTER, "key1",
679 1, INI_VA_NOCHECK);
680 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
681 assert_configuration_equal(add_new_value_after_key1,
682 sizeof(add_new_value_after_key1),
683 in_cfg);
684 ini_config_destroy(in_cfg);
685
686 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
687 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
688
689 /* add new value after "key1" with very big index (index ignored) */
690 ret = ini_config_add_str_value(in_cfg, "one", "newkey", "newvalue",
691 NULL, 0, WRAP_SIZE, COL_DSP_AFTER, "key1",
692 1000, INI_VA_NOCHECK);
693 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
694 assert_configuration_equal(add_new_value_after_key1,
695 sizeof(add_new_value_after_key1),
696 in_cfg);
697 ini_config_destroy(in_cfg);
698
699 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
700 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
701
702 /* add new value before "key2" */
703 ret = ini_config_add_str_value(in_cfg, "one", "newkey", "newvalue",
704 NULL, 0, WRAP_SIZE, COL_DSP_BEFORE, "key2",
705 0, INI_VA_NOCHECK);
706 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
707 assert_configuration_equal(add_new_value_before_key2,
708 sizeof(add_new_value_before_key2),
709 in_cfg);
710 ini_config_destroy(in_cfg);
711 }
712 END_TEST
713
714 START_TEST(test_add_str_duplicate)
715 {
716 int ret = EOK;
717 struct ini_cfgobj *in_cfg;
718
719 char input_data[] =
720 "[zero]\n"
721 "[one]\n"
722 "key0 = value0a\n"
723 "key1 = value1a\n"
724 "key1 = value1b\n"
725 "// This is a test1\n"
726 "key2 = value2a\n";
727
728 char add_first_duplicate[] =
729 "[zero]\n"
730 "[one]\n"
731 "key0 = newvalue\n"
732 "key0 = value0a\n"
733 "key1 = value1a\n"
734 "key1 = value1b\n"
735 "// This is a test1\n"
736 "key2 = value2a\n";
737
738 char add_first_duplicate_for_multi[] =
739 "[zero]\n"
740 "[one]\n"
741 "key0 = value0a\n"
742 "key1 = newvalue\n"
743 "key1 = value1a\n"
744 "key1 = value1b\n"
745 "// This is a test1\n"
746 "key2 = value2a\n";
747
748 char add_last_duplicate[] =
749 "[zero]\n"
750 "[one]\n"
751 "key0 = value0a\n"
752 "key1 = value1a\n"
753 "key1 = value1b\n"
754 "key1 = newvalue\n"
755 "// This is a test1\n"
756 "key2 = value2a\n";
757
758 char add_duplicate_with_index1[] =
759 "[zero]\n"
760 "[one]\n"
761 "key0 = value0a\n"
762 "key1 = value1a\n"
763 "// This is a test1\n"
764 "// This is a test2\n"
765 "key1 = newvalue\n"
766 "key1 = value1b\n"
767 "// This is a test1\n"
768 "key2 = value2a\n";
769
770 const char *comments[] = { "// This is a test1", "// This is a test2",
771 NULL };
772
773 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
774 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
775
776 /* cannot add key as the first duplicate for non-existing key */
777 ret = ini_config_add_str_value(in_cfg, "one", "noexist", "newvalue",
778 NULL, 0, WRAP_SIZE, COL_DSP_FIRSTDUP,
779 NULL, 0, INI_VA_NOCHECK);
780 fail_unless(ret == ENOENT, "Add duplicate key must fail. Error: %d", ret);
781 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
782
783 /* cannot add key as the last duplicate for non-existing key */
784 ret = ini_config_add_str_value(in_cfg, "one", "noexist", "newvalue",
785 NULL, 0, WRAP_SIZE, COL_DSP_LASTDUP,
786 NULL, 0, INI_VA_NOCHECK);
787 fail_unless(ret == ENOENT, "Add duplicate key must fail. Error: %d", ret);
788 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
789
790 /* Add duplicate value */
791 ret = ini_config_add_str_value(in_cfg, "one", "key0", "newvalue",
792 NULL, 0, WRAP_SIZE, COL_DSP_FIRSTDUP,
793 NULL, 0, INI_VA_NOCHECK);
794 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
795 assert_configuration_equal(add_first_duplicate,
796 sizeof(add_first_duplicate), in_cfg);
797 ini_config_destroy(in_cfg);
798
799 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
800 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
801
802 /* Add duplicate value with other_key (reference key is ignored) */
803 ret = ini_config_add_str_value(in_cfg, "one", "key0", "newvalue",
804 NULL, 0, WRAP_SIZE, COL_DSP_FIRSTDUP,
805 "key2", 0, INI_VA_NOCHECK);
806 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
807 assert_configuration_equal(add_first_duplicate,
808 sizeof(add_first_duplicate), in_cfg);
809 ini_config_destroy(in_cfg);
810
811 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
812 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
813
814 /* Add duplicate value with index (index is ignored) */
815 ret = ini_config_add_str_value(in_cfg, "one", "key0", "newvalue",
816 NULL, 0, WRAP_SIZE, COL_DSP_FIRSTDUP,
817 "key0", 1, INI_VA_NOCHECK);
818 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
819 assert_configuration_equal(add_first_duplicate,
820 sizeof(add_first_duplicate), in_cfg);
821 ini_config_destroy(in_cfg);
822
823 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
824 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
825
826 /* Add duplicate value with multiple keys */
827 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
828 NULL, 0, WRAP_SIZE, COL_DSP_FIRSTDUP,
829 NULL, 0, INI_VA_NOCHECK);
830 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
831 assert_configuration_equal(add_first_duplicate_for_multi,
832 sizeof(add_first_duplicate_for_multi), in_cfg);
833 ini_config_destroy(in_cfg);
834
835 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
836 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
837
838 /* Add duplicate as last */
839 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
840 NULL, 0, WRAP_SIZE, COL_DSP_LASTDUP,
841 NULL, 0, INI_VA_NOCHECK);
842 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
843 assert_configuration_equal(add_last_duplicate,
844 sizeof(add_last_duplicate), in_cfg);
845 ini_config_destroy(in_cfg);
846
847 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
848 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
849
850 /* Add duplicate with index 0 (the same as COL_DSP_FIRSTDUP) */
851 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
852 NULL, 0, WRAP_SIZE, COL_DSP_NDUP,
853 NULL, 0, INI_VA_NOCHECK);
854 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
855 assert_configuration_equal(add_first_duplicate_for_multi,
856 sizeof(add_first_duplicate_for_multi), in_cfg);
857 ini_config_destroy(in_cfg);
858
859 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
860 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
861
862 /* Add duplicate with index and other_key (other_key is ignored) */
863 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
864 NULL, 0, WRAP_SIZE, COL_DSP_NDUP,
865 "key0", 0, INI_VA_NOCHECK);
866 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
867 assert_configuration_equal(add_first_duplicate_for_multi,
868 sizeof(add_first_duplicate_for_multi), in_cfg);
869 ini_config_destroy(in_cfg);
870
871 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
872 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
873
874 /* Add duplicate with big index (the same as COL_DSP_NDUP) */
875 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
876 NULL, 0, WRAP_SIZE, COL_DSP_NDUP,
877 NULL, 100, INI_VA_NOCHECK);
878 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
879 assert_configuration_equal(add_last_duplicate,
880 sizeof(add_last_duplicate), in_cfg);
881 ini_config_destroy(in_cfg);
882
883 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
884 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
885
886 /* Add duplicate with index 1 */
887 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
888 comments, 0, WRAP_SIZE, COL_DSP_NDUP,
889 NULL, 1, INI_VA_NOCHECK);
890 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
891 assert_configuration_equal(add_duplicate_with_index1,
892 sizeof(add_duplicate_with_index1), in_cfg);
893 ini_config_destroy(in_cfg);
894 }
895 END_TEST
896
897 START_TEST(test_add_str_update_specific_value)
898 {
899 int ret = EOK;
900 struct ini_cfgobj *in_cfg;
901
902 char input_data[] =
903 "[zero]\n"
904 "[one]\n"
905 "key0 = valuer0\n"
906 "key1 = value1a\n"
907 "key1 = value1b\n"
908 "key1 = value1c\n"
909 "key1 = value1d\n"
910 "key2 = value2\n"
911 "key3 = value3\n";
912
913 const char *comments[] = { "// This is a test1", "// This is a test2",
914 NULL };
915
916 char modify_existing_value[] =
917 "[zero]\n"
918 "[one]\n"
919 "key0 = valuer0\n"
920 "// This is a test1\n"
921 "// This is a test2\n"
922 "key1 = newvalue\n"
923 "key1 = value1b\n"
924 "key1 = value1c\n"
925 "key1 = value1d\n"
926 "key2 = value2\n"
927 "key3 = value3\n";
928
929 char modify_existing_value_index[] =
930 "[zero]\n"
931 "[one]\n"
932 "key0 = valuer0\n"
933 "key1 = value1a\n"
934 "key1 = value1b\n"
935 "key1 = value1c\n"
936 "key1 = newvalue\n"
937 "key2 = value2\n"
938 "key3 = value3\n";
939
940 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
941 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
942
943 /* update of non-existing value fails */
944 ret = ini_config_add_str_value(in_cfg, "one", "key4", "newvalue",
945 comments, 2, WRAP_SIZE, COL_DSP_NDUP, NULL,
946 0, INI_VA_MOD);
947 fail_unless(ret == ENOENT, "Failed to add str. Error: %d", ret);
948 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
949 ini_config_destroy(in_cfg);
950
951 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
952 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
953
954 /* INI_VA_MOD will not add key for non-existing value
955 * even with proper position flag. This is difference between
956 * INI_VA_MOD and INI_VA_MODADD */
957 ret = ini_config_add_str_value(in_cfg, "one", "key1.x", "newvalue",
958 NULL, 0, WRAP_SIZE, COL_DSP_BEFORE, "key2",
959 0, INI_VA_MOD);
960 fail_unless(ret == ENOENT, "Failed to add str. Error: %d", ret);
961 assert_configuration_equal(input_data,
962 sizeof(input_data), in_cfg);
963 ini_config_destroy(in_cfg);
964
965 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
966 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
967
968 /* strict flag + update of non-existing value fails */
969 ret = ini_config_add_str_value(in_cfg, "one", "key4", "newvalue",
970 comments, 2, WRAP_SIZE, COL_DSP_NDUP, NULL,
971 0, INI_VA_MOD_E);
972 fail_unless(ret == ENOENT, "Failed to add str. Error: %d", ret);
973 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
974 ini_config_destroy(in_cfg);
975
976 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
977 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
978
979 /* add will update existing value */
980 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
981 comments, 2, WRAP_SIZE, COL_DSP_NDUP, NULL,
982 0, INI_VA_MOD);
983 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
984 assert_configuration_equal(modify_existing_value,
985 sizeof(modify_existing_value), in_cfg);
986 ini_config_destroy(in_cfg);
987
988 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
989 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
990
991 /* strict flag + add will update existing value */
992 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
993 comments, 2, WRAP_SIZE, COL_DSP_NDUP, NULL,
994 0, INI_VA_MOD_E);
995 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
996 assert_configuration_equal(modify_existing_value,
997 sizeof(modify_existing_value), in_cfg);
998 ini_config_destroy(in_cfg);
999
1000 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1001 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1002
1003 /* add will update existing value using index */
1004 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
1005 NULL, 0, WRAP_SIZE, COL_DSP_NDUP, NULL,
1006 3, INI_VA_MOD);
1007 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
1008 assert_configuration_equal(modify_existing_value_index,
1009 sizeof(modify_existing_value_index), in_cfg);
1010 ini_config_destroy(in_cfg);
1011
1012 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1013 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1014
1015 /* strict flag + add will update existing value using index */
1016 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
1017 NULL, 0, WRAP_SIZE, COL_DSP_NDUP, NULL,
1018 3, INI_VA_MOD_E);
1019 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
1020 assert_configuration_equal(modify_existing_value_index,
1021 sizeof(modify_existing_value_index), in_cfg);
1022 ini_config_destroy(in_cfg);
1023
1024 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1025 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1026
1027 /* add will update existing value using big index */
1028 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
1029 NULL, 0, WRAP_SIZE, COL_DSP_NDUP, NULL,
1030 1000, INI_VA_MOD);
1031 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
1032 assert_configuration_equal(modify_existing_value_index,
1033 sizeof(modify_existing_value_index), in_cfg);
1034 ini_config_destroy(in_cfg);
1035
1036 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1037 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1038
1039 /* strict flag + add will NOT update existing value using big index */
1040 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
1041 NULL, 0, WRAP_SIZE, COL_DSP_NDUP, NULL,
1042 1000, INI_VA_MOD_E);
1043 fail_unless(ret == ENOENT, "Failed to add str. Error: %d", ret);
1044 assert_configuration_equal(input_data,
1045 sizeof(input_data), in_cfg);
1046 ini_config_destroy(in_cfg);
1047 }
1048 END_TEST
1049
1050 START_TEST(test_add_str_update_MODADD)
1051 {
1052 int ret = EOK;
1053 struct ini_cfgobj *in_cfg;
1054
1055 char input_data[] =
1056 "[zero]\n"
1057 "[one]\n"
1058 "key0 = valuer0\n"
1059 "key1 = value1a\n"
1060 "key1 = value1b\n"
1061 "key1 = value1c\n"
1062 "key1 = value1d\n"
1063 "key2 = value2\n"
1064 "key3 = value3\n";
1065
1066 char modify_add_non_existing_value[] =
1067 "[zero]\n"
1068 "[one]\n"
1069 "key0 = valuer0\n"
1070 "key1 = value1a\n"
1071 "key1 = value1b\n"
1072 "key1 = value1c\n"
1073 "key1 = value1d\n"
1074 "key1.x = newvalue\n"
1075 "key2 = value2\n"
1076 "key3 = value3\n";
1077
1078 const char *comments[] = { "// This is a test1", "// This is a test2",
1079 NULL };
1080
1081 char modify_existing_value[] =
1082 "[zero]\n"
1083 "[one]\n"
1084 "key0 = valuer0\n"
1085 "// This is a test1\n"
1086 "// This is a test2\n"
1087 "key1 = newvalue\n"
1088 "key1 = value1b\n"
1089 "key1 = value1c\n"
1090 "key1 = value1d\n"
1091 "key2 = value2\n"
1092 "key3 = value3\n";
1093
1094 char modify_existing_value_index[] =
1095 "[zero]\n"
1096 "[one]\n"
1097 "key0 = valuer0\n"
1098 "key1 = value1a\n"
1099 "key1 = value1b\n"
1100 "key1 = value1c\n"
1101 "key1 = newvalue\n"
1102 "key2 = value2\n"
1103 "key3 = value3\n";
1104
1105 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1106 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1107
1108 /* update of non-existing value fails with wrong position */
1109 ret = ini_config_add_str_value(in_cfg, "one", "key4", "newvalue",
1110 comments, 2, WRAP_SIZE, COL_DSP_NDUP, NULL,
1111 0, INI_VA_MODADD);
1112 fail_unless(ret == ENOENT, "Failed to add str. Error: %d", ret);
1113 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1114 ini_config_destroy(in_cfg);
1115
1116 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1117 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1118
1119 /* INI_VA_MODADD will add key for non-existing value*/
1120 ret = ini_config_add_str_value(in_cfg, "one", "key1.x", "newvalue",
1121 NULL, 0, WRAP_SIZE, COL_DSP_BEFORE, "key2",
1122 0, INI_VA_MODADD);
1123 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
1124 assert_configuration_equal(modify_add_non_existing_value,
1125 sizeof(modify_add_non_existing_value), in_cfg);
1126 ini_config_destroy(in_cfg);
1127
1128 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1129 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1130
1131 /* strict flag + update of non-existing value fails */
1132 ret = ini_config_add_str_value(in_cfg, "one", "key4", "newvalue",
1133 comments, 2, WRAP_SIZE, COL_DSP_NDUP, NULL,
1134 0, INI_VA_MODADD_E);
1135 fail_unless(ret == ENOENT, "Failed to add str. Error: %d", ret);
1136 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1137 ini_config_destroy(in_cfg);
1138
1139 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1140 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1141
1142 /* add will update existing value */
1143 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
1144 comments, 2, WRAP_SIZE, COL_DSP_NDUP, NULL,
1145 0, INI_VA_MODADD);
1146 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
1147 assert_configuration_equal(modify_existing_value,
1148 sizeof(modify_existing_value), in_cfg);
1149 ini_config_destroy(in_cfg);
1150
1151 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1152 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1153
1154 /* strict flag + add will update existing value */
1155 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
1156 comments, 2, WRAP_SIZE, COL_DSP_NDUP, NULL,
1157 0, INI_VA_MODADD_E);
1158 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
1159 assert_configuration_equal(modify_existing_value,
1160 sizeof(modify_existing_value), in_cfg);
1161 ini_config_destroy(in_cfg);
1162
1163 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1164 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1165
1166 /* add will update existing value using index */
1167 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
1168 NULL, 0, WRAP_SIZE, COL_DSP_NDUP, NULL,
1169 3, INI_VA_MODADD);
1170 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
1171 assert_configuration_equal(modify_existing_value_index,
1172 sizeof(modify_existing_value_index), in_cfg);
1173 ini_config_destroy(in_cfg);
1174
1175 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1176 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1177
1178 /* strict flag + add will update existing value using index */
1179 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
1180 NULL, 0, WRAP_SIZE, COL_DSP_NDUP, NULL,
1181 3, INI_VA_MODADD_E);
1182 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
1183 assert_configuration_equal(modify_existing_value_index,
1184 sizeof(modify_existing_value_index), in_cfg);
1185 ini_config_destroy(in_cfg);
1186
1187 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1188 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1189
1190 /* add will update existing value using big index */
1191 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
1192 NULL, 0, WRAP_SIZE, COL_DSP_NDUP, NULL,
1193 1000, INI_VA_MODADD);
1194 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
1195 assert_configuration_equal(modify_existing_value_index,
1196 sizeof(modify_existing_value_index), in_cfg);
1197 ini_config_destroy(in_cfg);
1198
1199 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1200 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1201
1202 /* strict flag + add will NOT update existing value using big index */
1203 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
1204 NULL, 0, WRAP_SIZE, COL_DSP_NDUP, NULL,
1205 1000, INI_VA_MODADD_E);
1206 fail_unless(ret == ENOENT, "Failed to add str. Error: %d", ret);
1207 assert_configuration_equal(input_data,
1208 sizeof(input_data), in_cfg);
1209 ini_config_destroy(in_cfg);
1210 }
1211 END_TEST
1212
1213 /* INI_VA_CLEAN does not have effect to operation without duplicit key */
1214 START_TEST(test_add_str_simple_clean)
1215 {
1216 int ret = EOK;
1217 struct ini_cfgobj *in_cfg;
1218
1219 char input_data[] =
1220 "[zero]\n"
1221 "[one]\n"
1222 "key1 = value1a\n"
1223 "key1 = value1b\n"
1224 "key2 = value2a\n";
1225
1226 const char *comments[] = { "// This is a test1", "// This is a test2",
1227 NULL };
1228
1229 char add_new_value_to_end[] =
1230 "[zero]\n"
1231 "[one]\n"
1232 "key1 = value1a\n"
1233 "key1 = value1b\n"
1234 "key2 = value2a\n"
1235 "newkey = newvalue\n";
1236
1237 char add_new_value_to_end_with_comment[] =
1238 "[zero]\n"
1239 "[one]\n"
1240 "key1 = value1a\n"
1241 "key1 = value1b\n"
1242 "key2 = value2a\n"
1243 "// This is a test1\n"
1244 "// This is a test2\n"
1245 "newkey = newvalue\n";
1246
1247 char add_new_value_to_front[] =
1248 "[zero]\n"
1249 "[one]\n"
1250 "newkey = newvalue\n"
1251 "key1 = value1a\n"
1252 "key1 = value1b\n"
1253 "key2 = value2a\n";
1254
1255 char add_new_value_to_front_with_comment[] =
1256 "[zero]\n"
1257 "[one]\n"
1258 "// This is a test1\n"
1259 "// This is a test2\n"
1260 "newkey = newvalue\n"
1261 "key1 = value1a\n"
1262 "key1 = value1b\n"
1263 "key2 = value2a\n";
1264
1265 char add_new_value_after_key1[] =
1266 "[zero]\n"
1267 "[one]\n"
1268 "key1 = value1a\n"
1269 "newkey = newvalue\n"
1270 "key1 = value1b\n"
1271 "key2 = value2a\n";
1272
1273 char add_new_value_before_key2[] =
1274 "[zero]\n"
1275 "[one]\n"
1276 "key1 = value1a\n"
1277 "key1 = value1b\n"
1278 "newkey = newvalue\n"
1279 "key2 = value2a\n";
1280
1281 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1282 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1283
1284 /* Simple add new value to end of section */
1285 ret = ini_config_add_str_value(in_cfg, "one", "newkey", "newvalue",
1286 NULL, 0, WRAP_SIZE, COL_DSP_END, NULL,
1287 0, INI_VA_CLEAN);
1288 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
1289 assert_configuration_equal(add_new_value_to_end,
1290 sizeof(add_new_value_to_end), in_cfg);
1291 ini_config_destroy(in_cfg);
1292
1293 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1294 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1295
1296 /* add new value with comment to end of section */
1297 ret = ini_config_add_str_value(in_cfg, "one", "newkey", "newvalue",
1298 comments, 2, WRAP_SIZE, COL_DSP_END, NULL,
1299 0, INI_VA_CLEAN);
1300 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
1301 assert_configuration_equal(add_new_value_to_end_with_comment,
1302 sizeof(add_new_value_to_end_with_comment),
1303 in_cfg);
1304 ini_config_destroy(in_cfg);
1305
1306
1307 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1308 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1309
1310 /* Simple add new value to the begin of section */
1311 ret = ini_config_add_str_value(in_cfg, "one", "newkey", "newvalue",
1312 NULL, 0, WRAP_SIZE, COL_DSP_FRONT, NULL,
1313 0, INI_VA_CLEAN);
1314 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
1315 assert_configuration_equal(add_new_value_to_front,
1316 sizeof(add_new_value_to_front), in_cfg);
1317 ini_config_destroy(in_cfg);
1318
1319 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1320 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1321
1322 /* add new value with comment to the begin of section */
1323 ret = ini_config_add_str_value(in_cfg, "one", "newkey", "newvalue",
1324 comments, 2, WRAP_SIZE, COL_DSP_FRONT, NULL,
1325 0, INI_VA_CLEAN);
1326 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
1327 assert_configuration_equal(add_new_value_to_front_with_comment,
1328 sizeof(add_new_value_to_front_with_comment),
1329 in_cfg);
1330 ini_config_destroy(in_cfg);
1331
1332 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1333 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1334
1335 /* add new value after "key1" with index 0 */
1336 ret = ini_config_add_str_value(in_cfg, "one", "newkey", "newvalue",
1337 NULL, 0, WRAP_SIZE, COL_DSP_AFTER, "key1",
1338 0, INI_VA_CLEAN);
1339 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
1340 assert_configuration_equal(add_new_value_after_key1,
1341 sizeof(add_new_value_after_key1),
1342 in_cfg);
1343 ini_config_destroy(in_cfg);
1344
1345 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1346 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1347
1348 /* add new value after "key1" with index 1 (index ignored) */
1349 ret = ini_config_add_str_value(in_cfg, "one", "newkey", "newvalue",
1350 NULL, 0, WRAP_SIZE, COL_DSP_AFTER, "key1",
1351 1, INI_VA_CLEAN);
1352 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
1353 assert_configuration_equal(add_new_value_after_key1,
1354 sizeof(add_new_value_after_key1),
1355 in_cfg);
1356 ini_config_destroy(in_cfg);
1357
1358 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1359 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1360
1361 /* add new value after "key1" with very big index (index ignored) */
1362 ret = ini_config_add_str_value(in_cfg, "one", "newkey", "newvalue",
1363 NULL, 0, WRAP_SIZE, COL_DSP_AFTER, "key1",
1364 1000, INI_VA_CLEAN);
1365 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
1366 assert_configuration_equal(add_new_value_after_key1,
1367 sizeof(add_new_value_after_key1),
1368 in_cfg);
1369 ini_config_destroy(in_cfg);
1370
1371 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1372 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1373
1374 /* add new value before "key2" */
1375 ret = ini_config_add_str_value(in_cfg, "one", "newkey", "newvalue",
1376 NULL, 0, WRAP_SIZE, COL_DSP_BEFORE, "key2",
1377 0, INI_VA_CLEAN);
1378 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
1379 assert_configuration_equal(add_new_value_before_key2,
1380 sizeof(add_new_value_before_key2),
1381 in_cfg);
1382 ini_config_destroy(in_cfg);
1383 }
1384 END_TEST
1385
1386 START_TEST(test_add_str_duplicate_error)
1387 {
1388 int ret = EOK;
1389 struct ini_cfgobj *in_cfg;
1390
1391 char input_data[] =
1392 "[zero]\n"
1393 "[one]\n"
1394 "key0 = value0a\n"
1395 "key1 = value1a\n"
1396 "key1 = value1b\n"
1397 "key2 = value2a\n";
1398
1399 char add_non_existing_value[] =
1400 "[zero]\n"
1401 "[one]\n"
1402 "key0 = value0a\n"
1403 "key1 = value1a\n"
1404 "key1 = value1b\n"
1405 "key1.x = newvalue\n"
1406 "key2 = value2a\n";
1407
1408 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1409 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1410
1411 /* INI_VA_DUPERROR will return EEXIST for duplicit key
1412 * and COL_DSP_FIRSTDUP will return error due to nonexisting duplicate
1413 */
1414 ret = ini_config_add_str_value(in_cfg, "one", "key0", "newvalue",
1415 NULL, 0, WRAP_SIZE, COL_DSP_FIRSTDUP,
1416 NULL, 0, INI_VA_DUPERROR);
1417 fail_unless(ret == EEXIST, "Failed to add str. Error: %d", ret);
1418 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1419 ini_config_destroy(in_cfg);
1420
1421 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1422 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1423
1424 /* INI_VA_DUPERROR will return EEXIST for duplicit key
1425 * and COL_DSP_FIRSTDUP will return error due to nonexisting duplicate
1426 */
1427 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
1428 NULL, 0, WRAP_SIZE, COL_DSP_FIRSTDUP,
1429 NULL, 0, INI_VA_DUPERROR);
1430 fail_unless(ret == EEXIST, "Failed to add str. Error: %d", ret);
1431 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1432 ini_config_destroy(in_cfg);
1433
1434 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1435 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1436
1437 /* INI_VA_DUPERROR will return EEXIST for duplicit key
1438 * and COL_DSP_FIRSTDUP will return error due to nonexisting duplicate
1439 */
1440 ret = ini_config_add_str_value(in_cfg, "one", "key2", "newvalue",
1441 NULL, 0, WRAP_SIZE, COL_DSP_FIRSTDUP,
1442 NULL, 0, INI_VA_DUPERROR);
1443 fail_unless(ret == EEXIST, "Failed to add str. Error: %d", ret);
1444 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1445 ini_config_destroy(in_cfg);
1446
1447 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1448 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1449
1450 /* INI_VA_DUPERROR will return EEXIST for duplicit key
1451 * and COL_DSP_LASTDUP will return error due to nonexisting duplicate
1452 */
1453 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
1454 NULL, 0, WRAP_SIZE, COL_DSP_LASTDUP,
1455 NULL, 0, INI_VA_DUPERROR);
1456 fail_unless(ret == EEXIST, "Failed to add str. Error: %d", ret);
1457 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1458 ini_config_destroy(in_cfg);
1459
1460 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1461 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1462
1463 /* INI_VA_DUPERROR will return EEXIST for duplicit key
1464 * and COL_DSP_NDUP will return error due to nonexisting duplicate
1465 */
1466 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
1467 NULL, 0, WRAP_SIZE, COL_DSP_NDUP,
1468 NULL, 0, INI_VA_DUPERROR);
1469 fail_unless(ret == EEXIST, "Failed to add str. Error: %d", ret);
1470 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1471 ini_config_destroy(in_cfg);
1472
1473 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1474 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1475
1476 /* INI_VA_DUPERROR will return EEXIST for duplicit key
1477 * and COL_DSP_NDUP will return error due to nonexisting duplicate
1478 * (index and other_key are ignored.
1479 */
1480 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
1481 NULL, 0, WRAP_SIZE, COL_DSP_NDUP,
1482 "key1", 1, INI_VA_DUPERROR);
1483 fail_unless(ret == EEXIST, "Failed to add str. Error: %d", ret);
1484 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1485 ini_config_destroy(in_cfg);
1486
1487 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1488 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1489
1490 /* INI_VA_DUPERROR will return EEXIST for duplicit key
1491 * and COL_DSP_NDUP will return error due to nonexisting duplicate
1492 * big index is ignored.
1493 */
1494 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
1495 NULL, 0, WRAP_SIZE, COL_DSP_NDUP,
1496 NULL, 100, INI_VA_DUPERROR);
1497 fail_unless(ret == EEXIST, "Failed to add str. Error: %d", ret);
1498 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1499 ini_config_destroy(in_cfg);
1500
1501 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1502 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1503
1504 /* entry will be added with INI_VA_DUPERROR and non-existing key. */
1505 ret = ini_config_add_str_value(in_cfg, "one", "key1.x", "newvalue",
1506 NULL, 0, WRAP_SIZE, COL_DSP_BEFORE, "key2",
1507 0, INI_VA_DUPERROR);
1508 fail_unless(ret == EOK, "Failed to add str. Error: %d", ret);
1509 assert_configuration_equal(add_non_existing_value,
1510 sizeof(add_non_existing_value), in_cfg);
1511 ini_config_destroy(in_cfg);
1512 }
1513 END_TEST
1514
1515 START_TEST(test_add_str_duplicate_clean)
1516 {
1517 int ret = EOK;
1518 struct ini_cfgobj *in_cfg;
1519
1520 char input_data[] =
1521 "[zero]\n"
1522 "[one]\n"
1523 "key0 = value0a\n"
1524 "key1 = value1a\n"
1525 "key1 = value1b\n"
1526 "// This is a test1\n"
1527 "key2 = value2a\n";
1528
1529 char only_removed_key[] =
1530 "[zero]\n"
1531 "[one]\n"
1532 "key1 = value1a\n"
1533 "key1 = value1b\n"
1534 "// This is a test1\n"
1535 "key2 = value2a\n";
1536
1537 char only_removed_all_duplicates[] =
1538 "[zero]\n"
1539 "[one]\n"
1540 "key0 = value0a\n"
1541 "// This is a test1\n"
1542 "key2 = value2a\n";
1543
1544 char only_removed_key_with_comment[] =
1545 "[zero]\n"
1546 "[one]\n"
1547 "key0 = value0a\n"
1548 "key1 = value1a\n"
1549 "key1 = value1b\n";
1550
1551 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1552 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1553
1554 /* INI_VA_CLEAN will remove duplicit key
1555 * and COL_DSP_FIRSTDUP will return error due to nonexisting duplicate
1556 */
1557 ret = ini_config_add_str_value(in_cfg, "one", "key0", "newvalue",
1558 NULL, 0, WRAP_SIZE, COL_DSP_FIRSTDUP,
1559 NULL, 0, INI_VA_CLEAN);
1560 fail_unless(ret == ENOENT, "Failed to add str. Error: %d", ret);
1561 assert_configuration_equal(only_removed_key, sizeof(only_removed_key),
1562 in_cfg);
1563 ini_config_destroy(in_cfg);
1564
1565 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1566 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1567
1568 /* INI_VA_CLEAN will remove app duplicit keys
1569 * and COL_DSP_FIRSTDUP will return error due to nonexisting duplicate
1570 */
1571 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
1572 NULL, 0, WRAP_SIZE, COL_DSP_FIRSTDUP,
1573 NULL, 0, INI_VA_CLEAN);
1574 fail_unless(ret == ENOENT, "Failed to add str. Error: %d", ret);
1575 assert_configuration_equal(only_removed_all_duplicates,
1576 sizeof(only_removed_all_duplicates), in_cfg);
1577 ini_config_destroy(in_cfg);
1578
1579 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1580 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1581
1582 /* INI_VA_CLEAN will remove app duplicit keys
1583 * and COL_DSP_FIRSTDUP will return error due to nonexisting duplicate
1584 */
1585 ret = ini_config_add_str_value(in_cfg, "one", "key2", "newvalue",
1586 NULL, 0, WRAP_SIZE, COL_DSP_FIRSTDUP,
1587 NULL, 0, INI_VA_CLEAN);
1588 fail_unless(ret == ENOENT, "Failed to add str. Error: %d", ret);
1589 assert_configuration_equal(only_removed_key_with_comment,
1590 sizeof(only_removed_key_with_comment), in_cfg);
1591 ini_config_destroy(in_cfg);
1592
1593 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1594 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1595
1596 /* INI_VA_CLEAN will remove all duplicit keys
1597 * and COL_DSP_LASTDUP will return error due to nonexisting duplicate
1598 */
1599 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
1600 NULL, 0, WRAP_SIZE, COL_DSP_LASTDUP,
1601 NULL, 0, INI_VA_CLEAN);
1602 fail_unless(ret == ENOENT, "Failed to add str. Error: %d", ret);
1603 assert_configuration_equal(only_removed_all_duplicates,
1604 sizeof(only_removed_all_duplicates), in_cfg);
1605 ini_config_destroy(in_cfg);
1606
1607 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1608 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1609
1610 /* INI_VA_CLEAN will remove all duplicit keys
1611 * and COL_DSP_NDUP will return error due to nonexisting duplicate
1612 */
1613 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
1614 NULL, 0, WRAP_SIZE, COL_DSP_NDUP,
1615 NULL, 0, INI_VA_CLEAN);
1616 fail_unless(ret == ENOENT, "Failed to add str. Error: %d", ret);
1617 assert_configuration_equal(only_removed_all_duplicates,
1618 sizeof(only_removed_all_duplicates), in_cfg);
1619 ini_config_destroy(in_cfg);
1620
1621 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1622 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1623
1624 /* INI_VA_CLEAN will remove all duplicit keys
1625 * and COL_DSP_NDUP will return error due to nonexisting duplicate
1626 * (index and other_key are ignored.
1627 */
1628 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
1629 NULL, 0, WRAP_SIZE, COL_DSP_NDUP,
1630 "key1", 1, INI_VA_CLEAN);
1631 fail_unless(ret == ENOENT, "Failed to add str. Error: %d", ret);
1632 assert_configuration_equal(only_removed_all_duplicates,
1633 sizeof(only_removed_all_duplicates), in_cfg);
1634 ini_config_destroy(in_cfg);
1635
1636 in_cfg = get_ini_config_from_str(input_data, sizeof(input_data));
1637 assert_configuration_equal(input_data, sizeof(input_data), in_cfg);
1638
1639 /* INI_VA_CLEAN will remove all duplicit keys
1640 * and COL_DSP_NDUP will return error due to nonexisting duplicate
1641 * big index is ignored.
1642 */
1643 ret = ini_config_add_str_value(in_cfg, "one", "key1", "newvalue",
1644 NULL, 0, WRAP_SIZE, COL_DSP_NDUP,
1645 NULL, 100, INI_VA_CLEAN);
1646 fail_unless(ret == ENOENT, "Failed to add str. Error: %d", ret);
1647 assert_configuration_equal(only_removed_all_duplicates,
1648 sizeof(only_removed_all_duplicates), in_cfg);
1649 ini_config_destroy(in_cfg);
1650 }
1651 END_TEST
1652
1653 static Suite *ini_configmod_utils_suite(void)
1654 {
1655 Suite *s = suite_create("ini_configmod");
1656
1657 TCase *tc_delete_properties = tcase_create("delete_properties");
1658 tcase_add_test(tc_delete_properties, test_delete_value_wrong_arguments);
1659 tcase_add_test(tc_delete_properties, test_delete_value);
1660
1661 suite_add_tcase(s, tc_delete_properties);
1662
1663 TCase *tc_update_comments = tcase_create("update_comments");
1664 tcase_add_test(tc_update_comments, test_update_comments_wrong_arguments);
1665 tcase_add_test(tc_update_comments, test_update_comments);
1666
1667 suite_add_tcase(s, tc_update_comments);
1668
1669 TCase *tc_add_string = tcase_create("add_string");
1670 tcase_add_test(tc_add_string, test_add_str_wrong_arguments);
1671 tcase_add_test(tc_add_string, test_add_str_simple);
1672 tcase_add_test(tc_add_string, test_add_str_duplicate);
1673 tcase_add_test(tc_add_string, test_add_str_update_specific_value);
1674 tcase_add_test(tc_add_string, test_add_str_update_MODADD);
1675 tcase_add_test(tc_add_string, test_add_str_simple_clean);
1676 tcase_add_test(tc_add_string, test_add_str_duplicate_clean);
1677 tcase_add_test(tc_add_string, test_add_str_duplicate_error);
1678
1679 suite_add_tcase(s, tc_add_string);
1680
1681 return s;
1682 }
1683
1684 int main(void)
1685 {
1686 int number_failed;
1687
1688 Suite *s = ini_configmod_utils_suite();
1689 SRunner *sr = srunner_create(s);
1690 /* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
1691 srunner_run_all(sr, CK_ENV);
1692 number_failed = srunner_ntests_failed(sr);
1693 srunner_free(sr);
1694 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
1695 }