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 == NULL || value2 == NULL) {
431 if (value1->type != value2->type)
434 switch (value1->type) {
435 case EC_CONFIG_TYPE_BOOL:
436 if (value1->boolean == value2->boolean)
438 case EC_CONFIG_TYPE_INT64:
439 if (value1->i64 == value2->i64)
441 case EC_CONFIG_TYPE_UINT64:
442 if (value1->u64 == value2->u64)
444 case EC_CONFIG_TYPE_STRING:
445 if (!strcmp(value1->string, value2->string))
447 case EC_CONFIG_TYPE_NODE:
448 if (value1->node == value2->node)
450 case EC_CONFIG_TYPE_LIST:
451 return ec_config_list_cmp(&value1->list, &value2->list);
452 case EC_CONFIG_TYPE_DICT:
453 return ec_config_dict_cmp(value1->dict, value2->dict);
462 ec_config_list_validate(const struct ec_config_list *list,
463 const struct ec_config_schema *sch)
465 const struct ec_config *value;
467 TAILQ_FOREACH(value, list, next) {
468 if (value->type != sch->type) {
473 if (value->type == EC_CONFIG_TYPE_LIST) {
474 if (ec_config_list_validate(&value->list,
477 } else if (value->type == EC_CONFIG_TYPE_DICT) {
478 if (ec_config_dict_validate(value->dict,
479 sch->subschema, sch->subschema_len) < 0)
488 ec_config_dict_validate(const struct ec_keyval *dict,
489 const struct ec_config_schema *schema,
492 const struct ec_config *value;
493 struct ec_keyval_iter *iter = NULL;
494 const struct ec_config_schema *sch;
497 for (iter = ec_keyval_iter(dict);
498 ec_keyval_iter_valid(iter);
499 ec_keyval_iter_next(iter)) {
501 key = ec_keyval_iter_get_key(iter);
502 value = ec_keyval_iter_get_val(iter);
503 sch = ec_config_schema_lookup(schema, schema_len,
510 if (value->type == EC_CONFIG_TYPE_LIST) {
511 if (ec_config_list_validate(&value->list,
514 } else if (value->type == EC_CONFIG_TYPE_DICT) {
515 if (ec_config_dict_validate(value->dict,
516 sch->subschema, sch->subschema_len) < 0)
521 ec_keyval_iter_free(iter);
525 ec_keyval_iter_free(iter);
530 ec_config_validate(const struct ec_config *dict,
531 const struct ec_config_schema *schema,
534 if (dict->type != EC_CONFIG_TYPE_DICT || schema == NULL) {
539 if (ec_config_dict_validate(dict->dict, schema, schema_len) < 0)
549 ec_config_dict_get(const struct ec_config *config, const char *key)
551 if (config == NULL) {
556 if (config->type != EC_CONFIG_TYPE_DICT) {
561 return ec_keyval_get(config->dict, key);
565 ec_config_list_first(struct ec_config *list)
567 if (list == NULL || list->type != EC_CONFIG_TYPE_LIST) {
572 return TAILQ_FIRST(&list->list);
576 ec_config_list_next(struct ec_config *list, struct ec_config *config)
579 return TAILQ_NEXT(config, next);
582 /* value is consumed */
583 int ec_config_dict_set(struct ec_config *config, const char *key,
584 struct ec_config *value)
586 void (*free_cb)(struct ec_config *) = ec_config_free;
588 if (config == NULL || key == NULL || value == NULL) {
592 if (config->type != EC_CONFIG_TYPE_DICT) {
597 return ec_keyval_set(config->dict, key, value,
598 (void (*)(void *))free_cb);
601 ec_config_free(value);
605 int ec_config_dict_del(struct ec_config *config, const char *key)
607 if (config == NULL || key == NULL) {
611 if (config->type != EC_CONFIG_TYPE_DICT) {
616 return ec_keyval_del(config->dict, key);
619 /* value is consumed */
621 ec_config_list_add(struct ec_config *list,
622 struct ec_config *value)
624 if (list == NULL || list->type != EC_CONFIG_TYPE_LIST) {
629 TAILQ_INSERT_TAIL(&list->list, value, next);
634 ec_config_free(value);
638 int ec_config_list_del(struct ec_config *list, struct ec_config *config)
640 if (list == NULL || list->type != EC_CONFIG_TYPE_LIST) {
645 TAILQ_REMOVE(&list->list, config, next);
646 ec_config_free(config);
650 static struct ec_config *
651 ec_config_list_dup(const struct ec_config_list *list)
653 struct ec_config *dup = NULL, *v, *value;
655 dup = ec_config_list();
659 TAILQ_FOREACH(v, list, next) {
660 value = ec_config_dup(v);
663 if (ec_config_list_add(dup, value) < 0)
674 static struct ec_config *
675 ec_config_dict_dup(const struct ec_keyval *dict)
677 struct ec_config *dup = NULL, *value;
678 struct ec_keyval_iter *iter = NULL;
681 dup = ec_config_dict();
685 for (iter = ec_keyval_iter(dict);
686 ec_keyval_iter_valid(iter);
687 ec_keyval_iter_next(iter)) {
688 key = ec_keyval_iter_get_key(iter);
689 value = ec_config_dup(ec_keyval_iter_get_val(iter));
692 if (ec_config_dict_set(dup, key, value) < 0)
695 ec_keyval_iter_free(iter);
701 ec_keyval_iter_free(iter);
706 ec_config_dup(const struct ec_config *config)
708 if (config == NULL) {
713 switch (config->type) {
714 case EC_CONFIG_TYPE_BOOL:
715 return ec_config_bool(config->boolean);
716 case EC_CONFIG_TYPE_INT64:
717 return ec_config_i64(config->i64);
718 case EC_CONFIG_TYPE_UINT64:
719 return ec_config_u64(config->u64);
720 case EC_CONFIG_TYPE_STRING:
721 return ec_config_string(config->string);
722 case EC_CONFIG_TYPE_NODE:
723 return ec_config_node(ec_node_clone(config->node));
724 case EC_CONFIG_TYPE_LIST:
725 return ec_config_list_dup(&config->list);
726 case EC_CONFIG_TYPE_DICT:
727 return ec_config_dict_dup(config->dict);
737 ec_config_list_dump(FILE *out, const struct ec_config_list *list,
740 const struct ec_config *v;
742 fprintf(out, "%*s" "type=list:\n", (int)indent * 4, "");
744 TAILQ_FOREACH(v, list, next) {
745 if (__ec_config_dump(out, NULL, v, indent + 1) < 0)
753 ec_config_dict_dump(FILE *out, const struct ec_keyval *dict,
756 const struct ec_config *value;
757 struct ec_keyval_iter *iter;
760 fprintf(out, "%*s" "type=dict:\n", (int)indent * 4, "");
761 for (iter = ec_keyval_iter(dict);
762 ec_keyval_iter_valid(iter);
763 ec_keyval_iter_next(iter)) {
764 key = ec_keyval_iter_get_key(iter);
765 value = ec_keyval_iter_get_val(iter);
766 if (__ec_config_dump(out, key, value, indent + 1) < 0)
769 ec_keyval_iter_free(iter);
773 ec_keyval_iter_free(iter);
778 __ec_config_dump(FILE *out, const char *key, const struct ec_config *value,
781 char *val_str = NULL;
783 switch (value->type) {
784 case EC_CONFIG_TYPE_BOOL:
786 ec_asprintf(&val_str, "true");
788 ec_asprintf(&val_str, "false");
790 case EC_CONFIG_TYPE_INT64:
791 ec_asprintf(&val_str, "%"PRIu64, value->u64);
793 case EC_CONFIG_TYPE_UINT64:
794 ec_asprintf(&val_str, "%"PRIi64, value->i64);
796 case EC_CONFIG_TYPE_STRING:
797 ec_asprintf(&val_str, "%s", value->string);
799 case EC_CONFIG_TYPE_NODE:
800 ec_asprintf(&val_str, "%p", value->node);
802 case EC_CONFIG_TYPE_LIST:
803 return ec_config_list_dump(out, &value->list, indent);
804 case EC_CONFIG_TYPE_DICT:
805 return ec_config_dict_dump(out, value->dict, indent);
811 /* errno is already set on error */
815 fprintf(out, "%*s" "%s%s%stype=%s val=%s\n", (int)indent * 4, "",
819 ec_config_type_str(value->type), val_str);
830 ec_config_dump(FILE *out, const struct ec_config *config)
832 fprintf(out, "------------------- config dump:\n");
834 if (config == NULL) {
835 fprintf(out, "no config\n");
839 if (__ec_config_dump(out, NULL, config, 0) < 0)
840 fprintf(out, "error while dumping\n");
843 /* LCOV_EXCL_START */
844 static const struct ec_config_schema sch_intlist_elt[] = {
846 .desc = "This is a description for int",
847 .type = EC_CONFIG_TYPE_INT64,
851 static const struct ec_config_schema sch_dict[] = {
854 .desc = "This is a description for int",
855 .type = EC_CONFIG_TYPE_INT64,
859 .desc = "This is a description for int2",
860 .type = EC_CONFIG_TYPE_INT64,
864 static const struct ec_config_schema sch_dictlist_elt[] = {
866 .desc = "This is a description for dict",
867 .type = EC_CONFIG_TYPE_DICT,
868 .subschema = sch_dict,
869 .subschema_len = EC_COUNT_OF(sch_dict),
873 static const struct ec_config_schema sch_baseconfig[] = {
876 .desc = "This is a description for bool",
877 .type = EC_CONFIG_TYPE_BOOL,
881 .desc = "This is a description for int",
882 .type = EC_CONFIG_TYPE_INT64,
886 .desc = "This is a description for string",
887 .type = EC_CONFIG_TYPE_STRING,
891 .desc = "This is a description for node",
892 .type = EC_CONFIG_TYPE_NODE,
896 .desc = "This is a description for list",
897 .type = EC_CONFIG_TYPE_LIST,
898 .subschema = sch_intlist_elt,
899 .subschema_len = EC_COUNT_OF(sch_intlist_elt),
902 .key = "my_dictlist",
903 .desc = "This is a description for list",
904 .type = EC_CONFIG_TYPE_LIST,
905 .subschema = sch_dictlist_elt,
906 .subschema_len = EC_COUNT_OF(sch_dictlist_elt),
910 static int ec_config_testcase(void)
912 struct ec_node *node = NULL;
913 struct ec_keyval *dict = NULL;
914 const struct ec_config *value = NULL;
915 struct ec_config *config = NULL, *config2 = NULL;
916 struct ec_config *list = NULL, *subconfig = NULL;
917 struct ec_config *list_, *config_;
921 node = ec_node("empty", EC_NO_ID);
925 if (ec_config_schema_validate(sch_baseconfig,
926 EC_COUNT_OF(sch_baseconfig)) < 0) {
927 EC_LOG(EC_LOG_ERR, "invalid config schema\n");
931 ec_config_schema_dump(stdout, sch_baseconfig,
932 EC_COUNT_OF(sch_baseconfig));
934 config = ec_config_dict();
938 ret = ec_config_dict_set(config, "my_bool", ec_config_bool(true));
939 testres |= EC_TEST_CHECK(ret == 0, "cannot set boolean");
940 value = ec_config_dict_get(config, "my_bool");
941 testres |= EC_TEST_CHECK(
943 value->type == EC_CONFIG_TYPE_BOOL &&
944 value->boolean == true,
945 "unexpected boolean value");
947 ret = ec_config_dict_set(config, "my_int", ec_config_i64(1234));
948 testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
949 value = ec_config_dict_get(config, "my_int");
950 testres |= EC_TEST_CHECK(
952 value->type == EC_CONFIG_TYPE_INT64 &&
954 "unexpected int value");
956 testres |= EC_TEST_CHECK(
957 ec_config_validate(config, sch_baseconfig,
958 EC_COUNT_OF(sch_baseconfig)) == 0,
959 "cannot validate config\n");
961 ret = ec_config_dict_set(config, "my_string", ec_config_string("toto"));
962 testres |= EC_TEST_CHECK(ret == 0, "cannot set string");
963 value = ec_config_dict_get(config, "my_string");
964 testres |= EC_TEST_CHECK(
966 value->type == EC_CONFIG_TYPE_STRING &&
967 !strcmp(value->string, "toto"),
968 "unexpected string value");
970 list = ec_config_list();
974 subconfig = ec_config_dict();
975 if (subconfig == NULL)
978 ret = ec_config_dict_set(subconfig, "my_int", ec_config_i64(1));
979 testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
980 value = ec_config_dict_get(subconfig, "my_int");
981 testres |= EC_TEST_CHECK(
983 value->type == EC_CONFIG_TYPE_INT64 &&
985 "unexpected int value");
987 ret = ec_config_dict_set(subconfig, "my_int2", ec_config_i64(2));
988 testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
989 value = ec_config_dict_get(subconfig, "my_int2");
990 testres |= EC_TEST_CHECK(
992 value->type == EC_CONFIG_TYPE_INT64 &&
994 "unexpected int value");
996 testres |= EC_TEST_CHECK(
997 ec_config_validate(subconfig, sch_dict,
998 EC_COUNT_OF(sch_dict)) == 0,
999 "cannot validate subconfig\n");
1001 ret = ec_config_list_add(list, subconfig);
1002 subconfig = NULL; /* freed */
1003 testres |= EC_TEST_CHECK(ret == 0, "cannot add in list");
1005 subconfig = ec_config_dict();
1006 if (subconfig == NULL)
1009 ret = ec_config_dict_set(subconfig, "my_int", ec_config_i64(3));
1010 testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
1011 value = ec_config_dict_get(subconfig, "my_int");
1012 testres |= EC_TEST_CHECK(
1014 value->type == EC_CONFIG_TYPE_INT64 &&
1016 "unexpected int value");
1018 ret = ec_config_dict_set(subconfig, "my_int2", ec_config_i64(4));
1019 testres |= EC_TEST_CHECK(ret == 0, "cannot set int");
1020 value = ec_config_dict_get(subconfig, "my_int2");
1021 testres |= EC_TEST_CHECK(
1023 value->type == EC_CONFIG_TYPE_INT64 &&
1025 "unexpected int value");
1027 testres |= EC_TEST_CHECK(
1028 ec_config_validate(subconfig, sch_dict,
1029 EC_COUNT_OF(sch_dict)) == 0,
1030 "cannot validate subconfig\n");
1032 ret = ec_config_list_add(list, subconfig);
1033 subconfig = NULL; /* freed */
1034 testres |= EC_TEST_CHECK(ret == 0, "cannot add in list");
1036 ret = ec_config_dict_set(config, "my_dictlist", list);
1038 testres |= EC_TEST_CHECK(ret == 0, "cannot set list");
1040 testres |= EC_TEST_CHECK(
1041 ec_config_validate(config, sch_baseconfig,
1042 EC_COUNT_OF(sch_baseconfig)) == 0,
1043 "cannot validate config\n");
1045 list_ = ec_config_dict_get(config, "my_dictlist");
1046 for (config_ = ec_config_list_first(list_); config_ != NULL;
1047 config_ = ec_config_list_next(list_, config_)) {
1048 ec_config_dump(stdout, config_);
1051 ec_config_dump(stdout, config);
1053 config2 = ec_config_dup(config);
1054 testres |= EC_TEST_CHECK(config2 != NULL, "cannot duplicate config");
1055 testres |= EC_TEST_CHECK(
1056 ec_config_cmp(config, config2) == 0,
1057 "fail to compare config");
1058 ec_config_free(config2);
1061 /* remove the first element */
1062 ec_config_list_del(list_, ec_config_list_first(list_));
1063 testres |= EC_TEST_CHECK(
1064 ec_config_validate(config, sch_baseconfig,
1065 EC_COUNT_OF(sch_baseconfig)) == 0,
1066 "cannot validate config\n");
1068 ec_config_dump(stdout, config);
1070 ec_config_free(list);
1071 ec_config_free(subconfig);
1072 ec_config_free(config);
1073 ec_keyval_free(dict);
1079 ec_config_free(list);
1080 ec_config_free(subconfig);
1081 ec_config_free(config);
1082 ec_config_free(config2);
1083 ec_keyval_free(dict);
1088 /* LCOV_EXCL_STOP */
1090 static struct ec_test ec_config_test = {
1092 .test = ec_config_testcase,
1095 EC_TEST_REGISTER(ec_config_test);