blob: 61537921caee233853db1863b82ef6a190ff847f [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ák85d0eca2019-07-24 15:15:21 +020045void lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action);
David Sedlák992fb7c2019-07-24 16:51:01 +020046void lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment);
David Sedlák4ffcec82019-07-25 15:10:21 +020047void lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d);
David Sedlák555c7202019-07-04 12:14:12 +020048
David Sedlák68a1af12019-03-08 13:46:54 +010049struct state {
David Sedlák3b4db242018-10-19 16:11:01 +020050 struct ly_ctx *ctx;
David Sedlák3017da42019-02-15 09:48:04 +010051 struct lys_module *mod;
David Sedlák619db942019-07-03 14:47:30 +020052 struct lysp_module *lysp_mod;
David Sedlákda8ffa32019-07-08 14:17:10 +020053 struct yin_parser_ctx *yin_ctx;
David Sedlák79e50cb2019-06-05 16:33:09 +020054 bool finished_correctly;
David Sedlák68a1af12019-03-08 13:46:54 +010055};
David Sedlák872c7b42018-10-26 13:15:20 +020056
David Sedlák79e50cb2019-06-05 16:33:09 +020057#define BUFSIZE 1024
58char logbuf[BUFSIZE] = {0};
59int store = -1; /* negative for infinite logging, positive for limited logging */
60
61/* set to 0 to printing error messages to stderr instead of checking them in code */
David Sedlák4ffcec82019-07-25 15:10:21 +020062#define ENABLE_LOGGER_CHECKING 1
David Sedlák79e50cb2019-06-05 16:33:09 +020063
64#if ENABLE_LOGGER_CHECKING
65static void
66logger(LY_LOG_LEVEL level, const char *msg, const char *path)
67{
68 (void) level; /* unused */
69 if (store) {
70 if (path && path[0]) {
71 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
72 } else {
73 strncpy(logbuf, msg, BUFSIZE - 1);
74 }
75 if (store > 0) {
76 --store;
77 }
78 }
79}
80#endif
81
82#if ENABLE_LOGGER_CHECKING
83# define logbuf_assert(str) assert_string_equal(logbuf, str)
84#else
85# define logbuf_assert(str)
86#endif
87
88#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
89 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
90 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
91 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
92 CLEANUP
93
David Sedlák8e7bda82019-07-16 17:57:50 +020094int
95setup_ly_ctx(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +010096{
97 struct state *st = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +020098
David Sedlák68a1af12019-03-08 13:46:54 +010099 /* allocate state variable */
100 (*state) = st = calloc(1, sizeof(*st));
101 if (!st) {
102 fprintf(stderr, "Memmory allocation failed");
103 return EXIT_FAILURE;
104 }
David Sedlák872c7b42018-10-26 13:15:20 +0200105
David Sedlák68a1af12019-03-08 13:46:54 +0100106 /* create new libyang context */
107 ly_ctx_new(NULL, 0, &st->ctx);
David Sedlák872c7b42018-10-26 13:15:20 +0200108
David Sedlák8e7bda82019-07-16 17:57:50 +0200109 return EXIT_SUCCESS;
110}
111
112int
113destroy_ly_ctx(void **state)
114{
115 struct state *st = *state;
116 ly_ctx_destroy(st->ctx, NULL);
117 free(st);
118
119 return EXIT_SUCCESS;
120}
121
122static int
123setup_f(void **state)
124{
125 struct state *st = *state;
126
127#if ENABLE_LOGGER_CHECKING
128 /* setup logger */
129 ly_set_log_clb(logger, 1);
130#endif
131
David Sedlák68a1af12019-03-08 13:46:54 +0100132 /* allocate new module */
133 st->mod = calloc(1, sizeof(*st->mod));
134 st->mod->ctx = st->ctx;
135
David Sedlák619db942019-07-03 14:47:30 +0200136 /* allocate new parsed module */
137 st->lysp_mod = calloc(1, sizeof(*st->lysp_mod));
138 st->lysp_mod->mod = calloc(1, sizeof(*st->lysp_mod->mod));
139 st->lysp_mod->mod->ctx = st->ctx;
140
141 /* allocate parser context */
David Sedlákda8ffa32019-07-08 14:17:10 +0200142 st->yin_ctx = calloc(1, sizeof(*st->yin_ctx));
143 st->yin_ctx->xml_ctx.ctx = st->ctx;
144 st->yin_ctx->xml_ctx.line = 1;
David Sedlák8f5bce02019-06-03 16:41:08 +0200145
David Sedlák68a1af12019-03-08 13:46:54 +0100146 return EXIT_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +0200147}
148
149static int
David Sedlák68a1af12019-03-08 13:46:54 +0100150teardown_f(void **state)
151{
152 struct state *st = *(struct state **)state;
David Sedlák619db942019-07-03 14:47:30 +0200153 struct lys_module *temp;
David Sedlák68a1af12019-03-08 13:46:54 +0100154
David Sedlák79e50cb2019-06-05 16:33:09 +0200155#if ENABLE_LOGGER_CHECKING
156 /* teardown logger */
157 if (!st->finished_correctly && logbuf[0] != '\0') {
158 fprintf(stderr, "%s\n", logbuf);
159 }
160#endif
161
David Sedlák619db942019-07-03 14:47:30 +0200162 temp = st->lysp_mod->mod;
163
David Sedlákda8ffa32019-07-08 14:17:10 +0200164 lyxml_context_clear(&st->yin_ctx->xml_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100165 lys_module_free(st->mod, NULL);
David Sedlák619db942019-07-03 14:47:30 +0200166 lysp_module_free(st->lysp_mod);
167 lys_module_free(temp, NULL);
David Sedlákda8ffa32019-07-08 14:17:10 +0200168 free(st->yin_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100169
170 return EXIT_SUCCESS;
171}
172
David Sedlák392af4f2019-06-04 16:02:42 +0200173static struct state*
174reset_state(void **state)
175{
David Sedlák79e50cb2019-06-05 16:33:09 +0200176 ((struct state *)*state)->finished_correctly = true;
David Sedlák555c7202019-07-04 12:14:12 +0200177 logbuf[0] = '\0';
David Sedlák392af4f2019-06-04 16:02:42 +0200178 teardown_f(state);
179 setup_f(state);
180
181 return *state;
182}
183
David Sedlák79e50cb2019-06-05 16:33:09 +0200184void
185logbuf_clean(void)
186{
187 logbuf[0] = '\0';
188}
189
David Sedlák68a1af12019-03-08 13:46:54 +0100190static void
David Sedlák392af4f2019-06-04 16:02:42 +0200191test_yin_parse_module(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +0100192{
193 LY_ERR ret = LY_SUCCESS;
194 struct state *st = *state;
195
196 ret = yin_parse_module(st->ctx,
David Sedlák2b214ac2019-06-06 16:11:03 +0200197 "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\
198 name=\"example-foo\"\
David Sedlák18730132019-03-15 15:51:34 +0100199 xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\
200 xmlns:foo=\"urn:example:foo\"\
201 xmlns:myext=\"urn:example:extensions\">\
David Sedlákcd0c9512019-03-29 13:23:06 +0100202 <namespace uri=\"urn:example:foo\" xmlns:myext=\"urn:example:extensions\"/>\
David Sedláka7406952019-04-05 10:33:07 +0200203 <prefix xmlns:myxt=\"urn:emple:extensions\" value=\"foo\" xmlns:myext=\"urn:example:extensions\"/>\
David Sedlákd9d3a312019-06-04 09:47:10 +0200204 </module>",
David Sedlák68a1af12019-03-08 13:46:54 +0100205 st->mod);
206
207 assert_int_equal(ret, LY_SUCCESS);
208 assert_string_equal(st->mod->parsed->mod->name, "example-foo");
209 assert_string_equal(st->mod->parsed->mod->prefix, "foo");
David Sedlákcd0c9512019-03-29 13:23:06 +0100210 assert_string_equal(st->mod->parsed->mod->ns, "urn:example:foo");
David Sedlák392af4f2019-06-04 16:02:42 +0200211
212 st = reset_state(state);
213 ret = yin_parse_module(st->ctx,
David Sedlák2b214ac2019-06-06 16:11:03 +0200214 "<module name=\"example-foo\">\
215 <invalid-tag uri=\"urn:example:foo\"\"/>\
216 </module>",
217 st->mod);
David Sedlák392af4f2019-06-04 16:02:42 +0200218 assert_int_equal(ret, LY_EVALID);
219
220 st = reset_state(state);
221 ret = yin_parse_module(st->ctx,
David Sedlák8f7a1172019-06-20 14:42:18 +0200222 "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\
David Sedlák57715b12019-06-17 13:05:22 +0200223 </module>",
David Sedlák2b214ac2019-06-06 16:11:03 +0200224 st->mod);
David Sedlák392af4f2019-06-04 16:02:42 +0200225 assert_int_equal(ret, LY_EVALID);
David Sedlák882a8072019-07-08 17:51:20 +0200226 logbuf_assert("Missing mandatory attribute name of module element. Line number 1.");
David Sedlák392af4f2019-06-04 16:02:42 +0200227
228 st = reset_state(state);
229 ret = yin_parse_module(st->ctx,
230 "",
231 st->mod);
232 assert_int_equal(ret, LY_EVALID);
David Sedlák79e50cb2019-06-05 16:33:09 +0200233 logbuf_assert("Invalid keyword \"(null)\", expected \"module\" or \"submodule\". Line number 1.");
234 st->finished_correctly = true;
David Sedlák3b4db242018-10-19 16:11:01 +0200235}
236
237static void
David Sedlák1bccdfa2019-06-17 15:55:27 +0200238test_yin_match_keyword(void **state)
David Sedlák3b4db242018-10-19 16:11:01 +0200239{
David Sedlák8f7a1172019-06-20 14:42:18 +0200240 struct state *st = *state;
David Sedlák3b4db242018-10-19 16:11:01 +0200241
David Sedlák8f7a1172019-06-20 14:42:18 +0200242 const char *prefix, *name;
243 struct yin_arg_record *args = NULL;
244 size_t prefix_len, name_len;
245 /* create mock yin namespace in xml context */
246 const char *data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" />";
David Sedlákda8ffa32019-07-08 14:17:10 +0200247 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
248 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák8f7a1172019-06-20 14:42:18 +0200249 LY_ARRAY_FREE(args);
250
David Sedlákc1771b12019-07-10 15:55:46 +0200251 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydatax", strlen("anydatax"), prefix, prefix_len, YANG_NONE), YANG_NONE);
252 assert_int_equal(yin_match_keyword(st->yin_ctx, "asdasd", strlen("asdasd"), prefix, prefix_len, YANG_NONE), YANG_NONE);
253 assert_int_equal(yin_match_keyword(st->yin_ctx, "", 0, prefix, prefix_len, YANG_NONE), YANG_NONE);
254 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydata", strlen("anydata"), prefix, prefix_len, YANG_NONE), YANG_ANYDATA);
255 assert_int_equal(yin_match_keyword(st->yin_ctx, "anyxml", strlen("anyxml"), prefix, prefix_len, YANG_NONE), YANG_ANYXML);
256 assert_int_equal(yin_match_keyword(st->yin_ctx, "argument", strlen("argument"), prefix, prefix_len, YANG_NONE), YANG_ARGUMENT);
257 assert_int_equal(yin_match_keyword(st->yin_ctx, "augment", strlen("augment"), prefix, prefix_len, YANG_NONE), YANG_AUGMENT);
258 assert_int_equal(yin_match_keyword(st->yin_ctx, "base", strlen("base"), prefix, prefix_len, YANG_NONE), YANG_BASE);
259 assert_int_equal(yin_match_keyword(st->yin_ctx, "belongs-to", strlen("belongs-to"), prefix, prefix_len, YANG_NONE), YANG_BELONGS_TO);
260 assert_int_equal(yin_match_keyword(st->yin_ctx, "bit", strlen("bit"), prefix, prefix_len, YANG_NONE), YANG_BIT);
261 assert_int_equal(yin_match_keyword(st->yin_ctx, "case", strlen("case"), prefix, prefix_len, YANG_NONE), YANG_CASE);
262 assert_int_equal(yin_match_keyword(st->yin_ctx, "choice", strlen("choice"), prefix, prefix_len, YANG_NONE), YANG_CHOICE);
263 assert_int_equal(yin_match_keyword(st->yin_ctx, "config", strlen("config"), prefix, prefix_len, YANG_NONE), YANG_CONFIG);
264 assert_int_equal(yin_match_keyword(st->yin_ctx, "contact", strlen("contact"), prefix, prefix_len, YANG_NONE), YANG_CONTACT);
265 assert_int_equal(yin_match_keyword(st->yin_ctx, "container", strlen("container"), prefix, prefix_len, YANG_NONE), YANG_CONTAINER);
266 assert_int_equal(yin_match_keyword(st->yin_ctx, "default", strlen("default"), prefix, prefix_len, YANG_NONE), YANG_DEFAULT);
267 assert_int_equal(yin_match_keyword(st->yin_ctx, "description", strlen("description"), prefix, prefix_len, YANG_NONE), YANG_DESCRIPTION);
268 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviate", strlen("deviate"), prefix, prefix_len, YANG_NONE), YANG_DEVIATE);
269 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviation", strlen("deviation"), prefix, prefix_len, YANG_NONE), YANG_DEVIATION);
270 assert_int_equal(yin_match_keyword(st->yin_ctx, "enum", strlen("enum"), prefix, prefix_len, YANG_NONE), YANG_ENUM);
271 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);
272 assert_int_equal(yin_match_keyword(st->yin_ctx, "error-message", strlen("error-message"), prefix, prefix_len, YANG_NONE), YANG_ERROR_MESSAGE);
273 assert_int_equal(yin_match_keyword(st->yin_ctx, "extension", strlen("extension"), prefix, prefix_len, YANG_NONE), YANG_EXTENSION);
274 assert_int_equal(yin_match_keyword(st->yin_ctx, "feature", strlen("feature"), prefix, prefix_len, YANG_NONE), YANG_FEATURE);
275 assert_int_equal(yin_match_keyword(st->yin_ctx, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len, YANG_NONE), YANG_FRACTION_DIGITS);
276 assert_int_equal(yin_match_keyword(st->yin_ctx, "grouping", strlen("grouping"), prefix, prefix_len, YANG_NONE), YANG_GROUPING);
277 assert_int_equal(yin_match_keyword(st->yin_ctx, "identity", strlen("identity"), prefix, prefix_len, YANG_NONE), YANG_IDENTITY);
278 assert_int_equal(yin_match_keyword(st->yin_ctx, "if-feature", strlen("if-feature"), prefix, prefix_len, YANG_NONE), YANG_IF_FEATURE);
279 assert_int_equal(yin_match_keyword(st->yin_ctx, "import", strlen("import"), prefix, prefix_len, YANG_NONE), YANG_IMPORT);
280 assert_int_equal(yin_match_keyword(st->yin_ctx, "include", strlen("include"), prefix, prefix_len, YANG_NONE), YANG_INCLUDE);
281 assert_int_equal(yin_match_keyword(st->yin_ctx, "input", strlen("input"), prefix, prefix_len, YANG_NONE), YANG_INPUT);
282 assert_int_equal(yin_match_keyword(st->yin_ctx, "key", strlen("key"), prefix, prefix_len, YANG_NONE), YANG_KEY);
283 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf", strlen("leaf"), prefix, prefix_len, YANG_NONE), YANG_LEAF);
284 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf-list", strlen("leaf-list"), prefix, prefix_len, YANG_NONE), YANG_LEAF_LIST);
285 assert_int_equal(yin_match_keyword(st->yin_ctx, "length", strlen("length"), prefix, prefix_len, YANG_NONE), YANG_LENGTH);
286 assert_int_equal(yin_match_keyword(st->yin_ctx, "list", strlen("list"), prefix, prefix_len, YANG_NONE), YANG_LIST);
287 assert_int_equal(yin_match_keyword(st->yin_ctx, "mandatory", strlen("mandatory"), prefix, prefix_len, YANG_NONE), YANG_MANDATORY);
288 assert_int_equal(yin_match_keyword(st->yin_ctx, "max-elements", strlen("max-elements"), prefix, prefix_len, YANG_NONE), YANG_MAX_ELEMENTS);
289 assert_int_equal(yin_match_keyword(st->yin_ctx, "min-elements", strlen("min-elements"), prefix, prefix_len, YANG_NONE), YANG_MIN_ELEMENTS);
290 assert_int_equal(yin_match_keyword(st->yin_ctx, "modifier", strlen("modifier"), prefix, prefix_len, YANG_NONE), YANG_MODIFIER);
291 assert_int_equal(yin_match_keyword(st->yin_ctx, "module", strlen("module"), prefix, prefix_len, YANG_NONE), YANG_MODULE);
292 assert_int_equal(yin_match_keyword(st->yin_ctx, "must", strlen("must"), prefix, prefix_len, YANG_NONE), YANG_MUST);
293 assert_int_equal(yin_match_keyword(st->yin_ctx, "namespace", strlen("namespace"), prefix, prefix_len, YANG_NONE), YANG_NAMESPACE);
294 assert_int_equal(yin_match_keyword(st->yin_ctx, "notification", strlen("notification"), prefix, prefix_len, YANG_NONE), YANG_NOTIFICATION);
295 assert_int_equal(yin_match_keyword(st->yin_ctx, "ordered-by", strlen("ordered-by"), prefix, prefix_len, YANG_NONE), YANG_ORDERED_BY);
296 assert_int_equal(yin_match_keyword(st->yin_ctx, "organization", strlen("organization"), prefix, prefix_len, YANG_NONE), YANG_ORGANIZATION);
297 assert_int_equal(yin_match_keyword(st->yin_ctx, "output", strlen("output"), prefix, prefix_len, YANG_NONE), YANG_OUTPUT);
298 assert_int_equal(yin_match_keyword(st->yin_ctx, "path", strlen("path"), prefix, prefix_len, YANG_NONE), YANG_PATH);
299 assert_int_equal(yin_match_keyword(st->yin_ctx, "pattern", strlen("pattern"), prefix, prefix_len, YANG_NONE), YANG_PATTERN);
300 assert_int_equal(yin_match_keyword(st->yin_ctx, "position", strlen("position"), prefix, prefix_len, YANG_NONE), YANG_POSITION);
301 assert_int_equal(yin_match_keyword(st->yin_ctx, "prefix", strlen("prefix"), prefix, prefix_len, YANG_NONE), YANG_PREFIX);
302 assert_int_equal(yin_match_keyword(st->yin_ctx, "presence", strlen("presence"), prefix, prefix_len, YANG_NONE), YANG_PRESENCE);
303 assert_int_equal(yin_match_keyword(st->yin_ctx, "range", strlen("range"), prefix, prefix_len, YANG_NONE), YANG_RANGE);
304 assert_int_equal(yin_match_keyword(st->yin_ctx, "reference", strlen("reference"), prefix, prefix_len, YANG_NONE), YANG_REFERENCE);
305 assert_int_equal(yin_match_keyword(st->yin_ctx, "refine", strlen("refine"), prefix, prefix_len, YANG_NONE), YANG_REFINE);
306 assert_int_equal(yin_match_keyword(st->yin_ctx, "require-instance", strlen("require-instance"), prefix, prefix_len, YANG_NONE), YANG_REQUIRE_INSTANCE);
307 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision", strlen("revision"), prefix, prefix_len, YANG_NONE), YANG_REVISION);
308 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision-date", strlen("revision-date"), prefix, prefix_len, YANG_NONE), YANG_REVISION_DATE);
309 assert_int_equal(yin_match_keyword(st->yin_ctx, "rpc", strlen("rpc"), prefix, prefix_len, YANG_NONE), YANG_RPC);
310 assert_int_equal(yin_match_keyword(st->yin_ctx, "status", strlen("status"), prefix, prefix_len, YANG_NONE), YANG_STATUS);
311 assert_int_equal(yin_match_keyword(st->yin_ctx, "submodule", strlen("submodule"), prefix, prefix_len, YANG_NONE), YANG_SUBMODULE);
312 assert_int_equal(yin_match_keyword(st->yin_ctx, "type", strlen("type"), prefix, prefix_len, YANG_NONE), YANG_TYPE);
313 assert_int_equal(yin_match_keyword(st->yin_ctx, "typedef", strlen("typedef"), prefix, prefix_len, YANG_NONE), YANG_TYPEDEF);
314 assert_int_equal(yin_match_keyword(st->yin_ctx, "unique", strlen("unique"), prefix, prefix_len, YANG_NONE), YANG_UNIQUE);
315 assert_int_equal(yin_match_keyword(st->yin_ctx, "units", strlen("units"), prefix, prefix_len, YANG_NONE), YANG_UNITS);
316 assert_int_equal(yin_match_keyword(st->yin_ctx, "uses", strlen("uses"), prefix, prefix_len, YANG_NONE), YANG_USES);
317 assert_int_equal(yin_match_keyword(st->yin_ctx, "value", strlen("value"), prefix, prefix_len, YANG_NONE), YANG_VALUE);
318 assert_int_equal(yin_match_keyword(st->yin_ctx, "when", strlen("when"), prefix, prefix_len, YANG_NONE), YANG_WHEN);
319 assert_int_equal(yin_match_keyword(st->yin_ctx, "yang-version", strlen("yang-version"), prefix, prefix_len, YANG_NONE), YANG_YANG_VERSION);
320 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 +0200321
322 st->finished_correctly = true;
David Sedlák872c7b42018-10-26 13:15:20 +0200323}
David Sedlák3b4db242018-10-19 16:11:01 +0200324
David Sedlák872c7b42018-10-26 13:15:20 +0200325static void
David Sedlák060b00e2019-06-19 11:12:06 +0200326test_yin_match_argument_name(void **state)
David Sedlák872c7b42018-10-26 13:15:20 +0200327{
David Sedlák68a1af12019-03-08 13:46:54 +0100328 (void)state; /* unused */
David Sedlák872c7b42018-10-26 13:15:20 +0200329
David Sedlák060b00e2019-06-19 11:12:06 +0200330 assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN);
331 assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN);
332 assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN);
333 assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION);
334 assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE);
335 assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE);
336 assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME);
337 assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG);
338 assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
339 assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT);
340 assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI);
341 assert_int_equal(yin_match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200342}
343
David Sedlák68a1af12019-03-08 13:46:54 +0100344static void
David Sedlákb1a78352019-06-28 16:16:29 +0200345test_yin_parse_element_generic(void **state)
346{
347 const char *prefix, *name;
348 struct state *st = *state;
349 struct lysp_ext_instance exts;
350 size_t prefix_len, name_len;
351 LY_ERR ret;
352
353 memset(&exts, 0, sizeof(exts));
354
355 const char *data = "<elem attr=\"value\">text_value</elem>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200356 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák4ffcec82019-07-25 15:10:21 +0200357 ret = yin_parse_element_generic(st->yin_ctx, name, name_len, &data, &exts.child);
David Sedlákb1a78352019-06-28 16:16:29 +0200358 assert_int_equal(ret, LY_SUCCESS);
David Sedlák1fdb2522019-07-09 16:22:57 +0200359 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákb1a78352019-06-28 16:16:29 +0200360 assert_string_equal(exts.child->stmt, "elem");
361 assert_string_equal(exts.child->arg, "text_value");
David Sedlákb1a78352019-06-28 16:16:29 +0200362 assert_string_equal(exts.child->child->stmt, "attr");
363 assert_string_equal(exts.child->child->arg, "value");
364 assert_true(exts.child->child->flags & LYS_YIN_ATTR);
David Sedlákb1a78352019-06-28 16:16:29 +0200365 lysp_ext_instance_free(st->ctx, &exts);
David Sedlák5392a212019-07-01 09:19:10 +0200366 st = reset_state(state);
367
368 data = "<elem></elem>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200369 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák4ffcec82019-07-25 15:10:21 +0200370 ret = yin_parse_element_generic(st->yin_ctx, name, name_len, &data, &exts.child);
David Sedlák5392a212019-07-01 09:19:10 +0200371 assert_int_equal(ret, LY_SUCCESS);
372 assert_string_equal(exts.child->stmt, "elem");
373 assert_null(exts.child->child);
374 assert_null(exts.child->arg);
David Sedlákda8ffa32019-07-08 14:17:10 +0200375 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlák5392a212019-07-01 09:19:10 +0200376 lysp_ext_instance_free(st->ctx, &exts);
377
David Sedlákb1a78352019-06-28 16:16:29 +0200378 st->finished_correctly = true;
379}
380
381static void
382test_yin_parse_extension_instance(void **state)
383{
384 LY_ERR ret;
385 struct state *st = *state;
386 const char *prefix, *name;
387 size_t prefix_len, name_len;
388 struct yin_arg_record *args = NULL;
389 struct lysp_ext_instance *exts = NULL;
David Sedlákb1a78352019-06-28 16:16:29 +0200390 const char *data = "<ext value1=\"test\" value=\"test2\"><subelem>text</subelem></ext>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200391 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
392 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák1f90d252019-07-10 17:09:32 +0200393 ret = yin_parse_extension_instance(st->yin_ctx, args, &data, name2fullname(name, prefix_len),
David Sedlák619db942019-07-03 14:47:30 +0200394 namelen2fulllen(name_len, prefix_len), LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200395 assert_int_equal(ret, LY_SUCCESS);
396 assert_string_equal(exts->name, "ext");
397 assert_int_equal(exts->insubstmt_index, 0);
398 assert_true(exts->insubstmt == LYEXT_SUBSTMT_CONTACT);
399 assert_true(exts->yin & LYS_YIN);
400 assert_string_equal(exts->child->stmt, "value1");
401 assert_string_equal(exts->child->arg, "test");
402 assert_null(exts->child->child);
403 assert_true(exts->child->flags & LYS_YIN_ATTR);
404 assert_string_equal(exts->child->next->stmt, "value");
405 assert_string_equal(exts->child->next->arg, "test2");
406 assert_null(exts->child->next->child);
407 assert_true(exts->child->next->flags & LYS_YIN_ATTR);
408
409 assert_string_equal(exts->child->next->next->stmt, "subelem");
410 assert_string_equal(exts->child->next->next->arg, "text");
411 assert_null(exts->child->next->next->child);
412 assert_null(exts->child->next->next->next);
413 assert_false(exts->child->next->next->flags & LYS_YIN_ATTR);
David Sedlákda8ffa32019-07-08 14:17:10 +0200414 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákb1a78352019-06-28 16:16:29 +0200415 LY_ARRAY_FREE(args);
416 lysp_ext_instance_free(st->ctx, exts);
417 LY_ARRAY_FREE(exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200418 exts = NULL;
419 args = NULL;
420 st = reset_state(state);
421
422 data = "<extension-elem />";
David Sedlákda8ffa32019-07-08 14:17:10 +0200423 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
424 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák1f90d252019-07-10 17:09:32 +0200425 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 +0200426 assert_int_equal(ret, LY_SUCCESS);
427 assert_string_equal(exts->name, "extension-elem");
428 assert_null(exts->argument);
429 assert_null(exts->child);
430 assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT);
431 assert_int_equal(exts->insubstmt_index, 0);
432 assert_true(exts->yin & LYS_YIN);
David Sedlákda8ffa32019-07-08 14:17:10 +0200433 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákf250ecf2019-07-01 11:02:05 +0200434 LY_ARRAY_FREE(args);
435 lysp_ext_instance_free(st->ctx, exts);
436 LY_ARRAY_FREE(exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200437 st->finished_correctly = true;
438}
439
David Sedlák555c7202019-07-04 12:14:12 +0200440static void
441test_yin_parse_content(void **state)
442{
443 struct state *st = *state;
444 LY_ERR ret = LY_SUCCESS;
445 struct sized_string name, prefix;
446 const char *data = "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
447 "<custom xmlns=\"my-ext\">"
448 "totally amazing extension"
449 "</custom>"
David Sedlák986cb412019-07-04 13:10:11 +0200450 "<extension name=\"ext\">"
451 "<argument name=\"argname\"></argument>"
452 "<description><text>desc</text></description>"
453 "<reference><text>ref</text></reference>"
454 "<status value=\"deprecated\"></status>"
455 "</extension>"
David Sedlák555c7202019-07-04 12:14:12 +0200456 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedlák32488102019-07-15 17:44:10 +0200457 "<if-feature name=\"foo\"></if-feature>"
David Sedlák32eee7b2019-07-09 12:38:44 +0200458 "<when condition=\"condition...\">"
459 "<reference><text>when_ref</text></reference>"
460 "<description><text>when_desc</text></description>"
461 "</when>"
David Sedláke1a30302019-07-10 13:49:38 +0200462 "<config value=\"true\"/>"
David Sedlákc1771b12019-07-10 15:55:46 +0200463 "<error-message>"
464 "<value>error-msg</value>"
465 "</error-message>"
David Sedlák2ce1be62019-07-10 16:15:09 +0200466 "<error-app-tag value=\"err-app-tag\"/>"
David Sedláka5b1d382019-07-10 16:31:09 +0200467 "<units name=\"radians\"></units>"
David Sedláke7084ce2019-07-10 16:44:15 +0200468 "<default value=\"default-value\"/>"
David Sedlák5545f5d2019-07-11 11:55:16 +0200469 "<position value=\"25\"></position>"
470 "<value value=\"-5\"/>"
David Sedlákcf5569a2019-07-11 13:31:34 +0200471 "<require-instance value=\"true\"></require-instance>"
David Sedlákb7296dd2019-07-11 14:58:38 +0200472 "<range value=\"5..10\" />"
David Sedlák438ae432019-07-11 15:36:54 +0200473 "<length value=\"baf\"/>"
David Sedlákd3983112019-07-12 11:20:56 +0200474 "<pattern value='pattern'>"
475 "<modifier value='invert-match'/>"
476 "</pattern>"
David Sedlákfd5b9c32019-07-12 15:33:13 +0200477 "<enum name=\"yay\">"
478 "</enum>"
David Sedlák555c7202019-07-04 12:14:12 +0200479 "</prefix>";
480 struct lysp_ext_instance *exts = NULL;
David Sedlák5f8191e2019-07-08 16:35:52 +0200481 const char **if_features = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200482 struct yin_arg_record *attrs = NULL;
David Sedláke7084ce2019-07-10 16:44:15 +0200483 const char *value, *err_msg, *app_tag, *units, *def;
David Sedlák986cb412019-07-04 13:10:11 +0200484 struct lysp_ext *ext_def = NULL;
David Sedlák32eee7b2019-07-09 12:38:44 +0200485 struct lysp_when *when_p = NULL;
David Sedlákcf5569a2019-07-11 13:31:34 +0200486 struct lysp_type_enum pos_enum = {}, val_enum = {};
David Sedlákfd5b9c32019-07-12 15:33:13 +0200487 struct lysp_type req_type = {}, range_type = {}, len_type = {}, patter_type = {}, enum_type = {};
David Sedláke1a30302019-07-10 13:49:38 +0200488 uint8_t config = 0;
David Sedlák555c7202019-07-04 12:14:12 +0200489
David Sedlákda8ffa32019-07-08 14:17:10 +0200490 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
491 yin_load_attributes(st->yin_ctx, &data, &attrs);
David Sedlák555c7202019-07-04 12:14:12 +0200492
David Sedlákfd5b9c32019-07-12 15:33:13 +0200493 struct yin_subelement subelems[17] = {
David Sedlák438ae432019-07-11 15:36:54 +0200494 {YANG_CONFIG, &config, 0},
David Sedlákc3da3ef2019-07-19 12:56:08 +0200495 {YANG_DEFAULT, &def, YIN_SUBELEM_UNIQUE},
David Sedlákfd5b9c32019-07-12 15:33:13 +0200496 {YANG_ENUM, &enum_type, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200497 {YANG_ERROR_APP_TAG, &app_tag, 0},
498 {YANG_ERROR_MESSAGE, &err_msg, 0},
499 {YANG_EXTENSION, &ext_def, 0},
500 {YANG_IF_FEATURE, &if_features, 0},
501 {YANG_LENGTH, &len_type, 0},
David Sedlákd3983112019-07-12 11:20:56 +0200502 {YANG_PATTERN, &patter_type, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200503 {YANG_RANGE, &range_type, 0},
504 {YANG_REQUIRE_INSTANCE, &req_type, 0},
505 {YANG_UNITS, &units, 0},
David Sedlák374d2b32019-07-17 15:06:55 +0200506 {YANG_POSITION, &pos_enum, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200507 {YANG_VALUE, &val_enum, 0},
508 {YANG_WHEN, &when_p, 0},
509 {YANG_CUSTOM, NULL, 0},
510 {YIN_TEXT, &value, 0}
David Sedlákd3983112019-07-12 11:20:56 +0200511 };
David Sedlákfd5b9c32019-07-12 15:33:13 +0200512 ret = yin_parse_content(st->yin_ctx, subelems, 17, &data, YANG_PREFIX, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200513 assert_int_equal(ret, LY_SUCCESS);
David Sedlák1fdb2522019-07-09 16:22:57 +0200514 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlák2ce1be62019-07-10 16:15:09 +0200515 /* check parsed values */
David Sedláke7084ce2019-07-10 16:44:15 +0200516 assert_string_equal(def, "default-value");
David Sedlák555c7202019-07-04 12:14:12 +0200517 assert_string_equal(exts->name, "custom");
518 assert_string_equal(exts->argument, "totally amazing extension");
519 assert_string_equal(value, "wsefsdf");
David Sedláka5b1d382019-07-10 16:31:09 +0200520 assert_string_equal(units, "radians");
David Sedlák32eee7b2019-07-09 12:38:44 +0200521 assert_string_equal(when_p->cond, "condition...");
522 assert_string_equal(when_p->dsc, "when_desc");
523 assert_string_equal(when_p->ref, "when_ref");
David Sedláke1a30302019-07-10 13:49:38 +0200524 assert_int_equal(config, LYS_CONFIG_W);
David Sedlák5545f5d2019-07-11 11:55:16 +0200525 assert_int_equal(pos_enum.value, 25);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200526 assert_true(pos_enum.flags & LYS_SET_VALUE);
David Sedlák5545f5d2019-07-11 11:55:16 +0200527 assert_int_equal(val_enum.value, -5);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200528 assert_true(val_enum.flags & LYS_SET_VALUE);
David Sedlákcf5569a2019-07-11 13:31:34 +0200529 assert_int_equal(req_type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200530 assert_true(req_type.flags &= LYS_SET_REQINST);
David Sedlákb7296dd2019-07-11 14:58:38 +0200531 assert_string_equal(range_type.range->arg, "5..10");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200532 assert_true(range_type.flags & LYS_SET_RANGE);
David Sedlákc1771b12019-07-10 15:55:46 +0200533 assert_string_equal(err_msg, "error-msg");
David Sedlák2ce1be62019-07-10 16:15:09 +0200534 assert_string_equal(app_tag, "err-app-tag");
David Sedlákfd5b9c32019-07-12 15:33:13 +0200535 assert_string_equal(enum_type.enums->name, "yay");
David Sedlák438ae432019-07-11 15:36:54 +0200536 assert_string_equal(len_type.length->arg, "baf");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200537 assert_true(len_type.flags & LYS_SET_LENGTH);
David Sedlákd3983112019-07-12 11:20:56 +0200538 assert_string_equal(patter_type.patterns->arg, "\x015pattern");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200539 assert_true(patter_type.flags & LYS_SET_PATTERN);
David Sedlák2ce1be62019-07-10 16:15:09 +0200540 /* cleanup */
David Sedlák555c7202019-07-04 12:14:12 +0200541 lysp_ext_instance_free(st->ctx, exts);
David Sedlák32eee7b2019-07-09 12:38:44 +0200542 lysp_when_free(st->ctx, when_p);
David Sedlák986cb412019-07-04 13:10:11 +0200543 lysp_ext_free(st->ctx, ext_def);
David Sedlák5f8191e2019-07-08 16:35:52 +0200544 FREE_STRING(st->ctx, *if_features);
David Sedlákc1771b12019-07-10 15:55:46 +0200545 FREE_STRING(st->ctx, err_msg);
David Sedlák2ce1be62019-07-10 16:15:09 +0200546 FREE_STRING(st->ctx, app_tag);
David Sedláka5b1d382019-07-10 16:31:09 +0200547 FREE_STRING(st->ctx, units);
David Sedlákd3983112019-07-12 11:20:56 +0200548 FREE_STRING(st->ctx, patter_type.patterns->arg);
David Sedláke7084ce2019-07-10 16:44:15 +0200549 FREE_STRING(st->ctx, def);
David Sedlákb7296dd2019-07-11 14:58:38 +0200550 FREE_STRING(st->ctx, range_type.range->arg);
David Sedlák438ae432019-07-11 15:36:54 +0200551 FREE_STRING(st->ctx, len_type.length->arg);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200552 FREE_STRING(st->ctx, enum_type.enums->name);
David Sedlákb7296dd2019-07-11 14:58:38 +0200553 FREE_STRING(st->ctx, value);
David Sedlák5f8191e2019-07-08 16:35:52 +0200554 LY_ARRAY_FREE(if_features);
David Sedlák555c7202019-07-04 12:14:12 +0200555 LY_ARRAY_FREE(exts);
David Sedlák986cb412019-07-04 13:10:11 +0200556 LY_ARRAY_FREE(ext_def);
David Sedlák555c7202019-07-04 12:14:12 +0200557 LY_ARRAY_FREE(attrs);
David Sedlákd3983112019-07-12 11:20:56 +0200558 LY_ARRAY_FREE(patter_type.patterns);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200559 LY_ARRAY_FREE(enum_type.enums);
David Sedlák32eee7b2019-07-09 12:38:44 +0200560 free(when_p);
David Sedlákb7296dd2019-07-11 14:58:38 +0200561 free(range_type.range);
David Sedlák438ae432019-07-11 15:36:54 +0200562 free(len_type.length);
David Sedlák555c7202019-07-04 12:14:12 +0200563 attrs = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200564 st = reset_state(state);
565
566 /* test unique subelem */
567 const char *prefix_value;
568 struct yin_subelement subelems2[2] = {{YANG_PREFIX, &prefix_value, 0},
569 {YIN_TEXT, &value, YIN_SUBELEM_UNIQUE}};
570 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
571 "<prefix value=\"inv_mod\" />"
572 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
573 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
574 "</module>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200575 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
576 yin_load_attributes(st->yin_ctx, &data, &attrs);
577 ret = yin_parse_content(st->yin_ctx, subelems2, 2, &data, YANG_MODULE, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200578 assert_int_equal(ret, LY_EVALID);
579 logbuf_assert("Redefinition of text element in module element. Line number 1.");
580 lydict_remove(st->ctx, prefix_value);
581 lydict_remove(st->ctx, value);
582 st = reset_state(state);
583 LY_ARRAY_FREE(attrs);
584 attrs = NULL;
585
586 /* test first subelem */
587 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
588 "<prefix value=\"inv_mod\" />"
589 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
590 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
591 "</module>";
592 struct yin_subelement subelems3[2] = {{YANG_PREFIX, &prefix_value, 0},
593 {YIN_TEXT, &value, YIN_SUBELEM_FIRST}};
David Sedlákda8ffa32019-07-08 14:17:10 +0200594 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
595 yin_load_attributes(st->yin_ctx, &data, &attrs);
596 ret = yin_parse_content(st->yin_ctx, subelems3, 2, &data, YANG_MODULE, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200597 assert_int_equal(ret, LY_EVALID);
598 logbuf_assert("Subelement text of module element must be defined as first subelement. Line number 1.");
599 lydict_remove(st->ctx, prefix_value);
600 st = reset_state(state);
601 LY_ARRAY_FREE(attrs);
602 attrs = NULL;
603
604 /* test mandatory subelem */
605 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
606 "</module>";
607 struct yin_subelement subelems4[1] = {{YANG_PREFIX, &prefix_value, YIN_SUBELEM_MANDATORY}};
David Sedlákda8ffa32019-07-08 14:17:10 +0200608 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
609 yin_load_attributes(st->yin_ctx, &data, &attrs);
610 ret = yin_parse_content(st->yin_ctx, subelems4, 1, &data, YANG_MODULE, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200611 assert_int_equal(ret, LY_EVALID);
612 logbuf_assert("Missing mandatory subelement prefix of module element. Line number 1.");
613 LY_ARRAY_FREE(attrs);
614
615 st->finished_correctly = true;
616}
617
David Sedlák92147b02019-07-09 14:01:01 +0200618static void
David Sedlák4a650532019-07-10 11:55:18 +0200619test_validate_value(void **state)
620{
621 struct state *st = *state;
622 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "#invalid", 8), LY_EVALID);
623 logbuf_assert("Invalid identifier character '#'. Line number 1.");
624 assert_int_equal(yin_validate_value(st->yin_ctx, Y_STR_ARG, "", 0), LY_SUCCESS);
625 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "pre:b", 5), LY_EVALID);
626 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:b", 5), LY_SUCCESS);
627 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:pre:b", 9), LY_EVALID);
628
629 st->finished_correctly = true;
630}
631
David Sedlák32488102019-07-15 17:44:10 +0200632static int
633setup_element_test(void **state)
634{
David Sedlák8e7bda82019-07-16 17:57:50 +0200635 struct state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +0200636
637#if ENABLE_LOGGER_CHECKING
638 /* setup logger */
639 ly_set_log_clb(logger, 1);
640#endif
641
642 /* reset logbuf */
643 logbuf[0] = '\0';
David Sedlák32488102019-07-15 17:44:10 +0200644
645 /* allocate parser context */
646 st->yin_ctx = calloc(1, sizeof(*st->yin_ctx));
647 st->yin_ctx->xml_ctx.ctx = st->ctx;
648 st->yin_ctx->xml_ctx.line = 1;
649
650 return EXIT_SUCCESS;
651}
652
653static int
654teardown_element_test(void **state)
655{
656 struct state *st = *(struct state **)state;
657
658#if ENABLE_LOGGER_CHECKING
659 /* teardown logger */
660 if (!st->finished_correctly && logbuf[0] != '\0') {
661 fprintf(stderr, "%s\n", logbuf);
662 }
663#endif
664
665 lyxml_context_clear(&st->yin_ctx->xml_ctx);
David Sedlák32488102019-07-15 17:44:10 +0200666 free(st->yin_ctx);
David Sedlák32488102019-07-15 17:44:10 +0200667
668 return EXIT_SUCCESS;
669}
670
671#define ELEMENT_WRAPPER_START "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
672#define ELEMENT_WRAPPER_END "</module>"
673
674/* helper function to simplify unit test of each element using parse_content function */
675LY_ERR
676test_element_helper(struct state *st, const char **data, void *dest, const char **text,
677 struct lysp_ext_instance **exts, bool valid)
678{
679 struct yin_arg_record *attrs = NULL;
680 struct sized_string name, prefix;
681 LY_ERR ret = LY_SUCCESS;
682 struct yin_subelement subelems[71] = {
683 {YANG_ACTION, dest, 0},
684 {YANG_ANYDATA, dest, 0},
685 {YANG_ANYXML, dest, 0},
686 {YANG_ARGUMENT,dest, 0},
687 {YANG_AUGMENT, dest, 0},
688 {YANG_BASE, dest, 0},
689 {YANG_BELONGS_TO, dest, 0},
690 {YANG_BIT, dest, 0},
691 {YANG_CASE, dest, 0},
692 {YANG_CHOICE, dest, 0},
693 {YANG_CONFIG, dest, 0},
694 {YANG_CONTACT, dest, 0},
695 {YANG_CONTAINER, dest, 0},
David Sedlákc3da3ef2019-07-19 12:56:08 +0200696 {YANG_DEFAULT, dest, YIN_SUBELEM_UNIQUE},
David Sedlák32488102019-07-15 17:44:10 +0200697 {YANG_DESCRIPTION, dest, 0},
698 {YANG_DEVIATE, dest, 0},
699 {YANG_DEVIATION, dest, 0},
700 {YANG_ENUM, dest, 0},
701 {YANG_ERROR_APP_TAG, dest, 0},
702 {YANG_ERROR_MESSAGE, dest, 0},
703 {YANG_EXTENSION, dest, 0},
704 {YANG_FEATURE, dest, 0},
705 {YANG_FRACTION_DIGITS, dest, 0},
706 {YANG_GROUPING, dest, 0},
707 {YANG_IDENTITY, dest, 0},
708 {YANG_IF_FEATURE, dest, 0},
709 {YANG_IMPORT, dest, 0},
710 {YANG_INCLUDE, dest, 0},
711 {YANG_INPUT, dest, 0},
712 {YANG_KEY, dest, 0},
713 {YANG_LEAF, dest, 0},
714 {YANG_LEAF_LIST, dest, 0},
715 {YANG_LENGTH, dest, 0},
716 {YANG_LIST, dest, 0},
717 {YANG_MANDATORY, dest, 0},
718 {YANG_MAX_ELEMENTS, dest, 0},
719 {YANG_MIN_ELEMENTS, dest, 0},
720 {YANG_MODIFIER, dest, 0},
721 {YANG_MODULE, dest, 0},
722 {YANG_MUST, dest, 0},
723 {YANG_NAMESPACE, dest, 0},
724 {YANG_NOTIFICATION, dest, 0},
725 {YANG_ORDERED_BY, dest, 0},
726 {YANG_ORGANIZATION, dest, 0},
727 {YANG_OUTPUT, dest, 0},
728 {YANG_PATH, dest, 0},
729 {YANG_PATTERN, dest, 0},
David Sedlák32488102019-07-15 17:44:10 +0200730 {YANG_PREFIX, dest, 0},
731 {YANG_PRESENCE, dest, 0},
732 {YANG_RANGE, dest, 0},
733 {YANG_REFERENCE, dest, 0},
734 {YANG_REFINE, dest, 0},
735 {YANG_REQUIRE_INSTANCE, dest, 0},
736 {YANG_REVISION, dest, 0},
737 {YANG_REVISION_DATE, dest, 0},
738 {YANG_RPC, dest, 0},
739 {YANG_STATUS, dest, 0},
740 {YANG_SUBMODULE, dest, 0},
741 {YANG_TYPE, dest, 0},
742 {YANG_TYPEDEF, dest, 0},
743 {YANG_UNIQUE, dest, 0},
744 {YANG_UNITS, dest, 0},
745 {YANG_USES, dest, 0},
David Sedlák374d2b32019-07-17 15:06:55 +0200746 {YANG_POSITION, dest, 0},
David Sedlák32488102019-07-15 17:44:10 +0200747 {YANG_VALUE, dest, 0},
748 {YANG_WHEN, dest, 0},
749 {YANG_YANG_VERSION, dest, 0},
750 {YANG_YIN_ELEMENT, dest, 0},
751 {YANG_CUSTOM, dest, 0},
752 {YIN_TEXT, dest, 0},
753 {YIN_VALUE, dest, 0}
754 };
755 LY_CHECK_RET(lyxml_get_element(&st->yin_ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len));\
756 LY_CHECK_RET(yin_load_attributes(st->yin_ctx, data, &attrs));\
David Sedlák8e7bda82019-07-16 17:57:50 +0200757 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 +0200758 LY_ARRAY_FREE(attrs);
759 if (valid) {
760 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
761 }
762 /* reset status */
763 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
764 return ret;
765}
766
767static void
768test_enum_bit_elem(void **state)
769{
770 /* yin_parse_enum_bit is function that is being mainly tested by this test */
771 struct state *st = *state;
772 struct lysp_type type = {};
773 const char *data;
774 data = ELEMENT_WRAPPER_START
775 "<enum name=\"enum-name\">"
776 "<if-feature name=\"feature\" />"
777 "<value value=\"55\" />"
778 "<status value=\"deprecated\" />"
779 "<description><text>desc...</text></description>"
780 "<reference><text>ref...</text></reference>"
781 "</enum>"
782 ELEMENT_WRAPPER_END;
783 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
784 assert_string_equal(*type.enums->iffeatures, "feature");
785 assert_int_equal(type.enums->value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200786 assert_true((type.enums->flags & LYS_STATUS_DEPRC) && (type.enums->flags & LYS_SET_VALUE));
David Sedlák32488102019-07-15 17:44:10 +0200787 assert_string_equal(type.enums->dsc, "desc...");
788 assert_string_equal(type.enums->ref, "ref...");
789 lysp_type_free(st->ctx, &type);
790 memset(&type, 0, sizeof type);
791
792 /* todo bit element test */
793 st->finished_correctly = true;
794}
795
796static void
797test_meta_elem(void **state)
798{
799 struct state *st = *state;
800 char *value = NULL;
801 const char *data;
802
803 /* organization element */
804 data = ELEMENT_WRAPPER_START
805 "<organization><text>organization...</text></organization>"
806 ELEMENT_WRAPPER_END;
807 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
808 assert_string_equal(value, "organization...");
809 FREE_STRING(st->ctx, value);
810 value = NULL;
811 /* contact element */
812 data = ELEMENT_WRAPPER_START
813 "<contact><text>contact...</text></contact>"
814 ELEMENT_WRAPPER_END;
815 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
816 assert_string_equal(value, "contact...");
817 FREE_STRING(st->ctx, value);
818 value = NULL;
819 /* description element */
820 data = ELEMENT_WRAPPER_START
821 "<description><text>description...</text></description>"
822 ELEMENT_WRAPPER_END;
823 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
824 assert_string_equal(value, "description...");
825 FREE_STRING(st->ctx, value);
826 value = NULL;
827 /* reference element */
828 data = ELEMENT_WRAPPER_START
829 "<reference><text>reference...</text></reference>"
830 ELEMENT_WRAPPER_END;
831 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
832 assert_string_equal(value, "reference...");
833 FREE_STRING(st->ctx, value);
834 value = NULL;
835
836 /* missing text subelement */
837 data = ELEMENT_WRAPPER_START
838 "<reference>reference...</reference>"
839 ELEMENT_WRAPPER_END;
840 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, false), LY_EVALID);
841 logbuf_assert("Missing mandatory subelement text of reference element. Line number 1.");
842
843 st->finished_correctly = true;
844}
845
846static void
847test_import_elem(void **state)
848{
849 struct state *st = *state;
850 const char *data;
851 struct lys_module *lys_mod = calloc(1, sizeof *lys_mod);
852 struct lysp_module *lysp_mod = calloc(1, sizeof *lysp_mod);
853 lys_mod->ctx = st->ctx;
854 lysp_mod->mod = lys_mod;
855
856 /* max subelems */
857 data = ELEMENT_WRAPPER_START
858 "<import module=\"a\">"
859 "<prefix value=\"a_mod\"/>"
860 "<revision-date date=\"2015-01-01\"></revision-date>"
861 "<description><text>import description</text></description>"
862 "<reference><text>import reference</text></reference>"
863 "</import>"
864 ELEMENT_WRAPPER_END;
865 assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, true), LY_SUCCESS);
866 assert_string_equal(lysp_mod->imports->name, "a");
867 assert_string_equal(lysp_mod->imports->prefix, "a_mod");
868 assert_string_equal(lysp_mod->imports->rev, "2015-01-01");
869 assert_string_equal(lysp_mod->imports->dsc, "import description");
870 assert_string_equal(lysp_mod->imports->ref, "import reference");
871 lysp_module_free(lysp_mod);
872 lys_module_free(lys_mod, NULL);
873
874 /* min subelems */
875 lys_mod = calloc(1, sizeof *lys_mod);
876 lysp_mod = calloc(1, sizeof *lysp_mod);
877 lys_mod->ctx = st->ctx;
878 lysp_mod->mod = lys_mod;
879 data = ELEMENT_WRAPPER_START
880 "<import module=\"a\">"
881 "<prefix value=\"a_mod\"/>"
882 "</import>"
883 ELEMENT_WRAPPER_END;
884 assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, true), LY_SUCCESS);
885 assert_string_equal(lysp_mod->imports->prefix, "a_mod");
886 lysp_module_free(lysp_mod);
887 lys_module_free(lys_mod, NULL);
888
889 /* invalid (missing prefix) */
890 lys_mod = calloc(1, sizeof *lys_mod);
891 lysp_mod = calloc(1, sizeof *lysp_mod);
892 lys_mod->ctx = st->ctx;
893 lysp_mod->mod = lys_mod;
894 data = ELEMENT_WRAPPER_START "<import module=\"a\">""</import>" ELEMENT_WRAPPER_END;
895 assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, false), LY_EVALID);
896 logbuf_assert("Missing mandatory subelement prefix of import element. Line number 1.");
897 lysp_module_free(lysp_mod);
898 lys_module_free(lys_mod, NULL);
899
900 /* invalid reused prefix */
901 lys_mod = calloc(1, sizeof *lys_mod);
902 lysp_mod = calloc(1, sizeof *lysp_mod);
903 lys_mod->ctx = st->ctx;
904 lysp_mod->mod = lys_mod;
905 data = ELEMENT_WRAPPER_START
906 "<import module=\"a\">"
907 "<prefix value=\"a_mod\"/>"
908 "</import>"
909 "<import module=\"a\">"
910 "<prefix value=\"a_mod\"/>"
911 "</import>"
912 ELEMENT_WRAPPER_END;
913 assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, false), LY_EVALID);
914 logbuf_assert("Prefix \"a_mod\" already used to import \"a\" module. Line number 1.");
915 lysp_module_free(lysp_mod);
916 lys_module_free(lys_mod, NULL);
917
918 st->finished_correctly = true;
919}
920
921static void
922test_status_elem(void **state)
923{
924 struct state *st = *state;
925 const char *data;
926 uint16_t flags = 0;
927
928 /* test valid values */
929 data = ELEMENT_WRAPPER_START "<status value=\"current\" />" ELEMENT_WRAPPER_END;
930 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200931 assert_true(flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +0200932
933 data = ELEMENT_WRAPPER_START "<status value=\"deprecated\" />" ELEMENT_WRAPPER_END;
934 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200935 assert_true(flags & LYS_STATUS_DEPRC);
David Sedlák32488102019-07-15 17:44:10 +0200936
937 data = ELEMENT_WRAPPER_START "<status value=\"obsolete\"></status>" ELEMENT_WRAPPER_END;
938 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200939 assert_true(flags & LYS_STATUS_OBSLT);
David Sedlák32488102019-07-15 17:44:10 +0200940
941 /* test invalid value */
942 data = ELEMENT_WRAPPER_START "<status value=\"invalid\"></status>" ELEMENT_WRAPPER_END;
943 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
944 logbuf_assert("Invalid value \"invalid\" of \"status\". Line number 1.");
945 st->finished_correctly = true;
946}
947
948static void
949test_ext_elem(void **state)
950{
951 struct state *st = *state;
952 const char *data;
953 struct lysp_ext *ext = NULL;
954
955 /* max subelems */
956 data = ELEMENT_WRAPPER_START
957 "<extension name=\"ext_name\">"
958 "<argument name=\"arg\"></argument>"
959 "<status value=\"current\"/>"
960 "<description><text>ext_desc</text></description>"
961 "<reference><text>ext_ref</text></reference>"
962 "</extension>"
963 ELEMENT_WRAPPER_END;
964 assert_int_equal(test_element_helper(st, &data, &ext, NULL, NULL, true), LY_SUCCESS);
965 assert_string_equal(ext->name, "ext_name");
966 assert_string_equal(ext->argument, "arg");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200967 assert_true(ext->flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +0200968 assert_string_equal(ext->dsc, "ext_desc");
969 assert_string_equal(ext->ref, "ext_ref");
970 lysp_ext_free(st->ctx, ext);
971 LY_ARRAY_FREE(ext);
972 ext = NULL;
973
974 /* min subelems */
975 data = ELEMENT_WRAPPER_START "<extension name=\"ext_name\"></extension>" ELEMENT_WRAPPER_END;
976 assert_int_equal(test_element_helper(st, &data, &ext, NULL, NULL, true), LY_SUCCESS);
977 assert_string_equal(ext->name, "ext_name");
978 lysp_ext_free(st->ctx, ext);
979 LY_ARRAY_FREE(ext);
980 ext = NULL;
981
982 st->finished_correctly = true;
983}
984
985static void
986test_yin_element_elem(void **state)
987{
988 struct state *st = *state;
989 const char *data;
990 uint16_t flags = 0;
991
992 data = ELEMENT_WRAPPER_START "<yin-element value=\"true\" />" ELEMENT_WRAPPER_END;
993 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200994 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +0200995
996 data = ELEMENT_WRAPPER_START "<yin-element value=\"false\" />" ELEMENT_WRAPPER_END;
997 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200998 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +0200999
1000 data = ELEMENT_WRAPPER_START "<yin-element value=\"invalid\" />" ELEMENT_WRAPPER_END;
1001 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001002 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +02001003 logbuf_assert("Invalid value \"invalid\" of \"yin-element\". Line number 1.");
1004 st->finished_correctly = true;
1005}
1006
1007static void
1008test_yangversion_elem(void **state)
1009{
1010 struct state *st = *state;
1011 const char *data;
1012 uint8_t version = 0;
1013
1014 /* valid values */
1015 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.0\" />" ELEMENT_WRAPPER_END;
1016 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001017 assert_true(version & LYS_VERSION_1_0);
David Sedlák32488102019-07-15 17:44:10 +02001018 assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_0);
1019
1020 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.1\" />" ELEMENT_WRAPPER_END;
1021 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001022 assert_true(version & LYS_VERSION_1_1);
David Sedlák32488102019-07-15 17:44:10 +02001023 assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_1);
1024
1025 /* invalid value */
1026 data = ELEMENT_WRAPPER_START "<yang-version value=\"version\" />" ELEMENT_WRAPPER_END;
1027 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, false), LY_EVALID);
1028 logbuf_assert("Invalid value \"version\" of \"yang-version\". Line number 1.");
1029
1030 st->finished_correctly = true;
1031}
1032
1033static void
1034test_mandatory_elem(void **state)
1035{
1036 struct state *st = *state;
1037 const char *data;
1038 uint16_t man = 0;
1039
1040 /* valid values */
1041 data = ELEMENT_WRAPPER_START "<mandatory value=\"true\" />" ELEMENT_WRAPPER_END;
1042 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, true), LY_SUCCESS);
1043 assert_int_equal(man, LYS_MAND_TRUE);
1044 man = 0;
1045
1046 data = ELEMENT_WRAPPER_START "<mandatory value=\"false\" />" ELEMENT_WRAPPER_END;
1047 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, true), LY_SUCCESS);
1048 assert_int_equal(man, LYS_MAND_FALSE);
1049
1050 data = ELEMENT_WRAPPER_START "<mandatory value=\"invalid\" />" ELEMENT_WRAPPER_END;
1051 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, false), LY_EVALID);
1052 logbuf_assert("Invalid value \"invalid\" of \"mandatory\". Line number 1.");
1053
1054 st->finished_correctly = true;
1055}
1056
David Sedlák8e7bda82019-07-16 17:57:50 +02001057static void
1058test_argument_elem(void **state)
1059{
1060 struct state *st = *state;
1061 const char *data;
1062 uint16_t flags = 0;
1063 const char *arg;
1064 struct yin_argument_meta arg_meta = {&flags, &arg};
1065 /* max subelems */
1066 data = ELEMENT_WRAPPER_START
1067 "<argument name=\"arg-name\">"
1068 "<yin-element value=\"true\" />"
1069 "</argument>"
1070 ELEMENT_WRAPPER_END;
1071 assert_int_equal(test_element_helper(st, &data, &arg_meta, NULL, NULL, true), LY_SUCCESS);
1072 assert_string_equal(arg, "arg-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001073 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001074 flags = 0;
1075 FREE_STRING(st->ctx, arg);
1076 arg = NULL;
1077
1078 /* min subelems */
1079 data = ELEMENT_WRAPPER_START
1080 "<argument name=\"arg\">"
1081 "</argument>"
1082 ELEMENT_WRAPPER_END;
1083 assert_int_equal(test_element_helper(st, &data, &arg_meta, NULL, NULL, true), LY_SUCCESS);
1084 assert_string_equal(arg, "arg");
1085 assert_true(flags == 0);
1086 FREE_STRING(st->ctx, arg);
1087
1088 st->finished_correctly = true;
1089}
1090
1091static void
1092test_base_elem(void **state)
1093{
1094 struct state *st = *state;
1095 const char *data;
1096 const char **bases = NULL;
1097 struct lysp_type type = {};
1098
1099 /* as identity subelement */
1100 data = "<identity xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
1101 "<base name=\"base-name\"/>"
1102 "</identity>";
1103 assert_int_equal(test_element_helper(st, &data, &bases, NULL, NULL, true), LY_SUCCESS);
1104 assert_string_equal(*bases, "base-name");
1105 FREE_STRING(st->ctx, *bases);
1106 LY_ARRAY_FREE(bases);
1107
1108 /* as type subelement */
1109 data = "<type xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
1110 "<base name=\"base-name\"/>"
1111 "</type>";
1112 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1113 assert_string_equal(*type.bases, "base-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001114 assert_true(type.flags & LYS_SET_BASE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001115 FREE_STRING(st->ctx, *type.bases);
1116 LY_ARRAY_FREE(type.bases);
1117
1118 st->finished_correctly = true;
1119}
1120
1121static void
1122test_belongsto_elem(void **state)
1123{
1124 struct state *st = *state;
1125 const char *data;
1126 struct lysp_submodule submod;
1127
1128 data = ELEMENT_WRAPPER_START
1129 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>"
1130 ELEMENT_WRAPPER_END;
1131 assert_int_equal(test_element_helper(st, &data, &submod, NULL, NULL, true), LY_SUCCESS);
1132 assert_string_equal(submod.belongsto, "module-name");
1133 assert_string_equal(submod.prefix, "pref");
1134 FREE_STRING(st->ctx, submod.belongsto);
1135 FREE_STRING(st->ctx, submod.prefix);
1136
1137 data = ELEMENT_WRAPPER_START "<belongs-to module=\"module-name\"></belongs-to>" ELEMENT_WRAPPER_END;
1138 assert_int_equal(test_element_helper(st, &data, &submod, NULL, NULL, false), LY_EVALID);
1139 logbuf_assert("Missing mandatory subelement prefix of belongs-to element. Line number 1.");
1140 FREE_STRING(st->ctx, submod.belongsto);
1141
1142 st->finished_correctly = true;
1143}
1144
1145static void
1146test_config_elem(void **state)
1147{
1148 struct state *st = *state;
1149 const char *data;
1150 uint16_t flags = 0;
1151
1152 data = ELEMENT_WRAPPER_START "<config value=\"true\"/>" ELEMENT_WRAPPER_END;
1153 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001154 assert_true(flags & LYS_CONFIG_W);
David Sedlák8e7bda82019-07-16 17:57:50 +02001155 flags = 0;
1156
1157 data = ELEMENT_WRAPPER_START "<config value=\"false\"/>" ELEMENT_WRAPPER_END;
1158 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001159 assert_true(flags & LYS_CONFIG_R);
David Sedlák8e7bda82019-07-16 17:57:50 +02001160 flags = 0;
1161
1162 data = ELEMENT_WRAPPER_START "<config value=\"invalid\"/>" ELEMENT_WRAPPER_END;
1163 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
1164 logbuf_assert("Invalid value \"invalid\" of \"config\". Line number 1.");
1165
1166 st->finished_correctly = true;
1167}
1168
1169static void
1170test_default_elem(void **state)
1171{
1172 struct state *st = *state;
1173 const char *data;
1174 const char *val = NULL;
1175
1176 data = ELEMENT_WRAPPER_START "<default value=\"defaul-value\"/>" ELEMENT_WRAPPER_END;
1177 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1178 assert_string_equal(val, "defaul-value");
1179 FREE_STRING(st->ctx, val);
1180 val = NULL;
1181
1182 data = ELEMENT_WRAPPER_START "<default/>" ELEMENT_WRAPPER_END;
1183 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1184 logbuf_assert("Missing mandatory attribute value of default element. Line number 1.");
1185
1186 st->finished_correctly = true;
1187}
1188
1189static void
1190test_err_app_tag_elem(void **state)
1191{
1192 struct state *st = *state;
1193 const char *data;
1194 const char *val = NULL;
1195
1196 data = ELEMENT_WRAPPER_START "<error-app-tag value=\"val\"/>" ELEMENT_WRAPPER_END;
1197 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1198 assert_string_equal(val, "val");
1199 FREE_STRING(st->ctx, val);
1200 val = NULL;
1201
1202 data = ELEMENT_WRAPPER_START "<error-app-tag/>" ELEMENT_WRAPPER_END;
1203 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1204 logbuf_assert("Missing mandatory attribute value of error-app-tag element. Line number 1.");
1205
1206 st->finished_correctly = true;
1207}
1208
1209static void
1210test_err_msg_elem(void **state)
1211{
1212 struct state *st = *state;
1213 const char *data;
1214 const char *val = NULL;
1215
1216 data = ELEMENT_WRAPPER_START "<error-message><value>val</value></error-message>" ELEMENT_WRAPPER_END;
1217 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1218 assert_string_equal(val, "val");
1219 FREE_STRING(st->ctx, val);
1220
1221 data = ELEMENT_WRAPPER_START "<error-message></error-message>" ELEMENT_WRAPPER_END;
1222 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1223 logbuf_assert("Missing mandatory subelement value of error-message element. Line number 1.");
1224
1225 st->finished_correctly = true;
1226}
1227
1228static void
1229test_fracdigits_elem(void **state)
1230{
1231 struct state *st = *state;
1232 const char *data;
1233 struct lysp_type type = {};
1234
1235 /* valid value */
1236 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"10\"></fraction-digits>" ELEMENT_WRAPPER_END;
1237 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1238 assert_int_equal(type.fraction_digits, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001239 assert_true(type.flags & LYS_SET_FRDIGITS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001240
1241 /* invalid values */
1242 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"-1\"></fraction-digits>" ELEMENT_WRAPPER_END;
1243 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1244 logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1.");
1245
1246 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"02\"></fraction-digits>" ELEMENT_WRAPPER_END;
1247 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1248 logbuf_assert("Invalid value \"02\" of \"fraction-digits\". Line number 1.");
1249
1250 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"1p\"></fraction-digits>" ELEMENT_WRAPPER_END;
1251 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1252 logbuf_assert("Invalid value \"1p\" of \"fraction-digits\". Line number 1.");
1253
1254 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"19\"></fraction-digits>" ELEMENT_WRAPPER_END;
1255 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1256 logbuf_assert("Invalid value \"19\" of \"fraction-digits\". Line number 1.");
1257
1258 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"999999999999999999\"></fraction-digits>" ELEMENT_WRAPPER_END;
1259 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1260 logbuf_assert("Invalid value \"999999999999999999\" of \"fraction-digits\". Line number 1.");
1261
1262 st->finished_correctly = true;
1263}
1264
1265static void
1266test_iffeature_elem(void **state)
1267{
1268 struct state *st = *state;
1269 const char *data;
1270 const char **iffeatures = NULL;
1271
1272 data = ELEMENT_WRAPPER_START "<if-feature name=\"local-storage\"></if-feature>" ELEMENT_WRAPPER_END;
1273 assert_int_equal(test_element_helper(st, &data, &iffeatures, NULL, NULL, true), LY_SUCCESS);
1274 assert_string_equal(*iffeatures, "local-storage");
1275 FREE_STRING(st->ctx, *iffeatures);
1276 LY_ARRAY_FREE(iffeatures);
1277 iffeatures = NULL;
1278
1279 data = ELEMENT_WRAPPER_START "<if-feature/>" ELEMENT_WRAPPER_END;
1280 assert_int_equal(test_element_helper(st, &data, &iffeatures, NULL, NULL, false), LY_EVALID);
1281 logbuf_assert("Missing mandatory attribute name of if-feature element. Line number 1.");
1282 LY_ARRAY_FREE(iffeatures);
1283 iffeatures = NULL;
1284
1285 st->finished_correctly = true;
1286}
1287
1288static void
1289test_length_elem(void **state)
1290{
1291 struct state *st = *state;
1292 const char *data;
1293 struct lysp_type type = {};
1294
1295 /* max subelems */
1296 data = ELEMENT_WRAPPER_START
1297 "<length value=\"length-str\">"
1298 "<error-message><value>err-msg</value></error-message>"
1299 "<error-app-tag value=\"err-app-tag\"/>"
1300 "<description><text>desc</text></description>"
1301 "<reference><text>ref</text></reference>"
1302 "</length>"
1303 ELEMENT_WRAPPER_END;
1304 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1305 assert_string_equal(type.length->arg, "length-str");
1306 assert_string_equal(type.length->emsg, "err-msg");
1307 assert_string_equal(type.length->eapptag, "err-app-tag");
1308 assert_string_equal(type.length->dsc, "desc");
1309 assert_string_equal(type.length->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001310 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001311 lysp_type_free(st->ctx, &type);
1312 memset(&type, 0, sizeof(type));
1313
1314 /* min subelems */
1315 data = ELEMENT_WRAPPER_START
1316 "<length value=\"length-str\">"
1317 "</length>"
1318 ELEMENT_WRAPPER_END;
1319 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1320 assert_string_equal(type.length->arg, "length-str");
1321 lysp_type_free(st->ctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001322 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001323 memset(&type, 0, sizeof(type));
1324
1325 data = ELEMENT_WRAPPER_START "<length></length>" ELEMENT_WRAPPER_END;
1326 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1327 logbuf_assert("Missing mandatory attribute value of length element. Line number 1.");
1328 lysp_type_free(st->ctx, &type);
1329 memset(&type, 0, sizeof(type));
1330
1331 st->finished_correctly = true;
1332}
1333
1334static void
1335test_modifier_elem(void **state)
1336{
1337 struct state *st = *state;
1338 const char *data;
1339 const char *pat = lydict_insert(st->ctx, "\006pattern", 8);
1340
1341 data = ELEMENT_WRAPPER_START "<modifier value=\"invert-match\" />" ELEMENT_WRAPPER_END;
1342 assert_int_equal(test_element_helper(st, &data, &pat, NULL, NULL, true), LY_SUCCESS);
1343 assert_string_equal(pat, "\x015pattern");
1344 FREE_STRING(st->ctx, pat);
1345
1346 pat = lydict_insert(st->ctx, "\006pattern", 8);
1347 data = ELEMENT_WRAPPER_START "<modifier value=\"invert\" />" ELEMENT_WRAPPER_END;
1348 assert_int_equal(test_element_helper(st, &data, &pat, NULL, NULL, false), LY_EVALID);
1349 logbuf_assert("Invalid value \"invert\" of \"modifier\". Line number 1.");
1350 FREE_STRING(st->ctx, pat);
1351
1352 st->finished_correctly = true;
1353}
1354
1355static void
1356test_namespace_elem(void **state)
1357{
1358 struct state *st = *state;
1359 const char *data;
1360 const char *ns;
1361
1362 data = ELEMENT_WRAPPER_START "<namespace uri=\"ns\"/>" ELEMENT_WRAPPER_END;
1363 assert_int_equal(test_element_helper(st, &data, &ns, NULL, NULL, true), LY_SUCCESS);
1364 assert_string_equal(ns, "ns");
1365 FREE_STRING(st->ctx, ns);
1366
1367 data = ELEMENT_WRAPPER_START "<namespace/>" ELEMENT_WRAPPER_END;
1368 assert_int_equal(test_element_helper(st, &data, &ns, NULL, NULL, false), LY_EVALID);
1369 logbuf_assert("Missing mandatory attribute uri of namespace element. Line number 1.");
1370
1371 st->finished_correctly = true;
1372}
1373
1374static void
1375test_path_elem(void **state)
1376{
1377 struct state *st = *state;
1378 const char *data;
1379 struct lysp_type type = {};
1380
1381 data = ELEMENT_WRAPPER_START "<path value=\"path-val\"/>" ELEMENT_WRAPPER_END;
1382 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1383 assert_string_equal("path-val", type.path);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001384 assert_true(type.flags & LYS_SET_PATH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001385 lysp_type_free(st->ctx, &type);
1386
1387 st->finished_correctly = true;
1388}
1389
1390static void
1391test_pattern_elem(void **state)
1392{
1393 struct state *st = *state;
1394 const char *data;
1395 struct lysp_type type = {};
1396
1397 /* max subelems */
1398 data = ELEMENT_WRAPPER_START
1399 "<pattern value=\"super_pattern\">"
1400 "<modifier value=\"invert-match\"/>"
1401 "<error-message><value>err-msg-value</value></error-message>"
1402 "<error-app-tag value=\"err-app-tag-value\"/>"
1403 "<description><text>pattern-desc</text></description>"
1404 "<reference><text>pattern-ref</text></reference>"
1405 "</pattern>"
1406 ELEMENT_WRAPPER_END;
1407 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001408 assert_true(type.flags & LYS_SET_PATTERN);
David Sedlák8e7bda82019-07-16 17:57:50 +02001409 assert_string_equal(type.patterns->arg, "\x015super_pattern");
1410 assert_string_equal(type.patterns->dsc, "pattern-desc");
1411 assert_string_equal(type.patterns->eapptag, "err-app-tag-value");
1412 assert_string_equal(type.patterns->emsg, "err-msg-value");
1413 assert_string_equal(type.patterns->dsc, "pattern-desc");
1414 assert_string_equal(type.patterns->ref, "pattern-ref");
1415 lysp_type_free(st->ctx, &type);
1416 memset(&type, 0, sizeof(type));
1417
1418 /* min subelems */
1419 data = ELEMENT_WRAPPER_START "<pattern value=\"pattern\"> </pattern>" ELEMENT_WRAPPER_END;
1420 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1421 assert_string_equal(type.patterns->arg, "\x006pattern");
1422 lysp_type_free(st->ctx, &type);
1423 memset(&type, 0, sizeof(type));
1424
1425 st->finished_correctly = true;
1426}
1427
1428static void
1429test_value_position_elem(void **state)
1430{
1431 struct state *st = *state;
1432 const char *data;
1433 struct lysp_type_enum en = {};
1434
1435 /* valid values */
1436 data = ELEMENT_WRAPPER_START "<value value=\"55\" />" ELEMENT_WRAPPER_END;
1437 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1438 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001439 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001440 memset(&en, 0, sizeof(en));
1441
1442 data = ELEMENT_WRAPPER_START "<value value=\"-55\"/>" ELEMENT_WRAPPER_END;
1443 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1444 assert_int_equal(en.value, -55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001445 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001446 memset(&en, 0, sizeof(en));
1447
1448 data = ELEMENT_WRAPPER_START "<value value=\"0\"/>" ELEMENT_WRAPPER_END;
1449 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1450 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001451 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001452 memset(&en, 0, sizeof(en));
1453
1454 data = ELEMENT_WRAPPER_START "<value value=\"-0\"/>" ELEMENT_WRAPPER_END;
1455 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1456 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001457 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001458 memset(&en, 0, sizeof(en));
1459
1460 /* valid positions */
1461 data = ELEMENT_WRAPPER_START "<position value=\"55\" />" ELEMENT_WRAPPER_END;
1462 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1463 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001464 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001465 memset(&en, 0, sizeof(en));
1466
1467 data = ELEMENT_WRAPPER_START "<position value=\"0\" />" ELEMENT_WRAPPER_END;
1468 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1469 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001470 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001471 memset(&en, 0, sizeof(en));
1472
1473 /* invalid values */
1474 data = ELEMENT_WRAPPER_START "<value value=\"99999999999999999999999\"/>" ELEMENT_WRAPPER_END;
1475 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1476 logbuf_assert("Invalid value \"99999999999999999999999\" of \"value\". Line number 1.");
1477
1478 data = ELEMENT_WRAPPER_START "<value value=\"1k\"/>" ELEMENT_WRAPPER_END;
1479 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1480 logbuf_assert("Invalid value \"1k\" of \"value\". Line number 1.");
1481
David Sedlák69f01612019-07-17 11:41:08 +02001482 data = ELEMENT_WRAPPER_START "<value value=\"\"/>" ELEMENT_WRAPPER_END;
1483 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1484 logbuf_assert("Invalid value \"\" of \"value\". Line number 1.");
1485
David Sedlák8e7bda82019-07-16 17:57:50 +02001486 /*invalid positions */
1487 data = ELEMENT_WRAPPER_START "<position value=\"-5\"/>" ELEMENT_WRAPPER_END;
1488 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1489 logbuf_assert("Invalid value \"-5\" of \"position\". Line number 1.");
1490
1491 data = ELEMENT_WRAPPER_START "<position value=\"-0\"/>" ELEMENT_WRAPPER_END;
1492 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1493 logbuf_assert("Invalid value \"-0\" of \"position\". Line number 1.");
1494
1495 data = ELEMENT_WRAPPER_START "<position value=\"99999999999999999999\"/>" ELEMENT_WRAPPER_END;
1496 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1497 logbuf_assert("Invalid value \"99999999999999999999\" of \"position\". Line number 1.");
1498
David Sedlák69f01612019-07-17 11:41:08 +02001499 data = ELEMENT_WRAPPER_START "<position value=\"\"/>" ELEMENT_WRAPPER_END;
1500 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1501 logbuf_assert("Invalid value \"\" of \"position\". Line number 1.");
1502
1503 st->finished_correctly = true;
1504}
1505
1506static void
1507test_prefix_elem(void **state)
1508{
1509 struct state *st = *state;
1510 const char *data;
1511 const char *value = NULL;
1512
1513 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\"/>" ELEMENT_WRAPPER_END;
1514 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
1515 assert_string_equal(value, "pref");
1516 FREE_STRING(st->ctx, value);
1517
1518 st->finished_correctly = true;
1519}
1520
1521static void
1522test_range_elem(void **state)
1523{
1524 struct state *st = *state;
1525 const char *data;
1526 struct lysp_type type = {};
1527
1528 /* max subelems */
1529 data = ELEMENT_WRAPPER_START
1530 "<range value=\"range-str\">"
1531 "<error-message><value>err-msg</value></error-message>"
1532 "<error-app-tag value=\"err-app-tag\" />"
1533 "<description><text>desc</text></description>"
1534 "<reference><text>ref</text></reference>"
1535 "</range>"
1536 ELEMENT_WRAPPER_END;
1537 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1538 assert_string_equal(type.range->arg, "range-str");
1539 assert_string_equal(type.range->dsc, "desc");
1540 assert_string_equal(type.range->eapptag, "err-app-tag");
1541 assert_string_equal(type.range->emsg, "err-msg");
1542 assert_string_equal(type.range->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001543 assert_true(type.flags & LYS_SET_RANGE);
David Sedlák69f01612019-07-17 11:41:08 +02001544 lysp_type_free(st->ctx, &type);
1545 memset(&type, 0, sizeof(type));
1546
1547 /* min subelems */
1548 data = ELEMENT_WRAPPER_START "<range value=\"range-str\"/>" ELEMENT_WRAPPER_END;
1549 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1550 assert_string_equal(type.range->arg, "range-str");
1551 lysp_type_free(st->ctx, &type);
1552 memset(&type, 0, sizeof(type));
1553
1554 st->finished_correctly = true;
1555}
1556
1557static void
1558test_reqinstance_elem(void **state)
1559{
1560 struct state *st = *state;
1561 const char *data;
1562 struct lysp_type type = {};
1563
1564 data = ELEMENT_WRAPPER_START "<require-instance value=\"true\"/>" ELEMENT_WRAPPER_END;
1565 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1566 assert_int_equal(type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001567 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001568 memset(&type, 0, sizeof(type));
1569
1570 data = ELEMENT_WRAPPER_START "<require-instance value=\"false\"/>" ELEMENT_WRAPPER_END;
1571 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1572 assert_int_equal(type.require_instance, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001573 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001574 memset(&type, 0, sizeof(type));
1575
1576 data = ELEMENT_WRAPPER_START "<require-instance value=\"invalid\"/>" ELEMENT_WRAPPER_END;
1577 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1578 memset(&type, 0, sizeof(type));
1579 logbuf_assert("Invalid value \"invalid\" of \"require-instance\". Line number 1.");
1580
1581 st->finished_correctly = true;
1582}
1583
1584static void
1585test_revision_date_elem(void **state)
1586{
1587 struct state *st = *state;
1588 const char *data;
1589 char rev[LY_REV_SIZE];
1590
1591 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\"/>" ELEMENT_WRAPPER_END;
1592 assert_int_equal(test_element_helper(st, &data, rev, NULL, NULL, true), LY_SUCCESS);
1593 assert_string_equal(rev, "2000-01-01");
1594
1595 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-50-05\"/>" ELEMENT_WRAPPER_END;
1596 assert_int_equal(test_element_helper(st, &data, rev, NULL, NULL, false), LY_EVALID);
1597 logbuf_assert("Invalid value \"2000-50-05\" of \"revision-date\". Line number 1.");
1598
1599 st->finished_correctly = true;
1600}
1601
1602static void
1603test_unique_elem(void **state)
1604{
1605 struct state *st = *state;
1606 const char *data;
1607 const char **values = NULL;
1608
1609 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\"/>" ELEMENT_WRAPPER_END;
1610 assert_int_equal(test_element_helper(st, &data, &values, NULL, NULL, true), LY_SUCCESS);
1611 assert_string_equal(*values, "tag");
1612 FREE_STRING(st->ctx, *values);
1613 LY_ARRAY_FREE(values);
1614
1615 st->finished_correctly = true;
1616}
1617
1618static void
1619test_units_elem(void **state)
1620{
1621 struct state *st = *state;
1622 const char *data;
1623 const char *values = NULL;
1624
1625 data = ELEMENT_WRAPPER_START "<units name=\"name\"/>" ELEMENT_WRAPPER_END;
1626 assert_int_equal(test_element_helper(st, &data, &values, NULL, NULL, true), LY_SUCCESS);
1627 assert_string_equal(values, "name");
1628 FREE_STRING(st->ctx, values);
1629
1630 st->finished_correctly = true;
1631}
1632
1633static void
1634test_when_elem(void **state)
1635{
1636 struct state *st = *state;
1637 const char *data;
1638 struct lysp_when *when = NULL;
1639
1640 data = ELEMENT_WRAPPER_START
1641 "<when condition=\"cond\">"
1642 "<description><text>desc</text></description>"
1643 "<reference><text>ref</text></reference>"
1644 "</when>"
1645 ELEMENT_WRAPPER_END;
1646 assert_int_equal(test_element_helper(st, &data, &when, NULL, NULL, true), LY_SUCCESS);
1647 assert_string_equal(when->cond, "cond");
1648 assert_string_equal(when->dsc, "desc");
1649 assert_string_equal(when->ref, "ref");
1650 lysp_when_free(st->ctx, when);
1651 free(when);
1652 when = NULL;
1653
1654 data = ELEMENT_WRAPPER_START "<when condition=\"cond\" />" ELEMENT_WRAPPER_END;
1655 assert_int_equal(test_element_helper(st, &data, &when, NULL, NULL, true), LY_SUCCESS);
1656 assert_string_equal(when->cond, "cond");
1657 lysp_when_free(st->ctx, when);
1658 free(when);
1659 when = NULL;
1660
1661 st->finished_correctly = true;
1662}
1663
1664static void
1665test_yin_text_value_elem(void **state)
1666{
1667 struct state *st = *state;
1668 const char *data;
1669 const char *val;
1670
1671 data = ELEMENT_WRAPPER_START "<text>text</text>" ELEMENT_WRAPPER_END;
1672 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1673 assert_string_equal(val, "text");
1674 FREE_STRING(st->ctx, val);
1675
1676 data = "<error-message xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <value>text</value> </error-message>";
1677 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1678 assert_string_equal(val, "text");
1679 FREE_STRING(st->ctx, val);
1680
1681 data = ELEMENT_WRAPPER_START "<text></text>" ELEMENT_WRAPPER_END;
1682 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1683 assert_string_equal("", val);
1684 FREE_STRING(st->ctx, val);
1685
David Sedlák8e7bda82019-07-16 17:57:50 +02001686 st->finished_correctly = true;
1687}
David Sedlák32488102019-07-15 17:44:10 +02001688
David Sedlák374d2b32019-07-17 15:06:55 +02001689static void
1690test_type_elem(void **state)
1691{
1692 struct state *st = *state;
1693 const char *data;
1694 struct lysp_type type = {};
1695
1696 /* max subelems */
1697 data = ELEMENT_WRAPPER_START
1698 "<type name=\"type-name\">"
1699 "<base name=\"base-name\"/>"
1700 "<bit name=\"bit\"/>"
1701 "<enum name=\"enum\"/>"
1702 "<fraction-digits value=\"2\"/>"
1703 "<length value=\"length\"/>"
1704 "<path value=\"path\"/>"
1705 "<pattern value=\"pattern\"/>"
1706 "<range value=\"range\" />"
1707 "<require-instance value=\"true\"/>"
1708 "<type name=\"sub-type-name\"/>"
1709 "</type>"
1710 ELEMENT_WRAPPER_END;
1711 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1712 assert_string_equal(type.name, "type-name");
1713 assert_string_equal(*type.bases, "base-name");
1714 assert_string_equal(type.bits->name, "bit");
1715 assert_string_equal(type.enums->name, "enum");
1716 assert_int_equal(type.fraction_digits, 2);
1717 assert_string_equal(type.length->arg, "length");
1718 assert_string_equal(type.path, "path");
1719 assert_string_equal(type.patterns->arg, "\006pattern");
1720 assert_string_equal(type.range->arg, "range");
1721 assert_int_equal(type.require_instance, 1);
1722 assert_string_equal(type.types->name, "sub-type-name");
1723 lysp_type_free(st->ctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001724 assert_true(type.flags & LYS_SET_BASE);
1725 assert_true(type.flags & LYS_SET_BIT);
1726 assert_true(type.flags & LYS_SET_ENUM);
1727 assert_true(type.flags & LYS_SET_FRDIGITS);
1728 assert_true(type.flags & LYS_SET_LENGTH);
1729 assert_true(type.flags & LYS_SET_PATH);
1730 assert_true(type.flags & LYS_SET_PATTERN);
1731 assert_true(type.flags & LYS_SET_RANGE);
1732 assert_true(type.flags & LYS_SET_REQINST);
1733 assert_true(type.flags & LYS_SET_TYPE);
David Sedlák374d2b32019-07-17 15:06:55 +02001734 memset(&type, 0, sizeof(type));
1735
1736 /* min subelems */
1737 data = ELEMENT_WRAPPER_START "<type name=\"type-name\"/>" ELEMENT_WRAPPER_END;
1738 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1739 lysp_type_free(st->ctx, &type);
1740 memset(&type, 0, sizeof(type));
1741
1742 st->finished_correctly = true;
1743}
1744
David Sedlák1af868e2019-07-17 17:03:14 +02001745static void
1746test_max_elems_elem(void **state)
1747{
1748 struct state *st = *state;
1749 const char *data;
1750 struct lysp_node_list list = {};
1751 struct lysp_node_leaflist llist = {};
1752 struct lysp_refine refine = {};
1753
1754 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"unbounded\"/> </refine>";
1755 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
1756 assert_int_equal(refine.max, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001757 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001758
1759 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"5\"/> </list>";
1760 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, true), LY_SUCCESS);
1761 assert_int_equal(list.max, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001762 assert_true(list.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001763
1764 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"85\"/> </leaf-list>";
1765 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, true), LY_SUCCESS);
1766 assert_int_equal(llist.max, 85);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001767 assert_true(llist.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001768
1769 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"10\"/> </refine>";
1770 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
1771 assert_int_equal(refine.max, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001772 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001773
1774 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"0\"/> </list>";
1775 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1776 logbuf_assert("Invalid value \"0\" of \"max-elements\". Line number 1.");
1777
1778 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"-10\"/> </list>";
1779 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1780 logbuf_assert("Invalid value \"-10\" of \"max-elements\". Line number 1.");
1781
1782 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"k\"/> </list>";
1783 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1784 logbuf_assert("Invalid value \"k\" of \"max-elements\". Line number 1.");
1785
1786 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"u12\"/> </list>";
1787 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1788 logbuf_assert("Invalid value \"u12\" of \"max-elements\". Line number 1.");
1789
1790 st->finished_correctly = true;
1791}
1792
David Sedlák09e18c92019-07-18 11:17:11 +02001793static void
1794test_min_elems_elem(void **state)
1795{
1796 struct state *st = *state;
1797 const char *data;
1798 struct lysp_node_list list = {};
1799 struct lysp_node_leaflist llist = {};
1800 struct lysp_refine refine = {};
1801
1802 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"0\"/> </refine>";
1803 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
1804 assert_int_equal(refine.min, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001805 assert_true(refine.flags & LYS_SET_MIN);
David Sedlák09e18c92019-07-18 11:17:11 +02001806
1807 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"41\"/> </list>";
1808 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, true), LY_SUCCESS);
1809 assert_int_equal(list.min, 41);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001810 assert_true(list.flags & LYS_SET_MIN);
David Sedlák09e18c92019-07-18 11:17:11 +02001811
1812 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"50\"/> </leaf-list>";
1813 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, true), LY_SUCCESS);
1814 assert_int_equal(llist.min, 50);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001815 assert_true(llist.flags & LYS_SET_MIN);
David Sedlák09e18c92019-07-18 11:17:11 +02001816
1817 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"-5\"/> </leaf-list>";
1818 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1819 logbuf_assert("Value \"-5\" is out of \"min-elements\" bounds. Line number 1.");
1820
1821 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"99999999999999999\"/> </leaf-list>";
1822 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1823 logbuf_assert("Value \"99999999999999999\" is out of \"min-elements\" bounds. Line number 1.");
1824
1825 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"5k\"/> </leaf-list>";
1826 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1827 logbuf_assert("Invalid value \"5k\" of \"min-elements\". Line number 1.");
1828
1829 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"05\"/> </leaf-list>";
1830 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1831 logbuf_assert("Invalid value \"05\" of \"min-elements\". Line number 1.");
1832
1833 st->finished_correctly = true;
1834}
1835
David Sedláka2dad212019-07-18 12:45:19 +02001836static void
1837test_ordby_elem(void **state)
1838{
1839 struct state *st = *state;
1840 const char *data;
1841 uint16_t flags = 0;
1842
1843 data = ELEMENT_WRAPPER_START "<ordered-by value=\"system\"/>" ELEMENT_WRAPPER_END;
1844 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001845 assert_true(flags & LYS_ORDBY_SYSTEM);
David Sedláka2dad212019-07-18 12:45:19 +02001846
1847 data = ELEMENT_WRAPPER_START "<ordered-by value=\"user\"/>" ELEMENT_WRAPPER_END;
1848 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001849 assert_true(flags & LYS_ORDBY_USER);
David Sedláka2dad212019-07-18 12:45:19 +02001850
1851 data = ELEMENT_WRAPPER_START "<ordered-by value=\"inv\"/>" ELEMENT_WRAPPER_END;
1852 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
1853 logbuf_assert("Invalid value \"inv\" of \"ordered-by\". Line number 1.");
1854
1855 st->finished_correctly = true;
1856}
1857
David Sedlák8a83bbb2019-07-18 14:46:00 +02001858static void
1859test_any_elem(void **state)
1860{
1861 struct state *st = *state;
1862 const char *data;
1863 struct lysp_node *siblings = NULL;
1864 struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
1865 struct lysp_node_anydata *parsed = NULL;
1866
1867 /* anyxml max subelems */
1868 data = ELEMENT_WRAPPER_START
1869 "<anyxml name=\"any-name\">"
1870 "<config value=\"true\" />"
1871 "<description><text>desc</text></description>"
1872 "<if-feature name=\"feature\" />"
1873 "<mandatory value=\"true\" />"
1874 "<must condition=\"must-cond\" />"
1875 "<reference><text>ref</text></reference>"
1876 "<status value=\"deprecated\"/>"
1877 "<when condition=\"when-cond\"/>"
1878 "</anyxml>"
1879 ELEMENT_WRAPPER_END;
1880 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1881 parsed = (struct lysp_node_anydata *)siblings;
1882 assert_null(parsed->parent);
1883 assert_int_equal(parsed->nodetype, LYS_ANYXML);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001884 assert_true(parsed->flags & LYS_CONFIG_W);
1885 assert_true(parsed->flags & LYS_MAND_TRUE);
1886 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001887 assert_null(parsed->next);
1888 assert_string_equal(parsed->name, "any-name");
1889 assert_string_equal(parsed->dsc, "desc");
1890 assert_string_equal(parsed->ref, "ref");
1891 assert_string_equal(parsed->when->cond, "when-cond");
1892 assert_string_equal(*parsed->iffeatures, "feature");
1893 assert_null(parsed->exts);
1894 lysp_node_free(st->ctx, siblings);
1895 siblings = NULL;
1896
1897 /* anydata max subelems */
1898 data = ELEMENT_WRAPPER_START
1899 "<anydata name=\"any-name\">"
1900 "<config value=\"true\" />"
1901 "<description><text>desc</text></description>"
1902 "<if-feature name=\"feature\" />"
1903 "<mandatory value=\"true\" />"
1904 "<must condition=\"must-cond\" />"
1905 "<reference><text>ref</text></reference>"
1906 "<status value=\"deprecated\"/>"
1907 "<when condition=\"when-cond\"/>"
1908 "</anydata>"
1909 ELEMENT_WRAPPER_END;
1910 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1911 parsed = (struct lysp_node_anydata *)siblings;
1912 assert_null(parsed->parent);
1913 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001914 assert_true(parsed->flags & LYS_CONFIG_W);
1915 assert_true(parsed->flags & LYS_MAND_TRUE);
1916 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001917 assert_null(parsed->next);
1918 assert_string_equal(parsed->name, "any-name");
1919 assert_string_equal(parsed->dsc, "desc");
1920 assert_string_equal(parsed->ref, "ref");
1921 assert_string_equal(parsed->when->cond, "when-cond");
1922 assert_string_equal(*parsed->iffeatures, "feature");
1923 assert_null(parsed->exts);
1924 lysp_node_free(st->ctx, siblings);
1925 siblings = NULL;
1926
1927 /* min subelems */
1928 node_meta.parent = (void *)0x10;
1929 data = ELEMENT_WRAPPER_START "<anydata name=\"any-name\"> </anydata>" ELEMENT_WRAPPER_END;
1930 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1931 parsed = (struct lysp_node_anydata *)siblings;
1932 assert_ptr_equal(parsed->parent, node_meta.parent);
1933 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
1934 assert_null(parsed->next);
1935 assert_null(parsed->exts);
1936 lysp_node_free(st->ctx, siblings);
1937
1938 st->finished_correctly = true;
1939}
1940
David Sedlák203ca3a2019-07-18 15:26:25 +02001941static void
1942test_leaf_elem(void **state)
1943{
1944 struct state *st = *state;
1945 const char *data;
1946 struct lysp_node *siblings = NULL;
1947 struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
1948 struct lysp_node_leaf *parsed = NULL;
1949
1950 /* max elements */
1951 data = ELEMENT_WRAPPER_START
1952 "<leaf name=\"leaf\">"
1953 "<config value=\"true\" />"
1954 "<default value=\"def-val\"/>"
1955 "<description><text>desc</text></description>"
1956 "<if-feature name=\"feature\" />"
1957 "<mandatory value=\"true\" />"
1958 "<must condition=\"must-cond\" />"
1959 "<reference><text>ref</text></reference>"
1960 "<status value=\"deprecated\"/>"
1961 "<type name=\"type\"/>"
1962 "<units name=\"uni\"/>"
1963 "<when condition=\"when-cond\"/>"
1964 "</leaf>"
1965 ELEMENT_WRAPPER_END;
1966 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1967 parsed = (struct lysp_node_leaf *)siblings;
1968 assert_null(parsed->parent);
1969 assert_int_equal(parsed->nodetype, LYS_LEAF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001970 assert_true(parsed->flags & LYS_CONFIG_W);
1971 assert_true(parsed->flags & LYS_MAND_TRUE);
1972 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák203ca3a2019-07-18 15:26:25 +02001973 assert_null(parsed->next);
1974 assert_string_equal(parsed->name, "leaf");
1975 assert_string_equal(parsed->dsc, "desc");
1976 assert_string_equal(parsed->ref, "ref");
1977 assert_string_equal(parsed->when->cond, "when-cond");
1978 assert_string_equal(*parsed->iffeatures, "feature");
1979 assert_null(parsed->exts);
1980 assert_string_equal(parsed->musts->arg, "must-cond");
1981 assert_string_equal(parsed->type.name, "type");
1982 assert_string_equal(parsed->units, "uni");
1983 assert_string_equal(parsed->dflt, "def-val");
1984 lysp_node_free(st->ctx, siblings);
1985 siblings = NULL;
1986
1987 /* min elements */
1988 data = ELEMENT_WRAPPER_START "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>" ELEMENT_WRAPPER_END;
1989 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1990 parsed = (struct lysp_node_leaf *)siblings;
1991 assert_string_equal(parsed->name, "leaf");
1992 assert_string_equal(parsed->type.name, "type");
1993 lysp_node_free(st->ctx, siblings);
1994 siblings = NULL;
1995
1996 st->finished_correctly = true;
1997}
1998
David Sedlákc3da3ef2019-07-19 12:56:08 +02001999static void
2000test_leaf_list_elem(void **state)
2001{
2002 struct state *st = *state;
2003 const char *data;
2004 struct lysp_node *siblings = NULL;
2005 struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
2006 struct lysp_node_leaflist *parsed = NULL;
2007
2008 data = ELEMENT_WRAPPER_START
2009 "<leaf-list name=\"llist\">"
2010 "<config value=\"true\" />"
2011 "<default value=\"def-val0\"/>"
2012 "<default value=\"def-val1\"/>"
2013 "<description><text>desc</text></description>"
2014 "<if-feature name=\"feature\"/>"
2015 "<max-elements value=\"5\"/>"
2016 "<must condition=\"must-cond\"/>"
2017 "<ordered-by value=\"user\" />"
2018 "<reference><text>ref</text></reference>"
2019 "<status value=\"current\"/>"
2020 "<type name=\"type\"/>"
2021 "<units name=\"uni\"/>"
2022 "<when condition=\"when-cond\"/>"
2023 "</leaf-list>"
2024 ELEMENT_WRAPPER_END;
2025 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2026 parsed = (struct lysp_node_leaflist *)siblings;
2027 assert_string_equal(parsed->dflts[0], "def-val0");
2028 assert_string_equal(parsed->dflts[1], "def-val1");
2029 assert_string_equal(parsed->dsc, "desc");
2030 assert_string_equal(*parsed->iffeatures, "feature");
2031 assert_int_equal(parsed->max, 5);
2032 assert_string_equal(parsed->musts->arg, "must-cond");
2033 assert_string_equal(parsed->name, "llist");
2034 assert_null(parsed->next);
2035 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2036 assert_null(parsed->parent);
2037 assert_string_equal(parsed->ref, "ref");
2038 assert_string_equal(parsed->type.name, "type");
2039 assert_string_equal(parsed->units, "uni");
2040 assert_string_equal(parsed->when->cond, "when-cond");
2041 assert_true(parsed->flags & LYS_CONFIG_W);
2042 assert_true(parsed->flags & LYS_ORDBY_USER);
2043 assert_true(parsed->flags & LYS_STATUS_CURR);
2044 lysp_node_free(st->ctx, siblings);
2045 siblings = NULL;
2046
2047 data = ELEMENT_WRAPPER_START
2048 "<leaf-list name=\"llist\">"
2049 "<config value=\"true\" />"
2050 "<description><text>desc</text></description>"
2051 "<if-feature name=\"feature\"/>"
2052 "<min-elements value=\"5\"/>"
2053 "<must condition=\"must-cond\"/>"
2054 "<ordered-by value=\"user\" />"
2055 "<reference><text>ref</text></reference>"
2056 "<status value=\"current\"/>"
2057 "<type name=\"type\"/>"
2058 "<units name=\"uni\"/>"
2059 "<when condition=\"when-cond\"/>"
2060 "</leaf-list>"
2061 ELEMENT_WRAPPER_END;
2062 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2063 parsed = (struct lysp_node_leaflist *)siblings;
2064 assert_string_equal(parsed->dsc, "desc");
2065 assert_string_equal(*parsed->iffeatures, "feature");
2066 assert_int_equal(parsed->min, 5);
2067 assert_string_equal(parsed->musts->arg, "must-cond");
2068 assert_string_equal(parsed->name, "llist");
2069 assert_null(parsed->next);
2070 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2071 assert_null(parsed->parent);
2072 assert_string_equal(parsed->ref, "ref");
2073 assert_string_equal(parsed->type.name, "type");
2074 assert_string_equal(parsed->units, "uni");
2075 assert_string_equal(parsed->when->cond, "when-cond");
2076 assert_true(parsed->flags & LYS_CONFIG_W);
2077 assert_true(parsed->flags & LYS_ORDBY_USER);
2078 assert_true(parsed->flags & LYS_STATUS_CURR);
2079 lysp_node_free(st->ctx, siblings);
2080 siblings = NULL;
2081
2082 data = ELEMENT_WRAPPER_START
2083 "<leaf-list name=\"llist\">"
2084 "<config value=\"true\" />"
2085 "<description><text>desc</text></description>"
2086 "<if-feature name=\"feature\"/>"
2087 "<max-elements value=\"15\"/>"
2088 "<min-elements value=\"5\"/>"
2089 "<must condition=\"must-cond\"/>"
2090 "<ordered-by value=\"user\" />"
2091 "<reference><text>ref</text></reference>"
2092 "<status value=\"current\"/>"
2093 "<type name=\"type\"/>"
2094 "<units name=\"uni\"/>"
2095 "<when condition=\"when-cond\"/>"
2096 "</leaf-list>"
2097 ELEMENT_WRAPPER_END;
2098 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2099 parsed = (struct lysp_node_leaflist *)siblings;
2100 assert_string_equal(parsed->dsc, "desc");
2101 assert_string_equal(*parsed->iffeatures, "feature");
2102 assert_int_equal(parsed->min, 5);
2103 assert_int_equal(parsed->max, 15);
2104 assert_string_equal(parsed->musts->arg, "must-cond");
2105 assert_string_equal(parsed->name, "llist");
2106 assert_null(parsed->next);
2107 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2108 assert_null(parsed->parent);
2109 assert_string_equal(parsed->ref, "ref");
2110 assert_string_equal(parsed->type.name, "type");
2111 assert_string_equal(parsed->units, "uni");
2112 assert_string_equal(parsed->when->cond, "when-cond");
2113 assert_true(parsed->flags & LYS_CONFIG_W);
2114 assert_true(parsed->flags & LYS_ORDBY_USER);
2115 assert_true(parsed->flags & LYS_STATUS_CURR);
2116 lysp_node_free(st->ctx, siblings);
2117 siblings = NULL;
2118
2119 data = ELEMENT_WRAPPER_START
2120 "<leaf-list name=\"llist\">"
2121 "<type name=\"type\"/>"
2122 "</leaf-list>"
2123 ELEMENT_WRAPPER_END;
2124 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2125 parsed = (struct lysp_node_leaflist *)siblings;
2126 assert_string_equal(parsed->name, "llist");
2127 assert_string_equal(parsed->type.name, "type");
2128 lysp_node_free(st->ctx, siblings);
2129 siblings = NULL;
2130
2131 /* invalid combinations */
2132 data = ELEMENT_WRAPPER_START
2133 "<leaf-list name=\"llist\">"
2134 "<max-elements value=\"5\"/>"
2135 "<min-elements value=\"15\"/>"
2136 "<type name=\"type\"/>"
2137 "</leaf-list>"
2138 ELEMENT_WRAPPER_END;
2139 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
2140 logbuf_assert("Invalid combination of min-elements and max-elements: min value 15 is bigger than the max value 5. Line number 1.");
2141 lysp_node_free(st->ctx, siblings);
2142 siblings = NULL;
2143
2144 data = ELEMENT_WRAPPER_START
2145 "<leaf-list name=\"llist\">"
2146 "<default value=\"def-val1\"/>"
2147 "<min-elements value=\"15\"/>"
2148 "<type name=\"type\"/>"
2149 "</leaf-list>"
2150 ELEMENT_WRAPPER_END;
2151 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
2152 logbuf_assert("Invalid combination of keywords \"min-elements\" and \"default\" as substatements of \"leaf-list\". Line number 1.");
2153 lysp_node_free(st->ctx, siblings);
2154 siblings = NULL;
2155
2156 data = ELEMENT_WRAPPER_START
2157 "<leaf-list name=\"llist\">"
2158 "</leaf-list>"
2159 ELEMENT_WRAPPER_END;
2160 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
2161 logbuf_assert("Missing mandatory subelement type of leaf-list element. Line number 1.");
2162 lysp_node_free(st->ctx, siblings);
2163 siblings = NULL;
2164
2165 st->finished_correctly = true;
2166}
2167
David Sedlákcb39f642019-07-19 13:19:55 +02002168static void
2169test_presence_elem(void **state)
2170{
2171 struct state *st = *state;
2172 const char *data;
2173 const char *val;
2174
2175 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\"/>" ELEMENT_WRAPPER_END;
2176 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2177 assert_string_equal(val, "presence-val");
2178 FREE_STRING(st->ctx, val);
2179
2180 data = ELEMENT_WRAPPER_START "<presence/>" ELEMENT_WRAPPER_END;
2181 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
2182 logbuf_assert("Missing mandatory attribute value of presence element. Line number 1.");
2183
2184 st->finished_correctly = true;
2185}
2186
David Sedlák12470a82019-07-19 13:44:36 +02002187static void
2188test_key_elem(void **state)
2189{
2190 struct state *st = *state;
2191 const char *data;
2192 const char *val;
2193
2194 data = ELEMENT_WRAPPER_START "<key value=\"key-value\"/>" ELEMENT_WRAPPER_END;
2195 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2196 assert_string_equal(val, "key-value");
2197 FREE_STRING(st->ctx, val);
2198
2199 data = ELEMENT_WRAPPER_START "<key/>" ELEMENT_WRAPPER_END;
2200 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
2201 logbuf_assert("Missing mandatory attribute value of key element. Line number 1.");
2202
2203 st->finished_correctly = true;
2204}
2205
David Sedlák04e17b22019-07-19 15:29:48 +02002206static void
2207test_typedef_elem(void **state)
2208{
2209 struct state *st = *state;
2210 const char *data;
2211 struct lysp_tpdf *tpdfs = NULL;
2212 struct typedef_meta typdef_meta = {NULL, &tpdfs};
2213
2214 data = ELEMENT_WRAPPER_START
2215 "<typedef name=\"tpdf-name\">"
2216 "<default value=\"def-val\"/>"
2217 "<description><text>desc-text</text></description>"
2218 "<reference><text>ref-text</text></reference>"
2219 "<status value=\"current\"/>"
2220 "<type name=\"type\"/>"
2221 "<units name=\"uni\"/>"
2222 "</typedef>"
2223 ELEMENT_WRAPPER_END;
2224 assert_int_equal(test_element_helper(st, &data, &typdef_meta, NULL, NULL, true), LY_SUCCESS);
2225 assert_string_equal(tpdfs[0].dflt, "def-val");
2226 assert_string_equal(tpdfs[0].dsc, "desc-text");
2227 assert_null(tpdfs[0].exts);
2228 assert_string_equal(tpdfs[0].name, "tpdf-name");
2229 assert_string_equal(tpdfs[0].ref, "ref-text");
2230 assert_string_equal(tpdfs[0].type.name, "type");
2231 assert_string_equal(tpdfs[0].units, "uni");
2232 assert_true(tpdfs[0].flags & LYS_STATUS_CURR);
2233 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2234 tpdfs = NULL;
2235
2236 data = ELEMENT_WRAPPER_START
2237 "<typedef name=\"tpdf-name\">"
2238 "<type name=\"type\"/>"
2239 "</typedef>"
2240 ELEMENT_WRAPPER_END;
2241 assert_int_equal(test_element_helper(st, &data, &typdef_meta, NULL, NULL, true), LY_SUCCESS);
2242 assert_string_equal(tpdfs[0].name, "tpdf-name");
2243 assert_string_equal(tpdfs[0].type.name, "type");
2244 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2245 tpdfs = NULL;
2246
2247 st->finished_correctly = true;
2248}
2249
David Sedlákd2d676a2019-07-22 11:28:19 +02002250static void
2251test_refine_elem(void **state)
2252{
2253 struct state *st = *state;
2254 const char *data;
2255 struct lysp_refine *refines = NULL;
2256
2257 /* max subelems */
2258 data = ELEMENT_WRAPPER_START
2259 "<refine target-node=\"target\">"
2260 "<if-feature name=\"feature\" />"
2261 "<must condition=\"cond\" />"
2262 "<presence value=\"presence\" />"
2263 "<default value=\"def\" />"
2264 "<config value=\"true\" />"
2265 "<mandatory value=\"true\" />"
2266 "<min-elements value=\"10\" />"
2267 "<max-elements value=\"20\" />"
2268 "<description><text>desc</text></description>"
2269 "<reference><text>ref</text></reference>"
2270 "</refine>"
2271 ELEMENT_WRAPPER_END;
2272 assert_int_equal(test_element_helper(st, &data, &refines, NULL, NULL, true), LY_SUCCESS);
2273 assert_string_equal(refines->nodeid, "target");
2274 assert_string_equal(*refines->dflts, "def");
2275 assert_string_equal(refines->dsc, "desc");
2276 assert_null(refines->exts);
2277 assert_true(refines->flags & LYS_CONFIG_W);
2278 assert_true(refines->flags & LYS_MAND_TRUE);
2279 assert_string_equal(*refines->iffeatures, "feature");
2280 assert_int_equal(refines->max, 20);
2281 assert_int_equal(refines->min, 10);
2282 assert_string_equal(refines->musts->arg, "cond");
2283 assert_string_equal(refines->presence, "presence");
2284 assert_string_equal(refines->ref, "ref");
2285 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2286 refines = NULL;
2287
2288 /* min subelems */
2289 data = ELEMENT_WRAPPER_START "<refine target-node=\"target\" />" ELEMENT_WRAPPER_END;
2290 assert_int_equal(test_element_helper(st, &data, &refines, NULL, NULL, true), LY_SUCCESS);
2291 assert_string_equal(refines->nodeid, "target");
2292 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2293 refines = NULL;
2294
2295 st->finished_correctly = true;
2296}
2297
David Sedlák0d6de5a2019-07-22 13:25:44 +02002298static void
2299test_uses_elem(void **state)
2300{
2301 struct state *st = *state;
2302 const char *data;
2303 struct lysp_node *siblings = NULL;
2304 struct tree_node_meta node_meta = {NULL, &siblings};
2305 struct lysp_node_uses *parsed = NULL;
2306
2307 /* max subelems */
2308 data = ELEMENT_WRAPPER_START
2309 "<uses name=\"uses-name\">"
2310 "<when condition=\"cond\" />"
2311 "<if-feature name=\"feature\" />"
2312 "<status value=\"obsolete\" />"
2313 "<description><text>desc</text></description>"
2314 "<reference><text>ref</text></reference>"
2315 "<refine target-node=\"target\"/>"
David Sedlák992fb7c2019-07-24 16:51:01 +02002316 "<augment target-node=\"target\" />"
David Sedlák0d6de5a2019-07-22 13:25:44 +02002317 "</uses>"
2318 ELEMENT_WRAPPER_END;
2319 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2320 parsed = (struct lysp_node_uses *)&siblings[0];
2321 assert_string_equal(parsed->name, "uses-name");
2322 assert_string_equal(parsed->dsc, "desc");
2323 assert_null(parsed->exts);
2324 assert_true(parsed->flags & LYS_STATUS_OBSLT);
2325 assert_string_equal(*parsed->iffeatures, "feature");
2326 assert_null(parsed->next);
2327 assert_int_equal(parsed->nodetype, LYS_USES);
2328 assert_null(parsed->parent);
2329 assert_string_equal(parsed->ref, "ref");
2330 assert_string_equal(parsed->refines->nodeid, "target");
2331 assert_string_equal(parsed->when->cond, "cond");
David Sedlák992fb7c2019-07-24 16:51:01 +02002332 assert_string_equal(parsed->augments->nodeid, "target");
David Sedlák0d6de5a2019-07-22 13:25:44 +02002333 lysp_node_free(st->ctx, siblings);
2334 siblings = NULL;
2335
2336 /* min subelems */
2337 data = ELEMENT_WRAPPER_START "<uses name=\"uses-name\"/>" ELEMENT_WRAPPER_END;
2338 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2339 assert_string_equal(siblings[0].name, "uses-name");
2340 lysp_node_free(st->ctx, siblings);
2341 siblings = NULL;
2342
2343 st->finished_correctly = true;
2344}
2345
David Sedlákaa854b02019-07-22 14:17:10 +02002346static void
2347test_revision_elem(void **state)
2348{
2349 struct state *st = *state;
2350 const char *data;
2351 struct lysp_revision *revs = NULL;
2352
2353 /* max subelems */
2354 data = ELEMENT_WRAPPER_START
2355 "<revision date=\"2018-12-25\">"
2356 "<description><text>desc</text></description>"
2357 "<reference><text>ref</text></reference>"
2358 "</revision>"
2359 ELEMENT_WRAPPER_END;
2360 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, true), LY_SUCCESS);
2361 assert_string_equal(revs->date, "2018-12-25");
2362 assert_string_equal(revs->dsc, "desc");
2363 assert_string_equal(revs->ref, "ref");
2364 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2365 revs = NULL;
2366
2367 /* min subelems */
2368 data = ELEMENT_WRAPPER_START "<revision date=\"2005-05-05\" />" ELEMENT_WRAPPER_END;
2369 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, true), LY_SUCCESS);
2370 assert_string_equal(revs->date, "2005-05-05");
2371 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2372 revs = NULL;
2373
2374 /* invalid value */
2375 data = ELEMENT_WRAPPER_START "<revision date=\"05-05-2005\" />" ELEMENT_WRAPPER_END;
2376 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, false), LY_EVALID);
2377 logbuf_assert("Invalid value \"05-05-2005\" of \"revision\". Line number 1.");
2378 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2379 revs = NULL;
2380
2381 st->finished_correctly = true;
2382}
2383
David Sedlák0c2bab92019-07-22 15:33:19 +02002384static void
2385test_include_elem(void **state)
2386{
2387 struct state *st = *state;
2388 const char *data;
2389 struct lysp_include *includes = NULL;
2390 struct include_meta inc_meta = {"module-name", &includes};
2391
2392 /* max subelems */
2393 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2394 data = ELEMENT_WRAPPER_START
2395 "<include module=\"mod\">"
2396 "<description><text>desc</text></description>"
2397 "<reference><text>ref</text></reference>"
2398 "<revision-date date=\"1999-09-09\"/>"
2399 "</include>"
2400 ELEMENT_WRAPPER_END;
2401 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, true), LY_SUCCESS);
2402 assert_string_equal(includes->name, "mod");
2403 assert_string_equal(includes->dsc, "desc");
2404 assert_string_equal(includes->ref, "ref");
2405 assert_null(includes->exts);
2406 assert_string_equal(includes->rev, "1999-09-09");
2407 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2408 includes = NULL;
2409
2410 /* min subelems */
2411 data = ELEMENT_WRAPPER_START "<include module=\"mod\"/>" ELEMENT_WRAPPER_END;
2412 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, true), LY_SUCCESS);
2413 assert_string_equal(includes->name, "mod");
2414 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2415 includes = NULL;
2416
2417 /* invalid combinations */
2418 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2419 data = ELEMENT_WRAPPER_START
2420 "<include module=\"mod\">"
2421 "<description><text>desc</text></description>"
2422 "<revision-date date=\"1999-09-09\"/>"
2423 "</include>"
2424 ELEMENT_WRAPPER_END;
2425 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, false), LY_EVALID);
2426 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.");
2427 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2428 includes = NULL;
2429
2430 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2431 data = ELEMENT_WRAPPER_START
2432 "<include module=\"mod\">"
2433 "<reference><text>ref</text></reference>"
2434 "<revision-date date=\"1999-09-09\"/>"
2435 "</include>"
2436 ELEMENT_WRAPPER_END;
2437 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, false), LY_EVALID);
2438 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.");
2439 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2440 includes = NULL;
2441
2442 st->finished_correctly = true;
2443}
2444
David Sedlák5e13dea2019-07-22 16:06:45 +02002445static void
2446test_feature_elem(void **state)
2447{
2448 struct state *st = *state;
2449 const char *data;
2450 struct lysp_feature *features = NULL;
2451
2452 /* max subelems */
2453 data = ELEMENT_WRAPPER_START
2454 "<feature name=\"feature-name\">"
2455 "<if-feature name=\"iff\"/>"
2456 "<status value=\"deprecated\"/>"
2457 "<description><text>desc</text></description>"
2458 "<reference><text>ref</text></reference>"
2459 "</feature>"
2460 ELEMENT_WRAPPER_END;
2461 assert_int_equal(test_element_helper(st, &data, &features, NULL, NULL, true), LY_SUCCESS);
2462 assert_string_equal(features->name, "feature-name");
2463 assert_string_equal(features->dsc, "desc");
2464 assert_null(features->exts);
2465 assert_true(features->flags & LYS_STATUS_DEPRC);
2466 assert_string_equal(*features->iffeatures, "iff");
2467 assert_string_equal(features->ref, "ref");
2468 FREE_ARRAY(st->ctx, features, lysp_feature_free);
2469 features = NULL;
2470
2471 /* min subelems */
2472 data = ELEMENT_WRAPPER_START "<feature name=\"feature-name\"/>" ELEMENT_WRAPPER_END;
2473 assert_int_equal(test_element_helper(st, &data, &features, NULL, NULL, true), LY_SUCCESS);
2474 assert_string_equal(features->name, "feature-name");
2475 FREE_ARRAY(st->ctx, features, lysp_feature_free);
2476 features = NULL;
2477
2478 st->finished_correctly = true;
2479}
2480
David Sedlák28794f22019-07-22 16:45:00 +02002481static void
2482test_identity_elem(void **state)
2483{
2484 struct state *st = *state;
2485 const char *data;
2486 struct lysp_ident *identities = NULL;
2487
2488 /* max subelems */
2489 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2490 data = ELEMENT_WRAPPER_START
2491 "<identity name=\"ident-name\">"
2492 "<if-feature name=\"iff\"/>"
2493 "<base name=\"base-name\"/>"
2494 "<status value=\"deprecated\"/>"
2495 "<description><text>desc</text></description>"
2496 "<reference><text>ref</text></reference>"
2497 "</identity>"
2498 ELEMENT_WRAPPER_END;
2499 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, true), LY_SUCCESS);
2500 assert_string_equal(identities->name, "ident-name");
2501 assert_string_equal(*identities->bases, "base-name");
2502 assert_string_equal(*identities->iffeatures, "iff");
2503 assert_string_equal(identities->dsc, "desc");
2504 assert_string_equal(identities->ref, "ref");
2505 assert_true(identities->flags & LYS_STATUS_DEPRC);
2506 assert_null(identities->exts);
2507 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2508 identities = NULL;
2509
2510 /* min subelems */
2511 data = ELEMENT_WRAPPER_START "<identity name=\"ident-name\" />" ELEMENT_WRAPPER_END;
2512 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, true), LY_SUCCESS);
2513 assert_string_equal(identities->name, "ident-name");
2514 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2515 identities = NULL;
2516
2517 /* invalid */
2518 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2519 data = ELEMENT_WRAPPER_START
2520 "<identity name=\"ident-name\">"
2521 "<if-feature name=\"iff\"/>"
2522 "</identity>"
2523 ELEMENT_WRAPPER_END;
2524 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, false), LY_EVALID);
2525 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.");
2526 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2527 identities = NULL;
2528
2529 st->finished_correctly = true;
2530}
2531
David Sedlákaf536aa2019-07-23 13:42:23 +02002532static void
2533test_list_elem(void **state)
2534{
2535 struct state *st = *state;
2536 const char *data;
2537 struct lysp_node *siblings = NULL;
2538 struct tree_node_meta node_meta = {NULL, &siblings};
2539 struct lysp_node_list *parsed = NULL;
2540
2541 /* max subelems */
2542 data = ELEMENT_WRAPPER_START
2543 "<list name=\"list-name\">"
2544 "<when condition=\"when\"/>"
2545 "<if-feature name=\"iff\"/>"
2546 "<must condition=\"must-cond\"/>"
2547 "<key value=\"key\"/>"
2548 "<unique tag=\"utag\"/>"
2549 "<config value=\"true\"/>"
2550 "<min-elements value=\"10\"/>"
2551 "<ordered-by value=\"user\"/>"
2552 "<status value=\"deprecated\"/>"
2553 "<description><text>desc</text></description>"
2554 "<reference><text>ref</text></reference>"
2555 "<anydata name=\"anyd\"/>"
2556 "<anyxml name=\"anyx\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02002557 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002558 "<choice name=\"choice\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02002559 "<action name=\"action\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02002560 "<grouping name=\"grp\"/>"
David Sedlák031b9e72019-07-23 15:19:37 +02002561 "<notification name=\"notf\"/>"
David Sedlákaf536aa2019-07-23 13:42:23 +02002562 "<leaf name=\"leaf\"/>"
2563 "<leaf-list name=\"llist\"/>"
2564 "<list name=\"sub-list\"/>"
2565 "<typedef name=\"tpdf\"/>"
2566 "<uses name=\"uses-name\"/>"
2567 "</list>"
2568 ELEMENT_WRAPPER_END;
2569 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2570 parsed = (struct lysp_node_list *)&siblings[0];
2571 assert_string_equal(parsed->dsc, "desc");
2572 assert_string_equal(parsed->child->name, "anyd");
2573 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2574 assert_string_equal(parsed->child->next->name, "anyx");
2575 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
David Sedlákf111bcb2019-07-23 17:15:51 +02002576 assert_string_equal(parsed->child->next->next->name, "cont");
2577 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002578 assert_string_equal(parsed->child->next->next->next->name, "choice");
2579 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
David Sedlák85d0eca2019-07-24 15:15:21 +02002580 assert_string_equal(parsed->child->next->next->next->next->name, "leaf");
2581 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAF);
2582 assert_string_equal(parsed->child->next->next->next->next->next->name, "llist");
2583 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2584 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "sub-list");
2585 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LIST);
2586 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "uses-name");
2587 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_USES);
2588 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002589 assert_string_equal(parsed->groupings->name, "grp");
David Sedlák85d0eca2019-07-24 15:15:21 +02002590 assert_string_equal(parsed->actions->name, "action");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002591 assert_int_equal(parsed->groupings->nodetype, LYS_GROUPING);
David Sedlák031b9e72019-07-23 15:19:37 +02002592 assert_string_equal(parsed->notifs->name, "notf");
David Sedlákaf536aa2019-07-23 13:42:23 +02002593 assert_null(parsed->exts);
2594 assert_true(parsed->flags & LYS_ORDBY_USER);
2595 assert_true(parsed->flags & LYS_STATUS_DEPRC);
2596 assert_true(parsed->flags & LYS_CONFIG_W);
2597 assert_string_equal(*parsed->iffeatures, "iff");
2598 assert_string_equal(parsed->key, "key");
2599 assert_int_equal(parsed->min, 10);
2600 assert_string_equal(parsed->musts->arg, "must-cond");
2601 assert_string_equal(parsed->name, "list-name");
2602 assert_null(parsed->next);
2603 assert_int_equal(parsed->nodetype, LYS_LIST);
2604 assert_null(parsed->parent);
2605 assert_string_equal(parsed->ref, "ref");
2606 assert_string_equal(parsed->typedefs->name, "tpdf");
2607 assert_string_equal(*parsed->uniques, "utag");
2608 assert_string_equal(parsed->when->cond, "when");
2609 lysp_node_free(st->ctx, siblings);
2610 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
2611 siblings = NULL;
2612
2613 /* min subelems */
2614 data = ELEMENT_WRAPPER_START "<list name=\"list-name\" />" ELEMENT_WRAPPER_END;
2615 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2616 parsed = (struct lysp_node_list *)&siblings[0];
2617 assert_string_equal(parsed->name, "list-name");
2618 lysp_node_free(st->ctx, siblings);
2619 siblings = NULL;
2620
2621 st->finished_correctly = true;
2622}
2623
David Sedlák031b9e72019-07-23 15:19:37 +02002624static void
2625test_notification_elem(void **state)
2626{
2627 struct state *st = *state;
2628 const char *data;
2629 struct lysp_notif *notifs = NULL;
2630 struct notif_meta notif_meta = {NULL, &notifs};
2631
2632 /* max subelems */
2633 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2634 data = ELEMENT_WRAPPER_START
2635 "<notification name=\"notif-name\">"
2636 "<anydata name=\"anyd\"/>"
2637 "<anyxml name=\"anyx\"/>"
2638 "<description><text>desc</text></description>"
2639 "<if-feature name=\"iff\"/>"
2640 "<leaf name=\"leaf\"/>"
2641 "<leaf-list name=\"llist\"/>"
2642 "<list name=\"sub-list\"/>"
2643 "<must condition=\"cond\"/>"
2644 "<reference><text>ref</text></reference>"
2645 "<status value=\"deprecated\"/>"
2646 "<typedef name=\"tpdf\"/>"
2647 "<uses name=\"uses-name\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02002648 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002649 "<choice name=\"choice\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02002650 "<grouping name=\"grp\"/>"
David Sedlák031b9e72019-07-23 15:19:37 +02002651 "</notification>"
2652 ELEMENT_WRAPPER_END;
2653 assert_int_equal(test_element_helper(st, &data, &notif_meta, NULL, NULL, true), LY_SUCCESS);
2654 assert_string_equal(notifs->name, "notif-name");
2655 assert_string_equal(notifs->data->name, "anyd");
2656 assert_int_equal(notifs->data->nodetype, LYS_ANYDATA);
2657 assert_string_equal(notifs->data->next->name, "anyx");
2658 assert_int_equal(notifs->data->next->nodetype, LYS_ANYXML);
2659 assert_string_equal(notifs->data->next->next->name, "leaf");
2660 assert_int_equal(notifs->data->next->next->nodetype, LYS_LEAF);
2661 assert_string_equal(notifs->data->next->next->next->name, "llist");
2662 assert_int_equal(notifs->data->next->next->next->nodetype, LYS_LEAFLIST);
2663 assert_string_equal(notifs->data->next->next->next->next->name, "sub-list");
2664 assert_int_equal(notifs->data->next->next->next->next->nodetype, LYS_LIST);
2665 assert_null(notifs->exts);
2666 assert_true(notifs->flags & LYS_STATUS_DEPRC);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002667 assert_string_equal(notifs->groupings->name, "grp");
2668 assert_int_equal(notifs->groupings->nodetype, LYS_GROUPING);
David Sedlákf111bcb2019-07-23 17:15:51 +02002669 assert_string_equal(notifs->data->next->next->next->next->next->name, "uses-name");
2670 assert_int_equal(notifs->data->next->next->next->next->next->nodetype, LYS_USES);
2671 assert_string_equal(notifs->data->next->next->next->next->next->next->name, "cont");
2672 assert_int_equal(notifs->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002673 assert_int_equal(notifs->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2674 assert_string_equal(notifs->data->next->next->next->next->next->next->next->name, "choice");
2675 assert_null(notifs->data->next->next->next->next->next->next->next->next);
David Sedlák031b9e72019-07-23 15:19:37 +02002676 assert_string_equal(*notifs->iffeatures, "iff");
2677 assert_string_equal(notifs->musts->arg, "cond");
2678 assert_int_equal(notifs->nodetype, LYS_NOTIF);
2679 assert_null(notifs->parent);
2680 assert_string_equal(notifs->ref, "ref");
2681 assert_string_equal(notifs->typedefs->name, "tpdf");
2682 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
2683 notifs = NULL;
2684
2685 /* min subelems */
2686 data = ELEMENT_WRAPPER_START "<notification name=\"notif-name\" />" ELEMENT_WRAPPER_END;
2687 assert_int_equal(test_element_helper(st, &data, &notif_meta, NULL, NULL, true), LY_SUCCESS);
2688 assert_string_equal(notifs->name, "notif-name");
2689 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002690 notifs = NULL;
2691
2692 st->finished_correctly = true;
2693}
2694
2695static void
2696test_grouping_elem(void **state)
2697{
2698 struct state *st = *state;
2699 const char *data;
2700 struct lysp_grp *grps = NULL;
2701 struct grouping_meta grp_meta = {NULL, &grps};
2702
2703 /* max subelems */
2704 data = ELEMENT_WRAPPER_START
2705 "<grouping name=\"grp-name\">"
2706 "<anydata name=\"anyd\"/>"
2707 "<anyxml name=\"anyx\"/>"
2708 "<description><text>desc</text></description>"
2709 "<grouping name=\"sub-grp\"/>"
2710 "<leaf name=\"leaf\"/>"
2711 "<leaf-list name=\"llist\"/>"
2712 "<list name=\"list\"/>"
2713 "<notification name=\"notf\"/>"
2714 "<reference><text>ref</text></reference>"
2715 "<status value=\"current\"/>"
2716 "<typedef name=\"tpdf\"/>"
2717 "<uses name=\"uses-name\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02002718 "<action name=\"act\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02002719 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002720 "<choice name=\"choice\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02002721 "</grouping>"
2722 ELEMENT_WRAPPER_END;
2723 assert_int_equal(test_element_helper(st, &data, &grp_meta, NULL, NULL, true), LY_SUCCESS);
2724 assert_string_equal(grps->name, "grp-name");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002725 assert_string_equal(grps->data->name, "anyd");
2726 assert_string_equal(grps->data->next->name, "anyx");
2727 assert_string_equal(grps->data->next->next->name, "leaf");
2728 assert_string_equal(grps->data->next->next->next->name, "llist");
2729 assert_string_equal(grps->data->next->next->next->next->name, "list");
2730 assert_string_equal(grps->dsc, "desc");
2731 assert_null(grps->exts);
2732 assert_true(grps->flags & LYS_STATUS_CURR);
2733 assert_string_equal(grps->groupings->name, "sub-grp");
2734 assert_int_equal(grps->nodetype, LYS_GROUPING);
2735 assert_string_equal(grps->notifs->name, "notf");
2736 assert_null(grps->parent);
2737 assert_string_equal(grps->ref, "ref");
2738 assert_string_equal(grps->typedefs->name, "tpdf");
David Sedlák85d0eca2019-07-24 15:15:21 +02002739 assert_string_equal(grps->actions->name, "act");
David Sedlákf111bcb2019-07-23 17:15:51 +02002740 assert_string_equal(grps->data->next->next->next->next->next->name, "uses-name");
David Sedlákb7abcfa2019-07-24 12:33:35 +02002741 assert_int_equal(grps->data->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákf111bcb2019-07-23 17:15:51 +02002742 assert_string_equal(grps->data->next->next->next->next->next->next->name, "cont");
David Sedlákb7abcfa2019-07-24 12:33:35 +02002743 assert_int_equal(grps->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
2744 assert_string_equal(grps->data->next->next->next->next->next->next->next->name, "choice");
2745 assert_int_equal(grps->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002746 FREE_ARRAY(st->ctx, grps, lysp_grp_free);
2747 grps = NULL;
2748
2749 /* min subelems */
2750 data = ELEMENT_WRAPPER_START "<grouping name=\"grp-name\" />" ELEMENT_WRAPPER_END;
2751 assert_int_equal(test_element_helper(st, &data, &grp_meta, NULL, NULL, true), LY_SUCCESS);
2752 assert_string_equal(grps->name, "grp-name");
2753 FREE_ARRAY(st->ctx, grps, lysp_grp_free);
2754 grps = NULL;
David Sedlák031b9e72019-07-23 15:19:37 +02002755
2756 st->finished_correctly = true;
2757}
2758
David Sedlákf111bcb2019-07-23 17:15:51 +02002759static void
2760test_container_elem(void **state)
2761{
2762 struct state *st = *state;
2763 const char *data;
2764 struct lysp_node *siblings = NULL;
2765 struct tree_node_meta node_meta = {NULL, &siblings};
2766 struct lysp_node_container *parsed = NULL;
2767
2768 /* max subelems */
David Sedláke2dc9e92019-07-24 09:59:21 +02002769 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2770 data = ELEMENT_WRAPPER_START
2771 "<container name=\"cont-name\">"
2772 "<anydata name=\"anyd\"/>"
2773 "<anyxml name=\"anyx\"/>"
2774 "<config value=\"true\"/>"
2775 "<container name=\"subcont\"/>"
2776 "<description><text>desc</text></description>"
2777 "<grouping name=\"sub-grp\"/>"
2778 "<if-feature name=\"iff\"/>"
2779 "<leaf name=\"leaf\"/>"
2780 "<leaf-list name=\"llist\"/>"
2781 "<list name=\"list\"/>"
2782 "<must condition=\"cond\"/>"
2783 "<notification name=\"notf\"/>"
2784 "<presence value=\"presence\"/>"
2785 "<reference><text>ref</text></reference>"
2786 "<status value=\"current\"/>"
2787 "<typedef name=\"tpdf\"/>"
2788 "<uses name=\"uses-name\"/>"
2789 "<when condition=\"when-cond\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02002790 "<action name=\"act\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002791 "<choice name=\"choice\"/>"
David Sedláke2dc9e92019-07-24 09:59:21 +02002792 "</container>"
2793 ELEMENT_WRAPPER_END;
2794 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2795 parsed = (struct lysp_node_container *)siblings;
2796 assert_string_equal(parsed->name, "cont-name");
2797 assert_null(parsed->parent);
2798 assert_int_equal(parsed->nodetype, LYS_CONTAINER);
2799 assert_true(parsed->flags & LYS_CONFIG_W);
2800 assert_true(parsed->flags & LYS_STATUS_CURR);
2801 assert_null(parsed->next);
2802 assert_string_equal(parsed->dsc, "desc");
2803 assert_string_equal(parsed->ref, "ref");
2804 assert_string_equal(parsed->when->cond, "when-cond");
2805 assert_string_equal(*parsed->iffeatures, "iff");
2806 assert_null(parsed->exts);
2807 assert_string_equal(parsed->musts->arg, "cond");
2808 assert_string_equal(parsed->presence, "presence");
2809 assert_string_equal(parsed->typedefs->name, "tpdf");
2810 assert_string_equal(parsed->groupings->name, "sub-grp");
2811 assert_string_equal(parsed->child->name, "anyd");
2812 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2813 assert_string_equal(parsed->child->next->name, "anyx");
2814 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2815 assert_string_equal(parsed->child->next->next->name, "subcont");
2816 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
2817 assert_string_equal(parsed->child->next->next->next->name, "leaf");
2818 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
2819 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
2820 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
2821 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
2822 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
2823 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
2824 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002825 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
2826 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2827 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke2dc9e92019-07-24 09:59:21 +02002828 assert_string_equal(parsed->notifs->name, "notf");
David Sedlák85d0eca2019-07-24 15:15:21 +02002829 assert_string_equal(parsed->actions->name, "act");
David Sedláke2dc9e92019-07-24 09:59:21 +02002830 lysp_node_free(st->ctx, siblings);
2831 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
2832 siblings = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02002833
2834 /* min subelems */
2835 data = ELEMENT_WRAPPER_START "<container name=\"cont-name\" />" ELEMENT_WRAPPER_END;
2836 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2837 parsed = (struct lysp_node_container *)siblings;
2838 assert_string_equal(parsed->name, "cont-name");
2839 lysp_node_free(st->ctx, siblings);
2840 siblings = NULL;
2841
2842 st->finished_correctly = true;
2843}
2844
David Sedlák5379d392019-07-24 10:42:03 +02002845static void
2846test_case_elem(void **state)
2847{
2848 struct state *st = *state;
2849 const char *data;
2850 struct lysp_node *siblings = NULL;
2851 struct tree_node_meta node_meta = {NULL, &siblings};
2852 struct lysp_node_case *parsed = NULL;
2853
2854 /* max subelems */
2855 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2856 data = ELEMENT_WRAPPER_START
2857 "<case name=\"case-name\">"
2858 "<anydata name=\"anyd\"/>"
2859 "<anyxml name=\"anyx\"/>"
2860 "<container name=\"subcont\"/>"
2861 "<description><text>desc</text></description>"
2862 "<if-feature name=\"iff\"/>"
2863 "<leaf name=\"leaf\"/>"
2864 "<leaf-list name=\"llist\"/>"
2865 "<list name=\"list\"/>"
2866 "<reference><text>ref</text></reference>"
2867 "<status value=\"current\"/>"
2868 "<uses name=\"uses-name\"/>"
2869 "<when condition=\"when-cond\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002870 "<choice name=\"choice\"/>"
David Sedlák5379d392019-07-24 10:42:03 +02002871 "</case>"
2872 ELEMENT_WRAPPER_END;
2873 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2874 parsed = (struct lysp_node_case *)siblings;
2875 assert_string_equal(parsed->name, "case-name");
2876 assert_null(parsed->parent);
2877 assert_int_equal(parsed->nodetype, LYS_CASE);
2878 assert_true(parsed->flags & LYS_STATUS_CURR);
2879 assert_null(parsed->next);
2880 assert_string_equal(parsed->dsc, "desc");
2881 assert_string_equal(parsed->ref, "ref");
2882 assert_string_equal(parsed->when->cond, "when-cond");
2883 assert_string_equal(*parsed->iffeatures, "iff");
2884 assert_null(parsed->exts);
2885 assert_string_equal(parsed->child->name, "anyd");
2886 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2887 assert_string_equal(parsed->child->next->name, "anyx");
2888 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2889 assert_string_equal(parsed->child->next->next->name, "subcont");
2890 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
2891 assert_string_equal(parsed->child->next->next->next->name, "leaf");
2892 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
2893 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
2894 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
2895 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
2896 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
2897 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
2898 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002899 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
2900 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2901 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedlák5379d392019-07-24 10:42:03 +02002902 lysp_node_free(st->ctx, siblings);
2903 siblings = NULL;
2904
2905 /* min subelems */
2906 data = ELEMENT_WRAPPER_START "<case name=\"case-name\" />" ELEMENT_WRAPPER_END;
2907 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2908 parsed = (struct lysp_node_case *)siblings;
2909 assert_string_equal(parsed->name, "case-name");
2910 lysp_node_free(st->ctx, siblings);
2911 siblings = NULL;
2912
2913 st->finished_correctly = true;
2914}
2915
David Sedlákb7abcfa2019-07-24 12:33:35 +02002916static void
2917test_choice_elem(void **state)
2918{
2919 struct state *st = *state;
2920 const char *data;
2921 struct lysp_node *siblings = NULL;
2922 struct tree_node_meta node_meta = {NULL, &siblings};
2923 struct lysp_node_choice *parsed = NULL;
2924
2925 /* max subelems */
2926 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2927 data = ELEMENT_WRAPPER_START
2928 "<choice name=\"choice-name\">"
2929 "<anydata name=\"anyd\"/>"
2930 "<anyxml name=\"anyx\"/>"
2931 "<case name=\"sub-case\"/>"
2932 "<choice name=\"choice\"/>"
2933 "<config value=\"true\"/>"
2934 "<container name=\"subcont\"/>"
2935 "<default value=\"def\"/>"
2936 "<description><text>desc</text></description>"
2937 "<if-feature name=\"iff\"/>"
2938 "<leaf name=\"leaf\"/>"
2939 "<leaf-list name=\"llist\"/>"
2940 "<list name=\"list\"/>"
2941 "<mandatory value=\"true\" />"
2942 "<reference><text>ref</text></reference>"
2943 "<status value=\"current\"/>"
2944 "<when condition=\"when-cond\"/>"
2945 "</choice>"
2946 ELEMENT_WRAPPER_END;
2947 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2948 parsed = (struct lysp_node_choice *)siblings;
2949 assert_string_equal(parsed->name, "choice-name");
2950 assert_null(parsed->parent);
2951 assert_int_equal(parsed->nodetype, LYS_CHOICE);
2952 assert_true(parsed->flags & LYS_CONFIG_W && parsed->flags & LYS_MAND_TRUE && parsed->flags & LYS_STATUS_CURR);
2953 assert_null(parsed->next);
2954 assert_string_equal(parsed->dsc, "desc");
2955 assert_string_equal(parsed->ref, "ref");
2956 assert_string_equal(parsed->when->cond, "when-cond");
2957 assert_string_equal(*parsed->iffeatures, "iff");
2958 assert_null(parsed->exts);
2959 assert_string_equal(parsed->child->name, "anyd");
2960 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2961 assert_string_equal(parsed->child->next->name, "anyx");
2962 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2963 assert_string_equal(parsed->child->next->next->name, "sub-case");
2964 assert_int_equal(parsed->child->next->next->nodetype, LYS_CASE);
2965 assert_string_equal(parsed->child->next->next->next->name, "choice");
2966 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
2967 assert_string_equal(parsed->child->next->next->next->next->name, "subcont");
2968 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_CONTAINER);
2969 assert_string_equal(parsed->child->next->next->next->next->next->name, "leaf");
2970 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAF);
2971 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "llist");
2972 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2973 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "list");
2974 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
2975 assert_null(parsed->child->next->next->next->next->next->next->next->next);
2976 lysp_node_free(st->ctx, siblings);
2977 siblings = NULL;
2978
2979 /* min subelems */
2980 data = ELEMENT_WRAPPER_START "<choice name=\"choice-name\" />" ELEMENT_WRAPPER_END;
2981 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2982 parsed = (struct lysp_node_choice *)siblings;
2983 assert_string_equal(parsed->name, "choice-name");
2984 lysp_node_free(st->ctx, siblings);
2985 siblings = NULL;
2986
2987 st->finished_correctly = true;
2988}
2989
David Sedlák05404f62019-07-24 14:11:53 +02002990static void
2991test_inout_elem(void **state)
2992{
2993 struct state *st = *state;
2994 const char *data;
2995 struct lysp_action_inout inout = {};
2996 struct inout_meta inout_meta = {NULL, &inout};
2997
2998 /* max subelements */
2999 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3000 data = ELEMENT_WRAPPER_START
3001 "<input>"
3002 "<anydata name=\"anyd\"/>"
3003 "<anyxml name=\"anyx\"/>"
3004 "<choice name=\"choice\"/>"
3005 "<container name=\"subcont\"/>"
3006 "<grouping name=\"sub-grp\"/>"
3007 "<leaf name=\"leaf\"/>"
3008 "<leaf-list name=\"llist\"/>"
3009 "<list name=\"list\"/>"
3010 "<must condition=\"cond\"/>"
3011 "<typedef name=\"tpdf\"/>"
3012 "<uses name=\"uses-name\"/>"
3013 "</input>"
3014 ELEMENT_WRAPPER_END;
3015 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3016 assert_null(inout.parent);
3017 assert_int_equal(inout.nodetype, LYS_INPUT);
3018 assert_string_equal(inout.musts->arg, "cond");
3019 assert_string_equal(inout.typedefs->name, "tpdf");
3020 assert_string_equal(inout.groupings->name, "sub-grp");
3021 assert_string_equal(inout.data->name, "anyd");
3022 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
3023 assert_string_equal(inout.data->next->name, "anyx");
3024 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
3025 assert_string_equal(inout.data->next->next->name, "choice");
3026 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
3027 assert_string_equal(inout.data->next->next->next->name, "subcont");
3028 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
3029 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
3030 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
3031 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
3032 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3033 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
3034 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
3035 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
3036 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3037 assert_null(inout.data->next->next->next->next->next->next->next->next);
3038 lysp_action_inout_free(st->ctx, &inout);
3039 memset(&inout, 0, sizeof inout);
3040
3041 /* max subelements */
3042 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3043 data = ELEMENT_WRAPPER_START
3044 "<output>"
3045 "<anydata name=\"anyd\"/>"
3046 "<anyxml name=\"anyx\"/>"
3047 "<choice name=\"choice\"/>"
3048 "<container name=\"subcont\"/>"
3049 "<grouping name=\"sub-grp\"/>"
3050 "<leaf name=\"leaf\"/>"
3051 "<leaf-list name=\"llist\"/>"
3052 "<list name=\"list\"/>"
3053 "<must condition=\"cond\"/>"
3054 "<typedef name=\"tpdf\"/>"
3055 "<uses name=\"uses-name\"/>"
3056 "</output>"
3057 ELEMENT_WRAPPER_END;
3058 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3059 assert_null(inout.parent);
3060 assert_int_equal(inout.nodetype, LYS_OUTPUT);
3061 assert_string_equal(inout.musts->arg, "cond");
3062 assert_string_equal(inout.typedefs->name, "tpdf");
3063 assert_string_equal(inout.groupings->name, "sub-grp");
3064 assert_string_equal(inout.data->name, "anyd");
3065 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
3066 assert_string_equal(inout.data->next->name, "anyx");
3067 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
3068 assert_string_equal(inout.data->next->next->name, "choice");
3069 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
3070 assert_string_equal(inout.data->next->next->next->name, "subcont");
3071 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
3072 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
3073 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
3074 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
3075 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3076 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
3077 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
3078 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
3079 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3080 assert_null(inout.data->next->next->next->next->next->next->next->next);
3081 lysp_action_inout_free(st->ctx, &inout);
3082 memset(&inout, 0, sizeof inout);
3083
3084 /* min subelems */
3085 data = ELEMENT_WRAPPER_START "<input />" ELEMENT_WRAPPER_END;
3086 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3087 memset(&inout, 0, sizeof inout);
3088
3089 data = ELEMENT_WRAPPER_START "<output />" ELEMENT_WRAPPER_END;
3090 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3091 memset(&inout, 0, sizeof inout);
3092
3093 /* invalid combinations */
3094 data = ELEMENT_WRAPPER_START "<input name=\"test\"/>" ELEMENT_WRAPPER_END;
3095 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, false), LY_EVALID);
3096 logbuf_assert("Unexpected attribute \"name\" of input element. Line number 1.");
3097 memset(&inout, 0, sizeof inout);
3098
3099 st->finished_correctly = true;
3100}
3101
David Sedlák85d0eca2019-07-24 15:15:21 +02003102static void
3103test_action_elem(void **state)
3104{
3105 struct state *st = *state;
3106 const char *data;
3107 struct lysp_action *actions = NULL;
3108 struct action_meta act_meta = {NULL, &actions};
3109
3110 /* max subelems */
3111 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3112 data = ELEMENT_WRAPPER_START
3113 "<action name=\"act\">"
3114 "<description><text>desc</text></description>"
3115 "<grouping name=\"grouping\"/>"
3116 "<if-feature name=\"iff\"/>"
3117 "<input><uses name=\"uses-name\"/></input>"
3118 "<output><must condition=\"cond\"/></output>"
3119 "<reference><text>ref</text></reference>"
3120 "<status value=\"deprecated\"/>"
3121 "<typedef name=\"tpdf\"/>"
3122 "</action>"
3123 ELEMENT_WRAPPER_END;
3124 assert_int_equal(test_element_helper(st, &data, &act_meta, NULL, NULL, true), LY_SUCCESS);
3125 assert_null(actions->parent);
3126 assert_int_equal(actions->nodetype, LYS_ACTION);
3127 assert_true(actions->flags & LYS_STATUS_DEPRC);
3128 assert_string_equal(actions->name, "act");
3129 assert_string_equal(actions->dsc, "desc");
3130 assert_string_equal(actions->ref, "ref");
3131 assert_string_equal(*actions->iffeatures, "iff");
3132 assert_string_equal(actions->typedefs->name, "tpdf");
3133 assert_string_equal(actions->groupings->name, "grouping");
3134 assert_string_equal(actions->input.data->name, "uses-name");
3135 assert_string_equal(actions->output.musts->arg, "cond");
3136 assert_null(actions->exts);
3137 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3138 actions = NULL;
3139
David Sedlákeaa45792019-07-24 15:25:01 +02003140 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3141 data = ELEMENT_WRAPPER_START
3142 "<rpc name=\"act\">"
3143 "<description><text>desc</text></description>"
3144 "<grouping name=\"grouping\"/>"
3145 "<if-feature name=\"iff\"/>"
3146 "<input><uses name=\"uses-name\"/></input>"
3147 "<output><must condition=\"cond\"/></output>"
3148 "<reference><text>ref</text></reference>"
3149 "<status value=\"deprecated\"/>"
3150 "<typedef name=\"tpdf\"/>"
3151 "</rpc>"
3152 ELEMENT_WRAPPER_END;
3153 assert_int_equal(test_element_helper(st, &data, &act_meta, NULL, NULL, true), LY_SUCCESS);
3154 assert_null(actions->parent);
3155 assert_int_equal(actions->nodetype, LYS_ACTION);
3156 assert_true(actions->flags & LYS_STATUS_DEPRC);
3157 assert_string_equal(actions->name, "act");
3158 assert_string_equal(actions->dsc, "desc");
3159 assert_string_equal(actions->ref, "ref");
3160 assert_string_equal(*actions->iffeatures, "iff");
3161 assert_string_equal(actions->typedefs->name, "tpdf");
3162 assert_string_equal(actions->groupings->name, "grouping");
3163 assert_string_equal(actions->input.data->name, "uses-name");
3164 assert_string_equal(actions->output.musts->arg, "cond");
3165 assert_null(actions->exts);
3166 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3167 actions = NULL;
3168
David Sedlák85d0eca2019-07-24 15:15:21 +02003169 /* min subelems */
3170 data = ELEMENT_WRAPPER_START "<action name=\"act\" />" ELEMENT_WRAPPER_END;
3171 assert_int_equal(test_element_helper(st, &data, &act_meta, NULL, NULL, true), LY_SUCCESS);
3172 assert_string_equal(actions->name, "act");
3173 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3174 actions = NULL;
3175
3176 st->finished_correctly = true;
3177}
3178
David Sedlák992fb7c2019-07-24 16:51:01 +02003179static void
3180test_augment_elem(void **state)
3181{
3182 struct state *st = *state;
3183 const char *data;
3184 struct lysp_augment *augments = NULL;
3185 struct augment_meta aug_meta = {NULL, &augments};
3186
3187 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3188 data = ELEMENT_WRAPPER_START
3189 "<augment target-node=\"target\">"
3190 "<action name=\"action\"/>"
3191 "<anydata name=\"anyd\"/>"
3192 "<anyxml name=\"anyx\"/>"
3193 "<case name=\"case\"/>"
3194 "<choice name=\"choice\"/>"
3195 "<container name=\"subcont\"/>"
3196 "<description><text>desc</text></description>"
3197 "<if-feature name=\"iff\"/>"
3198 "<leaf name=\"leaf\"/>"
3199 "<leaf-list name=\"llist\"/>"
3200 "<list name=\"list\"/>"
3201 "<notification name=\"notif\"/>"
3202 "<reference><text>ref</text></reference>"
3203 "<status value=\"current\"/>"
3204 "<uses name=\"uses\"/>"
3205 "<when condition=\"when-cond\"/>"
3206 "</augment>"
3207 ELEMENT_WRAPPER_END;
3208 assert_int_equal(test_element_helper(st, &data, &aug_meta, NULL, NULL, true), LY_SUCCESS);
3209 assert_string_equal(augments->nodeid, "target");
3210 assert_null(augments->parent);
3211 assert_int_equal(augments->nodetype, LYS_AUGMENT);
3212 assert_true(augments->flags & LYS_STATUS_CURR);
3213 assert_string_equal(augments->dsc, "desc");
3214 assert_string_equal(augments->ref, "ref");
3215 assert_string_equal(augments->when->cond, "when-cond");
3216 assert_string_equal(*augments->iffeatures, "iff");
3217 assert_string_equal(augments->child->name, "anyd");
3218 assert_int_equal(augments->child->nodetype, LYS_ANYDATA);
3219 assert_string_equal(augments->child->next->name, "anyx");
3220 assert_int_equal(augments->child->next->nodetype, LYS_ANYXML);
3221 assert_string_equal(augments->child->next->next->name, "case");
3222 assert_int_equal(augments->child->next->next->nodetype, LYS_CASE);
3223 assert_string_equal(augments->child->next->next->next->name, "choice");
3224 assert_int_equal(augments->child->next->next->next->nodetype, LYS_CHOICE);
3225 assert_string_equal(augments->child->next->next->next->next->name, "subcont");
3226 assert_int_equal(augments->child->next->next->next->next->nodetype, LYS_CONTAINER);
3227 assert_string_equal(augments->child->next->next->next->next->next->name, "leaf");
3228 assert_int_equal(augments->child->next->next->next->next->next->nodetype, LYS_LEAF);
3229 assert_string_equal(augments->child->next->next->next->next->next->next->name, "llist");
3230 assert_int_equal(augments->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3231 assert_string_equal(augments->child->next->next->next->next->next->next->next->name, "list");
3232 assert_int_equal(augments->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
3233 assert_string_equal(augments->child->next->next->next->next->next->next->next->next->name, "uses");
3234 assert_int_equal(augments->child->next->next->next->next->next->next->next->next->nodetype, LYS_USES);
3235 assert_null(augments->child->next->next->next->next->next->next->next->next->next);
3236 assert_string_equal(augments->actions->name, "action");
3237 assert_string_equal(augments->notifs->name, "notif");
3238 assert_null(augments->exts);
3239 FREE_ARRAY(st->ctx, augments, lysp_augment_free)
3240 augments = NULL;
3241
3242 data = ELEMENT_WRAPPER_START "<augment target-node=\"target\" />" ELEMENT_WRAPPER_END;
3243 assert_int_equal(test_element_helper(st, &data, &aug_meta, NULL, NULL, true), LY_SUCCESS);
3244 assert_string_equal(augments->nodeid, "target");
3245 FREE_ARRAY(st->ctx, augments, lysp_augment_free)
3246 augments = NULL;
3247
3248 st->finished_correctly = true;
3249}
3250
David Sedlák4ffcec82019-07-25 15:10:21 +02003251static void
3252test_deviate_elem(void **state)
3253{
3254 struct state *st = *state;
3255 const char *data;
3256 struct lysp_deviate *deviates = NULL;
3257 struct lysp_deviate_add *d_add;
3258 struct lysp_deviate_rpl *d_rpl;
3259 struct lysp_deviate_del *d_del;
3260
3261 /* all valid arguments with min subelems */
3262 data = ELEMENT_WRAPPER_START "<deviate value=\"not-supported\" />" ELEMENT_WRAPPER_END;
3263 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3264 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
3265 lysp_deviate_free(st->ctx, deviates);
3266 free(deviates);
3267 deviates = NULL;
3268
3269 data = ELEMENT_WRAPPER_START "<deviate value=\"add\" />" ELEMENT_WRAPPER_END;
3270 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3271 assert_int_equal(deviates->mod, LYS_DEV_ADD);
3272 lysp_deviate_free(st->ctx, deviates);
3273 free(deviates);
3274 deviates = NULL;
3275
3276 data = ELEMENT_WRAPPER_START "<deviate value=\"replace\" />" ELEMENT_WRAPPER_END;
3277 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3278 assert_int_equal(deviates->mod, LYS_DEV_REPLACE);
3279 lysp_deviate_free(st->ctx, deviates);
3280 free(deviates);
3281 deviates = NULL;
3282
3283 data = ELEMENT_WRAPPER_START "<deviate value=\"delete\" />" ELEMENT_WRAPPER_END;
3284 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3285 assert_int_equal(deviates->mod, LYS_DEV_DELETE);
3286 lysp_deviate_free(st->ctx, deviates);
3287 free(deviates);
3288 deviates = NULL;
3289
3290 /* max subelems and valid arguments */
3291 data = ELEMENT_WRAPPER_START
3292 "<deviate value=\"not-supported\">"
3293 "</deviate>"
3294 ELEMENT_WRAPPER_END;
3295 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3296 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
3297 lysp_deviate_free(st->ctx, deviates);
3298 free(deviates);
3299 deviates = NULL;
3300
3301 data = ELEMENT_WRAPPER_START
3302 "<deviate value=\"add\">"
3303 "<units name=\"units\"/>"
3304 "<must condition=\"cond\"/>"
3305 "<unique tag=\"utag\"/>"
3306 "<default value=\"def\"/>"
3307 "<config value=\"true\"/>"
3308 "<mandatory value=\"true\"/>"
3309 "<min-elements value=\"5\"/>"
3310 "<max-elements value=\"15\"/>"
3311 "</deviate>"
3312 ELEMENT_WRAPPER_END;
3313 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3314 d_add = (struct lysp_deviate_add *)deviates;
3315 assert_int_equal(d_add->mod, LYS_DEV_ADD);
3316 assert_null(d_add->next);
3317 assert_null(d_add->exts);
3318 assert_string_equal(d_add->units, "units");
3319 assert_string_equal(d_add->musts->arg, "cond");
3320 assert_string_equal(*d_add->uniques, "utag");
3321 assert_string_equal(*d_add->dflts, "def");
3322 assert_true(d_add->flags & LYS_MAND_TRUE && d_add->flags & LYS_CONFIG_W);
3323 assert_int_equal(d_add->min, 5);
3324 assert_int_equal(d_add->max, 15);
3325 lysp_deviate_free(st->ctx, deviates);
3326 free(deviates);
3327 deviates = NULL;
3328
3329 data = ELEMENT_WRAPPER_START
3330 "<deviate value=\"replace\">"
3331 "<type name=\"newtype\"/>"
3332 "<units name=\"uni\"/>"
3333 "<default value=\"def\"/>"
3334 "<config value=\"true\"/>"
3335 "<mandatory value=\"true\"/>"
3336 "<min-elements value=\"5\"/>"
3337 "<max-elements value=\"15\"/>"
3338 "</deviate>"
3339 ELEMENT_WRAPPER_END;
3340 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3341 d_rpl = (struct lysp_deviate_rpl *)deviates;
3342 assert_int_equal(d_rpl->mod, LYS_DEV_REPLACE);
3343 assert_null(d_rpl->next);
3344 assert_null(d_rpl->exts);
3345 assert_string_equal(d_rpl->type->name, "newtype");
3346 assert_string_equal(d_rpl->units, "uni");
3347 assert_string_equal(d_rpl->dflt, "def");
3348 assert_true(d_rpl->flags & LYS_MAND_TRUE && d_rpl->flags & LYS_CONFIG_W);
3349 assert_int_equal(d_rpl->min, 5);
3350 assert_int_equal(d_rpl->max, 15);
3351 lysp_deviate_free(st->ctx, deviates);
3352 free(deviates);
3353 deviates = NULL;
3354
3355 data = ELEMENT_WRAPPER_START
3356 "<deviate value=\"delete\">"
3357 "<units name=\"u\"/>"
3358 "<must condition=\"c\"/>"
3359 "<unique tag=\"tag\"/>"
3360 "<default value=\"default\"/>"
3361 "</deviate>"
3362 ELEMENT_WRAPPER_END;
3363 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3364 d_del = (struct lysp_deviate_del *)deviates;
3365 assert_int_equal(d_del->mod, LYS_DEV_DELETE);
3366 assert_null(d_del->next);
3367 assert_null(d_del->exts);
3368 assert_string_equal(d_del->units, "u");
3369 assert_string_equal(d_del->musts->arg, "c");
3370 assert_string_equal(*d_del->uniques, "tag");
3371 assert_string_equal(*d_del->dflts, "default");
3372 lysp_deviate_free(st->ctx, deviates);
3373 free(deviates);
3374 deviates = NULL;
3375
3376 /* invalid arguments */
3377 data = ELEMENT_WRAPPER_START "<deviate value=\"\" />" ELEMENT_WRAPPER_END;
3378 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
3379 logbuf_assert("Invalid value \"\" of \"deviate\". Line number 1.");
3380 deviates = NULL;
3381
3382 data = ELEMENT_WRAPPER_START "<deviate value=\"invalid\" />" ELEMENT_WRAPPER_END;
3383 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
3384 logbuf_assert("Invalid value \"invalid\" of \"deviate\". Line number 1.");
3385 deviates = NULL;
3386
3387 data = ELEMENT_WRAPPER_START "<deviate value=\"ad\" />" ELEMENT_WRAPPER_END;
3388 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
3389 logbuf_assert("Invalid value \"ad\" of \"deviate\". Line number 1.");
3390 deviates = NULL;
3391
3392 data = ELEMENT_WRAPPER_START "<deviate value=\"adds\" />" ELEMENT_WRAPPER_END;
3393 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
3394 logbuf_assert("Invalid value \"adds\" of \"deviate\". Line number 1.");
3395 deviates = NULL;
3396
3397 data = ELEMENT_WRAPPER_START
3398 "<deviate value=\"not-supported\">"
3399 "<must condition=\"c\"/>"
3400 "</deviate>"
3401 ELEMENT_WRAPPER_END;
3402 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
3403 logbuf_assert("Deviate of this type doesn't allow \"must\" as it's sub-element. Line number 1.");
3404
3405 st->finished_correctly = true;
3406}
3407
David Sedlák3b4db242018-10-19 16:11:01 +02003408int
3409main(void)
3410{
3411
3412 const struct CMUnitTest tests[] = {
David Sedlák392af4f2019-06-04 16:02:42 +02003413 cmocka_unit_test_setup_teardown(test_yin_parse_module, setup_f, teardown_f),
David Sedlák8f7a1172019-06-20 14:42:18 +02003414 cmocka_unit_test_setup_teardown(test_yin_match_keyword, setup_f, teardown_f),
David Sedlákb1a78352019-06-28 16:16:29 +02003415 cmocka_unit_test_setup_teardown(test_yin_parse_element_generic, setup_f, teardown_f),
3416 cmocka_unit_test_setup_teardown(test_yin_parse_extension_instance, setup_f, teardown_f),
David Sedlák555c7202019-07-04 12:14:12 +02003417 cmocka_unit_test_setup_teardown(test_yin_parse_content, setup_f, teardown_f),
David Sedlák4a650532019-07-10 11:55:18 +02003418 cmocka_unit_test_setup_teardown(test_validate_value, setup_f, teardown_f),
David Sedlák32488102019-07-15 17:44:10 +02003419
David Sedlák8e7bda82019-07-16 17:57:50 +02003420 cmocka_unit_test(test_yin_match_argument_name),
David Sedlák32488102019-07-15 17:44:10 +02003421 cmocka_unit_test_setup_teardown(test_enum_bit_elem, setup_element_test, teardown_element_test),
3422 cmocka_unit_test_setup_teardown(test_meta_elem, setup_element_test, teardown_element_test),
3423 cmocka_unit_test_setup_teardown(test_import_elem, setup_element_test, teardown_element_test),
3424 cmocka_unit_test_setup_teardown(test_status_elem, setup_element_test, teardown_element_test),
3425 cmocka_unit_test_setup_teardown(test_ext_elem, setup_element_test, teardown_element_test),
3426 cmocka_unit_test_setup_teardown(test_yin_element_elem, setup_element_test, teardown_element_test),
3427 cmocka_unit_test_setup_teardown(test_yangversion_elem, setup_element_test, teardown_element_test),
3428 cmocka_unit_test_setup_teardown(test_mandatory_elem, setup_element_test, teardown_element_test),
David Sedlák8e7bda82019-07-16 17:57:50 +02003429 cmocka_unit_test_setup_teardown(test_argument_elem, setup_element_test, teardown_element_test),
3430 cmocka_unit_test_setup_teardown(test_base_elem, setup_element_test, teardown_element_test),
3431 cmocka_unit_test_setup_teardown(test_belongsto_elem, setup_element_test, teardown_element_test),
3432 cmocka_unit_test_setup_teardown(test_config_elem, setup_element_test, teardown_element_test),
3433 cmocka_unit_test_setup_teardown(test_default_elem, setup_element_test, teardown_element_test),
3434 cmocka_unit_test_setup_teardown(test_err_app_tag_elem, setup_element_test, teardown_element_test),
3435 cmocka_unit_test_setup_teardown(test_err_msg_elem, setup_element_test, teardown_element_test),
3436 cmocka_unit_test_setup_teardown(test_fracdigits_elem, setup_element_test, teardown_element_test),
3437 cmocka_unit_test_setup_teardown(test_iffeature_elem, setup_element_test, teardown_element_test),
3438 cmocka_unit_test_setup_teardown(test_length_elem, setup_element_test, teardown_element_test),
3439 cmocka_unit_test_setup_teardown(test_modifier_elem, setup_element_test, teardown_element_test),
3440 cmocka_unit_test_setup_teardown(test_namespace_elem, setup_element_test, teardown_element_test),
3441 cmocka_unit_test_setup_teardown(test_path_elem, setup_element_test, teardown_element_test),
3442 cmocka_unit_test_setup_teardown(test_pattern_elem, setup_element_test, teardown_element_test),
3443 cmocka_unit_test_setup_teardown(test_value_position_elem, setup_element_test, teardown_element_test),
David Sedlák69f01612019-07-17 11:41:08 +02003444 cmocka_unit_test_setup_teardown(test_prefix_elem, setup_element_test, teardown_element_test),
3445 cmocka_unit_test_setup_teardown(test_range_elem, setup_element_test, teardown_element_test),
3446 cmocka_unit_test_setup_teardown(test_reqinstance_elem, setup_element_test, teardown_element_test),
3447 cmocka_unit_test_setup_teardown(test_revision_date_elem, setup_element_test, teardown_element_test),
3448 cmocka_unit_test_setup_teardown(test_unique_elem, setup_element_test, teardown_element_test),
3449 cmocka_unit_test_setup_teardown(test_units_elem, setup_element_test, teardown_element_test),
3450 cmocka_unit_test_setup_teardown(test_when_elem, setup_element_test, teardown_element_test),
3451 cmocka_unit_test_setup_teardown(test_yin_text_value_elem, setup_element_test, teardown_element_test),
David Sedlák374d2b32019-07-17 15:06:55 +02003452 cmocka_unit_test_setup_teardown(test_type_elem, setup_element_test, teardown_element_test),
David Sedlák1af868e2019-07-17 17:03:14 +02003453 cmocka_unit_test_setup_teardown(test_max_elems_elem, setup_element_test, teardown_element_test),
David Sedlák09e18c92019-07-18 11:17:11 +02003454 cmocka_unit_test_setup_teardown(test_min_elems_elem, setup_element_test, teardown_element_test),
David Sedláka2dad212019-07-18 12:45:19 +02003455 cmocka_unit_test_setup_teardown(test_ordby_elem, setup_element_test, teardown_element_test),
David Sedlák8a83bbb2019-07-18 14:46:00 +02003456 cmocka_unit_test_setup_teardown(test_any_elem, setup_element_test, teardown_element_test),
David Sedlák203ca3a2019-07-18 15:26:25 +02003457 cmocka_unit_test_setup_teardown(test_leaf_elem, setup_element_test, teardown_element_test),
David Sedlákc3da3ef2019-07-19 12:56:08 +02003458 cmocka_unit_test_setup_teardown(test_leaf_list_elem, setup_element_test, teardown_element_test),
David Sedlákcb39f642019-07-19 13:19:55 +02003459 cmocka_unit_test_setup_teardown(test_presence_elem, setup_element_test, teardown_element_test),
David Sedlák12470a82019-07-19 13:44:36 +02003460 cmocka_unit_test_setup_teardown(test_key_elem, setup_element_test, teardown_element_test),
David Sedlák04e17b22019-07-19 15:29:48 +02003461 cmocka_unit_test_setup_teardown(test_typedef_elem, setup_element_test, teardown_element_test),
David Sedlákd2d676a2019-07-22 11:28:19 +02003462 cmocka_unit_test_setup_teardown(test_refine_elem, setup_element_test, teardown_element_test),
David Sedlák0d6de5a2019-07-22 13:25:44 +02003463 cmocka_unit_test_setup_teardown(test_uses_elem, setup_element_test, teardown_element_test),
David Sedlákaa854b02019-07-22 14:17:10 +02003464 cmocka_unit_test_setup_teardown(test_revision_elem, setup_element_test, teardown_element_test),
David Sedlák0c2bab92019-07-22 15:33:19 +02003465 cmocka_unit_test_setup_teardown(test_include_elem, setup_element_test, teardown_element_test),
David Sedlák5e13dea2019-07-22 16:06:45 +02003466 cmocka_unit_test_setup_teardown(test_feature_elem, setup_element_test, teardown_element_test),
David Sedlák28794f22019-07-22 16:45:00 +02003467 cmocka_unit_test_setup_teardown(test_identity_elem, setup_element_test, teardown_element_test),
David Sedlákaf536aa2019-07-23 13:42:23 +02003468 cmocka_unit_test_setup_teardown(test_list_elem, setup_element_test, teardown_element_test),
David Sedlák031b9e72019-07-23 15:19:37 +02003469 cmocka_unit_test_setup_teardown(test_notification_elem, setup_element_test, teardown_element_test),
David Sedláke3ce9ef2019-07-23 16:34:30 +02003470 cmocka_unit_test_setup_teardown(test_grouping_elem, setup_element_test, teardown_element_test),
David Sedlákf111bcb2019-07-23 17:15:51 +02003471 cmocka_unit_test_setup_teardown(test_container_elem, setup_element_test, teardown_element_test),
David Sedlák5379d392019-07-24 10:42:03 +02003472 cmocka_unit_test_setup_teardown(test_case_elem, setup_element_test, teardown_element_test),
David Sedlákb7abcfa2019-07-24 12:33:35 +02003473 cmocka_unit_test_setup_teardown(test_choice_elem, setup_element_test, teardown_element_test),
David Sedlák05404f62019-07-24 14:11:53 +02003474 cmocka_unit_test_setup_teardown(test_inout_elem, setup_element_test, teardown_element_test),
David Sedlák85d0eca2019-07-24 15:15:21 +02003475 cmocka_unit_test_setup_teardown(test_action_elem, setup_element_test, teardown_element_test),
David Sedlák992fb7c2019-07-24 16:51:01 +02003476 cmocka_unit_test_setup_teardown(test_augment_elem, setup_element_test, teardown_element_test),
David Sedlák4ffcec82019-07-25 15:10:21 +02003477 cmocka_unit_test_setup_teardown(test_deviate_elem, setup_element_test, teardown_element_test),
David Sedlák3b4db242018-10-19 16:11:01 +02003478 };
3479
David Sedlák8e7bda82019-07-16 17:57:50 +02003480 return cmocka_run_group_tests(tests, setup_ly_ctx, destroy_ly_ctx);
David Sedlák3b4db242018-10-19 16:11:01 +02003481}