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