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