blob: 4549d0b0f2dd2db6e31e8d377a885f1f23c7ca14 [file] [log] [blame]
David Sedlákb1ce3f82019-06-05 14:37:26 +02001/**
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ák3b4db242018-10-19 16:11:01 +020015#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ák79e50cb2019-06-05 16:33:09 +020022#include <stdbool.h>
David Sedlák3b4db242018-10-19 16:11:01 +020023
David Sedlákecf5eb82019-06-03 14:12:44 +020024#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ák8f5bce02019-06-03 16:41:08 +020028#include "../../src/xml.h"
David Sedlák3b4db242018-10-19 16:11:01 +020029
David Sedlák555c7202019-07-04 12:14:12 +020030/* prototypes of static functions */
31void lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext);
David Sedlák986cb412019-07-04 13:10:11 +020032void lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext);
David Sedlák32eee7b2019-07-09 12:38:44 +020033void lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when);
David Sedlák32488102019-07-15 17:44:10 +020034void lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type);
David Sedlák8a83bbb2019-07-18 14:46:00 +020035void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
David Sedlák04e17b22019-07-19 15:29:48 +020036void lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf);
David Sedlákd2d676a2019-07-22 11:28:19 +020037void lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref);
David Sedlák555c7202019-07-04 12:14:12 +020038
David Sedlák68a1af12019-03-08 13:46:54 +010039struct state {
David Sedlák3b4db242018-10-19 16:11:01 +020040 struct ly_ctx *ctx;
David Sedlák3017da42019-02-15 09:48:04 +010041 struct lys_module *mod;
David Sedlák619db942019-07-03 14:47:30 +020042 struct lysp_module *lysp_mod;
David Sedlákda8ffa32019-07-08 14:17:10 +020043 struct yin_parser_ctx *yin_ctx;
David Sedlák79e50cb2019-06-05 16:33:09 +020044 bool finished_correctly;
David Sedlák68a1af12019-03-08 13:46:54 +010045};
David Sedlák872c7b42018-10-26 13:15:20 +020046
David Sedlák79e50cb2019-06-05 16:33:09 +020047#define BUFSIZE 1024
48char logbuf[BUFSIZE] = {0};
49int 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ák203ca3a2019-07-18 15:26:25 +020052#define ENABLE_LOGGER_CHECKING 1
David Sedlák79e50cb2019-06-05 16:33:09 +020053
54#if ENABLE_LOGGER_CHECKING
55static void
56logger(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ák8e7bda82019-07-16 17:57:50 +020084int
85setup_ly_ctx(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +010086{
87 struct state *st = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +020088
David Sedlák68a1af12019-03-08 13:46:54 +010089 /* 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ák872c7b42018-10-26 13:15:20 +020095
David Sedlák68a1af12019-03-08 13:46:54 +010096 /* create new libyang context */
97 ly_ctx_new(NULL, 0, &st->ctx);
David Sedlák872c7b42018-10-26 13:15:20 +020098
David Sedlák8e7bda82019-07-16 17:57:50 +020099 return EXIT_SUCCESS;
100}
101
102int
103destroy_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
112static int
113setup_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ák68a1af12019-03-08 13:46:54 +0100122 /* allocate new module */
123 st->mod = calloc(1, sizeof(*st->mod));
124 st->mod->ctx = st->ctx;
125
David Sedlák619db942019-07-03 14:47:30 +0200126 /* 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ákda8ffa32019-07-08 14:17:10 +0200132 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ák8f5bce02019-06-03 16:41:08 +0200135
David Sedlák68a1af12019-03-08 13:46:54 +0100136 return EXIT_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +0200137}
138
139static int
David Sedlák68a1af12019-03-08 13:46:54 +0100140teardown_f(void **state)
141{
142 struct state *st = *(struct state **)state;
David Sedlák619db942019-07-03 14:47:30 +0200143 struct lys_module *temp;
David Sedlák68a1af12019-03-08 13:46:54 +0100144
David Sedlák79e50cb2019-06-05 16:33:09 +0200145#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ák619db942019-07-03 14:47:30 +0200152 temp = st->lysp_mod->mod;
153
David Sedlákda8ffa32019-07-08 14:17:10 +0200154 lyxml_context_clear(&st->yin_ctx->xml_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100155 lys_module_free(st->mod, NULL);
David Sedlák619db942019-07-03 14:47:30 +0200156 lysp_module_free(st->lysp_mod);
157 lys_module_free(temp, NULL);
David Sedlákda8ffa32019-07-08 14:17:10 +0200158 free(st->yin_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100159
160 return EXIT_SUCCESS;
161}
162
David Sedlák392af4f2019-06-04 16:02:42 +0200163static struct state*
164reset_state(void **state)
165{
David Sedlák79e50cb2019-06-05 16:33:09 +0200166 ((struct state *)*state)->finished_correctly = true;
David Sedlák555c7202019-07-04 12:14:12 +0200167 logbuf[0] = '\0';
David Sedlák392af4f2019-06-04 16:02:42 +0200168 teardown_f(state);
169 setup_f(state);
170
171 return *state;
172}
173
David Sedlák79e50cb2019-06-05 16:33:09 +0200174void
175logbuf_clean(void)
176{
177 logbuf[0] = '\0';
178}
179
David Sedlák68a1af12019-03-08 13:46:54 +0100180static void
David Sedlák392af4f2019-06-04 16:02:42 +0200181test_yin_parse_module(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +0100182{
183 LY_ERR ret = LY_SUCCESS;
184 struct state *st = *state;
185
186 ret = yin_parse_module(st->ctx,
David Sedlák2b214ac2019-06-06 16:11:03 +0200187 "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\
188 name=\"example-foo\"\
David Sedlák18730132019-03-15 15:51:34 +0100189 xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\
190 xmlns:foo=\"urn:example:foo\"\
191 xmlns:myext=\"urn:example:extensions\">\
David Sedlákcd0c9512019-03-29 13:23:06 +0100192 <namespace uri=\"urn:example:foo\" xmlns:myext=\"urn:example:extensions\"/>\
David Sedláka7406952019-04-05 10:33:07 +0200193 <prefix xmlns:myxt=\"urn:emple:extensions\" value=\"foo\" xmlns:myext=\"urn:example:extensions\"/>\
David Sedlákd9d3a312019-06-04 09:47:10 +0200194 </module>",
David Sedlák68a1af12019-03-08 13:46:54 +0100195 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ákcd0c9512019-03-29 13:23:06 +0100200 assert_string_equal(st->mod->parsed->mod->ns, "urn:example:foo");
David Sedlák392af4f2019-06-04 16:02:42 +0200201
202 st = reset_state(state);
203 ret = yin_parse_module(st->ctx,
David Sedlák2b214ac2019-06-06 16:11:03 +0200204 "<module name=\"example-foo\">\
205 <invalid-tag uri=\"urn:example:foo\"\"/>\
206 </module>",
207 st->mod);
David Sedlák392af4f2019-06-04 16:02:42 +0200208 assert_int_equal(ret, LY_EVALID);
209
210 st = reset_state(state);
211 ret = yin_parse_module(st->ctx,
David Sedlák8f7a1172019-06-20 14:42:18 +0200212 "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\
David Sedlák57715b12019-06-17 13:05:22 +0200213 </module>",
David Sedlák2b214ac2019-06-06 16:11:03 +0200214 st->mod);
David Sedlák392af4f2019-06-04 16:02:42 +0200215 assert_int_equal(ret, LY_EVALID);
David Sedlák882a8072019-07-08 17:51:20 +0200216 logbuf_assert("Missing mandatory attribute name of module element. Line number 1.");
David Sedlák392af4f2019-06-04 16:02:42 +0200217
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ák79e50cb2019-06-05 16:33:09 +0200223 logbuf_assert("Invalid keyword \"(null)\", expected \"module\" or \"submodule\". Line number 1.");
224 st->finished_correctly = true;
David Sedlák3b4db242018-10-19 16:11:01 +0200225}
226
227static void
David Sedlák1bccdfa2019-06-17 15:55:27 +0200228test_yin_match_keyword(void **state)
David Sedlák3b4db242018-10-19 16:11:01 +0200229{
David Sedlák8f7a1172019-06-20 14:42:18 +0200230 struct state *st = *state;
David Sedlák3b4db242018-10-19 16:11:01 +0200231
David Sedlák8f7a1172019-06-20 14:42:18 +0200232 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ákda8ffa32019-07-08 14:17:10 +0200237 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ák8f7a1172019-06-20 14:42:18 +0200239 LY_ARRAY_FREE(args);
240
David Sedlákc1771b12019-07-10 15:55:46 +0200241 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ák8f7a1172019-06-20 14:42:18 +0200311
312 st->finished_correctly = true;
David Sedlák872c7b42018-10-26 13:15:20 +0200313}
David Sedlák3b4db242018-10-19 16:11:01 +0200314
David Sedlák872c7b42018-10-26 13:15:20 +0200315static void
David Sedlák060b00e2019-06-19 11:12:06 +0200316test_yin_match_argument_name(void **state)
David Sedlák872c7b42018-10-26 13:15:20 +0200317{
David Sedlák68a1af12019-03-08 13:46:54 +0100318 (void)state; /* unused */
David Sedlák872c7b42018-10-26 13:15:20 +0200319
David Sedlák060b00e2019-06-19 11:12:06 +0200320 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ák3b4db242018-10-19 16:11:01 +0200332}
333
David Sedlák68a1af12019-03-08 13:46:54 +0100334static void
David Sedlákb1a78352019-06-28 16:16:29 +0200335test_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ákda8ffa32019-07-08 14:17:10 +0200346 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ákb1a78352019-06-28 16:16:29 +0200348 assert_int_equal(ret, LY_SUCCESS);
David Sedlák1fdb2522019-07-09 16:22:57 +0200349 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákb1a78352019-06-28 16:16:29 +0200350 assert_string_equal(exts.child->stmt, "elem");
351 assert_string_equal(exts.child->arg, "text_value");
David Sedlákb1a78352019-06-28 16:16:29 +0200352 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ákb1a78352019-06-28 16:16:29 +0200355 lysp_ext_instance_free(st->ctx, &exts);
David Sedlák5392a212019-07-01 09:19:10 +0200356 st = reset_state(state);
357
358 data = "<elem></elem>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200359 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ák5392a212019-07-01 09:19:10 +0200361 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ákda8ffa32019-07-08 14:17:10 +0200365 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlák5392a212019-07-01 09:19:10 +0200366 lysp_ext_instance_free(st->ctx, &exts);
367
David Sedlákb1a78352019-06-28 16:16:29 +0200368 st->finished_correctly = true;
369}
370
371static void
372test_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ákb1a78352019-06-28 16:16:29 +0200380 const char *data = "<ext value1=\"test\" value=\"test2\"><subelem>text</subelem></ext>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200381 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ák1f90d252019-07-10 17:09:32 +0200383 ret = yin_parse_extension_instance(st->yin_ctx, args, &data, name2fullname(name, prefix_len),
David Sedlák619db942019-07-03 14:47:30 +0200384 namelen2fulllen(name_len, prefix_len), LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200385 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ákda8ffa32019-07-08 14:17:10 +0200404 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákb1a78352019-06-28 16:16:29 +0200405 LY_ARRAY_FREE(args);
406 lysp_ext_instance_free(st->ctx, exts);
407 LY_ARRAY_FREE(exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200408 exts = NULL;
409 args = NULL;
410 st = reset_state(state);
411
412 data = "<extension-elem />";
David Sedlákda8ffa32019-07-08 14:17:10 +0200413 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ák1f90d252019-07-10 17:09:32 +0200415 ret = yin_parse_extension_instance(st->yin_ctx, args, &data, name, name_len, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200416 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ákda8ffa32019-07-08 14:17:10 +0200423 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákf250ecf2019-07-01 11:02:05 +0200424 LY_ARRAY_FREE(args);
425 lysp_ext_instance_free(st->ctx, exts);
426 LY_ARRAY_FREE(exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200427 st->finished_correctly = true;
428}
429
David Sedlák555c7202019-07-04 12:14:12 +0200430static void
431test_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ák986cb412019-07-04 13:10:11 +0200440 "<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ák555c7202019-07-04 12:14:12 +0200446 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedlák32488102019-07-15 17:44:10 +0200447 "<if-feature name=\"foo\"></if-feature>"
David Sedlák32eee7b2019-07-09 12:38:44 +0200448 "<when condition=\"condition...\">"
449 "<reference><text>when_ref</text></reference>"
450 "<description><text>when_desc</text></description>"
451 "</when>"
David Sedláke1a30302019-07-10 13:49:38 +0200452 "<config value=\"true\"/>"
David Sedlákc1771b12019-07-10 15:55:46 +0200453 "<error-message>"
454 "<value>error-msg</value>"
455 "</error-message>"
David Sedlák2ce1be62019-07-10 16:15:09 +0200456 "<error-app-tag value=\"err-app-tag\"/>"
David Sedláka5b1d382019-07-10 16:31:09 +0200457 "<units name=\"radians\"></units>"
David Sedláke7084ce2019-07-10 16:44:15 +0200458 "<default value=\"default-value\"/>"
David Sedlák5545f5d2019-07-11 11:55:16 +0200459 "<position value=\"25\"></position>"
460 "<value value=\"-5\"/>"
David Sedlákcf5569a2019-07-11 13:31:34 +0200461 "<require-instance value=\"true\"></require-instance>"
David Sedlákb7296dd2019-07-11 14:58:38 +0200462 "<range value=\"5..10\" />"
David Sedlák438ae432019-07-11 15:36:54 +0200463 "<length value=\"baf\"/>"
David Sedlákd3983112019-07-12 11:20:56 +0200464 "<pattern value='pattern'>"
465 "<modifier value='invert-match'/>"
466 "</pattern>"
David Sedlákfd5b9c32019-07-12 15:33:13 +0200467 "<enum name=\"yay\">"
468 "</enum>"
David Sedlák555c7202019-07-04 12:14:12 +0200469 "</prefix>";
470 struct lysp_ext_instance *exts = NULL;
David Sedlák5f8191e2019-07-08 16:35:52 +0200471 const char **if_features = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200472 struct yin_arg_record *attrs = NULL;
David Sedláke7084ce2019-07-10 16:44:15 +0200473 const char *value, *err_msg, *app_tag, *units, *def;
David Sedlák986cb412019-07-04 13:10:11 +0200474 struct lysp_ext *ext_def = NULL;
David Sedlák32eee7b2019-07-09 12:38:44 +0200475 struct lysp_when *when_p = NULL;
David Sedlákcf5569a2019-07-11 13:31:34 +0200476 struct lysp_type_enum pos_enum = {}, val_enum = {};
David Sedlákfd5b9c32019-07-12 15:33:13 +0200477 struct lysp_type req_type = {}, range_type = {}, len_type = {}, patter_type = {}, enum_type = {};
David Sedláke1a30302019-07-10 13:49:38 +0200478 uint8_t config = 0;
David Sedlák555c7202019-07-04 12:14:12 +0200479
David Sedlákda8ffa32019-07-08 14:17:10 +0200480 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ák555c7202019-07-04 12:14:12 +0200482
David Sedlákfd5b9c32019-07-12 15:33:13 +0200483 struct yin_subelement subelems[17] = {
David Sedlák438ae432019-07-11 15:36:54 +0200484 {YANG_CONFIG, &config, 0},
David Sedlákc3da3ef2019-07-19 12:56:08 +0200485 {YANG_DEFAULT, &def, YIN_SUBELEM_UNIQUE},
David Sedlákfd5b9c32019-07-12 15:33:13 +0200486 {YANG_ENUM, &enum_type, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200487 {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ákd3983112019-07-12 11:20:56 +0200492 {YANG_PATTERN, &patter_type, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200493 {YANG_RANGE, &range_type, 0},
494 {YANG_REQUIRE_INSTANCE, &req_type, 0},
495 {YANG_UNITS, &units, 0},
David Sedlák374d2b32019-07-17 15:06:55 +0200496 {YANG_POSITION, &pos_enum, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200497 {YANG_VALUE, &val_enum, 0},
498 {YANG_WHEN, &when_p, 0},
499 {YANG_CUSTOM, NULL, 0},
500 {YIN_TEXT, &value, 0}
David Sedlákd3983112019-07-12 11:20:56 +0200501 };
David Sedlákfd5b9c32019-07-12 15:33:13 +0200502 ret = yin_parse_content(st->yin_ctx, subelems, 17, &data, YANG_PREFIX, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200503 assert_int_equal(ret, LY_SUCCESS);
David Sedlák1fdb2522019-07-09 16:22:57 +0200504 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlák2ce1be62019-07-10 16:15:09 +0200505 /* check parsed values */
David Sedláke7084ce2019-07-10 16:44:15 +0200506 assert_string_equal(def, "default-value");
David Sedlák555c7202019-07-04 12:14:12 +0200507 assert_string_equal(exts->name, "custom");
508 assert_string_equal(exts->argument, "totally amazing extension");
509 assert_string_equal(value, "wsefsdf");
David Sedláka5b1d382019-07-10 16:31:09 +0200510 assert_string_equal(units, "radians");
David Sedlák32eee7b2019-07-09 12:38:44 +0200511 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áke1a30302019-07-10 13:49:38 +0200514 assert_int_equal(config, LYS_CONFIG_W);
David Sedlák5545f5d2019-07-11 11:55:16 +0200515 assert_int_equal(pos_enum.value, 25);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200516 assert_true(pos_enum.flags & LYS_SET_VALUE);
David Sedlák5545f5d2019-07-11 11:55:16 +0200517 assert_int_equal(val_enum.value, -5);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200518 assert_true(val_enum.flags & LYS_SET_VALUE);
David Sedlákcf5569a2019-07-11 13:31:34 +0200519 assert_int_equal(req_type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200520 assert_true(req_type.flags &= LYS_SET_REQINST);
David Sedlákb7296dd2019-07-11 14:58:38 +0200521 assert_string_equal(range_type.range->arg, "5..10");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200522 assert_true(range_type.flags & LYS_SET_RANGE);
David Sedlákc1771b12019-07-10 15:55:46 +0200523 assert_string_equal(err_msg, "error-msg");
David Sedlák2ce1be62019-07-10 16:15:09 +0200524 assert_string_equal(app_tag, "err-app-tag");
David Sedlákfd5b9c32019-07-12 15:33:13 +0200525 assert_string_equal(enum_type.enums->name, "yay");
David Sedlák438ae432019-07-11 15:36:54 +0200526 assert_string_equal(len_type.length->arg, "baf");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200527 assert_true(len_type.flags & LYS_SET_LENGTH);
David Sedlákd3983112019-07-12 11:20:56 +0200528 assert_string_equal(patter_type.patterns->arg, "\x015pattern");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200529 assert_true(patter_type.flags & LYS_SET_PATTERN);
David Sedlák2ce1be62019-07-10 16:15:09 +0200530 /* cleanup */
David Sedlák555c7202019-07-04 12:14:12 +0200531 lysp_ext_instance_free(st->ctx, exts);
David Sedlák32eee7b2019-07-09 12:38:44 +0200532 lysp_when_free(st->ctx, when_p);
David Sedlák986cb412019-07-04 13:10:11 +0200533 lysp_ext_free(st->ctx, ext_def);
David Sedlák5f8191e2019-07-08 16:35:52 +0200534 FREE_STRING(st->ctx, *if_features);
David Sedlákc1771b12019-07-10 15:55:46 +0200535 FREE_STRING(st->ctx, err_msg);
David Sedlák2ce1be62019-07-10 16:15:09 +0200536 FREE_STRING(st->ctx, app_tag);
David Sedláka5b1d382019-07-10 16:31:09 +0200537 FREE_STRING(st->ctx, units);
David Sedlákd3983112019-07-12 11:20:56 +0200538 FREE_STRING(st->ctx, patter_type.patterns->arg);
David Sedláke7084ce2019-07-10 16:44:15 +0200539 FREE_STRING(st->ctx, def);
David Sedlákb7296dd2019-07-11 14:58:38 +0200540 FREE_STRING(st->ctx, range_type.range->arg);
David Sedlák438ae432019-07-11 15:36:54 +0200541 FREE_STRING(st->ctx, len_type.length->arg);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200542 FREE_STRING(st->ctx, enum_type.enums->name);
David Sedlákb7296dd2019-07-11 14:58:38 +0200543 FREE_STRING(st->ctx, value);
David Sedlák5f8191e2019-07-08 16:35:52 +0200544 LY_ARRAY_FREE(if_features);
David Sedlák555c7202019-07-04 12:14:12 +0200545 LY_ARRAY_FREE(exts);
David Sedlák986cb412019-07-04 13:10:11 +0200546 LY_ARRAY_FREE(ext_def);
David Sedlák555c7202019-07-04 12:14:12 +0200547 LY_ARRAY_FREE(attrs);
David Sedlákd3983112019-07-12 11:20:56 +0200548 LY_ARRAY_FREE(patter_type.patterns);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200549 LY_ARRAY_FREE(enum_type.enums);
David Sedlák32eee7b2019-07-09 12:38:44 +0200550 free(when_p);
David Sedlákb7296dd2019-07-11 14:58:38 +0200551 free(range_type.range);
David Sedlák438ae432019-07-11 15:36:54 +0200552 free(len_type.length);
David Sedlák555c7202019-07-04 12:14:12 +0200553 attrs = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200554 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ákda8ffa32019-07-08 14:17:10 +0200565 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ák555c7202019-07-04 12:14:12 +0200568 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ákda8ffa32019-07-08 14:17:10 +0200584 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ák555c7202019-07-04 12:14:12 +0200587 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ákda8ffa32019-07-08 14:17:10 +0200598 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ák555c7202019-07-04 12:14:12 +0200601 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ák92147b02019-07-09 14:01:01 +0200608static void
David Sedlák4a650532019-07-10 11:55:18 +0200609test_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ák32488102019-07-15 17:44:10 +0200622static int
623setup_element_test(void **state)
624{
David Sedlák8e7bda82019-07-16 17:57:50 +0200625 struct state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +0200626
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ák32488102019-07-15 17:44:10 +0200634
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
643static int
644teardown_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ák32488102019-07-15 17:44:10 +0200656 free(st->yin_ctx);
David Sedlák32488102019-07-15 17:44:10 +0200657
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 */
665LY_ERR
666test_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ákc3da3ef2019-07-19 12:56:08 +0200686 {YANG_DEFAULT, dest, YIN_SUBELEM_UNIQUE},
David Sedlák32488102019-07-15 17:44:10 +0200687 {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ák32488102019-07-15 17:44:10 +0200720 {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ák374d2b32019-07-17 15:06:55 +0200736 {YANG_POSITION, dest, 0},
David Sedlák32488102019-07-15 17:44:10 +0200737 {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ák8e7bda82019-07-16 17:57:50 +0200747 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ák32488102019-07-15 17:44:10 +0200748 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
757static void
758test_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ákc3da3ef2019-07-19 12:56:08 +0200776 assert_true((type.enums->flags & LYS_STATUS_DEPRC) && (type.enums->flags & LYS_SET_VALUE));
David Sedlák32488102019-07-15 17:44:10 +0200777 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
786static void
787test_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
836static void
837test_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
911static void
912test_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ákc3da3ef2019-07-19 12:56:08 +0200921 assert_true(flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +0200922
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ákc3da3ef2019-07-19 12:56:08 +0200925 assert_true(flags & LYS_STATUS_DEPRC);
David Sedlák32488102019-07-15 17:44:10 +0200926
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ákc3da3ef2019-07-19 12:56:08 +0200929 assert_true(flags & LYS_STATUS_OBSLT);
David Sedlák32488102019-07-15 17:44:10 +0200930
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
938static void
939test_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ákc3da3ef2019-07-19 12:56:08 +0200957 assert_true(ext->flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +0200958 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
975static void
976test_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ákc3da3ef2019-07-19 12:56:08 +0200984 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +0200985
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ákc3da3ef2019-07-19 12:56:08 +0200988 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +0200989
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ákc3da3ef2019-07-19 12:56:08 +0200992 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +0200993 logbuf_assert("Invalid value \"invalid\" of \"yin-element\". Line number 1.");
994 st->finished_correctly = true;
995}
996
997static void
998test_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ákc3da3ef2019-07-19 12:56:08 +02001007 assert_true(version & LYS_VERSION_1_0);
David Sedlák32488102019-07-15 17:44:10 +02001008 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ákc3da3ef2019-07-19 12:56:08 +02001012 assert_true(version & LYS_VERSION_1_1);
David Sedlák32488102019-07-15 17:44:10 +02001013 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
1023static void
1024test_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ák8e7bda82019-07-16 17:57:50 +02001047static void
1048test_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ákc3da3ef2019-07-19 12:56:08 +02001063 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001064 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
1081static void
1082test_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ákc3da3ef2019-07-19 12:56:08 +02001104 assert_true(type.flags & LYS_SET_BASE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001105 FREE_STRING(st->ctx, *type.bases);
1106 LY_ARRAY_FREE(type.bases);
1107
1108 st->finished_correctly = true;
1109}
1110
1111static void
1112test_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
1135static void
1136test_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ákc3da3ef2019-07-19 12:56:08 +02001144 assert_true(flags & LYS_CONFIG_W);
David Sedlák8e7bda82019-07-16 17:57:50 +02001145 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ákc3da3ef2019-07-19 12:56:08 +02001149 assert_true(flags & LYS_CONFIG_R);
David Sedlák8e7bda82019-07-16 17:57:50 +02001150 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
1159static void
1160test_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
1179static void
1180test_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
1199static void
1200test_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
1218static void
1219test_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ákc3da3ef2019-07-19 12:56:08 +02001229 assert_true(type.flags & LYS_SET_FRDIGITS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001230
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
1255static void
1256test_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
1278static void
1279test_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ákc3da3ef2019-07-19 12:56:08 +02001300 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001301 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ákc3da3ef2019-07-19 12:56:08 +02001312 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001313 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
1324static void
1325test_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
1345static void
1346test_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
1364static void
1365test_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ákc3da3ef2019-07-19 12:56:08 +02001374 assert_true(type.flags & LYS_SET_PATH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001375 lysp_type_free(st->ctx, &type);
1376
1377 st->finished_correctly = true;
1378}
1379
1380static void
1381test_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ákc3da3ef2019-07-19 12:56:08 +02001398 assert_true(type.flags & LYS_SET_PATTERN);
David Sedlák8e7bda82019-07-16 17:57:50 +02001399 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
1418static void
1419test_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ákc3da3ef2019-07-19 12:56:08 +02001429 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001430 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ákc3da3ef2019-07-19 12:56:08 +02001435 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001436 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ákc3da3ef2019-07-19 12:56:08 +02001441 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001442 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ákc3da3ef2019-07-19 12:56:08 +02001447 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001448 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ákc3da3ef2019-07-19 12:56:08 +02001454 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001455 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ákc3da3ef2019-07-19 12:56:08 +02001460 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001461 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ák69f01612019-07-17 11:41:08 +02001472 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ák8e7bda82019-07-16 17:57:50 +02001476 /*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ák69f01612019-07-17 11:41:08 +02001489 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
1496static void
1497test_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
1511static void
1512test_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ákc3da3ef2019-07-19 12:56:08 +02001533 assert_true(type.flags & LYS_SET_RANGE);
David Sedlák69f01612019-07-17 11:41:08 +02001534 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
1547static void
1548test_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ákc3da3ef2019-07-19 12:56:08 +02001557 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001558 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ákc3da3ef2019-07-19 12:56:08 +02001563 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001564 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
1574static void
1575test_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
1592static void
1593test_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
1608static void
1609test_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
1623static void
1624test_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
1654static void
1655test_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ák8e7bda82019-07-16 17:57:50 +02001676 st->finished_correctly = true;
1677}
David Sedlák32488102019-07-15 17:44:10 +02001678
David Sedlák374d2b32019-07-17 15:06:55 +02001679static void
1680test_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ákc3da3ef2019-07-19 12:56:08 +02001714 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ák374d2b32019-07-17 15:06:55 +02001724 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ák1af868e2019-07-17 17:03:14 +02001735static void
1736test_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ákc3da3ef2019-07-19 12:56:08 +02001747 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001748
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ákc3da3ef2019-07-19 12:56:08 +02001752 assert_true(list.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001753
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ákc3da3ef2019-07-19 12:56:08 +02001757 assert_true(llist.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001758
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ákc3da3ef2019-07-19 12:56:08 +02001762 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001763
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ák09e18c92019-07-18 11:17:11 +02001783static void
1784test_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ákc3da3ef2019-07-19 12:56:08 +02001795 assert_true(refine.flags & LYS_SET_MIN);
David Sedlák09e18c92019-07-18 11:17:11 +02001796
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ákc3da3ef2019-07-19 12:56:08 +02001800 assert_true(list.flags & LYS_SET_MIN);
David Sedlák09e18c92019-07-18 11:17:11 +02001801
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ákc3da3ef2019-07-19 12:56:08 +02001805 assert_true(llist.flags & LYS_SET_MIN);
David Sedlák09e18c92019-07-18 11:17:11 +02001806
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áka2dad212019-07-18 12:45:19 +02001826static void
1827test_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ákc3da3ef2019-07-19 12:56:08 +02001835 assert_true(flags & LYS_ORDBY_SYSTEM);
David Sedláka2dad212019-07-18 12:45:19 +02001836
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ákc3da3ef2019-07-19 12:56:08 +02001839 assert_true(flags & LYS_ORDBY_USER);
David Sedláka2dad212019-07-18 12:45:19 +02001840
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ák8a83bbb2019-07-18 14:46:00 +02001848static void
1849test_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ákc3da3ef2019-07-19 12:56:08 +02001874 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ák8a83bbb2019-07-18 14:46:00 +02001877 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ákc3da3ef2019-07-19 12:56:08 +02001904 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ák8a83bbb2019-07-18 14:46:00 +02001907 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ák203ca3a2019-07-18 15:26:25 +02001931static void
1932test_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ákc3da3ef2019-07-19 12:56:08 +02001960 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ák203ca3a2019-07-18 15:26:25 +02001963 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ákc3da3ef2019-07-19 12:56:08 +02001989static void
1990test_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ákcb39f642019-07-19 13:19:55 +02002158static void
2159test_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ák12470a82019-07-19 13:44:36 +02002177static void
2178test_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ák04e17b22019-07-19 15:29:48 +02002196static void
2197test_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ákd2d676a2019-07-22 11:28:19 +02002240static void
2241test_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ák3b4db242018-10-19 16:11:01 +02002288int
2289main(void)
2290{
2291
2292 const struct CMUnitTest tests[] = {
David Sedlák392af4f2019-06-04 16:02:42 +02002293 cmocka_unit_test_setup_teardown(test_yin_parse_module, setup_f, teardown_f),
David Sedlák8f7a1172019-06-20 14:42:18 +02002294 cmocka_unit_test_setup_teardown(test_yin_match_keyword, setup_f, teardown_f),
David Sedlákb1a78352019-06-28 16:16:29 +02002295 cmocka_unit_test_setup_teardown(test_yin_parse_element_generic, setup_f, teardown_f),
2296 cmocka_unit_test_setup_teardown(test_yin_parse_extension_instance, setup_f, teardown_f),
David Sedlák555c7202019-07-04 12:14:12 +02002297 cmocka_unit_test_setup_teardown(test_yin_parse_content, setup_f, teardown_f),
David Sedlák4a650532019-07-10 11:55:18 +02002298 cmocka_unit_test_setup_teardown(test_validate_value, setup_f, teardown_f),
David Sedlák32488102019-07-15 17:44:10 +02002299
David Sedlák8e7bda82019-07-16 17:57:50 +02002300 cmocka_unit_test(test_yin_match_argument_name),
David Sedlák32488102019-07-15 17:44:10 +02002301 cmocka_unit_test_setup_teardown(test_enum_bit_elem, setup_element_test, teardown_element_test),
2302 cmocka_unit_test_setup_teardown(test_meta_elem, setup_element_test, teardown_element_test),
2303 cmocka_unit_test_setup_teardown(test_import_elem, setup_element_test, teardown_element_test),
2304 cmocka_unit_test_setup_teardown(test_status_elem, setup_element_test, teardown_element_test),
2305 cmocka_unit_test_setup_teardown(test_ext_elem, setup_element_test, teardown_element_test),
2306 cmocka_unit_test_setup_teardown(test_yin_element_elem, setup_element_test, teardown_element_test),
2307 cmocka_unit_test_setup_teardown(test_yangversion_elem, setup_element_test, teardown_element_test),
2308 cmocka_unit_test_setup_teardown(test_mandatory_elem, setup_element_test, teardown_element_test),
David Sedlák8e7bda82019-07-16 17:57:50 +02002309 cmocka_unit_test_setup_teardown(test_argument_elem, setup_element_test, teardown_element_test),
2310 cmocka_unit_test_setup_teardown(test_base_elem, setup_element_test, teardown_element_test),
2311 cmocka_unit_test_setup_teardown(test_belongsto_elem, setup_element_test, teardown_element_test),
2312 cmocka_unit_test_setup_teardown(test_config_elem, setup_element_test, teardown_element_test),
2313 cmocka_unit_test_setup_teardown(test_default_elem, setup_element_test, teardown_element_test),
2314 cmocka_unit_test_setup_teardown(test_err_app_tag_elem, setup_element_test, teardown_element_test),
2315 cmocka_unit_test_setup_teardown(test_err_msg_elem, setup_element_test, teardown_element_test),
2316 cmocka_unit_test_setup_teardown(test_fracdigits_elem, setup_element_test, teardown_element_test),
2317 cmocka_unit_test_setup_teardown(test_iffeature_elem, setup_element_test, teardown_element_test),
2318 cmocka_unit_test_setup_teardown(test_length_elem, setup_element_test, teardown_element_test),
2319 cmocka_unit_test_setup_teardown(test_modifier_elem, setup_element_test, teardown_element_test),
2320 cmocka_unit_test_setup_teardown(test_namespace_elem, setup_element_test, teardown_element_test),
2321 cmocka_unit_test_setup_teardown(test_path_elem, setup_element_test, teardown_element_test),
2322 cmocka_unit_test_setup_teardown(test_pattern_elem, setup_element_test, teardown_element_test),
2323 cmocka_unit_test_setup_teardown(test_value_position_elem, setup_element_test, teardown_element_test),
David Sedlák69f01612019-07-17 11:41:08 +02002324 cmocka_unit_test_setup_teardown(test_prefix_elem, setup_element_test, teardown_element_test),
2325 cmocka_unit_test_setup_teardown(test_range_elem, setup_element_test, teardown_element_test),
2326 cmocka_unit_test_setup_teardown(test_reqinstance_elem, setup_element_test, teardown_element_test),
2327 cmocka_unit_test_setup_teardown(test_revision_date_elem, setup_element_test, teardown_element_test),
2328 cmocka_unit_test_setup_teardown(test_unique_elem, setup_element_test, teardown_element_test),
2329 cmocka_unit_test_setup_teardown(test_units_elem, setup_element_test, teardown_element_test),
2330 cmocka_unit_test_setup_teardown(test_when_elem, setup_element_test, teardown_element_test),
2331 cmocka_unit_test_setup_teardown(test_yin_text_value_elem, setup_element_test, teardown_element_test),
David Sedlák374d2b32019-07-17 15:06:55 +02002332 cmocka_unit_test_setup_teardown(test_type_elem, setup_element_test, teardown_element_test),
David Sedlák1af868e2019-07-17 17:03:14 +02002333 cmocka_unit_test_setup_teardown(test_max_elems_elem, setup_element_test, teardown_element_test),
David Sedlák09e18c92019-07-18 11:17:11 +02002334 cmocka_unit_test_setup_teardown(test_min_elems_elem, setup_element_test, teardown_element_test),
David Sedláka2dad212019-07-18 12:45:19 +02002335 cmocka_unit_test_setup_teardown(test_ordby_elem, setup_element_test, teardown_element_test),
David Sedlák8a83bbb2019-07-18 14:46:00 +02002336 cmocka_unit_test_setup_teardown(test_any_elem, setup_element_test, teardown_element_test),
David Sedlák203ca3a2019-07-18 15:26:25 +02002337 cmocka_unit_test_setup_teardown(test_leaf_elem, setup_element_test, teardown_element_test),
David Sedlákc3da3ef2019-07-19 12:56:08 +02002338 cmocka_unit_test_setup_teardown(test_leaf_list_elem, setup_element_test, teardown_element_test),
David Sedlákcb39f642019-07-19 13:19:55 +02002339 cmocka_unit_test_setup_teardown(test_presence_elem, setup_element_test, teardown_element_test),
David Sedlák12470a82019-07-19 13:44:36 +02002340 cmocka_unit_test_setup_teardown(test_key_elem, setup_element_test, teardown_element_test),
David Sedlák04e17b22019-07-19 15:29:48 +02002341 cmocka_unit_test_setup_teardown(test_typedef_elem, setup_element_test, teardown_element_test),
David Sedlákd2d676a2019-07-22 11:28:19 +02002342 cmocka_unit_test_setup_teardown(test_refine_elem, setup_element_test, teardown_element_test),
David Sedlák3b4db242018-10-19 16:11:01 +02002343 };
2344
David Sedlák8e7bda82019-07-16 17:57:50 +02002345 return cmocka_run_group_tests(tests, setup_ly_ctx, destroy_ly_ctx);
David Sedlák3b4db242018-10-19 16:11:01 +02002346}