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); |
| 338 | yin_parse_text_element(st->xml_ctx, &args, &data, &res); |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 339 | assert_string_equal(res, "content"); |
| 340 | lydict_remove(st->ctx, "content"); |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 341 | st = reset_state(state); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 342 | LY_ARRAY_FREE(args); |
| 343 | args = NULL; |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 344 | |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 345 | data = "<elem xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">another-content</elem>"; |
| 346 | 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] | 347 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 348 | yin_parse_text_element(st->xml_ctx, &args, &data, &res); |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 349 | assert_string_equal(res, "another-content"); |
| 350 | lydict_remove(st->ctx, "another-content"); |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 351 | st = reset_state(state); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 352 | LY_ARRAY_FREE(args); |
| 353 | args = NULL; |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 354 | |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 355 | data = "<elem invalid=\"invalid\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">text</elem>"; |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 356 | 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] | 357 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 358 | ret = yin_parse_text_element(st->xml_ctx, &args, &data, &res); |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 359 | assert_int_equal(ret, LY_EVALID); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 360 | LY_ARRAY_FREE(args); |
| 361 | args = NULL; |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 362 | |
| 363 | st->finished_correctly = true; |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 364 | } |
| 365 | |
David Sedlák | d9d3a31 | 2019-06-04 09:47:10 +0200 | [diff] [blame] | 366 | static void |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 367 | test_yin_parse_import(void **state) |
| 368 | { |
| 369 | struct state *st = *state; |
| 370 | const char *prefix = NULL, *name = NULL; |
| 371 | size_t prefix_len = 0, name_len = 0; |
| 372 | LY_ERR ret = LY_SUCCESS; |
| 373 | struct lysp_import *imports = NULL; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 374 | struct yin_arg_record *args = NULL; |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 375 | |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 376 | 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] | 377 | <prefix value=\"a_mod\"/>\ |
| 378 | <revision-date date=\"2015-01-01\"/>\ |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 379 | <description><text>import description</text></description>\ |
| 380 | <reference><text>import reference</text></reference>\ |
David Sedlák | c67dcaa | 2019-06-04 14:49:05 +0200 | [diff] [blame] | 381 | </import>\ |
| 382 | \ |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 383 | <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] | 384 | <prefix value=\"a_mod\"/>\ |
| 385 | <revision-date date=\"2015-01-01\"/>\ |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 386 | </import>"; |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 387 | /* first import */ |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 388 | 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] | 389 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 390 | 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] | 391 | assert_int_equal(ret, LY_SUCCESS); |
| 392 | assert_string_equal(imports->name, "a"); |
| 393 | assert_string_equal(imports->prefix, "a_mod"); |
| 394 | assert_string_equal(imports->rev, "2015-01-01"); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 395 | assert_string_equal(imports->dsc, "import description"); |
| 396 | assert_string_equal(imports->ref, "import reference"); |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 397 | lydict_remove(st->ctx, imports->name); |
| 398 | lydict_remove(st->ctx, imports->prefix); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 399 | lydict_remove(st->ctx, imports->dsc); |
| 400 | lydict_remove(st->ctx, imports->ref); |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 401 | LY_ARRAY_FREE(imports); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 402 | LY_ARRAY_FREE(args); |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 403 | imports = NULL; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 404 | args = NULL; |
David Sedlák | c67dcaa | 2019-06-04 14:49:05 +0200 | [diff] [blame] | 405 | |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 406 | /* second invalid import */ |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 407 | 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] | 408 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 409 | 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] | 410 | assert_int_equal(ret, LY_EVALID); |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 411 | 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] | 412 | /* cleanup is supposed to be done by caller function */ |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 413 | lydict_remove(st->ctx, imports->name); |
| 414 | lydict_remove(st->ctx, imports->prefix); |
| 415 | LY_ARRAY_FREE(imports); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 416 | LY_ARRAY_FREE(args); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 417 | imports = NULL; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 418 | args = NULL; |
| 419 | |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 420 | st = reset_state(state); |
| 421 | /* import with unknown child element */ |
| 422 | data = "<import xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" module=\"a\">\ |
| 423 | <what value=\"a_mod\"/>\ |
| 424 | </import>"; |
| 425 | 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] | 426 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 427 | 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] | 428 | assert_int_equal(ret, LY_EVALID); |
| 429 | logbuf_assert("Unexpected child element \"what\" of import element."); |
| 430 | /* cleanup is supposed to be done by caller function */ |
| 431 | lydict_remove(st->ctx, imports->name); |
| 432 | LY_ARRAY_FREE(imports); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 433 | LY_ARRAY_FREE(args); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 434 | |
| 435 | st->finished_correctly = true; |
| 436 | } |
| 437 | |
| 438 | static void |
| 439 | test_yin_parse_status(void **state) |
| 440 | { |
| 441 | struct state *st = *state; |
| 442 | const char *prefix = NULL, *name = NULL; |
| 443 | size_t prefix_len = 0, name_len = 0; |
| 444 | LY_ERR ret = LY_SUCCESS; |
| 445 | uint16_t flags = 0; |
| 446 | struct lysp_ext_instance *exts; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 447 | struct yin_arg_record *args = NULL; |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 448 | |
| 449 | /* try all valid values */ |
| 450 | const char *data = "<status value=\"current\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>"; |
| 451 | 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] | 452 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 453 | ret = yin_parse_status(st->xml_ctx, &args, &data, &flags, &exts); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 454 | assert_int_equal(ret, LY_SUCCESS); |
| 455 | assert_true(flags & LYS_STATUS_CURR); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 456 | LY_ARRAY_FREE(args); |
| 457 | args = NULL; |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 458 | |
| 459 | st = reset_state(state); |
| 460 | flags = 0; |
| 461 | data = "<status value=\"deprecated\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>"; |
| 462 | 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] | 463 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 464 | ret = yin_parse_status(st->xml_ctx, &args, &data, &flags, &exts); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 465 | assert_int_equal(ret, LY_SUCCESS); |
| 466 | assert_true(flags & LYS_STATUS_DEPRC); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 467 | LY_ARRAY_FREE(args); |
| 468 | args = NULL; |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 469 | |
| 470 | st = reset_state(state); |
| 471 | flags = 0; |
| 472 | data = "<status value=\"obsolete\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>"; |
| 473 | 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] | 474 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 475 | ret = yin_parse_status(st->xml_ctx, &args, &data, &flags, &exts); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 476 | assert_int_equal(ret, LY_SUCCESS); |
| 477 | assert_true(flags & LYS_STATUS_OBSLT); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 478 | LY_ARRAY_FREE(args); |
| 479 | args = NULL; |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 480 | |
| 481 | /* duplicit definition (no reset_state() call) */ |
| 482 | data = "<status value=\"deprecated\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>"; |
| 483 | 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] | 484 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 485 | ret = yin_parse_status(st->xml_ctx, &args, &data, &flags, &exts); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 486 | assert_int_equal(ret, LY_EVALID); |
| 487 | logbuf_assert("Duplicate element \"status\". Line number 1."); |
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 | /* invalid status value */ |
| 492 | st = reset_state(state); |
| 493 | flags = 0; |
| 494 | data = "<status value=\"dunno\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>"; |
| 495 | 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] | 496 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 497 | ret = yin_parse_status(st->xml_ctx, &args, &data, &flags, &exts); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 498 | assert_int_equal(ret, LY_EVALID); |
| 499 | logbuf_assert("Invalid value \"dunno\" of \"status\". Line number 1."); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 500 | LY_ARRAY_FREE(args); |
| 501 | |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 502 | st->finished_correctly = true; |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 503 | } |
| 504 | |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 505 | static void |
| 506 | test_yin_parse_extension(void **state) |
| 507 | { |
| 508 | struct state *st = *state; |
| 509 | const char *prefix = NULL, *name = NULL; |
| 510 | size_t prefix_len = 0, name_len = 0; |
| 511 | LY_ERR ret = LY_SUCCESS; |
| 512 | struct yin_arg_record *args = NULL; |
| 513 | struct lysp_ext *exts = NULL, *iter = NULL; |
| 514 | |
| 515 | const char *data = "<extension name=\"b\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\ |
| 516 | <description><text>desc</text></description>\ |
| 517 | <reference><text>ref</text></reference>\ |
| 518 | <status value=\"deprecated\"></status>\ |
| 519 | </extension>"; |
| 520 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 521 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 522 | ret = yin_parse_extension(st->xml_ctx, &args, &data, &exts); |
| 523 | assert_int_equal(ret, LY_SUCCESS); |
| 524 | LY_ARRAY_FOR_ITER(exts, struct lysp_ext, iter) { |
| 525 | assert_string_equal(iter->name, "b"); |
| 526 | assert_string_equal(iter->dsc, "desc"); |
| 527 | assert_string_equal(iter->ref, "ref"); |
| 528 | assert_true(iter->flags & LYS_STATUS_DEPRC); |
| 529 | } |
David Sedlák | 288c147 | 2019-06-20 16:09:48 +0200 | [diff] [blame] | 530 | lydict_remove(st->ctx, "b"); |
| 531 | lydict_remove(st->ctx, "desc"); |
| 532 | lydict_remove(st->ctx, "ref"); |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 533 | LY_ARRAY_FREE(args); |
David Sedlák | 288c147 | 2019-06-20 16:09:48 +0200 | [diff] [blame] | 534 | LY_ARRAY_FREE(exts); |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 535 | st->finished_correctly = true; |
| 536 | } |
| 537 | |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 538 | int |
| 539 | main(void) |
| 540 | { |
| 541 | |
| 542 | const struct CMUnitTest tests[] = { |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 543 | 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] | 544 | cmocka_unit_test_setup_teardown(test_meta, setup_f, teardown_f), |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 545 | 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] | 546 | 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] | 547 | 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] | 548 | 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] | 549 | cmocka_unit_test_setup_teardown(test_yin_parse_extension, setup_f, teardown_f), |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 550 | cmocka_unit_test(test_yin_match_argument_name), |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 551 | }; |
| 552 | |
| 553 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 554 | } |