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 | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 222 | assert_int_equal(yin_match_keyword(st->yin_ctx, "anydatax", strlen("anydatax"), prefix, prefix_len, YANG_NONE), YANG_NONE); |
| 223 | assert_int_equal(yin_match_keyword(st->yin_ctx, "asdasd", strlen("asdasd"), prefix, prefix_len, YANG_NONE), YANG_NONE); |
| 224 | assert_int_equal(yin_match_keyword(st->yin_ctx, "", 0, prefix, prefix_len, YANG_NONE), YANG_NONE); |
| 225 | assert_int_equal(yin_match_keyword(st->yin_ctx, "anydata", strlen("anydata"), prefix, prefix_len, YANG_NONE), YANG_ANYDATA); |
| 226 | assert_int_equal(yin_match_keyword(st->yin_ctx, "anyxml", strlen("anyxml"), prefix, prefix_len, YANG_NONE), YANG_ANYXML); |
| 227 | assert_int_equal(yin_match_keyword(st->yin_ctx, "argument", strlen("argument"), prefix, prefix_len, YANG_NONE), YANG_ARGUMENT); |
| 228 | assert_int_equal(yin_match_keyword(st->yin_ctx, "augment", strlen("augment"), prefix, prefix_len, YANG_NONE), YANG_AUGMENT); |
| 229 | assert_int_equal(yin_match_keyword(st->yin_ctx, "base", strlen("base"), prefix, prefix_len, YANG_NONE), YANG_BASE); |
| 230 | assert_int_equal(yin_match_keyword(st->yin_ctx, "belongs-to", strlen("belongs-to"), prefix, prefix_len, YANG_NONE), YANG_BELONGS_TO); |
| 231 | assert_int_equal(yin_match_keyword(st->yin_ctx, "bit", strlen("bit"), prefix, prefix_len, YANG_NONE), YANG_BIT); |
| 232 | assert_int_equal(yin_match_keyword(st->yin_ctx, "case", strlen("case"), prefix, prefix_len, YANG_NONE), YANG_CASE); |
| 233 | assert_int_equal(yin_match_keyword(st->yin_ctx, "choice", strlen("choice"), prefix, prefix_len, YANG_NONE), YANG_CHOICE); |
| 234 | assert_int_equal(yin_match_keyword(st->yin_ctx, "config", strlen("config"), prefix, prefix_len, YANG_NONE), YANG_CONFIG); |
| 235 | assert_int_equal(yin_match_keyword(st->yin_ctx, "contact", strlen("contact"), prefix, prefix_len, YANG_NONE), YANG_CONTACT); |
| 236 | assert_int_equal(yin_match_keyword(st->yin_ctx, "container", strlen("container"), prefix, prefix_len, YANG_NONE), YANG_CONTAINER); |
| 237 | assert_int_equal(yin_match_keyword(st->yin_ctx, "default", strlen("default"), prefix, prefix_len, YANG_NONE), YANG_DEFAULT); |
| 238 | assert_int_equal(yin_match_keyword(st->yin_ctx, "description", strlen("description"), prefix, prefix_len, YANG_NONE), YANG_DESCRIPTION); |
| 239 | assert_int_equal(yin_match_keyword(st->yin_ctx, "deviate", strlen("deviate"), prefix, prefix_len, YANG_NONE), YANG_DEVIATE); |
| 240 | assert_int_equal(yin_match_keyword(st->yin_ctx, "deviation", strlen("deviation"), prefix, prefix_len, YANG_NONE), YANG_DEVIATION); |
| 241 | assert_int_equal(yin_match_keyword(st->yin_ctx, "enum", strlen("enum"), prefix, prefix_len, YANG_NONE), YANG_ENUM); |
| 242 | assert_int_equal(yin_match_keyword(st->yin_ctx, "error-app-tag", strlen("error-app-tag"), prefix, prefix_len, YANG_NONE), YANG_ERROR_APP_TAG); |
| 243 | assert_int_equal(yin_match_keyword(st->yin_ctx, "error-message", strlen("error-message"), prefix, prefix_len, YANG_NONE), YANG_ERROR_MESSAGE); |
| 244 | assert_int_equal(yin_match_keyword(st->yin_ctx, "extension", strlen("extension"), prefix, prefix_len, YANG_NONE), YANG_EXTENSION); |
| 245 | assert_int_equal(yin_match_keyword(st->yin_ctx, "feature", strlen("feature"), prefix, prefix_len, YANG_NONE), YANG_FEATURE); |
| 246 | assert_int_equal(yin_match_keyword(st->yin_ctx, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len, YANG_NONE), YANG_FRACTION_DIGITS); |
| 247 | assert_int_equal(yin_match_keyword(st->yin_ctx, "grouping", strlen("grouping"), prefix, prefix_len, YANG_NONE), YANG_GROUPING); |
| 248 | assert_int_equal(yin_match_keyword(st->yin_ctx, "identity", strlen("identity"), prefix, prefix_len, YANG_NONE), YANG_IDENTITY); |
| 249 | assert_int_equal(yin_match_keyword(st->yin_ctx, "if-feature", strlen("if-feature"), prefix, prefix_len, YANG_NONE), YANG_IF_FEATURE); |
| 250 | assert_int_equal(yin_match_keyword(st->yin_ctx, "import", strlen("import"), prefix, prefix_len, YANG_NONE), YANG_IMPORT); |
| 251 | assert_int_equal(yin_match_keyword(st->yin_ctx, "include", strlen("include"), prefix, prefix_len, YANG_NONE), YANG_INCLUDE); |
| 252 | assert_int_equal(yin_match_keyword(st->yin_ctx, "input", strlen("input"), prefix, prefix_len, YANG_NONE), YANG_INPUT); |
| 253 | assert_int_equal(yin_match_keyword(st->yin_ctx, "key", strlen("key"), prefix, prefix_len, YANG_NONE), YANG_KEY); |
| 254 | assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf", strlen("leaf"), prefix, prefix_len, YANG_NONE), YANG_LEAF); |
| 255 | assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf-list", strlen("leaf-list"), prefix, prefix_len, YANG_NONE), YANG_LEAF_LIST); |
| 256 | assert_int_equal(yin_match_keyword(st->yin_ctx, "length", strlen("length"), prefix, prefix_len, YANG_NONE), YANG_LENGTH); |
| 257 | assert_int_equal(yin_match_keyword(st->yin_ctx, "list", strlen("list"), prefix, prefix_len, YANG_NONE), YANG_LIST); |
| 258 | assert_int_equal(yin_match_keyword(st->yin_ctx, "mandatory", strlen("mandatory"), prefix, prefix_len, YANG_NONE), YANG_MANDATORY); |
| 259 | assert_int_equal(yin_match_keyword(st->yin_ctx, "max-elements", strlen("max-elements"), prefix, prefix_len, YANG_NONE), YANG_MAX_ELEMENTS); |
| 260 | assert_int_equal(yin_match_keyword(st->yin_ctx, "min-elements", strlen("min-elements"), prefix, prefix_len, YANG_NONE), YANG_MIN_ELEMENTS); |
| 261 | assert_int_equal(yin_match_keyword(st->yin_ctx, "modifier", strlen("modifier"), prefix, prefix_len, YANG_NONE), YANG_MODIFIER); |
| 262 | assert_int_equal(yin_match_keyword(st->yin_ctx, "module", strlen("module"), prefix, prefix_len, YANG_NONE), YANG_MODULE); |
| 263 | assert_int_equal(yin_match_keyword(st->yin_ctx, "must", strlen("must"), prefix, prefix_len, YANG_NONE), YANG_MUST); |
| 264 | assert_int_equal(yin_match_keyword(st->yin_ctx, "namespace", strlen("namespace"), prefix, prefix_len, YANG_NONE), YANG_NAMESPACE); |
| 265 | assert_int_equal(yin_match_keyword(st->yin_ctx, "notification", strlen("notification"), prefix, prefix_len, YANG_NONE), YANG_NOTIFICATION); |
| 266 | assert_int_equal(yin_match_keyword(st->yin_ctx, "ordered-by", strlen("ordered-by"), prefix, prefix_len, YANG_NONE), YANG_ORDERED_BY); |
| 267 | assert_int_equal(yin_match_keyword(st->yin_ctx, "organization", strlen("organization"), prefix, prefix_len, YANG_NONE), YANG_ORGANIZATION); |
| 268 | assert_int_equal(yin_match_keyword(st->yin_ctx, "output", strlen("output"), prefix, prefix_len, YANG_NONE), YANG_OUTPUT); |
| 269 | assert_int_equal(yin_match_keyword(st->yin_ctx, "path", strlen("path"), prefix, prefix_len, YANG_NONE), YANG_PATH); |
| 270 | assert_int_equal(yin_match_keyword(st->yin_ctx, "pattern", strlen("pattern"), prefix, prefix_len, YANG_NONE), YANG_PATTERN); |
| 271 | assert_int_equal(yin_match_keyword(st->yin_ctx, "position", strlen("position"), prefix, prefix_len, YANG_NONE), YANG_POSITION); |
| 272 | assert_int_equal(yin_match_keyword(st->yin_ctx, "prefix", strlen("prefix"), prefix, prefix_len, YANG_NONE), YANG_PREFIX); |
| 273 | assert_int_equal(yin_match_keyword(st->yin_ctx, "presence", strlen("presence"), prefix, prefix_len, YANG_NONE), YANG_PRESENCE); |
| 274 | assert_int_equal(yin_match_keyword(st->yin_ctx, "range", strlen("range"), prefix, prefix_len, YANG_NONE), YANG_RANGE); |
| 275 | assert_int_equal(yin_match_keyword(st->yin_ctx, "reference", strlen("reference"), prefix, prefix_len, YANG_NONE), YANG_REFERENCE); |
| 276 | assert_int_equal(yin_match_keyword(st->yin_ctx, "refine", strlen("refine"), prefix, prefix_len, YANG_NONE), YANG_REFINE); |
| 277 | assert_int_equal(yin_match_keyword(st->yin_ctx, "require-instance", strlen("require-instance"), prefix, prefix_len, YANG_NONE), YANG_REQUIRE_INSTANCE); |
| 278 | assert_int_equal(yin_match_keyword(st->yin_ctx, "revision", strlen("revision"), prefix, prefix_len, YANG_NONE), YANG_REVISION); |
| 279 | assert_int_equal(yin_match_keyword(st->yin_ctx, "revision-date", strlen("revision-date"), prefix, prefix_len, YANG_NONE), YANG_REVISION_DATE); |
| 280 | assert_int_equal(yin_match_keyword(st->yin_ctx, "rpc", strlen("rpc"), prefix, prefix_len, YANG_NONE), YANG_RPC); |
| 281 | assert_int_equal(yin_match_keyword(st->yin_ctx, "status", strlen("status"), prefix, prefix_len, YANG_NONE), YANG_STATUS); |
| 282 | assert_int_equal(yin_match_keyword(st->yin_ctx, "submodule", strlen("submodule"), prefix, prefix_len, YANG_NONE), YANG_SUBMODULE); |
| 283 | assert_int_equal(yin_match_keyword(st->yin_ctx, "type", strlen("type"), prefix, prefix_len, YANG_NONE), YANG_TYPE); |
| 284 | assert_int_equal(yin_match_keyword(st->yin_ctx, "typedef", strlen("typedef"), prefix, prefix_len, YANG_NONE), YANG_TYPEDEF); |
| 285 | assert_int_equal(yin_match_keyword(st->yin_ctx, "unique", strlen("unique"), prefix, prefix_len, YANG_NONE), YANG_UNIQUE); |
| 286 | assert_int_equal(yin_match_keyword(st->yin_ctx, "units", strlen("units"), prefix, prefix_len, YANG_NONE), YANG_UNITS); |
| 287 | assert_int_equal(yin_match_keyword(st->yin_ctx, "uses", strlen("uses"), prefix, prefix_len, YANG_NONE), YANG_USES); |
| 288 | assert_int_equal(yin_match_keyword(st->yin_ctx, "value", strlen("value"), prefix, prefix_len, YANG_NONE), YANG_VALUE); |
| 289 | assert_int_equal(yin_match_keyword(st->yin_ctx, "when", strlen("when"), prefix, prefix_len, YANG_NONE), YANG_WHEN); |
| 290 | assert_int_equal(yin_match_keyword(st->yin_ctx, "yang-version", strlen("yang-version"), prefix, prefix_len, YANG_NONE), YANG_YANG_VERSION); |
| 291 | assert_int_equal(yin_match_keyword(st->yin_ctx, "yin-element", strlen("yin-element"), prefix, prefix_len, YANG_NONE), 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 | 1f90d25 | 2019-07-10 17:09:32 +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 | 1f90d25 | 2019-07-10 17:09:32 +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 | 1f90d25 | 2019-07-10 17:09:32 +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); |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 422 | ret = yin_parse_status(st->yin_ctx, args, &data, &flags, &exts); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 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); |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 434 | ret = yin_parse_status(st->yin_ctx, args, &data, &flags, &exts); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 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); |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 446 | ret = yin_parse_status(st->yin_ctx, args, &data, &flags, &exts); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 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 | |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 453 | /* invalid status value */ |
| 454 | st = reset_state(state); |
| 455 | flags = 0; |
| 456 | 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] | 457 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 458 | yin_load_attributes(st->yin_ctx, &data, &args); |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 459 | ret = yin_parse_status(st->yin_ctx, args, &data, &flags, &exts); |
David Sedlák | b6e6597 | 2019-06-19 10:44:13 +0200 | [diff] [blame] | 460 | assert_int_equal(ret, LY_EVALID); |
| 461 | logbuf_assert("Invalid value \"dunno\" of \"status\". Line number 1."); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 462 | LY_ARRAY_FREE(args); |
| 463 | |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 464 | st->finished_correctly = true; |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 465 | } |
| 466 | |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 467 | static void |
| 468 | test_yin_parse_extension(void **state) |
| 469 | { |
| 470 | struct state *st = *state; |
| 471 | const char *prefix = NULL, *name = NULL; |
| 472 | size_t prefix_len = 0, name_len = 0; |
| 473 | LY_ERR ret = LY_SUCCESS; |
| 474 | struct yin_arg_record *args = NULL; |
| 475 | struct lysp_ext *exts = NULL, *iter = NULL; |
| 476 | |
| 477 | 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] | 478 | <argument name=\"argname\"></argument>\ |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 479 | <description><text>desc</text></description>\ |
| 480 | <reference><text>ref</text></reference>\ |
| 481 | <status value=\"deprecated\"></status>\ |
| 482 | </extension>"; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 483 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 484 | yin_load_attributes(st->yin_ctx, &data, &args); |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 485 | ret = yin_parse_extension(st->yin_ctx, args, &data, &exts); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 486 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 487 | assert_int_equal(ret, LY_SUCCESS); |
| 488 | LY_ARRAY_FOR_ITER(exts, struct lysp_ext, iter) { |
| 489 | assert_string_equal(iter->name, "b"); |
| 490 | assert_string_equal(iter->dsc, "desc"); |
| 491 | assert_string_equal(iter->ref, "ref"); |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 492 | assert_string_equal(iter->argument, "argname"); |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 493 | assert_true(iter->flags & LYS_STATUS_DEPRC); |
| 494 | } |
David Sedlák | 288c147 | 2019-06-20 16:09:48 +0200 | [diff] [blame] | 495 | lydict_remove(st->ctx, "b"); |
| 496 | lydict_remove(st->ctx, "desc"); |
| 497 | lydict_remove(st->ctx, "ref"); |
David Sedlák | 9494eb2 | 2019-06-21 16:06:53 +0200 | [diff] [blame] | 498 | lydict_remove(st->ctx, "argname"); |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 499 | LY_ARRAY_FREE(args); |
David Sedlák | 288c147 | 2019-06-20 16:09:48 +0200 | [diff] [blame] | 500 | LY_ARRAY_FREE(exts); |
David Sedlák | 554e36d | 2019-06-20 16:00:04 +0200 | [diff] [blame] | 501 | st->finished_correctly = true; |
| 502 | } |
| 503 | |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 504 | static void |
| 505 | test_yin_parse_yin_element_element(void **state) |
| 506 | { |
| 507 | struct state *st = *state; |
| 508 | const char *prefix = NULL, *name = NULL; |
| 509 | size_t prefix_len = 0, name_len = 0; |
| 510 | LY_ERR ret = LY_SUCCESS; |
| 511 | uint16_t flags = 0; |
David Sedlák | 3ffbc52 | 2019-07-02 17:49:28 +0200 | [diff] [blame] | 512 | struct lysp_ext_instance *exts; |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 513 | struct yin_arg_record *args = NULL; |
| 514 | |
| 515 | /* try all valid values */ |
| 516 | 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] | 517 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 518 | yin_load_attributes(st->yin_ctx, &data, &args); |
| 519 | ret = yin_parse_yin_element_element(st->yin_ctx, args, &data, &flags, &exts); |
| 520 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 521 | assert_int_equal(ret, LY_SUCCESS); |
| 522 | assert_true(flags & LYS_YINELEM_TRUE); |
| 523 | LY_ARRAY_FREE(args); |
| 524 | args = NULL; |
| 525 | |
| 526 | st = reset_state(state); |
| 527 | flags = 0; |
| 528 | 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] | 529 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 530 | yin_load_attributes(st->yin_ctx, &data, &args); |
| 531 | ret = yin_parse_yin_element_element(st->yin_ctx, args, &data, &flags, &exts); |
| 532 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
David Sedlák | 2721d3d | 2019-06-21 15:37:41 +0200 | [diff] [blame] | 533 | assert_int_equal(ret, LY_SUCCESS); |
| 534 | assert_true(flags & LYS_YINELEM_FALSE); |
| 535 | LY_ARRAY_FREE(args); |
| 536 | args = NULL; |
| 537 | |
| 538 | /* invalid value */ |
| 539 | st = reset_state(state); |
| 540 | flags = 0; |
| 541 | 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] | 542 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 543 | yin_load_attributes(st->yin_ctx, &data, &args); |
| 544 | 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] | 545 | assert_int_equal(ret, LY_EVALID); |
| 546 | LY_ARRAY_FREE(args); |
| 547 | args = NULL; |
| 548 | |
| 549 | st->finished_correctly = true; |
| 550 | } |
| 551 | |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 552 | static void |
| 553 | test_yin_parse_element_generic(void **state) |
| 554 | { |
| 555 | const char *prefix, *name; |
| 556 | struct state *st = *state; |
| 557 | struct lysp_ext_instance exts; |
| 558 | size_t prefix_len, name_len; |
| 559 | LY_ERR ret; |
| 560 | |
| 561 | memset(&exts, 0, sizeof(exts)); |
| 562 | |
| 563 | const char *data = "<elem attr=\"value\">text_value</elem>"; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 564 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 565 | 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] | 566 | assert_int_equal(ret, LY_SUCCESS); |
David Sedlák | 1fdb252 | 2019-07-09 16:22:57 +0200 | [diff] [blame] | 567 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 568 | assert_string_equal(exts.child->stmt, "elem"); |
| 569 | assert_string_equal(exts.child->arg, "text_value"); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 570 | assert_string_equal(exts.child->child->stmt, "attr"); |
| 571 | assert_string_equal(exts.child->child->arg, "value"); |
| 572 | assert_true(exts.child->child->flags & LYS_YIN_ATTR); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 573 | lysp_ext_instance_free(st->ctx, &exts); |
David Sedlák | 5392a21 | 2019-07-01 09:19:10 +0200 | [diff] [blame] | 574 | st = reset_state(state); |
| 575 | |
| 576 | data = "<elem></elem>"; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 577 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 578 | 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] | 579 | assert_int_equal(ret, LY_SUCCESS); |
| 580 | assert_string_equal(exts.child->stmt, "elem"); |
| 581 | assert_null(exts.child->child); |
| 582 | assert_null(exts.child->arg); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 583 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
David Sedlák | 5392a21 | 2019-07-01 09:19:10 +0200 | [diff] [blame] | 584 | lysp_ext_instance_free(st->ctx, &exts); |
| 585 | |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 586 | st->finished_correctly = true; |
| 587 | } |
| 588 | |
| 589 | static void |
| 590 | test_yin_parse_extension_instance(void **state) |
| 591 | { |
| 592 | LY_ERR ret; |
| 593 | struct state *st = *state; |
| 594 | const char *prefix, *name; |
| 595 | size_t prefix_len, name_len; |
| 596 | struct yin_arg_record *args = NULL; |
| 597 | struct lysp_ext_instance *exts = NULL; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 598 | 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] | 599 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 600 | yin_load_attributes(st->yin_ctx, &data, &args); |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 601 | 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] | 602 | namelen2fulllen(name_len, prefix_len), LYEXT_SUBSTMT_CONTACT, 0, &exts); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 603 | assert_int_equal(ret, LY_SUCCESS); |
| 604 | assert_string_equal(exts->name, "ext"); |
| 605 | assert_int_equal(exts->insubstmt_index, 0); |
| 606 | assert_true(exts->insubstmt == LYEXT_SUBSTMT_CONTACT); |
| 607 | assert_true(exts->yin & LYS_YIN); |
| 608 | assert_string_equal(exts->child->stmt, "value1"); |
| 609 | assert_string_equal(exts->child->arg, "test"); |
| 610 | assert_null(exts->child->child); |
| 611 | assert_true(exts->child->flags & LYS_YIN_ATTR); |
| 612 | assert_string_equal(exts->child->next->stmt, "value"); |
| 613 | assert_string_equal(exts->child->next->arg, "test2"); |
| 614 | assert_null(exts->child->next->child); |
| 615 | assert_true(exts->child->next->flags & LYS_YIN_ATTR); |
| 616 | |
| 617 | assert_string_equal(exts->child->next->next->stmt, "subelem"); |
| 618 | assert_string_equal(exts->child->next->next->arg, "text"); |
| 619 | assert_null(exts->child->next->next->child); |
| 620 | assert_null(exts->child->next->next->next); |
| 621 | assert_false(exts->child->next->next->flags & LYS_YIN_ATTR); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 622 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 623 | LY_ARRAY_FREE(args); |
| 624 | lysp_ext_instance_free(st->ctx, exts); |
| 625 | LY_ARRAY_FREE(exts); |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 626 | exts = NULL; |
| 627 | args = NULL; |
| 628 | st = reset_state(state); |
| 629 | |
| 630 | data = "<extension-elem />"; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 631 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 632 | yin_load_attributes(st->yin_ctx, &data, &args); |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 633 | 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] | 634 | assert_int_equal(ret, LY_SUCCESS); |
| 635 | assert_string_equal(exts->name, "extension-elem"); |
| 636 | assert_null(exts->argument); |
| 637 | assert_null(exts->child); |
| 638 | assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT); |
| 639 | assert_int_equal(exts->insubstmt_index, 0); |
| 640 | assert_true(exts->yin & LYS_YIN); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 641 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 642 | LY_ARRAY_FREE(args); |
| 643 | lysp_ext_instance_free(st->ctx, exts); |
| 644 | LY_ARRAY_FREE(exts); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 645 | st->finished_correctly = true; |
| 646 | } |
| 647 | |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 648 | static void |
| 649 | test_yin_parse_content(void **state) |
| 650 | { |
| 651 | struct state *st = *state; |
| 652 | LY_ERR ret = LY_SUCCESS; |
| 653 | struct sized_string name, prefix; |
| 654 | const char *data = "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">" |
| 655 | "<custom xmlns=\"my-ext\">" |
| 656 | "totally amazing extension" |
| 657 | "</custom>" |
David Sedlák | 986cb41 | 2019-07-04 13:10:11 +0200 | [diff] [blame] | 658 | "<extension name=\"ext\">" |
| 659 | "<argument name=\"argname\"></argument>" |
| 660 | "<description><text>desc</text></description>" |
| 661 | "<reference><text>ref</text></reference>" |
| 662 | "<status value=\"deprecated\"></status>" |
| 663 | "</extension>" |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 664 | "<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] | 665 | "<if-feature value=\"foo\"></if-feature>" |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 666 | "<when condition=\"condition...\">" |
| 667 | "<reference><text>when_ref</text></reference>" |
| 668 | "<description><text>when_desc</text></description>" |
| 669 | "</when>" |
David Sedlák | e1a3030 | 2019-07-10 13:49:38 +0200 | [diff] [blame] | 670 | "<config value=\"true\"/>" |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 671 | "<error-message>" |
| 672 | "<value>error-msg</value>" |
| 673 | "</error-message>" |
David Sedlák | 2ce1be6 | 2019-07-10 16:15:09 +0200 | [diff] [blame] | 674 | "<error-app-tag value=\"err-app-tag\"/>" |
David Sedlák | a5b1d38 | 2019-07-10 16:31:09 +0200 | [diff] [blame] | 675 | "<units name=\"radians\"></units>" |
David Sedlák | e7084ce | 2019-07-10 16:44:15 +0200 | [diff] [blame] | 676 | "<default value=\"default-value\"/>" |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 677 | "<position value=\"25\"></position>" |
| 678 | "<value value=\"-5\"/>" |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 679 | "<require-instance value=\"true\"></require-instance>" |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 680 | "<range value=\"5..10\" />" |
David Sedlák | 438ae43 | 2019-07-11 15:36:54 +0200 | [diff] [blame^] | 681 | "<length value=\"baf\"/>" |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 682 | "</prefix>"; |
| 683 | struct lysp_ext_instance *exts = NULL; |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 684 | const char **if_features = NULL; |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 685 | struct yin_arg_record *attrs = NULL; |
David Sedlák | e7084ce | 2019-07-10 16:44:15 +0200 | [diff] [blame] | 686 | const char *value, *err_msg, *app_tag, *units, *def; |
David Sedlák | 986cb41 | 2019-07-04 13:10:11 +0200 | [diff] [blame] | 687 | struct lysp_ext *ext_def = NULL; |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 688 | struct lysp_when *when_p = NULL; |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 689 | struct lysp_type_enum pos_enum = {}, val_enum = {}; |
David Sedlák | 438ae43 | 2019-07-11 15:36:54 +0200 | [diff] [blame^] | 690 | struct lysp_type req_type = {}, range_type = {}, len_type = {}; |
David Sedlák | e1a3030 | 2019-07-10 13:49:38 +0200 | [diff] [blame] | 691 | uint8_t config = 0; |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 692 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 693 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 694 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 695 | |
David Sedlák | 438ae43 | 2019-07-11 15:36:54 +0200 | [diff] [blame^] | 696 | struct yin_subelement subelems[15] = { |
| 697 | {YANG_CONFIG, &config, 0}, |
| 698 | {YANG_DEFAULT, &def, 0}, |
| 699 | {YANG_ERROR_APP_TAG, &app_tag, 0}, |
| 700 | {YANG_ERROR_MESSAGE, &err_msg, 0}, |
| 701 | {YANG_EXTENSION, &ext_def, 0}, |
| 702 | {YANG_IF_FEATURE, &if_features, 0}, |
| 703 | {YANG_LENGTH, &len_type, 0}, |
| 704 | {YANG_POSITION, &pos_enum, 0}, |
| 705 | {YANG_RANGE, &range_type, 0}, |
| 706 | {YANG_REQUIRE_INSTANCE, &req_type, 0}, |
| 707 | {YANG_UNITS, &units, 0}, |
| 708 | {YANG_VALUE, &val_enum, 0}, |
| 709 | {YANG_WHEN, &when_p, 0}, |
| 710 | {YANG_CUSTOM, NULL, 0}, |
| 711 | {YIN_TEXT, &value, 0} |
| 712 | }; |
| 713 | ret = yin_parse_content(st->yin_ctx, subelems, 15, &data, YANG_PREFIX, NULL, &exts); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 714 | assert_int_equal(ret, LY_SUCCESS); |
David Sedlák | 1fdb252 | 2019-07-09 16:22:57 +0200 | [diff] [blame] | 715 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
David Sedlák | 2ce1be6 | 2019-07-10 16:15:09 +0200 | [diff] [blame] | 716 | /* check parsed values */ |
David Sedlák | e7084ce | 2019-07-10 16:44:15 +0200 | [diff] [blame] | 717 | assert_string_equal(def, "default-value"); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 718 | assert_string_equal(exts->name, "custom"); |
| 719 | assert_string_equal(exts->argument, "totally amazing extension"); |
| 720 | assert_string_equal(value, "wsefsdf"); |
David Sedlák | a5b1d38 | 2019-07-10 16:31:09 +0200 | [diff] [blame] | 721 | assert_string_equal(units, "radians"); |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 722 | assert_string_equal(when_p->cond, "condition..."); |
| 723 | assert_string_equal(when_p->dsc, "when_desc"); |
| 724 | assert_string_equal(when_p->ref, "when_ref"); |
David Sedlák | e1a3030 | 2019-07-10 13:49:38 +0200 | [diff] [blame] | 725 | assert_int_equal(config, LYS_CONFIG_W); |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 726 | assert_int_equal(pos_enum.value, 25); |
| 727 | assert_true(pos_enum.flags | LYS_SET_VALUE); |
| 728 | assert_int_equal(val_enum.value, -5); |
| 729 | assert_true(val_enum.flags | LYS_SET_VALUE); |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 730 | assert_int_equal(req_type.require_instance, 1); |
| 731 | assert_true(req_type.flags |= LYS_SET_REQINST); |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 732 | assert_string_equal(range_type.range->arg, "5..10"); |
| 733 | assert_true(range_type.flags | LYS_SET_RANGE); |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 734 | assert_string_equal(err_msg, "error-msg"); |
David Sedlák | 2ce1be6 | 2019-07-10 16:15:09 +0200 | [diff] [blame] | 735 | assert_string_equal(app_tag, "err-app-tag"); |
David Sedlák | 438ae43 | 2019-07-11 15:36:54 +0200 | [diff] [blame^] | 736 | assert_string_equal(len_type.length->arg, "baf"); |
| 737 | assert_true(len_type.flags | LYS_SET_LENGTH); |
David Sedlák | 2ce1be6 | 2019-07-10 16:15:09 +0200 | [diff] [blame] | 738 | /* cleanup */ |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 739 | lysp_ext_instance_free(st->ctx, exts); |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 740 | lysp_when_free(st->ctx, when_p); |
David Sedlák | 986cb41 | 2019-07-04 13:10:11 +0200 | [diff] [blame] | 741 | lysp_ext_free(st->ctx, ext_def); |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 742 | FREE_STRING(st->ctx, *if_features); |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 743 | FREE_STRING(st->ctx, err_msg); |
David Sedlák | 2ce1be6 | 2019-07-10 16:15:09 +0200 | [diff] [blame] | 744 | FREE_STRING(st->ctx, app_tag); |
David Sedlák | a5b1d38 | 2019-07-10 16:31:09 +0200 | [diff] [blame] | 745 | FREE_STRING(st->ctx, units); |
David Sedlák | e7084ce | 2019-07-10 16:44:15 +0200 | [diff] [blame] | 746 | FREE_STRING(st->ctx, def); |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 747 | FREE_STRING(st->ctx, range_type.range->arg); |
David Sedlák | 438ae43 | 2019-07-11 15:36:54 +0200 | [diff] [blame^] | 748 | FREE_STRING(st->ctx, len_type.length->arg); |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 749 | FREE_STRING(st->ctx, value); |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 750 | LY_ARRAY_FREE(if_features); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 751 | LY_ARRAY_FREE(exts); |
David Sedlák | 986cb41 | 2019-07-04 13:10:11 +0200 | [diff] [blame] | 752 | LY_ARRAY_FREE(ext_def); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 753 | LY_ARRAY_FREE(attrs); |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 754 | free(when_p); |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 755 | free(range_type.range); |
David Sedlák | 438ae43 | 2019-07-11 15:36:54 +0200 | [diff] [blame^] | 756 | free(len_type.length); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 757 | attrs = NULL; |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 758 | st = reset_state(state); |
| 759 | |
| 760 | /* test unique subelem */ |
| 761 | const char *prefix_value; |
| 762 | struct yin_subelement subelems2[2] = {{YANG_PREFIX, &prefix_value, 0}, |
| 763 | {YIN_TEXT, &value, YIN_SUBELEM_UNIQUE}}; |
| 764 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">" |
| 765 | "<prefix value=\"inv_mod\" />" |
| 766 | "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>" |
| 767 | "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>" |
| 768 | "</module>"; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 769 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 770 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 771 | 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] | 772 | assert_int_equal(ret, LY_EVALID); |
| 773 | logbuf_assert("Redefinition of text element in module element. Line number 1."); |
| 774 | lydict_remove(st->ctx, prefix_value); |
| 775 | lydict_remove(st->ctx, value); |
| 776 | st = reset_state(state); |
| 777 | LY_ARRAY_FREE(attrs); |
| 778 | attrs = NULL; |
| 779 | |
| 780 | /* test first subelem */ |
| 781 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">" |
| 782 | "<prefix value=\"inv_mod\" />" |
| 783 | "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>" |
| 784 | "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>" |
| 785 | "</module>"; |
| 786 | struct yin_subelement subelems3[2] = {{YANG_PREFIX, &prefix_value, 0}, |
| 787 | {YIN_TEXT, &value, YIN_SUBELEM_FIRST}}; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 788 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 789 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 790 | 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] | 791 | assert_int_equal(ret, LY_EVALID); |
| 792 | logbuf_assert("Subelement text of module element must be defined as first subelement. Line number 1."); |
| 793 | lydict_remove(st->ctx, prefix_value); |
| 794 | st = reset_state(state); |
| 795 | LY_ARRAY_FREE(attrs); |
| 796 | attrs = NULL; |
| 797 | |
| 798 | /* test mandatory subelem */ |
| 799 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">" |
| 800 | "</module>"; |
| 801 | 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] | 802 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 803 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 804 | 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] | 805 | assert_int_equal(ret, LY_EVALID); |
| 806 | logbuf_assert("Missing mandatory subelement prefix of module element. Line number 1."); |
| 807 | LY_ARRAY_FREE(attrs); |
| 808 | |
| 809 | st->finished_correctly = true; |
| 810 | } |
| 811 | |
David Sedlák | 92147b0 | 2019-07-09 14:01:01 +0200 | [diff] [blame] | 812 | static void |
| 813 | test_yin_parse_yangversion(void **state) |
| 814 | { |
| 815 | struct state *st = *state; |
| 816 | LY_ERR ret = LY_SUCCESS; |
| 817 | struct sized_string name, prefix; |
| 818 | struct yin_arg_record *attrs = NULL; |
| 819 | uint8_t version; |
| 820 | |
| 821 | const char *data = "<yang-version xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" value=\"1.0\">\n" |
| 822 | "</yang-version>"; |
| 823 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 824 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 825 | ret = yin_parse_yangversion(st->yin_ctx, attrs, &data, &version, NULL); |
| 826 | assert_int_equal(LY_SUCCESS, ret); |
David Sedlák | 1fdb252 | 2019-07-09 16:22:57 +0200 | [diff] [blame] | 827 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
David Sedlák | 92147b0 | 2019-07-09 14:01:01 +0200 | [diff] [blame] | 828 | assert_true(version == LYS_VERSION_1_0); |
| 829 | assert_true(st->yin_ctx->mod_version == LYS_VERSION_1_0); |
| 830 | LY_ARRAY_FREE(attrs); |
| 831 | attrs = NULL; |
| 832 | st = reset_state(state); |
| 833 | |
| 834 | data = "<yang-version xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" value=\"1.1\">\n" |
| 835 | "</yang-version>"; |
| 836 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 837 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 838 | ret = yin_parse_yangversion(st->yin_ctx, attrs, &data, &version, NULL); |
| 839 | assert_int_equal(LY_SUCCESS, ret); |
David Sedlák | 1fdb252 | 2019-07-09 16:22:57 +0200 | [diff] [blame] | 840 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
David Sedlák | 92147b0 | 2019-07-09 14:01:01 +0200 | [diff] [blame] | 841 | assert_true(version == LYS_VERSION_1_1); |
| 842 | assert_true(st->yin_ctx->mod_version == LYS_VERSION_1_1); |
| 843 | LY_ARRAY_FREE(attrs); |
| 844 | attrs = NULL; |
| 845 | st = reset_state(state); |
| 846 | |
| 847 | data = "<yang-version xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" value=\"randomvalue\">\n" |
| 848 | "</yang-version>"; |
| 849 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 850 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 851 | ret = yin_parse_yangversion(st->yin_ctx, attrs, &data, &version, NULL); |
| 852 | assert_int_equal(ret, LY_EVALID); |
David Sedlák | 1fdb252 | 2019-07-09 16:22:57 +0200 | [diff] [blame] | 853 | logbuf_assert("Invalid value \"randomvalue\" of \"yang-version\". Line number 1."); |
David Sedlák | 92147b0 | 2019-07-09 14:01:01 +0200 | [diff] [blame] | 854 | LY_ARRAY_FREE(attrs); |
| 855 | attrs = NULL; |
David Sedlák | 1fdb252 | 2019-07-09 16:22:57 +0200 | [diff] [blame] | 856 | st = reset_state(state); |
| 857 | |
| 858 | data = "<yang-version xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n" |
| 859 | "</yang-version>"; |
| 860 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 861 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 862 | ret = yin_parse_yangversion(st->yin_ctx, attrs, &data, &version, NULL); |
| 863 | assert_int_equal(ret, LY_EVALID); |
| 864 | LY_ARRAY_FREE(attrs); |
| 865 | attrs = NULL; |
| 866 | logbuf_assert("Missing mandatory attribute value of yang-version element. Line number 1."); |
| 867 | st->finished_correctly = true; |
| 868 | } |
| 869 | |
| 870 | static void |
| 871 | test_yin_parse_mandatory(void **state) |
| 872 | { |
| 873 | struct state *st = *state; |
| 874 | LY_ERR ret = LY_SUCCESS; |
| 875 | struct sized_string name, prefix; |
| 876 | struct yin_arg_record *attrs = NULL; |
| 877 | uint16_t man = 0; |
| 878 | |
| 879 | const char *data = "<mandatory xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" value=\"true\">\n" |
| 880 | "</mandatory>"; |
| 881 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 882 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 883 | ret = yin_parse_mandatory(st->yin_ctx, attrs, &data, &man, NULL); |
| 884 | assert_int_equal(LY_SUCCESS, ret); |
| 885 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
| 886 | assert_true(man == LYS_MAND_TRUE); |
| 887 | LY_ARRAY_FREE(attrs); |
| 888 | attrs = NULL; |
| 889 | man = 0; |
| 890 | st = reset_state(state); |
| 891 | |
| 892 | data = "<mandatory xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" value=\"false\" />"; |
| 893 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 894 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 895 | ret = yin_parse_mandatory(st->yin_ctx, attrs, &data, &man, NULL); |
| 896 | assert_int_equal(LY_SUCCESS, ret); |
| 897 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
| 898 | assert_true(man == LYS_MAND_FALSE); |
| 899 | LY_ARRAY_FREE(attrs); |
| 900 | attrs = NULL; |
| 901 | man = 0; |
| 902 | st = reset_state(state); |
| 903 | |
| 904 | data = "<mandatory xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" value=\"randomvalue\">\n" |
| 905 | "</mandatory>"; |
| 906 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 907 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 908 | ret = yin_parse_mandatory(st->yin_ctx, attrs, &data, &man, NULL); |
| 909 | assert_int_equal(ret, LY_EVALID); |
| 910 | LY_ARRAY_FREE(attrs); |
| 911 | logbuf_assert("Invalid value \"randomvalue\" of \"mandatory\". Line number 1."); |
| 912 | attrs = NULL; |
| 913 | man = 0; |
| 914 | st = reset_state(state); |
| 915 | |
| 916 | data = "<mandatory xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n" |
| 917 | "</mandatory>"; |
| 918 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 919 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 920 | ret = yin_parse_mandatory(st->yin_ctx, attrs, &data, &man, NULL); |
| 921 | assert_int_equal(ret, LY_EVALID); |
| 922 | LY_ARRAY_FREE(attrs); |
| 923 | logbuf_assert("Missing mandatory attribute value of mandatory element. Line number 1."); |
David Sedlák | 92147b0 | 2019-07-09 14:01:01 +0200 | [diff] [blame] | 924 | st->finished_correctly = true; |
| 925 | } |
| 926 | |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 927 | static void |
| 928 | test_validate_value(void **state) |
| 929 | { |
| 930 | struct state *st = *state; |
| 931 | assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "#invalid", 8), LY_EVALID); |
| 932 | logbuf_assert("Invalid identifier character '#'. Line number 1."); |
| 933 | assert_int_equal(yin_validate_value(st->yin_ctx, Y_STR_ARG, "", 0), LY_SUCCESS); |
| 934 | assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "pre:b", 5), LY_EVALID); |
| 935 | assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:b", 5), LY_SUCCESS); |
| 936 | assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:pre:b", 9), LY_EVALID); |
| 937 | |
| 938 | st->finished_correctly = true; |
| 939 | } |
| 940 | |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 941 | int |
| 942 | main(void) |
| 943 | { |
| 944 | |
| 945 | const struct CMUnitTest tests[] = { |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 946 | 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] | 947 | cmocka_unit_test_setup_teardown(test_meta, setup_f, teardown_f), |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 948 | 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] | 949 | 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] | 950 | 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] | 951 | 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] | 952 | 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] | 953 | cmocka_unit_test_setup_teardown(test_yin_parse_element_generic, setup_f, teardown_f), |
| 954 | 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] | 955 | 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] | 956 | cmocka_unit_test_setup_teardown(test_yin_parse_yangversion, setup_f, teardown_f), |
David Sedlák | 1fdb252 | 2019-07-09 16:22:57 +0200 | [diff] [blame] | 957 | cmocka_unit_test_setup_teardown(test_yin_parse_mandatory, setup_f, teardown_f), |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 958 | cmocka_unit_test_setup_teardown(test_validate_value, setup_f, teardown_f), |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 959 | cmocka_unit_test(test_yin_match_argument_name), |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 960 | }; |
| 961 | |
| 962 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 963 | } |