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, |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 71 | "<module name=\"example-foo\"\ |
| 72 | xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\ |
| 73 | xmlns:foo=\"urn:example:foo\"\ |
| 74 | xmlns:myext=\"urn:example:extensions\">\ |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame^] | 75 | <namespace uri=\"urn:example:foo\" xmlns:myext=\"urn:example:extensions\"/>\ |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 76 | <prefix value=\"foo\"/>\ |
| 77 | </module>", |
| 78 | st->mod); |
| 79 | |
| 80 | assert_int_equal(ret, LY_SUCCESS); |
| 81 | assert_string_equal(st->mod->parsed->mod->name, "example-foo"); |
| 82 | assert_string_equal(st->mod->parsed->mod->prefix, "foo"); |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame^] | 83 | assert_string_equal(st->mod->parsed->mod->ns, "urn:example:foo"); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static void |
| 87 | test_match_keyword(void **state) |
| 88 | { |
| 89 | (void)state; /* unused */ |
| 90 | |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 91 | assert_int_equal(match_keyword("anydatax", strlen("anydatax"), 0), YANG_NONE); |
| 92 | assert_int_equal(match_keyword("asdasd", strlen("asdasd"), 0), YANG_NONE); |
| 93 | assert_int_equal(match_keyword("", 0, 0), YANG_NONE); |
| 94 | assert_int_equal(match_keyword("anydata", strlen("anydata"), 0), YANG_ANYDATA); |
| 95 | assert_int_equal(match_keyword("anyxml", strlen("anyxml"), 0), YANG_ANYXML); |
| 96 | assert_int_equal(match_keyword("argument", strlen("argument"), 0), YANG_ARGUMENT); |
| 97 | assert_int_equal(match_keyword("augment", strlen("augment"), 0), YANG_AUGMENT); |
| 98 | assert_int_equal(match_keyword("base", strlen("base"), 0), YANG_BASE); |
| 99 | assert_int_equal(match_keyword("belongs-to", strlen("belongs-to"), 0), YANG_BELONGS_TO); |
| 100 | assert_int_equal(match_keyword("bit", strlen("bit"), 0), YANG_BIT); |
| 101 | assert_int_equal(match_keyword("case", strlen("case"), 0), YANG_CASE); |
| 102 | assert_int_equal(match_keyword("choice", strlen("choice"), 0), YANG_CHOICE); |
| 103 | assert_int_equal(match_keyword("config", strlen("config"), 0), YANG_CONFIG); |
| 104 | assert_int_equal(match_keyword("contact", strlen("contact"), 0), YANG_CONTACT); |
| 105 | assert_int_equal(match_keyword("container", strlen("container"), 0), YANG_CONTAINER); |
| 106 | assert_int_equal(match_keyword("default", strlen("default"), 0), YANG_DEFAULT); |
| 107 | assert_int_equal(match_keyword("description", strlen("description"), 0), YANG_DESCRIPTION); |
| 108 | assert_int_equal(match_keyword("deviate", strlen("deviate"), 0), YANG_DEVIATE); |
| 109 | assert_int_equal(match_keyword("deviation", strlen("deviation"), 0), YANG_DEVIATION); |
| 110 | assert_int_equal(match_keyword("enum", strlen("enum"), 0), YANG_ENUM); |
| 111 | assert_int_equal(match_keyword("error-app-tag", strlen("error-app-tag"), 0), YANG_ERROR_APP_TAG); |
| 112 | assert_int_equal(match_keyword("error-message", strlen("error-message"), 0), YANG_ERROR_MESSAGE); |
| 113 | assert_int_equal(match_keyword("extension", strlen("extension"), 0), YANG_EXTENSION); |
| 114 | assert_int_equal(match_keyword("feature", strlen("feature"), 0), YANG_FEATURE); |
| 115 | assert_int_equal(match_keyword("fraction-digits", strlen("fraction-digits"), 0), YANG_FRACTION_DIGITS); |
| 116 | assert_int_equal(match_keyword("grouping", strlen("grouping"), 0), YANG_GROUPING); |
| 117 | assert_int_equal(match_keyword("identity", strlen("identity"), 0), YANG_IDENTITY); |
| 118 | assert_int_equal(match_keyword("if-feature", strlen("if-feature"), 0), YANG_IF_FEATURE); |
| 119 | assert_int_equal(match_keyword("import", strlen("import"), 0), YANG_IMPORT); |
| 120 | assert_int_equal(match_keyword("include", strlen("include"), 0), YANG_INCLUDE); |
| 121 | assert_int_equal(match_keyword("input", strlen("input"), 0), YANG_INPUT); |
| 122 | assert_int_equal(match_keyword("key", strlen("key"), 0), YANG_KEY); |
| 123 | assert_int_equal(match_keyword("leaf", strlen("leaf"), 0), YANG_LEAF); |
| 124 | assert_int_equal(match_keyword("leaf-list", strlen("leaf-list"), 0), YANG_LEAF_LIST); |
| 125 | assert_int_equal(match_keyword("length", strlen("length"), 0), YANG_LENGTH); |
| 126 | assert_int_equal(match_keyword("list", strlen("list"), 0), YANG_LIST); |
| 127 | assert_int_equal(match_keyword("mandatory", strlen("mandatory"), 0), YANG_MANDATORY); |
| 128 | assert_int_equal(match_keyword("max-elements", strlen("max-elements"), 0), YANG_MAX_ELEMENTS); |
| 129 | assert_int_equal(match_keyword("min-elements", strlen("min-elements"), 0), YANG_MIN_ELEMENTS); |
| 130 | assert_int_equal(match_keyword("modifier", strlen("modifier"), 0), YANG_MODIFIER); |
| 131 | assert_int_equal(match_keyword("module", strlen("module"), 0), YANG_MODULE); |
| 132 | assert_int_equal(match_keyword("must", strlen("must"), 0), YANG_MUST); |
| 133 | assert_int_equal(match_keyword("namespace", strlen("namespace"), 0), YANG_NAMESPACE); |
| 134 | assert_int_equal(match_keyword("notification", strlen("notification"), 0), YANG_NOTIFICATION); |
| 135 | assert_int_equal(match_keyword("ordered-by", strlen("ordered-by"), 0), YANG_ORDERED_BY); |
| 136 | assert_int_equal(match_keyword("organization", strlen("organization"), 0), YANG_ORGANIZATION); |
| 137 | assert_int_equal(match_keyword("output", strlen("output"), 0), YANG_OUTPUT); |
| 138 | assert_int_equal(match_keyword("path", strlen("path"), 0), YANG_PATH); |
| 139 | assert_int_equal(match_keyword("pattern", strlen("pattern"), 0), YANG_PATTERN); |
| 140 | assert_int_equal(match_keyword("position", strlen("position"), 0), YANG_POSITION); |
| 141 | assert_int_equal(match_keyword("prefix", strlen("prefix"), 0), YANG_PREFIX); |
| 142 | assert_int_equal(match_keyword("presence", strlen("presence"), 0), YANG_PRESENCE); |
| 143 | assert_int_equal(match_keyword("range", strlen("range"), 0), YANG_RANGE); |
| 144 | assert_int_equal(match_keyword("reference", strlen("reference"), 0), YANG_REFERENCE); |
| 145 | assert_int_equal(match_keyword("refine", strlen("refine"), 0), YANG_REFINE); |
| 146 | assert_int_equal(match_keyword("require-instance", strlen("require-instance"), 0), YANG_REQUIRE_INSTANCE); |
| 147 | assert_int_equal(match_keyword("revision", strlen("revision"), 0), YANG_REVISION); |
| 148 | assert_int_equal(match_keyword("revision-date", strlen("revision-date"), 0), YANG_REVISION_DATE); |
| 149 | assert_int_equal(match_keyword("rpc", strlen("rpc"), 0), YANG_RPC); |
| 150 | assert_int_equal(match_keyword("status", strlen("status"), 0), YANG_STATUS); |
| 151 | assert_int_equal(match_keyword("submodule", strlen("submodule"), 0), YANG_SUBMODULE); |
| 152 | assert_int_equal(match_keyword("type", strlen("type"), 0), YANG_TYPE); |
| 153 | assert_int_equal(match_keyword("typedef", strlen("typedef"), 0), YANG_TYPEDEF); |
| 154 | assert_int_equal(match_keyword("unique", strlen("unique"), 0), YANG_UNIQUE); |
| 155 | assert_int_equal(match_keyword("units", strlen("units"), 0), YANG_UNITS); |
| 156 | assert_int_equal(match_keyword("uses", strlen("uses"), 0), YANG_USES); |
| 157 | assert_int_equal(match_keyword("value", strlen("value"), 0), YANG_VALUE); |
| 158 | assert_int_equal(match_keyword("when", strlen("when"), 0), YANG_WHEN); |
| 159 | assert_int_equal(match_keyword("yang-version", strlen("yang-version"), 0), YANG_YANG_VERSION); |
| 160 | assert_int_equal(match_keyword("yin-element", strlen("yin-element"), 0), YANG_YIN_ELEMENT); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 161 | } |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 162 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 163 | static void |
| 164 | test_match_argument(void **state) |
| 165 | { |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 166 | (void)state; /* unused */ |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 167 | |
| 168 | assert_int_equal(match_argument_name("", 5), YIN_ARG_NONE); |
| 169 | assert_int_equal(match_argument_name("qwertyasd", 5), YIN_ARG_NONE); |
| 170 | assert_int_equal(match_argument_name("conditionasd", 8), YIN_ARG_NONE); |
| 171 | assert_int_equal(match_argument_name("condition", 9), YIN_ARG_CONDITION); |
| 172 | assert_int_equal(match_argument_name("date", 4), YIN_ARG_DATE); |
| 173 | assert_int_equal(match_argument_name("module", 6), YIN_ARG_MODULE); |
| 174 | assert_int_equal(match_argument_name("name", 4), YIN_ARG_NAME); |
| 175 | assert_int_equal(match_argument_name("tag", 3), YIN_ARG_TAG); |
| 176 | assert_int_equal(match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE); |
| 177 | assert_int_equal(match_argument_name("text", 4), YIN_ARG_TEXT); |
| 178 | assert_int_equal(match_argument_name("uri", 3), YIN_ARG_URI); |
| 179 | assert_int_equal(match_argument_name("value", 5), YIN_ARG_VALUE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 180 | } |
| 181 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 182 | static void |
| 183 | test_meta(void **state) |
| 184 | { |
| 185 | LY_ERR ret = LY_SUCCESS; |
| 186 | struct state *st = *state; |
| 187 | |
| 188 | ret = yin_parse_module(st->ctx,"<module name=\"example-foo\">\ |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 189 | <organization xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">organiz<ation...</organization>\ |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 190 | <contact>contact...</contact>\ |
| 191 | <description>description...</description>\ |
| 192 | <reference>reference...</reference>\ |
| 193 | </module>", st->mod); |
| 194 | |
| 195 | assert_int_equal(ret, LY_SUCCESS); |
David Sedlák | 0daba9a | 2019-03-22 14:07:49 +0100 | [diff] [blame] | 196 | assert_string_equal(st->mod->parsed->mod->org, "organiz<ation..."); |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 197 | assert_string_equal(st->mod->parsed->mod->contact, "contact..."); |
| 198 | assert_string_equal(st->mod->parsed->mod->dsc, "description..."); |
| 199 | assert_string_equal(st->mod->parsed->mod->ref, "reference..."); |
| 200 | } |
| 201 | |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 202 | int |
| 203 | main(void) |
| 204 | { |
| 205 | |
| 206 | const struct CMUnitTest tests[] = { |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 207 | cmocka_unit_test_setup_teardown(test_parse, setup_f, teardown_f), |
| 208 | cmocka_unit_test_setup_teardown(test_meta, setup_f, teardown_f), |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 209 | cmocka_unit_test(test_match_keyword), |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 210 | cmocka_unit_test(test_match_argument), |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 214 | } |