blob: 1104994b9110e79cbfeb5aa92769ab51893fb1f4 [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áke6cd89e2019-08-07 12:46:02 +020052/* wrapping element used for mocking has nothing to do with real module structure */
53#define ELEMENT_WRAPPER_START "<status xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
54#define ELEMENT_WRAPPER_END "</status>"
55
David Sedlák68a1af12019-03-08 13:46:54 +010056struct state {
David Sedlák3b4db242018-10-19 16:11:01 +020057 struct ly_ctx *ctx;
David Sedlák3017da42019-02-15 09:48:04 +010058 struct lys_module *mod;
David Sedlák619db942019-07-03 14:47:30 +020059 struct lysp_module *lysp_mod;
David Sedlákda8ffa32019-07-08 14:17:10 +020060 struct yin_parser_ctx *yin_ctx;
David Sedlák79e50cb2019-06-05 16:33:09 +020061 bool finished_correctly;
David Sedlák68a1af12019-03-08 13:46:54 +010062};
David Sedlák872c7b42018-10-26 13:15:20 +020063
David Sedlák79e50cb2019-06-05 16:33:09 +020064#define BUFSIZE 1024
65char logbuf[BUFSIZE] = {0};
66int store = -1; /* negative for infinite logging, positive for limited logging */
67
68/* set to 0 to printing error messages to stderr instead of checking them in code */
David Sedlák298ff6d2019-07-26 14:29:03 +020069#define ENABLE_LOGGER_CHECKING 1
David Sedlák79e50cb2019-06-05 16:33:09 +020070
71#if ENABLE_LOGGER_CHECKING
72static void
73logger(LY_LOG_LEVEL level, const char *msg, const char *path)
74{
75 (void) level; /* unused */
76 if (store) {
77 if (path && path[0]) {
78 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
79 } else {
80 strncpy(logbuf, msg, BUFSIZE - 1);
81 }
82 if (store > 0) {
83 --store;
84 }
85 }
86}
87#endif
88
89#if ENABLE_LOGGER_CHECKING
90# define logbuf_assert(str) assert_string_equal(logbuf, str)
91#else
92# define logbuf_assert(str)
93#endif
94
95#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
96 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
97 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
98 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
99 CLEANUP
100
David Sedlák8e7bda82019-07-16 17:57:50 +0200101int
102setup_ly_ctx(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +0100103{
104 struct state *st = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +0200105
David Sedlák68a1af12019-03-08 13:46:54 +0100106 /* allocate state variable */
107 (*state) = st = calloc(1, sizeof(*st));
108 if (!st) {
109 fprintf(stderr, "Memmory allocation failed");
110 return EXIT_FAILURE;
111 }
David Sedlák872c7b42018-10-26 13:15:20 +0200112
David Sedlák68a1af12019-03-08 13:46:54 +0100113 /* create new libyang context */
114 ly_ctx_new(NULL, 0, &st->ctx);
David Sedlák872c7b42018-10-26 13:15:20 +0200115
David Sedlák8e7bda82019-07-16 17:57:50 +0200116 return EXIT_SUCCESS;
117}
118
119int
120destroy_ly_ctx(void **state)
121{
122 struct state *st = *state;
123 ly_ctx_destroy(st->ctx, NULL);
124 free(st);
125
126 return EXIT_SUCCESS;
127}
128
129static int
130setup_f(void **state)
131{
132 struct state *st = *state;
133
134#if ENABLE_LOGGER_CHECKING
135 /* setup logger */
136 ly_set_log_clb(logger, 1);
137#endif
138
David Sedlák68a1af12019-03-08 13:46:54 +0100139 /* allocate new module */
140 st->mod = calloc(1, sizeof(*st->mod));
141 st->mod->ctx = st->ctx;
142
David Sedlák619db942019-07-03 14:47:30 +0200143 /* allocate new parsed module */
144 st->lysp_mod = calloc(1, sizeof(*st->lysp_mod));
145 st->lysp_mod->mod = calloc(1, sizeof(*st->lysp_mod->mod));
146 st->lysp_mod->mod->ctx = st->ctx;
147
148 /* allocate parser context */
David Sedlákda8ffa32019-07-08 14:17:10 +0200149 st->yin_ctx = calloc(1, sizeof(*st->yin_ctx));
150 st->yin_ctx->xml_ctx.ctx = st->ctx;
151 st->yin_ctx->xml_ctx.line = 1;
David Sedlák8f5bce02019-06-03 16:41:08 +0200152
David Sedlák68a1af12019-03-08 13:46:54 +0100153 return EXIT_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +0200154}
155
156static int
David Sedlák68a1af12019-03-08 13:46:54 +0100157teardown_f(void **state)
158{
159 struct state *st = *(struct state **)state;
David Sedlák619db942019-07-03 14:47:30 +0200160 struct lys_module *temp;
David Sedlák68a1af12019-03-08 13:46:54 +0100161
David Sedlák79e50cb2019-06-05 16:33:09 +0200162#if ENABLE_LOGGER_CHECKING
163 /* teardown logger */
164 if (!st->finished_correctly && logbuf[0] != '\0') {
165 fprintf(stderr, "%s\n", logbuf);
166 }
167#endif
168
David Sedlák619db942019-07-03 14:47:30 +0200169 temp = st->lysp_mod->mod;
170
David Sedlákda8ffa32019-07-08 14:17:10 +0200171 lyxml_context_clear(&st->yin_ctx->xml_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100172 lys_module_free(st->mod, NULL);
David Sedlák619db942019-07-03 14:47:30 +0200173 lysp_module_free(st->lysp_mod);
174 lys_module_free(temp, NULL);
David Sedlákda8ffa32019-07-08 14:17:10 +0200175 free(st->yin_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100176
177 return EXIT_SUCCESS;
178}
179
David Sedlák392af4f2019-06-04 16:02:42 +0200180static struct state*
181reset_state(void **state)
182{
David Sedlák79e50cb2019-06-05 16:33:09 +0200183 ((struct state *)*state)->finished_correctly = true;
David Sedlák555c7202019-07-04 12:14:12 +0200184 logbuf[0] = '\0';
David Sedlák392af4f2019-06-04 16:02:42 +0200185 teardown_f(state);
186 setup_f(state);
187
188 return *state;
189}
190
David Sedlák79e50cb2019-06-05 16:33:09 +0200191void
192logbuf_clean(void)
193{
194 logbuf[0] = '\0';
195}
196
David Sedlák8985a142019-07-31 16:43:06 +0200197static int
198setup_logger(void **state)
199{
200 (void)state; /* unused */
201#if ENABLE_LOGGER_CHECKING
202 /* setup logger */
203 ly_set_log_clb(logger, 1);
204#endif
205
206 logbuf[0] = '\0';
207
208 return EXIT_SUCCESS;
209}
210
211static int
212teardown_logger(void **state)
213{
214 struct state *st = *state;
215
216#if ENABLE_LOGGER_CHECKING
217 /* teardown logger */
218 if (!st->finished_correctly && logbuf[0] != '\0') {
219 fprintf(stderr, "%s\n", logbuf);
220 }
221#endif
222
223 return EXIT_SUCCESS;
224}
225
226static int
227setup_element_test(void **state)
228{
229 setup_logger(state);
230 struct state *st = *state;
231
232 st->yin_ctx = calloc(1, sizeof(*st->yin_ctx));
233
234 /* allocate parser context */
235 st->yin_ctx->xml_ctx.ctx = st->ctx;
236 st->yin_ctx->xml_ctx.line = 1;
237
238 return EXIT_SUCCESS;
239}
240
241static int
242teardown_element_test(void **state)
243{
244 struct state *st = *(struct state **)state;
245
246 lyxml_context_clear(&st->yin_ctx->xml_ctx);
247 free(st->yin_ctx);
248
249 teardown_logger(state);
250
251 return EXIT_SUCCESS;
252}
253
David Sedlák68a1af12019-03-08 13:46:54 +0100254static void
David Sedlák1bccdfa2019-06-17 15:55:27 +0200255test_yin_match_keyword(void **state)
David Sedlák3b4db242018-10-19 16:11:01 +0200256{
David Sedlák8f7a1172019-06-20 14:42:18 +0200257 struct state *st = *state;
David Sedlák3b4db242018-10-19 16:11:01 +0200258
David Sedlák8f7a1172019-06-20 14:42:18 +0200259 const char *prefix, *name;
260 struct yin_arg_record *args = NULL;
261 size_t prefix_len, name_len;
262 /* create mock yin namespace in xml context */
263 const char *data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" />";
David Sedlákda8ffa32019-07-08 14:17:10 +0200264 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
265 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák8f7a1172019-06-20 14:42:18 +0200266 LY_ARRAY_FREE(args);
267
David Sedlákc1771b12019-07-10 15:55:46 +0200268 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydatax", strlen("anydatax"), prefix, prefix_len, YANG_NONE), YANG_NONE);
269 assert_int_equal(yin_match_keyword(st->yin_ctx, "asdasd", strlen("asdasd"), prefix, prefix_len, YANG_NONE), YANG_NONE);
270 assert_int_equal(yin_match_keyword(st->yin_ctx, "", 0, prefix, prefix_len, YANG_NONE), YANG_NONE);
271 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydata", strlen("anydata"), prefix, prefix_len, YANG_NONE), YANG_ANYDATA);
272 assert_int_equal(yin_match_keyword(st->yin_ctx, "anyxml", strlen("anyxml"), prefix, prefix_len, YANG_NONE), YANG_ANYXML);
273 assert_int_equal(yin_match_keyword(st->yin_ctx, "argument", strlen("argument"), prefix, prefix_len, YANG_NONE), YANG_ARGUMENT);
274 assert_int_equal(yin_match_keyword(st->yin_ctx, "augment", strlen("augment"), prefix, prefix_len, YANG_NONE), YANG_AUGMENT);
275 assert_int_equal(yin_match_keyword(st->yin_ctx, "base", strlen("base"), prefix, prefix_len, YANG_NONE), YANG_BASE);
276 assert_int_equal(yin_match_keyword(st->yin_ctx, "belongs-to", strlen("belongs-to"), prefix, prefix_len, YANG_NONE), YANG_BELONGS_TO);
277 assert_int_equal(yin_match_keyword(st->yin_ctx, "bit", strlen("bit"), prefix, prefix_len, YANG_NONE), YANG_BIT);
278 assert_int_equal(yin_match_keyword(st->yin_ctx, "case", strlen("case"), prefix, prefix_len, YANG_NONE), YANG_CASE);
279 assert_int_equal(yin_match_keyword(st->yin_ctx, "choice", strlen("choice"), prefix, prefix_len, YANG_NONE), YANG_CHOICE);
280 assert_int_equal(yin_match_keyword(st->yin_ctx, "config", strlen("config"), prefix, prefix_len, YANG_NONE), YANG_CONFIG);
281 assert_int_equal(yin_match_keyword(st->yin_ctx, "contact", strlen("contact"), prefix, prefix_len, YANG_NONE), YANG_CONTACT);
282 assert_int_equal(yin_match_keyword(st->yin_ctx, "container", strlen("container"), prefix, prefix_len, YANG_NONE), YANG_CONTAINER);
283 assert_int_equal(yin_match_keyword(st->yin_ctx, "default", strlen("default"), prefix, prefix_len, YANG_NONE), YANG_DEFAULT);
284 assert_int_equal(yin_match_keyword(st->yin_ctx, "description", strlen("description"), prefix, prefix_len, YANG_NONE), YANG_DESCRIPTION);
285 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviate", strlen("deviate"), prefix, prefix_len, YANG_NONE), YANG_DEVIATE);
286 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviation", strlen("deviation"), prefix, prefix_len, YANG_NONE), YANG_DEVIATION);
287 assert_int_equal(yin_match_keyword(st->yin_ctx, "enum", strlen("enum"), prefix, prefix_len, YANG_NONE), YANG_ENUM);
288 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);
289 assert_int_equal(yin_match_keyword(st->yin_ctx, "error-message", strlen("error-message"), prefix, prefix_len, YANG_NONE), YANG_ERROR_MESSAGE);
290 assert_int_equal(yin_match_keyword(st->yin_ctx, "extension", strlen("extension"), prefix, prefix_len, YANG_NONE), YANG_EXTENSION);
291 assert_int_equal(yin_match_keyword(st->yin_ctx, "feature", strlen("feature"), prefix, prefix_len, YANG_NONE), YANG_FEATURE);
292 assert_int_equal(yin_match_keyword(st->yin_ctx, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len, YANG_NONE), YANG_FRACTION_DIGITS);
293 assert_int_equal(yin_match_keyword(st->yin_ctx, "grouping", strlen("grouping"), prefix, prefix_len, YANG_NONE), YANG_GROUPING);
294 assert_int_equal(yin_match_keyword(st->yin_ctx, "identity", strlen("identity"), prefix, prefix_len, YANG_NONE), YANG_IDENTITY);
295 assert_int_equal(yin_match_keyword(st->yin_ctx, "if-feature", strlen("if-feature"), prefix, prefix_len, YANG_NONE), YANG_IF_FEATURE);
296 assert_int_equal(yin_match_keyword(st->yin_ctx, "import", strlen("import"), prefix, prefix_len, YANG_NONE), YANG_IMPORT);
297 assert_int_equal(yin_match_keyword(st->yin_ctx, "include", strlen("include"), prefix, prefix_len, YANG_NONE), YANG_INCLUDE);
298 assert_int_equal(yin_match_keyword(st->yin_ctx, "input", strlen("input"), prefix, prefix_len, YANG_NONE), YANG_INPUT);
299 assert_int_equal(yin_match_keyword(st->yin_ctx, "key", strlen("key"), prefix, prefix_len, YANG_NONE), YANG_KEY);
300 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf", strlen("leaf"), prefix, prefix_len, YANG_NONE), YANG_LEAF);
301 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf-list", strlen("leaf-list"), prefix, prefix_len, YANG_NONE), YANG_LEAF_LIST);
302 assert_int_equal(yin_match_keyword(st->yin_ctx, "length", strlen("length"), prefix, prefix_len, YANG_NONE), YANG_LENGTH);
303 assert_int_equal(yin_match_keyword(st->yin_ctx, "list", strlen("list"), prefix, prefix_len, YANG_NONE), YANG_LIST);
304 assert_int_equal(yin_match_keyword(st->yin_ctx, "mandatory", strlen("mandatory"), prefix, prefix_len, YANG_NONE), YANG_MANDATORY);
305 assert_int_equal(yin_match_keyword(st->yin_ctx, "max-elements", strlen("max-elements"), prefix, prefix_len, YANG_NONE), YANG_MAX_ELEMENTS);
306 assert_int_equal(yin_match_keyword(st->yin_ctx, "min-elements", strlen("min-elements"), prefix, prefix_len, YANG_NONE), YANG_MIN_ELEMENTS);
307 assert_int_equal(yin_match_keyword(st->yin_ctx, "modifier", strlen("modifier"), prefix, prefix_len, YANG_NONE), YANG_MODIFIER);
308 assert_int_equal(yin_match_keyword(st->yin_ctx, "module", strlen("module"), prefix, prefix_len, YANG_NONE), YANG_MODULE);
309 assert_int_equal(yin_match_keyword(st->yin_ctx, "must", strlen("must"), prefix, prefix_len, YANG_NONE), YANG_MUST);
310 assert_int_equal(yin_match_keyword(st->yin_ctx, "namespace", strlen("namespace"), prefix, prefix_len, YANG_NONE), YANG_NAMESPACE);
311 assert_int_equal(yin_match_keyword(st->yin_ctx, "notification", strlen("notification"), prefix, prefix_len, YANG_NONE), YANG_NOTIFICATION);
312 assert_int_equal(yin_match_keyword(st->yin_ctx, "ordered-by", strlen("ordered-by"), prefix, prefix_len, YANG_NONE), YANG_ORDERED_BY);
313 assert_int_equal(yin_match_keyword(st->yin_ctx, "organization", strlen("organization"), prefix, prefix_len, YANG_NONE), YANG_ORGANIZATION);
314 assert_int_equal(yin_match_keyword(st->yin_ctx, "output", strlen("output"), prefix, prefix_len, YANG_NONE), YANG_OUTPUT);
315 assert_int_equal(yin_match_keyword(st->yin_ctx, "path", strlen("path"), prefix, prefix_len, YANG_NONE), YANG_PATH);
316 assert_int_equal(yin_match_keyword(st->yin_ctx, "pattern", strlen("pattern"), prefix, prefix_len, YANG_NONE), YANG_PATTERN);
317 assert_int_equal(yin_match_keyword(st->yin_ctx, "position", strlen("position"), prefix, prefix_len, YANG_NONE), YANG_POSITION);
318 assert_int_equal(yin_match_keyword(st->yin_ctx, "prefix", strlen("prefix"), prefix, prefix_len, YANG_NONE), YANG_PREFIX);
319 assert_int_equal(yin_match_keyword(st->yin_ctx, "presence", strlen("presence"), prefix, prefix_len, YANG_NONE), YANG_PRESENCE);
320 assert_int_equal(yin_match_keyword(st->yin_ctx, "range", strlen("range"), prefix, prefix_len, YANG_NONE), YANG_RANGE);
321 assert_int_equal(yin_match_keyword(st->yin_ctx, "reference", strlen("reference"), prefix, prefix_len, YANG_NONE), YANG_REFERENCE);
322 assert_int_equal(yin_match_keyword(st->yin_ctx, "refine", strlen("refine"), prefix, prefix_len, YANG_NONE), YANG_REFINE);
323 assert_int_equal(yin_match_keyword(st->yin_ctx, "require-instance", strlen("require-instance"), prefix, prefix_len, YANG_NONE), YANG_REQUIRE_INSTANCE);
324 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision", strlen("revision"), prefix, prefix_len, YANG_NONE), YANG_REVISION);
325 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision-date", strlen("revision-date"), prefix, prefix_len, YANG_NONE), YANG_REVISION_DATE);
326 assert_int_equal(yin_match_keyword(st->yin_ctx, "rpc", strlen("rpc"), prefix, prefix_len, YANG_NONE), YANG_RPC);
327 assert_int_equal(yin_match_keyword(st->yin_ctx, "status", strlen("status"), prefix, prefix_len, YANG_NONE), YANG_STATUS);
328 assert_int_equal(yin_match_keyword(st->yin_ctx, "submodule", strlen("submodule"), prefix, prefix_len, YANG_NONE), YANG_SUBMODULE);
329 assert_int_equal(yin_match_keyword(st->yin_ctx, "type", strlen("type"), prefix, prefix_len, YANG_NONE), YANG_TYPE);
330 assert_int_equal(yin_match_keyword(st->yin_ctx, "typedef", strlen("typedef"), prefix, prefix_len, YANG_NONE), YANG_TYPEDEF);
331 assert_int_equal(yin_match_keyword(st->yin_ctx, "unique", strlen("unique"), prefix, prefix_len, YANG_NONE), YANG_UNIQUE);
332 assert_int_equal(yin_match_keyword(st->yin_ctx, "units", strlen("units"), prefix, prefix_len, YANG_NONE), YANG_UNITS);
333 assert_int_equal(yin_match_keyword(st->yin_ctx, "uses", strlen("uses"), prefix, prefix_len, YANG_NONE), YANG_USES);
334 assert_int_equal(yin_match_keyword(st->yin_ctx, "value", strlen("value"), prefix, prefix_len, YANG_NONE), YANG_VALUE);
335 assert_int_equal(yin_match_keyword(st->yin_ctx, "when", strlen("when"), prefix, prefix_len, YANG_NONE), YANG_WHEN);
336 assert_int_equal(yin_match_keyword(st->yin_ctx, "yang-version", strlen("yang-version"), prefix, prefix_len, YANG_NONE), YANG_YANG_VERSION);
337 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 +0200338
339 st->finished_correctly = true;
David Sedlák872c7b42018-10-26 13:15:20 +0200340}
David Sedlák3b4db242018-10-19 16:11:01 +0200341
David Sedlák872c7b42018-10-26 13:15:20 +0200342static void
David Sedlák060b00e2019-06-19 11:12:06 +0200343test_yin_match_argument_name(void **state)
David Sedlák872c7b42018-10-26 13:15:20 +0200344{
David Sedlák68a1af12019-03-08 13:46:54 +0100345 (void)state; /* unused */
David Sedlák872c7b42018-10-26 13:15:20 +0200346
David Sedlák060b00e2019-06-19 11:12:06 +0200347 assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN);
348 assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN);
349 assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN);
350 assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION);
351 assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE);
352 assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE);
353 assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME);
354 assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG);
355 assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
356 assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT);
357 assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI);
358 assert_int_equal(yin_match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200359}
360
David Sedlák68a1af12019-03-08 13:46:54 +0100361static void
David Sedlákb1a78352019-06-28 16:16:29 +0200362test_yin_parse_element_generic(void **state)
363{
364 const char *prefix, *name;
365 struct state *st = *state;
366 struct lysp_ext_instance exts;
367 size_t prefix_len, name_len;
368 LY_ERR ret;
369
370 memset(&exts, 0, sizeof(exts));
371
372 const char *data = "<elem attr=\"value\">text_value</elem>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200373 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák4ffcec82019-07-25 15:10:21 +0200374 ret = yin_parse_element_generic(st->yin_ctx, name, name_len, &data, &exts.child);
David Sedlákb1a78352019-06-28 16:16:29 +0200375 assert_int_equal(ret, LY_SUCCESS);
David Sedlák1fdb2522019-07-09 16:22:57 +0200376 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákb1a78352019-06-28 16:16:29 +0200377 assert_string_equal(exts.child->stmt, "elem");
378 assert_string_equal(exts.child->arg, "text_value");
David Sedlákb1a78352019-06-28 16:16:29 +0200379 assert_string_equal(exts.child->child->stmt, "attr");
380 assert_string_equal(exts.child->child->arg, "value");
381 assert_true(exts.child->child->flags & LYS_YIN_ATTR);
David Sedlákb1a78352019-06-28 16:16:29 +0200382 lysp_ext_instance_free(st->ctx, &exts);
David Sedlák5392a212019-07-01 09:19:10 +0200383 st = reset_state(state);
384
385 data = "<elem></elem>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200386 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák4ffcec82019-07-25 15:10:21 +0200387 ret = yin_parse_element_generic(st->yin_ctx, name, name_len, &data, &exts.child);
David Sedlák5392a212019-07-01 09:19:10 +0200388 assert_int_equal(ret, LY_SUCCESS);
389 assert_string_equal(exts.child->stmt, "elem");
390 assert_null(exts.child->child);
391 assert_null(exts.child->arg);
David Sedlákda8ffa32019-07-08 14:17:10 +0200392 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlák5392a212019-07-01 09:19:10 +0200393 lysp_ext_instance_free(st->ctx, &exts);
394
David Sedlákb1a78352019-06-28 16:16:29 +0200395 st->finished_correctly = true;
396}
397
398static void
399test_yin_parse_extension_instance(void **state)
400{
401 LY_ERR ret;
402 struct state *st = *state;
403 const char *prefix, *name;
404 size_t prefix_len, name_len;
405 struct yin_arg_record *args = NULL;
406 struct lysp_ext_instance *exts = NULL;
David Sedlákb1a78352019-06-28 16:16:29 +0200407 const char *data = "<ext value1=\"test\" value=\"test2\"><subelem>text</subelem></ext>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200408 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
409 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák1f90d252019-07-10 17:09:32 +0200410 ret = yin_parse_extension_instance(st->yin_ctx, args, &data, name2fullname(name, prefix_len),
David Sedlák619db942019-07-03 14:47:30 +0200411 namelen2fulllen(name_len, prefix_len), LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200412 assert_int_equal(ret, LY_SUCCESS);
413 assert_string_equal(exts->name, "ext");
414 assert_int_equal(exts->insubstmt_index, 0);
415 assert_true(exts->insubstmt == LYEXT_SUBSTMT_CONTACT);
416 assert_true(exts->yin & LYS_YIN);
417 assert_string_equal(exts->child->stmt, "value1");
418 assert_string_equal(exts->child->arg, "test");
419 assert_null(exts->child->child);
420 assert_true(exts->child->flags & LYS_YIN_ATTR);
421 assert_string_equal(exts->child->next->stmt, "value");
422 assert_string_equal(exts->child->next->arg, "test2");
423 assert_null(exts->child->next->child);
424 assert_true(exts->child->next->flags & LYS_YIN_ATTR);
425
426 assert_string_equal(exts->child->next->next->stmt, "subelem");
427 assert_string_equal(exts->child->next->next->arg, "text");
428 assert_null(exts->child->next->next->child);
429 assert_null(exts->child->next->next->next);
430 assert_false(exts->child->next->next->flags & LYS_YIN_ATTR);
David Sedlákda8ffa32019-07-08 14:17:10 +0200431 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákb1a78352019-06-28 16:16:29 +0200432 LY_ARRAY_FREE(args);
433 lysp_ext_instance_free(st->ctx, exts);
434 LY_ARRAY_FREE(exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200435 exts = NULL;
436 args = NULL;
437 st = reset_state(state);
438
439 data = "<extension-elem />";
David Sedlákda8ffa32019-07-08 14:17:10 +0200440 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
441 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák1f90d252019-07-10 17:09:32 +0200442 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 +0200443 assert_int_equal(ret, LY_SUCCESS);
444 assert_string_equal(exts->name, "extension-elem");
445 assert_null(exts->argument);
446 assert_null(exts->child);
447 assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT);
448 assert_int_equal(exts->insubstmt_index, 0);
449 assert_true(exts->yin & LYS_YIN);
David Sedlákda8ffa32019-07-08 14:17:10 +0200450 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákf250ecf2019-07-01 11:02:05 +0200451 LY_ARRAY_FREE(args);
452 lysp_ext_instance_free(st->ctx, exts);
453 LY_ARRAY_FREE(exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200454 st->finished_correctly = true;
455}
456
David Sedlák555c7202019-07-04 12:14:12 +0200457static void
458test_yin_parse_content(void **state)
459{
460 struct state *st = *state;
461 LY_ERR ret = LY_SUCCESS;
David Sedlákc5b20842019-08-13 10:18:31 +0200462 const char *name, *prefix;
463 size_t name_len, prefix_len;
David Sedlák555c7202019-07-04 12:14:12 +0200464 const char *data = "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
465 "<custom xmlns=\"my-ext\">"
466 "totally amazing extension"
467 "</custom>"
David Sedlák986cb412019-07-04 13:10:11 +0200468 "<extension name=\"ext\">"
469 "<argument name=\"argname\"></argument>"
470 "<description><text>desc</text></description>"
471 "<reference><text>ref</text></reference>"
472 "<status value=\"deprecated\"></status>"
473 "</extension>"
David Sedlák555c7202019-07-04 12:14:12 +0200474 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedlák32488102019-07-15 17:44:10 +0200475 "<if-feature name=\"foo\"></if-feature>"
David Sedlák32eee7b2019-07-09 12:38:44 +0200476 "<when condition=\"condition...\">"
477 "<reference><text>when_ref</text></reference>"
478 "<description><text>when_desc</text></description>"
479 "</when>"
David Sedláke1a30302019-07-10 13:49:38 +0200480 "<config value=\"true\"/>"
David Sedlákc1771b12019-07-10 15:55:46 +0200481 "<error-message>"
482 "<value>error-msg</value>"
483 "</error-message>"
David Sedlák2ce1be62019-07-10 16:15:09 +0200484 "<error-app-tag value=\"err-app-tag\"/>"
David Sedláka5b1d382019-07-10 16:31:09 +0200485 "<units name=\"radians\"></units>"
David Sedláke7084ce2019-07-10 16:44:15 +0200486 "<default value=\"default-value\"/>"
David Sedlák5545f5d2019-07-11 11:55:16 +0200487 "<position value=\"25\"></position>"
488 "<value value=\"-5\"/>"
David Sedlákcf5569a2019-07-11 13:31:34 +0200489 "<require-instance value=\"true\"></require-instance>"
David Sedlákb7296dd2019-07-11 14:58:38 +0200490 "<range value=\"5..10\" />"
David Sedlák438ae432019-07-11 15:36:54 +0200491 "<length value=\"baf\"/>"
David Sedlákd3983112019-07-12 11:20:56 +0200492 "<pattern value='pattern'>"
493 "<modifier value='invert-match'/>"
494 "</pattern>"
David Sedlákfd5b9c32019-07-12 15:33:13 +0200495 "<enum name=\"yay\">"
496 "</enum>"
David Sedlák555c7202019-07-04 12:14:12 +0200497 "</prefix>";
498 struct lysp_ext_instance *exts = NULL;
David Sedlák5f8191e2019-07-08 16:35:52 +0200499 const char **if_features = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200500 struct yin_arg_record *attrs = NULL;
David Sedláke7084ce2019-07-10 16:44:15 +0200501 const char *value, *err_msg, *app_tag, *units, *def;
David Sedlák986cb412019-07-04 13:10:11 +0200502 struct lysp_ext *ext_def = NULL;
David Sedlák32eee7b2019-07-09 12:38:44 +0200503 struct lysp_when *when_p = NULL;
David Sedlákcf5569a2019-07-11 13:31:34 +0200504 struct lysp_type_enum pos_enum = {}, val_enum = {};
David Sedlákfd5b9c32019-07-12 15:33:13 +0200505 struct lysp_type req_type = {}, range_type = {}, len_type = {}, patter_type = {}, enum_type = {};
David Sedláke1a30302019-07-10 13:49:38 +0200506 uint8_t config = 0;
David Sedlák555c7202019-07-04 12:14:12 +0200507
David Sedlákc5b20842019-08-13 10:18:31 +0200508 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlákda8ffa32019-07-08 14:17:10 +0200509 yin_load_attributes(st->yin_ctx, &data, &attrs);
David Sedlák555c7202019-07-04 12:14:12 +0200510
David Sedlákfd5b9c32019-07-12 15:33:13 +0200511 struct yin_subelement subelems[17] = {
David Sedlák438ae432019-07-11 15:36:54 +0200512 {YANG_CONFIG, &config, 0},
David Sedlákc3da3ef2019-07-19 12:56:08 +0200513 {YANG_DEFAULT, &def, YIN_SUBELEM_UNIQUE},
David Sedlákfd5b9c32019-07-12 15:33:13 +0200514 {YANG_ENUM, &enum_type, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200515 {YANG_ERROR_APP_TAG, &app_tag, 0},
516 {YANG_ERROR_MESSAGE, &err_msg, 0},
517 {YANG_EXTENSION, &ext_def, 0},
518 {YANG_IF_FEATURE, &if_features, 0},
519 {YANG_LENGTH, &len_type, 0},
David Sedlákd3983112019-07-12 11:20:56 +0200520 {YANG_PATTERN, &patter_type, 0},
David Sedlák43801c92019-08-05 15:58:54 +0200521 {YANG_POSITION, &pos_enum, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200522 {YANG_RANGE, &range_type, 0},
523 {YANG_REQUIRE_INSTANCE, &req_type, 0},
524 {YANG_UNITS, &units, 0},
525 {YANG_VALUE, &val_enum, 0},
526 {YANG_WHEN, &when_p, 0},
527 {YANG_CUSTOM, NULL, 0},
528 {YIN_TEXT, &value, 0}
David Sedlákd3983112019-07-12 11:20:56 +0200529 };
David Sedlákfd5b9c32019-07-12 15:33:13 +0200530 ret = yin_parse_content(st->yin_ctx, subelems, 17, &data, YANG_PREFIX, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200531 assert_int_equal(ret, LY_SUCCESS);
David Sedlák1fdb2522019-07-09 16:22:57 +0200532 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlák2ce1be62019-07-10 16:15:09 +0200533 /* check parsed values */
David Sedláke7084ce2019-07-10 16:44:15 +0200534 assert_string_equal(def, "default-value");
David Sedlák555c7202019-07-04 12:14:12 +0200535 assert_string_equal(exts->name, "custom");
536 assert_string_equal(exts->argument, "totally amazing extension");
537 assert_string_equal(value, "wsefsdf");
David Sedláka5b1d382019-07-10 16:31:09 +0200538 assert_string_equal(units, "radians");
David Sedlák32eee7b2019-07-09 12:38:44 +0200539 assert_string_equal(when_p->cond, "condition...");
540 assert_string_equal(when_p->dsc, "when_desc");
541 assert_string_equal(when_p->ref, "when_ref");
David Sedláke1a30302019-07-10 13:49:38 +0200542 assert_int_equal(config, LYS_CONFIG_W);
David Sedlák5545f5d2019-07-11 11:55:16 +0200543 assert_int_equal(pos_enum.value, 25);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200544 assert_true(pos_enum.flags & LYS_SET_VALUE);
David Sedlák5545f5d2019-07-11 11:55:16 +0200545 assert_int_equal(val_enum.value, -5);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200546 assert_true(val_enum.flags & LYS_SET_VALUE);
David Sedlákcf5569a2019-07-11 13:31:34 +0200547 assert_int_equal(req_type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200548 assert_true(req_type.flags &= LYS_SET_REQINST);
David Sedlákb7296dd2019-07-11 14:58:38 +0200549 assert_string_equal(range_type.range->arg, "5..10");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200550 assert_true(range_type.flags & LYS_SET_RANGE);
David Sedlákc1771b12019-07-10 15:55:46 +0200551 assert_string_equal(err_msg, "error-msg");
David Sedlák2ce1be62019-07-10 16:15:09 +0200552 assert_string_equal(app_tag, "err-app-tag");
David Sedlákfd5b9c32019-07-12 15:33:13 +0200553 assert_string_equal(enum_type.enums->name, "yay");
David Sedlák438ae432019-07-11 15:36:54 +0200554 assert_string_equal(len_type.length->arg, "baf");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200555 assert_true(len_type.flags & LYS_SET_LENGTH);
David Sedlákd3983112019-07-12 11:20:56 +0200556 assert_string_equal(patter_type.patterns->arg, "\x015pattern");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200557 assert_true(patter_type.flags & LYS_SET_PATTERN);
David Sedlák2ce1be62019-07-10 16:15:09 +0200558 /* cleanup */
David Sedlák555c7202019-07-04 12:14:12 +0200559 lysp_ext_instance_free(st->ctx, exts);
David Sedlák32eee7b2019-07-09 12:38:44 +0200560 lysp_when_free(st->ctx, when_p);
David Sedlák986cb412019-07-04 13:10:11 +0200561 lysp_ext_free(st->ctx, ext_def);
David Sedlák5f8191e2019-07-08 16:35:52 +0200562 FREE_STRING(st->ctx, *if_features);
David Sedlákc1771b12019-07-10 15:55:46 +0200563 FREE_STRING(st->ctx, err_msg);
David Sedlák2ce1be62019-07-10 16:15:09 +0200564 FREE_STRING(st->ctx, app_tag);
David Sedláka5b1d382019-07-10 16:31:09 +0200565 FREE_STRING(st->ctx, units);
David Sedlákd3983112019-07-12 11:20:56 +0200566 FREE_STRING(st->ctx, patter_type.patterns->arg);
David Sedláke7084ce2019-07-10 16:44:15 +0200567 FREE_STRING(st->ctx, def);
David Sedlákb7296dd2019-07-11 14:58:38 +0200568 FREE_STRING(st->ctx, range_type.range->arg);
David Sedlák438ae432019-07-11 15:36:54 +0200569 FREE_STRING(st->ctx, len_type.length->arg);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200570 FREE_STRING(st->ctx, enum_type.enums->name);
David Sedlákb7296dd2019-07-11 14:58:38 +0200571 FREE_STRING(st->ctx, value);
David Sedlák5f8191e2019-07-08 16:35:52 +0200572 LY_ARRAY_FREE(if_features);
David Sedlák555c7202019-07-04 12:14:12 +0200573 LY_ARRAY_FREE(exts);
David Sedlák986cb412019-07-04 13:10:11 +0200574 LY_ARRAY_FREE(ext_def);
David Sedlák555c7202019-07-04 12:14:12 +0200575 LY_ARRAY_FREE(attrs);
David Sedlákd3983112019-07-12 11:20:56 +0200576 LY_ARRAY_FREE(patter_type.patterns);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200577 LY_ARRAY_FREE(enum_type.enums);
David Sedlák32eee7b2019-07-09 12:38:44 +0200578 free(when_p);
David Sedlákb7296dd2019-07-11 14:58:38 +0200579 free(range_type.range);
David Sedlák438ae432019-07-11 15:36:54 +0200580 free(len_type.length);
David Sedlák555c7202019-07-04 12:14:12 +0200581 attrs = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200582 st = reset_state(state);
583
584 /* test unique subelem */
585 const char *prefix_value;
586 struct yin_subelement subelems2[2] = {{YANG_PREFIX, &prefix_value, 0},
587 {YIN_TEXT, &value, YIN_SUBELEM_UNIQUE}};
David Sedláke6cd89e2019-08-07 12:46:02 +0200588 data = ELEMENT_WRAPPER_START
David Sedlák555c7202019-07-04 12:14:12 +0200589 "<prefix value=\"inv_mod\" />"
590 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
591 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedláke6cd89e2019-08-07 12:46:02 +0200592 ELEMENT_WRAPPER_END;
David Sedlákc5b20842019-08-13 10:18:31 +0200593 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlákda8ffa32019-07-08 14:17:10 +0200594 yin_load_attributes(st->yin_ctx, &data, &attrs);
David Sedláke6cd89e2019-08-07 12:46:02 +0200595 ret = yin_parse_content(st->yin_ctx, subelems2, 2, &data, YANG_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200596 assert_int_equal(ret, LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200597 logbuf_assert("Redefinition of \"text\" sub-element in \"status\" element. Line number 1.");
David Sedlák555c7202019-07-04 12:14:12 +0200598 lydict_remove(st->ctx, prefix_value);
599 lydict_remove(st->ctx, value);
600 st = reset_state(state);
601 LY_ARRAY_FREE(attrs);
602 attrs = NULL;
603
604 /* test first subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200605 data = ELEMENT_WRAPPER_START
David Sedlák555c7202019-07-04 12:14:12 +0200606 "<prefix value=\"inv_mod\" />"
607 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
608 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedláke6cd89e2019-08-07 12:46:02 +0200609 ELEMENT_WRAPPER_END;
David Sedlák555c7202019-07-04 12:14:12 +0200610 struct yin_subelement subelems3[2] = {{YANG_PREFIX, &prefix_value, 0},
611 {YIN_TEXT, &value, YIN_SUBELEM_FIRST}};
David Sedlákc5b20842019-08-13 10:18:31 +0200612 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlákda8ffa32019-07-08 14:17:10 +0200613 yin_load_attributes(st->yin_ctx, &data, &attrs);
David Sedláke6cd89e2019-08-07 12:46:02 +0200614 ret = yin_parse_content(st->yin_ctx, subelems3, 2, &data, YANG_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200615 assert_int_equal(ret, LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200616 logbuf_assert("Sub-element \"text\" of \"status\" element must be defined as it's first sub-element. Line number 1.");
David Sedlák555c7202019-07-04 12:14:12 +0200617 lydict_remove(st->ctx, prefix_value);
618 st = reset_state(state);
619 LY_ARRAY_FREE(attrs);
620 attrs = NULL;
621
622 /* test mandatory subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200623 data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END;
David Sedlák555c7202019-07-04 12:14:12 +0200624 struct yin_subelement subelems4[1] = {{YANG_PREFIX, &prefix_value, YIN_SUBELEM_MANDATORY}};
David Sedlákc5b20842019-08-13 10:18:31 +0200625 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlákda8ffa32019-07-08 14:17:10 +0200626 yin_load_attributes(st->yin_ctx, &data, &attrs);
David Sedláke6cd89e2019-08-07 12:46:02 +0200627 ret = yin_parse_content(st->yin_ctx, subelems4, 1, &data, YANG_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200628 assert_int_equal(ret, LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200629 logbuf_assert("Missing mandatory sub-element \"prefix\" of \"status\" element. Line number 1.");
David Sedlák555c7202019-07-04 12:14:12 +0200630 LY_ARRAY_FREE(attrs);
631
632 st->finished_correctly = true;
633}
634
David Sedlák92147b02019-07-09 14:01:01 +0200635static void
David Sedlák4a650532019-07-10 11:55:18 +0200636test_validate_value(void **state)
637{
638 struct state *st = *state;
639 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "#invalid", 8), LY_EVALID);
640 logbuf_assert("Invalid identifier character '#'. Line number 1.");
641 assert_int_equal(yin_validate_value(st->yin_ctx, Y_STR_ARG, "", 0), LY_SUCCESS);
642 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "pre:b", 5), LY_EVALID);
643 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:b", 5), LY_SUCCESS);
644 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:pre:b", 9), LY_EVALID);
645
646 st->finished_correctly = true;
647}
648
David Sedlák32488102019-07-15 17:44:10 +0200649/* helper function to simplify unit test of each element using parse_content function */
650LY_ERR
651test_element_helper(struct state *st, const char **data, void *dest, const char **text,
652 struct lysp_ext_instance **exts, bool valid)
653{
654 struct yin_arg_record *attrs = NULL;
David Sedlákc5b20842019-08-13 10:18:31 +0200655 const char *name, *prefix;
656 size_t name_len, prefix_len;
David Sedlák32488102019-07-15 17:44:10 +0200657 LY_ERR ret = LY_SUCCESS;
658 struct yin_subelement subelems[71] = {
659 {YANG_ACTION, dest, 0},
660 {YANG_ANYDATA, dest, 0},
661 {YANG_ANYXML, dest, 0},
662 {YANG_ARGUMENT,dest, 0},
663 {YANG_AUGMENT, dest, 0},
664 {YANG_BASE, dest, 0},
665 {YANG_BELONGS_TO, dest, 0},
666 {YANG_BIT, dest, 0},
667 {YANG_CASE, dest, 0},
668 {YANG_CHOICE, dest, 0},
669 {YANG_CONFIG, dest, 0},
670 {YANG_CONTACT, dest, 0},
671 {YANG_CONTAINER, dest, 0},
David Sedlákc3da3ef2019-07-19 12:56:08 +0200672 {YANG_DEFAULT, dest, YIN_SUBELEM_UNIQUE},
David Sedlák32488102019-07-15 17:44:10 +0200673 {YANG_DESCRIPTION, dest, 0},
674 {YANG_DEVIATE, dest, 0},
675 {YANG_DEVIATION, dest, 0},
676 {YANG_ENUM, dest, 0},
677 {YANG_ERROR_APP_TAG, dest, 0},
678 {YANG_ERROR_MESSAGE, dest, 0},
679 {YANG_EXTENSION, dest, 0},
680 {YANG_FEATURE, dest, 0},
681 {YANG_FRACTION_DIGITS, dest, 0},
682 {YANG_GROUPING, dest, 0},
683 {YANG_IDENTITY, dest, 0},
684 {YANG_IF_FEATURE, dest, 0},
685 {YANG_IMPORT, dest, 0},
686 {YANG_INCLUDE, dest, 0},
687 {YANG_INPUT, dest, 0},
688 {YANG_KEY, dest, 0},
689 {YANG_LEAF, dest, 0},
690 {YANG_LEAF_LIST, dest, 0},
691 {YANG_LENGTH, dest, 0},
692 {YANG_LIST, dest, 0},
693 {YANG_MANDATORY, dest, 0},
694 {YANG_MAX_ELEMENTS, dest, 0},
695 {YANG_MIN_ELEMENTS, dest, 0},
696 {YANG_MODIFIER, dest, 0},
697 {YANG_MODULE, dest, 0},
698 {YANG_MUST, dest, 0},
699 {YANG_NAMESPACE, dest, 0},
700 {YANG_NOTIFICATION, dest, 0},
701 {YANG_ORDERED_BY, dest, 0},
702 {YANG_ORGANIZATION, dest, 0},
703 {YANG_OUTPUT, dest, 0},
704 {YANG_PATH, dest, 0},
705 {YANG_PATTERN, dest, 0},
David Sedlák43801c92019-08-05 15:58:54 +0200706 {YANG_POSITION, dest, 0},
David Sedlák32488102019-07-15 17:44:10 +0200707 {YANG_PREFIX, dest, 0},
708 {YANG_PRESENCE, dest, 0},
709 {YANG_RANGE, dest, 0},
710 {YANG_REFERENCE, dest, 0},
711 {YANG_REFINE, dest, 0},
712 {YANG_REQUIRE_INSTANCE, dest, 0},
713 {YANG_REVISION, dest, 0},
714 {YANG_REVISION_DATE, dest, 0},
715 {YANG_RPC, dest, 0},
716 {YANG_STATUS, dest, 0},
717 {YANG_SUBMODULE, dest, 0},
718 {YANG_TYPE, dest, 0},
719 {YANG_TYPEDEF, dest, 0},
720 {YANG_UNIQUE, dest, 0},
721 {YANG_UNITS, dest, 0},
722 {YANG_USES, dest, 0},
723 {YANG_VALUE, dest, 0},
724 {YANG_WHEN, dest, 0},
725 {YANG_YANG_VERSION, dest, 0},
726 {YANG_YIN_ELEMENT, dest, 0},
727 {YANG_CUSTOM, dest, 0},
728 {YIN_TEXT, dest, 0},
729 {YIN_VALUE, dest, 0}
730 };
David Sedlákc5b20842019-08-13 10:18:31 +0200731 LY_CHECK_RET(lyxml_get_element(&st->yin_ctx->xml_ctx, data, &prefix, &prefix_len, &name, &name_len));
David Sedlák4f03b932019-07-26 13:01:47 +0200732 LY_CHECK_RET(yin_load_attributes(st->yin_ctx, data, &attrs));
David Sedlákc5b20842019-08-13 10:18:31 +0200733 ret = yin_parse_content(st->yin_ctx, subelems, 71, data, yin_match_keyword(st->yin_ctx, name, name_len, prefix, prefix_len, YANG_NONE), text, exts);
David Sedlák32488102019-07-15 17:44:10 +0200734 LY_ARRAY_FREE(attrs);
735 if (valid) {
736 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
737 }
738 /* reset status */
739 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
740 return ret;
741}
742
David Sedlákd1144562019-08-06 12:36:14 +0200743#define EXT_SUBELEM "<myext:c-define name=\"MY_MTU\" xmlns:myext=\"urn:example:extensions\"/>"
744
David Sedlák32488102019-07-15 17:44:10 +0200745static void
David Sedlák43801c92019-08-05 15:58:54 +0200746test_enum_elem(void **state)
David Sedlák32488102019-07-15 17:44:10 +0200747{
David Sedlák32488102019-07-15 17:44:10 +0200748 struct state *st = *state;
749 struct lysp_type type = {};
750 const char *data;
751 data = ELEMENT_WRAPPER_START
752 "<enum name=\"enum-name\">"
753 "<if-feature name=\"feature\" />"
754 "<value value=\"55\" />"
755 "<status value=\"deprecated\" />"
756 "<description><text>desc...</text></description>"
757 "<reference><text>ref...</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +0200758 EXT_SUBELEM
David Sedlák32488102019-07-15 17:44:10 +0200759 "</enum>"
760 ELEMENT_WRAPPER_END;
761 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200762 assert_string_equal(type.enums->name, "enum-name");
David Sedlák32488102019-07-15 17:44:10 +0200763 assert_string_equal(*type.enums->iffeatures, "feature");
764 assert_int_equal(type.enums->value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200765 assert_true((type.enums->flags & LYS_STATUS_DEPRC) && (type.enums->flags & LYS_SET_VALUE));
David Sedlák32488102019-07-15 17:44:10 +0200766 assert_string_equal(type.enums->dsc, "desc...");
767 assert_string_equal(type.enums->ref, "ref...");
David Sedlákd1144562019-08-06 12:36:14 +0200768 assert_string_equal(type.enums->exts->name, "myext:c-define");
769 assert_int_equal(type.enums->exts->insubstmt_index, 0);
770 assert_int_equal(type.enums->exts->insubstmt, LYEXT_SUBSTMT_SELF);
771 lysp_type_free(st->ctx, &type);
772 memset(&type, 0, sizeof type);
773
774 data = ELEMENT_WRAPPER_START
775 "<enum name=\"enum-name\"></enum>"
776 ELEMENT_WRAPPER_END;
777 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
778 assert_string_equal(type.enums->name, "enum-name");
David Sedlák32488102019-07-15 17:44:10 +0200779 lysp_type_free(st->ctx, &type);
780 memset(&type, 0, sizeof type);
781
David Sedlák43801c92019-08-05 15:58:54 +0200782 st->finished_correctly = true;
783}
784
785static void
786test_bit_elem(void **state)
787{
788 struct state *st = *state;
789 struct lysp_type type = {};
790 const char *data;
791 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200792 "<bit name=\"bit-name\">"
David Sedlák43801c92019-08-05 15:58:54 +0200793 "<if-feature name=\"feature\" />"
794 "<position value=\"55\" />"
795 "<status value=\"deprecated\" />"
796 "<description><text>desc...</text></description>"
797 "<reference><text>ref...</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +0200798 EXT_SUBELEM
David Sedlák43801c92019-08-05 15:58:54 +0200799 "</bit>"
800 ELEMENT_WRAPPER_END;
801 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200802 assert_string_equal(type.bits->name, "bit-name");
David Sedlák43801c92019-08-05 15:58:54 +0200803 assert_string_equal(*type.bits->iffeatures, "feature");
804 assert_int_equal(type.bits->value, 55);
805 assert_true((type.bits->flags & LYS_STATUS_DEPRC) && (type.bits->flags & LYS_SET_VALUE));
806 assert_string_equal(type.bits->dsc, "desc...");
807 assert_string_equal(type.bits->ref, "ref...");
David Sedlákd1144562019-08-06 12:36:14 +0200808 assert_string_equal(type.bits->exts->name, "myext:c-define");
809 assert_int_equal(type.bits->exts->insubstmt_index, 0);
810 assert_int_equal(type.bits->exts->insubstmt, LYEXT_SUBSTMT_SELF);
811 lysp_type_free(st->ctx, &type);
812 memset(&type, 0, sizeof type);
813
814 data = ELEMENT_WRAPPER_START
815 "<bit name=\"bit-name\"> </bit>"
816 ELEMENT_WRAPPER_END;
817 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
818 assert_string_equal(type.bits->name, "bit-name");
David Sedlák43801c92019-08-05 15:58:54 +0200819 lysp_type_free(st->ctx, &type);
820 memset(&type, 0, sizeof type);
821
David Sedlák32488102019-07-15 17:44:10 +0200822 st->finished_correctly = true;
823}
824
825static void
826test_meta_elem(void **state)
827{
828 struct state *st = *state;
829 char *value = NULL;
830 const char *data;
David Sedlákd1144562019-08-06 12:36:14 +0200831 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200832
833 /* organization element */
834 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200835 "<organization><text>organization...</text>" EXT_SUBELEM EXT_SUBELEM "</organization>"
David Sedlák32488102019-07-15 17:44:10 +0200836 ELEMENT_WRAPPER_END;
David Sedlákd1144562019-08-06 12:36:14 +0200837 assert_int_equal(test_element_helper(st, &data, &value, NULL, &exts, true), LY_SUCCESS);
838 assert_string_equal(exts[0].name, "myext:c-define");
839 assert_int_equal(exts[0].insubstmt_index, 0);
840 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ORGANIZATION);
841 assert_string_equal(exts[1].name, "myext:c-define");
842 assert_int_equal(exts[1].insubstmt_index, 0);
843 assert_int_equal(exts[1].insubstmt, LYEXT_SUBSTMT_ORGANIZATION);
David Sedlák32488102019-07-15 17:44:10 +0200844 assert_string_equal(value, "organization...");
845 FREE_STRING(st->ctx, value);
David Sedlákd1144562019-08-06 12:36:14 +0200846 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +0200847 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +0200848 exts = NULL;
849
David Sedlák32488102019-07-15 17:44:10 +0200850 /* contact element */
851 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200852 "<contact><text>contact...</text>" EXT_SUBELEM "</contact>"
David Sedlák32488102019-07-15 17:44:10 +0200853 ELEMENT_WRAPPER_END;
David Sedlákd1144562019-08-06 12:36:14 +0200854 assert_int_equal(test_element_helper(st, &data, &value, NULL, &exts, true), LY_SUCCESS);
855 assert_string_equal(exts[0].name, "myext:c-define");
856 assert_int_equal(exts[0].insubstmt_index, 0);
857 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_CONTACT);
David Sedlák32488102019-07-15 17:44:10 +0200858 assert_string_equal(value, "contact...");
859 FREE_STRING(st->ctx, value);
David Sedlákd1144562019-08-06 12:36:14 +0200860 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
861 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200862 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +0200863
David Sedlák32488102019-07-15 17:44:10 +0200864 /* description element */
865 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200866 "<description><text>description...</text>" EXT_SUBELEM "</description>"
David Sedlák32488102019-07-15 17:44:10 +0200867 ELEMENT_WRAPPER_END;
David Sedlákd1144562019-08-06 12:36:14 +0200868 assert_int_equal(test_element_helper(st, &data, &value, NULL, &exts, true), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +0200869 assert_string_equal(value, "description...");
David Sedlákd1144562019-08-06 12:36:14 +0200870 assert_string_equal(exts[0].name, "myext:c-define");
871 assert_int_equal(exts[0].insubstmt_index, 0);
872 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_DESCRIPTION);
David Sedlák32488102019-07-15 17:44:10 +0200873 FREE_STRING(st->ctx, value);
874 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +0200875 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
876 exts = NULL;
877
David Sedlák32488102019-07-15 17:44:10 +0200878 /* reference element */
879 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200880 "<reference><text>reference...</text>" EXT_SUBELEM "</reference>"
David Sedlák32488102019-07-15 17:44:10 +0200881 ELEMENT_WRAPPER_END;
David Sedlákd1144562019-08-06 12:36:14 +0200882 assert_int_equal(test_element_helper(st, &data, &value, NULL, &exts, true), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +0200883 assert_string_equal(value, "reference...");
David Sedlákd1144562019-08-06 12:36:14 +0200884 assert_string_equal(exts[0].name, "myext:c-define");
885 assert_int_equal(exts[0].insubstmt_index, 0);
886 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_REFERENCE);
David Sedlák32488102019-07-15 17:44:10 +0200887 FREE_STRING(st->ctx, value);
888 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +0200889 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
890 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200891
David Sedlákdf2a9732019-08-07 13:23:16 +0200892 /* reference element */
893 data = ELEMENT_WRAPPER_START
894 "<reference invalid=\"text\"><text>reference...</text>""</reference>"
895 ELEMENT_WRAPPER_END;
896 assert_int_equal(test_element_helper(st, &data, &value, NULL, &exts, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200897 logbuf_assert("Unexpected attribute \"invalid\" of \"reference\" element. Line number 1.");
David Sedlákdf2a9732019-08-07 13:23:16 +0200898 FREE_STRING(st->ctx, value);
899 value = NULL;
900 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
901 exts = NULL;
902
David Sedlák32488102019-07-15 17:44:10 +0200903 /* missing text subelement */
904 data = ELEMENT_WRAPPER_START
905 "<reference>reference...</reference>"
906 ELEMENT_WRAPPER_END;
David Sedlákd1144562019-08-06 12:36:14 +0200907 assert_int_equal(test_element_helper(st, &data, &value, NULL, &exts, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200908 logbuf_assert("Missing mandatory sub-element \"text\" of \"reference\" element. Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +0200909
David Sedlákd1144562019-08-06 12:36:14 +0200910 /* reference element */
911 data = ELEMENT_WRAPPER_START
912 "<reference>" EXT_SUBELEM "<text>reference...</text></reference>"
913 ELEMENT_WRAPPER_END;
914 assert_int_equal(test_element_helper(st, &data, &value, NULL, &exts, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200915 logbuf_assert("Sub-element \"text\" of \"reference\" element must be defined as it's first sub-element. Line number 1.");
David Sedlákd1144562019-08-06 12:36:14 +0200916 FREE_STRING(st->ctx, value);
917 value = NULL;
918 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
919 exts = NULL;
920
David Sedlák32488102019-07-15 17:44:10 +0200921 st->finished_correctly = true;
922}
923
924static void
925test_import_elem(void **state)
926{
927 struct state *st = *state;
928 const char *data;
David Sedlák298ff6d2019-07-26 14:29:03 +0200929 struct lysp_import *imports = NULL;
930 struct import_meta imp_meta = {"prefix", &imports};
David Sedlák32488102019-07-15 17:44:10 +0200931
932 /* max subelems */
933 data = ELEMENT_WRAPPER_START
934 "<import module=\"a\">"
David Sedlákd1144562019-08-06 12:36:14 +0200935 EXT_SUBELEM
David Sedlák32488102019-07-15 17:44:10 +0200936 "<prefix value=\"a_mod\"/>"
937 "<revision-date date=\"2015-01-01\"></revision-date>"
938 "<description><text>import description</text></description>"
939 "<reference><text>import reference</text></reference>"
940 "</import>"
941 ELEMENT_WRAPPER_END;
David Sedlák298ff6d2019-07-26 14:29:03 +0200942 assert_int_equal(test_element_helper(st, &data, &imp_meta, NULL, NULL, true), LY_SUCCESS);
943 assert_string_equal(imports->name, "a");
944 assert_string_equal(imports->prefix, "a_mod");
945 assert_string_equal(imports->rev, "2015-01-01");
946 assert_string_equal(imports->dsc, "import description");
947 assert_string_equal(imports->ref, "import reference");
David Sedlákd1144562019-08-06 12:36:14 +0200948 assert_string_equal(imports->exts->name, "myext:c-define");
949 assert_int_equal(imports->exts->insubstmt, LYEXT_SUBSTMT_SELF);
950 assert_int_equal(imports->exts->insubstmt_index, 0);
David Sedlák298ff6d2019-07-26 14:29:03 +0200951 FREE_ARRAY(st->ctx, imports, lysp_import_free);
952 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200953
954 /* min subelems */
David Sedlák32488102019-07-15 17:44:10 +0200955 data = ELEMENT_WRAPPER_START
956 "<import module=\"a\">"
957 "<prefix value=\"a_mod\"/>"
958 "</import>"
959 ELEMENT_WRAPPER_END;
David Sedlák298ff6d2019-07-26 14:29:03 +0200960 assert_int_equal(test_element_helper(st, &data, &imp_meta, NULL, NULL, true), LY_SUCCESS);
961 assert_string_equal(imports->prefix, "a_mod");
962 FREE_ARRAY(st->ctx, imports, lysp_import_free);
963 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200964
965 /* invalid (missing prefix) */
David Sedlák298ff6d2019-07-26 14:29:03 +0200966 data = ELEMENT_WRAPPER_START "<import module=\"a\"></import>" ELEMENT_WRAPPER_END;
967 assert_int_equal(test_element_helper(st, &data, &imp_meta, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200968 logbuf_assert("Missing mandatory sub-element \"prefix\" of \"import\" element. Line number 1.");
David Sedlák298ff6d2019-07-26 14:29:03 +0200969 FREE_ARRAY(st->ctx, imports, lysp_import_free);
970 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200971
972 /* invalid reused prefix */
David Sedlák32488102019-07-15 17:44:10 +0200973 data = ELEMENT_WRAPPER_START
974 "<import module=\"a\">"
David Sedlák298ff6d2019-07-26 14:29:03 +0200975 "<prefix value=\"prefix\"/>"
David Sedlák32488102019-07-15 17:44:10 +0200976 "</import>"
977 "<import module=\"a\">"
David Sedlák298ff6d2019-07-26 14:29:03 +0200978 "<prefix value=\"prefix\"/>"
David Sedlák32488102019-07-15 17:44:10 +0200979 "</import>"
980 ELEMENT_WRAPPER_END;
David Sedlák298ff6d2019-07-26 14:29:03 +0200981 assert_int_equal(test_element_helper(st, &data, &imp_meta, NULL, NULL, false), LY_EVALID);
982 logbuf_assert("Prefix \"prefix\" already used as module prefix. Line number 1.");
983 FREE_ARRAY(st->ctx, imports, lysp_import_free);
984 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200985
986 st->finished_correctly = true;
987}
988
989static void
990test_status_elem(void **state)
991{
992 struct state *st = *state;
993 const char *data;
994 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +0200995 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200996
997 /* test valid values */
998 data = ELEMENT_WRAPPER_START "<status value=\"current\" />" ELEMENT_WRAPPER_END;
999 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001000 assert_true(flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +02001001
1002 data = ELEMENT_WRAPPER_START "<status value=\"deprecated\" />" ELEMENT_WRAPPER_END;
1003 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001004 assert_true(flags & LYS_STATUS_DEPRC);
David Sedlák32488102019-07-15 17:44:10 +02001005
David Sedlákd1144562019-08-06 12:36:14 +02001006 data = ELEMENT_WRAPPER_START "<status value=\"obsolete\">"EXT_SUBELEM"</status>" ELEMENT_WRAPPER_END;
1007 assert_int_equal(test_element_helper(st, &data, &flags, NULL, &exts, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001008 assert_true(flags & LYS_STATUS_OBSLT);
David Sedlákd1144562019-08-06 12:36:14 +02001009 assert_string_equal(exts[0].name, "myext:c-define");
1010 assert_int_equal(exts[0].insubstmt_index, 0);
1011 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_STATUS);
1012 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1013 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001014
1015 /* test invalid value */
1016 data = ELEMENT_WRAPPER_START "<status value=\"invalid\"></status>" ELEMENT_WRAPPER_END;
1017 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001018 logbuf_assert("Invalid value \"invalid\" of \"value\" attribute in \"status\" element. Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001019 st->finished_correctly = true;
1020}
1021
1022static void
1023test_ext_elem(void **state)
1024{
1025 struct state *st = *state;
1026 const char *data;
1027 struct lysp_ext *ext = NULL;
1028
1029 /* max subelems */
1030 data = ELEMENT_WRAPPER_START
1031 "<extension name=\"ext_name\">"
1032 "<argument name=\"arg\"></argument>"
1033 "<status value=\"current\"/>"
1034 "<description><text>ext_desc</text></description>"
1035 "<reference><text>ext_ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001036 EXT_SUBELEM
David Sedlák32488102019-07-15 17:44:10 +02001037 "</extension>"
1038 ELEMENT_WRAPPER_END;
1039 assert_int_equal(test_element_helper(st, &data, &ext, NULL, NULL, true), LY_SUCCESS);
1040 assert_string_equal(ext->name, "ext_name");
1041 assert_string_equal(ext->argument, "arg");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001042 assert_true(ext->flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +02001043 assert_string_equal(ext->dsc, "ext_desc");
1044 assert_string_equal(ext->ref, "ext_ref");
David Sedlákd1144562019-08-06 12:36:14 +02001045 assert_string_equal(ext->exts[0].name, "myext:c-define");
1046 assert_int_equal(ext->exts[0].insubstmt_index, 0);
1047 assert_int_equal(ext->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák32488102019-07-15 17:44:10 +02001048 lysp_ext_free(st->ctx, ext);
1049 LY_ARRAY_FREE(ext);
1050 ext = NULL;
1051
1052 /* min subelems */
1053 data = ELEMENT_WRAPPER_START "<extension name=\"ext_name\"></extension>" ELEMENT_WRAPPER_END;
1054 assert_int_equal(test_element_helper(st, &data, &ext, NULL, NULL, true), LY_SUCCESS);
1055 assert_string_equal(ext->name, "ext_name");
1056 lysp_ext_free(st->ctx, ext);
1057 LY_ARRAY_FREE(ext);
1058 ext = NULL;
1059
1060 st->finished_correctly = true;
1061}
1062
1063static void
1064test_yin_element_elem(void **state)
1065{
1066 struct state *st = *state;
1067 const char *data;
1068 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001069 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001070
1071 data = ELEMENT_WRAPPER_START "<yin-element value=\"true\" />" ELEMENT_WRAPPER_END;
1072 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001073 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +02001074
David Sedlákd1144562019-08-06 12:36:14 +02001075 data = ELEMENT_WRAPPER_START "<yin-element value=\"false\">" EXT_SUBELEM "</yin-element>" ELEMENT_WRAPPER_END;
1076 assert_int_equal(test_element_helper(st, &data, &flags, NULL, &exts, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001077 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlákd1144562019-08-06 12:36:14 +02001078 assert_string_equal(exts[0].name, "myext:c-define");
1079 assert_int_equal(exts[0].insubstmt_index, 0);
1080 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_YINELEM);
1081 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001082
1083 data = ELEMENT_WRAPPER_START "<yin-element value=\"invalid\" />" ELEMENT_WRAPPER_END;
1084 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001085 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák1538a842019-08-08 15:38:51 +02001086 logbuf_assert("Invalid value \"invalid\" of \"value\" attribute in \"yin-element\" element. Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001087 st->finished_correctly = true;
1088}
1089
1090static void
1091test_yangversion_elem(void **state)
1092{
1093 struct state *st = *state;
1094 const char *data;
1095 uint8_t version = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001096 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001097
1098 /* valid values */
1099 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.0\" />" ELEMENT_WRAPPER_END;
1100 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001101 assert_true(version & LYS_VERSION_1_0);
David Sedlák32488102019-07-15 17:44:10 +02001102 assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_0);
1103
David Sedlákd1144562019-08-06 12:36:14 +02001104 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.1\">" EXT_SUBELEM "</yang-version>" ELEMENT_WRAPPER_END;
1105 assert_int_equal(test_element_helper(st, &data, &version, NULL, &exts, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001106 assert_true(version & LYS_VERSION_1_1);
David Sedlák32488102019-07-15 17:44:10 +02001107 assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_1);
David Sedlákd1144562019-08-06 12:36:14 +02001108 assert_string_equal(exts[0].name, "myext:c-define");
1109 assert_int_equal(exts[0].insubstmt_index, 0);
1110 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_VERSION);
1111 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001112
1113 /* invalid value */
1114 data = ELEMENT_WRAPPER_START "<yang-version value=\"version\" />" ELEMENT_WRAPPER_END;
1115 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001116 logbuf_assert("Invalid value \"version\" of \"value\" attribute in \"yang-version\" element. Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001117
1118 st->finished_correctly = true;
1119}
1120
1121static void
1122test_mandatory_elem(void **state)
1123{
1124 struct state *st = *state;
1125 const char *data;
1126 uint16_t man = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001127 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001128
1129 /* valid values */
1130 data = ELEMENT_WRAPPER_START "<mandatory value=\"true\" />" ELEMENT_WRAPPER_END;
1131 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, true), LY_SUCCESS);
1132 assert_int_equal(man, LYS_MAND_TRUE);
1133 man = 0;
1134
David Sedlákd1144562019-08-06 12:36:14 +02001135 data = ELEMENT_WRAPPER_START "<mandatory value=\"false\">" EXT_SUBELEM "</mandatory>" ELEMENT_WRAPPER_END;
1136 assert_int_equal(test_element_helper(st, &data, &man, NULL, &exts, true), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001137 assert_int_equal(man, LYS_MAND_FALSE);
David Sedlákd1144562019-08-06 12:36:14 +02001138 assert_string_equal(exts[0].name, "myext:c-define");
1139 assert_int_equal(exts[0].insubstmt_index, 0);
1140 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_MANDATORY);
1141 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001142
1143 data = ELEMENT_WRAPPER_START "<mandatory value=\"invalid\" />" ELEMENT_WRAPPER_END;
1144 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001145 logbuf_assert("Invalid value \"invalid\" of \"value\" attribute in \"mandatory\" element. Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001146
1147 st->finished_correctly = true;
1148}
1149
David Sedlák8e7bda82019-07-16 17:57:50 +02001150static void
1151test_argument_elem(void **state)
1152{
1153 struct state *st = *state;
1154 const char *data;
1155 uint16_t flags = 0;
1156 const char *arg;
1157 struct yin_argument_meta arg_meta = {&flags, &arg};
David Sedlákd1144562019-08-06 12:36:14 +02001158 struct lysp_ext_instance *exts = NULL;
1159
David Sedlák8e7bda82019-07-16 17:57:50 +02001160 /* max subelems */
1161 data = ELEMENT_WRAPPER_START
1162 "<argument name=\"arg-name\">"
1163 "<yin-element value=\"true\" />"
David Sedlákd1144562019-08-06 12:36:14 +02001164 EXT_SUBELEM
David Sedlák8e7bda82019-07-16 17:57:50 +02001165 "</argument>"
1166 ELEMENT_WRAPPER_END;
David Sedlákd1144562019-08-06 12:36:14 +02001167 assert_int_equal(test_element_helper(st, &data, &arg_meta, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001168 assert_string_equal(arg, "arg-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001169 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlákd1144562019-08-06 12:36:14 +02001170 assert_string_equal(exts[0].name, "myext:c-define");
1171 assert_int_equal(exts[0].insubstmt_index, 0);
1172 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ARGUMENT);
1173 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1174 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001175 flags = 0;
1176 FREE_STRING(st->ctx, arg);
1177 arg = NULL;
1178
1179 /* min subelems */
1180 data = ELEMENT_WRAPPER_START
1181 "<argument name=\"arg\">"
1182 "</argument>"
1183 ELEMENT_WRAPPER_END;
1184 assert_int_equal(test_element_helper(st, &data, &arg_meta, NULL, NULL, true), LY_SUCCESS);
1185 assert_string_equal(arg, "arg");
1186 assert_true(flags == 0);
1187 FREE_STRING(st->ctx, arg);
1188
1189 st->finished_correctly = true;
1190}
1191
1192static void
1193test_base_elem(void **state)
1194{
1195 struct state *st = *state;
1196 const char *data;
1197 const char **bases = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001198 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001199 struct lysp_type type = {};
1200
1201 /* as identity subelement */
1202 data = "<identity xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlákd1144562019-08-06 12:36:14 +02001203 "<base name=\"base-name\">"
1204 EXT_SUBELEM
1205 "</base>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001206 "</identity>";
David Sedlákd1144562019-08-06 12:36:14 +02001207 assert_int_equal(test_element_helper(st, &data, &bases, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001208 assert_string_equal(*bases, "base-name");
David Sedlákd1144562019-08-06 12:36:14 +02001209 assert_string_equal(exts[0].name, "myext:c-define");
1210 assert_int_equal(exts[0].insubstmt_index, 0);
1211 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_BASE);
1212 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1213 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001214 FREE_STRING(st->ctx, *bases);
1215 LY_ARRAY_FREE(bases);
1216
1217 /* as type subelement */
1218 data = "<type xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlákd1144562019-08-06 12:36:14 +02001219 "<base name=\"base-name\">"
1220 EXT_SUBELEM
1221 "</base>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001222 "</type>";
David Sedlákd1144562019-08-06 12:36:14 +02001223 assert_int_equal(test_element_helper(st, &data, &type, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001224 assert_string_equal(*type.bases, "base-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001225 assert_true(type.flags & LYS_SET_BASE);
David Sedlákd1144562019-08-06 12:36:14 +02001226 assert_string_equal(exts[0].name, "myext:c-define");
1227 assert_int_equal(exts[0].insubstmt_index, 0);
1228 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_BASE);
1229 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1230 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001231 FREE_STRING(st->ctx, *type.bases);
1232 LY_ARRAY_FREE(type.bases);
1233
1234 st->finished_correctly = true;
1235}
1236
1237static void
1238test_belongsto_elem(void **state)
1239{
1240 struct state *st = *state;
1241 const char *data;
1242 struct lysp_submodule submod;
David Sedlákd1144562019-08-06 12:36:14 +02001243 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001244
1245 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +02001246 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/>"EXT_SUBELEM"</belongs-to>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001247 ELEMENT_WRAPPER_END;
David Sedlákd1144562019-08-06 12:36:14 +02001248 assert_int_equal(test_element_helper(st, &data, &submod, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001249 assert_string_equal(submod.belongsto, "module-name");
1250 assert_string_equal(submod.prefix, "pref");
David Sedlákd1144562019-08-06 12:36:14 +02001251 assert_string_equal(exts[0].name, "myext:c-define");
1252 assert_int_equal(exts[0].insubstmt_index, 0);
1253 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_BELONGSTO);
1254 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1255 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001256 FREE_STRING(st->ctx, submod.belongsto);
1257 FREE_STRING(st->ctx, submod.prefix);
1258
1259 data = ELEMENT_WRAPPER_START "<belongs-to module=\"module-name\"></belongs-to>" ELEMENT_WRAPPER_END;
1260 assert_int_equal(test_element_helper(st, &data, &submod, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001261 logbuf_assert("Missing mandatory sub-element \"prefix\" of \"belongs-to\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001262 FREE_STRING(st->ctx, submod.belongsto);
1263
1264 st->finished_correctly = true;
1265}
1266
1267static void
1268test_config_elem(void **state)
1269{
1270 struct state *st = *state;
1271 const char *data;
1272 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001273 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001274
David Sedlákd1144562019-08-06 12:36:14 +02001275 data = ELEMENT_WRAPPER_START "<config value=\"true\">" EXT_SUBELEM "</config>" ELEMENT_WRAPPER_END;
1276 assert_int_equal(test_element_helper(st, &data, &flags, NULL, &exts, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001277 assert_true(flags & LYS_CONFIG_W);
David Sedlákd1144562019-08-06 12:36:14 +02001278 assert_string_equal(exts[0].name, "myext:c-define");
1279 assert_int_equal(exts[0].insubstmt_index, 0);
1280 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_CONFIG);
1281 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1282 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001283 flags = 0;
1284
1285 data = ELEMENT_WRAPPER_START "<config value=\"false\"/>" ELEMENT_WRAPPER_END;
1286 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001287 assert_true(flags & LYS_CONFIG_R);
David Sedlák8e7bda82019-07-16 17:57:50 +02001288 flags = 0;
1289
1290 data = ELEMENT_WRAPPER_START "<config value=\"invalid\"/>" ELEMENT_WRAPPER_END;
1291 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001292 logbuf_assert("Invalid value \"invalid\" of \"value\" attribute in \"config\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001293
1294 st->finished_correctly = true;
1295}
1296
1297static void
1298test_default_elem(void **state)
1299{
1300 struct state *st = *state;
1301 const char *data;
1302 const char *val = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001303 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001304
David Sedlákd1144562019-08-06 12:36:14 +02001305 data = ELEMENT_WRAPPER_START "<default value=\"defaul-value\">"EXT_SUBELEM"</default>" ELEMENT_WRAPPER_END;
1306 assert_int_equal(test_element_helper(st, &data, &val, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001307 assert_string_equal(val, "defaul-value");
David Sedlákd1144562019-08-06 12:36:14 +02001308 assert_string_equal(exts[0].name, "myext:c-define");
1309 assert_int_equal(exts[0].insubstmt_index, 0);
1310 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_DEFAULT);
1311 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1312 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001313 FREE_STRING(st->ctx, val);
1314 val = NULL;
1315
1316 data = ELEMENT_WRAPPER_START "<default/>" ELEMENT_WRAPPER_END;
1317 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1318 logbuf_assert("Missing mandatory attribute value of default element. Line number 1.");
1319
1320 st->finished_correctly = true;
1321}
1322
1323static void
1324test_err_app_tag_elem(void **state)
1325{
1326 struct state *st = *state;
1327 const char *data;
1328 const char *val = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001329 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001330
David Sedlákd1144562019-08-06 12:36:14 +02001331 data = ELEMENT_WRAPPER_START "<error-app-tag value=\"val\">"EXT_SUBELEM"</error-app-tag>" ELEMENT_WRAPPER_END;
1332 assert_int_equal(test_element_helper(st, &data, &val, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001333 assert_string_equal(val, "val");
David Sedlákd1144562019-08-06 12:36:14 +02001334 assert_string_equal(exts[0].name, "myext:c-define");
1335 assert_int_equal(exts[0].insubstmt_index, 0);
1336 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ERRTAG);
1337 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1338 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001339 FREE_STRING(st->ctx, val);
1340 val = NULL;
1341
1342 data = ELEMENT_WRAPPER_START "<error-app-tag/>" ELEMENT_WRAPPER_END;
1343 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1344 logbuf_assert("Missing mandatory attribute value of error-app-tag element. Line number 1.");
1345
1346 st->finished_correctly = true;
1347}
1348
1349static void
1350test_err_msg_elem(void **state)
1351{
1352 struct state *st = *state;
1353 const char *data;
1354 const char *val = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001355 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001356
David Sedlákd1144562019-08-06 12:36:14 +02001357 data = ELEMENT_WRAPPER_START "<error-message><value>val</value>"EXT_SUBELEM"</error-message>" ELEMENT_WRAPPER_END;
1358 assert_int_equal(test_element_helper(st, &data, &val, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001359 assert_string_equal(val, "val");
David Sedlákd1144562019-08-06 12:36:14 +02001360 assert_string_equal(exts[0].name, "myext:c-define");
1361 assert_int_equal(exts[0].insubstmt_index, 0);
1362 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ERRMSG);
1363 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1364 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001365 FREE_STRING(st->ctx, val);
1366
1367 data = ELEMENT_WRAPPER_START "<error-message></error-message>" ELEMENT_WRAPPER_END;
1368 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001369 logbuf_assert("Missing mandatory sub-element \"value\" of \"error-message\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001370
David Sedlákdf2a9732019-08-07 13:23:16 +02001371 data = ELEMENT_WRAPPER_START "<error-message invalid=\"text\"/>" ELEMENT_WRAPPER_END;
1372 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001373 logbuf_assert("Unexpected attribute \"invalid\" of \"error-message\" element. Line number 1.");
David Sedlákdf2a9732019-08-07 13:23:16 +02001374
David Sedlák8e7bda82019-07-16 17:57:50 +02001375 st->finished_correctly = true;
1376}
1377
1378static void
1379test_fracdigits_elem(void **state)
1380{
1381 struct state *st = *state;
1382 const char *data;
1383 struct lysp_type type = {};
1384
1385 /* valid value */
David Sedlákd1144562019-08-06 12:36:14 +02001386 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"10\">"EXT_SUBELEM"</fraction-digits>" ELEMENT_WRAPPER_END;
David Sedlák8e7bda82019-07-16 17:57:50 +02001387 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001388 assert_string_equal(type.exts[0].name, "myext:c-define");
1389 assert_int_equal(type.exts[0].insubstmt_index, 0);
1390 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_FRACDIGITS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001391 assert_int_equal(type.fraction_digits, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001392 assert_true(type.flags & LYS_SET_FRDIGITS);
David Sedlákd1144562019-08-06 12:36:14 +02001393 FREE_ARRAY(st->ctx, type.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001394
1395 /* invalid values */
1396 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"-1\"></fraction-digits>" ELEMENT_WRAPPER_END;
1397 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001398 logbuf_assert("Invalid value \"-1\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001399
1400 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"02\"></fraction-digits>" ELEMENT_WRAPPER_END;
1401 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001402 logbuf_assert("Invalid value \"02\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001403
1404 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"1p\"></fraction-digits>" ELEMENT_WRAPPER_END;
1405 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001406 logbuf_assert("Invalid value \"1p\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001407
1408 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"19\"></fraction-digits>" ELEMENT_WRAPPER_END;
1409 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001410 logbuf_assert("Invalid value \"19\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001411
1412 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"999999999999999999\"></fraction-digits>" ELEMENT_WRAPPER_END;
1413 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001414 logbuf_assert("Invalid value \"999999999999999999\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001415
1416 st->finished_correctly = true;
1417}
1418
1419static void
1420test_iffeature_elem(void **state)
1421{
1422 struct state *st = *state;
1423 const char *data;
1424 const char **iffeatures = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001425 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001426
David Sedlákd1144562019-08-06 12:36:14 +02001427 data = ELEMENT_WRAPPER_START "<if-feature name=\"local-storage\">"EXT_SUBELEM"</if-feature>" ELEMENT_WRAPPER_END;
1428 assert_int_equal(test_element_helper(st, &data, &iffeatures, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001429 assert_string_equal(*iffeatures, "local-storage");
David Sedlákd1144562019-08-06 12:36:14 +02001430 assert_string_equal(exts[0].name, "myext:c-define");
1431 assert_int_equal(exts[0].insubstmt_index, 0);
1432 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_IFFEATURE);
1433 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1434 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001435 FREE_STRING(st->ctx, *iffeatures);
1436 LY_ARRAY_FREE(iffeatures);
1437 iffeatures = NULL;
1438
1439 data = ELEMENT_WRAPPER_START "<if-feature/>" ELEMENT_WRAPPER_END;
1440 assert_int_equal(test_element_helper(st, &data, &iffeatures, NULL, NULL, false), LY_EVALID);
1441 logbuf_assert("Missing mandatory attribute name of if-feature element. Line number 1.");
1442 LY_ARRAY_FREE(iffeatures);
1443 iffeatures = NULL;
1444
1445 st->finished_correctly = true;
1446}
1447
1448static void
1449test_length_elem(void **state)
1450{
1451 struct state *st = *state;
1452 const char *data;
1453 struct lysp_type type = {};
1454
1455 /* max subelems */
1456 data = ELEMENT_WRAPPER_START
1457 "<length value=\"length-str\">"
1458 "<error-message><value>err-msg</value></error-message>"
1459 "<error-app-tag value=\"err-app-tag\"/>"
1460 "<description><text>desc</text></description>"
1461 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001462 EXT_SUBELEM
David Sedlák8e7bda82019-07-16 17:57:50 +02001463 "</length>"
1464 ELEMENT_WRAPPER_END;
1465 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1466 assert_string_equal(type.length->arg, "length-str");
1467 assert_string_equal(type.length->emsg, "err-msg");
1468 assert_string_equal(type.length->eapptag, "err-app-tag");
1469 assert_string_equal(type.length->dsc, "desc");
1470 assert_string_equal(type.length->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001471 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlákd1144562019-08-06 12:36:14 +02001472 assert_string_equal(type.length->exts[0].name, "myext:c-define");
1473 assert_int_equal(type.length->exts[0].insubstmt_index, 0);
1474 assert_int_equal(type.length->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8e7bda82019-07-16 17:57:50 +02001475 lysp_type_free(st->ctx, &type);
1476 memset(&type, 0, sizeof(type));
1477
1478 /* min subelems */
1479 data = ELEMENT_WRAPPER_START
1480 "<length value=\"length-str\">"
1481 "</length>"
1482 ELEMENT_WRAPPER_END;
1483 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1484 assert_string_equal(type.length->arg, "length-str");
1485 lysp_type_free(st->ctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001486 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001487 memset(&type, 0, sizeof(type));
1488
1489 data = ELEMENT_WRAPPER_START "<length></length>" ELEMENT_WRAPPER_END;
1490 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1491 logbuf_assert("Missing mandatory attribute value of length element. Line number 1.");
1492 lysp_type_free(st->ctx, &type);
1493 memset(&type, 0, sizeof(type));
1494
1495 st->finished_correctly = true;
1496}
1497
1498static void
1499test_modifier_elem(void **state)
1500{
1501 struct state *st = *state;
1502 const char *data;
1503 const char *pat = lydict_insert(st->ctx, "\006pattern", 8);
David Sedlákd1144562019-08-06 12:36:14 +02001504 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001505
David Sedlákd1144562019-08-06 12:36:14 +02001506 data = ELEMENT_WRAPPER_START "<modifier value=\"invert-match\">" EXT_SUBELEM "</modifier>" ELEMENT_WRAPPER_END;
1507 assert_int_equal(test_element_helper(st, &data, &pat, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001508 assert_string_equal(pat, "\x015pattern");
David Sedlákd1144562019-08-06 12:36:14 +02001509 assert_string_equal(exts[0].name, "myext:c-define");
1510 assert_int_equal(exts[0].insubstmt_index, 0);
1511 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_MODIFIER);
1512 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1513 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001514 FREE_STRING(st->ctx, pat);
1515
1516 pat = lydict_insert(st->ctx, "\006pattern", 8);
1517 data = ELEMENT_WRAPPER_START "<modifier value=\"invert\" />" ELEMENT_WRAPPER_END;
1518 assert_int_equal(test_element_helper(st, &data, &pat, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001519 logbuf_assert("Invalid value \"invert\" of \"value\" attribute in \"modifier\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001520 FREE_STRING(st->ctx, pat);
1521
1522 st->finished_correctly = true;
1523}
1524
1525static void
1526test_namespace_elem(void **state)
1527{
1528 struct state *st = *state;
1529 const char *data;
1530 const char *ns;
David Sedlákd1144562019-08-06 12:36:14 +02001531 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001532
David Sedlákd1144562019-08-06 12:36:14 +02001533 data = ELEMENT_WRAPPER_START "<namespace uri=\"ns\">" EXT_SUBELEM "</namespace>" ELEMENT_WRAPPER_END;
1534 assert_int_equal(test_element_helper(st, &data, &ns, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001535 assert_string_equal(ns, "ns");
David Sedlákd1144562019-08-06 12:36:14 +02001536 assert_string_equal(exts[0].name, "myext:c-define");
1537 assert_int_equal(exts[0].insubstmt_index, 0);
1538 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_NAMESPACE);
1539 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1540 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001541 FREE_STRING(st->ctx, ns);
1542
1543 data = ELEMENT_WRAPPER_START "<namespace/>" ELEMENT_WRAPPER_END;
1544 assert_int_equal(test_element_helper(st, &data, &ns, NULL, NULL, false), LY_EVALID);
1545 logbuf_assert("Missing mandatory attribute uri of namespace element. Line number 1.");
1546
1547 st->finished_correctly = true;
1548}
1549
1550static void
1551test_path_elem(void **state)
1552{
1553 struct state *st = *state;
1554 const char *data;
1555 struct lysp_type type = {};
1556
David Sedlákd1144562019-08-06 12:36:14 +02001557 data = ELEMENT_WRAPPER_START "<path value=\"path-val\">" EXT_SUBELEM "</path>" ELEMENT_WRAPPER_END;
David Sedlák8e7bda82019-07-16 17:57:50 +02001558 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1559 assert_string_equal("path-val", type.path);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001560 assert_true(type.flags & LYS_SET_PATH);
David Sedlákd1144562019-08-06 12:36:14 +02001561 assert_string_equal(type.exts[0].name, "myext:c-define");
1562 assert_int_equal(type.exts[0].insubstmt_index, 0);
1563 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_PATH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001564 lysp_type_free(st->ctx, &type);
1565
1566 st->finished_correctly = true;
1567}
1568
1569static void
1570test_pattern_elem(void **state)
1571{
1572 struct state *st = *state;
1573 const char *data;
1574 struct lysp_type type = {};
1575
1576 /* max subelems */
1577 data = ELEMENT_WRAPPER_START
1578 "<pattern value=\"super_pattern\">"
1579 "<modifier value=\"invert-match\"/>"
1580 "<error-message><value>err-msg-value</value></error-message>"
1581 "<error-app-tag value=\"err-app-tag-value\"/>"
1582 "<description><text>pattern-desc</text></description>"
1583 "<reference><text>pattern-ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001584 EXT_SUBELEM
David Sedlák8e7bda82019-07-16 17:57:50 +02001585 "</pattern>"
1586 ELEMENT_WRAPPER_END;
1587 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001588 assert_true(type.flags & LYS_SET_PATTERN);
David Sedlák8e7bda82019-07-16 17:57:50 +02001589 assert_string_equal(type.patterns->arg, "\x015super_pattern");
1590 assert_string_equal(type.patterns->dsc, "pattern-desc");
1591 assert_string_equal(type.patterns->eapptag, "err-app-tag-value");
1592 assert_string_equal(type.patterns->emsg, "err-msg-value");
1593 assert_string_equal(type.patterns->dsc, "pattern-desc");
1594 assert_string_equal(type.patterns->ref, "pattern-ref");
David Sedlákd1144562019-08-06 12:36:14 +02001595 assert_string_equal(type.patterns->exts[0].name, "myext:c-define");
1596 assert_int_equal(type.patterns->exts[0].insubstmt_index, 0);
1597 assert_int_equal(type.patterns->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8e7bda82019-07-16 17:57:50 +02001598 lysp_type_free(st->ctx, &type);
1599 memset(&type, 0, sizeof(type));
1600
1601 /* min subelems */
1602 data = ELEMENT_WRAPPER_START "<pattern value=\"pattern\"> </pattern>" ELEMENT_WRAPPER_END;
1603 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1604 assert_string_equal(type.patterns->arg, "\x006pattern");
1605 lysp_type_free(st->ctx, &type);
1606 memset(&type, 0, sizeof(type));
1607
1608 st->finished_correctly = true;
1609}
1610
1611static void
1612test_value_position_elem(void **state)
1613{
1614 struct state *st = *state;
1615 const char *data;
1616 struct lysp_type_enum en = {};
1617
1618 /* valid values */
David Sedlákd1144562019-08-06 12:36:14 +02001619 data = ELEMENT_WRAPPER_START "<value value=\"55\">" EXT_SUBELEM "</value>" ELEMENT_WRAPPER_END;
David Sedlák8e7bda82019-07-16 17:57:50 +02001620 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1621 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001622 assert_true(en.flags & LYS_SET_VALUE);
David Sedlákd1144562019-08-06 12:36:14 +02001623 assert_string_equal(en.exts[0].name, "myext:c-define");
1624 assert_int_equal(en.exts[0].insubstmt_index, 0);
1625 assert_int_equal(en.exts[0].insubstmt, LYEXT_SUBSTMT_VALUE);
1626 FREE_ARRAY(st->ctx, en.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001627 memset(&en, 0, sizeof(en));
1628
1629 data = ELEMENT_WRAPPER_START "<value value=\"-55\"/>" ELEMENT_WRAPPER_END;
1630 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1631 assert_int_equal(en.value, -55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001632 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001633 memset(&en, 0, sizeof(en));
1634
1635 data = ELEMENT_WRAPPER_START "<value value=\"0\"/>" ELEMENT_WRAPPER_END;
1636 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1637 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001638 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001639 memset(&en, 0, sizeof(en));
1640
1641 data = ELEMENT_WRAPPER_START "<value value=\"-0\"/>" ELEMENT_WRAPPER_END;
1642 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1643 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001644 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001645 memset(&en, 0, sizeof(en));
1646
1647 /* valid positions */
David Sedlák8d552d62019-08-06 15:29:05 +02001648 data = ELEMENT_WRAPPER_START "<position value=\"55\">" EXT_SUBELEM "</position>" ELEMENT_WRAPPER_END;
David Sedlák8e7bda82019-07-16 17:57:50 +02001649 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1650 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001651 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8d552d62019-08-06 15:29:05 +02001652 assert_string_equal(en.exts[0].name, "myext:c-define");
1653 assert_int_equal(en.exts[0].insubstmt_index, 0);
1654 assert_int_equal(en.exts[0].insubstmt, LYEXT_SUBSTMT_POSITION);
1655 FREE_ARRAY(st->ctx, en.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001656 memset(&en, 0, sizeof(en));
1657
1658 data = ELEMENT_WRAPPER_START "<position value=\"0\" />" ELEMENT_WRAPPER_END;
1659 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1660 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001661 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001662 memset(&en, 0, sizeof(en));
1663
1664 /* invalid values */
1665 data = ELEMENT_WRAPPER_START "<value value=\"99999999999999999999999\"/>" ELEMENT_WRAPPER_END;
1666 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001667 logbuf_assert("Invalid value \"99999999999999999999999\" of \"value\" attribute in \"value\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001668
1669 data = ELEMENT_WRAPPER_START "<value value=\"1k\"/>" ELEMENT_WRAPPER_END;
1670 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001671 logbuf_assert("Invalid value \"1k\" of \"value\" attribute in \"value\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001672
David Sedlák69f01612019-07-17 11:41:08 +02001673 data = ELEMENT_WRAPPER_START "<value value=\"\"/>" ELEMENT_WRAPPER_END;
1674 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001675 logbuf_assert("Invalid value \"\" of \"value\" attribute in \"value\" element. Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001676
David Sedlák8e7bda82019-07-16 17:57:50 +02001677 /*invalid positions */
1678 data = ELEMENT_WRAPPER_START "<position value=\"-5\"/>" ELEMENT_WRAPPER_END;
1679 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001680 logbuf_assert("Invalid value \"-5\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001681
1682 data = ELEMENT_WRAPPER_START "<position value=\"-0\"/>" ELEMENT_WRAPPER_END;
1683 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001684 logbuf_assert("Invalid value \"-0\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001685
1686 data = ELEMENT_WRAPPER_START "<position value=\"99999999999999999999\"/>" ELEMENT_WRAPPER_END;
1687 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001688 logbuf_assert("Invalid value \"99999999999999999999\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001689
David Sedlák69f01612019-07-17 11:41:08 +02001690 data = ELEMENT_WRAPPER_START "<position value=\"\"/>" ELEMENT_WRAPPER_END;
1691 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001692 logbuf_assert("Invalid value \"\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001693
1694 st->finished_correctly = true;
1695}
1696
1697static void
1698test_prefix_elem(void **state)
1699{
1700 struct state *st = *state;
1701 const char *data;
1702 const char *value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001703 struct lysp_ext_instance *exts = NULL;
1704
1705 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\">" EXT_SUBELEM "</prefix>" ELEMENT_WRAPPER_END;
1706 assert_int_equal(test_element_helper(st, &data, &value, NULL, &exts, true), LY_SUCCESS);
1707 assert_string_equal(value, "pref");
1708 assert_string_equal(exts[0].name, "myext:c-define");
1709 assert_int_equal(exts[0].insubstmt_index, 0);
1710 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_PREFIX);
1711 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1712 exts = NULL;
1713 FREE_STRING(st->ctx, value);
David Sedlák69f01612019-07-17 11:41:08 +02001714
1715 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\"/>" ELEMENT_WRAPPER_END;
1716 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
1717 assert_string_equal(value, "pref");
1718 FREE_STRING(st->ctx, value);
1719
1720 st->finished_correctly = true;
1721}
1722
1723static void
1724test_range_elem(void **state)
1725{
1726 struct state *st = *state;
1727 const char *data;
1728 struct lysp_type type = {};
1729
1730 /* max subelems */
1731 data = ELEMENT_WRAPPER_START
1732 "<range value=\"range-str\">"
1733 "<error-message><value>err-msg</value></error-message>"
1734 "<error-app-tag value=\"err-app-tag\" />"
1735 "<description><text>desc</text></description>"
1736 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001737 EXT_SUBELEM
David Sedlák69f01612019-07-17 11:41:08 +02001738 "</range>"
1739 ELEMENT_WRAPPER_END;
1740 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1741 assert_string_equal(type.range->arg, "range-str");
1742 assert_string_equal(type.range->dsc, "desc");
1743 assert_string_equal(type.range->eapptag, "err-app-tag");
1744 assert_string_equal(type.range->emsg, "err-msg");
1745 assert_string_equal(type.range->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001746 assert_true(type.flags & LYS_SET_RANGE);
David Sedlákd1144562019-08-06 12:36:14 +02001747 assert_string_equal(type.range->exts[0].name, "myext:c-define");
1748 assert_int_equal(type.range->exts[0].insubstmt_index, 0);
1749 assert_int_equal(type.range->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák69f01612019-07-17 11:41:08 +02001750 lysp_type_free(st->ctx, &type);
1751 memset(&type, 0, sizeof(type));
1752
1753 /* min subelems */
1754 data = ELEMENT_WRAPPER_START "<range value=\"range-str\"/>" ELEMENT_WRAPPER_END;
1755 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1756 assert_string_equal(type.range->arg, "range-str");
1757 lysp_type_free(st->ctx, &type);
1758 memset(&type, 0, sizeof(type));
1759
1760 st->finished_correctly = true;
1761}
1762
1763static void
1764test_reqinstance_elem(void **state)
1765{
1766 struct state *st = *state;
1767 const char *data;
1768 struct lysp_type type = {};
1769
David Sedlákd1144562019-08-06 12:36:14 +02001770 data = ELEMENT_WRAPPER_START "<require-instance value=\"true\">" EXT_SUBELEM "</require-instance>" ELEMENT_WRAPPER_END;
David Sedlák69f01612019-07-17 11:41:08 +02001771 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1772 assert_int_equal(type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001773 assert_true(type.flags & LYS_SET_REQINST);
David Sedlákd1144562019-08-06 12:36:14 +02001774 assert_string_equal(type.exts[0].name, "myext:c-define");
1775 assert_int_equal(type.exts[0].insubstmt_index, 0);
1776 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_REQINSTANCE);
1777 lysp_type_free(st->ctx, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001778 memset(&type, 0, sizeof(type));
1779
1780 data = ELEMENT_WRAPPER_START "<require-instance value=\"false\"/>" ELEMENT_WRAPPER_END;
1781 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1782 assert_int_equal(type.require_instance, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001783 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001784 memset(&type, 0, sizeof(type));
1785
1786 data = ELEMENT_WRAPPER_START "<require-instance value=\"invalid\"/>" ELEMENT_WRAPPER_END;
1787 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1788 memset(&type, 0, sizeof(type));
David Sedlák1538a842019-08-08 15:38:51 +02001789 logbuf_assert("Invalid value \"invalid\" of \"value\" attribute in \"require-instance\" element. Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001790
1791 st->finished_correctly = true;
1792}
1793
1794static void
1795test_revision_date_elem(void **state)
1796{
1797 struct state *st = *state;
1798 const char *data;
1799 char rev[LY_REV_SIZE];
David Sedlákd1144562019-08-06 12:36:14 +02001800 struct lysp_ext_instance *exts = NULL;
1801
1802 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\">"EXT_SUBELEM"</revision-date>" ELEMENT_WRAPPER_END;
1803 assert_int_equal(test_element_helper(st, &data, rev, NULL, &exts, true), LY_SUCCESS);
1804 assert_string_equal(rev, "2000-01-01");
1805 assert_string_equal(exts[0].name, "myext:c-define");
1806 assert_int_equal(exts[0].insubstmt_index, 0);
1807 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_REVISIONDATE);
1808 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák69f01612019-07-17 11:41:08 +02001809
1810 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\"/>" ELEMENT_WRAPPER_END;
1811 assert_int_equal(test_element_helper(st, &data, rev, NULL, NULL, true), LY_SUCCESS);
1812 assert_string_equal(rev, "2000-01-01");
1813
1814 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-50-05\"/>" ELEMENT_WRAPPER_END;
1815 assert_int_equal(test_element_helper(st, &data, rev, NULL, NULL, false), LY_EVALID);
1816 logbuf_assert("Invalid value \"2000-50-05\" of \"revision-date\". Line number 1.");
1817
1818 st->finished_correctly = true;
1819}
1820
1821static void
1822test_unique_elem(void **state)
1823{
1824 struct state *st = *state;
1825 const char *data;
1826 const char **values = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001827 struct lysp_ext_instance *exts = NULL;
1828
1829 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\">"EXT_SUBELEM"</unique>" ELEMENT_WRAPPER_END;
1830 assert_int_equal(test_element_helper(st, &data, &values, NULL, &exts, true), LY_SUCCESS);
1831 assert_string_equal(*values, "tag");
1832 assert_string_equal(exts[0].name, "myext:c-define");
1833 assert_int_equal(exts[0].insubstmt_index, 0);
1834 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_UNIQUE);
1835 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1836 FREE_STRING(st->ctx, *values);
1837 LY_ARRAY_FREE(values);
1838 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001839
1840 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\"/>" ELEMENT_WRAPPER_END;
1841 assert_int_equal(test_element_helper(st, &data, &values, NULL, NULL, true), LY_SUCCESS);
1842 assert_string_equal(*values, "tag");
1843 FREE_STRING(st->ctx, *values);
1844 LY_ARRAY_FREE(values);
David Sedlákd1144562019-08-06 12:36:14 +02001845 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001846
1847 st->finished_correctly = true;
1848}
1849
1850static void
1851test_units_elem(void **state)
1852{
1853 struct state *st = *state;
1854 const char *data;
1855 const char *values = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001856 struct lysp_ext_instance *exts = NULL;
1857
1858 data = ELEMENT_WRAPPER_START "<units name=\"name\">"EXT_SUBELEM"</units>" ELEMENT_WRAPPER_END;
1859 assert_int_equal(test_element_helper(st, &data, &values, NULL, &exts, true), LY_SUCCESS);
1860 assert_string_equal(values, "name");
1861 assert_string_equal(exts[0].name, "myext:c-define");
1862 assert_int_equal(exts[0].insubstmt_index, 0);
1863 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_UNITS);
1864 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1865 FREE_STRING(st->ctx, values);
1866 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001867
1868 data = ELEMENT_WRAPPER_START "<units name=\"name\"/>" ELEMENT_WRAPPER_END;
1869 assert_int_equal(test_element_helper(st, &data, &values, NULL, NULL, true), LY_SUCCESS);
1870 assert_string_equal(values, "name");
1871 FREE_STRING(st->ctx, values);
David Sedlákd1144562019-08-06 12:36:14 +02001872 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001873
1874 st->finished_correctly = true;
1875}
1876
1877static void
1878test_when_elem(void **state)
1879{
1880 struct state *st = *state;
1881 const char *data;
1882 struct lysp_when *when = NULL;
1883
1884 data = ELEMENT_WRAPPER_START
1885 "<when condition=\"cond\">"
1886 "<description><text>desc</text></description>"
1887 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001888 EXT_SUBELEM
David Sedlák69f01612019-07-17 11:41:08 +02001889 "</when>"
1890 ELEMENT_WRAPPER_END;
1891 assert_int_equal(test_element_helper(st, &data, &when, NULL, NULL, true), LY_SUCCESS);
1892 assert_string_equal(when->cond, "cond");
1893 assert_string_equal(when->dsc, "desc");
1894 assert_string_equal(when->ref, "ref");
David Sedlákd1144562019-08-06 12:36:14 +02001895 assert_string_equal(when->exts[0].name, "myext:c-define");
1896 assert_int_equal(when->exts[0].insubstmt_index, 0);
1897 assert_int_equal(when->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák69f01612019-07-17 11:41:08 +02001898 lysp_when_free(st->ctx, when);
1899 free(when);
1900 when = NULL;
1901
1902 data = ELEMENT_WRAPPER_START "<when condition=\"cond\" />" ELEMENT_WRAPPER_END;
1903 assert_int_equal(test_element_helper(st, &data, &when, NULL, NULL, true), LY_SUCCESS);
1904 assert_string_equal(when->cond, "cond");
1905 lysp_when_free(st->ctx, when);
1906 free(when);
1907 when = NULL;
1908
1909 st->finished_correctly = true;
1910}
1911
1912static void
1913test_yin_text_value_elem(void **state)
1914{
1915 struct state *st = *state;
1916 const char *data;
1917 const char *val;
1918
1919 data = ELEMENT_WRAPPER_START "<text>text</text>" ELEMENT_WRAPPER_END;
1920 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1921 assert_string_equal(val, "text");
1922 FREE_STRING(st->ctx, val);
1923
1924 data = "<error-message xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <value>text</value> </error-message>";
1925 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1926 assert_string_equal(val, "text");
1927 FREE_STRING(st->ctx, val);
1928
1929 data = ELEMENT_WRAPPER_START "<text></text>" ELEMENT_WRAPPER_END;
1930 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1931 assert_string_equal("", val);
1932 FREE_STRING(st->ctx, val);
1933
David Sedlák8e7bda82019-07-16 17:57:50 +02001934 st->finished_correctly = true;
1935}
David Sedlák32488102019-07-15 17:44:10 +02001936
David Sedlák374d2b32019-07-17 15:06:55 +02001937static void
1938test_type_elem(void **state)
1939{
1940 struct state *st = *state;
1941 const char *data;
1942 struct lysp_type type = {};
1943
1944 /* max subelems */
1945 data = ELEMENT_WRAPPER_START
1946 "<type name=\"type-name\">"
1947 "<base name=\"base-name\"/>"
1948 "<bit name=\"bit\"/>"
1949 "<enum name=\"enum\"/>"
1950 "<fraction-digits value=\"2\"/>"
1951 "<length value=\"length\"/>"
1952 "<path value=\"path\"/>"
1953 "<pattern value=\"pattern\"/>"
1954 "<range value=\"range\" />"
1955 "<require-instance value=\"true\"/>"
1956 "<type name=\"sub-type-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02001957 EXT_SUBELEM
David Sedlák374d2b32019-07-17 15:06:55 +02001958 "</type>"
1959 ELEMENT_WRAPPER_END;
1960 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1961 assert_string_equal(type.name, "type-name");
1962 assert_string_equal(*type.bases, "base-name");
1963 assert_string_equal(type.bits->name, "bit");
1964 assert_string_equal(type.enums->name, "enum");
1965 assert_int_equal(type.fraction_digits, 2);
1966 assert_string_equal(type.length->arg, "length");
1967 assert_string_equal(type.path, "path");
1968 assert_string_equal(type.patterns->arg, "\006pattern");
1969 assert_string_equal(type.range->arg, "range");
1970 assert_int_equal(type.require_instance, 1);
1971 assert_string_equal(type.types->name, "sub-type-name");
David Sedlákd1144562019-08-06 12:36:14 +02001972 assert_string_equal(type.exts[0].name, "myext:c-define");
1973 assert_int_equal(type.exts[0].insubstmt_index, 0);
1974 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001975 assert_true(type.flags & LYS_SET_BASE);
1976 assert_true(type.flags & LYS_SET_BIT);
1977 assert_true(type.flags & LYS_SET_ENUM);
1978 assert_true(type.flags & LYS_SET_FRDIGITS);
1979 assert_true(type.flags & LYS_SET_LENGTH);
1980 assert_true(type.flags & LYS_SET_PATH);
1981 assert_true(type.flags & LYS_SET_PATTERN);
1982 assert_true(type.flags & LYS_SET_RANGE);
1983 assert_true(type.flags & LYS_SET_REQINST);
1984 assert_true(type.flags & LYS_SET_TYPE);
David Sedlákd1144562019-08-06 12:36:14 +02001985 lysp_type_free(st->ctx, &type);
David Sedlák374d2b32019-07-17 15:06:55 +02001986 memset(&type, 0, sizeof(type));
1987
1988 /* min subelems */
1989 data = ELEMENT_WRAPPER_START "<type name=\"type-name\"/>" ELEMENT_WRAPPER_END;
1990 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1991 lysp_type_free(st->ctx, &type);
1992 memset(&type, 0, sizeof(type));
1993
1994 st->finished_correctly = true;
1995}
1996
David Sedlák1af868e2019-07-17 17:03:14 +02001997static void
1998test_max_elems_elem(void **state)
1999{
2000 struct state *st = *state;
2001 const char *data;
2002 struct lysp_node_list list = {};
2003 struct lysp_node_leaflist llist = {};
2004 struct lysp_refine refine = {};
2005
David Sedlákd1144562019-08-06 12:36:14 +02002006 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"unbounded\">"EXT_SUBELEM"</max-elements> </refine>";
David Sedlák1af868e2019-07-17 17:03:14 +02002007 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
2008 assert_int_equal(refine.max, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002009 assert_true(refine.flags & LYS_SET_MAX);
David Sedlákd1144562019-08-06 12:36:14 +02002010 assert_string_equal(refine.exts[0].name, "myext:c-define");
2011 assert_int_equal(refine.exts[0].insubstmt_index, 0);
2012 assert_int_equal(refine.exts[0].insubstmt, LYEXT_SUBSTMT_MAX);
2013 FREE_ARRAY(st->ctx, refine.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02002014
David Sedlákd1144562019-08-06 12:36:14 +02002015 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"5\">"EXT_SUBELEM"</max-elements> </list>";
David Sedlák1af868e2019-07-17 17:03:14 +02002016 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, true), LY_SUCCESS);
2017 assert_int_equal(list.max, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002018 assert_true(list.flags & LYS_SET_MAX);
David Sedlákd1144562019-08-06 12:36:14 +02002019 assert_string_equal(list.exts[0].name, "myext:c-define");
2020 assert_int_equal(list.exts[0].insubstmt_index, 0);
2021 assert_int_equal(list.exts[0].insubstmt, LYEXT_SUBSTMT_MAX);
2022 FREE_ARRAY(st->ctx, list.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02002023
David Sedlákd1144562019-08-06 12:36:14 +02002024 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"85\">"EXT_SUBELEM"</max-elements> </leaf-list>";
David Sedlák1af868e2019-07-17 17:03:14 +02002025 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, true), LY_SUCCESS);
2026 assert_int_equal(llist.max, 85);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002027 assert_true(llist.flags & LYS_SET_MAX);
David Sedlákd1144562019-08-06 12:36:14 +02002028 assert_string_equal(llist.exts[0].name, "myext:c-define");
2029 assert_int_equal(llist.exts[0].insubstmt_index, 0);
2030 assert_int_equal(llist.exts[0].insubstmt, LYEXT_SUBSTMT_MAX);
2031 FREE_ARRAY(st->ctx, llist.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02002032
2033 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"10\"/> </refine>";
2034 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
2035 assert_int_equal(refine.max, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002036 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02002037
2038 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"0\"/> </list>";
2039 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002040 logbuf_assert("Invalid value \"0\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002041
2042 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"-10\"/> </list>";
2043 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002044 logbuf_assert("Invalid value \"-10\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002045
2046 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"k\"/> </list>";
2047 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002048 logbuf_assert("Invalid value \"k\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002049
2050 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"u12\"/> </list>";
2051 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002052 logbuf_assert("Invalid value \"u12\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002053
2054 st->finished_correctly = true;
2055}
2056
David Sedlák09e18c92019-07-18 11:17:11 +02002057static void
2058test_min_elems_elem(void **state)
2059{
2060 struct state *st = *state;
2061 const char *data;
2062 struct lysp_node_list list = {};
2063 struct lysp_node_leaflist llist = {};
2064 struct lysp_refine refine = {};
2065
David Sedlákd1144562019-08-06 12:36:14 +02002066 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"0\">"EXT_SUBELEM"</min-elements> </refine>";
David Sedlák09e18c92019-07-18 11:17:11 +02002067 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
2068 assert_int_equal(refine.min, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002069 assert_true(refine.flags & LYS_SET_MIN);
David Sedlákd1144562019-08-06 12:36:14 +02002070 assert_string_equal(refine.exts[0].name, "myext:c-define");
2071 assert_int_equal(refine.exts[0].insubstmt_index, 0);
2072 assert_int_equal(refine.exts[0].insubstmt, LYEXT_SUBSTMT_MIN);
2073 FREE_ARRAY(st->ctx, refine.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02002074
David Sedlákd1144562019-08-06 12:36:14 +02002075 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"41\">"EXT_SUBELEM"</min-elements> </list>";
David Sedlák09e18c92019-07-18 11:17:11 +02002076 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, true), LY_SUCCESS);
2077 assert_int_equal(list.min, 41);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002078 assert_true(list.flags & LYS_SET_MIN);
David Sedlákd1144562019-08-06 12:36:14 +02002079 assert_string_equal(list.exts[0].name, "myext:c-define");
2080 assert_int_equal(list.exts[0].insubstmt_index, 0);
2081 assert_int_equal(list.exts[0].insubstmt, LYEXT_SUBSTMT_MIN);
2082 FREE_ARRAY(st->ctx, list.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02002083
David Sedlákd1144562019-08-06 12:36:14 +02002084 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"50\">"EXT_SUBELEM"</min-elements> </leaf-list>";
David Sedlák09e18c92019-07-18 11:17:11 +02002085 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, true), LY_SUCCESS);
2086 assert_int_equal(llist.min, 50);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002087 assert_true(llist.flags & LYS_SET_MIN);
David Sedlákd1144562019-08-06 12:36:14 +02002088 assert_string_equal(llist.exts[0].name, "myext:c-define");
2089 assert_int_equal(llist.exts[0].insubstmt_index, 0);
2090 assert_int_equal(llist.exts[0].insubstmt, LYEXT_SUBSTMT_MIN);
2091 FREE_ARRAY(st->ctx, llist.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02002092
2093 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"-5\"/> </leaf-list>";
2094 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002095 logbuf_assert("Value \"-5\" of \"value\" attribute in \"min-elements\" element is out of bounds. Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02002096
2097 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"99999999999999999\"/> </leaf-list>";
2098 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002099 logbuf_assert("Value \"99999999999999999\" of \"value\" attribute in \"min-elements\" element is out of bounds. Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02002100
2101 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"5k\"/> </leaf-list>";
2102 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002103 logbuf_assert("Invalid value \"5k\" of \"value\" attribute in \"min-elements\" element. Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02002104
2105 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"05\"/> </leaf-list>";
2106 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002107 logbuf_assert("Invalid value \"05\" of \"value\" attribute in \"min-elements\" element. Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02002108
2109 st->finished_correctly = true;
2110}
2111
David Sedláka2dad212019-07-18 12:45:19 +02002112static void
2113test_ordby_elem(void **state)
2114{
2115 struct state *st = *state;
2116 const char *data;
2117 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02002118 struct lysp_ext_instance *exts = NULL;
David Sedláka2dad212019-07-18 12:45:19 +02002119
David Sedlákd1144562019-08-06 12:36:14 +02002120 data = ELEMENT_WRAPPER_START "<ordered-by value=\"system\">"EXT_SUBELEM"</ordered-by>" ELEMENT_WRAPPER_END;
2121 assert_int_equal(test_element_helper(st, &data, &flags, NULL, &exts, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002122 assert_true(flags & LYS_ORDBY_SYSTEM);
David Sedlákd1144562019-08-06 12:36:14 +02002123 assert_string_equal(exts[0].name, "myext:c-define");
2124 assert_int_equal(exts[0].insubstmt_index, 0);
2125 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ORDEREDBY);
2126 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedláka2dad212019-07-18 12:45:19 +02002127
2128 data = ELEMENT_WRAPPER_START "<ordered-by value=\"user\"/>" ELEMENT_WRAPPER_END;
2129 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002130 assert_true(flags & LYS_ORDBY_USER);
David Sedláka2dad212019-07-18 12:45:19 +02002131
2132 data = ELEMENT_WRAPPER_START "<ordered-by value=\"inv\"/>" ELEMENT_WRAPPER_END;
2133 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002134 logbuf_assert("Invalid value \"inv\" of \"value\" attribute in \"ordered-by\" element. Line number 1.");
David Sedláka2dad212019-07-18 12:45:19 +02002135
2136 st->finished_correctly = true;
2137}
2138
David Sedlák8a83bbb2019-07-18 14:46:00 +02002139static void
2140test_any_elem(void **state)
2141{
2142 struct state *st = *state;
2143 const char *data;
2144 struct lysp_node *siblings = NULL;
2145 struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
2146 struct lysp_node_anydata *parsed = NULL;
2147
2148 /* anyxml max subelems */
2149 data = ELEMENT_WRAPPER_START
2150 "<anyxml name=\"any-name\">"
2151 "<config value=\"true\" />"
2152 "<description><text>desc</text></description>"
2153 "<if-feature name=\"feature\" />"
2154 "<mandatory value=\"true\" />"
2155 "<must condition=\"must-cond\" />"
2156 "<reference><text>ref</text></reference>"
2157 "<status value=\"deprecated\"/>"
2158 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002159 EXT_SUBELEM
David Sedlák8a83bbb2019-07-18 14:46:00 +02002160 "</anyxml>"
2161 ELEMENT_WRAPPER_END;
2162 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2163 parsed = (struct lysp_node_anydata *)siblings;
2164 assert_null(parsed->parent);
2165 assert_int_equal(parsed->nodetype, LYS_ANYXML);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002166 assert_true(parsed->flags & LYS_CONFIG_W);
2167 assert_true(parsed->flags & LYS_MAND_TRUE);
2168 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002169 assert_null(parsed->next);
2170 assert_string_equal(parsed->name, "any-name");
2171 assert_string_equal(parsed->dsc, "desc");
2172 assert_string_equal(parsed->ref, "ref");
2173 assert_string_equal(parsed->when->cond, "when-cond");
2174 assert_string_equal(*parsed->iffeatures, "feature");
David Sedlákd1144562019-08-06 12:36:14 +02002175 assert_string_equal(parsed->exts[0].name, "myext:c-define");
2176 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2177 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002178 lysp_node_free(st->ctx, siblings);
2179 siblings = NULL;
2180
2181 /* anydata max subelems */
2182 data = ELEMENT_WRAPPER_START
2183 "<anydata name=\"any-name\">"
2184 "<config value=\"true\" />"
2185 "<description><text>desc</text></description>"
2186 "<if-feature name=\"feature\" />"
2187 "<mandatory value=\"true\" />"
2188 "<must condition=\"must-cond\" />"
2189 "<reference><text>ref</text></reference>"
2190 "<status value=\"deprecated\"/>"
2191 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002192 EXT_SUBELEM
David Sedlák8a83bbb2019-07-18 14:46:00 +02002193 "</anydata>"
2194 ELEMENT_WRAPPER_END;
2195 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2196 parsed = (struct lysp_node_anydata *)siblings;
2197 assert_null(parsed->parent);
2198 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002199 assert_true(parsed->flags & LYS_CONFIG_W);
2200 assert_true(parsed->flags & LYS_MAND_TRUE);
2201 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002202 assert_null(parsed->next);
2203 assert_string_equal(parsed->name, "any-name");
2204 assert_string_equal(parsed->dsc, "desc");
2205 assert_string_equal(parsed->ref, "ref");
2206 assert_string_equal(parsed->when->cond, "when-cond");
2207 assert_string_equal(*parsed->iffeatures, "feature");
David Sedlákd1144562019-08-06 12:36:14 +02002208 assert_string_equal(parsed->exts[0].name, "myext:c-define");
2209 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2210 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002211 lysp_node_free(st->ctx, siblings);
2212 siblings = NULL;
2213
2214 /* min subelems */
2215 node_meta.parent = (void *)0x10;
2216 data = ELEMENT_WRAPPER_START "<anydata name=\"any-name\"> </anydata>" ELEMENT_WRAPPER_END;
2217 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2218 parsed = (struct lysp_node_anydata *)siblings;
2219 assert_ptr_equal(parsed->parent, node_meta.parent);
2220 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
2221 assert_null(parsed->next);
2222 assert_null(parsed->exts);
2223 lysp_node_free(st->ctx, siblings);
2224
2225 st->finished_correctly = true;
2226}
2227
David Sedlák203ca3a2019-07-18 15:26:25 +02002228static void
2229test_leaf_elem(void **state)
2230{
2231 struct state *st = *state;
2232 const char *data;
2233 struct lysp_node *siblings = NULL;
2234 struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
2235 struct lysp_node_leaf *parsed = NULL;
2236
2237 /* max elements */
2238 data = ELEMENT_WRAPPER_START
2239 "<leaf name=\"leaf\">"
2240 "<config value=\"true\" />"
2241 "<default value=\"def-val\"/>"
2242 "<description><text>desc</text></description>"
2243 "<if-feature name=\"feature\" />"
2244 "<mandatory value=\"true\" />"
2245 "<must condition=\"must-cond\" />"
2246 "<reference><text>ref</text></reference>"
2247 "<status value=\"deprecated\"/>"
2248 "<type name=\"type\"/>"
2249 "<units name=\"uni\"/>"
2250 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002251 EXT_SUBELEM
David Sedlák203ca3a2019-07-18 15:26:25 +02002252 "</leaf>"
2253 ELEMENT_WRAPPER_END;
2254 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2255 parsed = (struct lysp_node_leaf *)siblings;
2256 assert_null(parsed->parent);
2257 assert_int_equal(parsed->nodetype, LYS_LEAF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002258 assert_true(parsed->flags & LYS_CONFIG_W);
2259 assert_true(parsed->flags & LYS_MAND_TRUE);
2260 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák203ca3a2019-07-18 15:26:25 +02002261 assert_null(parsed->next);
2262 assert_string_equal(parsed->name, "leaf");
2263 assert_string_equal(parsed->dsc, "desc");
2264 assert_string_equal(parsed->ref, "ref");
2265 assert_string_equal(parsed->when->cond, "when-cond");
2266 assert_string_equal(*parsed->iffeatures, "feature");
David Sedlákd1144562019-08-06 12:36:14 +02002267 assert_string_equal(parsed->exts[0].name, "myext:c-define");
2268 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2269 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák203ca3a2019-07-18 15:26:25 +02002270 assert_string_equal(parsed->musts->arg, "must-cond");
2271 assert_string_equal(parsed->type.name, "type");
2272 assert_string_equal(parsed->units, "uni");
2273 assert_string_equal(parsed->dflt, "def-val");
2274 lysp_node_free(st->ctx, siblings);
2275 siblings = NULL;
2276
2277 /* min elements */
2278 data = ELEMENT_WRAPPER_START "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>" ELEMENT_WRAPPER_END;
2279 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2280 parsed = (struct lysp_node_leaf *)siblings;
2281 assert_string_equal(parsed->name, "leaf");
2282 assert_string_equal(parsed->type.name, "type");
2283 lysp_node_free(st->ctx, siblings);
2284 siblings = NULL;
2285
2286 st->finished_correctly = true;
2287}
2288
David Sedlákc3da3ef2019-07-19 12:56:08 +02002289static void
2290test_leaf_list_elem(void **state)
2291{
2292 struct state *st = *state;
2293 const char *data;
2294 struct lysp_node *siblings = NULL;
2295 struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
2296 struct lysp_node_leaflist *parsed = NULL;
2297
2298 data = ELEMENT_WRAPPER_START
2299 "<leaf-list name=\"llist\">"
2300 "<config value=\"true\" />"
2301 "<default value=\"def-val0\"/>"
2302 "<default value=\"def-val1\"/>"
2303 "<description><text>desc</text></description>"
2304 "<if-feature name=\"feature\"/>"
2305 "<max-elements value=\"5\"/>"
2306 "<must condition=\"must-cond\"/>"
2307 "<ordered-by value=\"user\" />"
2308 "<reference><text>ref</text></reference>"
2309 "<status value=\"current\"/>"
2310 "<type name=\"type\"/>"
2311 "<units name=\"uni\"/>"
2312 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002313 EXT_SUBELEM
David Sedlákc3da3ef2019-07-19 12:56:08 +02002314 "</leaf-list>"
2315 ELEMENT_WRAPPER_END;
2316 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2317 parsed = (struct lysp_node_leaflist *)siblings;
2318 assert_string_equal(parsed->dflts[0], "def-val0");
2319 assert_string_equal(parsed->dflts[1], "def-val1");
2320 assert_string_equal(parsed->dsc, "desc");
2321 assert_string_equal(*parsed->iffeatures, "feature");
2322 assert_int_equal(parsed->max, 5);
2323 assert_string_equal(parsed->musts->arg, "must-cond");
2324 assert_string_equal(parsed->name, "llist");
2325 assert_null(parsed->next);
2326 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2327 assert_null(parsed->parent);
2328 assert_string_equal(parsed->ref, "ref");
2329 assert_string_equal(parsed->type.name, "type");
2330 assert_string_equal(parsed->units, "uni");
2331 assert_string_equal(parsed->when->cond, "when-cond");
2332 assert_true(parsed->flags & LYS_CONFIG_W);
2333 assert_true(parsed->flags & LYS_ORDBY_USER);
2334 assert_true(parsed->flags & LYS_STATUS_CURR);
David Sedlákd1144562019-08-06 12:36:14 +02002335 assert_string_equal(parsed->exts[0].name, "myext:c-define");
2336 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2337 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002338 lysp_node_free(st->ctx, siblings);
2339 siblings = NULL;
2340
2341 data = ELEMENT_WRAPPER_START
2342 "<leaf-list name=\"llist\">"
2343 "<config value=\"true\" />"
2344 "<description><text>desc</text></description>"
2345 "<if-feature name=\"feature\"/>"
2346 "<min-elements value=\"5\"/>"
2347 "<must condition=\"must-cond\"/>"
2348 "<ordered-by value=\"user\" />"
2349 "<reference><text>ref</text></reference>"
2350 "<status value=\"current\"/>"
2351 "<type name=\"type\"/>"
2352 "<units name=\"uni\"/>"
2353 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002354 EXT_SUBELEM
David Sedlákc3da3ef2019-07-19 12:56:08 +02002355 "</leaf-list>"
2356 ELEMENT_WRAPPER_END;
2357 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2358 parsed = (struct lysp_node_leaflist *)siblings;
2359 assert_string_equal(parsed->dsc, "desc");
2360 assert_string_equal(*parsed->iffeatures, "feature");
2361 assert_int_equal(parsed->min, 5);
2362 assert_string_equal(parsed->musts->arg, "must-cond");
2363 assert_string_equal(parsed->name, "llist");
2364 assert_null(parsed->next);
2365 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2366 assert_null(parsed->parent);
2367 assert_string_equal(parsed->ref, "ref");
2368 assert_string_equal(parsed->type.name, "type");
2369 assert_string_equal(parsed->units, "uni");
2370 assert_string_equal(parsed->when->cond, "when-cond");
2371 assert_true(parsed->flags & LYS_CONFIG_W);
2372 assert_true(parsed->flags & LYS_ORDBY_USER);
2373 assert_true(parsed->flags & LYS_STATUS_CURR);
David Sedlákd1144562019-08-06 12:36:14 +02002374 assert_string_equal(parsed->exts[0].name, "myext:c-define");
2375 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2376 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002377 lysp_node_free(st->ctx, siblings);
2378 siblings = NULL;
2379
2380 data = ELEMENT_WRAPPER_START
2381 "<leaf-list name=\"llist\">"
2382 "<config value=\"true\" />"
2383 "<description><text>desc</text></description>"
2384 "<if-feature name=\"feature\"/>"
2385 "<max-elements value=\"15\"/>"
2386 "<min-elements value=\"5\"/>"
2387 "<must condition=\"must-cond\"/>"
2388 "<ordered-by value=\"user\" />"
2389 "<reference><text>ref</text></reference>"
2390 "<status value=\"current\"/>"
2391 "<type name=\"type\"/>"
2392 "<units name=\"uni\"/>"
2393 "<when condition=\"when-cond\"/>"
2394 "</leaf-list>"
2395 ELEMENT_WRAPPER_END;
2396 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2397 parsed = (struct lysp_node_leaflist *)siblings;
2398 assert_string_equal(parsed->dsc, "desc");
2399 assert_string_equal(*parsed->iffeatures, "feature");
2400 assert_int_equal(parsed->min, 5);
2401 assert_int_equal(parsed->max, 15);
2402 assert_string_equal(parsed->musts->arg, "must-cond");
2403 assert_string_equal(parsed->name, "llist");
2404 assert_null(parsed->next);
2405 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2406 assert_null(parsed->parent);
2407 assert_string_equal(parsed->ref, "ref");
2408 assert_string_equal(parsed->type.name, "type");
2409 assert_string_equal(parsed->units, "uni");
2410 assert_string_equal(parsed->when->cond, "when-cond");
2411 assert_true(parsed->flags & LYS_CONFIG_W);
2412 assert_true(parsed->flags & LYS_ORDBY_USER);
2413 assert_true(parsed->flags & LYS_STATUS_CURR);
2414 lysp_node_free(st->ctx, siblings);
2415 siblings = NULL;
2416
2417 data = ELEMENT_WRAPPER_START
2418 "<leaf-list name=\"llist\">"
2419 "<type name=\"type\"/>"
2420 "</leaf-list>"
2421 ELEMENT_WRAPPER_END;
2422 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2423 parsed = (struct lysp_node_leaflist *)siblings;
2424 assert_string_equal(parsed->name, "llist");
2425 assert_string_equal(parsed->type.name, "type");
2426 lysp_node_free(st->ctx, siblings);
2427 siblings = NULL;
2428
2429 /* invalid combinations */
2430 data = ELEMENT_WRAPPER_START
2431 "<leaf-list name=\"llist\">"
2432 "<max-elements value=\"5\"/>"
2433 "<min-elements value=\"15\"/>"
2434 "<type name=\"type\"/>"
2435 "</leaf-list>"
2436 ELEMENT_WRAPPER_END;
2437 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
2438 logbuf_assert("Invalid combination of min-elements and max-elements: min value 15 is bigger than the max value 5. Line number 1.");
2439 lysp_node_free(st->ctx, siblings);
2440 siblings = NULL;
2441
2442 data = ELEMENT_WRAPPER_START
2443 "<leaf-list name=\"llist\">"
2444 "<default value=\"def-val1\"/>"
2445 "<min-elements value=\"15\"/>"
2446 "<type name=\"type\"/>"
2447 "</leaf-list>"
2448 ELEMENT_WRAPPER_END;
2449 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002450 logbuf_assert("Invalid combination of sub-elemnts \"min-elements\" and \"default\" in \"leaf-list\" element. Line number 1.");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002451 lysp_node_free(st->ctx, siblings);
2452 siblings = NULL;
2453
2454 data = ELEMENT_WRAPPER_START
2455 "<leaf-list name=\"llist\">"
2456 "</leaf-list>"
2457 ELEMENT_WRAPPER_END;
2458 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002459 logbuf_assert("Missing mandatory sub-element \"type\" of \"leaf-list\" element. Line number 1.");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002460 lysp_node_free(st->ctx, siblings);
2461 siblings = NULL;
2462
2463 st->finished_correctly = true;
2464}
2465
David Sedlákcb39f642019-07-19 13:19:55 +02002466static void
2467test_presence_elem(void **state)
2468{
2469 struct state *st = *state;
2470 const char *data;
2471 const char *val;
David Sedlákd1144562019-08-06 12:36:14 +02002472 struct lysp_ext_instance *exts = NULL;
2473
2474 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\">"EXT_SUBELEM"</presence>" ELEMENT_WRAPPER_END;
2475 assert_int_equal(test_element_helper(st, &data, &val, NULL, &exts, true), LY_SUCCESS);
2476 assert_string_equal(val, "presence-val");
2477 assert_string_equal(exts[0].name, "myext:c-define");
2478 assert_int_equal(exts[0].insubstmt_index, 0);
2479 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_PRESENCE);
2480 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
2481 FREE_STRING(st->ctx, val);
David Sedlákcb39f642019-07-19 13:19:55 +02002482
2483 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\"/>" ELEMENT_WRAPPER_END;
2484 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2485 assert_string_equal(val, "presence-val");
2486 FREE_STRING(st->ctx, val);
2487
2488 data = ELEMENT_WRAPPER_START "<presence/>" ELEMENT_WRAPPER_END;
2489 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
2490 logbuf_assert("Missing mandatory attribute value of presence element. Line number 1.");
2491
2492 st->finished_correctly = true;
2493}
2494
David Sedlák12470a82019-07-19 13:44:36 +02002495static void
2496test_key_elem(void **state)
2497{
2498 struct state *st = *state;
2499 const char *data;
2500 const char *val;
David Sedlákd1144562019-08-06 12:36:14 +02002501 struct lysp_ext_instance *exts = NULL;
2502
2503 data = ELEMENT_WRAPPER_START "<key value=\"key-value\">"EXT_SUBELEM"</key>" ELEMENT_WRAPPER_END;
2504 assert_int_equal(test_element_helper(st, &data, &val, NULL, &exts, true), LY_SUCCESS);
2505 assert_string_equal(val, "key-value");
2506 assert_string_equal(exts[0].name, "myext:c-define");
2507 assert_int_equal(exts[0].insubstmt_index, 0);
2508 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_KEY);
2509 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
2510 FREE_STRING(st->ctx, val);
David Sedlák12470a82019-07-19 13:44:36 +02002511
2512 data = ELEMENT_WRAPPER_START "<key value=\"key-value\"/>" ELEMENT_WRAPPER_END;
2513 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2514 assert_string_equal(val, "key-value");
2515 FREE_STRING(st->ctx, val);
2516
2517 data = ELEMENT_WRAPPER_START "<key/>" ELEMENT_WRAPPER_END;
2518 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
2519 logbuf_assert("Missing mandatory attribute value of key element. Line number 1.");
2520
2521 st->finished_correctly = true;
2522}
2523
David Sedlák04e17b22019-07-19 15:29:48 +02002524static void
2525test_typedef_elem(void **state)
2526{
2527 struct state *st = *state;
2528 const char *data;
2529 struct lysp_tpdf *tpdfs = NULL;
2530 struct typedef_meta typdef_meta = {NULL, &tpdfs};
2531
2532 data = ELEMENT_WRAPPER_START
2533 "<typedef name=\"tpdf-name\">"
2534 "<default value=\"def-val\"/>"
2535 "<description><text>desc-text</text></description>"
2536 "<reference><text>ref-text</text></reference>"
2537 "<status value=\"current\"/>"
2538 "<type name=\"type\"/>"
2539 "<units name=\"uni\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002540 EXT_SUBELEM
David Sedlák04e17b22019-07-19 15:29:48 +02002541 "</typedef>"
2542 ELEMENT_WRAPPER_END;
2543 assert_int_equal(test_element_helper(st, &data, &typdef_meta, NULL, NULL, true), LY_SUCCESS);
2544 assert_string_equal(tpdfs[0].dflt, "def-val");
2545 assert_string_equal(tpdfs[0].dsc, "desc-text");
David Sedlák04e17b22019-07-19 15:29:48 +02002546 assert_string_equal(tpdfs[0].name, "tpdf-name");
2547 assert_string_equal(tpdfs[0].ref, "ref-text");
2548 assert_string_equal(tpdfs[0].type.name, "type");
2549 assert_string_equal(tpdfs[0].units, "uni");
2550 assert_true(tpdfs[0].flags & LYS_STATUS_CURR);
David Sedlákd1144562019-08-06 12:36:14 +02002551 assert_string_equal(tpdfs[0].exts[0].name, "myext:c-define");
2552 assert_int_equal(tpdfs[0].exts[0].insubstmt_index, 0);
2553 assert_int_equal(tpdfs[0].exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák04e17b22019-07-19 15:29:48 +02002554 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2555 tpdfs = NULL;
2556
2557 data = ELEMENT_WRAPPER_START
2558 "<typedef name=\"tpdf-name\">"
2559 "<type name=\"type\"/>"
2560 "</typedef>"
2561 ELEMENT_WRAPPER_END;
2562 assert_int_equal(test_element_helper(st, &data, &typdef_meta, NULL, NULL, true), LY_SUCCESS);
2563 assert_string_equal(tpdfs[0].name, "tpdf-name");
2564 assert_string_equal(tpdfs[0].type.name, "type");
2565 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2566 tpdfs = NULL;
2567
2568 st->finished_correctly = true;
2569}
2570
David Sedlákd2d676a2019-07-22 11:28:19 +02002571static void
2572test_refine_elem(void **state)
2573{
2574 struct state *st = *state;
2575 const char *data;
2576 struct lysp_refine *refines = NULL;
2577
2578 /* max subelems */
2579 data = ELEMENT_WRAPPER_START
2580 "<refine target-node=\"target\">"
2581 "<if-feature name=\"feature\" />"
2582 "<must condition=\"cond\" />"
2583 "<presence value=\"presence\" />"
2584 "<default value=\"def\" />"
2585 "<config value=\"true\" />"
2586 "<mandatory value=\"true\" />"
2587 "<min-elements value=\"10\" />"
2588 "<max-elements value=\"20\" />"
2589 "<description><text>desc</text></description>"
2590 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02002591 EXT_SUBELEM
David Sedlákd2d676a2019-07-22 11:28:19 +02002592 "</refine>"
2593 ELEMENT_WRAPPER_END;
2594 assert_int_equal(test_element_helper(st, &data, &refines, NULL, NULL, true), LY_SUCCESS);
2595 assert_string_equal(refines->nodeid, "target");
2596 assert_string_equal(*refines->dflts, "def");
2597 assert_string_equal(refines->dsc, "desc");
David Sedlákd2d676a2019-07-22 11:28:19 +02002598 assert_true(refines->flags & LYS_CONFIG_W);
2599 assert_true(refines->flags & LYS_MAND_TRUE);
2600 assert_string_equal(*refines->iffeatures, "feature");
2601 assert_int_equal(refines->max, 20);
2602 assert_int_equal(refines->min, 10);
2603 assert_string_equal(refines->musts->arg, "cond");
2604 assert_string_equal(refines->presence, "presence");
2605 assert_string_equal(refines->ref, "ref");
David Sedlákd1144562019-08-06 12:36:14 +02002606 assert_string_equal(refines->exts[0].name, "myext:c-define");
2607 assert_int_equal(refines->exts[0].insubstmt_index, 0);
2608 assert_int_equal(refines->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákd2d676a2019-07-22 11:28:19 +02002609 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2610 refines = NULL;
2611
2612 /* min subelems */
2613 data = ELEMENT_WRAPPER_START "<refine target-node=\"target\" />" ELEMENT_WRAPPER_END;
2614 assert_int_equal(test_element_helper(st, &data, &refines, NULL, NULL, true), LY_SUCCESS);
2615 assert_string_equal(refines->nodeid, "target");
2616 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2617 refines = NULL;
2618
2619 st->finished_correctly = true;
2620}
2621
David Sedlák0d6de5a2019-07-22 13:25:44 +02002622static void
2623test_uses_elem(void **state)
2624{
2625 struct state *st = *state;
2626 const char *data;
2627 struct lysp_node *siblings = NULL;
2628 struct tree_node_meta node_meta = {NULL, &siblings};
2629 struct lysp_node_uses *parsed = NULL;
2630
2631 /* max subelems */
2632 data = ELEMENT_WRAPPER_START
2633 "<uses name=\"uses-name\">"
2634 "<when condition=\"cond\" />"
2635 "<if-feature name=\"feature\" />"
2636 "<status value=\"obsolete\" />"
2637 "<description><text>desc</text></description>"
2638 "<reference><text>ref</text></reference>"
2639 "<refine target-node=\"target\"/>"
David Sedlák992fb7c2019-07-24 16:51:01 +02002640 "<augment target-node=\"target\" />"
David Sedlákd1144562019-08-06 12:36:14 +02002641 EXT_SUBELEM
David Sedlák0d6de5a2019-07-22 13:25:44 +02002642 "</uses>"
2643 ELEMENT_WRAPPER_END;
2644 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2645 parsed = (struct lysp_node_uses *)&siblings[0];
2646 assert_string_equal(parsed->name, "uses-name");
2647 assert_string_equal(parsed->dsc, "desc");
David Sedlák0d6de5a2019-07-22 13:25:44 +02002648 assert_true(parsed->flags & LYS_STATUS_OBSLT);
2649 assert_string_equal(*parsed->iffeatures, "feature");
2650 assert_null(parsed->next);
2651 assert_int_equal(parsed->nodetype, LYS_USES);
2652 assert_null(parsed->parent);
2653 assert_string_equal(parsed->ref, "ref");
2654 assert_string_equal(parsed->refines->nodeid, "target");
2655 assert_string_equal(parsed->when->cond, "cond");
David Sedlák992fb7c2019-07-24 16:51:01 +02002656 assert_string_equal(parsed->augments->nodeid, "target");
David Sedlákd1144562019-08-06 12:36:14 +02002657 assert_string_equal(parsed->exts[0].name, "myext:c-define");
2658 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2659 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002660 lysp_node_free(st->ctx, siblings);
2661 siblings = NULL;
2662
2663 /* min subelems */
2664 data = ELEMENT_WRAPPER_START "<uses name=\"uses-name\"/>" ELEMENT_WRAPPER_END;
2665 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2666 assert_string_equal(siblings[0].name, "uses-name");
2667 lysp_node_free(st->ctx, siblings);
2668 siblings = NULL;
2669
2670 st->finished_correctly = true;
2671}
2672
David Sedlákaa854b02019-07-22 14:17:10 +02002673static void
2674test_revision_elem(void **state)
2675{
2676 struct state *st = *state;
2677 const char *data;
2678 struct lysp_revision *revs = NULL;
2679
2680 /* max subelems */
2681 data = ELEMENT_WRAPPER_START
2682 "<revision date=\"2018-12-25\">"
2683 "<description><text>desc</text></description>"
2684 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02002685 EXT_SUBELEM
David Sedlákaa854b02019-07-22 14:17:10 +02002686 "</revision>"
2687 ELEMENT_WRAPPER_END;
2688 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, true), LY_SUCCESS);
2689 assert_string_equal(revs->date, "2018-12-25");
2690 assert_string_equal(revs->dsc, "desc");
2691 assert_string_equal(revs->ref, "ref");
David Sedlákd1144562019-08-06 12:36:14 +02002692 assert_string_equal(revs->exts[0].name, "myext:c-define");
2693 assert_int_equal(revs->exts[0].insubstmt_index, 0);
2694 assert_int_equal(revs->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákaa854b02019-07-22 14:17:10 +02002695 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2696 revs = NULL;
2697
2698 /* min subelems */
2699 data = ELEMENT_WRAPPER_START "<revision date=\"2005-05-05\" />" ELEMENT_WRAPPER_END;
2700 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, true), LY_SUCCESS);
2701 assert_string_equal(revs->date, "2005-05-05");
2702 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2703 revs = NULL;
2704
2705 /* invalid value */
2706 data = ELEMENT_WRAPPER_START "<revision date=\"05-05-2005\" />" ELEMENT_WRAPPER_END;
2707 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, false), LY_EVALID);
2708 logbuf_assert("Invalid value \"05-05-2005\" of \"revision\". Line number 1.");
2709 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2710 revs = NULL;
2711
2712 st->finished_correctly = true;
2713}
2714
David Sedlák0c2bab92019-07-22 15:33:19 +02002715static void
2716test_include_elem(void **state)
2717{
2718 struct state *st = *state;
2719 const char *data;
2720 struct lysp_include *includes = NULL;
2721 struct include_meta inc_meta = {"module-name", &includes};
2722
2723 /* max subelems */
2724 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2725 data = ELEMENT_WRAPPER_START
2726 "<include module=\"mod\">"
2727 "<description><text>desc</text></description>"
2728 "<reference><text>ref</text></reference>"
2729 "<revision-date date=\"1999-09-09\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002730 EXT_SUBELEM
David Sedlák0c2bab92019-07-22 15:33:19 +02002731 "</include>"
2732 ELEMENT_WRAPPER_END;
2733 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, true), LY_SUCCESS);
2734 assert_string_equal(includes->name, "mod");
2735 assert_string_equal(includes->dsc, "desc");
2736 assert_string_equal(includes->ref, "ref");
David Sedlák0c2bab92019-07-22 15:33:19 +02002737 assert_string_equal(includes->rev, "1999-09-09");
David Sedlákd1144562019-08-06 12:36:14 +02002738 assert_string_equal(includes->exts[0].name, "myext:c-define");
2739 assert_int_equal(includes->exts[0].insubstmt_index, 0);
2740 assert_int_equal(includes->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák0c2bab92019-07-22 15:33:19 +02002741 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2742 includes = NULL;
2743
2744 /* min subelems */
2745 data = ELEMENT_WRAPPER_START "<include module=\"mod\"/>" ELEMENT_WRAPPER_END;
2746 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, true), LY_SUCCESS);
2747 assert_string_equal(includes->name, "mod");
2748 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2749 includes = NULL;
2750
2751 /* invalid combinations */
2752 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2753 data = ELEMENT_WRAPPER_START
2754 "<include module=\"mod\">"
2755 "<description><text>desc</text></description>"
2756 "<revision-date date=\"1999-09-09\"/>"
2757 "</include>"
2758 ELEMENT_WRAPPER_END;
2759 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, false), LY_EVALID);
2760 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.");
2761 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2762 includes = NULL;
2763
2764 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2765 data = ELEMENT_WRAPPER_START
2766 "<include module=\"mod\">"
2767 "<reference><text>ref</text></reference>"
2768 "<revision-date date=\"1999-09-09\"/>"
2769 "</include>"
2770 ELEMENT_WRAPPER_END;
2771 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, false), LY_EVALID);
2772 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.");
2773 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2774 includes = NULL;
2775
2776 st->finished_correctly = true;
2777}
2778
David Sedlák5e13dea2019-07-22 16:06:45 +02002779static void
2780test_feature_elem(void **state)
2781{
2782 struct state *st = *state;
2783 const char *data;
2784 struct lysp_feature *features = NULL;
2785
2786 /* max subelems */
2787 data = ELEMENT_WRAPPER_START
2788 "<feature name=\"feature-name\">"
2789 "<if-feature name=\"iff\"/>"
2790 "<status value=\"deprecated\"/>"
2791 "<description><text>desc</text></description>"
2792 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02002793 EXT_SUBELEM
David Sedlák5e13dea2019-07-22 16:06:45 +02002794 "</feature>"
2795 ELEMENT_WRAPPER_END;
2796 assert_int_equal(test_element_helper(st, &data, &features, NULL, NULL, true), LY_SUCCESS);
2797 assert_string_equal(features->name, "feature-name");
2798 assert_string_equal(features->dsc, "desc");
David Sedlák5e13dea2019-07-22 16:06:45 +02002799 assert_true(features->flags & LYS_STATUS_DEPRC);
2800 assert_string_equal(*features->iffeatures, "iff");
2801 assert_string_equal(features->ref, "ref");
David Sedlákd1144562019-08-06 12:36:14 +02002802 assert_string_equal(features->exts[0].name, "myext:c-define");
2803 assert_int_equal(features->exts[0].insubstmt_index, 0);
2804 assert_int_equal(features->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák5e13dea2019-07-22 16:06:45 +02002805 FREE_ARRAY(st->ctx, features, lysp_feature_free);
2806 features = NULL;
2807
2808 /* min subelems */
2809 data = ELEMENT_WRAPPER_START "<feature name=\"feature-name\"/>" ELEMENT_WRAPPER_END;
2810 assert_int_equal(test_element_helper(st, &data, &features, NULL, NULL, true), LY_SUCCESS);
2811 assert_string_equal(features->name, "feature-name");
2812 FREE_ARRAY(st->ctx, features, lysp_feature_free);
2813 features = NULL;
2814
2815 st->finished_correctly = true;
2816}
2817
David Sedlák28794f22019-07-22 16:45:00 +02002818static void
2819test_identity_elem(void **state)
2820{
2821 struct state *st = *state;
2822 const char *data;
2823 struct lysp_ident *identities = NULL;
2824
2825 /* max subelems */
2826 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2827 data = ELEMENT_WRAPPER_START
2828 "<identity name=\"ident-name\">"
2829 "<if-feature name=\"iff\"/>"
2830 "<base name=\"base-name\"/>"
2831 "<status value=\"deprecated\"/>"
2832 "<description><text>desc</text></description>"
2833 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02002834 EXT_SUBELEM
David Sedlák28794f22019-07-22 16:45:00 +02002835 "</identity>"
2836 ELEMENT_WRAPPER_END;
2837 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, true), LY_SUCCESS);
2838 assert_string_equal(identities->name, "ident-name");
2839 assert_string_equal(*identities->bases, "base-name");
2840 assert_string_equal(*identities->iffeatures, "iff");
2841 assert_string_equal(identities->dsc, "desc");
2842 assert_string_equal(identities->ref, "ref");
2843 assert_true(identities->flags & LYS_STATUS_DEPRC);
David Sedlákd1144562019-08-06 12:36:14 +02002844 assert_string_equal(identities->exts[0].name, "myext:c-define");
2845 assert_int_equal(identities->exts[0].insubstmt_index, 0);
2846 assert_int_equal(identities->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák28794f22019-07-22 16:45:00 +02002847 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2848 identities = NULL;
2849
2850 /* min subelems */
2851 data = ELEMENT_WRAPPER_START "<identity name=\"ident-name\" />" ELEMENT_WRAPPER_END;
2852 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, true), LY_SUCCESS);
2853 assert_string_equal(identities->name, "ident-name");
2854 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2855 identities = NULL;
2856
2857 /* invalid */
2858 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2859 data = ELEMENT_WRAPPER_START
2860 "<identity name=\"ident-name\">"
2861 "<if-feature name=\"iff\"/>"
2862 "</identity>"
2863 ELEMENT_WRAPPER_END;
2864 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, false), LY_EVALID);
2865 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.");
2866 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2867 identities = NULL;
2868
2869 st->finished_correctly = true;
2870}
2871
David Sedlákaf536aa2019-07-23 13:42:23 +02002872static void
2873test_list_elem(void **state)
2874{
2875 struct state *st = *state;
2876 const char *data;
2877 struct lysp_node *siblings = NULL;
2878 struct tree_node_meta node_meta = {NULL, &siblings};
2879 struct lysp_node_list *parsed = NULL;
2880
2881 /* max subelems */
2882 data = ELEMENT_WRAPPER_START
2883 "<list name=\"list-name\">"
2884 "<when condition=\"when\"/>"
2885 "<if-feature name=\"iff\"/>"
2886 "<must condition=\"must-cond\"/>"
2887 "<key value=\"key\"/>"
2888 "<unique tag=\"utag\"/>"
2889 "<config value=\"true\"/>"
2890 "<min-elements value=\"10\"/>"
2891 "<ordered-by value=\"user\"/>"
2892 "<status value=\"deprecated\"/>"
2893 "<description><text>desc</text></description>"
2894 "<reference><text>ref</text></reference>"
2895 "<anydata name=\"anyd\"/>"
2896 "<anyxml name=\"anyx\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02002897 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002898 "<choice name=\"choice\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02002899 "<action name=\"action\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02002900 "<grouping name=\"grp\"/>"
David Sedlák031b9e72019-07-23 15:19:37 +02002901 "<notification name=\"notf\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002902 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
2903 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlákaf536aa2019-07-23 13:42:23 +02002904 "<list name=\"sub-list\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002905 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákaf536aa2019-07-23 13:42:23 +02002906 "<uses name=\"uses-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002907 EXT_SUBELEM
David Sedlákaf536aa2019-07-23 13:42:23 +02002908 "</list>"
2909 ELEMENT_WRAPPER_END;
2910 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2911 parsed = (struct lysp_node_list *)&siblings[0];
2912 assert_string_equal(parsed->dsc, "desc");
2913 assert_string_equal(parsed->child->name, "anyd");
2914 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2915 assert_string_equal(parsed->child->next->name, "anyx");
2916 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
David Sedlákf111bcb2019-07-23 17:15:51 +02002917 assert_string_equal(parsed->child->next->next->name, "cont");
2918 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002919 assert_string_equal(parsed->child->next->next->next->name, "choice");
2920 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
David Sedlák85d0eca2019-07-24 15:15:21 +02002921 assert_string_equal(parsed->child->next->next->next->next->name, "leaf");
2922 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAF);
2923 assert_string_equal(parsed->child->next->next->next->next->next->name, "llist");
2924 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2925 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "sub-list");
2926 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LIST);
2927 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "uses-name");
2928 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_USES);
2929 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002930 assert_string_equal(parsed->groupings->name, "grp");
David Sedlák85d0eca2019-07-24 15:15:21 +02002931 assert_string_equal(parsed->actions->name, "action");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002932 assert_int_equal(parsed->groupings->nodetype, LYS_GROUPING);
David Sedlák031b9e72019-07-23 15:19:37 +02002933 assert_string_equal(parsed->notifs->name, "notf");
David Sedlákaf536aa2019-07-23 13:42:23 +02002934 assert_true(parsed->flags & LYS_ORDBY_USER);
2935 assert_true(parsed->flags & LYS_STATUS_DEPRC);
2936 assert_true(parsed->flags & LYS_CONFIG_W);
2937 assert_string_equal(*parsed->iffeatures, "iff");
2938 assert_string_equal(parsed->key, "key");
2939 assert_int_equal(parsed->min, 10);
2940 assert_string_equal(parsed->musts->arg, "must-cond");
2941 assert_string_equal(parsed->name, "list-name");
2942 assert_null(parsed->next);
2943 assert_int_equal(parsed->nodetype, LYS_LIST);
2944 assert_null(parsed->parent);
2945 assert_string_equal(parsed->ref, "ref");
2946 assert_string_equal(parsed->typedefs->name, "tpdf");
2947 assert_string_equal(*parsed->uniques, "utag");
2948 assert_string_equal(parsed->when->cond, "when");
David Sedlákd1144562019-08-06 12:36:14 +02002949 assert_string_equal(parsed->exts[0].name, "myext:c-define");
2950 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2951 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákaf536aa2019-07-23 13:42:23 +02002952 lysp_node_free(st->ctx, siblings);
2953 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
2954 siblings = NULL;
2955
2956 /* min subelems */
2957 data = ELEMENT_WRAPPER_START "<list name=\"list-name\" />" ELEMENT_WRAPPER_END;
2958 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2959 parsed = (struct lysp_node_list *)&siblings[0];
2960 assert_string_equal(parsed->name, "list-name");
2961 lysp_node_free(st->ctx, siblings);
2962 siblings = NULL;
2963
2964 st->finished_correctly = true;
2965}
2966
David Sedlák031b9e72019-07-23 15:19:37 +02002967static void
2968test_notification_elem(void **state)
2969{
2970 struct state *st = *state;
2971 const char *data;
2972 struct lysp_notif *notifs = NULL;
2973 struct notif_meta notif_meta = {NULL, &notifs};
2974
2975 /* max subelems */
2976 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2977 data = ELEMENT_WRAPPER_START
2978 "<notification name=\"notif-name\">"
2979 "<anydata name=\"anyd\"/>"
2980 "<anyxml name=\"anyx\"/>"
2981 "<description><text>desc</text></description>"
2982 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002983 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
2984 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák031b9e72019-07-23 15:19:37 +02002985 "<list name=\"sub-list\"/>"
2986 "<must condition=\"cond\"/>"
2987 "<reference><text>ref</text></reference>"
2988 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002989 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák031b9e72019-07-23 15:19:37 +02002990 "<uses name=\"uses-name\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02002991 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002992 "<choice name=\"choice\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02002993 "<grouping name=\"grp\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002994 EXT_SUBELEM
David Sedlák031b9e72019-07-23 15:19:37 +02002995 "</notification>"
2996 ELEMENT_WRAPPER_END;
2997 assert_int_equal(test_element_helper(st, &data, &notif_meta, NULL, NULL, true), LY_SUCCESS);
2998 assert_string_equal(notifs->name, "notif-name");
2999 assert_string_equal(notifs->data->name, "anyd");
3000 assert_int_equal(notifs->data->nodetype, LYS_ANYDATA);
3001 assert_string_equal(notifs->data->next->name, "anyx");
3002 assert_int_equal(notifs->data->next->nodetype, LYS_ANYXML);
3003 assert_string_equal(notifs->data->next->next->name, "leaf");
3004 assert_int_equal(notifs->data->next->next->nodetype, LYS_LEAF);
3005 assert_string_equal(notifs->data->next->next->next->name, "llist");
3006 assert_int_equal(notifs->data->next->next->next->nodetype, LYS_LEAFLIST);
3007 assert_string_equal(notifs->data->next->next->next->next->name, "sub-list");
3008 assert_int_equal(notifs->data->next->next->next->next->nodetype, LYS_LIST);
David Sedlák031b9e72019-07-23 15:19:37 +02003009 assert_true(notifs->flags & LYS_STATUS_DEPRC);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003010 assert_string_equal(notifs->groupings->name, "grp");
3011 assert_int_equal(notifs->groupings->nodetype, LYS_GROUPING);
David Sedlákf111bcb2019-07-23 17:15:51 +02003012 assert_string_equal(notifs->data->next->next->next->next->next->name, "uses-name");
3013 assert_int_equal(notifs->data->next->next->next->next->next->nodetype, LYS_USES);
3014 assert_string_equal(notifs->data->next->next->next->next->next->next->name, "cont");
3015 assert_int_equal(notifs->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003016 assert_int_equal(notifs->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
3017 assert_string_equal(notifs->data->next->next->next->next->next->next->next->name, "choice");
3018 assert_null(notifs->data->next->next->next->next->next->next->next->next);
David Sedlák031b9e72019-07-23 15:19:37 +02003019 assert_string_equal(*notifs->iffeatures, "iff");
3020 assert_string_equal(notifs->musts->arg, "cond");
3021 assert_int_equal(notifs->nodetype, LYS_NOTIF);
3022 assert_null(notifs->parent);
3023 assert_string_equal(notifs->ref, "ref");
3024 assert_string_equal(notifs->typedefs->name, "tpdf");
David Sedlákd1144562019-08-06 12:36:14 +02003025 assert_string_equal(notifs->exts[0].name, "myext:c-define");
3026 assert_int_equal(notifs->exts[0].insubstmt_index, 0);
3027 assert_int_equal(notifs->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák031b9e72019-07-23 15:19:37 +02003028 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
3029 notifs = NULL;
3030
3031 /* min subelems */
3032 data = ELEMENT_WRAPPER_START "<notification name=\"notif-name\" />" ELEMENT_WRAPPER_END;
3033 assert_int_equal(test_element_helper(st, &data, &notif_meta, NULL, NULL, true), LY_SUCCESS);
3034 assert_string_equal(notifs->name, "notif-name");
3035 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003036 notifs = NULL;
3037
3038 st->finished_correctly = true;
3039}
3040
3041static void
3042test_grouping_elem(void **state)
3043{
3044 struct state *st = *state;
3045 const char *data;
3046 struct lysp_grp *grps = NULL;
3047 struct grouping_meta grp_meta = {NULL, &grps};
3048
3049 /* max subelems */
3050 data = ELEMENT_WRAPPER_START
3051 "<grouping name=\"grp-name\">"
3052 "<anydata name=\"anyd\"/>"
3053 "<anyxml name=\"anyx\"/>"
3054 "<description><text>desc</text></description>"
3055 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003056 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3057 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02003058 "<list name=\"list\"/>"
3059 "<notification name=\"notf\"/>"
3060 "<reference><text>ref</text></reference>"
3061 "<status value=\"current\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003062 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02003063 "<uses name=\"uses-name\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02003064 "<action name=\"act\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02003065 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003066 "<choice name=\"choice\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003067 EXT_SUBELEM
David Sedláke3ce9ef2019-07-23 16:34:30 +02003068 "</grouping>"
3069 ELEMENT_WRAPPER_END;
3070 assert_int_equal(test_element_helper(st, &data, &grp_meta, NULL, NULL, true), LY_SUCCESS);
3071 assert_string_equal(grps->name, "grp-name");
David Sedláke3ce9ef2019-07-23 16:34:30 +02003072 assert_string_equal(grps->data->name, "anyd");
3073 assert_string_equal(grps->data->next->name, "anyx");
3074 assert_string_equal(grps->data->next->next->name, "leaf");
3075 assert_string_equal(grps->data->next->next->next->name, "llist");
3076 assert_string_equal(grps->data->next->next->next->next->name, "list");
3077 assert_string_equal(grps->dsc, "desc");
David Sedláke3ce9ef2019-07-23 16:34:30 +02003078 assert_true(grps->flags & LYS_STATUS_CURR);
3079 assert_string_equal(grps->groupings->name, "sub-grp");
3080 assert_int_equal(grps->nodetype, LYS_GROUPING);
3081 assert_string_equal(grps->notifs->name, "notf");
3082 assert_null(grps->parent);
3083 assert_string_equal(grps->ref, "ref");
3084 assert_string_equal(grps->typedefs->name, "tpdf");
David Sedlák85d0eca2019-07-24 15:15:21 +02003085 assert_string_equal(grps->actions->name, "act");
David Sedlákf111bcb2019-07-23 17:15:51 +02003086 assert_string_equal(grps->data->next->next->next->next->next->name, "uses-name");
David Sedlákb7abcfa2019-07-24 12:33:35 +02003087 assert_int_equal(grps->data->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákf111bcb2019-07-23 17:15:51 +02003088 assert_string_equal(grps->data->next->next->next->next->next->next->name, "cont");
David Sedlákb7abcfa2019-07-24 12:33:35 +02003089 assert_int_equal(grps->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
3090 assert_string_equal(grps->data->next->next->next->next->next->next->next->name, "choice");
3091 assert_int_equal(grps->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
David Sedlákd1144562019-08-06 12:36:14 +02003092 assert_string_equal(grps->exts[0].name, "myext:c-define");
3093 assert_int_equal(grps->exts[0].insubstmt_index, 0);
3094 assert_int_equal(grps->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003095 FREE_ARRAY(st->ctx, grps, lysp_grp_free);
3096 grps = NULL;
3097
3098 /* min subelems */
3099 data = ELEMENT_WRAPPER_START "<grouping name=\"grp-name\" />" ELEMENT_WRAPPER_END;
3100 assert_int_equal(test_element_helper(st, &data, &grp_meta, NULL, NULL, true), LY_SUCCESS);
3101 assert_string_equal(grps->name, "grp-name");
3102 FREE_ARRAY(st->ctx, grps, lysp_grp_free);
3103 grps = NULL;
David Sedlák031b9e72019-07-23 15:19:37 +02003104
3105 st->finished_correctly = true;
3106}
3107
David Sedlákf111bcb2019-07-23 17:15:51 +02003108static void
3109test_container_elem(void **state)
3110{
3111 struct state *st = *state;
3112 const char *data;
3113 struct lysp_node *siblings = NULL;
3114 struct tree_node_meta node_meta = {NULL, &siblings};
3115 struct lysp_node_container *parsed = NULL;
3116
3117 /* max subelems */
David Sedláke2dc9e92019-07-24 09:59:21 +02003118 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3119 data = ELEMENT_WRAPPER_START
3120 "<container name=\"cont-name\">"
3121 "<anydata name=\"anyd\"/>"
3122 "<anyxml name=\"anyx\"/>"
3123 "<config value=\"true\"/>"
3124 "<container name=\"subcont\"/>"
3125 "<description><text>desc</text></description>"
3126 "<grouping name=\"sub-grp\"/>"
3127 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003128 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3129 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedláke2dc9e92019-07-24 09:59:21 +02003130 "<list name=\"list\"/>"
3131 "<must condition=\"cond\"/>"
3132 "<notification name=\"notf\"/>"
3133 "<presence value=\"presence\"/>"
3134 "<reference><text>ref</text></reference>"
3135 "<status value=\"current\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003136 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedláke2dc9e92019-07-24 09:59:21 +02003137 "<uses name=\"uses-name\"/>"
3138 "<when condition=\"when-cond\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02003139 "<action name=\"act\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003140 "<choice name=\"choice\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003141 EXT_SUBELEM
David Sedláke2dc9e92019-07-24 09:59:21 +02003142 "</container>"
3143 ELEMENT_WRAPPER_END;
3144 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
3145 parsed = (struct lysp_node_container *)siblings;
3146 assert_string_equal(parsed->name, "cont-name");
3147 assert_null(parsed->parent);
3148 assert_int_equal(parsed->nodetype, LYS_CONTAINER);
3149 assert_true(parsed->flags & LYS_CONFIG_W);
3150 assert_true(parsed->flags & LYS_STATUS_CURR);
3151 assert_null(parsed->next);
3152 assert_string_equal(parsed->dsc, "desc");
3153 assert_string_equal(parsed->ref, "ref");
3154 assert_string_equal(parsed->when->cond, "when-cond");
3155 assert_string_equal(*parsed->iffeatures, "iff");
David Sedláke2dc9e92019-07-24 09:59:21 +02003156 assert_string_equal(parsed->musts->arg, "cond");
3157 assert_string_equal(parsed->presence, "presence");
3158 assert_string_equal(parsed->typedefs->name, "tpdf");
3159 assert_string_equal(parsed->groupings->name, "sub-grp");
3160 assert_string_equal(parsed->child->name, "anyd");
3161 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
3162 assert_string_equal(parsed->child->next->name, "anyx");
3163 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
3164 assert_string_equal(parsed->child->next->next->name, "subcont");
3165 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
3166 assert_string_equal(parsed->child->next->next->next->name, "leaf");
3167 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
3168 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
3169 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
3170 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
3171 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
3172 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
3173 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003174 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
3175 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
3176 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke2dc9e92019-07-24 09:59:21 +02003177 assert_string_equal(parsed->notifs->name, "notf");
David Sedlák85d0eca2019-07-24 15:15:21 +02003178 assert_string_equal(parsed->actions->name, "act");
David Sedlákd1144562019-08-06 12:36:14 +02003179 assert_string_equal(parsed->exts[0].name, "myext:c-define");
3180 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3181 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedláke2dc9e92019-07-24 09:59:21 +02003182 lysp_node_free(st->ctx, siblings);
3183 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
3184 siblings = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02003185
3186 /* min subelems */
3187 data = ELEMENT_WRAPPER_START "<container name=\"cont-name\" />" ELEMENT_WRAPPER_END;
3188 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
3189 parsed = (struct lysp_node_container *)siblings;
3190 assert_string_equal(parsed->name, "cont-name");
3191 lysp_node_free(st->ctx, siblings);
3192 siblings = NULL;
3193
3194 st->finished_correctly = true;
3195}
3196
David Sedlák5379d392019-07-24 10:42:03 +02003197static void
3198test_case_elem(void **state)
3199{
3200 struct state *st = *state;
3201 const char *data;
3202 struct lysp_node *siblings = NULL;
3203 struct tree_node_meta node_meta = {NULL, &siblings};
3204 struct lysp_node_case *parsed = NULL;
3205
3206 /* max subelems */
3207 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3208 data = ELEMENT_WRAPPER_START
3209 "<case name=\"case-name\">"
3210 "<anydata name=\"anyd\"/>"
3211 "<anyxml name=\"anyx\"/>"
3212 "<container name=\"subcont\"/>"
3213 "<description><text>desc</text></description>"
3214 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003215 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3216 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák5379d392019-07-24 10:42:03 +02003217 "<list name=\"list\"/>"
3218 "<reference><text>ref</text></reference>"
3219 "<status value=\"current\"/>"
3220 "<uses name=\"uses-name\"/>"
3221 "<when condition=\"when-cond\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003222 "<choice name=\"choice\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003223 EXT_SUBELEM
David Sedlák5379d392019-07-24 10:42:03 +02003224 "</case>"
3225 ELEMENT_WRAPPER_END;
3226 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
3227 parsed = (struct lysp_node_case *)siblings;
3228 assert_string_equal(parsed->name, "case-name");
3229 assert_null(parsed->parent);
3230 assert_int_equal(parsed->nodetype, LYS_CASE);
3231 assert_true(parsed->flags & LYS_STATUS_CURR);
3232 assert_null(parsed->next);
3233 assert_string_equal(parsed->dsc, "desc");
3234 assert_string_equal(parsed->ref, "ref");
3235 assert_string_equal(parsed->when->cond, "when-cond");
3236 assert_string_equal(*parsed->iffeatures, "iff");
David Sedlák5379d392019-07-24 10:42:03 +02003237 assert_string_equal(parsed->child->name, "anyd");
3238 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
3239 assert_string_equal(parsed->child->next->name, "anyx");
3240 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
3241 assert_string_equal(parsed->child->next->next->name, "subcont");
3242 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
3243 assert_string_equal(parsed->child->next->next->next->name, "leaf");
3244 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
3245 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
3246 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
3247 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
3248 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
3249 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
3250 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003251 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
3252 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
3253 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedlákd1144562019-08-06 12:36:14 +02003254 assert_string_equal(parsed->exts[0].name, "myext:c-define");
3255 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3256 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák5379d392019-07-24 10:42:03 +02003257 lysp_node_free(st->ctx, siblings);
3258 siblings = NULL;
3259
3260 /* min subelems */
3261 data = ELEMENT_WRAPPER_START "<case name=\"case-name\" />" ELEMENT_WRAPPER_END;
3262 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
3263 parsed = (struct lysp_node_case *)siblings;
3264 assert_string_equal(parsed->name, "case-name");
3265 lysp_node_free(st->ctx, siblings);
3266 siblings = NULL;
3267
3268 st->finished_correctly = true;
3269}
3270
David Sedlákb7abcfa2019-07-24 12:33:35 +02003271static void
3272test_choice_elem(void **state)
3273{
3274 struct state *st = *state;
3275 const char *data;
3276 struct lysp_node *siblings = NULL;
3277 struct tree_node_meta node_meta = {NULL, &siblings};
3278 struct lysp_node_choice *parsed = NULL;
3279
3280 /* max subelems */
3281 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3282 data = ELEMENT_WRAPPER_START
3283 "<choice name=\"choice-name\">"
3284 "<anydata name=\"anyd\"/>"
3285 "<anyxml name=\"anyx\"/>"
3286 "<case name=\"sub-case\"/>"
3287 "<choice name=\"choice\"/>"
3288 "<config value=\"true\"/>"
3289 "<container name=\"subcont\"/>"
3290 "<default value=\"def\"/>"
3291 "<description><text>desc</text></description>"
3292 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003293 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3294 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003295 "<list name=\"list\"/>"
3296 "<mandatory value=\"true\" />"
3297 "<reference><text>ref</text></reference>"
3298 "<status value=\"current\"/>"
3299 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003300 EXT_SUBELEM
David Sedlákb7abcfa2019-07-24 12:33:35 +02003301 "</choice>"
3302 ELEMENT_WRAPPER_END;
3303 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
3304 parsed = (struct lysp_node_choice *)siblings;
3305 assert_string_equal(parsed->name, "choice-name");
3306 assert_null(parsed->parent);
3307 assert_int_equal(parsed->nodetype, LYS_CHOICE);
3308 assert_true(parsed->flags & LYS_CONFIG_W && parsed->flags & LYS_MAND_TRUE && parsed->flags & LYS_STATUS_CURR);
3309 assert_null(parsed->next);
3310 assert_string_equal(parsed->dsc, "desc");
3311 assert_string_equal(parsed->ref, "ref");
3312 assert_string_equal(parsed->when->cond, "when-cond");
3313 assert_string_equal(*parsed->iffeatures, "iff");
David Sedlákb7abcfa2019-07-24 12:33:35 +02003314 assert_string_equal(parsed->child->name, "anyd");
3315 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
3316 assert_string_equal(parsed->child->next->name, "anyx");
3317 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
3318 assert_string_equal(parsed->child->next->next->name, "sub-case");
3319 assert_int_equal(parsed->child->next->next->nodetype, LYS_CASE);
3320 assert_string_equal(parsed->child->next->next->next->name, "choice");
3321 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
3322 assert_string_equal(parsed->child->next->next->next->next->name, "subcont");
3323 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_CONTAINER);
3324 assert_string_equal(parsed->child->next->next->next->next->next->name, "leaf");
3325 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAF);
3326 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "llist");
3327 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3328 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "list");
3329 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
3330 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedlákd1144562019-08-06 12:36:14 +02003331 assert_string_equal(parsed->exts[0].name, "myext:c-define");
3332 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3333 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003334 lysp_node_free(st->ctx, siblings);
3335 siblings = NULL;
3336
3337 /* min subelems */
3338 data = ELEMENT_WRAPPER_START "<choice name=\"choice-name\" />" ELEMENT_WRAPPER_END;
3339 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
3340 parsed = (struct lysp_node_choice *)siblings;
3341 assert_string_equal(parsed->name, "choice-name");
3342 lysp_node_free(st->ctx, siblings);
3343 siblings = NULL;
3344
3345 st->finished_correctly = true;
3346}
3347
David Sedlák05404f62019-07-24 14:11:53 +02003348static void
3349test_inout_elem(void **state)
3350{
3351 struct state *st = *state;
3352 const char *data;
3353 struct lysp_action_inout inout = {};
3354 struct inout_meta inout_meta = {NULL, &inout};
3355
3356 /* max subelements */
3357 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3358 data = ELEMENT_WRAPPER_START
3359 "<input>"
3360 "<anydata name=\"anyd\"/>"
3361 "<anyxml name=\"anyx\"/>"
3362 "<choice name=\"choice\"/>"
3363 "<container name=\"subcont\"/>"
3364 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003365 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3366 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák05404f62019-07-24 14:11:53 +02003367 "<list name=\"list\"/>"
3368 "<must condition=\"cond\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003369 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák05404f62019-07-24 14:11:53 +02003370 "<uses name=\"uses-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003371 EXT_SUBELEM
David Sedlák05404f62019-07-24 14:11:53 +02003372 "</input>"
3373 ELEMENT_WRAPPER_END;
3374 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3375 assert_null(inout.parent);
3376 assert_int_equal(inout.nodetype, LYS_INPUT);
3377 assert_string_equal(inout.musts->arg, "cond");
3378 assert_string_equal(inout.typedefs->name, "tpdf");
3379 assert_string_equal(inout.groupings->name, "sub-grp");
3380 assert_string_equal(inout.data->name, "anyd");
3381 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
3382 assert_string_equal(inout.data->next->name, "anyx");
3383 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
3384 assert_string_equal(inout.data->next->next->name, "choice");
3385 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
3386 assert_string_equal(inout.data->next->next->next->name, "subcont");
3387 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
3388 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
3389 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
3390 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
3391 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3392 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
3393 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
3394 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
3395 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3396 assert_null(inout.data->next->next->next->next->next->next->next->next);
David Sedlákd1144562019-08-06 12:36:14 +02003397 assert_string_equal(inout.exts[0].name, "myext:c-define");
3398 assert_int_equal(inout.exts[0].insubstmt_index, 0);
3399 assert_int_equal(inout.exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák05404f62019-07-24 14:11:53 +02003400 lysp_action_inout_free(st->ctx, &inout);
3401 memset(&inout, 0, sizeof inout);
3402
3403 /* max subelements */
3404 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3405 data = ELEMENT_WRAPPER_START
3406 "<output>"
3407 "<anydata name=\"anyd\"/>"
3408 "<anyxml name=\"anyx\"/>"
3409 "<choice name=\"choice\"/>"
3410 "<container name=\"subcont\"/>"
3411 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003412 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3413 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák05404f62019-07-24 14:11:53 +02003414 "<list name=\"list\"/>"
3415 "<must condition=\"cond\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003416 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák05404f62019-07-24 14:11:53 +02003417 "<uses name=\"uses-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003418 EXT_SUBELEM
David Sedlák05404f62019-07-24 14:11:53 +02003419 "</output>"
3420 ELEMENT_WRAPPER_END;
3421 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3422 assert_null(inout.parent);
3423 assert_int_equal(inout.nodetype, LYS_OUTPUT);
3424 assert_string_equal(inout.musts->arg, "cond");
3425 assert_string_equal(inout.typedefs->name, "tpdf");
3426 assert_string_equal(inout.groupings->name, "sub-grp");
3427 assert_string_equal(inout.data->name, "anyd");
3428 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
3429 assert_string_equal(inout.data->next->name, "anyx");
3430 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
3431 assert_string_equal(inout.data->next->next->name, "choice");
3432 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
3433 assert_string_equal(inout.data->next->next->next->name, "subcont");
3434 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
3435 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
3436 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
3437 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
3438 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3439 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
3440 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
3441 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
3442 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3443 assert_null(inout.data->next->next->next->next->next->next->next->next);
David Sedlákd1144562019-08-06 12:36:14 +02003444 assert_string_equal(inout.exts[0].name, "myext:c-define");
3445 assert_int_equal(inout.exts[0].insubstmt_index, 0);
3446 assert_int_equal(inout.exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák05404f62019-07-24 14:11:53 +02003447 lysp_action_inout_free(st->ctx, &inout);
3448 memset(&inout, 0, sizeof inout);
3449
3450 /* min subelems */
3451 data = ELEMENT_WRAPPER_START "<input />" ELEMENT_WRAPPER_END;
3452 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3453 memset(&inout, 0, sizeof inout);
3454
3455 data = ELEMENT_WRAPPER_START "<output />" ELEMENT_WRAPPER_END;
3456 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3457 memset(&inout, 0, sizeof inout);
3458
3459 /* invalid combinations */
3460 data = ELEMENT_WRAPPER_START "<input name=\"test\"/>" ELEMENT_WRAPPER_END;
3461 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02003462 logbuf_assert("Unexpected attribute \"name\" of \"input\" element. Line number 1.");
David Sedlák05404f62019-07-24 14:11:53 +02003463 memset(&inout, 0, sizeof inout);
3464
3465 st->finished_correctly = true;
3466}
3467
David Sedlák85d0eca2019-07-24 15:15:21 +02003468static void
3469test_action_elem(void **state)
3470{
3471 struct state *st = *state;
3472 const char *data;
3473 struct lysp_action *actions = NULL;
3474 struct action_meta act_meta = {NULL, &actions};
3475
3476 /* max subelems */
3477 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3478 data = ELEMENT_WRAPPER_START
3479 "<action name=\"act\">"
3480 "<description><text>desc</text></description>"
3481 "<grouping name=\"grouping\"/>"
3482 "<if-feature name=\"iff\"/>"
3483 "<input><uses name=\"uses-name\"/></input>"
3484 "<output><must condition=\"cond\"/></output>"
3485 "<reference><text>ref</text></reference>"
3486 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003487 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákd1144562019-08-06 12:36:14 +02003488 EXT_SUBELEM
David Sedlák85d0eca2019-07-24 15:15:21 +02003489 "</action>"
3490 ELEMENT_WRAPPER_END;
3491 assert_int_equal(test_element_helper(st, &data, &act_meta, NULL, NULL, true), LY_SUCCESS);
3492 assert_null(actions->parent);
3493 assert_int_equal(actions->nodetype, LYS_ACTION);
3494 assert_true(actions->flags & LYS_STATUS_DEPRC);
3495 assert_string_equal(actions->name, "act");
3496 assert_string_equal(actions->dsc, "desc");
3497 assert_string_equal(actions->ref, "ref");
3498 assert_string_equal(*actions->iffeatures, "iff");
3499 assert_string_equal(actions->typedefs->name, "tpdf");
3500 assert_string_equal(actions->groupings->name, "grouping");
3501 assert_string_equal(actions->input.data->name, "uses-name");
3502 assert_string_equal(actions->output.musts->arg, "cond");
David Sedlákd1144562019-08-06 12:36:14 +02003503 assert_string_equal(actions->exts[0].name, "myext:c-define");
3504 assert_int_equal(actions->exts[0].insubstmt_index, 0);
3505 assert_int_equal(actions->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák85d0eca2019-07-24 15:15:21 +02003506 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3507 actions = NULL;
3508
David Sedlákeaa45792019-07-24 15:25:01 +02003509 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3510 data = ELEMENT_WRAPPER_START
3511 "<rpc name=\"act\">"
3512 "<description><text>desc</text></description>"
3513 "<grouping name=\"grouping\"/>"
3514 "<if-feature name=\"iff\"/>"
3515 "<input><uses name=\"uses-name\"/></input>"
3516 "<output><must condition=\"cond\"/></output>"
3517 "<reference><text>ref</text></reference>"
3518 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003519 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákd1144562019-08-06 12:36:14 +02003520 EXT_SUBELEM
David Sedlákeaa45792019-07-24 15:25:01 +02003521 "</rpc>"
3522 ELEMENT_WRAPPER_END;
3523 assert_int_equal(test_element_helper(st, &data, &act_meta, NULL, NULL, true), LY_SUCCESS);
3524 assert_null(actions->parent);
3525 assert_int_equal(actions->nodetype, LYS_ACTION);
3526 assert_true(actions->flags & LYS_STATUS_DEPRC);
3527 assert_string_equal(actions->name, "act");
3528 assert_string_equal(actions->dsc, "desc");
3529 assert_string_equal(actions->ref, "ref");
3530 assert_string_equal(*actions->iffeatures, "iff");
3531 assert_string_equal(actions->typedefs->name, "tpdf");
3532 assert_string_equal(actions->groupings->name, "grouping");
3533 assert_string_equal(actions->input.data->name, "uses-name");
3534 assert_string_equal(actions->output.musts->arg, "cond");
David Sedlákd1144562019-08-06 12:36:14 +02003535 assert_string_equal(actions->exts[0].name, "myext:c-define");
3536 assert_int_equal(actions->exts[0].insubstmt_index, 0);
3537 assert_int_equal(actions->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákeaa45792019-07-24 15:25:01 +02003538 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3539 actions = NULL;
3540
David Sedlák85d0eca2019-07-24 15:15:21 +02003541 /* min subelems */
3542 data = ELEMENT_WRAPPER_START "<action name=\"act\" />" ELEMENT_WRAPPER_END;
3543 assert_int_equal(test_element_helper(st, &data, &act_meta, NULL, NULL, true), LY_SUCCESS);
3544 assert_string_equal(actions->name, "act");
3545 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3546 actions = NULL;
3547
3548 st->finished_correctly = true;
3549}
3550
David Sedlák992fb7c2019-07-24 16:51:01 +02003551static void
3552test_augment_elem(void **state)
3553{
3554 struct state *st = *state;
3555 const char *data;
3556 struct lysp_augment *augments = NULL;
3557 struct augment_meta aug_meta = {NULL, &augments};
3558
3559 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3560 data = ELEMENT_WRAPPER_START
3561 "<augment target-node=\"target\">"
3562 "<action name=\"action\"/>"
3563 "<anydata name=\"anyd\"/>"
3564 "<anyxml name=\"anyx\"/>"
3565 "<case name=\"case\"/>"
3566 "<choice name=\"choice\"/>"
3567 "<container name=\"subcont\"/>"
3568 "<description><text>desc</text></description>"
3569 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003570 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3571 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák992fb7c2019-07-24 16:51:01 +02003572 "<list name=\"list\"/>"
3573 "<notification name=\"notif\"/>"
3574 "<reference><text>ref</text></reference>"
3575 "<status value=\"current\"/>"
3576 "<uses name=\"uses\"/>"
3577 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003578 EXT_SUBELEM
David Sedlák992fb7c2019-07-24 16:51:01 +02003579 "</augment>"
3580 ELEMENT_WRAPPER_END;
3581 assert_int_equal(test_element_helper(st, &data, &aug_meta, NULL, NULL, true), LY_SUCCESS);
3582 assert_string_equal(augments->nodeid, "target");
3583 assert_null(augments->parent);
3584 assert_int_equal(augments->nodetype, LYS_AUGMENT);
3585 assert_true(augments->flags & LYS_STATUS_CURR);
3586 assert_string_equal(augments->dsc, "desc");
3587 assert_string_equal(augments->ref, "ref");
3588 assert_string_equal(augments->when->cond, "when-cond");
3589 assert_string_equal(*augments->iffeatures, "iff");
3590 assert_string_equal(augments->child->name, "anyd");
3591 assert_int_equal(augments->child->nodetype, LYS_ANYDATA);
3592 assert_string_equal(augments->child->next->name, "anyx");
3593 assert_int_equal(augments->child->next->nodetype, LYS_ANYXML);
3594 assert_string_equal(augments->child->next->next->name, "case");
3595 assert_int_equal(augments->child->next->next->nodetype, LYS_CASE);
3596 assert_string_equal(augments->child->next->next->next->name, "choice");
3597 assert_int_equal(augments->child->next->next->next->nodetype, LYS_CHOICE);
3598 assert_string_equal(augments->child->next->next->next->next->name, "subcont");
3599 assert_int_equal(augments->child->next->next->next->next->nodetype, LYS_CONTAINER);
3600 assert_string_equal(augments->child->next->next->next->next->next->name, "leaf");
3601 assert_int_equal(augments->child->next->next->next->next->next->nodetype, LYS_LEAF);
3602 assert_string_equal(augments->child->next->next->next->next->next->next->name, "llist");
3603 assert_int_equal(augments->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3604 assert_string_equal(augments->child->next->next->next->next->next->next->next->name, "list");
3605 assert_int_equal(augments->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
3606 assert_string_equal(augments->child->next->next->next->next->next->next->next->next->name, "uses");
3607 assert_int_equal(augments->child->next->next->next->next->next->next->next->next->nodetype, LYS_USES);
3608 assert_null(augments->child->next->next->next->next->next->next->next->next->next);
3609 assert_string_equal(augments->actions->name, "action");
3610 assert_string_equal(augments->notifs->name, "notif");
David Sedlákd1144562019-08-06 12:36:14 +02003611 assert_string_equal(augments->exts[0].name, "myext:c-define");
3612 assert_int_equal(augments->exts[0].insubstmt_index, 0);
3613 assert_int_equal(augments->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák992fb7c2019-07-24 16:51:01 +02003614 FREE_ARRAY(st->ctx, augments, lysp_augment_free)
3615 augments = NULL;
3616
3617 data = ELEMENT_WRAPPER_START "<augment target-node=\"target\" />" ELEMENT_WRAPPER_END;
3618 assert_int_equal(test_element_helper(st, &data, &aug_meta, NULL, NULL, true), LY_SUCCESS);
3619 assert_string_equal(augments->nodeid, "target");
3620 FREE_ARRAY(st->ctx, augments, lysp_augment_free)
3621 augments = NULL;
3622
3623 st->finished_correctly = true;
3624}
3625
David Sedlák4ffcec82019-07-25 15:10:21 +02003626static void
3627test_deviate_elem(void **state)
3628{
3629 struct state *st = *state;
3630 const char *data;
3631 struct lysp_deviate *deviates = NULL;
3632 struct lysp_deviate_add *d_add;
3633 struct lysp_deviate_rpl *d_rpl;
3634 struct lysp_deviate_del *d_del;
3635
3636 /* all valid arguments with min subelems */
3637 data = ELEMENT_WRAPPER_START "<deviate value=\"not-supported\" />" ELEMENT_WRAPPER_END;
3638 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3639 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
3640 lysp_deviate_free(st->ctx, deviates);
3641 free(deviates);
3642 deviates = NULL;
3643
3644 data = ELEMENT_WRAPPER_START "<deviate value=\"add\" />" ELEMENT_WRAPPER_END;
3645 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3646 assert_int_equal(deviates->mod, LYS_DEV_ADD);
3647 lysp_deviate_free(st->ctx, deviates);
3648 free(deviates);
3649 deviates = NULL;
3650
3651 data = ELEMENT_WRAPPER_START "<deviate value=\"replace\" />" ELEMENT_WRAPPER_END;
3652 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3653 assert_int_equal(deviates->mod, LYS_DEV_REPLACE);
3654 lysp_deviate_free(st->ctx, deviates);
3655 free(deviates);
3656 deviates = NULL;
3657
3658 data = ELEMENT_WRAPPER_START "<deviate value=\"delete\" />" ELEMENT_WRAPPER_END;
3659 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3660 assert_int_equal(deviates->mod, LYS_DEV_DELETE);
3661 lysp_deviate_free(st->ctx, deviates);
3662 free(deviates);
3663 deviates = NULL;
3664
3665 /* max subelems and valid arguments */
3666 data = ELEMENT_WRAPPER_START
3667 "<deviate value=\"not-supported\">"
David Sedlákd1144562019-08-06 12:36:14 +02003668 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003669 "</deviate>"
3670 ELEMENT_WRAPPER_END;
3671 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3672 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
David Sedlákd1144562019-08-06 12:36:14 +02003673 assert_string_equal(deviates->exts[0].name, "myext:c-define");
3674 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3675 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003676 lysp_deviate_free(st->ctx, deviates);
3677 free(deviates);
3678 deviates = NULL;
3679
3680 data = ELEMENT_WRAPPER_START
3681 "<deviate value=\"add\">"
3682 "<units name=\"units\"/>"
3683 "<must condition=\"cond\"/>"
3684 "<unique tag=\"utag\"/>"
3685 "<default value=\"def\"/>"
3686 "<config value=\"true\"/>"
3687 "<mandatory value=\"true\"/>"
3688 "<min-elements value=\"5\"/>"
3689 "<max-elements value=\"15\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003690 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003691 "</deviate>"
3692 ELEMENT_WRAPPER_END;
3693 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3694 d_add = (struct lysp_deviate_add *)deviates;
3695 assert_int_equal(d_add->mod, LYS_DEV_ADD);
3696 assert_null(d_add->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003697 assert_string_equal(d_add->units, "units");
3698 assert_string_equal(d_add->musts->arg, "cond");
3699 assert_string_equal(*d_add->uniques, "utag");
3700 assert_string_equal(*d_add->dflts, "def");
3701 assert_true(d_add->flags & LYS_MAND_TRUE && d_add->flags & LYS_CONFIG_W);
3702 assert_int_equal(d_add->min, 5);
3703 assert_int_equal(d_add->max, 15);
David Sedlákd1144562019-08-06 12:36:14 +02003704 assert_string_equal(deviates->exts[0].name, "myext:c-define");
3705 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3706 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003707 lysp_deviate_free(st->ctx, deviates);
3708 free(deviates);
3709 deviates = NULL;
3710
3711 data = ELEMENT_WRAPPER_START
3712 "<deviate value=\"replace\">"
3713 "<type name=\"newtype\"/>"
3714 "<units name=\"uni\"/>"
3715 "<default value=\"def\"/>"
3716 "<config value=\"true\"/>"
3717 "<mandatory value=\"true\"/>"
3718 "<min-elements value=\"5\"/>"
3719 "<max-elements value=\"15\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003720 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003721 "</deviate>"
3722 ELEMENT_WRAPPER_END;
3723 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3724 d_rpl = (struct lysp_deviate_rpl *)deviates;
3725 assert_int_equal(d_rpl->mod, LYS_DEV_REPLACE);
3726 assert_null(d_rpl->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003727 assert_string_equal(d_rpl->type->name, "newtype");
3728 assert_string_equal(d_rpl->units, "uni");
3729 assert_string_equal(d_rpl->dflt, "def");
3730 assert_true(d_rpl->flags & LYS_MAND_TRUE && d_rpl->flags & LYS_CONFIG_W);
3731 assert_int_equal(d_rpl->min, 5);
3732 assert_int_equal(d_rpl->max, 15);
David Sedlákd1144562019-08-06 12:36:14 +02003733 assert_string_equal(deviates->exts[0].name, "myext:c-define");
3734 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3735 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003736 lysp_deviate_free(st->ctx, deviates);
3737 free(deviates);
3738 deviates = NULL;
3739
3740 data = ELEMENT_WRAPPER_START
3741 "<deviate value=\"delete\">"
3742 "<units name=\"u\"/>"
3743 "<must condition=\"c\"/>"
3744 "<unique tag=\"tag\"/>"
3745 "<default value=\"default\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003746 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003747 "</deviate>"
3748 ELEMENT_WRAPPER_END;
3749 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3750 d_del = (struct lysp_deviate_del *)deviates;
3751 assert_int_equal(d_del->mod, LYS_DEV_DELETE);
3752 assert_null(d_del->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003753 assert_string_equal(d_del->units, "u");
3754 assert_string_equal(d_del->musts->arg, "c");
3755 assert_string_equal(*d_del->uniques, "tag");
3756 assert_string_equal(*d_del->dflts, "default");
David Sedlákd1144562019-08-06 12:36:14 +02003757 assert_string_equal(deviates->exts[0].name, "myext:c-define");
3758 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3759 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003760 lysp_deviate_free(st->ctx, deviates);
3761 free(deviates);
3762 deviates = NULL;
3763
3764 /* invalid arguments */
3765 data = ELEMENT_WRAPPER_START "<deviate value=\"\" />" ELEMENT_WRAPPER_END;
3766 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02003767 logbuf_assert("Invalid value \"\" of \"value\" attribute in \"deviate\" element. Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003768 deviates = NULL;
3769
3770 data = ELEMENT_WRAPPER_START "<deviate value=\"invalid\" />" ELEMENT_WRAPPER_END;
3771 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02003772 logbuf_assert("Invalid value \"invalid\" of \"value\" attribute in \"deviate\" element. Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003773 deviates = NULL;
3774
3775 data = ELEMENT_WRAPPER_START "<deviate value=\"ad\" />" ELEMENT_WRAPPER_END;
3776 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02003777 logbuf_assert("Invalid value \"ad\" of \"value\" attribute in \"deviate\" element. Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003778 deviates = NULL;
3779
3780 data = ELEMENT_WRAPPER_START "<deviate value=\"adds\" />" ELEMENT_WRAPPER_END;
3781 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02003782 logbuf_assert("Invalid value \"adds\" of \"value\" attribute in \"deviate\" element. Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003783 deviates = NULL;
3784
3785 data = ELEMENT_WRAPPER_START
3786 "<deviate value=\"not-supported\">"
3787 "<must condition=\"c\"/>"
3788 "</deviate>"
3789 ELEMENT_WRAPPER_END;
3790 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
3791 logbuf_assert("Deviate of this type doesn't allow \"must\" as it's sub-element. Line number 1.");
3792
3793 st->finished_correctly = true;
3794}
3795
David Sedlák8b754462019-07-25 16:22:13 +02003796static void
3797test_deviation_elem(void **state)
3798{
3799 struct state *st = *state;
3800 const char *data;
3801 struct lysp_deviation *deviations = NULL;
3802
3803 /* min subelems */
3804 data = ELEMENT_WRAPPER_START
3805 "<deviation target-node=\"target\">"
3806 "<deviate value=\"not-supported\"/>"
3807 "</deviation>"
3808 ELEMENT_WRAPPER_END;
3809 assert_int_equal(test_element_helper(st, &data, &deviations, NULL, NULL, true), LY_SUCCESS);
3810 assert_string_equal(deviations->nodeid, "target");
3811 assert_int_equal(deviations->deviates->mod, LYS_DEV_NOT_SUPPORTED);
3812 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3813 deviations = NULL;
3814
3815 /* max subelems */
3816 data = ELEMENT_WRAPPER_START
3817 "<deviation target-node=\"target\">"
3818 "<reference><text>ref</text></reference>"
3819 "<description><text>desc</text></description>"
3820 "<deviate value=\"add\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003821 EXT_SUBELEM
David Sedlák8b754462019-07-25 16:22:13 +02003822 "</deviation>"
3823 ELEMENT_WRAPPER_END;
3824 assert_int_equal(test_element_helper(st, &data, &deviations, NULL, NULL, true), LY_SUCCESS);
3825 assert_string_equal(deviations->nodeid, "target");
3826 assert_int_equal(deviations->deviates->mod, LYS_DEV_ADD);
3827 assert_string_equal(deviations->ref, "ref");
3828 assert_string_equal(deviations->dsc, "desc");
David Sedlákd1144562019-08-06 12:36:14 +02003829 assert_string_equal(deviations->exts[0].name, "myext:c-define");
3830 assert_int_equal(deviations->exts[0].insubstmt_index, 0);
3831 assert_int_equal(deviations->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8b754462019-07-25 16:22:13 +02003832 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3833 deviations = NULL;
3834
3835 /* invalid */
3836 data = ELEMENT_WRAPPER_START "<deviation target-node=\"target\"/>" ELEMENT_WRAPPER_END;
3837 assert_int_equal(test_element_helper(st, &data, &deviations, NULL, NULL, false), LY_EVALID);
3838 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3839 deviations = NULL;
David Sedlák1538a842019-08-08 15:38:51 +02003840 logbuf_assert("Missing mandatory sub-element \"deviate\" of \"deviation\" element. Line number 1.");
3841 /* TODO */
David Sedlák8b754462019-07-25 16:22:13 +02003842 st->finished_correctly = true;
3843}
3844
David Sedlák4f03b932019-07-26 13:01:47 +02003845static void
3846test_module_elem(void **state)
3847{
3848 struct state *st = *state;
David Sedlákc5b20842019-08-13 10:18:31 +02003849 const char *data, *name, *prefix;
3850 size_t name_len, prefix_len;
David Sedlák4f03b932019-07-26 13:01:47 +02003851 struct yin_arg_record *attrs = NULL;
David Sedlák4f03b932019-07-26 13:01:47 +02003852 struct lys_module *lys_mod = NULL;
3853 struct lysp_module *lysp_mod = NULL;
3854
3855 /* max subelems */
3856 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
3857 lys_mod = calloc(1, sizeof *lys_mod);
3858 lysp_mod = calloc(1, sizeof *lysp_mod);
3859 lys_mod->ctx = st->ctx;
3860 lysp_mod->mod = lys_mod;
3861 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
3862 "<yang-version value=\"1.1\"/>\n"
3863 "<namespace uri=\"ns\"/>\n"
3864 "<prefix value=\"pref\"/>\n"
3865 "<include module=\"b-mod\"/>\n"
3866 "<import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
3867 "<organization><text>org</text></organization>\n"
3868 "<contact><text>contact</text></contact>\n"
3869 "<description><text>desc</text></description>"
3870 "<reference><text>ref</text></reference>\n"
3871 "<revision date=\"2019-02-02\"/>\n"
3872 "<anydata name=\"anyd\"/>\n"
3873 "<anyxml name=\"anyx\"/>\n"
3874 "<choice name=\"choice\"/>\n"
3875 "<container name=\"cont\"/>\n"
3876 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3877 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3878 "<list name=\"sub-list\"/>\n"
3879 "<uses name=\"uses-name\"/>\n"
3880 "<augment target-node=\"target\"/>\n"
3881 "<deviation target-node=\"target\">""<deviate value=\"not-supported\"/>""</deviation>\n"
3882 "<extension name=\"ext\"/>\n"
3883 "<feature name=\"feature\"/>\n"
3884 "<grouping name=\"grp\"/>\n"
3885 "<identity name=\"ident-name\"/>\n"
3886 "<notification name=\"notf\"/>\n"
3887 "<rpc name=\"rpc-name\"/>\n"
3888 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
David Sedlákd1144562019-08-06 12:36:14 +02003889 EXT_SUBELEM"\n"
David Sedlák4f03b932019-07-26 13:01:47 +02003890 "</module>\n";
David Sedlákc5b20842019-08-13 10:18:31 +02003891 assert_int_equal(lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len), LY_SUCCESS);
David Sedlák4f03b932019-07-26 13:01:47 +02003892 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
3893 assert_int_equal(yin_parse_mod(st->yin_ctx, attrs, &data, lysp_mod), LY_SUCCESS);
3894 assert_string_equal(lysp_mod->mod->name, "mod");
3895 assert_string_equal(lysp_mod->revs, "2019-02-02");
3896 assert_string_equal(lysp_mod->mod->ns, "ns");
3897 assert_string_equal(lysp_mod->mod->prefix, "pref");
3898 assert_null(lysp_mod->mod->filepath);
3899 assert_string_equal(lysp_mod->mod->org, "org");
3900 assert_string_equal(lysp_mod->mod->contact, "contact");
3901 assert_string_equal(lysp_mod->mod->dsc, "desc");
3902 assert_string_equal(lysp_mod->mod->ref, "ref");
3903 assert_int_equal(lysp_mod->mod->version, LYS_VERSION_1_1);
3904 assert_string_equal(lysp_mod->imports->name, "a-mod");
3905 assert_string_equal(lysp_mod->includes->name, "b-mod");
3906 assert_string_equal(lysp_mod->extensions->name, "ext");
3907 assert_string_equal(lysp_mod->features->name, "feature");
3908 assert_string_equal(lysp_mod->identities->name, "ident-name");
3909 assert_string_equal(lysp_mod->typedefs->name, "tpdf");
3910 assert_string_equal(lysp_mod->groupings->name, "grp");
3911 assert_string_equal(lysp_mod->data->name, "anyd");
3912 assert_int_equal(lysp_mod->data->nodetype, LYS_ANYDATA);
3913 assert_string_equal(lysp_mod->data->next->name, "anyx");
3914 assert_int_equal(lysp_mod->data->next->nodetype, LYS_ANYXML);
3915 assert_string_equal(lysp_mod->data->next->next->name, "choice");
3916 assert_int_equal(lysp_mod->data->next->next->nodetype, LYS_CHOICE);
3917 assert_string_equal(lysp_mod->data->next->next->next->name, "cont");
3918 assert_int_equal(lysp_mod->data->next->next->next->nodetype, LYS_CONTAINER);
3919 assert_string_equal(lysp_mod->data->next->next->next->next->name, "leaf");
3920 assert_int_equal(lysp_mod->data->next->next->next->next->nodetype, LYS_LEAF);
3921 assert_string_equal(lysp_mod->data->next->next->next->next->next->name, "llist");
3922 assert_int_equal(lysp_mod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3923 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->name, "sub-list");
3924 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
3925 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->next->name, "uses-name");
3926 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3927 assert_null(lysp_mod->data->next->next->next->next->next->next->next->next);
3928 assert_string_equal(lysp_mod->augments->nodeid, "target");
3929 assert_string_equal(lysp_mod->rpcs->name, "rpc-name");
3930 assert_string_equal(lysp_mod->notifs->name, "notf");
3931 assert_string_equal(lysp_mod->deviations->nodeid, "target");
David Sedlákd1144562019-08-06 12:36:14 +02003932 assert_string_equal(lysp_mod->exts[0].name, "myext:c-define");
3933 assert_int_equal(lysp_mod->exts[0].insubstmt_index, 0);
3934 assert_int_equal(lysp_mod->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4f03b932019-07-26 13:01:47 +02003935 lysp_module_free(lysp_mod);
3936 lys_module_free(lys_mod, NULL);
3937 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
3938 attrs = NULL;
3939
3940 /* min subelems */
3941 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
3942 lys_mod = calloc(1, sizeof *lys_mod);
3943 lysp_mod = calloc(1, sizeof *lysp_mod);
3944 lys_mod->ctx = st->ctx;
3945 lysp_mod->mod = lys_mod;
3946 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">"
3947 "<namespace uri=\"ns\"/>"
3948 "<prefix value=\"pref\"/>"
3949 "<yang-version value=\"1.1\"/>"
3950 "</module>";
David Sedlákc5b20842019-08-13 10:18:31 +02003951 assert_int_equal(lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len), LY_SUCCESS);
David Sedlák4f03b932019-07-26 13:01:47 +02003952 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
3953 assert_int_equal(yin_parse_mod(st->yin_ctx, attrs, &data, lysp_mod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003954 assert_string_equal(lysp_mod->mod->name, "mod");
David Sedlák4f03b932019-07-26 13:01:47 +02003955 lysp_module_free(lysp_mod);
3956 lys_module_free(lys_mod, NULL);
3957 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
3958 attrs = NULL;
David Sedlák298ff6d2019-07-26 14:29:03 +02003959
David Sedláke6cd89e2019-08-07 12:46:02 +02003960 /* incorrect subelem order */
3961 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
3962 lys_mod = calloc(1, sizeof *lys_mod);
3963 lysp_mod = calloc(1, sizeof *lysp_mod);
3964 lys_mod->ctx = st->ctx;
3965 lysp_mod->mod = lys_mod;
3966 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">"
3967 "<feature name=\"feature\"/>\n"
3968 "<namespace uri=\"ns\"/>"
3969 "<prefix value=\"pref\"/>"
3970 "<yang-version value=\"1.1\"/>"
3971 "</module>";
David Sedlákc5b20842019-08-13 10:18:31 +02003972 assert_int_equal(lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len), LY_SUCCESS);
David Sedláke6cd89e2019-08-07 12:46:02 +02003973 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
3974 assert_int_equal(yin_parse_mod(st->yin_ctx, attrs, &data, lysp_mod), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02003975 logbuf_assert("Invalid order of module\'s sub-elements \"namespace\" can\'t appear after \"feature\". Line number 30.");
David Sedláke6cd89e2019-08-07 12:46:02 +02003976 lysp_module_free(lysp_mod);
3977 lys_module_free(lys_mod, NULL);
3978 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
3979 attrs = NULL;
3980
David Sedlák298ff6d2019-07-26 14:29:03 +02003981 st->finished_correctly = true;
3982}
3983
3984static void
3985test_submodule_elem(void **state)
3986{
3987 struct state *st = *state;
David Sedlákc5b20842019-08-13 10:18:31 +02003988 const char *data, *name, *prefix;
3989 size_t name_len, prefix_len;
David Sedlák298ff6d2019-07-26 14:29:03 +02003990 struct yin_arg_record *attrs = NULL;
David Sedlák298ff6d2019-07-26 14:29:03 +02003991 struct lysp_submodule *lysp_submod = NULL;
3992
3993 /* max subelements */
3994 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
3995 lysp_submod = calloc(1, sizeof *lysp_submod);
3996 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
3997 "<yang-version value=\"1.1\"/>\n"
3998 "<belongs-to module=\"mod-name\"><prefix value=\"pref\"/></belongs-to>"
3999 "<include module=\"b-mod\"/>\n"
4000 "<import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
4001 "<organization><text>org</text></organization>\n"
4002 "<contact><text>contact</text></contact>\n"
4003 "<description><text>desc</text></description>"
4004 "<reference><text>ref</text></reference>\n"
4005 "<revision date=\"2019-02-02\"/>\n"
4006 "<anydata name=\"anyd\"/>\n"
4007 "<anyxml name=\"anyx\"/>\n"
4008 "<choice name=\"choice\"/>\n"
4009 "<container name=\"cont\"/>\n"
4010 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
4011 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
4012 "<list name=\"sub-list\"/>\n"
4013 "<uses name=\"uses-name\"/>\n"
4014 "<augment target-node=\"target\"/>\n"
4015 "<deviation target-node=\"target\">""<deviate value=\"not-supported\"/>""</deviation>\n"
4016 "<extension name=\"ext\"/>\n"
4017 "<feature name=\"feature\"/>\n"
4018 "<grouping name=\"grp\"/>\n"
4019 "<identity name=\"ident-name\"/>\n"
4020 "<notification name=\"notf\"/>\n"
4021 "<rpc name=\"rpc-name\"/>\n"
4022 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
David Sedlákd1144562019-08-06 12:36:14 +02004023 EXT_SUBELEM"\n"
David Sedlák298ff6d2019-07-26 14:29:03 +02004024 "</submodule>\n";
David Sedlákc5b20842019-08-13 10:18:31 +02004025 assert_int_equal(lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02004026 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
4027 assert_int_equal(yin_parse_submod(st->yin_ctx, attrs, &data, lysp_submod), LY_SUCCESS);
4028
4029 assert_string_equal(lysp_submod->name, "mod");
4030 assert_string_equal(lysp_submod->revs, "2019-02-02");
David Sedlák298ff6d2019-07-26 14:29:03 +02004031 assert_string_equal(lysp_submod->prefix, "pref");
4032 assert_null(lysp_submod->filepath);
4033 assert_string_equal(lysp_submod->org, "org");
4034 assert_string_equal(lysp_submod->contact, "contact");
4035 assert_string_equal(lysp_submod->dsc, "desc");
4036 assert_string_equal(lysp_submod->ref, "ref");
4037 assert_int_equal(lysp_submod->version, LYS_VERSION_1_1);
4038 assert_string_equal(lysp_submod->imports->name, "a-mod");
4039 assert_string_equal(lysp_submod->includes->name, "b-mod");
4040 assert_string_equal(lysp_submod->extensions->name, "ext");
4041 assert_string_equal(lysp_submod->features->name, "feature");
4042 assert_string_equal(lysp_submod->identities->name, "ident-name");
4043 assert_string_equal(lysp_submod->typedefs->name, "tpdf");
4044 assert_string_equal(lysp_submod->groupings->name, "grp");
4045 assert_string_equal(lysp_submod->data->name, "anyd");
4046 assert_int_equal(lysp_submod->data->nodetype, LYS_ANYDATA);
4047 assert_string_equal(lysp_submod->data->next->name, "anyx");
4048 assert_int_equal(lysp_submod->data->next->nodetype, LYS_ANYXML);
4049 assert_string_equal(lysp_submod->data->next->next->name, "choice");
4050 assert_int_equal(lysp_submod->data->next->next->nodetype, LYS_CHOICE);
4051 assert_string_equal(lysp_submod->data->next->next->next->name, "cont");
4052 assert_int_equal(lysp_submod->data->next->next->next->nodetype, LYS_CONTAINER);
4053 assert_string_equal(lysp_submod->data->next->next->next->next->name, "leaf");
4054 assert_int_equal(lysp_submod->data->next->next->next->next->nodetype, LYS_LEAF);
4055 assert_string_equal(lysp_submod->data->next->next->next->next->next->name, "llist");
4056 assert_int_equal(lysp_submod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
4057 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->name, "sub-list");
4058 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
4059 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->next->name, "uses-name");
4060 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
4061 assert_null(lysp_submod->data->next->next->next->next->next->next->next->next);
4062 assert_string_equal(lysp_submod->augments->nodeid, "target");
4063 assert_string_equal(lysp_submod->rpcs->name, "rpc-name");
4064 assert_string_equal(lysp_submod->notifs->name, "notf");
4065 assert_string_equal(lysp_submod->deviations->nodeid, "target");
David Sedlákd1144562019-08-06 12:36:14 +02004066 assert_string_equal(lysp_submod->exts[0].name, "myext:c-define");
4067 assert_int_equal(lysp_submod->exts[0].insubstmt_index, 0);
4068 assert_int_equal(lysp_submod->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák298ff6d2019-07-26 14:29:03 +02004069
4070 lysp_submodule_free(st->ctx, lysp_submod);
4071 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
4072 attrs = NULL;
4073
4074 /* min subelemnts */
4075 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
4076 lysp_submod = calloc(1, sizeof *lysp_submod);
4077 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">"
4078 "<yang-version value=\"1.0\"/>"
4079 "<belongs-to module=\"mod-name\"><prefix value=\"pref\"/></belongs-to>"
4080 "</submodule>";
David Sedlákc5b20842019-08-13 10:18:31 +02004081 assert_int_equal(lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02004082 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
4083 assert_int_equal(yin_parse_submod(st->yin_ctx, attrs, &data, lysp_submod), LY_SUCCESS);
4084 assert_string_equal(lysp_submod->prefix, "pref");
4085 assert_string_equal(lysp_submod->belongsto, "mod-name");
4086 assert_int_equal(lysp_submod->version, LYS_VERSION_1_0);
4087 lysp_submodule_free(st->ctx, lysp_submod);
4088 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
4089 attrs = NULL;
4090
David Sedláke6cd89e2019-08-07 12:46:02 +02004091 /* incorrect subelem order */
4092 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
4093 lysp_submod = calloc(1, sizeof *lysp_submod);
4094 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">"
4095 "<yang-version value=\"1.0\"/>"
4096 "<reference><text>ref</text></reference>\n"
4097 "<belongs-to module=\"mod-name\"><prefix value=\"pref\"/></belongs-to>"
4098 "</submodule>";
David Sedlákc5b20842019-08-13 10:18:31 +02004099 assert_int_equal(lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len), LY_SUCCESS);
David Sedláke6cd89e2019-08-07 12:46:02 +02004100 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
4101 assert_int_equal(yin_parse_submod(st->yin_ctx, attrs, &data, lysp_submod), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02004102 logbuf_assert("Invalid order of submodule's sub-elements \"belongs-to\" can't appear after \"reference\". Line number 28.");
David Sedláke6cd89e2019-08-07 12:46:02 +02004103 lysp_submodule_free(st->ctx, lysp_submod);
4104 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
4105 attrs = NULL;
4106
David Sedlák298ff6d2019-07-26 14:29:03 +02004107 st->finished_correctly = true;
David Sedlák4f03b932019-07-26 13:01:47 +02004108}
4109
David Sedlák8985a142019-07-31 16:43:06 +02004110static void
4111test_yin_parse_module(void **state)
4112{
4113 struct state *st = *state;
4114 const char *data;
4115 struct lys_module *mod;
4116 struct yin_parser_ctx *yin_ctx = NULL;
4117
4118 mod = calloc(1, sizeof *mod);
4119 mod->ctx = st->ctx;
4120 data = "<module name=\"example-foo\""
4121 "xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
4122 "xmlns:foo=\"urn:example:foo\""
4123 "xmlns:myext=\"urn:example:extensions\">\n"
4124
4125 "<yang-version value=\"1.0\"/>\n"
4126
4127 "<namespace uri=\"urn:example:foo\"/>\n"
4128 "<prefix value=\"foo\"/>\n"
4129
4130 "<import module=\"example-extensions\">\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004131 "<prefix value=\"myext\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02004132 "</import>\n"
4133
4134 "<list name=\"interface\">\n"
4135 "<key value=\"name\"/>\n"
4136 "<leaf name=\"name\">\n"
4137 "<type name=\"string\"/>\n"
4138 "</leaf>\n"
4139 "<leaf name=\"mtu\">\n"
4140 "<type name=\"uint32\"/>\n"
4141 "<description>\n"
4142 "<text>The MTU of the interface.</text>\n"
4143 "</description>\n"
4144 "<myext:c-define name=\"MY_MTU\"/>\n"
4145 "</leaf>\n"
4146 "</list>\n"
4147 "</module>\n";
4148 assert_int_equal(yin_parse_module(&yin_ctx, data, mod), LY_SUCCESS);
4149 lys_module_free(mod, NULL);
4150 yin_parser_ctx_free(yin_ctx);
4151 mod = NULL;
4152 yin_ctx = NULL;
4153
4154 mod = calloc(1, sizeof *mod);
4155 mod->ctx = st->ctx;
David Sedlák6d781b62019-08-02 15:22:52 +02004156 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
4157 "<yang-version value=\"1.0\"/>\n"
4158 "<namespace uri=\"urn:example:foo\"/>\n"
4159 "<prefix value=\"foo\"/>\n"
4160 "</module>\n";
4161 assert_int_equal(yin_parse_module(&yin_ctx, data, mod), LY_SUCCESS);
4162 lys_module_free(mod, NULL);
4163 yin_parser_ctx_free(yin_ctx);
4164 mod = NULL;
4165 yin_ctx = NULL;
4166
4167
4168 mod = calloc(1, sizeof *mod);
4169 mod->ctx = st->ctx;
David Sedlák8985a142019-07-31 16:43:06 +02004170 data = "<submodule name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
4171 "</submodule>\n";
4172 assert_int_equal(yin_parse_module(&yin_ctx, data, mod), LY_EINVAL);
4173 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
4174 lys_module_free(mod, NULL);
4175 yin_parser_ctx_free(yin_ctx);
4176
David Sedlák6d781b62019-08-02 15:22:52 +02004177 mod = calloc(1, sizeof *mod);
4178 mod->ctx = st->ctx;
4179 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
4180 "<yang-version value=\"1.0\"/>\n"
4181 "<namespace uri=\"urn:example:foo\"/>\n"
4182 "<prefix value=\"foo\"/>\n"
4183 "</module>"
4184 "<module>";
4185 assert_int_equal(yin_parse_module(&yin_ctx, data, mod), LY_EVALID);
4186 logbuf_assert("Trailing garbage \"<module>\" after module, expected end-of-input. Line number 5.");
4187 lys_module_free(mod, NULL);
4188 yin_parser_ctx_free(yin_ctx);
4189 mod = NULL;
4190 yin_ctx = NULL;
4191
David Sedlák8985a142019-07-31 16:43:06 +02004192 st->finished_correctly = true;
4193}
4194
4195static void
4196test_yin_parse_submodule(void **state)
4197{
4198 struct state *st = *state;
4199 const char *data;
4200 struct yin_parser_ctx *yin_ctx = NULL;
4201 struct lysp_submodule *submod = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02004202 struct lys_parser_ctx main_ctx = {};
David Sedlák8985a142019-07-31 16:43:06 +02004203
4204 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
4205 "<submodule name=\"asub\""
4206 "xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
4207 "xmlns:a=\"urn:a\">"
4208 "<yang-version value=\"1.0\"/>\n"
4209 "<belongs-to module=\"a\">"
4210 "<prefix value=\"a_pref\"/>"
4211 "</belongs-to>"
4212 "<include module=\"atop\"/>"
4213 "<feature name=\"fox\"/>"
4214 "<notification name=\"bar-notif\">"
4215 "<if-feature name=\"bar\"/>"
4216 "</notification>"
4217 "<notification name=\"fox-notif\">"
4218 "<if-feature name=\"fox\"/>"
4219 "</notification>"
4220 "<augment target-node=\"/a_pref:top\">"
4221 "<if-feature name=\"bar\"/>"
4222 "<container name=\"bar-sub\"/>"
4223 "</augment>"
4224 "<augment target-node=\"/top\">"
4225 "<container name=\"bar-sub2\"/>"
4226 "</augment>"
4227 "</submodule>";
David Sedlák1b623122019-08-05 15:27:49 +02004228 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, &main_ctx, data, &submod), LY_SUCCESS);
David Sedlák8985a142019-07-31 16:43:06 +02004229 lysp_submodule_free(st->ctx, submod);
4230 yin_parser_ctx_free(yin_ctx);
4231 yin_ctx = NULL;
4232 submod = NULL;
4233
4234 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
David Sedlák6d781b62019-08-02 15:22:52 +02004235 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
4236 "<yang-version value=\"1.0\"/>\n"
4237 "<belongs-to module=\"a\">"
4238 "<prefix value=\"a_pref\"/>"
4239 "</belongs-to>"
4240 "</submodule>";
David Sedlák1b623122019-08-05 15:27:49 +02004241 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, &main_ctx, data, &submod), LY_SUCCESS);
David Sedlák6d781b62019-08-02 15:22:52 +02004242 lysp_submodule_free(st->ctx, submod);
4243 yin_parser_ctx_free(yin_ctx);
4244 yin_ctx = NULL;
4245 submod = NULL;
4246
4247 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
David Sedlák8985a142019-07-31 16:43:06 +02004248 "<module name=\"inval\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
4249 "</module>";
David Sedlák1b623122019-08-05 15:27:49 +02004250 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, &main_ctx, data, &submod), LY_EINVAL);
David Sedlák8985a142019-07-31 16:43:06 +02004251 logbuf_assert("Input data contains module in situation when a submodule is expected.");
4252 lysp_submodule_free(st->ctx, submod);
4253 yin_parser_ctx_free(yin_ctx);
4254 yin_ctx = NULL;
4255 submod = NULL;
4256
David Sedlák6d781b62019-08-02 15:22:52 +02004257 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
4258 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
4259 "<yang-version value=\"1.0\"/>\n"
4260 "<belongs-to module=\"a\">"
4261 "<prefix value=\"a_pref\"/>"
4262 "</belongs-to>"
4263 "</submodule>"
4264 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
4265 "<yang-version value=\"1.0\"/>\n"
4266 "<belongs-to module=\"a\">"
4267 "<prefix value=\"a_pref\"/>"
4268 "</belongs-to>"
4269 "</submodule>";
David Sedlák1b623122019-08-05 15:27:49 +02004270 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, &main_ctx, data, &submod), LY_EVALID);
David Sedlák6d781b62019-08-02 15:22:52 +02004271 logbuf_assert("Trailing garbage \"<submodule name...\" after submodule, expected end-of-input. Line number 2.");
4272 lysp_submodule_free(st->ctx, submod);
4273 yin_parser_ctx_free(yin_ctx);
4274 yin_ctx = NULL;
4275 submod = NULL;
4276
David Sedlák8985a142019-07-31 16:43:06 +02004277 st->finished_correctly = true;
4278}
4279
David Sedlák3b4db242018-10-19 16:11:01 +02004280int
4281main(void)
4282{
4283
4284 const struct CMUnitTest tests[] = {
David Sedlák8f7a1172019-06-20 14:42:18 +02004285 cmocka_unit_test_setup_teardown(test_yin_match_keyword, setup_f, teardown_f),
David Sedlákb1a78352019-06-28 16:16:29 +02004286 cmocka_unit_test_setup_teardown(test_yin_parse_element_generic, setup_f, teardown_f),
4287 cmocka_unit_test_setup_teardown(test_yin_parse_extension_instance, setup_f, teardown_f),
David Sedlák555c7202019-07-04 12:14:12 +02004288 cmocka_unit_test_setup_teardown(test_yin_parse_content, setup_f, teardown_f),
David Sedlák4a650532019-07-10 11:55:18 +02004289 cmocka_unit_test_setup_teardown(test_validate_value, setup_f, teardown_f),
David Sedlák32488102019-07-15 17:44:10 +02004290
David Sedlák8e7bda82019-07-16 17:57:50 +02004291 cmocka_unit_test(test_yin_match_argument_name),
David Sedlák43801c92019-08-05 15:58:54 +02004292 cmocka_unit_test_setup_teardown(test_enum_elem, setup_element_test, teardown_element_test),
4293 cmocka_unit_test_setup_teardown(test_bit_elem, setup_element_test, teardown_element_test),
David Sedlák32488102019-07-15 17:44:10 +02004294 cmocka_unit_test_setup_teardown(test_meta_elem, setup_element_test, teardown_element_test),
4295 cmocka_unit_test_setup_teardown(test_import_elem, setup_element_test, teardown_element_test),
4296 cmocka_unit_test_setup_teardown(test_status_elem, setup_element_test, teardown_element_test),
4297 cmocka_unit_test_setup_teardown(test_ext_elem, setup_element_test, teardown_element_test),
4298 cmocka_unit_test_setup_teardown(test_yin_element_elem, setup_element_test, teardown_element_test),
4299 cmocka_unit_test_setup_teardown(test_yangversion_elem, setup_element_test, teardown_element_test),
4300 cmocka_unit_test_setup_teardown(test_mandatory_elem, setup_element_test, teardown_element_test),
David Sedlák8e7bda82019-07-16 17:57:50 +02004301 cmocka_unit_test_setup_teardown(test_argument_elem, setup_element_test, teardown_element_test),
4302 cmocka_unit_test_setup_teardown(test_base_elem, setup_element_test, teardown_element_test),
4303 cmocka_unit_test_setup_teardown(test_belongsto_elem, setup_element_test, teardown_element_test),
4304 cmocka_unit_test_setup_teardown(test_config_elem, setup_element_test, teardown_element_test),
4305 cmocka_unit_test_setup_teardown(test_default_elem, setup_element_test, teardown_element_test),
4306 cmocka_unit_test_setup_teardown(test_err_app_tag_elem, setup_element_test, teardown_element_test),
4307 cmocka_unit_test_setup_teardown(test_err_msg_elem, setup_element_test, teardown_element_test),
4308 cmocka_unit_test_setup_teardown(test_fracdigits_elem, setup_element_test, teardown_element_test),
4309 cmocka_unit_test_setup_teardown(test_iffeature_elem, setup_element_test, teardown_element_test),
4310 cmocka_unit_test_setup_teardown(test_length_elem, setup_element_test, teardown_element_test),
4311 cmocka_unit_test_setup_teardown(test_modifier_elem, setup_element_test, teardown_element_test),
4312 cmocka_unit_test_setup_teardown(test_namespace_elem, setup_element_test, teardown_element_test),
4313 cmocka_unit_test_setup_teardown(test_path_elem, setup_element_test, teardown_element_test),
4314 cmocka_unit_test_setup_teardown(test_pattern_elem, setup_element_test, teardown_element_test),
4315 cmocka_unit_test_setup_teardown(test_value_position_elem, setup_element_test, teardown_element_test),
David Sedlák69f01612019-07-17 11:41:08 +02004316 cmocka_unit_test_setup_teardown(test_prefix_elem, setup_element_test, teardown_element_test),
4317 cmocka_unit_test_setup_teardown(test_range_elem, setup_element_test, teardown_element_test),
4318 cmocka_unit_test_setup_teardown(test_reqinstance_elem, setup_element_test, teardown_element_test),
4319 cmocka_unit_test_setup_teardown(test_revision_date_elem, setup_element_test, teardown_element_test),
4320 cmocka_unit_test_setup_teardown(test_unique_elem, setup_element_test, teardown_element_test),
4321 cmocka_unit_test_setup_teardown(test_units_elem, setup_element_test, teardown_element_test),
4322 cmocka_unit_test_setup_teardown(test_when_elem, setup_element_test, teardown_element_test),
4323 cmocka_unit_test_setup_teardown(test_yin_text_value_elem, setup_element_test, teardown_element_test),
David Sedlák374d2b32019-07-17 15:06:55 +02004324 cmocka_unit_test_setup_teardown(test_type_elem, setup_element_test, teardown_element_test),
David Sedlák1af868e2019-07-17 17:03:14 +02004325 cmocka_unit_test_setup_teardown(test_max_elems_elem, setup_element_test, teardown_element_test),
David Sedlák09e18c92019-07-18 11:17:11 +02004326 cmocka_unit_test_setup_teardown(test_min_elems_elem, setup_element_test, teardown_element_test),
David Sedláka2dad212019-07-18 12:45:19 +02004327 cmocka_unit_test_setup_teardown(test_ordby_elem, setup_element_test, teardown_element_test),
David Sedlák8a83bbb2019-07-18 14:46:00 +02004328 cmocka_unit_test_setup_teardown(test_any_elem, setup_element_test, teardown_element_test),
David Sedlák203ca3a2019-07-18 15:26:25 +02004329 cmocka_unit_test_setup_teardown(test_leaf_elem, setup_element_test, teardown_element_test),
David Sedlákc3da3ef2019-07-19 12:56:08 +02004330 cmocka_unit_test_setup_teardown(test_leaf_list_elem, setup_element_test, teardown_element_test),
David Sedlákcb39f642019-07-19 13:19:55 +02004331 cmocka_unit_test_setup_teardown(test_presence_elem, setup_element_test, teardown_element_test),
David Sedlák12470a82019-07-19 13:44:36 +02004332 cmocka_unit_test_setup_teardown(test_key_elem, setup_element_test, teardown_element_test),
David Sedlák04e17b22019-07-19 15:29:48 +02004333 cmocka_unit_test_setup_teardown(test_typedef_elem, setup_element_test, teardown_element_test),
David Sedlákd2d676a2019-07-22 11:28:19 +02004334 cmocka_unit_test_setup_teardown(test_refine_elem, setup_element_test, teardown_element_test),
David Sedlák0d6de5a2019-07-22 13:25:44 +02004335 cmocka_unit_test_setup_teardown(test_uses_elem, setup_element_test, teardown_element_test),
David Sedlákaa854b02019-07-22 14:17:10 +02004336 cmocka_unit_test_setup_teardown(test_revision_elem, setup_element_test, teardown_element_test),
David Sedlák0c2bab92019-07-22 15:33:19 +02004337 cmocka_unit_test_setup_teardown(test_include_elem, setup_element_test, teardown_element_test),
David Sedlák5e13dea2019-07-22 16:06:45 +02004338 cmocka_unit_test_setup_teardown(test_feature_elem, setup_element_test, teardown_element_test),
David Sedlák28794f22019-07-22 16:45:00 +02004339 cmocka_unit_test_setup_teardown(test_identity_elem, setup_element_test, teardown_element_test),
David Sedlákaf536aa2019-07-23 13:42:23 +02004340 cmocka_unit_test_setup_teardown(test_list_elem, setup_element_test, teardown_element_test),
David Sedlák031b9e72019-07-23 15:19:37 +02004341 cmocka_unit_test_setup_teardown(test_notification_elem, setup_element_test, teardown_element_test),
David Sedláke3ce9ef2019-07-23 16:34:30 +02004342 cmocka_unit_test_setup_teardown(test_grouping_elem, setup_element_test, teardown_element_test),
David Sedlákf111bcb2019-07-23 17:15:51 +02004343 cmocka_unit_test_setup_teardown(test_container_elem, setup_element_test, teardown_element_test),
David Sedlák5379d392019-07-24 10:42:03 +02004344 cmocka_unit_test_setup_teardown(test_case_elem, setup_element_test, teardown_element_test),
David Sedlákb7abcfa2019-07-24 12:33:35 +02004345 cmocka_unit_test_setup_teardown(test_choice_elem, setup_element_test, teardown_element_test),
David Sedlák05404f62019-07-24 14:11:53 +02004346 cmocka_unit_test_setup_teardown(test_inout_elem, setup_element_test, teardown_element_test),
David Sedlák85d0eca2019-07-24 15:15:21 +02004347 cmocka_unit_test_setup_teardown(test_action_elem, setup_element_test, teardown_element_test),
David Sedlák992fb7c2019-07-24 16:51:01 +02004348 cmocka_unit_test_setup_teardown(test_augment_elem, setup_element_test, teardown_element_test),
David Sedlák4ffcec82019-07-25 15:10:21 +02004349 cmocka_unit_test_setup_teardown(test_deviate_elem, setup_element_test, teardown_element_test),
David Sedlák8b754462019-07-25 16:22:13 +02004350 cmocka_unit_test_setup_teardown(test_deviation_elem, setup_element_test, teardown_element_test),
David Sedlák4f03b932019-07-26 13:01:47 +02004351 cmocka_unit_test_setup_teardown(test_module_elem, setup_element_test, teardown_element_test),
David Sedlák298ff6d2019-07-26 14:29:03 +02004352 cmocka_unit_test_setup_teardown(test_submodule_elem, setup_element_test, teardown_element_test),
David Sedlák8985a142019-07-31 16:43:06 +02004353
4354 cmocka_unit_test_setup_teardown(test_yin_parse_module, setup_logger, teardown_logger),
4355 cmocka_unit_test_setup_teardown(test_yin_parse_submodule, setup_logger, teardown_logger),
David Sedlák3b4db242018-10-19 16:11:01 +02004356 };
4357
David Sedlák8e7bda82019-07-16 17:57:50 +02004358 return cmocka_run_group_tests(tests, setup_ly_ctx, destroy_ly_ctx);
David Sedlák3b4db242018-10-19 16:11:01 +02004359}