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