Sun, 14 Jul 2013 17:11:34 +0200
added properties load/store functions
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
29 #include "prop_tests.h"
31 UCX_TEST_IMPLEMENT(test_ucx_prop_new) {
32 UcxPropParser *parser = ucx_prop_new();
34 UCX_TEST_BEGIN
36 UCX_TEST_ASSERT(parser != NULL, "failed");
37 UCX_TEST_ASSERT(parser->buffer == NULL, "parser has buffer");
38 UCX_TEST_ASSERT(parser->tmp == NULL, "parser has tmp buffer");
40 UCX_TEST_END
42 ucx_prop_free(parser);
43 }
45 UCX_TEST_IMPLEMENT(test_ucx_prop_parse) {
46 const char *tests[] = {
47 "name = value\n",
48 "name=value\n",
49 "n=value\n",
50 "name=v\n",
51 "n=v\n",
52 "name = value # comment\n",
53 "#comment\nn=v\n",
54 "# comment1\n# comment2\n\n \n\nname = value\n",
55 " name = value\n",
56 "name = value\n\n"
57 };
59 const char *names[] = {
60 "name",
61 "name",
62 "n",
63 "name",
64 "n",
65 "name",
66 "n",
67 "name",
68 "name",
69 "name"
70 };
72 const char *values[] = {
73 "value",
74 "value",
75 "value",
76 "v",
77 "v",
78 "value",
79 "v",
80 "value",
81 "value",
82 "value"
83 };
85 UCX_TEST_BEGIN
87 sstr_t name;
88 sstr_t value;
90 for(int i=0;i<10;i++) {
91 UcxPropParser *parser = ucx_prop_new();
93 ucx_prop_fill(parser, (char*)tests[i], strlen(tests[i]));
94 UCX_TEST_ASSERT(parser->buffer == tests[i], "fill failed");
95 UCX_TEST_ASSERT(parser->buflen == strlen(tests[i]), "wrong buflen");
97 int r = ucx_prop_parse(parser, &name, &value);
98 sstr_t n = sstr((char*)names[i]);
99 sstr_t v = sstr((char*)values[i]);
100 UCX_TEST_ASSERT(r == 1, "parse returned 0");
101 UCX_TEST_ASSERT((!sstrcmp(name, n)), "wrong property name");
102 UCX_TEST_ASSERT((!sstrcmp(value, v)), "wrong property value");
104 r = ucx_prop_parse(parser, &name, &value);
105 UCX_TEST_ASSERT(r == 0, "parse returned 1");
106 UCX_TEST_ASSERT(parser->tmp == NULL, "tmp not NULL");
107 UCX_TEST_ASSERT(parser->tmpcap == 0, "tmpcap not NULL");
108 UCX_TEST_ASSERT(parser->tmplen == 0, "tmplen not NULL");
110 ucx_prop_free(parser);
111 }
113 UCX_TEST_END
114 }
116 UCX_TEST_IMPLEMENT(test_ucx_prop_parse_multi) {
117 const char *names[] = {
118 "a",
119 "b",
120 "c",
121 "uap",
122 "name",
123 "key1",
124 "key2",
125 "key3"
126 };
128 const char *values[] = {
129 "a value",
130 "b value",
131 "core",
132 "core",
133 "ucx",
134 "value1",
135 "value2",
136 "value3"
137 };
139 const char *str = "#\n"
140 "# properties\n"
141 "# contains key/value pairs\n"
142 "#\n"
143 "a = a value\n"
144 "b = b value\n"
145 "c = core\n"
146 "\n# test\n"
147 "uap = core\n"
148 "name = ucx\n"
149 "# no = property\n"
150 "key1 = value1\n"
151 "#key1 = wrong value\n"
152 "#key2 = not value 2\n"
153 "key2 = value2\n"
154 "\n\n\n \n key3=value3\n";
156 UcxPropParser *parser = ucx_prop_new();
158 UCX_TEST_BEGIN
160 ucx_prop_fill(parser, (char*)str, strlen(str));
162 sstr_t name;
163 sstr_t value;
164 for(int i=0;i<8;i++) {
165 int r = ucx_prop_parse(parser, &name, &value);
166 UCX_TEST_ASSERT(r == 1, "parse returned 0");
167 UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)names[i]))), "wrong name");
168 UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)values[i]))),
169 "wrong value");
170 }
171 int r = ucx_prop_parse(parser, &name, &value);
172 UCX_TEST_ASSERT(r == 0, "parse returned 1");
174 UCX_TEST_END
176 ucx_prop_free(parser);
177 }
179 UCX_TEST_IMPLEMENT(test_ucx_prop_parse_part) {
180 UcxPropParser *parser = ucx_prop_new();
181 const char *str;
182 int r;
183 sstr_t name;
184 sstr_t value;
186 UCX_TEST_BEGIN
188 str = "";
189 ucx_prop_fill(parser, (char*)str, strlen(str));
190 r = ucx_prop_parse(parser, &name, &value);
191 UCX_TEST_ASSERT(r == 0, "parse returned 1");
193 str = " \n";
194 ucx_prop_fill(parser, (char*)str, strlen(str));
195 r = ucx_prop_parse(parser, &name, &value);
196 UCX_TEST_ASSERT(r == 0, "parse returned 1");
198 str = "name";
199 ucx_prop_fill(parser, (char*)str, strlen(str));
200 r = ucx_prop_parse(parser, &name, &value);
201 UCX_TEST_ASSERT(r == 0, "parse returned 1");
203 str = " ";
204 ucx_prop_fill(parser, (char*)str, strlen(str));
205 r = ucx_prop_parse(parser, &name, &value);
206 UCX_TEST_ASSERT(r == 0, "parse returned 1");
208 str = "= ";
209 ucx_prop_fill(parser, (char*)str, strlen(str));
210 r = ucx_prop_parse(parser, &name, &value);
211 UCX_TEST_ASSERT(r == 0, "parse returned 1");
213 str = "value";
214 ucx_prop_fill(parser, (char*)str, strlen(str));
215 r = ucx_prop_parse(parser, &name, &value);
216 UCX_TEST_ASSERT(r == 0, "parse returned 1");
218 str = "\n";
219 ucx_prop_fill(parser, (char*)str, strlen(str));
220 r = ucx_prop_parse(parser, &name, &value);
221 UCX_TEST_ASSERT(r == 1, "parse returned 0");
222 UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"name"))), "wrong name");
223 UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"value"))), "wrong value");
225 // second round
226 str = "#comment\n";
227 ucx_prop_fill(parser, (char*)str, strlen(str));
228 r = ucx_prop_parse(parser, &name, &value);
229 UCX_TEST_ASSERT(r == 0, "parse returned 1");
231 str = "#comment\nname = ";
232 ucx_prop_fill(parser, (char*)str, strlen(str));
233 r = ucx_prop_parse(parser, &name, &value);
234 UCX_TEST_ASSERT(r == 0, "parse returned 1");
236 str = "value\na = b\n";
237 ucx_prop_fill(parser, (char*)str, strlen(str));
238 r = ucx_prop_parse(parser, &name, &value);
239 UCX_TEST_ASSERT(r == 1, "parse returned 0");
240 UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"name"))), "wrong name");
241 UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"value"))), "wrong value");
243 r = ucx_prop_parse(parser, &name, &value);
244 UCX_TEST_ASSERT(r == 1, "parse returned 0");
245 UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"a"))), "wrong name");
246 UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"b"))), "wrong value");
248 str = "# comment\n#\n#\ntests = ";
249 ucx_prop_fill(parser, (char*)str, strlen(str));
250 r = ucx_prop_parse(parser, &name, &value);
251 UCX_TEST_ASSERT(r == 0, "parse returned 1");
253 str = "test1 ";
254 ucx_prop_fill(parser, (char*)str, strlen(str));
255 r = ucx_prop_parse(parser, &name, &value);
256 UCX_TEST_ASSERT(r == 0, "parse returned 1");
258 str = "test2 test3 test4\n";
259 sstr_t testv = sstr((char*)"test1 test2 test3 test4");
260 ucx_prop_fill(parser, (char*)str, strlen(str));
261 r = ucx_prop_parse(parser, &name, &value);
262 UCX_TEST_ASSERT(r == 1, "parse returned 0");
263 UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"tests"))), "wrong name");
264 UCX_TEST_ASSERT((!sstrcmp(value, testv)), "wrong value");
266 // test if parse finds a name/value after a tmp comment
267 str = "# just a comment";
268 ucx_prop_fill(parser, (char*)str, strlen(str));
269 r = ucx_prop_parse(parser, &name, &value);
270 UCX_TEST_ASSERT(r == 0, "parse returned 1");
272 str = " in 3";
273 ucx_prop_fill(parser, (char*)str, strlen(str));
274 r = ucx_prop_parse(parser, &name, &value);
275 UCX_TEST_ASSERT(r == 0, "parse returned 1");
277 str = " parts\na = 1\n";
278 ucx_prop_fill(parser, (char*)str, strlen(str));
279 r = ucx_prop_parse(parser, &name, &value);
280 UCX_TEST_ASSERT(r == 1, "parse returned 0");
281 UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"a"))), "wrong name");
282 UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"1"))), "wrong value");
284 UCX_TEST_END
286 ucx_prop_free(parser);
287 }
289 UCX_TEST_IMPLEMENT(test_ucx_prop_parse_long) {
290 UcxPropParser *parser = ucx_prop_new();
291 int r;
292 size_t name_len = 512;
293 char *long_name = (char*)malloc(name_len);
294 memset(long_name, 'a', 70);
295 memset(long_name+70, 'b', 242);
296 memset(long_name+312, 'c', 200);
298 size_t value_len = 2048;
299 char *long_value = (char*)malloc(value_len);
300 memset(long_value, 'x', 1024);
301 memset(long_value+1024, 'y', 1024);
303 UCX_TEST_BEGIN
305 sstr_t name;
306 sstr_t value;
308 ucx_prop_fill(parser, long_name, 10);
309 r = ucx_prop_parse(parser, &name, &value);
310 UCX_TEST_ASSERT(r == 0, "parse returned 1");
312 ucx_prop_fill(parser, long_name+10, 202);
313 r = ucx_prop_parse(parser, &name, &value);
314 UCX_TEST_ASSERT(r == 0, "parse returned 1");
316 ucx_prop_fill(parser, long_name+212, 200);
317 r = ucx_prop_parse(parser, &name, &value);
318 UCX_TEST_ASSERT(r == 0, "parse returned 1");
320 ucx_prop_fill(parser, long_name+412, 100);
321 r = ucx_prop_parse(parser, &name, &value);
322 UCX_TEST_ASSERT(r == 0, "parse returned 1");
324 const char *str = " = ";
325 ucx_prop_fill(parser, (char*)str, strlen(str));
326 r = ucx_prop_parse(parser, &name, &value);
327 UCX_TEST_ASSERT(r == 0, "parse returned 1");
329 ucx_prop_fill(parser, long_value, 512);
330 r = ucx_prop_parse(parser, &name, &value);
331 UCX_TEST_ASSERT(r == 0, "parse returned 1");
333 ucx_prop_fill(parser, long_value+512, 1024);
334 r = ucx_prop_parse(parser, &name, &value);
335 UCX_TEST_ASSERT(r == 0, "parse returned 1");
337 ucx_prop_fill(parser, long_value+1536, 512);
338 r = ucx_prop_parse(parser, &name, &value);
339 UCX_TEST_ASSERT(r == 0, "parse returned 1");
341 str = "\n#comment\nkey = value\n";
342 ucx_prop_fill(parser, (char*)str, strlen(str));
343 r = ucx_prop_parse(parser, &name, &value);
344 sstr_t n = sstrn(long_name, name_len);
345 sstr_t v = sstrn(long_value, value_len);
346 UCX_TEST_ASSERT(r == 1, "parse returned 0");
347 UCX_TEST_ASSERT((!sstrcmp(name, n)), "wrong name");
348 UCX_TEST_ASSERT((!sstrcmp(value, v)), "wrong value");
350 r = ucx_prop_parse(parser, &name, &value);
351 UCX_TEST_ASSERT(r == 1, "parse returned 0");
352 UCX_TEST_ASSERT((!sstrcmp(name, sstr((char*)"key"))), "wrong name");
353 UCX_TEST_ASSERT((!sstrcmp(value, sstr((char*)"value"))), "wrong value");
355 r = ucx_prop_parse(parser, &name, &value);
356 UCX_TEST_ASSERT(r == 0, "parse returned 1");
358 UCX_TEST_END
360 free(long_name);
361 free(long_value);
362 ucx_prop_free(parser);
363 }
365 UCX_TEST_IMPLEMENT(test_ucx_prop_parse2map) {
366 UcxMap *map = ucx_map_new(16);
367 UcxPropParser *parser = ucx_prop_new();
369 UCX_TEST_BEGIN
371 const char *str = "key1 = value1\nkey2 = value2\n\n#comment\n\nkey3 = value3\n";
372 ucx_prop_fill(parser, (char*)str, strlen(str));
374 int r = ucx_prop_parse2map(parser, map);
376 UCX_TEST_ASSERT(r == 0, "parse2map failed");
377 UCX_TEST_ASSERT(map->count == 3, "wrong number of properties");
379 char *v1 = (char*)ucx_map_cstr_get(map, "key1");
380 char *v2 = (char*)ucx_map_cstr_get(map, "key2");
381 char *v3 = (char*)ucx_map_cstr_get(map, "key3");
383 UCX_TEST_ASSERT(v1, "value for key1 not found");
384 UCX_TEST_ASSERT(v2, "value for key2 not found");
385 UCX_TEST_ASSERT(v3, "value for key3 not found");
387 UCX_TEST_ASSERT((!strcmp(v1, "value1")), "wrong value for key1");
388 UCX_TEST_ASSERT((!strcmp(v2, "value2")), "wrong value for key2");
389 UCX_TEST_ASSERT((!strcmp(v3, "value3")), "wrong value for key3");
391 // second test
392 ucx_map_free(map);
393 free(v1);
394 free(v2);
395 free(v3);
396 map = ucx_map_new(16);
398 str = "\n#comment\n";
399 ucx_prop_fill(parser, (char*)str, strlen(str));
401 r = ucx_prop_parse2map(parser, map);
402 UCX_TEST_ASSERT(r == 0, "parse2map failed");
403 UCX_TEST_ASSERT(map->count == 0, "wrong number of properties");
405 str = "key1 = value1\nsyntax error line\n";
406 ucx_prop_fill(parser, (char*)str, strlen(str));
408 r = ucx_prop_parse2map(parser, map);
409 UCX_TEST_ASSERT(r == 1, "parse2map should return 1");
410 UCX_TEST_ASSERT(map->count == 1, "wrong number of properties");
412 char *v = (char*)ucx_map_cstr_get(map, "key1");
413 UCX_TEST_ASSERT((!strcmp(v, "value1")), "wrong value");
415 UCX_TEST_END
417 ucx_prop_free(parser);
418 }
420 UCX_TEST_IMPLEMENT(test_ucx_properties_load) {
421 UCX_TEST_BEGIN
422 FILE *f = tmpfile();
423 UCX_TEST_ASSERT(f, "test file cannot be opened, test aborted");
425 fprintf(f, "# properties file\n\nkey1 = value1\nkey2 = value2\n");
426 fprintf(f, "\n\nkey3 = value3\n\n");
428 size_t name_len = 512;
429 char *long_name = (char*)malloc(name_len);
430 memset(long_name, 'k', 512);
432 size_t value_len = 2048;
433 char *long_value = (char*)malloc(value_len);
434 memset(long_value, 'v', 2048);
436 fwrite(long_name, 1, name_len, f);
437 fprintf(f, " = ");
438 fwrite(long_value, 1, value_len, f);
439 fprintf(f, " \n");
441 fprintf(f, "\n\n\n\nlast_key = property value\n");
443 fflush(f);
444 fseek(f, 0, SEEK_SET);
446 UcxMap *map = ucx_map_new(8);
447 int r = ucx_properties_load(map, f);
449 UCX_TEST_ASSERT(r == 0, "ucx_properties_load failed");
450 UCX_TEST_ASSERT(map->count == 5, "wrong number of properties");
452 char *v1 = (char*)ucx_map_cstr_get(map, "key1");
453 char *v2 = (char*)ucx_map_cstr_get(map, "key2");
454 char *v3 = (char*)ucx_map_cstr_get(map, "key3");
455 char *lv = (char*)ucx_map_sstr_get(map, sstrn(long_name, name_len));
456 char *lk = (char*)ucx_map_cstr_get(map, "last_key");
458 UCX_TEST_ASSERT(v1, "value for key1 not found");
459 UCX_TEST_ASSERT(v2, "value for key2 not found");
460 UCX_TEST_ASSERT(v3, "value for key3 not found");
461 UCX_TEST_ASSERT(lv, "value for long key not found");
462 UCX_TEST_ASSERT(lk, "value for last_key not found");
464 UCX_TEST_ASSERT((!strcmp(v1, "value1")), "wrong value for key1");
465 UCX_TEST_ASSERT((!strcmp(v2, "value2")), "wrong value for key2");
466 UCX_TEST_ASSERT((!strcmp(v3, "value3")), "wrong value for key3");
467 sstr_t long1 = sstrn(long_value, value_len);
468 sstr_t long2 = sstr(lv);
469 UCX_TEST_ASSERT((!sstrcmp(long1, long2)), "wrong value for long key");
470 UCX_TEST_ASSERT(!strcmp(lk, "property value"), "wrong value for last_key");
472 free(v1);
473 free(v2);
474 free(v3);
475 free(lv);
476 free(lk);
477 ucx_map_free(map);
478 fclose(f);
480 UCX_TEST_END
481 }
483 UCX_TEST_IMPLEMENT(test_ucx_properties_store) {
484 UcxMap *map1 = ucx_map_new(16);
485 ucx_map_cstr_put(map1, "key1", "value1");
486 ucx_map_cstr_put(map1, "key2", "value2");
487 ucx_map_cstr_put(map1, "key3", "value3");
488 ucx_map_cstr_put(map1, "key4", "value4");
489 ucx_map_cstr_put(map1, "property.key1", "some value 1");
490 ucx_map_cstr_put(map1, "property.key2", "some value 2");
491 ucx_map_cstr_put(map1, "property.key3", "some value 3");
492 ucx_map_cstr_put(map1, "property.key4", "some value 4");
494 UCX_TEST_BEGIN
496 FILE *f = tmpfile();
497 fprintf(f, "#\n# test property file\n#\n#\n");
498 ucx_properties_store(map1, f);
500 fflush(f);
501 fseek(f, 0, SEEK_SET);
502 UcxMap *map2 = ucx_map_new(16);
503 ucx_properties_load(map2, f);
505 UCX_TEST_ASSERT(map2->count == 8, "wrong number of properties in map2");
507 char *v1 = (char*)ucx_map_cstr_get(map2, "key1");
508 char *v2 = (char*)ucx_map_cstr_get(map2, "key2");
509 char *v3 = (char*)ucx_map_cstr_get(map2, "key3");
510 char *v4 = (char*)ucx_map_cstr_get(map2, "key4");
511 char *v5 = (char*)ucx_map_cstr_get(map2, "property.key1");
512 char *v6 = (char*)ucx_map_cstr_get(map2, "property.key2");
513 char *v7 = (char*)ucx_map_cstr_get(map2, "property.key3");
514 char *v8 = (char*)ucx_map_cstr_get(map2, "property.key4");
516 UCX_TEST_ASSERT(v1 && v2 && v3 && v4 && v5 && v6 && v7 && v8,
517 "missing values");
519 UCX_TEST_ASSERT((!strcmp(v1, "value1")), "wrong value 1");
520 UCX_TEST_ASSERT((!strcmp(v2, "value2")), "wrong value 2");
521 UCX_TEST_ASSERT((!strcmp(v3, "value3")), "wrong value 3");
522 UCX_TEST_ASSERT((!strcmp(v4, "value4")), "wrong value 4");
523 UCX_TEST_ASSERT((!strcmp(v5, "some value 1")), "wrong value 5");
524 UCX_TEST_ASSERT((!strcmp(v6, "some value 2")), "wrong value 6");
525 UCX_TEST_ASSERT((!strcmp(v7, "some value 3")), "wrong value 7");
526 UCX_TEST_ASSERT((!strcmp(v8, "some value 4")), "wrong value 8");
528 free(v1);
529 free(v2);
530 free(v3);
531 free(v4);
532 free(v5);
533 free(v6);
534 free(v7);
535 free(v8);
536 ucx_map_free(map2);
537 fclose(f);
539 UCX_TEST_END
541 ucx_map_free(map1);
542 }