fix invalid free
[protos/libecoli.git] / lib / ecoli_config.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2018, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #include <sys/queue.h>
6 #include <stddef.h>
7 #include <stdio.h>
8 #include <errno.h>
9 #include <inttypes.h>
10
11 #include <ecoli_string.h>
12 #include <ecoli_malloc.h>
13 #include <ecoli_keyval.h>
14 #include <ecoli_node.h>
15 #include <ecoli_log.h>
16 #include <ecoli_test.h>
17 #include <ecoli_config.h>
18
19 EC_LOG_TYPE_REGISTER(config);
20
21 static int
22 __ec_config_dump(FILE *out, const char *key, const struct ec_config *config,
23         size_t indent);
24 static int
25 ec_config_dict_validate(const struct ec_keyval *dict,
26                         const struct ec_config_schema *schema,
27                         size_t schema_len);
28
29 /* return ec_value type as a string */
30 static const char *
31 ec_config_type_str(enum ec_config_type type)
32 {
33         switch (type) {
34         case EC_CONFIG_TYPE_BOOL: return "bool";
35         case EC_CONFIG_TYPE_INT64: return "int64";
36         case EC_CONFIG_TYPE_UINT64: return "uint64";
37         case EC_CONFIG_TYPE_STRING: return "string";
38         case EC_CONFIG_TYPE_NODE: return "node";
39         case EC_CONFIG_TYPE_LIST: return "list";
40         case EC_CONFIG_TYPE_DICT: return "dict";
41         default: return "unknown";
42         }
43 }
44
45 static int
46 __ec_config_schema_validate(const struct ec_config_schema *schema,
47                         size_t schema_len, enum ec_config_type type)
48 {
49         size_t i, j;
50         int ret;
51
52         if (type == EC_CONFIG_TYPE_LIST) {
53                 if (schema[0].key != NULL) {
54                         errno = EINVAL;
55                         EC_LOG(EC_LOG_ERR, "list schema key must be NULL\n");
56                         return -1;
57                 }
58         } else if (type == EC_CONFIG_TYPE_DICT) {
59                 for (i = 0; i < schema_len; i++) {
60                         if (schema[i].key == NULL) {
61                                 errno = EINVAL;
62                                 EC_LOG(EC_LOG_ERR,
63                                         "dict schema key should not be NULL\n");
64                                 return -1;
65                         }
66                 }
67         } else {
68                 errno = EINVAL;
69                 EC_LOG(EC_LOG_ERR, "invalid schema type\n");
70                 return -1;
71         }
72
73         for (i = 0; i < schema_len; i++) {
74                 /* check for duplicate name if more than one element */
75                 for (j = i + 1; j < schema_len; j++) {
76                         if (!strcmp(schema[i].key, schema[j].key)) {
77                                 errno = EEXIST;
78                                 EC_LOG(EC_LOG_ERR,
79                                         "duplicate key <%s> in schema\n",
80                                         schema[i].key);
81                                 return -1;
82                         }
83                 }
84
85                 switch (schema[i].type) {
86                 case EC_CONFIG_TYPE_BOOL:
87                 case EC_CONFIG_TYPE_INT64:
88                 case EC_CONFIG_TYPE_UINT64:
89                 case EC_CONFIG_TYPE_STRING:
90                 case EC_CONFIG_TYPE_NODE:
91                         if (schema[i].subschema != NULL ||
92                                         schema[i].subschema_len != 0) {
93                                 errno = EINVAL;
94                                 EC_LOG(EC_LOG_ERR,
95                                         "key <%s> should not have subtype/subschema\n",
96                                         schema[i].key);
97                                 return -1;
98                         }
99                         break;
100                 case EC_CONFIG_TYPE_LIST:
101                         if (schema[i].subschema == NULL ||
102                                         schema[i].subschema_len != 1) {
103                                 errno = EINVAL;
104                                 EC_LOG(EC_LOG_ERR,
105                                         "key <%s> must have subschema of length 1\n",
106                                         schema[i].key);
107                                 return -1;
108                         }
109                         break;
110                 case EC_CONFIG_TYPE_DICT:
111                         if (schema[i].subschema == NULL ||
112                                         schema[i].subschema_len == 0) {
113                                 errno = EINVAL;
114                                 EC_LOG(EC_LOG_ERR,
115                                         "key <%s> must have subschema\n",
116                                         schema[i].key);
117                                 return -1;
118                         }
119                         break;
120                 default:
121                         EC_LOG(EC_LOG_ERR, "invalid type for key <%s>\n",
122                                 schema[i].key);
123                         errno = EINVAL;
124                         return -1;
125                 }
126
127                 if (schema[i].subschema == NULL)
128                         continue;
129
130                 ret = __ec_config_schema_validate(schema[i].subschema,
131                                                 schema[i].subschema_len,
132                                                 schema[i].type);
133                 if (ret < 0) {
134                         EC_LOG(EC_LOG_ERR, "cannot parse subschema %s%s\n",
135                                 schema[i].key ? "key=" : "",
136                                 schema[i].key ? : "");
137                         return ret;
138                 }
139         }
140
141         return 0;
142 }
143
144 int
145 ec_config_schema_validate(const struct ec_config_schema *schema,
146                         size_t schema_len)
147 {
148         return __ec_config_schema_validate(schema, schema_len,
149                                         EC_CONFIG_TYPE_DICT);
150 }
151
152 static void
153 __ec_config_schema_dump(FILE *out, const struct ec_config_schema *schema,
154                         size_t schema_len, size_t indent)
155 {
156         size_t i;
157
158         for (i = 0; i < schema_len; i++) {
159                 fprintf(out, "%*s" "%s%s%stype=%s desc='%s'\n",
160                         (int)indent * 4, "",
161                         schema[i].key ? "key=": "",
162                         schema[i].key ? : "",
163                         schema[i].key ? " ": "",
164                         ec_config_type_str(schema[i].type),
165                         schema[i].desc);
166                 if (schema[i].subschema == NULL)
167                         continue;
168                 __ec_config_schema_dump(out, schema[i].subschema,
169                                         schema[i].subschema_len, indent + 1);
170         }
171 }
172
173 void
174 ec_config_schema_dump(FILE *out, const struct ec_config_schema *schema,
175                 size_t schema_len)
176 {
177         fprintf(out, "------------------- schema dump:\n");
178
179         if (schema == NULL || schema_len == 0) {
180                 fprintf(out, "no schema\n");
181                 return;
182         }
183
184         __ec_config_schema_dump(out, schema, schema_len, 0);
185 }
186
187 struct ec_config *
188 ec_config_bool(bool boolean)
189 {
190         struct ec_config *value = NULL;
191
192         value = ec_calloc(1, sizeof(*value));
193         if (value == NULL)
194                 return NULL;
195
196         value->type = EC_CONFIG_TYPE_BOOL;
197         value->boolean = boolean;
198
199         return value;
200 }
201
202 struct ec_config *
203 ec_config_i64(int64_t i64)
204 {
205         struct ec_config *value = NULL;
206
207         value = ec_calloc(1, sizeof(*value));
208         if (value == NULL)
209                 return NULL;
210
211         value->type = EC_CONFIG_TYPE_INT64;
212         value->i64 = i64;
213
214         return value;
215 }
216
217 struct ec_config *
218 ec_config_u64(uint64_t u64)
219 {
220         struct ec_config *value = NULL;
221
222         value = ec_calloc(1, sizeof(*value));
223         if (value == NULL)
224                 return NULL;
225
226         value->type = EC_CONFIG_TYPE_UINT64;
227         value->u64 = u64;
228
229         return value;
230 }
231
232 /* duplicate string */
233 struct ec_config *
234 ec_config_string(const char *string)
235 {
236         struct ec_config *value = NULL;
237         char *s = NULL;
238
239         if (string == NULL)
240                 goto fail;
241
242         s = ec_strdup(string);
243         if (s == NULL)
244                 goto fail;
245
246         value = ec_calloc(1, sizeof(*value));
247         if (value == NULL)
248                 goto fail;
249
250         value->type = EC_CONFIG_TYPE_STRING;
251         value->string = s;
252
253         return value;
254
255 fail:
256         ec_free(value);
257         ec_free(s);
258         return NULL;
259 }
260
261 /* "consume" the node */
262 struct ec_config *
263 ec_config_node(struct ec_node *node)
264 {
265         struct ec_config *value = NULL;
266
267         if (node == NULL)
268                 goto fail;
269
270         value = ec_calloc(1, sizeof(*value));
271         if (value == NULL)
272                 goto fail;
273
274         value->type = EC_CONFIG_TYPE_NODE;
275         value->node = node;
276
277         return value;
278
279 fail:
280         ec_node_free(node);
281         ec_free(value);
282         return NULL;
283 }
284
285 struct ec_config *
286 ec_config_dict(void)
287 {
288         struct ec_config *value = NULL;
289         struct ec_keyval *dict = NULL;
290
291         dict = ec_keyval();
292         if (dict == NULL)
293                 goto fail;
294
295         value = ec_calloc(1, sizeof(*value));
296         if (value == NULL)
297                 goto fail;
298
299         value->type = EC_CONFIG_TYPE_DICT;
300         value->dict = dict;
301
302         return value;
303
304 fail:
305         ec_keyval_free(dict);
306         ec_free(value);
307         return NULL;
308 }
309
310 struct ec_config *
311 ec_config_list(void)
312 {
313         struct ec_config *value = NULL;
314
315         value = ec_calloc(1, sizeof(*value));
316         if (value == NULL)
317                 return NULL;
318
319         value->type = EC_CONFIG_TYPE_LIST;
320         TAILQ_INIT(&value->list);
321
322         return value;
323 }
324
325 static const struct ec_config_schema *
326 ec_config_schema_lookup(const struct ec_config_schema *schema,
327                         size_t schema_len, const char *key,
328                         enum ec_config_type type)
329 {
330         size_t i;
331
332         for (i = 0; i < schema_len; i++) {
333                 if (!strcmp(key, schema[i].key) &&
334                                 type == schema[i].type)
335                         return &schema[i];
336         }
337
338         errno = ENOENT;
339         return NULL;
340 }
341
342 void
343 ec_config_free(struct ec_config *value)
344 {
345         if (value == NULL)
346                 return;
347
348         switch (value->type) {
349         case EC_CONFIG_TYPE_STRING:
350                 ec_free(value->string);
351                 break;
352         case EC_CONFIG_TYPE_NODE:
353                 ec_node_free(value->node);
354                 break;
355         case EC_CONFIG_TYPE_LIST:
356                 while (!TAILQ_EMPTY(&value->list)) {
357                         struct ec_config *v;
358                         v = TAILQ_FIRST(&value->list);
359                         TAILQ_REMOVE(&value->list, v, next);
360                         ec_config_free(v);
361                 }
362                 break;
363         case EC_CONFIG_TYPE_DICT:
364                 ec_keyval_free(value->dict);
365                 break;
366         default:
367                 break;
368         }
369
370         ec_free(value);
371 }
372
373 static int
374 ec_config_list_cmp(const struct ec_config_list *list1,
375                 const struct ec_config_list *list2)
376 {
377         const struct ec_config *v1, *v2;
378
379         for (v1 = TAILQ_FIRST(list1), v2 = TAILQ_FIRST(list2);
380              v1 != NULL && v2 != NULL;
381              v1 = TAILQ_NEXT(v1, next), v2 = TAILQ_NEXT(v2, next)) {
382                 if (ec_config_cmp(v1, v2))
383                         return -1;
384         }
385         if (v1 != NULL || v2 != NULL)
386                 return -1;
387
388         return 0;
389 }
390
391 /* XXX -> ec_keyval_cmp() */
392 static int
393 ec_config_dict_cmp(const struct ec_keyval *d1,
394                 const struct ec_keyval *d2)
395 {
396         const struct ec_config *v1, *v2;
397         struct ec_keyval_iter *iter = NULL;
398         const char *key;
399
400         if (ec_keyval_len(d1) != ec_keyval_len(d2))
401                 return -1;
402
403         for (iter = ec_keyval_iter(d1);
404              ec_keyval_iter_valid(iter);
405              ec_keyval_iter_next(iter)) {
406                 key = ec_keyval_iter_get_key(iter);
407                 v1 = ec_keyval_iter_get_val(iter);
408                 v2 = ec_keyval_get(d2, key);
409
410                 if (ec_config_cmp(v1, v2))
411                         goto fail;
412         }
413
414         ec_keyval_iter_free(iter);
415         return 0;
416
417 fail:
418         ec_keyval_iter_free(iter);
419         return -1;
420 }
421
422 int
423 ec_config_cmp(const struct ec_config *value1,
424                 const struct ec_config *value2)
425 {
426         if (value1 == NULL || value2 == NULL) {
427                 errno = EINVAL;
428                 return -1;
429         }
430
431         if (value1->type != value2->type)
432                 return -1;
433
434         switch (value1->type) {
435         case EC_CONFIG_TYPE_BOOL:
436                 if (value1->boolean == value2->boolean)
437                         return 0;
438         case EC_CONFIG_TYPE_INT64:
439                 if (value1->i64 == value2->i64)
440                         return 0;
441         case EC_CONFIG_TYPE_UINT64:
442                 if (value1->u64 == value2->u64)
443                         return 0;
444         case EC_CONFIG_TYPE_STRING:
445                 if (!strcmp(value1->string, value2->string))
446                         return 0;
447         case EC_CONFIG_TYPE_NODE:
448                 if (value1->node == value2->node)
449                         return 0;
450         case EC_CONFIG_TYPE_LIST:
451                 return ec_config_list_cmp(&value1->list, &value2->list);
452         case EC_CONFIG_TYPE_DICT:
453                 return ec_config_dict_cmp(value1->dict, value2->dict);
454         default:
455                 break;
456         }
457
458         return -1;
459 }
460
461 static int
462 ec_config_list_validate(const struct ec_config_list *list,
463                         const struct ec_config_schema *sch)
464 {
465         const struct ec_config *value;
466
467         TAILQ_FOREACH(value, list, next) {
468                 if (value->type != sch->type) {
469                         errno = EBADMSG;
470                         return -1;
471                 }
472
473                 if (value->type == EC_CONFIG_TYPE_LIST) {
474                         if (ec_config_list_validate(&value->list,
475                                                         sch->subschema) < 0)
476                                 return -1;
477                 } else if (value->type == EC_CONFIG_TYPE_DICT) {
478                         if (ec_config_dict_validate(value->dict,
479                                         sch->subschema, sch->subschema_len) < 0)
480                                 return -1;
481                 }
482         }
483
484         return 0;
485 }
486
487 static int
488 ec_config_dict_validate(const struct ec_keyval *dict,
489                         const struct ec_config_schema *schema,
490                         size_t schema_len)
491 {
492         const struct ec_config *value;
493         struct ec_keyval_iter *iter = NULL;
494         const struct ec_config_schema *sch;
495         const char *key;
496
497         for (iter = ec_keyval_iter(dict);
498              ec_keyval_iter_valid(iter);
499              ec_keyval_iter_next(iter)) {
500
501                 key = ec_keyval_iter_get_key(iter);
502                 value = ec_keyval_iter_get_val(iter);
503                 sch = ec_config_schema_lookup(schema, schema_len,
504                                         key, value->type);
505                 if (sch == NULL) {
506                         errno = EBADMSG;
507                         goto fail;
508                 }
509
510                 if (value->type == EC_CONFIG_TYPE_LIST) {
511                         if (ec_config_list_validate(&value->list,
512                                                         sch->subschema) < 0)
513                                 goto fail;
514                 } else if (value->type == EC_CONFIG_TYPE_DICT) {
515                         if (ec_config_dict_validate(value->dict,
516                                         sch->subschema, sch->subschema_len) < 0)
517                                 goto fail;
518                 }
519         }
520
521         ec_keyval_iter_free(iter);
522         return 0;
523
524 fail:
525         ec_keyval_iter_free(iter);
526         return -1;
527 }
528
529 int
530 ec_config_validate(const struct ec_config *dict,
531                 const struct ec_config_schema *schema,
532                 size_t schema_len)
533 {
534         if (dict->type != EC_CONFIG_TYPE_DICT || schema == NULL) {
535                 errno = EINVAL;
536                 goto fail;
537         }
538
539         if (ec_config_dict_validate(dict->dict, schema, schema_len) < 0)
540                 goto fail;
541
542         return 0
543 ;
544 fail:
545         return -1;
546 }
547
548 struct ec_config *
549 ec_config_dict_get(const struct ec_config *config, const char *key)
550 {
551         if (config == NULL) {
552                 errno = EINVAL;
553                 return NULL;
554         }
555
556         if (config->type != EC_CONFIG_TYPE_DICT) {
557                 errno = EINVAL;
558                 return NULL;
559         }
560
561         return ec_keyval_get(config->dict, key);
562 }
563
564 struct ec_config *
565 ec_config_list_first(struct ec_config *list)
566 {
567         if (list  == NULL || list->type != EC_CONFIG_TYPE_LIST) {
568                 errno = EINVAL;
569                 return NULL;
570         }
571
572         return TAILQ_FIRST(&list->list);
573 }
574
575 struct ec_config *
576 ec_config_list_next(struct ec_config *list, struct ec_config *config)
577 {
578         (void)list;
579         return TAILQ_NEXT(config, next);
580 }
581
582 /* value is consumed */
583 int ec_config_dict_set(struct ec_config *config, const char *key,
584                 struct ec_config *value)
585 {
586         void (*free_cb)(struct ec_config *) = ec_config_free;
587
588         if (config == NULL || key == NULL || value == NULL) {
589                 errno = EINVAL;
590                 goto fail;
591         }
592         if (config->type != EC_CONFIG_TYPE_DICT) {
593                 errno = EINVAL;
594                 goto fail;
595         }
596
597         return ec_keyval_set(config->dict, key, value,
598                         (void (*)(void *))free_cb);
599
600 fail:
601         ec_config_free(value);
602         return -1;
603 }
604
605 int ec_config_dict_del(struct ec_config *config, const char *key)
606 {
607         if (config == NULL || key == NULL) {
608                 errno = EINVAL;
609                 return -1;
610         }
611         if (config->type != EC_CONFIG_TYPE_DICT) {
612                 errno = EINVAL;
613                 return -1;
614         }
615
616         return ec_keyval_del(config->dict, key);
617 }
618
619 /* value is consumed */
620 int
621 ec_config_list_add(struct ec_config *list,
622                 struct ec_config *value)
623 {
624         if (list == NULL || list->type != EC_CONFIG_TYPE_LIST) {
625                 errno = EINVAL;
626                 goto fail;
627         }
628
629         TAILQ_INSERT_TAIL(&list->list, value, next);
630
631         return 0;
632
633 fail:
634         ec_config_free(value);
635         return -1;
636 }
637
638 int ec_config_list_del(struct ec_config *list, struct ec_config *config)
639 {
640         if (list == NULL || list->type != EC_CONFIG_TYPE_LIST) {
641                 errno = EINVAL;
642                 return -1;
643         }
644
645         TAILQ_REMOVE(&list->list, config, next);
646         ec_config_free(config);
647         return 0;
648 }
649
650 static struct ec_config *
651 ec_config_list_dup(const struct ec_config_list *list)
652 {
653         struct ec_config *dup = NULL, *v, *value;
654
655         dup = ec_config_list();
656         if (dup == NULL)
657                 goto fail;
658
659         TAILQ_FOREACH(v, list, next) {
660                 value = ec_config_dup(v);
661                 if (value == NULL)
662                         goto fail;
663                 if (ec_config_list_add(dup, value) < 0)
664                         goto fail;
665         }
666
667         return dup;
668
669 fail:
670         ec_config_free(dup);
671         return NULL;
672 }
673
674 static struct ec_config *
675 ec_config_dict_dup(const struct ec_keyval *dict)
676 {
677         struct ec_config *dup = NULL, *value;
678         struct ec_keyval_iter *iter = NULL;
679         const char *key;
680
681         dup = ec_config_dict();
682         if (dup == NULL)
683                 goto fail;
684
685         for (iter = ec_keyval_iter(dict);
686              ec_keyval_iter_valid(iter);
687              ec_keyval_iter_next(iter)) {
688                 key = ec_keyval_iter_get_key(iter);
689                 value = ec_config_dup(ec_keyval_iter_get_val(iter));
690                 if (value == NULL)
691                         goto fail;
692                 if (ec_config_dict_set(dup, key, value) < 0)
693                         goto fail;
694         }
695         ec_keyval_iter_free(iter);
696
697         return dup;
698
699 fail:
700         ec_config_free(dup);
701         ec_keyval_iter_free(iter);
702         return NULL;
703 }
704
705 struct ec_config *
706 ec_config_dup(const struct ec_config *config)
707 {
708         if (config == NULL) {
709                 errno = EINVAL;
710                 return NULL;
711         }
712
713         switch (config->type) {
714         case EC_CONFIG_TYPE_BOOL:
715                 return ec_config_bool(config->boolean);
716         case EC_CONFIG_TYPE_INT64:
717                 return ec_config_i64(config->i64);
718         case EC_CONFIG_TYPE_UINT64:
719                 return ec_config_u64(config->u64);
720         case EC_CONFIG_TYPE_STRING:
721                 return ec_config_string(config->string);
722         case EC_CONFIG_TYPE_NODE:
723                 return ec_config_node(ec_node_clone(config->node));
724         case EC_CONFIG_TYPE_LIST:
725                 return ec_config_list_dup(&config->list);
726         case EC_CONFIG_TYPE_DICT:
727                 return ec_config_dict_dup(config->dict);
728         default:
729                 errno = EINVAL;
730                 break;
731         }
732
733         return NULL;
734 }
735
736 static int
737 ec_config_list_dump(FILE *out, const struct ec_config_list *list,
738                 size_t indent)
739 {
740         const struct ec_config *v;
741
742         fprintf(out, "%*s" "type=list:\n", (int)indent * 4, "");
743
744         TAILQ_FOREACH(v, list, next) {
745                 if (__ec_config_dump(out, NULL, v, indent + 1) < 0)
746                         return -1;
747         }
748
749         return 0;
750 }
751
752 static int
753 ec_config_dict_dump(FILE *out, const struct ec_keyval *dict,
754                 size_t indent)
755 {
756         const struct ec_config *value;
757         struct ec_keyval_iter *iter;
758         const char *key;
759
760         fprintf(out, "%*s" "type=dict:\n", (int)indent * 4, "");
761         for (iter = ec_keyval_iter(dict);
762              ec_keyval_iter_valid(iter);
763              ec_keyval_iter_next(iter)) {
764                 key = ec_keyval_iter_get_key(iter);
765                 value = ec_keyval_iter_get_val(iter);
766                 if (__ec_config_dump(out, key, value, indent + 1) < 0)
767                         goto fail;
768         }
769         ec_keyval_iter_free(iter);
770         return 0;
771
772 fail:
773         ec_keyval_iter_free(iter);
774         return -1;
775 }
776
777 static int
778 __ec_config_dump(FILE *out, const char *key, const struct ec_config *value,
779                 size_t indent)
780 {
781         char *val_str = NULL;
782
783         switch (value->type) {
784         case EC_CONFIG_TYPE_BOOL:
785                 if (value->boolean)
786                         ec_asprintf(&val_str, "true");
787                 else
788                         ec_asprintf(&val_str, "false");
789                 break;
790         case EC_CONFIG_TYPE_INT64:
791                 ec_asprintf(&val_str, "%"PRIu64, value->u64);
792                 break;
793         case EC_CONFIG_TYPE_UINT64:
794                 ec_asprintf(&val_str, "%"PRIi64, value->i64);
795                 break;
796         case EC_CONFIG_TYPE_STRING:
797                 ec_asprintf(&val_str, "%s", value->string);
798                 break;
799         case EC_CONFIG_TYPE_NODE:
800                 ec_asprintf(&val_str, "%p", value->node);
801                 break;
802         case EC_CONFIG_TYPE_LIST:
803                 return ec_config_list_dump(out, &value->list, indent);
804         case EC_CONFIG_TYPE_DICT:
805                 return ec_config_dict_dump(out, value->dict, indent);
806         default:
807                 errno = EINVAL;
808                 break;
809         }
810
811         /* errno is already set on error */
812         if (val_str == NULL)
813                 goto fail;
814
815         fprintf(out, "%*s" "%s%s%stype=%s val=%s\n", (int)indent * 4, "",
816                 key ? "key=": "",
817                 key ? key: "",
818                 key ? " ": "",
819                 ec_config_type_str(value->type), val_str);
820
821         ec_free(val_str);
822         return 0;
823
824 fail:
825         ec_free(val_str);
826         return -1;
827 }
828
829 void
830 ec_config_dump(FILE *out, const struct ec_config *config)
831 {
832         fprintf(out, "------------------- config dump:\n");
833
834         if (config == NULL) {
835                 fprintf(out, "no config\n");
836                 return;
837         }
838
839         if (__ec_config_dump(out, NULL, config, 0) < 0)
840                 fprintf(out, "error while dumping\n");
841 }
842
843 /* LCOV_EXCL_START */
844 static const struct ec_config_schema sch_intlist_elt[] = {
845         {
846                 .desc = "This is a description for int",
847                 .type = EC_CONFIG_TYPE_INT64,
848         },
849 };
850
851 static const struct ec_config_schema sch_dict[] = {
852         {
853                 .key = "my_int",
854                 .desc = "This is a description for int",
855                 .type = EC_CONFIG_TYPE_INT64,
856         },
857         {
858                 .key = "my_int2",
859                 .desc = "This is a description for int2",
860                 .type = EC_CONFIG_TYPE_INT64,
861         },
862 };
863
864 static const struct ec_config_schema sch_dictlist_elt[] = {
865         {
866                 .desc = "This is a description for dict",
867                 .type = EC_CONFIG_TYPE_DICT,
868                 .subschema = sch_dict,
869                 .subschema_len = EC_COUNT_OF(sch_dict),
870         },
871 };
872
873 static const struct ec_config_schema sch_baseconfig[] = {
874         {
875                 .key = "my_bool",
876                 .desc = "This is a description for bool",
877                 .type = EC_CONFIG_TYPE_BOOL,
878         },
879         {
880                 .key = "my_int",
881                 .desc = "This is a description for int",
882                 .type = EC_CONFIG_TYPE_INT64,
883         },
884         {
885                 .key = "my_string",
886                 .desc = "This is a description for string",
887                 .type = EC_CONFIG_TYPE_STRING,
888         },
889         {
890                 .key = "my_node",
891                 .desc = "This is a description for node",
892                 .type = EC_CONFIG_TYPE_NODE,
893         },
894         {
895                 .key = "my_intlist",
896                 .desc = "This is a description for list",
897                 .type = EC_CONFIG_TYPE_LIST,
898                 .subschema = sch_intlist_elt,
899                 .subschema_len = EC_COUNT_OF(sch_intlist_elt),
900         },
901         {
902                 .key = "my_dictlist",
903                 .desc = "This is a description for list",
904                 .type = EC_CONFIG_TYPE_LIST,
905                 .subschema = sch_dictlist_elt,
906                 .subschema_len = EC_COUNT_OF(sch_dictlist_elt),
907         },
908 };
909
910 static int ec_config_testcase(void)
911 {
912         struct ec_node *node = NULL;
913         struct ec_keyval *dict = NULL;
914         const struct ec_config *value = NULL;
915         struct ec_config *config = NULL, *config2 = NULL;
916         struct ec_config *list = NULL, *subconfig = NULL;
917         struct ec_config *list_, *config_;
918         int testres = 0;
919         int ret;
920
921         node = ec_node("empty", EC_NO_ID);
922         if (node == NULL)
923                 goto fail;
924
925         if (ec_config_schema_validate(sch_baseconfig,
926                                         EC_COUNT_OF(sch_baseconfig)) < 0) {
927                 EC_LOG(EC_LOG_ERR, "invalid config schema\n");
928                 goto fail;
929         }
930
931         ec_config_schema_dump(stdout, sch_baseconfig,
932                         EC_COUNT_OF(sch_baseconfig));
933
934         config = ec_config_dict();
935         if (config == NULL)
936                 goto fail;
937
938         ret = ec_config_dict_set(config, "my_bool", ec_config_bool(true));
939         testres |= EC_TEST_CHECK(ret == 0, "cannot set boolean");
940         value = ec_config_dict_get(config, "my_bool");
941         testres |= EC_TEST_CHECK(
942                 value != NULL &&
943                 value->type == EC_CONFIG_TYPE_BOOL &&
944                 value->boolean == true,
945                 "unexpected boolean value");
946
947         ret = ec_config_dict_set(config, "my_int", ec_config_i64(1234));
948         testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
949         value = ec_config_dict_get(config, "my_int");
950         testres |= EC_TEST_CHECK(
951                 value != NULL &&
952                 value->type == EC_CONFIG_TYPE_INT64 &&
953                 value->i64 == 1234,
954                 "unexpected int value");
955
956         testres |= EC_TEST_CHECK(
957                 ec_config_validate(config, sch_baseconfig,
958                                 EC_COUNT_OF(sch_baseconfig)) == 0,
959                 "cannot validate config\n");
960
961         ret = ec_config_dict_set(config, "my_string", ec_config_string("toto"));
962         testres |= EC_TEST_CHECK(ret == 0, "cannot set string");
963         value = ec_config_dict_get(config, "my_string");
964         testres |= EC_TEST_CHECK(
965                 value != NULL &&
966                 value->type == EC_CONFIG_TYPE_STRING &&
967                 !strcmp(value->string, "toto"),
968                 "unexpected string value");
969
970         list = ec_config_list();
971         if (list == NULL)
972                 goto fail;
973
974         subconfig = ec_config_dict();
975         if (subconfig == NULL)
976                 goto fail;
977
978         ret = ec_config_dict_set(subconfig, "my_int", ec_config_i64(1));
979         testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
980         value = ec_config_dict_get(subconfig, "my_int");
981         testres |= EC_TEST_CHECK(
982                 value != NULL &&
983                 value->type == EC_CONFIG_TYPE_INT64 &&
984                 value->i64 == 1,
985                 "unexpected int value");
986
987         ret = ec_config_dict_set(subconfig, "my_int2", ec_config_i64(2));
988         testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
989         value = ec_config_dict_get(subconfig, "my_int2");
990         testres |= EC_TEST_CHECK(
991                 value != NULL &&
992                 value->type == EC_CONFIG_TYPE_INT64 &&
993                 value->i64 == 2,
994                 "unexpected int value");
995
996         testres |= EC_TEST_CHECK(
997                 ec_config_validate(subconfig, sch_dict,
998                                 EC_COUNT_OF(sch_dict)) == 0,
999                 "cannot validate subconfig\n");
1000
1001         ret = ec_config_list_add(list, subconfig);
1002         subconfig = NULL; /* freed */
1003         testres |= EC_TEST_CHECK(ret == 0, "cannot add in list");
1004
1005         subconfig = ec_config_dict();
1006         if (subconfig == NULL)
1007                 goto fail;
1008
1009         ret = ec_config_dict_set(subconfig, "my_int", ec_config_i64(3));
1010         testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
1011         value = ec_config_dict_get(subconfig, "my_int");
1012         testres |= EC_TEST_CHECK(
1013                 value != NULL &&
1014                 value->type == EC_CONFIG_TYPE_INT64 &&
1015                 value->i64 == 3,
1016                 "unexpected int value");
1017
1018         ret = ec_config_dict_set(subconfig, "my_int2", ec_config_i64(4));
1019         testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
1020         value = ec_config_dict_get(subconfig, "my_int2");
1021         testres |= EC_TEST_CHECK(
1022                 value != NULL &&
1023                 value->type == EC_CONFIG_TYPE_INT64 &&
1024                 value->i64 == 4,
1025                 "unexpected int value");
1026
1027         testres |= EC_TEST_CHECK(
1028                 ec_config_validate(subconfig, sch_dict,
1029                                 EC_COUNT_OF(sch_dict)) == 0,
1030                 "cannot validate subconfig\n");
1031
1032         ret = ec_config_list_add(list, subconfig);
1033         subconfig = NULL; /* freed */
1034         testres |= EC_TEST_CHECK(ret == 0, "cannot add in list");
1035
1036         ret = ec_config_dict_set(config, "my_dictlist", list);
1037         list = NULL;
1038         testres |= EC_TEST_CHECK(ret == 0, "cannot set list");
1039
1040         testres |= EC_TEST_CHECK(
1041                 ec_config_validate(config, sch_baseconfig,
1042                                 EC_COUNT_OF(sch_baseconfig)) == 0,
1043                 "cannot validate config\n");
1044
1045         list_ = ec_config_dict_get(config, "my_dictlist");
1046         for (config_ = ec_config_list_first(list_); config_ != NULL;
1047              config_ = ec_config_list_next(list_, config_)) {
1048                 ec_config_dump(stdout, config_);
1049         }
1050
1051         ec_config_dump(stdout, config);
1052
1053         config2 = ec_config_dup(config);
1054         testres |= EC_TEST_CHECK(config2 != NULL, "cannot duplicate config");
1055         testres |= EC_TEST_CHECK(
1056                 ec_config_cmp(config, config2) == 0,
1057                 "fail to compare config");
1058         ec_config_free(config2);
1059         config2 = NULL;
1060
1061         /* remove the first element */
1062         ec_config_list_del(list_, ec_config_list_first(list_));
1063         testres |= EC_TEST_CHECK(
1064                 ec_config_validate(config, sch_baseconfig,
1065                                 EC_COUNT_OF(sch_baseconfig)) == 0,
1066                 "cannot validate config\n");
1067
1068         ec_config_dump(stdout, config);
1069
1070         ec_config_free(list);
1071         ec_config_free(subconfig);
1072         ec_config_free(config);
1073         ec_keyval_free(dict);
1074         ec_node_free(node);
1075
1076         return testres;
1077
1078 fail:
1079         ec_config_free(list);
1080         ec_config_free(subconfig);
1081         ec_config_free(config);
1082         ec_config_free(config2);
1083         ec_keyval_free(dict);
1084         ec_node_free(node);
1085
1086         return -1;
1087 }
1088 /* LCOV_EXCL_STOP */
1089
1090 static struct ec_test ec_config_test = {
1091         .name = "config",
1092         .test = ec_config_testcase,
1093 };
1094
1095 EC_TEST_REGISTER(ec_config_test);