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 | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 30 | /* prototypes of static functions */ |
| 31 | void lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext); |
David Sedlák | 986cb41 | 2019-07-04 13:10:11 +0200 | [diff] [blame] | 32 | void lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 33 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 34 | struct state { |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 35 | struct ly_ctx *ctx; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 36 | struct lys_module *mod; |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 37 | struct lysp_module *lysp_mod; |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 38 | struct lyxml_context *xml_ctx; |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 39 | bool finished_correctly; |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 40 | }; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 41 | |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 42 | #define BUFSIZE 1024 |
| 43 | char logbuf[BUFSIZE] = {0}; |
| 44 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
| 45 | |
| 46 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 47 | #define ENABLE_LOGGER_CHECKING 1 |
| 48 | |
| 49 | #if ENABLE_LOGGER_CHECKING |
| 50 | static void |
| 51 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 52 | { |
| 53 | (void) level; /* unused */ |
| 54 | if (store) { |
| 55 | if (path && path[0]) { |
| 56 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 57 | } else { |
| 58 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 59 | } |
| 60 | if (store > 0) { |
| 61 | --store; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | #endif |
| 66 | |
| 67 | #if ENABLE_LOGGER_CHECKING |
| 68 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 69 | #else |
| 70 | # define logbuf_assert(str) |
| 71 | #endif |
| 72 | |
| 73 | #define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \ |
| 74 | str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 75 | assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \ |
| 76 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \ |
| 77 | CLEANUP |
| 78 | |
| 79 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 80 | static int |
| 81 | setup_f(void **state) |
| 82 | { |
| 83 | struct state *st = NULL; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 84 | |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 85 | #if ENABLE_LOGGER_CHECKING |
| 86 | /* setup logger */ |
| 87 | ly_set_log_clb(logger, 1); |
| 88 | #endif |
| 89 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 90 | /* allocate state variable */ |
| 91 | (*state) = st = calloc(1, sizeof(*st)); |
| 92 | if (!st) { |
| 93 | fprintf(stderr, "Memmory allocation failed"); |
| 94 | return EXIT_FAILURE; |
| 95 | } |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 96 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 97 | /* create new libyang context */ |
| 98 | ly_ctx_new(NULL, 0, &st->ctx); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 99 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 100 | /* allocate new module */ |
| 101 | st->mod = calloc(1, sizeof(*st->mod)); |
| 102 | st->mod->ctx = st->ctx; |
| 103 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 104 | /* allocate new parsed module */ |
| 105 | st->lysp_mod = calloc(1, sizeof(*st->lysp_mod)); |
| 106 | st->lysp_mod->mod = calloc(1, sizeof(*st->lysp_mod->mod)); |
| 107 | st->lysp_mod->mod->ctx = st->ctx; |
| 108 | |
| 109 | /* allocate parser context */ |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 110 | st->xml_ctx = calloc(1, sizeof(struct lys_parser_ctx)); |
| 111 | st->xml_ctx->ctx = st->ctx; |
| 112 | st->xml_ctx->line = 1; |
| 113 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 114 | return EXIT_SUCCESS; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static int |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 118 | teardown_f(void **state) |
| 119 | { |
| 120 | struct state *st = *(struct state **)state; |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 121 | struct lys_module *temp; |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 122 | |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 123 | #if ENABLE_LOGGER_CHECKING |
| 124 | /* teardown logger */ |
| 125 | if (!st->finished_correctly && logbuf[0] != '\0') { |
| 126 | fprintf(stderr, "%s\n", logbuf); |
| 127 | } |
| 128 | #endif |
| 129 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 130 | temp = st->lysp_mod->mod; |
| 131 | |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 132 | lyxml_context_clear(st->xml_ctx); |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 133 | lys_module_free(st->mod, NULL); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 134 | lysp_module_free(st->lysp_mod); |
| 135 | lys_module_free(temp, NULL); |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 136 | ly_ctx_destroy(st->ctx, NULL); |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 137 | free(st->xml_ctx); |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 138 | free(st); |
| 139 | |
| 140 | return EXIT_SUCCESS; |
| 141 | } |
| 142 | |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 143 | static struct state* |
| 144 | reset_state(void **state) |
| 145 | { |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 146 | ((struct state *)*state)->finished_correctly = true; |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 147 | logbuf[0] = '\0'; |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 148 | teardown_f(state); |
| 149 | setup_f(state); |
| 150 | |
| 151 | return *state; |
| 152 | } |
| 153 | |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 154 | void |
| 155 | logbuf_clean(void) |
| 156 | { |
| 157 | logbuf[0] = '\0'; |
| 158 | } |
| 159 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 160 | static void |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 161 | test_yin_parse_module(void **state) |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 162 | { |
| 163 | LY_ERR ret = LY_SUCCESS; |
| 164 | struct state *st = *state; |
| 165 | |
| 166 | ret = yin_parse_module(st->ctx, |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 167 | "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\ |
| 168 | name=\"example-foo\"\ |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 169 | xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\ |
| 170 | xmlns:foo=\"urn:example:foo\"\ |
| 171 | xmlns:myext=\"urn:example:extensions\">\ |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 172 | <namespace uri=\"urn:example:foo\" xmlns:myext=\"urn:example:extensions\"/>\ |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 173 | <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] | 174 | </module>", |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 175 | st->mod); |
| 176 | |
| 177 | assert_int_equal(ret, LY_SUCCESS); |
| 178 | assert_string_equal(st->mod->parsed->mod->name, "example-foo"); |
| 179 | assert_string_equal(st->mod->parsed->mod->prefix, "foo"); |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 180 | assert_string_equal(st->mod->parsed->mod->ns, "urn:example:foo"); |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 181 | |
| 182 | st = reset_state(state); |
| 183 | ret = yin_parse_module(st->ctx, |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 184 | "<module name=\"example-foo\">\ |
| 185 | <invalid-tag uri=\"urn:example:foo\"\"/>\ |
| 186 | </module>", |
| 187 | st->mod); |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 188 | assert_int_equal(ret, LY_EVALID); |
| 189 | |
| 190 | st = reset_state(state); |
| 191 | ret = yin_parse_module(st->ctx, |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 192 | "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\ |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 193 | </module>", |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 194 | st->mod); |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 195 | assert_int_equal(ret, LY_EVALID); |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 196 | logbuf_assert("Missing mandatory attribute \"name\" of module element. Line number 1."); |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 197 | |
| 198 | st = reset_state(state); |
| 199 | ret = yin_parse_module(st->ctx, |
| 200 | "", |
| 201 | st->mod); |
| 202 | assert_int_equal(ret, LY_EVALID); |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 203 | logbuf_assert("Invalid keyword \"(null)\", expected \"module\" or \"submodule\". Line number 1."); |
| 204 | st->finished_correctly = true; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static void |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 208 | test_yin_match_keyword(void **state) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 209 | { |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 210 | struct state *st = *state; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 211 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 212 | const char *prefix, *name; |
| 213 | struct yin_arg_record *args = NULL; |
| 214 | size_t prefix_len, name_len; |
| 215 | /* create mock yin namespace in xml context */ |
| 216 | const char *data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" />"; |
| 217 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 218 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 219 | LY_ARRAY_FREE(args); |
| 220 | |
| 221 | assert_int_equal(yin_match_keyword(st->xml_ctx, "anydatax", strlen("anydatax"), prefix, prefix_len), YANG_NONE); |
| 222 | assert_int_equal(yin_match_keyword(st->xml_ctx, "asdasd", strlen("asdasd"), prefix, prefix_len), YANG_NONE); |
| 223 | assert_int_equal(yin_match_keyword(st->xml_ctx, "", 0, prefix, prefix_len), YANG_NONE); |
| 224 | assert_int_equal(yin_match_keyword(st->xml_ctx, "anydata", strlen("anydata"), prefix, prefix_len), YANG_ANYDATA); |
| 225 | assert_int_equal(yin_match_keyword(st->xml_ctx, "anyxml", strlen("anyxml"), prefix, prefix_len), YANG_ANYXML); |
| 226 | assert_int_equal(yin_match_keyword(st->xml_ctx, "argument", strlen("argument"), prefix, prefix_len), YANG_ARGUMENT); |
| 227 | assert_int_equal(yin_match_keyword(st->xml_ctx, "augment", strlen("augment"), prefix, prefix_len), YANG_AUGMENT); |
| 228 | assert_int_equal(yin_match_keyword(st->xml_ctx, "base", strlen("base"), prefix, prefix_len), YANG_BASE); |
| 229 | assert_int_equal(yin_match_keyword(st->xml_ctx, "belongs-to", strlen("belongs-to"), prefix, prefix_len), YANG_BELONGS_TO); |
| 230 | assert_int_equal(yin_match_keyword(st->xml_ctx, "bit", strlen("bit"), prefix, prefix_len), YANG_BIT); |
| 231 | assert_int_equal(yin_match_keyword(st->xml_ctx, "case", strlen("case"), prefix, prefix_len), YANG_CASE); |
| 232 | assert_int_equal(yin_match_keyword(st->xml_ctx, "choice", strlen("choice"), prefix, prefix_len), YANG_CHOICE); |
| 233 | assert_int_equal(yin_match_keyword(st->xml_ctx, "config", strlen("config"), prefix, prefix_len), YANG_CONFIG); |
| 234 | assert_int_equal(yin_match_keyword(st->xml_ctx, "contact", strlen("contact"), prefix, prefix_len), YANG_CONTACT); |
| 235 | assert_int_equal(yin_match_keyword(st->xml_ctx, "container", strlen("container"), prefix, prefix_len), YANG_CONTAINER); |
| 236 | assert_int_equal(yin_match_keyword(st->xml_ctx, "default", strlen("default"), prefix, prefix_len), YANG_DEFAULT); |
| 237 | assert_int_equal(yin_match_keyword(st->xml_ctx, "description", strlen("description"), prefix, prefix_len), YANG_DESCRIPTION); |
| 238 | assert_int_equal(yin_match_keyword(st->xml_ctx, "deviate", strlen("deviate"), prefix, prefix_len), YANG_DEVIATE); |
| 239 | assert_int_equal(yin_match_keyword(st->xml_ctx, "deviation", strlen("deviation"), prefix, prefix_len), YANG_DEVIATION); |
| 240 | assert_int_equal(yin_match_keyword(st->xml_ctx, "enum", strlen("enum"), prefix, prefix_len), YANG_ENUM); |
| 241 | assert_int_equal(yin_match_keyword(st->xml_ctx, "error-app-tag", strlen("error-app-tag"), prefix, prefix_len), YANG_ERROR_APP_TAG); |
| 242 | assert_int_equal(yin_match_keyword(st->xml_ctx, "error-message", strlen("error-message"), prefix, prefix_len), YANG_ERROR_MESSAGE); |
| 243 | assert_int_equal(yin_match_keyword(st->xml_ctx, "extension", strlen("extension"), prefix, prefix_len), YANG_EXTENSION); |
| 244 | assert_int_equal(yin_match_keyword(st->xml_ctx, "feature", strlen("feature"), prefix, prefix_len), YANG_FEATURE); |
| 245 | assert_int_equal(yin_match_keyword(st->xml_ctx, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len), YANG_FRACTION_DIGITS); |
| 246 | assert_int_equal(yin_match_keyword(st->xml_ctx, "grouping", strlen("grouping"), prefix, prefix_len), YANG_GROUPING); |
| 247 | assert_int_equal(yin_match_keyword(st->xml_ctx, "identity", strlen("identity"), prefix, prefix_len), YANG_IDENTITY); |
| 248 | assert_int_equal(yin_match_keyword(st->xml_ctx, "if-feature", strlen("if-feature"), prefix, prefix_len), YANG_IF_FEATURE); |
| 249 | assert_int_equal(yin_match_keyword(st->xml_ctx, "import", strlen("import"), prefix, prefix_len), YANG_IMPORT); |
| 250 | assert_int_equal(yin_match_keyword(st->xml_ctx, "include", strlen("include"), prefix, prefix_len), YANG_INCLUDE); |
| 251 | assert_int_equal(yin_match_keyword(st->xml_ctx, "input", strlen("input"), prefix, prefix_len), YANG_INPUT); |
| 252 | assert_int_equal(yin_match_keyword(st->xml_ctx, "key", strlen("key"), prefix, prefix_len), YANG_KEY); |
| 253 | assert_int_equal(yin_match_keyword(st->xml_ctx, "leaf", strlen("leaf"), prefix, prefix_len), YANG_LEAF); |
| 254 | assert_int_equal(yin_match_keyword(st->xml_ctx, "leaf-list", strlen("leaf-list"), prefix, prefix_len), YANG_LEAF_LIST); |
| 255 | assert_int_equal(yin_match_keyword(st->xml_ctx, "length", strlen("length"), prefix, prefix_len), YANG_LENGTH); |
| 256 | assert_int_equal(yin_match_keyword(st->xml_ctx, "list", strlen("list"), prefix, prefix_len), YANG_LIST); |
| 257 | assert_int_equal(yin_match_keyword(st->xml_ctx, "mandatory", strlen("mandatory"), prefix, prefix_len), YANG_MANDATORY); |
| 258 | assert_int_equal(yin_match_keyword(st->xml_ctx, "max-elements", strlen("max-elements"), prefix, prefix_len), YANG_MAX_ELEMENTS); |
| 259 | assert_int_equal(yin_match_keyword(st->xml_ctx, "min-elements", strlen("min-elements"), prefix, prefix_len), YANG_MIN_ELEMENTS); |
| 260 | assert_int_equal(yin_match_keyword(st->xml_ctx, "modifier", strlen("modifier"), prefix, prefix_len), YANG_MODIFIER); |
| 261 | assert_int_equal(yin_match_keyword(st->xml_ctx, "module", strlen("module"), prefix, prefix_len), YANG_MODULE); |
| 262 | assert_int_equal(yin_match_keyword(st->xml_ctx, "must", strlen("must"), prefix, prefix_len), YANG_MUST); |
| 263 | assert_int_equal(yin_match_keyword(st->xml_ctx, "namespace", strlen("namespace"), prefix, prefix_len), YANG_NAMESPACE); |
| 264 | assert_int_equal(yin_match_keyword(st->xml_ctx, "notification", strlen("notification"), prefix, prefix_len), YANG_NOTIFICATION); |
| 265 | assert_int_equal(yin_match_keyword(st->xml_ctx, "ordered-by", strlen("ordered-by"), prefix, prefix_len), YANG_ORDERED_BY); |
| 266 | assert_int_equal(yin_match_keyword(st->xml_ctx, "organization", strlen("organization"), prefix, prefix_len), YANG_ORGANIZATION); |
| 267 | assert_int_equal(yin_match_keyword(st->xml_ctx, "output", strlen("output"), prefix, prefix_len), YANG_OUTPUT); |
| 268 | assert_int_equal(yin_match_keyword(st->xml_ctx, "path", strlen("path"), prefix, prefix_len), YANG_PATH); |
| 269 | assert_int_equal(yin_match_keyword(st->xml_ctx, "pattern", strlen("pattern"), prefix, prefix_len), YANG_PATTERN); |
| 270 | assert_int_equal(yin_match_keyword(st->xml_ctx, "position", strlen("position"), prefix, prefix_len), YANG_POSITION); |
| 271 | assert_int_equal(yin_match_keyword(st->xml_ctx, "prefix", strlen("prefix"), prefix, prefix_len), YANG_PREFIX); |
| 272 | assert_int_equal(yin_match_keyword(st->xml_ctx, "presence", strlen("presence"), prefix, prefix_len), YANG_PRESENCE); |
| 273 | assert_int_equal(yin_match_keyword(st->xml_ctx, "range", strlen("range"), prefix, prefix_len), YANG_RANGE); |
| 274 | assert_int_equal(yin_match_keyword(st->xml_ctx, "reference", strlen("reference"), prefix, prefix_len), YANG_REFERENCE); |
| 275 | assert_int_equal(yin_match_keyword(st->xml_ctx, "refine", strlen("refine"), prefix, prefix_len), YANG_REFINE); |
| 276 | assert_int_equal(yin_match_keyword(st->xml_ctx, "require-instance", strlen("require-instance"), prefix, prefix_len), YANG_REQUIRE_INSTANCE); |
| 277 | assert_int_equal(yin_match_keyword(st->xml_ctx, "revision", strlen("revision"), prefix, prefix_len), YANG_REVISION); |
| 278 | assert_int_equal(yin_match_keyword(st->xml_ctx, "revision-date", strlen("revision-date"), prefix, prefix_len), YANG_REVISION_DATE); |
| 279 | assert_int_equal(yin_match_keyword(st->xml_ctx, "rpc", strlen("rpc"), prefix, prefix_len), YANG_RPC); |
| 280 | assert_int_equal(yin_match_keyword(st->xml_ctx, "status", strlen("status"), prefix, prefix_len), YANG_STATUS); |
| 281 | assert_int_equal(yin_match_keyword(st->xml_ctx, "submodule", strlen("submodule"), prefix, prefix_len), YANG_SUBMODULE); |
| 282 | assert_int_equal(yin_match_keyword(st->xml_ctx, "type", strlen("type"), prefix, prefix_len), YANG_TYPE); |
| 283 | assert_int_equal(yin_match_keyword(st->xml_ctx, "typedef", strlen("typedef"), prefix, prefix_len), YANG_TYPEDEF); |
| 284 | assert_int_equal(yin_match_keyword(st->xml_ctx, "unique", strlen("unique"), prefix, prefix_len), YANG_UNIQUE); |
| 285 | assert_int_equal(yin_match_keyword(st->xml_ctx, "units", strlen("units"), prefix, prefix_len), YANG_UNITS); |
| 286 | assert_int_equal(yin_match_keyword(st->xml_ctx, "uses", strlen("uses"), prefix, prefix_len), YANG_USES); |
| 287 | assert_int_equal(yin_match_keyword(st->xml_ctx, "value", strlen("value"), prefix, prefix_len), YANG_VALUE); |
| 288 | assert_int_equal(yin_match_keyword(st->xml_ctx, "when", strlen("when"), prefix, prefix_len), YANG_WHEN); |
| 289 | assert_int_equal(yin_match_keyword(st->xml_ctx, "yang-version", strlen("yang-version"), prefix, prefix_len), YANG_YANG_VERSION); |
| 290 | assert_int_equal(yin_match_keyword(st->xml_ctx, "yin-element", strlen("yin-element"), prefix, prefix_len), YANG_YIN_ELEMENT); |
| 291 | |
| 292 | st->finished_correctly = true; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 293 | } |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 294 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 295 | static void |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 296 | test_yin_match_argument_name(void **state) |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 297 | { |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 298 | (void)state; /* unused */ |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 299 | |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 300 | assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN); |
| 301 | assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN); |
| 302 | assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN); |
| 303 | assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION); |
| 304 | assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE); |
| 305 | assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE); |
| 306 | assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME); |
| 307 | assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG); |
| 308 | assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE); |
| 309 | assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT); |
| 310 | assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI); |
| 311 | 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] | 312 | } |
| 313 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 314 | static void |
| 315 | test_meta(void **state) |
| 316 | { |
| 317 | LY_ERR ret = LY_SUCCESS; |
| 318 | struct state *st = *state; |
| 319 | |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 320 | ret = yin_parse_module(st->ctx,"<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\ |
| 321 | name=\"example-foo\">\ |
David Sedlák | 21f87cd | 2019-07-03 16:53:23 +0200 | [diff] [blame] | 322 | <prefix value=\"foo\">ignored</prefix>\ |
| 323 | <namespace uri=\"urn:example:foo\" xmlns:myext=\"urn:example:extensions\"/>\ |
David Sedlák | 15a9266 | 2019-06-18 11:55:15 +0200 | [diff] [blame] | 324 | <organization xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"><text>organization...</text></organization>\ |
| 325 | <contact><text>contact...</text></contact>\ |
| 326 | <description><text>description...</text></description>\ |
| 327 | <reference><text>reference...</text></reference>\ |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 328 | </module>", st->mod); |
| 329 | |
| 330 | assert_int_equal(ret, LY_SUCCESS); |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 331 | assert_string_equal(st->mod->parsed->mod->org, "organization..."); |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 332 | assert_string_equal(st->mod->parsed->mod->contact, "contact..."); |
| 333 | assert_string_equal(st->mod->parsed->mod->dsc, "description..."); |
| 334 | assert_string_equal(st->mod->parsed->mod->ref, "reference..."); |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 335 | |
| 336 | st = reset_state(state); |
| 337 | ret = yin_parse_module(st->ctx,"<module name=\"example-foo\">\ |
| 338 | <organization test=\"invalid-argument\">organization...</organization>\ |
| 339 | </module>", st->mod); |
| 340 | assert_int_equal(ret, LY_EVALID); |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 341 | |
| 342 | st->finished_correctly = true; |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 343 | } |
| 344 | |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 345 | static void |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 346 | test_yin_parse_import(void **state) |
| 347 | { |
| 348 | struct state *st = *state; |
| 349 | const char *prefix = NULL, *name = NULL; |
| 350 | size_t prefix_len = 0, name_len = 0; |
| 351 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 352 | struct yin_arg_record *args = NULL; |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 353 | |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 354 | 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] | 355 | <prefix value=\"a_mod\"/>\ |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 356 | <revision-date date=\"2015-01-01\"></revision-date>\ |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 357 | <description><text>import description</text></description>\ |
| 358 | <reference><text>import reference</text></reference>\ |
David Sedlák | c67dcaa | 2019-06-04 14:49:05 +0200 | [diff] [blame] | 359 | </import>\ |
| 360 | \ |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 361 | <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] | 362 | <prefix value=\"a_mod\"/>\ |
David Sedlák | 0025034 | 2019-06-21 14:19:39 +0200 | [diff] [blame] | 363 | <revision-date date=\"2015-01-01\" />\ |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 364 | </import>"; |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 365 | /* first import */ |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 366 | 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] | 367 | yin_load_attributes(st->xml_ctx, &data, &args); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 368 | st->lysp_mod->mod->prefix = "b-mod"; |
| 369 | ret = yin_parse_import(st->xml_ctx, &args, &data, st->lysp_mod); |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 370 | assert_int_equal(ret, LY_SUCCESS); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 371 | assert_string_equal(st->lysp_mod->imports->name, "a"); |
| 372 | assert_string_equal(st->lysp_mod->imports->prefix, "a_mod"); |
| 373 | assert_string_equal(st->lysp_mod->imports->rev, "2015-01-01"); |
| 374 | assert_string_equal(st->lysp_mod->imports->dsc, "import description"); |
| 375 | assert_string_equal(st->lysp_mod->imports->ref, "import reference"); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 376 | LY_ARRAY_FREE(args); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 377 | args = NULL; |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 378 | st = reset_state(state); |
David Sedlák | c67dcaa | 2019-06-04 14:49:05 +0200 | [diff] [blame] | 379 | |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 380 | /* second invalid import */ |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 381 | 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] | 382 | yin_load_attributes(st->xml_ctx, &data, &args); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 383 | st->lysp_mod->mod->prefix = "a_mod"; |
| 384 | ret = yin_parse_import(st->xml_ctx, &args, &data, st->lysp_mod); |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 385 | assert_int_equal(ret, LY_EVALID); |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 386 | logbuf_assert("Prefix \"a_mod\" already used as module prefix. Line number 1."); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 387 | LY_ARRAY_FREE(args); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 388 | args = NULL; |
| 389 | |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 390 | st = reset_state(state); |
| 391 | /* import with unknown child element */ |
| 392 | data = "<import xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" module=\"a\">\ |
| 393 | <what value=\"a_mod\"/>\ |
| 394 | </import>"; |
| 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); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 397 | st->lysp_mod->mod->prefix = "invalid_mod"; |
| 398 | ret = yin_parse_import(st->xml_ctx, &args, &data, st->lysp_mod); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 399 | assert_int_equal(ret, LY_EVALID); |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 400 | logbuf_assert("Unexpected child element \"what\" of import element. Line number 1."); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 401 | LY_ARRAY_FREE(args); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 402 | |
| 403 | st->finished_correctly = true; |
| 404 | } |
| 405 | |
| 406 | static void |
| 407 | test_yin_parse_status(void **state) |
| 408 | { |
| 409 | struct state *st = *state; |
| 410 | const char *prefix = NULL, *name = NULL; |
| 411 | size_t prefix_len = 0, name_len = 0; |
| 412 | LY_ERR ret = LY_SUCCESS; |
| 413 | uint16_t flags = 0; |
| 414 | struct lysp_ext_instance *exts; |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 415 | struct yin_arg_record *args = NULL; |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 416 | |
| 417 | /* try all valid values */ |
| 418 | const char *data = "<status value=\"current\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>"; |
| 419 | 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] | 420 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 421 | ret = yin_parse_status(st->xml_ctx, &args, &data, &flags, &exts); |
David Sedlák | 4af339b | 2019-06-24 15:37:29 +0200 | [diff] [blame] | 422 | assert_int_equal(st->xml_ctx->status, LYXML_END); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 423 | assert_int_equal(ret, LY_SUCCESS); |
| 424 | assert_true(flags & LYS_STATUS_CURR); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 425 | LY_ARRAY_FREE(args); |
| 426 | args = NULL; |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 427 | |
| 428 | st = reset_state(state); |
| 429 | flags = 0; |
| 430 | data = "<status value=\"deprecated\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>"; |
| 431 | 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] | 432 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 433 | ret = yin_parse_status(st->xml_ctx, &args, &data, &flags, &exts); |
David Sedlák | 4af339b | 2019-06-24 15:37:29 +0200 | [diff] [blame] | 434 | assert_int_equal(st->xml_ctx->status, LYXML_END); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 435 | assert_int_equal(ret, LY_SUCCESS); |
| 436 | assert_true(flags & LYS_STATUS_DEPRC); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 437 | LY_ARRAY_FREE(args); |
| 438 | args = NULL; |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 439 | |
| 440 | st = reset_state(state); |
| 441 | flags = 0; |
| 442 | data = "<status value=\"obsolete\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>"; |
| 443 | 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] | 444 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 445 | ret = yin_parse_status(st->xml_ctx, &args, &data, &flags, &exts); |
David Sedlák | 4af339b | 2019-06-24 15:37:29 +0200 | [diff] [blame] | 446 | assert_int_equal(st->xml_ctx->status, LYXML_END); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 447 | assert_int_equal(ret, LY_SUCCESS); |
| 448 | assert_true(flags & LYS_STATUS_OBSLT); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 449 | LY_ARRAY_FREE(args); |
| 450 | args = NULL; |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 451 | |
| 452 | /* duplicit definition (no reset_state() call) */ |
| 453 | data = "<status value=\"deprecated\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>"; |
| 454 | 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] | 455 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 456 | ret = yin_parse_status(st->xml_ctx, &args, &data, &flags, &exts); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 457 | assert_int_equal(ret, LY_EVALID); |
| 458 | logbuf_assert("Duplicate element \"status\". Line number 1."); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 459 | LY_ARRAY_FREE(args); |
| 460 | args = NULL; |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 461 | |
| 462 | /* invalid status value */ |
| 463 | st = reset_state(state); |
| 464 | flags = 0; |
| 465 | data = "<status value=\"dunno\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>"; |
| 466 | 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] | 467 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 468 | ret = yin_parse_status(st->xml_ctx, &args, &data, &flags, &exts); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 469 | assert_int_equal(ret, LY_EVALID); |
| 470 | logbuf_assert("Invalid value \"dunno\" of \"status\". Line number 1."); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 471 | LY_ARRAY_FREE(args); |
| 472 | |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 473 | st->finished_correctly = true; |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 474 | } |
| 475 | |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 476 | static void |
| 477 | test_yin_parse_extension(void **state) |
| 478 | { |
| 479 | struct state *st = *state; |
| 480 | const char *prefix = NULL, *name = NULL; |
| 481 | size_t prefix_len = 0, name_len = 0; |
| 482 | LY_ERR ret = LY_SUCCESS; |
| 483 | struct yin_arg_record *args = NULL; |
| 484 | struct lysp_ext *exts = NULL, *iter = NULL; |
| 485 | |
| 486 | 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] | 487 | <argument name=\"argname\"></argument>\ |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 488 | <description><text>desc</text></description>\ |
| 489 | <reference><text>ref</text></reference>\ |
| 490 | <status value=\"deprecated\"></status>\ |
| 491 | </extension>"; |
| 492 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 493 | yin_load_attributes(st->xml_ctx, &data, &args); |
| 494 | ret = yin_parse_extension(st->xml_ctx, &args, &data, &exts); |
David Sedlák | 4af339b | 2019-06-24 15:37:29 +0200 | [diff] [blame] | 495 | assert_int_equal(st->xml_ctx->status, LYXML_END); |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 496 | assert_int_equal(ret, LY_SUCCESS); |
| 497 | LY_ARRAY_FOR_ITER(exts, struct lysp_ext, iter) { |
| 498 | assert_string_equal(iter->name, "b"); |
| 499 | assert_string_equal(iter->dsc, "desc"); |
| 500 | assert_string_equal(iter->ref, "ref"); |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 501 | assert_string_equal(iter->argument, "argname"); |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 502 | assert_true(iter->flags & LYS_STATUS_DEPRC); |
| 503 | } |
David Sedlák | 288c147 | 2019-06-20 16:09:48 +0200 | [diff] [blame] | 504 | lydict_remove(st->ctx, "b"); |
| 505 | lydict_remove(st->ctx, "desc"); |
| 506 | lydict_remove(st->ctx, "ref"); |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 507 | lydict_remove(st->ctx, "argname"); |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 508 | LY_ARRAY_FREE(args); |
David Sedlák | 288c147 | 2019-06-20 16:09:48 +0200 | [diff] [blame] | 509 | LY_ARRAY_FREE(exts); |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 510 | st->finished_correctly = true; |
| 511 | } |
| 512 | |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 513 | static void |
| 514 | test_yin_parse_yin_element_element(void **state) |
| 515 | { |
| 516 | struct state *st = *state; |
| 517 | const char *prefix = NULL, *name = NULL; |
| 518 | size_t prefix_len = 0, name_len = 0; |
| 519 | LY_ERR ret = LY_SUCCESS; |
| 520 | uint16_t flags = 0; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 521 | struct lysp_ext_instance *exts; |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 522 | struct yin_arg_record *args = NULL; |
| 523 | |
| 524 | /* try all valid values */ |
| 525 | const char *data = "<yin-element value=\"true\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>"; |
| 526 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 527 | yin_load_attributes(st->xml_ctx, &data, &args); |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 528 | 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] | 529 | assert_int_equal(st->xml_ctx->status, LYXML_END); |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 530 | assert_int_equal(ret, LY_SUCCESS); |
| 531 | assert_true(flags & LYS_YINELEM_TRUE); |
| 532 | LY_ARRAY_FREE(args); |
| 533 | args = NULL; |
| 534 | |
| 535 | st = reset_state(state); |
| 536 | flags = 0; |
| 537 | data = "<yin-element value=\"false\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>"; |
| 538 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 539 | yin_load_attributes(st->xml_ctx, &data, &args); |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 540 | 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] | 541 | assert_int_equal(st->xml_ctx->status, LYXML_END); |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 542 | assert_int_equal(ret, LY_SUCCESS); |
| 543 | assert_true(flags & LYS_YINELEM_FALSE); |
| 544 | LY_ARRAY_FREE(args); |
| 545 | args = NULL; |
| 546 | |
| 547 | /* invalid value */ |
| 548 | st = reset_state(state); |
| 549 | flags = 0; |
| 550 | data = "<yin-element value=\"invalid\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>"; |
| 551 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 552 | yin_load_attributes(st->xml_ctx, &data, &args); |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 553 | ret = yin_parse_yin_element_element(st->xml_ctx, args, &data, &flags, &exts); |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 554 | assert_int_equal(ret, LY_EVALID); |
| 555 | LY_ARRAY_FREE(args); |
| 556 | args = NULL; |
| 557 | |
| 558 | st->finished_correctly = true; |
| 559 | } |
| 560 | |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 561 | static void |
| 562 | test_yin_parse_element_generic(void **state) |
| 563 | { |
| 564 | const char *prefix, *name; |
| 565 | struct state *st = *state; |
| 566 | struct lysp_ext_instance exts; |
| 567 | size_t prefix_len, name_len; |
| 568 | LY_ERR ret; |
| 569 | |
| 570 | memset(&exts, 0, sizeof(exts)); |
| 571 | |
| 572 | const char *data = "<elem attr=\"value\">text_value</elem>"; |
| 573 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 574 | ret = yin_parse_element_generic(st->xml_ctx, name, name_len, prefix, prefix_len, &data, &exts.child); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 575 | assert_int_equal(ret, LY_SUCCESS); |
| 576 | assert_string_equal(exts.child->stmt, "elem"); |
| 577 | assert_string_equal(exts.child->arg, "text_value"); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 578 | assert_string_equal(exts.child->child->stmt, "attr"); |
| 579 | assert_string_equal(exts.child->child->arg, "value"); |
| 580 | assert_true(exts.child->child->flags & LYS_YIN_ATTR); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 581 | lysp_ext_instance_free(st->ctx, &exts); |
David Sedlák | 5392a21 | 2019-07-01 09:19:10 +0200 | [diff] [blame] | 582 | st = reset_state(state); |
| 583 | |
| 584 | data = "<elem></elem>"; |
| 585 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 586 | ret = yin_parse_element_generic(st->xml_ctx, name, name_len, prefix, prefix_len, &data, &exts.child); |
David Sedlák | 5392a21 | 2019-07-01 09:19:10 +0200 | [diff] [blame] | 587 | assert_int_equal(ret, LY_SUCCESS); |
| 588 | assert_string_equal(exts.child->stmt, "elem"); |
| 589 | assert_null(exts.child->child); |
| 590 | assert_null(exts.child->arg); |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 591 | assert_int_equal(st->xml_ctx->status, LYXML_END); |
David Sedlák | 5392a21 | 2019-07-01 09:19:10 +0200 | [diff] [blame] | 592 | lysp_ext_instance_free(st->ctx, &exts); |
| 593 | |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 594 | st->finished_correctly = true; |
| 595 | } |
| 596 | |
| 597 | static void |
| 598 | test_yin_parse_extension_instance(void **state) |
| 599 | { |
| 600 | LY_ERR ret; |
| 601 | struct state *st = *state; |
| 602 | const char *prefix, *name; |
| 603 | size_t prefix_len, name_len; |
| 604 | struct yin_arg_record *args = NULL; |
| 605 | struct lysp_ext_instance *exts = NULL; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 606 | const char *data = "<ext value1=\"test\" value=\"test2\"><subelem>text</subelem></ext>"; |
| 607 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 608 | yin_load_attributes(st->xml_ctx, &data, &args); |
David Sedlák | d6e5689 | 2019-07-01 15:40:24 +0200 | [diff] [blame] | 609 | ret = yin_parse_extension_instance(st->xml_ctx, &args, &data, name2fullname(name, prefix_len), |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 610 | namelen2fulllen(name_len, prefix_len), LYEXT_SUBSTMT_CONTACT, 0, &exts); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 611 | assert_int_equal(ret, LY_SUCCESS); |
| 612 | assert_string_equal(exts->name, "ext"); |
| 613 | assert_int_equal(exts->insubstmt_index, 0); |
| 614 | assert_true(exts->insubstmt == LYEXT_SUBSTMT_CONTACT); |
| 615 | assert_true(exts->yin & LYS_YIN); |
| 616 | assert_string_equal(exts->child->stmt, "value1"); |
| 617 | assert_string_equal(exts->child->arg, "test"); |
| 618 | assert_null(exts->child->child); |
| 619 | assert_true(exts->child->flags & LYS_YIN_ATTR); |
| 620 | assert_string_equal(exts->child->next->stmt, "value"); |
| 621 | assert_string_equal(exts->child->next->arg, "test2"); |
| 622 | assert_null(exts->child->next->child); |
| 623 | assert_true(exts->child->next->flags & LYS_YIN_ATTR); |
| 624 | |
| 625 | assert_string_equal(exts->child->next->next->stmt, "subelem"); |
| 626 | assert_string_equal(exts->child->next->next->arg, "text"); |
| 627 | assert_null(exts->child->next->next->child); |
| 628 | assert_null(exts->child->next->next->next); |
| 629 | assert_false(exts->child->next->next->flags & LYS_YIN_ATTR); |
| 630 | assert_int_equal(st->xml_ctx->status, LYXML_END); |
| 631 | LY_ARRAY_FREE(args); |
| 632 | lysp_ext_instance_free(st->ctx, exts); |
| 633 | LY_ARRAY_FREE(exts); |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 634 | exts = NULL; |
| 635 | args = NULL; |
| 636 | st = reset_state(state); |
| 637 | |
| 638 | data = "<extension-elem />"; |
| 639 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 640 | yin_load_attributes(st->xml_ctx, &data, &args); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 641 | ret = yin_parse_extension_instance(st->xml_ctx, &args, &data, name, name_len, LYEXT_SUBSTMT_CONTACT, 0, &exts); |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 642 | assert_int_equal(ret, LY_SUCCESS); |
| 643 | assert_string_equal(exts->name, "extension-elem"); |
| 644 | assert_null(exts->argument); |
| 645 | assert_null(exts->child); |
| 646 | assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT); |
| 647 | assert_int_equal(exts->insubstmt_index, 0); |
| 648 | assert_true(exts->yin & LYS_YIN); |
| 649 | assert_int_equal(st->xml_ctx->status, LYXML_END); |
| 650 | LY_ARRAY_FREE(args); |
| 651 | lysp_ext_instance_free(st->ctx, exts); |
| 652 | LY_ARRAY_FREE(exts); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 653 | st->finished_correctly = true; |
| 654 | } |
| 655 | |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 656 | static void |
| 657 | test_yin_parse_content(void **state) |
| 658 | { |
| 659 | struct state *st = *state; |
| 660 | LY_ERR ret = LY_SUCCESS; |
| 661 | struct sized_string name, prefix; |
| 662 | const char *data = "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">" |
| 663 | "<custom xmlns=\"my-ext\">" |
| 664 | "totally amazing extension" |
| 665 | "</custom>" |
David Sedlák | 986cb41 | 2019-07-04 13:10:11 +0200 | [diff] [blame] | 666 | "<extension name=\"ext\">" |
| 667 | "<argument name=\"argname\"></argument>" |
| 668 | "<description><text>desc</text></description>" |
| 669 | "<reference><text>ref</text></reference>" |
| 670 | "<status value=\"deprecated\"></status>" |
| 671 | "</extension>" |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 672 | "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>" |
| 673 | "</prefix>"; |
| 674 | struct lysp_ext_instance *exts = NULL; |
| 675 | struct yin_arg_record *attrs = NULL; |
| 676 | const char *value; |
David Sedlák | 986cb41 | 2019-07-04 13:10:11 +0200 | [diff] [blame] | 677 | struct lysp_ext *ext_def = NULL; |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 678 | |
| 679 | lyxml_get_element(st->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 680 | yin_load_attributes(st->xml_ctx, &data, &attrs); |
| 681 | |
David Sedlák | b0faad8 | 2019-07-04 14:28:59 +0200 | [diff] [blame^] | 682 | struct yin_subelement subelems[3] = {{YANG_EXTENSION, &ext_def, 0}, |
| 683 | {YANG_CUSTOM, NULL, 0}, |
| 684 | {YIN_TEXT, &value, 0}}; |
David Sedlák | 986cb41 | 2019-07-04 13:10:11 +0200 | [diff] [blame] | 685 | ret = yin_parse_content(st->xml_ctx, subelems, 3, &data, YANG_ACTION, NULL, &exts); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 686 | assert_int_equal(ret, LY_SUCCESS); |
| 687 | assert_string_equal(exts->name, "custom"); |
| 688 | assert_string_equal(exts->argument, "totally amazing extension"); |
| 689 | assert_string_equal(value, "wsefsdf"); |
| 690 | lysp_ext_instance_free(st->ctx, exts); |
David Sedlák | 986cb41 | 2019-07-04 13:10:11 +0200 | [diff] [blame] | 691 | lysp_ext_free(st->ctx, ext_def); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 692 | LY_ARRAY_FREE(exts); |
David Sedlák | 986cb41 | 2019-07-04 13:10:11 +0200 | [diff] [blame] | 693 | LY_ARRAY_FREE(ext_def); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 694 | LY_ARRAY_FREE(attrs); |
| 695 | attrs = NULL; |
| 696 | lydict_remove(st->ctx, value); |
| 697 | st = reset_state(state); |
| 698 | |
| 699 | /* test unique subelem */ |
| 700 | const char *prefix_value; |
| 701 | struct yin_subelement subelems2[2] = {{YANG_PREFIX, &prefix_value, 0}, |
| 702 | {YIN_TEXT, &value, YIN_SUBELEM_UNIQUE}}; |
| 703 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">" |
| 704 | "<prefix value=\"inv_mod\" />" |
| 705 | "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>" |
| 706 | "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>" |
| 707 | "</module>"; |
| 708 | lyxml_get_element(st->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 709 | yin_load_attributes(st->xml_ctx, &data, &attrs); |
| 710 | ret = yin_parse_content(st->xml_ctx, subelems2, 2, &data, YANG_MODULE, NULL, &exts); |
| 711 | assert_int_equal(ret, LY_EVALID); |
| 712 | logbuf_assert("Redefinition of text element in module element. Line number 1."); |
| 713 | lydict_remove(st->ctx, prefix_value); |
| 714 | lydict_remove(st->ctx, value); |
| 715 | st = reset_state(state); |
| 716 | LY_ARRAY_FREE(attrs); |
| 717 | attrs = NULL; |
| 718 | |
| 719 | /* test first subelem */ |
| 720 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">" |
| 721 | "<prefix value=\"inv_mod\" />" |
| 722 | "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>" |
| 723 | "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>" |
| 724 | "</module>"; |
| 725 | struct yin_subelement subelems3[2] = {{YANG_PREFIX, &prefix_value, 0}, |
| 726 | {YIN_TEXT, &value, YIN_SUBELEM_FIRST}}; |
| 727 | lyxml_get_element(st->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 728 | yin_load_attributes(st->xml_ctx, &data, &attrs); |
| 729 | ret = yin_parse_content(st->xml_ctx, subelems3, 2, &data, YANG_MODULE, NULL, &exts); |
| 730 | assert_int_equal(ret, LY_EVALID); |
| 731 | logbuf_assert("Subelement text of module element must be defined as first subelement. Line number 1."); |
| 732 | lydict_remove(st->ctx, prefix_value); |
| 733 | st = reset_state(state); |
| 734 | LY_ARRAY_FREE(attrs); |
| 735 | attrs = NULL; |
| 736 | |
| 737 | /* test mandatory subelem */ |
| 738 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">" |
| 739 | "</module>"; |
| 740 | struct yin_subelement subelems4[1] = {{YANG_PREFIX, &prefix_value, YIN_SUBELEM_MANDATORY}}; |
| 741 | lyxml_get_element(st->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 742 | yin_load_attributes(st->xml_ctx, &data, &attrs); |
| 743 | ret = yin_parse_content(st->xml_ctx, subelems4, 1, &data, YANG_MODULE, NULL, &exts); |
| 744 | assert_int_equal(ret, LY_EVALID); |
| 745 | logbuf_assert("Missing mandatory subelement prefix of module element. Line number 1."); |
| 746 | LY_ARRAY_FREE(attrs); |
| 747 | |
| 748 | st->finished_correctly = true; |
| 749 | } |
| 750 | |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 751 | int |
| 752 | main(void) |
| 753 | { |
| 754 | |
| 755 | const struct CMUnitTest tests[] = { |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 756 | 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] | 757 | cmocka_unit_test_setup_teardown(test_meta, setup_f, teardown_f), |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 758 | 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] | 759 | 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] | 760 | 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] | 761 | 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] | 762 | cmocka_unit_test_setup_teardown(test_yin_parse_yin_element_element, setup_f, teardown_f), |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 763 | cmocka_unit_test_setup_teardown(test_yin_parse_element_generic, setup_f, teardown_f), |
| 764 | cmocka_unit_test_setup_teardown(test_yin_parse_extension_instance, setup_f, teardown_f), |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 765 | cmocka_unit_test_setup_teardown(test_yin_parse_content, setup_f, teardown_f), |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 766 | cmocka_unit_test(test_yin_match_argument_name), |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 767 | }; |
| 768 | |
| 769 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 770 | } |