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