blob: 296b1dc8b0e330d839cd5ccd4cf616a79b0dc9c5 [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ák8b754462019-07-25 16:22:13 +020048void lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev);
David Sedlák298ff6d2019-07-26 14:29:03 +020049void lysp_submodule_free(struct ly_ctx *ctx, struct lysp_submodule *submod);
50void lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import);
David Sedlák555c7202019-07-04 12:14:12 +020051
David Sedlák68a1af12019-03-08 13:46:54 +010052struct state {
David Sedlák3b4db242018-10-19 16:11:01 +020053 struct ly_ctx *ctx;
David Sedlák3017da42019-02-15 09:48:04 +010054 struct lys_module *mod;
David Sedlák619db942019-07-03 14:47:30 +020055 struct lysp_module *lysp_mod;
David Sedlákda8ffa32019-07-08 14:17:10 +020056 struct yin_parser_ctx *yin_ctx;
David Sedlák79e50cb2019-06-05 16:33:09 +020057 bool finished_correctly;
David Sedlák68a1af12019-03-08 13:46:54 +010058};
David Sedlák872c7b42018-10-26 13:15:20 +020059
David Sedlák79e50cb2019-06-05 16:33:09 +020060#define BUFSIZE 1024
61char logbuf[BUFSIZE] = {0};
62int store = -1; /* negative for infinite logging, positive for limited logging */
63
64/* set to 0 to printing error messages to stderr instead of checking them in code */
David Sedlák298ff6d2019-07-26 14:29:03 +020065#define ENABLE_LOGGER_CHECKING 1
David Sedlák79e50cb2019-06-05 16:33:09 +020066
67#if ENABLE_LOGGER_CHECKING
68static void
69logger(LY_LOG_LEVEL level, const char *msg, const char *path)
70{
71 (void) level; /* unused */
72 if (store) {
73 if (path && path[0]) {
74 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
75 } else {
76 strncpy(logbuf, msg, BUFSIZE - 1);
77 }
78 if (store > 0) {
79 --store;
80 }
81 }
82}
83#endif
84
85#if ENABLE_LOGGER_CHECKING
86# define logbuf_assert(str) assert_string_equal(logbuf, str)
87#else
88# define logbuf_assert(str)
89#endif
90
91#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
92 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
93 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
94 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
95 CLEANUP
96
David Sedlák8e7bda82019-07-16 17:57:50 +020097int
98setup_ly_ctx(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +010099{
100 struct state *st = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +0200101
David Sedlák68a1af12019-03-08 13:46:54 +0100102 /* allocate state variable */
103 (*state) = st = calloc(1, sizeof(*st));
104 if (!st) {
105 fprintf(stderr, "Memmory allocation failed");
106 return EXIT_FAILURE;
107 }
David Sedlák872c7b42018-10-26 13:15:20 +0200108
David Sedlák68a1af12019-03-08 13:46:54 +0100109 /* create new libyang context */
110 ly_ctx_new(NULL, 0, &st->ctx);
David Sedlák872c7b42018-10-26 13:15:20 +0200111
David Sedlák8e7bda82019-07-16 17:57:50 +0200112 return EXIT_SUCCESS;
113}
114
115int
116destroy_ly_ctx(void **state)
117{
118 struct state *st = *state;
119 ly_ctx_destroy(st->ctx, NULL);
120 free(st);
121
122 return EXIT_SUCCESS;
123}
124
125static int
126setup_f(void **state)
127{
128 struct state *st = *state;
129
130#if ENABLE_LOGGER_CHECKING
131 /* setup logger */
132 ly_set_log_clb(logger, 1);
133#endif
134
David Sedlák68a1af12019-03-08 13:46:54 +0100135 /* allocate new module */
136 st->mod = calloc(1, sizeof(*st->mod));
137 st->mod->ctx = st->ctx;
138
David Sedlák619db942019-07-03 14:47:30 +0200139 /* allocate new parsed module */
140 st->lysp_mod = calloc(1, sizeof(*st->lysp_mod));
141 st->lysp_mod->mod = calloc(1, sizeof(*st->lysp_mod->mod));
142 st->lysp_mod->mod->ctx = st->ctx;
143
144 /* allocate parser context */
David Sedlákda8ffa32019-07-08 14:17:10 +0200145 st->yin_ctx = calloc(1, sizeof(*st->yin_ctx));
146 st->yin_ctx->xml_ctx.ctx = st->ctx;
147 st->yin_ctx->xml_ctx.line = 1;
David Sedlák8f5bce02019-06-03 16:41:08 +0200148
David Sedlák68a1af12019-03-08 13:46:54 +0100149 return EXIT_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +0200150}
151
152static int
David Sedlák68a1af12019-03-08 13:46:54 +0100153teardown_f(void **state)
154{
155 struct state *st = *(struct state **)state;
David Sedlák619db942019-07-03 14:47:30 +0200156 struct lys_module *temp;
David Sedlák68a1af12019-03-08 13:46:54 +0100157
David Sedlák79e50cb2019-06-05 16:33:09 +0200158#if ENABLE_LOGGER_CHECKING
159 /* teardown logger */
160 if (!st->finished_correctly && logbuf[0] != '\0') {
161 fprintf(stderr, "%s\n", logbuf);
162 }
163#endif
164
David Sedlák619db942019-07-03 14:47:30 +0200165 temp = st->lysp_mod->mod;
166
David Sedlákda8ffa32019-07-08 14:17:10 +0200167 lyxml_context_clear(&st->yin_ctx->xml_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100168 lys_module_free(st->mod, NULL);
David Sedlák619db942019-07-03 14:47:30 +0200169 lysp_module_free(st->lysp_mod);
170 lys_module_free(temp, NULL);
David Sedlákda8ffa32019-07-08 14:17:10 +0200171 free(st->yin_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100172
173 return EXIT_SUCCESS;
174}
175
David Sedlák392af4f2019-06-04 16:02:42 +0200176static struct state*
177reset_state(void **state)
178{
David Sedlák79e50cb2019-06-05 16:33:09 +0200179 ((struct state *)*state)->finished_correctly = true;
David Sedlák555c7202019-07-04 12:14:12 +0200180 logbuf[0] = '\0';
David Sedlák392af4f2019-06-04 16:02:42 +0200181 teardown_f(state);
182 setup_f(state);
183
184 return *state;
185}
186
David Sedlák79e50cb2019-06-05 16:33:09 +0200187void
188logbuf_clean(void)
189{
190 logbuf[0] = '\0';
191}
192
David Sedlák8985a142019-07-31 16:43:06 +0200193static int
194setup_logger(void **state)
195{
196 (void)state; /* unused */
197#if ENABLE_LOGGER_CHECKING
198 /* setup logger */
199 ly_set_log_clb(logger, 1);
200#endif
201
202 logbuf[0] = '\0';
203
204 return EXIT_SUCCESS;
205}
206
207static int
208teardown_logger(void **state)
209{
210 struct state *st = *state;
211
212#if ENABLE_LOGGER_CHECKING
213 /* teardown logger */
214 if (!st->finished_correctly && logbuf[0] != '\0') {
215 fprintf(stderr, "%s\n", logbuf);
216 }
217#endif
218
219 return EXIT_SUCCESS;
220}
221
222static int
223setup_element_test(void **state)
224{
225 setup_logger(state);
226 struct state *st = *state;
227
228 st->yin_ctx = calloc(1, sizeof(*st->yin_ctx));
229
230 /* allocate parser context */
231 st->yin_ctx->xml_ctx.ctx = st->ctx;
232 st->yin_ctx->xml_ctx.line = 1;
233
234 return EXIT_SUCCESS;
235}
236
237static int
238teardown_element_test(void **state)
239{
240 struct state *st = *(struct state **)state;
241
242 lyxml_context_clear(&st->yin_ctx->xml_ctx);
243 free(st->yin_ctx);
244
245 teardown_logger(state);
246
247 return EXIT_SUCCESS;
248}
249
David Sedlák68a1af12019-03-08 13:46:54 +0100250static void
David Sedlák1bccdfa2019-06-17 15:55:27 +0200251test_yin_match_keyword(void **state)
David Sedlák3b4db242018-10-19 16:11:01 +0200252{
David Sedlák8f7a1172019-06-20 14:42:18 +0200253 struct state *st = *state;
David Sedlák3b4db242018-10-19 16:11:01 +0200254
David Sedlák8f7a1172019-06-20 14:42:18 +0200255 const char *prefix, *name;
256 struct yin_arg_record *args = NULL;
257 size_t prefix_len, name_len;
258 /* create mock yin namespace in xml context */
259 const char *data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" />";
David Sedlákda8ffa32019-07-08 14:17:10 +0200260 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
261 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák8f7a1172019-06-20 14:42:18 +0200262 LY_ARRAY_FREE(args);
263
David Sedlákc1771b12019-07-10 15:55:46 +0200264 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydatax", strlen("anydatax"), prefix, prefix_len, YANG_NONE), YANG_NONE);
265 assert_int_equal(yin_match_keyword(st->yin_ctx, "asdasd", strlen("asdasd"), prefix, prefix_len, YANG_NONE), YANG_NONE);
266 assert_int_equal(yin_match_keyword(st->yin_ctx, "", 0, prefix, prefix_len, YANG_NONE), YANG_NONE);
267 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydata", strlen("anydata"), prefix, prefix_len, YANG_NONE), YANG_ANYDATA);
268 assert_int_equal(yin_match_keyword(st->yin_ctx, "anyxml", strlen("anyxml"), prefix, prefix_len, YANG_NONE), YANG_ANYXML);
269 assert_int_equal(yin_match_keyword(st->yin_ctx, "argument", strlen("argument"), prefix, prefix_len, YANG_NONE), YANG_ARGUMENT);
270 assert_int_equal(yin_match_keyword(st->yin_ctx, "augment", strlen("augment"), prefix, prefix_len, YANG_NONE), YANG_AUGMENT);
271 assert_int_equal(yin_match_keyword(st->yin_ctx, "base", strlen("base"), prefix, prefix_len, YANG_NONE), YANG_BASE);
272 assert_int_equal(yin_match_keyword(st->yin_ctx, "belongs-to", strlen("belongs-to"), prefix, prefix_len, YANG_NONE), YANG_BELONGS_TO);
273 assert_int_equal(yin_match_keyword(st->yin_ctx, "bit", strlen("bit"), prefix, prefix_len, YANG_NONE), YANG_BIT);
274 assert_int_equal(yin_match_keyword(st->yin_ctx, "case", strlen("case"), prefix, prefix_len, YANG_NONE), YANG_CASE);
275 assert_int_equal(yin_match_keyword(st->yin_ctx, "choice", strlen("choice"), prefix, prefix_len, YANG_NONE), YANG_CHOICE);
276 assert_int_equal(yin_match_keyword(st->yin_ctx, "config", strlen("config"), prefix, prefix_len, YANG_NONE), YANG_CONFIG);
277 assert_int_equal(yin_match_keyword(st->yin_ctx, "contact", strlen("contact"), prefix, prefix_len, YANG_NONE), YANG_CONTACT);
278 assert_int_equal(yin_match_keyword(st->yin_ctx, "container", strlen("container"), prefix, prefix_len, YANG_NONE), YANG_CONTAINER);
279 assert_int_equal(yin_match_keyword(st->yin_ctx, "default", strlen("default"), prefix, prefix_len, YANG_NONE), YANG_DEFAULT);
280 assert_int_equal(yin_match_keyword(st->yin_ctx, "description", strlen("description"), prefix, prefix_len, YANG_NONE), YANG_DESCRIPTION);
281 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviate", strlen("deviate"), prefix, prefix_len, YANG_NONE), YANG_DEVIATE);
282 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviation", strlen("deviation"), prefix, prefix_len, YANG_NONE), YANG_DEVIATION);
283 assert_int_equal(yin_match_keyword(st->yin_ctx, "enum", strlen("enum"), prefix, prefix_len, YANG_NONE), YANG_ENUM);
284 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);
285 assert_int_equal(yin_match_keyword(st->yin_ctx, "error-message", strlen("error-message"), prefix, prefix_len, YANG_NONE), YANG_ERROR_MESSAGE);
286 assert_int_equal(yin_match_keyword(st->yin_ctx, "extension", strlen("extension"), prefix, prefix_len, YANG_NONE), YANG_EXTENSION);
287 assert_int_equal(yin_match_keyword(st->yin_ctx, "feature", strlen("feature"), prefix, prefix_len, YANG_NONE), YANG_FEATURE);
288 assert_int_equal(yin_match_keyword(st->yin_ctx, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len, YANG_NONE), YANG_FRACTION_DIGITS);
289 assert_int_equal(yin_match_keyword(st->yin_ctx, "grouping", strlen("grouping"), prefix, prefix_len, YANG_NONE), YANG_GROUPING);
290 assert_int_equal(yin_match_keyword(st->yin_ctx, "identity", strlen("identity"), prefix, prefix_len, YANG_NONE), YANG_IDENTITY);
291 assert_int_equal(yin_match_keyword(st->yin_ctx, "if-feature", strlen("if-feature"), prefix, prefix_len, YANG_NONE), YANG_IF_FEATURE);
292 assert_int_equal(yin_match_keyword(st->yin_ctx, "import", strlen("import"), prefix, prefix_len, YANG_NONE), YANG_IMPORT);
293 assert_int_equal(yin_match_keyword(st->yin_ctx, "include", strlen("include"), prefix, prefix_len, YANG_NONE), YANG_INCLUDE);
294 assert_int_equal(yin_match_keyword(st->yin_ctx, "input", strlen("input"), prefix, prefix_len, YANG_NONE), YANG_INPUT);
295 assert_int_equal(yin_match_keyword(st->yin_ctx, "key", strlen("key"), prefix, prefix_len, YANG_NONE), YANG_KEY);
296 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf", strlen("leaf"), prefix, prefix_len, YANG_NONE), YANG_LEAF);
297 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf-list", strlen("leaf-list"), prefix, prefix_len, YANG_NONE), YANG_LEAF_LIST);
298 assert_int_equal(yin_match_keyword(st->yin_ctx, "length", strlen("length"), prefix, prefix_len, YANG_NONE), YANG_LENGTH);
299 assert_int_equal(yin_match_keyword(st->yin_ctx, "list", strlen("list"), prefix, prefix_len, YANG_NONE), YANG_LIST);
300 assert_int_equal(yin_match_keyword(st->yin_ctx, "mandatory", strlen("mandatory"), prefix, prefix_len, YANG_NONE), YANG_MANDATORY);
301 assert_int_equal(yin_match_keyword(st->yin_ctx, "max-elements", strlen("max-elements"), prefix, prefix_len, YANG_NONE), YANG_MAX_ELEMENTS);
302 assert_int_equal(yin_match_keyword(st->yin_ctx, "min-elements", strlen("min-elements"), prefix, prefix_len, YANG_NONE), YANG_MIN_ELEMENTS);
303 assert_int_equal(yin_match_keyword(st->yin_ctx, "modifier", strlen("modifier"), prefix, prefix_len, YANG_NONE), YANG_MODIFIER);
304 assert_int_equal(yin_match_keyword(st->yin_ctx, "module", strlen("module"), prefix, prefix_len, YANG_NONE), YANG_MODULE);
305 assert_int_equal(yin_match_keyword(st->yin_ctx, "must", strlen("must"), prefix, prefix_len, YANG_NONE), YANG_MUST);
306 assert_int_equal(yin_match_keyword(st->yin_ctx, "namespace", strlen("namespace"), prefix, prefix_len, YANG_NONE), YANG_NAMESPACE);
307 assert_int_equal(yin_match_keyword(st->yin_ctx, "notification", strlen("notification"), prefix, prefix_len, YANG_NONE), YANG_NOTIFICATION);
308 assert_int_equal(yin_match_keyword(st->yin_ctx, "ordered-by", strlen("ordered-by"), prefix, prefix_len, YANG_NONE), YANG_ORDERED_BY);
309 assert_int_equal(yin_match_keyword(st->yin_ctx, "organization", strlen("organization"), prefix, prefix_len, YANG_NONE), YANG_ORGANIZATION);
310 assert_int_equal(yin_match_keyword(st->yin_ctx, "output", strlen("output"), prefix, prefix_len, YANG_NONE), YANG_OUTPUT);
311 assert_int_equal(yin_match_keyword(st->yin_ctx, "path", strlen("path"), prefix, prefix_len, YANG_NONE), YANG_PATH);
312 assert_int_equal(yin_match_keyword(st->yin_ctx, "pattern", strlen("pattern"), prefix, prefix_len, YANG_NONE), YANG_PATTERN);
313 assert_int_equal(yin_match_keyword(st->yin_ctx, "position", strlen("position"), prefix, prefix_len, YANG_NONE), YANG_POSITION);
314 assert_int_equal(yin_match_keyword(st->yin_ctx, "prefix", strlen("prefix"), prefix, prefix_len, YANG_NONE), YANG_PREFIX);
315 assert_int_equal(yin_match_keyword(st->yin_ctx, "presence", strlen("presence"), prefix, prefix_len, YANG_NONE), YANG_PRESENCE);
316 assert_int_equal(yin_match_keyword(st->yin_ctx, "range", strlen("range"), prefix, prefix_len, YANG_NONE), YANG_RANGE);
317 assert_int_equal(yin_match_keyword(st->yin_ctx, "reference", strlen("reference"), prefix, prefix_len, YANG_NONE), YANG_REFERENCE);
318 assert_int_equal(yin_match_keyword(st->yin_ctx, "refine", strlen("refine"), prefix, prefix_len, YANG_NONE), YANG_REFINE);
319 assert_int_equal(yin_match_keyword(st->yin_ctx, "require-instance", strlen("require-instance"), prefix, prefix_len, YANG_NONE), YANG_REQUIRE_INSTANCE);
320 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision", strlen("revision"), prefix, prefix_len, YANG_NONE), YANG_REVISION);
321 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision-date", strlen("revision-date"), prefix, prefix_len, YANG_NONE), YANG_REVISION_DATE);
322 assert_int_equal(yin_match_keyword(st->yin_ctx, "rpc", strlen("rpc"), prefix, prefix_len, YANG_NONE), YANG_RPC);
323 assert_int_equal(yin_match_keyword(st->yin_ctx, "status", strlen("status"), prefix, prefix_len, YANG_NONE), YANG_STATUS);
324 assert_int_equal(yin_match_keyword(st->yin_ctx, "submodule", strlen("submodule"), prefix, prefix_len, YANG_NONE), YANG_SUBMODULE);
325 assert_int_equal(yin_match_keyword(st->yin_ctx, "type", strlen("type"), prefix, prefix_len, YANG_NONE), YANG_TYPE);
326 assert_int_equal(yin_match_keyword(st->yin_ctx, "typedef", strlen("typedef"), prefix, prefix_len, YANG_NONE), YANG_TYPEDEF);
327 assert_int_equal(yin_match_keyword(st->yin_ctx, "unique", strlen("unique"), prefix, prefix_len, YANG_NONE), YANG_UNIQUE);
328 assert_int_equal(yin_match_keyword(st->yin_ctx, "units", strlen("units"), prefix, prefix_len, YANG_NONE), YANG_UNITS);
329 assert_int_equal(yin_match_keyword(st->yin_ctx, "uses", strlen("uses"), prefix, prefix_len, YANG_NONE), YANG_USES);
330 assert_int_equal(yin_match_keyword(st->yin_ctx, "value", strlen("value"), prefix, prefix_len, YANG_NONE), YANG_VALUE);
331 assert_int_equal(yin_match_keyword(st->yin_ctx, "when", strlen("when"), prefix, prefix_len, YANG_NONE), YANG_WHEN);
332 assert_int_equal(yin_match_keyword(st->yin_ctx, "yang-version", strlen("yang-version"), prefix, prefix_len, YANG_NONE), YANG_YANG_VERSION);
333 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 +0200334
335 st->finished_correctly = true;
David Sedlák872c7b42018-10-26 13:15:20 +0200336}
David Sedlák3b4db242018-10-19 16:11:01 +0200337
David Sedlák872c7b42018-10-26 13:15:20 +0200338static void
David Sedlák060b00e2019-06-19 11:12:06 +0200339test_yin_match_argument_name(void **state)
David Sedlák872c7b42018-10-26 13:15:20 +0200340{
David Sedlák68a1af12019-03-08 13:46:54 +0100341 (void)state; /* unused */
David Sedlák872c7b42018-10-26 13:15:20 +0200342
David Sedlák060b00e2019-06-19 11:12:06 +0200343 assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN);
344 assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN);
345 assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN);
346 assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION);
347 assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE);
348 assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE);
349 assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME);
350 assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG);
351 assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
352 assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT);
353 assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI);
354 assert_int_equal(yin_match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200355}
356
David Sedlák68a1af12019-03-08 13:46:54 +0100357static void
David Sedlákb1a78352019-06-28 16:16:29 +0200358test_yin_parse_element_generic(void **state)
359{
360 const char *prefix, *name;
361 struct state *st = *state;
362 struct lysp_ext_instance exts;
363 size_t prefix_len, name_len;
364 LY_ERR ret;
365
366 memset(&exts, 0, sizeof(exts));
367
368 const char *data = "<elem attr=\"value\">text_value</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ákb1a78352019-06-28 16:16:29 +0200371 assert_int_equal(ret, LY_SUCCESS);
David Sedlák1fdb2522019-07-09 16:22:57 +0200372 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákb1a78352019-06-28 16:16:29 +0200373 assert_string_equal(exts.child->stmt, "elem");
374 assert_string_equal(exts.child->arg, "text_value");
David Sedlákb1a78352019-06-28 16:16:29 +0200375 assert_string_equal(exts.child->child->stmt, "attr");
376 assert_string_equal(exts.child->child->arg, "value");
377 assert_true(exts.child->child->flags & LYS_YIN_ATTR);
David Sedlákb1a78352019-06-28 16:16:29 +0200378 lysp_ext_instance_free(st->ctx, &exts);
David Sedlák5392a212019-07-01 09:19:10 +0200379 st = reset_state(state);
380
381 data = "<elem></elem>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200382 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák4ffcec82019-07-25 15:10:21 +0200383 ret = yin_parse_element_generic(st->yin_ctx, name, name_len, &data, &exts.child);
David Sedlák5392a212019-07-01 09:19:10 +0200384 assert_int_equal(ret, LY_SUCCESS);
385 assert_string_equal(exts.child->stmt, "elem");
386 assert_null(exts.child->child);
387 assert_null(exts.child->arg);
David Sedlákda8ffa32019-07-08 14:17:10 +0200388 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlák5392a212019-07-01 09:19:10 +0200389 lysp_ext_instance_free(st->ctx, &exts);
390
David Sedlákb1a78352019-06-28 16:16:29 +0200391 st->finished_correctly = true;
392}
393
394static void
395test_yin_parse_extension_instance(void **state)
396{
397 LY_ERR ret;
398 struct state *st = *state;
399 const char *prefix, *name;
400 size_t prefix_len, name_len;
401 struct yin_arg_record *args = NULL;
402 struct lysp_ext_instance *exts = NULL;
David Sedlákb1a78352019-06-28 16:16:29 +0200403 const char *data = "<ext value1=\"test\" value=\"test2\"><subelem>text</subelem></ext>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200404 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
405 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák1f90d252019-07-10 17:09:32 +0200406 ret = yin_parse_extension_instance(st->yin_ctx, args, &data, name2fullname(name, prefix_len),
David Sedlák619db942019-07-03 14:47:30 +0200407 namelen2fulllen(name_len, prefix_len), LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200408 assert_int_equal(ret, LY_SUCCESS);
409 assert_string_equal(exts->name, "ext");
410 assert_int_equal(exts->insubstmt_index, 0);
411 assert_true(exts->insubstmt == LYEXT_SUBSTMT_CONTACT);
412 assert_true(exts->yin & LYS_YIN);
413 assert_string_equal(exts->child->stmt, "value1");
414 assert_string_equal(exts->child->arg, "test");
415 assert_null(exts->child->child);
416 assert_true(exts->child->flags & LYS_YIN_ATTR);
417 assert_string_equal(exts->child->next->stmt, "value");
418 assert_string_equal(exts->child->next->arg, "test2");
419 assert_null(exts->child->next->child);
420 assert_true(exts->child->next->flags & LYS_YIN_ATTR);
421
422 assert_string_equal(exts->child->next->next->stmt, "subelem");
423 assert_string_equal(exts->child->next->next->arg, "text");
424 assert_null(exts->child->next->next->child);
425 assert_null(exts->child->next->next->next);
426 assert_false(exts->child->next->next->flags & LYS_YIN_ATTR);
David Sedlákda8ffa32019-07-08 14:17:10 +0200427 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákb1a78352019-06-28 16:16:29 +0200428 LY_ARRAY_FREE(args);
429 lysp_ext_instance_free(st->ctx, exts);
430 LY_ARRAY_FREE(exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200431 exts = NULL;
432 args = NULL;
433 st = reset_state(state);
434
435 data = "<extension-elem />";
David Sedlákda8ffa32019-07-08 14:17:10 +0200436 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
437 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák1f90d252019-07-10 17:09:32 +0200438 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 +0200439 assert_int_equal(ret, LY_SUCCESS);
440 assert_string_equal(exts->name, "extension-elem");
441 assert_null(exts->argument);
442 assert_null(exts->child);
443 assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT);
444 assert_int_equal(exts->insubstmt_index, 0);
445 assert_true(exts->yin & LYS_YIN);
David Sedlákda8ffa32019-07-08 14:17:10 +0200446 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákf250ecf2019-07-01 11:02:05 +0200447 LY_ARRAY_FREE(args);
448 lysp_ext_instance_free(st->ctx, exts);
449 LY_ARRAY_FREE(exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200450 st->finished_correctly = true;
451}
452
David Sedlák555c7202019-07-04 12:14:12 +0200453static void
454test_yin_parse_content(void **state)
455{
456 struct state *st = *state;
457 LY_ERR ret = LY_SUCCESS;
458 struct sized_string name, prefix;
459 const char *data = "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
460 "<custom xmlns=\"my-ext\">"
461 "totally amazing extension"
462 "</custom>"
David Sedlák986cb412019-07-04 13:10:11 +0200463 "<extension name=\"ext\">"
464 "<argument name=\"argname\"></argument>"
465 "<description><text>desc</text></description>"
466 "<reference><text>ref</text></reference>"
467 "<status value=\"deprecated\"></status>"
468 "</extension>"
David Sedlák555c7202019-07-04 12:14:12 +0200469 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedlák32488102019-07-15 17:44:10 +0200470 "<if-feature name=\"foo\"></if-feature>"
David Sedlák32eee7b2019-07-09 12:38:44 +0200471 "<when condition=\"condition...\">"
472 "<reference><text>when_ref</text></reference>"
473 "<description><text>when_desc</text></description>"
474 "</when>"
David Sedláke1a30302019-07-10 13:49:38 +0200475 "<config value=\"true\"/>"
David Sedlákc1771b12019-07-10 15:55:46 +0200476 "<error-message>"
477 "<value>error-msg</value>"
478 "</error-message>"
David Sedlák2ce1be62019-07-10 16:15:09 +0200479 "<error-app-tag value=\"err-app-tag\"/>"
David Sedláka5b1d382019-07-10 16:31:09 +0200480 "<units name=\"radians\"></units>"
David Sedláke7084ce2019-07-10 16:44:15 +0200481 "<default value=\"default-value\"/>"
David Sedlák5545f5d2019-07-11 11:55:16 +0200482 "<position value=\"25\"></position>"
483 "<value value=\"-5\"/>"
David Sedlákcf5569a2019-07-11 13:31:34 +0200484 "<require-instance value=\"true\"></require-instance>"
David Sedlákb7296dd2019-07-11 14:58:38 +0200485 "<range value=\"5..10\" />"
David Sedlák438ae432019-07-11 15:36:54 +0200486 "<length value=\"baf\"/>"
David Sedlákd3983112019-07-12 11:20:56 +0200487 "<pattern value='pattern'>"
488 "<modifier value='invert-match'/>"
489 "</pattern>"
David Sedlákfd5b9c32019-07-12 15:33:13 +0200490 "<enum name=\"yay\">"
491 "</enum>"
David Sedlák555c7202019-07-04 12:14:12 +0200492 "</prefix>";
493 struct lysp_ext_instance *exts = NULL;
David Sedlák5f8191e2019-07-08 16:35:52 +0200494 const char **if_features = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200495 struct yin_arg_record *attrs = NULL;
David Sedláke7084ce2019-07-10 16:44:15 +0200496 const char *value, *err_msg, *app_tag, *units, *def;
David Sedlák986cb412019-07-04 13:10:11 +0200497 struct lysp_ext *ext_def = NULL;
David Sedlák32eee7b2019-07-09 12:38:44 +0200498 struct lysp_when *when_p = NULL;
David Sedlákcf5569a2019-07-11 13:31:34 +0200499 struct lysp_type_enum pos_enum = {}, val_enum = {};
David Sedlákfd5b9c32019-07-12 15:33:13 +0200500 struct lysp_type req_type = {}, range_type = {}, len_type = {}, patter_type = {}, enum_type = {};
David Sedláke1a30302019-07-10 13:49:38 +0200501 uint8_t config = 0;
David Sedlák555c7202019-07-04 12:14:12 +0200502
David Sedlákda8ffa32019-07-08 14:17:10 +0200503 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
504 yin_load_attributes(st->yin_ctx, &data, &attrs);
David Sedlák555c7202019-07-04 12:14:12 +0200505
David Sedlákfd5b9c32019-07-12 15:33:13 +0200506 struct yin_subelement subelems[17] = {
David Sedlák438ae432019-07-11 15:36:54 +0200507 {YANG_CONFIG, &config, 0},
David Sedlákc3da3ef2019-07-19 12:56:08 +0200508 {YANG_DEFAULT, &def, YIN_SUBELEM_UNIQUE},
David Sedlákfd5b9c32019-07-12 15:33:13 +0200509 {YANG_ENUM, &enum_type, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200510 {YANG_ERROR_APP_TAG, &app_tag, 0},
511 {YANG_ERROR_MESSAGE, &err_msg, 0},
512 {YANG_EXTENSION, &ext_def, 0},
513 {YANG_IF_FEATURE, &if_features, 0},
514 {YANG_LENGTH, &len_type, 0},
David Sedlákd3983112019-07-12 11:20:56 +0200515 {YANG_PATTERN, &patter_type, 0},
David Sedlák43801c92019-08-05 15:58:54 +0200516 {YANG_POSITION, &pos_enum, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200517 {YANG_RANGE, &range_type, 0},
518 {YANG_REQUIRE_INSTANCE, &req_type, 0},
519 {YANG_UNITS, &units, 0},
520 {YANG_VALUE, &val_enum, 0},
521 {YANG_WHEN, &when_p, 0},
522 {YANG_CUSTOM, NULL, 0},
523 {YIN_TEXT, &value, 0}
David Sedlákd3983112019-07-12 11:20:56 +0200524 };
David Sedlákfd5b9c32019-07-12 15:33:13 +0200525 ret = yin_parse_content(st->yin_ctx, subelems, 17, &data, YANG_PREFIX, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200526 assert_int_equal(ret, LY_SUCCESS);
David Sedlák1fdb2522019-07-09 16:22:57 +0200527 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlák2ce1be62019-07-10 16:15:09 +0200528 /* check parsed values */
David Sedláke7084ce2019-07-10 16:44:15 +0200529 assert_string_equal(def, "default-value");
David Sedlák555c7202019-07-04 12:14:12 +0200530 assert_string_equal(exts->name, "custom");
531 assert_string_equal(exts->argument, "totally amazing extension");
532 assert_string_equal(value, "wsefsdf");
David Sedláka5b1d382019-07-10 16:31:09 +0200533 assert_string_equal(units, "radians");
David Sedlák32eee7b2019-07-09 12:38:44 +0200534 assert_string_equal(when_p->cond, "condition...");
535 assert_string_equal(when_p->dsc, "when_desc");
536 assert_string_equal(when_p->ref, "when_ref");
David Sedláke1a30302019-07-10 13:49:38 +0200537 assert_int_equal(config, LYS_CONFIG_W);
David Sedlák5545f5d2019-07-11 11:55:16 +0200538 assert_int_equal(pos_enum.value, 25);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200539 assert_true(pos_enum.flags & LYS_SET_VALUE);
David Sedlák5545f5d2019-07-11 11:55:16 +0200540 assert_int_equal(val_enum.value, -5);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200541 assert_true(val_enum.flags & LYS_SET_VALUE);
David Sedlákcf5569a2019-07-11 13:31:34 +0200542 assert_int_equal(req_type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200543 assert_true(req_type.flags &= LYS_SET_REQINST);
David Sedlákb7296dd2019-07-11 14:58:38 +0200544 assert_string_equal(range_type.range->arg, "5..10");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200545 assert_true(range_type.flags & LYS_SET_RANGE);
David Sedlákc1771b12019-07-10 15:55:46 +0200546 assert_string_equal(err_msg, "error-msg");
David Sedlák2ce1be62019-07-10 16:15:09 +0200547 assert_string_equal(app_tag, "err-app-tag");
David Sedlákfd5b9c32019-07-12 15:33:13 +0200548 assert_string_equal(enum_type.enums->name, "yay");
David Sedlák438ae432019-07-11 15:36:54 +0200549 assert_string_equal(len_type.length->arg, "baf");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200550 assert_true(len_type.flags & LYS_SET_LENGTH);
David Sedlákd3983112019-07-12 11:20:56 +0200551 assert_string_equal(patter_type.patterns->arg, "\x015pattern");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200552 assert_true(patter_type.flags & LYS_SET_PATTERN);
David Sedlák2ce1be62019-07-10 16:15:09 +0200553 /* cleanup */
David Sedlák555c7202019-07-04 12:14:12 +0200554 lysp_ext_instance_free(st->ctx, exts);
David Sedlák32eee7b2019-07-09 12:38:44 +0200555 lysp_when_free(st->ctx, when_p);
David Sedlák986cb412019-07-04 13:10:11 +0200556 lysp_ext_free(st->ctx, ext_def);
David Sedlák5f8191e2019-07-08 16:35:52 +0200557 FREE_STRING(st->ctx, *if_features);
David Sedlákc1771b12019-07-10 15:55:46 +0200558 FREE_STRING(st->ctx, err_msg);
David Sedlák2ce1be62019-07-10 16:15:09 +0200559 FREE_STRING(st->ctx, app_tag);
David Sedláka5b1d382019-07-10 16:31:09 +0200560 FREE_STRING(st->ctx, units);
David Sedlákd3983112019-07-12 11:20:56 +0200561 FREE_STRING(st->ctx, patter_type.patterns->arg);
David Sedláke7084ce2019-07-10 16:44:15 +0200562 FREE_STRING(st->ctx, def);
David Sedlákb7296dd2019-07-11 14:58:38 +0200563 FREE_STRING(st->ctx, range_type.range->arg);
David Sedlák438ae432019-07-11 15:36:54 +0200564 FREE_STRING(st->ctx, len_type.length->arg);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200565 FREE_STRING(st->ctx, enum_type.enums->name);
David Sedlákb7296dd2019-07-11 14:58:38 +0200566 FREE_STRING(st->ctx, value);
David Sedlák5f8191e2019-07-08 16:35:52 +0200567 LY_ARRAY_FREE(if_features);
David Sedlák555c7202019-07-04 12:14:12 +0200568 LY_ARRAY_FREE(exts);
David Sedlák986cb412019-07-04 13:10:11 +0200569 LY_ARRAY_FREE(ext_def);
David Sedlák555c7202019-07-04 12:14:12 +0200570 LY_ARRAY_FREE(attrs);
David Sedlákd3983112019-07-12 11:20:56 +0200571 LY_ARRAY_FREE(patter_type.patterns);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200572 LY_ARRAY_FREE(enum_type.enums);
David Sedlák32eee7b2019-07-09 12:38:44 +0200573 free(when_p);
David Sedlákb7296dd2019-07-11 14:58:38 +0200574 free(range_type.range);
David Sedlák438ae432019-07-11 15:36:54 +0200575 free(len_type.length);
David Sedlák555c7202019-07-04 12:14:12 +0200576 attrs = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200577 st = reset_state(state);
578
579 /* test unique subelem */
580 const char *prefix_value;
581 struct yin_subelement subelems2[2] = {{YANG_PREFIX, &prefix_value, 0},
582 {YIN_TEXT, &value, YIN_SUBELEM_UNIQUE}};
583 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
584 "<prefix value=\"inv_mod\" />"
585 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
586 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
587 "</module>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200588 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
589 yin_load_attributes(st->yin_ctx, &data, &attrs);
590 ret = yin_parse_content(st->yin_ctx, subelems2, 2, &data, YANG_MODULE, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200591 assert_int_equal(ret, LY_EVALID);
592 logbuf_assert("Redefinition of text element in module element. Line number 1.");
593 lydict_remove(st->ctx, prefix_value);
594 lydict_remove(st->ctx, value);
595 st = reset_state(state);
596 LY_ARRAY_FREE(attrs);
597 attrs = NULL;
598
599 /* test first subelem */
600 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
601 "<prefix value=\"inv_mod\" />"
602 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
603 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
604 "</module>";
605 struct yin_subelement subelems3[2] = {{YANG_PREFIX, &prefix_value, 0},
606 {YIN_TEXT, &value, YIN_SUBELEM_FIRST}};
David Sedlákda8ffa32019-07-08 14:17:10 +0200607 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
608 yin_load_attributes(st->yin_ctx, &data, &attrs);
609 ret = yin_parse_content(st->yin_ctx, subelems3, 2, &data, YANG_MODULE, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200610 assert_int_equal(ret, LY_EVALID);
611 logbuf_assert("Subelement text of module element must be defined as first subelement. Line number 1.");
612 lydict_remove(st->ctx, prefix_value);
613 st = reset_state(state);
614 LY_ARRAY_FREE(attrs);
615 attrs = NULL;
616
617 /* test mandatory subelem */
618 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
619 "</module>";
620 struct yin_subelement subelems4[1] = {{YANG_PREFIX, &prefix_value, YIN_SUBELEM_MANDATORY}};
David Sedlákda8ffa32019-07-08 14:17:10 +0200621 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
622 yin_load_attributes(st->yin_ctx, &data, &attrs);
623 ret = yin_parse_content(st->yin_ctx, subelems4, 1, &data, YANG_MODULE, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200624 assert_int_equal(ret, LY_EVALID);
625 logbuf_assert("Missing mandatory subelement prefix of module element. Line number 1.");
626 LY_ARRAY_FREE(attrs);
627
628 st->finished_correctly = true;
629}
630
David Sedlák92147b02019-07-09 14:01:01 +0200631static void
David Sedlák4a650532019-07-10 11:55:18 +0200632test_validate_value(void **state)
633{
634 struct state *st = *state;
635 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "#invalid", 8), LY_EVALID);
636 logbuf_assert("Invalid identifier character '#'. Line number 1.");
637 assert_int_equal(yin_validate_value(st->yin_ctx, Y_STR_ARG, "", 0), LY_SUCCESS);
638 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "pre:b", 5), LY_EVALID);
639 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:b", 5), LY_SUCCESS);
640 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:pre:b", 9), LY_EVALID);
641
642 st->finished_correctly = true;
643}
644
David Sedlák32488102019-07-15 17:44:10 +0200645#define ELEMENT_WRAPPER_START "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
646#define ELEMENT_WRAPPER_END "</module>"
647
648/* helper function to simplify unit test of each element using parse_content function */
649LY_ERR
650test_element_helper(struct state *st, const char **data, void *dest, const char **text,
651 struct lysp_ext_instance **exts, bool valid)
652{
653 struct yin_arg_record *attrs = NULL;
654 struct sized_string name, prefix;
655 LY_ERR ret = LY_SUCCESS;
656 struct yin_subelement subelems[71] = {
657 {YANG_ACTION, dest, 0},
658 {YANG_ANYDATA, dest, 0},
659 {YANG_ANYXML, dest, 0},
660 {YANG_ARGUMENT,dest, 0},
661 {YANG_AUGMENT, dest, 0},
662 {YANG_BASE, dest, 0},
663 {YANG_BELONGS_TO, dest, 0},
664 {YANG_BIT, dest, 0},
665 {YANG_CASE, dest, 0},
666 {YANG_CHOICE, dest, 0},
667 {YANG_CONFIG, dest, 0},
668 {YANG_CONTACT, dest, 0},
669 {YANG_CONTAINER, dest, 0},
David Sedlákc3da3ef2019-07-19 12:56:08 +0200670 {YANG_DEFAULT, dest, YIN_SUBELEM_UNIQUE},
David Sedlák32488102019-07-15 17:44:10 +0200671 {YANG_DESCRIPTION, dest, 0},
672 {YANG_DEVIATE, dest, 0},
673 {YANG_DEVIATION, dest, 0},
674 {YANG_ENUM, dest, 0},
675 {YANG_ERROR_APP_TAG, dest, 0},
676 {YANG_ERROR_MESSAGE, dest, 0},
677 {YANG_EXTENSION, dest, 0},
678 {YANG_FEATURE, dest, 0},
679 {YANG_FRACTION_DIGITS, dest, 0},
680 {YANG_GROUPING, dest, 0},
681 {YANG_IDENTITY, dest, 0},
682 {YANG_IF_FEATURE, dest, 0},
683 {YANG_IMPORT, dest, 0},
684 {YANG_INCLUDE, dest, 0},
685 {YANG_INPUT, dest, 0},
686 {YANG_KEY, dest, 0},
687 {YANG_LEAF, dest, 0},
688 {YANG_LEAF_LIST, dest, 0},
689 {YANG_LENGTH, dest, 0},
690 {YANG_LIST, dest, 0},
691 {YANG_MANDATORY, dest, 0},
692 {YANG_MAX_ELEMENTS, dest, 0},
693 {YANG_MIN_ELEMENTS, dest, 0},
694 {YANG_MODIFIER, dest, 0},
695 {YANG_MODULE, dest, 0},
696 {YANG_MUST, dest, 0},
697 {YANG_NAMESPACE, dest, 0},
698 {YANG_NOTIFICATION, dest, 0},
699 {YANG_ORDERED_BY, dest, 0},
700 {YANG_ORGANIZATION, dest, 0},
701 {YANG_OUTPUT, dest, 0},
702 {YANG_PATH, dest, 0},
703 {YANG_PATTERN, dest, 0},
David Sedlák43801c92019-08-05 15:58:54 +0200704 {YANG_POSITION, dest, 0},
David Sedlák32488102019-07-15 17:44:10 +0200705 {YANG_PREFIX, dest, 0},
706 {YANG_PRESENCE, dest, 0},
707 {YANG_RANGE, dest, 0},
708 {YANG_REFERENCE, dest, 0},
709 {YANG_REFINE, dest, 0},
710 {YANG_REQUIRE_INSTANCE, dest, 0},
711 {YANG_REVISION, dest, 0},
712 {YANG_REVISION_DATE, dest, 0},
713 {YANG_RPC, dest, 0},
714 {YANG_STATUS, dest, 0},
715 {YANG_SUBMODULE, dest, 0},
716 {YANG_TYPE, dest, 0},
717 {YANG_TYPEDEF, dest, 0},
718 {YANG_UNIQUE, dest, 0},
719 {YANG_UNITS, dest, 0},
720 {YANG_USES, dest, 0},
721 {YANG_VALUE, dest, 0},
722 {YANG_WHEN, dest, 0},
723 {YANG_YANG_VERSION, dest, 0},
724 {YANG_YIN_ELEMENT, dest, 0},
725 {YANG_CUSTOM, dest, 0},
726 {YIN_TEXT, dest, 0},
727 {YIN_VALUE, dest, 0}
728 };
David Sedlák4f03b932019-07-26 13:01:47 +0200729 LY_CHECK_RET(lyxml_get_element(&st->yin_ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len));
730 LY_CHECK_RET(yin_load_attributes(st->yin_ctx, data, &attrs));
David Sedlák8e7bda82019-07-16 17:57:50 +0200731 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 +0200732 LY_ARRAY_FREE(attrs);
733 if (valid) {
734 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
735 }
736 /* reset status */
737 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
738 return ret;
739}
740
741static void
David Sedlák43801c92019-08-05 15:58:54 +0200742test_enum_elem(void **state)
David Sedlák32488102019-07-15 17:44:10 +0200743{
David Sedlák32488102019-07-15 17:44:10 +0200744 struct state *st = *state;
745 struct lysp_type type = {};
746 const char *data;
747 data = ELEMENT_WRAPPER_START
748 "<enum name=\"enum-name\">"
749 "<if-feature name=\"feature\" />"
750 "<value value=\"55\" />"
751 "<status value=\"deprecated\" />"
752 "<description><text>desc...</text></description>"
753 "<reference><text>ref...</text></reference>"
754 "</enum>"
755 ELEMENT_WRAPPER_END;
756 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
757 assert_string_equal(*type.enums->iffeatures, "feature");
758 assert_int_equal(type.enums->value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200759 assert_true((type.enums->flags & LYS_STATUS_DEPRC) && (type.enums->flags & LYS_SET_VALUE));
David Sedlák32488102019-07-15 17:44:10 +0200760 assert_string_equal(type.enums->dsc, "desc...");
761 assert_string_equal(type.enums->ref, "ref...");
762 lysp_type_free(st->ctx, &type);
763 memset(&type, 0, sizeof type);
764
David Sedlák43801c92019-08-05 15:58:54 +0200765 st->finished_correctly = true;
766}
767
768static void
769test_bit_elem(void **state)
770{
771 struct state *st = *state;
772 struct lysp_type type = {};
773 const char *data;
774 data = ELEMENT_WRAPPER_START
775 "<bit name=\"enum-name\">"
776 "<if-feature name=\"feature\" />"
777 "<position value=\"55\" />"
778 "<status value=\"deprecated\" />"
779 "<description><text>desc...</text></description>"
780 "<reference><text>ref...</text></reference>"
781 "</bit>"
782 ELEMENT_WRAPPER_END;
783 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
784 assert_string_equal(*type.bits->iffeatures, "feature");
785 assert_int_equal(type.bits->value, 55);
786 assert_true((type.bits->flags & LYS_STATUS_DEPRC) && (type.bits->flags & LYS_SET_VALUE));
787 assert_string_equal(type.bits->dsc, "desc...");
788 assert_string_equal(type.bits->ref, "ref...");
789 lysp_type_free(st->ctx, &type);
790 memset(&type, 0, sizeof type);
791
David Sedlák32488102019-07-15 17:44:10 +0200792 st->finished_correctly = true;
793}
794
795static void
796test_meta_elem(void **state)
797{
798 struct state *st = *state;
799 char *value = NULL;
800 const char *data;
801
802 /* organization element */
803 data = ELEMENT_WRAPPER_START
804 "<organization><text>organization...</text></organization>"
805 ELEMENT_WRAPPER_END;
806 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
807 assert_string_equal(value, "organization...");
808 FREE_STRING(st->ctx, value);
809 value = NULL;
810 /* contact element */
811 data = ELEMENT_WRAPPER_START
812 "<contact><text>contact...</text></contact>"
813 ELEMENT_WRAPPER_END;
814 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
815 assert_string_equal(value, "contact...");
816 FREE_STRING(st->ctx, value);
817 value = NULL;
818 /* description element */
819 data = ELEMENT_WRAPPER_START
820 "<description><text>description...</text></description>"
821 ELEMENT_WRAPPER_END;
822 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
823 assert_string_equal(value, "description...");
824 FREE_STRING(st->ctx, value);
825 value = NULL;
826 /* reference element */
827 data = ELEMENT_WRAPPER_START
828 "<reference><text>reference...</text></reference>"
829 ELEMENT_WRAPPER_END;
830 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
831 assert_string_equal(value, "reference...");
832 FREE_STRING(st->ctx, value);
833 value = NULL;
834
835 /* missing text subelement */
836 data = ELEMENT_WRAPPER_START
837 "<reference>reference...</reference>"
838 ELEMENT_WRAPPER_END;
839 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, false), LY_EVALID);
840 logbuf_assert("Missing mandatory subelement text of reference element. Line number 1.");
841
842 st->finished_correctly = true;
843}
844
845static void
846test_import_elem(void **state)
847{
848 struct state *st = *state;
849 const char *data;
David Sedlák298ff6d2019-07-26 14:29:03 +0200850 struct lysp_import *imports = NULL;
851 struct import_meta imp_meta = {"prefix", &imports};
David Sedlák32488102019-07-15 17:44:10 +0200852
853 /* max subelems */
854 data = ELEMENT_WRAPPER_START
855 "<import module=\"a\">"
856 "<prefix value=\"a_mod\"/>"
857 "<revision-date date=\"2015-01-01\"></revision-date>"
858 "<description><text>import description</text></description>"
859 "<reference><text>import reference</text></reference>"
860 "</import>"
861 ELEMENT_WRAPPER_END;
David Sedlák298ff6d2019-07-26 14:29:03 +0200862 assert_int_equal(test_element_helper(st, &data, &imp_meta, NULL, NULL, true), LY_SUCCESS);
863 assert_string_equal(imports->name, "a");
864 assert_string_equal(imports->prefix, "a_mod");
865 assert_string_equal(imports->rev, "2015-01-01");
866 assert_string_equal(imports->dsc, "import description");
867 assert_string_equal(imports->ref, "import reference");
868 FREE_ARRAY(st->ctx, imports, lysp_import_free);
869 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200870
871 /* min subelems */
David Sedlák32488102019-07-15 17:44:10 +0200872 data = ELEMENT_WRAPPER_START
873 "<import module=\"a\">"
874 "<prefix value=\"a_mod\"/>"
875 "</import>"
876 ELEMENT_WRAPPER_END;
David Sedlák298ff6d2019-07-26 14:29:03 +0200877 assert_int_equal(test_element_helper(st, &data, &imp_meta, NULL, NULL, true), LY_SUCCESS);
878 assert_string_equal(imports->prefix, "a_mod");
879 FREE_ARRAY(st->ctx, imports, lysp_import_free);
880 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200881
882 /* invalid (missing prefix) */
David Sedlák298ff6d2019-07-26 14:29:03 +0200883 data = ELEMENT_WRAPPER_START "<import module=\"a\"></import>" ELEMENT_WRAPPER_END;
884 assert_int_equal(test_element_helper(st, &data, &imp_meta, NULL, NULL, false), LY_EVALID);
David Sedlák32488102019-07-15 17:44:10 +0200885 logbuf_assert("Missing mandatory subelement prefix of import element. Line number 1.");
David Sedlák298ff6d2019-07-26 14:29:03 +0200886 FREE_ARRAY(st->ctx, imports, lysp_import_free);
887 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200888
889 /* invalid reused prefix */
David Sedlák32488102019-07-15 17:44:10 +0200890 data = ELEMENT_WRAPPER_START
891 "<import module=\"a\">"
David Sedlák298ff6d2019-07-26 14:29:03 +0200892 "<prefix value=\"prefix\"/>"
David Sedlák32488102019-07-15 17:44:10 +0200893 "</import>"
894 "<import module=\"a\">"
David Sedlák298ff6d2019-07-26 14:29:03 +0200895 "<prefix value=\"prefix\"/>"
David Sedlák32488102019-07-15 17:44:10 +0200896 "</import>"
897 ELEMENT_WRAPPER_END;
David Sedlák298ff6d2019-07-26 14:29:03 +0200898 assert_int_equal(test_element_helper(st, &data, &imp_meta, NULL, NULL, false), LY_EVALID);
899 logbuf_assert("Prefix \"prefix\" already used as module prefix. Line number 1.");
900 FREE_ARRAY(st->ctx, imports, lysp_import_free);
901 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200902
903 st->finished_correctly = true;
904}
905
906static void
907test_status_elem(void **state)
908{
909 struct state *st = *state;
910 const char *data;
911 uint16_t flags = 0;
912
913 /* test valid values */
914 data = ELEMENT_WRAPPER_START "<status value=\"current\" />" ELEMENT_WRAPPER_END;
915 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200916 assert_true(flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +0200917
918 data = ELEMENT_WRAPPER_START "<status value=\"deprecated\" />" ELEMENT_WRAPPER_END;
919 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200920 assert_true(flags & LYS_STATUS_DEPRC);
David Sedlák32488102019-07-15 17:44:10 +0200921
922 data = ELEMENT_WRAPPER_START "<status value=\"obsolete\"></status>" ELEMENT_WRAPPER_END;
923 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200924 assert_true(flags & LYS_STATUS_OBSLT);
David Sedlák32488102019-07-15 17:44:10 +0200925
926 /* test invalid value */
927 data = ELEMENT_WRAPPER_START "<status value=\"invalid\"></status>" ELEMENT_WRAPPER_END;
928 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
929 logbuf_assert("Invalid value \"invalid\" of \"status\". Line number 1.");
930 st->finished_correctly = true;
931}
932
933static void
934test_ext_elem(void **state)
935{
936 struct state *st = *state;
937 const char *data;
938 struct lysp_ext *ext = NULL;
939
940 /* max subelems */
941 data = ELEMENT_WRAPPER_START
942 "<extension name=\"ext_name\">"
943 "<argument name=\"arg\"></argument>"
944 "<status value=\"current\"/>"
945 "<description><text>ext_desc</text></description>"
946 "<reference><text>ext_ref</text></reference>"
947 "</extension>"
948 ELEMENT_WRAPPER_END;
949 assert_int_equal(test_element_helper(st, &data, &ext, NULL, NULL, true), LY_SUCCESS);
950 assert_string_equal(ext->name, "ext_name");
951 assert_string_equal(ext->argument, "arg");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200952 assert_true(ext->flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +0200953 assert_string_equal(ext->dsc, "ext_desc");
954 assert_string_equal(ext->ref, "ext_ref");
955 lysp_ext_free(st->ctx, ext);
956 LY_ARRAY_FREE(ext);
957 ext = NULL;
958
959 /* min subelems */
960 data = ELEMENT_WRAPPER_START "<extension name=\"ext_name\"></extension>" ELEMENT_WRAPPER_END;
961 assert_int_equal(test_element_helper(st, &data, &ext, NULL, NULL, true), LY_SUCCESS);
962 assert_string_equal(ext->name, "ext_name");
963 lysp_ext_free(st->ctx, ext);
964 LY_ARRAY_FREE(ext);
965 ext = NULL;
966
967 st->finished_correctly = true;
968}
969
970static void
971test_yin_element_elem(void **state)
972{
973 struct state *st = *state;
974 const char *data;
975 uint16_t flags = 0;
976
977 data = ELEMENT_WRAPPER_START "<yin-element value=\"true\" />" ELEMENT_WRAPPER_END;
978 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200979 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +0200980
981 data = ELEMENT_WRAPPER_START "<yin-element value=\"false\" />" ELEMENT_WRAPPER_END;
982 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200983 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +0200984
985 data = ELEMENT_WRAPPER_START "<yin-element value=\"invalid\" />" ELEMENT_WRAPPER_END;
986 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200987 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +0200988 logbuf_assert("Invalid value \"invalid\" of \"yin-element\". Line number 1.");
989 st->finished_correctly = true;
990}
991
992static void
993test_yangversion_elem(void **state)
994{
995 struct state *st = *state;
996 const char *data;
997 uint8_t version = 0;
998
999 /* valid values */
1000 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.0\" />" ELEMENT_WRAPPER_END;
1001 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001002 assert_true(version & LYS_VERSION_1_0);
David Sedlák32488102019-07-15 17:44:10 +02001003 assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_0);
1004
1005 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.1\" />" ELEMENT_WRAPPER_END;
1006 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001007 assert_true(version & LYS_VERSION_1_1);
David Sedlák32488102019-07-15 17:44:10 +02001008 assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_1);
1009
1010 /* invalid value */
1011 data = ELEMENT_WRAPPER_START "<yang-version value=\"version\" />" ELEMENT_WRAPPER_END;
1012 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, false), LY_EVALID);
1013 logbuf_assert("Invalid value \"version\" of \"yang-version\". Line number 1.");
1014
1015 st->finished_correctly = true;
1016}
1017
1018static void
1019test_mandatory_elem(void **state)
1020{
1021 struct state *st = *state;
1022 const char *data;
1023 uint16_t man = 0;
1024
1025 /* valid values */
1026 data = ELEMENT_WRAPPER_START "<mandatory value=\"true\" />" ELEMENT_WRAPPER_END;
1027 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, true), LY_SUCCESS);
1028 assert_int_equal(man, LYS_MAND_TRUE);
1029 man = 0;
1030
1031 data = ELEMENT_WRAPPER_START "<mandatory value=\"false\" />" ELEMENT_WRAPPER_END;
1032 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, true), LY_SUCCESS);
1033 assert_int_equal(man, LYS_MAND_FALSE);
1034
1035 data = ELEMENT_WRAPPER_START "<mandatory value=\"invalid\" />" ELEMENT_WRAPPER_END;
1036 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, false), LY_EVALID);
1037 logbuf_assert("Invalid value \"invalid\" of \"mandatory\". Line number 1.");
1038
1039 st->finished_correctly = true;
1040}
1041
David Sedlák8e7bda82019-07-16 17:57:50 +02001042static void
1043test_argument_elem(void **state)
1044{
1045 struct state *st = *state;
1046 const char *data;
1047 uint16_t flags = 0;
1048 const char *arg;
1049 struct yin_argument_meta arg_meta = {&flags, &arg};
1050 /* max subelems */
1051 data = ELEMENT_WRAPPER_START
1052 "<argument name=\"arg-name\">"
1053 "<yin-element value=\"true\" />"
1054 "</argument>"
1055 ELEMENT_WRAPPER_END;
1056 assert_int_equal(test_element_helper(st, &data, &arg_meta, NULL, NULL, true), LY_SUCCESS);
1057 assert_string_equal(arg, "arg-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001058 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001059 flags = 0;
1060 FREE_STRING(st->ctx, arg);
1061 arg = NULL;
1062
1063 /* min subelems */
1064 data = ELEMENT_WRAPPER_START
1065 "<argument name=\"arg\">"
1066 "</argument>"
1067 ELEMENT_WRAPPER_END;
1068 assert_int_equal(test_element_helper(st, &data, &arg_meta, NULL, NULL, true), LY_SUCCESS);
1069 assert_string_equal(arg, "arg");
1070 assert_true(flags == 0);
1071 FREE_STRING(st->ctx, arg);
1072
1073 st->finished_correctly = true;
1074}
1075
1076static void
1077test_base_elem(void **state)
1078{
1079 struct state *st = *state;
1080 const char *data;
1081 const char **bases = NULL;
1082 struct lysp_type type = {};
1083
1084 /* as identity subelement */
1085 data = "<identity xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
1086 "<base name=\"base-name\"/>"
1087 "</identity>";
1088 assert_int_equal(test_element_helper(st, &data, &bases, NULL, NULL, true), LY_SUCCESS);
1089 assert_string_equal(*bases, "base-name");
1090 FREE_STRING(st->ctx, *bases);
1091 LY_ARRAY_FREE(bases);
1092
1093 /* as type subelement */
1094 data = "<type xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
1095 "<base name=\"base-name\"/>"
1096 "</type>";
1097 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1098 assert_string_equal(*type.bases, "base-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001099 assert_true(type.flags & LYS_SET_BASE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001100 FREE_STRING(st->ctx, *type.bases);
1101 LY_ARRAY_FREE(type.bases);
1102
1103 st->finished_correctly = true;
1104}
1105
1106static void
1107test_belongsto_elem(void **state)
1108{
1109 struct state *st = *state;
1110 const char *data;
1111 struct lysp_submodule submod;
1112
1113 data = ELEMENT_WRAPPER_START
1114 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>"
1115 ELEMENT_WRAPPER_END;
1116 assert_int_equal(test_element_helper(st, &data, &submod, NULL, NULL, true), LY_SUCCESS);
1117 assert_string_equal(submod.belongsto, "module-name");
1118 assert_string_equal(submod.prefix, "pref");
1119 FREE_STRING(st->ctx, submod.belongsto);
1120 FREE_STRING(st->ctx, submod.prefix);
1121
1122 data = ELEMENT_WRAPPER_START "<belongs-to module=\"module-name\"></belongs-to>" ELEMENT_WRAPPER_END;
1123 assert_int_equal(test_element_helper(st, &data, &submod, NULL, NULL, false), LY_EVALID);
1124 logbuf_assert("Missing mandatory subelement prefix of belongs-to element. Line number 1.");
1125 FREE_STRING(st->ctx, submod.belongsto);
1126
1127 st->finished_correctly = true;
1128}
1129
1130static void
1131test_config_elem(void **state)
1132{
1133 struct state *st = *state;
1134 const char *data;
1135 uint16_t flags = 0;
1136
1137 data = ELEMENT_WRAPPER_START "<config value=\"true\"/>" ELEMENT_WRAPPER_END;
1138 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001139 assert_true(flags & LYS_CONFIG_W);
David Sedlák8e7bda82019-07-16 17:57:50 +02001140 flags = 0;
1141
1142 data = ELEMENT_WRAPPER_START "<config value=\"false\"/>" ELEMENT_WRAPPER_END;
1143 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001144 assert_true(flags & LYS_CONFIG_R);
David Sedlák8e7bda82019-07-16 17:57:50 +02001145 flags = 0;
1146
1147 data = ELEMENT_WRAPPER_START "<config value=\"invalid\"/>" ELEMENT_WRAPPER_END;
1148 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
1149 logbuf_assert("Invalid value \"invalid\" of \"config\". Line number 1.");
1150
1151 st->finished_correctly = true;
1152}
1153
1154static void
1155test_default_elem(void **state)
1156{
1157 struct state *st = *state;
1158 const char *data;
1159 const char *val = NULL;
1160
1161 data = ELEMENT_WRAPPER_START "<default value=\"defaul-value\"/>" ELEMENT_WRAPPER_END;
1162 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1163 assert_string_equal(val, "defaul-value");
1164 FREE_STRING(st->ctx, val);
1165 val = NULL;
1166
1167 data = ELEMENT_WRAPPER_START "<default/>" ELEMENT_WRAPPER_END;
1168 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1169 logbuf_assert("Missing mandatory attribute value of default element. Line number 1.");
1170
1171 st->finished_correctly = true;
1172}
1173
1174static void
1175test_err_app_tag_elem(void **state)
1176{
1177 struct state *st = *state;
1178 const char *data;
1179 const char *val = NULL;
1180
1181 data = ELEMENT_WRAPPER_START "<error-app-tag value=\"val\"/>" ELEMENT_WRAPPER_END;
1182 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1183 assert_string_equal(val, "val");
1184 FREE_STRING(st->ctx, val);
1185 val = NULL;
1186
1187 data = ELEMENT_WRAPPER_START "<error-app-tag/>" ELEMENT_WRAPPER_END;
1188 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1189 logbuf_assert("Missing mandatory attribute value of error-app-tag element. Line number 1.");
1190
1191 st->finished_correctly = true;
1192}
1193
1194static void
1195test_err_msg_elem(void **state)
1196{
1197 struct state *st = *state;
1198 const char *data;
1199 const char *val = NULL;
1200
1201 data = ELEMENT_WRAPPER_START "<error-message><value>val</value></error-message>" ELEMENT_WRAPPER_END;
1202 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1203 assert_string_equal(val, "val");
1204 FREE_STRING(st->ctx, val);
1205
1206 data = ELEMENT_WRAPPER_START "<error-message></error-message>" ELEMENT_WRAPPER_END;
1207 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1208 logbuf_assert("Missing mandatory subelement value of error-message element. Line number 1.");
1209
1210 st->finished_correctly = true;
1211}
1212
1213static void
1214test_fracdigits_elem(void **state)
1215{
1216 struct state *st = *state;
1217 const char *data;
1218 struct lysp_type type = {};
1219
1220 /* valid value */
1221 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"10\"></fraction-digits>" ELEMENT_WRAPPER_END;
1222 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1223 assert_int_equal(type.fraction_digits, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001224 assert_true(type.flags & LYS_SET_FRDIGITS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001225
1226 /* invalid values */
1227 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"-1\"></fraction-digits>" ELEMENT_WRAPPER_END;
1228 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1229 logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1.");
1230
1231 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"02\"></fraction-digits>" ELEMENT_WRAPPER_END;
1232 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1233 logbuf_assert("Invalid value \"02\" of \"fraction-digits\". Line number 1.");
1234
1235 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"1p\"></fraction-digits>" ELEMENT_WRAPPER_END;
1236 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1237 logbuf_assert("Invalid value \"1p\" of \"fraction-digits\". Line number 1.");
1238
1239 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"19\"></fraction-digits>" ELEMENT_WRAPPER_END;
1240 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1241 logbuf_assert("Invalid value \"19\" of \"fraction-digits\". Line number 1.");
1242
1243 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"999999999999999999\"></fraction-digits>" ELEMENT_WRAPPER_END;
1244 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1245 logbuf_assert("Invalid value \"999999999999999999\" of \"fraction-digits\". Line number 1.");
1246
1247 st->finished_correctly = true;
1248}
1249
1250static void
1251test_iffeature_elem(void **state)
1252{
1253 struct state *st = *state;
1254 const char *data;
1255 const char **iffeatures = NULL;
1256
1257 data = ELEMENT_WRAPPER_START "<if-feature name=\"local-storage\"></if-feature>" ELEMENT_WRAPPER_END;
1258 assert_int_equal(test_element_helper(st, &data, &iffeatures, NULL, NULL, true), LY_SUCCESS);
1259 assert_string_equal(*iffeatures, "local-storage");
1260 FREE_STRING(st->ctx, *iffeatures);
1261 LY_ARRAY_FREE(iffeatures);
1262 iffeatures = NULL;
1263
1264 data = ELEMENT_WRAPPER_START "<if-feature/>" ELEMENT_WRAPPER_END;
1265 assert_int_equal(test_element_helper(st, &data, &iffeatures, NULL, NULL, false), LY_EVALID);
1266 logbuf_assert("Missing mandatory attribute name of if-feature element. Line number 1.");
1267 LY_ARRAY_FREE(iffeatures);
1268 iffeatures = NULL;
1269
1270 st->finished_correctly = true;
1271}
1272
1273static void
1274test_length_elem(void **state)
1275{
1276 struct state *st = *state;
1277 const char *data;
1278 struct lysp_type type = {};
1279
1280 /* max subelems */
1281 data = ELEMENT_WRAPPER_START
1282 "<length value=\"length-str\">"
1283 "<error-message><value>err-msg</value></error-message>"
1284 "<error-app-tag value=\"err-app-tag\"/>"
1285 "<description><text>desc</text></description>"
1286 "<reference><text>ref</text></reference>"
1287 "</length>"
1288 ELEMENT_WRAPPER_END;
1289 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1290 assert_string_equal(type.length->arg, "length-str");
1291 assert_string_equal(type.length->emsg, "err-msg");
1292 assert_string_equal(type.length->eapptag, "err-app-tag");
1293 assert_string_equal(type.length->dsc, "desc");
1294 assert_string_equal(type.length->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001295 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001296 lysp_type_free(st->ctx, &type);
1297 memset(&type, 0, sizeof(type));
1298
1299 /* min subelems */
1300 data = ELEMENT_WRAPPER_START
1301 "<length value=\"length-str\">"
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 lysp_type_free(st->ctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001307 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001308 memset(&type, 0, sizeof(type));
1309
1310 data = ELEMENT_WRAPPER_START "<length></length>" ELEMENT_WRAPPER_END;
1311 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1312 logbuf_assert("Missing mandatory attribute value of length element. Line number 1.");
1313 lysp_type_free(st->ctx, &type);
1314 memset(&type, 0, sizeof(type));
1315
1316 st->finished_correctly = true;
1317}
1318
1319static void
1320test_modifier_elem(void **state)
1321{
1322 struct state *st = *state;
1323 const char *data;
1324 const char *pat = lydict_insert(st->ctx, "\006pattern", 8);
1325
1326 data = ELEMENT_WRAPPER_START "<modifier value=\"invert-match\" />" ELEMENT_WRAPPER_END;
1327 assert_int_equal(test_element_helper(st, &data, &pat, NULL, NULL, true), LY_SUCCESS);
1328 assert_string_equal(pat, "\x015pattern");
1329 FREE_STRING(st->ctx, pat);
1330
1331 pat = lydict_insert(st->ctx, "\006pattern", 8);
1332 data = ELEMENT_WRAPPER_START "<modifier value=\"invert\" />" ELEMENT_WRAPPER_END;
1333 assert_int_equal(test_element_helper(st, &data, &pat, NULL, NULL, false), LY_EVALID);
1334 logbuf_assert("Invalid value \"invert\" of \"modifier\". Line number 1.");
1335 FREE_STRING(st->ctx, pat);
1336
1337 st->finished_correctly = true;
1338}
1339
1340static void
1341test_namespace_elem(void **state)
1342{
1343 struct state *st = *state;
1344 const char *data;
1345 const char *ns;
1346
1347 data = ELEMENT_WRAPPER_START "<namespace uri=\"ns\"/>" ELEMENT_WRAPPER_END;
1348 assert_int_equal(test_element_helper(st, &data, &ns, NULL, NULL, true), LY_SUCCESS);
1349 assert_string_equal(ns, "ns");
1350 FREE_STRING(st->ctx, ns);
1351
1352 data = ELEMENT_WRAPPER_START "<namespace/>" ELEMENT_WRAPPER_END;
1353 assert_int_equal(test_element_helper(st, &data, &ns, NULL, NULL, false), LY_EVALID);
1354 logbuf_assert("Missing mandatory attribute uri of namespace element. Line number 1.");
1355
1356 st->finished_correctly = true;
1357}
1358
1359static void
1360test_path_elem(void **state)
1361{
1362 struct state *st = *state;
1363 const char *data;
1364 struct lysp_type type = {};
1365
1366 data = ELEMENT_WRAPPER_START "<path value=\"path-val\"/>" ELEMENT_WRAPPER_END;
1367 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1368 assert_string_equal("path-val", type.path);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001369 assert_true(type.flags & LYS_SET_PATH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001370 lysp_type_free(st->ctx, &type);
1371
1372 st->finished_correctly = true;
1373}
1374
1375static void
1376test_pattern_elem(void **state)
1377{
1378 struct state *st = *state;
1379 const char *data;
1380 struct lysp_type type = {};
1381
1382 /* max subelems */
1383 data = ELEMENT_WRAPPER_START
1384 "<pattern value=\"super_pattern\">"
1385 "<modifier value=\"invert-match\"/>"
1386 "<error-message><value>err-msg-value</value></error-message>"
1387 "<error-app-tag value=\"err-app-tag-value\"/>"
1388 "<description><text>pattern-desc</text></description>"
1389 "<reference><text>pattern-ref</text></reference>"
1390 "</pattern>"
1391 ELEMENT_WRAPPER_END;
1392 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001393 assert_true(type.flags & LYS_SET_PATTERN);
David Sedlák8e7bda82019-07-16 17:57:50 +02001394 assert_string_equal(type.patterns->arg, "\x015super_pattern");
1395 assert_string_equal(type.patterns->dsc, "pattern-desc");
1396 assert_string_equal(type.patterns->eapptag, "err-app-tag-value");
1397 assert_string_equal(type.patterns->emsg, "err-msg-value");
1398 assert_string_equal(type.patterns->dsc, "pattern-desc");
1399 assert_string_equal(type.patterns->ref, "pattern-ref");
1400 lysp_type_free(st->ctx, &type);
1401 memset(&type, 0, sizeof(type));
1402
1403 /* min subelems */
1404 data = ELEMENT_WRAPPER_START "<pattern value=\"pattern\"> </pattern>" ELEMENT_WRAPPER_END;
1405 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1406 assert_string_equal(type.patterns->arg, "\x006pattern");
1407 lysp_type_free(st->ctx, &type);
1408 memset(&type, 0, sizeof(type));
1409
1410 st->finished_correctly = true;
1411}
1412
1413static void
1414test_value_position_elem(void **state)
1415{
1416 struct state *st = *state;
1417 const char *data;
1418 struct lysp_type_enum en = {};
1419
1420 /* valid values */
1421 data = ELEMENT_WRAPPER_START "<value value=\"55\" />" ELEMENT_WRAPPER_END;
1422 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1423 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001424 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001425 memset(&en, 0, sizeof(en));
1426
1427 data = ELEMENT_WRAPPER_START "<value value=\"-55\"/>" ELEMENT_WRAPPER_END;
1428 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1429 assert_int_equal(en.value, -55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001430 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001431 memset(&en, 0, sizeof(en));
1432
1433 data = ELEMENT_WRAPPER_START "<value value=\"0\"/>" ELEMENT_WRAPPER_END;
1434 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1435 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001436 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001437 memset(&en, 0, sizeof(en));
1438
1439 data = ELEMENT_WRAPPER_START "<value value=\"-0\"/>" ELEMENT_WRAPPER_END;
1440 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1441 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001442 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001443 memset(&en, 0, sizeof(en));
1444
1445 /* valid positions */
1446 data = ELEMENT_WRAPPER_START "<position value=\"55\" />" ELEMENT_WRAPPER_END;
1447 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1448 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001449 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001450 memset(&en, 0, sizeof(en));
1451
1452 data = ELEMENT_WRAPPER_START "<position value=\"0\" />" ELEMENT_WRAPPER_END;
1453 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1454 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001455 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001456 memset(&en, 0, sizeof(en));
1457
1458 /* invalid values */
1459 data = ELEMENT_WRAPPER_START "<value value=\"99999999999999999999999\"/>" ELEMENT_WRAPPER_END;
1460 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1461 logbuf_assert("Invalid value \"99999999999999999999999\" of \"value\". Line number 1.");
1462
1463 data = ELEMENT_WRAPPER_START "<value value=\"1k\"/>" ELEMENT_WRAPPER_END;
1464 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1465 logbuf_assert("Invalid value \"1k\" of \"value\". Line number 1.");
1466
David Sedlák69f01612019-07-17 11:41:08 +02001467 data = ELEMENT_WRAPPER_START "<value value=\"\"/>" ELEMENT_WRAPPER_END;
1468 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1469 logbuf_assert("Invalid value \"\" of \"value\". Line number 1.");
1470
David Sedlák8e7bda82019-07-16 17:57:50 +02001471 /*invalid positions */
1472 data = ELEMENT_WRAPPER_START "<position value=\"-5\"/>" ELEMENT_WRAPPER_END;
1473 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1474 logbuf_assert("Invalid value \"-5\" of \"position\". Line number 1.");
1475
1476 data = ELEMENT_WRAPPER_START "<position value=\"-0\"/>" ELEMENT_WRAPPER_END;
1477 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1478 logbuf_assert("Invalid value \"-0\" of \"position\". Line number 1.");
1479
1480 data = ELEMENT_WRAPPER_START "<position value=\"99999999999999999999\"/>" ELEMENT_WRAPPER_END;
1481 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1482 logbuf_assert("Invalid value \"99999999999999999999\" of \"position\". Line number 1.");
1483
David Sedlák69f01612019-07-17 11:41:08 +02001484 data = ELEMENT_WRAPPER_START "<position value=\"\"/>" ELEMENT_WRAPPER_END;
1485 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1486 logbuf_assert("Invalid value \"\" of \"position\". Line number 1.");
1487
1488 st->finished_correctly = true;
1489}
1490
1491static void
1492test_prefix_elem(void **state)
1493{
1494 struct state *st = *state;
1495 const char *data;
1496 const char *value = NULL;
1497
1498 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\"/>" ELEMENT_WRAPPER_END;
1499 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
1500 assert_string_equal(value, "pref");
1501 FREE_STRING(st->ctx, value);
1502
1503 st->finished_correctly = true;
1504}
1505
1506static void
1507test_range_elem(void **state)
1508{
1509 struct state *st = *state;
1510 const char *data;
1511 struct lysp_type type = {};
1512
1513 /* max subelems */
1514 data = ELEMENT_WRAPPER_START
1515 "<range value=\"range-str\">"
1516 "<error-message><value>err-msg</value></error-message>"
1517 "<error-app-tag value=\"err-app-tag\" />"
1518 "<description><text>desc</text></description>"
1519 "<reference><text>ref</text></reference>"
1520 "</range>"
1521 ELEMENT_WRAPPER_END;
1522 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1523 assert_string_equal(type.range->arg, "range-str");
1524 assert_string_equal(type.range->dsc, "desc");
1525 assert_string_equal(type.range->eapptag, "err-app-tag");
1526 assert_string_equal(type.range->emsg, "err-msg");
1527 assert_string_equal(type.range->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001528 assert_true(type.flags & LYS_SET_RANGE);
David Sedlák69f01612019-07-17 11:41:08 +02001529 lysp_type_free(st->ctx, &type);
1530 memset(&type, 0, sizeof(type));
1531
1532 /* min subelems */
1533 data = ELEMENT_WRAPPER_START "<range value=\"range-str\"/>" ELEMENT_WRAPPER_END;
1534 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1535 assert_string_equal(type.range->arg, "range-str");
1536 lysp_type_free(st->ctx, &type);
1537 memset(&type, 0, sizeof(type));
1538
1539 st->finished_correctly = true;
1540}
1541
1542static void
1543test_reqinstance_elem(void **state)
1544{
1545 struct state *st = *state;
1546 const char *data;
1547 struct lysp_type type = {};
1548
1549 data = ELEMENT_WRAPPER_START "<require-instance value=\"true\"/>" ELEMENT_WRAPPER_END;
1550 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1551 assert_int_equal(type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001552 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001553 memset(&type, 0, sizeof(type));
1554
1555 data = ELEMENT_WRAPPER_START "<require-instance value=\"false\"/>" ELEMENT_WRAPPER_END;
1556 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1557 assert_int_equal(type.require_instance, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001558 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001559 memset(&type, 0, sizeof(type));
1560
1561 data = ELEMENT_WRAPPER_START "<require-instance value=\"invalid\"/>" ELEMENT_WRAPPER_END;
1562 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1563 memset(&type, 0, sizeof(type));
1564 logbuf_assert("Invalid value \"invalid\" of \"require-instance\". Line number 1.");
1565
1566 st->finished_correctly = true;
1567}
1568
1569static void
1570test_revision_date_elem(void **state)
1571{
1572 struct state *st = *state;
1573 const char *data;
1574 char rev[LY_REV_SIZE];
1575
1576 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\"/>" ELEMENT_WRAPPER_END;
1577 assert_int_equal(test_element_helper(st, &data, rev, NULL, NULL, true), LY_SUCCESS);
1578 assert_string_equal(rev, "2000-01-01");
1579
1580 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-50-05\"/>" ELEMENT_WRAPPER_END;
1581 assert_int_equal(test_element_helper(st, &data, rev, NULL, NULL, false), LY_EVALID);
1582 logbuf_assert("Invalid value \"2000-50-05\" of \"revision-date\". Line number 1.");
1583
1584 st->finished_correctly = true;
1585}
1586
1587static void
1588test_unique_elem(void **state)
1589{
1590 struct state *st = *state;
1591 const char *data;
1592 const char **values = NULL;
1593
1594 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\"/>" ELEMENT_WRAPPER_END;
1595 assert_int_equal(test_element_helper(st, &data, &values, NULL, NULL, true), LY_SUCCESS);
1596 assert_string_equal(*values, "tag");
1597 FREE_STRING(st->ctx, *values);
1598 LY_ARRAY_FREE(values);
1599
1600 st->finished_correctly = true;
1601}
1602
1603static void
1604test_units_elem(void **state)
1605{
1606 struct state *st = *state;
1607 const char *data;
1608 const char *values = NULL;
1609
1610 data = ELEMENT_WRAPPER_START "<units name=\"name\"/>" ELEMENT_WRAPPER_END;
1611 assert_int_equal(test_element_helper(st, &data, &values, NULL, NULL, true), LY_SUCCESS);
1612 assert_string_equal(values, "name");
1613 FREE_STRING(st->ctx, values);
1614
1615 st->finished_correctly = true;
1616}
1617
1618static void
1619test_when_elem(void **state)
1620{
1621 struct state *st = *state;
1622 const char *data;
1623 struct lysp_when *when = NULL;
1624
1625 data = ELEMENT_WRAPPER_START
1626 "<when condition=\"cond\">"
1627 "<description><text>desc</text></description>"
1628 "<reference><text>ref</text></reference>"
1629 "</when>"
1630 ELEMENT_WRAPPER_END;
1631 assert_int_equal(test_element_helper(st, &data, &when, NULL, NULL, true), LY_SUCCESS);
1632 assert_string_equal(when->cond, "cond");
1633 assert_string_equal(when->dsc, "desc");
1634 assert_string_equal(when->ref, "ref");
1635 lysp_when_free(st->ctx, when);
1636 free(when);
1637 when = NULL;
1638
1639 data = ELEMENT_WRAPPER_START "<when condition=\"cond\" />" ELEMENT_WRAPPER_END;
1640 assert_int_equal(test_element_helper(st, &data, &when, NULL, NULL, true), LY_SUCCESS);
1641 assert_string_equal(when->cond, "cond");
1642 lysp_when_free(st->ctx, when);
1643 free(when);
1644 when = NULL;
1645
1646 st->finished_correctly = true;
1647}
1648
1649static void
1650test_yin_text_value_elem(void **state)
1651{
1652 struct state *st = *state;
1653 const char *data;
1654 const char *val;
1655
1656 data = ELEMENT_WRAPPER_START "<text>text</text>" ELEMENT_WRAPPER_END;
1657 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1658 assert_string_equal(val, "text");
1659 FREE_STRING(st->ctx, val);
1660
1661 data = "<error-message xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <value>text</value> </error-message>";
1662 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1663 assert_string_equal(val, "text");
1664 FREE_STRING(st->ctx, val);
1665
1666 data = ELEMENT_WRAPPER_START "<text></text>" ELEMENT_WRAPPER_END;
1667 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1668 assert_string_equal("", val);
1669 FREE_STRING(st->ctx, val);
1670
David Sedlák8e7bda82019-07-16 17:57:50 +02001671 st->finished_correctly = true;
1672}
David Sedlák32488102019-07-15 17:44:10 +02001673
David Sedlák374d2b32019-07-17 15:06:55 +02001674static void
1675test_type_elem(void **state)
1676{
1677 struct state *st = *state;
1678 const char *data;
1679 struct lysp_type type = {};
1680
1681 /* max subelems */
1682 data = ELEMENT_WRAPPER_START
1683 "<type name=\"type-name\">"
1684 "<base name=\"base-name\"/>"
1685 "<bit name=\"bit\"/>"
1686 "<enum name=\"enum\"/>"
1687 "<fraction-digits value=\"2\"/>"
1688 "<length value=\"length\"/>"
1689 "<path value=\"path\"/>"
1690 "<pattern value=\"pattern\"/>"
1691 "<range value=\"range\" />"
1692 "<require-instance value=\"true\"/>"
1693 "<type name=\"sub-type-name\"/>"
1694 "</type>"
1695 ELEMENT_WRAPPER_END;
1696 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1697 assert_string_equal(type.name, "type-name");
1698 assert_string_equal(*type.bases, "base-name");
1699 assert_string_equal(type.bits->name, "bit");
1700 assert_string_equal(type.enums->name, "enum");
1701 assert_int_equal(type.fraction_digits, 2);
1702 assert_string_equal(type.length->arg, "length");
1703 assert_string_equal(type.path, "path");
1704 assert_string_equal(type.patterns->arg, "\006pattern");
1705 assert_string_equal(type.range->arg, "range");
1706 assert_int_equal(type.require_instance, 1);
1707 assert_string_equal(type.types->name, "sub-type-name");
1708 lysp_type_free(st->ctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001709 assert_true(type.flags & LYS_SET_BASE);
1710 assert_true(type.flags & LYS_SET_BIT);
1711 assert_true(type.flags & LYS_SET_ENUM);
1712 assert_true(type.flags & LYS_SET_FRDIGITS);
1713 assert_true(type.flags & LYS_SET_LENGTH);
1714 assert_true(type.flags & LYS_SET_PATH);
1715 assert_true(type.flags & LYS_SET_PATTERN);
1716 assert_true(type.flags & LYS_SET_RANGE);
1717 assert_true(type.flags & LYS_SET_REQINST);
1718 assert_true(type.flags & LYS_SET_TYPE);
David Sedlák374d2b32019-07-17 15:06:55 +02001719 memset(&type, 0, sizeof(type));
1720
1721 /* min subelems */
1722 data = ELEMENT_WRAPPER_START "<type name=\"type-name\"/>" ELEMENT_WRAPPER_END;
1723 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1724 lysp_type_free(st->ctx, &type);
1725 memset(&type, 0, sizeof(type));
1726
1727 st->finished_correctly = true;
1728}
1729
David Sedlák1af868e2019-07-17 17:03:14 +02001730static void
1731test_max_elems_elem(void **state)
1732{
1733 struct state *st = *state;
1734 const char *data;
1735 struct lysp_node_list list = {};
1736 struct lysp_node_leaflist llist = {};
1737 struct lysp_refine refine = {};
1738
1739 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"unbounded\"/> </refine>";
1740 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
1741 assert_int_equal(refine.max, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001742 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001743
1744 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"5\"/> </list>";
1745 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, true), LY_SUCCESS);
1746 assert_int_equal(list.max, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001747 assert_true(list.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001748
1749 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"85\"/> </leaf-list>";
1750 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, true), LY_SUCCESS);
1751 assert_int_equal(llist.max, 85);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001752 assert_true(llist.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001753
1754 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"10\"/> </refine>";
1755 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
1756 assert_int_equal(refine.max, 10);
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=\"0\"/> </list>";
1760 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1761 logbuf_assert("Invalid value \"0\" of \"max-elements\". Line number 1.");
1762
1763 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"-10\"/> </list>";
1764 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1765 logbuf_assert("Invalid value \"-10\" of \"max-elements\". Line number 1.");
1766
1767 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"k\"/> </list>";
1768 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1769 logbuf_assert("Invalid value \"k\" of \"max-elements\". Line number 1.");
1770
1771 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"u12\"/> </list>";
1772 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1773 logbuf_assert("Invalid value \"u12\" of \"max-elements\". Line number 1.");
1774
1775 st->finished_correctly = true;
1776}
1777
David Sedlák09e18c92019-07-18 11:17:11 +02001778static void
1779test_min_elems_elem(void **state)
1780{
1781 struct state *st = *state;
1782 const char *data;
1783 struct lysp_node_list list = {};
1784 struct lysp_node_leaflist llist = {};
1785 struct lysp_refine refine = {};
1786
1787 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"0\"/> </refine>";
1788 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
1789 assert_int_equal(refine.min, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001790 assert_true(refine.flags & LYS_SET_MIN);
David Sedlák09e18c92019-07-18 11:17:11 +02001791
1792 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"41\"/> </list>";
1793 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, true), LY_SUCCESS);
1794 assert_int_equal(list.min, 41);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001795 assert_true(list.flags & LYS_SET_MIN);
David Sedlák09e18c92019-07-18 11:17:11 +02001796
1797 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"50\"/> </leaf-list>";
1798 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, true), LY_SUCCESS);
1799 assert_int_equal(llist.min, 50);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001800 assert_true(llist.flags & LYS_SET_MIN);
David Sedlák09e18c92019-07-18 11:17:11 +02001801
1802 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"-5\"/> </leaf-list>";
1803 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1804 logbuf_assert("Value \"-5\" is out of \"min-elements\" bounds. Line number 1.");
1805
1806 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"99999999999999999\"/> </leaf-list>";
1807 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1808 logbuf_assert("Value \"99999999999999999\" is out of \"min-elements\" bounds. Line number 1.");
1809
1810 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"5k\"/> </leaf-list>";
1811 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1812 logbuf_assert("Invalid value \"5k\" of \"min-elements\". Line number 1.");
1813
1814 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"05\"/> </leaf-list>";
1815 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1816 logbuf_assert("Invalid value \"05\" of \"min-elements\". Line number 1.");
1817
1818 st->finished_correctly = true;
1819}
1820
David Sedláka2dad212019-07-18 12:45:19 +02001821static void
1822test_ordby_elem(void **state)
1823{
1824 struct state *st = *state;
1825 const char *data;
1826 uint16_t flags = 0;
1827
1828 data = ELEMENT_WRAPPER_START "<ordered-by value=\"system\"/>" ELEMENT_WRAPPER_END;
1829 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001830 assert_true(flags & LYS_ORDBY_SYSTEM);
David Sedláka2dad212019-07-18 12:45:19 +02001831
1832 data = ELEMENT_WRAPPER_START "<ordered-by value=\"user\"/>" ELEMENT_WRAPPER_END;
1833 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001834 assert_true(flags & LYS_ORDBY_USER);
David Sedláka2dad212019-07-18 12:45:19 +02001835
1836 data = ELEMENT_WRAPPER_START "<ordered-by value=\"inv\"/>" ELEMENT_WRAPPER_END;
1837 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
1838 logbuf_assert("Invalid value \"inv\" of \"ordered-by\". Line number 1.");
1839
1840 st->finished_correctly = true;
1841}
1842
David Sedlák8a83bbb2019-07-18 14:46:00 +02001843static void
1844test_any_elem(void **state)
1845{
1846 struct state *st = *state;
1847 const char *data;
1848 struct lysp_node *siblings = NULL;
1849 struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
1850 struct lysp_node_anydata *parsed = NULL;
1851
1852 /* anyxml max subelems */
1853 data = ELEMENT_WRAPPER_START
1854 "<anyxml name=\"any-name\">"
1855 "<config value=\"true\" />"
1856 "<description><text>desc</text></description>"
1857 "<if-feature name=\"feature\" />"
1858 "<mandatory value=\"true\" />"
1859 "<must condition=\"must-cond\" />"
1860 "<reference><text>ref</text></reference>"
1861 "<status value=\"deprecated\"/>"
1862 "<when condition=\"when-cond\"/>"
1863 "</anyxml>"
1864 ELEMENT_WRAPPER_END;
1865 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1866 parsed = (struct lysp_node_anydata *)siblings;
1867 assert_null(parsed->parent);
1868 assert_int_equal(parsed->nodetype, LYS_ANYXML);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001869 assert_true(parsed->flags & LYS_CONFIG_W);
1870 assert_true(parsed->flags & LYS_MAND_TRUE);
1871 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001872 assert_null(parsed->next);
1873 assert_string_equal(parsed->name, "any-name");
1874 assert_string_equal(parsed->dsc, "desc");
1875 assert_string_equal(parsed->ref, "ref");
1876 assert_string_equal(parsed->when->cond, "when-cond");
1877 assert_string_equal(*parsed->iffeatures, "feature");
1878 assert_null(parsed->exts);
1879 lysp_node_free(st->ctx, siblings);
1880 siblings = NULL;
1881
1882 /* anydata max subelems */
1883 data = ELEMENT_WRAPPER_START
1884 "<anydata name=\"any-name\">"
1885 "<config value=\"true\" />"
1886 "<description><text>desc</text></description>"
1887 "<if-feature name=\"feature\" />"
1888 "<mandatory value=\"true\" />"
1889 "<must condition=\"must-cond\" />"
1890 "<reference><text>ref</text></reference>"
1891 "<status value=\"deprecated\"/>"
1892 "<when condition=\"when-cond\"/>"
1893 "</anydata>"
1894 ELEMENT_WRAPPER_END;
1895 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1896 parsed = (struct lysp_node_anydata *)siblings;
1897 assert_null(parsed->parent);
1898 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001899 assert_true(parsed->flags & LYS_CONFIG_W);
1900 assert_true(parsed->flags & LYS_MAND_TRUE);
1901 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001902 assert_null(parsed->next);
1903 assert_string_equal(parsed->name, "any-name");
1904 assert_string_equal(parsed->dsc, "desc");
1905 assert_string_equal(parsed->ref, "ref");
1906 assert_string_equal(parsed->when->cond, "when-cond");
1907 assert_string_equal(*parsed->iffeatures, "feature");
1908 assert_null(parsed->exts);
1909 lysp_node_free(st->ctx, siblings);
1910 siblings = NULL;
1911
1912 /* min subelems */
1913 node_meta.parent = (void *)0x10;
1914 data = ELEMENT_WRAPPER_START "<anydata name=\"any-name\"> </anydata>" ELEMENT_WRAPPER_END;
1915 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1916 parsed = (struct lysp_node_anydata *)siblings;
1917 assert_ptr_equal(parsed->parent, node_meta.parent);
1918 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
1919 assert_null(parsed->next);
1920 assert_null(parsed->exts);
1921 lysp_node_free(st->ctx, siblings);
1922
1923 st->finished_correctly = true;
1924}
1925
David Sedlák203ca3a2019-07-18 15:26:25 +02001926static void
1927test_leaf_elem(void **state)
1928{
1929 struct state *st = *state;
1930 const char *data;
1931 struct lysp_node *siblings = NULL;
1932 struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
1933 struct lysp_node_leaf *parsed = NULL;
1934
1935 /* max elements */
1936 data = ELEMENT_WRAPPER_START
1937 "<leaf name=\"leaf\">"
1938 "<config value=\"true\" />"
1939 "<default value=\"def-val\"/>"
1940 "<description><text>desc</text></description>"
1941 "<if-feature name=\"feature\" />"
1942 "<mandatory value=\"true\" />"
1943 "<must condition=\"must-cond\" />"
1944 "<reference><text>ref</text></reference>"
1945 "<status value=\"deprecated\"/>"
1946 "<type name=\"type\"/>"
1947 "<units name=\"uni\"/>"
1948 "<when condition=\"when-cond\"/>"
1949 "</leaf>"
1950 ELEMENT_WRAPPER_END;
1951 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1952 parsed = (struct lysp_node_leaf *)siblings;
1953 assert_null(parsed->parent);
1954 assert_int_equal(parsed->nodetype, LYS_LEAF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001955 assert_true(parsed->flags & LYS_CONFIG_W);
1956 assert_true(parsed->flags & LYS_MAND_TRUE);
1957 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák203ca3a2019-07-18 15:26:25 +02001958 assert_null(parsed->next);
1959 assert_string_equal(parsed->name, "leaf");
1960 assert_string_equal(parsed->dsc, "desc");
1961 assert_string_equal(parsed->ref, "ref");
1962 assert_string_equal(parsed->when->cond, "when-cond");
1963 assert_string_equal(*parsed->iffeatures, "feature");
1964 assert_null(parsed->exts);
1965 assert_string_equal(parsed->musts->arg, "must-cond");
1966 assert_string_equal(parsed->type.name, "type");
1967 assert_string_equal(parsed->units, "uni");
1968 assert_string_equal(parsed->dflt, "def-val");
1969 lysp_node_free(st->ctx, siblings);
1970 siblings = NULL;
1971
1972 /* min elements */
1973 data = ELEMENT_WRAPPER_START "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>" ELEMENT_WRAPPER_END;
1974 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1975 parsed = (struct lysp_node_leaf *)siblings;
1976 assert_string_equal(parsed->name, "leaf");
1977 assert_string_equal(parsed->type.name, "type");
1978 lysp_node_free(st->ctx, siblings);
1979 siblings = NULL;
1980
1981 st->finished_correctly = true;
1982}
1983
David Sedlákc3da3ef2019-07-19 12:56:08 +02001984static void
1985test_leaf_list_elem(void **state)
1986{
1987 struct state *st = *state;
1988 const char *data;
1989 struct lysp_node *siblings = NULL;
1990 struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
1991 struct lysp_node_leaflist *parsed = NULL;
1992
1993 data = ELEMENT_WRAPPER_START
1994 "<leaf-list name=\"llist\">"
1995 "<config value=\"true\" />"
1996 "<default value=\"def-val0\"/>"
1997 "<default value=\"def-val1\"/>"
1998 "<description><text>desc</text></description>"
1999 "<if-feature name=\"feature\"/>"
2000 "<max-elements value=\"5\"/>"
2001 "<must condition=\"must-cond\"/>"
2002 "<ordered-by value=\"user\" />"
2003 "<reference><text>ref</text></reference>"
2004 "<status value=\"current\"/>"
2005 "<type name=\"type\"/>"
2006 "<units name=\"uni\"/>"
2007 "<when condition=\"when-cond\"/>"
2008 "</leaf-list>"
2009 ELEMENT_WRAPPER_END;
2010 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2011 parsed = (struct lysp_node_leaflist *)siblings;
2012 assert_string_equal(parsed->dflts[0], "def-val0");
2013 assert_string_equal(parsed->dflts[1], "def-val1");
2014 assert_string_equal(parsed->dsc, "desc");
2015 assert_string_equal(*parsed->iffeatures, "feature");
2016 assert_int_equal(parsed->max, 5);
2017 assert_string_equal(parsed->musts->arg, "must-cond");
2018 assert_string_equal(parsed->name, "llist");
2019 assert_null(parsed->next);
2020 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2021 assert_null(parsed->parent);
2022 assert_string_equal(parsed->ref, "ref");
2023 assert_string_equal(parsed->type.name, "type");
2024 assert_string_equal(parsed->units, "uni");
2025 assert_string_equal(parsed->when->cond, "when-cond");
2026 assert_true(parsed->flags & LYS_CONFIG_W);
2027 assert_true(parsed->flags & LYS_ORDBY_USER);
2028 assert_true(parsed->flags & LYS_STATUS_CURR);
2029 lysp_node_free(st->ctx, siblings);
2030 siblings = NULL;
2031
2032 data = ELEMENT_WRAPPER_START
2033 "<leaf-list name=\"llist\">"
2034 "<config value=\"true\" />"
2035 "<description><text>desc</text></description>"
2036 "<if-feature name=\"feature\"/>"
2037 "<min-elements value=\"5\"/>"
2038 "<must condition=\"must-cond\"/>"
2039 "<ordered-by value=\"user\" />"
2040 "<reference><text>ref</text></reference>"
2041 "<status value=\"current\"/>"
2042 "<type name=\"type\"/>"
2043 "<units name=\"uni\"/>"
2044 "<when condition=\"when-cond\"/>"
2045 "</leaf-list>"
2046 ELEMENT_WRAPPER_END;
2047 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2048 parsed = (struct lysp_node_leaflist *)siblings;
2049 assert_string_equal(parsed->dsc, "desc");
2050 assert_string_equal(*parsed->iffeatures, "feature");
2051 assert_int_equal(parsed->min, 5);
2052 assert_string_equal(parsed->musts->arg, "must-cond");
2053 assert_string_equal(parsed->name, "llist");
2054 assert_null(parsed->next);
2055 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2056 assert_null(parsed->parent);
2057 assert_string_equal(parsed->ref, "ref");
2058 assert_string_equal(parsed->type.name, "type");
2059 assert_string_equal(parsed->units, "uni");
2060 assert_string_equal(parsed->when->cond, "when-cond");
2061 assert_true(parsed->flags & LYS_CONFIG_W);
2062 assert_true(parsed->flags & LYS_ORDBY_USER);
2063 assert_true(parsed->flags & LYS_STATUS_CURR);
2064 lysp_node_free(st->ctx, siblings);
2065 siblings = NULL;
2066
2067 data = ELEMENT_WRAPPER_START
2068 "<leaf-list name=\"llist\">"
2069 "<config value=\"true\" />"
2070 "<description><text>desc</text></description>"
2071 "<if-feature name=\"feature\"/>"
2072 "<max-elements value=\"15\"/>"
2073 "<min-elements value=\"5\"/>"
2074 "<must condition=\"must-cond\"/>"
2075 "<ordered-by value=\"user\" />"
2076 "<reference><text>ref</text></reference>"
2077 "<status value=\"current\"/>"
2078 "<type name=\"type\"/>"
2079 "<units name=\"uni\"/>"
2080 "<when condition=\"when-cond\"/>"
2081 "</leaf-list>"
2082 ELEMENT_WRAPPER_END;
2083 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2084 parsed = (struct lysp_node_leaflist *)siblings;
2085 assert_string_equal(parsed->dsc, "desc");
2086 assert_string_equal(*parsed->iffeatures, "feature");
2087 assert_int_equal(parsed->min, 5);
2088 assert_int_equal(parsed->max, 15);
2089 assert_string_equal(parsed->musts->arg, "must-cond");
2090 assert_string_equal(parsed->name, "llist");
2091 assert_null(parsed->next);
2092 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2093 assert_null(parsed->parent);
2094 assert_string_equal(parsed->ref, "ref");
2095 assert_string_equal(parsed->type.name, "type");
2096 assert_string_equal(parsed->units, "uni");
2097 assert_string_equal(parsed->when->cond, "when-cond");
2098 assert_true(parsed->flags & LYS_CONFIG_W);
2099 assert_true(parsed->flags & LYS_ORDBY_USER);
2100 assert_true(parsed->flags & LYS_STATUS_CURR);
2101 lysp_node_free(st->ctx, siblings);
2102 siblings = NULL;
2103
2104 data = ELEMENT_WRAPPER_START
2105 "<leaf-list name=\"llist\">"
2106 "<type name=\"type\"/>"
2107 "</leaf-list>"
2108 ELEMENT_WRAPPER_END;
2109 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2110 parsed = (struct lysp_node_leaflist *)siblings;
2111 assert_string_equal(parsed->name, "llist");
2112 assert_string_equal(parsed->type.name, "type");
2113 lysp_node_free(st->ctx, siblings);
2114 siblings = NULL;
2115
2116 /* invalid combinations */
2117 data = ELEMENT_WRAPPER_START
2118 "<leaf-list name=\"llist\">"
2119 "<max-elements value=\"5\"/>"
2120 "<min-elements value=\"15\"/>"
2121 "<type name=\"type\"/>"
2122 "</leaf-list>"
2123 ELEMENT_WRAPPER_END;
2124 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
2125 logbuf_assert("Invalid combination of min-elements and max-elements: min value 15 is bigger than the max value 5. Line number 1.");
2126 lysp_node_free(st->ctx, siblings);
2127 siblings = NULL;
2128
2129 data = ELEMENT_WRAPPER_START
2130 "<leaf-list name=\"llist\">"
2131 "<default value=\"def-val1\"/>"
2132 "<min-elements value=\"15\"/>"
2133 "<type name=\"type\"/>"
2134 "</leaf-list>"
2135 ELEMENT_WRAPPER_END;
2136 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
2137 logbuf_assert("Invalid combination of keywords \"min-elements\" and \"default\" as substatements of \"leaf-list\". Line number 1.");
2138 lysp_node_free(st->ctx, siblings);
2139 siblings = NULL;
2140
2141 data = ELEMENT_WRAPPER_START
2142 "<leaf-list name=\"llist\">"
2143 "</leaf-list>"
2144 ELEMENT_WRAPPER_END;
2145 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
2146 logbuf_assert("Missing mandatory subelement type of leaf-list element. Line number 1.");
2147 lysp_node_free(st->ctx, siblings);
2148 siblings = NULL;
2149
2150 st->finished_correctly = true;
2151}
2152
David Sedlákcb39f642019-07-19 13:19:55 +02002153static void
2154test_presence_elem(void **state)
2155{
2156 struct state *st = *state;
2157 const char *data;
2158 const char *val;
2159
2160 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\"/>" ELEMENT_WRAPPER_END;
2161 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2162 assert_string_equal(val, "presence-val");
2163 FREE_STRING(st->ctx, val);
2164
2165 data = ELEMENT_WRAPPER_START "<presence/>" ELEMENT_WRAPPER_END;
2166 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
2167 logbuf_assert("Missing mandatory attribute value of presence element. Line number 1.");
2168
2169 st->finished_correctly = true;
2170}
2171
David Sedlák12470a82019-07-19 13:44:36 +02002172static void
2173test_key_elem(void **state)
2174{
2175 struct state *st = *state;
2176 const char *data;
2177 const char *val;
2178
2179 data = ELEMENT_WRAPPER_START "<key value=\"key-value\"/>" ELEMENT_WRAPPER_END;
2180 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2181 assert_string_equal(val, "key-value");
2182 FREE_STRING(st->ctx, val);
2183
2184 data = ELEMENT_WRAPPER_START "<key/>" ELEMENT_WRAPPER_END;
2185 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
2186 logbuf_assert("Missing mandatory attribute value of key element. Line number 1.");
2187
2188 st->finished_correctly = true;
2189}
2190
David Sedlák04e17b22019-07-19 15:29:48 +02002191static void
2192test_typedef_elem(void **state)
2193{
2194 struct state *st = *state;
2195 const char *data;
2196 struct lysp_tpdf *tpdfs = NULL;
2197 struct typedef_meta typdef_meta = {NULL, &tpdfs};
2198
2199 data = ELEMENT_WRAPPER_START
2200 "<typedef name=\"tpdf-name\">"
2201 "<default value=\"def-val\"/>"
2202 "<description><text>desc-text</text></description>"
2203 "<reference><text>ref-text</text></reference>"
2204 "<status value=\"current\"/>"
2205 "<type name=\"type\"/>"
2206 "<units name=\"uni\"/>"
2207 "</typedef>"
2208 ELEMENT_WRAPPER_END;
2209 assert_int_equal(test_element_helper(st, &data, &typdef_meta, NULL, NULL, true), LY_SUCCESS);
2210 assert_string_equal(tpdfs[0].dflt, "def-val");
2211 assert_string_equal(tpdfs[0].dsc, "desc-text");
2212 assert_null(tpdfs[0].exts);
2213 assert_string_equal(tpdfs[0].name, "tpdf-name");
2214 assert_string_equal(tpdfs[0].ref, "ref-text");
2215 assert_string_equal(tpdfs[0].type.name, "type");
2216 assert_string_equal(tpdfs[0].units, "uni");
2217 assert_true(tpdfs[0].flags & LYS_STATUS_CURR);
2218 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2219 tpdfs = NULL;
2220
2221 data = ELEMENT_WRAPPER_START
2222 "<typedef name=\"tpdf-name\">"
2223 "<type name=\"type\"/>"
2224 "</typedef>"
2225 ELEMENT_WRAPPER_END;
2226 assert_int_equal(test_element_helper(st, &data, &typdef_meta, NULL, NULL, true), LY_SUCCESS);
2227 assert_string_equal(tpdfs[0].name, "tpdf-name");
2228 assert_string_equal(tpdfs[0].type.name, "type");
2229 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2230 tpdfs = NULL;
2231
2232 st->finished_correctly = true;
2233}
2234
David Sedlákd2d676a2019-07-22 11:28:19 +02002235static void
2236test_refine_elem(void **state)
2237{
2238 struct state *st = *state;
2239 const char *data;
2240 struct lysp_refine *refines = NULL;
2241
2242 /* max subelems */
2243 data = ELEMENT_WRAPPER_START
2244 "<refine target-node=\"target\">"
2245 "<if-feature name=\"feature\" />"
2246 "<must condition=\"cond\" />"
2247 "<presence value=\"presence\" />"
2248 "<default value=\"def\" />"
2249 "<config value=\"true\" />"
2250 "<mandatory value=\"true\" />"
2251 "<min-elements value=\"10\" />"
2252 "<max-elements value=\"20\" />"
2253 "<description><text>desc</text></description>"
2254 "<reference><text>ref</text></reference>"
2255 "</refine>"
2256 ELEMENT_WRAPPER_END;
2257 assert_int_equal(test_element_helper(st, &data, &refines, NULL, NULL, true), LY_SUCCESS);
2258 assert_string_equal(refines->nodeid, "target");
2259 assert_string_equal(*refines->dflts, "def");
2260 assert_string_equal(refines->dsc, "desc");
2261 assert_null(refines->exts);
2262 assert_true(refines->flags & LYS_CONFIG_W);
2263 assert_true(refines->flags & LYS_MAND_TRUE);
2264 assert_string_equal(*refines->iffeatures, "feature");
2265 assert_int_equal(refines->max, 20);
2266 assert_int_equal(refines->min, 10);
2267 assert_string_equal(refines->musts->arg, "cond");
2268 assert_string_equal(refines->presence, "presence");
2269 assert_string_equal(refines->ref, "ref");
2270 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2271 refines = NULL;
2272
2273 /* min subelems */
2274 data = ELEMENT_WRAPPER_START "<refine target-node=\"target\" />" ELEMENT_WRAPPER_END;
2275 assert_int_equal(test_element_helper(st, &data, &refines, NULL, NULL, true), LY_SUCCESS);
2276 assert_string_equal(refines->nodeid, "target");
2277 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2278 refines = NULL;
2279
2280 st->finished_correctly = true;
2281}
2282
David Sedlák0d6de5a2019-07-22 13:25:44 +02002283static void
2284test_uses_elem(void **state)
2285{
2286 struct state *st = *state;
2287 const char *data;
2288 struct lysp_node *siblings = NULL;
2289 struct tree_node_meta node_meta = {NULL, &siblings};
2290 struct lysp_node_uses *parsed = NULL;
2291
2292 /* max subelems */
2293 data = ELEMENT_WRAPPER_START
2294 "<uses name=\"uses-name\">"
2295 "<when condition=\"cond\" />"
2296 "<if-feature name=\"feature\" />"
2297 "<status value=\"obsolete\" />"
2298 "<description><text>desc</text></description>"
2299 "<reference><text>ref</text></reference>"
2300 "<refine target-node=\"target\"/>"
David Sedlák992fb7c2019-07-24 16:51:01 +02002301 "<augment target-node=\"target\" />"
David Sedlák0d6de5a2019-07-22 13:25:44 +02002302 "</uses>"
2303 ELEMENT_WRAPPER_END;
2304 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2305 parsed = (struct lysp_node_uses *)&siblings[0];
2306 assert_string_equal(parsed->name, "uses-name");
2307 assert_string_equal(parsed->dsc, "desc");
2308 assert_null(parsed->exts);
2309 assert_true(parsed->flags & LYS_STATUS_OBSLT);
2310 assert_string_equal(*parsed->iffeatures, "feature");
2311 assert_null(parsed->next);
2312 assert_int_equal(parsed->nodetype, LYS_USES);
2313 assert_null(parsed->parent);
2314 assert_string_equal(parsed->ref, "ref");
2315 assert_string_equal(parsed->refines->nodeid, "target");
2316 assert_string_equal(parsed->when->cond, "cond");
David Sedlák992fb7c2019-07-24 16:51:01 +02002317 assert_string_equal(parsed->augments->nodeid, "target");
David Sedlák0d6de5a2019-07-22 13:25:44 +02002318 lysp_node_free(st->ctx, siblings);
2319 siblings = NULL;
2320
2321 /* min subelems */
2322 data = ELEMENT_WRAPPER_START "<uses name=\"uses-name\"/>" ELEMENT_WRAPPER_END;
2323 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2324 assert_string_equal(siblings[0].name, "uses-name");
2325 lysp_node_free(st->ctx, siblings);
2326 siblings = NULL;
2327
2328 st->finished_correctly = true;
2329}
2330
David Sedlákaa854b02019-07-22 14:17:10 +02002331static void
2332test_revision_elem(void **state)
2333{
2334 struct state *st = *state;
2335 const char *data;
2336 struct lysp_revision *revs = NULL;
2337
2338 /* max subelems */
2339 data = ELEMENT_WRAPPER_START
2340 "<revision date=\"2018-12-25\">"
2341 "<description><text>desc</text></description>"
2342 "<reference><text>ref</text></reference>"
2343 "</revision>"
2344 ELEMENT_WRAPPER_END;
2345 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, true), LY_SUCCESS);
2346 assert_string_equal(revs->date, "2018-12-25");
2347 assert_string_equal(revs->dsc, "desc");
2348 assert_string_equal(revs->ref, "ref");
2349 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2350 revs = NULL;
2351
2352 /* min subelems */
2353 data = ELEMENT_WRAPPER_START "<revision date=\"2005-05-05\" />" ELEMENT_WRAPPER_END;
2354 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, true), LY_SUCCESS);
2355 assert_string_equal(revs->date, "2005-05-05");
2356 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2357 revs = NULL;
2358
2359 /* invalid value */
2360 data = ELEMENT_WRAPPER_START "<revision date=\"05-05-2005\" />" ELEMENT_WRAPPER_END;
2361 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, false), LY_EVALID);
2362 logbuf_assert("Invalid value \"05-05-2005\" of \"revision\". Line number 1.");
2363 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2364 revs = NULL;
2365
2366 st->finished_correctly = true;
2367}
2368
David Sedlák0c2bab92019-07-22 15:33:19 +02002369static void
2370test_include_elem(void **state)
2371{
2372 struct state *st = *state;
2373 const char *data;
2374 struct lysp_include *includes = NULL;
2375 struct include_meta inc_meta = {"module-name", &includes};
2376
2377 /* max subelems */
2378 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2379 data = ELEMENT_WRAPPER_START
2380 "<include module=\"mod\">"
2381 "<description><text>desc</text></description>"
2382 "<reference><text>ref</text></reference>"
2383 "<revision-date date=\"1999-09-09\"/>"
2384 "</include>"
2385 ELEMENT_WRAPPER_END;
2386 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, true), LY_SUCCESS);
2387 assert_string_equal(includes->name, "mod");
2388 assert_string_equal(includes->dsc, "desc");
2389 assert_string_equal(includes->ref, "ref");
2390 assert_null(includes->exts);
2391 assert_string_equal(includes->rev, "1999-09-09");
2392 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2393 includes = NULL;
2394
2395 /* min subelems */
2396 data = ELEMENT_WRAPPER_START "<include module=\"mod\"/>" ELEMENT_WRAPPER_END;
2397 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, true), LY_SUCCESS);
2398 assert_string_equal(includes->name, "mod");
2399 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2400 includes = NULL;
2401
2402 /* invalid combinations */
2403 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2404 data = ELEMENT_WRAPPER_START
2405 "<include module=\"mod\">"
2406 "<description><text>desc</text></description>"
2407 "<revision-date date=\"1999-09-09\"/>"
2408 "</include>"
2409 ELEMENT_WRAPPER_END;
2410 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, false), LY_EVALID);
2411 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.");
2412 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2413 includes = NULL;
2414
2415 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2416 data = ELEMENT_WRAPPER_START
2417 "<include module=\"mod\">"
2418 "<reference><text>ref</text></reference>"
2419 "<revision-date date=\"1999-09-09\"/>"
2420 "</include>"
2421 ELEMENT_WRAPPER_END;
2422 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, false), LY_EVALID);
2423 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.");
2424 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2425 includes = NULL;
2426
2427 st->finished_correctly = true;
2428}
2429
David Sedlák5e13dea2019-07-22 16:06:45 +02002430static void
2431test_feature_elem(void **state)
2432{
2433 struct state *st = *state;
2434 const char *data;
2435 struct lysp_feature *features = NULL;
2436
2437 /* max subelems */
2438 data = ELEMENT_WRAPPER_START
2439 "<feature name=\"feature-name\">"
2440 "<if-feature name=\"iff\"/>"
2441 "<status value=\"deprecated\"/>"
2442 "<description><text>desc</text></description>"
2443 "<reference><text>ref</text></reference>"
2444 "</feature>"
2445 ELEMENT_WRAPPER_END;
2446 assert_int_equal(test_element_helper(st, &data, &features, NULL, NULL, true), LY_SUCCESS);
2447 assert_string_equal(features->name, "feature-name");
2448 assert_string_equal(features->dsc, "desc");
2449 assert_null(features->exts);
2450 assert_true(features->flags & LYS_STATUS_DEPRC);
2451 assert_string_equal(*features->iffeatures, "iff");
2452 assert_string_equal(features->ref, "ref");
2453 FREE_ARRAY(st->ctx, features, lysp_feature_free);
2454 features = NULL;
2455
2456 /* min subelems */
2457 data = ELEMENT_WRAPPER_START "<feature name=\"feature-name\"/>" ELEMENT_WRAPPER_END;
2458 assert_int_equal(test_element_helper(st, &data, &features, NULL, NULL, true), LY_SUCCESS);
2459 assert_string_equal(features->name, "feature-name");
2460 FREE_ARRAY(st->ctx, features, lysp_feature_free);
2461 features = NULL;
2462
2463 st->finished_correctly = true;
2464}
2465
David Sedlák28794f22019-07-22 16:45:00 +02002466static void
2467test_identity_elem(void **state)
2468{
2469 struct state *st = *state;
2470 const char *data;
2471 struct lysp_ident *identities = NULL;
2472
2473 /* max subelems */
2474 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2475 data = ELEMENT_WRAPPER_START
2476 "<identity name=\"ident-name\">"
2477 "<if-feature name=\"iff\"/>"
2478 "<base name=\"base-name\"/>"
2479 "<status value=\"deprecated\"/>"
2480 "<description><text>desc</text></description>"
2481 "<reference><text>ref</text></reference>"
2482 "</identity>"
2483 ELEMENT_WRAPPER_END;
2484 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, true), LY_SUCCESS);
2485 assert_string_equal(identities->name, "ident-name");
2486 assert_string_equal(*identities->bases, "base-name");
2487 assert_string_equal(*identities->iffeatures, "iff");
2488 assert_string_equal(identities->dsc, "desc");
2489 assert_string_equal(identities->ref, "ref");
2490 assert_true(identities->flags & LYS_STATUS_DEPRC);
2491 assert_null(identities->exts);
2492 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2493 identities = NULL;
2494
2495 /* min subelems */
2496 data = ELEMENT_WRAPPER_START "<identity name=\"ident-name\" />" ELEMENT_WRAPPER_END;
2497 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, true), LY_SUCCESS);
2498 assert_string_equal(identities->name, "ident-name");
2499 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2500 identities = NULL;
2501
2502 /* invalid */
2503 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2504 data = ELEMENT_WRAPPER_START
2505 "<identity name=\"ident-name\">"
2506 "<if-feature name=\"iff\"/>"
2507 "</identity>"
2508 ELEMENT_WRAPPER_END;
2509 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, false), LY_EVALID);
2510 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.");
2511 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2512 identities = NULL;
2513
2514 st->finished_correctly = true;
2515}
2516
David Sedlákaf536aa2019-07-23 13:42:23 +02002517static void
2518test_list_elem(void **state)
2519{
2520 struct state *st = *state;
2521 const char *data;
2522 struct lysp_node *siblings = NULL;
2523 struct tree_node_meta node_meta = {NULL, &siblings};
2524 struct lysp_node_list *parsed = NULL;
2525
2526 /* max subelems */
2527 data = ELEMENT_WRAPPER_START
2528 "<list name=\"list-name\">"
2529 "<when condition=\"when\"/>"
2530 "<if-feature name=\"iff\"/>"
2531 "<must condition=\"must-cond\"/>"
2532 "<key value=\"key\"/>"
2533 "<unique tag=\"utag\"/>"
2534 "<config value=\"true\"/>"
2535 "<min-elements value=\"10\"/>"
2536 "<ordered-by value=\"user\"/>"
2537 "<status value=\"deprecated\"/>"
2538 "<description><text>desc</text></description>"
2539 "<reference><text>ref</text></reference>"
2540 "<anydata name=\"anyd\"/>"
2541 "<anyxml name=\"anyx\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02002542 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002543 "<choice name=\"choice\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02002544 "<action name=\"action\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02002545 "<grouping name=\"grp\"/>"
David Sedlák031b9e72019-07-23 15:19:37 +02002546 "<notification name=\"notf\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002547 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
2548 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlákaf536aa2019-07-23 13:42:23 +02002549 "<list name=\"sub-list\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002550 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákaf536aa2019-07-23 13:42:23 +02002551 "<uses name=\"uses-name\"/>"
2552 "</list>"
2553 ELEMENT_WRAPPER_END;
2554 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2555 parsed = (struct lysp_node_list *)&siblings[0];
2556 assert_string_equal(parsed->dsc, "desc");
2557 assert_string_equal(parsed->child->name, "anyd");
2558 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2559 assert_string_equal(parsed->child->next->name, "anyx");
2560 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
David Sedlákf111bcb2019-07-23 17:15:51 +02002561 assert_string_equal(parsed->child->next->next->name, "cont");
2562 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002563 assert_string_equal(parsed->child->next->next->next->name, "choice");
2564 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
David Sedlák85d0eca2019-07-24 15:15:21 +02002565 assert_string_equal(parsed->child->next->next->next->next->name, "leaf");
2566 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAF);
2567 assert_string_equal(parsed->child->next->next->next->next->next->name, "llist");
2568 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2569 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "sub-list");
2570 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LIST);
2571 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "uses-name");
2572 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_USES);
2573 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002574 assert_string_equal(parsed->groupings->name, "grp");
David Sedlák85d0eca2019-07-24 15:15:21 +02002575 assert_string_equal(parsed->actions->name, "action");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002576 assert_int_equal(parsed->groupings->nodetype, LYS_GROUPING);
David Sedlák031b9e72019-07-23 15:19:37 +02002577 assert_string_equal(parsed->notifs->name, "notf");
David Sedlákaf536aa2019-07-23 13:42:23 +02002578 assert_null(parsed->exts);
2579 assert_true(parsed->flags & LYS_ORDBY_USER);
2580 assert_true(parsed->flags & LYS_STATUS_DEPRC);
2581 assert_true(parsed->flags & LYS_CONFIG_W);
2582 assert_string_equal(*parsed->iffeatures, "iff");
2583 assert_string_equal(parsed->key, "key");
2584 assert_int_equal(parsed->min, 10);
2585 assert_string_equal(parsed->musts->arg, "must-cond");
2586 assert_string_equal(parsed->name, "list-name");
2587 assert_null(parsed->next);
2588 assert_int_equal(parsed->nodetype, LYS_LIST);
2589 assert_null(parsed->parent);
2590 assert_string_equal(parsed->ref, "ref");
2591 assert_string_equal(parsed->typedefs->name, "tpdf");
2592 assert_string_equal(*parsed->uniques, "utag");
2593 assert_string_equal(parsed->when->cond, "when");
2594 lysp_node_free(st->ctx, siblings);
2595 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
2596 siblings = NULL;
2597
2598 /* min subelems */
2599 data = ELEMENT_WRAPPER_START "<list name=\"list-name\" />" ELEMENT_WRAPPER_END;
2600 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2601 parsed = (struct lysp_node_list *)&siblings[0];
2602 assert_string_equal(parsed->name, "list-name");
2603 lysp_node_free(st->ctx, siblings);
2604 siblings = NULL;
2605
2606 st->finished_correctly = true;
2607}
2608
David Sedlák031b9e72019-07-23 15:19:37 +02002609static void
2610test_notification_elem(void **state)
2611{
2612 struct state *st = *state;
2613 const char *data;
2614 struct lysp_notif *notifs = NULL;
2615 struct notif_meta notif_meta = {NULL, &notifs};
2616
2617 /* max subelems */
2618 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2619 data = ELEMENT_WRAPPER_START
2620 "<notification name=\"notif-name\">"
2621 "<anydata name=\"anyd\"/>"
2622 "<anyxml name=\"anyx\"/>"
2623 "<description><text>desc</text></description>"
2624 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002625 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
2626 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák031b9e72019-07-23 15:19:37 +02002627 "<list name=\"sub-list\"/>"
2628 "<must condition=\"cond\"/>"
2629 "<reference><text>ref</text></reference>"
2630 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002631 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák031b9e72019-07-23 15:19:37 +02002632 "<uses name=\"uses-name\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02002633 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002634 "<choice name=\"choice\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02002635 "<grouping name=\"grp\"/>"
David Sedlák031b9e72019-07-23 15:19:37 +02002636 "</notification>"
2637 ELEMENT_WRAPPER_END;
2638 assert_int_equal(test_element_helper(st, &data, &notif_meta, NULL, NULL, true), LY_SUCCESS);
2639 assert_string_equal(notifs->name, "notif-name");
2640 assert_string_equal(notifs->data->name, "anyd");
2641 assert_int_equal(notifs->data->nodetype, LYS_ANYDATA);
2642 assert_string_equal(notifs->data->next->name, "anyx");
2643 assert_int_equal(notifs->data->next->nodetype, LYS_ANYXML);
2644 assert_string_equal(notifs->data->next->next->name, "leaf");
2645 assert_int_equal(notifs->data->next->next->nodetype, LYS_LEAF);
2646 assert_string_equal(notifs->data->next->next->next->name, "llist");
2647 assert_int_equal(notifs->data->next->next->next->nodetype, LYS_LEAFLIST);
2648 assert_string_equal(notifs->data->next->next->next->next->name, "sub-list");
2649 assert_int_equal(notifs->data->next->next->next->next->nodetype, LYS_LIST);
2650 assert_null(notifs->exts);
2651 assert_true(notifs->flags & LYS_STATUS_DEPRC);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002652 assert_string_equal(notifs->groupings->name, "grp");
2653 assert_int_equal(notifs->groupings->nodetype, LYS_GROUPING);
David Sedlákf111bcb2019-07-23 17:15:51 +02002654 assert_string_equal(notifs->data->next->next->next->next->next->name, "uses-name");
2655 assert_int_equal(notifs->data->next->next->next->next->next->nodetype, LYS_USES);
2656 assert_string_equal(notifs->data->next->next->next->next->next->next->name, "cont");
2657 assert_int_equal(notifs->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002658 assert_int_equal(notifs->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2659 assert_string_equal(notifs->data->next->next->next->next->next->next->next->name, "choice");
2660 assert_null(notifs->data->next->next->next->next->next->next->next->next);
David Sedlák031b9e72019-07-23 15:19:37 +02002661 assert_string_equal(*notifs->iffeatures, "iff");
2662 assert_string_equal(notifs->musts->arg, "cond");
2663 assert_int_equal(notifs->nodetype, LYS_NOTIF);
2664 assert_null(notifs->parent);
2665 assert_string_equal(notifs->ref, "ref");
2666 assert_string_equal(notifs->typedefs->name, "tpdf");
2667 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
2668 notifs = NULL;
2669
2670 /* min subelems */
2671 data = ELEMENT_WRAPPER_START "<notification name=\"notif-name\" />" ELEMENT_WRAPPER_END;
2672 assert_int_equal(test_element_helper(st, &data, &notif_meta, NULL, NULL, true), LY_SUCCESS);
2673 assert_string_equal(notifs->name, "notif-name");
2674 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002675 notifs = NULL;
2676
2677 st->finished_correctly = true;
2678}
2679
2680static void
2681test_grouping_elem(void **state)
2682{
2683 struct state *st = *state;
2684 const char *data;
2685 struct lysp_grp *grps = NULL;
2686 struct grouping_meta grp_meta = {NULL, &grps};
2687
2688 /* max subelems */
2689 data = ELEMENT_WRAPPER_START
2690 "<grouping name=\"grp-name\">"
2691 "<anydata name=\"anyd\"/>"
2692 "<anyxml name=\"anyx\"/>"
2693 "<description><text>desc</text></description>"
2694 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002695 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
2696 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02002697 "<list name=\"list\"/>"
2698 "<notification name=\"notf\"/>"
2699 "<reference><text>ref</text></reference>"
2700 "<status value=\"current\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002701 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02002702 "<uses name=\"uses-name\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02002703 "<action name=\"act\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02002704 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002705 "<choice name=\"choice\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02002706 "</grouping>"
2707 ELEMENT_WRAPPER_END;
2708 assert_int_equal(test_element_helper(st, &data, &grp_meta, NULL, NULL, true), LY_SUCCESS);
2709 assert_string_equal(grps->name, "grp-name");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002710 assert_string_equal(grps->data->name, "anyd");
2711 assert_string_equal(grps->data->next->name, "anyx");
2712 assert_string_equal(grps->data->next->next->name, "leaf");
2713 assert_string_equal(grps->data->next->next->next->name, "llist");
2714 assert_string_equal(grps->data->next->next->next->next->name, "list");
2715 assert_string_equal(grps->dsc, "desc");
2716 assert_null(grps->exts);
2717 assert_true(grps->flags & LYS_STATUS_CURR);
2718 assert_string_equal(grps->groupings->name, "sub-grp");
2719 assert_int_equal(grps->nodetype, LYS_GROUPING);
2720 assert_string_equal(grps->notifs->name, "notf");
2721 assert_null(grps->parent);
2722 assert_string_equal(grps->ref, "ref");
2723 assert_string_equal(grps->typedefs->name, "tpdf");
David Sedlák85d0eca2019-07-24 15:15:21 +02002724 assert_string_equal(grps->actions->name, "act");
David Sedlákf111bcb2019-07-23 17:15:51 +02002725 assert_string_equal(grps->data->next->next->next->next->next->name, "uses-name");
David Sedlákb7abcfa2019-07-24 12:33:35 +02002726 assert_int_equal(grps->data->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákf111bcb2019-07-23 17:15:51 +02002727 assert_string_equal(grps->data->next->next->next->next->next->next->name, "cont");
David Sedlákb7abcfa2019-07-24 12:33:35 +02002728 assert_int_equal(grps->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
2729 assert_string_equal(grps->data->next->next->next->next->next->next->next->name, "choice");
2730 assert_int_equal(grps->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002731 FREE_ARRAY(st->ctx, grps, lysp_grp_free);
2732 grps = NULL;
2733
2734 /* min subelems */
2735 data = ELEMENT_WRAPPER_START "<grouping name=\"grp-name\" />" ELEMENT_WRAPPER_END;
2736 assert_int_equal(test_element_helper(st, &data, &grp_meta, NULL, NULL, true), LY_SUCCESS);
2737 assert_string_equal(grps->name, "grp-name");
2738 FREE_ARRAY(st->ctx, grps, lysp_grp_free);
2739 grps = NULL;
David Sedlák031b9e72019-07-23 15:19:37 +02002740
2741 st->finished_correctly = true;
2742}
2743
David Sedlákf111bcb2019-07-23 17:15:51 +02002744static void
2745test_container_elem(void **state)
2746{
2747 struct state *st = *state;
2748 const char *data;
2749 struct lysp_node *siblings = NULL;
2750 struct tree_node_meta node_meta = {NULL, &siblings};
2751 struct lysp_node_container *parsed = NULL;
2752
2753 /* max subelems */
David Sedláke2dc9e92019-07-24 09:59:21 +02002754 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2755 data = ELEMENT_WRAPPER_START
2756 "<container name=\"cont-name\">"
2757 "<anydata name=\"anyd\"/>"
2758 "<anyxml name=\"anyx\"/>"
2759 "<config value=\"true\"/>"
2760 "<container name=\"subcont\"/>"
2761 "<description><text>desc</text></description>"
2762 "<grouping name=\"sub-grp\"/>"
2763 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002764 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
2765 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedláke2dc9e92019-07-24 09:59:21 +02002766 "<list name=\"list\"/>"
2767 "<must condition=\"cond\"/>"
2768 "<notification name=\"notf\"/>"
2769 "<presence value=\"presence\"/>"
2770 "<reference><text>ref</text></reference>"
2771 "<status value=\"current\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002772 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedláke2dc9e92019-07-24 09:59:21 +02002773 "<uses name=\"uses-name\"/>"
2774 "<when condition=\"when-cond\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02002775 "<action name=\"act\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002776 "<choice name=\"choice\"/>"
David Sedláke2dc9e92019-07-24 09:59:21 +02002777 "</container>"
2778 ELEMENT_WRAPPER_END;
2779 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2780 parsed = (struct lysp_node_container *)siblings;
2781 assert_string_equal(parsed->name, "cont-name");
2782 assert_null(parsed->parent);
2783 assert_int_equal(parsed->nodetype, LYS_CONTAINER);
2784 assert_true(parsed->flags & LYS_CONFIG_W);
2785 assert_true(parsed->flags & LYS_STATUS_CURR);
2786 assert_null(parsed->next);
2787 assert_string_equal(parsed->dsc, "desc");
2788 assert_string_equal(parsed->ref, "ref");
2789 assert_string_equal(parsed->when->cond, "when-cond");
2790 assert_string_equal(*parsed->iffeatures, "iff");
2791 assert_null(parsed->exts);
2792 assert_string_equal(parsed->musts->arg, "cond");
2793 assert_string_equal(parsed->presence, "presence");
2794 assert_string_equal(parsed->typedefs->name, "tpdf");
2795 assert_string_equal(parsed->groupings->name, "sub-grp");
2796 assert_string_equal(parsed->child->name, "anyd");
2797 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2798 assert_string_equal(parsed->child->next->name, "anyx");
2799 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2800 assert_string_equal(parsed->child->next->next->name, "subcont");
2801 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
2802 assert_string_equal(parsed->child->next->next->next->name, "leaf");
2803 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
2804 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
2805 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
2806 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
2807 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
2808 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
2809 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002810 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
2811 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2812 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke2dc9e92019-07-24 09:59:21 +02002813 assert_string_equal(parsed->notifs->name, "notf");
David Sedlák85d0eca2019-07-24 15:15:21 +02002814 assert_string_equal(parsed->actions->name, "act");
David Sedláke2dc9e92019-07-24 09:59:21 +02002815 lysp_node_free(st->ctx, siblings);
2816 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
2817 siblings = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02002818
2819 /* min subelems */
2820 data = ELEMENT_WRAPPER_START "<container name=\"cont-name\" />" ELEMENT_WRAPPER_END;
2821 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2822 parsed = (struct lysp_node_container *)siblings;
2823 assert_string_equal(parsed->name, "cont-name");
2824 lysp_node_free(st->ctx, siblings);
2825 siblings = NULL;
2826
2827 st->finished_correctly = true;
2828}
2829
David Sedlák5379d392019-07-24 10:42:03 +02002830static void
2831test_case_elem(void **state)
2832{
2833 struct state *st = *state;
2834 const char *data;
2835 struct lysp_node *siblings = NULL;
2836 struct tree_node_meta node_meta = {NULL, &siblings};
2837 struct lysp_node_case *parsed = NULL;
2838
2839 /* max subelems */
2840 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2841 data = ELEMENT_WRAPPER_START
2842 "<case name=\"case-name\">"
2843 "<anydata name=\"anyd\"/>"
2844 "<anyxml name=\"anyx\"/>"
2845 "<container name=\"subcont\"/>"
2846 "<description><text>desc</text></description>"
2847 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002848 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
2849 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák5379d392019-07-24 10:42:03 +02002850 "<list name=\"list\"/>"
2851 "<reference><text>ref</text></reference>"
2852 "<status value=\"current\"/>"
2853 "<uses name=\"uses-name\"/>"
2854 "<when condition=\"when-cond\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002855 "<choice name=\"choice\"/>"
David Sedlák5379d392019-07-24 10:42:03 +02002856 "</case>"
2857 ELEMENT_WRAPPER_END;
2858 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2859 parsed = (struct lysp_node_case *)siblings;
2860 assert_string_equal(parsed->name, "case-name");
2861 assert_null(parsed->parent);
2862 assert_int_equal(parsed->nodetype, LYS_CASE);
2863 assert_true(parsed->flags & LYS_STATUS_CURR);
2864 assert_null(parsed->next);
2865 assert_string_equal(parsed->dsc, "desc");
2866 assert_string_equal(parsed->ref, "ref");
2867 assert_string_equal(parsed->when->cond, "when-cond");
2868 assert_string_equal(*parsed->iffeatures, "iff");
2869 assert_null(parsed->exts);
2870 assert_string_equal(parsed->child->name, "anyd");
2871 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2872 assert_string_equal(parsed->child->next->name, "anyx");
2873 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2874 assert_string_equal(parsed->child->next->next->name, "subcont");
2875 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
2876 assert_string_equal(parsed->child->next->next->next->name, "leaf");
2877 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
2878 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
2879 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
2880 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
2881 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
2882 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
2883 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002884 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
2885 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2886 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedlák5379d392019-07-24 10:42:03 +02002887 lysp_node_free(st->ctx, siblings);
2888 siblings = NULL;
2889
2890 /* min subelems */
2891 data = ELEMENT_WRAPPER_START "<case name=\"case-name\" />" ELEMENT_WRAPPER_END;
2892 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2893 parsed = (struct lysp_node_case *)siblings;
2894 assert_string_equal(parsed->name, "case-name");
2895 lysp_node_free(st->ctx, siblings);
2896 siblings = NULL;
2897
2898 st->finished_correctly = true;
2899}
2900
David Sedlákb7abcfa2019-07-24 12:33:35 +02002901static void
2902test_choice_elem(void **state)
2903{
2904 struct state *st = *state;
2905 const char *data;
2906 struct lysp_node *siblings = NULL;
2907 struct tree_node_meta node_meta = {NULL, &siblings};
2908 struct lysp_node_choice *parsed = NULL;
2909
2910 /* max subelems */
2911 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2912 data = ELEMENT_WRAPPER_START
2913 "<choice name=\"choice-name\">"
2914 "<anydata name=\"anyd\"/>"
2915 "<anyxml name=\"anyx\"/>"
2916 "<case name=\"sub-case\"/>"
2917 "<choice name=\"choice\"/>"
2918 "<config value=\"true\"/>"
2919 "<container name=\"subcont\"/>"
2920 "<default value=\"def\"/>"
2921 "<description><text>desc</text></description>"
2922 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002923 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
2924 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002925 "<list name=\"list\"/>"
2926 "<mandatory value=\"true\" />"
2927 "<reference><text>ref</text></reference>"
2928 "<status value=\"current\"/>"
2929 "<when condition=\"when-cond\"/>"
2930 "</choice>"
2931 ELEMENT_WRAPPER_END;
2932 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2933 parsed = (struct lysp_node_choice *)siblings;
2934 assert_string_equal(parsed->name, "choice-name");
2935 assert_null(parsed->parent);
2936 assert_int_equal(parsed->nodetype, LYS_CHOICE);
2937 assert_true(parsed->flags & LYS_CONFIG_W && parsed->flags & LYS_MAND_TRUE && parsed->flags & LYS_STATUS_CURR);
2938 assert_null(parsed->next);
2939 assert_string_equal(parsed->dsc, "desc");
2940 assert_string_equal(parsed->ref, "ref");
2941 assert_string_equal(parsed->when->cond, "when-cond");
2942 assert_string_equal(*parsed->iffeatures, "iff");
2943 assert_null(parsed->exts);
2944 assert_string_equal(parsed->child->name, "anyd");
2945 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2946 assert_string_equal(parsed->child->next->name, "anyx");
2947 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2948 assert_string_equal(parsed->child->next->next->name, "sub-case");
2949 assert_int_equal(parsed->child->next->next->nodetype, LYS_CASE);
2950 assert_string_equal(parsed->child->next->next->next->name, "choice");
2951 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
2952 assert_string_equal(parsed->child->next->next->next->next->name, "subcont");
2953 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_CONTAINER);
2954 assert_string_equal(parsed->child->next->next->next->next->next->name, "leaf");
2955 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAF);
2956 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "llist");
2957 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2958 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "list");
2959 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
2960 assert_null(parsed->child->next->next->next->next->next->next->next->next);
2961 lysp_node_free(st->ctx, siblings);
2962 siblings = NULL;
2963
2964 /* min subelems */
2965 data = ELEMENT_WRAPPER_START "<choice name=\"choice-name\" />" ELEMENT_WRAPPER_END;
2966 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2967 parsed = (struct lysp_node_choice *)siblings;
2968 assert_string_equal(parsed->name, "choice-name");
2969 lysp_node_free(st->ctx, siblings);
2970 siblings = NULL;
2971
2972 st->finished_correctly = true;
2973}
2974
David Sedlák05404f62019-07-24 14:11:53 +02002975static void
2976test_inout_elem(void **state)
2977{
2978 struct state *st = *state;
2979 const char *data;
2980 struct lysp_action_inout inout = {};
2981 struct inout_meta inout_meta = {NULL, &inout};
2982
2983 /* max subelements */
2984 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2985 data = ELEMENT_WRAPPER_START
2986 "<input>"
2987 "<anydata name=\"anyd\"/>"
2988 "<anyxml name=\"anyx\"/>"
2989 "<choice name=\"choice\"/>"
2990 "<container name=\"subcont\"/>"
2991 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002992 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
2993 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák05404f62019-07-24 14:11:53 +02002994 "<list name=\"list\"/>"
2995 "<must condition=\"cond\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002996 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák05404f62019-07-24 14:11:53 +02002997 "<uses name=\"uses-name\"/>"
2998 "</input>"
2999 ELEMENT_WRAPPER_END;
3000 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3001 assert_null(inout.parent);
3002 assert_int_equal(inout.nodetype, LYS_INPUT);
3003 assert_string_equal(inout.musts->arg, "cond");
3004 assert_string_equal(inout.typedefs->name, "tpdf");
3005 assert_string_equal(inout.groupings->name, "sub-grp");
3006 assert_string_equal(inout.data->name, "anyd");
3007 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
3008 assert_string_equal(inout.data->next->name, "anyx");
3009 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
3010 assert_string_equal(inout.data->next->next->name, "choice");
3011 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
3012 assert_string_equal(inout.data->next->next->next->name, "subcont");
3013 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
3014 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
3015 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
3016 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
3017 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3018 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
3019 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
3020 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
3021 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3022 assert_null(inout.data->next->next->next->next->next->next->next->next);
3023 lysp_action_inout_free(st->ctx, &inout);
3024 memset(&inout, 0, sizeof inout);
3025
3026 /* max subelements */
3027 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3028 data = ELEMENT_WRAPPER_START
3029 "<output>"
3030 "<anydata name=\"anyd\"/>"
3031 "<anyxml name=\"anyx\"/>"
3032 "<choice name=\"choice\"/>"
3033 "<container name=\"subcont\"/>"
3034 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003035 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3036 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák05404f62019-07-24 14:11:53 +02003037 "<list name=\"list\"/>"
3038 "<must condition=\"cond\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003039 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák05404f62019-07-24 14:11:53 +02003040 "<uses name=\"uses-name\"/>"
3041 "</output>"
3042 ELEMENT_WRAPPER_END;
3043 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3044 assert_null(inout.parent);
3045 assert_int_equal(inout.nodetype, LYS_OUTPUT);
3046 assert_string_equal(inout.musts->arg, "cond");
3047 assert_string_equal(inout.typedefs->name, "tpdf");
3048 assert_string_equal(inout.groupings->name, "sub-grp");
3049 assert_string_equal(inout.data->name, "anyd");
3050 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
3051 assert_string_equal(inout.data->next->name, "anyx");
3052 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
3053 assert_string_equal(inout.data->next->next->name, "choice");
3054 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
3055 assert_string_equal(inout.data->next->next->next->name, "subcont");
3056 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
3057 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
3058 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
3059 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
3060 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3061 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
3062 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
3063 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
3064 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3065 assert_null(inout.data->next->next->next->next->next->next->next->next);
3066 lysp_action_inout_free(st->ctx, &inout);
3067 memset(&inout, 0, sizeof inout);
3068
3069 /* min subelems */
3070 data = ELEMENT_WRAPPER_START "<input />" ELEMENT_WRAPPER_END;
3071 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3072 memset(&inout, 0, sizeof inout);
3073
3074 data = ELEMENT_WRAPPER_START "<output />" ELEMENT_WRAPPER_END;
3075 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3076 memset(&inout, 0, sizeof inout);
3077
3078 /* invalid combinations */
3079 data = ELEMENT_WRAPPER_START "<input name=\"test\"/>" ELEMENT_WRAPPER_END;
3080 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, false), LY_EVALID);
3081 logbuf_assert("Unexpected attribute \"name\" of input element. Line number 1.");
3082 memset(&inout, 0, sizeof inout);
3083
3084 st->finished_correctly = true;
3085}
3086
David Sedlák85d0eca2019-07-24 15:15:21 +02003087static void
3088test_action_elem(void **state)
3089{
3090 struct state *st = *state;
3091 const char *data;
3092 struct lysp_action *actions = NULL;
3093 struct action_meta act_meta = {NULL, &actions};
3094
3095 /* max subelems */
3096 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3097 data = ELEMENT_WRAPPER_START
3098 "<action name=\"act\">"
3099 "<description><text>desc</text></description>"
3100 "<grouping name=\"grouping\"/>"
3101 "<if-feature name=\"iff\"/>"
3102 "<input><uses name=\"uses-name\"/></input>"
3103 "<output><must condition=\"cond\"/></output>"
3104 "<reference><text>ref</text></reference>"
3105 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003106 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák85d0eca2019-07-24 15:15:21 +02003107 "</action>"
3108 ELEMENT_WRAPPER_END;
3109 assert_int_equal(test_element_helper(st, &data, &act_meta, NULL, NULL, true), LY_SUCCESS);
3110 assert_null(actions->parent);
3111 assert_int_equal(actions->nodetype, LYS_ACTION);
3112 assert_true(actions->flags & LYS_STATUS_DEPRC);
3113 assert_string_equal(actions->name, "act");
3114 assert_string_equal(actions->dsc, "desc");
3115 assert_string_equal(actions->ref, "ref");
3116 assert_string_equal(*actions->iffeatures, "iff");
3117 assert_string_equal(actions->typedefs->name, "tpdf");
3118 assert_string_equal(actions->groupings->name, "grouping");
3119 assert_string_equal(actions->input.data->name, "uses-name");
3120 assert_string_equal(actions->output.musts->arg, "cond");
3121 assert_null(actions->exts);
3122 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3123 actions = NULL;
3124
David Sedlákeaa45792019-07-24 15:25:01 +02003125 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3126 data = ELEMENT_WRAPPER_START
3127 "<rpc name=\"act\">"
3128 "<description><text>desc</text></description>"
3129 "<grouping name=\"grouping\"/>"
3130 "<if-feature name=\"iff\"/>"
3131 "<input><uses name=\"uses-name\"/></input>"
3132 "<output><must condition=\"cond\"/></output>"
3133 "<reference><text>ref</text></reference>"
3134 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003135 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákeaa45792019-07-24 15:25:01 +02003136 "</rpc>"
3137 ELEMENT_WRAPPER_END;
3138 assert_int_equal(test_element_helper(st, &data, &act_meta, NULL, NULL, true), LY_SUCCESS);
3139 assert_null(actions->parent);
3140 assert_int_equal(actions->nodetype, LYS_ACTION);
3141 assert_true(actions->flags & LYS_STATUS_DEPRC);
3142 assert_string_equal(actions->name, "act");
3143 assert_string_equal(actions->dsc, "desc");
3144 assert_string_equal(actions->ref, "ref");
3145 assert_string_equal(*actions->iffeatures, "iff");
3146 assert_string_equal(actions->typedefs->name, "tpdf");
3147 assert_string_equal(actions->groupings->name, "grouping");
3148 assert_string_equal(actions->input.data->name, "uses-name");
3149 assert_string_equal(actions->output.musts->arg, "cond");
3150 assert_null(actions->exts);
3151 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3152 actions = NULL;
3153
David Sedlák85d0eca2019-07-24 15:15:21 +02003154 /* min subelems */
3155 data = ELEMENT_WRAPPER_START "<action name=\"act\" />" ELEMENT_WRAPPER_END;
3156 assert_int_equal(test_element_helper(st, &data, &act_meta, NULL, NULL, true), LY_SUCCESS);
3157 assert_string_equal(actions->name, "act");
3158 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3159 actions = NULL;
3160
3161 st->finished_correctly = true;
3162}
3163
David Sedlák992fb7c2019-07-24 16:51:01 +02003164static void
3165test_augment_elem(void **state)
3166{
3167 struct state *st = *state;
3168 const char *data;
3169 struct lysp_augment *augments = NULL;
3170 struct augment_meta aug_meta = {NULL, &augments};
3171
3172 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3173 data = ELEMENT_WRAPPER_START
3174 "<augment target-node=\"target\">"
3175 "<action name=\"action\"/>"
3176 "<anydata name=\"anyd\"/>"
3177 "<anyxml name=\"anyx\"/>"
3178 "<case name=\"case\"/>"
3179 "<choice name=\"choice\"/>"
3180 "<container name=\"subcont\"/>"
3181 "<description><text>desc</text></description>"
3182 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003183 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3184 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák992fb7c2019-07-24 16:51:01 +02003185 "<list name=\"list\"/>"
3186 "<notification name=\"notif\"/>"
3187 "<reference><text>ref</text></reference>"
3188 "<status value=\"current\"/>"
3189 "<uses name=\"uses\"/>"
3190 "<when condition=\"when-cond\"/>"
3191 "</augment>"
3192 ELEMENT_WRAPPER_END;
3193 assert_int_equal(test_element_helper(st, &data, &aug_meta, NULL, NULL, true), LY_SUCCESS);
3194 assert_string_equal(augments->nodeid, "target");
3195 assert_null(augments->parent);
3196 assert_int_equal(augments->nodetype, LYS_AUGMENT);
3197 assert_true(augments->flags & LYS_STATUS_CURR);
3198 assert_string_equal(augments->dsc, "desc");
3199 assert_string_equal(augments->ref, "ref");
3200 assert_string_equal(augments->when->cond, "when-cond");
3201 assert_string_equal(*augments->iffeatures, "iff");
3202 assert_string_equal(augments->child->name, "anyd");
3203 assert_int_equal(augments->child->nodetype, LYS_ANYDATA);
3204 assert_string_equal(augments->child->next->name, "anyx");
3205 assert_int_equal(augments->child->next->nodetype, LYS_ANYXML);
3206 assert_string_equal(augments->child->next->next->name, "case");
3207 assert_int_equal(augments->child->next->next->nodetype, LYS_CASE);
3208 assert_string_equal(augments->child->next->next->next->name, "choice");
3209 assert_int_equal(augments->child->next->next->next->nodetype, LYS_CHOICE);
3210 assert_string_equal(augments->child->next->next->next->next->name, "subcont");
3211 assert_int_equal(augments->child->next->next->next->next->nodetype, LYS_CONTAINER);
3212 assert_string_equal(augments->child->next->next->next->next->next->name, "leaf");
3213 assert_int_equal(augments->child->next->next->next->next->next->nodetype, LYS_LEAF);
3214 assert_string_equal(augments->child->next->next->next->next->next->next->name, "llist");
3215 assert_int_equal(augments->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3216 assert_string_equal(augments->child->next->next->next->next->next->next->next->name, "list");
3217 assert_int_equal(augments->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
3218 assert_string_equal(augments->child->next->next->next->next->next->next->next->next->name, "uses");
3219 assert_int_equal(augments->child->next->next->next->next->next->next->next->next->nodetype, LYS_USES);
3220 assert_null(augments->child->next->next->next->next->next->next->next->next->next);
3221 assert_string_equal(augments->actions->name, "action");
3222 assert_string_equal(augments->notifs->name, "notif");
3223 assert_null(augments->exts);
3224 FREE_ARRAY(st->ctx, augments, lysp_augment_free)
3225 augments = NULL;
3226
3227 data = ELEMENT_WRAPPER_START "<augment target-node=\"target\" />" ELEMENT_WRAPPER_END;
3228 assert_int_equal(test_element_helper(st, &data, &aug_meta, NULL, NULL, true), LY_SUCCESS);
3229 assert_string_equal(augments->nodeid, "target");
3230 FREE_ARRAY(st->ctx, augments, lysp_augment_free)
3231 augments = NULL;
3232
3233 st->finished_correctly = true;
3234}
3235
David Sedlák4ffcec82019-07-25 15:10:21 +02003236static void
3237test_deviate_elem(void **state)
3238{
3239 struct state *st = *state;
3240 const char *data;
3241 struct lysp_deviate *deviates = NULL;
3242 struct lysp_deviate_add *d_add;
3243 struct lysp_deviate_rpl *d_rpl;
3244 struct lysp_deviate_del *d_del;
3245
3246 /* all valid arguments with min subelems */
3247 data = ELEMENT_WRAPPER_START "<deviate value=\"not-supported\" />" ELEMENT_WRAPPER_END;
3248 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3249 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
3250 lysp_deviate_free(st->ctx, deviates);
3251 free(deviates);
3252 deviates = NULL;
3253
3254 data = ELEMENT_WRAPPER_START "<deviate value=\"add\" />" ELEMENT_WRAPPER_END;
3255 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3256 assert_int_equal(deviates->mod, LYS_DEV_ADD);
3257 lysp_deviate_free(st->ctx, deviates);
3258 free(deviates);
3259 deviates = NULL;
3260
3261 data = ELEMENT_WRAPPER_START "<deviate value=\"replace\" />" ELEMENT_WRAPPER_END;
3262 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3263 assert_int_equal(deviates->mod, LYS_DEV_REPLACE);
3264 lysp_deviate_free(st->ctx, deviates);
3265 free(deviates);
3266 deviates = NULL;
3267
3268 data = ELEMENT_WRAPPER_START "<deviate value=\"delete\" />" ELEMENT_WRAPPER_END;
3269 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3270 assert_int_equal(deviates->mod, LYS_DEV_DELETE);
3271 lysp_deviate_free(st->ctx, deviates);
3272 free(deviates);
3273 deviates = NULL;
3274
3275 /* max subelems and valid arguments */
3276 data = ELEMENT_WRAPPER_START
3277 "<deviate value=\"not-supported\">"
3278 "</deviate>"
3279 ELEMENT_WRAPPER_END;
3280 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3281 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
3282 lysp_deviate_free(st->ctx, deviates);
3283 free(deviates);
3284 deviates = NULL;
3285
3286 data = ELEMENT_WRAPPER_START
3287 "<deviate value=\"add\">"
3288 "<units name=\"units\"/>"
3289 "<must condition=\"cond\"/>"
3290 "<unique tag=\"utag\"/>"
3291 "<default value=\"def\"/>"
3292 "<config value=\"true\"/>"
3293 "<mandatory value=\"true\"/>"
3294 "<min-elements value=\"5\"/>"
3295 "<max-elements value=\"15\"/>"
3296 "</deviate>"
3297 ELEMENT_WRAPPER_END;
3298 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3299 d_add = (struct lysp_deviate_add *)deviates;
3300 assert_int_equal(d_add->mod, LYS_DEV_ADD);
3301 assert_null(d_add->next);
3302 assert_null(d_add->exts);
3303 assert_string_equal(d_add->units, "units");
3304 assert_string_equal(d_add->musts->arg, "cond");
3305 assert_string_equal(*d_add->uniques, "utag");
3306 assert_string_equal(*d_add->dflts, "def");
3307 assert_true(d_add->flags & LYS_MAND_TRUE && d_add->flags & LYS_CONFIG_W);
3308 assert_int_equal(d_add->min, 5);
3309 assert_int_equal(d_add->max, 15);
3310 lysp_deviate_free(st->ctx, deviates);
3311 free(deviates);
3312 deviates = NULL;
3313
3314 data = ELEMENT_WRAPPER_START
3315 "<deviate value=\"replace\">"
3316 "<type name=\"newtype\"/>"
3317 "<units name=\"uni\"/>"
3318 "<default value=\"def\"/>"
3319 "<config value=\"true\"/>"
3320 "<mandatory value=\"true\"/>"
3321 "<min-elements value=\"5\"/>"
3322 "<max-elements value=\"15\"/>"
3323 "</deviate>"
3324 ELEMENT_WRAPPER_END;
3325 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3326 d_rpl = (struct lysp_deviate_rpl *)deviates;
3327 assert_int_equal(d_rpl->mod, LYS_DEV_REPLACE);
3328 assert_null(d_rpl->next);
3329 assert_null(d_rpl->exts);
3330 assert_string_equal(d_rpl->type->name, "newtype");
3331 assert_string_equal(d_rpl->units, "uni");
3332 assert_string_equal(d_rpl->dflt, "def");
3333 assert_true(d_rpl->flags & LYS_MAND_TRUE && d_rpl->flags & LYS_CONFIG_W);
3334 assert_int_equal(d_rpl->min, 5);
3335 assert_int_equal(d_rpl->max, 15);
3336 lysp_deviate_free(st->ctx, deviates);
3337 free(deviates);
3338 deviates = NULL;
3339
3340 data = ELEMENT_WRAPPER_START
3341 "<deviate value=\"delete\">"
3342 "<units name=\"u\"/>"
3343 "<must condition=\"c\"/>"
3344 "<unique tag=\"tag\"/>"
3345 "<default value=\"default\"/>"
3346 "</deviate>"
3347 ELEMENT_WRAPPER_END;
3348 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3349 d_del = (struct lysp_deviate_del *)deviates;
3350 assert_int_equal(d_del->mod, LYS_DEV_DELETE);
3351 assert_null(d_del->next);
3352 assert_null(d_del->exts);
3353 assert_string_equal(d_del->units, "u");
3354 assert_string_equal(d_del->musts->arg, "c");
3355 assert_string_equal(*d_del->uniques, "tag");
3356 assert_string_equal(*d_del->dflts, "default");
3357 lysp_deviate_free(st->ctx, deviates);
3358 free(deviates);
3359 deviates = NULL;
3360
3361 /* invalid arguments */
3362 data = ELEMENT_WRAPPER_START "<deviate value=\"\" />" ELEMENT_WRAPPER_END;
3363 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
3364 logbuf_assert("Invalid value \"\" of \"deviate\". Line number 1.");
3365 deviates = NULL;
3366
3367 data = ELEMENT_WRAPPER_START "<deviate value=\"invalid\" />" ELEMENT_WRAPPER_END;
3368 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
3369 logbuf_assert("Invalid value \"invalid\" of \"deviate\". Line number 1.");
3370 deviates = NULL;
3371
3372 data = ELEMENT_WRAPPER_START "<deviate value=\"ad\" />" ELEMENT_WRAPPER_END;
3373 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
3374 logbuf_assert("Invalid value \"ad\" of \"deviate\". Line number 1.");
3375 deviates = NULL;
3376
3377 data = ELEMENT_WRAPPER_START "<deviate value=\"adds\" />" ELEMENT_WRAPPER_END;
3378 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
3379 logbuf_assert("Invalid value \"adds\" of \"deviate\". Line number 1.");
3380 deviates = NULL;
3381
3382 data = ELEMENT_WRAPPER_START
3383 "<deviate value=\"not-supported\">"
3384 "<must condition=\"c\"/>"
3385 "</deviate>"
3386 ELEMENT_WRAPPER_END;
3387 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
3388 logbuf_assert("Deviate of this type doesn't allow \"must\" as it's sub-element. Line number 1.");
3389
3390 st->finished_correctly = true;
3391}
3392
David Sedlák8b754462019-07-25 16:22:13 +02003393static void
3394test_deviation_elem(void **state)
3395{
3396 struct state *st = *state;
3397 const char *data;
3398 struct lysp_deviation *deviations = NULL;
3399
3400 /* min subelems */
3401 data = ELEMENT_WRAPPER_START
3402 "<deviation target-node=\"target\">"
3403 "<deviate value=\"not-supported\"/>"
3404 "</deviation>"
3405 ELEMENT_WRAPPER_END;
3406 assert_int_equal(test_element_helper(st, &data, &deviations, NULL, NULL, true), LY_SUCCESS);
3407 assert_string_equal(deviations->nodeid, "target");
3408 assert_int_equal(deviations->deviates->mod, LYS_DEV_NOT_SUPPORTED);
3409 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3410 deviations = NULL;
3411
3412 /* max subelems */
3413 data = ELEMENT_WRAPPER_START
3414 "<deviation target-node=\"target\">"
3415 "<reference><text>ref</text></reference>"
3416 "<description><text>desc</text></description>"
3417 "<deviate value=\"add\"/>"
3418 "</deviation>"
3419 ELEMENT_WRAPPER_END;
3420 assert_int_equal(test_element_helper(st, &data, &deviations, NULL, NULL, true), LY_SUCCESS);
3421 assert_string_equal(deviations->nodeid, "target");
3422 assert_int_equal(deviations->deviates->mod, LYS_DEV_ADD);
3423 assert_string_equal(deviations->ref, "ref");
3424 assert_string_equal(deviations->dsc, "desc");
3425 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3426 deviations = NULL;
3427
3428 /* invalid */
3429 data = ELEMENT_WRAPPER_START "<deviation target-node=\"target\"/>" ELEMENT_WRAPPER_END;
3430 assert_int_equal(test_element_helper(st, &data, &deviations, NULL, NULL, false), LY_EVALID);
3431 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3432 deviations = NULL;
3433 logbuf_assert("Missing mandatory subelement deviate of deviate element. Line number 1.");
3434
3435 st->finished_correctly = true;
3436}
3437
David Sedlák4f03b932019-07-26 13:01:47 +02003438static void
3439test_module_elem(void **state)
3440{
3441 struct state *st = *state;
3442 const char *data;
3443 struct yin_arg_record *attrs = NULL;
3444 struct sized_string name, prefix;
3445 struct lys_module *lys_mod = NULL;
3446 struct lysp_module *lysp_mod = NULL;
3447
3448 /* max subelems */
3449 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
3450 lys_mod = calloc(1, sizeof *lys_mod);
3451 lysp_mod = calloc(1, sizeof *lysp_mod);
3452 lys_mod->ctx = st->ctx;
3453 lysp_mod->mod = lys_mod;
3454 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
3455 "<yang-version value=\"1.1\"/>\n"
3456 "<namespace uri=\"ns\"/>\n"
3457 "<prefix value=\"pref\"/>\n"
3458 "<include module=\"b-mod\"/>\n"
3459 "<import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
3460 "<organization><text>org</text></organization>\n"
3461 "<contact><text>contact</text></contact>\n"
3462 "<description><text>desc</text></description>"
3463 "<reference><text>ref</text></reference>\n"
3464 "<revision date=\"2019-02-02\"/>\n"
3465 "<anydata name=\"anyd\"/>\n"
3466 "<anyxml name=\"anyx\"/>\n"
3467 "<choice name=\"choice\"/>\n"
3468 "<container name=\"cont\"/>\n"
3469 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3470 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3471 "<list name=\"sub-list\"/>\n"
3472 "<uses name=\"uses-name\"/>\n"
3473 "<augment target-node=\"target\"/>\n"
3474 "<deviation target-node=\"target\">""<deviate value=\"not-supported\"/>""</deviation>\n"
3475 "<extension name=\"ext\"/>\n"
3476 "<feature name=\"feature\"/>\n"
3477 "<grouping name=\"grp\"/>\n"
3478 "<identity name=\"ident-name\"/>\n"
3479 "<notification name=\"notf\"/>\n"
3480 "<rpc name=\"rpc-name\"/>\n"
3481 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3482 "</module>\n";
3483 assert_int_equal(lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len), LY_SUCCESS);
3484 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
3485 assert_int_equal(yin_parse_mod(st->yin_ctx, attrs, &data, lysp_mod), LY_SUCCESS);
3486 assert_string_equal(lysp_mod->mod->name, "mod");
3487 assert_string_equal(lysp_mod->revs, "2019-02-02");
3488 assert_string_equal(lysp_mod->mod->ns, "ns");
3489 assert_string_equal(lysp_mod->mod->prefix, "pref");
3490 assert_null(lysp_mod->mod->filepath);
3491 assert_string_equal(lysp_mod->mod->org, "org");
3492 assert_string_equal(lysp_mod->mod->contact, "contact");
3493 assert_string_equal(lysp_mod->mod->dsc, "desc");
3494 assert_string_equal(lysp_mod->mod->ref, "ref");
3495 assert_int_equal(lysp_mod->mod->version, LYS_VERSION_1_1);
3496 assert_string_equal(lysp_mod->imports->name, "a-mod");
3497 assert_string_equal(lysp_mod->includes->name, "b-mod");
3498 assert_string_equal(lysp_mod->extensions->name, "ext");
3499 assert_string_equal(lysp_mod->features->name, "feature");
3500 assert_string_equal(lysp_mod->identities->name, "ident-name");
3501 assert_string_equal(lysp_mod->typedefs->name, "tpdf");
3502 assert_string_equal(lysp_mod->groupings->name, "grp");
3503 assert_string_equal(lysp_mod->data->name, "anyd");
3504 assert_int_equal(lysp_mod->data->nodetype, LYS_ANYDATA);
3505 assert_string_equal(lysp_mod->data->next->name, "anyx");
3506 assert_int_equal(lysp_mod->data->next->nodetype, LYS_ANYXML);
3507 assert_string_equal(lysp_mod->data->next->next->name, "choice");
3508 assert_int_equal(lysp_mod->data->next->next->nodetype, LYS_CHOICE);
3509 assert_string_equal(lysp_mod->data->next->next->next->name, "cont");
3510 assert_int_equal(lysp_mod->data->next->next->next->nodetype, LYS_CONTAINER);
3511 assert_string_equal(lysp_mod->data->next->next->next->next->name, "leaf");
3512 assert_int_equal(lysp_mod->data->next->next->next->next->nodetype, LYS_LEAF);
3513 assert_string_equal(lysp_mod->data->next->next->next->next->next->name, "llist");
3514 assert_int_equal(lysp_mod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3515 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->name, "sub-list");
3516 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
3517 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->next->name, "uses-name");
3518 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3519 assert_null(lysp_mod->data->next->next->next->next->next->next->next->next);
3520 assert_string_equal(lysp_mod->augments->nodeid, "target");
3521 assert_string_equal(lysp_mod->rpcs->name, "rpc-name");
3522 assert_string_equal(lysp_mod->notifs->name, "notf");
3523 assert_string_equal(lysp_mod->deviations->nodeid, "target");
3524 assert_null(lysp_mod->exts);
3525 lysp_module_free(lysp_mod);
3526 lys_module_free(lys_mod, NULL);
3527 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
3528 attrs = NULL;
3529
3530 /* min subelems */
3531 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
3532 lys_mod = calloc(1, sizeof *lys_mod);
3533 lysp_mod = calloc(1, sizeof *lysp_mod);
3534 lys_mod->ctx = st->ctx;
3535 lysp_mod->mod = lys_mod;
3536 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">"
3537 "<namespace uri=\"ns\"/>"
3538 "<prefix value=\"pref\"/>"
3539 "<yang-version value=\"1.1\"/>"
3540 "</module>";
3541 assert_int_equal(lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len), LY_SUCCESS);
3542 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
3543 assert_int_equal(yin_parse_mod(st->yin_ctx, attrs, &data, lysp_mod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003544 assert_string_equal(lysp_mod->mod->name, "mod");
David Sedlák4f03b932019-07-26 13:01:47 +02003545 lysp_module_free(lysp_mod);
3546 lys_module_free(lys_mod, NULL);
3547 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
3548 attrs = NULL;
David Sedlák298ff6d2019-07-26 14:29:03 +02003549
3550 st->finished_correctly = true;
3551}
3552
3553static void
3554test_submodule_elem(void **state)
3555{
3556 struct state *st = *state;
3557 const char *data;
3558 struct yin_arg_record *attrs = NULL;
3559 struct sized_string name, prefix;
3560 struct lysp_submodule *lysp_submod = NULL;
3561
3562 /* max subelements */
3563 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
3564 lysp_submod = calloc(1, sizeof *lysp_submod);
3565 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
3566 "<yang-version value=\"1.1\"/>\n"
3567 "<belongs-to module=\"mod-name\"><prefix value=\"pref\"/></belongs-to>"
3568 "<include module=\"b-mod\"/>\n"
3569 "<import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
3570 "<organization><text>org</text></organization>\n"
3571 "<contact><text>contact</text></contact>\n"
3572 "<description><text>desc</text></description>"
3573 "<reference><text>ref</text></reference>\n"
3574 "<revision date=\"2019-02-02\"/>\n"
3575 "<anydata name=\"anyd\"/>\n"
3576 "<anyxml name=\"anyx\"/>\n"
3577 "<choice name=\"choice\"/>\n"
3578 "<container name=\"cont\"/>\n"
3579 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3580 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3581 "<list name=\"sub-list\"/>\n"
3582 "<uses name=\"uses-name\"/>\n"
3583 "<augment target-node=\"target\"/>\n"
3584 "<deviation target-node=\"target\">""<deviate value=\"not-supported\"/>""</deviation>\n"
3585 "<extension name=\"ext\"/>\n"
3586 "<feature name=\"feature\"/>\n"
3587 "<grouping name=\"grp\"/>\n"
3588 "<identity name=\"ident-name\"/>\n"
3589 "<notification name=\"notf\"/>\n"
3590 "<rpc name=\"rpc-name\"/>\n"
3591 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3592 "</submodule>\n";
3593 assert_int_equal(lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len), LY_SUCCESS);
3594 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
3595 assert_int_equal(yin_parse_submod(st->yin_ctx, attrs, &data, lysp_submod), LY_SUCCESS);
3596
3597 assert_string_equal(lysp_submod->name, "mod");
3598 assert_string_equal(lysp_submod->revs, "2019-02-02");
David Sedlák298ff6d2019-07-26 14:29:03 +02003599 assert_string_equal(lysp_submod->prefix, "pref");
3600 assert_null(lysp_submod->filepath);
3601 assert_string_equal(lysp_submod->org, "org");
3602 assert_string_equal(lysp_submod->contact, "contact");
3603 assert_string_equal(lysp_submod->dsc, "desc");
3604 assert_string_equal(lysp_submod->ref, "ref");
3605 assert_int_equal(lysp_submod->version, LYS_VERSION_1_1);
3606 assert_string_equal(lysp_submod->imports->name, "a-mod");
3607 assert_string_equal(lysp_submod->includes->name, "b-mod");
3608 assert_string_equal(lysp_submod->extensions->name, "ext");
3609 assert_string_equal(lysp_submod->features->name, "feature");
3610 assert_string_equal(lysp_submod->identities->name, "ident-name");
3611 assert_string_equal(lysp_submod->typedefs->name, "tpdf");
3612 assert_string_equal(lysp_submod->groupings->name, "grp");
3613 assert_string_equal(lysp_submod->data->name, "anyd");
3614 assert_int_equal(lysp_submod->data->nodetype, LYS_ANYDATA);
3615 assert_string_equal(lysp_submod->data->next->name, "anyx");
3616 assert_int_equal(lysp_submod->data->next->nodetype, LYS_ANYXML);
3617 assert_string_equal(lysp_submod->data->next->next->name, "choice");
3618 assert_int_equal(lysp_submod->data->next->next->nodetype, LYS_CHOICE);
3619 assert_string_equal(lysp_submod->data->next->next->next->name, "cont");
3620 assert_int_equal(lysp_submod->data->next->next->next->nodetype, LYS_CONTAINER);
3621 assert_string_equal(lysp_submod->data->next->next->next->next->name, "leaf");
3622 assert_int_equal(lysp_submod->data->next->next->next->next->nodetype, LYS_LEAF);
3623 assert_string_equal(lysp_submod->data->next->next->next->next->next->name, "llist");
3624 assert_int_equal(lysp_submod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3625 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->name, "sub-list");
3626 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
3627 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->next->name, "uses-name");
3628 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3629 assert_null(lysp_submod->data->next->next->next->next->next->next->next->next);
3630 assert_string_equal(lysp_submod->augments->nodeid, "target");
3631 assert_string_equal(lysp_submod->rpcs->name, "rpc-name");
3632 assert_string_equal(lysp_submod->notifs->name, "notf");
3633 assert_string_equal(lysp_submod->deviations->nodeid, "target");
3634 assert_null(lysp_submod->exts);
3635
3636 lysp_submodule_free(st->ctx, lysp_submod);
3637 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
3638 attrs = NULL;
3639
3640 /* min subelemnts */
3641 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
3642 lysp_submod = calloc(1, sizeof *lysp_submod);
3643 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">"
3644 "<yang-version value=\"1.0\"/>"
3645 "<belongs-to module=\"mod-name\"><prefix value=\"pref\"/></belongs-to>"
3646 "</submodule>";
3647 assert_int_equal(lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len), LY_SUCCESS);
3648 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
3649 assert_int_equal(yin_parse_submod(st->yin_ctx, attrs, &data, lysp_submod), LY_SUCCESS);
3650 assert_string_equal(lysp_submod->prefix, "pref");
3651 assert_string_equal(lysp_submod->belongsto, "mod-name");
3652 assert_int_equal(lysp_submod->version, LYS_VERSION_1_0);
3653 lysp_submodule_free(st->ctx, lysp_submod);
3654 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
3655 attrs = NULL;
3656
3657 st->finished_correctly = true;
David Sedlák4f03b932019-07-26 13:01:47 +02003658}
3659
David Sedlák8985a142019-07-31 16:43:06 +02003660static void
3661test_yin_parse_module(void **state)
3662{
3663 struct state *st = *state;
3664 const char *data;
3665 struct lys_module *mod;
3666 struct yin_parser_ctx *yin_ctx = NULL;
3667
3668 mod = calloc(1, sizeof *mod);
3669 mod->ctx = st->ctx;
3670 data = "<module name=\"example-foo\""
3671 "xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
3672 "xmlns:foo=\"urn:example:foo\""
3673 "xmlns:myext=\"urn:example:extensions\">\n"
3674
3675 "<yang-version value=\"1.0\"/>\n"
3676
3677 "<namespace uri=\"urn:example:foo\"/>\n"
3678 "<prefix value=\"foo\"/>\n"
3679
3680 "<import module=\"example-extensions\">\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003681 "<prefix value=\"myext\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003682 "</import>\n"
3683
3684 "<list name=\"interface\">\n"
3685 "<key value=\"name\"/>\n"
3686 "<leaf name=\"name\">\n"
3687 "<type name=\"string\"/>\n"
3688 "</leaf>\n"
3689 "<leaf name=\"mtu\">\n"
3690 "<type name=\"uint32\"/>\n"
3691 "<description>\n"
3692 "<text>The MTU of the interface.</text>\n"
3693 "</description>\n"
3694 "<myext:c-define name=\"MY_MTU\"/>\n"
3695 "</leaf>\n"
3696 "</list>\n"
3697 "</module>\n";
3698 assert_int_equal(yin_parse_module(&yin_ctx, data, mod), LY_SUCCESS);
3699 lys_module_free(mod, NULL);
3700 yin_parser_ctx_free(yin_ctx);
3701 mod = NULL;
3702 yin_ctx = NULL;
3703
3704 mod = calloc(1, sizeof *mod);
3705 mod->ctx = st->ctx;
David Sedlák6d781b62019-08-02 15:22:52 +02003706 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3707 "<yang-version value=\"1.0\"/>\n"
3708 "<namespace uri=\"urn:example:foo\"/>\n"
3709 "<prefix value=\"foo\"/>\n"
3710 "</module>\n";
3711 assert_int_equal(yin_parse_module(&yin_ctx, data, mod), LY_SUCCESS);
3712 lys_module_free(mod, NULL);
3713 yin_parser_ctx_free(yin_ctx);
3714 mod = NULL;
3715 yin_ctx = NULL;
3716
3717
3718 mod = calloc(1, sizeof *mod);
3719 mod->ctx = st->ctx;
David Sedlák8985a142019-07-31 16:43:06 +02003720 data = "<submodule name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
3721 "</submodule>\n";
3722 assert_int_equal(yin_parse_module(&yin_ctx, data, mod), LY_EINVAL);
3723 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
3724 lys_module_free(mod, NULL);
3725 yin_parser_ctx_free(yin_ctx);
3726
David Sedlák6d781b62019-08-02 15:22:52 +02003727 mod = calloc(1, sizeof *mod);
3728 mod->ctx = st->ctx;
3729 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3730 "<yang-version value=\"1.0\"/>\n"
3731 "<namespace uri=\"urn:example:foo\"/>\n"
3732 "<prefix value=\"foo\"/>\n"
3733 "</module>"
3734 "<module>";
3735 assert_int_equal(yin_parse_module(&yin_ctx, data, mod), LY_EVALID);
3736 logbuf_assert("Trailing garbage \"<module>\" after module, expected end-of-input. Line number 5.");
3737 lys_module_free(mod, NULL);
3738 yin_parser_ctx_free(yin_ctx);
3739 mod = NULL;
3740 yin_ctx = NULL;
3741
David Sedlák8985a142019-07-31 16:43:06 +02003742 st->finished_correctly = true;
3743}
3744
3745static void
3746test_yin_parse_submodule(void **state)
3747{
3748 struct state *st = *state;
3749 const char *data;
3750 struct yin_parser_ctx *yin_ctx = NULL;
3751 struct lysp_submodule *submod = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02003752 struct lys_parser_ctx main_ctx = {};
David Sedlák8985a142019-07-31 16:43:06 +02003753
3754 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
3755 "<submodule name=\"asub\""
3756 "xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
3757 "xmlns:a=\"urn:a\">"
3758 "<yang-version value=\"1.0\"/>\n"
3759 "<belongs-to module=\"a\">"
3760 "<prefix value=\"a_pref\"/>"
3761 "</belongs-to>"
3762 "<include module=\"atop\"/>"
3763 "<feature name=\"fox\"/>"
3764 "<notification name=\"bar-notif\">"
3765 "<if-feature name=\"bar\"/>"
3766 "</notification>"
3767 "<notification name=\"fox-notif\">"
3768 "<if-feature name=\"fox\"/>"
3769 "</notification>"
3770 "<augment target-node=\"/a_pref:top\">"
3771 "<if-feature name=\"bar\"/>"
3772 "<container name=\"bar-sub\"/>"
3773 "</augment>"
3774 "<augment target-node=\"/top\">"
3775 "<container name=\"bar-sub2\"/>"
3776 "</augment>"
3777 "</submodule>";
David Sedlák1b623122019-08-05 15:27:49 +02003778 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, &main_ctx, data, &submod), LY_SUCCESS);
David Sedlák8985a142019-07-31 16:43:06 +02003779 lysp_submodule_free(st->ctx, submod);
3780 yin_parser_ctx_free(yin_ctx);
3781 yin_ctx = NULL;
3782 submod = NULL;
3783
3784 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
David Sedlák6d781b62019-08-02 15:22:52 +02003785 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
3786 "<yang-version value=\"1.0\"/>\n"
3787 "<belongs-to module=\"a\">"
3788 "<prefix value=\"a_pref\"/>"
3789 "</belongs-to>"
3790 "</submodule>";
David Sedlák1b623122019-08-05 15:27:49 +02003791 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, &main_ctx, data, &submod), LY_SUCCESS);
David Sedlák6d781b62019-08-02 15:22:52 +02003792 lysp_submodule_free(st->ctx, submod);
3793 yin_parser_ctx_free(yin_ctx);
3794 yin_ctx = NULL;
3795 submod = NULL;
3796
3797 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
David Sedlák8985a142019-07-31 16:43:06 +02003798 "<module name=\"inval\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
3799 "</module>";
David Sedlák1b623122019-08-05 15:27:49 +02003800 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, &main_ctx, data, &submod), LY_EINVAL);
David Sedlák8985a142019-07-31 16:43:06 +02003801 logbuf_assert("Input data contains module in situation when a submodule is expected.");
3802 lysp_submodule_free(st->ctx, submod);
3803 yin_parser_ctx_free(yin_ctx);
3804 yin_ctx = NULL;
3805 submod = NULL;
3806
David Sedlák6d781b62019-08-02 15:22:52 +02003807 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
3808 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
3809 "<yang-version value=\"1.0\"/>\n"
3810 "<belongs-to module=\"a\">"
3811 "<prefix value=\"a_pref\"/>"
3812 "</belongs-to>"
3813 "</submodule>"
3814 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
3815 "<yang-version value=\"1.0\"/>\n"
3816 "<belongs-to module=\"a\">"
3817 "<prefix value=\"a_pref\"/>"
3818 "</belongs-to>"
3819 "</submodule>";
David Sedlák1b623122019-08-05 15:27:49 +02003820 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, &main_ctx, data, &submod), LY_EVALID);
David Sedlák6d781b62019-08-02 15:22:52 +02003821 logbuf_assert("Trailing garbage \"<submodule name...\" after submodule, expected end-of-input. Line number 2.");
3822 lysp_submodule_free(st->ctx, submod);
3823 yin_parser_ctx_free(yin_ctx);
3824 yin_ctx = NULL;
3825 submod = NULL;
3826
David Sedlák8985a142019-07-31 16:43:06 +02003827 st->finished_correctly = true;
3828}
3829
David Sedlák3b4db242018-10-19 16:11:01 +02003830int
3831main(void)
3832{
3833
3834 const struct CMUnitTest tests[] = {
David Sedlák8f7a1172019-06-20 14:42:18 +02003835 cmocka_unit_test_setup_teardown(test_yin_match_keyword, setup_f, teardown_f),
David Sedlákb1a78352019-06-28 16:16:29 +02003836 cmocka_unit_test_setup_teardown(test_yin_parse_element_generic, setup_f, teardown_f),
3837 cmocka_unit_test_setup_teardown(test_yin_parse_extension_instance, setup_f, teardown_f),
David Sedlák555c7202019-07-04 12:14:12 +02003838 cmocka_unit_test_setup_teardown(test_yin_parse_content, setup_f, teardown_f),
David Sedlák4a650532019-07-10 11:55:18 +02003839 cmocka_unit_test_setup_teardown(test_validate_value, setup_f, teardown_f),
David Sedlák32488102019-07-15 17:44:10 +02003840
David Sedlák8e7bda82019-07-16 17:57:50 +02003841 cmocka_unit_test(test_yin_match_argument_name),
David Sedlák43801c92019-08-05 15:58:54 +02003842 cmocka_unit_test_setup_teardown(test_enum_elem, setup_element_test, teardown_element_test),
3843 cmocka_unit_test_setup_teardown(test_bit_elem, setup_element_test, teardown_element_test),
David Sedlák32488102019-07-15 17:44:10 +02003844 cmocka_unit_test_setup_teardown(test_meta_elem, setup_element_test, teardown_element_test),
3845 cmocka_unit_test_setup_teardown(test_import_elem, setup_element_test, teardown_element_test),
3846 cmocka_unit_test_setup_teardown(test_status_elem, setup_element_test, teardown_element_test),
3847 cmocka_unit_test_setup_teardown(test_ext_elem, setup_element_test, teardown_element_test),
3848 cmocka_unit_test_setup_teardown(test_yin_element_elem, setup_element_test, teardown_element_test),
3849 cmocka_unit_test_setup_teardown(test_yangversion_elem, setup_element_test, teardown_element_test),
3850 cmocka_unit_test_setup_teardown(test_mandatory_elem, setup_element_test, teardown_element_test),
David Sedlák8e7bda82019-07-16 17:57:50 +02003851 cmocka_unit_test_setup_teardown(test_argument_elem, setup_element_test, teardown_element_test),
3852 cmocka_unit_test_setup_teardown(test_base_elem, setup_element_test, teardown_element_test),
3853 cmocka_unit_test_setup_teardown(test_belongsto_elem, setup_element_test, teardown_element_test),
3854 cmocka_unit_test_setup_teardown(test_config_elem, setup_element_test, teardown_element_test),
3855 cmocka_unit_test_setup_teardown(test_default_elem, setup_element_test, teardown_element_test),
3856 cmocka_unit_test_setup_teardown(test_err_app_tag_elem, setup_element_test, teardown_element_test),
3857 cmocka_unit_test_setup_teardown(test_err_msg_elem, setup_element_test, teardown_element_test),
3858 cmocka_unit_test_setup_teardown(test_fracdigits_elem, setup_element_test, teardown_element_test),
3859 cmocka_unit_test_setup_teardown(test_iffeature_elem, setup_element_test, teardown_element_test),
3860 cmocka_unit_test_setup_teardown(test_length_elem, setup_element_test, teardown_element_test),
3861 cmocka_unit_test_setup_teardown(test_modifier_elem, setup_element_test, teardown_element_test),
3862 cmocka_unit_test_setup_teardown(test_namespace_elem, setup_element_test, teardown_element_test),
3863 cmocka_unit_test_setup_teardown(test_path_elem, setup_element_test, teardown_element_test),
3864 cmocka_unit_test_setup_teardown(test_pattern_elem, setup_element_test, teardown_element_test),
3865 cmocka_unit_test_setup_teardown(test_value_position_elem, setup_element_test, teardown_element_test),
David Sedlák69f01612019-07-17 11:41:08 +02003866 cmocka_unit_test_setup_teardown(test_prefix_elem, setup_element_test, teardown_element_test),
3867 cmocka_unit_test_setup_teardown(test_range_elem, setup_element_test, teardown_element_test),
3868 cmocka_unit_test_setup_teardown(test_reqinstance_elem, setup_element_test, teardown_element_test),
3869 cmocka_unit_test_setup_teardown(test_revision_date_elem, setup_element_test, teardown_element_test),
3870 cmocka_unit_test_setup_teardown(test_unique_elem, setup_element_test, teardown_element_test),
3871 cmocka_unit_test_setup_teardown(test_units_elem, setup_element_test, teardown_element_test),
3872 cmocka_unit_test_setup_teardown(test_when_elem, setup_element_test, teardown_element_test),
3873 cmocka_unit_test_setup_teardown(test_yin_text_value_elem, setup_element_test, teardown_element_test),
David Sedlák374d2b32019-07-17 15:06:55 +02003874 cmocka_unit_test_setup_teardown(test_type_elem, setup_element_test, teardown_element_test),
David Sedlák1af868e2019-07-17 17:03:14 +02003875 cmocka_unit_test_setup_teardown(test_max_elems_elem, setup_element_test, teardown_element_test),
David Sedlák09e18c92019-07-18 11:17:11 +02003876 cmocka_unit_test_setup_teardown(test_min_elems_elem, setup_element_test, teardown_element_test),
David Sedláka2dad212019-07-18 12:45:19 +02003877 cmocka_unit_test_setup_teardown(test_ordby_elem, setup_element_test, teardown_element_test),
David Sedlák8a83bbb2019-07-18 14:46:00 +02003878 cmocka_unit_test_setup_teardown(test_any_elem, setup_element_test, teardown_element_test),
David Sedlák203ca3a2019-07-18 15:26:25 +02003879 cmocka_unit_test_setup_teardown(test_leaf_elem, setup_element_test, teardown_element_test),
David Sedlákc3da3ef2019-07-19 12:56:08 +02003880 cmocka_unit_test_setup_teardown(test_leaf_list_elem, setup_element_test, teardown_element_test),
David Sedlákcb39f642019-07-19 13:19:55 +02003881 cmocka_unit_test_setup_teardown(test_presence_elem, setup_element_test, teardown_element_test),
David Sedlák12470a82019-07-19 13:44:36 +02003882 cmocka_unit_test_setup_teardown(test_key_elem, setup_element_test, teardown_element_test),
David Sedlák04e17b22019-07-19 15:29:48 +02003883 cmocka_unit_test_setup_teardown(test_typedef_elem, setup_element_test, teardown_element_test),
David Sedlákd2d676a2019-07-22 11:28:19 +02003884 cmocka_unit_test_setup_teardown(test_refine_elem, setup_element_test, teardown_element_test),
David Sedlák0d6de5a2019-07-22 13:25:44 +02003885 cmocka_unit_test_setup_teardown(test_uses_elem, setup_element_test, teardown_element_test),
David Sedlákaa854b02019-07-22 14:17:10 +02003886 cmocka_unit_test_setup_teardown(test_revision_elem, setup_element_test, teardown_element_test),
David Sedlák0c2bab92019-07-22 15:33:19 +02003887 cmocka_unit_test_setup_teardown(test_include_elem, setup_element_test, teardown_element_test),
David Sedlák5e13dea2019-07-22 16:06:45 +02003888 cmocka_unit_test_setup_teardown(test_feature_elem, setup_element_test, teardown_element_test),
David Sedlák28794f22019-07-22 16:45:00 +02003889 cmocka_unit_test_setup_teardown(test_identity_elem, setup_element_test, teardown_element_test),
David Sedlákaf536aa2019-07-23 13:42:23 +02003890 cmocka_unit_test_setup_teardown(test_list_elem, setup_element_test, teardown_element_test),
David Sedlák031b9e72019-07-23 15:19:37 +02003891 cmocka_unit_test_setup_teardown(test_notification_elem, setup_element_test, teardown_element_test),
David Sedláke3ce9ef2019-07-23 16:34:30 +02003892 cmocka_unit_test_setup_teardown(test_grouping_elem, setup_element_test, teardown_element_test),
David Sedlákf111bcb2019-07-23 17:15:51 +02003893 cmocka_unit_test_setup_teardown(test_container_elem, setup_element_test, teardown_element_test),
David Sedlák5379d392019-07-24 10:42:03 +02003894 cmocka_unit_test_setup_teardown(test_case_elem, setup_element_test, teardown_element_test),
David Sedlákb7abcfa2019-07-24 12:33:35 +02003895 cmocka_unit_test_setup_teardown(test_choice_elem, setup_element_test, teardown_element_test),
David Sedlák05404f62019-07-24 14:11:53 +02003896 cmocka_unit_test_setup_teardown(test_inout_elem, setup_element_test, teardown_element_test),
David Sedlák85d0eca2019-07-24 15:15:21 +02003897 cmocka_unit_test_setup_teardown(test_action_elem, setup_element_test, teardown_element_test),
David Sedlák992fb7c2019-07-24 16:51:01 +02003898 cmocka_unit_test_setup_teardown(test_augment_elem, setup_element_test, teardown_element_test),
David Sedlák4ffcec82019-07-25 15:10:21 +02003899 cmocka_unit_test_setup_teardown(test_deviate_elem, setup_element_test, teardown_element_test),
David Sedlák8b754462019-07-25 16:22:13 +02003900 cmocka_unit_test_setup_teardown(test_deviation_elem, setup_element_test, teardown_element_test),
David Sedlák4f03b932019-07-26 13:01:47 +02003901 cmocka_unit_test_setup_teardown(test_module_elem, setup_element_test, teardown_element_test),
David Sedlák298ff6d2019-07-26 14:29:03 +02003902 cmocka_unit_test_setup_teardown(test_submodule_elem, setup_element_test, teardown_element_test),
David Sedlák8985a142019-07-31 16:43:06 +02003903
3904 cmocka_unit_test_setup_teardown(test_yin_parse_module, setup_logger, teardown_logger),
3905 cmocka_unit_test_setup_teardown(test_yin_parse_submodule, setup_logger, teardown_logger),
David Sedlák3b4db242018-10-19 16:11:01 +02003906 };
3907
David Sedlák8e7bda82019-07-16 17:57:50 +02003908 return cmocka_run_group_tests(tests, setup_ly_ctx, destroy_ly_ctx);
David Sedlák3b4db242018-10-19 16:11:01 +02003909}