blob: acd6c838b70fb22443a2082c88a04ad44bc15f69 [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ákaa854b02019-07-22 14:17:10 +020038void lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev);
David Sedlák0c2bab92019-07-22 15:33:19 +020039void lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include);
David Sedlák5e13dea2019-07-22 16:06:45 +020040void lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat);
David Sedlák28794f22019-07-22 16:45:00 +020041void lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident);
David Sedlák031b9e72019-07-23 15:19:37 +020042void lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif);
David Sedláke3ce9ef2019-07-23 16:34:30 +020043void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp);
David Sedlák05404f62019-07-24 14:11:53 +020044void lysp_action_inout_free(struct ly_ctx *ctx, struct lysp_action_inout *inout);
David Sedlák555c7202019-07-04 12:14:12 +020045
David Sedlák68a1af12019-03-08 13:46:54 +010046struct state {
David Sedlák3b4db242018-10-19 16:11:01 +020047 struct ly_ctx *ctx;
David Sedlák3017da42019-02-15 09:48:04 +010048 struct lys_module *mod;
David Sedlák619db942019-07-03 14:47:30 +020049 struct lysp_module *lysp_mod;
David Sedlákda8ffa32019-07-08 14:17:10 +020050 struct yin_parser_ctx *yin_ctx;
David Sedlák79e50cb2019-06-05 16:33:09 +020051 bool finished_correctly;
David Sedlák68a1af12019-03-08 13:46:54 +010052};
David Sedlák872c7b42018-10-26 13:15:20 +020053
David Sedlák79e50cb2019-06-05 16:33:09 +020054#define BUFSIZE 1024
55char logbuf[BUFSIZE] = {0};
56int store = -1; /* negative for infinite logging, positive for limited logging */
57
58/* set to 0 to printing error messages to stderr instead of checking them in code */
David Sedlákb7abcfa2019-07-24 12:33:35 +020059#define ENABLE_LOGGER_CHECKING 1
David Sedlák79e50cb2019-06-05 16:33:09 +020060
61#if ENABLE_LOGGER_CHECKING
62static void
63logger(LY_LOG_LEVEL level, const char *msg, const char *path)
64{
65 (void) level; /* unused */
66 if (store) {
67 if (path && path[0]) {
68 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
69 } else {
70 strncpy(logbuf, msg, BUFSIZE - 1);
71 }
72 if (store > 0) {
73 --store;
74 }
75 }
76}
77#endif
78
79#if ENABLE_LOGGER_CHECKING
80# define logbuf_assert(str) assert_string_equal(logbuf, str)
81#else
82# define logbuf_assert(str)
83#endif
84
85#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
86 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
87 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
88 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
89 CLEANUP
90
David Sedlák8e7bda82019-07-16 17:57:50 +020091int
92setup_ly_ctx(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +010093{
94 struct state *st = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +020095
David Sedlák68a1af12019-03-08 13:46:54 +010096 /* allocate state variable */
97 (*state) = st = calloc(1, sizeof(*st));
98 if (!st) {
99 fprintf(stderr, "Memmory allocation failed");
100 return EXIT_FAILURE;
101 }
David Sedlák872c7b42018-10-26 13:15:20 +0200102
David Sedlák68a1af12019-03-08 13:46:54 +0100103 /* create new libyang context */
104 ly_ctx_new(NULL, 0, &st->ctx);
David Sedlák872c7b42018-10-26 13:15:20 +0200105
David Sedlák8e7bda82019-07-16 17:57:50 +0200106 return EXIT_SUCCESS;
107}
108
109int
110destroy_ly_ctx(void **state)
111{
112 struct state *st = *state;
113 ly_ctx_destroy(st->ctx, NULL);
114 free(st);
115
116 return EXIT_SUCCESS;
117}
118
119static int
120setup_f(void **state)
121{
122 struct state *st = *state;
123
124#if ENABLE_LOGGER_CHECKING
125 /* setup logger */
126 ly_set_log_clb(logger, 1);
127#endif
128
David Sedlák68a1af12019-03-08 13:46:54 +0100129 /* allocate new module */
130 st->mod = calloc(1, sizeof(*st->mod));
131 st->mod->ctx = st->ctx;
132
David Sedlák619db942019-07-03 14:47:30 +0200133 /* allocate new parsed module */
134 st->lysp_mod = calloc(1, sizeof(*st->lysp_mod));
135 st->lysp_mod->mod = calloc(1, sizeof(*st->lysp_mod->mod));
136 st->lysp_mod->mod->ctx = st->ctx;
137
138 /* allocate parser context */
David Sedlákda8ffa32019-07-08 14:17:10 +0200139 st->yin_ctx = calloc(1, sizeof(*st->yin_ctx));
140 st->yin_ctx->xml_ctx.ctx = st->ctx;
141 st->yin_ctx->xml_ctx.line = 1;
David Sedlák8f5bce02019-06-03 16:41:08 +0200142
David Sedlák68a1af12019-03-08 13:46:54 +0100143 return EXIT_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +0200144}
145
146static int
David Sedlák68a1af12019-03-08 13:46:54 +0100147teardown_f(void **state)
148{
149 struct state *st = *(struct state **)state;
David Sedlák619db942019-07-03 14:47:30 +0200150 struct lys_module *temp;
David Sedlák68a1af12019-03-08 13:46:54 +0100151
David Sedlák79e50cb2019-06-05 16:33:09 +0200152#if ENABLE_LOGGER_CHECKING
153 /* teardown logger */
154 if (!st->finished_correctly && logbuf[0] != '\0') {
155 fprintf(stderr, "%s\n", logbuf);
156 }
157#endif
158
David Sedlák619db942019-07-03 14:47:30 +0200159 temp = st->lysp_mod->mod;
160
David Sedlákda8ffa32019-07-08 14:17:10 +0200161 lyxml_context_clear(&st->yin_ctx->xml_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100162 lys_module_free(st->mod, NULL);
David Sedlák619db942019-07-03 14:47:30 +0200163 lysp_module_free(st->lysp_mod);
164 lys_module_free(temp, NULL);
David Sedlákda8ffa32019-07-08 14:17:10 +0200165 free(st->yin_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100166
167 return EXIT_SUCCESS;
168}
169
David Sedlák392af4f2019-06-04 16:02:42 +0200170static struct state*
171reset_state(void **state)
172{
David Sedlák79e50cb2019-06-05 16:33:09 +0200173 ((struct state *)*state)->finished_correctly = true;
David Sedlák555c7202019-07-04 12:14:12 +0200174 logbuf[0] = '\0';
David Sedlák392af4f2019-06-04 16:02:42 +0200175 teardown_f(state);
176 setup_f(state);
177
178 return *state;
179}
180
David Sedlák79e50cb2019-06-05 16:33:09 +0200181void
182logbuf_clean(void)
183{
184 logbuf[0] = '\0';
185}
186
David Sedlák68a1af12019-03-08 13:46:54 +0100187static void
David Sedlák392af4f2019-06-04 16:02:42 +0200188test_yin_parse_module(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +0100189{
190 LY_ERR ret = LY_SUCCESS;
191 struct state *st = *state;
192
193 ret = yin_parse_module(st->ctx,
David Sedlák2b214ac2019-06-06 16:11:03 +0200194 "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\
195 name=\"example-foo\"\
David Sedlák18730132019-03-15 15:51:34 +0100196 xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\
197 xmlns:foo=\"urn:example:foo\"\
198 xmlns:myext=\"urn:example:extensions\">\
David Sedlákcd0c9512019-03-29 13:23:06 +0100199 <namespace uri=\"urn:example:foo\" xmlns:myext=\"urn:example:extensions\"/>\
David Sedláka7406952019-04-05 10:33:07 +0200200 <prefix xmlns:myxt=\"urn:emple:extensions\" value=\"foo\" xmlns:myext=\"urn:example:extensions\"/>\
David Sedlákd9d3a312019-06-04 09:47:10 +0200201 </module>",
David Sedlák68a1af12019-03-08 13:46:54 +0100202 st->mod);
203
204 assert_int_equal(ret, LY_SUCCESS);
205 assert_string_equal(st->mod->parsed->mod->name, "example-foo");
206 assert_string_equal(st->mod->parsed->mod->prefix, "foo");
David Sedlákcd0c9512019-03-29 13:23:06 +0100207 assert_string_equal(st->mod->parsed->mod->ns, "urn:example:foo");
David Sedlák392af4f2019-06-04 16:02:42 +0200208
209 st = reset_state(state);
210 ret = yin_parse_module(st->ctx,
David Sedlák2b214ac2019-06-06 16:11:03 +0200211 "<module name=\"example-foo\">\
212 <invalid-tag uri=\"urn:example:foo\"\"/>\
213 </module>",
214 st->mod);
David Sedlák392af4f2019-06-04 16:02:42 +0200215 assert_int_equal(ret, LY_EVALID);
216
217 st = reset_state(state);
218 ret = yin_parse_module(st->ctx,
David Sedlák8f7a1172019-06-20 14:42:18 +0200219 "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\
David Sedlák57715b12019-06-17 13:05:22 +0200220 </module>",
David Sedlák2b214ac2019-06-06 16:11:03 +0200221 st->mod);
David Sedlák392af4f2019-06-04 16:02:42 +0200222 assert_int_equal(ret, LY_EVALID);
David Sedlák882a8072019-07-08 17:51:20 +0200223 logbuf_assert("Missing mandatory attribute name of module element. Line number 1.");
David Sedlák392af4f2019-06-04 16:02:42 +0200224
225 st = reset_state(state);
226 ret = yin_parse_module(st->ctx,
227 "",
228 st->mod);
229 assert_int_equal(ret, LY_EVALID);
David Sedlák79e50cb2019-06-05 16:33:09 +0200230 logbuf_assert("Invalid keyword \"(null)\", expected \"module\" or \"submodule\". Line number 1.");
231 st->finished_correctly = true;
David Sedlák3b4db242018-10-19 16:11:01 +0200232}
233
234static void
David Sedlák1bccdfa2019-06-17 15:55:27 +0200235test_yin_match_keyword(void **state)
David Sedlák3b4db242018-10-19 16:11:01 +0200236{
David Sedlák8f7a1172019-06-20 14:42:18 +0200237 struct state *st = *state;
David Sedlák3b4db242018-10-19 16:11:01 +0200238
David Sedlák8f7a1172019-06-20 14:42:18 +0200239 const char *prefix, *name;
240 struct yin_arg_record *args = NULL;
241 size_t prefix_len, name_len;
242 /* create mock yin namespace in xml context */
243 const char *data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" />";
David Sedlákda8ffa32019-07-08 14:17:10 +0200244 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
245 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák8f7a1172019-06-20 14:42:18 +0200246 LY_ARRAY_FREE(args);
247
David Sedlákc1771b12019-07-10 15:55:46 +0200248 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydatax", strlen("anydatax"), prefix, prefix_len, YANG_NONE), YANG_NONE);
249 assert_int_equal(yin_match_keyword(st->yin_ctx, "asdasd", strlen("asdasd"), prefix, prefix_len, YANG_NONE), YANG_NONE);
250 assert_int_equal(yin_match_keyword(st->yin_ctx, "", 0, prefix, prefix_len, YANG_NONE), YANG_NONE);
251 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydata", strlen("anydata"), prefix, prefix_len, YANG_NONE), YANG_ANYDATA);
252 assert_int_equal(yin_match_keyword(st->yin_ctx, "anyxml", strlen("anyxml"), prefix, prefix_len, YANG_NONE), YANG_ANYXML);
253 assert_int_equal(yin_match_keyword(st->yin_ctx, "argument", strlen("argument"), prefix, prefix_len, YANG_NONE), YANG_ARGUMENT);
254 assert_int_equal(yin_match_keyword(st->yin_ctx, "augment", strlen("augment"), prefix, prefix_len, YANG_NONE), YANG_AUGMENT);
255 assert_int_equal(yin_match_keyword(st->yin_ctx, "base", strlen("base"), prefix, prefix_len, YANG_NONE), YANG_BASE);
256 assert_int_equal(yin_match_keyword(st->yin_ctx, "belongs-to", strlen("belongs-to"), prefix, prefix_len, YANG_NONE), YANG_BELONGS_TO);
257 assert_int_equal(yin_match_keyword(st->yin_ctx, "bit", strlen("bit"), prefix, prefix_len, YANG_NONE), YANG_BIT);
258 assert_int_equal(yin_match_keyword(st->yin_ctx, "case", strlen("case"), prefix, prefix_len, YANG_NONE), YANG_CASE);
259 assert_int_equal(yin_match_keyword(st->yin_ctx, "choice", strlen("choice"), prefix, prefix_len, YANG_NONE), YANG_CHOICE);
260 assert_int_equal(yin_match_keyword(st->yin_ctx, "config", strlen("config"), prefix, prefix_len, YANG_NONE), YANG_CONFIG);
261 assert_int_equal(yin_match_keyword(st->yin_ctx, "contact", strlen("contact"), prefix, prefix_len, YANG_NONE), YANG_CONTACT);
262 assert_int_equal(yin_match_keyword(st->yin_ctx, "container", strlen("container"), prefix, prefix_len, YANG_NONE), YANG_CONTAINER);
263 assert_int_equal(yin_match_keyword(st->yin_ctx, "default", strlen("default"), prefix, prefix_len, YANG_NONE), YANG_DEFAULT);
264 assert_int_equal(yin_match_keyword(st->yin_ctx, "description", strlen("description"), prefix, prefix_len, YANG_NONE), YANG_DESCRIPTION);
265 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviate", strlen("deviate"), prefix, prefix_len, YANG_NONE), YANG_DEVIATE);
266 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviation", strlen("deviation"), prefix, prefix_len, YANG_NONE), YANG_DEVIATION);
267 assert_int_equal(yin_match_keyword(st->yin_ctx, "enum", strlen("enum"), prefix, prefix_len, YANG_NONE), YANG_ENUM);
268 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);
269 assert_int_equal(yin_match_keyword(st->yin_ctx, "error-message", strlen("error-message"), prefix, prefix_len, YANG_NONE), YANG_ERROR_MESSAGE);
270 assert_int_equal(yin_match_keyword(st->yin_ctx, "extension", strlen("extension"), prefix, prefix_len, YANG_NONE), YANG_EXTENSION);
271 assert_int_equal(yin_match_keyword(st->yin_ctx, "feature", strlen("feature"), prefix, prefix_len, YANG_NONE), YANG_FEATURE);
272 assert_int_equal(yin_match_keyword(st->yin_ctx, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len, YANG_NONE), YANG_FRACTION_DIGITS);
273 assert_int_equal(yin_match_keyword(st->yin_ctx, "grouping", strlen("grouping"), prefix, prefix_len, YANG_NONE), YANG_GROUPING);
274 assert_int_equal(yin_match_keyword(st->yin_ctx, "identity", strlen("identity"), prefix, prefix_len, YANG_NONE), YANG_IDENTITY);
275 assert_int_equal(yin_match_keyword(st->yin_ctx, "if-feature", strlen("if-feature"), prefix, prefix_len, YANG_NONE), YANG_IF_FEATURE);
276 assert_int_equal(yin_match_keyword(st->yin_ctx, "import", strlen("import"), prefix, prefix_len, YANG_NONE), YANG_IMPORT);
277 assert_int_equal(yin_match_keyword(st->yin_ctx, "include", strlen("include"), prefix, prefix_len, YANG_NONE), YANG_INCLUDE);
278 assert_int_equal(yin_match_keyword(st->yin_ctx, "input", strlen("input"), prefix, prefix_len, YANG_NONE), YANG_INPUT);
279 assert_int_equal(yin_match_keyword(st->yin_ctx, "key", strlen("key"), prefix, prefix_len, YANG_NONE), YANG_KEY);
280 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf", strlen("leaf"), prefix, prefix_len, YANG_NONE), YANG_LEAF);
281 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf-list", strlen("leaf-list"), prefix, prefix_len, YANG_NONE), YANG_LEAF_LIST);
282 assert_int_equal(yin_match_keyword(st->yin_ctx, "length", strlen("length"), prefix, prefix_len, YANG_NONE), YANG_LENGTH);
283 assert_int_equal(yin_match_keyword(st->yin_ctx, "list", strlen("list"), prefix, prefix_len, YANG_NONE), YANG_LIST);
284 assert_int_equal(yin_match_keyword(st->yin_ctx, "mandatory", strlen("mandatory"), prefix, prefix_len, YANG_NONE), YANG_MANDATORY);
285 assert_int_equal(yin_match_keyword(st->yin_ctx, "max-elements", strlen("max-elements"), prefix, prefix_len, YANG_NONE), YANG_MAX_ELEMENTS);
286 assert_int_equal(yin_match_keyword(st->yin_ctx, "min-elements", strlen("min-elements"), prefix, prefix_len, YANG_NONE), YANG_MIN_ELEMENTS);
287 assert_int_equal(yin_match_keyword(st->yin_ctx, "modifier", strlen("modifier"), prefix, prefix_len, YANG_NONE), YANG_MODIFIER);
288 assert_int_equal(yin_match_keyword(st->yin_ctx, "module", strlen("module"), prefix, prefix_len, YANG_NONE), YANG_MODULE);
289 assert_int_equal(yin_match_keyword(st->yin_ctx, "must", strlen("must"), prefix, prefix_len, YANG_NONE), YANG_MUST);
290 assert_int_equal(yin_match_keyword(st->yin_ctx, "namespace", strlen("namespace"), prefix, prefix_len, YANG_NONE), YANG_NAMESPACE);
291 assert_int_equal(yin_match_keyword(st->yin_ctx, "notification", strlen("notification"), prefix, prefix_len, YANG_NONE), YANG_NOTIFICATION);
292 assert_int_equal(yin_match_keyword(st->yin_ctx, "ordered-by", strlen("ordered-by"), prefix, prefix_len, YANG_NONE), YANG_ORDERED_BY);
293 assert_int_equal(yin_match_keyword(st->yin_ctx, "organization", strlen("organization"), prefix, prefix_len, YANG_NONE), YANG_ORGANIZATION);
294 assert_int_equal(yin_match_keyword(st->yin_ctx, "output", strlen("output"), prefix, prefix_len, YANG_NONE), YANG_OUTPUT);
295 assert_int_equal(yin_match_keyword(st->yin_ctx, "path", strlen("path"), prefix, prefix_len, YANG_NONE), YANG_PATH);
296 assert_int_equal(yin_match_keyword(st->yin_ctx, "pattern", strlen("pattern"), prefix, prefix_len, YANG_NONE), YANG_PATTERN);
297 assert_int_equal(yin_match_keyword(st->yin_ctx, "position", strlen("position"), prefix, prefix_len, YANG_NONE), YANG_POSITION);
298 assert_int_equal(yin_match_keyword(st->yin_ctx, "prefix", strlen("prefix"), prefix, prefix_len, YANG_NONE), YANG_PREFIX);
299 assert_int_equal(yin_match_keyword(st->yin_ctx, "presence", strlen("presence"), prefix, prefix_len, YANG_NONE), YANG_PRESENCE);
300 assert_int_equal(yin_match_keyword(st->yin_ctx, "range", strlen("range"), prefix, prefix_len, YANG_NONE), YANG_RANGE);
301 assert_int_equal(yin_match_keyword(st->yin_ctx, "reference", strlen("reference"), prefix, prefix_len, YANG_NONE), YANG_REFERENCE);
302 assert_int_equal(yin_match_keyword(st->yin_ctx, "refine", strlen("refine"), prefix, prefix_len, YANG_NONE), YANG_REFINE);
303 assert_int_equal(yin_match_keyword(st->yin_ctx, "require-instance", strlen("require-instance"), prefix, prefix_len, YANG_NONE), YANG_REQUIRE_INSTANCE);
304 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision", strlen("revision"), prefix, prefix_len, YANG_NONE), YANG_REVISION);
305 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision-date", strlen("revision-date"), prefix, prefix_len, YANG_NONE), YANG_REVISION_DATE);
306 assert_int_equal(yin_match_keyword(st->yin_ctx, "rpc", strlen("rpc"), prefix, prefix_len, YANG_NONE), YANG_RPC);
307 assert_int_equal(yin_match_keyword(st->yin_ctx, "status", strlen("status"), prefix, prefix_len, YANG_NONE), YANG_STATUS);
308 assert_int_equal(yin_match_keyword(st->yin_ctx, "submodule", strlen("submodule"), prefix, prefix_len, YANG_NONE), YANG_SUBMODULE);
309 assert_int_equal(yin_match_keyword(st->yin_ctx, "type", strlen("type"), prefix, prefix_len, YANG_NONE), YANG_TYPE);
310 assert_int_equal(yin_match_keyword(st->yin_ctx, "typedef", strlen("typedef"), prefix, prefix_len, YANG_NONE), YANG_TYPEDEF);
311 assert_int_equal(yin_match_keyword(st->yin_ctx, "unique", strlen("unique"), prefix, prefix_len, YANG_NONE), YANG_UNIQUE);
312 assert_int_equal(yin_match_keyword(st->yin_ctx, "units", strlen("units"), prefix, prefix_len, YANG_NONE), YANG_UNITS);
313 assert_int_equal(yin_match_keyword(st->yin_ctx, "uses", strlen("uses"), prefix, prefix_len, YANG_NONE), YANG_USES);
314 assert_int_equal(yin_match_keyword(st->yin_ctx, "value", strlen("value"), prefix, prefix_len, YANG_NONE), YANG_VALUE);
315 assert_int_equal(yin_match_keyword(st->yin_ctx, "when", strlen("when"), prefix, prefix_len, YANG_NONE), YANG_WHEN);
316 assert_int_equal(yin_match_keyword(st->yin_ctx, "yang-version", strlen("yang-version"), prefix, prefix_len, YANG_NONE), YANG_YANG_VERSION);
317 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 +0200318
319 st->finished_correctly = true;
David Sedlák872c7b42018-10-26 13:15:20 +0200320}
David Sedlák3b4db242018-10-19 16:11:01 +0200321
David Sedlák872c7b42018-10-26 13:15:20 +0200322static void
David Sedlák060b00e2019-06-19 11:12:06 +0200323test_yin_match_argument_name(void **state)
David Sedlák872c7b42018-10-26 13:15:20 +0200324{
David Sedlák68a1af12019-03-08 13:46:54 +0100325 (void)state; /* unused */
David Sedlák872c7b42018-10-26 13:15:20 +0200326
David Sedlák060b00e2019-06-19 11:12:06 +0200327 assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN);
328 assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN);
329 assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN);
330 assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION);
331 assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE);
332 assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE);
333 assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME);
334 assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG);
335 assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
336 assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT);
337 assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI);
338 assert_int_equal(yin_match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200339}
340
David Sedlák68a1af12019-03-08 13:46:54 +0100341static void
David Sedlákb1a78352019-06-28 16:16:29 +0200342test_yin_parse_element_generic(void **state)
343{
344 const char *prefix, *name;
345 struct state *st = *state;
346 struct lysp_ext_instance exts;
347 size_t prefix_len, name_len;
348 LY_ERR ret;
349
350 memset(&exts, 0, sizeof(exts));
351
352 const char *data = "<elem attr=\"value\">text_value</elem>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200353 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
354 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 +0200355 assert_int_equal(ret, LY_SUCCESS);
David Sedlák1fdb2522019-07-09 16:22:57 +0200356 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákb1a78352019-06-28 16:16:29 +0200357 assert_string_equal(exts.child->stmt, "elem");
358 assert_string_equal(exts.child->arg, "text_value");
David Sedlákb1a78352019-06-28 16:16:29 +0200359 assert_string_equal(exts.child->child->stmt, "attr");
360 assert_string_equal(exts.child->child->arg, "value");
361 assert_true(exts.child->child->flags & LYS_YIN_ATTR);
David Sedlákb1a78352019-06-28 16:16:29 +0200362 lysp_ext_instance_free(st->ctx, &exts);
David Sedlák5392a212019-07-01 09:19:10 +0200363 st = reset_state(state);
364
365 data = "<elem></elem>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200366 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
367 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 +0200368 assert_int_equal(ret, LY_SUCCESS);
369 assert_string_equal(exts.child->stmt, "elem");
370 assert_null(exts.child->child);
371 assert_null(exts.child->arg);
David Sedlákda8ffa32019-07-08 14:17:10 +0200372 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlák5392a212019-07-01 09:19:10 +0200373 lysp_ext_instance_free(st->ctx, &exts);
374
David Sedlákb1a78352019-06-28 16:16:29 +0200375 st->finished_correctly = true;
376}
377
378static void
379test_yin_parse_extension_instance(void **state)
380{
381 LY_ERR ret;
382 struct state *st = *state;
383 const char *prefix, *name;
384 size_t prefix_len, name_len;
385 struct yin_arg_record *args = NULL;
386 struct lysp_ext_instance *exts = NULL;
David Sedlákb1a78352019-06-28 16:16:29 +0200387 const char *data = "<ext value1=\"test\" value=\"test2\"><subelem>text</subelem></ext>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200388 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
389 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák1f90d252019-07-10 17:09:32 +0200390 ret = yin_parse_extension_instance(st->yin_ctx, args, &data, name2fullname(name, prefix_len),
David Sedlák619db942019-07-03 14:47:30 +0200391 namelen2fulllen(name_len, prefix_len), LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200392 assert_int_equal(ret, LY_SUCCESS);
393 assert_string_equal(exts->name, "ext");
394 assert_int_equal(exts->insubstmt_index, 0);
395 assert_true(exts->insubstmt == LYEXT_SUBSTMT_CONTACT);
396 assert_true(exts->yin & LYS_YIN);
397 assert_string_equal(exts->child->stmt, "value1");
398 assert_string_equal(exts->child->arg, "test");
399 assert_null(exts->child->child);
400 assert_true(exts->child->flags & LYS_YIN_ATTR);
401 assert_string_equal(exts->child->next->stmt, "value");
402 assert_string_equal(exts->child->next->arg, "test2");
403 assert_null(exts->child->next->child);
404 assert_true(exts->child->next->flags & LYS_YIN_ATTR);
405
406 assert_string_equal(exts->child->next->next->stmt, "subelem");
407 assert_string_equal(exts->child->next->next->arg, "text");
408 assert_null(exts->child->next->next->child);
409 assert_null(exts->child->next->next->next);
410 assert_false(exts->child->next->next->flags & LYS_YIN_ATTR);
David Sedlákda8ffa32019-07-08 14:17:10 +0200411 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákb1a78352019-06-28 16:16:29 +0200412 LY_ARRAY_FREE(args);
413 lysp_ext_instance_free(st->ctx, exts);
414 LY_ARRAY_FREE(exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200415 exts = NULL;
416 args = NULL;
417 st = reset_state(state);
418
419 data = "<extension-elem />";
David Sedlákda8ffa32019-07-08 14:17:10 +0200420 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
421 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák1f90d252019-07-10 17:09:32 +0200422 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 +0200423 assert_int_equal(ret, LY_SUCCESS);
424 assert_string_equal(exts->name, "extension-elem");
425 assert_null(exts->argument);
426 assert_null(exts->child);
427 assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT);
428 assert_int_equal(exts->insubstmt_index, 0);
429 assert_true(exts->yin & LYS_YIN);
David Sedlákda8ffa32019-07-08 14:17:10 +0200430 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákf250ecf2019-07-01 11:02:05 +0200431 LY_ARRAY_FREE(args);
432 lysp_ext_instance_free(st->ctx, exts);
433 LY_ARRAY_FREE(exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200434 st->finished_correctly = true;
435}
436
David Sedlák555c7202019-07-04 12:14:12 +0200437static void
438test_yin_parse_content(void **state)
439{
440 struct state *st = *state;
441 LY_ERR ret = LY_SUCCESS;
442 struct sized_string name, prefix;
443 const char *data = "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
444 "<custom xmlns=\"my-ext\">"
445 "totally amazing extension"
446 "</custom>"
David Sedlák986cb412019-07-04 13:10:11 +0200447 "<extension name=\"ext\">"
448 "<argument name=\"argname\"></argument>"
449 "<description><text>desc</text></description>"
450 "<reference><text>ref</text></reference>"
451 "<status value=\"deprecated\"></status>"
452 "</extension>"
David Sedlák555c7202019-07-04 12:14:12 +0200453 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedlák32488102019-07-15 17:44:10 +0200454 "<if-feature name=\"foo\"></if-feature>"
David Sedlák32eee7b2019-07-09 12:38:44 +0200455 "<when condition=\"condition...\">"
456 "<reference><text>when_ref</text></reference>"
457 "<description><text>when_desc</text></description>"
458 "</when>"
David Sedláke1a30302019-07-10 13:49:38 +0200459 "<config value=\"true\"/>"
David Sedlákc1771b12019-07-10 15:55:46 +0200460 "<error-message>"
461 "<value>error-msg</value>"
462 "</error-message>"
David Sedlák2ce1be62019-07-10 16:15:09 +0200463 "<error-app-tag value=\"err-app-tag\"/>"
David Sedláka5b1d382019-07-10 16:31:09 +0200464 "<units name=\"radians\"></units>"
David Sedláke7084ce2019-07-10 16:44:15 +0200465 "<default value=\"default-value\"/>"
David Sedlák5545f5d2019-07-11 11:55:16 +0200466 "<position value=\"25\"></position>"
467 "<value value=\"-5\"/>"
David Sedlákcf5569a2019-07-11 13:31:34 +0200468 "<require-instance value=\"true\"></require-instance>"
David Sedlákb7296dd2019-07-11 14:58:38 +0200469 "<range value=\"5..10\" />"
David Sedlák438ae432019-07-11 15:36:54 +0200470 "<length value=\"baf\"/>"
David Sedlákd3983112019-07-12 11:20:56 +0200471 "<pattern value='pattern'>"
472 "<modifier value='invert-match'/>"
473 "</pattern>"
David Sedlákfd5b9c32019-07-12 15:33:13 +0200474 "<enum name=\"yay\">"
475 "</enum>"
David Sedlák555c7202019-07-04 12:14:12 +0200476 "</prefix>";
477 struct lysp_ext_instance *exts = NULL;
David Sedlák5f8191e2019-07-08 16:35:52 +0200478 const char **if_features = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200479 struct yin_arg_record *attrs = NULL;
David Sedláke7084ce2019-07-10 16:44:15 +0200480 const char *value, *err_msg, *app_tag, *units, *def;
David Sedlák986cb412019-07-04 13:10:11 +0200481 struct lysp_ext *ext_def = NULL;
David Sedlák32eee7b2019-07-09 12:38:44 +0200482 struct lysp_when *when_p = NULL;
David Sedlákcf5569a2019-07-11 13:31:34 +0200483 struct lysp_type_enum pos_enum = {}, val_enum = {};
David Sedlákfd5b9c32019-07-12 15:33:13 +0200484 struct lysp_type req_type = {}, range_type = {}, len_type = {}, patter_type = {}, enum_type = {};
David Sedláke1a30302019-07-10 13:49:38 +0200485 uint8_t config = 0;
David Sedlák555c7202019-07-04 12:14:12 +0200486
David Sedlákda8ffa32019-07-08 14:17:10 +0200487 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
488 yin_load_attributes(st->yin_ctx, &data, &attrs);
David Sedlák555c7202019-07-04 12:14:12 +0200489
David Sedlákfd5b9c32019-07-12 15:33:13 +0200490 struct yin_subelement subelems[17] = {
David Sedlák438ae432019-07-11 15:36:54 +0200491 {YANG_CONFIG, &config, 0},
David Sedlákc3da3ef2019-07-19 12:56:08 +0200492 {YANG_DEFAULT, &def, YIN_SUBELEM_UNIQUE},
David Sedlákfd5b9c32019-07-12 15:33:13 +0200493 {YANG_ENUM, &enum_type, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200494 {YANG_ERROR_APP_TAG, &app_tag, 0},
495 {YANG_ERROR_MESSAGE, &err_msg, 0},
496 {YANG_EXTENSION, &ext_def, 0},
497 {YANG_IF_FEATURE, &if_features, 0},
498 {YANG_LENGTH, &len_type, 0},
David Sedlákd3983112019-07-12 11:20:56 +0200499 {YANG_PATTERN, &patter_type, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200500 {YANG_RANGE, &range_type, 0},
501 {YANG_REQUIRE_INSTANCE, &req_type, 0},
502 {YANG_UNITS, &units, 0},
David Sedlák374d2b32019-07-17 15:06:55 +0200503 {YANG_POSITION, &pos_enum, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200504 {YANG_VALUE, &val_enum, 0},
505 {YANG_WHEN, &when_p, 0},
506 {YANG_CUSTOM, NULL, 0},
507 {YIN_TEXT, &value, 0}
David Sedlákd3983112019-07-12 11:20:56 +0200508 };
David Sedlákfd5b9c32019-07-12 15:33:13 +0200509 ret = yin_parse_content(st->yin_ctx, subelems, 17, &data, YANG_PREFIX, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200510 assert_int_equal(ret, LY_SUCCESS);
David Sedlák1fdb2522019-07-09 16:22:57 +0200511 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlák2ce1be62019-07-10 16:15:09 +0200512 /* check parsed values */
David Sedláke7084ce2019-07-10 16:44:15 +0200513 assert_string_equal(def, "default-value");
David Sedlák555c7202019-07-04 12:14:12 +0200514 assert_string_equal(exts->name, "custom");
515 assert_string_equal(exts->argument, "totally amazing extension");
516 assert_string_equal(value, "wsefsdf");
David Sedláka5b1d382019-07-10 16:31:09 +0200517 assert_string_equal(units, "radians");
David Sedlák32eee7b2019-07-09 12:38:44 +0200518 assert_string_equal(when_p->cond, "condition...");
519 assert_string_equal(when_p->dsc, "when_desc");
520 assert_string_equal(when_p->ref, "when_ref");
David Sedláke1a30302019-07-10 13:49:38 +0200521 assert_int_equal(config, LYS_CONFIG_W);
David Sedlák5545f5d2019-07-11 11:55:16 +0200522 assert_int_equal(pos_enum.value, 25);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200523 assert_true(pos_enum.flags & LYS_SET_VALUE);
David Sedlák5545f5d2019-07-11 11:55:16 +0200524 assert_int_equal(val_enum.value, -5);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200525 assert_true(val_enum.flags & LYS_SET_VALUE);
David Sedlákcf5569a2019-07-11 13:31:34 +0200526 assert_int_equal(req_type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200527 assert_true(req_type.flags &= LYS_SET_REQINST);
David Sedlákb7296dd2019-07-11 14:58:38 +0200528 assert_string_equal(range_type.range->arg, "5..10");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200529 assert_true(range_type.flags & LYS_SET_RANGE);
David Sedlákc1771b12019-07-10 15:55:46 +0200530 assert_string_equal(err_msg, "error-msg");
David Sedlák2ce1be62019-07-10 16:15:09 +0200531 assert_string_equal(app_tag, "err-app-tag");
David Sedlákfd5b9c32019-07-12 15:33:13 +0200532 assert_string_equal(enum_type.enums->name, "yay");
David Sedlák438ae432019-07-11 15:36:54 +0200533 assert_string_equal(len_type.length->arg, "baf");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200534 assert_true(len_type.flags & LYS_SET_LENGTH);
David Sedlákd3983112019-07-12 11:20:56 +0200535 assert_string_equal(patter_type.patterns->arg, "\x015pattern");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200536 assert_true(patter_type.flags & LYS_SET_PATTERN);
David Sedlák2ce1be62019-07-10 16:15:09 +0200537 /* cleanup */
David Sedlák555c7202019-07-04 12:14:12 +0200538 lysp_ext_instance_free(st->ctx, exts);
David Sedlák32eee7b2019-07-09 12:38:44 +0200539 lysp_when_free(st->ctx, when_p);
David Sedlák986cb412019-07-04 13:10:11 +0200540 lysp_ext_free(st->ctx, ext_def);
David Sedlák5f8191e2019-07-08 16:35:52 +0200541 FREE_STRING(st->ctx, *if_features);
David Sedlákc1771b12019-07-10 15:55:46 +0200542 FREE_STRING(st->ctx, err_msg);
David Sedlák2ce1be62019-07-10 16:15:09 +0200543 FREE_STRING(st->ctx, app_tag);
David Sedláka5b1d382019-07-10 16:31:09 +0200544 FREE_STRING(st->ctx, units);
David Sedlákd3983112019-07-12 11:20:56 +0200545 FREE_STRING(st->ctx, patter_type.patterns->arg);
David Sedláke7084ce2019-07-10 16:44:15 +0200546 FREE_STRING(st->ctx, def);
David Sedlákb7296dd2019-07-11 14:58:38 +0200547 FREE_STRING(st->ctx, range_type.range->arg);
David Sedlák438ae432019-07-11 15:36:54 +0200548 FREE_STRING(st->ctx, len_type.length->arg);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200549 FREE_STRING(st->ctx, enum_type.enums->name);
David Sedlákb7296dd2019-07-11 14:58:38 +0200550 FREE_STRING(st->ctx, value);
David Sedlák5f8191e2019-07-08 16:35:52 +0200551 LY_ARRAY_FREE(if_features);
David Sedlák555c7202019-07-04 12:14:12 +0200552 LY_ARRAY_FREE(exts);
David Sedlák986cb412019-07-04 13:10:11 +0200553 LY_ARRAY_FREE(ext_def);
David Sedlák555c7202019-07-04 12:14:12 +0200554 LY_ARRAY_FREE(attrs);
David Sedlákd3983112019-07-12 11:20:56 +0200555 LY_ARRAY_FREE(patter_type.patterns);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200556 LY_ARRAY_FREE(enum_type.enums);
David Sedlák32eee7b2019-07-09 12:38:44 +0200557 free(when_p);
David Sedlákb7296dd2019-07-11 14:58:38 +0200558 free(range_type.range);
David Sedlák438ae432019-07-11 15:36:54 +0200559 free(len_type.length);
David Sedlák555c7202019-07-04 12:14:12 +0200560 attrs = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200561 st = reset_state(state);
562
563 /* test unique subelem */
564 const char *prefix_value;
565 struct yin_subelement subelems2[2] = {{YANG_PREFIX, &prefix_value, 0},
566 {YIN_TEXT, &value, YIN_SUBELEM_UNIQUE}};
567 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
568 "<prefix value=\"inv_mod\" />"
569 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
570 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
571 "</module>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200572 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
573 yin_load_attributes(st->yin_ctx, &data, &attrs);
574 ret = yin_parse_content(st->yin_ctx, subelems2, 2, &data, YANG_MODULE, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200575 assert_int_equal(ret, LY_EVALID);
576 logbuf_assert("Redefinition of text element in module element. Line number 1.");
577 lydict_remove(st->ctx, prefix_value);
578 lydict_remove(st->ctx, value);
579 st = reset_state(state);
580 LY_ARRAY_FREE(attrs);
581 attrs = NULL;
582
583 /* test first subelem */
584 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
585 "<prefix value=\"inv_mod\" />"
586 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
587 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
588 "</module>";
589 struct yin_subelement subelems3[2] = {{YANG_PREFIX, &prefix_value, 0},
590 {YIN_TEXT, &value, YIN_SUBELEM_FIRST}};
David Sedlákda8ffa32019-07-08 14:17:10 +0200591 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
592 yin_load_attributes(st->yin_ctx, &data, &attrs);
593 ret = yin_parse_content(st->yin_ctx, subelems3, 2, &data, YANG_MODULE, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200594 assert_int_equal(ret, LY_EVALID);
595 logbuf_assert("Subelement text of module element must be defined as first subelement. Line number 1.");
596 lydict_remove(st->ctx, prefix_value);
597 st = reset_state(state);
598 LY_ARRAY_FREE(attrs);
599 attrs = NULL;
600
601 /* test mandatory subelem */
602 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
603 "</module>";
604 struct yin_subelement subelems4[1] = {{YANG_PREFIX, &prefix_value, YIN_SUBELEM_MANDATORY}};
David Sedlákda8ffa32019-07-08 14:17:10 +0200605 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
606 yin_load_attributes(st->yin_ctx, &data, &attrs);
607 ret = yin_parse_content(st->yin_ctx, subelems4, 1, &data, YANG_MODULE, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200608 assert_int_equal(ret, LY_EVALID);
609 logbuf_assert("Missing mandatory subelement prefix of module element. Line number 1.");
610 LY_ARRAY_FREE(attrs);
611
612 st->finished_correctly = true;
613}
614
David Sedlák92147b02019-07-09 14:01:01 +0200615static void
David Sedlák4a650532019-07-10 11:55:18 +0200616test_validate_value(void **state)
617{
618 struct state *st = *state;
619 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "#invalid", 8), LY_EVALID);
620 logbuf_assert("Invalid identifier character '#'. Line number 1.");
621 assert_int_equal(yin_validate_value(st->yin_ctx, Y_STR_ARG, "", 0), LY_SUCCESS);
622 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "pre:b", 5), LY_EVALID);
623 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:b", 5), LY_SUCCESS);
624 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:pre:b", 9), LY_EVALID);
625
626 st->finished_correctly = true;
627}
628
David Sedlák32488102019-07-15 17:44:10 +0200629static int
630setup_element_test(void **state)
631{
David Sedlák8e7bda82019-07-16 17:57:50 +0200632 struct state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +0200633
634#if ENABLE_LOGGER_CHECKING
635 /* setup logger */
636 ly_set_log_clb(logger, 1);
637#endif
638
639 /* reset logbuf */
640 logbuf[0] = '\0';
David Sedlák32488102019-07-15 17:44:10 +0200641
642 /* allocate parser context */
643 st->yin_ctx = calloc(1, sizeof(*st->yin_ctx));
644 st->yin_ctx->xml_ctx.ctx = st->ctx;
645 st->yin_ctx->xml_ctx.line = 1;
646
647 return EXIT_SUCCESS;
648}
649
650static int
651teardown_element_test(void **state)
652{
653 struct state *st = *(struct state **)state;
654
655#if ENABLE_LOGGER_CHECKING
656 /* teardown logger */
657 if (!st->finished_correctly && logbuf[0] != '\0') {
658 fprintf(stderr, "%s\n", logbuf);
659 }
660#endif
661
662 lyxml_context_clear(&st->yin_ctx->xml_ctx);
David Sedlák32488102019-07-15 17:44:10 +0200663 free(st->yin_ctx);
David Sedlák32488102019-07-15 17:44:10 +0200664
665 return EXIT_SUCCESS;
666}
667
668#define ELEMENT_WRAPPER_START "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
669#define ELEMENT_WRAPPER_END "</module>"
670
671/* helper function to simplify unit test of each element using parse_content function */
672LY_ERR
673test_element_helper(struct state *st, const char **data, void *dest, const char **text,
674 struct lysp_ext_instance **exts, bool valid)
675{
676 struct yin_arg_record *attrs = NULL;
677 struct sized_string name, prefix;
678 LY_ERR ret = LY_SUCCESS;
679 struct yin_subelement subelems[71] = {
680 {YANG_ACTION, dest, 0},
681 {YANG_ANYDATA, dest, 0},
682 {YANG_ANYXML, dest, 0},
683 {YANG_ARGUMENT,dest, 0},
684 {YANG_AUGMENT, dest, 0},
685 {YANG_BASE, dest, 0},
686 {YANG_BELONGS_TO, dest, 0},
687 {YANG_BIT, dest, 0},
688 {YANG_CASE, dest, 0},
689 {YANG_CHOICE, dest, 0},
690 {YANG_CONFIG, dest, 0},
691 {YANG_CONTACT, dest, 0},
692 {YANG_CONTAINER, dest, 0},
David Sedlákc3da3ef2019-07-19 12:56:08 +0200693 {YANG_DEFAULT, dest, YIN_SUBELEM_UNIQUE},
David Sedlák32488102019-07-15 17:44:10 +0200694 {YANG_DESCRIPTION, dest, 0},
695 {YANG_DEVIATE, dest, 0},
696 {YANG_DEVIATION, dest, 0},
697 {YANG_ENUM, dest, 0},
698 {YANG_ERROR_APP_TAG, dest, 0},
699 {YANG_ERROR_MESSAGE, dest, 0},
700 {YANG_EXTENSION, dest, 0},
701 {YANG_FEATURE, dest, 0},
702 {YANG_FRACTION_DIGITS, dest, 0},
703 {YANG_GROUPING, dest, 0},
704 {YANG_IDENTITY, dest, 0},
705 {YANG_IF_FEATURE, dest, 0},
706 {YANG_IMPORT, dest, 0},
707 {YANG_INCLUDE, dest, 0},
708 {YANG_INPUT, dest, 0},
709 {YANG_KEY, dest, 0},
710 {YANG_LEAF, dest, 0},
711 {YANG_LEAF_LIST, dest, 0},
712 {YANG_LENGTH, dest, 0},
713 {YANG_LIST, dest, 0},
714 {YANG_MANDATORY, dest, 0},
715 {YANG_MAX_ELEMENTS, dest, 0},
716 {YANG_MIN_ELEMENTS, dest, 0},
717 {YANG_MODIFIER, dest, 0},
718 {YANG_MODULE, dest, 0},
719 {YANG_MUST, dest, 0},
720 {YANG_NAMESPACE, dest, 0},
721 {YANG_NOTIFICATION, dest, 0},
722 {YANG_ORDERED_BY, dest, 0},
723 {YANG_ORGANIZATION, dest, 0},
724 {YANG_OUTPUT, dest, 0},
725 {YANG_PATH, dest, 0},
726 {YANG_PATTERN, dest, 0},
David Sedlák32488102019-07-15 17:44:10 +0200727 {YANG_PREFIX, dest, 0},
728 {YANG_PRESENCE, dest, 0},
729 {YANG_RANGE, dest, 0},
730 {YANG_REFERENCE, dest, 0},
731 {YANG_REFINE, dest, 0},
732 {YANG_REQUIRE_INSTANCE, dest, 0},
733 {YANG_REVISION, dest, 0},
734 {YANG_REVISION_DATE, dest, 0},
735 {YANG_RPC, dest, 0},
736 {YANG_STATUS, dest, 0},
737 {YANG_SUBMODULE, dest, 0},
738 {YANG_TYPE, dest, 0},
739 {YANG_TYPEDEF, dest, 0},
740 {YANG_UNIQUE, dest, 0},
741 {YANG_UNITS, dest, 0},
742 {YANG_USES, dest, 0},
David Sedlák374d2b32019-07-17 15:06:55 +0200743 {YANG_POSITION, dest, 0},
David Sedlák32488102019-07-15 17:44:10 +0200744 {YANG_VALUE, dest, 0},
745 {YANG_WHEN, dest, 0},
746 {YANG_YANG_VERSION, dest, 0},
747 {YANG_YIN_ELEMENT, dest, 0},
748 {YANG_CUSTOM, dest, 0},
749 {YIN_TEXT, dest, 0},
750 {YIN_VALUE, dest, 0}
751 };
752 LY_CHECK_RET(lyxml_get_element(&st->yin_ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len));\
753 LY_CHECK_RET(yin_load_attributes(st->yin_ctx, data, &attrs));\
David Sedlák8e7bda82019-07-16 17:57:50 +0200754 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 +0200755 LY_ARRAY_FREE(attrs);
756 if (valid) {
757 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
758 }
759 /* reset status */
760 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
761 return ret;
762}
763
764static void
765test_enum_bit_elem(void **state)
766{
767 /* yin_parse_enum_bit is function that is being mainly tested by this test */
768 struct state *st = *state;
769 struct lysp_type type = {};
770 const char *data;
771 data = ELEMENT_WRAPPER_START
772 "<enum name=\"enum-name\">"
773 "<if-feature name=\"feature\" />"
774 "<value value=\"55\" />"
775 "<status value=\"deprecated\" />"
776 "<description><text>desc...</text></description>"
777 "<reference><text>ref...</text></reference>"
778 "</enum>"
779 ELEMENT_WRAPPER_END;
780 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
781 assert_string_equal(*type.enums->iffeatures, "feature");
782 assert_int_equal(type.enums->value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200783 assert_true((type.enums->flags & LYS_STATUS_DEPRC) && (type.enums->flags & LYS_SET_VALUE));
David Sedlák32488102019-07-15 17:44:10 +0200784 assert_string_equal(type.enums->dsc, "desc...");
785 assert_string_equal(type.enums->ref, "ref...");
786 lysp_type_free(st->ctx, &type);
787 memset(&type, 0, sizeof type);
788
789 /* todo bit element test */
790 st->finished_correctly = true;
791}
792
793static void
794test_meta_elem(void **state)
795{
796 struct state *st = *state;
797 char *value = NULL;
798 const char *data;
799
800 /* organization element */
801 data = ELEMENT_WRAPPER_START
802 "<organization><text>organization...</text></organization>"
803 ELEMENT_WRAPPER_END;
804 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
805 assert_string_equal(value, "organization...");
806 FREE_STRING(st->ctx, value);
807 value = NULL;
808 /* contact element */
809 data = ELEMENT_WRAPPER_START
810 "<contact><text>contact...</text></contact>"
811 ELEMENT_WRAPPER_END;
812 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
813 assert_string_equal(value, "contact...");
814 FREE_STRING(st->ctx, value);
815 value = NULL;
816 /* description element */
817 data = ELEMENT_WRAPPER_START
818 "<description><text>description...</text></description>"
819 ELEMENT_WRAPPER_END;
820 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
821 assert_string_equal(value, "description...");
822 FREE_STRING(st->ctx, value);
823 value = NULL;
824 /* reference element */
825 data = ELEMENT_WRAPPER_START
826 "<reference><text>reference...</text></reference>"
827 ELEMENT_WRAPPER_END;
828 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
829 assert_string_equal(value, "reference...");
830 FREE_STRING(st->ctx, value);
831 value = NULL;
832
833 /* missing text subelement */
834 data = ELEMENT_WRAPPER_START
835 "<reference>reference...</reference>"
836 ELEMENT_WRAPPER_END;
837 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, false), LY_EVALID);
838 logbuf_assert("Missing mandatory subelement text of reference element. Line number 1.");
839
840 st->finished_correctly = true;
841}
842
843static void
844test_import_elem(void **state)
845{
846 struct state *st = *state;
847 const char *data;
848 struct lys_module *lys_mod = calloc(1, sizeof *lys_mod);
849 struct lysp_module *lysp_mod = calloc(1, sizeof *lysp_mod);
850 lys_mod->ctx = st->ctx;
851 lysp_mod->mod = lys_mod;
852
853 /* max subelems */
854 data = ELEMENT_WRAPPER_START
855 "<import module=\"a\">"
856 "<prefix value=\"a_mod\"/>"
857 "<revision-date date=\"2015-01-01\"></revision-date>"
858 "<description><text>import description</text></description>"
859 "<reference><text>import reference</text></reference>"
860 "</import>"
861 ELEMENT_WRAPPER_END;
862 assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, true), LY_SUCCESS);
863 assert_string_equal(lysp_mod->imports->name, "a");
864 assert_string_equal(lysp_mod->imports->prefix, "a_mod");
865 assert_string_equal(lysp_mod->imports->rev, "2015-01-01");
866 assert_string_equal(lysp_mod->imports->dsc, "import description");
867 assert_string_equal(lysp_mod->imports->ref, "import reference");
868 lysp_module_free(lysp_mod);
869 lys_module_free(lys_mod, NULL);
870
871 /* min subelems */
872 lys_mod = calloc(1, sizeof *lys_mod);
873 lysp_mod = calloc(1, sizeof *lysp_mod);
874 lys_mod->ctx = st->ctx;
875 lysp_mod->mod = lys_mod;
876 data = ELEMENT_WRAPPER_START
877 "<import module=\"a\">"
878 "<prefix value=\"a_mod\"/>"
879 "</import>"
880 ELEMENT_WRAPPER_END;
881 assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, true), LY_SUCCESS);
882 assert_string_equal(lysp_mod->imports->prefix, "a_mod");
883 lysp_module_free(lysp_mod);
884 lys_module_free(lys_mod, NULL);
885
886 /* invalid (missing prefix) */
887 lys_mod = calloc(1, sizeof *lys_mod);
888 lysp_mod = calloc(1, sizeof *lysp_mod);
889 lys_mod->ctx = st->ctx;
890 lysp_mod->mod = lys_mod;
891 data = ELEMENT_WRAPPER_START "<import module=\"a\">""</import>" ELEMENT_WRAPPER_END;
892 assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, false), LY_EVALID);
893 logbuf_assert("Missing mandatory subelement prefix of import element. Line number 1.");
894 lysp_module_free(lysp_mod);
895 lys_module_free(lys_mod, NULL);
896
897 /* invalid reused prefix */
898 lys_mod = calloc(1, sizeof *lys_mod);
899 lysp_mod = calloc(1, sizeof *lysp_mod);
900 lys_mod->ctx = st->ctx;
901 lysp_mod->mod = lys_mod;
902 data = ELEMENT_WRAPPER_START
903 "<import module=\"a\">"
904 "<prefix value=\"a_mod\"/>"
905 "</import>"
906 "<import module=\"a\">"
907 "<prefix value=\"a_mod\"/>"
908 "</import>"
909 ELEMENT_WRAPPER_END;
910 assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, false), LY_EVALID);
911 logbuf_assert("Prefix \"a_mod\" already used to import \"a\" module. Line number 1.");
912 lysp_module_free(lysp_mod);
913 lys_module_free(lys_mod, NULL);
914
915 st->finished_correctly = true;
916}
917
918static void
919test_status_elem(void **state)
920{
921 struct state *st = *state;
922 const char *data;
923 uint16_t flags = 0;
924
925 /* test valid values */
926 data = ELEMENT_WRAPPER_START "<status value=\"current\" />" ELEMENT_WRAPPER_END;
927 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200928 assert_true(flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +0200929
930 data = ELEMENT_WRAPPER_START "<status value=\"deprecated\" />" ELEMENT_WRAPPER_END;
931 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200932 assert_true(flags & LYS_STATUS_DEPRC);
David Sedlák32488102019-07-15 17:44:10 +0200933
934 data = ELEMENT_WRAPPER_START "<status value=\"obsolete\"></status>" ELEMENT_WRAPPER_END;
935 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200936 assert_true(flags & LYS_STATUS_OBSLT);
David Sedlák32488102019-07-15 17:44:10 +0200937
938 /* test invalid value */
939 data = ELEMENT_WRAPPER_START "<status value=\"invalid\"></status>" ELEMENT_WRAPPER_END;
940 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
941 logbuf_assert("Invalid value \"invalid\" of \"status\". Line number 1.");
942 st->finished_correctly = true;
943}
944
945static void
946test_ext_elem(void **state)
947{
948 struct state *st = *state;
949 const char *data;
950 struct lysp_ext *ext = NULL;
951
952 /* max subelems */
953 data = ELEMENT_WRAPPER_START
954 "<extension name=\"ext_name\">"
955 "<argument name=\"arg\"></argument>"
956 "<status value=\"current\"/>"
957 "<description><text>ext_desc</text></description>"
958 "<reference><text>ext_ref</text></reference>"
959 "</extension>"
960 ELEMENT_WRAPPER_END;
961 assert_int_equal(test_element_helper(st, &data, &ext, NULL, NULL, true), LY_SUCCESS);
962 assert_string_equal(ext->name, "ext_name");
963 assert_string_equal(ext->argument, "arg");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200964 assert_true(ext->flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +0200965 assert_string_equal(ext->dsc, "ext_desc");
966 assert_string_equal(ext->ref, "ext_ref");
967 lysp_ext_free(st->ctx, ext);
968 LY_ARRAY_FREE(ext);
969 ext = NULL;
970
971 /* min subelems */
972 data = ELEMENT_WRAPPER_START "<extension name=\"ext_name\"></extension>" ELEMENT_WRAPPER_END;
973 assert_int_equal(test_element_helper(st, &data, &ext, NULL, NULL, true), LY_SUCCESS);
974 assert_string_equal(ext->name, "ext_name");
975 lysp_ext_free(st->ctx, ext);
976 LY_ARRAY_FREE(ext);
977 ext = NULL;
978
979 st->finished_correctly = true;
980}
981
982static void
983test_yin_element_elem(void **state)
984{
985 struct state *st = *state;
986 const char *data;
987 uint16_t flags = 0;
988
989 data = ELEMENT_WRAPPER_START "<yin-element value=\"true\" />" ELEMENT_WRAPPER_END;
990 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200991 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +0200992
993 data = ELEMENT_WRAPPER_START "<yin-element value=\"false\" />" ELEMENT_WRAPPER_END;
994 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200995 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +0200996
997 data = ELEMENT_WRAPPER_START "<yin-element value=\"invalid\" />" ELEMENT_WRAPPER_END;
998 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200999 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +02001000 logbuf_assert("Invalid value \"invalid\" of \"yin-element\". Line number 1.");
1001 st->finished_correctly = true;
1002}
1003
1004static void
1005test_yangversion_elem(void **state)
1006{
1007 struct state *st = *state;
1008 const char *data;
1009 uint8_t version = 0;
1010
1011 /* valid values */
1012 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.0\" />" ELEMENT_WRAPPER_END;
1013 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001014 assert_true(version & LYS_VERSION_1_0);
David Sedlák32488102019-07-15 17:44:10 +02001015 assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_0);
1016
1017 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.1\" />" ELEMENT_WRAPPER_END;
1018 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001019 assert_true(version & LYS_VERSION_1_1);
David Sedlák32488102019-07-15 17:44:10 +02001020 assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_1);
1021
1022 /* invalid value */
1023 data = ELEMENT_WRAPPER_START "<yang-version value=\"version\" />" ELEMENT_WRAPPER_END;
1024 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, false), LY_EVALID);
1025 logbuf_assert("Invalid value \"version\" of \"yang-version\". Line number 1.");
1026
1027 st->finished_correctly = true;
1028}
1029
1030static void
1031test_mandatory_elem(void **state)
1032{
1033 struct state *st = *state;
1034 const char *data;
1035 uint16_t man = 0;
1036
1037 /* valid values */
1038 data = ELEMENT_WRAPPER_START "<mandatory value=\"true\" />" ELEMENT_WRAPPER_END;
1039 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, true), LY_SUCCESS);
1040 assert_int_equal(man, LYS_MAND_TRUE);
1041 man = 0;
1042
1043 data = ELEMENT_WRAPPER_START "<mandatory value=\"false\" />" ELEMENT_WRAPPER_END;
1044 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, true), LY_SUCCESS);
1045 assert_int_equal(man, LYS_MAND_FALSE);
1046
1047 data = ELEMENT_WRAPPER_START "<mandatory value=\"invalid\" />" ELEMENT_WRAPPER_END;
1048 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, false), LY_EVALID);
1049 logbuf_assert("Invalid value \"invalid\" of \"mandatory\". Line number 1.");
1050
1051 st->finished_correctly = true;
1052}
1053
David Sedlák8e7bda82019-07-16 17:57:50 +02001054static void
1055test_argument_elem(void **state)
1056{
1057 struct state *st = *state;
1058 const char *data;
1059 uint16_t flags = 0;
1060 const char *arg;
1061 struct yin_argument_meta arg_meta = {&flags, &arg};
1062 /* max subelems */
1063 data = ELEMENT_WRAPPER_START
1064 "<argument name=\"arg-name\">"
1065 "<yin-element value=\"true\" />"
1066 "</argument>"
1067 ELEMENT_WRAPPER_END;
1068 assert_int_equal(test_element_helper(st, &data, &arg_meta, NULL, NULL, true), LY_SUCCESS);
1069 assert_string_equal(arg, "arg-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001070 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001071 flags = 0;
1072 FREE_STRING(st->ctx, arg);
1073 arg = NULL;
1074
1075 /* min subelems */
1076 data = ELEMENT_WRAPPER_START
1077 "<argument name=\"arg\">"
1078 "</argument>"
1079 ELEMENT_WRAPPER_END;
1080 assert_int_equal(test_element_helper(st, &data, &arg_meta, NULL, NULL, true), LY_SUCCESS);
1081 assert_string_equal(arg, "arg");
1082 assert_true(flags == 0);
1083 FREE_STRING(st->ctx, arg);
1084
1085 st->finished_correctly = true;
1086}
1087
1088static void
1089test_base_elem(void **state)
1090{
1091 struct state *st = *state;
1092 const char *data;
1093 const char **bases = NULL;
1094 struct lysp_type type = {};
1095
1096 /* as identity subelement */
1097 data = "<identity xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
1098 "<base name=\"base-name\"/>"
1099 "</identity>";
1100 assert_int_equal(test_element_helper(st, &data, &bases, NULL, NULL, true), LY_SUCCESS);
1101 assert_string_equal(*bases, "base-name");
1102 FREE_STRING(st->ctx, *bases);
1103 LY_ARRAY_FREE(bases);
1104
1105 /* as type subelement */
1106 data = "<type xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
1107 "<base name=\"base-name\"/>"
1108 "</type>";
1109 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1110 assert_string_equal(*type.bases, "base-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001111 assert_true(type.flags & LYS_SET_BASE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001112 FREE_STRING(st->ctx, *type.bases);
1113 LY_ARRAY_FREE(type.bases);
1114
1115 st->finished_correctly = true;
1116}
1117
1118static void
1119test_belongsto_elem(void **state)
1120{
1121 struct state *st = *state;
1122 const char *data;
1123 struct lysp_submodule submod;
1124
1125 data = ELEMENT_WRAPPER_START
1126 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>"
1127 ELEMENT_WRAPPER_END;
1128 assert_int_equal(test_element_helper(st, &data, &submod, NULL, NULL, true), LY_SUCCESS);
1129 assert_string_equal(submod.belongsto, "module-name");
1130 assert_string_equal(submod.prefix, "pref");
1131 FREE_STRING(st->ctx, submod.belongsto);
1132 FREE_STRING(st->ctx, submod.prefix);
1133
1134 data = ELEMENT_WRAPPER_START "<belongs-to module=\"module-name\"></belongs-to>" ELEMENT_WRAPPER_END;
1135 assert_int_equal(test_element_helper(st, &data, &submod, NULL, NULL, false), LY_EVALID);
1136 logbuf_assert("Missing mandatory subelement prefix of belongs-to element. Line number 1.");
1137 FREE_STRING(st->ctx, submod.belongsto);
1138
1139 st->finished_correctly = true;
1140}
1141
1142static void
1143test_config_elem(void **state)
1144{
1145 struct state *st = *state;
1146 const char *data;
1147 uint16_t flags = 0;
1148
1149 data = ELEMENT_WRAPPER_START "<config value=\"true\"/>" ELEMENT_WRAPPER_END;
1150 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001151 assert_true(flags & LYS_CONFIG_W);
David Sedlák8e7bda82019-07-16 17:57:50 +02001152 flags = 0;
1153
1154 data = ELEMENT_WRAPPER_START "<config value=\"false\"/>" ELEMENT_WRAPPER_END;
1155 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001156 assert_true(flags & LYS_CONFIG_R);
David Sedlák8e7bda82019-07-16 17:57:50 +02001157 flags = 0;
1158
1159 data = ELEMENT_WRAPPER_START "<config value=\"invalid\"/>" ELEMENT_WRAPPER_END;
1160 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
1161 logbuf_assert("Invalid value \"invalid\" of \"config\". Line number 1.");
1162
1163 st->finished_correctly = true;
1164}
1165
1166static void
1167test_default_elem(void **state)
1168{
1169 struct state *st = *state;
1170 const char *data;
1171 const char *val = NULL;
1172
1173 data = ELEMENT_WRAPPER_START "<default value=\"defaul-value\"/>" ELEMENT_WRAPPER_END;
1174 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1175 assert_string_equal(val, "defaul-value");
1176 FREE_STRING(st->ctx, val);
1177 val = NULL;
1178
1179 data = ELEMENT_WRAPPER_START "<default/>" ELEMENT_WRAPPER_END;
1180 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1181 logbuf_assert("Missing mandatory attribute value of default element. Line number 1.");
1182
1183 st->finished_correctly = true;
1184}
1185
1186static void
1187test_err_app_tag_elem(void **state)
1188{
1189 struct state *st = *state;
1190 const char *data;
1191 const char *val = NULL;
1192
1193 data = ELEMENT_WRAPPER_START "<error-app-tag value=\"val\"/>" ELEMENT_WRAPPER_END;
1194 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1195 assert_string_equal(val, "val");
1196 FREE_STRING(st->ctx, val);
1197 val = NULL;
1198
1199 data = ELEMENT_WRAPPER_START "<error-app-tag/>" ELEMENT_WRAPPER_END;
1200 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1201 logbuf_assert("Missing mandatory attribute value of error-app-tag element. Line number 1.");
1202
1203 st->finished_correctly = true;
1204}
1205
1206static void
1207test_err_msg_elem(void **state)
1208{
1209 struct state *st = *state;
1210 const char *data;
1211 const char *val = NULL;
1212
1213 data = ELEMENT_WRAPPER_START "<error-message><value>val</value></error-message>" ELEMENT_WRAPPER_END;
1214 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1215 assert_string_equal(val, "val");
1216 FREE_STRING(st->ctx, val);
1217
1218 data = ELEMENT_WRAPPER_START "<error-message></error-message>" ELEMENT_WRAPPER_END;
1219 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1220 logbuf_assert("Missing mandatory subelement value of error-message element. Line number 1.");
1221
1222 st->finished_correctly = true;
1223}
1224
1225static void
1226test_fracdigits_elem(void **state)
1227{
1228 struct state *st = *state;
1229 const char *data;
1230 struct lysp_type type = {};
1231
1232 /* valid value */
1233 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"10\"></fraction-digits>" ELEMENT_WRAPPER_END;
1234 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1235 assert_int_equal(type.fraction_digits, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001236 assert_true(type.flags & LYS_SET_FRDIGITS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001237
1238 /* invalid values */
1239 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"-1\"></fraction-digits>" ELEMENT_WRAPPER_END;
1240 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1241 logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1.");
1242
1243 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"02\"></fraction-digits>" ELEMENT_WRAPPER_END;
1244 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1245 logbuf_assert("Invalid value \"02\" of \"fraction-digits\". Line number 1.");
1246
1247 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"1p\"></fraction-digits>" ELEMENT_WRAPPER_END;
1248 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1249 logbuf_assert("Invalid value \"1p\" of \"fraction-digits\". Line number 1.");
1250
1251 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"19\"></fraction-digits>" ELEMENT_WRAPPER_END;
1252 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1253 logbuf_assert("Invalid value \"19\" of \"fraction-digits\". Line number 1.");
1254
1255 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"999999999999999999\"></fraction-digits>" ELEMENT_WRAPPER_END;
1256 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1257 logbuf_assert("Invalid value \"999999999999999999\" of \"fraction-digits\". Line number 1.");
1258
1259 st->finished_correctly = true;
1260}
1261
1262static void
1263test_iffeature_elem(void **state)
1264{
1265 struct state *st = *state;
1266 const char *data;
1267 const char **iffeatures = NULL;
1268
1269 data = ELEMENT_WRAPPER_START "<if-feature name=\"local-storage\"></if-feature>" ELEMENT_WRAPPER_END;
1270 assert_int_equal(test_element_helper(st, &data, &iffeatures, NULL, NULL, true), LY_SUCCESS);
1271 assert_string_equal(*iffeatures, "local-storage");
1272 FREE_STRING(st->ctx, *iffeatures);
1273 LY_ARRAY_FREE(iffeatures);
1274 iffeatures = NULL;
1275
1276 data = ELEMENT_WRAPPER_START "<if-feature/>" ELEMENT_WRAPPER_END;
1277 assert_int_equal(test_element_helper(st, &data, &iffeatures, NULL, NULL, false), LY_EVALID);
1278 logbuf_assert("Missing mandatory attribute name of if-feature element. Line number 1.");
1279 LY_ARRAY_FREE(iffeatures);
1280 iffeatures = NULL;
1281
1282 st->finished_correctly = true;
1283}
1284
1285static void
1286test_length_elem(void **state)
1287{
1288 struct state *st = *state;
1289 const char *data;
1290 struct lysp_type type = {};
1291
1292 /* max subelems */
1293 data = ELEMENT_WRAPPER_START
1294 "<length value=\"length-str\">"
1295 "<error-message><value>err-msg</value></error-message>"
1296 "<error-app-tag value=\"err-app-tag\"/>"
1297 "<description><text>desc</text></description>"
1298 "<reference><text>ref</text></reference>"
1299 "</length>"
1300 ELEMENT_WRAPPER_END;
1301 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1302 assert_string_equal(type.length->arg, "length-str");
1303 assert_string_equal(type.length->emsg, "err-msg");
1304 assert_string_equal(type.length->eapptag, "err-app-tag");
1305 assert_string_equal(type.length->dsc, "desc");
1306 assert_string_equal(type.length->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001307 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001308 lysp_type_free(st->ctx, &type);
1309 memset(&type, 0, sizeof(type));
1310
1311 /* min subelems */
1312 data = ELEMENT_WRAPPER_START
1313 "<length value=\"length-str\">"
1314 "</length>"
1315 ELEMENT_WRAPPER_END;
1316 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1317 assert_string_equal(type.length->arg, "length-str");
1318 lysp_type_free(st->ctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001319 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001320 memset(&type, 0, sizeof(type));
1321
1322 data = ELEMENT_WRAPPER_START "<length></length>" ELEMENT_WRAPPER_END;
1323 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1324 logbuf_assert("Missing mandatory attribute value of length element. Line number 1.");
1325 lysp_type_free(st->ctx, &type);
1326 memset(&type, 0, sizeof(type));
1327
1328 st->finished_correctly = true;
1329}
1330
1331static void
1332test_modifier_elem(void **state)
1333{
1334 struct state *st = *state;
1335 const char *data;
1336 const char *pat = lydict_insert(st->ctx, "\006pattern", 8);
1337
1338 data = ELEMENT_WRAPPER_START "<modifier value=\"invert-match\" />" ELEMENT_WRAPPER_END;
1339 assert_int_equal(test_element_helper(st, &data, &pat, NULL, NULL, true), LY_SUCCESS);
1340 assert_string_equal(pat, "\x015pattern");
1341 FREE_STRING(st->ctx, pat);
1342
1343 pat = lydict_insert(st->ctx, "\006pattern", 8);
1344 data = ELEMENT_WRAPPER_START "<modifier value=\"invert\" />" ELEMENT_WRAPPER_END;
1345 assert_int_equal(test_element_helper(st, &data, &pat, NULL, NULL, false), LY_EVALID);
1346 logbuf_assert("Invalid value \"invert\" of \"modifier\". Line number 1.");
1347 FREE_STRING(st->ctx, pat);
1348
1349 st->finished_correctly = true;
1350}
1351
1352static void
1353test_namespace_elem(void **state)
1354{
1355 struct state *st = *state;
1356 const char *data;
1357 const char *ns;
1358
1359 data = ELEMENT_WRAPPER_START "<namespace uri=\"ns\"/>" ELEMENT_WRAPPER_END;
1360 assert_int_equal(test_element_helper(st, &data, &ns, NULL, NULL, true), LY_SUCCESS);
1361 assert_string_equal(ns, "ns");
1362 FREE_STRING(st->ctx, ns);
1363
1364 data = ELEMENT_WRAPPER_START "<namespace/>" ELEMENT_WRAPPER_END;
1365 assert_int_equal(test_element_helper(st, &data, &ns, NULL, NULL, false), LY_EVALID);
1366 logbuf_assert("Missing mandatory attribute uri of namespace element. Line number 1.");
1367
1368 st->finished_correctly = true;
1369}
1370
1371static void
1372test_path_elem(void **state)
1373{
1374 struct state *st = *state;
1375 const char *data;
1376 struct lysp_type type = {};
1377
1378 data = ELEMENT_WRAPPER_START "<path value=\"path-val\"/>" ELEMENT_WRAPPER_END;
1379 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1380 assert_string_equal("path-val", type.path);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001381 assert_true(type.flags & LYS_SET_PATH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001382 lysp_type_free(st->ctx, &type);
1383
1384 st->finished_correctly = true;
1385}
1386
1387static void
1388test_pattern_elem(void **state)
1389{
1390 struct state *st = *state;
1391 const char *data;
1392 struct lysp_type type = {};
1393
1394 /* max subelems */
1395 data = ELEMENT_WRAPPER_START
1396 "<pattern value=\"super_pattern\">"
1397 "<modifier value=\"invert-match\"/>"
1398 "<error-message><value>err-msg-value</value></error-message>"
1399 "<error-app-tag value=\"err-app-tag-value\"/>"
1400 "<description><text>pattern-desc</text></description>"
1401 "<reference><text>pattern-ref</text></reference>"
1402 "</pattern>"
1403 ELEMENT_WRAPPER_END;
1404 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001405 assert_true(type.flags & LYS_SET_PATTERN);
David Sedlák8e7bda82019-07-16 17:57:50 +02001406 assert_string_equal(type.patterns->arg, "\x015super_pattern");
1407 assert_string_equal(type.patterns->dsc, "pattern-desc");
1408 assert_string_equal(type.patterns->eapptag, "err-app-tag-value");
1409 assert_string_equal(type.patterns->emsg, "err-msg-value");
1410 assert_string_equal(type.patterns->dsc, "pattern-desc");
1411 assert_string_equal(type.patterns->ref, "pattern-ref");
1412 lysp_type_free(st->ctx, &type);
1413 memset(&type, 0, sizeof(type));
1414
1415 /* min subelems */
1416 data = ELEMENT_WRAPPER_START "<pattern value=\"pattern\"> </pattern>" ELEMENT_WRAPPER_END;
1417 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1418 assert_string_equal(type.patterns->arg, "\x006pattern");
1419 lysp_type_free(st->ctx, &type);
1420 memset(&type, 0, sizeof(type));
1421
1422 st->finished_correctly = true;
1423}
1424
1425static void
1426test_value_position_elem(void **state)
1427{
1428 struct state *st = *state;
1429 const char *data;
1430 struct lysp_type_enum en = {};
1431
1432 /* valid values */
1433 data = ELEMENT_WRAPPER_START "<value value=\"55\" />" ELEMENT_WRAPPER_END;
1434 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1435 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001436 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001437 memset(&en, 0, sizeof(en));
1438
1439 data = ELEMENT_WRAPPER_START "<value value=\"-55\"/>" ELEMENT_WRAPPER_END;
1440 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1441 assert_int_equal(en.value, -55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001442 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001443 memset(&en, 0, sizeof(en));
1444
1445 data = ELEMENT_WRAPPER_START "<value value=\"0\"/>" ELEMENT_WRAPPER_END;
1446 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1447 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001448 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001449 memset(&en, 0, sizeof(en));
1450
1451 data = ELEMENT_WRAPPER_START "<value value=\"-0\"/>" ELEMENT_WRAPPER_END;
1452 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1453 assert_int_equal(en.value, 0);
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 /* valid positions */
1458 data = ELEMENT_WRAPPER_START "<position value=\"55\" />" ELEMENT_WRAPPER_END;
1459 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1460 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001461 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001462 memset(&en, 0, sizeof(en));
1463
1464 data = ELEMENT_WRAPPER_START "<position value=\"0\" />" ELEMENT_WRAPPER_END;
1465 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1466 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001467 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001468 memset(&en, 0, sizeof(en));
1469
1470 /* invalid values */
1471 data = ELEMENT_WRAPPER_START "<value value=\"99999999999999999999999\"/>" ELEMENT_WRAPPER_END;
1472 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1473 logbuf_assert("Invalid value \"99999999999999999999999\" of \"value\". Line number 1.");
1474
1475 data = ELEMENT_WRAPPER_START "<value value=\"1k\"/>" ELEMENT_WRAPPER_END;
1476 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1477 logbuf_assert("Invalid value \"1k\" of \"value\". Line number 1.");
1478
David Sedlák69f01612019-07-17 11:41:08 +02001479 data = ELEMENT_WRAPPER_START "<value value=\"\"/>" ELEMENT_WRAPPER_END;
1480 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1481 logbuf_assert("Invalid value \"\" of \"value\". Line number 1.");
1482
David Sedlák8e7bda82019-07-16 17:57:50 +02001483 /*invalid positions */
1484 data = ELEMENT_WRAPPER_START "<position value=\"-5\"/>" ELEMENT_WRAPPER_END;
1485 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1486 logbuf_assert("Invalid value \"-5\" of \"position\". Line number 1.");
1487
1488 data = ELEMENT_WRAPPER_START "<position value=\"-0\"/>" ELEMENT_WRAPPER_END;
1489 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1490 logbuf_assert("Invalid value \"-0\" of \"position\". Line number 1.");
1491
1492 data = ELEMENT_WRAPPER_START "<position value=\"99999999999999999999\"/>" ELEMENT_WRAPPER_END;
1493 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1494 logbuf_assert("Invalid value \"99999999999999999999\" of \"position\". Line number 1.");
1495
David Sedlák69f01612019-07-17 11:41:08 +02001496 data = ELEMENT_WRAPPER_START "<position value=\"\"/>" ELEMENT_WRAPPER_END;
1497 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1498 logbuf_assert("Invalid value \"\" of \"position\". Line number 1.");
1499
1500 st->finished_correctly = true;
1501}
1502
1503static void
1504test_prefix_elem(void **state)
1505{
1506 struct state *st = *state;
1507 const char *data;
1508 const char *value = NULL;
1509
1510 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\"/>" ELEMENT_WRAPPER_END;
1511 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
1512 assert_string_equal(value, "pref");
1513 FREE_STRING(st->ctx, value);
1514
1515 st->finished_correctly = true;
1516}
1517
1518static void
1519test_range_elem(void **state)
1520{
1521 struct state *st = *state;
1522 const char *data;
1523 struct lysp_type type = {};
1524
1525 /* max subelems */
1526 data = ELEMENT_WRAPPER_START
1527 "<range value=\"range-str\">"
1528 "<error-message><value>err-msg</value></error-message>"
1529 "<error-app-tag value=\"err-app-tag\" />"
1530 "<description><text>desc</text></description>"
1531 "<reference><text>ref</text></reference>"
1532 "</range>"
1533 ELEMENT_WRAPPER_END;
1534 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1535 assert_string_equal(type.range->arg, "range-str");
1536 assert_string_equal(type.range->dsc, "desc");
1537 assert_string_equal(type.range->eapptag, "err-app-tag");
1538 assert_string_equal(type.range->emsg, "err-msg");
1539 assert_string_equal(type.range->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001540 assert_true(type.flags & LYS_SET_RANGE);
David Sedlák69f01612019-07-17 11:41:08 +02001541 lysp_type_free(st->ctx, &type);
1542 memset(&type, 0, sizeof(type));
1543
1544 /* min subelems */
1545 data = ELEMENT_WRAPPER_START "<range value=\"range-str\"/>" ELEMENT_WRAPPER_END;
1546 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1547 assert_string_equal(type.range->arg, "range-str");
1548 lysp_type_free(st->ctx, &type);
1549 memset(&type, 0, sizeof(type));
1550
1551 st->finished_correctly = true;
1552}
1553
1554static void
1555test_reqinstance_elem(void **state)
1556{
1557 struct state *st = *state;
1558 const char *data;
1559 struct lysp_type type = {};
1560
1561 data = ELEMENT_WRAPPER_START "<require-instance value=\"true\"/>" ELEMENT_WRAPPER_END;
1562 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1563 assert_int_equal(type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001564 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001565 memset(&type, 0, sizeof(type));
1566
1567 data = ELEMENT_WRAPPER_START "<require-instance value=\"false\"/>" ELEMENT_WRAPPER_END;
1568 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1569 assert_int_equal(type.require_instance, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001570 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001571 memset(&type, 0, sizeof(type));
1572
1573 data = ELEMENT_WRAPPER_START "<require-instance value=\"invalid\"/>" ELEMENT_WRAPPER_END;
1574 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1575 memset(&type, 0, sizeof(type));
1576 logbuf_assert("Invalid value \"invalid\" of \"require-instance\". Line number 1.");
1577
1578 st->finished_correctly = true;
1579}
1580
1581static void
1582test_revision_date_elem(void **state)
1583{
1584 struct state *st = *state;
1585 const char *data;
1586 char rev[LY_REV_SIZE];
1587
1588 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\"/>" ELEMENT_WRAPPER_END;
1589 assert_int_equal(test_element_helper(st, &data, rev, NULL, NULL, true), LY_SUCCESS);
1590 assert_string_equal(rev, "2000-01-01");
1591
1592 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-50-05\"/>" ELEMENT_WRAPPER_END;
1593 assert_int_equal(test_element_helper(st, &data, rev, NULL, NULL, false), LY_EVALID);
1594 logbuf_assert("Invalid value \"2000-50-05\" of \"revision-date\". Line number 1.");
1595
1596 st->finished_correctly = true;
1597}
1598
1599static void
1600test_unique_elem(void **state)
1601{
1602 struct state *st = *state;
1603 const char *data;
1604 const char **values = NULL;
1605
1606 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\"/>" ELEMENT_WRAPPER_END;
1607 assert_int_equal(test_element_helper(st, &data, &values, NULL, NULL, true), LY_SUCCESS);
1608 assert_string_equal(*values, "tag");
1609 FREE_STRING(st->ctx, *values);
1610 LY_ARRAY_FREE(values);
1611
1612 st->finished_correctly = true;
1613}
1614
1615static void
1616test_units_elem(void **state)
1617{
1618 struct state *st = *state;
1619 const char *data;
1620 const char *values = NULL;
1621
1622 data = ELEMENT_WRAPPER_START "<units name=\"name\"/>" ELEMENT_WRAPPER_END;
1623 assert_int_equal(test_element_helper(st, &data, &values, NULL, NULL, true), LY_SUCCESS);
1624 assert_string_equal(values, "name");
1625 FREE_STRING(st->ctx, values);
1626
1627 st->finished_correctly = true;
1628}
1629
1630static void
1631test_when_elem(void **state)
1632{
1633 struct state *st = *state;
1634 const char *data;
1635 struct lysp_when *when = NULL;
1636
1637 data = ELEMENT_WRAPPER_START
1638 "<when condition=\"cond\">"
1639 "<description><text>desc</text></description>"
1640 "<reference><text>ref</text></reference>"
1641 "</when>"
1642 ELEMENT_WRAPPER_END;
1643 assert_int_equal(test_element_helper(st, &data, &when, NULL, NULL, true), LY_SUCCESS);
1644 assert_string_equal(when->cond, "cond");
1645 assert_string_equal(when->dsc, "desc");
1646 assert_string_equal(when->ref, "ref");
1647 lysp_when_free(st->ctx, when);
1648 free(when);
1649 when = NULL;
1650
1651 data = ELEMENT_WRAPPER_START "<when condition=\"cond\" />" ELEMENT_WRAPPER_END;
1652 assert_int_equal(test_element_helper(st, &data, &when, NULL, NULL, true), LY_SUCCESS);
1653 assert_string_equal(when->cond, "cond");
1654 lysp_when_free(st->ctx, when);
1655 free(when);
1656 when = NULL;
1657
1658 st->finished_correctly = true;
1659}
1660
1661static void
1662test_yin_text_value_elem(void **state)
1663{
1664 struct state *st = *state;
1665 const char *data;
1666 const char *val;
1667
1668 data = ELEMENT_WRAPPER_START "<text>text</text>" ELEMENT_WRAPPER_END;
1669 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1670 assert_string_equal(val, "text");
1671 FREE_STRING(st->ctx, val);
1672
1673 data = "<error-message xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <value>text</value> </error-message>";
1674 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1675 assert_string_equal(val, "text");
1676 FREE_STRING(st->ctx, val);
1677
1678 data = ELEMENT_WRAPPER_START "<text></text>" ELEMENT_WRAPPER_END;
1679 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1680 assert_string_equal("", val);
1681 FREE_STRING(st->ctx, val);
1682
David Sedlák8e7bda82019-07-16 17:57:50 +02001683 st->finished_correctly = true;
1684}
David Sedlák32488102019-07-15 17:44:10 +02001685
David Sedlák374d2b32019-07-17 15:06:55 +02001686static void
1687test_type_elem(void **state)
1688{
1689 struct state *st = *state;
1690 const char *data;
1691 struct lysp_type type = {};
1692
1693 /* max subelems */
1694 data = ELEMENT_WRAPPER_START
1695 "<type name=\"type-name\">"
1696 "<base name=\"base-name\"/>"
1697 "<bit name=\"bit\"/>"
1698 "<enum name=\"enum\"/>"
1699 "<fraction-digits value=\"2\"/>"
1700 "<length value=\"length\"/>"
1701 "<path value=\"path\"/>"
1702 "<pattern value=\"pattern\"/>"
1703 "<range value=\"range\" />"
1704 "<require-instance value=\"true\"/>"
1705 "<type name=\"sub-type-name\"/>"
1706 "</type>"
1707 ELEMENT_WRAPPER_END;
1708 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1709 assert_string_equal(type.name, "type-name");
1710 assert_string_equal(*type.bases, "base-name");
1711 assert_string_equal(type.bits->name, "bit");
1712 assert_string_equal(type.enums->name, "enum");
1713 assert_int_equal(type.fraction_digits, 2);
1714 assert_string_equal(type.length->arg, "length");
1715 assert_string_equal(type.path, "path");
1716 assert_string_equal(type.patterns->arg, "\006pattern");
1717 assert_string_equal(type.range->arg, "range");
1718 assert_int_equal(type.require_instance, 1);
1719 assert_string_equal(type.types->name, "sub-type-name");
1720 lysp_type_free(st->ctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001721 assert_true(type.flags & LYS_SET_BASE);
1722 assert_true(type.flags & LYS_SET_BIT);
1723 assert_true(type.flags & LYS_SET_ENUM);
1724 assert_true(type.flags & LYS_SET_FRDIGITS);
1725 assert_true(type.flags & LYS_SET_LENGTH);
1726 assert_true(type.flags & LYS_SET_PATH);
1727 assert_true(type.flags & LYS_SET_PATTERN);
1728 assert_true(type.flags & LYS_SET_RANGE);
1729 assert_true(type.flags & LYS_SET_REQINST);
1730 assert_true(type.flags & LYS_SET_TYPE);
David Sedlák374d2b32019-07-17 15:06:55 +02001731 memset(&type, 0, sizeof(type));
1732
1733 /* min subelems */
1734 data = ELEMENT_WRAPPER_START "<type name=\"type-name\"/>" ELEMENT_WRAPPER_END;
1735 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1736 lysp_type_free(st->ctx, &type);
1737 memset(&type, 0, sizeof(type));
1738
1739 st->finished_correctly = true;
1740}
1741
David Sedlák1af868e2019-07-17 17:03:14 +02001742static void
1743test_max_elems_elem(void **state)
1744{
1745 struct state *st = *state;
1746 const char *data;
1747 struct lysp_node_list list = {};
1748 struct lysp_node_leaflist llist = {};
1749 struct lysp_refine refine = {};
1750
1751 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"unbounded\"/> </refine>";
1752 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
1753 assert_int_equal(refine.max, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001754 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001755
1756 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"5\"/> </list>";
1757 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, true), LY_SUCCESS);
1758 assert_int_equal(list.max, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001759 assert_true(list.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001760
1761 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"85\"/> </leaf-list>";
1762 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, true), LY_SUCCESS);
1763 assert_int_equal(llist.max, 85);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001764 assert_true(llist.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001765
1766 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"10\"/> </refine>";
1767 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
1768 assert_int_equal(refine.max, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001769 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001770
1771 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"0\"/> </list>";
1772 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1773 logbuf_assert("Invalid value \"0\" of \"max-elements\". Line number 1.");
1774
1775 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"-10\"/> </list>";
1776 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1777 logbuf_assert("Invalid value \"-10\" of \"max-elements\". Line number 1.");
1778
1779 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"k\"/> </list>";
1780 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1781 logbuf_assert("Invalid value \"k\" of \"max-elements\". Line number 1.");
1782
1783 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"u12\"/> </list>";
1784 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1785 logbuf_assert("Invalid value \"u12\" of \"max-elements\". Line number 1.");
1786
1787 st->finished_correctly = true;
1788}
1789
David Sedlák09e18c92019-07-18 11:17:11 +02001790static void
1791test_min_elems_elem(void **state)
1792{
1793 struct state *st = *state;
1794 const char *data;
1795 struct lysp_node_list list = {};
1796 struct lysp_node_leaflist llist = {};
1797 struct lysp_refine refine = {};
1798
1799 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"0\"/> </refine>";
1800 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
1801 assert_int_equal(refine.min, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001802 assert_true(refine.flags & LYS_SET_MIN);
David Sedlák09e18c92019-07-18 11:17:11 +02001803
1804 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"41\"/> </list>";
1805 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, true), LY_SUCCESS);
1806 assert_int_equal(list.min, 41);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001807 assert_true(list.flags & LYS_SET_MIN);
David Sedlák09e18c92019-07-18 11:17:11 +02001808
1809 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"50\"/> </leaf-list>";
1810 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, true), LY_SUCCESS);
1811 assert_int_equal(llist.min, 50);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001812 assert_true(llist.flags & LYS_SET_MIN);
David Sedlák09e18c92019-07-18 11:17:11 +02001813
1814 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"-5\"/> </leaf-list>";
1815 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1816 logbuf_assert("Value \"-5\" is out of \"min-elements\" bounds. Line number 1.");
1817
1818 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"99999999999999999\"/> </leaf-list>";
1819 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1820 logbuf_assert("Value \"99999999999999999\" is out of \"min-elements\" bounds. Line number 1.");
1821
1822 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"5k\"/> </leaf-list>";
1823 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1824 logbuf_assert("Invalid value \"5k\" of \"min-elements\". Line number 1.");
1825
1826 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"05\"/> </leaf-list>";
1827 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1828 logbuf_assert("Invalid value \"05\" of \"min-elements\". Line number 1.");
1829
1830 st->finished_correctly = true;
1831}
1832
David Sedláka2dad212019-07-18 12:45:19 +02001833static void
1834test_ordby_elem(void **state)
1835{
1836 struct state *st = *state;
1837 const char *data;
1838 uint16_t flags = 0;
1839
1840 data = ELEMENT_WRAPPER_START "<ordered-by value=\"system\"/>" ELEMENT_WRAPPER_END;
1841 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001842 assert_true(flags & LYS_ORDBY_SYSTEM);
David Sedláka2dad212019-07-18 12:45:19 +02001843
1844 data = ELEMENT_WRAPPER_START "<ordered-by value=\"user\"/>" ELEMENT_WRAPPER_END;
1845 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001846 assert_true(flags & LYS_ORDBY_USER);
David Sedláka2dad212019-07-18 12:45:19 +02001847
1848 data = ELEMENT_WRAPPER_START "<ordered-by value=\"inv\"/>" ELEMENT_WRAPPER_END;
1849 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
1850 logbuf_assert("Invalid value \"inv\" of \"ordered-by\". Line number 1.");
1851
1852 st->finished_correctly = true;
1853}
1854
David Sedlák8a83bbb2019-07-18 14:46:00 +02001855static void
1856test_any_elem(void **state)
1857{
1858 struct state *st = *state;
1859 const char *data;
1860 struct lysp_node *siblings = NULL;
1861 struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
1862 struct lysp_node_anydata *parsed = NULL;
1863
1864 /* anyxml max subelems */
1865 data = ELEMENT_WRAPPER_START
1866 "<anyxml name=\"any-name\">"
1867 "<config value=\"true\" />"
1868 "<description><text>desc</text></description>"
1869 "<if-feature name=\"feature\" />"
1870 "<mandatory value=\"true\" />"
1871 "<must condition=\"must-cond\" />"
1872 "<reference><text>ref</text></reference>"
1873 "<status value=\"deprecated\"/>"
1874 "<when condition=\"when-cond\"/>"
1875 "</anyxml>"
1876 ELEMENT_WRAPPER_END;
1877 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1878 parsed = (struct lysp_node_anydata *)siblings;
1879 assert_null(parsed->parent);
1880 assert_int_equal(parsed->nodetype, LYS_ANYXML);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001881 assert_true(parsed->flags & LYS_CONFIG_W);
1882 assert_true(parsed->flags & LYS_MAND_TRUE);
1883 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001884 assert_null(parsed->next);
1885 assert_string_equal(parsed->name, "any-name");
1886 assert_string_equal(parsed->dsc, "desc");
1887 assert_string_equal(parsed->ref, "ref");
1888 assert_string_equal(parsed->when->cond, "when-cond");
1889 assert_string_equal(*parsed->iffeatures, "feature");
1890 assert_null(parsed->exts);
1891 lysp_node_free(st->ctx, siblings);
1892 siblings = NULL;
1893
1894 /* anydata max subelems */
1895 data = ELEMENT_WRAPPER_START
1896 "<anydata name=\"any-name\">"
1897 "<config value=\"true\" />"
1898 "<description><text>desc</text></description>"
1899 "<if-feature name=\"feature\" />"
1900 "<mandatory value=\"true\" />"
1901 "<must condition=\"must-cond\" />"
1902 "<reference><text>ref</text></reference>"
1903 "<status value=\"deprecated\"/>"
1904 "<when condition=\"when-cond\"/>"
1905 "</anydata>"
1906 ELEMENT_WRAPPER_END;
1907 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1908 parsed = (struct lysp_node_anydata *)siblings;
1909 assert_null(parsed->parent);
1910 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001911 assert_true(parsed->flags & LYS_CONFIG_W);
1912 assert_true(parsed->flags & LYS_MAND_TRUE);
1913 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001914 assert_null(parsed->next);
1915 assert_string_equal(parsed->name, "any-name");
1916 assert_string_equal(parsed->dsc, "desc");
1917 assert_string_equal(parsed->ref, "ref");
1918 assert_string_equal(parsed->when->cond, "when-cond");
1919 assert_string_equal(*parsed->iffeatures, "feature");
1920 assert_null(parsed->exts);
1921 lysp_node_free(st->ctx, siblings);
1922 siblings = NULL;
1923
1924 /* min subelems */
1925 node_meta.parent = (void *)0x10;
1926 data = ELEMENT_WRAPPER_START "<anydata name=\"any-name\"> </anydata>" ELEMENT_WRAPPER_END;
1927 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1928 parsed = (struct lysp_node_anydata *)siblings;
1929 assert_ptr_equal(parsed->parent, node_meta.parent);
1930 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
1931 assert_null(parsed->next);
1932 assert_null(parsed->exts);
1933 lysp_node_free(st->ctx, siblings);
1934
1935 st->finished_correctly = true;
1936}
1937
David Sedlák203ca3a2019-07-18 15:26:25 +02001938static void
1939test_leaf_elem(void **state)
1940{
1941 struct state *st = *state;
1942 const char *data;
1943 struct lysp_node *siblings = NULL;
1944 struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
1945 struct lysp_node_leaf *parsed = NULL;
1946
1947 /* max elements */
1948 data = ELEMENT_WRAPPER_START
1949 "<leaf name=\"leaf\">"
1950 "<config value=\"true\" />"
1951 "<default value=\"def-val\"/>"
1952 "<description><text>desc</text></description>"
1953 "<if-feature name=\"feature\" />"
1954 "<mandatory value=\"true\" />"
1955 "<must condition=\"must-cond\" />"
1956 "<reference><text>ref</text></reference>"
1957 "<status value=\"deprecated\"/>"
1958 "<type name=\"type\"/>"
1959 "<units name=\"uni\"/>"
1960 "<when condition=\"when-cond\"/>"
1961 "</leaf>"
1962 ELEMENT_WRAPPER_END;
1963 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1964 parsed = (struct lysp_node_leaf *)siblings;
1965 assert_null(parsed->parent);
1966 assert_int_equal(parsed->nodetype, LYS_LEAF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001967 assert_true(parsed->flags & LYS_CONFIG_W);
1968 assert_true(parsed->flags & LYS_MAND_TRUE);
1969 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák203ca3a2019-07-18 15:26:25 +02001970 assert_null(parsed->next);
1971 assert_string_equal(parsed->name, "leaf");
1972 assert_string_equal(parsed->dsc, "desc");
1973 assert_string_equal(parsed->ref, "ref");
1974 assert_string_equal(parsed->when->cond, "when-cond");
1975 assert_string_equal(*parsed->iffeatures, "feature");
1976 assert_null(parsed->exts);
1977 assert_string_equal(parsed->musts->arg, "must-cond");
1978 assert_string_equal(parsed->type.name, "type");
1979 assert_string_equal(parsed->units, "uni");
1980 assert_string_equal(parsed->dflt, "def-val");
1981 lysp_node_free(st->ctx, siblings);
1982 siblings = NULL;
1983
1984 /* min elements */
1985 data = ELEMENT_WRAPPER_START "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>" ELEMENT_WRAPPER_END;
1986 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1987 parsed = (struct lysp_node_leaf *)siblings;
1988 assert_string_equal(parsed->name, "leaf");
1989 assert_string_equal(parsed->type.name, "type");
1990 lysp_node_free(st->ctx, siblings);
1991 siblings = NULL;
1992
1993 st->finished_correctly = true;
1994}
1995
David Sedlákc3da3ef2019-07-19 12:56:08 +02001996static void
1997test_leaf_list_elem(void **state)
1998{
1999 struct state *st = *state;
2000 const char *data;
2001 struct lysp_node *siblings = NULL;
2002 struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
2003 struct lysp_node_leaflist *parsed = NULL;
2004
2005 data = ELEMENT_WRAPPER_START
2006 "<leaf-list name=\"llist\">"
2007 "<config value=\"true\" />"
2008 "<default value=\"def-val0\"/>"
2009 "<default value=\"def-val1\"/>"
2010 "<description><text>desc</text></description>"
2011 "<if-feature name=\"feature\"/>"
2012 "<max-elements value=\"5\"/>"
2013 "<must condition=\"must-cond\"/>"
2014 "<ordered-by value=\"user\" />"
2015 "<reference><text>ref</text></reference>"
2016 "<status value=\"current\"/>"
2017 "<type name=\"type\"/>"
2018 "<units name=\"uni\"/>"
2019 "<when condition=\"when-cond\"/>"
2020 "</leaf-list>"
2021 ELEMENT_WRAPPER_END;
2022 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2023 parsed = (struct lysp_node_leaflist *)siblings;
2024 assert_string_equal(parsed->dflts[0], "def-val0");
2025 assert_string_equal(parsed->dflts[1], "def-val1");
2026 assert_string_equal(parsed->dsc, "desc");
2027 assert_string_equal(*parsed->iffeatures, "feature");
2028 assert_int_equal(parsed->max, 5);
2029 assert_string_equal(parsed->musts->arg, "must-cond");
2030 assert_string_equal(parsed->name, "llist");
2031 assert_null(parsed->next);
2032 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2033 assert_null(parsed->parent);
2034 assert_string_equal(parsed->ref, "ref");
2035 assert_string_equal(parsed->type.name, "type");
2036 assert_string_equal(parsed->units, "uni");
2037 assert_string_equal(parsed->when->cond, "when-cond");
2038 assert_true(parsed->flags & LYS_CONFIG_W);
2039 assert_true(parsed->flags & LYS_ORDBY_USER);
2040 assert_true(parsed->flags & LYS_STATUS_CURR);
2041 lysp_node_free(st->ctx, siblings);
2042 siblings = NULL;
2043
2044 data = ELEMENT_WRAPPER_START
2045 "<leaf-list name=\"llist\">"
2046 "<config value=\"true\" />"
2047 "<description><text>desc</text></description>"
2048 "<if-feature name=\"feature\"/>"
2049 "<min-elements value=\"5\"/>"
2050 "<must condition=\"must-cond\"/>"
2051 "<ordered-by value=\"user\" />"
2052 "<reference><text>ref</text></reference>"
2053 "<status value=\"current\"/>"
2054 "<type name=\"type\"/>"
2055 "<units name=\"uni\"/>"
2056 "<when condition=\"when-cond\"/>"
2057 "</leaf-list>"
2058 ELEMENT_WRAPPER_END;
2059 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2060 parsed = (struct lysp_node_leaflist *)siblings;
2061 assert_string_equal(parsed->dsc, "desc");
2062 assert_string_equal(*parsed->iffeatures, "feature");
2063 assert_int_equal(parsed->min, 5);
2064 assert_string_equal(parsed->musts->arg, "must-cond");
2065 assert_string_equal(parsed->name, "llist");
2066 assert_null(parsed->next);
2067 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2068 assert_null(parsed->parent);
2069 assert_string_equal(parsed->ref, "ref");
2070 assert_string_equal(parsed->type.name, "type");
2071 assert_string_equal(parsed->units, "uni");
2072 assert_string_equal(parsed->when->cond, "when-cond");
2073 assert_true(parsed->flags & LYS_CONFIG_W);
2074 assert_true(parsed->flags & LYS_ORDBY_USER);
2075 assert_true(parsed->flags & LYS_STATUS_CURR);
2076 lysp_node_free(st->ctx, siblings);
2077 siblings = NULL;
2078
2079 data = ELEMENT_WRAPPER_START
2080 "<leaf-list name=\"llist\">"
2081 "<config value=\"true\" />"
2082 "<description><text>desc</text></description>"
2083 "<if-feature name=\"feature\"/>"
2084 "<max-elements value=\"15\"/>"
2085 "<min-elements value=\"5\"/>"
2086 "<must condition=\"must-cond\"/>"
2087 "<ordered-by value=\"user\" />"
2088 "<reference><text>ref</text></reference>"
2089 "<status value=\"current\"/>"
2090 "<type name=\"type\"/>"
2091 "<units name=\"uni\"/>"
2092 "<when condition=\"when-cond\"/>"
2093 "</leaf-list>"
2094 ELEMENT_WRAPPER_END;
2095 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2096 parsed = (struct lysp_node_leaflist *)siblings;
2097 assert_string_equal(parsed->dsc, "desc");
2098 assert_string_equal(*parsed->iffeatures, "feature");
2099 assert_int_equal(parsed->min, 5);
2100 assert_int_equal(parsed->max, 15);
2101 assert_string_equal(parsed->musts->arg, "must-cond");
2102 assert_string_equal(parsed->name, "llist");
2103 assert_null(parsed->next);
2104 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2105 assert_null(parsed->parent);
2106 assert_string_equal(parsed->ref, "ref");
2107 assert_string_equal(parsed->type.name, "type");
2108 assert_string_equal(parsed->units, "uni");
2109 assert_string_equal(parsed->when->cond, "when-cond");
2110 assert_true(parsed->flags & LYS_CONFIG_W);
2111 assert_true(parsed->flags & LYS_ORDBY_USER);
2112 assert_true(parsed->flags & LYS_STATUS_CURR);
2113 lysp_node_free(st->ctx, siblings);
2114 siblings = NULL;
2115
2116 data = ELEMENT_WRAPPER_START
2117 "<leaf-list name=\"llist\">"
2118 "<type name=\"type\"/>"
2119 "</leaf-list>"
2120 ELEMENT_WRAPPER_END;
2121 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2122 parsed = (struct lysp_node_leaflist *)siblings;
2123 assert_string_equal(parsed->name, "llist");
2124 assert_string_equal(parsed->type.name, "type");
2125 lysp_node_free(st->ctx, siblings);
2126 siblings = NULL;
2127
2128 /* invalid combinations */
2129 data = ELEMENT_WRAPPER_START
2130 "<leaf-list name=\"llist\">"
2131 "<max-elements value=\"5\"/>"
2132 "<min-elements value=\"15\"/>"
2133 "<type name=\"type\"/>"
2134 "</leaf-list>"
2135 ELEMENT_WRAPPER_END;
2136 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
2137 logbuf_assert("Invalid combination of min-elements and max-elements: min value 15 is bigger than the max value 5. Line number 1.");
2138 lysp_node_free(st->ctx, siblings);
2139 siblings = NULL;
2140
2141 data = ELEMENT_WRAPPER_START
2142 "<leaf-list name=\"llist\">"
2143 "<default value=\"def-val1\"/>"
2144 "<min-elements value=\"15\"/>"
2145 "<type name=\"type\"/>"
2146 "</leaf-list>"
2147 ELEMENT_WRAPPER_END;
2148 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
2149 logbuf_assert("Invalid combination of keywords \"min-elements\" and \"default\" as substatements of \"leaf-list\". Line number 1.");
2150 lysp_node_free(st->ctx, siblings);
2151 siblings = NULL;
2152
2153 data = ELEMENT_WRAPPER_START
2154 "<leaf-list name=\"llist\">"
2155 "</leaf-list>"
2156 ELEMENT_WRAPPER_END;
2157 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
2158 logbuf_assert("Missing mandatory subelement type of leaf-list element. Line number 1.");
2159 lysp_node_free(st->ctx, siblings);
2160 siblings = NULL;
2161
2162 st->finished_correctly = true;
2163}
2164
David Sedlákcb39f642019-07-19 13:19:55 +02002165static void
2166test_presence_elem(void **state)
2167{
2168 struct state *st = *state;
2169 const char *data;
2170 const char *val;
2171
2172 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\"/>" ELEMENT_WRAPPER_END;
2173 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2174 assert_string_equal(val, "presence-val");
2175 FREE_STRING(st->ctx, val);
2176
2177 data = ELEMENT_WRAPPER_START "<presence/>" ELEMENT_WRAPPER_END;
2178 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
2179 logbuf_assert("Missing mandatory attribute value of presence element. Line number 1.");
2180
2181 st->finished_correctly = true;
2182}
2183
David Sedlák12470a82019-07-19 13:44:36 +02002184static void
2185test_key_elem(void **state)
2186{
2187 struct state *st = *state;
2188 const char *data;
2189 const char *val;
2190
2191 data = ELEMENT_WRAPPER_START "<key value=\"key-value\"/>" ELEMENT_WRAPPER_END;
2192 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2193 assert_string_equal(val, "key-value");
2194 FREE_STRING(st->ctx, val);
2195
2196 data = ELEMENT_WRAPPER_START "<key/>" ELEMENT_WRAPPER_END;
2197 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
2198 logbuf_assert("Missing mandatory attribute value of key element. Line number 1.");
2199
2200 st->finished_correctly = true;
2201}
2202
David Sedlák04e17b22019-07-19 15:29:48 +02002203static void
2204test_typedef_elem(void **state)
2205{
2206 struct state *st = *state;
2207 const char *data;
2208 struct lysp_tpdf *tpdfs = NULL;
2209 struct typedef_meta typdef_meta = {NULL, &tpdfs};
2210
2211 data = ELEMENT_WRAPPER_START
2212 "<typedef name=\"tpdf-name\">"
2213 "<default value=\"def-val\"/>"
2214 "<description><text>desc-text</text></description>"
2215 "<reference><text>ref-text</text></reference>"
2216 "<status value=\"current\"/>"
2217 "<type name=\"type\"/>"
2218 "<units name=\"uni\"/>"
2219 "</typedef>"
2220 ELEMENT_WRAPPER_END;
2221 assert_int_equal(test_element_helper(st, &data, &typdef_meta, NULL, NULL, true), LY_SUCCESS);
2222 assert_string_equal(tpdfs[0].dflt, "def-val");
2223 assert_string_equal(tpdfs[0].dsc, "desc-text");
2224 assert_null(tpdfs[0].exts);
2225 assert_string_equal(tpdfs[0].name, "tpdf-name");
2226 assert_string_equal(tpdfs[0].ref, "ref-text");
2227 assert_string_equal(tpdfs[0].type.name, "type");
2228 assert_string_equal(tpdfs[0].units, "uni");
2229 assert_true(tpdfs[0].flags & LYS_STATUS_CURR);
2230 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2231 tpdfs = NULL;
2232
2233 data = ELEMENT_WRAPPER_START
2234 "<typedef name=\"tpdf-name\">"
2235 "<type name=\"type\"/>"
2236 "</typedef>"
2237 ELEMENT_WRAPPER_END;
2238 assert_int_equal(test_element_helper(st, &data, &typdef_meta, NULL, NULL, true), LY_SUCCESS);
2239 assert_string_equal(tpdfs[0].name, "tpdf-name");
2240 assert_string_equal(tpdfs[0].type.name, "type");
2241 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2242 tpdfs = NULL;
2243
2244 st->finished_correctly = true;
2245}
2246
David Sedlákd2d676a2019-07-22 11:28:19 +02002247static void
2248test_refine_elem(void **state)
2249{
2250 struct state *st = *state;
2251 const char *data;
2252 struct lysp_refine *refines = NULL;
2253
2254 /* max subelems */
2255 data = ELEMENT_WRAPPER_START
2256 "<refine target-node=\"target\">"
2257 "<if-feature name=\"feature\" />"
2258 "<must condition=\"cond\" />"
2259 "<presence value=\"presence\" />"
2260 "<default value=\"def\" />"
2261 "<config value=\"true\" />"
2262 "<mandatory value=\"true\" />"
2263 "<min-elements value=\"10\" />"
2264 "<max-elements value=\"20\" />"
2265 "<description><text>desc</text></description>"
2266 "<reference><text>ref</text></reference>"
2267 "</refine>"
2268 ELEMENT_WRAPPER_END;
2269 assert_int_equal(test_element_helper(st, &data, &refines, NULL, NULL, true), LY_SUCCESS);
2270 assert_string_equal(refines->nodeid, "target");
2271 assert_string_equal(*refines->dflts, "def");
2272 assert_string_equal(refines->dsc, "desc");
2273 assert_null(refines->exts);
2274 assert_true(refines->flags & LYS_CONFIG_W);
2275 assert_true(refines->flags & LYS_MAND_TRUE);
2276 assert_string_equal(*refines->iffeatures, "feature");
2277 assert_int_equal(refines->max, 20);
2278 assert_int_equal(refines->min, 10);
2279 assert_string_equal(refines->musts->arg, "cond");
2280 assert_string_equal(refines->presence, "presence");
2281 assert_string_equal(refines->ref, "ref");
2282 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2283 refines = NULL;
2284
2285 /* min subelems */
2286 data = ELEMENT_WRAPPER_START "<refine target-node=\"target\" />" ELEMENT_WRAPPER_END;
2287 assert_int_equal(test_element_helper(st, &data, &refines, NULL, NULL, true), LY_SUCCESS);
2288 assert_string_equal(refines->nodeid, "target");
2289 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2290 refines = NULL;
2291
2292 st->finished_correctly = true;
2293}
2294
David Sedlák0d6de5a2019-07-22 13:25:44 +02002295static void
2296test_uses_elem(void **state)
2297{
2298 struct state *st = *state;
2299 const char *data;
2300 struct lysp_node *siblings = NULL;
2301 struct tree_node_meta node_meta = {NULL, &siblings};
2302 struct lysp_node_uses *parsed = NULL;
2303
2304 /* max subelems */
2305 data = ELEMENT_WRAPPER_START
2306 "<uses name=\"uses-name\">"
2307 "<when condition=\"cond\" />"
2308 "<if-feature name=\"feature\" />"
2309 "<status value=\"obsolete\" />"
2310 "<description><text>desc</text></description>"
2311 "<reference><text>ref</text></reference>"
2312 "<refine target-node=\"target\"/>"
2313 /* TODO add uses-augment-stmt instance */
2314 "</uses>"
2315 ELEMENT_WRAPPER_END;
2316 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2317 parsed = (struct lysp_node_uses *)&siblings[0];
2318 assert_string_equal(parsed->name, "uses-name");
2319 assert_string_equal(parsed->dsc, "desc");
2320 assert_null(parsed->exts);
2321 assert_true(parsed->flags & LYS_STATUS_OBSLT);
2322 assert_string_equal(*parsed->iffeatures, "feature");
2323 assert_null(parsed->next);
2324 assert_int_equal(parsed->nodetype, LYS_USES);
2325 assert_null(parsed->parent);
2326 assert_string_equal(parsed->ref, "ref");
2327 assert_string_equal(parsed->refines->nodeid, "target");
2328 assert_string_equal(parsed->when->cond, "cond");
2329 lysp_node_free(st->ctx, siblings);
2330 siblings = NULL;
2331
2332 /* min subelems */
2333 data = ELEMENT_WRAPPER_START "<uses name=\"uses-name\"/>" ELEMENT_WRAPPER_END;
2334 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2335 assert_string_equal(siblings[0].name, "uses-name");
2336 lysp_node_free(st->ctx, siblings);
2337 siblings = NULL;
2338
2339 st->finished_correctly = true;
2340}
2341
David Sedlákaa854b02019-07-22 14:17:10 +02002342static void
2343test_revision_elem(void **state)
2344{
2345 struct state *st = *state;
2346 const char *data;
2347 struct lysp_revision *revs = NULL;
2348
2349 /* max subelems */
2350 data = ELEMENT_WRAPPER_START
2351 "<revision date=\"2018-12-25\">"
2352 "<description><text>desc</text></description>"
2353 "<reference><text>ref</text></reference>"
2354 "</revision>"
2355 ELEMENT_WRAPPER_END;
2356 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, true), LY_SUCCESS);
2357 assert_string_equal(revs->date, "2018-12-25");
2358 assert_string_equal(revs->dsc, "desc");
2359 assert_string_equal(revs->ref, "ref");
2360 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2361 revs = NULL;
2362
2363 /* min subelems */
2364 data = ELEMENT_WRAPPER_START "<revision date=\"2005-05-05\" />" ELEMENT_WRAPPER_END;
2365 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, true), LY_SUCCESS);
2366 assert_string_equal(revs->date, "2005-05-05");
2367 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2368 revs = NULL;
2369
2370 /* invalid value */
2371 data = ELEMENT_WRAPPER_START "<revision date=\"05-05-2005\" />" ELEMENT_WRAPPER_END;
2372 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, false), LY_EVALID);
2373 logbuf_assert("Invalid value \"05-05-2005\" of \"revision\". Line number 1.");
2374 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2375 revs = NULL;
2376
2377 st->finished_correctly = true;
2378}
2379
David Sedlák0c2bab92019-07-22 15:33:19 +02002380static void
2381test_include_elem(void **state)
2382{
2383 struct state *st = *state;
2384 const char *data;
2385 struct lysp_include *includes = NULL;
2386 struct include_meta inc_meta = {"module-name", &includes};
2387
2388 /* max subelems */
2389 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2390 data = ELEMENT_WRAPPER_START
2391 "<include module=\"mod\">"
2392 "<description><text>desc</text></description>"
2393 "<reference><text>ref</text></reference>"
2394 "<revision-date date=\"1999-09-09\"/>"
2395 "</include>"
2396 ELEMENT_WRAPPER_END;
2397 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, true), LY_SUCCESS);
2398 assert_string_equal(includes->name, "mod");
2399 assert_string_equal(includes->dsc, "desc");
2400 assert_string_equal(includes->ref, "ref");
2401 assert_null(includes->exts);
2402 assert_string_equal(includes->rev, "1999-09-09");
2403 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2404 includes = NULL;
2405
2406 /* min subelems */
2407 data = ELEMENT_WRAPPER_START "<include module=\"mod\"/>" ELEMENT_WRAPPER_END;
2408 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, true), LY_SUCCESS);
2409 assert_string_equal(includes->name, "mod");
2410 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2411 includes = NULL;
2412
2413 /* invalid combinations */
2414 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2415 data = ELEMENT_WRAPPER_START
2416 "<include module=\"mod\">"
2417 "<description><text>desc</text></description>"
2418 "<revision-date date=\"1999-09-09\"/>"
2419 "</include>"
2420 ELEMENT_WRAPPER_END;
2421 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, false), LY_EVALID);
2422 logbuf_assert("Invalid sub-elemnt \"description\" of \"include\" element - this sub-element is allowed only in modules with version 1.1 or newer. Line number 1.");
2423 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2424 includes = NULL;
2425
2426 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2427 data = ELEMENT_WRAPPER_START
2428 "<include module=\"mod\">"
2429 "<reference><text>ref</text></reference>"
2430 "<revision-date date=\"1999-09-09\"/>"
2431 "</include>"
2432 ELEMENT_WRAPPER_END;
2433 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, false), LY_EVALID);
2434 logbuf_assert("Invalid sub-elemnt \"reference\" of \"include\" element - this sub-element is allowed only in modules with version 1.1 or newer. Line number 1.");
2435 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2436 includes = NULL;
2437
2438 st->finished_correctly = true;
2439}
2440
David Sedlák5e13dea2019-07-22 16:06:45 +02002441static void
2442test_feature_elem(void **state)
2443{
2444 struct state *st = *state;
2445 const char *data;
2446 struct lysp_feature *features = NULL;
2447
2448 /* max subelems */
2449 data = ELEMENT_WRAPPER_START
2450 "<feature name=\"feature-name\">"
2451 "<if-feature name=\"iff\"/>"
2452 "<status value=\"deprecated\"/>"
2453 "<description><text>desc</text></description>"
2454 "<reference><text>ref</text></reference>"
2455 "</feature>"
2456 ELEMENT_WRAPPER_END;
2457 assert_int_equal(test_element_helper(st, &data, &features, NULL, NULL, true), LY_SUCCESS);
2458 assert_string_equal(features->name, "feature-name");
2459 assert_string_equal(features->dsc, "desc");
2460 assert_null(features->exts);
2461 assert_true(features->flags & LYS_STATUS_DEPRC);
2462 assert_string_equal(*features->iffeatures, "iff");
2463 assert_string_equal(features->ref, "ref");
2464 FREE_ARRAY(st->ctx, features, lysp_feature_free);
2465 features = NULL;
2466
2467 /* min subelems */
2468 data = ELEMENT_WRAPPER_START "<feature name=\"feature-name\"/>" ELEMENT_WRAPPER_END;
2469 assert_int_equal(test_element_helper(st, &data, &features, NULL, NULL, true), LY_SUCCESS);
2470 assert_string_equal(features->name, "feature-name");
2471 FREE_ARRAY(st->ctx, features, lysp_feature_free);
2472 features = NULL;
2473
2474 st->finished_correctly = true;
2475}
2476
David Sedlák28794f22019-07-22 16:45:00 +02002477static void
2478test_identity_elem(void **state)
2479{
2480 struct state *st = *state;
2481 const char *data;
2482 struct lysp_ident *identities = NULL;
2483
2484 /* max subelems */
2485 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2486 data = ELEMENT_WRAPPER_START
2487 "<identity name=\"ident-name\">"
2488 "<if-feature name=\"iff\"/>"
2489 "<base name=\"base-name\"/>"
2490 "<status value=\"deprecated\"/>"
2491 "<description><text>desc</text></description>"
2492 "<reference><text>ref</text></reference>"
2493 "</identity>"
2494 ELEMENT_WRAPPER_END;
2495 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, true), LY_SUCCESS);
2496 assert_string_equal(identities->name, "ident-name");
2497 assert_string_equal(*identities->bases, "base-name");
2498 assert_string_equal(*identities->iffeatures, "iff");
2499 assert_string_equal(identities->dsc, "desc");
2500 assert_string_equal(identities->ref, "ref");
2501 assert_true(identities->flags & LYS_STATUS_DEPRC);
2502 assert_null(identities->exts);
2503 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2504 identities = NULL;
2505
2506 /* min subelems */
2507 data = ELEMENT_WRAPPER_START "<identity name=\"ident-name\" />" ELEMENT_WRAPPER_END;
2508 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, true), LY_SUCCESS);
2509 assert_string_equal(identities->name, "ident-name");
2510 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2511 identities = NULL;
2512
2513 /* invalid */
2514 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2515 data = ELEMENT_WRAPPER_START
2516 "<identity name=\"ident-name\">"
2517 "<if-feature name=\"iff\"/>"
2518 "</identity>"
2519 ELEMENT_WRAPPER_END;
2520 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, false), LY_EVALID);
2521 logbuf_assert("Invalid sub-elemnt \"if-feature\" of \"identity\" element - this sub-element is allowed only in modules with version 1.1 or newer. Line number 1.");
2522 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2523 identities = NULL;
2524
2525 st->finished_correctly = true;
2526}
2527
David Sedlákaf536aa2019-07-23 13:42:23 +02002528static void
2529test_list_elem(void **state)
2530{
2531 struct state *st = *state;
2532 const char *data;
2533 struct lysp_node *siblings = NULL;
2534 struct tree_node_meta node_meta = {NULL, &siblings};
2535 struct lysp_node_list *parsed = NULL;
2536
2537 /* max subelems */
2538 data = ELEMENT_WRAPPER_START
2539 "<list name=\"list-name\">"
2540 "<when condition=\"when\"/>"
2541 "<if-feature name=\"iff\"/>"
2542 "<must condition=\"must-cond\"/>"
2543 "<key value=\"key\"/>"
2544 "<unique tag=\"utag\"/>"
2545 "<config value=\"true\"/>"
2546 "<min-elements value=\"10\"/>"
2547 "<ordered-by value=\"user\"/>"
2548 "<status value=\"deprecated\"/>"
2549 "<description><text>desc</text></description>"
2550 "<reference><text>ref</text></reference>"
2551 "<anydata name=\"anyd\"/>"
2552 "<anyxml name=\"anyx\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02002553 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002554 "<choice name=\"choice\"/>"
David Sedlákaf536aa2019-07-23 13:42:23 +02002555 // "<action name=\"action\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02002556 "<grouping name=\"grp\"/>"
David Sedlák031b9e72019-07-23 15:19:37 +02002557 "<notification name=\"notf\"/>"
David Sedlákaf536aa2019-07-23 13:42:23 +02002558 "<leaf name=\"leaf\"/>"
2559 "<leaf-list name=\"llist\"/>"
2560 "<list name=\"sub-list\"/>"
2561 "<typedef name=\"tpdf\"/>"
2562 "<uses name=\"uses-name\"/>"
2563 "</list>"
2564 ELEMENT_WRAPPER_END;
2565 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2566 parsed = (struct lysp_node_list *)&siblings[0];
2567 assert_string_equal(parsed->dsc, "desc");
2568 assert_string_equal(parsed->child->name, "anyd");
2569 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2570 assert_string_equal(parsed->child->next->name, "anyx");
2571 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
David Sedlákf111bcb2019-07-23 17:15:51 +02002572 assert_string_equal(parsed->child->next->next->name, "cont");
2573 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002574 assert_string_equal(parsed->child->next->next->next->name, "choice");
2575 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
David Sedlákf111bcb2019-07-23 17:15:51 +02002576 // assert_string_equal(parsed->child->next->next->next->next->name, "action");
2577 // assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_ACTION);
2578 // assert_null(parsed->child->next->next->next->next->next);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002579 assert_string_equal(parsed->groupings->name, "grp");
2580 assert_int_equal(parsed->groupings->nodetype, LYS_GROUPING);
David Sedlák031b9e72019-07-23 15:19:37 +02002581 assert_string_equal(parsed->notifs->name, "notf");
David Sedlákaf536aa2019-07-23 13:42:23 +02002582 assert_null(parsed->exts);
2583 assert_true(parsed->flags & LYS_ORDBY_USER);
2584 assert_true(parsed->flags & LYS_STATUS_DEPRC);
2585 assert_true(parsed->flags & LYS_CONFIG_W);
2586 assert_string_equal(*parsed->iffeatures, "iff");
2587 assert_string_equal(parsed->key, "key");
2588 assert_int_equal(parsed->min, 10);
2589 assert_string_equal(parsed->musts->arg, "must-cond");
2590 assert_string_equal(parsed->name, "list-name");
2591 assert_null(parsed->next);
2592 assert_int_equal(parsed->nodetype, LYS_LIST);
2593 assert_null(parsed->parent);
2594 assert_string_equal(parsed->ref, "ref");
2595 assert_string_equal(parsed->typedefs->name, "tpdf");
2596 assert_string_equal(*parsed->uniques, "utag");
2597 assert_string_equal(parsed->when->cond, "when");
2598 lysp_node_free(st->ctx, siblings);
2599 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
2600 siblings = NULL;
2601
2602 /* min subelems */
2603 data = ELEMENT_WRAPPER_START "<list name=\"list-name\" />" ELEMENT_WRAPPER_END;
2604 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2605 parsed = (struct lysp_node_list *)&siblings[0];
2606 assert_string_equal(parsed->name, "list-name");
2607 lysp_node_free(st->ctx, siblings);
2608 siblings = NULL;
2609
2610 st->finished_correctly = true;
2611}
2612
David Sedlák031b9e72019-07-23 15:19:37 +02002613static void
2614test_notification_elem(void **state)
2615{
2616 struct state *st = *state;
2617 const char *data;
2618 struct lysp_notif *notifs = NULL;
2619 struct notif_meta notif_meta = {NULL, &notifs};
2620
2621 /* max subelems */
2622 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2623 data = ELEMENT_WRAPPER_START
2624 "<notification name=\"notif-name\">"
2625 "<anydata name=\"anyd\"/>"
2626 "<anyxml name=\"anyx\"/>"
2627 "<description><text>desc</text></description>"
2628 "<if-feature name=\"iff\"/>"
2629 "<leaf name=\"leaf\"/>"
2630 "<leaf-list name=\"llist\"/>"
2631 "<list name=\"sub-list\"/>"
2632 "<must condition=\"cond\"/>"
2633 "<reference><text>ref</text></reference>"
2634 "<status value=\"deprecated\"/>"
2635 "<typedef name=\"tpdf\"/>"
2636 "<uses name=\"uses-name\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02002637 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002638 "<choice name=\"choice\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02002639 "<grouping name=\"grp\"/>"
David Sedlák031b9e72019-07-23 15:19:37 +02002640 "</notification>"
2641 ELEMENT_WRAPPER_END;
2642 assert_int_equal(test_element_helper(st, &data, &notif_meta, NULL, NULL, true), LY_SUCCESS);
2643 assert_string_equal(notifs->name, "notif-name");
2644 assert_string_equal(notifs->data->name, "anyd");
2645 assert_int_equal(notifs->data->nodetype, LYS_ANYDATA);
2646 assert_string_equal(notifs->data->next->name, "anyx");
2647 assert_int_equal(notifs->data->next->nodetype, LYS_ANYXML);
2648 assert_string_equal(notifs->data->next->next->name, "leaf");
2649 assert_int_equal(notifs->data->next->next->nodetype, LYS_LEAF);
2650 assert_string_equal(notifs->data->next->next->next->name, "llist");
2651 assert_int_equal(notifs->data->next->next->next->nodetype, LYS_LEAFLIST);
2652 assert_string_equal(notifs->data->next->next->next->next->name, "sub-list");
2653 assert_int_equal(notifs->data->next->next->next->next->nodetype, LYS_LIST);
2654 assert_null(notifs->exts);
2655 assert_true(notifs->flags & LYS_STATUS_DEPRC);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002656 assert_string_equal(notifs->groupings->name, "grp");
2657 assert_int_equal(notifs->groupings->nodetype, LYS_GROUPING);
David Sedlákf111bcb2019-07-23 17:15:51 +02002658 assert_string_equal(notifs->data->next->next->next->next->next->name, "uses-name");
2659 assert_int_equal(notifs->data->next->next->next->next->next->nodetype, LYS_USES);
2660 assert_string_equal(notifs->data->next->next->next->next->next->next->name, "cont");
2661 assert_int_equal(notifs->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002662 assert_int_equal(notifs->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2663 assert_string_equal(notifs->data->next->next->next->next->next->next->next->name, "choice");
2664 assert_null(notifs->data->next->next->next->next->next->next->next->next);
David Sedlák031b9e72019-07-23 15:19:37 +02002665 assert_string_equal(*notifs->iffeatures, "iff");
2666 assert_string_equal(notifs->musts->arg, "cond");
2667 assert_int_equal(notifs->nodetype, LYS_NOTIF);
2668 assert_null(notifs->parent);
2669 assert_string_equal(notifs->ref, "ref");
2670 assert_string_equal(notifs->typedefs->name, "tpdf");
2671 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
2672 notifs = NULL;
2673
2674 /* min subelems */
2675 data = ELEMENT_WRAPPER_START "<notification name=\"notif-name\" />" ELEMENT_WRAPPER_END;
2676 assert_int_equal(test_element_helper(st, &data, &notif_meta, NULL, NULL, true), LY_SUCCESS);
2677 assert_string_equal(notifs->name, "notif-name");
2678 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002679 notifs = NULL;
2680
2681 st->finished_correctly = true;
2682}
2683
2684static void
2685test_grouping_elem(void **state)
2686{
2687 struct state *st = *state;
2688 const char *data;
2689 struct lysp_grp *grps = NULL;
2690 struct grouping_meta grp_meta = {NULL, &grps};
2691
2692 /* max subelems */
2693 data = ELEMENT_WRAPPER_START
2694 "<grouping name=\"grp-name\">"
2695 "<anydata name=\"anyd\"/>"
2696 "<anyxml name=\"anyx\"/>"
2697 "<description><text>desc</text></description>"
2698 "<grouping name=\"sub-grp\"/>"
2699 "<leaf name=\"leaf\"/>"
2700 "<leaf-list name=\"llist\"/>"
2701 "<list name=\"list\"/>"
2702 "<notification name=\"notf\"/>"
2703 "<reference><text>ref</text></reference>"
2704 "<status value=\"current\"/>"
2705 "<typedef name=\"tpdf\"/>"
2706 "<uses name=\"uses-name\"/>"
2707 // "<action name=\"act\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02002708 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002709 "<choice name=\"choice\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02002710 "</grouping>"
2711 ELEMENT_WRAPPER_END;
2712 assert_int_equal(test_element_helper(st, &data, &grp_meta, NULL, NULL, true), LY_SUCCESS);
2713 assert_string_equal(grps->name, "grp-name");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002714 assert_string_equal(grps->data->name, "anyd");
2715 assert_string_equal(grps->data->next->name, "anyx");
2716 assert_string_equal(grps->data->next->next->name, "leaf");
2717 assert_string_equal(grps->data->next->next->next->name, "llist");
2718 assert_string_equal(grps->data->next->next->next->next->name, "list");
2719 assert_string_equal(grps->dsc, "desc");
2720 assert_null(grps->exts);
2721 assert_true(grps->flags & LYS_STATUS_CURR);
2722 assert_string_equal(grps->groupings->name, "sub-grp");
2723 assert_int_equal(grps->nodetype, LYS_GROUPING);
2724 assert_string_equal(grps->notifs->name, "notf");
2725 assert_null(grps->parent);
2726 assert_string_equal(grps->ref, "ref");
2727 assert_string_equal(grps->typedefs->name, "tpdf");
2728 // assert_string_equal(grps->actions->name, "act");
David Sedlákf111bcb2019-07-23 17:15:51 +02002729 assert_string_equal(grps->data->next->next->next->next->next->name, "uses-name");
David Sedlákb7abcfa2019-07-24 12:33:35 +02002730 assert_int_equal(grps->data->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákf111bcb2019-07-23 17:15:51 +02002731 assert_string_equal(grps->data->next->next->next->next->next->next->name, "cont");
David Sedlákb7abcfa2019-07-24 12:33:35 +02002732 assert_int_equal(grps->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
2733 assert_string_equal(grps->data->next->next->next->next->next->next->next->name, "choice");
2734 assert_int_equal(grps->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002735 FREE_ARRAY(st->ctx, grps, lysp_grp_free);
2736 grps = NULL;
2737
2738 /* min subelems */
2739 data = ELEMENT_WRAPPER_START "<grouping name=\"grp-name\" />" ELEMENT_WRAPPER_END;
2740 assert_int_equal(test_element_helper(st, &data, &grp_meta, NULL, NULL, true), LY_SUCCESS);
2741 assert_string_equal(grps->name, "grp-name");
2742 FREE_ARRAY(st->ctx, grps, lysp_grp_free);
2743 grps = NULL;
David Sedlák031b9e72019-07-23 15:19:37 +02002744
2745 st->finished_correctly = true;
2746}
2747
David Sedlákf111bcb2019-07-23 17:15:51 +02002748static void
2749test_container_elem(void **state)
2750{
2751 struct state *st = *state;
2752 const char *data;
2753 struct lysp_node *siblings = NULL;
2754 struct tree_node_meta node_meta = {NULL, &siblings};
2755 struct lysp_node_container *parsed = NULL;
2756
2757 /* max subelems */
David Sedláke2dc9e92019-07-24 09:59:21 +02002758 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2759 data = ELEMENT_WRAPPER_START
2760 "<container name=\"cont-name\">"
2761 "<anydata name=\"anyd\"/>"
2762 "<anyxml name=\"anyx\"/>"
2763 "<config value=\"true\"/>"
2764 "<container name=\"subcont\"/>"
2765 "<description><text>desc</text></description>"
2766 "<grouping name=\"sub-grp\"/>"
2767 "<if-feature name=\"iff\"/>"
2768 "<leaf name=\"leaf\"/>"
2769 "<leaf-list name=\"llist\"/>"
2770 "<list name=\"list\"/>"
2771 "<must condition=\"cond\"/>"
2772 "<notification name=\"notf\"/>"
2773 "<presence value=\"presence\"/>"
2774 "<reference><text>ref</text></reference>"
2775 "<status value=\"current\"/>"
2776 "<typedef name=\"tpdf\"/>"
2777 "<uses name=\"uses-name\"/>"
2778 "<when condition=\"when-cond\"/>"
2779 // "<action name=\"act\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002780 "<choice name=\"choice\"/>"
David Sedláke2dc9e92019-07-24 09:59:21 +02002781 "</container>"
2782 ELEMENT_WRAPPER_END;
2783 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2784 parsed = (struct lysp_node_container *)siblings;
2785 assert_string_equal(parsed->name, "cont-name");
2786 assert_null(parsed->parent);
2787 assert_int_equal(parsed->nodetype, LYS_CONTAINER);
2788 assert_true(parsed->flags & LYS_CONFIG_W);
2789 assert_true(parsed->flags & LYS_STATUS_CURR);
2790 assert_null(parsed->next);
2791 assert_string_equal(parsed->dsc, "desc");
2792 assert_string_equal(parsed->ref, "ref");
2793 assert_string_equal(parsed->when->cond, "when-cond");
2794 assert_string_equal(*parsed->iffeatures, "iff");
2795 assert_null(parsed->exts);
2796 assert_string_equal(parsed->musts->arg, "cond");
2797 assert_string_equal(parsed->presence, "presence");
2798 assert_string_equal(parsed->typedefs->name, "tpdf");
2799 assert_string_equal(parsed->groupings->name, "sub-grp");
2800 assert_string_equal(parsed->child->name, "anyd");
2801 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2802 assert_string_equal(parsed->child->next->name, "anyx");
2803 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2804 assert_string_equal(parsed->child->next->next->name, "subcont");
2805 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
2806 assert_string_equal(parsed->child->next->next->next->name, "leaf");
2807 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
2808 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
2809 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
2810 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
2811 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
2812 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
2813 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002814 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
2815 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2816 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke2dc9e92019-07-24 09:59:21 +02002817 assert_string_equal(parsed->notifs->name, "notf");
2818 //assert_string_equal(parsed->actions->name, "act");
2819 lysp_node_free(st->ctx, siblings);
2820 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
2821 siblings = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02002822
2823 /* min subelems */
2824 data = ELEMENT_WRAPPER_START "<container name=\"cont-name\" />" ELEMENT_WRAPPER_END;
2825 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2826 parsed = (struct lysp_node_container *)siblings;
2827 assert_string_equal(parsed->name, "cont-name");
2828 lysp_node_free(st->ctx, siblings);
2829 siblings = NULL;
2830
2831 st->finished_correctly = true;
2832}
2833
David Sedlák5379d392019-07-24 10:42:03 +02002834static void
2835test_case_elem(void **state)
2836{
2837 struct state *st = *state;
2838 const char *data;
2839 struct lysp_node *siblings = NULL;
2840 struct tree_node_meta node_meta = {NULL, &siblings};
2841 struct lysp_node_case *parsed = NULL;
2842
2843 /* max subelems */
2844 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2845 data = ELEMENT_WRAPPER_START
2846 "<case name=\"case-name\">"
2847 "<anydata name=\"anyd\"/>"
2848 "<anyxml name=\"anyx\"/>"
2849 "<container name=\"subcont\"/>"
2850 "<description><text>desc</text></description>"
2851 "<if-feature name=\"iff\"/>"
2852 "<leaf name=\"leaf\"/>"
2853 "<leaf-list name=\"llist\"/>"
2854 "<list name=\"list\"/>"
2855 "<reference><text>ref</text></reference>"
2856 "<status value=\"current\"/>"
2857 "<uses name=\"uses-name\"/>"
2858 "<when condition=\"when-cond\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002859 "<choice name=\"choice\"/>"
David Sedlák5379d392019-07-24 10:42:03 +02002860 "</case>"
2861 ELEMENT_WRAPPER_END;
2862 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2863 parsed = (struct lysp_node_case *)siblings;
2864 assert_string_equal(parsed->name, "case-name");
2865 assert_null(parsed->parent);
2866 assert_int_equal(parsed->nodetype, LYS_CASE);
2867 assert_true(parsed->flags & LYS_STATUS_CURR);
2868 assert_null(parsed->next);
2869 assert_string_equal(parsed->dsc, "desc");
2870 assert_string_equal(parsed->ref, "ref");
2871 assert_string_equal(parsed->when->cond, "when-cond");
2872 assert_string_equal(*parsed->iffeatures, "iff");
2873 assert_null(parsed->exts);
2874 assert_string_equal(parsed->child->name, "anyd");
2875 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2876 assert_string_equal(parsed->child->next->name, "anyx");
2877 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2878 assert_string_equal(parsed->child->next->next->name, "subcont");
2879 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
2880 assert_string_equal(parsed->child->next->next->next->name, "leaf");
2881 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
2882 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
2883 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
2884 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
2885 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
2886 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
2887 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002888 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
2889 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2890 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedlák5379d392019-07-24 10:42:03 +02002891 lysp_node_free(st->ctx, siblings);
2892 siblings = NULL;
2893
2894 /* min subelems */
2895 data = ELEMENT_WRAPPER_START "<case name=\"case-name\" />" ELEMENT_WRAPPER_END;
2896 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2897 parsed = (struct lysp_node_case *)siblings;
2898 assert_string_equal(parsed->name, "case-name");
2899 lysp_node_free(st->ctx, siblings);
2900 siblings = NULL;
2901
2902 st->finished_correctly = true;
2903}
2904
David Sedlákb7abcfa2019-07-24 12:33:35 +02002905static void
2906test_choice_elem(void **state)
2907{
2908 struct state *st = *state;
2909 const char *data;
2910 struct lysp_node *siblings = NULL;
2911 struct tree_node_meta node_meta = {NULL, &siblings};
2912 struct lysp_node_choice *parsed = NULL;
2913
2914 /* max subelems */
2915 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2916 data = ELEMENT_WRAPPER_START
2917 "<choice name=\"choice-name\">"
2918 "<anydata name=\"anyd\"/>"
2919 "<anyxml name=\"anyx\"/>"
2920 "<case name=\"sub-case\"/>"
2921 "<choice name=\"choice\"/>"
2922 "<config value=\"true\"/>"
2923 "<container name=\"subcont\"/>"
2924 "<default value=\"def\"/>"
2925 "<description><text>desc</text></description>"
2926 "<if-feature name=\"iff\"/>"
2927 "<leaf name=\"leaf\"/>"
2928 "<leaf-list name=\"llist\"/>"
2929 "<list name=\"list\"/>"
2930 "<mandatory value=\"true\" />"
2931 "<reference><text>ref</text></reference>"
2932 "<status value=\"current\"/>"
2933 "<when condition=\"when-cond\"/>"
2934 "</choice>"
2935 ELEMENT_WRAPPER_END;
2936 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2937 parsed = (struct lysp_node_choice *)siblings;
2938 assert_string_equal(parsed->name, "choice-name");
2939 assert_null(parsed->parent);
2940 assert_int_equal(parsed->nodetype, LYS_CHOICE);
2941 assert_true(parsed->flags & LYS_CONFIG_W && parsed->flags & LYS_MAND_TRUE && parsed->flags & LYS_STATUS_CURR);
2942 assert_null(parsed->next);
2943 assert_string_equal(parsed->dsc, "desc");
2944 assert_string_equal(parsed->ref, "ref");
2945 assert_string_equal(parsed->when->cond, "when-cond");
2946 assert_string_equal(*parsed->iffeatures, "iff");
2947 assert_null(parsed->exts);
2948 assert_string_equal(parsed->child->name, "anyd");
2949 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2950 assert_string_equal(parsed->child->next->name, "anyx");
2951 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2952 assert_string_equal(parsed->child->next->next->name, "sub-case");
2953 assert_int_equal(parsed->child->next->next->nodetype, LYS_CASE);
2954 assert_string_equal(parsed->child->next->next->next->name, "choice");
2955 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
2956 assert_string_equal(parsed->child->next->next->next->next->name, "subcont");
2957 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_CONTAINER);
2958 assert_string_equal(parsed->child->next->next->next->next->next->name, "leaf");
2959 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAF);
2960 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "llist");
2961 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2962 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "list");
2963 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
2964 assert_null(parsed->child->next->next->next->next->next->next->next->next);
2965 lysp_node_free(st->ctx, siblings);
2966 siblings = NULL;
2967
2968 /* min subelems */
2969 data = ELEMENT_WRAPPER_START "<choice name=\"choice-name\" />" ELEMENT_WRAPPER_END;
2970 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2971 parsed = (struct lysp_node_choice *)siblings;
2972 assert_string_equal(parsed->name, "choice-name");
2973 lysp_node_free(st->ctx, siblings);
2974 siblings = NULL;
2975
2976 st->finished_correctly = true;
2977}
2978
David Sedlák05404f62019-07-24 14:11:53 +02002979static void
2980test_inout_elem(void **state)
2981{
2982 struct state *st = *state;
2983 const char *data;
2984 struct lysp_action_inout inout = {};
2985 struct inout_meta inout_meta = {NULL, &inout};
2986
2987 /* max subelements */
2988 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2989 data = ELEMENT_WRAPPER_START
2990 "<input>"
2991 "<anydata name=\"anyd\"/>"
2992 "<anyxml name=\"anyx\"/>"
2993 "<choice name=\"choice\"/>"
2994 "<container name=\"subcont\"/>"
2995 "<grouping name=\"sub-grp\"/>"
2996 "<leaf name=\"leaf\"/>"
2997 "<leaf-list name=\"llist\"/>"
2998 "<list name=\"list\"/>"
2999 "<must condition=\"cond\"/>"
3000 "<typedef name=\"tpdf\"/>"
3001 "<uses name=\"uses-name\"/>"
3002 "</input>"
3003 ELEMENT_WRAPPER_END;
3004 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3005 assert_null(inout.parent);
3006 assert_int_equal(inout.nodetype, LYS_INPUT);
3007 assert_string_equal(inout.musts->arg, "cond");
3008 assert_string_equal(inout.typedefs->name, "tpdf");
3009 assert_string_equal(inout.groupings->name, "sub-grp");
3010 assert_string_equal(inout.data->name, "anyd");
3011 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
3012 assert_string_equal(inout.data->next->name, "anyx");
3013 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
3014 assert_string_equal(inout.data->next->next->name, "choice");
3015 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
3016 assert_string_equal(inout.data->next->next->next->name, "subcont");
3017 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
3018 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
3019 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
3020 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
3021 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3022 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
3023 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
3024 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
3025 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3026 assert_null(inout.data->next->next->next->next->next->next->next->next);
3027 lysp_action_inout_free(st->ctx, &inout);
3028 memset(&inout, 0, sizeof inout);
3029
3030 /* max subelements */
3031 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3032 data = ELEMENT_WRAPPER_START
3033 "<output>"
3034 "<anydata name=\"anyd\"/>"
3035 "<anyxml name=\"anyx\"/>"
3036 "<choice name=\"choice\"/>"
3037 "<container name=\"subcont\"/>"
3038 "<grouping name=\"sub-grp\"/>"
3039 "<leaf name=\"leaf\"/>"
3040 "<leaf-list name=\"llist\"/>"
3041 "<list name=\"list\"/>"
3042 "<must condition=\"cond\"/>"
3043 "<typedef name=\"tpdf\"/>"
3044 "<uses name=\"uses-name\"/>"
3045 "</output>"
3046 ELEMENT_WRAPPER_END;
3047 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3048 assert_null(inout.parent);
3049 assert_int_equal(inout.nodetype, LYS_OUTPUT);
3050 assert_string_equal(inout.musts->arg, "cond");
3051 assert_string_equal(inout.typedefs->name, "tpdf");
3052 assert_string_equal(inout.groupings->name, "sub-grp");
3053 assert_string_equal(inout.data->name, "anyd");
3054 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
3055 assert_string_equal(inout.data->next->name, "anyx");
3056 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
3057 assert_string_equal(inout.data->next->next->name, "choice");
3058 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
3059 assert_string_equal(inout.data->next->next->next->name, "subcont");
3060 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
3061 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
3062 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
3063 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
3064 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3065 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
3066 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
3067 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
3068 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3069 assert_null(inout.data->next->next->next->next->next->next->next->next);
3070 lysp_action_inout_free(st->ctx, &inout);
3071 memset(&inout, 0, sizeof inout);
3072
3073 /* min subelems */
3074 data = ELEMENT_WRAPPER_START "<input />" ELEMENT_WRAPPER_END;
3075 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3076 memset(&inout, 0, sizeof inout);
3077
3078 data = ELEMENT_WRAPPER_START "<output />" ELEMENT_WRAPPER_END;
3079 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3080 memset(&inout, 0, sizeof inout);
3081
3082 /* invalid combinations */
3083 data = ELEMENT_WRAPPER_START "<input name=\"test\"/>" ELEMENT_WRAPPER_END;
3084 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, false), LY_EVALID);
3085 logbuf_assert("Unexpected attribute \"name\" of input element. Line number 1.");
3086 memset(&inout, 0, sizeof inout);
3087
3088 st->finished_correctly = true;
3089}
3090
David Sedlák3b4db242018-10-19 16:11:01 +02003091int
3092main(void)
3093{
3094
3095 const struct CMUnitTest tests[] = {
David Sedlák392af4f2019-06-04 16:02:42 +02003096 cmocka_unit_test_setup_teardown(test_yin_parse_module, setup_f, teardown_f),
David Sedlák8f7a1172019-06-20 14:42:18 +02003097 cmocka_unit_test_setup_teardown(test_yin_match_keyword, setup_f, teardown_f),
David Sedlákb1a78352019-06-28 16:16:29 +02003098 cmocka_unit_test_setup_teardown(test_yin_parse_element_generic, setup_f, teardown_f),
3099 cmocka_unit_test_setup_teardown(test_yin_parse_extension_instance, setup_f, teardown_f),
David Sedlák555c7202019-07-04 12:14:12 +02003100 cmocka_unit_test_setup_teardown(test_yin_parse_content, setup_f, teardown_f),
David Sedlák4a650532019-07-10 11:55:18 +02003101 cmocka_unit_test_setup_teardown(test_validate_value, setup_f, teardown_f),
David Sedlák32488102019-07-15 17:44:10 +02003102
David Sedlák8e7bda82019-07-16 17:57:50 +02003103 cmocka_unit_test(test_yin_match_argument_name),
David Sedlák32488102019-07-15 17:44:10 +02003104 cmocka_unit_test_setup_teardown(test_enum_bit_elem, setup_element_test, teardown_element_test),
3105 cmocka_unit_test_setup_teardown(test_meta_elem, setup_element_test, teardown_element_test),
3106 cmocka_unit_test_setup_teardown(test_import_elem, setup_element_test, teardown_element_test),
3107 cmocka_unit_test_setup_teardown(test_status_elem, setup_element_test, teardown_element_test),
3108 cmocka_unit_test_setup_teardown(test_ext_elem, setup_element_test, teardown_element_test),
3109 cmocka_unit_test_setup_teardown(test_yin_element_elem, setup_element_test, teardown_element_test),
3110 cmocka_unit_test_setup_teardown(test_yangversion_elem, setup_element_test, teardown_element_test),
3111 cmocka_unit_test_setup_teardown(test_mandatory_elem, setup_element_test, teardown_element_test),
David Sedlák8e7bda82019-07-16 17:57:50 +02003112 cmocka_unit_test_setup_teardown(test_argument_elem, setup_element_test, teardown_element_test),
3113 cmocka_unit_test_setup_teardown(test_base_elem, setup_element_test, teardown_element_test),
3114 cmocka_unit_test_setup_teardown(test_belongsto_elem, setup_element_test, teardown_element_test),
3115 cmocka_unit_test_setup_teardown(test_config_elem, setup_element_test, teardown_element_test),
3116 cmocka_unit_test_setup_teardown(test_default_elem, setup_element_test, teardown_element_test),
3117 cmocka_unit_test_setup_teardown(test_err_app_tag_elem, setup_element_test, teardown_element_test),
3118 cmocka_unit_test_setup_teardown(test_err_msg_elem, setup_element_test, teardown_element_test),
3119 cmocka_unit_test_setup_teardown(test_fracdigits_elem, setup_element_test, teardown_element_test),
3120 cmocka_unit_test_setup_teardown(test_iffeature_elem, setup_element_test, teardown_element_test),
3121 cmocka_unit_test_setup_teardown(test_length_elem, setup_element_test, teardown_element_test),
3122 cmocka_unit_test_setup_teardown(test_modifier_elem, setup_element_test, teardown_element_test),
3123 cmocka_unit_test_setup_teardown(test_namespace_elem, setup_element_test, teardown_element_test),
3124 cmocka_unit_test_setup_teardown(test_path_elem, setup_element_test, teardown_element_test),
3125 cmocka_unit_test_setup_teardown(test_pattern_elem, setup_element_test, teardown_element_test),
3126 cmocka_unit_test_setup_teardown(test_value_position_elem, setup_element_test, teardown_element_test),
David Sedlák69f01612019-07-17 11:41:08 +02003127 cmocka_unit_test_setup_teardown(test_prefix_elem, setup_element_test, teardown_element_test),
3128 cmocka_unit_test_setup_teardown(test_range_elem, setup_element_test, teardown_element_test),
3129 cmocka_unit_test_setup_teardown(test_reqinstance_elem, setup_element_test, teardown_element_test),
3130 cmocka_unit_test_setup_teardown(test_revision_date_elem, setup_element_test, teardown_element_test),
3131 cmocka_unit_test_setup_teardown(test_unique_elem, setup_element_test, teardown_element_test),
3132 cmocka_unit_test_setup_teardown(test_units_elem, setup_element_test, teardown_element_test),
3133 cmocka_unit_test_setup_teardown(test_when_elem, setup_element_test, teardown_element_test),
3134 cmocka_unit_test_setup_teardown(test_yin_text_value_elem, setup_element_test, teardown_element_test),
David Sedlák374d2b32019-07-17 15:06:55 +02003135 cmocka_unit_test_setup_teardown(test_type_elem, setup_element_test, teardown_element_test),
David Sedlák1af868e2019-07-17 17:03:14 +02003136 cmocka_unit_test_setup_teardown(test_max_elems_elem, setup_element_test, teardown_element_test),
David Sedlák09e18c92019-07-18 11:17:11 +02003137 cmocka_unit_test_setup_teardown(test_min_elems_elem, setup_element_test, teardown_element_test),
David Sedláka2dad212019-07-18 12:45:19 +02003138 cmocka_unit_test_setup_teardown(test_ordby_elem, setup_element_test, teardown_element_test),
David Sedlák8a83bbb2019-07-18 14:46:00 +02003139 cmocka_unit_test_setup_teardown(test_any_elem, setup_element_test, teardown_element_test),
David Sedlák203ca3a2019-07-18 15:26:25 +02003140 cmocka_unit_test_setup_teardown(test_leaf_elem, setup_element_test, teardown_element_test),
David Sedlákc3da3ef2019-07-19 12:56:08 +02003141 cmocka_unit_test_setup_teardown(test_leaf_list_elem, setup_element_test, teardown_element_test),
David Sedlákcb39f642019-07-19 13:19:55 +02003142 cmocka_unit_test_setup_teardown(test_presence_elem, setup_element_test, teardown_element_test),
David Sedlák12470a82019-07-19 13:44:36 +02003143 cmocka_unit_test_setup_teardown(test_key_elem, setup_element_test, teardown_element_test),
David Sedlák04e17b22019-07-19 15:29:48 +02003144 cmocka_unit_test_setup_teardown(test_typedef_elem, setup_element_test, teardown_element_test),
David Sedlákd2d676a2019-07-22 11:28:19 +02003145 cmocka_unit_test_setup_teardown(test_refine_elem, setup_element_test, teardown_element_test),
David Sedlák0d6de5a2019-07-22 13:25:44 +02003146 cmocka_unit_test_setup_teardown(test_uses_elem, setup_element_test, teardown_element_test),
David Sedlákaa854b02019-07-22 14:17:10 +02003147 cmocka_unit_test_setup_teardown(test_revision_elem, setup_element_test, teardown_element_test),
David Sedlák0c2bab92019-07-22 15:33:19 +02003148 cmocka_unit_test_setup_teardown(test_include_elem, setup_element_test, teardown_element_test),
David Sedlák5e13dea2019-07-22 16:06:45 +02003149 cmocka_unit_test_setup_teardown(test_feature_elem, setup_element_test, teardown_element_test),
David Sedlák28794f22019-07-22 16:45:00 +02003150 cmocka_unit_test_setup_teardown(test_identity_elem, setup_element_test, teardown_element_test),
David Sedlákaf536aa2019-07-23 13:42:23 +02003151 cmocka_unit_test_setup_teardown(test_list_elem, setup_element_test, teardown_element_test),
David Sedlák031b9e72019-07-23 15:19:37 +02003152 cmocka_unit_test_setup_teardown(test_notification_elem, setup_element_test, teardown_element_test),
David Sedláke3ce9ef2019-07-23 16:34:30 +02003153 cmocka_unit_test_setup_teardown(test_grouping_elem, setup_element_test, teardown_element_test),
David Sedlákf111bcb2019-07-23 17:15:51 +02003154 cmocka_unit_test_setup_teardown(test_container_elem, setup_element_test, teardown_element_test),
David Sedlák5379d392019-07-24 10:42:03 +02003155 cmocka_unit_test_setup_teardown(test_case_elem, setup_element_test, teardown_element_test),
David Sedlákb7abcfa2019-07-24 12:33:35 +02003156 cmocka_unit_test_setup_teardown(test_choice_elem, setup_element_test, teardown_element_test),
David Sedlák05404f62019-07-24 14:11:53 +02003157 cmocka_unit_test_setup_teardown(test_inout_elem, setup_element_test, teardown_element_test),
David Sedlák3b4db242018-10-19 16:11:01 +02003158 };
3159
David Sedlák8e7bda82019-07-16 17:57:50 +02003160 return cmocka_run_group_tests(tests, setup_ly_ctx, destroy_ly_ctx);
David Sedlák3b4db242018-10-19 16:11:01 +02003161}