David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1 | #include "../../src/common.c" |
| 2 | #include "../../src/set.c" |
| 3 | #include "../../src/log.c" |
| 4 | #include "../../src/hash_table.c" |
| 5 | #include "../../src/xpath.c" |
| 6 | #include "../../src/parser_yang.c" |
| 7 | #include "../../src/context.c" |
| 8 | #include "../../src/tree_schema_helpers.c" |
| 9 | #include "../../src/tree_schema_free.c" |
| 10 | #include "../../src/tree_schema_compile.c" |
| 11 | #include "../../src/tree_schema.c" |
| 12 | #include "../../src/xml.c" |
| 13 | #include "../../src/parser_yin.c" |
| 14 | |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 15 | #include <stdarg.h> |
| 16 | #include <stddef.h> |
| 17 | #include <setjmp.h> |
| 18 | #include <cmocka.h> |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include "libyang.h" |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 24 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 25 | struct state { |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 26 | struct ly_ctx *ctx; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 27 | struct lys_module *mod; |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 28 | }; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 29 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 30 | static int |
| 31 | setup_f(void **state) |
| 32 | { |
| 33 | struct state *st = NULL; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 34 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 35 | /* allocate state variable */ |
| 36 | (*state) = st = calloc(1, sizeof(*st)); |
| 37 | if (!st) { |
| 38 | fprintf(stderr, "Memmory allocation failed"); |
| 39 | return EXIT_FAILURE; |
| 40 | } |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 41 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 42 | /* create new libyang context */ |
| 43 | ly_ctx_new(NULL, 0, &st->ctx); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 44 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 45 | /* allocate new module */ |
| 46 | st->mod = calloc(1, sizeof(*st->mod)); |
| 47 | st->mod->ctx = st->ctx; |
| 48 | |
| 49 | return EXIT_SUCCESS; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static int |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 53 | teardown_f(void **state) |
| 54 | { |
| 55 | struct state *st = *(struct state **)state; |
| 56 | |
| 57 | lys_module_free(st->mod, NULL); |
| 58 | ly_ctx_destroy(st->ctx, NULL); |
| 59 | free(st); |
| 60 | |
| 61 | return EXIT_SUCCESS; |
| 62 | } |
| 63 | |
| 64 | static void |
| 65 | test_parse(void **state) |
| 66 | { |
| 67 | LY_ERR ret = LY_SUCCESS; |
| 68 | struct state *st = *state; |
| 69 | |
| 70 | ret = yin_parse_module(st->ctx, |
| 71 | "<module name=\"example-foo\">\ |
| 72 | <namespace uri=\"urn:example:foo\"/>\ |
| 73 | <prefix value=\"foo\"/>\ |
| 74 | </module>", |
| 75 | st->mod); |
| 76 | |
| 77 | assert_int_equal(ret, LY_SUCCESS); |
| 78 | assert_string_equal(st->mod->parsed->mod->name, "example-foo"); |
| 79 | assert_string_equal(st->mod->parsed->mod->prefix, "foo"); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static void |
| 83 | test_match_keyword(void **state) |
| 84 | { |
| 85 | (void)state; /* unused */ |
| 86 | |
David Sedlák | 9220612 | 2019-02-15 12:39:17 +0100 | [diff] [blame] | 87 | assert_int_equal(match_keyword("anydatax", strlen("anydatax")), YANG_NONE); |
| 88 | assert_int_equal(match_keyword("asdasd", strlen("asdasd")), YANG_NONE); |
| 89 | assert_int_equal(match_keyword("", 0), YANG_NONE); |
| 90 | assert_int_equal(match_keyword("anydata", strlen("anydata")), YANG_ANYDATA); |
| 91 | assert_int_equal(match_keyword("anyxml", strlen("anyxml")), YANG_ANYXML); |
| 92 | assert_int_equal(match_keyword("argument", strlen("argument")), YANG_ARGUMENT); |
| 93 | assert_int_equal(match_keyword("augment", strlen("augment")), YANG_AUGMENT); |
| 94 | assert_int_equal(match_keyword("base", strlen("base")), YANG_BASE); |
| 95 | assert_int_equal(match_keyword("belongs-to", strlen("belongs-to")), YANG_BELONGS_TO); |
| 96 | assert_int_equal(match_keyword("bit", strlen("bit")), YANG_BIT); |
| 97 | assert_int_equal(match_keyword("case", strlen("case")), YANG_CASE); |
| 98 | assert_int_equal(match_keyword("choice", strlen("choice")), YANG_CHOICE); |
| 99 | assert_int_equal(match_keyword("config", strlen("config")), YANG_CONFIG); |
| 100 | assert_int_equal(match_keyword("contact", strlen("contact")), YANG_CONTACT); |
| 101 | assert_int_equal(match_keyword("container", strlen("container")), YANG_CONTAINER); |
| 102 | assert_int_equal(match_keyword("default", strlen("default")), YANG_DEFAULT); |
| 103 | assert_int_equal(match_keyword("description", strlen("description")), YANG_DESCRIPTION); |
| 104 | assert_int_equal(match_keyword("deviate", strlen("deviate")), YANG_DEVIATE); |
| 105 | assert_int_equal(match_keyword("deviation", strlen("deviation")), YANG_DEVIATION); |
| 106 | assert_int_equal(match_keyword("enum", strlen("enum")), YANG_ENUM); |
| 107 | assert_int_equal(match_keyword("error-app-tag", strlen("error-app-tag")), YANG_ERROR_APP_TAG); |
| 108 | assert_int_equal(match_keyword("error-message", strlen("error-message")), YANG_ERROR_MESSAGE); |
| 109 | assert_int_equal(match_keyword("extension", strlen("extension")), YANG_EXTENSION); |
| 110 | assert_int_equal(match_keyword("feature", strlen("feature")), YANG_FEATURE); |
| 111 | assert_int_equal(match_keyword("fraction-digits", strlen("fraction-digits")), YANG_FRACTION_DIGITS); |
| 112 | assert_int_equal(match_keyword("grouping", strlen("grouping")), YANG_GROUPING); |
| 113 | assert_int_equal(match_keyword("identity", strlen("identity")), YANG_IDENTITY); |
| 114 | assert_int_equal(match_keyword("if-feature", strlen("if-feature")), YANG_IF_FEATURE); |
| 115 | assert_int_equal(match_keyword("import", strlen("import")), YANG_IMPORT); |
| 116 | assert_int_equal(match_keyword("include", strlen("include")), YANG_INCLUDE); |
| 117 | assert_int_equal(match_keyword("input", strlen("input")), YANG_INPUT); |
| 118 | assert_int_equal(match_keyword("key", strlen("key")), YANG_KEY); |
| 119 | assert_int_equal(match_keyword("leaf", strlen("leaf")), YANG_LEAF); |
| 120 | assert_int_equal(match_keyword("leaf-list", strlen("leaf-list")), YANG_LEAF_LIST); |
| 121 | assert_int_equal(match_keyword("length", strlen("length")), YANG_LENGTH); |
| 122 | assert_int_equal(match_keyword("list", strlen("list")), YANG_LIST); |
| 123 | assert_int_equal(match_keyword("mandatory", strlen("mandatory")), YANG_MANDATORY); |
| 124 | assert_int_equal(match_keyword("max-elements", strlen("max-elements")), YANG_MAX_ELEMENTS); |
| 125 | assert_int_equal(match_keyword("min-elements", strlen("min-elements")), YANG_MIN_ELEMENTS); |
| 126 | assert_int_equal(match_keyword("modifier", strlen("modifier")), YANG_MODIFIER); |
| 127 | assert_int_equal(match_keyword("module", strlen("module")), YANG_MODULE); |
| 128 | assert_int_equal(match_keyword("must", strlen("must")), YANG_MUST); |
| 129 | assert_int_equal(match_keyword("namespace", strlen("namespace")), YANG_NAMESPACE); |
| 130 | assert_int_equal(match_keyword("notification", strlen("notification")), YANG_NOTIFICATION); |
| 131 | assert_int_equal(match_keyword("ordered-by", strlen("ordered-by")), YANG_ORDERED_BY); |
| 132 | assert_int_equal(match_keyword("organization", strlen("organization")), YANG_ORGANIZATION); |
| 133 | assert_int_equal(match_keyword("output", strlen("output")), YANG_OUTPUT); |
| 134 | assert_int_equal(match_keyword("path", strlen("path")), YANG_PATH); |
| 135 | assert_int_equal(match_keyword("pattern", strlen("pattern")), YANG_PATTERN); |
| 136 | assert_int_equal(match_keyword("position", strlen("position")), YANG_POSITION); |
| 137 | assert_int_equal(match_keyword("prefix", strlen("prefix")), YANG_PREFIX); |
| 138 | assert_int_equal(match_keyword("presence", strlen("presence")), YANG_PRESENCE); |
| 139 | assert_int_equal(match_keyword("range", strlen("range")), YANG_RANGE); |
| 140 | assert_int_equal(match_keyword("reference", strlen("reference")), YANG_REFERENCE); |
| 141 | assert_int_equal(match_keyword("refine", strlen("refine")), YANG_REFINE); |
| 142 | assert_int_equal(match_keyword("require-instance", strlen("require-instance")), YANG_REQUIRE_INSTANCE); |
| 143 | assert_int_equal(match_keyword("revision", strlen("revision")), YANG_REVISION); |
| 144 | assert_int_equal(match_keyword("revision-date", strlen("revision-date")), YANG_REVISION_DATE); |
| 145 | assert_int_equal(match_keyword("rpc", strlen("rpc")), YANG_RPC); |
| 146 | assert_int_equal(match_keyword("status", strlen("status")), YANG_STATUS); |
| 147 | assert_int_equal(match_keyword("submodule", strlen("submodule")), YANG_SUBMODULE); |
| 148 | assert_int_equal(match_keyword("type", strlen("type")), YANG_TYPE); |
| 149 | assert_int_equal(match_keyword("typedef", strlen("typedef")), YANG_TYPEDEF); |
| 150 | assert_int_equal(match_keyword("unique", strlen("unique")), YANG_UNIQUE); |
| 151 | assert_int_equal(match_keyword("units", strlen("units")), YANG_UNITS); |
| 152 | assert_int_equal(match_keyword("uses", strlen("uses")), YANG_USES); |
| 153 | assert_int_equal(match_keyword("value", strlen("value")), YANG_VALUE); |
| 154 | assert_int_equal(match_keyword("when", strlen("when")), YANG_WHEN); |
| 155 | assert_int_equal(match_keyword("yang-version", strlen("yang-version")), YANG_YANG_VERSION); |
| 156 | assert_int_equal(match_keyword("yin-element", strlen("yin-element")), YANG_YIN_ELEMENT); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 157 | } |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 158 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 159 | static void |
| 160 | test_match_argument(void **state) |
| 161 | { |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 162 | (void)state; /* unused */ |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 163 | |
| 164 | assert_int_equal(match_argument_name("", 5), YIN_ARG_NONE); |
| 165 | assert_int_equal(match_argument_name("qwertyasd", 5), YIN_ARG_NONE); |
| 166 | assert_int_equal(match_argument_name("conditionasd", 8), YIN_ARG_NONE); |
| 167 | assert_int_equal(match_argument_name("condition", 9), YIN_ARG_CONDITION); |
| 168 | assert_int_equal(match_argument_name("date", 4), YIN_ARG_DATE); |
| 169 | assert_int_equal(match_argument_name("module", 6), YIN_ARG_MODULE); |
| 170 | assert_int_equal(match_argument_name("name", 4), YIN_ARG_NAME); |
| 171 | assert_int_equal(match_argument_name("tag", 3), YIN_ARG_TAG); |
| 172 | assert_int_equal(match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE); |
| 173 | assert_int_equal(match_argument_name("text", 4), YIN_ARG_TEXT); |
| 174 | assert_int_equal(match_argument_name("uri", 3), YIN_ARG_URI); |
| 175 | assert_int_equal(match_argument_name("value", 5), YIN_ARG_VALUE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 176 | } |
| 177 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 178 | static void |
| 179 | test_meta(void **state) |
| 180 | { |
| 181 | LY_ERR ret = LY_SUCCESS; |
| 182 | struct state *st = *state; |
| 183 | |
| 184 | ret = yin_parse_module(st->ctx,"<module name=\"example-foo\">\ |
| 185 | <organization>organization...</organization>\ |
| 186 | <contact>contact...</contact>\ |
| 187 | <description>description...</description>\ |
| 188 | <reference>reference...</reference>\ |
| 189 | </module>", st->mod); |
| 190 | |
| 191 | assert_int_equal(ret, LY_SUCCESS); |
| 192 | assert_string_equal(st->mod->parsed->mod->org, "organization..."); |
| 193 | assert_string_equal(st->mod->parsed->mod->contact, "contact..."); |
| 194 | assert_string_equal(st->mod->parsed->mod->dsc, "description..."); |
| 195 | assert_string_equal(st->mod->parsed->mod->ref, "reference..."); |
| 196 | } |
| 197 | |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 198 | int |
| 199 | main(void) |
| 200 | { |
| 201 | |
| 202 | const struct CMUnitTest tests[] = { |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 203 | cmocka_unit_test_setup_teardown(test_parse, setup_f, teardown_f), |
| 204 | cmocka_unit_test_setup_teardown(test_meta, setup_f, teardown_f), |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 205 | cmocka_unit_test(test_match_keyword), |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 206 | cmocka_unit_test(test_match_argument), |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 210 | } |