1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2018, Olivier MATZ <zer0@droids-corp.org>
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>
19 EC_LOG_TYPE_REGISTER(config);
22 __ec_config_dump(FILE *out, const char *key, const struct ec_config *config,
25 ec_config_dict_validate(const struct ec_keyval *dict,
26 const struct ec_config_schema *schema,
29 /* return ec_value type as a string */
31 ec_config_type_str(enum ec_config_type 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";
46 __ec_config_schema_validate(const struct ec_config_schema *schema,
47 size_t schema_len, enum ec_config_type type)
52 if (type == EC_CONFIG_TYPE_LIST) {
53 if (schema[0].key != NULL) {
55 EC_LOG(EC_LOG_ERR, "list schema key must be NULL\n");
58 } else if (type == EC_CONFIG_TYPE_DICT) {
59 for (i = 0; i < schema_len; i++) {
60 if (schema[i].key == NULL) {
63 "dict schema key should not be NULL\n");
69 EC_LOG(EC_LOG_ERR, "invalid schema type\n");
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)) {
79 "duplicate key <%s> in schema\n",
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) {
95 "key <%s> should not have subtype/subschema\n",
100 case EC_CONFIG_TYPE_LIST:
101 if (schema[i].subschema == NULL ||
102 schema[i].subschema_len != 1) {
105 "key <%s> must have subschema of length 1\n",
110 case EC_CONFIG_TYPE_DICT:
111 if (schema[i].subschema == NULL ||
112 schema[i].subschema_len == 0) {
115 "key <%s> must have subschema\n",
121 EC_LOG(EC_LOG_ERR, "invalid type for key <%s>\n",
127 if (schema[i].subschema == NULL)
130 ret = __ec_config_schema_validate(schema[i].subschema,
131 schema[i].subschema_len,
134 EC_LOG(EC_LOG_ERR, "cannot parse subschema %s%s\n",
135 schema[i].key ? "key=" : "",
136 schema[i].key ? : "");
145 ec_config_schema_validate(const struct ec_config_schema *schema,
148 return __ec_config_schema_validate(schema, schema_len,
149 EC_CONFIG_TYPE_DICT);
153 __ec_config_schema_dump(FILE *out, const struct ec_config_schema *schema,
154 size_t schema_len, size_t indent)
158 for (i = 0; i < schema_len; i++) {
159 fprintf(out, "%*s" "%s%s%stype=%s desc='%s'\n",
161 schema[i].key ? "key=": "",
162 schema[i].key ? : "",
163 schema[i].key ? " ": "",
164 ec_config_type_str(schema[i].type),
166 if (schema[i].subschema == NULL)
168 __ec_config_schema_dump(out, schema[i].subschema,
169 schema[i].subschema_len, indent + 1);
174 ec_config_schema_dump(FILE *out, const struct ec_config_schema *schema,
177 fprintf(out, "------------------- schema dump:\n");
179 if (schema == NULL || schema_len == 0) {
180 fprintf(out, "no schema\n");
184 __ec_config_schema_dump(out, schema, schema_len, 0);
188 ec_config_bool(bool boolean)
190 struct ec_config *value = NULL;
192 value = ec_calloc(1, sizeof(*value));
196 value->type = EC_CONFIG_TYPE_BOOL;
197 value->boolean = boolean;
203 ec_config_i64(int64_t i64)
205 struct ec_config *value = NULL;
207 value = ec_calloc(1, sizeof(*value));
211 value->type = EC_CONFIG_TYPE_INT64;
218 ec_config_u64(uint64_t u64)
220 struct ec_config *value = NULL;
222 value = ec_calloc(1, sizeof(*value));
226 value->type = EC_CONFIG_TYPE_UINT64;
232 /* duplicate string */
234 ec_config_string(const char *string)
236 struct ec_config *value = NULL;
242 s = ec_strdup(string);
246 value = ec_calloc(1, sizeof(*value));
250 value->type = EC_CONFIG_TYPE_STRING;
261 /* "consume" the node */
263 ec_config_node(struct ec_node *node)
265 struct ec_config *value = NULL;
270 value = ec_calloc(1, sizeof(*value));
274 value->type = EC_CONFIG_TYPE_NODE;
288 struct ec_config *value = NULL;
289 struct ec_keyval *dict = NULL;
295 value = ec_calloc(1, sizeof(*value));
299 value->type = EC_CONFIG_TYPE_DICT;
305 ec_keyval_free(dict);
313 struct ec_config *value = NULL;
315 value = ec_calloc(1, sizeof(*value));
319 value->type = EC_CONFIG_TYPE_LIST;
320 TAILQ_INIT(&value->list);
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)
332 for (i = 0; i < schema_len; i++) {
333 if (!strcmp(key, schema[i].key) &&
334 type == schema[i].type)
343 ec_config_free(struct ec_config *value)
348 switch (value->type) {
349 case EC_CONFIG_TYPE_STRING:
350 ec_free(value->string);
352 case EC_CONFIG_TYPE_NODE:
353 ec_node_free(value->node);
355 case EC_CONFIG_TYPE_LIST:
356 while (!TAILQ_EMPTY(&value->list)) {
358 v = TAILQ_FIRST(&value->list);
359 TAILQ_REMOVE(&value->list, v, next);
363 case EC_CONFIG_TYPE_DICT:
364 ec_keyval_free(value->dict);
374 ec_config_list_cmp(const struct ec_config_list *list1,
375 const struct ec_config_list *list2)
377 const struct ec_config *v1, *v2;
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))
385 if (v1 != NULL || v2 != NULL)
391 /* XXX -> ec_keyval_cmp() */
393 ec_config_dict_cmp(const struct ec_keyval *d1,
394 const struct ec_keyval *d2)
396 const struct ec_config *v1, *v2;
397 struct ec_keyval_iter *iter = NULL;
400 if (ec_keyval_len(d1) != ec_keyval_len(d2))
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);
410 if (ec_config_cmp(v1, v2))
414 ec_keyval_iter_free(iter);
418 ec_keyval_iter_free(iter);
423 ec_config_cmp(const struct ec_config *value1,
424 const struct ec_config *value2)
426 if (value1->type != value2->type)
429 switch (value1->type) {
430 case EC_CONFIG_TYPE_BOOL:
431 if (value1->boolean == value2->boolean)
433 case EC_CONFIG_TYPE_INT64:
434 if (value1->i64 == value2->i64)
436 case EC_CONFIG_TYPE_UINT64:
437 if (value1->u64 == value2->u64)
439 case EC_CONFIG_TYPE_STRING:
440 if (!strcmp(value1->string, value2->string))
442 case EC_CONFIG_TYPE_NODE:
443 if (value1->node == value2->node)
445 case EC_CONFIG_TYPE_LIST:
446 return ec_config_list_cmp(&value1->list, &value2->list);
447 case EC_CONFIG_TYPE_DICT:
448 return ec_config_dict_cmp(value1->dict, value2->dict);
457 ec_config_list_validate(const struct ec_config_list *list,
458 const struct ec_config_schema *sch)
460 const struct ec_config *value;
462 TAILQ_FOREACH(value, list, next) {
463 if (value->type != sch->type) {
468 if (value->type == EC_CONFIG_TYPE_LIST) {
469 if (ec_config_list_validate(&value->list,
472 } else if (value->type == EC_CONFIG_TYPE_DICT) {
473 if (ec_config_dict_validate(value->dict,
474 sch->subschema, sch->subschema_len) < 0)
483 ec_config_dict_validate(const struct ec_keyval *dict,
484 const struct ec_config_schema *schema,
487 const struct ec_config *value;
488 struct ec_keyval_iter *iter = NULL;
489 const struct ec_config_schema *sch;
492 for (iter = ec_keyval_iter(dict);
493 ec_keyval_iter_valid(iter);
494 ec_keyval_iter_next(iter)) {
496 key = ec_keyval_iter_get_key(iter);
497 value = ec_keyval_iter_get_val(iter);
498 sch = ec_config_schema_lookup(schema, schema_len,
505 if (value->type == EC_CONFIG_TYPE_LIST) {
506 if (ec_config_list_validate(&value->list,
509 } else if (value->type == EC_CONFIG_TYPE_DICT) {
510 if (ec_config_dict_validate(value->dict,
511 sch->subschema, sch->subschema_len) < 0)
516 ec_keyval_iter_free(iter);
520 ec_keyval_iter_free(iter);
525 ec_config_validate(const struct ec_config *dict,
526 const struct ec_config_schema *schema,
529 if (dict->type != EC_CONFIG_TYPE_DICT || schema == NULL) {
534 if (ec_config_dict_validate(dict->dict, schema, schema_len) < 0)
544 ec_config_get(const struct ec_config *config, const char *key)
549 return ec_keyval_get(config->dict, key);
553 ec_config_list_first(struct ec_config *list)
555 if (list == NULL || list->type != EC_CONFIG_TYPE_LIST) {
560 return TAILQ_FIRST(&list->list);
564 ec_config_list_next(struct ec_config *list, struct ec_config *config)
567 return TAILQ_NEXT(config, next);
570 /* value is consumed */
571 int ec_config_dict_set(struct ec_config *config, const char *key,
572 struct ec_config *value)
574 void (*free_cb)(struct ec_config *) = ec_config_free;
576 if (config == NULL || key == NULL || value == NULL) {
580 if (config->type != EC_CONFIG_TYPE_DICT) {
585 return ec_keyval_set(config->dict, key, value,
586 (void (*)(void *))free_cb);
589 ec_config_free(value);
593 int ec_config_dict_del(struct ec_config *config, const char *key)
595 if (config == NULL || key == NULL) {
599 if (config->type != EC_CONFIG_TYPE_DICT) {
604 return ec_keyval_del(config->dict, key);
607 /* value is consumed */
609 ec_config_list_add(struct ec_config *list,
610 struct ec_config *value)
612 if (list == NULL || list->type != EC_CONFIG_TYPE_LIST) {
617 TAILQ_INSERT_TAIL(&list->list, value, next);
622 ec_config_free(value);
626 int ec_config_list_del(struct ec_config *list, struct ec_config *config)
628 if (list == NULL || list->type != EC_CONFIG_TYPE_LIST) {
633 TAILQ_REMOVE(&list->list, config, next);
634 ec_config_free(config);
639 ec_config_list_dump(FILE *out, const struct ec_config_list *list,
642 const struct ec_config *v;
644 fprintf(out, "%*s" "type=list:\n", (int)indent * 4, "");
646 TAILQ_FOREACH(v, list, next) {
647 if (__ec_config_dump(out, NULL, v, indent + 1) < 0)
655 ec_config_dict_dump(FILE *out, const struct ec_keyval *dict,
658 const struct ec_config *value;
659 struct ec_keyval_iter *iter;
662 fprintf(out, "%*s" "type=dict:\n", (int)indent * 4, "");
663 for (iter = ec_keyval_iter(dict);
664 ec_keyval_iter_valid(iter);
665 ec_keyval_iter_next(iter)) {
666 key = ec_keyval_iter_get_key(iter);
667 value = ec_keyval_iter_get_val(iter);
668 if (__ec_config_dump(out, key, value, indent + 1) < 0)
671 ec_keyval_iter_free(iter);
675 ec_keyval_iter_free(iter);
680 __ec_config_dump(FILE *out, const char *key, const struct ec_config *value,
683 char *val_str = NULL;
685 switch (value->type) {
686 case EC_CONFIG_TYPE_BOOL:
688 ec_asprintf(&val_str, "true");
690 ec_asprintf(&val_str, "false");
692 case EC_CONFIG_TYPE_INT64:
693 ec_asprintf(&val_str, "%"PRIu64, value->u64);
695 case EC_CONFIG_TYPE_UINT64:
696 ec_asprintf(&val_str, "%"PRIi64, value->i64);
698 case EC_CONFIG_TYPE_STRING:
699 ec_asprintf(&val_str, "%s", value->string);
701 case EC_CONFIG_TYPE_NODE:
702 ec_asprintf(&val_str, "%p", value->node);
704 case EC_CONFIG_TYPE_LIST:
705 return ec_config_list_dump(out, &value->list, indent);
706 case EC_CONFIG_TYPE_DICT:
707 return ec_config_dict_dump(out, value->dict, indent);
713 /* errno is already set on error */
717 fprintf(out, "%*s" "%s%s%stype=%s val=%s\n", (int)indent * 4, "",
721 ec_config_type_str(value->type), val_str);
732 ec_config_dump(FILE *out, const struct ec_config *config)
734 fprintf(out, "------------------- config dump:\n");
736 if (config == NULL) {
737 fprintf(out, "no config\n");
741 if (__ec_config_dump(out, NULL, config, 0) < 0)
742 fprintf(out, "error while dumping\n");
745 /* LCOV_EXCL_START */
746 static const struct ec_config_schema sch_intlist_elt[] = {
748 .desc = "This is a description for int",
749 .type = EC_CONFIG_TYPE_INT64,
753 static const struct ec_config_schema sch_dict[] = {
756 .desc = "This is a description for int",
757 .type = EC_CONFIG_TYPE_INT64,
761 .desc = "This is a description for int2",
762 .type = EC_CONFIG_TYPE_INT64,
766 static const struct ec_config_schema sch_dictlist_elt[] = {
768 .desc = "This is a description for dict",
769 .type = EC_CONFIG_TYPE_DICT,
770 .subschema = sch_dict,
771 .subschema_len = EC_COUNT_OF(sch_dict),
775 static const struct ec_config_schema sch_baseconfig[] = {
778 .desc = "This is a description for bool",
779 .type = EC_CONFIG_TYPE_BOOL,
783 .desc = "This is a description for int",
784 .type = EC_CONFIG_TYPE_INT64,
788 .desc = "This is a description for string",
789 .type = EC_CONFIG_TYPE_STRING,
793 .desc = "This is a description for node",
794 .type = EC_CONFIG_TYPE_NODE,
798 .desc = "This is a description for list",
799 .type = EC_CONFIG_TYPE_LIST,
800 .subschema = sch_intlist_elt,
801 .subschema_len = EC_COUNT_OF(sch_intlist_elt),
804 .key = "my_dictlist",
805 .desc = "This is a description for list",
806 .type = EC_CONFIG_TYPE_LIST,
807 .subschema = sch_dictlist_elt,
808 .subschema_len = EC_COUNT_OF(sch_dictlist_elt),
812 static int ec_config_testcase(void)
814 struct ec_node *node = NULL;
815 struct ec_keyval *dict = NULL;
816 const struct ec_config *value = NULL;
817 struct ec_config *config = NULL, *list = NULL, *subconfig = NULL;
818 struct ec_config *list_, *config_;
822 node = ec_node("empty", EC_NO_ID);
826 if (ec_config_schema_validate(sch_baseconfig,
827 EC_COUNT_OF(sch_baseconfig)) < 0) {
828 EC_LOG(EC_LOG_ERR, "invalid config schema\n");
832 ec_config_schema_dump(stdout, sch_baseconfig,
833 EC_COUNT_OF(sch_baseconfig));
835 config = ec_config_dict();
839 ret = ec_config_dict_set(config, "my_bool", ec_config_bool(true));
840 testres |= EC_TEST_CHECK(ret == 0, "cannot set boolean");
841 value = ec_config_get(config, "my_bool");
842 testres |= EC_TEST_CHECK(
844 value->type == EC_CONFIG_TYPE_BOOL &&
845 value->boolean == true,
846 "unexpected boolean value");
848 ret = ec_config_dict_set(config, "my_int", ec_config_i64(1234));
849 testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
850 value = ec_config_get(config, "my_int");
851 testres |= EC_TEST_CHECK(
853 value->type == EC_CONFIG_TYPE_INT64 &&
855 "unexpected int value");
857 testres |= EC_TEST_CHECK(
858 ec_config_validate(config, sch_baseconfig,
859 EC_COUNT_OF(sch_baseconfig)) == 0,
860 "cannot validate config\n");
862 ret = ec_config_dict_set(config, "my_string", ec_config_string("toto"));
863 testres |= EC_TEST_CHECK(ret == 0, "cannot set string");
864 value = ec_config_get(config, "my_string");
865 testres |= EC_TEST_CHECK(
867 value->type == EC_CONFIG_TYPE_STRING &&
868 !strcmp(value->string, "toto"),
869 "unexpected string value");
871 list = ec_config_list();
875 subconfig = ec_config_dict();
876 if (subconfig == NULL)
879 ret = ec_config_dict_set(subconfig, "my_int", ec_config_i64(1));
880 testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
881 value = ec_config_get(subconfig, "my_int");
882 testres |= EC_TEST_CHECK(
884 value->type == EC_CONFIG_TYPE_INT64 &&
886 "unexpected int value");
888 ret = ec_config_dict_set(subconfig, "my_int2", ec_config_i64(2));
889 testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
890 value = ec_config_get(subconfig, "my_int2");
891 testres |= EC_TEST_CHECK(
893 value->type == EC_CONFIG_TYPE_INT64 &&
895 "unexpected int value");
897 testres |= EC_TEST_CHECK(
898 ec_config_validate(subconfig, sch_dict,
899 EC_COUNT_OF(sch_dict)) == 0,
900 "cannot validate subconfig\n");
902 ret = ec_config_list_add(list, subconfig);
903 subconfig = NULL; /* freed */
904 testres |= EC_TEST_CHECK(ret == 0, "cannot add in list");
906 subconfig = ec_config_dict();
907 if (subconfig == NULL)
910 ret = ec_config_dict_set(subconfig, "my_int", ec_config_i64(3));
911 testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
912 value = ec_config_get(subconfig, "my_int");
913 testres |= EC_TEST_CHECK(
915 value->type == EC_CONFIG_TYPE_INT64 &&
917 "unexpected int value");
919 ret = ec_config_dict_set(subconfig, "my_int2", ec_config_i64(4));
920 testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
921 value = ec_config_get(subconfig, "my_int2");
922 testres |= EC_TEST_CHECK(
924 value->type == EC_CONFIG_TYPE_INT64 &&
926 "unexpected int value");
928 testres |= EC_TEST_CHECK(
929 ec_config_validate(subconfig, sch_dict,
930 EC_COUNT_OF(sch_dict)) == 0,
931 "cannot validate subconfig\n");
933 ret = ec_config_list_add(list, subconfig);
934 subconfig = NULL; /* freed */
935 testres |= EC_TEST_CHECK(ret == 0, "cannot add in list");
937 ret = ec_config_dict_set(config, "my_dictlist", list);
939 testres |= EC_TEST_CHECK(ret == 0, "cannot set list");
941 testres |= EC_TEST_CHECK(
942 ec_config_validate(config, sch_baseconfig,
943 EC_COUNT_OF(sch_baseconfig)) == 0,
944 "cannot validate config\n");
946 list_ = ec_config_get(config, "my_dictlist");
947 printf("list = %p\n", list_);
948 for (config_ = ec_config_list_first(list_); config_ != NULL;
949 config_ = ec_config_list_next(list_, config_)) {
950 ec_config_dump(stdout, config_);
953 ec_config_dump(stdout, config);
955 /* remove the first element */
956 ec_config_list_del(list_, ec_config_list_first(list_));
957 testres |= EC_TEST_CHECK(
958 ec_config_validate(config, sch_baseconfig,
959 EC_COUNT_OF(sch_baseconfig)) == 0,
960 "cannot validate config\n");
962 ec_config_dump(stdout, config);
964 ec_config_free(list);
965 ec_config_free(subconfig);
966 ec_config_free(config);
967 ec_keyval_free(dict);
973 ec_config_free(list);
974 ec_config_free(subconfig);
975 ec_config_free(config);
976 ec_keyval_free(dict);
983 static struct ec_test ec_config_test = {
985 .test = ec_config_testcase,
988 EC_TEST_REGISTER(ec_config_test);