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);
187 enum ec_config_type ec_config_get_type(const struct ec_config *config)
193 ec_config_bool(bool boolean)
195 struct ec_config *value = NULL;
197 value = ec_calloc(1, sizeof(*value));
201 value->type = EC_CONFIG_TYPE_BOOL;
202 value->boolean = boolean;
208 ec_config_i64(int64_t i64)
210 struct ec_config *value = NULL;
212 value = ec_calloc(1, sizeof(*value));
216 value->type = EC_CONFIG_TYPE_INT64;
223 ec_config_u64(uint64_t u64)
225 struct ec_config *value = NULL;
227 value = ec_calloc(1, sizeof(*value));
231 value->type = EC_CONFIG_TYPE_UINT64;
237 /* duplicate string */
239 ec_config_string(const char *string)
241 struct ec_config *value = NULL;
247 s = ec_strdup(string);
251 value = ec_calloc(1, sizeof(*value));
255 value->type = EC_CONFIG_TYPE_STRING;
266 /* "consume" the node */
268 ec_config_node(struct ec_node *node)
270 struct ec_config *value = NULL;
275 value = ec_calloc(1, sizeof(*value));
279 value->type = EC_CONFIG_TYPE_NODE;
293 struct ec_config *value = NULL;
294 struct ec_keyval *dict = NULL;
300 value = ec_calloc(1, sizeof(*value));
304 value->type = EC_CONFIG_TYPE_DICT;
310 ec_keyval_free(dict);
318 struct ec_config *value = NULL;
320 value = ec_calloc(1, sizeof(*value));
324 value->type = EC_CONFIG_TYPE_LIST;
325 TAILQ_INIT(&value->list);
330 static const struct ec_config_schema *
331 ec_config_schema_lookup(const struct ec_config_schema *schema,
332 size_t schema_len, const char *key,
333 enum ec_config_type type)
337 for (i = 0; i < schema_len; i++) {
338 if (!strcmp(key, schema[i].key) &&
339 type == schema[i].type)
348 ec_config_free(struct ec_config *value)
353 switch (value->type) {
354 case EC_CONFIG_TYPE_STRING:
355 ec_free(value->string);
357 case EC_CONFIG_TYPE_NODE:
358 ec_node_free(value->node);
360 case EC_CONFIG_TYPE_LIST:
361 while (!TAILQ_EMPTY(&value->list)) {
363 v = TAILQ_FIRST(&value->list);
364 TAILQ_REMOVE(&value->list, v, next);
368 case EC_CONFIG_TYPE_DICT:
369 ec_keyval_free(value->dict);
379 ec_config_list_cmp(const struct ec_config_list *list1,
380 const struct ec_config_list *list2)
382 const struct ec_config *v1, *v2;
384 for (v1 = TAILQ_FIRST(list1), v2 = TAILQ_FIRST(list2);
385 v1 != NULL && v2 != NULL;
386 v1 = TAILQ_NEXT(v1, next), v2 = TAILQ_NEXT(v2, next)) {
387 if (ec_config_cmp(v1, v2))
390 if (v1 != NULL || v2 != NULL)
396 /* XXX -> ec_keyval_cmp() */
398 ec_config_dict_cmp(const struct ec_keyval *d1,
399 const struct ec_keyval *d2)
401 const struct ec_config *v1, *v2;
402 struct ec_keyval_iter *iter = NULL;
405 if (ec_keyval_len(d1) != ec_keyval_len(d2))
408 for (iter = ec_keyval_iter(d1);
409 ec_keyval_iter_valid(iter);
410 ec_keyval_iter_next(iter)) {
411 key = ec_keyval_iter_get_key(iter);
412 v1 = ec_keyval_iter_get_val(iter);
413 v2 = ec_keyval_get(d2, key);
415 if (ec_config_cmp(v1, v2))
419 ec_keyval_iter_free(iter);
423 ec_keyval_iter_free(iter);
428 ec_config_cmp(const struct ec_config *value1,
429 const struct ec_config *value2)
431 if (value1 == NULL || value2 == NULL) {
436 if (value1->type != value2->type)
439 switch (value1->type) {
440 case EC_CONFIG_TYPE_BOOL:
441 if (value1->boolean == value2->boolean)
443 case EC_CONFIG_TYPE_INT64:
444 if (value1->i64 == value2->i64)
446 case EC_CONFIG_TYPE_UINT64:
447 if (value1->u64 == value2->u64)
449 case EC_CONFIG_TYPE_STRING:
450 if (!strcmp(value1->string, value2->string))
452 case EC_CONFIG_TYPE_NODE:
453 if (value1->node == value2->node)
455 case EC_CONFIG_TYPE_LIST:
456 return ec_config_list_cmp(&value1->list, &value2->list);
457 case EC_CONFIG_TYPE_DICT:
458 return ec_config_dict_cmp(value1->dict, value2->dict);
467 ec_config_list_validate(const struct ec_config_list *list,
468 const struct ec_config_schema *sch)
470 const struct ec_config *value;
472 TAILQ_FOREACH(value, list, next) {
473 if (value->type != sch->type) {
478 if (value->type == EC_CONFIG_TYPE_LIST) {
479 if (ec_config_list_validate(&value->list,
482 } else if (value->type == EC_CONFIG_TYPE_DICT) {
483 if (ec_config_dict_validate(value->dict,
484 sch->subschema, sch->subschema_len) < 0)
493 ec_config_dict_validate(const struct ec_keyval *dict,
494 const struct ec_config_schema *schema,
497 const struct ec_config *value;
498 struct ec_keyval_iter *iter = NULL;
499 const struct ec_config_schema *sch;
502 for (iter = ec_keyval_iter(dict);
503 ec_keyval_iter_valid(iter);
504 ec_keyval_iter_next(iter)) {
506 key = ec_keyval_iter_get_key(iter);
507 value = ec_keyval_iter_get_val(iter);
508 sch = ec_config_schema_lookup(schema, schema_len,
515 if (value->type == EC_CONFIG_TYPE_LIST) {
516 if (ec_config_list_validate(&value->list,
519 } else if (value->type == EC_CONFIG_TYPE_DICT) {
520 if (ec_config_dict_validate(value->dict,
521 sch->subschema, sch->subschema_len) < 0)
526 ec_keyval_iter_free(iter);
530 ec_keyval_iter_free(iter);
535 ec_config_validate(const struct ec_config *dict,
536 const struct ec_config_schema *schema,
539 if (dict->type != EC_CONFIG_TYPE_DICT || schema == NULL) {
544 if (ec_config_dict_validate(dict->dict, schema, schema_len) < 0)
554 ec_config_dict_get(const struct ec_config *config, const char *key)
556 if (config == NULL) {
561 if (config->type != EC_CONFIG_TYPE_DICT) {
566 return ec_keyval_get(config->dict, key);
570 ec_config_list_first(struct ec_config *list)
572 if (list == NULL || list->type != EC_CONFIG_TYPE_LIST) {
577 return TAILQ_FIRST(&list->list);
581 ec_config_list_next(struct ec_config *list, struct ec_config *config)
584 return TAILQ_NEXT(config, next);
587 /* value is consumed */
588 int ec_config_dict_set(struct ec_config *config, const char *key,
589 struct ec_config *value)
591 void (*free_cb)(struct ec_config *) = ec_config_free;
593 if (config == NULL || key == NULL || value == NULL) {
597 if (config->type != EC_CONFIG_TYPE_DICT) {
602 return ec_keyval_set(config->dict, key, value,
603 (void (*)(void *))free_cb);
606 ec_config_free(value);
610 int ec_config_dict_del(struct ec_config *config, const char *key)
612 if (config == NULL || key == NULL) {
616 if (config->type != EC_CONFIG_TYPE_DICT) {
621 return ec_keyval_del(config->dict, key);
624 /* value is consumed */
626 ec_config_list_add(struct ec_config *list,
627 struct ec_config *value)
629 if (list == NULL || list->type != EC_CONFIG_TYPE_LIST || value == NULL) {
634 TAILQ_INSERT_TAIL(&list->list, value, next);
639 ec_config_free(value);
643 int ec_config_list_del(struct ec_config *list, struct ec_config *config)
645 if (list == NULL || list->type != EC_CONFIG_TYPE_LIST) {
650 TAILQ_REMOVE(&list->list, config, next);
651 ec_config_free(config);
655 static struct ec_config *
656 ec_config_list_dup(const struct ec_config_list *list)
658 struct ec_config *dup = NULL, *v, *value;
660 dup = ec_config_list();
664 TAILQ_FOREACH(v, list, next) {
665 value = ec_config_dup(v);
668 if (ec_config_list_add(dup, value) < 0)
679 static struct ec_config *
680 ec_config_dict_dup(const struct ec_keyval *dict)
682 struct ec_config *dup = NULL, *value;
683 struct ec_keyval_iter *iter = NULL;
686 dup = ec_config_dict();
690 for (iter = ec_keyval_iter(dict);
691 ec_keyval_iter_valid(iter);
692 ec_keyval_iter_next(iter)) {
693 key = ec_keyval_iter_get_key(iter);
694 value = ec_config_dup(ec_keyval_iter_get_val(iter));
697 if (ec_config_dict_set(dup, key, value) < 0)
700 ec_keyval_iter_free(iter);
706 ec_keyval_iter_free(iter);
711 ec_config_dup(const struct ec_config *config)
713 if (config == NULL) {
718 switch (config->type) {
719 case EC_CONFIG_TYPE_BOOL:
720 return ec_config_bool(config->boolean);
721 case EC_CONFIG_TYPE_INT64:
722 return ec_config_i64(config->i64);
723 case EC_CONFIG_TYPE_UINT64:
724 return ec_config_u64(config->u64);
725 case EC_CONFIG_TYPE_STRING:
726 return ec_config_string(config->string);
727 case EC_CONFIG_TYPE_NODE:
728 return ec_config_node(ec_node_clone(config->node));
729 case EC_CONFIG_TYPE_LIST:
730 return ec_config_list_dup(&config->list);
731 case EC_CONFIG_TYPE_DICT:
732 return ec_config_dict_dup(config->dict);
742 ec_config_list_dump(FILE *out, const struct ec_config_list *list,
745 const struct ec_config *v;
747 fprintf(out, "%*s" "type=list:\n", (int)indent * 4, "");
749 TAILQ_FOREACH(v, list, next) {
750 if (__ec_config_dump(out, NULL, v, indent + 1) < 0)
758 ec_config_dict_dump(FILE *out, const struct ec_keyval *dict,
761 const struct ec_config *value;
762 struct ec_keyval_iter *iter;
765 fprintf(out, "%*s" "type=dict:\n", (int)indent * 4, "");
766 for (iter = ec_keyval_iter(dict);
767 ec_keyval_iter_valid(iter);
768 ec_keyval_iter_next(iter)) {
769 key = ec_keyval_iter_get_key(iter);
770 value = ec_keyval_iter_get_val(iter);
771 if (__ec_config_dump(out, key, value, indent + 1) < 0)
774 ec_keyval_iter_free(iter);
778 ec_keyval_iter_free(iter);
783 __ec_config_dump(FILE *out, const char *key, const struct ec_config *value,
786 char *val_str = NULL;
788 switch (value->type) {
789 case EC_CONFIG_TYPE_BOOL:
791 ec_asprintf(&val_str, "true");
793 ec_asprintf(&val_str, "false");
795 case EC_CONFIG_TYPE_INT64:
796 ec_asprintf(&val_str, "%"PRIu64, value->u64);
798 case EC_CONFIG_TYPE_UINT64:
799 ec_asprintf(&val_str, "%"PRIi64, value->i64);
801 case EC_CONFIG_TYPE_STRING:
802 ec_asprintf(&val_str, "%s", value->string);
804 case EC_CONFIG_TYPE_NODE:
805 ec_asprintf(&val_str, "%p", value->node);
807 case EC_CONFIG_TYPE_LIST:
808 return ec_config_list_dump(out, &value->list, indent);
809 case EC_CONFIG_TYPE_DICT:
810 return ec_config_dict_dump(out, value->dict, indent);
816 /* errno is already set on error */
820 fprintf(out, "%*s" "%s%s%stype=%s val=%s\n", (int)indent * 4, "",
824 ec_config_type_str(value->type), val_str);
835 ec_config_dump(FILE *out, const struct ec_config *config)
837 fprintf(out, "------------------- config dump:\n");
839 if (config == NULL) {
840 fprintf(out, "no config\n");
844 if (__ec_config_dump(out, NULL, config, 0) < 0)
845 fprintf(out, "error while dumping\n");
848 /* LCOV_EXCL_START */
849 static const struct ec_config_schema sch_intlist_elt[] = {
851 .desc = "This is a description for int",
852 .type = EC_CONFIG_TYPE_INT64,
856 static const struct ec_config_schema sch_dict[] = {
859 .desc = "This is a description for int",
860 .type = EC_CONFIG_TYPE_INT64,
864 .desc = "This is a description for int2",
865 .type = EC_CONFIG_TYPE_INT64,
869 static const struct ec_config_schema sch_dictlist_elt[] = {
871 .desc = "This is a description for dict",
872 .type = EC_CONFIG_TYPE_DICT,
873 .subschema = sch_dict,
874 .subschema_len = EC_COUNT_OF(sch_dict),
878 static const struct ec_config_schema sch_baseconfig[] = {
881 .desc = "This is a description for bool",
882 .type = EC_CONFIG_TYPE_BOOL,
886 .desc = "This is a description for int",
887 .type = EC_CONFIG_TYPE_INT64,
891 .desc = "This is a description for string",
892 .type = EC_CONFIG_TYPE_STRING,
896 .desc = "This is a description for node",
897 .type = EC_CONFIG_TYPE_NODE,
901 .desc = "This is a description for list",
902 .type = EC_CONFIG_TYPE_LIST,
903 .subschema = sch_intlist_elt,
904 .subschema_len = EC_COUNT_OF(sch_intlist_elt),
907 .key = "my_dictlist",
908 .desc = "This is a description for list",
909 .type = EC_CONFIG_TYPE_LIST,
910 .subschema = sch_dictlist_elt,
911 .subschema_len = EC_COUNT_OF(sch_dictlist_elt),
915 static int ec_config_testcase(void)
917 struct ec_node *node = NULL;
918 struct ec_keyval *dict = NULL;
919 const struct ec_config *value = NULL;
920 struct ec_config *config = NULL, *config2 = NULL;
921 struct ec_config *list = NULL, *subconfig = NULL;
922 struct ec_config *list_, *config_;
926 node = ec_node("empty", EC_NO_ID);
930 if (ec_config_schema_validate(sch_baseconfig,
931 EC_COUNT_OF(sch_baseconfig)) < 0) {
932 EC_LOG(EC_LOG_ERR, "invalid config schema\n");
936 ec_config_schema_dump(stdout, sch_baseconfig,
937 EC_COUNT_OF(sch_baseconfig));
939 config = ec_config_dict();
943 ret = ec_config_dict_set(config, "my_bool", ec_config_bool(true));
944 testres |= EC_TEST_CHECK(ret == 0, "cannot set boolean");
945 value = ec_config_dict_get(config, "my_bool");
946 testres |= EC_TEST_CHECK(
948 value->type == EC_CONFIG_TYPE_BOOL &&
949 value->boolean == true,
950 "unexpected boolean value");
952 ret = ec_config_dict_set(config, "my_int", ec_config_i64(1234));
953 testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
954 value = ec_config_dict_get(config, "my_int");
955 testres |= EC_TEST_CHECK(
957 value->type == EC_CONFIG_TYPE_INT64 &&
959 "unexpected int value");
961 testres |= EC_TEST_CHECK(
962 ec_config_validate(config, sch_baseconfig,
963 EC_COUNT_OF(sch_baseconfig)) == 0,
964 "cannot validate config\n");
966 ret = ec_config_dict_set(config, "my_string", ec_config_string("toto"));
967 testres |= EC_TEST_CHECK(ret == 0, "cannot set string");
968 value = ec_config_dict_get(config, "my_string");
969 testres |= EC_TEST_CHECK(
971 value->type == EC_CONFIG_TYPE_STRING &&
972 !strcmp(value->string, "toto"),
973 "unexpected string value");
975 list = ec_config_list();
979 subconfig = ec_config_dict();
980 if (subconfig == NULL)
983 ret = ec_config_dict_set(subconfig, "my_int", ec_config_i64(1));
984 testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
985 value = ec_config_dict_get(subconfig, "my_int");
986 testres |= EC_TEST_CHECK(
988 value->type == EC_CONFIG_TYPE_INT64 &&
990 "unexpected int value");
992 ret = ec_config_dict_set(subconfig, "my_int2", ec_config_i64(2));
993 testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
994 value = ec_config_dict_get(subconfig, "my_int2");
995 testres |= EC_TEST_CHECK(
997 value->type == EC_CONFIG_TYPE_INT64 &&
999 "unexpected int value");
1001 testres |= EC_TEST_CHECK(
1002 ec_config_validate(subconfig, sch_dict,
1003 EC_COUNT_OF(sch_dict)) == 0,
1004 "cannot validate subconfig\n");
1006 ret = ec_config_list_add(list, subconfig);
1007 subconfig = NULL; /* freed */
1008 testres |= EC_TEST_CHECK(ret == 0, "cannot add in list");
1010 subconfig = ec_config_dict();
1011 if (subconfig == NULL)
1014 ret = ec_config_dict_set(subconfig, "my_int", ec_config_i64(3));
1015 testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
1016 value = ec_config_dict_get(subconfig, "my_int");
1017 testres |= EC_TEST_CHECK(
1019 value->type == EC_CONFIG_TYPE_INT64 &&
1021 "unexpected int value");
1023 ret = ec_config_dict_set(subconfig, "my_int2", ec_config_i64(4));
1024 testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
1025 value = ec_config_dict_get(subconfig, "my_int2");
1026 testres |= EC_TEST_CHECK(
1028 value->type == EC_CONFIG_TYPE_INT64 &&
1030 "unexpected int value");
1032 testres |= EC_TEST_CHECK(
1033 ec_config_validate(subconfig, sch_dict,
1034 EC_COUNT_OF(sch_dict)) == 0,
1035 "cannot validate subconfig\n");
1037 ret = ec_config_list_add(list, subconfig);
1038 subconfig = NULL; /* freed */
1039 testres |= EC_TEST_CHECK(ret == 0, "cannot add in list");
1041 ret = ec_config_dict_set(config, "my_dictlist", list);
1043 testres |= EC_TEST_CHECK(ret == 0, "cannot set list");
1045 testres |= EC_TEST_CHECK(
1046 ec_config_validate(config, sch_baseconfig,
1047 EC_COUNT_OF(sch_baseconfig)) == 0,
1048 "cannot validate config\n");
1050 list_ = ec_config_dict_get(config, "my_dictlist");
1051 for (config_ = ec_config_list_first(list_); config_ != NULL;
1052 config_ = ec_config_list_next(list_, config_)) {
1053 ec_config_dump(stdout, config_);
1056 ec_config_dump(stdout, config);
1058 config2 = ec_config_dup(config);
1059 testres |= EC_TEST_CHECK(config2 != NULL, "cannot duplicate config");
1060 testres |= EC_TEST_CHECK(
1061 ec_config_cmp(config, config2) == 0,
1062 "fail to compare config");
1063 ec_config_free(config2);
1066 /* remove the first element */
1067 ec_config_list_del(list_, ec_config_list_first(list_));
1068 testres |= EC_TEST_CHECK(
1069 ec_config_validate(config, sch_baseconfig,
1070 EC_COUNT_OF(sch_baseconfig)) == 0,
1071 "cannot validate config\n");
1073 ec_config_dump(stdout, config);
1075 ec_config_free(list);
1076 ec_config_free(subconfig);
1077 ec_config_free(config);
1078 ec_keyval_free(dict);
1084 ec_config_free(list);
1085 ec_config_free(subconfig);
1086 ec_config_free(config);
1087 ec_config_free(config2);
1088 ec_keyval_free(dict);
1093 /* LCOV_EXCL_STOP */
1095 static struct ec_test ec_config_test = {
1097 .test = ec_config_testcase,
1100 EC_TEST_REGISTER(ec_config_test);