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 | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 677 | "</prefix>"; |
| 678 | struct lysp_ext_instance *exts = NULL; |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 679 | const char **if_features = NULL; |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 680 | struct yin_arg_record *attrs = NULL; |
David Sedlák | e7084ce | 2019-07-10 16:44:15 +0200 | [diff] [blame] | 681 | const char *value, *err_msg, *app_tag, *units, *def; |
David Sedlák | 986cb41 | 2019-07-04 13:10:11 +0200 | [diff] [blame] | 682 | struct lysp_ext *ext_def = NULL; |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 683 | struct lysp_when *when_p = NULL; |
David Sedlák | e1a3030 | 2019-07-10 13:49:38 +0200 | [diff] [blame] | 684 | uint8_t config = 0; |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 685 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 686 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 687 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 688 | |
David Sedlák | e7084ce | 2019-07-10 16:44:15 +0200 | [diff] [blame] | 689 | struct yin_subelement subelems[10] = {{YANG_CONFIG, &config, 0}, |
| 690 | {YANG_DEFAULT, &def, 0}, |
David Sedlák | 2ce1be6 | 2019-07-10 16:15:09 +0200 | [diff] [blame] | 691 | {YANG_ERROR_APP_TAG, &app_tag, 0}, |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 692 | {YANG_ERROR_MESSAGE, &err_msg, 0}, |
David Sedlák | e1a3030 | 2019-07-10 13:49:38 +0200 | [diff] [blame] | 693 | {YANG_EXTENSION, &ext_def, 0}, |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 694 | {YANG_IF_FEATURE, &if_features, 0}, |
David Sedlák | a5b1d38 | 2019-07-10 16:31:09 +0200 | [diff] [blame] | 695 | {YANG_UNITS, &units, 0}, |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 696 | {YANG_WHEN, &when_p, 0}, |
David Sedlák | b0faad8 | 2019-07-04 14:28:59 +0200 | [diff] [blame] | 697 | {YANG_CUSTOM, NULL, 0}, |
| 698 | {YIN_TEXT, &value, 0}}; |
David Sedlák | e7084ce | 2019-07-10 16:44:15 +0200 | [diff] [blame] | 699 | ret = yin_parse_content(st->yin_ctx, subelems, 10, &data, YANG_PREFIX, NULL, &exts); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 700 | assert_int_equal(ret, LY_SUCCESS); |
David Sedlák | 1fdb252 | 2019-07-09 16:22:57 +0200 | [diff] [blame] | 701 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
David Sedlák | 2ce1be6 | 2019-07-10 16:15:09 +0200 | [diff] [blame] | 702 | /* check parsed values */ |
David Sedlák | e7084ce | 2019-07-10 16:44:15 +0200 | [diff] [blame] | 703 | assert_string_equal(def, "default-value"); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 704 | assert_string_equal(exts->name, "custom"); |
| 705 | assert_string_equal(exts->argument, "totally amazing extension"); |
| 706 | assert_string_equal(value, "wsefsdf"); |
David Sedlák | a5b1d38 | 2019-07-10 16:31:09 +0200 | [diff] [blame] | 707 | assert_string_equal(units, "radians"); |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 708 | assert_string_equal(when_p->cond, "condition..."); |
| 709 | assert_string_equal(when_p->dsc, "when_desc"); |
| 710 | assert_string_equal(when_p->ref, "when_ref"); |
David Sedlák | e1a3030 | 2019-07-10 13:49:38 +0200 | [diff] [blame] | 711 | assert_int_equal(config, LYS_CONFIG_W); |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 712 | assert_string_equal(err_msg, "error-msg"); |
David Sedlák | 2ce1be6 | 2019-07-10 16:15:09 +0200 | [diff] [blame] | 713 | assert_string_equal(app_tag, "err-app-tag"); |
| 714 | /* cleanup */ |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 715 | lysp_ext_instance_free(st->ctx, exts); |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 716 | lysp_when_free(st->ctx, when_p); |
David Sedlák | 986cb41 | 2019-07-04 13:10:11 +0200 | [diff] [blame] | 717 | lysp_ext_free(st->ctx, ext_def); |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 718 | FREE_STRING(st->ctx, *if_features); |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 719 | FREE_STRING(st->ctx, err_msg); |
David Sedlák | 2ce1be6 | 2019-07-10 16:15:09 +0200 | [diff] [blame] | 720 | FREE_STRING(st->ctx, app_tag); |
David Sedlák | a5b1d38 | 2019-07-10 16:31:09 +0200 | [diff] [blame] | 721 | FREE_STRING(st->ctx, units); |
David Sedlák | e7084ce | 2019-07-10 16:44:15 +0200 | [diff] [blame] | 722 | FREE_STRING(st->ctx, def); |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 723 | LY_ARRAY_FREE(if_features); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 724 | LY_ARRAY_FREE(exts); |
David Sedlák | 986cb41 | 2019-07-04 13:10:11 +0200 | [diff] [blame] | 725 | LY_ARRAY_FREE(ext_def); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 726 | LY_ARRAY_FREE(attrs); |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 727 | free(when_p); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 728 | attrs = NULL; |
| 729 | lydict_remove(st->ctx, value); |
| 730 | st = reset_state(state); |
| 731 | |
| 732 | /* test unique subelem */ |
| 733 | const char *prefix_value; |
| 734 | struct yin_subelement subelems2[2] = {{YANG_PREFIX, &prefix_value, 0}, |
| 735 | {YIN_TEXT, &value, YIN_SUBELEM_UNIQUE}}; |
| 736 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">" |
| 737 | "<prefix value=\"inv_mod\" />" |
| 738 | "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>" |
| 739 | "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>" |
| 740 | "</module>"; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 741 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 742 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 743 | 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] | 744 | assert_int_equal(ret, LY_EVALID); |
| 745 | logbuf_assert("Redefinition of text element in module element. Line number 1."); |
| 746 | lydict_remove(st->ctx, prefix_value); |
| 747 | lydict_remove(st->ctx, value); |
| 748 | st = reset_state(state); |
| 749 | LY_ARRAY_FREE(attrs); |
| 750 | attrs = NULL; |
| 751 | |
| 752 | /* test first subelem */ |
| 753 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">" |
| 754 | "<prefix value=\"inv_mod\" />" |
| 755 | "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>" |
| 756 | "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>" |
| 757 | "</module>"; |
| 758 | struct yin_subelement subelems3[2] = {{YANG_PREFIX, &prefix_value, 0}, |
| 759 | {YIN_TEXT, &value, YIN_SUBELEM_FIRST}}; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 760 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 761 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 762 | 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] | 763 | assert_int_equal(ret, LY_EVALID); |
| 764 | logbuf_assert("Subelement text of module element must be defined as first subelement. Line number 1."); |
| 765 | lydict_remove(st->ctx, prefix_value); |
| 766 | st = reset_state(state); |
| 767 | LY_ARRAY_FREE(attrs); |
| 768 | attrs = NULL; |
| 769 | |
| 770 | /* test mandatory subelem */ |
| 771 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">" |
| 772 | "</module>"; |
| 773 | 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] | 774 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 775 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 776 | 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] | 777 | assert_int_equal(ret, LY_EVALID); |
| 778 | logbuf_assert("Missing mandatory subelement prefix of module element. Line number 1."); |
| 779 | LY_ARRAY_FREE(attrs); |
| 780 | |
| 781 | st->finished_correctly = true; |
| 782 | } |
| 783 | |
David Sedlák | 92147b0 | 2019-07-09 14:01:01 +0200 | [diff] [blame] | 784 | static void |
| 785 | test_yin_parse_yangversion(void **state) |
| 786 | { |
| 787 | struct state *st = *state; |
| 788 | LY_ERR ret = LY_SUCCESS; |
| 789 | struct sized_string name, prefix; |
| 790 | struct yin_arg_record *attrs = NULL; |
| 791 | uint8_t version; |
| 792 | |
| 793 | const char *data = "<yang-version xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" value=\"1.0\">\n" |
| 794 | "</yang-version>"; |
| 795 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 796 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 797 | ret = yin_parse_yangversion(st->yin_ctx, attrs, &data, &version, NULL); |
| 798 | assert_int_equal(LY_SUCCESS, ret); |
David Sedlák | 1fdb252 | 2019-07-09 16:22:57 +0200 | [diff] [blame] | 799 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
David Sedlák | 92147b0 | 2019-07-09 14:01:01 +0200 | [diff] [blame] | 800 | assert_true(version == LYS_VERSION_1_0); |
| 801 | assert_true(st->yin_ctx->mod_version == LYS_VERSION_1_0); |
| 802 | LY_ARRAY_FREE(attrs); |
| 803 | attrs = NULL; |
| 804 | st = reset_state(state); |
| 805 | |
| 806 | data = "<yang-version xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" value=\"1.1\">\n" |
| 807 | "</yang-version>"; |
| 808 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 809 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 810 | ret = yin_parse_yangversion(st->yin_ctx, attrs, &data, &version, NULL); |
| 811 | assert_int_equal(LY_SUCCESS, ret); |
David Sedlák | 1fdb252 | 2019-07-09 16:22:57 +0200 | [diff] [blame] | 812 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
David Sedlák | 92147b0 | 2019-07-09 14:01:01 +0200 | [diff] [blame] | 813 | assert_true(version == LYS_VERSION_1_1); |
| 814 | assert_true(st->yin_ctx->mod_version == LYS_VERSION_1_1); |
| 815 | LY_ARRAY_FREE(attrs); |
| 816 | attrs = NULL; |
| 817 | st = reset_state(state); |
| 818 | |
| 819 | data = "<yang-version xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" value=\"randomvalue\">\n" |
| 820 | "</yang-version>"; |
| 821 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 822 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 823 | ret = yin_parse_yangversion(st->yin_ctx, attrs, &data, &version, NULL); |
| 824 | assert_int_equal(ret, LY_EVALID); |
David Sedlák | 1fdb252 | 2019-07-09 16:22:57 +0200 | [diff] [blame] | 825 | logbuf_assert("Invalid value \"randomvalue\" of \"yang-version\". Line number 1."); |
David Sedlák | 92147b0 | 2019-07-09 14:01:01 +0200 | [diff] [blame] | 826 | LY_ARRAY_FREE(attrs); |
| 827 | attrs = NULL; |
David Sedlák | 1fdb252 | 2019-07-09 16:22:57 +0200 | [diff] [blame] | 828 | st = reset_state(state); |
| 829 | |
| 830 | data = "<yang-version xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n" |
| 831 | "</yang-version>"; |
| 832 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 833 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 834 | ret = yin_parse_yangversion(st->yin_ctx, attrs, &data, &version, NULL); |
| 835 | assert_int_equal(ret, LY_EVALID); |
| 836 | LY_ARRAY_FREE(attrs); |
| 837 | attrs = NULL; |
| 838 | logbuf_assert("Missing mandatory attribute value of yang-version element. Line number 1."); |
| 839 | st->finished_correctly = true; |
| 840 | } |
| 841 | |
| 842 | static void |
| 843 | test_yin_parse_mandatory(void **state) |
| 844 | { |
| 845 | struct state *st = *state; |
| 846 | LY_ERR ret = LY_SUCCESS; |
| 847 | struct sized_string name, prefix; |
| 848 | struct yin_arg_record *attrs = NULL; |
| 849 | uint16_t man = 0; |
| 850 | |
| 851 | const char *data = "<mandatory xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" value=\"true\">\n" |
| 852 | "</mandatory>"; |
| 853 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 854 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 855 | ret = yin_parse_mandatory(st->yin_ctx, attrs, &data, &man, NULL); |
| 856 | assert_int_equal(LY_SUCCESS, ret); |
| 857 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
| 858 | assert_true(man == LYS_MAND_TRUE); |
| 859 | LY_ARRAY_FREE(attrs); |
| 860 | attrs = NULL; |
| 861 | man = 0; |
| 862 | st = reset_state(state); |
| 863 | |
| 864 | data = "<mandatory xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" value=\"false\" />"; |
| 865 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 866 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 867 | ret = yin_parse_mandatory(st->yin_ctx, attrs, &data, &man, NULL); |
| 868 | assert_int_equal(LY_SUCCESS, ret); |
| 869 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
| 870 | assert_true(man == LYS_MAND_FALSE); |
| 871 | LY_ARRAY_FREE(attrs); |
| 872 | attrs = NULL; |
| 873 | man = 0; |
| 874 | st = reset_state(state); |
| 875 | |
| 876 | data = "<mandatory xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" value=\"randomvalue\">\n" |
| 877 | "</mandatory>"; |
| 878 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 879 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 880 | ret = yin_parse_mandatory(st->yin_ctx, attrs, &data, &man, NULL); |
| 881 | assert_int_equal(ret, LY_EVALID); |
| 882 | LY_ARRAY_FREE(attrs); |
| 883 | logbuf_assert("Invalid value \"randomvalue\" of \"mandatory\". Line number 1."); |
| 884 | attrs = NULL; |
| 885 | man = 0; |
| 886 | st = reset_state(state); |
| 887 | |
| 888 | data = "<mandatory xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n" |
| 889 | "</mandatory>"; |
| 890 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 891 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 892 | ret = yin_parse_mandatory(st->yin_ctx, attrs, &data, &man, NULL); |
| 893 | assert_int_equal(ret, LY_EVALID); |
| 894 | LY_ARRAY_FREE(attrs); |
| 895 | 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] | 896 | st->finished_correctly = true; |
| 897 | } |
| 898 | |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 899 | static void |
| 900 | test_validate_value(void **state) |
| 901 | { |
| 902 | struct state *st = *state; |
| 903 | assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "#invalid", 8), LY_EVALID); |
| 904 | logbuf_assert("Invalid identifier character '#'. Line number 1."); |
| 905 | assert_int_equal(yin_validate_value(st->yin_ctx, Y_STR_ARG, "", 0), LY_SUCCESS); |
| 906 | assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "pre:b", 5), LY_EVALID); |
| 907 | assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:b", 5), LY_SUCCESS); |
| 908 | assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:pre:b", 9), LY_EVALID); |
| 909 | |
| 910 | st->finished_correctly = true; |
| 911 | } |
| 912 | |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 913 | int |
| 914 | main(void) |
| 915 | { |
| 916 | |
| 917 | const struct CMUnitTest tests[] = { |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 918 | 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] | 919 | cmocka_unit_test_setup_teardown(test_meta, setup_f, teardown_f), |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 920 | 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] | 921 | 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] | 922 | 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] | 923 | 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] | 924 | 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] | 925 | cmocka_unit_test_setup_teardown(test_yin_parse_element_generic, setup_f, teardown_f), |
| 926 | 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] | 927 | 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] | 928 | 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] | 929 | 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] | 930 | 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] | 931 | cmocka_unit_test(test_yin_match_argument_name), |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 932 | }; |
| 933 | |
| 934 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 935 | } |