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 | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 34 | void lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type); |
David Sedlák | 8a83bbb | 2019-07-18 14:46:00 +0200 | [diff] [blame] | 35 | void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node); |
David Sedlák | 04e17b2 | 2019-07-19 15:29:48 +0200 | [diff] [blame] | 36 | void lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf); |
David Sedlák | d2d676a | 2019-07-22 11:28:19 +0200 | [diff] [blame] | 37 | void lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref); |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 38 | void lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev); |
David Sedlák | 0c2bab9 | 2019-07-22 15:33:19 +0200 | [diff] [blame^] | 39 | void lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 40 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 41 | struct state { |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 42 | struct ly_ctx *ctx; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 43 | struct lys_module *mod; |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 44 | struct lysp_module *lysp_mod; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 45 | struct yin_parser_ctx *yin_ctx; |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 46 | bool finished_correctly; |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 47 | }; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 48 | |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 49 | #define BUFSIZE 1024 |
| 50 | char logbuf[BUFSIZE] = {0}; |
| 51 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
| 52 | |
| 53 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
David Sedlák | 203ca3a | 2019-07-18 15:26:25 +0200 | [diff] [blame] | 54 | #define ENABLE_LOGGER_CHECKING 1 |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 55 | |
| 56 | #if ENABLE_LOGGER_CHECKING |
| 57 | static void |
| 58 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 59 | { |
| 60 | (void) level; /* unused */ |
| 61 | if (store) { |
| 62 | if (path && path[0]) { |
| 63 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 64 | } else { |
| 65 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 66 | } |
| 67 | if (store > 0) { |
| 68 | --store; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | #endif |
| 73 | |
| 74 | #if ENABLE_LOGGER_CHECKING |
| 75 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 76 | #else |
| 77 | # define logbuf_assert(str) |
| 78 | #endif |
| 79 | |
| 80 | #define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \ |
| 81 | str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 82 | assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \ |
| 83 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \ |
| 84 | CLEANUP |
| 85 | |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 86 | int |
| 87 | setup_ly_ctx(void **state) |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 88 | { |
| 89 | struct state *st = NULL; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 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 | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 101 | return EXIT_SUCCESS; |
| 102 | } |
| 103 | |
| 104 | int |
| 105 | destroy_ly_ctx(void **state) |
| 106 | { |
| 107 | struct state *st = *state; |
| 108 | ly_ctx_destroy(st->ctx, NULL); |
| 109 | free(st); |
| 110 | |
| 111 | return EXIT_SUCCESS; |
| 112 | } |
| 113 | |
| 114 | static int |
| 115 | setup_f(void **state) |
| 116 | { |
| 117 | struct state *st = *state; |
| 118 | |
| 119 | #if ENABLE_LOGGER_CHECKING |
| 120 | /* setup logger */ |
| 121 | ly_set_log_clb(logger, 1); |
| 122 | #endif |
| 123 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 124 | /* allocate new module */ |
| 125 | st->mod = calloc(1, sizeof(*st->mod)); |
| 126 | st->mod->ctx = st->ctx; |
| 127 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 128 | /* allocate new parsed module */ |
| 129 | st->lysp_mod = calloc(1, sizeof(*st->lysp_mod)); |
| 130 | st->lysp_mod->mod = calloc(1, sizeof(*st->lysp_mod->mod)); |
| 131 | st->lysp_mod->mod->ctx = st->ctx; |
| 132 | |
| 133 | /* allocate parser context */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 134 | st->yin_ctx = calloc(1, sizeof(*st->yin_ctx)); |
| 135 | st->yin_ctx->xml_ctx.ctx = st->ctx; |
| 136 | st->yin_ctx->xml_ctx.line = 1; |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 137 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 138 | return EXIT_SUCCESS; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static int |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 142 | teardown_f(void **state) |
| 143 | { |
| 144 | struct state *st = *(struct state **)state; |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 145 | struct lys_module *temp; |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 146 | |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 147 | #if ENABLE_LOGGER_CHECKING |
| 148 | /* teardown logger */ |
| 149 | if (!st->finished_correctly && logbuf[0] != '\0') { |
| 150 | fprintf(stderr, "%s\n", logbuf); |
| 151 | } |
| 152 | #endif |
| 153 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 154 | temp = st->lysp_mod->mod; |
| 155 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 156 | lyxml_context_clear(&st->yin_ctx->xml_ctx); |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 157 | lys_module_free(st->mod, NULL); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 158 | lysp_module_free(st->lysp_mod); |
| 159 | lys_module_free(temp, NULL); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 160 | free(st->yin_ctx); |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 161 | |
| 162 | return EXIT_SUCCESS; |
| 163 | } |
| 164 | |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 165 | static struct state* |
| 166 | reset_state(void **state) |
| 167 | { |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 168 | ((struct state *)*state)->finished_correctly = true; |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 169 | logbuf[0] = '\0'; |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 170 | teardown_f(state); |
| 171 | setup_f(state); |
| 172 | |
| 173 | return *state; |
| 174 | } |
| 175 | |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 176 | void |
| 177 | logbuf_clean(void) |
| 178 | { |
| 179 | logbuf[0] = '\0'; |
| 180 | } |
| 181 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 182 | static void |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 183 | test_yin_parse_module(void **state) |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 184 | { |
| 185 | LY_ERR ret = LY_SUCCESS; |
| 186 | struct state *st = *state; |
| 187 | |
| 188 | ret = yin_parse_module(st->ctx, |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 189 | "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\ |
| 190 | name=\"example-foo\"\ |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 191 | xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\ |
| 192 | xmlns:foo=\"urn:example:foo\"\ |
| 193 | xmlns:myext=\"urn:example:extensions\">\ |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 194 | <namespace uri=\"urn:example:foo\" xmlns:myext=\"urn:example:extensions\"/>\ |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 195 | <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] | 196 | </module>", |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 197 | st->mod); |
| 198 | |
| 199 | assert_int_equal(ret, LY_SUCCESS); |
| 200 | assert_string_equal(st->mod->parsed->mod->name, "example-foo"); |
| 201 | assert_string_equal(st->mod->parsed->mod->prefix, "foo"); |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 202 | assert_string_equal(st->mod->parsed->mod->ns, "urn:example:foo"); |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 203 | |
| 204 | st = reset_state(state); |
| 205 | ret = yin_parse_module(st->ctx, |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 206 | "<module name=\"example-foo\">\ |
| 207 | <invalid-tag uri=\"urn:example:foo\"\"/>\ |
| 208 | </module>", |
| 209 | st->mod); |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 210 | assert_int_equal(ret, LY_EVALID); |
| 211 | |
| 212 | st = reset_state(state); |
| 213 | ret = yin_parse_module(st->ctx, |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 214 | "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\ |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 215 | </module>", |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 216 | st->mod); |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 217 | assert_int_equal(ret, LY_EVALID); |
David Sedlák | 882a807 | 2019-07-08 17:51:20 +0200 | [diff] [blame] | 218 | 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] | 219 | |
| 220 | st = reset_state(state); |
| 221 | ret = yin_parse_module(st->ctx, |
| 222 | "", |
| 223 | st->mod); |
| 224 | assert_int_equal(ret, LY_EVALID); |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 225 | logbuf_assert("Invalid keyword \"(null)\", expected \"module\" or \"submodule\". Line number 1."); |
| 226 | st->finished_correctly = true; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static void |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 230 | test_yin_match_keyword(void **state) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 231 | { |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 232 | struct state *st = *state; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 233 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 234 | const char *prefix, *name; |
| 235 | struct yin_arg_record *args = NULL; |
| 236 | size_t prefix_len, name_len; |
| 237 | /* create mock yin namespace in xml context */ |
| 238 | 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] | 239 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 240 | yin_load_attributes(st->yin_ctx, &data, &args); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 241 | LY_ARRAY_FREE(args); |
| 242 | |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 243 | assert_int_equal(yin_match_keyword(st->yin_ctx, "anydatax", strlen("anydatax"), prefix, prefix_len, YANG_NONE), YANG_NONE); |
| 244 | assert_int_equal(yin_match_keyword(st->yin_ctx, "asdasd", strlen("asdasd"), prefix, prefix_len, YANG_NONE), YANG_NONE); |
| 245 | assert_int_equal(yin_match_keyword(st->yin_ctx, "", 0, prefix, prefix_len, YANG_NONE), YANG_NONE); |
| 246 | assert_int_equal(yin_match_keyword(st->yin_ctx, "anydata", strlen("anydata"), prefix, prefix_len, YANG_NONE), YANG_ANYDATA); |
| 247 | assert_int_equal(yin_match_keyword(st->yin_ctx, "anyxml", strlen("anyxml"), prefix, prefix_len, YANG_NONE), YANG_ANYXML); |
| 248 | assert_int_equal(yin_match_keyword(st->yin_ctx, "argument", strlen("argument"), prefix, prefix_len, YANG_NONE), YANG_ARGUMENT); |
| 249 | assert_int_equal(yin_match_keyword(st->yin_ctx, "augment", strlen("augment"), prefix, prefix_len, YANG_NONE), YANG_AUGMENT); |
| 250 | assert_int_equal(yin_match_keyword(st->yin_ctx, "base", strlen("base"), prefix, prefix_len, YANG_NONE), YANG_BASE); |
| 251 | assert_int_equal(yin_match_keyword(st->yin_ctx, "belongs-to", strlen("belongs-to"), prefix, prefix_len, YANG_NONE), YANG_BELONGS_TO); |
| 252 | assert_int_equal(yin_match_keyword(st->yin_ctx, "bit", strlen("bit"), prefix, prefix_len, YANG_NONE), YANG_BIT); |
| 253 | assert_int_equal(yin_match_keyword(st->yin_ctx, "case", strlen("case"), prefix, prefix_len, YANG_NONE), YANG_CASE); |
| 254 | assert_int_equal(yin_match_keyword(st->yin_ctx, "choice", strlen("choice"), prefix, prefix_len, YANG_NONE), YANG_CHOICE); |
| 255 | assert_int_equal(yin_match_keyword(st->yin_ctx, "config", strlen("config"), prefix, prefix_len, YANG_NONE), YANG_CONFIG); |
| 256 | assert_int_equal(yin_match_keyword(st->yin_ctx, "contact", strlen("contact"), prefix, prefix_len, YANG_NONE), YANG_CONTACT); |
| 257 | assert_int_equal(yin_match_keyword(st->yin_ctx, "container", strlen("container"), prefix, prefix_len, YANG_NONE), YANG_CONTAINER); |
| 258 | assert_int_equal(yin_match_keyword(st->yin_ctx, "default", strlen("default"), prefix, prefix_len, YANG_NONE), YANG_DEFAULT); |
| 259 | assert_int_equal(yin_match_keyword(st->yin_ctx, "description", strlen("description"), prefix, prefix_len, YANG_NONE), YANG_DESCRIPTION); |
| 260 | assert_int_equal(yin_match_keyword(st->yin_ctx, "deviate", strlen("deviate"), prefix, prefix_len, YANG_NONE), YANG_DEVIATE); |
| 261 | assert_int_equal(yin_match_keyword(st->yin_ctx, "deviation", strlen("deviation"), prefix, prefix_len, YANG_NONE), YANG_DEVIATION); |
| 262 | assert_int_equal(yin_match_keyword(st->yin_ctx, "enum", strlen("enum"), prefix, prefix_len, YANG_NONE), YANG_ENUM); |
| 263 | 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); |
| 264 | assert_int_equal(yin_match_keyword(st->yin_ctx, "error-message", strlen("error-message"), prefix, prefix_len, YANG_NONE), YANG_ERROR_MESSAGE); |
| 265 | assert_int_equal(yin_match_keyword(st->yin_ctx, "extension", strlen("extension"), prefix, prefix_len, YANG_NONE), YANG_EXTENSION); |
| 266 | assert_int_equal(yin_match_keyword(st->yin_ctx, "feature", strlen("feature"), prefix, prefix_len, YANG_NONE), YANG_FEATURE); |
| 267 | assert_int_equal(yin_match_keyword(st->yin_ctx, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len, YANG_NONE), YANG_FRACTION_DIGITS); |
| 268 | assert_int_equal(yin_match_keyword(st->yin_ctx, "grouping", strlen("grouping"), prefix, prefix_len, YANG_NONE), YANG_GROUPING); |
| 269 | assert_int_equal(yin_match_keyword(st->yin_ctx, "identity", strlen("identity"), prefix, prefix_len, YANG_NONE), YANG_IDENTITY); |
| 270 | assert_int_equal(yin_match_keyword(st->yin_ctx, "if-feature", strlen("if-feature"), prefix, prefix_len, YANG_NONE), YANG_IF_FEATURE); |
| 271 | assert_int_equal(yin_match_keyword(st->yin_ctx, "import", strlen("import"), prefix, prefix_len, YANG_NONE), YANG_IMPORT); |
| 272 | assert_int_equal(yin_match_keyword(st->yin_ctx, "include", strlen("include"), prefix, prefix_len, YANG_NONE), YANG_INCLUDE); |
| 273 | assert_int_equal(yin_match_keyword(st->yin_ctx, "input", strlen("input"), prefix, prefix_len, YANG_NONE), YANG_INPUT); |
| 274 | assert_int_equal(yin_match_keyword(st->yin_ctx, "key", strlen("key"), prefix, prefix_len, YANG_NONE), YANG_KEY); |
| 275 | assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf", strlen("leaf"), prefix, prefix_len, YANG_NONE), YANG_LEAF); |
| 276 | assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf-list", strlen("leaf-list"), prefix, prefix_len, YANG_NONE), YANG_LEAF_LIST); |
| 277 | assert_int_equal(yin_match_keyword(st->yin_ctx, "length", strlen("length"), prefix, prefix_len, YANG_NONE), YANG_LENGTH); |
| 278 | assert_int_equal(yin_match_keyword(st->yin_ctx, "list", strlen("list"), prefix, prefix_len, YANG_NONE), YANG_LIST); |
| 279 | assert_int_equal(yin_match_keyword(st->yin_ctx, "mandatory", strlen("mandatory"), prefix, prefix_len, YANG_NONE), YANG_MANDATORY); |
| 280 | assert_int_equal(yin_match_keyword(st->yin_ctx, "max-elements", strlen("max-elements"), prefix, prefix_len, YANG_NONE), YANG_MAX_ELEMENTS); |
| 281 | assert_int_equal(yin_match_keyword(st->yin_ctx, "min-elements", strlen("min-elements"), prefix, prefix_len, YANG_NONE), YANG_MIN_ELEMENTS); |
| 282 | assert_int_equal(yin_match_keyword(st->yin_ctx, "modifier", strlen("modifier"), prefix, prefix_len, YANG_NONE), YANG_MODIFIER); |
| 283 | assert_int_equal(yin_match_keyword(st->yin_ctx, "module", strlen("module"), prefix, prefix_len, YANG_NONE), YANG_MODULE); |
| 284 | assert_int_equal(yin_match_keyword(st->yin_ctx, "must", strlen("must"), prefix, prefix_len, YANG_NONE), YANG_MUST); |
| 285 | assert_int_equal(yin_match_keyword(st->yin_ctx, "namespace", strlen("namespace"), prefix, prefix_len, YANG_NONE), YANG_NAMESPACE); |
| 286 | assert_int_equal(yin_match_keyword(st->yin_ctx, "notification", strlen("notification"), prefix, prefix_len, YANG_NONE), YANG_NOTIFICATION); |
| 287 | assert_int_equal(yin_match_keyword(st->yin_ctx, "ordered-by", strlen("ordered-by"), prefix, prefix_len, YANG_NONE), YANG_ORDERED_BY); |
| 288 | assert_int_equal(yin_match_keyword(st->yin_ctx, "organization", strlen("organization"), prefix, prefix_len, YANG_NONE), YANG_ORGANIZATION); |
| 289 | assert_int_equal(yin_match_keyword(st->yin_ctx, "output", strlen("output"), prefix, prefix_len, YANG_NONE), YANG_OUTPUT); |
| 290 | assert_int_equal(yin_match_keyword(st->yin_ctx, "path", strlen("path"), prefix, prefix_len, YANG_NONE), YANG_PATH); |
| 291 | assert_int_equal(yin_match_keyword(st->yin_ctx, "pattern", strlen("pattern"), prefix, prefix_len, YANG_NONE), YANG_PATTERN); |
| 292 | assert_int_equal(yin_match_keyword(st->yin_ctx, "position", strlen("position"), prefix, prefix_len, YANG_NONE), YANG_POSITION); |
| 293 | assert_int_equal(yin_match_keyword(st->yin_ctx, "prefix", strlen("prefix"), prefix, prefix_len, YANG_NONE), YANG_PREFIX); |
| 294 | assert_int_equal(yin_match_keyword(st->yin_ctx, "presence", strlen("presence"), prefix, prefix_len, YANG_NONE), YANG_PRESENCE); |
| 295 | assert_int_equal(yin_match_keyword(st->yin_ctx, "range", strlen("range"), prefix, prefix_len, YANG_NONE), YANG_RANGE); |
| 296 | assert_int_equal(yin_match_keyword(st->yin_ctx, "reference", strlen("reference"), prefix, prefix_len, YANG_NONE), YANG_REFERENCE); |
| 297 | assert_int_equal(yin_match_keyword(st->yin_ctx, "refine", strlen("refine"), prefix, prefix_len, YANG_NONE), YANG_REFINE); |
| 298 | assert_int_equal(yin_match_keyword(st->yin_ctx, "require-instance", strlen("require-instance"), prefix, prefix_len, YANG_NONE), YANG_REQUIRE_INSTANCE); |
| 299 | assert_int_equal(yin_match_keyword(st->yin_ctx, "revision", strlen("revision"), prefix, prefix_len, YANG_NONE), YANG_REVISION); |
| 300 | assert_int_equal(yin_match_keyword(st->yin_ctx, "revision-date", strlen("revision-date"), prefix, prefix_len, YANG_NONE), YANG_REVISION_DATE); |
| 301 | assert_int_equal(yin_match_keyword(st->yin_ctx, "rpc", strlen("rpc"), prefix, prefix_len, YANG_NONE), YANG_RPC); |
| 302 | assert_int_equal(yin_match_keyword(st->yin_ctx, "status", strlen("status"), prefix, prefix_len, YANG_NONE), YANG_STATUS); |
| 303 | assert_int_equal(yin_match_keyword(st->yin_ctx, "submodule", strlen("submodule"), prefix, prefix_len, YANG_NONE), YANG_SUBMODULE); |
| 304 | assert_int_equal(yin_match_keyword(st->yin_ctx, "type", strlen("type"), prefix, prefix_len, YANG_NONE), YANG_TYPE); |
| 305 | assert_int_equal(yin_match_keyword(st->yin_ctx, "typedef", strlen("typedef"), prefix, prefix_len, YANG_NONE), YANG_TYPEDEF); |
| 306 | assert_int_equal(yin_match_keyword(st->yin_ctx, "unique", strlen("unique"), prefix, prefix_len, YANG_NONE), YANG_UNIQUE); |
| 307 | assert_int_equal(yin_match_keyword(st->yin_ctx, "units", strlen("units"), prefix, prefix_len, YANG_NONE), YANG_UNITS); |
| 308 | assert_int_equal(yin_match_keyword(st->yin_ctx, "uses", strlen("uses"), prefix, prefix_len, YANG_NONE), YANG_USES); |
| 309 | assert_int_equal(yin_match_keyword(st->yin_ctx, "value", strlen("value"), prefix, prefix_len, YANG_NONE), YANG_VALUE); |
| 310 | assert_int_equal(yin_match_keyword(st->yin_ctx, "when", strlen("when"), prefix, prefix_len, YANG_NONE), YANG_WHEN); |
| 311 | assert_int_equal(yin_match_keyword(st->yin_ctx, "yang-version", strlen("yang-version"), prefix, prefix_len, YANG_NONE), YANG_YANG_VERSION); |
| 312 | 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] | 313 | |
| 314 | st->finished_correctly = true; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 315 | } |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 316 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 317 | static void |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 318 | test_yin_match_argument_name(void **state) |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 319 | { |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 320 | (void)state; /* unused */ |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 321 | |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 322 | assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN); |
| 323 | assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN); |
| 324 | assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN); |
| 325 | assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION); |
| 326 | assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE); |
| 327 | assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE); |
| 328 | assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME); |
| 329 | assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG); |
| 330 | assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE); |
| 331 | assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT); |
| 332 | assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI); |
| 333 | 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] | 334 | } |
| 335 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 336 | static void |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 337 | test_yin_parse_element_generic(void **state) |
| 338 | { |
| 339 | const char *prefix, *name; |
| 340 | struct state *st = *state; |
| 341 | struct lysp_ext_instance exts; |
| 342 | size_t prefix_len, name_len; |
| 343 | LY_ERR ret; |
| 344 | |
| 345 | memset(&exts, 0, sizeof(exts)); |
| 346 | |
| 347 | const char *data = "<elem attr=\"value\">text_value</elem>"; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 348 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 349 | 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] | 350 | assert_int_equal(ret, LY_SUCCESS); |
David Sedlák | 1fdb252 | 2019-07-09 16:22:57 +0200 | [diff] [blame] | 351 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 352 | assert_string_equal(exts.child->stmt, "elem"); |
| 353 | assert_string_equal(exts.child->arg, "text_value"); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 354 | assert_string_equal(exts.child->child->stmt, "attr"); |
| 355 | assert_string_equal(exts.child->child->arg, "value"); |
| 356 | assert_true(exts.child->child->flags & LYS_YIN_ATTR); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 357 | lysp_ext_instance_free(st->ctx, &exts); |
David Sedlák | 5392a21 | 2019-07-01 09:19:10 +0200 | [diff] [blame] | 358 | st = reset_state(state); |
| 359 | |
| 360 | data = "<elem></elem>"; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 361 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 362 | 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] | 363 | assert_int_equal(ret, LY_SUCCESS); |
| 364 | assert_string_equal(exts.child->stmt, "elem"); |
| 365 | assert_null(exts.child->child); |
| 366 | assert_null(exts.child->arg); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 367 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
David Sedlák | 5392a21 | 2019-07-01 09:19:10 +0200 | [diff] [blame] | 368 | lysp_ext_instance_free(st->ctx, &exts); |
| 369 | |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 370 | st->finished_correctly = true; |
| 371 | } |
| 372 | |
| 373 | static void |
| 374 | test_yin_parse_extension_instance(void **state) |
| 375 | { |
| 376 | LY_ERR ret; |
| 377 | struct state *st = *state; |
| 378 | const char *prefix, *name; |
| 379 | size_t prefix_len, name_len; |
| 380 | struct yin_arg_record *args = NULL; |
| 381 | struct lysp_ext_instance *exts = NULL; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 382 | 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] | 383 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 384 | yin_load_attributes(st->yin_ctx, &data, &args); |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 385 | 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] | 386 | namelen2fulllen(name_len, prefix_len), LYEXT_SUBSTMT_CONTACT, 0, &exts); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 387 | assert_int_equal(ret, LY_SUCCESS); |
| 388 | assert_string_equal(exts->name, "ext"); |
| 389 | assert_int_equal(exts->insubstmt_index, 0); |
| 390 | assert_true(exts->insubstmt == LYEXT_SUBSTMT_CONTACT); |
| 391 | assert_true(exts->yin & LYS_YIN); |
| 392 | assert_string_equal(exts->child->stmt, "value1"); |
| 393 | assert_string_equal(exts->child->arg, "test"); |
| 394 | assert_null(exts->child->child); |
| 395 | assert_true(exts->child->flags & LYS_YIN_ATTR); |
| 396 | assert_string_equal(exts->child->next->stmt, "value"); |
| 397 | assert_string_equal(exts->child->next->arg, "test2"); |
| 398 | assert_null(exts->child->next->child); |
| 399 | assert_true(exts->child->next->flags & LYS_YIN_ATTR); |
| 400 | |
| 401 | assert_string_equal(exts->child->next->next->stmt, "subelem"); |
| 402 | assert_string_equal(exts->child->next->next->arg, "text"); |
| 403 | assert_null(exts->child->next->next->child); |
| 404 | assert_null(exts->child->next->next->next); |
| 405 | assert_false(exts->child->next->next->flags & LYS_YIN_ATTR); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 406 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 407 | LY_ARRAY_FREE(args); |
| 408 | lysp_ext_instance_free(st->ctx, exts); |
| 409 | LY_ARRAY_FREE(exts); |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 410 | exts = NULL; |
| 411 | args = NULL; |
| 412 | st = reset_state(state); |
| 413 | |
| 414 | data = "<extension-elem />"; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 415 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 416 | yin_load_attributes(st->yin_ctx, &data, &args); |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 417 | 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] | 418 | assert_int_equal(ret, LY_SUCCESS); |
| 419 | assert_string_equal(exts->name, "extension-elem"); |
| 420 | assert_null(exts->argument); |
| 421 | assert_null(exts->child); |
| 422 | assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT); |
| 423 | assert_int_equal(exts->insubstmt_index, 0); |
| 424 | assert_true(exts->yin & LYS_YIN); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 425 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 426 | LY_ARRAY_FREE(args); |
| 427 | lysp_ext_instance_free(st->ctx, exts); |
| 428 | LY_ARRAY_FREE(exts); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 429 | st->finished_correctly = true; |
| 430 | } |
| 431 | |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 432 | static void |
| 433 | test_yin_parse_content(void **state) |
| 434 | { |
| 435 | struct state *st = *state; |
| 436 | LY_ERR ret = LY_SUCCESS; |
| 437 | struct sized_string name, prefix; |
| 438 | const char *data = "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">" |
| 439 | "<custom xmlns=\"my-ext\">" |
| 440 | "totally amazing extension" |
| 441 | "</custom>" |
David Sedlák | 986cb41 | 2019-07-04 13:10:11 +0200 | [diff] [blame] | 442 | "<extension name=\"ext\">" |
| 443 | "<argument name=\"argname\"></argument>" |
| 444 | "<description><text>desc</text></description>" |
| 445 | "<reference><text>ref</text></reference>" |
| 446 | "<status value=\"deprecated\"></status>" |
| 447 | "</extension>" |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 448 | "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>" |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 449 | "<if-feature name=\"foo\"></if-feature>" |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 450 | "<when condition=\"condition...\">" |
| 451 | "<reference><text>when_ref</text></reference>" |
| 452 | "<description><text>when_desc</text></description>" |
| 453 | "</when>" |
David Sedlák | e1a3030 | 2019-07-10 13:49:38 +0200 | [diff] [blame] | 454 | "<config value=\"true\"/>" |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 455 | "<error-message>" |
| 456 | "<value>error-msg</value>" |
| 457 | "</error-message>" |
David Sedlák | 2ce1be6 | 2019-07-10 16:15:09 +0200 | [diff] [blame] | 458 | "<error-app-tag value=\"err-app-tag\"/>" |
David Sedlák | a5b1d38 | 2019-07-10 16:31:09 +0200 | [diff] [blame] | 459 | "<units name=\"radians\"></units>" |
David Sedlák | e7084ce | 2019-07-10 16:44:15 +0200 | [diff] [blame] | 460 | "<default value=\"default-value\"/>" |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 461 | "<position value=\"25\"></position>" |
| 462 | "<value value=\"-5\"/>" |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 463 | "<require-instance value=\"true\"></require-instance>" |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 464 | "<range value=\"5..10\" />" |
David Sedlák | 438ae43 | 2019-07-11 15:36:54 +0200 | [diff] [blame] | 465 | "<length value=\"baf\"/>" |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 466 | "<pattern value='pattern'>" |
| 467 | "<modifier value='invert-match'/>" |
| 468 | "</pattern>" |
David Sedlák | fd5b9c3 | 2019-07-12 15:33:13 +0200 | [diff] [blame] | 469 | "<enum name=\"yay\">" |
| 470 | "</enum>" |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 471 | "</prefix>"; |
| 472 | struct lysp_ext_instance *exts = NULL; |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 473 | const char **if_features = NULL; |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 474 | struct yin_arg_record *attrs = NULL; |
David Sedlák | e7084ce | 2019-07-10 16:44:15 +0200 | [diff] [blame] | 475 | const char *value, *err_msg, *app_tag, *units, *def; |
David Sedlák | 986cb41 | 2019-07-04 13:10:11 +0200 | [diff] [blame] | 476 | struct lysp_ext *ext_def = NULL; |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 477 | struct lysp_when *when_p = NULL; |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 478 | struct lysp_type_enum pos_enum = {}, val_enum = {}; |
David Sedlák | fd5b9c3 | 2019-07-12 15:33:13 +0200 | [diff] [blame] | 479 | struct lysp_type req_type = {}, range_type = {}, len_type = {}, patter_type = {}, enum_type = {}; |
David Sedlák | e1a3030 | 2019-07-10 13:49:38 +0200 | [diff] [blame] | 480 | uint8_t config = 0; |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 481 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 482 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 483 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 484 | |
David Sedlák | fd5b9c3 | 2019-07-12 15:33:13 +0200 | [diff] [blame] | 485 | struct yin_subelement subelems[17] = { |
David Sedlák | 438ae43 | 2019-07-11 15:36:54 +0200 | [diff] [blame] | 486 | {YANG_CONFIG, &config, 0}, |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 487 | {YANG_DEFAULT, &def, YIN_SUBELEM_UNIQUE}, |
David Sedlák | fd5b9c3 | 2019-07-12 15:33:13 +0200 | [diff] [blame] | 488 | {YANG_ENUM, &enum_type, 0}, |
David Sedlák | 438ae43 | 2019-07-11 15:36:54 +0200 | [diff] [blame] | 489 | {YANG_ERROR_APP_TAG, &app_tag, 0}, |
| 490 | {YANG_ERROR_MESSAGE, &err_msg, 0}, |
| 491 | {YANG_EXTENSION, &ext_def, 0}, |
| 492 | {YANG_IF_FEATURE, &if_features, 0}, |
| 493 | {YANG_LENGTH, &len_type, 0}, |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 494 | {YANG_PATTERN, &patter_type, 0}, |
David Sedlák | 438ae43 | 2019-07-11 15:36:54 +0200 | [diff] [blame] | 495 | {YANG_RANGE, &range_type, 0}, |
| 496 | {YANG_REQUIRE_INSTANCE, &req_type, 0}, |
| 497 | {YANG_UNITS, &units, 0}, |
David Sedlák | 374d2b3 | 2019-07-17 15:06:55 +0200 | [diff] [blame] | 498 | {YANG_POSITION, &pos_enum, 0}, |
David Sedlák | 438ae43 | 2019-07-11 15:36:54 +0200 | [diff] [blame] | 499 | {YANG_VALUE, &val_enum, 0}, |
| 500 | {YANG_WHEN, &when_p, 0}, |
| 501 | {YANG_CUSTOM, NULL, 0}, |
| 502 | {YIN_TEXT, &value, 0} |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 503 | }; |
David Sedlák | fd5b9c3 | 2019-07-12 15:33:13 +0200 | [diff] [blame] | 504 | ret = yin_parse_content(st->yin_ctx, subelems, 17, &data, YANG_PREFIX, NULL, &exts); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 505 | assert_int_equal(ret, LY_SUCCESS); |
David Sedlák | 1fdb252 | 2019-07-09 16:22:57 +0200 | [diff] [blame] | 506 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
David Sedlák | 2ce1be6 | 2019-07-10 16:15:09 +0200 | [diff] [blame] | 507 | /* check parsed values */ |
David Sedlák | e7084ce | 2019-07-10 16:44:15 +0200 | [diff] [blame] | 508 | assert_string_equal(def, "default-value"); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 509 | assert_string_equal(exts->name, "custom"); |
| 510 | assert_string_equal(exts->argument, "totally amazing extension"); |
| 511 | assert_string_equal(value, "wsefsdf"); |
David Sedlák | a5b1d38 | 2019-07-10 16:31:09 +0200 | [diff] [blame] | 512 | assert_string_equal(units, "radians"); |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 513 | assert_string_equal(when_p->cond, "condition..."); |
| 514 | assert_string_equal(when_p->dsc, "when_desc"); |
| 515 | assert_string_equal(when_p->ref, "when_ref"); |
David Sedlák | e1a3030 | 2019-07-10 13:49:38 +0200 | [diff] [blame] | 516 | assert_int_equal(config, LYS_CONFIG_W); |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 517 | assert_int_equal(pos_enum.value, 25); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 518 | assert_true(pos_enum.flags & LYS_SET_VALUE); |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 519 | assert_int_equal(val_enum.value, -5); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 520 | assert_true(val_enum.flags & LYS_SET_VALUE); |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 521 | assert_int_equal(req_type.require_instance, 1); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 522 | assert_true(req_type.flags &= LYS_SET_REQINST); |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 523 | assert_string_equal(range_type.range->arg, "5..10"); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 524 | assert_true(range_type.flags & LYS_SET_RANGE); |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 525 | assert_string_equal(err_msg, "error-msg"); |
David Sedlák | 2ce1be6 | 2019-07-10 16:15:09 +0200 | [diff] [blame] | 526 | assert_string_equal(app_tag, "err-app-tag"); |
David Sedlák | fd5b9c3 | 2019-07-12 15:33:13 +0200 | [diff] [blame] | 527 | assert_string_equal(enum_type.enums->name, "yay"); |
David Sedlák | 438ae43 | 2019-07-11 15:36:54 +0200 | [diff] [blame] | 528 | assert_string_equal(len_type.length->arg, "baf"); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 529 | assert_true(len_type.flags & LYS_SET_LENGTH); |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 530 | assert_string_equal(patter_type.patterns->arg, "\x015pattern"); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 531 | assert_true(patter_type.flags & LYS_SET_PATTERN); |
David Sedlák | 2ce1be6 | 2019-07-10 16:15:09 +0200 | [diff] [blame] | 532 | /* cleanup */ |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 533 | lysp_ext_instance_free(st->ctx, exts); |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 534 | lysp_when_free(st->ctx, when_p); |
David Sedlák | 986cb41 | 2019-07-04 13:10:11 +0200 | [diff] [blame] | 535 | lysp_ext_free(st->ctx, ext_def); |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 536 | FREE_STRING(st->ctx, *if_features); |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 537 | FREE_STRING(st->ctx, err_msg); |
David Sedlák | 2ce1be6 | 2019-07-10 16:15:09 +0200 | [diff] [blame] | 538 | FREE_STRING(st->ctx, app_tag); |
David Sedlák | a5b1d38 | 2019-07-10 16:31:09 +0200 | [diff] [blame] | 539 | FREE_STRING(st->ctx, units); |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 540 | FREE_STRING(st->ctx, patter_type.patterns->arg); |
David Sedlák | e7084ce | 2019-07-10 16:44:15 +0200 | [diff] [blame] | 541 | FREE_STRING(st->ctx, def); |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 542 | FREE_STRING(st->ctx, range_type.range->arg); |
David Sedlák | 438ae43 | 2019-07-11 15:36:54 +0200 | [diff] [blame] | 543 | FREE_STRING(st->ctx, len_type.length->arg); |
David Sedlák | fd5b9c3 | 2019-07-12 15:33:13 +0200 | [diff] [blame] | 544 | FREE_STRING(st->ctx, enum_type.enums->name); |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 545 | FREE_STRING(st->ctx, value); |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 546 | LY_ARRAY_FREE(if_features); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 547 | LY_ARRAY_FREE(exts); |
David Sedlák | 986cb41 | 2019-07-04 13:10:11 +0200 | [diff] [blame] | 548 | LY_ARRAY_FREE(ext_def); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 549 | LY_ARRAY_FREE(attrs); |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 550 | LY_ARRAY_FREE(patter_type.patterns); |
David Sedlák | fd5b9c3 | 2019-07-12 15:33:13 +0200 | [diff] [blame] | 551 | LY_ARRAY_FREE(enum_type.enums); |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 552 | free(when_p); |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 553 | free(range_type.range); |
David Sedlák | 438ae43 | 2019-07-11 15:36:54 +0200 | [diff] [blame] | 554 | free(len_type.length); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 555 | attrs = NULL; |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 556 | st = reset_state(state); |
| 557 | |
| 558 | /* test unique subelem */ |
| 559 | const char *prefix_value; |
| 560 | struct yin_subelement subelems2[2] = {{YANG_PREFIX, &prefix_value, 0}, |
| 561 | {YIN_TEXT, &value, YIN_SUBELEM_UNIQUE}}; |
| 562 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">" |
| 563 | "<prefix value=\"inv_mod\" />" |
| 564 | "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>" |
| 565 | "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>" |
| 566 | "</module>"; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 567 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 568 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 569 | 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] | 570 | assert_int_equal(ret, LY_EVALID); |
| 571 | logbuf_assert("Redefinition of text element in module element. Line number 1."); |
| 572 | lydict_remove(st->ctx, prefix_value); |
| 573 | lydict_remove(st->ctx, value); |
| 574 | st = reset_state(state); |
| 575 | LY_ARRAY_FREE(attrs); |
| 576 | attrs = NULL; |
| 577 | |
| 578 | /* test first subelem */ |
| 579 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">" |
| 580 | "<prefix value=\"inv_mod\" />" |
| 581 | "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>" |
| 582 | "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>" |
| 583 | "</module>"; |
| 584 | struct yin_subelement subelems3[2] = {{YANG_PREFIX, &prefix_value, 0}, |
| 585 | {YIN_TEXT, &value, YIN_SUBELEM_FIRST}}; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 586 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 587 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 588 | 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] | 589 | assert_int_equal(ret, LY_EVALID); |
| 590 | logbuf_assert("Subelement text of module element must be defined as first subelement. Line number 1."); |
| 591 | lydict_remove(st->ctx, prefix_value); |
| 592 | st = reset_state(state); |
| 593 | LY_ARRAY_FREE(attrs); |
| 594 | attrs = NULL; |
| 595 | |
| 596 | /* test mandatory subelem */ |
| 597 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">" |
| 598 | "</module>"; |
| 599 | 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] | 600 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 601 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 602 | 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] | 603 | assert_int_equal(ret, LY_EVALID); |
| 604 | logbuf_assert("Missing mandatory subelement prefix of module element. Line number 1."); |
| 605 | LY_ARRAY_FREE(attrs); |
| 606 | |
| 607 | st->finished_correctly = true; |
| 608 | } |
| 609 | |
David Sedlák | 92147b0 | 2019-07-09 14:01:01 +0200 | [diff] [blame] | 610 | static void |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 611 | test_validate_value(void **state) |
| 612 | { |
| 613 | struct state *st = *state; |
| 614 | assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "#invalid", 8), LY_EVALID); |
| 615 | logbuf_assert("Invalid identifier character '#'. Line number 1."); |
| 616 | assert_int_equal(yin_validate_value(st->yin_ctx, Y_STR_ARG, "", 0), LY_SUCCESS); |
| 617 | assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "pre:b", 5), LY_EVALID); |
| 618 | assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:b", 5), LY_SUCCESS); |
| 619 | assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:pre:b", 9), LY_EVALID); |
| 620 | |
| 621 | st->finished_correctly = true; |
| 622 | } |
| 623 | |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 624 | static int |
| 625 | setup_element_test(void **state) |
| 626 | { |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 627 | struct state *st = *state; |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 628 | |
| 629 | #if ENABLE_LOGGER_CHECKING |
| 630 | /* setup logger */ |
| 631 | ly_set_log_clb(logger, 1); |
| 632 | #endif |
| 633 | |
| 634 | /* reset logbuf */ |
| 635 | logbuf[0] = '\0'; |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 636 | |
| 637 | /* allocate parser context */ |
| 638 | st->yin_ctx = calloc(1, sizeof(*st->yin_ctx)); |
| 639 | st->yin_ctx->xml_ctx.ctx = st->ctx; |
| 640 | st->yin_ctx->xml_ctx.line = 1; |
| 641 | |
| 642 | return EXIT_SUCCESS; |
| 643 | } |
| 644 | |
| 645 | static int |
| 646 | teardown_element_test(void **state) |
| 647 | { |
| 648 | struct state *st = *(struct state **)state; |
| 649 | |
| 650 | #if ENABLE_LOGGER_CHECKING |
| 651 | /* teardown logger */ |
| 652 | if (!st->finished_correctly && logbuf[0] != '\0') { |
| 653 | fprintf(stderr, "%s\n", logbuf); |
| 654 | } |
| 655 | #endif |
| 656 | |
| 657 | lyxml_context_clear(&st->yin_ctx->xml_ctx); |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 658 | free(st->yin_ctx); |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 659 | |
| 660 | return EXIT_SUCCESS; |
| 661 | } |
| 662 | |
| 663 | #define ELEMENT_WRAPPER_START "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">" |
| 664 | #define ELEMENT_WRAPPER_END "</module>" |
| 665 | |
| 666 | /* helper function to simplify unit test of each element using parse_content function */ |
| 667 | LY_ERR |
| 668 | test_element_helper(struct state *st, const char **data, void *dest, const char **text, |
| 669 | struct lysp_ext_instance **exts, bool valid) |
| 670 | { |
| 671 | struct yin_arg_record *attrs = NULL; |
| 672 | struct sized_string name, prefix; |
| 673 | LY_ERR ret = LY_SUCCESS; |
| 674 | struct yin_subelement subelems[71] = { |
| 675 | {YANG_ACTION, dest, 0}, |
| 676 | {YANG_ANYDATA, dest, 0}, |
| 677 | {YANG_ANYXML, dest, 0}, |
| 678 | {YANG_ARGUMENT,dest, 0}, |
| 679 | {YANG_AUGMENT, dest, 0}, |
| 680 | {YANG_BASE, dest, 0}, |
| 681 | {YANG_BELONGS_TO, dest, 0}, |
| 682 | {YANG_BIT, dest, 0}, |
| 683 | {YANG_CASE, dest, 0}, |
| 684 | {YANG_CHOICE, dest, 0}, |
| 685 | {YANG_CONFIG, dest, 0}, |
| 686 | {YANG_CONTACT, dest, 0}, |
| 687 | {YANG_CONTAINER, dest, 0}, |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 688 | {YANG_DEFAULT, dest, YIN_SUBELEM_UNIQUE}, |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 689 | {YANG_DESCRIPTION, dest, 0}, |
| 690 | {YANG_DEVIATE, dest, 0}, |
| 691 | {YANG_DEVIATION, dest, 0}, |
| 692 | {YANG_ENUM, dest, 0}, |
| 693 | {YANG_ERROR_APP_TAG, dest, 0}, |
| 694 | {YANG_ERROR_MESSAGE, dest, 0}, |
| 695 | {YANG_EXTENSION, dest, 0}, |
| 696 | {YANG_FEATURE, dest, 0}, |
| 697 | {YANG_FRACTION_DIGITS, dest, 0}, |
| 698 | {YANG_GROUPING, dest, 0}, |
| 699 | {YANG_IDENTITY, dest, 0}, |
| 700 | {YANG_IF_FEATURE, dest, 0}, |
| 701 | {YANG_IMPORT, dest, 0}, |
| 702 | {YANG_INCLUDE, dest, 0}, |
| 703 | {YANG_INPUT, dest, 0}, |
| 704 | {YANG_KEY, dest, 0}, |
| 705 | {YANG_LEAF, dest, 0}, |
| 706 | {YANG_LEAF_LIST, dest, 0}, |
| 707 | {YANG_LENGTH, dest, 0}, |
| 708 | {YANG_LIST, dest, 0}, |
| 709 | {YANG_MANDATORY, dest, 0}, |
| 710 | {YANG_MAX_ELEMENTS, dest, 0}, |
| 711 | {YANG_MIN_ELEMENTS, dest, 0}, |
| 712 | {YANG_MODIFIER, dest, 0}, |
| 713 | {YANG_MODULE, dest, 0}, |
| 714 | {YANG_MUST, dest, 0}, |
| 715 | {YANG_NAMESPACE, dest, 0}, |
| 716 | {YANG_NOTIFICATION, dest, 0}, |
| 717 | {YANG_ORDERED_BY, dest, 0}, |
| 718 | {YANG_ORGANIZATION, dest, 0}, |
| 719 | {YANG_OUTPUT, dest, 0}, |
| 720 | {YANG_PATH, dest, 0}, |
| 721 | {YANG_PATTERN, dest, 0}, |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 722 | {YANG_PREFIX, dest, 0}, |
| 723 | {YANG_PRESENCE, dest, 0}, |
| 724 | {YANG_RANGE, dest, 0}, |
| 725 | {YANG_REFERENCE, dest, 0}, |
| 726 | {YANG_REFINE, dest, 0}, |
| 727 | {YANG_REQUIRE_INSTANCE, dest, 0}, |
| 728 | {YANG_REVISION, dest, 0}, |
| 729 | {YANG_REVISION_DATE, dest, 0}, |
| 730 | {YANG_RPC, dest, 0}, |
| 731 | {YANG_STATUS, dest, 0}, |
| 732 | {YANG_SUBMODULE, dest, 0}, |
| 733 | {YANG_TYPE, dest, 0}, |
| 734 | {YANG_TYPEDEF, dest, 0}, |
| 735 | {YANG_UNIQUE, dest, 0}, |
| 736 | {YANG_UNITS, dest, 0}, |
| 737 | {YANG_USES, dest, 0}, |
David Sedlák | 374d2b3 | 2019-07-17 15:06:55 +0200 | [diff] [blame] | 738 | {YANG_POSITION, dest, 0}, |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 739 | {YANG_VALUE, dest, 0}, |
| 740 | {YANG_WHEN, dest, 0}, |
| 741 | {YANG_YANG_VERSION, dest, 0}, |
| 742 | {YANG_YIN_ELEMENT, dest, 0}, |
| 743 | {YANG_CUSTOM, dest, 0}, |
| 744 | {YIN_TEXT, dest, 0}, |
| 745 | {YIN_VALUE, dest, 0} |
| 746 | }; |
| 747 | LY_CHECK_RET(lyxml_get_element(&st->yin_ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len));\ |
| 748 | LY_CHECK_RET(yin_load_attributes(st->yin_ctx, data, &attrs));\ |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 749 | ret = yin_parse_content(st->yin_ctx, subelems, 71, data, yin_match_keyword(st->yin_ctx, name.value, name.len, prefix.value, prefix.len, YANG_NONE), text, exts); |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 750 | LY_ARRAY_FREE(attrs); |
| 751 | if (valid) { |
| 752 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
| 753 | } |
| 754 | /* reset status */ |
| 755 | st->yin_ctx->xml_ctx.status = LYXML_ELEMENT; |
| 756 | return ret; |
| 757 | } |
| 758 | |
| 759 | static void |
| 760 | test_enum_bit_elem(void **state) |
| 761 | { |
| 762 | /* yin_parse_enum_bit is function that is being mainly tested by this test */ |
| 763 | struct state *st = *state; |
| 764 | struct lysp_type type = {}; |
| 765 | const char *data; |
| 766 | data = ELEMENT_WRAPPER_START |
| 767 | "<enum name=\"enum-name\">" |
| 768 | "<if-feature name=\"feature\" />" |
| 769 | "<value value=\"55\" />" |
| 770 | "<status value=\"deprecated\" />" |
| 771 | "<description><text>desc...</text></description>" |
| 772 | "<reference><text>ref...</text></reference>" |
| 773 | "</enum>" |
| 774 | ELEMENT_WRAPPER_END; |
| 775 | assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS); |
| 776 | assert_string_equal(*type.enums->iffeatures, "feature"); |
| 777 | assert_int_equal(type.enums->value, 55); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 778 | assert_true((type.enums->flags & LYS_STATUS_DEPRC) && (type.enums->flags & LYS_SET_VALUE)); |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 779 | assert_string_equal(type.enums->dsc, "desc..."); |
| 780 | assert_string_equal(type.enums->ref, "ref..."); |
| 781 | lysp_type_free(st->ctx, &type); |
| 782 | memset(&type, 0, sizeof type); |
| 783 | |
| 784 | /* todo bit element test */ |
| 785 | st->finished_correctly = true; |
| 786 | } |
| 787 | |
| 788 | static void |
| 789 | test_meta_elem(void **state) |
| 790 | { |
| 791 | struct state *st = *state; |
| 792 | char *value = NULL; |
| 793 | const char *data; |
| 794 | |
| 795 | /* organization element */ |
| 796 | data = ELEMENT_WRAPPER_START |
| 797 | "<organization><text>organization...</text></organization>" |
| 798 | ELEMENT_WRAPPER_END; |
| 799 | assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS); |
| 800 | assert_string_equal(value, "organization..."); |
| 801 | FREE_STRING(st->ctx, value); |
| 802 | value = NULL; |
| 803 | /* contact element */ |
| 804 | data = ELEMENT_WRAPPER_START |
| 805 | "<contact><text>contact...</text></contact>" |
| 806 | ELEMENT_WRAPPER_END; |
| 807 | assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS); |
| 808 | assert_string_equal(value, "contact..."); |
| 809 | FREE_STRING(st->ctx, value); |
| 810 | value = NULL; |
| 811 | /* description element */ |
| 812 | data = ELEMENT_WRAPPER_START |
| 813 | "<description><text>description...</text></description>" |
| 814 | ELEMENT_WRAPPER_END; |
| 815 | assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS); |
| 816 | assert_string_equal(value, "description..."); |
| 817 | FREE_STRING(st->ctx, value); |
| 818 | value = NULL; |
| 819 | /* reference element */ |
| 820 | data = ELEMENT_WRAPPER_START |
| 821 | "<reference><text>reference...</text></reference>" |
| 822 | ELEMENT_WRAPPER_END; |
| 823 | assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS); |
| 824 | assert_string_equal(value, "reference..."); |
| 825 | FREE_STRING(st->ctx, value); |
| 826 | value = NULL; |
| 827 | |
| 828 | /* missing text subelement */ |
| 829 | data = ELEMENT_WRAPPER_START |
| 830 | "<reference>reference...</reference>" |
| 831 | ELEMENT_WRAPPER_END; |
| 832 | assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, false), LY_EVALID); |
| 833 | logbuf_assert("Missing mandatory subelement text of reference element. Line number 1."); |
| 834 | |
| 835 | st->finished_correctly = true; |
| 836 | } |
| 837 | |
| 838 | static void |
| 839 | test_import_elem(void **state) |
| 840 | { |
| 841 | struct state *st = *state; |
| 842 | const char *data; |
| 843 | struct lys_module *lys_mod = calloc(1, sizeof *lys_mod); |
| 844 | struct lysp_module *lysp_mod = calloc(1, sizeof *lysp_mod); |
| 845 | lys_mod->ctx = st->ctx; |
| 846 | lysp_mod->mod = lys_mod; |
| 847 | |
| 848 | /* max subelems */ |
| 849 | data = ELEMENT_WRAPPER_START |
| 850 | "<import module=\"a\">" |
| 851 | "<prefix value=\"a_mod\"/>" |
| 852 | "<revision-date date=\"2015-01-01\"></revision-date>" |
| 853 | "<description><text>import description</text></description>" |
| 854 | "<reference><text>import reference</text></reference>" |
| 855 | "</import>" |
| 856 | ELEMENT_WRAPPER_END; |
| 857 | assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, true), LY_SUCCESS); |
| 858 | assert_string_equal(lysp_mod->imports->name, "a"); |
| 859 | assert_string_equal(lysp_mod->imports->prefix, "a_mod"); |
| 860 | assert_string_equal(lysp_mod->imports->rev, "2015-01-01"); |
| 861 | assert_string_equal(lysp_mod->imports->dsc, "import description"); |
| 862 | assert_string_equal(lysp_mod->imports->ref, "import reference"); |
| 863 | lysp_module_free(lysp_mod); |
| 864 | lys_module_free(lys_mod, NULL); |
| 865 | |
| 866 | /* min subelems */ |
| 867 | lys_mod = calloc(1, sizeof *lys_mod); |
| 868 | lysp_mod = calloc(1, sizeof *lysp_mod); |
| 869 | lys_mod->ctx = st->ctx; |
| 870 | lysp_mod->mod = lys_mod; |
| 871 | data = ELEMENT_WRAPPER_START |
| 872 | "<import module=\"a\">" |
| 873 | "<prefix value=\"a_mod\"/>" |
| 874 | "</import>" |
| 875 | ELEMENT_WRAPPER_END; |
| 876 | assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, true), LY_SUCCESS); |
| 877 | assert_string_equal(lysp_mod->imports->prefix, "a_mod"); |
| 878 | lysp_module_free(lysp_mod); |
| 879 | lys_module_free(lys_mod, NULL); |
| 880 | |
| 881 | /* invalid (missing prefix) */ |
| 882 | lys_mod = calloc(1, sizeof *lys_mod); |
| 883 | lysp_mod = calloc(1, sizeof *lysp_mod); |
| 884 | lys_mod->ctx = st->ctx; |
| 885 | lysp_mod->mod = lys_mod; |
| 886 | data = ELEMENT_WRAPPER_START "<import module=\"a\">""</import>" ELEMENT_WRAPPER_END; |
| 887 | assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, false), LY_EVALID); |
| 888 | logbuf_assert("Missing mandatory subelement prefix of import element. Line number 1."); |
| 889 | lysp_module_free(lysp_mod); |
| 890 | lys_module_free(lys_mod, NULL); |
| 891 | |
| 892 | /* invalid reused prefix */ |
| 893 | lys_mod = calloc(1, sizeof *lys_mod); |
| 894 | lysp_mod = calloc(1, sizeof *lysp_mod); |
| 895 | lys_mod->ctx = st->ctx; |
| 896 | lysp_mod->mod = lys_mod; |
| 897 | data = ELEMENT_WRAPPER_START |
| 898 | "<import module=\"a\">" |
| 899 | "<prefix value=\"a_mod\"/>" |
| 900 | "</import>" |
| 901 | "<import module=\"a\">" |
| 902 | "<prefix value=\"a_mod\"/>" |
| 903 | "</import>" |
| 904 | ELEMENT_WRAPPER_END; |
| 905 | assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, false), LY_EVALID); |
| 906 | logbuf_assert("Prefix \"a_mod\" already used to import \"a\" module. Line number 1."); |
| 907 | lysp_module_free(lysp_mod); |
| 908 | lys_module_free(lys_mod, NULL); |
| 909 | |
| 910 | st->finished_correctly = true; |
| 911 | } |
| 912 | |
| 913 | static void |
| 914 | test_status_elem(void **state) |
| 915 | { |
| 916 | struct state *st = *state; |
| 917 | const char *data; |
| 918 | uint16_t flags = 0; |
| 919 | |
| 920 | /* test valid values */ |
| 921 | data = ELEMENT_WRAPPER_START "<status value=\"current\" />" ELEMENT_WRAPPER_END; |
| 922 | assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 923 | assert_true(flags & LYS_STATUS_CURR); |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 924 | |
| 925 | data = ELEMENT_WRAPPER_START "<status value=\"deprecated\" />" ELEMENT_WRAPPER_END; |
| 926 | assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 927 | assert_true(flags & LYS_STATUS_DEPRC); |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 928 | |
| 929 | data = ELEMENT_WRAPPER_START "<status value=\"obsolete\"></status>" ELEMENT_WRAPPER_END; |
| 930 | assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 931 | assert_true(flags & LYS_STATUS_OBSLT); |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 932 | |
| 933 | /* test invalid value */ |
| 934 | data = ELEMENT_WRAPPER_START "<status value=\"invalid\"></status>" ELEMENT_WRAPPER_END; |
| 935 | assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID); |
| 936 | logbuf_assert("Invalid value \"invalid\" of \"status\". Line number 1."); |
| 937 | st->finished_correctly = true; |
| 938 | } |
| 939 | |
| 940 | static void |
| 941 | test_ext_elem(void **state) |
| 942 | { |
| 943 | struct state *st = *state; |
| 944 | const char *data; |
| 945 | struct lysp_ext *ext = NULL; |
| 946 | |
| 947 | /* max subelems */ |
| 948 | data = ELEMENT_WRAPPER_START |
| 949 | "<extension name=\"ext_name\">" |
| 950 | "<argument name=\"arg\"></argument>" |
| 951 | "<status value=\"current\"/>" |
| 952 | "<description><text>ext_desc</text></description>" |
| 953 | "<reference><text>ext_ref</text></reference>" |
| 954 | "</extension>" |
| 955 | ELEMENT_WRAPPER_END; |
| 956 | assert_int_equal(test_element_helper(st, &data, &ext, NULL, NULL, true), LY_SUCCESS); |
| 957 | assert_string_equal(ext->name, "ext_name"); |
| 958 | assert_string_equal(ext->argument, "arg"); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 959 | assert_true(ext->flags & LYS_STATUS_CURR); |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 960 | assert_string_equal(ext->dsc, "ext_desc"); |
| 961 | assert_string_equal(ext->ref, "ext_ref"); |
| 962 | lysp_ext_free(st->ctx, ext); |
| 963 | LY_ARRAY_FREE(ext); |
| 964 | ext = NULL; |
| 965 | |
| 966 | /* min subelems */ |
| 967 | data = ELEMENT_WRAPPER_START "<extension name=\"ext_name\"></extension>" ELEMENT_WRAPPER_END; |
| 968 | assert_int_equal(test_element_helper(st, &data, &ext, NULL, NULL, true), LY_SUCCESS); |
| 969 | assert_string_equal(ext->name, "ext_name"); |
| 970 | lysp_ext_free(st->ctx, ext); |
| 971 | LY_ARRAY_FREE(ext); |
| 972 | ext = NULL; |
| 973 | |
| 974 | st->finished_correctly = true; |
| 975 | } |
| 976 | |
| 977 | static void |
| 978 | test_yin_element_elem(void **state) |
| 979 | { |
| 980 | struct state *st = *state; |
| 981 | const char *data; |
| 982 | uint16_t flags = 0; |
| 983 | |
| 984 | data = ELEMENT_WRAPPER_START "<yin-element value=\"true\" />" ELEMENT_WRAPPER_END; |
| 985 | assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 986 | assert_true(flags & LYS_YINELEM_TRUE); |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 987 | |
| 988 | data = ELEMENT_WRAPPER_START "<yin-element value=\"false\" />" ELEMENT_WRAPPER_END; |
| 989 | assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 990 | assert_true(flags & LYS_YINELEM_TRUE); |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 991 | |
| 992 | data = ELEMENT_WRAPPER_START "<yin-element value=\"invalid\" />" ELEMENT_WRAPPER_END; |
| 993 | assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 994 | assert_true(flags & LYS_YINELEM_TRUE); |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 995 | logbuf_assert("Invalid value \"invalid\" of \"yin-element\". Line number 1."); |
| 996 | st->finished_correctly = true; |
| 997 | } |
| 998 | |
| 999 | static void |
| 1000 | test_yangversion_elem(void **state) |
| 1001 | { |
| 1002 | struct state *st = *state; |
| 1003 | const char *data; |
| 1004 | uint8_t version = 0; |
| 1005 | |
| 1006 | /* valid values */ |
| 1007 | data = ELEMENT_WRAPPER_START "<yang-version value=\"1.0\" />" ELEMENT_WRAPPER_END; |
| 1008 | assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, true), LY_SUCCESS); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1009 | assert_true(version & LYS_VERSION_1_0); |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 1010 | assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_0); |
| 1011 | |
| 1012 | data = ELEMENT_WRAPPER_START "<yang-version value=\"1.1\" />" ELEMENT_WRAPPER_END; |
| 1013 | assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, true), LY_SUCCESS); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1014 | assert_true(version & LYS_VERSION_1_1); |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 1015 | assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_1); |
| 1016 | |
| 1017 | /* invalid value */ |
| 1018 | data = ELEMENT_WRAPPER_START "<yang-version value=\"version\" />" ELEMENT_WRAPPER_END; |
| 1019 | assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, false), LY_EVALID); |
| 1020 | logbuf_assert("Invalid value \"version\" of \"yang-version\". Line number 1."); |
| 1021 | |
| 1022 | st->finished_correctly = true; |
| 1023 | } |
| 1024 | |
| 1025 | static void |
| 1026 | test_mandatory_elem(void **state) |
| 1027 | { |
| 1028 | struct state *st = *state; |
| 1029 | const char *data; |
| 1030 | uint16_t man = 0; |
| 1031 | |
| 1032 | /* valid values */ |
| 1033 | data = ELEMENT_WRAPPER_START "<mandatory value=\"true\" />" ELEMENT_WRAPPER_END; |
| 1034 | assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, true), LY_SUCCESS); |
| 1035 | assert_int_equal(man, LYS_MAND_TRUE); |
| 1036 | man = 0; |
| 1037 | |
| 1038 | data = ELEMENT_WRAPPER_START "<mandatory value=\"false\" />" ELEMENT_WRAPPER_END; |
| 1039 | assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, true), LY_SUCCESS); |
| 1040 | assert_int_equal(man, LYS_MAND_FALSE); |
| 1041 | |
| 1042 | data = ELEMENT_WRAPPER_START "<mandatory value=\"invalid\" />" ELEMENT_WRAPPER_END; |
| 1043 | assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, false), LY_EVALID); |
| 1044 | logbuf_assert("Invalid value \"invalid\" of \"mandatory\". Line number 1."); |
| 1045 | |
| 1046 | st->finished_correctly = true; |
| 1047 | } |
| 1048 | |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 1049 | static void |
| 1050 | test_argument_elem(void **state) |
| 1051 | { |
| 1052 | struct state *st = *state; |
| 1053 | const char *data; |
| 1054 | uint16_t flags = 0; |
| 1055 | const char *arg; |
| 1056 | struct yin_argument_meta arg_meta = {&flags, &arg}; |
| 1057 | /* max subelems */ |
| 1058 | data = ELEMENT_WRAPPER_START |
| 1059 | "<argument name=\"arg-name\">" |
| 1060 | "<yin-element value=\"true\" />" |
| 1061 | "</argument>" |
| 1062 | ELEMENT_WRAPPER_END; |
| 1063 | assert_int_equal(test_element_helper(st, &data, &arg_meta, NULL, NULL, true), LY_SUCCESS); |
| 1064 | assert_string_equal(arg, "arg-name"); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1065 | assert_true(flags & LYS_YINELEM_TRUE); |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 1066 | flags = 0; |
| 1067 | FREE_STRING(st->ctx, arg); |
| 1068 | arg = NULL; |
| 1069 | |
| 1070 | /* min subelems */ |
| 1071 | data = ELEMENT_WRAPPER_START |
| 1072 | "<argument name=\"arg\">" |
| 1073 | "</argument>" |
| 1074 | ELEMENT_WRAPPER_END; |
| 1075 | assert_int_equal(test_element_helper(st, &data, &arg_meta, NULL, NULL, true), LY_SUCCESS); |
| 1076 | assert_string_equal(arg, "arg"); |
| 1077 | assert_true(flags == 0); |
| 1078 | FREE_STRING(st->ctx, arg); |
| 1079 | |
| 1080 | st->finished_correctly = true; |
| 1081 | } |
| 1082 | |
| 1083 | static void |
| 1084 | test_base_elem(void **state) |
| 1085 | { |
| 1086 | struct state *st = *state; |
| 1087 | const char *data; |
| 1088 | const char **bases = NULL; |
| 1089 | struct lysp_type type = {}; |
| 1090 | |
| 1091 | /* as identity subelement */ |
| 1092 | data = "<identity xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">" |
| 1093 | "<base name=\"base-name\"/>" |
| 1094 | "</identity>"; |
| 1095 | assert_int_equal(test_element_helper(st, &data, &bases, NULL, NULL, true), LY_SUCCESS); |
| 1096 | assert_string_equal(*bases, "base-name"); |
| 1097 | FREE_STRING(st->ctx, *bases); |
| 1098 | LY_ARRAY_FREE(bases); |
| 1099 | |
| 1100 | /* as type subelement */ |
| 1101 | data = "<type xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">" |
| 1102 | "<base name=\"base-name\"/>" |
| 1103 | "</type>"; |
| 1104 | assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS); |
| 1105 | assert_string_equal(*type.bases, "base-name"); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1106 | assert_true(type.flags & LYS_SET_BASE); |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 1107 | FREE_STRING(st->ctx, *type.bases); |
| 1108 | LY_ARRAY_FREE(type.bases); |
| 1109 | |
| 1110 | st->finished_correctly = true; |
| 1111 | } |
| 1112 | |
| 1113 | static void |
| 1114 | test_belongsto_elem(void **state) |
| 1115 | { |
| 1116 | struct state *st = *state; |
| 1117 | const char *data; |
| 1118 | struct lysp_submodule submod; |
| 1119 | |
| 1120 | data = ELEMENT_WRAPPER_START |
| 1121 | "<belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>" |
| 1122 | ELEMENT_WRAPPER_END; |
| 1123 | assert_int_equal(test_element_helper(st, &data, &submod, NULL, NULL, true), LY_SUCCESS); |
| 1124 | assert_string_equal(submod.belongsto, "module-name"); |
| 1125 | assert_string_equal(submod.prefix, "pref"); |
| 1126 | FREE_STRING(st->ctx, submod.belongsto); |
| 1127 | FREE_STRING(st->ctx, submod.prefix); |
| 1128 | |
| 1129 | data = ELEMENT_WRAPPER_START "<belongs-to module=\"module-name\"></belongs-to>" ELEMENT_WRAPPER_END; |
| 1130 | assert_int_equal(test_element_helper(st, &data, &submod, NULL, NULL, false), LY_EVALID); |
| 1131 | logbuf_assert("Missing mandatory subelement prefix of belongs-to element. Line number 1."); |
| 1132 | FREE_STRING(st->ctx, submod.belongsto); |
| 1133 | |
| 1134 | st->finished_correctly = true; |
| 1135 | } |
| 1136 | |
| 1137 | static void |
| 1138 | test_config_elem(void **state) |
| 1139 | { |
| 1140 | struct state *st = *state; |
| 1141 | const char *data; |
| 1142 | uint16_t flags = 0; |
| 1143 | |
| 1144 | data = ELEMENT_WRAPPER_START "<config value=\"true\"/>" ELEMENT_WRAPPER_END; |
| 1145 | assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1146 | assert_true(flags & LYS_CONFIG_W); |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 1147 | flags = 0; |
| 1148 | |
| 1149 | data = ELEMENT_WRAPPER_START "<config value=\"false\"/>" ELEMENT_WRAPPER_END; |
| 1150 | assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1151 | assert_true(flags & LYS_CONFIG_R); |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 1152 | flags = 0; |
| 1153 | |
| 1154 | data = ELEMENT_WRAPPER_START "<config value=\"invalid\"/>" ELEMENT_WRAPPER_END; |
| 1155 | assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID); |
| 1156 | logbuf_assert("Invalid value \"invalid\" of \"config\". Line number 1."); |
| 1157 | |
| 1158 | st->finished_correctly = true; |
| 1159 | } |
| 1160 | |
| 1161 | static void |
| 1162 | test_default_elem(void **state) |
| 1163 | { |
| 1164 | struct state *st = *state; |
| 1165 | const char *data; |
| 1166 | const char *val = NULL; |
| 1167 | |
| 1168 | data = ELEMENT_WRAPPER_START "<default value=\"defaul-value\"/>" ELEMENT_WRAPPER_END; |
| 1169 | assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS); |
| 1170 | assert_string_equal(val, "defaul-value"); |
| 1171 | FREE_STRING(st->ctx, val); |
| 1172 | val = NULL; |
| 1173 | |
| 1174 | data = ELEMENT_WRAPPER_START "<default/>" ELEMENT_WRAPPER_END; |
| 1175 | assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID); |
| 1176 | logbuf_assert("Missing mandatory attribute value of default element. Line number 1."); |
| 1177 | |
| 1178 | st->finished_correctly = true; |
| 1179 | } |
| 1180 | |
| 1181 | static void |
| 1182 | test_err_app_tag_elem(void **state) |
| 1183 | { |
| 1184 | struct state *st = *state; |
| 1185 | const char *data; |
| 1186 | const char *val = NULL; |
| 1187 | |
| 1188 | data = ELEMENT_WRAPPER_START "<error-app-tag value=\"val\"/>" ELEMENT_WRAPPER_END; |
| 1189 | assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS); |
| 1190 | assert_string_equal(val, "val"); |
| 1191 | FREE_STRING(st->ctx, val); |
| 1192 | val = NULL; |
| 1193 | |
| 1194 | data = ELEMENT_WRAPPER_START "<error-app-tag/>" ELEMENT_WRAPPER_END; |
| 1195 | assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID); |
| 1196 | logbuf_assert("Missing mandatory attribute value of error-app-tag element. Line number 1."); |
| 1197 | |
| 1198 | st->finished_correctly = true; |
| 1199 | } |
| 1200 | |
| 1201 | static void |
| 1202 | test_err_msg_elem(void **state) |
| 1203 | { |
| 1204 | struct state *st = *state; |
| 1205 | const char *data; |
| 1206 | const char *val = NULL; |
| 1207 | |
| 1208 | data = ELEMENT_WRAPPER_START "<error-message><value>val</value></error-message>" ELEMENT_WRAPPER_END; |
| 1209 | assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS); |
| 1210 | assert_string_equal(val, "val"); |
| 1211 | FREE_STRING(st->ctx, val); |
| 1212 | |
| 1213 | data = ELEMENT_WRAPPER_START "<error-message></error-message>" ELEMENT_WRAPPER_END; |
| 1214 | assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID); |
| 1215 | logbuf_assert("Missing mandatory subelement value of error-message element. Line number 1."); |
| 1216 | |
| 1217 | st->finished_correctly = true; |
| 1218 | } |
| 1219 | |
| 1220 | static void |
| 1221 | test_fracdigits_elem(void **state) |
| 1222 | { |
| 1223 | struct state *st = *state; |
| 1224 | const char *data; |
| 1225 | struct lysp_type type = {}; |
| 1226 | |
| 1227 | /* valid value */ |
| 1228 | data = ELEMENT_WRAPPER_START "<fraction-digits value=\"10\"></fraction-digits>" ELEMENT_WRAPPER_END; |
| 1229 | assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS); |
| 1230 | assert_int_equal(type.fraction_digits, 10); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1231 | assert_true(type.flags & LYS_SET_FRDIGITS); |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 1232 | |
| 1233 | /* invalid values */ |
| 1234 | data = ELEMENT_WRAPPER_START "<fraction-digits value=\"-1\"></fraction-digits>" ELEMENT_WRAPPER_END; |
| 1235 | assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID); |
| 1236 | logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1."); |
| 1237 | |
| 1238 | data = ELEMENT_WRAPPER_START "<fraction-digits value=\"02\"></fraction-digits>" ELEMENT_WRAPPER_END; |
| 1239 | assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID); |
| 1240 | logbuf_assert("Invalid value \"02\" of \"fraction-digits\". Line number 1."); |
| 1241 | |
| 1242 | data = ELEMENT_WRAPPER_START "<fraction-digits value=\"1p\"></fraction-digits>" ELEMENT_WRAPPER_END; |
| 1243 | assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID); |
| 1244 | logbuf_assert("Invalid value \"1p\" of \"fraction-digits\". Line number 1."); |
| 1245 | |
| 1246 | data = ELEMENT_WRAPPER_START "<fraction-digits value=\"19\"></fraction-digits>" ELEMENT_WRAPPER_END; |
| 1247 | assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID); |
| 1248 | logbuf_assert("Invalid value \"19\" of \"fraction-digits\". Line number 1."); |
| 1249 | |
| 1250 | data = ELEMENT_WRAPPER_START "<fraction-digits value=\"999999999999999999\"></fraction-digits>" ELEMENT_WRAPPER_END; |
| 1251 | assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID); |
| 1252 | logbuf_assert("Invalid value \"999999999999999999\" of \"fraction-digits\". Line number 1."); |
| 1253 | |
| 1254 | st->finished_correctly = true; |
| 1255 | } |
| 1256 | |
| 1257 | static void |
| 1258 | test_iffeature_elem(void **state) |
| 1259 | { |
| 1260 | struct state *st = *state; |
| 1261 | const char *data; |
| 1262 | const char **iffeatures = NULL; |
| 1263 | |
| 1264 | data = ELEMENT_WRAPPER_START "<if-feature name=\"local-storage\"></if-feature>" ELEMENT_WRAPPER_END; |
| 1265 | assert_int_equal(test_element_helper(st, &data, &iffeatures, NULL, NULL, true), LY_SUCCESS); |
| 1266 | assert_string_equal(*iffeatures, "local-storage"); |
| 1267 | FREE_STRING(st->ctx, *iffeatures); |
| 1268 | LY_ARRAY_FREE(iffeatures); |
| 1269 | iffeatures = NULL; |
| 1270 | |
| 1271 | data = ELEMENT_WRAPPER_START "<if-feature/>" ELEMENT_WRAPPER_END; |
| 1272 | assert_int_equal(test_element_helper(st, &data, &iffeatures, NULL, NULL, false), LY_EVALID); |
| 1273 | logbuf_assert("Missing mandatory attribute name of if-feature element. Line number 1."); |
| 1274 | LY_ARRAY_FREE(iffeatures); |
| 1275 | iffeatures = NULL; |
| 1276 | |
| 1277 | st->finished_correctly = true; |
| 1278 | } |
| 1279 | |
| 1280 | static void |
| 1281 | test_length_elem(void **state) |
| 1282 | { |
| 1283 | struct state *st = *state; |
| 1284 | const char *data; |
| 1285 | struct lysp_type type = {}; |
| 1286 | |
| 1287 | /* max subelems */ |
| 1288 | data = ELEMENT_WRAPPER_START |
| 1289 | "<length value=\"length-str\">" |
| 1290 | "<error-message><value>err-msg</value></error-message>" |
| 1291 | "<error-app-tag value=\"err-app-tag\"/>" |
| 1292 | "<description><text>desc</text></description>" |
| 1293 | "<reference><text>ref</text></reference>" |
| 1294 | "</length>" |
| 1295 | ELEMENT_WRAPPER_END; |
| 1296 | assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS); |
| 1297 | assert_string_equal(type.length->arg, "length-str"); |
| 1298 | assert_string_equal(type.length->emsg, "err-msg"); |
| 1299 | assert_string_equal(type.length->eapptag, "err-app-tag"); |
| 1300 | assert_string_equal(type.length->dsc, "desc"); |
| 1301 | assert_string_equal(type.length->ref, "ref"); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1302 | assert_true(type.flags & LYS_SET_LENGTH); |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 1303 | lysp_type_free(st->ctx, &type); |
| 1304 | memset(&type, 0, sizeof(type)); |
| 1305 | |
| 1306 | /* min subelems */ |
| 1307 | data = ELEMENT_WRAPPER_START |
| 1308 | "<length value=\"length-str\">" |
| 1309 | "</length>" |
| 1310 | ELEMENT_WRAPPER_END; |
| 1311 | assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS); |
| 1312 | assert_string_equal(type.length->arg, "length-str"); |
| 1313 | lysp_type_free(st->ctx, &type); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1314 | assert_true(type.flags & LYS_SET_LENGTH); |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 1315 | memset(&type, 0, sizeof(type)); |
| 1316 | |
| 1317 | data = ELEMENT_WRAPPER_START "<length></length>" ELEMENT_WRAPPER_END; |
| 1318 | assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID); |
| 1319 | logbuf_assert("Missing mandatory attribute value of length element. Line number 1."); |
| 1320 | lysp_type_free(st->ctx, &type); |
| 1321 | memset(&type, 0, sizeof(type)); |
| 1322 | |
| 1323 | st->finished_correctly = true; |
| 1324 | } |
| 1325 | |
| 1326 | static void |
| 1327 | test_modifier_elem(void **state) |
| 1328 | { |
| 1329 | struct state *st = *state; |
| 1330 | const char *data; |
| 1331 | const char *pat = lydict_insert(st->ctx, "\006pattern", 8); |
| 1332 | |
| 1333 | data = ELEMENT_WRAPPER_START "<modifier value=\"invert-match\" />" ELEMENT_WRAPPER_END; |
| 1334 | assert_int_equal(test_element_helper(st, &data, &pat, NULL, NULL, true), LY_SUCCESS); |
| 1335 | assert_string_equal(pat, "\x015pattern"); |
| 1336 | FREE_STRING(st->ctx, pat); |
| 1337 | |
| 1338 | pat = lydict_insert(st->ctx, "\006pattern", 8); |
| 1339 | data = ELEMENT_WRAPPER_START "<modifier value=\"invert\" />" ELEMENT_WRAPPER_END; |
| 1340 | assert_int_equal(test_element_helper(st, &data, &pat, NULL, NULL, false), LY_EVALID); |
| 1341 | logbuf_assert("Invalid value \"invert\" of \"modifier\". Line number 1."); |
| 1342 | FREE_STRING(st->ctx, pat); |
| 1343 | |
| 1344 | st->finished_correctly = true; |
| 1345 | } |
| 1346 | |
| 1347 | static void |
| 1348 | test_namespace_elem(void **state) |
| 1349 | { |
| 1350 | struct state *st = *state; |
| 1351 | const char *data; |
| 1352 | const char *ns; |
| 1353 | |
| 1354 | data = ELEMENT_WRAPPER_START "<namespace uri=\"ns\"/>" ELEMENT_WRAPPER_END; |
| 1355 | assert_int_equal(test_element_helper(st, &data, &ns, NULL, NULL, true), LY_SUCCESS); |
| 1356 | assert_string_equal(ns, "ns"); |
| 1357 | FREE_STRING(st->ctx, ns); |
| 1358 | |
| 1359 | data = ELEMENT_WRAPPER_START "<namespace/>" ELEMENT_WRAPPER_END; |
| 1360 | assert_int_equal(test_element_helper(st, &data, &ns, NULL, NULL, false), LY_EVALID); |
| 1361 | logbuf_assert("Missing mandatory attribute uri of namespace element. Line number 1."); |
| 1362 | |
| 1363 | st->finished_correctly = true; |
| 1364 | } |
| 1365 | |
| 1366 | static void |
| 1367 | test_path_elem(void **state) |
| 1368 | { |
| 1369 | struct state *st = *state; |
| 1370 | const char *data; |
| 1371 | struct lysp_type type = {}; |
| 1372 | |
| 1373 | data = ELEMENT_WRAPPER_START "<path value=\"path-val\"/>" ELEMENT_WRAPPER_END; |
| 1374 | assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS); |
| 1375 | assert_string_equal("path-val", type.path); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1376 | assert_true(type.flags & LYS_SET_PATH); |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 1377 | lysp_type_free(st->ctx, &type); |
| 1378 | |
| 1379 | st->finished_correctly = true; |
| 1380 | } |
| 1381 | |
| 1382 | static void |
| 1383 | test_pattern_elem(void **state) |
| 1384 | { |
| 1385 | struct state *st = *state; |
| 1386 | const char *data; |
| 1387 | struct lysp_type type = {}; |
| 1388 | |
| 1389 | /* max subelems */ |
| 1390 | data = ELEMENT_WRAPPER_START |
| 1391 | "<pattern value=\"super_pattern\">" |
| 1392 | "<modifier value=\"invert-match\"/>" |
| 1393 | "<error-message><value>err-msg-value</value></error-message>" |
| 1394 | "<error-app-tag value=\"err-app-tag-value\"/>" |
| 1395 | "<description><text>pattern-desc</text></description>" |
| 1396 | "<reference><text>pattern-ref</text></reference>" |
| 1397 | "</pattern>" |
| 1398 | ELEMENT_WRAPPER_END; |
| 1399 | assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1400 | assert_true(type.flags & LYS_SET_PATTERN); |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 1401 | assert_string_equal(type.patterns->arg, "\x015super_pattern"); |
| 1402 | assert_string_equal(type.patterns->dsc, "pattern-desc"); |
| 1403 | assert_string_equal(type.patterns->eapptag, "err-app-tag-value"); |
| 1404 | assert_string_equal(type.patterns->emsg, "err-msg-value"); |
| 1405 | assert_string_equal(type.patterns->dsc, "pattern-desc"); |
| 1406 | assert_string_equal(type.patterns->ref, "pattern-ref"); |
| 1407 | lysp_type_free(st->ctx, &type); |
| 1408 | memset(&type, 0, sizeof(type)); |
| 1409 | |
| 1410 | /* min subelems */ |
| 1411 | data = ELEMENT_WRAPPER_START "<pattern value=\"pattern\"> </pattern>" ELEMENT_WRAPPER_END; |
| 1412 | assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS); |
| 1413 | assert_string_equal(type.patterns->arg, "\x006pattern"); |
| 1414 | lysp_type_free(st->ctx, &type); |
| 1415 | memset(&type, 0, sizeof(type)); |
| 1416 | |
| 1417 | st->finished_correctly = true; |
| 1418 | } |
| 1419 | |
| 1420 | static void |
| 1421 | test_value_position_elem(void **state) |
| 1422 | { |
| 1423 | struct state *st = *state; |
| 1424 | const char *data; |
| 1425 | struct lysp_type_enum en = {}; |
| 1426 | |
| 1427 | /* valid values */ |
| 1428 | data = ELEMENT_WRAPPER_START "<value value=\"55\" />" ELEMENT_WRAPPER_END; |
| 1429 | assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS); |
| 1430 | assert_int_equal(en.value, 55); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1431 | assert_true(en.flags & LYS_SET_VALUE); |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 1432 | memset(&en, 0, sizeof(en)); |
| 1433 | |
| 1434 | data = ELEMENT_WRAPPER_START "<value value=\"-55\"/>" ELEMENT_WRAPPER_END; |
| 1435 | assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS); |
| 1436 | assert_int_equal(en.value, -55); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1437 | assert_true(en.flags & LYS_SET_VALUE); |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 1438 | memset(&en, 0, sizeof(en)); |
| 1439 | |
| 1440 | data = ELEMENT_WRAPPER_START "<value value=\"0\"/>" ELEMENT_WRAPPER_END; |
| 1441 | assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS); |
| 1442 | assert_int_equal(en.value, 0); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1443 | assert_true(en.flags & LYS_SET_VALUE); |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 1444 | memset(&en, 0, sizeof(en)); |
| 1445 | |
| 1446 | data = ELEMENT_WRAPPER_START "<value value=\"-0\"/>" ELEMENT_WRAPPER_END; |
| 1447 | assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS); |
| 1448 | assert_int_equal(en.value, 0); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1449 | assert_true(en.flags & LYS_SET_VALUE); |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 1450 | memset(&en, 0, sizeof(en)); |
| 1451 | |
| 1452 | /* valid positions */ |
| 1453 | data = ELEMENT_WRAPPER_START "<position value=\"55\" />" ELEMENT_WRAPPER_END; |
| 1454 | assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS); |
| 1455 | assert_int_equal(en.value, 55); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1456 | assert_true(en.flags & LYS_SET_VALUE); |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 1457 | memset(&en, 0, sizeof(en)); |
| 1458 | |
| 1459 | data = ELEMENT_WRAPPER_START "<position value=\"0\" />" ELEMENT_WRAPPER_END; |
| 1460 | assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS); |
| 1461 | assert_int_equal(en.value, 0); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1462 | assert_true(en.flags & LYS_SET_VALUE); |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 1463 | memset(&en, 0, sizeof(en)); |
| 1464 | |
| 1465 | /* invalid values */ |
| 1466 | data = ELEMENT_WRAPPER_START "<value value=\"99999999999999999999999\"/>" ELEMENT_WRAPPER_END; |
| 1467 | assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID); |
| 1468 | logbuf_assert("Invalid value \"99999999999999999999999\" of \"value\". Line number 1."); |
| 1469 | |
| 1470 | data = ELEMENT_WRAPPER_START "<value value=\"1k\"/>" ELEMENT_WRAPPER_END; |
| 1471 | assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID); |
| 1472 | logbuf_assert("Invalid value \"1k\" of \"value\". Line number 1."); |
| 1473 | |
David Sedlák | 69f0161 | 2019-07-17 11:41:08 +0200 | [diff] [blame] | 1474 | data = ELEMENT_WRAPPER_START "<value value=\"\"/>" ELEMENT_WRAPPER_END; |
| 1475 | assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID); |
| 1476 | logbuf_assert("Invalid value \"\" of \"value\". Line number 1."); |
| 1477 | |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 1478 | /*invalid positions */ |
| 1479 | data = ELEMENT_WRAPPER_START "<position value=\"-5\"/>" ELEMENT_WRAPPER_END; |
| 1480 | assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID); |
| 1481 | logbuf_assert("Invalid value \"-5\" of \"position\". Line number 1."); |
| 1482 | |
| 1483 | data = ELEMENT_WRAPPER_START "<position value=\"-0\"/>" ELEMENT_WRAPPER_END; |
| 1484 | assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID); |
| 1485 | logbuf_assert("Invalid value \"-0\" of \"position\". Line number 1."); |
| 1486 | |
| 1487 | data = ELEMENT_WRAPPER_START "<position value=\"99999999999999999999\"/>" ELEMENT_WRAPPER_END; |
| 1488 | assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID); |
| 1489 | logbuf_assert("Invalid value \"99999999999999999999\" of \"position\". Line number 1."); |
| 1490 | |
David Sedlák | 69f0161 | 2019-07-17 11:41:08 +0200 | [diff] [blame] | 1491 | data = ELEMENT_WRAPPER_START "<position value=\"\"/>" ELEMENT_WRAPPER_END; |
| 1492 | assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID); |
| 1493 | logbuf_assert("Invalid value \"\" of \"position\". Line number 1."); |
| 1494 | |
| 1495 | st->finished_correctly = true; |
| 1496 | } |
| 1497 | |
| 1498 | static void |
| 1499 | test_prefix_elem(void **state) |
| 1500 | { |
| 1501 | struct state *st = *state; |
| 1502 | const char *data; |
| 1503 | const char *value = NULL; |
| 1504 | |
| 1505 | data = ELEMENT_WRAPPER_START "<prefix value=\"pref\"/>" ELEMENT_WRAPPER_END; |
| 1506 | assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS); |
| 1507 | assert_string_equal(value, "pref"); |
| 1508 | FREE_STRING(st->ctx, value); |
| 1509 | |
| 1510 | st->finished_correctly = true; |
| 1511 | } |
| 1512 | |
| 1513 | static void |
| 1514 | test_range_elem(void **state) |
| 1515 | { |
| 1516 | struct state *st = *state; |
| 1517 | const char *data; |
| 1518 | struct lysp_type type = {}; |
| 1519 | |
| 1520 | /* max subelems */ |
| 1521 | data = ELEMENT_WRAPPER_START |
| 1522 | "<range value=\"range-str\">" |
| 1523 | "<error-message><value>err-msg</value></error-message>" |
| 1524 | "<error-app-tag value=\"err-app-tag\" />" |
| 1525 | "<description><text>desc</text></description>" |
| 1526 | "<reference><text>ref</text></reference>" |
| 1527 | "</range>" |
| 1528 | ELEMENT_WRAPPER_END; |
| 1529 | assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS); |
| 1530 | assert_string_equal(type.range->arg, "range-str"); |
| 1531 | assert_string_equal(type.range->dsc, "desc"); |
| 1532 | assert_string_equal(type.range->eapptag, "err-app-tag"); |
| 1533 | assert_string_equal(type.range->emsg, "err-msg"); |
| 1534 | assert_string_equal(type.range->ref, "ref"); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1535 | assert_true(type.flags & LYS_SET_RANGE); |
David Sedlák | 69f0161 | 2019-07-17 11:41:08 +0200 | [diff] [blame] | 1536 | lysp_type_free(st->ctx, &type); |
| 1537 | memset(&type, 0, sizeof(type)); |
| 1538 | |
| 1539 | /* min subelems */ |
| 1540 | data = ELEMENT_WRAPPER_START "<range value=\"range-str\"/>" ELEMENT_WRAPPER_END; |
| 1541 | assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS); |
| 1542 | assert_string_equal(type.range->arg, "range-str"); |
| 1543 | lysp_type_free(st->ctx, &type); |
| 1544 | memset(&type, 0, sizeof(type)); |
| 1545 | |
| 1546 | st->finished_correctly = true; |
| 1547 | } |
| 1548 | |
| 1549 | static void |
| 1550 | test_reqinstance_elem(void **state) |
| 1551 | { |
| 1552 | struct state *st = *state; |
| 1553 | const char *data; |
| 1554 | struct lysp_type type = {}; |
| 1555 | |
| 1556 | data = ELEMENT_WRAPPER_START "<require-instance value=\"true\"/>" ELEMENT_WRAPPER_END; |
| 1557 | assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS); |
| 1558 | assert_int_equal(type.require_instance, 1); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1559 | assert_true(type.flags & LYS_SET_REQINST); |
David Sedlák | 69f0161 | 2019-07-17 11:41:08 +0200 | [diff] [blame] | 1560 | memset(&type, 0, sizeof(type)); |
| 1561 | |
| 1562 | data = ELEMENT_WRAPPER_START "<require-instance value=\"false\"/>" ELEMENT_WRAPPER_END; |
| 1563 | assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS); |
| 1564 | assert_int_equal(type.require_instance, 0); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1565 | assert_true(type.flags & LYS_SET_REQINST); |
David Sedlák | 69f0161 | 2019-07-17 11:41:08 +0200 | [diff] [blame] | 1566 | memset(&type, 0, sizeof(type)); |
| 1567 | |
| 1568 | data = ELEMENT_WRAPPER_START "<require-instance value=\"invalid\"/>" ELEMENT_WRAPPER_END; |
| 1569 | assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID); |
| 1570 | memset(&type, 0, sizeof(type)); |
| 1571 | logbuf_assert("Invalid value \"invalid\" of \"require-instance\". Line number 1."); |
| 1572 | |
| 1573 | st->finished_correctly = true; |
| 1574 | } |
| 1575 | |
| 1576 | static void |
| 1577 | test_revision_date_elem(void **state) |
| 1578 | { |
| 1579 | struct state *st = *state; |
| 1580 | const char *data; |
| 1581 | char rev[LY_REV_SIZE]; |
| 1582 | |
| 1583 | data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\"/>" ELEMENT_WRAPPER_END; |
| 1584 | assert_int_equal(test_element_helper(st, &data, rev, NULL, NULL, true), LY_SUCCESS); |
| 1585 | assert_string_equal(rev, "2000-01-01"); |
| 1586 | |
| 1587 | data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-50-05\"/>" ELEMENT_WRAPPER_END; |
| 1588 | assert_int_equal(test_element_helper(st, &data, rev, NULL, NULL, false), LY_EVALID); |
| 1589 | logbuf_assert("Invalid value \"2000-50-05\" of \"revision-date\". Line number 1."); |
| 1590 | |
| 1591 | st->finished_correctly = true; |
| 1592 | } |
| 1593 | |
| 1594 | static void |
| 1595 | test_unique_elem(void **state) |
| 1596 | { |
| 1597 | struct state *st = *state; |
| 1598 | const char *data; |
| 1599 | const char **values = NULL; |
| 1600 | |
| 1601 | data = ELEMENT_WRAPPER_START "<unique tag=\"tag\"/>" ELEMENT_WRAPPER_END; |
| 1602 | assert_int_equal(test_element_helper(st, &data, &values, NULL, NULL, true), LY_SUCCESS); |
| 1603 | assert_string_equal(*values, "tag"); |
| 1604 | FREE_STRING(st->ctx, *values); |
| 1605 | LY_ARRAY_FREE(values); |
| 1606 | |
| 1607 | st->finished_correctly = true; |
| 1608 | } |
| 1609 | |
| 1610 | static void |
| 1611 | test_units_elem(void **state) |
| 1612 | { |
| 1613 | struct state *st = *state; |
| 1614 | const char *data; |
| 1615 | const char *values = NULL; |
| 1616 | |
| 1617 | data = ELEMENT_WRAPPER_START "<units name=\"name\"/>" ELEMENT_WRAPPER_END; |
| 1618 | assert_int_equal(test_element_helper(st, &data, &values, NULL, NULL, true), LY_SUCCESS); |
| 1619 | assert_string_equal(values, "name"); |
| 1620 | FREE_STRING(st->ctx, values); |
| 1621 | |
| 1622 | st->finished_correctly = true; |
| 1623 | } |
| 1624 | |
| 1625 | static void |
| 1626 | test_when_elem(void **state) |
| 1627 | { |
| 1628 | struct state *st = *state; |
| 1629 | const char *data; |
| 1630 | struct lysp_when *when = NULL; |
| 1631 | |
| 1632 | data = ELEMENT_WRAPPER_START |
| 1633 | "<when condition=\"cond\">" |
| 1634 | "<description><text>desc</text></description>" |
| 1635 | "<reference><text>ref</text></reference>" |
| 1636 | "</when>" |
| 1637 | ELEMENT_WRAPPER_END; |
| 1638 | assert_int_equal(test_element_helper(st, &data, &when, NULL, NULL, true), LY_SUCCESS); |
| 1639 | assert_string_equal(when->cond, "cond"); |
| 1640 | assert_string_equal(when->dsc, "desc"); |
| 1641 | assert_string_equal(when->ref, "ref"); |
| 1642 | lysp_when_free(st->ctx, when); |
| 1643 | free(when); |
| 1644 | when = NULL; |
| 1645 | |
| 1646 | data = ELEMENT_WRAPPER_START "<when condition=\"cond\" />" ELEMENT_WRAPPER_END; |
| 1647 | assert_int_equal(test_element_helper(st, &data, &when, NULL, NULL, true), LY_SUCCESS); |
| 1648 | assert_string_equal(when->cond, "cond"); |
| 1649 | lysp_when_free(st->ctx, when); |
| 1650 | free(when); |
| 1651 | when = NULL; |
| 1652 | |
| 1653 | st->finished_correctly = true; |
| 1654 | } |
| 1655 | |
| 1656 | static void |
| 1657 | test_yin_text_value_elem(void **state) |
| 1658 | { |
| 1659 | struct state *st = *state; |
| 1660 | const char *data; |
| 1661 | const char *val; |
| 1662 | |
| 1663 | data = ELEMENT_WRAPPER_START "<text>text</text>" ELEMENT_WRAPPER_END; |
| 1664 | assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS); |
| 1665 | assert_string_equal(val, "text"); |
| 1666 | FREE_STRING(st->ctx, val); |
| 1667 | |
| 1668 | data = "<error-message xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <value>text</value> </error-message>"; |
| 1669 | assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS); |
| 1670 | assert_string_equal(val, "text"); |
| 1671 | FREE_STRING(st->ctx, val); |
| 1672 | |
| 1673 | data = ELEMENT_WRAPPER_START "<text></text>" ELEMENT_WRAPPER_END; |
| 1674 | assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS); |
| 1675 | assert_string_equal("", val); |
| 1676 | FREE_STRING(st->ctx, val); |
| 1677 | |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 1678 | st->finished_correctly = true; |
| 1679 | } |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 1680 | |
David Sedlák | 374d2b3 | 2019-07-17 15:06:55 +0200 | [diff] [blame] | 1681 | static void |
| 1682 | test_type_elem(void **state) |
| 1683 | { |
| 1684 | struct state *st = *state; |
| 1685 | const char *data; |
| 1686 | struct lysp_type type = {}; |
| 1687 | |
| 1688 | /* max subelems */ |
| 1689 | data = ELEMENT_WRAPPER_START |
| 1690 | "<type name=\"type-name\">" |
| 1691 | "<base name=\"base-name\"/>" |
| 1692 | "<bit name=\"bit\"/>" |
| 1693 | "<enum name=\"enum\"/>" |
| 1694 | "<fraction-digits value=\"2\"/>" |
| 1695 | "<length value=\"length\"/>" |
| 1696 | "<path value=\"path\"/>" |
| 1697 | "<pattern value=\"pattern\"/>" |
| 1698 | "<range value=\"range\" />" |
| 1699 | "<require-instance value=\"true\"/>" |
| 1700 | "<type name=\"sub-type-name\"/>" |
| 1701 | "</type>" |
| 1702 | ELEMENT_WRAPPER_END; |
| 1703 | assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS); |
| 1704 | assert_string_equal(type.name, "type-name"); |
| 1705 | assert_string_equal(*type.bases, "base-name"); |
| 1706 | assert_string_equal(type.bits->name, "bit"); |
| 1707 | assert_string_equal(type.enums->name, "enum"); |
| 1708 | assert_int_equal(type.fraction_digits, 2); |
| 1709 | assert_string_equal(type.length->arg, "length"); |
| 1710 | assert_string_equal(type.path, "path"); |
| 1711 | assert_string_equal(type.patterns->arg, "\006pattern"); |
| 1712 | assert_string_equal(type.range->arg, "range"); |
| 1713 | assert_int_equal(type.require_instance, 1); |
| 1714 | assert_string_equal(type.types->name, "sub-type-name"); |
| 1715 | lysp_type_free(st->ctx, &type); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1716 | assert_true(type.flags & LYS_SET_BASE); |
| 1717 | assert_true(type.flags & LYS_SET_BIT); |
| 1718 | assert_true(type.flags & LYS_SET_ENUM); |
| 1719 | assert_true(type.flags & LYS_SET_FRDIGITS); |
| 1720 | assert_true(type.flags & LYS_SET_LENGTH); |
| 1721 | assert_true(type.flags & LYS_SET_PATH); |
| 1722 | assert_true(type.flags & LYS_SET_PATTERN); |
| 1723 | assert_true(type.flags & LYS_SET_RANGE); |
| 1724 | assert_true(type.flags & LYS_SET_REQINST); |
| 1725 | assert_true(type.flags & LYS_SET_TYPE); |
David Sedlák | 374d2b3 | 2019-07-17 15:06:55 +0200 | [diff] [blame] | 1726 | memset(&type, 0, sizeof(type)); |
| 1727 | |
| 1728 | /* min subelems */ |
| 1729 | data = ELEMENT_WRAPPER_START "<type name=\"type-name\"/>" ELEMENT_WRAPPER_END; |
| 1730 | assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS); |
| 1731 | lysp_type_free(st->ctx, &type); |
| 1732 | memset(&type, 0, sizeof(type)); |
| 1733 | |
| 1734 | st->finished_correctly = true; |
| 1735 | } |
| 1736 | |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1737 | static void |
| 1738 | test_max_elems_elem(void **state) |
| 1739 | { |
| 1740 | struct state *st = *state; |
| 1741 | const char *data; |
| 1742 | struct lysp_node_list list = {}; |
| 1743 | struct lysp_node_leaflist llist = {}; |
| 1744 | struct lysp_refine refine = {}; |
| 1745 | |
| 1746 | data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"unbounded\"/> </refine>"; |
| 1747 | assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS); |
| 1748 | assert_int_equal(refine.max, 0); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1749 | assert_true(refine.flags & LYS_SET_MAX); |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1750 | |
| 1751 | data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"5\"/> </list>"; |
| 1752 | assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, true), LY_SUCCESS); |
| 1753 | assert_int_equal(list.max, 5); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1754 | assert_true(list.flags & LYS_SET_MAX); |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1755 | |
| 1756 | data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"85\"/> </leaf-list>"; |
| 1757 | assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, true), LY_SUCCESS); |
| 1758 | assert_int_equal(llist.max, 85); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1759 | assert_true(llist.flags & LYS_SET_MAX); |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1760 | |
| 1761 | data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"10\"/> </refine>"; |
| 1762 | assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS); |
| 1763 | assert_int_equal(refine.max, 10); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1764 | assert_true(refine.flags & LYS_SET_MAX); |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 1765 | |
| 1766 | data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"0\"/> </list>"; |
| 1767 | assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID); |
| 1768 | logbuf_assert("Invalid value \"0\" of \"max-elements\". Line number 1."); |
| 1769 | |
| 1770 | data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"-10\"/> </list>"; |
| 1771 | assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID); |
| 1772 | logbuf_assert("Invalid value \"-10\" of \"max-elements\". Line number 1."); |
| 1773 | |
| 1774 | data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"k\"/> </list>"; |
| 1775 | assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID); |
| 1776 | logbuf_assert("Invalid value \"k\" of \"max-elements\". Line number 1."); |
| 1777 | |
| 1778 | data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"u12\"/> </list>"; |
| 1779 | assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID); |
| 1780 | logbuf_assert("Invalid value \"u12\" of \"max-elements\". Line number 1."); |
| 1781 | |
| 1782 | st->finished_correctly = true; |
| 1783 | } |
| 1784 | |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1785 | static void |
| 1786 | test_min_elems_elem(void **state) |
| 1787 | { |
| 1788 | struct state *st = *state; |
| 1789 | const char *data; |
| 1790 | struct lysp_node_list list = {}; |
| 1791 | struct lysp_node_leaflist llist = {}; |
| 1792 | struct lysp_refine refine = {}; |
| 1793 | |
| 1794 | data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"0\"/> </refine>"; |
| 1795 | assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS); |
| 1796 | assert_int_equal(refine.min, 0); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1797 | assert_true(refine.flags & LYS_SET_MIN); |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1798 | |
| 1799 | data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"41\"/> </list>"; |
| 1800 | assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, true), LY_SUCCESS); |
| 1801 | assert_int_equal(list.min, 41); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1802 | assert_true(list.flags & LYS_SET_MIN); |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1803 | |
| 1804 | data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"50\"/> </leaf-list>"; |
| 1805 | assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, true), LY_SUCCESS); |
| 1806 | assert_int_equal(llist.min, 50); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1807 | assert_true(llist.flags & LYS_SET_MIN); |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 1808 | |
| 1809 | data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"-5\"/> </leaf-list>"; |
| 1810 | assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID); |
| 1811 | logbuf_assert("Value \"-5\" is out of \"min-elements\" bounds. Line number 1."); |
| 1812 | |
| 1813 | data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"99999999999999999\"/> </leaf-list>"; |
| 1814 | assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID); |
| 1815 | logbuf_assert("Value \"99999999999999999\" is out of \"min-elements\" bounds. Line number 1."); |
| 1816 | |
| 1817 | data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"5k\"/> </leaf-list>"; |
| 1818 | assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID); |
| 1819 | logbuf_assert("Invalid value \"5k\" of \"min-elements\". Line number 1."); |
| 1820 | |
| 1821 | data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"05\"/> </leaf-list>"; |
| 1822 | assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID); |
| 1823 | logbuf_assert("Invalid value \"05\" of \"min-elements\". Line number 1."); |
| 1824 | |
| 1825 | st->finished_correctly = true; |
| 1826 | } |
| 1827 | |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1828 | static void |
| 1829 | test_ordby_elem(void **state) |
| 1830 | { |
| 1831 | struct state *st = *state; |
| 1832 | const char *data; |
| 1833 | uint16_t flags = 0; |
| 1834 | |
| 1835 | data = ELEMENT_WRAPPER_START "<ordered-by value=\"system\"/>" ELEMENT_WRAPPER_END; |
| 1836 | assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1837 | assert_true(flags & LYS_ORDBY_SYSTEM); |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1838 | |
| 1839 | data = ELEMENT_WRAPPER_START "<ordered-by value=\"user\"/>" ELEMENT_WRAPPER_END; |
| 1840 | assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1841 | assert_true(flags & LYS_ORDBY_USER); |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 1842 | |
| 1843 | data = ELEMENT_WRAPPER_START "<ordered-by value=\"inv\"/>" ELEMENT_WRAPPER_END; |
| 1844 | assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID); |
| 1845 | logbuf_assert("Invalid value \"inv\" of \"ordered-by\". Line number 1."); |
| 1846 | |
| 1847 | st->finished_correctly = true; |
| 1848 | } |
| 1849 | |
David Sedlák | 8a83bbb | 2019-07-18 14:46:00 +0200 | [diff] [blame] | 1850 | static void |
| 1851 | test_any_elem(void **state) |
| 1852 | { |
| 1853 | struct state *st = *state; |
| 1854 | const char *data; |
| 1855 | struct lysp_node *siblings = NULL; |
| 1856 | struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings}; |
| 1857 | struct lysp_node_anydata *parsed = NULL; |
| 1858 | |
| 1859 | /* anyxml max subelems */ |
| 1860 | data = ELEMENT_WRAPPER_START |
| 1861 | "<anyxml name=\"any-name\">" |
| 1862 | "<config value=\"true\" />" |
| 1863 | "<description><text>desc</text></description>" |
| 1864 | "<if-feature name=\"feature\" />" |
| 1865 | "<mandatory value=\"true\" />" |
| 1866 | "<must condition=\"must-cond\" />" |
| 1867 | "<reference><text>ref</text></reference>" |
| 1868 | "<status value=\"deprecated\"/>" |
| 1869 | "<when condition=\"when-cond\"/>" |
| 1870 | "</anyxml>" |
| 1871 | ELEMENT_WRAPPER_END; |
| 1872 | assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS); |
| 1873 | parsed = (struct lysp_node_anydata *)siblings; |
| 1874 | assert_null(parsed->parent); |
| 1875 | assert_int_equal(parsed->nodetype, LYS_ANYXML); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1876 | assert_true(parsed->flags & LYS_CONFIG_W); |
| 1877 | assert_true(parsed->flags & LYS_MAND_TRUE); |
| 1878 | assert_true(parsed->flags & LYS_STATUS_DEPRC); |
David Sedlák | 8a83bbb | 2019-07-18 14:46:00 +0200 | [diff] [blame] | 1879 | assert_null(parsed->next); |
| 1880 | assert_string_equal(parsed->name, "any-name"); |
| 1881 | assert_string_equal(parsed->dsc, "desc"); |
| 1882 | assert_string_equal(parsed->ref, "ref"); |
| 1883 | assert_string_equal(parsed->when->cond, "when-cond"); |
| 1884 | assert_string_equal(*parsed->iffeatures, "feature"); |
| 1885 | assert_null(parsed->exts); |
| 1886 | lysp_node_free(st->ctx, siblings); |
| 1887 | siblings = NULL; |
| 1888 | |
| 1889 | /* anydata max subelems */ |
| 1890 | data = ELEMENT_WRAPPER_START |
| 1891 | "<anydata name=\"any-name\">" |
| 1892 | "<config value=\"true\" />" |
| 1893 | "<description><text>desc</text></description>" |
| 1894 | "<if-feature name=\"feature\" />" |
| 1895 | "<mandatory value=\"true\" />" |
| 1896 | "<must condition=\"must-cond\" />" |
| 1897 | "<reference><text>ref</text></reference>" |
| 1898 | "<status value=\"deprecated\"/>" |
| 1899 | "<when condition=\"when-cond\"/>" |
| 1900 | "</anydata>" |
| 1901 | ELEMENT_WRAPPER_END; |
| 1902 | assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS); |
| 1903 | parsed = (struct lysp_node_anydata *)siblings; |
| 1904 | assert_null(parsed->parent); |
| 1905 | assert_int_equal(parsed->nodetype, LYS_ANYDATA); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1906 | assert_true(parsed->flags & LYS_CONFIG_W); |
| 1907 | assert_true(parsed->flags & LYS_MAND_TRUE); |
| 1908 | assert_true(parsed->flags & LYS_STATUS_DEPRC); |
David Sedlák | 8a83bbb | 2019-07-18 14:46:00 +0200 | [diff] [blame] | 1909 | assert_null(parsed->next); |
| 1910 | assert_string_equal(parsed->name, "any-name"); |
| 1911 | assert_string_equal(parsed->dsc, "desc"); |
| 1912 | assert_string_equal(parsed->ref, "ref"); |
| 1913 | assert_string_equal(parsed->when->cond, "when-cond"); |
| 1914 | assert_string_equal(*parsed->iffeatures, "feature"); |
| 1915 | assert_null(parsed->exts); |
| 1916 | lysp_node_free(st->ctx, siblings); |
| 1917 | siblings = NULL; |
| 1918 | |
| 1919 | /* min subelems */ |
| 1920 | node_meta.parent = (void *)0x10; |
| 1921 | data = ELEMENT_WRAPPER_START "<anydata name=\"any-name\"> </anydata>" ELEMENT_WRAPPER_END; |
| 1922 | assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS); |
| 1923 | parsed = (struct lysp_node_anydata *)siblings; |
| 1924 | assert_ptr_equal(parsed->parent, node_meta.parent); |
| 1925 | assert_int_equal(parsed->nodetype, LYS_ANYDATA); |
| 1926 | assert_null(parsed->next); |
| 1927 | assert_null(parsed->exts); |
| 1928 | lysp_node_free(st->ctx, siblings); |
| 1929 | |
| 1930 | st->finished_correctly = true; |
| 1931 | } |
| 1932 | |
David Sedlák | 203ca3a | 2019-07-18 15:26:25 +0200 | [diff] [blame] | 1933 | static void |
| 1934 | test_leaf_elem(void **state) |
| 1935 | { |
| 1936 | struct state *st = *state; |
| 1937 | const char *data; |
| 1938 | struct lysp_node *siblings = NULL; |
| 1939 | struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings}; |
| 1940 | struct lysp_node_leaf *parsed = NULL; |
| 1941 | |
| 1942 | /* max elements */ |
| 1943 | data = ELEMENT_WRAPPER_START |
| 1944 | "<leaf name=\"leaf\">" |
| 1945 | "<config value=\"true\" />" |
| 1946 | "<default value=\"def-val\"/>" |
| 1947 | "<description><text>desc</text></description>" |
| 1948 | "<if-feature name=\"feature\" />" |
| 1949 | "<mandatory value=\"true\" />" |
| 1950 | "<must condition=\"must-cond\" />" |
| 1951 | "<reference><text>ref</text></reference>" |
| 1952 | "<status value=\"deprecated\"/>" |
| 1953 | "<type name=\"type\"/>" |
| 1954 | "<units name=\"uni\"/>" |
| 1955 | "<when condition=\"when-cond\"/>" |
| 1956 | "</leaf>" |
| 1957 | ELEMENT_WRAPPER_END; |
| 1958 | assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS); |
| 1959 | parsed = (struct lysp_node_leaf *)siblings; |
| 1960 | assert_null(parsed->parent); |
| 1961 | assert_int_equal(parsed->nodetype, LYS_LEAF); |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1962 | assert_true(parsed->flags & LYS_CONFIG_W); |
| 1963 | assert_true(parsed->flags & LYS_MAND_TRUE); |
| 1964 | assert_true(parsed->flags & LYS_STATUS_DEPRC); |
David Sedlák | 203ca3a | 2019-07-18 15:26:25 +0200 | [diff] [blame] | 1965 | assert_null(parsed->next); |
| 1966 | assert_string_equal(parsed->name, "leaf"); |
| 1967 | assert_string_equal(parsed->dsc, "desc"); |
| 1968 | assert_string_equal(parsed->ref, "ref"); |
| 1969 | assert_string_equal(parsed->when->cond, "when-cond"); |
| 1970 | assert_string_equal(*parsed->iffeatures, "feature"); |
| 1971 | assert_null(parsed->exts); |
| 1972 | assert_string_equal(parsed->musts->arg, "must-cond"); |
| 1973 | assert_string_equal(parsed->type.name, "type"); |
| 1974 | assert_string_equal(parsed->units, "uni"); |
| 1975 | assert_string_equal(parsed->dflt, "def-val"); |
| 1976 | lysp_node_free(st->ctx, siblings); |
| 1977 | siblings = NULL; |
| 1978 | |
| 1979 | /* min elements */ |
| 1980 | data = ELEMENT_WRAPPER_START "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>" ELEMENT_WRAPPER_END; |
| 1981 | assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS); |
| 1982 | parsed = (struct lysp_node_leaf *)siblings; |
| 1983 | assert_string_equal(parsed->name, "leaf"); |
| 1984 | assert_string_equal(parsed->type.name, "type"); |
| 1985 | lysp_node_free(st->ctx, siblings); |
| 1986 | siblings = NULL; |
| 1987 | |
| 1988 | st->finished_correctly = true; |
| 1989 | } |
| 1990 | |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 1991 | static void |
| 1992 | test_leaf_list_elem(void **state) |
| 1993 | { |
| 1994 | struct state *st = *state; |
| 1995 | const char *data; |
| 1996 | struct lysp_node *siblings = NULL; |
| 1997 | struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings}; |
| 1998 | struct lysp_node_leaflist *parsed = NULL; |
| 1999 | |
| 2000 | data = ELEMENT_WRAPPER_START |
| 2001 | "<leaf-list name=\"llist\">" |
| 2002 | "<config value=\"true\" />" |
| 2003 | "<default value=\"def-val0\"/>" |
| 2004 | "<default value=\"def-val1\"/>" |
| 2005 | "<description><text>desc</text></description>" |
| 2006 | "<if-feature name=\"feature\"/>" |
| 2007 | "<max-elements value=\"5\"/>" |
| 2008 | "<must condition=\"must-cond\"/>" |
| 2009 | "<ordered-by value=\"user\" />" |
| 2010 | "<reference><text>ref</text></reference>" |
| 2011 | "<status value=\"current\"/>" |
| 2012 | "<type name=\"type\"/>" |
| 2013 | "<units name=\"uni\"/>" |
| 2014 | "<when condition=\"when-cond\"/>" |
| 2015 | "</leaf-list>" |
| 2016 | ELEMENT_WRAPPER_END; |
| 2017 | assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS); |
| 2018 | parsed = (struct lysp_node_leaflist *)siblings; |
| 2019 | assert_string_equal(parsed->dflts[0], "def-val0"); |
| 2020 | assert_string_equal(parsed->dflts[1], "def-val1"); |
| 2021 | assert_string_equal(parsed->dsc, "desc"); |
| 2022 | assert_string_equal(*parsed->iffeatures, "feature"); |
| 2023 | assert_int_equal(parsed->max, 5); |
| 2024 | assert_string_equal(parsed->musts->arg, "must-cond"); |
| 2025 | assert_string_equal(parsed->name, "llist"); |
| 2026 | assert_null(parsed->next); |
| 2027 | assert_int_equal(parsed->nodetype, LYS_LEAFLIST); |
| 2028 | assert_null(parsed->parent); |
| 2029 | assert_string_equal(parsed->ref, "ref"); |
| 2030 | assert_string_equal(parsed->type.name, "type"); |
| 2031 | assert_string_equal(parsed->units, "uni"); |
| 2032 | assert_string_equal(parsed->when->cond, "when-cond"); |
| 2033 | assert_true(parsed->flags & LYS_CONFIG_W); |
| 2034 | assert_true(parsed->flags & LYS_ORDBY_USER); |
| 2035 | assert_true(parsed->flags & LYS_STATUS_CURR); |
| 2036 | lysp_node_free(st->ctx, siblings); |
| 2037 | siblings = NULL; |
| 2038 | |
| 2039 | data = ELEMENT_WRAPPER_START |
| 2040 | "<leaf-list name=\"llist\">" |
| 2041 | "<config value=\"true\" />" |
| 2042 | "<description><text>desc</text></description>" |
| 2043 | "<if-feature name=\"feature\"/>" |
| 2044 | "<min-elements value=\"5\"/>" |
| 2045 | "<must condition=\"must-cond\"/>" |
| 2046 | "<ordered-by value=\"user\" />" |
| 2047 | "<reference><text>ref</text></reference>" |
| 2048 | "<status value=\"current\"/>" |
| 2049 | "<type name=\"type\"/>" |
| 2050 | "<units name=\"uni\"/>" |
| 2051 | "<when condition=\"when-cond\"/>" |
| 2052 | "</leaf-list>" |
| 2053 | ELEMENT_WRAPPER_END; |
| 2054 | assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS); |
| 2055 | parsed = (struct lysp_node_leaflist *)siblings; |
| 2056 | assert_string_equal(parsed->dsc, "desc"); |
| 2057 | assert_string_equal(*parsed->iffeatures, "feature"); |
| 2058 | assert_int_equal(parsed->min, 5); |
| 2059 | assert_string_equal(parsed->musts->arg, "must-cond"); |
| 2060 | assert_string_equal(parsed->name, "llist"); |
| 2061 | assert_null(parsed->next); |
| 2062 | assert_int_equal(parsed->nodetype, LYS_LEAFLIST); |
| 2063 | assert_null(parsed->parent); |
| 2064 | assert_string_equal(parsed->ref, "ref"); |
| 2065 | assert_string_equal(parsed->type.name, "type"); |
| 2066 | assert_string_equal(parsed->units, "uni"); |
| 2067 | assert_string_equal(parsed->when->cond, "when-cond"); |
| 2068 | assert_true(parsed->flags & LYS_CONFIG_W); |
| 2069 | assert_true(parsed->flags & LYS_ORDBY_USER); |
| 2070 | assert_true(parsed->flags & LYS_STATUS_CURR); |
| 2071 | lysp_node_free(st->ctx, siblings); |
| 2072 | siblings = NULL; |
| 2073 | |
| 2074 | data = ELEMENT_WRAPPER_START |
| 2075 | "<leaf-list name=\"llist\">" |
| 2076 | "<config value=\"true\" />" |
| 2077 | "<description><text>desc</text></description>" |
| 2078 | "<if-feature name=\"feature\"/>" |
| 2079 | "<max-elements value=\"15\"/>" |
| 2080 | "<min-elements value=\"5\"/>" |
| 2081 | "<must condition=\"must-cond\"/>" |
| 2082 | "<ordered-by value=\"user\" />" |
| 2083 | "<reference><text>ref</text></reference>" |
| 2084 | "<status value=\"current\"/>" |
| 2085 | "<type name=\"type\"/>" |
| 2086 | "<units name=\"uni\"/>" |
| 2087 | "<when condition=\"when-cond\"/>" |
| 2088 | "</leaf-list>" |
| 2089 | ELEMENT_WRAPPER_END; |
| 2090 | assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS); |
| 2091 | parsed = (struct lysp_node_leaflist *)siblings; |
| 2092 | assert_string_equal(parsed->dsc, "desc"); |
| 2093 | assert_string_equal(*parsed->iffeatures, "feature"); |
| 2094 | assert_int_equal(parsed->min, 5); |
| 2095 | assert_int_equal(parsed->max, 15); |
| 2096 | assert_string_equal(parsed->musts->arg, "must-cond"); |
| 2097 | assert_string_equal(parsed->name, "llist"); |
| 2098 | assert_null(parsed->next); |
| 2099 | assert_int_equal(parsed->nodetype, LYS_LEAFLIST); |
| 2100 | assert_null(parsed->parent); |
| 2101 | assert_string_equal(parsed->ref, "ref"); |
| 2102 | assert_string_equal(parsed->type.name, "type"); |
| 2103 | assert_string_equal(parsed->units, "uni"); |
| 2104 | assert_string_equal(parsed->when->cond, "when-cond"); |
| 2105 | assert_true(parsed->flags & LYS_CONFIG_W); |
| 2106 | assert_true(parsed->flags & LYS_ORDBY_USER); |
| 2107 | assert_true(parsed->flags & LYS_STATUS_CURR); |
| 2108 | lysp_node_free(st->ctx, siblings); |
| 2109 | siblings = NULL; |
| 2110 | |
| 2111 | data = ELEMENT_WRAPPER_START |
| 2112 | "<leaf-list name=\"llist\">" |
| 2113 | "<type name=\"type\"/>" |
| 2114 | "</leaf-list>" |
| 2115 | ELEMENT_WRAPPER_END; |
| 2116 | assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS); |
| 2117 | parsed = (struct lysp_node_leaflist *)siblings; |
| 2118 | assert_string_equal(parsed->name, "llist"); |
| 2119 | assert_string_equal(parsed->type.name, "type"); |
| 2120 | lysp_node_free(st->ctx, siblings); |
| 2121 | siblings = NULL; |
| 2122 | |
| 2123 | /* invalid combinations */ |
| 2124 | data = ELEMENT_WRAPPER_START |
| 2125 | "<leaf-list name=\"llist\">" |
| 2126 | "<max-elements value=\"5\"/>" |
| 2127 | "<min-elements value=\"15\"/>" |
| 2128 | "<type name=\"type\"/>" |
| 2129 | "</leaf-list>" |
| 2130 | ELEMENT_WRAPPER_END; |
| 2131 | assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID); |
| 2132 | logbuf_assert("Invalid combination of min-elements and max-elements: min value 15 is bigger than the max value 5. Line number 1."); |
| 2133 | lysp_node_free(st->ctx, siblings); |
| 2134 | siblings = NULL; |
| 2135 | |
| 2136 | data = ELEMENT_WRAPPER_START |
| 2137 | "<leaf-list name=\"llist\">" |
| 2138 | "<default value=\"def-val1\"/>" |
| 2139 | "<min-elements value=\"15\"/>" |
| 2140 | "<type name=\"type\"/>" |
| 2141 | "</leaf-list>" |
| 2142 | ELEMENT_WRAPPER_END; |
| 2143 | assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID); |
| 2144 | logbuf_assert("Invalid combination of keywords \"min-elements\" and \"default\" as substatements of \"leaf-list\". Line number 1."); |
| 2145 | lysp_node_free(st->ctx, siblings); |
| 2146 | siblings = NULL; |
| 2147 | |
| 2148 | data = ELEMENT_WRAPPER_START |
| 2149 | "<leaf-list name=\"llist\">" |
| 2150 | "</leaf-list>" |
| 2151 | ELEMENT_WRAPPER_END; |
| 2152 | assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID); |
| 2153 | logbuf_assert("Missing mandatory subelement type of leaf-list element. Line number 1."); |
| 2154 | lysp_node_free(st->ctx, siblings); |
| 2155 | siblings = NULL; |
| 2156 | |
| 2157 | st->finished_correctly = true; |
| 2158 | } |
| 2159 | |
David Sedlák | cb39f64 | 2019-07-19 13:19:55 +0200 | [diff] [blame] | 2160 | static void |
| 2161 | test_presence_elem(void **state) |
| 2162 | { |
| 2163 | struct state *st = *state; |
| 2164 | const char *data; |
| 2165 | const char *val; |
| 2166 | |
| 2167 | data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\"/>" ELEMENT_WRAPPER_END; |
| 2168 | assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS); |
| 2169 | assert_string_equal(val, "presence-val"); |
| 2170 | FREE_STRING(st->ctx, val); |
| 2171 | |
| 2172 | data = ELEMENT_WRAPPER_START "<presence/>" ELEMENT_WRAPPER_END; |
| 2173 | assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID); |
| 2174 | logbuf_assert("Missing mandatory attribute value of presence element. Line number 1."); |
| 2175 | |
| 2176 | st->finished_correctly = true; |
| 2177 | } |
| 2178 | |
David Sedlák | 12470a8 | 2019-07-19 13:44:36 +0200 | [diff] [blame] | 2179 | static void |
| 2180 | test_key_elem(void **state) |
| 2181 | { |
| 2182 | struct state *st = *state; |
| 2183 | const char *data; |
| 2184 | const char *val; |
| 2185 | |
| 2186 | data = ELEMENT_WRAPPER_START "<key value=\"key-value\"/>" ELEMENT_WRAPPER_END; |
| 2187 | assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS); |
| 2188 | assert_string_equal(val, "key-value"); |
| 2189 | FREE_STRING(st->ctx, val); |
| 2190 | |
| 2191 | data = ELEMENT_WRAPPER_START "<key/>" ELEMENT_WRAPPER_END; |
| 2192 | assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID); |
| 2193 | logbuf_assert("Missing mandatory attribute value of key element. Line number 1."); |
| 2194 | |
| 2195 | st->finished_correctly = true; |
| 2196 | } |
| 2197 | |
David Sedlák | 04e17b2 | 2019-07-19 15:29:48 +0200 | [diff] [blame] | 2198 | static void |
| 2199 | test_typedef_elem(void **state) |
| 2200 | { |
| 2201 | struct state *st = *state; |
| 2202 | const char *data; |
| 2203 | struct lysp_tpdf *tpdfs = NULL; |
| 2204 | struct typedef_meta typdef_meta = {NULL, &tpdfs}; |
| 2205 | |
| 2206 | data = ELEMENT_WRAPPER_START |
| 2207 | "<typedef name=\"tpdf-name\">" |
| 2208 | "<default value=\"def-val\"/>" |
| 2209 | "<description><text>desc-text</text></description>" |
| 2210 | "<reference><text>ref-text</text></reference>" |
| 2211 | "<status value=\"current\"/>" |
| 2212 | "<type name=\"type\"/>" |
| 2213 | "<units name=\"uni\"/>" |
| 2214 | "</typedef>" |
| 2215 | ELEMENT_WRAPPER_END; |
| 2216 | assert_int_equal(test_element_helper(st, &data, &typdef_meta, NULL, NULL, true), LY_SUCCESS); |
| 2217 | assert_string_equal(tpdfs[0].dflt, "def-val"); |
| 2218 | assert_string_equal(tpdfs[0].dsc, "desc-text"); |
| 2219 | assert_null(tpdfs[0].exts); |
| 2220 | assert_string_equal(tpdfs[0].name, "tpdf-name"); |
| 2221 | assert_string_equal(tpdfs[0].ref, "ref-text"); |
| 2222 | assert_string_equal(tpdfs[0].type.name, "type"); |
| 2223 | assert_string_equal(tpdfs[0].units, "uni"); |
| 2224 | assert_true(tpdfs[0].flags & LYS_STATUS_CURR); |
| 2225 | FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free); |
| 2226 | tpdfs = NULL; |
| 2227 | |
| 2228 | data = ELEMENT_WRAPPER_START |
| 2229 | "<typedef name=\"tpdf-name\">" |
| 2230 | "<type name=\"type\"/>" |
| 2231 | "</typedef>" |
| 2232 | ELEMENT_WRAPPER_END; |
| 2233 | assert_int_equal(test_element_helper(st, &data, &typdef_meta, NULL, NULL, true), LY_SUCCESS); |
| 2234 | assert_string_equal(tpdfs[0].name, "tpdf-name"); |
| 2235 | assert_string_equal(tpdfs[0].type.name, "type"); |
| 2236 | FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free); |
| 2237 | tpdfs = NULL; |
| 2238 | |
| 2239 | st->finished_correctly = true; |
| 2240 | } |
| 2241 | |
David Sedlák | d2d676a | 2019-07-22 11:28:19 +0200 | [diff] [blame] | 2242 | static void |
| 2243 | test_refine_elem(void **state) |
| 2244 | { |
| 2245 | struct state *st = *state; |
| 2246 | const char *data; |
| 2247 | struct lysp_refine *refines = NULL; |
| 2248 | |
| 2249 | /* max subelems */ |
| 2250 | data = ELEMENT_WRAPPER_START |
| 2251 | "<refine target-node=\"target\">" |
| 2252 | "<if-feature name=\"feature\" />" |
| 2253 | "<must condition=\"cond\" />" |
| 2254 | "<presence value=\"presence\" />" |
| 2255 | "<default value=\"def\" />" |
| 2256 | "<config value=\"true\" />" |
| 2257 | "<mandatory value=\"true\" />" |
| 2258 | "<min-elements value=\"10\" />" |
| 2259 | "<max-elements value=\"20\" />" |
| 2260 | "<description><text>desc</text></description>" |
| 2261 | "<reference><text>ref</text></reference>" |
| 2262 | "</refine>" |
| 2263 | ELEMENT_WRAPPER_END; |
| 2264 | assert_int_equal(test_element_helper(st, &data, &refines, NULL, NULL, true), LY_SUCCESS); |
| 2265 | assert_string_equal(refines->nodeid, "target"); |
| 2266 | assert_string_equal(*refines->dflts, "def"); |
| 2267 | assert_string_equal(refines->dsc, "desc"); |
| 2268 | assert_null(refines->exts); |
| 2269 | assert_true(refines->flags & LYS_CONFIG_W); |
| 2270 | assert_true(refines->flags & LYS_MAND_TRUE); |
| 2271 | assert_string_equal(*refines->iffeatures, "feature"); |
| 2272 | assert_int_equal(refines->max, 20); |
| 2273 | assert_int_equal(refines->min, 10); |
| 2274 | assert_string_equal(refines->musts->arg, "cond"); |
| 2275 | assert_string_equal(refines->presence, "presence"); |
| 2276 | assert_string_equal(refines->ref, "ref"); |
| 2277 | FREE_ARRAY(st->ctx, refines, lysp_refine_free); |
| 2278 | refines = NULL; |
| 2279 | |
| 2280 | /* min subelems */ |
| 2281 | data = ELEMENT_WRAPPER_START "<refine target-node=\"target\" />" ELEMENT_WRAPPER_END; |
| 2282 | assert_int_equal(test_element_helper(st, &data, &refines, NULL, NULL, true), LY_SUCCESS); |
| 2283 | assert_string_equal(refines->nodeid, "target"); |
| 2284 | FREE_ARRAY(st->ctx, refines, lysp_refine_free); |
| 2285 | refines = NULL; |
| 2286 | |
| 2287 | st->finished_correctly = true; |
| 2288 | } |
| 2289 | |
David Sedlák | 0d6de5a | 2019-07-22 13:25:44 +0200 | [diff] [blame] | 2290 | static void |
| 2291 | test_uses_elem(void **state) |
| 2292 | { |
| 2293 | struct state *st = *state; |
| 2294 | const char *data; |
| 2295 | struct lysp_node *siblings = NULL; |
| 2296 | struct tree_node_meta node_meta = {NULL, &siblings}; |
| 2297 | struct lysp_node_uses *parsed = NULL; |
| 2298 | |
| 2299 | /* max subelems */ |
| 2300 | data = ELEMENT_WRAPPER_START |
| 2301 | "<uses name=\"uses-name\">" |
| 2302 | "<when condition=\"cond\" />" |
| 2303 | "<if-feature name=\"feature\" />" |
| 2304 | "<status value=\"obsolete\" />" |
| 2305 | "<description><text>desc</text></description>" |
| 2306 | "<reference><text>ref</text></reference>" |
| 2307 | "<refine target-node=\"target\"/>" |
| 2308 | /* TODO add uses-augment-stmt instance */ |
| 2309 | "</uses>" |
| 2310 | ELEMENT_WRAPPER_END; |
| 2311 | assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS); |
| 2312 | parsed = (struct lysp_node_uses *)&siblings[0]; |
| 2313 | assert_string_equal(parsed->name, "uses-name"); |
| 2314 | assert_string_equal(parsed->dsc, "desc"); |
| 2315 | assert_null(parsed->exts); |
| 2316 | assert_true(parsed->flags & LYS_STATUS_OBSLT); |
| 2317 | assert_string_equal(*parsed->iffeatures, "feature"); |
| 2318 | assert_null(parsed->next); |
| 2319 | assert_int_equal(parsed->nodetype, LYS_USES); |
| 2320 | assert_null(parsed->parent); |
| 2321 | assert_string_equal(parsed->ref, "ref"); |
| 2322 | assert_string_equal(parsed->refines->nodeid, "target"); |
| 2323 | assert_string_equal(parsed->when->cond, "cond"); |
| 2324 | lysp_node_free(st->ctx, siblings); |
| 2325 | siblings = NULL; |
| 2326 | |
| 2327 | /* min subelems */ |
| 2328 | data = ELEMENT_WRAPPER_START "<uses name=\"uses-name\"/>" ELEMENT_WRAPPER_END; |
| 2329 | assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS); |
| 2330 | assert_string_equal(siblings[0].name, "uses-name"); |
| 2331 | lysp_node_free(st->ctx, siblings); |
| 2332 | siblings = NULL; |
| 2333 | |
| 2334 | st->finished_correctly = true; |
| 2335 | } |
| 2336 | |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 2337 | static void |
| 2338 | test_revision_elem(void **state) |
| 2339 | { |
| 2340 | struct state *st = *state; |
| 2341 | const char *data; |
| 2342 | struct lysp_revision *revs = NULL; |
| 2343 | |
| 2344 | /* max subelems */ |
| 2345 | data = ELEMENT_WRAPPER_START |
| 2346 | "<revision date=\"2018-12-25\">" |
| 2347 | "<description><text>desc</text></description>" |
| 2348 | "<reference><text>ref</text></reference>" |
| 2349 | "</revision>" |
| 2350 | ELEMENT_WRAPPER_END; |
| 2351 | assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, true), LY_SUCCESS); |
| 2352 | assert_string_equal(revs->date, "2018-12-25"); |
| 2353 | assert_string_equal(revs->dsc, "desc"); |
| 2354 | assert_string_equal(revs->ref, "ref"); |
| 2355 | FREE_ARRAY(st->ctx, revs, lysp_revision_free); |
| 2356 | revs = NULL; |
| 2357 | |
| 2358 | /* min subelems */ |
| 2359 | data = ELEMENT_WRAPPER_START "<revision date=\"2005-05-05\" />" ELEMENT_WRAPPER_END; |
| 2360 | assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, true), LY_SUCCESS); |
| 2361 | assert_string_equal(revs->date, "2005-05-05"); |
| 2362 | FREE_ARRAY(st->ctx, revs, lysp_revision_free); |
| 2363 | revs = NULL; |
| 2364 | |
| 2365 | /* invalid value */ |
| 2366 | data = ELEMENT_WRAPPER_START "<revision date=\"05-05-2005\" />" ELEMENT_WRAPPER_END; |
| 2367 | assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, false), LY_EVALID); |
| 2368 | logbuf_assert("Invalid value \"05-05-2005\" of \"revision\". Line number 1."); |
| 2369 | FREE_ARRAY(st->ctx, revs, lysp_revision_free); |
| 2370 | revs = NULL; |
| 2371 | |
| 2372 | st->finished_correctly = true; |
| 2373 | } |
| 2374 | |
David Sedlák | 0c2bab9 | 2019-07-22 15:33:19 +0200 | [diff] [blame^] | 2375 | static void |
| 2376 | test_include_elem(void **state) |
| 2377 | { |
| 2378 | struct state *st = *state; |
| 2379 | const char *data; |
| 2380 | struct lysp_include *includes = NULL; |
| 2381 | struct include_meta inc_meta = {"module-name", &includes}; |
| 2382 | |
| 2383 | /* max subelems */ |
| 2384 | st->yin_ctx->mod_version = LYS_VERSION_1_1; |
| 2385 | data = ELEMENT_WRAPPER_START |
| 2386 | "<include module=\"mod\">" |
| 2387 | "<description><text>desc</text></description>" |
| 2388 | "<reference><text>ref</text></reference>" |
| 2389 | "<revision-date date=\"1999-09-09\"/>" |
| 2390 | "</include>" |
| 2391 | ELEMENT_WRAPPER_END; |
| 2392 | assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, true), LY_SUCCESS); |
| 2393 | assert_string_equal(includes->name, "mod"); |
| 2394 | assert_string_equal(includes->dsc, "desc"); |
| 2395 | assert_string_equal(includes->ref, "ref"); |
| 2396 | assert_null(includes->exts); |
| 2397 | assert_string_equal(includes->rev, "1999-09-09"); |
| 2398 | FREE_ARRAY(st->ctx, includes, lysp_include_free); |
| 2399 | includes = NULL; |
| 2400 | |
| 2401 | /* min subelems */ |
| 2402 | data = ELEMENT_WRAPPER_START "<include module=\"mod\"/>" ELEMENT_WRAPPER_END; |
| 2403 | assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, true), LY_SUCCESS); |
| 2404 | assert_string_equal(includes->name, "mod"); |
| 2405 | FREE_ARRAY(st->ctx, includes, lysp_include_free); |
| 2406 | includes = NULL; |
| 2407 | |
| 2408 | /* invalid combinations */ |
| 2409 | st->yin_ctx->mod_version = LYS_VERSION_1_0; |
| 2410 | data = ELEMENT_WRAPPER_START |
| 2411 | "<include module=\"mod\">" |
| 2412 | "<description><text>desc</text></description>" |
| 2413 | "<revision-date date=\"1999-09-09\"/>" |
| 2414 | "</include>" |
| 2415 | ELEMENT_WRAPPER_END; |
| 2416 | assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, false), LY_EVALID); |
| 2417 | logbuf_assert("Invalid sub-elemnt \"description\" of \"include\" element - this sub-element is allowed only in modules with version 1.1 or newer. Line number 1."); |
| 2418 | FREE_ARRAY(st->ctx, includes, lysp_include_free); |
| 2419 | includes = NULL; |
| 2420 | |
| 2421 | st->yin_ctx->mod_version = LYS_VERSION_1_0; |
| 2422 | data = ELEMENT_WRAPPER_START |
| 2423 | "<include module=\"mod\">" |
| 2424 | "<reference><text>ref</text></reference>" |
| 2425 | "<revision-date date=\"1999-09-09\"/>" |
| 2426 | "</include>" |
| 2427 | ELEMENT_WRAPPER_END; |
| 2428 | assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, false), LY_EVALID); |
| 2429 | logbuf_assert("Invalid sub-elemnt \"reference\" of \"include\" element - this sub-element is allowed only in modules with version 1.1 or newer. Line number 1."); |
| 2430 | FREE_ARRAY(st->ctx, includes, lysp_include_free); |
| 2431 | includes = NULL; |
| 2432 | |
| 2433 | st->finished_correctly = true; |
| 2434 | } |
| 2435 | |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 2436 | int |
| 2437 | main(void) |
| 2438 | { |
| 2439 | |
| 2440 | const struct CMUnitTest tests[] = { |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 2441 | cmocka_unit_test_setup_teardown(test_yin_parse_module, setup_f, teardown_f), |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 2442 | cmocka_unit_test_setup_teardown(test_yin_match_keyword, setup_f, teardown_f), |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 2443 | cmocka_unit_test_setup_teardown(test_yin_parse_element_generic, setup_f, teardown_f), |
| 2444 | 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] | 2445 | cmocka_unit_test_setup_teardown(test_yin_parse_content, setup_f, teardown_f), |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 2446 | cmocka_unit_test_setup_teardown(test_validate_value, setup_f, teardown_f), |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 2447 | |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 2448 | cmocka_unit_test(test_yin_match_argument_name), |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame] | 2449 | cmocka_unit_test_setup_teardown(test_enum_bit_elem, setup_element_test, teardown_element_test), |
| 2450 | cmocka_unit_test_setup_teardown(test_meta_elem, setup_element_test, teardown_element_test), |
| 2451 | cmocka_unit_test_setup_teardown(test_import_elem, setup_element_test, teardown_element_test), |
| 2452 | cmocka_unit_test_setup_teardown(test_status_elem, setup_element_test, teardown_element_test), |
| 2453 | cmocka_unit_test_setup_teardown(test_ext_elem, setup_element_test, teardown_element_test), |
| 2454 | cmocka_unit_test_setup_teardown(test_yin_element_elem, setup_element_test, teardown_element_test), |
| 2455 | cmocka_unit_test_setup_teardown(test_yangversion_elem, setup_element_test, teardown_element_test), |
| 2456 | cmocka_unit_test_setup_teardown(test_mandatory_elem, setup_element_test, teardown_element_test), |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 2457 | cmocka_unit_test_setup_teardown(test_argument_elem, setup_element_test, teardown_element_test), |
| 2458 | cmocka_unit_test_setup_teardown(test_base_elem, setup_element_test, teardown_element_test), |
| 2459 | cmocka_unit_test_setup_teardown(test_belongsto_elem, setup_element_test, teardown_element_test), |
| 2460 | cmocka_unit_test_setup_teardown(test_config_elem, setup_element_test, teardown_element_test), |
| 2461 | cmocka_unit_test_setup_teardown(test_default_elem, setup_element_test, teardown_element_test), |
| 2462 | cmocka_unit_test_setup_teardown(test_err_app_tag_elem, setup_element_test, teardown_element_test), |
| 2463 | cmocka_unit_test_setup_teardown(test_err_msg_elem, setup_element_test, teardown_element_test), |
| 2464 | cmocka_unit_test_setup_teardown(test_fracdigits_elem, setup_element_test, teardown_element_test), |
| 2465 | cmocka_unit_test_setup_teardown(test_iffeature_elem, setup_element_test, teardown_element_test), |
| 2466 | cmocka_unit_test_setup_teardown(test_length_elem, setup_element_test, teardown_element_test), |
| 2467 | cmocka_unit_test_setup_teardown(test_modifier_elem, setup_element_test, teardown_element_test), |
| 2468 | cmocka_unit_test_setup_teardown(test_namespace_elem, setup_element_test, teardown_element_test), |
| 2469 | cmocka_unit_test_setup_teardown(test_path_elem, setup_element_test, teardown_element_test), |
| 2470 | cmocka_unit_test_setup_teardown(test_pattern_elem, setup_element_test, teardown_element_test), |
| 2471 | cmocka_unit_test_setup_teardown(test_value_position_elem, setup_element_test, teardown_element_test), |
David Sedlák | 69f0161 | 2019-07-17 11:41:08 +0200 | [diff] [blame] | 2472 | cmocka_unit_test_setup_teardown(test_prefix_elem, setup_element_test, teardown_element_test), |
| 2473 | cmocka_unit_test_setup_teardown(test_range_elem, setup_element_test, teardown_element_test), |
| 2474 | cmocka_unit_test_setup_teardown(test_reqinstance_elem, setup_element_test, teardown_element_test), |
| 2475 | cmocka_unit_test_setup_teardown(test_revision_date_elem, setup_element_test, teardown_element_test), |
| 2476 | cmocka_unit_test_setup_teardown(test_unique_elem, setup_element_test, teardown_element_test), |
| 2477 | cmocka_unit_test_setup_teardown(test_units_elem, setup_element_test, teardown_element_test), |
| 2478 | cmocka_unit_test_setup_teardown(test_when_elem, setup_element_test, teardown_element_test), |
| 2479 | cmocka_unit_test_setup_teardown(test_yin_text_value_elem, setup_element_test, teardown_element_test), |
David Sedlák | 374d2b3 | 2019-07-17 15:06:55 +0200 | [diff] [blame] | 2480 | cmocka_unit_test_setup_teardown(test_type_elem, setup_element_test, teardown_element_test), |
David Sedlák | 1af868e | 2019-07-17 17:03:14 +0200 | [diff] [blame] | 2481 | cmocka_unit_test_setup_teardown(test_max_elems_elem, setup_element_test, teardown_element_test), |
David Sedlák | 09e18c9 | 2019-07-18 11:17:11 +0200 | [diff] [blame] | 2482 | cmocka_unit_test_setup_teardown(test_min_elems_elem, setup_element_test, teardown_element_test), |
David Sedlák | a2dad21 | 2019-07-18 12:45:19 +0200 | [diff] [blame] | 2483 | cmocka_unit_test_setup_teardown(test_ordby_elem, setup_element_test, teardown_element_test), |
David Sedlák | 8a83bbb | 2019-07-18 14:46:00 +0200 | [diff] [blame] | 2484 | cmocka_unit_test_setup_teardown(test_any_elem, setup_element_test, teardown_element_test), |
David Sedlák | 203ca3a | 2019-07-18 15:26:25 +0200 | [diff] [blame] | 2485 | cmocka_unit_test_setup_teardown(test_leaf_elem, setup_element_test, teardown_element_test), |
David Sedlák | c3da3ef | 2019-07-19 12:56:08 +0200 | [diff] [blame] | 2486 | cmocka_unit_test_setup_teardown(test_leaf_list_elem, setup_element_test, teardown_element_test), |
David Sedlák | cb39f64 | 2019-07-19 13:19:55 +0200 | [diff] [blame] | 2487 | cmocka_unit_test_setup_teardown(test_presence_elem, setup_element_test, teardown_element_test), |
David Sedlák | 12470a8 | 2019-07-19 13:44:36 +0200 | [diff] [blame] | 2488 | cmocka_unit_test_setup_teardown(test_key_elem, setup_element_test, teardown_element_test), |
David Sedlák | 04e17b2 | 2019-07-19 15:29:48 +0200 | [diff] [blame] | 2489 | cmocka_unit_test_setup_teardown(test_typedef_elem, setup_element_test, teardown_element_test), |
David Sedlák | d2d676a | 2019-07-22 11:28:19 +0200 | [diff] [blame] | 2490 | cmocka_unit_test_setup_teardown(test_refine_elem, setup_element_test, teardown_element_test), |
David Sedlák | 0d6de5a | 2019-07-22 13:25:44 +0200 | [diff] [blame] | 2491 | cmocka_unit_test_setup_teardown(test_uses_elem, setup_element_test, teardown_element_test), |
David Sedlák | aa854b0 | 2019-07-22 14:17:10 +0200 | [diff] [blame] | 2492 | cmocka_unit_test_setup_teardown(test_revision_elem, setup_element_test, teardown_element_test), |
David Sedlák | 0c2bab9 | 2019-07-22 15:33:19 +0200 | [diff] [blame^] | 2493 | cmocka_unit_test_setup_teardown(test_include_elem, setup_element_test, teardown_element_test), |
| 2494 | |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 2495 | }; |
| 2496 | |
David Sedlák | 8e7bda8 | 2019-07-16 17:57:50 +0200 | [diff] [blame] | 2497 | return cmocka_run_group_tests(tests, setup_ly_ctx, destroy_ly_ctx); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 2498 | } |