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