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);
28 /* return ec_value type as a string */
30 ec_config_type_str(enum ec_config_type type)
33 case EC_CONFIG_TYPE_BOOL: return "bool";
34 case EC_CONFIG_TYPE_INT64: return "int64";
35 case EC_CONFIG_TYPE_UINT64: return "uint64";
36 case EC_CONFIG_TYPE_STRING: return "string";
37 case EC_CONFIG_TYPE_NODE: return "node";
38 case EC_CONFIG_TYPE_LIST: return "list";
39 case EC_CONFIG_TYPE_DICT: return "dict";
40 default: return "unknown";
45 ec_config_schema_len(const struct ec_config_schema *schema)
51 for (i = 0; schema[i].type != EC_CONFIG_TYPE_NONE; i++)
57 __ec_config_schema_validate(const struct ec_config_schema *schema,
58 enum ec_config_type type)
63 if (type == EC_CONFIG_TYPE_LIST) {
64 if (schema[0].key != NULL) {
66 EC_LOG(EC_LOG_ERR, "list schema key must be NULL\n");
69 } else if (type == EC_CONFIG_TYPE_DICT) {
70 for (i = 0; schema[i].type != EC_CONFIG_TYPE_NONE; i++) {
71 if (schema[i].key == NULL) {
74 "dict schema key should not be NULL\n");
80 EC_LOG(EC_LOG_ERR, "invalid schema type\n");
84 for (i = 0; schema[i].type != EC_CONFIG_TYPE_NONE; i++) {
85 /* check for duplicate name if more than one element */
86 for (j = i + 1; schema[j].type != EC_CONFIG_TYPE_NONE; j++) {
87 if (!strcmp(schema[i].key, schema[j].key)) {
90 "duplicate key <%s> in schema\n",
96 switch (schema[i].type) {
97 case EC_CONFIG_TYPE_BOOL:
98 case EC_CONFIG_TYPE_INT64:
99 case EC_CONFIG_TYPE_UINT64:
100 case EC_CONFIG_TYPE_STRING:
101 case EC_CONFIG_TYPE_NODE:
102 if (schema[i].subschema != NULL || ec_config_schema_len(
103 schema[i].subschema) != 0) {
106 "key <%s> should not have subtype/subschema\n",
111 case EC_CONFIG_TYPE_LIST:
112 if (schema[i].subschema == NULL || ec_config_schema_len(
113 schema[i].subschema) != 1) {
116 "key <%s> must have subschema of length 1\n",
121 case EC_CONFIG_TYPE_DICT:
122 if (schema[i].subschema == NULL || ec_config_schema_len(
123 schema[i].subschema) == 0) {
126 "key <%s> must have subschema\n",
132 EC_LOG(EC_LOG_ERR, "invalid type for key <%s>\n",
138 if (schema[i].subschema == NULL)
141 ret = __ec_config_schema_validate(schema[i].subschema,
144 EC_LOG(EC_LOG_ERR, "cannot parse subschema %s%s\n",
145 schema[i].key ? "key=" : "",
146 schema[i].key ? : "");
155 ec_config_schema_validate(const struct ec_config_schema *schema)
157 return __ec_config_schema_validate(schema, EC_CONFIG_TYPE_DICT);
161 __ec_config_schema_dump(FILE *out, const struct ec_config_schema *schema,
166 for (i = 0; schema[i].type != EC_CONFIG_TYPE_NONE; i++) {
167 fprintf(out, "%*s" "%s%s%stype=%s desc='%s'\n",
169 schema[i].key ? "key=": "",
170 schema[i].key ? : "",
171 schema[i].key ? " ": "",
172 ec_config_type_str(schema[i].type),
174 if (schema[i].subschema == NULL)
176 __ec_config_schema_dump(out, schema[i].subschema,
182 ec_config_schema_dump(FILE *out, const struct ec_config_schema *schema)
184 fprintf(out, "------------------- schema dump:\n");
186 if (schema == NULL) {
187 fprintf(out, "no schema\n");
191 __ec_config_schema_dump(out, schema, 0);
194 enum ec_config_type ec_config_get_type(const struct ec_config *config)
200 ec_config_bool(bool boolean)
202 struct ec_config *value = NULL;
204 value = ec_calloc(1, sizeof(*value));
208 value->type = EC_CONFIG_TYPE_BOOL;
209 value->boolean = boolean;
215 ec_config_i64(int64_t i64)
217 struct ec_config *value = NULL;
219 value = ec_calloc(1, sizeof(*value));
223 value->type = EC_CONFIG_TYPE_INT64;
230 ec_config_u64(uint64_t u64)
232 struct ec_config *value = NULL;
234 value = ec_calloc(1, sizeof(*value));
238 value->type = EC_CONFIG_TYPE_UINT64;
244 /* duplicate string */
246 ec_config_string(const char *string)
248 struct ec_config *value = NULL;
254 s = ec_strdup(string);
258 value = ec_calloc(1, sizeof(*value));
262 value->type = EC_CONFIG_TYPE_STRING;
273 /* "consume" the node */
275 ec_config_node(struct ec_node *node)
277 struct ec_config *value = NULL;
282 value = ec_calloc(1, sizeof(*value));
286 value->type = EC_CONFIG_TYPE_NODE;
300 struct ec_config *value = NULL;
301 struct ec_keyval *dict = NULL;
307 value = ec_calloc(1, sizeof(*value));
311 value->type = EC_CONFIG_TYPE_DICT;
317 ec_keyval_free(dict);
325 struct ec_config *value = NULL;
327 value = ec_calloc(1, sizeof(*value));
331 value->type = EC_CONFIG_TYPE_LIST;
332 TAILQ_INIT(&value->list);
337 static const struct ec_config_schema *
338 ec_config_schema_lookup(const struct ec_config_schema *schema,
339 const char *key, enum ec_config_type type)
343 for (i = 0; schema[i].type != EC_CONFIG_TYPE_NONE; i++) {
344 if (!strcmp(key, schema[i].key) &&
345 type == schema[i].type)
354 ec_config_free(struct ec_config *value)
359 switch (value->type) {
360 case EC_CONFIG_TYPE_STRING:
361 ec_free(value->string);
363 case EC_CONFIG_TYPE_NODE:
364 ec_node_free(value->node);
366 case EC_CONFIG_TYPE_LIST:
367 while (!TAILQ_EMPTY(&value->list)) {
369 v = TAILQ_FIRST(&value->list);
370 TAILQ_REMOVE(&value->list, v, next);
374 case EC_CONFIG_TYPE_DICT:
375 ec_keyval_free(value->dict);
385 ec_config_list_cmp(const struct ec_config_list *list1,
386 const struct ec_config_list *list2)
388 const struct ec_config *v1, *v2;
390 for (v1 = TAILQ_FIRST(list1), v2 = TAILQ_FIRST(list2);
391 v1 != NULL && v2 != NULL;
392 v1 = TAILQ_NEXT(v1, next), v2 = TAILQ_NEXT(v2, next)) {
393 if (ec_config_cmp(v1, v2))
396 if (v1 != NULL || v2 != NULL)
402 /* XXX -> ec_keyval_cmp() */
404 ec_config_dict_cmp(const struct ec_keyval *d1,
405 const struct ec_keyval *d2)
407 const struct ec_config *v1, *v2;
408 struct ec_keyval_iter *iter = NULL;
411 if (ec_keyval_len(d1) != ec_keyval_len(d2))
414 for (iter = ec_keyval_iter(d1);
415 ec_keyval_iter_valid(iter);
416 ec_keyval_iter_next(iter)) {
417 key = ec_keyval_iter_get_key(iter);
418 v1 = ec_keyval_iter_get_val(iter);
419 v2 = ec_keyval_get(d2, key);
421 if (ec_config_cmp(v1, v2))
425 ec_keyval_iter_free(iter);
429 ec_keyval_iter_free(iter);
434 ec_config_cmp(const struct ec_config *value1,
435 const struct ec_config *value2)
437 if (value1 == NULL || value2 == NULL) {
442 if (value1->type != value2->type)
445 switch (value1->type) {
446 case EC_CONFIG_TYPE_BOOL:
447 if (value1->boolean == value2->boolean)
449 case EC_CONFIG_TYPE_INT64:
450 if (value1->i64 == value2->i64)
452 case EC_CONFIG_TYPE_UINT64:
453 if (value1->u64 == value2->u64)
455 case EC_CONFIG_TYPE_STRING:
456 if (!strcmp(value1->string, value2->string))
458 case EC_CONFIG_TYPE_NODE:
459 if (value1->node == value2->node)
461 case EC_CONFIG_TYPE_LIST:
462 return ec_config_list_cmp(&value1->list, &value2->list);
463 case EC_CONFIG_TYPE_DICT:
464 return ec_config_dict_cmp(value1->dict, value2->dict);
473 ec_config_list_validate(const struct ec_config_list *list,
474 const struct ec_config_schema *sch)
476 const struct ec_config *value;
478 TAILQ_FOREACH(value, list, next) {
479 if (value->type != sch->type) {
484 if (value->type == EC_CONFIG_TYPE_LIST) {
485 if (ec_config_list_validate(&value->list,
488 } else if (value->type == EC_CONFIG_TYPE_DICT) {
489 if (ec_config_dict_validate(value->dict,
499 ec_config_dict_validate(const struct ec_keyval *dict,
500 const struct ec_config_schema *schema)
502 const struct ec_config *value;
503 struct ec_keyval_iter *iter = NULL;
504 const struct ec_config_schema *sch;
507 for (iter = ec_keyval_iter(dict);
508 ec_keyval_iter_valid(iter);
509 ec_keyval_iter_next(iter)) {
511 key = ec_keyval_iter_get_key(iter);
512 value = ec_keyval_iter_get_val(iter);
513 sch = ec_config_schema_lookup(schema, key, value->type);
519 if (value->type == EC_CONFIG_TYPE_LIST) {
520 if (ec_config_list_validate(&value->list,
523 } else if (value->type == EC_CONFIG_TYPE_DICT) {
524 if (ec_config_dict_validate(value->dict,
530 ec_keyval_iter_free(iter);
534 ec_keyval_iter_free(iter);
539 ec_config_validate(const struct ec_config *dict,
540 const struct ec_config_schema *schema)
542 if (dict->type != EC_CONFIG_TYPE_DICT || schema == NULL) {
547 if (ec_config_dict_validate(dict->dict, schema) < 0)
557 ec_config_dict_get(const struct ec_config *config, const char *key)
559 if (config == NULL) {
564 if (config->type != EC_CONFIG_TYPE_DICT) {
569 return ec_keyval_get(config->dict, key);
573 ec_config_list_first(struct ec_config *list)
575 if (list == NULL || list->type != EC_CONFIG_TYPE_LIST) {
580 return TAILQ_FIRST(&list->list);
584 ec_config_list_next(struct ec_config *list, struct ec_config *config)
587 return TAILQ_NEXT(config, next);
590 /* value is consumed */
591 int ec_config_dict_set(struct ec_config *config, const char *key,
592 struct ec_config *value)
594 void (*free_cb)(struct ec_config *) = ec_config_free;
596 if (config == NULL || key == NULL || value == NULL) {
600 if (config->type != EC_CONFIG_TYPE_DICT) {
605 return ec_keyval_set(config->dict, key, value,
606 (void (*)(void *))free_cb);
609 ec_config_free(value);
613 int ec_config_dict_del(struct ec_config *config, const char *key)
615 if (config == NULL || key == NULL) {
619 if (config->type != EC_CONFIG_TYPE_DICT) {
624 return ec_keyval_del(config->dict, key);
627 /* value is consumed */
629 ec_config_list_add(struct ec_config *list,
630 struct ec_config *value)
632 if (list == NULL || list->type != EC_CONFIG_TYPE_LIST || value == NULL) {
637 TAILQ_INSERT_TAIL(&list->list, value, next);
642 ec_config_free(value);
646 int ec_config_list_del(struct ec_config *list, struct ec_config *config)
648 if (list == NULL || list->type != EC_CONFIG_TYPE_LIST) {
653 TAILQ_REMOVE(&list->list, config, next);
654 ec_config_free(config);
658 static struct ec_config *
659 ec_config_list_dup(const struct ec_config_list *list)
661 struct ec_config *dup = NULL, *v, *value;
663 dup = ec_config_list();
667 TAILQ_FOREACH(v, list, next) {
668 value = ec_config_dup(v);
671 if (ec_config_list_add(dup, value) < 0)
682 static struct ec_config *
683 ec_config_dict_dup(const struct ec_keyval *dict)
685 struct ec_config *dup = NULL, *value;
686 struct ec_keyval_iter *iter = NULL;
689 dup = ec_config_dict();
693 for (iter = ec_keyval_iter(dict);
694 ec_keyval_iter_valid(iter);
695 ec_keyval_iter_next(iter)) {
696 key = ec_keyval_iter_get_key(iter);
697 value = ec_config_dup(ec_keyval_iter_get_val(iter));
700 if (ec_config_dict_set(dup, key, value) < 0)
703 ec_keyval_iter_free(iter);
709 ec_keyval_iter_free(iter);
714 ec_config_dup(const struct ec_config *config)
716 if (config == NULL) {
721 switch (config->type) {
722 case EC_CONFIG_TYPE_BOOL:
723 return ec_config_bool(config->boolean);
724 case EC_CONFIG_TYPE_INT64:
725 return ec_config_i64(config->i64);
726 case EC_CONFIG_TYPE_UINT64:
727 return ec_config_u64(config->u64);
728 case EC_CONFIG_TYPE_STRING:
729 return ec_config_string(config->string);
730 case EC_CONFIG_TYPE_NODE:
731 return ec_config_node(ec_node_clone(config->node));
732 case EC_CONFIG_TYPE_LIST:
733 return ec_config_list_dup(&config->list);
734 case EC_CONFIG_TYPE_DICT:
735 return ec_config_dict_dup(config->dict);
745 ec_config_list_dump(FILE *out, const struct ec_config_list *list,
748 const struct ec_config *v;
750 fprintf(out, "%*s" "type=list:\n", (int)indent * 4, "");
752 TAILQ_FOREACH(v, list, next) {
753 if (__ec_config_dump(out, NULL, v, indent + 1) < 0)
761 ec_config_dict_dump(FILE *out, const struct ec_keyval *dict,
764 const struct ec_config *value;
765 struct ec_keyval_iter *iter;
768 fprintf(out, "%*s" "type=dict:\n", (int)indent * 4, "");
769 for (iter = ec_keyval_iter(dict);
770 ec_keyval_iter_valid(iter);
771 ec_keyval_iter_next(iter)) {
772 key = ec_keyval_iter_get_key(iter);
773 value = ec_keyval_iter_get_val(iter);
774 if (__ec_config_dump(out, key, value, indent + 1) < 0)
777 ec_keyval_iter_free(iter);
781 ec_keyval_iter_free(iter);
786 __ec_config_dump(FILE *out, const char *key, const struct ec_config *value,
789 char *val_str = NULL;
791 switch (value->type) {
792 case EC_CONFIG_TYPE_BOOL:
794 ec_asprintf(&val_str, "true");
796 ec_asprintf(&val_str, "false");
798 case EC_CONFIG_TYPE_INT64:
799 ec_asprintf(&val_str, "%"PRIu64, value->u64);
801 case EC_CONFIG_TYPE_UINT64:
802 ec_asprintf(&val_str, "%"PRIi64, value->i64);
804 case EC_CONFIG_TYPE_STRING:
805 ec_asprintf(&val_str, "%s", value->string);
807 case EC_CONFIG_TYPE_NODE:
808 ec_asprintf(&val_str, "%p", value->node);
810 case EC_CONFIG_TYPE_LIST:
811 return ec_config_list_dump(out, &value->list, indent);
812 case EC_CONFIG_TYPE_DICT:
813 return ec_config_dict_dump(out, value->dict, indent);
819 /* errno is already set on error */
823 fprintf(out, "%*s" "%s%s%stype=%s val=%s\n", (int)indent * 4, "",
827 ec_config_type_str(value->type), val_str);
838 ec_config_dump(FILE *out, const struct ec_config *config)
840 fprintf(out, "------------------- config dump:\n");
842 if (config == NULL) {
843 fprintf(out, "no config\n");
847 if (__ec_config_dump(out, NULL, config, 0) < 0)
848 fprintf(out, "error while dumping\n");
851 /* LCOV_EXCL_START */
852 static const struct ec_config_schema sch_intlist_elt[] = {
854 .desc = "This is a description for int",
855 .type = EC_CONFIG_TYPE_INT64,
858 .type = EC_CONFIG_TYPE_NONE,
862 static const struct ec_config_schema sch_dict[] = {
865 .desc = "This is a description for int",
866 .type = EC_CONFIG_TYPE_INT64,
870 .desc = "This is a description for int2",
871 .type = EC_CONFIG_TYPE_INT64,
874 .type = EC_CONFIG_TYPE_NONE,
878 static const struct ec_config_schema sch_dictlist_elt[] = {
880 .desc = "This is a description for dict",
881 .type = EC_CONFIG_TYPE_DICT,
882 .subschema = sch_dict,
885 .type = EC_CONFIG_TYPE_NONE,
889 static const struct ec_config_schema sch_baseconfig[] = {
892 .desc = "This is a description for bool",
893 .type = EC_CONFIG_TYPE_BOOL,
897 .desc = "This is a description for int",
898 .type = EC_CONFIG_TYPE_INT64,
902 .desc = "This is a description for string",
903 .type = EC_CONFIG_TYPE_STRING,
907 .desc = "This is a description for node",
908 .type = EC_CONFIG_TYPE_NODE,
912 .desc = "This is a description for list",
913 .type = EC_CONFIG_TYPE_LIST,
914 .subschema = sch_intlist_elt,
917 .key = "my_dictlist",
918 .desc = "This is a description for list",
919 .type = EC_CONFIG_TYPE_LIST,
920 .subschema = sch_dictlist_elt,
923 .type = EC_CONFIG_TYPE_NONE,
927 static int ec_config_testcase(void)
929 struct ec_node *node = NULL;
930 struct ec_keyval *dict = NULL;
931 const struct ec_config *value = NULL;
932 struct ec_config *config = NULL, *config2 = NULL;
933 struct ec_config *list = NULL, *subconfig = NULL;
934 struct ec_config *list_, *config_;
938 node = ec_node("empty", EC_NO_ID);
942 if (ec_config_schema_validate(sch_baseconfig) < 0) {
943 EC_LOG(EC_LOG_ERR, "invalid config schema\n");
947 ec_config_schema_dump(stdout, sch_baseconfig);
949 config = ec_config_dict();
953 ret = ec_config_dict_set(config, "my_bool", ec_config_bool(true));
954 testres |= EC_TEST_CHECK(ret == 0, "cannot set boolean");
955 value = ec_config_dict_get(config, "my_bool");
956 testres |= EC_TEST_CHECK(
958 value->type == EC_CONFIG_TYPE_BOOL &&
959 value->boolean == true,
960 "unexpected boolean value");
962 ret = ec_config_dict_set(config, "my_int", ec_config_i64(1234));
963 testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
964 value = ec_config_dict_get(config, "my_int");
965 testres |= EC_TEST_CHECK(
967 value->type == EC_CONFIG_TYPE_INT64 &&
969 "unexpected int value");
971 testres |= EC_TEST_CHECK(
972 ec_config_validate(config, sch_baseconfig) == 0,
973 "cannot validate config\n");
975 ret = ec_config_dict_set(config, "my_string", ec_config_string("toto"));
976 testres |= EC_TEST_CHECK(ret == 0, "cannot set string");
977 value = ec_config_dict_get(config, "my_string");
978 testres |= EC_TEST_CHECK(
980 value->type == EC_CONFIG_TYPE_STRING &&
981 !strcmp(value->string, "toto"),
982 "unexpected string value");
984 list = ec_config_list();
988 subconfig = ec_config_dict();
989 if (subconfig == NULL)
992 ret = ec_config_dict_set(subconfig, "my_int", ec_config_i64(1));
993 testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
994 value = ec_config_dict_get(subconfig, "my_int");
995 testres |= EC_TEST_CHECK(
997 value->type == EC_CONFIG_TYPE_INT64 &&
999 "unexpected int value");
1001 ret = ec_config_dict_set(subconfig, "my_int2", ec_config_i64(2));
1002 testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
1003 value = ec_config_dict_get(subconfig, "my_int2");
1004 testres |= EC_TEST_CHECK(
1006 value->type == EC_CONFIG_TYPE_INT64 &&
1008 "unexpected int value");
1010 testres |= EC_TEST_CHECK(
1011 ec_config_validate(subconfig, sch_dict) == 0,
1012 "cannot validate subconfig\n");
1014 ret = ec_config_list_add(list, subconfig);
1015 subconfig = NULL; /* freed */
1016 testres |= EC_TEST_CHECK(ret == 0, "cannot add in list");
1018 subconfig = ec_config_dict();
1019 if (subconfig == NULL)
1022 ret = ec_config_dict_set(subconfig, "my_int", ec_config_i64(3));
1023 testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
1024 value = ec_config_dict_get(subconfig, "my_int");
1025 testres |= EC_TEST_CHECK(
1027 value->type == EC_CONFIG_TYPE_INT64 &&
1029 "unexpected int value");
1031 ret = ec_config_dict_set(subconfig, "my_int2", ec_config_i64(4));
1032 testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
1033 value = ec_config_dict_get(subconfig, "my_int2");
1034 testres |= EC_TEST_CHECK(
1036 value->type == EC_CONFIG_TYPE_INT64 &&
1038 "unexpected int value");
1040 testres |= EC_TEST_CHECK(
1041 ec_config_validate(subconfig, sch_dict) == 0,
1042 "cannot validate subconfig\n");
1044 ret = ec_config_list_add(list, subconfig);
1045 subconfig = NULL; /* freed */
1046 testres |= EC_TEST_CHECK(ret == 0, "cannot add in list");
1048 ret = ec_config_dict_set(config, "my_dictlist", list);
1050 testres |= EC_TEST_CHECK(ret == 0, "cannot set list");
1052 testres |= EC_TEST_CHECK(
1053 ec_config_validate(config, sch_baseconfig) == 0,
1054 "cannot validate config\n");
1056 list_ = ec_config_dict_get(config, "my_dictlist");
1057 for (config_ = ec_config_list_first(list_); config_ != NULL;
1058 config_ = ec_config_list_next(list_, config_)) {
1059 ec_config_dump(stdout, config_);
1062 ec_config_dump(stdout, config);
1064 config2 = ec_config_dup(config);
1065 testres |= EC_TEST_CHECK(config2 != NULL, "cannot duplicate config");
1066 testres |= EC_TEST_CHECK(
1067 ec_config_cmp(config, config2) == 0,
1068 "fail to compare config");
1069 ec_config_free(config2);
1072 /* remove the first element */
1073 ec_config_list_del(list_, ec_config_list_first(list_));
1074 testres |= EC_TEST_CHECK(
1075 ec_config_validate(config, sch_baseconfig) == 0,
1076 "cannot validate config\n");
1078 ec_config_dump(stdout, config);
1080 ec_config_free(list);
1081 ec_config_free(subconfig);
1082 ec_config_free(config);
1083 ec_keyval_free(dict);
1089 ec_config_free(list);
1090 ec_config_free(subconfig);
1091 ec_config_free(config);
1092 ec_config_free(config2);
1093 ec_keyval_free(dict);
1098 /* LCOV_EXCL_STOP */
1100 static struct ec_test ec_config_test = {
1102 .test = ec_config_testcase,
1105 EC_TEST_REGISTER(ec_config_test);