David Sedlák | b1ce3f8 | 2019-06-05 14:37:26 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file test_parser_yin.c |
| 3 | * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz> |
| 4 | * @brief unit tests for functions from parser_yin.c |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2019 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 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> |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 22 | #include <stdbool.h> |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 23 | |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 24 | #include "../../src/common.h" |
| 25 | #include "../../src/tree_schema.h" |
| 26 | #include "../../src/tree_schema_internal.h" |
| 27 | #include "../../src/parser_yin.h" |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 28 | #include "../../src/xml.h" |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 29 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 30 | struct state { |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 31 | struct ly_ctx *ctx; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 32 | struct lys_module *mod; |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 33 | struct lyxml_context *xml_ctx; |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 34 | bool finished_correctly; |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 35 | }; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 36 | |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 37 | #define BUFSIZE 1024 |
| 38 | char logbuf[BUFSIZE] = {0}; |
| 39 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
| 40 | |
| 41 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 42 | #define ENABLE_LOGGER_CHECKING 1 |
| 43 | |
| 44 | #if ENABLE_LOGGER_CHECKING |
| 45 | static void |
| 46 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 47 | { |
| 48 | (void) level; /* unused */ |
| 49 | if (store) { |
| 50 | if (path && path[0]) { |
| 51 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 52 | } else { |
| 53 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 54 | } |
| 55 | if (store > 0) { |
| 56 | --store; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | #endif |
| 61 | |
| 62 | #if ENABLE_LOGGER_CHECKING |
| 63 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 64 | #else |
| 65 | # define logbuf_assert(str) |
| 66 | #endif |
| 67 | |
| 68 | #define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \ |
| 69 | str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 70 | assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \ |
| 71 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \ |
| 72 | CLEANUP |
| 73 | |
| 74 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 75 | static int |
| 76 | setup_f(void **state) |
| 77 | { |
| 78 | struct state *st = NULL; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 79 | |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 80 | #if ENABLE_LOGGER_CHECKING |
| 81 | /* setup logger */ |
| 82 | ly_set_log_clb(logger, 1); |
| 83 | #endif |
| 84 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 85 | /* allocate state variable */ |
| 86 | (*state) = st = calloc(1, sizeof(*st)); |
| 87 | if (!st) { |
| 88 | fprintf(stderr, "Memmory allocation failed"); |
| 89 | return EXIT_FAILURE; |
| 90 | } |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 91 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 92 | /* create new libyang context */ |
| 93 | ly_ctx_new(NULL, 0, &st->ctx); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 94 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 95 | /* allocate new module */ |
| 96 | st->mod = calloc(1, sizeof(*st->mod)); |
| 97 | st->mod->ctx = st->ctx; |
| 98 | |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 99 | st->xml_ctx = calloc(1, sizeof(struct lys_parser_ctx)); |
| 100 | st->xml_ctx->ctx = st->ctx; |
| 101 | st->xml_ctx->line = 1; |
| 102 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 103 | return EXIT_SUCCESS; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static int |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 107 | teardown_f(void **state) |
| 108 | { |
| 109 | struct state *st = *(struct state **)state; |
| 110 | |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 111 | #if ENABLE_LOGGER_CHECKING |
| 112 | /* teardown logger */ |
| 113 | if (!st->finished_correctly && logbuf[0] != '\0') { |
| 114 | fprintf(stderr, "%s\n", logbuf); |
| 115 | } |
| 116 | #endif |
| 117 | |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 118 | lyxml_context_clear(st->xml_ctx); |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 119 | lys_module_free(st->mod, NULL); |
| 120 | ly_ctx_destroy(st->ctx, NULL); |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 121 | free(st->xml_ctx); |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 122 | free(st); |
| 123 | |
| 124 | return EXIT_SUCCESS; |
| 125 | } |
| 126 | |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 127 | static struct state* |
| 128 | reset_state(void **state) |
| 129 | { |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 130 | ((struct state *)*state)->finished_correctly = true; |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 131 | teardown_f(state); |
| 132 | setup_f(state); |
| 133 | |
| 134 | return *state; |
| 135 | } |
| 136 | |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 137 | void |
| 138 | logbuf_clean(void) |
| 139 | { |
| 140 | logbuf[0] = '\0'; |
| 141 | } |
| 142 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 143 | static void |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 144 | test_yin_parse_module(void **state) |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 145 | { |
| 146 | LY_ERR ret = LY_SUCCESS; |
| 147 | struct state *st = *state; |
| 148 | |
| 149 | ret = yin_parse_module(st->ctx, |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 150 | "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\ |
| 151 | name=\"example-foo\"\ |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 152 | xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\ |
| 153 | xmlns:foo=\"urn:example:foo\"\ |
| 154 | xmlns:myext=\"urn:example:extensions\">\ |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 155 | <namespace uri=\"urn:example:foo\" xmlns:myext=\"urn:example:extensions\"/>\ |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 156 | <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] | 157 | </module>", |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 158 | st->mod); |
| 159 | |
| 160 | assert_int_equal(ret, LY_SUCCESS); |
| 161 | assert_string_equal(st->mod->parsed->mod->name, "example-foo"); |
| 162 | assert_string_equal(st->mod->parsed->mod->prefix, "foo"); |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 163 | assert_string_equal(st->mod->parsed->mod->ns, "urn:example:foo"); |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 164 | |
| 165 | st = reset_state(state); |
| 166 | ret = yin_parse_module(st->ctx, |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 167 | "<module name=\"example-foo\">\ |
| 168 | <invalid-tag uri=\"urn:example:foo\"\"/>\ |
| 169 | </module>", |
| 170 | st->mod); |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 171 | assert_int_equal(ret, LY_EVALID); |
| 172 | |
| 173 | st = reset_state(state); |
| 174 | ret = yin_parse_module(st->ctx, |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 175 | "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\ |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 176 | </module>", |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 177 | st->mod); |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 178 | assert_int_equal(ret, LY_EVALID); |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 179 | logbuf_assert("Missing argument name of a module Line number 1."); |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 180 | |
| 181 | st = reset_state(state); |
| 182 | ret = yin_parse_module(st->ctx, |
| 183 | "", |
| 184 | st->mod); |
| 185 | assert_int_equal(ret, LY_EVALID); |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 186 | logbuf_assert("Invalid keyword \"(null)\", expected \"module\" or \"submodule\". Line number 1."); |
| 187 | st->finished_correctly = true; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static void |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 191 | test_yin_match_keyword(void **state) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 192 | { |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 193 | struct state *st = *state; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 194 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 195 | const char *prefix, *name; |
| 196 | struct yin_arg_record *args = NULL; |
| 197 | size_t prefix_len, name_len; |
| 198 | /* create mock yin namespace in xml context */ |
| 199 | const char *data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" />"; |
| 200 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 201 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 202 | LY_ARRAY_FREE(args); |
| 203 | |
| 204 | assert_int_equal(yin_match_keyword(st->xml_ctx, "anydatax", strlen("anydatax"), prefix, prefix_len), YANG_NONE); |
| 205 | assert_int_equal(yin_match_keyword(st->xml_ctx, "asdasd", strlen("asdasd"), prefix, prefix_len), YANG_NONE); |
| 206 | assert_int_equal(yin_match_keyword(st->xml_ctx, "", 0, prefix, prefix_len), YANG_NONE); |
| 207 | assert_int_equal(yin_match_keyword(st->xml_ctx, "anydata", strlen("anydata"), prefix, prefix_len), YANG_ANYDATA); |
| 208 | assert_int_equal(yin_match_keyword(st->xml_ctx, "anyxml", strlen("anyxml"), prefix, prefix_len), YANG_ANYXML); |
| 209 | assert_int_equal(yin_match_keyword(st->xml_ctx, "argument", strlen("argument"), prefix, prefix_len), YANG_ARGUMENT); |
| 210 | assert_int_equal(yin_match_keyword(st->xml_ctx, "augment", strlen("augment"), prefix, prefix_len), YANG_AUGMENT); |
| 211 | assert_int_equal(yin_match_keyword(st->xml_ctx, "base", strlen("base"), prefix, prefix_len), YANG_BASE); |
| 212 | assert_int_equal(yin_match_keyword(st->xml_ctx, "belongs-to", strlen("belongs-to"), prefix, prefix_len), YANG_BELONGS_TO); |
| 213 | assert_int_equal(yin_match_keyword(st->xml_ctx, "bit", strlen("bit"), prefix, prefix_len), YANG_BIT); |
| 214 | assert_int_equal(yin_match_keyword(st->xml_ctx, "case", strlen("case"), prefix, prefix_len), YANG_CASE); |
| 215 | assert_int_equal(yin_match_keyword(st->xml_ctx, "choice", strlen("choice"), prefix, prefix_len), YANG_CHOICE); |
| 216 | assert_int_equal(yin_match_keyword(st->xml_ctx, "config", strlen("config"), prefix, prefix_len), YANG_CONFIG); |
| 217 | assert_int_equal(yin_match_keyword(st->xml_ctx, "contact", strlen("contact"), prefix, prefix_len), YANG_CONTACT); |
| 218 | assert_int_equal(yin_match_keyword(st->xml_ctx, "container", strlen("container"), prefix, prefix_len), YANG_CONTAINER); |
| 219 | assert_int_equal(yin_match_keyword(st->xml_ctx, "default", strlen("default"), prefix, prefix_len), YANG_DEFAULT); |
| 220 | assert_int_equal(yin_match_keyword(st->xml_ctx, "description", strlen("description"), prefix, prefix_len), YANG_DESCRIPTION); |
| 221 | assert_int_equal(yin_match_keyword(st->xml_ctx, "deviate", strlen("deviate"), prefix, prefix_len), YANG_DEVIATE); |
| 222 | assert_int_equal(yin_match_keyword(st->xml_ctx, "deviation", strlen("deviation"), prefix, prefix_len), YANG_DEVIATION); |
| 223 | assert_int_equal(yin_match_keyword(st->xml_ctx, "enum", strlen("enum"), prefix, prefix_len), YANG_ENUM); |
| 224 | assert_int_equal(yin_match_keyword(st->xml_ctx, "error-app-tag", strlen("error-app-tag"), prefix, prefix_len), YANG_ERROR_APP_TAG); |
| 225 | assert_int_equal(yin_match_keyword(st->xml_ctx, "error-message", strlen("error-message"), prefix, prefix_len), YANG_ERROR_MESSAGE); |
| 226 | assert_int_equal(yin_match_keyword(st->xml_ctx, "extension", strlen("extension"), prefix, prefix_len), YANG_EXTENSION); |
| 227 | assert_int_equal(yin_match_keyword(st->xml_ctx, "feature", strlen("feature"), prefix, prefix_len), YANG_FEATURE); |
| 228 | assert_int_equal(yin_match_keyword(st->xml_ctx, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len), YANG_FRACTION_DIGITS); |
| 229 | assert_int_equal(yin_match_keyword(st->xml_ctx, "grouping", strlen("grouping"), prefix, prefix_len), YANG_GROUPING); |
| 230 | assert_int_equal(yin_match_keyword(st->xml_ctx, "identity", strlen("identity"), prefix, prefix_len), YANG_IDENTITY); |
| 231 | assert_int_equal(yin_match_keyword(st->xml_ctx, "if-feature", strlen("if-feature"), prefix, prefix_len), YANG_IF_FEATURE); |
| 232 | assert_int_equal(yin_match_keyword(st->xml_ctx, "import", strlen("import"), prefix, prefix_len), YANG_IMPORT); |
| 233 | assert_int_equal(yin_match_keyword(st->xml_ctx, "include", strlen("include"), prefix, prefix_len), YANG_INCLUDE); |
| 234 | assert_int_equal(yin_match_keyword(st->xml_ctx, "input", strlen("input"), prefix, prefix_len), YANG_INPUT); |
| 235 | assert_int_equal(yin_match_keyword(st->xml_ctx, "key", strlen("key"), prefix, prefix_len), YANG_KEY); |
| 236 | assert_int_equal(yin_match_keyword(st->xml_ctx, "leaf", strlen("leaf"), prefix, prefix_len), YANG_LEAF); |
| 237 | assert_int_equal(yin_match_keyword(st->xml_ctx, "leaf-list", strlen("leaf-list"), prefix, prefix_len), YANG_LEAF_LIST); |
| 238 | assert_int_equal(yin_match_keyword(st->xml_ctx, "length", strlen("length"), prefix, prefix_len), YANG_LENGTH); |
| 239 | assert_int_equal(yin_match_keyword(st->xml_ctx, "list", strlen("list"), prefix, prefix_len), YANG_LIST); |
| 240 | assert_int_equal(yin_match_keyword(st->xml_ctx, "mandatory", strlen("mandatory"), prefix, prefix_len), YANG_MANDATORY); |
| 241 | assert_int_equal(yin_match_keyword(st->xml_ctx, "max-elements", strlen("max-elements"), prefix, prefix_len), YANG_MAX_ELEMENTS); |
| 242 | assert_int_equal(yin_match_keyword(st->xml_ctx, "min-elements", strlen("min-elements"), prefix, prefix_len), YANG_MIN_ELEMENTS); |
| 243 | assert_int_equal(yin_match_keyword(st->xml_ctx, "modifier", strlen("modifier"), prefix, prefix_len), YANG_MODIFIER); |
| 244 | assert_int_equal(yin_match_keyword(st->xml_ctx, "module", strlen("module"), prefix, prefix_len), YANG_MODULE); |
| 245 | assert_int_equal(yin_match_keyword(st->xml_ctx, "must", strlen("must"), prefix, prefix_len), YANG_MUST); |
| 246 | assert_int_equal(yin_match_keyword(st->xml_ctx, "namespace", strlen("namespace"), prefix, prefix_len), YANG_NAMESPACE); |
| 247 | assert_int_equal(yin_match_keyword(st->xml_ctx, "notification", strlen("notification"), prefix, prefix_len), YANG_NOTIFICATION); |
| 248 | assert_int_equal(yin_match_keyword(st->xml_ctx, "ordered-by", strlen("ordered-by"), prefix, prefix_len), YANG_ORDERED_BY); |
| 249 | assert_int_equal(yin_match_keyword(st->xml_ctx, "organization", strlen("organization"), prefix, prefix_len), YANG_ORGANIZATION); |
| 250 | assert_int_equal(yin_match_keyword(st->xml_ctx, "output", strlen("output"), prefix, prefix_len), YANG_OUTPUT); |
| 251 | assert_int_equal(yin_match_keyword(st->xml_ctx, "path", strlen("path"), prefix, prefix_len), YANG_PATH); |
| 252 | assert_int_equal(yin_match_keyword(st->xml_ctx, "pattern", strlen("pattern"), prefix, prefix_len), YANG_PATTERN); |
| 253 | assert_int_equal(yin_match_keyword(st->xml_ctx, "position", strlen("position"), prefix, prefix_len), YANG_POSITION); |
| 254 | assert_int_equal(yin_match_keyword(st->xml_ctx, "prefix", strlen("prefix"), prefix, prefix_len), YANG_PREFIX); |
| 255 | assert_int_equal(yin_match_keyword(st->xml_ctx, "presence", strlen("presence"), prefix, prefix_len), YANG_PRESENCE); |
| 256 | assert_int_equal(yin_match_keyword(st->xml_ctx, "range", strlen("range"), prefix, prefix_len), YANG_RANGE); |
| 257 | assert_int_equal(yin_match_keyword(st->xml_ctx, "reference", strlen("reference"), prefix, prefix_len), YANG_REFERENCE); |
| 258 | assert_int_equal(yin_match_keyword(st->xml_ctx, "refine", strlen("refine"), prefix, prefix_len), YANG_REFINE); |
| 259 | assert_int_equal(yin_match_keyword(st->xml_ctx, "require-instance", strlen("require-instance"), prefix, prefix_len), YANG_REQUIRE_INSTANCE); |
| 260 | assert_int_equal(yin_match_keyword(st->xml_ctx, "revision", strlen("revision"), prefix, prefix_len), YANG_REVISION); |
| 261 | assert_int_equal(yin_match_keyword(st->xml_ctx, "revision-date", strlen("revision-date"), prefix, prefix_len), YANG_REVISION_DATE); |
| 262 | assert_int_equal(yin_match_keyword(st->xml_ctx, "rpc", strlen("rpc"), prefix, prefix_len), YANG_RPC); |
| 263 | assert_int_equal(yin_match_keyword(st->xml_ctx, "status", strlen("status"), prefix, prefix_len), YANG_STATUS); |
| 264 | assert_int_equal(yin_match_keyword(st->xml_ctx, "submodule", strlen("submodule"), prefix, prefix_len), YANG_SUBMODULE); |
| 265 | assert_int_equal(yin_match_keyword(st->xml_ctx, "type", strlen("type"), prefix, prefix_len), YANG_TYPE); |
| 266 | assert_int_equal(yin_match_keyword(st->xml_ctx, "typedef", strlen("typedef"), prefix, prefix_len), YANG_TYPEDEF); |
| 267 | assert_int_equal(yin_match_keyword(st->xml_ctx, "unique", strlen("unique"), prefix, prefix_len), YANG_UNIQUE); |
| 268 | assert_int_equal(yin_match_keyword(st->xml_ctx, "units", strlen("units"), prefix, prefix_len), YANG_UNITS); |
| 269 | assert_int_equal(yin_match_keyword(st->xml_ctx, "uses", strlen("uses"), prefix, prefix_len), YANG_USES); |
| 270 | assert_int_equal(yin_match_keyword(st->xml_ctx, "value", strlen("value"), prefix, prefix_len), YANG_VALUE); |
| 271 | assert_int_equal(yin_match_keyword(st->xml_ctx, "when", strlen("when"), prefix, prefix_len), YANG_WHEN); |
| 272 | assert_int_equal(yin_match_keyword(st->xml_ctx, "yang-version", strlen("yang-version"), prefix, prefix_len), YANG_YANG_VERSION); |
| 273 | assert_int_equal(yin_match_keyword(st->xml_ctx, "yin-element", strlen("yin-element"), prefix, prefix_len), YANG_YIN_ELEMENT); |
| 274 | |
| 275 | st->finished_correctly = true; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 276 | } |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 277 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 278 | static void |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 279 | test_yin_match_argument_name(void **state) |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 280 | { |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 281 | (void)state; /* unused */ |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 282 | |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 283 | assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN); |
| 284 | assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN); |
| 285 | assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN); |
| 286 | assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION); |
| 287 | assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE); |
| 288 | assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE); |
| 289 | assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME); |
| 290 | assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG); |
| 291 | assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE); |
| 292 | assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT); |
| 293 | assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI); |
| 294 | assert_int_equal(yin_match_argument_name("value", 5), YIN_ARG_VALUE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 295 | } |
| 296 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 297 | static void |
| 298 | test_meta(void **state) |
| 299 | { |
| 300 | LY_ERR ret = LY_SUCCESS; |
| 301 | struct state *st = *state; |
| 302 | |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 303 | ret = yin_parse_module(st->ctx,"<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\ |
| 304 | name=\"example-foo\">\ |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 305 | <organization xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"><text>organization...</text></organization>\ |
| 306 | <contact><text>contact...</text></contact>\ |
| 307 | <description><text>description...</text></description>\ |
| 308 | <reference><text>reference...</text></reference>\ |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 309 | </module>", st->mod); |
| 310 | |
| 311 | assert_int_equal(ret, LY_SUCCESS); |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 312 | assert_string_equal(st->mod->parsed->mod->org, "organization..."); |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 313 | assert_string_equal(st->mod->parsed->mod->contact, "contact..."); |
| 314 | assert_string_equal(st->mod->parsed->mod->dsc, "description..."); |
| 315 | assert_string_equal(st->mod->parsed->mod->ref, "reference..."); |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 316 | |
| 317 | st = reset_state(state); |
| 318 | ret = yin_parse_module(st->ctx,"<module name=\"example-foo\">\ |
| 319 | <organization test=\"invalid-argument\">organization...</organization>\ |
| 320 | </module>", st->mod); |
| 321 | assert_int_equal(ret, LY_EVALID); |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 322 | |
| 323 | st->finished_correctly = true; |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 324 | } |
| 325 | |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 326 | static void |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 327 | test_yin_parse_text_element(void **state) |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 328 | { |
| 329 | struct state *st = *state; |
| 330 | const char *res = NULL, *prefix = NULL, *name = NULL; |
| 331 | size_t prefix_len = 0, name_len = 0; |
| 332 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 333 | struct yin_arg_record *args = NULL; |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 334 | |
| 335 | const char *data = "<elem>content</elem>"; |
| 336 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 337 | yin_load_attributes(st->xml_ctx, &data, &args); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 338 | ret = yin_parse_text_element(st->xml_ctx, &args, &data, &res); |
David Sedlák | 4af339b | 2019-06-24 15:37:29 +0200 | [diff] [blame] | 339 | assert_int_equal(st->xml_ctx->status, LYXML_END); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 340 | assert_int_equal(ret, LY_SUCCESS); |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 341 | assert_string_equal(res, "content"); |
| 342 | lydict_remove(st->ctx, "content"); |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 343 | st = reset_state(state); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 344 | LY_ARRAY_FREE(args); |
| 345 | args = NULL; |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 346 | |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 347 | data = "<elem xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">another-content</elem>"; |
| 348 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 349 | yin_load_attributes(st->xml_ctx, &data, &args); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 350 | ret = yin_parse_text_element(st->xml_ctx, &args, &data, &res); |
David Sedlák | 4af339b | 2019-06-24 15:37:29 +0200 | [diff] [blame] | 351 | assert_int_equal(st->xml_ctx->status, LYXML_END); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 352 | assert_int_equal(ret, LY_SUCCESS); |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 353 | assert_string_equal(res, "another-content"); |
| 354 | lydict_remove(st->ctx, "another-content"); |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 355 | st = reset_state(state); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 356 | LY_ARRAY_FREE(args); |
| 357 | args = NULL; |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 358 | |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 359 | data = "<elem unknown=\"unknown\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">text</elem>"; |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 360 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 361 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 362 | ret = yin_parse_text_element(st->xml_ctx, &args, &data, &res); |
David Sedlák | 4af339b | 2019-06-24 15:37:29 +0200 | [diff] [blame] | 363 | assert_int_equal(st->xml_ctx->status, LYXML_END); |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 364 | assert_int_equal(ret, LY_SUCCESS); |
| 365 | assert_string_equal(res, "text"); |
| 366 | lydict_remove(st->ctx, "text"); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 367 | LY_ARRAY_FREE(args); |
| 368 | args = NULL; |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 369 | |
| 370 | st->finished_correctly = true; |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 371 | } |
| 372 | |
David Sedlák | d9d3a31 | 2019-06-04 09:47:10 +0200 | [diff] [blame] | 373 | static void |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 374 | test_yin_parse_import(void **state) |
| 375 | { |
| 376 | struct state *st = *state; |
| 377 | const char *prefix = NULL, *name = NULL; |
| 378 | size_t prefix_len = 0, name_len = 0; |
| 379 | LY_ERR ret = LY_SUCCESS; |
| 380 | struct lysp_import *imports = NULL; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 381 | struct yin_arg_record *args = NULL; |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 382 | |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 383 | const char *data = "<import xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" module=\"a\">\ |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 384 | <prefix value=\"a_mod\"/>\ |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 385 | <revision-date date=\"2015-01-01\"></revision-date>\ |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 386 | <description><text>import description</text></description>\ |
| 387 | <reference><text>import reference</text></reference>\ |
David Sedlák | c67dcaa | 2019-06-04 14:49:05 +0200 | [diff] [blame] | 388 | </import>\ |
| 389 | \ |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 390 | <import xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" module=\"a\">\ |
David Sedlák | c67dcaa | 2019-06-04 14:49:05 +0200 | [diff] [blame] | 391 | <prefix value=\"a_mod\"/>\ |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 392 | <revision-date date=\"2015-01-01\" />\ |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 393 | </import>"; |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 394 | /* first import */ |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 395 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 396 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 397 | ret = yin_parse_import(st->xml_ctx, &args, "b-mod", &data, &imports); |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 398 | assert_int_equal(ret, LY_SUCCESS); |
| 399 | assert_string_equal(imports->name, "a"); |
| 400 | assert_string_equal(imports->prefix, "a_mod"); |
| 401 | assert_string_equal(imports->rev, "2015-01-01"); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 402 | assert_string_equal(imports->dsc, "import description"); |
| 403 | assert_string_equal(imports->ref, "import reference"); |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 404 | lydict_remove(st->ctx, imports->name); |
| 405 | lydict_remove(st->ctx, imports->prefix); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 406 | lydict_remove(st->ctx, imports->dsc); |
| 407 | lydict_remove(st->ctx, imports->ref); |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 408 | LY_ARRAY_FREE(imports); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 409 | LY_ARRAY_FREE(args); |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 410 | imports = NULL; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 411 | args = NULL; |
David Sedlák | c67dcaa | 2019-06-04 14:49:05 +0200 | [diff] [blame] | 412 | |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 413 | /* second invalid import */ |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 414 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 415 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 416 | ret = yin_parse_import(st->xml_ctx, &args, "a_mod", &data, &imports); |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 417 | assert_int_equal(ret, LY_EVALID); |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 418 | logbuf_assert("Prefix \"a_mod\" already used as module prefix. Line number 1."); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 419 | /* cleanup is supposed to be done by caller function */ |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 420 | lydict_remove(st->ctx, imports->name); |
| 421 | lydict_remove(st->ctx, imports->prefix); |
| 422 | LY_ARRAY_FREE(imports); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 423 | LY_ARRAY_FREE(args); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 424 | imports = NULL; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 425 | args = NULL; |
| 426 | |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 427 | st = reset_state(state); |
| 428 | /* import with unknown child element */ |
| 429 | data = "<import xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" module=\"a\">\ |
| 430 | <what value=\"a_mod\"/>\ |
| 431 | </import>"; |
| 432 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 433 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 434 | ret = yin_parse_import(st->xml_ctx, &args, "invalid_mod", &data, &imports); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 435 | assert_int_equal(ret, LY_EVALID); |
| 436 | logbuf_assert("Unexpected child element \"what\" of import element."); |
| 437 | /* cleanup is supposed to be done by caller function */ |
| 438 | lydict_remove(st->ctx, imports->name); |
| 439 | LY_ARRAY_FREE(imports); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 440 | LY_ARRAY_FREE(args); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 441 | |
| 442 | st->finished_correctly = true; |
| 443 | } |
| 444 | |
| 445 | static void |
| 446 | test_yin_parse_status(void **state) |
| 447 | { |
| 448 | struct state *st = *state; |
| 449 | const char *prefix = NULL, *name = NULL; |
| 450 | size_t prefix_len = 0, name_len = 0; |
| 451 | LY_ERR ret = LY_SUCCESS; |
| 452 | uint16_t flags = 0; |
| 453 | struct lysp_ext_instance *exts; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 454 | struct yin_arg_record *args = NULL; |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 455 | |
| 456 | /* try all valid values */ |
| 457 | const char *data = "<status value=\"current\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>"; |
| 458 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 459 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 460 | ret = yin_parse_status(st->xml_ctx, &args, &data, &flags, &exts); |
David Sedlák | 4af339b | 2019-06-24 15:37:29 +0200 | [diff] [blame] | 461 | assert_int_equal(st->xml_ctx->status, LYXML_END); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 462 | assert_int_equal(ret, LY_SUCCESS); |
| 463 | assert_true(flags & LYS_STATUS_CURR); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 464 | LY_ARRAY_FREE(args); |
| 465 | args = NULL; |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 466 | |
| 467 | st = reset_state(state); |
| 468 | flags = 0; |
| 469 | data = "<status value=\"deprecated\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>"; |
| 470 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 471 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 472 | ret = yin_parse_status(st->xml_ctx, &args, &data, &flags, &exts); |
David Sedlák | 4af339b | 2019-06-24 15:37:29 +0200 | [diff] [blame] | 473 | assert_int_equal(st->xml_ctx->status, LYXML_END); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 474 | assert_int_equal(ret, LY_SUCCESS); |
| 475 | assert_true(flags & LYS_STATUS_DEPRC); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 476 | LY_ARRAY_FREE(args); |
| 477 | args = NULL; |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 478 | |
| 479 | st = reset_state(state); |
| 480 | flags = 0; |
| 481 | data = "<status value=\"obsolete\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>"; |
| 482 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 483 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 484 | ret = yin_parse_status(st->xml_ctx, &args, &data, &flags, &exts); |
David Sedlák | 4af339b | 2019-06-24 15:37:29 +0200 | [diff] [blame] | 485 | assert_int_equal(st->xml_ctx->status, LYXML_END); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 486 | assert_int_equal(ret, LY_SUCCESS); |
| 487 | assert_true(flags & LYS_STATUS_OBSLT); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 488 | LY_ARRAY_FREE(args); |
| 489 | args = NULL; |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 490 | |
| 491 | /* duplicit definition (no reset_state() call) */ |
| 492 | data = "<status value=\"deprecated\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>"; |
| 493 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 494 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 495 | ret = yin_parse_status(st->xml_ctx, &args, &data, &flags, &exts); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 496 | assert_int_equal(ret, LY_EVALID); |
| 497 | logbuf_assert("Duplicate element \"status\". Line number 1."); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 498 | LY_ARRAY_FREE(args); |
| 499 | args = NULL; |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 500 | |
| 501 | /* invalid status value */ |
| 502 | st = reset_state(state); |
| 503 | flags = 0; |
| 504 | data = "<status value=\"dunno\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>"; |
| 505 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 506 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 507 | ret = yin_parse_status(st->xml_ctx, &args, &data, &flags, &exts); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 508 | assert_int_equal(ret, LY_EVALID); |
| 509 | logbuf_assert("Invalid value \"dunno\" of \"status\". Line number 1."); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 510 | LY_ARRAY_FREE(args); |
| 511 | |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 512 | st->finished_correctly = true; |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 513 | } |
| 514 | |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 515 | static void |
| 516 | test_yin_parse_extension(void **state) |
| 517 | { |
| 518 | struct state *st = *state; |
| 519 | const char *prefix = NULL, *name = NULL; |
| 520 | size_t prefix_len = 0, name_len = 0; |
| 521 | LY_ERR ret = LY_SUCCESS; |
| 522 | struct yin_arg_record *args = NULL; |
| 523 | struct lysp_ext *exts = NULL, *iter = NULL; |
| 524 | |
| 525 | const char *data = "<extension name=\"b\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\ |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 526 | <argument name=\"argname\"></argument>\ |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 527 | <description><text>desc</text></description>\ |
| 528 | <reference><text>ref</text></reference>\ |
| 529 | <status value=\"deprecated\"></status>\ |
| 530 | </extension>"; |
| 531 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 532 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 533 | ret = yin_parse_extension(st->xml_ctx, &args, &data, &exts); |
David Sedlák | 4af339b | 2019-06-24 15:37:29 +0200 | [diff] [blame] | 534 | assert_int_equal(st->xml_ctx->status, LYXML_END); |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 535 | assert_int_equal(ret, LY_SUCCESS); |
| 536 | LY_ARRAY_FOR_ITER(exts, struct lysp_ext, iter) { |
| 537 | assert_string_equal(iter->name, "b"); |
| 538 | assert_string_equal(iter->dsc, "desc"); |
| 539 | assert_string_equal(iter->ref, "ref"); |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 540 | assert_string_equal(iter->argument, "argname"); |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 541 | assert_true(iter->flags & LYS_STATUS_DEPRC); |
| 542 | } |
David Sedlák | 288c147 | 2019-06-20 16:09:48 +0200 | [diff] [blame] | 543 | lydict_remove(st->ctx, "b"); |
| 544 | lydict_remove(st->ctx, "desc"); |
| 545 | lydict_remove(st->ctx, "ref"); |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 546 | lydict_remove(st->ctx, "argname"); |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 547 | LY_ARRAY_FREE(args); |
David Sedlák | 288c147 | 2019-06-20 16:09:48 +0200 | [diff] [blame] | 548 | LY_ARRAY_FREE(exts); |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 549 | st->finished_correctly = true; |
| 550 | } |
| 551 | |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 552 | static void |
| 553 | test_yin_parse_yin_element_element(void **state) |
| 554 | { |
| 555 | struct state *st = *state; |
| 556 | const char *prefix = NULL, *name = NULL; |
| 557 | size_t prefix_len = 0, name_len = 0; |
| 558 | LY_ERR ret = LY_SUCCESS; |
| 559 | uint16_t flags = 0; |
| 560 | struct lysp_ext *exts; |
| 561 | struct yin_arg_record *args = NULL; |
| 562 | |
| 563 | /* try all valid values */ |
| 564 | const char *data = "<yin-element value=\"true\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>"; |
| 565 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 566 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 567 | ret = yin_parse_yin_element_element(st->xml_ctx, &args, &data, &flags, &exts); |
David Sedlák | 4af339b | 2019-06-24 15:37:29 +0200 | [diff] [blame] | 568 | assert_int_equal(st->xml_ctx->status, LYXML_END); |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 569 | assert_int_equal(ret, LY_SUCCESS); |
| 570 | assert_true(flags & LYS_YINELEM_TRUE); |
| 571 | LY_ARRAY_FREE(args); |
| 572 | args = NULL; |
| 573 | |
| 574 | st = reset_state(state); |
| 575 | flags = 0; |
| 576 | data = "<yin-element value=\"false\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>"; |
| 577 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 578 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 579 | ret = yin_parse_yin_element_element(st->xml_ctx, &args, &data, &flags, &exts); |
David Sedlák | 4af339b | 2019-06-24 15:37:29 +0200 | [diff] [blame] | 580 | assert_int_equal(st->xml_ctx->status, LYXML_END); |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 581 | assert_int_equal(ret, LY_SUCCESS); |
| 582 | assert_true(flags & LYS_YINELEM_FALSE); |
| 583 | LY_ARRAY_FREE(args); |
| 584 | args = NULL; |
| 585 | |
| 586 | /* invalid value */ |
| 587 | st = reset_state(state); |
| 588 | flags = 0; |
| 589 | data = "<yin-element value=\"invalid\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>"; |
| 590 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 591 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 592 | ret = yin_parse_yin_element_element(st->xml_ctx, &args, &data, &flags, &exts); |
| 593 | assert_int_equal(ret, LY_EVALID); |
| 594 | LY_ARRAY_FREE(args); |
| 595 | args = NULL; |
| 596 | |
| 597 | st->finished_correctly = true; |
| 598 | } |
| 599 | |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 600 | int |
| 601 | main(void) |
| 602 | { |
| 603 | |
| 604 | const struct CMUnitTest tests[] = { |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 605 | cmocka_unit_test_setup_teardown(test_yin_parse_module, setup_f, teardown_f), |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 606 | cmocka_unit_test_setup_teardown(test_meta, setup_f, teardown_f), |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 607 | cmocka_unit_test_setup_teardown(test_yin_parse_text_element, setup_f, teardown_f), |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 608 | cmocka_unit_test_setup_teardown(test_yin_parse_import, setup_f, teardown_f), |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 609 | cmocka_unit_test_setup_teardown(test_yin_parse_status, setup_f, teardown_f), |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 610 | cmocka_unit_test_setup_teardown(test_yin_match_keyword, setup_f, teardown_f), |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 611 | cmocka_unit_test_setup_teardown(test_yin_parse_extension, setup_f, teardown_f), |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 612 | cmocka_unit_test_setup_teardown(test_yin_parse_yin_element_element, setup_f, teardown_f), |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 613 | cmocka_unit_test(test_yin_match_argument_name), |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 614 | }; |
| 615 | |
| 616 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 617 | } |