blob: 5ef068145ec165b43548080b7429266bbd2cb229 [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ák5c2ed5f2019-08-19 12:00:29 +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
Radek Krejcid6b76452019-09-03 17:03:03 +0200268 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydatax", strlen("anydatax"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
269 assert_int_equal(yin_match_keyword(st->yin_ctx, "asdasd", strlen("asdasd"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
270 assert_int_equal(yin_match_keyword(st->yin_ctx, "", 0, prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
271 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydata", strlen("anydata"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ANYDATA);
272 assert_int_equal(yin_match_keyword(st->yin_ctx, "anyxml", strlen("anyxml"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ANYXML);
273 assert_int_equal(yin_match_keyword(st->yin_ctx, "argument", strlen("argument"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ARGUMENT);
274 assert_int_equal(yin_match_keyword(st->yin_ctx, "augment", strlen("augment"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_AUGMENT);
275 assert_int_equal(yin_match_keyword(st->yin_ctx, "base", strlen("base"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BASE);
276 assert_int_equal(yin_match_keyword(st->yin_ctx, "belongs-to", strlen("belongs-to"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BELONGS_TO);
277 assert_int_equal(yin_match_keyword(st->yin_ctx, "bit", strlen("bit"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BIT);
278 assert_int_equal(yin_match_keyword(st->yin_ctx, "case", strlen("case"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CASE);
279 assert_int_equal(yin_match_keyword(st->yin_ctx, "choice", strlen("choice"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CHOICE);
280 assert_int_equal(yin_match_keyword(st->yin_ctx, "config", strlen("config"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONFIG);
281 assert_int_equal(yin_match_keyword(st->yin_ctx, "contact", strlen("contact"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONTACT);
282 assert_int_equal(yin_match_keyword(st->yin_ctx, "container", strlen("container"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONTAINER);
283 assert_int_equal(yin_match_keyword(st->yin_ctx, "default", strlen("default"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEFAULT);
284 assert_int_equal(yin_match_keyword(st->yin_ctx, "description", strlen("description"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DESCRIPTION);
285 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviate", strlen("deviate"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEVIATE);
286 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviation", strlen("deviation"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEVIATION);
287 assert_int_equal(yin_match_keyword(st->yin_ctx, "enum", strlen("enum"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ENUM);
288 assert_int_equal(yin_match_keyword(st->yin_ctx, "error-app-tag", strlen("error-app-tag"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ERROR_APP_TAG);
289 assert_int_equal(yin_match_keyword(st->yin_ctx, "error-message", strlen("error-message"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ERROR_MESSAGE);
290 assert_int_equal(yin_match_keyword(st->yin_ctx, "extension", strlen("extension"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_EXTENSION);
291 assert_int_equal(yin_match_keyword(st->yin_ctx, "feature", strlen("feature"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_FEATURE);
292 assert_int_equal(yin_match_keyword(st->yin_ctx, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_FRACTION_DIGITS);
293 assert_int_equal(yin_match_keyword(st->yin_ctx, "grouping", strlen("grouping"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_GROUPING);
294 assert_int_equal(yin_match_keyword(st->yin_ctx, "identity", strlen("identity"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IDENTITY);
295 assert_int_equal(yin_match_keyword(st->yin_ctx, "if-feature", strlen("if-feature"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IF_FEATURE);
296 assert_int_equal(yin_match_keyword(st->yin_ctx, "import", strlen("import"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IMPORT);
297 assert_int_equal(yin_match_keyword(st->yin_ctx, "include", strlen("include"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_INCLUDE);
298 assert_int_equal(yin_match_keyword(st->yin_ctx, "input", strlen("input"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_INPUT);
299 assert_int_equal(yin_match_keyword(st->yin_ctx, "key", strlen("key"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_KEY);
300 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf", strlen("leaf"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LEAF);
301 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf-list", strlen("leaf-list"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LEAF_LIST);
302 assert_int_equal(yin_match_keyword(st->yin_ctx, "length", strlen("length"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LENGTH);
303 assert_int_equal(yin_match_keyword(st->yin_ctx, "list", strlen("list"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LIST);
304 assert_int_equal(yin_match_keyword(st->yin_ctx, "mandatory", strlen("mandatory"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MANDATORY);
305 assert_int_equal(yin_match_keyword(st->yin_ctx, "max-elements", strlen("max-elements"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MAX_ELEMENTS);
306 assert_int_equal(yin_match_keyword(st->yin_ctx, "min-elements", strlen("min-elements"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MIN_ELEMENTS);
307 assert_int_equal(yin_match_keyword(st->yin_ctx, "modifier", strlen("modifier"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MODIFIER);
308 assert_int_equal(yin_match_keyword(st->yin_ctx, "module", strlen("module"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MODULE);
309 assert_int_equal(yin_match_keyword(st->yin_ctx, "must", strlen("must"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MUST);
310 assert_int_equal(yin_match_keyword(st->yin_ctx, "namespace", strlen("namespace"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NAMESPACE);
311 assert_int_equal(yin_match_keyword(st->yin_ctx, "notification", strlen("notification"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NOTIFICATION);
312 assert_int_equal(yin_match_keyword(st->yin_ctx, "ordered-by", strlen("ordered-by"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ORDERED_BY);
313 assert_int_equal(yin_match_keyword(st->yin_ctx, "organization", strlen("organization"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ORGANIZATION);
314 assert_int_equal(yin_match_keyword(st->yin_ctx, "output", strlen("output"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_OUTPUT);
315 assert_int_equal(yin_match_keyword(st->yin_ctx, "path", strlen("path"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PATH);
316 assert_int_equal(yin_match_keyword(st->yin_ctx, "pattern", strlen("pattern"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PATTERN);
317 assert_int_equal(yin_match_keyword(st->yin_ctx, "position", strlen("position"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_POSITION);
318 assert_int_equal(yin_match_keyword(st->yin_ctx, "prefix", strlen("prefix"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PREFIX);
319 assert_int_equal(yin_match_keyword(st->yin_ctx, "presence", strlen("presence"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PRESENCE);
320 assert_int_equal(yin_match_keyword(st->yin_ctx, "range", strlen("range"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_RANGE);
321 assert_int_equal(yin_match_keyword(st->yin_ctx, "reference", strlen("reference"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REFERENCE);
322 assert_int_equal(yin_match_keyword(st->yin_ctx, "refine", strlen("refine"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REFINE);
323 assert_int_equal(yin_match_keyword(st->yin_ctx, "require-instance", strlen("require-instance"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REQUIRE_INSTANCE);
324 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision", strlen("revision"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REVISION);
325 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision-date", strlen("revision-date"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REVISION_DATE);
326 assert_int_equal(yin_match_keyword(st->yin_ctx, "rpc", strlen("rpc"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_RPC);
327 assert_int_equal(yin_match_keyword(st->yin_ctx, "status", strlen("status"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_STATUS);
328 assert_int_equal(yin_match_keyword(st->yin_ctx, "submodule", strlen("submodule"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_SUBMODULE);
329 assert_int_equal(yin_match_keyword(st->yin_ctx, "type", strlen("type"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_TYPE);
330 assert_int_equal(yin_match_keyword(st->yin_ctx, "typedef", strlen("typedef"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_TYPEDEF);
331 assert_int_equal(yin_match_keyword(st->yin_ctx, "unique", strlen("unique"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_UNIQUE);
332 assert_int_equal(yin_match_keyword(st->yin_ctx, "units", strlen("units"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_UNITS);
333 assert_int_equal(yin_match_keyword(st->yin_ctx, "uses", strlen("uses"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_USES);
334 assert_int_equal(yin_match_keyword(st->yin_ctx, "value", strlen("value"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_VALUE);
335 assert_int_equal(yin_match_keyword(st->yin_ctx, "when", strlen("when"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_WHEN);
336 assert_int_equal(yin_match_keyword(st->yin_ctx, "yang-version", strlen("yang-version"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_YANG_VERSION);
337 assert_int_equal(yin_match_keyword(st->yin_ctx, "yin-element", strlen("yin-element"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_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ákadd0c2e2019-08-16 10:49:12 +0200410 ret = yin_parse_extension_instance(st->yin_ctx, args, &data, name2fname(name, prefix_len),
411 len2flen(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ákadd0c2e2019-08-16 10:49:12 +0200454 exts = NULL;
455 args = NULL;
456 st = reset_state(state);
457
458 data = "<ext attr1=\"text1\" attr2=\"text2\">"
459 "<ext-sub1/>"
460 "<ext-sub2 sattr1=\"stext2\">"
461 "<ext-sub21>"
462 "<ext-sub211 sattr21=\"text21\"/>"
463 "</ext-sub21>"
464 "</ext-sub2>"
465 "<ext-sub3 attr3=\"text3\"></ext-sub3>"
466 "</ext>";
467 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
468 yin_load_attributes(st->yin_ctx, &data, &args);
469 ret = yin_parse_extension_instance(st->yin_ctx, args, &data, name, name_len, LYEXT_SUBSTMT_CONTACT, 0, &exts);
470 assert_int_equal(ret, LY_SUCCESS);
471
472 assert_string_equal(exts->name, "ext");
473 assert_null(exts->argument);
474 assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT);
475 assert_int_equal(exts->insubstmt_index, 0);
476 assert_true(exts->yin & LYS_YIN);
477 assert_string_equal(exts->child->stmt, "attr1");
478 assert_string_equal(exts->child->arg, "text1");
479 assert_null(exts->child->child);
480 assert_true(exts->child->flags & LYS_YIN_ATTR);
481
482 assert_string_equal(exts->child->next->stmt, "attr2");
483 assert_string_equal(exts->child->next->arg, "text2");
484 assert_null(exts->child->next->child);
485 assert_true(exts->child->next->flags & LYS_YIN_ATTR);
486
487 assert_string_equal(exts->child->next->next->stmt, "ext-sub1");
488 assert_null(exts->child->next->next->arg);
489 assert_null(exts->child->next->next->child);
490 assert_int_equal(exts->child->next->next->flags, 0);
491
492 assert_string_equal(exts->child->next->next->next->stmt, "ext-sub2");
493 assert_null(exts->child->next->next->next->arg);
494 assert_int_equal(exts->child->next->next->next->flags, 0);
495 assert_string_equal(exts->child->next->next->next->child->stmt, "sattr1");
496 assert_string_equal(exts->child->next->next->next->child->arg, "stext2");
497 assert_null(exts->child->next->next->next->child->child);
498 assert_true(exts->child->next->next->next->child->flags & LYS_YIN_ATTR);
499
500 assert_string_equal(exts->child->next->next->next->child->next->stmt, "ext-sub21");
501 assert_null(exts->child->next->next->next->child->next->arg);
502 assert_null(exts->child->next->next->next->child->next->next);
503 assert_int_equal(exts->child->next->next->next->child->next->flags, 0);
504
505 assert_string_equal(exts->child->next->next->next->child->next->child->stmt, "ext-sub211");
506 assert_null(exts->child->next->next->next->child->next->child->arg);
507 assert_int_equal(exts->child->next->next->next->child->next->child->flags, 0);
508 assert_null(exts->child->next->next->next->child->next->child->next);
509
510 assert_string_equal(exts->child->next->next->next->child->next->child->child->stmt, "sattr21");
511 assert_string_equal(exts->child->next->next->next->child->next->child->child->arg, "text21");
512 assert_null(exts->child->next->next->next->child->next->child->child->next);
513 assert_null(exts->child->next->next->next->child->next->child->child->child);
514 assert_true(exts->child->next->next->next->child->next->child->child->flags & LYS_YIN_ATTR);
515
516 assert_string_equal(exts->child->next->next->next->next->stmt, "ext-sub3");
517 assert_null(exts->child->next->next->next->next->arg);
518 assert_null(exts->child->next->next->next->next->next);
519 assert_int_equal(exts->child->next->next->next->next->flags, 0);
520
521 assert_string_equal(exts->child->next->next->next->next->child->stmt, "attr3");
522 assert_string_equal(exts->child->next->next->next->next->child->arg, "text3");
523 assert_null(exts->child->next->next->next->next->child->next);
524 assert_null(exts->child->next->next->next->next->child->child);
525 assert_true(exts->child->next->next->next->next->child->flags & LYS_YIN_ATTR);
526
527 LY_ARRAY_FREE(args);
528 lysp_ext_instance_free(st->ctx, exts);
529 LY_ARRAY_FREE(exts);
530 exts = NULL;
531 args = NULL;
532
David Sedlákb1a78352019-06-28 16:16:29 +0200533 st->finished_correctly = true;
534}
535
David Sedlák555c7202019-07-04 12:14:12 +0200536static void
537test_yin_parse_content(void **state)
538{
539 struct state *st = *state;
540 LY_ERR ret = LY_SUCCESS;
David Sedlákc5b20842019-08-13 10:18:31 +0200541 const char *name, *prefix;
542 size_t name_len, prefix_len;
David Sedlák555c7202019-07-04 12:14:12 +0200543 const char *data = "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
544 "<custom xmlns=\"my-ext\">"
545 "totally amazing extension"
546 "</custom>"
David Sedlák986cb412019-07-04 13:10:11 +0200547 "<extension name=\"ext\">"
548 "<argument name=\"argname\"></argument>"
549 "<description><text>desc</text></description>"
550 "<reference><text>ref</text></reference>"
551 "<status value=\"deprecated\"></status>"
552 "</extension>"
David Sedlák555c7202019-07-04 12:14:12 +0200553 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedlák32488102019-07-15 17:44:10 +0200554 "<if-feature name=\"foo\"></if-feature>"
David Sedlák32eee7b2019-07-09 12:38:44 +0200555 "<when condition=\"condition...\">"
556 "<reference><text>when_ref</text></reference>"
557 "<description><text>when_desc</text></description>"
558 "</when>"
David Sedláke1a30302019-07-10 13:49:38 +0200559 "<config value=\"true\"/>"
David Sedlákc1771b12019-07-10 15:55:46 +0200560 "<error-message>"
561 "<value>error-msg</value>"
562 "</error-message>"
David Sedlák2ce1be62019-07-10 16:15:09 +0200563 "<error-app-tag value=\"err-app-tag\"/>"
David Sedláka5b1d382019-07-10 16:31:09 +0200564 "<units name=\"radians\"></units>"
David Sedláke7084ce2019-07-10 16:44:15 +0200565 "<default value=\"default-value\"/>"
David Sedlák5545f5d2019-07-11 11:55:16 +0200566 "<position value=\"25\"></position>"
567 "<value value=\"-5\"/>"
David Sedlákcf5569a2019-07-11 13:31:34 +0200568 "<require-instance value=\"true\"></require-instance>"
David Sedlákb7296dd2019-07-11 14:58:38 +0200569 "<range value=\"5..10\" />"
David Sedlák438ae432019-07-11 15:36:54 +0200570 "<length value=\"baf\"/>"
David Sedlákd3983112019-07-12 11:20:56 +0200571 "<pattern value='pattern'>"
572 "<modifier value='invert-match'/>"
573 "</pattern>"
David Sedlákfd5b9c32019-07-12 15:33:13 +0200574 "<enum name=\"yay\">"
575 "</enum>"
David Sedlák555c7202019-07-04 12:14:12 +0200576 "</prefix>";
577 struct lysp_ext_instance *exts = NULL;
David Sedlák5f8191e2019-07-08 16:35:52 +0200578 const char **if_features = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200579 struct yin_arg_record *attrs = NULL;
David Sedláke7084ce2019-07-10 16:44:15 +0200580 const char *value, *err_msg, *app_tag, *units, *def;
David Sedlák986cb412019-07-04 13:10:11 +0200581 struct lysp_ext *ext_def = NULL;
David Sedlák32eee7b2019-07-09 12:38:44 +0200582 struct lysp_when *when_p = NULL;
David Sedlákcf5569a2019-07-11 13:31:34 +0200583 struct lysp_type_enum pos_enum = {}, val_enum = {};
David Sedlákfd5b9c32019-07-12 15:33:13 +0200584 struct lysp_type req_type = {}, range_type = {}, len_type = {}, patter_type = {}, enum_type = {};
David Sedláke1a30302019-07-10 13:49:38 +0200585 uint8_t config = 0;
David Sedlák555c7202019-07-04 12:14:12 +0200586
David Sedlákc5b20842019-08-13 10:18:31 +0200587 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlákda8ffa32019-07-08 14:17:10 +0200588 yin_load_attributes(st->yin_ctx, &data, &attrs);
David Sedlák555c7202019-07-04 12:14:12 +0200589
David Sedlákfd5b9c32019-07-12 15:33:13 +0200590 struct yin_subelement subelems[17] = {
Radek Krejcid6b76452019-09-03 17:03:03 +0200591 {LY_STMT_CONFIG, &config, 0},
592 {LY_STMT_DEFAULT, &def, YIN_SUBELEM_UNIQUE},
593 {LY_STMT_ENUM, &enum_type, 0},
594 {LY_STMT_ERROR_APP_TAG, &app_tag, YIN_SUBELEM_UNIQUE},
595 {LY_STMT_ERROR_MESSAGE, &err_msg, 0},
596 {LY_STMT_EXTENSION, &ext_def, 0},
597 {LY_STMT_IF_FEATURE, &if_features, 0},
598 {LY_STMT_LENGTH, &len_type, 0},
599 {LY_STMT_PATTERN, &patter_type, 0},
600 {LY_STMT_POSITION, &pos_enum, 0},
601 {LY_STMT_RANGE, &range_type, 0},
602 {LY_STMT_REQUIRE_INSTANCE, &req_type, 0},
603 {LY_STMT_UNITS, &units, YIN_SUBELEM_UNIQUE},
604 {LY_STMT_VALUE, &val_enum, 0},
605 {LY_STMT_WHEN, &when_p, 0},
606 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
607 {LY_STMT_ARG_TEXT, &value, 0}
David Sedlákd3983112019-07-12 11:20:56 +0200608 };
Radek Krejcid6b76452019-09-03 17:03:03 +0200609 ret = yin_parse_content(st->yin_ctx, subelems, 17, &data, LY_STMT_PREFIX, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200610 assert_int_equal(ret, LY_SUCCESS);
David Sedlák1fdb2522019-07-09 16:22:57 +0200611 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlák2ce1be62019-07-10 16:15:09 +0200612 /* check parsed values */
David Sedláke7084ce2019-07-10 16:44:15 +0200613 assert_string_equal(def, "default-value");
David Sedlák555c7202019-07-04 12:14:12 +0200614 assert_string_equal(exts->name, "custom");
615 assert_string_equal(exts->argument, "totally amazing extension");
616 assert_string_equal(value, "wsefsdf");
David Sedláka5b1d382019-07-10 16:31:09 +0200617 assert_string_equal(units, "radians");
David Sedlák32eee7b2019-07-09 12:38:44 +0200618 assert_string_equal(when_p->cond, "condition...");
619 assert_string_equal(when_p->dsc, "when_desc");
620 assert_string_equal(when_p->ref, "when_ref");
David Sedláke1a30302019-07-10 13:49:38 +0200621 assert_int_equal(config, LYS_CONFIG_W);
David Sedlák5545f5d2019-07-11 11:55:16 +0200622 assert_int_equal(pos_enum.value, 25);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200623 assert_true(pos_enum.flags & LYS_SET_VALUE);
David Sedlák5545f5d2019-07-11 11:55:16 +0200624 assert_int_equal(val_enum.value, -5);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200625 assert_true(val_enum.flags & LYS_SET_VALUE);
David Sedlákcf5569a2019-07-11 13:31:34 +0200626 assert_int_equal(req_type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200627 assert_true(req_type.flags &= LYS_SET_REQINST);
David Sedlákb7296dd2019-07-11 14:58:38 +0200628 assert_string_equal(range_type.range->arg, "5..10");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200629 assert_true(range_type.flags & LYS_SET_RANGE);
David Sedlákc1771b12019-07-10 15:55:46 +0200630 assert_string_equal(err_msg, "error-msg");
David Sedlák2ce1be62019-07-10 16:15:09 +0200631 assert_string_equal(app_tag, "err-app-tag");
David Sedlákfd5b9c32019-07-12 15:33:13 +0200632 assert_string_equal(enum_type.enums->name, "yay");
David Sedlák438ae432019-07-11 15:36:54 +0200633 assert_string_equal(len_type.length->arg, "baf");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200634 assert_true(len_type.flags & LYS_SET_LENGTH);
David Sedlákd3983112019-07-12 11:20:56 +0200635 assert_string_equal(patter_type.patterns->arg, "\x015pattern");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200636 assert_true(patter_type.flags & LYS_SET_PATTERN);
David Sedlák2ce1be62019-07-10 16:15:09 +0200637 /* cleanup */
David Sedlák555c7202019-07-04 12:14:12 +0200638 lysp_ext_instance_free(st->ctx, exts);
David Sedlák32eee7b2019-07-09 12:38:44 +0200639 lysp_when_free(st->ctx, when_p);
David Sedlák986cb412019-07-04 13:10:11 +0200640 lysp_ext_free(st->ctx, ext_def);
David Sedlák5f8191e2019-07-08 16:35:52 +0200641 FREE_STRING(st->ctx, *if_features);
David Sedlákc1771b12019-07-10 15:55:46 +0200642 FREE_STRING(st->ctx, err_msg);
David Sedlák2ce1be62019-07-10 16:15:09 +0200643 FREE_STRING(st->ctx, app_tag);
David Sedláka5b1d382019-07-10 16:31:09 +0200644 FREE_STRING(st->ctx, units);
David Sedlákd3983112019-07-12 11:20:56 +0200645 FREE_STRING(st->ctx, patter_type.patterns->arg);
David Sedláke7084ce2019-07-10 16:44:15 +0200646 FREE_STRING(st->ctx, def);
David Sedlákb7296dd2019-07-11 14:58:38 +0200647 FREE_STRING(st->ctx, range_type.range->arg);
David Sedlák438ae432019-07-11 15:36:54 +0200648 FREE_STRING(st->ctx, len_type.length->arg);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200649 FREE_STRING(st->ctx, enum_type.enums->name);
David Sedlákb7296dd2019-07-11 14:58:38 +0200650 FREE_STRING(st->ctx, value);
David Sedlák5f8191e2019-07-08 16:35:52 +0200651 LY_ARRAY_FREE(if_features);
David Sedlák555c7202019-07-04 12:14:12 +0200652 LY_ARRAY_FREE(exts);
David Sedlák986cb412019-07-04 13:10:11 +0200653 LY_ARRAY_FREE(ext_def);
David Sedlák555c7202019-07-04 12:14:12 +0200654 LY_ARRAY_FREE(attrs);
David Sedlákd3983112019-07-12 11:20:56 +0200655 LY_ARRAY_FREE(patter_type.patterns);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200656 LY_ARRAY_FREE(enum_type.enums);
David Sedlák32eee7b2019-07-09 12:38:44 +0200657 free(when_p);
David Sedlákb7296dd2019-07-11 14:58:38 +0200658 free(range_type.range);
David Sedlák438ae432019-07-11 15:36:54 +0200659 free(len_type.length);
David Sedlák555c7202019-07-04 12:14:12 +0200660 attrs = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200661 st = reset_state(state);
662
663 /* test unique subelem */
664 const char *prefix_value;
Radek Krejcid6b76452019-09-03 17:03:03 +0200665 struct yin_subelement subelems2[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE},
666 {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_UNIQUE}};
David Sedláke6cd89e2019-08-07 12:46:02 +0200667 data = ELEMENT_WRAPPER_START
David Sedlák555c7202019-07-04 12:14:12 +0200668 "<prefix value=\"inv_mod\" />"
669 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
670 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedláke6cd89e2019-08-07 12:46:02 +0200671 ELEMENT_WRAPPER_END;
David Sedlákc5b20842019-08-13 10:18:31 +0200672 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlákda8ffa32019-07-08 14:17:10 +0200673 yin_load_attributes(st->yin_ctx, &data, &attrs);
Radek Krejcid6b76452019-09-03 17:03:03 +0200674 ret = yin_parse_content(st->yin_ctx, subelems2, 2, &data, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200675 assert_int_equal(ret, LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200676 logbuf_assert("Redefinition of \"text\" sub-element in \"status\" element. Line number 1.");
David Sedlák555c7202019-07-04 12:14:12 +0200677 lydict_remove(st->ctx, prefix_value);
678 lydict_remove(st->ctx, value);
679 st = reset_state(state);
680 LY_ARRAY_FREE(attrs);
681 attrs = NULL;
682
683 /* test first subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200684 data = ELEMENT_WRAPPER_START
David Sedlák555c7202019-07-04 12:14:12 +0200685 "<prefix value=\"inv_mod\" />"
686 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
687 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedláke6cd89e2019-08-07 12:46:02 +0200688 ELEMENT_WRAPPER_END;
Radek Krejcid6b76452019-09-03 17:03:03 +0200689 struct yin_subelement subelems3[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE},
690 {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_FIRST}};
David Sedlákc5b20842019-08-13 10:18:31 +0200691 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlákda8ffa32019-07-08 14:17:10 +0200692 yin_load_attributes(st->yin_ctx, &data, &attrs);
Radek Krejcid6b76452019-09-03 17:03:03 +0200693 ret = yin_parse_content(st->yin_ctx, subelems3, 2, &data, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200694 assert_int_equal(ret, LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200695 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 +0200696 lydict_remove(st->ctx, prefix_value);
697 st = reset_state(state);
698 LY_ARRAY_FREE(attrs);
699 attrs = NULL;
700
701 /* test mandatory subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200702 data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END;
Radek Krejcid6b76452019-09-03 17:03:03 +0200703 struct yin_subelement subelems4[1] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}};
David Sedlákc5b20842019-08-13 10:18:31 +0200704 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlákda8ffa32019-07-08 14:17:10 +0200705 yin_load_attributes(st->yin_ctx, &data, &attrs);
Radek Krejcid6b76452019-09-03 17:03:03 +0200706 ret = yin_parse_content(st->yin_ctx, subelems4, 1, &data, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200707 assert_int_equal(ret, LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200708 logbuf_assert("Missing mandatory sub-element \"prefix\" of \"status\" element. Line number 1.");
David Sedlák555c7202019-07-04 12:14:12 +0200709 LY_ARRAY_FREE(attrs);
710
711 st->finished_correctly = true;
712}
713
David Sedlák92147b02019-07-09 14:01:01 +0200714static void
David Sedlák4a650532019-07-10 11:55:18 +0200715test_validate_value(void **state)
716{
717 struct state *st = *state;
718 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "#invalid", 8), LY_EVALID);
719 logbuf_assert("Invalid identifier character '#'. Line number 1.");
720 assert_int_equal(yin_validate_value(st->yin_ctx, Y_STR_ARG, "", 0), LY_SUCCESS);
721 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "pre:b", 5), LY_EVALID);
722 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:b", 5), LY_SUCCESS);
723 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:pre:b", 9), LY_EVALID);
724
725 st->finished_correctly = true;
726}
727
David Sedlák32488102019-07-15 17:44:10 +0200728/* helper function to simplify unit test of each element using parse_content function */
729LY_ERR
730test_element_helper(struct state *st, const char **data, void *dest, const char **text,
731 struct lysp_ext_instance **exts, bool valid)
732{
733 struct yin_arg_record *attrs = NULL;
David Sedlákc5b20842019-08-13 10:18:31 +0200734 const char *name, *prefix;
735 size_t name_len, prefix_len;
David Sedlák32488102019-07-15 17:44:10 +0200736 LY_ERR ret = LY_SUCCESS;
737 struct yin_subelement subelems[71] = {
Radek Krejcid6b76452019-09-03 17:03:03 +0200738 {LY_STMT_ACTION, dest, 0},
739 {LY_STMT_ANYDATA, dest, 0},
740 {LY_STMT_ANYXML, dest, 0},
741 {LY_STMT_ARGUMENT,dest, 0},
742 {LY_STMT_AUGMENT, dest, 0},
743 {LY_STMT_BASE, dest, 0},
744 {LY_STMT_BELONGS_TO, dest, 0},
745 {LY_STMT_BIT, dest, 0},
746 {LY_STMT_CASE, dest, 0},
747 {LY_STMT_CHOICE, dest, 0},
748 {LY_STMT_CONFIG, dest, 0},
749 {LY_STMT_CONTACT, dest, 0},
750 {LY_STMT_CONTAINER, dest, 0},
751 {LY_STMT_DEFAULT, dest, YIN_SUBELEM_UNIQUE},
752 {LY_STMT_DESCRIPTION, dest, 0},
753 {LY_STMT_DEVIATE, dest, 0},
754 {LY_STMT_DEVIATION, dest, 0},
755 {LY_STMT_ENUM, dest, 0},
756 {LY_STMT_ERROR_APP_TAG, dest, YIN_SUBELEM_UNIQUE},
757 {LY_STMT_ERROR_MESSAGE, dest, 0},
758 {LY_STMT_EXTENSION, dest, 0},
759 {LY_STMT_FEATURE, dest, 0},
760 {LY_STMT_FRACTION_DIGITS, dest, 0},
761 {LY_STMT_GROUPING, dest, 0},
762 {LY_STMT_IDENTITY, dest, 0},
763 {LY_STMT_IF_FEATURE, dest, 0},
764 {LY_STMT_IMPORT, dest, 0},
765 {LY_STMT_INCLUDE, dest, 0},
766 {LY_STMT_INPUT, dest, 0},
767 {LY_STMT_KEY, dest, YIN_SUBELEM_UNIQUE},
768 {LY_STMT_LEAF, dest, 0},
769 {LY_STMT_LEAF_LIST, dest, 0},
770 {LY_STMT_LENGTH, dest, 0},
771 {LY_STMT_LIST, dest, 0},
772 {LY_STMT_MANDATORY, dest, 0},
773 {LY_STMT_MAX_ELEMENTS, dest, 0},
774 {LY_STMT_MIN_ELEMENTS, dest, 0},
775 {LY_STMT_MODIFIER, dest, 0},
776 {LY_STMT_MODULE, dest, 0},
777 {LY_STMT_MUST, dest, 0},
778 {LY_STMT_NAMESPACE, dest, YIN_SUBELEM_UNIQUE},
779 {LY_STMT_NOTIFICATION, dest, 0},
780 {LY_STMT_ORDERED_BY, dest, 0},
781 {LY_STMT_ORGANIZATION, dest, 0},
782 {LY_STMT_OUTPUT, dest, 0},
783 {LY_STMT_PATH, dest, 0},
784 {LY_STMT_PATTERN, dest, 0},
785 {LY_STMT_POSITION, dest, 0},
786 {LY_STMT_PREFIX, dest, YIN_SUBELEM_UNIQUE},
787 {LY_STMT_PRESENCE, dest, YIN_SUBELEM_UNIQUE},
788 {LY_STMT_RANGE, dest, 0},
789 {LY_STMT_REFERENCE, dest, 0},
790 {LY_STMT_REFINE, dest, 0},
791 {LY_STMT_REQUIRE_INSTANCE, dest, 0},
792 {LY_STMT_REVISION, dest, 0},
793 {LY_STMT_REVISION_DATE, dest, 0},
794 {LY_STMT_RPC, dest, 0},
795 {LY_STMT_STATUS, dest, 0},
796 {LY_STMT_SUBMODULE, dest, 0},
797 {LY_STMT_TYPE, dest, 0},
798 {LY_STMT_TYPEDEF, dest, 0},
799 {LY_STMT_UNIQUE, dest, 0},
800 {LY_STMT_UNITS, dest, YIN_SUBELEM_UNIQUE},
801 {LY_STMT_USES, dest, 0},
802 {LY_STMT_VALUE, dest, 0},
803 {LY_STMT_WHEN, dest, 0},
804 {LY_STMT_YANG_VERSION, dest, 0},
805 {LY_STMT_YIN_ELEMENT, dest, 0},
806 {LY_STMT_EXTENSION_INSTANCE, dest, 0},
807 {LY_STMT_ARG_TEXT, dest, 0},
808 {LY_STMT_ARG_VALUE, dest, 0}
David Sedlák32488102019-07-15 17:44:10 +0200809 };
David Sedlákc5b20842019-08-13 10:18:31 +0200810 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 +0200811 LY_CHECK_RET(yin_load_attributes(st->yin_ctx, data, &attrs));
Radek Krejcid6b76452019-09-03 17:03:03 +0200812 ret = yin_parse_content(st->yin_ctx, subelems, 71, data, yin_match_keyword(st->yin_ctx, name, name_len, prefix, prefix_len, LY_STMT_NONE), text, exts);
David Sedlák32488102019-07-15 17:44:10 +0200813 LY_ARRAY_FREE(attrs);
814 if (valid) {
815 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
816 }
817 /* reset status */
818 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
819 return ret;
820}
821
David Sedlákd1144562019-08-06 12:36:14 +0200822#define EXT_SUBELEM "<myext:c-define name=\"MY_MTU\" xmlns:myext=\"urn:example:extensions\"/>"
823
David Sedlák32488102019-07-15 17:44:10 +0200824static void
David Sedlák43801c92019-08-05 15:58:54 +0200825test_enum_elem(void **state)
David Sedlák32488102019-07-15 17:44:10 +0200826{
David Sedlák32488102019-07-15 17:44:10 +0200827 struct state *st = *state;
828 struct lysp_type type = {};
829 const char *data;
830 data = ELEMENT_WRAPPER_START
831 "<enum name=\"enum-name\">"
832 "<if-feature name=\"feature\" />"
833 "<value value=\"55\" />"
834 "<status value=\"deprecated\" />"
835 "<description><text>desc...</text></description>"
836 "<reference><text>ref...</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +0200837 EXT_SUBELEM
David Sedlák32488102019-07-15 17:44:10 +0200838 "</enum>"
839 ELEMENT_WRAPPER_END;
840 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200841 assert_string_equal(type.enums->name, "enum-name");
David Sedlák32488102019-07-15 17:44:10 +0200842 assert_string_equal(*type.enums->iffeatures, "feature");
843 assert_int_equal(type.enums->value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200844 assert_true((type.enums->flags & LYS_STATUS_DEPRC) && (type.enums->flags & LYS_SET_VALUE));
David Sedlák32488102019-07-15 17:44:10 +0200845 assert_string_equal(type.enums->dsc, "desc...");
846 assert_string_equal(type.enums->ref, "ref...");
David Sedlákd1144562019-08-06 12:36:14 +0200847 assert_string_equal(type.enums->exts->name, "myext:c-define");
848 assert_int_equal(type.enums->exts->insubstmt_index, 0);
849 assert_int_equal(type.enums->exts->insubstmt, LYEXT_SUBSTMT_SELF);
850 lysp_type_free(st->ctx, &type);
851 memset(&type, 0, sizeof type);
852
853 data = ELEMENT_WRAPPER_START
854 "<enum name=\"enum-name\"></enum>"
855 ELEMENT_WRAPPER_END;
856 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
857 assert_string_equal(type.enums->name, "enum-name");
David Sedlák32488102019-07-15 17:44:10 +0200858 lysp_type_free(st->ctx, &type);
859 memset(&type, 0, sizeof type);
860
David Sedlák43801c92019-08-05 15:58:54 +0200861 st->finished_correctly = true;
862}
863
864static void
865test_bit_elem(void **state)
866{
867 struct state *st = *state;
868 struct lysp_type type = {};
869 const char *data;
870 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200871 "<bit name=\"bit-name\">"
David Sedlák43801c92019-08-05 15:58:54 +0200872 "<if-feature name=\"feature\" />"
873 "<position value=\"55\" />"
874 "<status value=\"deprecated\" />"
875 "<description><text>desc...</text></description>"
876 "<reference><text>ref...</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +0200877 EXT_SUBELEM
David Sedlák43801c92019-08-05 15:58:54 +0200878 "</bit>"
879 ELEMENT_WRAPPER_END;
880 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200881 assert_string_equal(type.bits->name, "bit-name");
David Sedlák43801c92019-08-05 15:58:54 +0200882 assert_string_equal(*type.bits->iffeatures, "feature");
883 assert_int_equal(type.bits->value, 55);
884 assert_true((type.bits->flags & LYS_STATUS_DEPRC) && (type.bits->flags & LYS_SET_VALUE));
885 assert_string_equal(type.bits->dsc, "desc...");
886 assert_string_equal(type.bits->ref, "ref...");
David Sedlákd1144562019-08-06 12:36:14 +0200887 assert_string_equal(type.bits->exts->name, "myext:c-define");
888 assert_int_equal(type.bits->exts->insubstmt_index, 0);
889 assert_int_equal(type.bits->exts->insubstmt, LYEXT_SUBSTMT_SELF);
890 lysp_type_free(st->ctx, &type);
891 memset(&type, 0, sizeof type);
892
893 data = ELEMENT_WRAPPER_START
894 "<bit name=\"bit-name\"> </bit>"
895 ELEMENT_WRAPPER_END;
896 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
897 assert_string_equal(type.bits->name, "bit-name");
David Sedlák43801c92019-08-05 15:58:54 +0200898 lysp_type_free(st->ctx, &type);
899 memset(&type, 0, sizeof type);
900
David Sedlák32488102019-07-15 17:44:10 +0200901 st->finished_correctly = true;
902}
903
904static void
905test_meta_elem(void **state)
906{
907 struct state *st = *state;
908 char *value = NULL;
909 const char *data;
David Sedlákd1144562019-08-06 12:36:14 +0200910 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200911
912 /* organization element */
913 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200914 "<organization><text>organization...</text>" EXT_SUBELEM EXT_SUBELEM "</organization>"
David Sedlák32488102019-07-15 17:44:10 +0200915 ELEMENT_WRAPPER_END;
David Sedlákd1144562019-08-06 12:36:14 +0200916 assert_int_equal(test_element_helper(st, &data, &value, NULL, &exts, true), LY_SUCCESS);
917 assert_string_equal(exts[0].name, "myext:c-define");
918 assert_int_equal(exts[0].insubstmt_index, 0);
919 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ORGANIZATION);
920 assert_string_equal(exts[1].name, "myext:c-define");
921 assert_int_equal(exts[1].insubstmt_index, 0);
922 assert_int_equal(exts[1].insubstmt, LYEXT_SUBSTMT_ORGANIZATION);
David Sedlák32488102019-07-15 17:44:10 +0200923 assert_string_equal(value, "organization...");
924 FREE_STRING(st->ctx, value);
David Sedlákd1144562019-08-06 12:36:14 +0200925 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +0200926 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +0200927 exts = NULL;
928
David Sedlák32488102019-07-15 17:44:10 +0200929 /* contact element */
930 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200931 "<contact><text>contact...</text>" EXT_SUBELEM "</contact>"
David Sedlák32488102019-07-15 17:44:10 +0200932 ELEMENT_WRAPPER_END;
David Sedlákd1144562019-08-06 12:36:14 +0200933 assert_int_equal(test_element_helper(st, &data, &value, NULL, &exts, true), LY_SUCCESS);
934 assert_string_equal(exts[0].name, "myext:c-define");
935 assert_int_equal(exts[0].insubstmt_index, 0);
936 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_CONTACT);
David Sedlák32488102019-07-15 17:44:10 +0200937 assert_string_equal(value, "contact...");
938 FREE_STRING(st->ctx, value);
David Sedlákd1144562019-08-06 12:36:14 +0200939 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
940 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200941 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +0200942
David Sedlák32488102019-07-15 17:44:10 +0200943 /* description element */
944 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200945 "<description><text>description...</text>" EXT_SUBELEM "</description>"
David Sedlák32488102019-07-15 17:44:10 +0200946 ELEMENT_WRAPPER_END;
David Sedlákd1144562019-08-06 12:36:14 +0200947 assert_int_equal(test_element_helper(st, &data, &value, NULL, &exts, true), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +0200948 assert_string_equal(value, "description...");
David Sedlákd1144562019-08-06 12:36:14 +0200949 assert_string_equal(exts[0].name, "myext:c-define");
950 assert_int_equal(exts[0].insubstmt_index, 0);
951 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_DESCRIPTION);
David Sedlák32488102019-07-15 17:44:10 +0200952 FREE_STRING(st->ctx, value);
953 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +0200954 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
955 exts = NULL;
956
David Sedlák32488102019-07-15 17:44:10 +0200957 /* reference element */
958 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200959 "<reference><text>reference...</text>" EXT_SUBELEM "</reference>"
David Sedlák32488102019-07-15 17:44:10 +0200960 ELEMENT_WRAPPER_END;
David Sedlákd1144562019-08-06 12:36:14 +0200961 assert_int_equal(test_element_helper(st, &data, &value, NULL, &exts, true), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +0200962 assert_string_equal(value, "reference...");
David Sedlákd1144562019-08-06 12:36:14 +0200963 assert_string_equal(exts[0].name, "myext:c-define");
964 assert_int_equal(exts[0].insubstmt_index, 0);
965 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_REFERENCE);
David Sedlák32488102019-07-15 17:44:10 +0200966 FREE_STRING(st->ctx, value);
967 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +0200968 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
969 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200970
David Sedlákdf2a9732019-08-07 13:23:16 +0200971 /* reference element */
972 data = ELEMENT_WRAPPER_START
973 "<reference invalid=\"text\"><text>reference...</text>""</reference>"
974 ELEMENT_WRAPPER_END;
975 assert_int_equal(test_element_helper(st, &data, &value, NULL, &exts, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200976 logbuf_assert("Unexpected attribute \"invalid\" of \"reference\" element. Line number 1.");
David Sedlákdf2a9732019-08-07 13:23:16 +0200977 FREE_STRING(st->ctx, value);
978 value = NULL;
979 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
980 exts = NULL;
981
David Sedlák32488102019-07-15 17:44:10 +0200982 /* missing text subelement */
983 data = ELEMENT_WRAPPER_START
984 "<reference>reference...</reference>"
985 ELEMENT_WRAPPER_END;
David Sedlákd1144562019-08-06 12:36:14 +0200986 assert_int_equal(test_element_helper(st, &data, &value, NULL, &exts, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200987 logbuf_assert("Missing mandatory sub-element \"text\" of \"reference\" element. Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +0200988
David Sedlákd1144562019-08-06 12:36:14 +0200989 /* reference element */
990 data = ELEMENT_WRAPPER_START
991 "<reference>" EXT_SUBELEM "<text>reference...</text></reference>"
992 ELEMENT_WRAPPER_END;
993 assert_int_equal(test_element_helper(st, &data, &value, NULL, &exts, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200994 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 +0200995 FREE_STRING(st->ctx, value);
996 value = NULL;
997 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
998 exts = NULL;
999
David Sedlák32488102019-07-15 17:44:10 +02001000 st->finished_correctly = true;
1001}
1002
1003static void
1004test_import_elem(void **state)
1005{
1006 struct state *st = *state;
1007 const char *data;
David Sedlák298ff6d2019-07-26 14:29:03 +02001008 struct lysp_import *imports = NULL;
1009 struct import_meta imp_meta = {"prefix", &imports};
David Sedlák32488102019-07-15 17:44:10 +02001010
1011 /* max subelems */
1012 data = ELEMENT_WRAPPER_START
1013 "<import module=\"a\">"
David Sedlákd1144562019-08-06 12:36:14 +02001014 EXT_SUBELEM
David Sedlák32488102019-07-15 17:44:10 +02001015 "<prefix value=\"a_mod\"/>"
1016 "<revision-date date=\"2015-01-01\"></revision-date>"
1017 "<description><text>import description</text></description>"
1018 "<reference><text>import reference</text></reference>"
1019 "</import>"
1020 ELEMENT_WRAPPER_END;
David Sedlák298ff6d2019-07-26 14:29:03 +02001021 assert_int_equal(test_element_helper(st, &data, &imp_meta, NULL, NULL, true), LY_SUCCESS);
1022 assert_string_equal(imports->name, "a");
1023 assert_string_equal(imports->prefix, "a_mod");
1024 assert_string_equal(imports->rev, "2015-01-01");
1025 assert_string_equal(imports->dsc, "import description");
1026 assert_string_equal(imports->ref, "import reference");
David Sedlákd1144562019-08-06 12:36:14 +02001027 assert_string_equal(imports->exts->name, "myext:c-define");
1028 assert_int_equal(imports->exts->insubstmt, LYEXT_SUBSTMT_SELF);
1029 assert_int_equal(imports->exts->insubstmt_index, 0);
David Sedlák298ff6d2019-07-26 14:29:03 +02001030 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1031 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001032
1033 /* min subelems */
David Sedlák32488102019-07-15 17:44:10 +02001034 data = ELEMENT_WRAPPER_START
1035 "<import module=\"a\">"
1036 "<prefix value=\"a_mod\"/>"
1037 "</import>"
1038 ELEMENT_WRAPPER_END;
David Sedlák298ff6d2019-07-26 14:29:03 +02001039 assert_int_equal(test_element_helper(st, &data, &imp_meta, NULL, NULL, true), LY_SUCCESS);
1040 assert_string_equal(imports->prefix, "a_mod");
1041 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1042 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001043
1044 /* invalid (missing prefix) */
David Sedlák298ff6d2019-07-26 14:29:03 +02001045 data = ELEMENT_WRAPPER_START "<import module=\"a\"></import>" ELEMENT_WRAPPER_END;
1046 assert_int_equal(test_element_helper(st, &data, &imp_meta, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001047 logbuf_assert("Missing mandatory sub-element \"prefix\" of \"import\" element. Line number 1.");
David Sedlák298ff6d2019-07-26 14:29:03 +02001048 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1049 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001050
1051 /* invalid reused prefix */
David Sedlák32488102019-07-15 17:44:10 +02001052 data = ELEMENT_WRAPPER_START
1053 "<import module=\"a\">"
David Sedlák298ff6d2019-07-26 14:29:03 +02001054 "<prefix value=\"prefix\"/>"
David Sedlák32488102019-07-15 17:44:10 +02001055 "</import>"
1056 "<import module=\"a\">"
David Sedlák298ff6d2019-07-26 14:29:03 +02001057 "<prefix value=\"prefix\"/>"
David Sedlák32488102019-07-15 17:44:10 +02001058 "</import>"
1059 ELEMENT_WRAPPER_END;
David Sedlák298ff6d2019-07-26 14:29:03 +02001060 assert_int_equal(test_element_helper(st, &data, &imp_meta, NULL, NULL, false), LY_EVALID);
1061 logbuf_assert("Prefix \"prefix\" already used as module prefix. Line number 1.");
1062 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1063 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001064
1065 st->finished_correctly = true;
1066}
1067
1068static void
1069test_status_elem(void **state)
1070{
1071 struct state *st = *state;
1072 const char *data;
1073 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001074 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001075
1076 /* test valid values */
1077 data = ELEMENT_WRAPPER_START "<status value=\"current\" />" ELEMENT_WRAPPER_END;
1078 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001079 assert_true(flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +02001080
1081 data = ELEMENT_WRAPPER_START "<status value=\"deprecated\" />" ELEMENT_WRAPPER_END;
1082 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001083 assert_true(flags & LYS_STATUS_DEPRC);
David Sedlák32488102019-07-15 17:44:10 +02001084
David Sedlákd1144562019-08-06 12:36:14 +02001085 data = ELEMENT_WRAPPER_START "<status value=\"obsolete\">"EXT_SUBELEM"</status>" ELEMENT_WRAPPER_END;
1086 assert_int_equal(test_element_helper(st, &data, &flags, NULL, &exts, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001087 assert_true(flags & LYS_STATUS_OBSLT);
David Sedlákd1144562019-08-06 12:36:14 +02001088 assert_string_equal(exts[0].name, "myext:c-define");
1089 assert_int_equal(exts[0].insubstmt_index, 0);
1090 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_STATUS);
1091 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1092 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001093
1094 /* test invalid value */
1095 data = ELEMENT_WRAPPER_START "<status value=\"invalid\"></status>" ELEMENT_WRAPPER_END;
1096 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001097 logbuf_assert("Invalid value \"invalid\" of \"value\" attribute in \"status\" element. Valid values are \"current\", \"deprecated\" and \"obsolete\". Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001098 st->finished_correctly = true;
1099}
1100
1101static void
1102test_ext_elem(void **state)
1103{
1104 struct state *st = *state;
1105 const char *data;
1106 struct lysp_ext *ext = NULL;
1107
1108 /* max subelems */
1109 data = ELEMENT_WRAPPER_START
1110 "<extension name=\"ext_name\">"
1111 "<argument name=\"arg\"></argument>"
1112 "<status value=\"current\"/>"
1113 "<description><text>ext_desc</text></description>"
1114 "<reference><text>ext_ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001115 EXT_SUBELEM
David Sedlák32488102019-07-15 17:44:10 +02001116 "</extension>"
1117 ELEMENT_WRAPPER_END;
1118 assert_int_equal(test_element_helper(st, &data, &ext, NULL, NULL, true), LY_SUCCESS);
1119 assert_string_equal(ext->name, "ext_name");
1120 assert_string_equal(ext->argument, "arg");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001121 assert_true(ext->flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +02001122 assert_string_equal(ext->dsc, "ext_desc");
1123 assert_string_equal(ext->ref, "ext_ref");
David Sedlákd1144562019-08-06 12:36:14 +02001124 assert_string_equal(ext->exts[0].name, "myext:c-define");
1125 assert_int_equal(ext->exts[0].insubstmt_index, 0);
1126 assert_int_equal(ext->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák32488102019-07-15 17:44:10 +02001127 lysp_ext_free(st->ctx, ext);
1128 LY_ARRAY_FREE(ext);
1129 ext = NULL;
1130
1131 /* min subelems */
1132 data = ELEMENT_WRAPPER_START "<extension name=\"ext_name\"></extension>" ELEMENT_WRAPPER_END;
1133 assert_int_equal(test_element_helper(st, &data, &ext, NULL, NULL, true), LY_SUCCESS);
1134 assert_string_equal(ext->name, "ext_name");
1135 lysp_ext_free(st->ctx, ext);
1136 LY_ARRAY_FREE(ext);
1137 ext = NULL;
1138
1139 st->finished_correctly = true;
1140}
1141
1142static void
1143test_yin_element_elem(void **state)
1144{
1145 struct state *st = *state;
1146 const char *data;
1147 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001148 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001149
1150 data = ELEMENT_WRAPPER_START "<yin-element value=\"true\" />" ELEMENT_WRAPPER_END;
1151 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001152 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +02001153
David Sedlákd1144562019-08-06 12:36:14 +02001154 data = ELEMENT_WRAPPER_START "<yin-element value=\"false\">" EXT_SUBELEM "</yin-element>" ELEMENT_WRAPPER_END;
1155 assert_int_equal(test_element_helper(st, &data, &flags, NULL, &exts, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001156 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlákd1144562019-08-06 12:36:14 +02001157 assert_string_equal(exts[0].name, "myext:c-define");
1158 assert_int_equal(exts[0].insubstmt_index, 0);
1159 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_YINELEM);
1160 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001161
1162 data = ELEMENT_WRAPPER_START "<yin-element value=\"invalid\" />" ELEMENT_WRAPPER_END;
1163 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001164 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák26ea1432019-08-14 13:42:23 +02001165 logbuf_assert("Invalid value \"invalid\" of \"value\" attribute in \"yin-element\" element. Valid values are \"true\" and \"false\". Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001166 st->finished_correctly = true;
1167}
1168
1169static void
1170test_yangversion_elem(void **state)
1171{
1172 struct state *st = *state;
1173 const char *data;
1174 uint8_t version = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001175 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001176
1177 /* valid values */
1178 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.0\" />" ELEMENT_WRAPPER_END;
1179 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001180 assert_true(version & LYS_VERSION_1_0);
David Sedlák32488102019-07-15 17:44:10 +02001181 assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_0);
1182
David Sedlákd1144562019-08-06 12:36:14 +02001183 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.1\">" EXT_SUBELEM "</yang-version>" ELEMENT_WRAPPER_END;
1184 assert_int_equal(test_element_helper(st, &data, &version, NULL, &exts, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001185 assert_true(version & LYS_VERSION_1_1);
David Sedlák32488102019-07-15 17:44:10 +02001186 assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_1);
David Sedlákd1144562019-08-06 12:36:14 +02001187 assert_string_equal(exts[0].name, "myext:c-define");
1188 assert_int_equal(exts[0].insubstmt_index, 0);
1189 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_VERSION);
1190 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001191
1192 /* invalid value */
1193 data = ELEMENT_WRAPPER_START "<yang-version value=\"version\" />" ELEMENT_WRAPPER_END;
1194 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, false), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001195 logbuf_assert("Invalid value \"version\" of \"value\" attribute in \"yang-version\" element. Valid values are \"1.0\" and \"1.1\". Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001196
1197 st->finished_correctly = true;
1198}
1199
1200static void
1201test_mandatory_elem(void **state)
1202{
1203 struct state *st = *state;
1204 const char *data;
1205 uint16_t man = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001206 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001207
1208 /* valid values */
1209 data = ELEMENT_WRAPPER_START "<mandatory value=\"true\" />" ELEMENT_WRAPPER_END;
1210 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, true), LY_SUCCESS);
1211 assert_int_equal(man, LYS_MAND_TRUE);
1212 man = 0;
1213
David Sedlákd1144562019-08-06 12:36:14 +02001214 data = ELEMENT_WRAPPER_START "<mandatory value=\"false\">" EXT_SUBELEM "</mandatory>" ELEMENT_WRAPPER_END;
1215 assert_int_equal(test_element_helper(st, &data, &man, NULL, &exts, true), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001216 assert_int_equal(man, LYS_MAND_FALSE);
David Sedlákd1144562019-08-06 12:36:14 +02001217 assert_string_equal(exts[0].name, "myext:c-define");
1218 assert_int_equal(exts[0].insubstmt_index, 0);
1219 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_MANDATORY);
1220 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001221
1222 data = ELEMENT_WRAPPER_START "<mandatory value=\"invalid\" />" ELEMENT_WRAPPER_END;
1223 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, false), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001224 logbuf_assert("Invalid value \"invalid\" of \"value\" attribute in \"mandatory\" element. Valid values are \"true\" and \"false\". Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001225
1226 st->finished_correctly = true;
1227}
1228
David Sedlák8e7bda82019-07-16 17:57:50 +02001229static void
1230test_argument_elem(void **state)
1231{
1232 struct state *st = *state;
1233 const char *data;
1234 uint16_t flags = 0;
1235 const char *arg;
1236 struct yin_argument_meta arg_meta = {&flags, &arg};
David Sedlákd1144562019-08-06 12:36:14 +02001237 struct lysp_ext_instance *exts = NULL;
1238
David Sedlák8e7bda82019-07-16 17:57:50 +02001239 /* max subelems */
1240 data = ELEMENT_WRAPPER_START
1241 "<argument name=\"arg-name\">"
1242 "<yin-element value=\"true\" />"
David Sedlákd1144562019-08-06 12:36:14 +02001243 EXT_SUBELEM
David Sedlák8e7bda82019-07-16 17:57:50 +02001244 "</argument>"
1245 ELEMENT_WRAPPER_END;
David Sedlákd1144562019-08-06 12:36:14 +02001246 assert_int_equal(test_element_helper(st, &data, &arg_meta, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001247 assert_string_equal(arg, "arg-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001248 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlákd1144562019-08-06 12:36:14 +02001249 assert_string_equal(exts[0].name, "myext:c-define");
1250 assert_int_equal(exts[0].insubstmt_index, 0);
1251 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ARGUMENT);
1252 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1253 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001254 flags = 0;
1255 FREE_STRING(st->ctx, arg);
1256 arg = NULL;
1257
1258 /* min subelems */
1259 data = ELEMENT_WRAPPER_START
1260 "<argument name=\"arg\">"
1261 "</argument>"
1262 ELEMENT_WRAPPER_END;
1263 assert_int_equal(test_element_helper(st, &data, &arg_meta, NULL, NULL, true), LY_SUCCESS);
1264 assert_string_equal(arg, "arg");
1265 assert_true(flags == 0);
1266 FREE_STRING(st->ctx, arg);
1267
1268 st->finished_correctly = true;
1269}
1270
1271static void
1272test_base_elem(void **state)
1273{
1274 struct state *st = *state;
1275 const char *data;
1276 const char **bases = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001277 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001278 struct lysp_type type = {};
1279
1280 /* as identity subelement */
1281 data = "<identity xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlákd1144562019-08-06 12:36:14 +02001282 "<base name=\"base-name\">"
1283 EXT_SUBELEM
1284 "</base>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001285 "</identity>";
David Sedlákd1144562019-08-06 12:36:14 +02001286 assert_int_equal(test_element_helper(st, &data, &bases, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001287 assert_string_equal(*bases, "base-name");
David Sedlákd1144562019-08-06 12:36:14 +02001288 assert_string_equal(exts[0].name, "myext:c-define");
1289 assert_int_equal(exts[0].insubstmt_index, 0);
1290 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_BASE);
1291 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1292 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001293 FREE_STRING(st->ctx, *bases);
1294 LY_ARRAY_FREE(bases);
1295
1296 /* as type subelement */
1297 data = "<type xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlákd1144562019-08-06 12:36:14 +02001298 "<base name=\"base-name\">"
1299 EXT_SUBELEM
1300 "</base>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001301 "</type>";
David Sedlákd1144562019-08-06 12:36:14 +02001302 assert_int_equal(test_element_helper(st, &data, &type, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001303 assert_string_equal(*type.bases, "base-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001304 assert_true(type.flags & LYS_SET_BASE);
David Sedlákd1144562019-08-06 12:36:14 +02001305 assert_string_equal(exts[0].name, "myext:c-define");
1306 assert_int_equal(exts[0].insubstmt_index, 0);
1307 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_BASE);
1308 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1309 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001310 FREE_STRING(st->ctx, *type.bases);
1311 LY_ARRAY_FREE(type.bases);
1312
1313 st->finished_correctly = true;
1314}
1315
1316static void
1317test_belongsto_elem(void **state)
1318{
1319 struct state *st = *state;
1320 const char *data;
1321 struct lysp_submodule submod;
David Sedlákd1144562019-08-06 12:36:14 +02001322 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001323
1324 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +02001325 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/>"EXT_SUBELEM"</belongs-to>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001326 ELEMENT_WRAPPER_END;
David Sedlákd1144562019-08-06 12:36:14 +02001327 assert_int_equal(test_element_helper(st, &data, &submod, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001328 assert_string_equal(submod.belongsto, "module-name");
1329 assert_string_equal(submod.prefix, "pref");
David Sedlákd1144562019-08-06 12:36:14 +02001330 assert_string_equal(exts[0].name, "myext:c-define");
1331 assert_int_equal(exts[0].insubstmt_index, 0);
1332 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_BELONGSTO);
1333 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1334 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001335 FREE_STRING(st->ctx, submod.belongsto);
1336 FREE_STRING(st->ctx, submod.prefix);
1337
1338 data = ELEMENT_WRAPPER_START "<belongs-to module=\"module-name\"></belongs-to>" ELEMENT_WRAPPER_END;
1339 assert_int_equal(test_element_helper(st, &data, &submod, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001340 logbuf_assert("Missing mandatory sub-element \"prefix\" of \"belongs-to\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001341 FREE_STRING(st->ctx, submod.belongsto);
1342
1343 st->finished_correctly = true;
1344}
1345
1346static void
1347test_config_elem(void **state)
1348{
1349 struct state *st = *state;
1350 const char *data;
1351 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001352 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001353
David Sedlákd1144562019-08-06 12:36:14 +02001354 data = ELEMENT_WRAPPER_START "<config value=\"true\">" EXT_SUBELEM "</config>" ELEMENT_WRAPPER_END;
1355 assert_int_equal(test_element_helper(st, &data, &flags, NULL, &exts, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001356 assert_true(flags & LYS_CONFIG_W);
David Sedlákd1144562019-08-06 12:36:14 +02001357 assert_string_equal(exts[0].name, "myext:c-define");
1358 assert_int_equal(exts[0].insubstmt_index, 0);
1359 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_CONFIG);
1360 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1361 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001362 flags = 0;
1363
1364 data = ELEMENT_WRAPPER_START "<config value=\"false\"/>" ELEMENT_WRAPPER_END;
1365 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001366 assert_true(flags & LYS_CONFIG_R);
David Sedlák8e7bda82019-07-16 17:57:50 +02001367 flags = 0;
1368
1369 data = ELEMENT_WRAPPER_START "<config value=\"invalid\"/>" ELEMENT_WRAPPER_END;
1370 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001371 logbuf_assert("Invalid value \"invalid\" of \"value\" attribute in \"config\" element. Valid values are \"true\" and \"false\". Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001372
1373 st->finished_correctly = true;
1374}
1375
1376static void
1377test_default_elem(void **state)
1378{
1379 struct state *st = *state;
1380 const char *data;
1381 const char *val = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001382 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001383
David Sedlákd1144562019-08-06 12:36:14 +02001384 data = ELEMENT_WRAPPER_START "<default value=\"defaul-value\">"EXT_SUBELEM"</default>" ELEMENT_WRAPPER_END;
1385 assert_int_equal(test_element_helper(st, &data, &val, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001386 assert_string_equal(val, "defaul-value");
David Sedlákd1144562019-08-06 12:36:14 +02001387 assert_string_equal(exts[0].name, "myext:c-define");
1388 assert_int_equal(exts[0].insubstmt_index, 0);
1389 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_DEFAULT);
1390 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1391 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001392 FREE_STRING(st->ctx, val);
1393 val = NULL;
1394
1395 data = ELEMENT_WRAPPER_START "<default/>" ELEMENT_WRAPPER_END;
1396 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1397 logbuf_assert("Missing mandatory attribute value of default element. Line number 1.");
1398
1399 st->finished_correctly = true;
1400}
1401
1402static void
1403test_err_app_tag_elem(void **state)
1404{
1405 struct state *st = *state;
1406 const char *data;
1407 const char *val = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001408 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001409
David Sedlákd1144562019-08-06 12:36:14 +02001410 data = ELEMENT_WRAPPER_START "<error-app-tag value=\"val\">"EXT_SUBELEM"</error-app-tag>" ELEMENT_WRAPPER_END;
1411 assert_int_equal(test_element_helper(st, &data, &val, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001412 assert_string_equal(val, "val");
David Sedlákd1144562019-08-06 12:36:14 +02001413 assert_string_equal(exts[0].name, "myext:c-define");
1414 assert_int_equal(exts[0].insubstmt_index, 0);
1415 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ERRTAG);
1416 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1417 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001418 FREE_STRING(st->ctx, val);
1419 val = NULL;
1420
1421 data = ELEMENT_WRAPPER_START "<error-app-tag/>" ELEMENT_WRAPPER_END;
1422 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1423 logbuf_assert("Missing mandatory attribute value of error-app-tag element. Line number 1.");
1424
1425 st->finished_correctly = true;
1426}
1427
1428static void
1429test_err_msg_elem(void **state)
1430{
1431 struct state *st = *state;
1432 const char *data;
1433 const char *val = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001434 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001435
David Sedlákd1144562019-08-06 12:36:14 +02001436 data = ELEMENT_WRAPPER_START "<error-message><value>val</value>"EXT_SUBELEM"</error-message>" ELEMENT_WRAPPER_END;
1437 assert_int_equal(test_element_helper(st, &data, &val, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001438 assert_string_equal(val, "val");
David Sedlákd1144562019-08-06 12:36:14 +02001439 assert_string_equal(exts[0].name, "myext:c-define");
1440 assert_int_equal(exts[0].insubstmt_index, 0);
1441 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ERRMSG);
1442 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1443 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001444 FREE_STRING(st->ctx, val);
1445
1446 data = ELEMENT_WRAPPER_START "<error-message></error-message>" ELEMENT_WRAPPER_END;
1447 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001448 logbuf_assert("Missing mandatory sub-element \"value\" of \"error-message\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001449
David Sedlákdf2a9732019-08-07 13:23:16 +02001450 data = ELEMENT_WRAPPER_START "<error-message invalid=\"text\"/>" ELEMENT_WRAPPER_END;
1451 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001452 logbuf_assert("Unexpected attribute \"invalid\" of \"error-message\" element. Line number 1.");
David Sedlákdf2a9732019-08-07 13:23:16 +02001453
David Sedlák8e7bda82019-07-16 17:57:50 +02001454 st->finished_correctly = true;
1455}
1456
1457static void
1458test_fracdigits_elem(void **state)
1459{
1460 struct state *st = *state;
1461 const char *data;
1462 struct lysp_type type = {};
1463
1464 /* valid value */
David Sedlákd1144562019-08-06 12:36:14 +02001465 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"10\">"EXT_SUBELEM"</fraction-digits>" ELEMENT_WRAPPER_END;
David Sedlák8e7bda82019-07-16 17:57:50 +02001466 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001467 assert_string_equal(type.exts[0].name, "myext:c-define");
1468 assert_int_equal(type.exts[0].insubstmt_index, 0);
1469 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_FRACDIGITS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001470 assert_int_equal(type.fraction_digits, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001471 assert_true(type.flags & LYS_SET_FRDIGITS);
David Sedlákd1144562019-08-06 12:36:14 +02001472 FREE_ARRAY(st->ctx, type.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001473
1474 /* invalid values */
1475 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"-1\"></fraction-digits>" ELEMENT_WRAPPER_END;
1476 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001477 logbuf_assert("Invalid value \"-1\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001478
1479 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"02\"></fraction-digits>" ELEMENT_WRAPPER_END;
1480 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001481 logbuf_assert("Invalid value \"02\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001482
1483 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"1p\"></fraction-digits>" ELEMENT_WRAPPER_END;
1484 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001485 logbuf_assert("Invalid value \"1p\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001486
1487 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"19\"></fraction-digits>" ELEMENT_WRAPPER_END;
1488 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001489 logbuf_assert("Invalid value \"19\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001490
1491 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"999999999999999999\"></fraction-digits>" ELEMENT_WRAPPER_END;
1492 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001493 logbuf_assert("Invalid value \"999999999999999999\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001494
1495 st->finished_correctly = true;
1496}
1497
1498static void
1499test_iffeature_elem(void **state)
1500{
1501 struct state *st = *state;
1502 const char *data;
1503 const char **iffeatures = NULL;
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 "<if-feature name=\"local-storage\">"EXT_SUBELEM"</if-feature>" ELEMENT_WRAPPER_END;
1507 assert_int_equal(test_element_helper(st, &data, &iffeatures, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001508 assert_string_equal(*iffeatures, "local-storage");
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_IFFEATURE);
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, *iffeatures);
1515 LY_ARRAY_FREE(iffeatures);
1516 iffeatures = NULL;
1517
1518 data = ELEMENT_WRAPPER_START "<if-feature/>" ELEMENT_WRAPPER_END;
1519 assert_int_equal(test_element_helper(st, &data, &iffeatures, NULL, NULL, false), LY_EVALID);
1520 logbuf_assert("Missing mandatory attribute name of if-feature element. Line number 1.");
1521 LY_ARRAY_FREE(iffeatures);
1522 iffeatures = NULL;
1523
1524 st->finished_correctly = true;
1525}
1526
1527static void
1528test_length_elem(void **state)
1529{
1530 struct state *st = *state;
1531 const char *data;
1532 struct lysp_type type = {};
1533
1534 /* max subelems */
1535 data = ELEMENT_WRAPPER_START
1536 "<length value=\"length-str\">"
1537 "<error-message><value>err-msg</value></error-message>"
1538 "<error-app-tag value=\"err-app-tag\"/>"
1539 "<description><text>desc</text></description>"
1540 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001541 EXT_SUBELEM
David Sedlák8e7bda82019-07-16 17:57:50 +02001542 "</length>"
1543 ELEMENT_WRAPPER_END;
1544 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1545 assert_string_equal(type.length->arg, "length-str");
1546 assert_string_equal(type.length->emsg, "err-msg");
1547 assert_string_equal(type.length->eapptag, "err-app-tag");
1548 assert_string_equal(type.length->dsc, "desc");
1549 assert_string_equal(type.length->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001550 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlákd1144562019-08-06 12:36:14 +02001551 assert_string_equal(type.length->exts[0].name, "myext:c-define");
1552 assert_int_equal(type.length->exts[0].insubstmt_index, 0);
1553 assert_int_equal(type.length->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8e7bda82019-07-16 17:57:50 +02001554 lysp_type_free(st->ctx, &type);
1555 memset(&type, 0, sizeof(type));
1556
1557 /* min subelems */
1558 data = ELEMENT_WRAPPER_START
1559 "<length value=\"length-str\">"
1560 "</length>"
1561 ELEMENT_WRAPPER_END;
1562 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1563 assert_string_equal(type.length->arg, "length-str");
1564 lysp_type_free(st->ctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001565 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001566 memset(&type, 0, sizeof(type));
1567
1568 data = ELEMENT_WRAPPER_START "<length></length>" ELEMENT_WRAPPER_END;
1569 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1570 logbuf_assert("Missing mandatory attribute value of length element. Line number 1.");
1571 lysp_type_free(st->ctx, &type);
1572 memset(&type, 0, sizeof(type));
1573
1574 st->finished_correctly = true;
1575}
1576
1577static void
1578test_modifier_elem(void **state)
1579{
1580 struct state *st = *state;
1581 const char *data;
1582 const char *pat = lydict_insert(st->ctx, "\006pattern", 8);
David Sedlákd1144562019-08-06 12:36:14 +02001583 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001584
David Sedlákd1144562019-08-06 12:36:14 +02001585 data = ELEMENT_WRAPPER_START "<modifier value=\"invert-match\">" EXT_SUBELEM "</modifier>" ELEMENT_WRAPPER_END;
1586 assert_int_equal(test_element_helper(st, &data, &pat, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001587 assert_string_equal(pat, "\x015pattern");
David Sedlákd1144562019-08-06 12:36:14 +02001588 assert_string_equal(exts[0].name, "myext:c-define");
1589 assert_int_equal(exts[0].insubstmt_index, 0);
1590 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_MODIFIER);
1591 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1592 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001593 FREE_STRING(st->ctx, pat);
1594
1595 pat = lydict_insert(st->ctx, "\006pattern", 8);
1596 data = ELEMENT_WRAPPER_START "<modifier value=\"invert\" />" ELEMENT_WRAPPER_END;
1597 assert_int_equal(test_element_helper(st, &data, &pat, NULL, NULL, false), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001598 logbuf_assert("Invalid value \"invert\" of \"value\" attribute in \"modifier\" element. Only valid value is \"invert-match\". Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001599 FREE_STRING(st->ctx, pat);
1600
1601 st->finished_correctly = true;
1602}
1603
1604static void
1605test_namespace_elem(void **state)
1606{
1607 struct state *st = *state;
1608 const char *data;
1609 const char *ns;
David Sedlákd1144562019-08-06 12:36:14 +02001610 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001611
David Sedlákd1144562019-08-06 12:36:14 +02001612 data = ELEMENT_WRAPPER_START "<namespace uri=\"ns\">" EXT_SUBELEM "</namespace>" ELEMENT_WRAPPER_END;
1613 assert_int_equal(test_element_helper(st, &data, &ns, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001614 assert_string_equal(ns, "ns");
David Sedlákd1144562019-08-06 12:36:14 +02001615 assert_string_equal(exts[0].name, "myext:c-define");
1616 assert_int_equal(exts[0].insubstmt_index, 0);
1617 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_NAMESPACE);
1618 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1619 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001620 FREE_STRING(st->ctx, ns);
1621
1622 data = ELEMENT_WRAPPER_START "<namespace/>" ELEMENT_WRAPPER_END;
1623 assert_int_equal(test_element_helper(st, &data, &ns, NULL, NULL, false), LY_EVALID);
1624 logbuf_assert("Missing mandatory attribute uri of namespace element. Line number 1.");
1625
1626 st->finished_correctly = true;
1627}
1628
1629static void
1630test_path_elem(void **state)
1631{
1632 struct state *st = *state;
1633 const char *data;
1634 struct lysp_type type = {};
1635
David Sedlák169cc522019-08-15 13:23:45 +02001636 data = ELEMENT_WRAPPER_START "<path value=\"p&amp;th-val\">" EXT_SUBELEM "</path>" ELEMENT_WRAPPER_END;
David Sedlák8e7bda82019-07-16 17:57:50 +02001637 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
David Sedlák169cc522019-08-15 13:23:45 +02001638 assert_string_equal("p&th-val", type.path);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001639 assert_true(type.flags & LYS_SET_PATH);
David Sedlákd1144562019-08-06 12:36:14 +02001640 assert_string_equal(type.exts[0].name, "myext:c-define");
1641 assert_int_equal(type.exts[0].insubstmt_index, 0);
1642 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_PATH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001643 lysp_type_free(st->ctx, &type);
1644
1645 st->finished_correctly = true;
1646}
1647
1648static void
1649test_pattern_elem(void **state)
1650{
1651 struct state *st = *state;
1652 const char *data;
1653 struct lysp_type type = {};
1654
1655 /* max subelems */
1656 data = ELEMENT_WRAPPER_START
1657 "<pattern value=\"super_pattern\">"
1658 "<modifier value=\"invert-match\"/>"
1659 "<error-message><value>err-msg-value</value></error-message>"
1660 "<error-app-tag value=\"err-app-tag-value\"/>"
David Sedlák169cc522019-08-15 13:23:45 +02001661 "<description><text>&quot;pattern-desc&quot;</text></description>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001662 "<reference><text>pattern-ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001663 EXT_SUBELEM
David Sedlák8e7bda82019-07-16 17:57:50 +02001664 "</pattern>"
1665 ELEMENT_WRAPPER_END;
1666 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001667 assert_true(type.flags & LYS_SET_PATTERN);
David Sedlák8e7bda82019-07-16 17:57:50 +02001668 assert_string_equal(type.patterns->arg, "\x015super_pattern");
David Sedlák169cc522019-08-15 13:23:45 +02001669 assert_string_equal(type.patterns->dsc, "\"pattern-desc\"");
David Sedlák8e7bda82019-07-16 17:57:50 +02001670 assert_string_equal(type.patterns->eapptag, "err-app-tag-value");
1671 assert_string_equal(type.patterns->emsg, "err-msg-value");
David Sedlák8e7bda82019-07-16 17:57:50 +02001672 assert_string_equal(type.patterns->ref, "pattern-ref");
David Sedlákd1144562019-08-06 12:36:14 +02001673 assert_string_equal(type.patterns->exts[0].name, "myext:c-define");
1674 assert_int_equal(type.patterns->exts[0].insubstmt_index, 0);
1675 assert_int_equal(type.patterns->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8e7bda82019-07-16 17:57:50 +02001676 lysp_type_free(st->ctx, &type);
1677 memset(&type, 0, sizeof(type));
1678
1679 /* min subelems */
1680 data = ELEMENT_WRAPPER_START "<pattern value=\"pattern\"> </pattern>" ELEMENT_WRAPPER_END;
1681 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1682 assert_string_equal(type.patterns->arg, "\x006pattern");
1683 lysp_type_free(st->ctx, &type);
1684 memset(&type, 0, sizeof(type));
1685
1686 st->finished_correctly = true;
1687}
1688
1689static void
1690test_value_position_elem(void **state)
1691{
1692 struct state *st = *state;
1693 const char *data;
1694 struct lysp_type_enum en = {};
1695
1696 /* valid values */
David Sedlákd1144562019-08-06 12:36:14 +02001697 data = ELEMENT_WRAPPER_START "<value value=\"55\">" EXT_SUBELEM "</value>" ELEMENT_WRAPPER_END;
David Sedlák8e7bda82019-07-16 17:57:50 +02001698 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1699 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001700 assert_true(en.flags & LYS_SET_VALUE);
David Sedlákd1144562019-08-06 12:36:14 +02001701 assert_string_equal(en.exts[0].name, "myext:c-define");
1702 assert_int_equal(en.exts[0].insubstmt_index, 0);
1703 assert_int_equal(en.exts[0].insubstmt, LYEXT_SUBSTMT_VALUE);
1704 FREE_ARRAY(st->ctx, en.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001705 memset(&en, 0, sizeof(en));
1706
1707 data = ELEMENT_WRAPPER_START "<value value=\"-55\"/>" ELEMENT_WRAPPER_END;
1708 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1709 assert_int_equal(en.value, -55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001710 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001711 memset(&en, 0, sizeof(en));
1712
1713 data = ELEMENT_WRAPPER_START "<value value=\"0\"/>" ELEMENT_WRAPPER_END;
1714 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1715 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001716 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001717 memset(&en, 0, sizeof(en));
1718
1719 data = ELEMENT_WRAPPER_START "<value value=\"-0\"/>" ELEMENT_WRAPPER_END;
1720 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1721 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001722 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001723 memset(&en, 0, sizeof(en));
1724
1725 /* valid positions */
David Sedlák8d552d62019-08-06 15:29:05 +02001726 data = ELEMENT_WRAPPER_START "<position value=\"55\">" EXT_SUBELEM "</position>" ELEMENT_WRAPPER_END;
David Sedlák8e7bda82019-07-16 17:57:50 +02001727 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1728 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001729 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8d552d62019-08-06 15:29:05 +02001730 assert_string_equal(en.exts[0].name, "myext:c-define");
1731 assert_int_equal(en.exts[0].insubstmt_index, 0);
1732 assert_int_equal(en.exts[0].insubstmt, LYEXT_SUBSTMT_POSITION);
1733 FREE_ARRAY(st->ctx, en.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001734 memset(&en, 0, sizeof(en));
1735
1736 data = ELEMENT_WRAPPER_START "<position value=\"0\" />" ELEMENT_WRAPPER_END;
1737 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1738 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001739 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001740 memset(&en, 0, sizeof(en));
1741
1742 /* invalid values */
1743 data = ELEMENT_WRAPPER_START "<value value=\"99999999999999999999999\"/>" ELEMENT_WRAPPER_END;
1744 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001745 logbuf_assert("Invalid value \"99999999999999999999999\" of \"value\" attribute in \"value\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001746
1747 data = ELEMENT_WRAPPER_START "<value value=\"1k\"/>" ELEMENT_WRAPPER_END;
1748 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001749 logbuf_assert("Invalid value \"1k\" of \"value\" attribute in \"value\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001750
David Sedlák69f01612019-07-17 11:41:08 +02001751 data = ELEMENT_WRAPPER_START "<value value=\"\"/>" ELEMENT_WRAPPER_END;
1752 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001753 logbuf_assert("Invalid value \"\" of \"value\" attribute in \"value\" element. Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001754
David Sedlák8e7bda82019-07-16 17:57:50 +02001755 /*invalid positions */
1756 data = ELEMENT_WRAPPER_START "<position value=\"-5\"/>" ELEMENT_WRAPPER_END;
1757 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001758 logbuf_assert("Invalid value \"-5\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001759
1760 data = ELEMENT_WRAPPER_START "<position value=\"-0\"/>" ELEMENT_WRAPPER_END;
1761 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001762 logbuf_assert("Invalid value \"-0\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001763
1764 data = ELEMENT_WRAPPER_START "<position value=\"99999999999999999999\"/>" ELEMENT_WRAPPER_END;
1765 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001766 logbuf_assert("Invalid value \"99999999999999999999\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001767
David Sedlák69f01612019-07-17 11:41:08 +02001768 data = ELEMENT_WRAPPER_START "<position value=\"\"/>" ELEMENT_WRAPPER_END;
1769 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001770 logbuf_assert("Invalid value \"\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001771
1772 st->finished_correctly = true;
1773}
1774
1775static void
1776test_prefix_elem(void **state)
1777{
1778 struct state *st = *state;
1779 const char *data;
1780 const char *value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001781 struct lysp_ext_instance *exts = NULL;
1782
1783 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\">" EXT_SUBELEM "</prefix>" ELEMENT_WRAPPER_END;
1784 assert_int_equal(test_element_helper(st, &data, &value, NULL, &exts, true), LY_SUCCESS);
1785 assert_string_equal(value, "pref");
1786 assert_string_equal(exts[0].name, "myext:c-define");
1787 assert_int_equal(exts[0].insubstmt_index, 0);
1788 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_PREFIX);
1789 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1790 exts = NULL;
1791 FREE_STRING(st->ctx, value);
David Sedlák69f01612019-07-17 11:41:08 +02001792
1793 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\"/>" ELEMENT_WRAPPER_END;
1794 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
1795 assert_string_equal(value, "pref");
1796 FREE_STRING(st->ctx, value);
1797
1798 st->finished_correctly = true;
1799}
1800
1801static void
1802test_range_elem(void **state)
1803{
1804 struct state *st = *state;
1805 const char *data;
1806 struct lysp_type type = {};
1807
1808 /* max subelems */
1809 data = ELEMENT_WRAPPER_START
1810 "<range value=\"range-str\">"
1811 "<error-message><value>err-msg</value></error-message>"
1812 "<error-app-tag value=\"err-app-tag\" />"
1813 "<description><text>desc</text></description>"
1814 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001815 EXT_SUBELEM
David Sedlák69f01612019-07-17 11:41:08 +02001816 "</range>"
1817 ELEMENT_WRAPPER_END;
1818 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1819 assert_string_equal(type.range->arg, "range-str");
1820 assert_string_equal(type.range->dsc, "desc");
1821 assert_string_equal(type.range->eapptag, "err-app-tag");
1822 assert_string_equal(type.range->emsg, "err-msg");
1823 assert_string_equal(type.range->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001824 assert_true(type.flags & LYS_SET_RANGE);
David Sedlákd1144562019-08-06 12:36:14 +02001825 assert_string_equal(type.range->exts[0].name, "myext:c-define");
1826 assert_int_equal(type.range->exts[0].insubstmt_index, 0);
1827 assert_int_equal(type.range->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák69f01612019-07-17 11:41:08 +02001828 lysp_type_free(st->ctx, &type);
1829 memset(&type, 0, sizeof(type));
1830
1831 /* min subelems */
1832 data = ELEMENT_WRAPPER_START "<range value=\"range-str\"/>" ELEMENT_WRAPPER_END;
1833 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1834 assert_string_equal(type.range->arg, "range-str");
1835 lysp_type_free(st->ctx, &type);
1836 memset(&type, 0, sizeof(type));
1837
1838 st->finished_correctly = true;
1839}
1840
1841static void
1842test_reqinstance_elem(void **state)
1843{
1844 struct state *st = *state;
1845 const char *data;
1846 struct lysp_type type = {};
1847
David Sedlákd1144562019-08-06 12:36:14 +02001848 data = ELEMENT_WRAPPER_START "<require-instance value=\"true\">" EXT_SUBELEM "</require-instance>" ELEMENT_WRAPPER_END;
David Sedlák69f01612019-07-17 11:41:08 +02001849 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1850 assert_int_equal(type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001851 assert_true(type.flags & LYS_SET_REQINST);
David Sedlákd1144562019-08-06 12:36:14 +02001852 assert_string_equal(type.exts[0].name, "myext:c-define");
1853 assert_int_equal(type.exts[0].insubstmt_index, 0);
1854 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_REQINSTANCE);
1855 lysp_type_free(st->ctx, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001856 memset(&type, 0, sizeof(type));
1857
1858 data = ELEMENT_WRAPPER_START "<require-instance value=\"false\"/>" ELEMENT_WRAPPER_END;
1859 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1860 assert_int_equal(type.require_instance, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001861 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001862 memset(&type, 0, sizeof(type));
1863
1864 data = ELEMENT_WRAPPER_START "<require-instance value=\"invalid\"/>" ELEMENT_WRAPPER_END;
1865 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1866 memset(&type, 0, sizeof(type));
David Sedlák26ea1432019-08-14 13:42:23 +02001867 logbuf_assert("Invalid value \"invalid\" of \"value\" attribute in \"require-instance\" element. Valid values are \"true\" and \"false\". Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001868
1869 st->finished_correctly = true;
1870}
1871
1872static void
1873test_revision_date_elem(void **state)
1874{
1875 struct state *st = *state;
1876 const char *data;
1877 char rev[LY_REV_SIZE];
David Sedlákd1144562019-08-06 12:36:14 +02001878 struct lysp_ext_instance *exts = NULL;
1879
1880 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\">"EXT_SUBELEM"</revision-date>" ELEMENT_WRAPPER_END;
1881 assert_int_equal(test_element_helper(st, &data, rev, NULL, &exts, true), LY_SUCCESS);
1882 assert_string_equal(rev, "2000-01-01");
1883 assert_string_equal(exts[0].name, "myext:c-define");
1884 assert_int_equal(exts[0].insubstmt_index, 0);
1885 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_REVISIONDATE);
1886 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák69f01612019-07-17 11:41:08 +02001887
1888 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\"/>" ELEMENT_WRAPPER_END;
1889 assert_int_equal(test_element_helper(st, &data, rev, NULL, NULL, true), LY_SUCCESS);
1890 assert_string_equal(rev, "2000-01-01");
1891
1892 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-50-05\"/>" ELEMENT_WRAPPER_END;
1893 assert_int_equal(test_element_helper(st, &data, rev, NULL, NULL, false), LY_EVALID);
1894 logbuf_assert("Invalid value \"2000-50-05\" of \"revision-date\". Line number 1.");
1895
1896 st->finished_correctly = true;
1897}
1898
1899static void
1900test_unique_elem(void **state)
1901{
1902 struct state *st = *state;
1903 const char *data;
1904 const char **values = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001905 struct lysp_ext_instance *exts = NULL;
1906
1907 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\">"EXT_SUBELEM"</unique>" ELEMENT_WRAPPER_END;
1908 assert_int_equal(test_element_helper(st, &data, &values, NULL, &exts, true), LY_SUCCESS);
1909 assert_string_equal(*values, "tag");
1910 assert_string_equal(exts[0].name, "myext:c-define");
1911 assert_int_equal(exts[0].insubstmt_index, 0);
1912 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_UNIQUE);
1913 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1914 FREE_STRING(st->ctx, *values);
1915 LY_ARRAY_FREE(values);
1916 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001917
1918 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\"/>" ELEMENT_WRAPPER_END;
1919 assert_int_equal(test_element_helper(st, &data, &values, NULL, NULL, true), LY_SUCCESS);
1920 assert_string_equal(*values, "tag");
1921 FREE_STRING(st->ctx, *values);
1922 LY_ARRAY_FREE(values);
David Sedlákd1144562019-08-06 12:36:14 +02001923 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001924
1925 st->finished_correctly = true;
1926}
1927
1928static void
1929test_units_elem(void **state)
1930{
1931 struct state *st = *state;
1932 const char *data;
1933 const char *values = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001934 struct lysp_ext_instance *exts = NULL;
1935
1936 data = ELEMENT_WRAPPER_START "<units name=\"name\">"EXT_SUBELEM"</units>" ELEMENT_WRAPPER_END;
1937 assert_int_equal(test_element_helper(st, &data, &values, NULL, &exts, true), LY_SUCCESS);
1938 assert_string_equal(values, "name");
1939 assert_string_equal(exts[0].name, "myext:c-define");
1940 assert_int_equal(exts[0].insubstmt_index, 0);
1941 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_UNITS);
1942 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1943 FREE_STRING(st->ctx, values);
1944 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001945
1946 data = ELEMENT_WRAPPER_START "<units name=\"name\"/>" ELEMENT_WRAPPER_END;
1947 assert_int_equal(test_element_helper(st, &data, &values, NULL, NULL, true), LY_SUCCESS);
1948 assert_string_equal(values, "name");
1949 FREE_STRING(st->ctx, values);
David Sedlákd1144562019-08-06 12:36:14 +02001950 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001951
1952 st->finished_correctly = true;
1953}
1954
1955static void
1956test_when_elem(void **state)
1957{
1958 struct state *st = *state;
1959 const char *data;
1960 struct lysp_when *when = NULL;
1961
1962 data = ELEMENT_WRAPPER_START
1963 "<when condition=\"cond\">"
1964 "<description><text>desc</text></description>"
1965 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001966 EXT_SUBELEM
David Sedlák69f01612019-07-17 11:41:08 +02001967 "</when>"
1968 ELEMENT_WRAPPER_END;
1969 assert_int_equal(test_element_helper(st, &data, &when, NULL, NULL, true), LY_SUCCESS);
1970 assert_string_equal(when->cond, "cond");
1971 assert_string_equal(when->dsc, "desc");
1972 assert_string_equal(when->ref, "ref");
David Sedlákd1144562019-08-06 12:36:14 +02001973 assert_string_equal(when->exts[0].name, "myext:c-define");
1974 assert_int_equal(when->exts[0].insubstmt_index, 0);
1975 assert_int_equal(when->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák69f01612019-07-17 11:41:08 +02001976 lysp_when_free(st->ctx, when);
1977 free(when);
1978 when = NULL;
1979
1980 data = ELEMENT_WRAPPER_START "<when condition=\"cond\" />" ELEMENT_WRAPPER_END;
1981 assert_int_equal(test_element_helper(st, &data, &when, NULL, NULL, true), LY_SUCCESS);
1982 assert_string_equal(when->cond, "cond");
1983 lysp_when_free(st->ctx, when);
1984 free(when);
1985 when = NULL;
1986
1987 st->finished_correctly = true;
1988}
1989
1990static void
1991test_yin_text_value_elem(void **state)
1992{
1993 struct state *st = *state;
1994 const char *data;
1995 const char *val;
1996
1997 data = ELEMENT_WRAPPER_START "<text>text</text>" ELEMENT_WRAPPER_END;
1998 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1999 assert_string_equal(val, "text");
2000 FREE_STRING(st->ctx, val);
2001
2002 data = "<error-message xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <value>text</value> </error-message>";
2003 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2004 assert_string_equal(val, "text");
2005 FREE_STRING(st->ctx, val);
2006
2007 data = ELEMENT_WRAPPER_START "<text></text>" ELEMENT_WRAPPER_END;
2008 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2009 assert_string_equal("", val);
2010 FREE_STRING(st->ctx, val);
2011
David Sedlák8e7bda82019-07-16 17:57:50 +02002012 st->finished_correctly = true;
2013}
David Sedlák32488102019-07-15 17:44:10 +02002014
David Sedlák374d2b32019-07-17 15:06:55 +02002015static void
2016test_type_elem(void **state)
2017{
2018 struct state *st = *state;
2019 const char *data;
2020 struct lysp_type type = {};
2021
2022 /* max subelems */
2023 data = ELEMENT_WRAPPER_START
2024 "<type name=\"type-name\">"
2025 "<base name=\"base-name\"/>"
2026 "<bit name=\"bit\"/>"
2027 "<enum name=\"enum\"/>"
2028 "<fraction-digits value=\"2\"/>"
2029 "<length value=\"length\"/>"
2030 "<path value=\"path\"/>"
2031 "<pattern value=\"pattern\"/>"
2032 "<range value=\"range\" />"
2033 "<require-instance value=\"true\"/>"
2034 "<type name=\"sub-type-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002035 EXT_SUBELEM
David Sedlák374d2b32019-07-17 15:06:55 +02002036 "</type>"
2037 ELEMENT_WRAPPER_END;
2038 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
2039 assert_string_equal(type.name, "type-name");
2040 assert_string_equal(*type.bases, "base-name");
2041 assert_string_equal(type.bits->name, "bit");
2042 assert_string_equal(type.enums->name, "enum");
2043 assert_int_equal(type.fraction_digits, 2);
2044 assert_string_equal(type.length->arg, "length");
2045 assert_string_equal(type.path, "path");
2046 assert_string_equal(type.patterns->arg, "\006pattern");
2047 assert_string_equal(type.range->arg, "range");
2048 assert_int_equal(type.require_instance, 1);
2049 assert_string_equal(type.types->name, "sub-type-name");
David Sedlákd1144562019-08-06 12:36:14 +02002050 assert_string_equal(type.exts[0].name, "myext:c-define");
2051 assert_int_equal(type.exts[0].insubstmt_index, 0);
2052 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002053 assert_true(type.flags & LYS_SET_BASE);
2054 assert_true(type.flags & LYS_SET_BIT);
2055 assert_true(type.flags & LYS_SET_ENUM);
2056 assert_true(type.flags & LYS_SET_FRDIGITS);
2057 assert_true(type.flags & LYS_SET_LENGTH);
2058 assert_true(type.flags & LYS_SET_PATH);
2059 assert_true(type.flags & LYS_SET_PATTERN);
2060 assert_true(type.flags & LYS_SET_RANGE);
2061 assert_true(type.flags & LYS_SET_REQINST);
2062 assert_true(type.flags & LYS_SET_TYPE);
David Sedlákd1144562019-08-06 12:36:14 +02002063 lysp_type_free(st->ctx, &type);
David Sedlák374d2b32019-07-17 15:06:55 +02002064 memset(&type, 0, sizeof(type));
2065
2066 /* min subelems */
2067 data = ELEMENT_WRAPPER_START "<type name=\"type-name\"/>" ELEMENT_WRAPPER_END;
2068 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
2069 lysp_type_free(st->ctx, &type);
2070 memset(&type, 0, sizeof(type));
2071
2072 st->finished_correctly = true;
2073}
2074
David Sedlák1af868e2019-07-17 17:03:14 +02002075static void
2076test_max_elems_elem(void **state)
2077{
2078 struct state *st = *state;
2079 const char *data;
2080 struct lysp_node_list list = {};
2081 struct lysp_node_leaflist llist = {};
2082 struct lysp_refine refine = {};
2083
David Sedlákd1144562019-08-06 12:36:14 +02002084 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 +02002085 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
2086 assert_int_equal(refine.max, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002087 assert_true(refine.flags & LYS_SET_MAX);
David Sedlákd1144562019-08-06 12:36:14 +02002088 assert_string_equal(refine.exts[0].name, "myext:c-define");
2089 assert_int_equal(refine.exts[0].insubstmt_index, 0);
2090 assert_int_equal(refine.exts[0].insubstmt, LYEXT_SUBSTMT_MAX);
2091 FREE_ARRAY(st->ctx, refine.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02002092
David Sedlákd1144562019-08-06 12:36:14 +02002093 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 +02002094 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, true), LY_SUCCESS);
2095 assert_int_equal(list.max, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002096 assert_true(list.flags & LYS_SET_MAX);
David Sedlákd1144562019-08-06 12:36:14 +02002097 assert_string_equal(list.exts[0].name, "myext:c-define");
2098 assert_int_equal(list.exts[0].insubstmt_index, 0);
2099 assert_int_equal(list.exts[0].insubstmt, LYEXT_SUBSTMT_MAX);
2100 FREE_ARRAY(st->ctx, list.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02002101
David Sedlákd1144562019-08-06 12:36:14 +02002102 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 +02002103 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, true), LY_SUCCESS);
2104 assert_int_equal(llist.max, 85);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002105 assert_true(llist.flags & LYS_SET_MAX);
David Sedlákd1144562019-08-06 12:36:14 +02002106 assert_string_equal(llist.exts[0].name, "myext:c-define");
2107 assert_int_equal(llist.exts[0].insubstmt_index, 0);
2108 assert_int_equal(llist.exts[0].insubstmt, LYEXT_SUBSTMT_MAX);
2109 FREE_ARRAY(st->ctx, llist.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02002110
2111 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"10\"/> </refine>";
2112 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
2113 assert_int_equal(refine.max, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002114 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02002115
2116 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"0\"/> </list>";
2117 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002118 logbuf_assert("Invalid value \"0\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002119
2120 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"-10\"/> </list>";
2121 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002122 logbuf_assert("Invalid value \"-10\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002123
2124 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"k\"/> </list>";
2125 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002126 logbuf_assert("Invalid value \"k\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002127
2128 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"u12\"/> </list>";
2129 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002130 logbuf_assert("Invalid value \"u12\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002131
2132 st->finished_correctly = true;
2133}
2134
David Sedlák09e18c92019-07-18 11:17:11 +02002135static void
2136test_min_elems_elem(void **state)
2137{
2138 struct state *st = *state;
2139 const char *data;
2140 struct lysp_node_list list = {};
2141 struct lysp_node_leaflist llist = {};
2142 struct lysp_refine refine = {};
2143
David Sedlákd1144562019-08-06 12:36:14 +02002144 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 +02002145 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
2146 assert_int_equal(refine.min, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002147 assert_true(refine.flags & LYS_SET_MIN);
David Sedlákd1144562019-08-06 12:36:14 +02002148 assert_string_equal(refine.exts[0].name, "myext:c-define");
2149 assert_int_equal(refine.exts[0].insubstmt_index, 0);
2150 assert_int_equal(refine.exts[0].insubstmt, LYEXT_SUBSTMT_MIN);
2151 FREE_ARRAY(st->ctx, refine.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02002152
David Sedlákd1144562019-08-06 12:36:14 +02002153 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 +02002154 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, true), LY_SUCCESS);
2155 assert_int_equal(list.min, 41);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002156 assert_true(list.flags & LYS_SET_MIN);
David Sedlákd1144562019-08-06 12:36:14 +02002157 assert_string_equal(list.exts[0].name, "myext:c-define");
2158 assert_int_equal(list.exts[0].insubstmt_index, 0);
2159 assert_int_equal(list.exts[0].insubstmt, LYEXT_SUBSTMT_MIN);
2160 FREE_ARRAY(st->ctx, list.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02002161
David Sedlákd1144562019-08-06 12:36:14 +02002162 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 +02002163 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, true), LY_SUCCESS);
2164 assert_int_equal(llist.min, 50);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002165 assert_true(llist.flags & LYS_SET_MIN);
David Sedlákd1144562019-08-06 12:36:14 +02002166 assert_string_equal(llist.exts[0].name, "myext:c-define");
2167 assert_int_equal(llist.exts[0].insubstmt_index, 0);
2168 assert_int_equal(llist.exts[0].insubstmt, LYEXT_SUBSTMT_MIN);
2169 FREE_ARRAY(st->ctx, llist.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02002170
2171 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"-5\"/> </leaf-list>";
2172 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002173 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 +02002174
2175 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"99999999999999999\"/> </leaf-list>";
2176 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002177 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 +02002178
2179 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"5k\"/> </leaf-list>";
2180 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002181 logbuf_assert("Invalid value \"5k\" of \"value\" attribute in \"min-elements\" element. Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02002182
2183 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"05\"/> </leaf-list>";
2184 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002185 logbuf_assert("Invalid value \"05\" of \"value\" attribute in \"min-elements\" element. Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02002186
2187 st->finished_correctly = true;
2188}
2189
David Sedláka2dad212019-07-18 12:45:19 +02002190static void
2191test_ordby_elem(void **state)
2192{
2193 struct state *st = *state;
2194 const char *data;
2195 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02002196 struct lysp_ext_instance *exts = NULL;
David Sedláka2dad212019-07-18 12:45:19 +02002197
David Sedlákd1144562019-08-06 12:36:14 +02002198 data = ELEMENT_WRAPPER_START "<ordered-by value=\"system\">"EXT_SUBELEM"</ordered-by>" ELEMENT_WRAPPER_END;
2199 assert_int_equal(test_element_helper(st, &data, &flags, NULL, &exts, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002200 assert_true(flags & LYS_ORDBY_SYSTEM);
David Sedlákd1144562019-08-06 12:36:14 +02002201 assert_string_equal(exts[0].name, "myext:c-define");
2202 assert_int_equal(exts[0].insubstmt_index, 0);
2203 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ORDEREDBY);
2204 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedláka2dad212019-07-18 12:45:19 +02002205
2206 data = ELEMENT_WRAPPER_START "<ordered-by value=\"user\"/>" ELEMENT_WRAPPER_END;
2207 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002208 assert_true(flags & LYS_ORDBY_USER);
David Sedláka2dad212019-07-18 12:45:19 +02002209
2210 data = ELEMENT_WRAPPER_START "<ordered-by value=\"inv\"/>" ELEMENT_WRAPPER_END;
2211 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02002212 logbuf_assert("Invalid value \"inv\" of \"value\" attribute in \"ordered-by\" element. Valid values are \"system\" and \"user\". Line number 1.");
David Sedláka2dad212019-07-18 12:45:19 +02002213
2214 st->finished_correctly = true;
2215}
2216
David Sedlák8a83bbb2019-07-18 14:46:00 +02002217static void
2218test_any_elem(void **state)
2219{
2220 struct state *st = *state;
2221 const char *data;
2222 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002223 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák8a83bbb2019-07-18 14:46:00 +02002224 struct lysp_node_anydata *parsed = NULL;
2225
2226 /* anyxml max subelems */
2227 data = ELEMENT_WRAPPER_START
2228 "<anyxml name=\"any-name\">"
2229 "<config value=\"true\" />"
2230 "<description><text>desc</text></description>"
2231 "<if-feature name=\"feature\" />"
2232 "<mandatory value=\"true\" />"
2233 "<must condition=\"must-cond\" />"
2234 "<reference><text>ref</text></reference>"
2235 "<status value=\"deprecated\"/>"
2236 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002237 EXT_SUBELEM
David Sedlák8a83bbb2019-07-18 14:46:00 +02002238 "</anyxml>"
2239 ELEMENT_WRAPPER_END;
2240 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2241 parsed = (struct lysp_node_anydata *)siblings;
2242 assert_null(parsed->parent);
2243 assert_int_equal(parsed->nodetype, LYS_ANYXML);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002244 assert_true(parsed->flags & LYS_CONFIG_W);
2245 assert_true(parsed->flags & LYS_MAND_TRUE);
2246 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002247 assert_null(parsed->next);
2248 assert_string_equal(parsed->name, "any-name");
2249 assert_string_equal(parsed->dsc, "desc");
2250 assert_string_equal(parsed->ref, "ref");
2251 assert_string_equal(parsed->when->cond, "when-cond");
2252 assert_string_equal(*parsed->iffeatures, "feature");
David Sedlákd1144562019-08-06 12:36:14 +02002253 assert_string_equal(parsed->exts[0].name, "myext:c-define");
2254 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2255 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002256 lysp_node_free(st->ctx, siblings);
2257 siblings = NULL;
2258
2259 /* anydata max subelems */
2260 data = ELEMENT_WRAPPER_START
2261 "<anydata name=\"any-name\">"
2262 "<config value=\"true\" />"
2263 "<description><text>desc</text></description>"
2264 "<if-feature name=\"feature\" />"
2265 "<mandatory value=\"true\" />"
2266 "<must condition=\"must-cond\" />"
2267 "<reference><text>ref</text></reference>"
2268 "<status value=\"deprecated\"/>"
2269 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002270 EXT_SUBELEM
David Sedlák8a83bbb2019-07-18 14:46:00 +02002271 "</anydata>"
2272 ELEMENT_WRAPPER_END;
2273 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2274 parsed = (struct lysp_node_anydata *)siblings;
2275 assert_null(parsed->parent);
2276 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002277 assert_true(parsed->flags & LYS_CONFIG_W);
2278 assert_true(parsed->flags & LYS_MAND_TRUE);
2279 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002280 assert_null(parsed->next);
2281 assert_string_equal(parsed->name, "any-name");
2282 assert_string_equal(parsed->dsc, "desc");
2283 assert_string_equal(parsed->ref, "ref");
2284 assert_string_equal(parsed->when->cond, "when-cond");
2285 assert_string_equal(*parsed->iffeatures, "feature");
David Sedlákd1144562019-08-06 12:36:14 +02002286 assert_string_equal(parsed->exts[0].name, "myext:c-define");
2287 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2288 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002289 lysp_node_free(st->ctx, siblings);
2290 siblings = NULL;
2291
2292 /* min subelems */
2293 node_meta.parent = (void *)0x10;
2294 data = ELEMENT_WRAPPER_START "<anydata name=\"any-name\"> </anydata>" ELEMENT_WRAPPER_END;
2295 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2296 parsed = (struct lysp_node_anydata *)siblings;
2297 assert_ptr_equal(parsed->parent, node_meta.parent);
2298 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
2299 assert_null(parsed->next);
2300 assert_null(parsed->exts);
2301 lysp_node_free(st->ctx, siblings);
2302
2303 st->finished_correctly = true;
2304}
2305
David Sedlák203ca3a2019-07-18 15:26:25 +02002306static void
2307test_leaf_elem(void **state)
2308{
2309 struct state *st = *state;
2310 const char *data;
2311 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002312 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák203ca3a2019-07-18 15:26:25 +02002313 struct lysp_node_leaf *parsed = NULL;
2314
2315 /* max elements */
2316 data = ELEMENT_WRAPPER_START
2317 "<leaf name=\"leaf\">"
2318 "<config value=\"true\" />"
2319 "<default value=\"def-val\"/>"
2320 "<description><text>desc</text></description>"
2321 "<if-feature name=\"feature\" />"
2322 "<mandatory value=\"true\" />"
2323 "<must condition=\"must-cond\" />"
2324 "<reference><text>ref</text></reference>"
2325 "<status value=\"deprecated\"/>"
2326 "<type name=\"type\"/>"
2327 "<units name=\"uni\"/>"
2328 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002329 EXT_SUBELEM
David Sedlák203ca3a2019-07-18 15:26:25 +02002330 "</leaf>"
2331 ELEMENT_WRAPPER_END;
2332 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2333 parsed = (struct lysp_node_leaf *)siblings;
2334 assert_null(parsed->parent);
2335 assert_int_equal(parsed->nodetype, LYS_LEAF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002336 assert_true(parsed->flags & LYS_CONFIG_W);
2337 assert_true(parsed->flags & LYS_MAND_TRUE);
2338 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák203ca3a2019-07-18 15:26:25 +02002339 assert_null(parsed->next);
2340 assert_string_equal(parsed->name, "leaf");
2341 assert_string_equal(parsed->dsc, "desc");
2342 assert_string_equal(parsed->ref, "ref");
2343 assert_string_equal(parsed->when->cond, "when-cond");
2344 assert_string_equal(*parsed->iffeatures, "feature");
David Sedlákd1144562019-08-06 12:36:14 +02002345 assert_string_equal(parsed->exts[0].name, "myext:c-define");
2346 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2347 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák203ca3a2019-07-18 15:26:25 +02002348 assert_string_equal(parsed->musts->arg, "must-cond");
2349 assert_string_equal(parsed->type.name, "type");
2350 assert_string_equal(parsed->units, "uni");
2351 assert_string_equal(parsed->dflt, "def-val");
2352 lysp_node_free(st->ctx, siblings);
2353 siblings = NULL;
2354
2355 /* min elements */
2356 data = ELEMENT_WRAPPER_START "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>" ELEMENT_WRAPPER_END;
2357 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2358 parsed = (struct lysp_node_leaf *)siblings;
2359 assert_string_equal(parsed->name, "leaf");
2360 assert_string_equal(parsed->type.name, "type");
2361 lysp_node_free(st->ctx, siblings);
2362 siblings = NULL;
2363
2364 st->finished_correctly = true;
2365}
2366
David Sedlákc3da3ef2019-07-19 12:56:08 +02002367static void
2368test_leaf_list_elem(void **state)
2369{
2370 struct state *st = *state;
2371 const char *data;
2372 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002373 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlákc3da3ef2019-07-19 12:56:08 +02002374 struct lysp_node_leaflist *parsed = NULL;
2375
2376 data = ELEMENT_WRAPPER_START
2377 "<leaf-list name=\"llist\">"
2378 "<config value=\"true\" />"
2379 "<default value=\"def-val0\"/>"
2380 "<default value=\"def-val1\"/>"
2381 "<description><text>desc</text></description>"
2382 "<if-feature name=\"feature\"/>"
2383 "<max-elements value=\"5\"/>"
2384 "<must condition=\"must-cond\"/>"
2385 "<ordered-by value=\"user\" />"
2386 "<reference><text>ref</text></reference>"
2387 "<status value=\"current\"/>"
2388 "<type name=\"type\"/>"
2389 "<units name=\"uni\"/>"
2390 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002391 EXT_SUBELEM
David Sedlákc3da3ef2019-07-19 12:56:08 +02002392 "</leaf-list>"
2393 ELEMENT_WRAPPER_END;
2394 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2395 parsed = (struct lysp_node_leaflist *)siblings;
2396 assert_string_equal(parsed->dflts[0], "def-val0");
2397 assert_string_equal(parsed->dflts[1], "def-val1");
2398 assert_string_equal(parsed->dsc, "desc");
2399 assert_string_equal(*parsed->iffeatures, "feature");
2400 assert_int_equal(parsed->max, 5);
2401 assert_string_equal(parsed->musts->arg, "must-cond");
2402 assert_string_equal(parsed->name, "llist");
2403 assert_null(parsed->next);
2404 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2405 assert_null(parsed->parent);
2406 assert_string_equal(parsed->ref, "ref");
2407 assert_string_equal(parsed->type.name, "type");
2408 assert_string_equal(parsed->units, "uni");
2409 assert_string_equal(parsed->when->cond, "when-cond");
2410 assert_true(parsed->flags & LYS_CONFIG_W);
2411 assert_true(parsed->flags & LYS_ORDBY_USER);
2412 assert_true(parsed->flags & LYS_STATUS_CURR);
David Sedlákd1144562019-08-06 12:36:14 +02002413 assert_string_equal(parsed->exts[0].name, "myext:c-define");
2414 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2415 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002416 lysp_node_free(st->ctx, siblings);
2417 siblings = NULL;
2418
2419 data = ELEMENT_WRAPPER_START
2420 "<leaf-list name=\"llist\">"
2421 "<config value=\"true\" />"
2422 "<description><text>desc</text></description>"
2423 "<if-feature name=\"feature\"/>"
2424 "<min-elements value=\"5\"/>"
2425 "<must condition=\"must-cond\"/>"
2426 "<ordered-by value=\"user\" />"
2427 "<reference><text>ref</text></reference>"
2428 "<status value=\"current\"/>"
2429 "<type name=\"type\"/>"
2430 "<units name=\"uni\"/>"
2431 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002432 EXT_SUBELEM
David Sedlákc3da3ef2019-07-19 12:56:08 +02002433 "</leaf-list>"
2434 ELEMENT_WRAPPER_END;
2435 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2436 parsed = (struct lysp_node_leaflist *)siblings;
2437 assert_string_equal(parsed->dsc, "desc");
2438 assert_string_equal(*parsed->iffeatures, "feature");
2439 assert_int_equal(parsed->min, 5);
2440 assert_string_equal(parsed->musts->arg, "must-cond");
2441 assert_string_equal(parsed->name, "llist");
2442 assert_null(parsed->next);
2443 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2444 assert_null(parsed->parent);
2445 assert_string_equal(parsed->ref, "ref");
2446 assert_string_equal(parsed->type.name, "type");
2447 assert_string_equal(parsed->units, "uni");
2448 assert_string_equal(parsed->when->cond, "when-cond");
2449 assert_true(parsed->flags & LYS_CONFIG_W);
2450 assert_true(parsed->flags & LYS_ORDBY_USER);
2451 assert_true(parsed->flags & LYS_STATUS_CURR);
David Sedlákd1144562019-08-06 12:36:14 +02002452 assert_string_equal(parsed->exts[0].name, "myext:c-define");
2453 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2454 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002455 lysp_node_free(st->ctx, siblings);
2456 siblings = NULL;
2457
2458 data = ELEMENT_WRAPPER_START
2459 "<leaf-list name=\"llist\">"
2460 "<config value=\"true\" />"
2461 "<description><text>desc</text></description>"
2462 "<if-feature name=\"feature\"/>"
2463 "<max-elements value=\"15\"/>"
2464 "<min-elements value=\"5\"/>"
2465 "<must condition=\"must-cond\"/>"
2466 "<ordered-by value=\"user\" />"
2467 "<reference><text>ref</text></reference>"
2468 "<status value=\"current\"/>"
2469 "<type name=\"type\"/>"
2470 "<units name=\"uni\"/>"
2471 "<when condition=\"when-cond\"/>"
2472 "</leaf-list>"
2473 ELEMENT_WRAPPER_END;
2474 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2475 parsed = (struct lysp_node_leaflist *)siblings;
2476 assert_string_equal(parsed->dsc, "desc");
2477 assert_string_equal(*parsed->iffeatures, "feature");
2478 assert_int_equal(parsed->min, 5);
2479 assert_int_equal(parsed->max, 15);
2480 assert_string_equal(parsed->musts->arg, "must-cond");
2481 assert_string_equal(parsed->name, "llist");
2482 assert_null(parsed->next);
2483 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2484 assert_null(parsed->parent);
2485 assert_string_equal(parsed->ref, "ref");
2486 assert_string_equal(parsed->type.name, "type");
2487 assert_string_equal(parsed->units, "uni");
2488 assert_string_equal(parsed->when->cond, "when-cond");
2489 assert_true(parsed->flags & LYS_CONFIG_W);
2490 assert_true(parsed->flags & LYS_ORDBY_USER);
2491 assert_true(parsed->flags & LYS_STATUS_CURR);
2492 lysp_node_free(st->ctx, siblings);
2493 siblings = NULL;
2494
2495 data = ELEMENT_WRAPPER_START
2496 "<leaf-list name=\"llist\">"
2497 "<type name=\"type\"/>"
2498 "</leaf-list>"
2499 ELEMENT_WRAPPER_END;
2500 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2501 parsed = (struct lysp_node_leaflist *)siblings;
2502 assert_string_equal(parsed->name, "llist");
2503 assert_string_equal(parsed->type.name, "type");
2504 lysp_node_free(st->ctx, siblings);
2505 siblings = NULL;
2506
2507 /* invalid combinations */
2508 data = ELEMENT_WRAPPER_START
2509 "<leaf-list name=\"llist\">"
2510 "<max-elements value=\"5\"/>"
2511 "<min-elements value=\"15\"/>"
2512 "<type name=\"type\"/>"
2513 "</leaf-list>"
2514 ELEMENT_WRAPPER_END;
2515 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
2516 logbuf_assert("Invalid combination of min-elements and max-elements: min value 15 is bigger than the max value 5. Line number 1.");
2517 lysp_node_free(st->ctx, siblings);
2518 siblings = NULL;
2519
2520 data = ELEMENT_WRAPPER_START
2521 "<leaf-list name=\"llist\">"
2522 "<default value=\"def-val1\"/>"
2523 "<min-elements value=\"15\"/>"
2524 "<type name=\"type\"/>"
2525 "</leaf-list>"
2526 ELEMENT_WRAPPER_END;
2527 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002528 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 +02002529 lysp_node_free(st->ctx, siblings);
2530 siblings = NULL;
2531
2532 data = ELEMENT_WRAPPER_START
2533 "<leaf-list name=\"llist\">"
2534 "</leaf-list>"
2535 ELEMENT_WRAPPER_END;
2536 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002537 logbuf_assert("Missing mandatory sub-element \"type\" of \"leaf-list\" element. Line number 1.");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002538 lysp_node_free(st->ctx, siblings);
2539 siblings = NULL;
2540
2541 st->finished_correctly = true;
2542}
2543
David Sedlákcb39f642019-07-19 13:19:55 +02002544static void
2545test_presence_elem(void **state)
2546{
2547 struct state *st = *state;
2548 const char *data;
2549 const char *val;
David Sedlákd1144562019-08-06 12:36:14 +02002550 struct lysp_ext_instance *exts = NULL;
2551
2552 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\">"EXT_SUBELEM"</presence>" ELEMENT_WRAPPER_END;
2553 assert_int_equal(test_element_helper(st, &data, &val, NULL, &exts, true), LY_SUCCESS);
2554 assert_string_equal(val, "presence-val");
2555 assert_string_equal(exts[0].name, "myext:c-define");
2556 assert_int_equal(exts[0].insubstmt_index, 0);
2557 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_PRESENCE);
2558 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
2559 FREE_STRING(st->ctx, val);
David Sedlákcb39f642019-07-19 13:19:55 +02002560
2561 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\"/>" ELEMENT_WRAPPER_END;
2562 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2563 assert_string_equal(val, "presence-val");
2564 FREE_STRING(st->ctx, val);
2565
2566 data = ELEMENT_WRAPPER_START "<presence/>" ELEMENT_WRAPPER_END;
2567 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
2568 logbuf_assert("Missing mandatory attribute value of presence element. Line number 1.");
2569
2570 st->finished_correctly = true;
2571}
2572
David Sedlák12470a82019-07-19 13:44:36 +02002573static void
2574test_key_elem(void **state)
2575{
2576 struct state *st = *state;
2577 const char *data;
2578 const char *val;
David Sedlákd1144562019-08-06 12:36:14 +02002579 struct lysp_ext_instance *exts = NULL;
2580
2581 data = ELEMENT_WRAPPER_START "<key value=\"key-value\">"EXT_SUBELEM"</key>" ELEMENT_WRAPPER_END;
2582 assert_int_equal(test_element_helper(st, &data, &val, NULL, &exts, true), LY_SUCCESS);
2583 assert_string_equal(val, "key-value");
2584 assert_string_equal(exts[0].name, "myext:c-define");
2585 assert_int_equal(exts[0].insubstmt_index, 0);
2586 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_KEY);
2587 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
2588 FREE_STRING(st->ctx, val);
David Sedlák12470a82019-07-19 13:44:36 +02002589
2590 data = ELEMENT_WRAPPER_START "<key value=\"key-value\"/>" ELEMENT_WRAPPER_END;
2591 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2592 assert_string_equal(val, "key-value");
2593 FREE_STRING(st->ctx, val);
2594
2595 data = ELEMENT_WRAPPER_START "<key/>" ELEMENT_WRAPPER_END;
2596 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
2597 logbuf_assert("Missing mandatory attribute value of key element. Line number 1.");
2598
2599 st->finished_correctly = true;
2600}
2601
David Sedlák04e17b22019-07-19 15:29:48 +02002602static void
2603test_typedef_elem(void **state)
2604{
2605 struct state *st = *state;
2606 const char *data;
2607 struct lysp_tpdf *tpdfs = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002608 struct tree_node_meta typdef_meta = {NULL, (struct lysp_node **)&tpdfs};
David Sedlák04e17b22019-07-19 15:29:48 +02002609
2610 data = ELEMENT_WRAPPER_START
2611 "<typedef name=\"tpdf-name\">"
2612 "<default value=\"def-val\"/>"
2613 "<description><text>desc-text</text></description>"
2614 "<reference><text>ref-text</text></reference>"
2615 "<status value=\"current\"/>"
2616 "<type name=\"type\"/>"
2617 "<units name=\"uni\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002618 EXT_SUBELEM
David Sedlák04e17b22019-07-19 15:29:48 +02002619 "</typedef>"
2620 ELEMENT_WRAPPER_END;
2621 assert_int_equal(test_element_helper(st, &data, &typdef_meta, NULL, NULL, true), LY_SUCCESS);
2622 assert_string_equal(tpdfs[0].dflt, "def-val");
2623 assert_string_equal(tpdfs[0].dsc, "desc-text");
David Sedlák04e17b22019-07-19 15:29:48 +02002624 assert_string_equal(tpdfs[0].name, "tpdf-name");
2625 assert_string_equal(tpdfs[0].ref, "ref-text");
2626 assert_string_equal(tpdfs[0].type.name, "type");
2627 assert_string_equal(tpdfs[0].units, "uni");
2628 assert_true(tpdfs[0].flags & LYS_STATUS_CURR);
David Sedlákd1144562019-08-06 12:36:14 +02002629 assert_string_equal(tpdfs[0].exts[0].name, "myext:c-define");
2630 assert_int_equal(tpdfs[0].exts[0].insubstmt_index, 0);
2631 assert_int_equal(tpdfs[0].exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák04e17b22019-07-19 15:29:48 +02002632 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2633 tpdfs = NULL;
2634
2635 data = ELEMENT_WRAPPER_START
2636 "<typedef name=\"tpdf-name\">"
2637 "<type name=\"type\"/>"
2638 "</typedef>"
2639 ELEMENT_WRAPPER_END;
2640 assert_int_equal(test_element_helper(st, &data, &typdef_meta, NULL, NULL, true), LY_SUCCESS);
2641 assert_string_equal(tpdfs[0].name, "tpdf-name");
2642 assert_string_equal(tpdfs[0].type.name, "type");
2643 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2644 tpdfs = NULL;
2645
2646 st->finished_correctly = true;
2647}
2648
David Sedlákd2d676a2019-07-22 11:28:19 +02002649static void
2650test_refine_elem(void **state)
2651{
2652 struct state *st = *state;
2653 const char *data;
2654 struct lysp_refine *refines = NULL;
2655
2656 /* max subelems */
2657 data = ELEMENT_WRAPPER_START
2658 "<refine target-node=\"target\">"
2659 "<if-feature name=\"feature\" />"
2660 "<must condition=\"cond\" />"
2661 "<presence value=\"presence\" />"
2662 "<default value=\"def\" />"
2663 "<config value=\"true\" />"
2664 "<mandatory value=\"true\" />"
2665 "<min-elements value=\"10\" />"
2666 "<max-elements value=\"20\" />"
2667 "<description><text>desc</text></description>"
2668 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02002669 EXT_SUBELEM
David Sedlákd2d676a2019-07-22 11:28:19 +02002670 "</refine>"
2671 ELEMENT_WRAPPER_END;
2672 assert_int_equal(test_element_helper(st, &data, &refines, NULL, NULL, true), LY_SUCCESS);
2673 assert_string_equal(refines->nodeid, "target");
2674 assert_string_equal(*refines->dflts, "def");
2675 assert_string_equal(refines->dsc, "desc");
David Sedlákd2d676a2019-07-22 11:28:19 +02002676 assert_true(refines->flags & LYS_CONFIG_W);
2677 assert_true(refines->flags & LYS_MAND_TRUE);
2678 assert_string_equal(*refines->iffeatures, "feature");
2679 assert_int_equal(refines->max, 20);
2680 assert_int_equal(refines->min, 10);
2681 assert_string_equal(refines->musts->arg, "cond");
2682 assert_string_equal(refines->presence, "presence");
2683 assert_string_equal(refines->ref, "ref");
David Sedlákd1144562019-08-06 12:36:14 +02002684 assert_string_equal(refines->exts[0].name, "myext:c-define");
2685 assert_int_equal(refines->exts[0].insubstmt_index, 0);
2686 assert_int_equal(refines->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákd2d676a2019-07-22 11:28:19 +02002687 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2688 refines = NULL;
2689
2690 /* min subelems */
2691 data = ELEMENT_WRAPPER_START "<refine target-node=\"target\" />" ELEMENT_WRAPPER_END;
2692 assert_int_equal(test_element_helper(st, &data, &refines, NULL, NULL, true), LY_SUCCESS);
2693 assert_string_equal(refines->nodeid, "target");
2694 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2695 refines = NULL;
2696
2697 st->finished_correctly = true;
2698}
2699
David Sedlák0d6de5a2019-07-22 13:25:44 +02002700static void
2701test_uses_elem(void **state)
2702{
2703 struct state *st = *state;
2704 const char *data;
2705 struct lysp_node *siblings = NULL;
2706 struct tree_node_meta node_meta = {NULL, &siblings};
2707 struct lysp_node_uses *parsed = NULL;
2708
2709 /* max subelems */
2710 data = ELEMENT_WRAPPER_START
2711 "<uses name=\"uses-name\">"
2712 "<when condition=\"cond\" />"
2713 "<if-feature name=\"feature\" />"
2714 "<status value=\"obsolete\" />"
2715 "<description><text>desc</text></description>"
2716 "<reference><text>ref</text></reference>"
2717 "<refine target-node=\"target\"/>"
David Sedlák992fb7c2019-07-24 16:51:01 +02002718 "<augment target-node=\"target\" />"
David Sedlákd1144562019-08-06 12:36:14 +02002719 EXT_SUBELEM
David Sedlák0d6de5a2019-07-22 13:25:44 +02002720 "</uses>"
2721 ELEMENT_WRAPPER_END;
2722 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2723 parsed = (struct lysp_node_uses *)&siblings[0];
2724 assert_string_equal(parsed->name, "uses-name");
2725 assert_string_equal(parsed->dsc, "desc");
David Sedlák0d6de5a2019-07-22 13:25:44 +02002726 assert_true(parsed->flags & LYS_STATUS_OBSLT);
2727 assert_string_equal(*parsed->iffeatures, "feature");
2728 assert_null(parsed->next);
2729 assert_int_equal(parsed->nodetype, LYS_USES);
2730 assert_null(parsed->parent);
2731 assert_string_equal(parsed->ref, "ref");
2732 assert_string_equal(parsed->refines->nodeid, "target");
2733 assert_string_equal(parsed->when->cond, "cond");
David Sedlák992fb7c2019-07-24 16:51:01 +02002734 assert_string_equal(parsed->augments->nodeid, "target");
David Sedlákd1144562019-08-06 12:36:14 +02002735 assert_string_equal(parsed->exts[0].name, "myext:c-define");
2736 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2737 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002738 lysp_node_free(st->ctx, siblings);
2739 siblings = NULL;
2740
2741 /* min subelems */
2742 data = ELEMENT_WRAPPER_START "<uses name=\"uses-name\"/>" ELEMENT_WRAPPER_END;
2743 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2744 assert_string_equal(siblings[0].name, "uses-name");
2745 lysp_node_free(st->ctx, siblings);
2746 siblings = NULL;
2747
2748 st->finished_correctly = true;
2749}
2750
David Sedlákaa854b02019-07-22 14:17:10 +02002751static void
2752test_revision_elem(void **state)
2753{
2754 struct state *st = *state;
2755 const char *data;
2756 struct lysp_revision *revs = NULL;
2757
2758 /* max subelems */
2759 data = ELEMENT_WRAPPER_START
2760 "<revision date=\"2018-12-25\">"
2761 "<description><text>desc</text></description>"
2762 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02002763 EXT_SUBELEM
David Sedlákaa854b02019-07-22 14:17:10 +02002764 "</revision>"
2765 ELEMENT_WRAPPER_END;
2766 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, true), LY_SUCCESS);
2767 assert_string_equal(revs->date, "2018-12-25");
2768 assert_string_equal(revs->dsc, "desc");
2769 assert_string_equal(revs->ref, "ref");
David Sedlákd1144562019-08-06 12:36:14 +02002770 assert_string_equal(revs->exts[0].name, "myext:c-define");
2771 assert_int_equal(revs->exts[0].insubstmt_index, 0);
2772 assert_int_equal(revs->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákaa854b02019-07-22 14:17:10 +02002773 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2774 revs = NULL;
2775
2776 /* min subelems */
2777 data = ELEMENT_WRAPPER_START "<revision date=\"2005-05-05\" />" ELEMENT_WRAPPER_END;
2778 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, true), LY_SUCCESS);
2779 assert_string_equal(revs->date, "2005-05-05");
2780 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2781 revs = NULL;
2782
2783 /* invalid value */
2784 data = ELEMENT_WRAPPER_START "<revision date=\"05-05-2005\" />" ELEMENT_WRAPPER_END;
2785 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, false), LY_EVALID);
2786 logbuf_assert("Invalid value \"05-05-2005\" of \"revision\". Line number 1.");
2787 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2788 revs = NULL;
2789
2790 st->finished_correctly = true;
2791}
2792
David Sedlák0c2bab92019-07-22 15:33:19 +02002793static void
2794test_include_elem(void **state)
2795{
2796 struct state *st = *state;
2797 const char *data;
2798 struct lysp_include *includes = NULL;
2799 struct include_meta inc_meta = {"module-name", &includes};
2800
2801 /* max subelems */
2802 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2803 data = ELEMENT_WRAPPER_START
2804 "<include module=\"mod\">"
2805 "<description><text>desc</text></description>"
2806 "<reference><text>ref</text></reference>"
2807 "<revision-date date=\"1999-09-09\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002808 EXT_SUBELEM
David Sedlák0c2bab92019-07-22 15:33:19 +02002809 "</include>"
2810 ELEMENT_WRAPPER_END;
2811 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, true), LY_SUCCESS);
2812 assert_string_equal(includes->name, "mod");
2813 assert_string_equal(includes->dsc, "desc");
2814 assert_string_equal(includes->ref, "ref");
David Sedlák0c2bab92019-07-22 15:33:19 +02002815 assert_string_equal(includes->rev, "1999-09-09");
David Sedlákd1144562019-08-06 12:36:14 +02002816 assert_string_equal(includes->exts[0].name, "myext:c-define");
2817 assert_int_equal(includes->exts[0].insubstmt_index, 0);
2818 assert_int_equal(includes->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák0c2bab92019-07-22 15:33:19 +02002819 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2820 includes = NULL;
2821
2822 /* min subelems */
2823 data = ELEMENT_WRAPPER_START "<include module=\"mod\"/>" ELEMENT_WRAPPER_END;
2824 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, true), LY_SUCCESS);
2825 assert_string_equal(includes->name, "mod");
2826 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2827 includes = NULL;
2828
2829 /* invalid combinations */
2830 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2831 data = ELEMENT_WRAPPER_START
2832 "<include module=\"mod\">"
2833 "<description><text>desc</text></description>"
2834 "<revision-date date=\"1999-09-09\"/>"
2835 "</include>"
2836 ELEMENT_WRAPPER_END;
2837 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, false), LY_EVALID);
2838 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.");
2839 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2840 includes = NULL;
2841
2842 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2843 data = ELEMENT_WRAPPER_START
2844 "<include module=\"mod\">"
2845 "<reference><text>ref</text></reference>"
2846 "<revision-date date=\"1999-09-09\"/>"
2847 "</include>"
2848 ELEMENT_WRAPPER_END;
2849 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, false), LY_EVALID);
2850 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.");
2851 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2852 includes = NULL;
2853
2854 st->finished_correctly = true;
2855}
2856
David Sedlák5e13dea2019-07-22 16:06:45 +02002857static void
2858test_feature_elem(void **state)
2859{
2860 struct state *st = *state;
2861 const char *data;
2862 struct lysp_feature *features = NULL;
2863
2864 /* max subelems */
2865 data = ELEMENT_WRAPPER_START
2866 "<feature name=\"feature-name\">"
2867 "<if-feature name=\"iff\"/>"
2868 "<status value=\"deprecated\"/>"
2869 "<description><text>desc</text></description>"
2870 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02002871 EXT_SUBELEM
David Sedlák5e13dea2019-07-22 16:06:45 +02002872 "</feature>"
2873 ELEMENT_WRAPPER_END;
2874 assert_int_equal(test_element_helper(st, &data, &features, NULL, NULL, true), LY_SUCCESS);
2875 assert_string_equal(features->name, "feature-name");
2876 assert_string_equal(features->dsc, "desc");
David Sedlák5e13dea2019-07-22 16:06:45 +02002877 assert_true(features->flags & LYS_STATUS_DEPRC);
2878 assert_string_equal(*features->iffeatures, "iff");
2879 assert_string_equal(features->ref, "ref");
David Sedlákd1144562019-08-06 12:36:14 +02002880 assert_string_equal(features->exts[0].name, "myext:c-define");
2881 assert_int_equal(features->exts[0].insubstmt_index, 0);
2882 assert_int_equal(features->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák5e13dea2019-07-22 16:06:45 +02002883 FREE_ARRAY(st->ctx, features, lysp_feature_free);
2884 features = NULL;
2885
2886 /* min subelems */
2887 data = ELEMENT_WRAPPER_START "<feature name=\"feature-name\"/>" ELEMENT_WRAPPER_END;
2888 assert_int_equal(test_element_helper(st, &data, &features, NULL, NULL, true), LY_SUCCESS);
2889 assert_string_equal(features->name, "feature-name");
2890 FREE_ARRAY(st->ctx, features, lysp_feature_free);
2891 features = NULL;
2892
2893 st->finished_correctly = true;
2894}
2895
David Sedlák28794f22019-07-22 16:45:00 +02002896static void
2897test_identity_elem(void **state)
2898{
2899 struct state *st = *state;
2900 const char *data;
2901 struct lysp_ident *identities = NULL;
2902
2903 /* max subelems */
2904 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2905 data = ELEMENT_WRAPPER_START
2906 "<identity name=\"ident-name\">"
2907 "<if-feature name=\"iff\"/>"
2908 "<base name=\"base-name\"/>"
2909 "<status value=\"deprecated\"/>"
2910 "<description><text>desc</text></description>"
2911 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02002912 EXT_SUBELEM
David Sedlák28794f22019-07-22 16:45:00 +02002913 "</identity>"
2914 ELEMENT_WRAPPER_END;
2915 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, true), LY_SUCCESS);
2916 assert_string_equal(identities->name, "ident-name");
2917 assert_string_equal(*identities->bases, "base-name");
2918 assert_string_equal(*identities->iffeatures, "iff");
2919 assert_string_equal(identities->dsc, "desc");
2920 assert_string_equal(identities->ref, "ref");
2921 assert_true(identities->flags & LYS_STATUS_DEPRC);
David Sedlákd1144562019-08-06 12:36:14 +02002922 assert_string_equal(identities->exts[0].name, "myext:c-define");
2923 assert_int_equal(identities->exts[0].insubstmt_index, 0);
2924 assert_int_equal(identities->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák28794f22019-07-22 16:45:00 +02002925 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2926 identities = NULL;
2927
2928 /* min subelems */
2929 data = ELEMENT_WRAPPER_START "<identity name=\"ident-name\" />" ELEMENT_WRAPPER_END;
2930 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, true), LY_SUCCESS);
2931 assert_string_equal(identities->name, "ident-name");
2932 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2933 identities = NULL;
2934
2935 /* invalid */
2936 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2937 data = ELEMENT_WRAPPER_START
2938 "<identity name=\"ident-name\">"
2939 "<if-feature name=\"iff\"/>"
2940 "</identity>"
2941 ELEMENT_WRAPPER_END;
2942 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, false), LY_EVALID);
2943 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.");
2944 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2945 identities = NULL;
2946
2947 st->finished_correctly = true;
2948}
2949
David Sedlákaf536aa2019-07-23 13:42:23 +02002950static void
2951test_list_elem(void **state)
2952{
2953 struct state *st = *state;
2954 const char *data;
2955 struct lysp_node *siblings = NULL;
2956 struct tree_node_meta node_meta = {NULL, &siblings};
2957 struct lysp_node_list *parsed = NULL;
2958
2959 /* max subelems */
2960 data = ELEMENT_WRAPPER_START
2961 "<list name=\"list-name\">"
2962 "<when condition=\"when\"/>"
2963 "<if-feature name=\"iff\"/>"
2964 "<must condition=\"must-cond\"/>"
2965 "<key value=\"key\"/>"
2966 "<unique tag=\"utag\"/>"
2967 "<config value=\"true\"/>"
2968 "<min-elements value=\"10\"/>"
2969 "<ordered-by value=\"user\"/>"
2970 "<status value=\"deprecated\"/>"
2971 "<description><text>desc</text></description>"
2972 "<reference><text>ref</text></reference>"
2973 "<anydata name=\"anyd\"/>"
2974 "<anyxml name=\"anyx\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02002975 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002976 "<choice name=\"choice\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02002977 "<action name=\"action\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02002978 "<grouping name=\"grp\"/>"
David Sedlák031b9e72019-07-23 15:19:37 +02002979 "<notification name=\"notf\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002980 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
2981 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlákaf536aa2019-07-23 13:42:23 +02002982 "<list name=\"sub-list\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002983 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákaf536aa2019-07-23 13:42:23 +02002984 "<uses name=\"uses-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002985 EXT_SUBELEM
David Sedlákaf536aa2019-07-23 13:42:23 +02002986 "</list>"
2987 ELEMENT_WRAPPER_END;
2988 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2989 parsed = (struct lysp_node_list *)&siblings[0];
2990 assert_string_equal(parsed->dsc, "desc");
2991 assert_string_equal(parsed->child->name, "anyd");
2992 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2993 assert_string_equal(parsed->child->next->name, "anyx");
2994 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
David Sedlákf111bcb2019-07-23 17:15:51 +02002995 assert_string_equal(parsed->child->next->next->name, "cont");
2996 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002997 assert_string_equal(parsed->child->next->next->next->name, "choice");
2998 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
David Sedlák85d0eca2019-07-24 15:15:21 +02002999 assert_string_equal(parsed->child->next->next->next->next->name, "leaf");
3000 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAF);
3001 assert_string_equal(parsed->child->next->next->next->next->next->name, "llist");
3002 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3003 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "sub-list");
3004 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LIST);
3005 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "uses-name");
3006 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_USES);
3007 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003008 assert_string_equal(parsed->groupings->name, "grp");
David Sedlák85d0eca2019-07-24 15:15:21 +02003009 assert_string_equal(parsed->actions->name, "action");
David Sedláke3ce9ef2019-07-23 16:34:30 +02003010 assert_int_equal(parsed->groupings->nodetype, LYS_GROUPING);
David Sedlák031b9e72019-07-23 15:19:37 +02003011 assert_string_equal(parsed->notifs->name, "notf");
David Sedlákaf536aa2019-07-23 13:42:23 +02003012 assert_true(parsed->flags & LYS_ORDBY_USER);
3013 assert_true(parsed->flags & LYS_STATUS_DEPRC);
3014 assert_true(parsed->flags & LYS_CONFIG_W);
3015 assert_string_equal(*parsed->iffeatures, "iff");
3016 assert_string_equal(parsed->key, "key");
3017 assert_int_equal(parsed->min, 10);
3018 assert_string_equal(parsed->musts->arg, "must-cond");
3019 assert_string_equal(parsed->name, "list-name");
3020 assert_null(parsed->next);
3021 assert_int_equal(parsed->nodetype, LYS_LIST);
3022 assert_null(parsed->parent);
3023 assert_string_equal(parsed->ref, "ref");
3024 assert_string_equal(parsed->typedefs->name, "tpdf");
3025 assert_string_equal(*parsed->uniques, "utag");
3026 assert_string_equal(parsed->when->cond, "when");
David Sedlákd1144562019-08-06 12:36:14 +02003027 assert_string_equal(parsed->exts[0].name, "myext:c-define");
3028 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3029 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákaf536aa2019-07-23 13:42:23 +02003030 lysp_node_free(st->ctx, siblings);
3031 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
3032 siblings = NULL;
3033
3034 /* min subelems */
3035 data = ELEMENT_WRAPPER_START "<list name=\"list-name\" />" ELEMENT_WRAPPER_END;
3036 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
3037 parsed = (struct lysp_node_list *)&siblings[0];
3038 assert_string_equal(parsed->name, "list-name");
3039 lysp_node_free(st->ctx, siblings);
3040 siblings = NULL;
3041
3042 st->finished_correctly = true;
3043}
3044
David Sedlák031b9e72019-07-23 15:19:37 +02003045static void
3046test_notification_elem(void **state)
3047{
3048 struct state *st = *state;
3049 const char *data;
3050 struct lysp_notif *notifs = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003051 struct tree_node_meta notif_meta = {NULL, (struct lysp_node **)&notifs};
David Sedlák031b9e72019-07-23 15:19:37 +02003052
3053 /* max subelems */
3054 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3055 data = ELEMENT_WRAPPER_START
3056 "<notification name=\"notif-name\">"
3057 "<anydata name=\"anyd\"/>"
3058 "<anyxml name=\"anyx\"/>"
3059 "<description><text>desc</text></description>"
3060 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003061 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3062 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák031b9e72019-07-23 15:19:37 +02003063 "<list name=\"sub-list\"/>"
3064 "<must condition=\"cond\"/>"
3065 "<reference><text>ref</text></reference>"
3066 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003067 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák031b9e72019-07-23 15:19:37 +02003068 "<uses name=\"uses-name\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02003069 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003070 "<choice name=\"choice\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02003071 "<grouping name=\"grp\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003072 EXT_SUBELEM
David Sedlák031b9e72019-07-23 15:19:37 +02003073 "</notification>"
3074 ELEMENT_WRAPPER_END;
3075 assert_int_equal(test_element_helper(st, &data, &notif_meta, NULL, NULL, true), LY_SUCCESS);
3076 assert_string_equal(notifs->name, "notif-name");
3077 assert_string_equal(notifs->data->name, "anyd");
3078 assert_int_equal(notifs->data->nodetype, LYS_ANYDATA);
3079 assert_string_equal(notifs->data->next->name, "anyx");
3080 assert_int_equal(notifs->data->next->nodetype, LYS_ANYXML);
3081 assert_string_equal(notifs->data->next->next->name, "leaf");
3082 assert_int_equal(notifs->data->next->next->nodetype, LYS_LEAF);
3083 assert_string_equal(notifs->data->next->next->next->name, "llist");
3084 assert_int_equal(notifs->data->next->next->next->nodetype, LYS_LEAFLIST);
3085 assert_string_equal(notifs->data->next->next->next->next->name, "sub-list");
3086 assert_int_equal(notifs->data->next->next->next->next->nodetype, LYS_LIST);
David Sedlák031b9e72019-07-23 15:19:37 +02003087 assert_true(notifs->flags & LYS_STATUS_DEPRC);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003088 assert_string_equal(notifs->groupings->name, "grp");
3089 assert_int_equal(notifs->groupings->nodetype, LYS_GROUPING);
David Sedlákf111bcb2019-07-23 17:15:51 +02003090 assert_string_equal(notifs->data->next->next->next->next->next->name, "uses-name");
3091 assert_int_equal(notifs->data->next->next->next->next->next->nodetype, LYS_USES);
3092 assert_string_equal(notifs->data->next->next->next->next->next->next->name, "cont");
3093 assert_int_equal(notifs->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003094 assert_int_equal(notifs->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
3095 assert_string_equal(notifs->data->next->next->next->next->next->next->next->name, "choice");
3096 assert_null(notifs->data->next->next->next->next->next->next->next->next);
David Sedlák031b9e72019-07-23 15:19:37 +02003097 assert_string_equal(*notifs->iffeatures, "iff");
3098 assert_string_equal(notifs->musts->arg, "cond");
3099 assert_int_equal(notifs->nodetype, LYS_NOTIF);
3100 assert_null(notifs->parent);
3101 assert_string_equal(notifs->ref, "ref");
3102 assert_string_equal(notifs->typedefs->name, "tpdf");
David Sedlákd1144562019-08-06 12:36:14 +02003103 assert_string_equal(notifs->exts[0].name, "myext:c-define");
3104 assert_int_equal(notifs->exts[0].insubstmt_index, 0);
3105 assert_int_equal(notifs->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák031b9e72019-07-23 15:19:37 +02003106 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
3107 notifs = NULL;
3108
3109 /* min subelems */
3110 data = ELEMENT_WRAPPER_START "<notification name=\"notif-name\" />" ELEMENT_WRAPPER_END;
3111 assert_int_equal(test_element_helper(st, &data, &notif_meta, NULL, NULL, true), LY_SUCCESS);
3112 assert_string_equal(notifs->name, "notif-name");
3113 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003114 notifs = NULL;
3115
3116 st->finished_correctly = true;
3117}
3118
3119static void
3120test_grouping_elem(void **state)
3121{
3122 struct state *st = *state;
3123 const char *data;
3124 struct lysp_grp *grps = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003125 struct tree_node_meta grp_meta = {NULL, (struct lysp_node **)&grps};
David Sedláke3ce9ef2019-07-23 16:34:30 +02003126
3127 /* max subelems */
3128 data = ELEMENT_WRAPPER_START
3129 "<grouping name=\"grp-name\">"
3130 "<anydata name=\"anyd\"/>"
3131 "<anyxml name=\"anyx\"/>"
3132 "<description><text>desc</text></description>"
3133 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003134 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3135 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02003136 "<list name=\"list\"/>"
3137 "<notification name=\"notf\"/>"
3138 "<reference><text>ref</text></reference>"
3139 "<status value=\"current\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003140 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02003141 "<uses name=\"uses-name\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02003142 "<action name=\"act\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02003143 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003144 "<choice name=\"choice\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003145 EXT_SUBELEM
David Sedláke3ce9ef2019-07-23 16:34:30 +02003146 "</grouping>"
3147 ELEMENT_WRAPPER_END;
3148 assert_int_equal(test_element_helper(st, &data, &grp_meta, NULL, NULL, true), LY_SUCCESS);
3149 assert_string_equal(grps->name, "grp-name");
David Sedláke3ce9ef2019-07-23 16:34:30 +02003150 assert_string_equal(grps->data->name, "anyd");
3151 assert_string_equal(grps->data->next->name, "anyx");
3152 assert_string_equal(grps->data->next->next->name, "leaf");
3153 assert_string_equal(grps->data->next->next->next->name, "llist");
3154 assert_string_equal(grps->data->next->next->next->next->name, "list");
3155 assert_string_equal(grps->dsc, "desc");
David Sedláke3ce9ef2019-07-23 16:34:30 +02003156 assert_true(grps->flags & LYS_STATUS_CURR);
3157 assert_string_equal(grps->groupings->name, "sub-grp");
3158 assert_int_equal(grps->nodetype, LYS_GROUPING);
3159 assert_string_equal(grps->notifs->name, "notf");
3160 assert_null(grps->parent);
3161 assert_string_equal(grps->ref, "ref");
3162 assert_string_equal(grps->typedefs->name, "tpdf");
David Sedlák85d0eca2019-07-24 15:15:21 +02003163 assert_string_equal(grps->actions->name, "act");
David Sedlákf111bcb2019-07-23 17:15:51 +02003164 assert_string_equal(grps->data->next->next->next->next->next->name, "uses-name");
David Sedlákb7abcfa2019-07-24 12:33:35 +02003165 assert_int_equal(grps->data->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákf111bcb2019-07-23 17:15:51 +02003166 assert_string_equal(grps->data->next->next->next->next->next->next->name, "cont");
David Sedlákb7abcfa2019-07-24 12:33:35 +02003167 assert_int_equal(grps->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
3168 assert_string_equal(grps->data->next->next->next->next->next->next->next->name, "choice");
3169 assert_int_equal(grps->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
David Sedlákd1144562019-08-06 12:36:14 +02003170 assert_string_equal(grps->exts[0].name, "myext:c-define");
3171 assert_int_equal(grps->exts[0].insubstmt_index, 0);
3172 assert_int_equal(grps->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003173 FREE_ARRAY(st->ctx, grps, lysp_grp_free);
3174 grps = NULL;
3175
3176 /* min subelems */
3177 data = ELEMENT_WRAPPER_START "<grouping name=\"grp-name\" />" ELEMENT_WRAPPER_END;
3178 assert_int_equal(test_element_helper(st, &data, &grp_meta, NULL, NULL, true), LY_SUCCESS);
3179 assert_string_equal(grps->name, "grp-name");
3180 FREE_ARRAY(st->ctx, grps, lysp_grp_free);
3181 grps = NULL;
David Sedlák031b9e72019-07-23 15:19:37 +02003182
3183 st->finished_correctly = true;
3184}
3185
David Sedlákf111bcb2019-07-23 17:15:51 +02003186static void
3187test_container_elem(void **state)
3188{
3189 struct state *st = *state;
3190 const char *data;
3191 struct lysp_node *siblings = NULL;
3192 struct tree_node_meta node_meta = {NULL, &siblings};
3193 struct lysp_node_container *parsed = NULL;
3194
3195 /* max subelems */
David Sedláke2dc9e92019-07-24 09:59:21 +02003196 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3197 data = ELEMENT_WRAPPER_START
3198 "<container name=\"cont-name\">"
3199 "<anydata name=\"anyd\"/>"
3200 "<anyxml name=\"anyx\"/>"
3201 "<config value=\"true\"/>"
3202 "<container name=\"subcont\"/>"
3203 "<description><text>desc</text></description>"
3204 "<grouping name=\"sub-grp\"/>"
3205 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003206 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3207 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedláke2dc9e92019-07-24 09:59:21 +02003208 "<list name=\"list\"/>"
3209 "<must condition=\"cond\"/>"
3210 "<notification name=\"notf\"/>"
3211 "<presence value=\"presence\"/>"
3212 "<reference><text>ref</text></reference>"
3213 "<status value=\"current\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003214 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedláke2dc9e92019-07-24 09:59:21 +02003215 "<uses name=\"uses-name\"/>"
3216 "<when condition=\"when-cond\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02003217 "<action name=\"act\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003218 "<choice name=\"choice\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003219 EXT_SUBELEM
David Sedláke2dc9e92019-07-24 09:59:21 +02003220 "</container>"
3221 ELEMENT_WRAPPER_END;
3222 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
3223 parsed = (struct lysp_node_container *)siblings;
3224 assert_string_equal(parsed->name, "cont-name");
3225 assert_null(parsed->parent);
3226 assert_int_equal(parsed->nodetype, LYS_CONTAINER);
3227 assert_true(parsed->flags & LYS_CONFIG_W);
3228 assert_true(parsed->flags & LYS_STATUS_CURR);
3229 assert_null(parsed->next);
3230 assert_string_equal(parsed->dsc, "desc");
3231 assert_string_equal(parsed->ref, "ref");
3232 assert_string_equal(parsed->when->cond, "when-cond");
3233 assert_string_equal(*parsed->iffeatures, "iff");
David Sedláke2dc9e92019-07-24 09:59:21 +02003234 assert_string_equal(parsed->musts->arg, "cond");
3235 assert_string_equal(parsed->presence, "presence");
3236 assert_string_equal(parsed->typedefs->name, "tpdf");
3237 assert_string_equal(parsed->groupings->name, "sub-grp");
3238 assert_string_equal(parsed->child->name, "anyd");
3239 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
3240 assert_string_equal(parsed->child->next->name, "anyx");
3241 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
3242 assert_string_equal(parsed->child->next->next->name, "subcont");
3243 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
3244 assert_string_equal(parsed->child->next->next->next->name, "leaf");
3245 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
3246 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
3247 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
3248 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
3249 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
3250 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
3251 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003252 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
3253 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
3254 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke2dc9e92019-07-24 09:59:21 +02003255 assert_string_equal(parsed->notifs->name, "notf");
David Sedlák85d0eca2019-07-24 15:15:21 +02003256 assert_string_equal(parsed->actions->name, "act");
David Sedlákd1144562019-08-06 12:36:14 +02003257 assert_string_equal(parsed->exts[0].name, "myext:c-define");
3258 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3259 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedláke2dc9e92019-07-24 09:59:21 +02003260 lysp_node_free(st->ctx, siblings);
3261 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
3262 siblings = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02003263
3264 /* min subelems */
3265 data = ELEMENT_WRAPPER_START "<container name=\"cont-name\" />" ELEMENT_WRAPPER_END;
3266 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
3267 parsed = (struct lysp_node_container *)siblings;
3268 assert_string_equal(parsed->name, "cont-name");
3269 lysp_node_free(st->ctx, siblings);
3270 siblings = NULL;
3271
3272 st->finished_correctly = true;
3273}
3274
David Sedlák5379d392019-07-24 10:42:03 +02003275static void
3276test_case_elem(void **state)
3277{
3278 struct state *st = *state;
3279 const char *data;
3280 struct lysp_node *siblings = NULL;
3281 struct tree_node_meta node_meta = {NULL, &siblings};
3282 struct lysp_node_case *parsed = NULL;
3283
3284 /* max subelems */
3285 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3286 data = ELEMENT_WRAPPER_START
3287 "<case name=\"case-name\">"
3288 "<anydata name=\"anyd\"/>"
3289 "<anyxml name=\"anyx\"/>"
3290 "<container name=\"subcont\"/>"
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ák5379d392019-07-24 10:42:03 +02003295 "<list name=\"list\"/>"
3296 "<reference><text>ref</text></reference>"
3297 "<status value=\"current\"/>"
3298 "<uses name=\"uses-name\"/>"
3299 "<when condition=\"when-cond\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003300 "<choice name=\"choice\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003301 EXT_SUBELEM
David Sedlák5379d392019-07-24 10:42:03 +02003302 "</case>"
3303 ELEMENT_WRAPPER_END;
3304 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
3305 parsed = (struct lysp_node_case *)siblings;
3306 assert_string_equal(parsed->name, "case-name");
3307 assert_null(parsed->parent);
3308 assert_int_equal(parsed->nodetype, LYS_CASE);
3309 assert_true(parsed->flags & LYS_STATUS_CURR);
3310 assert_null(parsed->next);
3311 assert_string_equal(parsed->dsc, "desc");
3312 assert_string_equal(parsed->ref, "ref");
3313 assert_string_equal(parsed->when->cond, "when-cond");
3314 assert_string_equal(*parsed->iffeatures, "iff");
David Sedlák5379d392019-07-24 10:42:03 +02003315 assert_string_equal(parsed->child->name, "anyd");
3316 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
3317 assert_string_equal(parsed->child->next->name, "anyx");
3318 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
3319 assert_string_equal(parsed->child->next->next->name, "subcont");
3320 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
3321 assert_string_equal(parsed->child->next->next->next->name, "leaf");
3322 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
3323 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
3324 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
3325 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
3326 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
3327 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
3328 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003329 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
3330 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
3331 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedlákd1144562019-08-06 12:36:14 +02003332 assert_string_equal(parsed->exts[0].name, "myext:c-define");
3333 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3334 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák5379d392019-07-24 10:42:03 +02003335 lysp_node_free(st->ctx, siblings);
3336 siblings = NULL;
3337
3338 /* min subelems */
3339 data = ELEMENT_WRAPPER_START "<case name=\"case-name\" />" ELEMENT_WRAPPER_END;
3340 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
3341 parsed = (struct lysp_node_case *)siblings;
3342 assert_string_equal(parsed->name, "case-name");
3343 lysp_node_free(st->ctx, siblings);
3344 siblings = NULL;
3345
3346 st->finished_correctly = true;
3347}
3348
David Sedlákb7abcfa2019-07-24 12:33:35 +02003349static void
3350test_choice_elem(void **state)
3351{
3352 struct state *st = *state;
3353 const char *data;
3354 struct lysp_node *siblings = NULL;
3355 struct tree_node_meta node_meta = {NULL, &siblings};
3356 struct lysp_node_choice *parsed = NULL;
3357
3358 /* max subelems */
3359 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3360 data = ELEMENT_WRAPPER_START
3361 "<choice name=\"choice-name\">"
3362 "<anydata name=\"anyd\"/>"
3363 "<anyxml name=\"anyx\"/>"
3364 "<case name=\"sub-case\"/>"
3365 "<choice name=\"choice\"/>"
3366 "<config value=\"true\"/>"
3367 "<container name=\"subcont\"/>"
3368 "<default value=\"def\"/>"
3369 "<description><text>desc</text></description>"
3370 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003371 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3372 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003373 "<list name=\"list\"/>"
3374 "<mandatory value=\"true\" />"
3375 "<reference><text>ref</text></reference>"
3376 "<status value=\"current\"/>"
3377 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003378 EXT_SUBELEM
David Sedlákb7abcfa2019-07-24 12:33:35 +02003379 "</choice>"
3380 ELEMENT_WRAPPER_END;
3381 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
3382 parsed = (struct lysp_node_choice *)siblings;
3383 assert_string_equal(parsed->name, "choice-name");
3384 assert_null(parsed->parent);
3385 assert_int_equal(parsed->nodetype, LYS_CHOICE);
3386 assert_true(parsed->flags & LYS_CONFIG_W && parsed->flags & LYS_MAND_TRUE && parsed->flags & LYS_STATUS_CURR);
3387 assert_null(parsed->next);
3388 assert_string_equal(parsed->dsc, "desc");
3389 assert_string_equal(parsed->ref, "ref");
3390 assert_string_equal(parsed->when->cond, "when-cond");
3391 assert_string_equal(*parsed->iffeatures, "iff");
David Sedlákb7abcfa2019-07-24 12:33:35 +02003392 assert_string_equal(parsed->child->name, "anyd");
3393 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
3394 assert_string_equal(parsed->child->next->name, "anyx");
3395 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
3396 assert_string_equal(parsed->child->next->next->name, "sub-case");
3397 assert_int_equal(parsed->child->next->next->nodetype, LYS_CASE);
3398 assert_string_equal(parsed->child->next->next->next->name, "choice");
3399 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
3400 assert_string_equal(parsed->child->next->next->next->next->name, "subcont");
3401 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_CONTAINER);
3402 assert_string_equal(parsed->child->next->next->next->next->next->name, "leaf");
3403 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAF);
3404 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "llist");
3405 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3406 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "list");
3407 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
3408 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedlákd1144562019-08-06 12:36:14 +02003409 assert_string_equal(parsed->exts[0].name, "myext:c-define");
3410 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3411 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003412 lysp_node_free(st->ctx, siblings);
3413 siblings = NULL;
3414
3415 /* min subelems */
3416 data = ELEMENT_WRAPPER_START "<choice name=\"choice-name\" />" ELEMENT_WRAPPER_END;
3417 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
3418 parsed = (struct lysp_node_choice *)siblings;
3419 assert_string_equal(parsed->name, "choice-name");
3420 lysp_node_free(st->ctx, siblings);
3421 siblings = NULL;
3422
3423 st->finished_correctly = true;
3424}
3425
David Sedlák05404f62019-07-24 14:11:53 +02003426static void
3427test_inout_elem(void **state)
3428{
3429 struct state *st = *state;
3430 const char *data;
3431 struct lysp_action_inout inout = {};
3432 struct inout_meta inout_meta = {NULL, &inout};
3433
3434 /* max subelements */
3435 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3436 data = ELEMENT_WRAPPER_START
3437 "<input>"
3438 "<anydata name=\"anyd\"/>"
3439 "<anyxml name=\"anyx\"/>"
3440 "<choice name=\"choice\"/>"
3441 "<container name=\"subcont\"/>"
3442 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003443 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3444 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák05404f62019-07-24 14:11:53 +02003445 "<list name=\"list\"/>"
3446 "<must condition=\"cond\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003447 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák05404f62019-07-24 14:11:53 +02003448 "<uses name=\"uses-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003449 EXT_SUBELEM
David Sedlák05404f62019-07-24 14:11:53 +02003450 "</input>"
3451 ELEMENT_WRAPPER_END;
3452 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3453 assert_null(inout.parent);
3454 assert_int_equal(inout.nodetype, LYS_INPUT);
3455 assert_string_equal(inout.musts->arg, "cond");
3456 assert_string_equal(inout.typedefs->name, "tpdf");
3457 assert_string_equal(inout.groupings->name, "sub-grp");
3458 assert_string_equal(inout.data->name, "anyd");
3459 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
3460 assert_string_equal(inout.data->next->name, "anyx");
3461 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
3462 assert_string_equal(inout.data->next->next->name, "choice");
3463 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
3464 assert_string_equal(inout.data->next->next->next->name, "subcont");
3465 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
3466 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
3467 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
3468 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
3469 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3470 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
3471 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
3472 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
3473 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3474 assert_null(inout.data->next->next->next->next->next->next->next->next);
David Sedlákd1144562019-08-06 12:36:14 +02003475 assert_string_equal(inout.exts[0].name, "myext:c-define");
3476 assert_int_equal(inout.exts[0].insubstmt_index, 0);
3477 assert_int_equal(inout.exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák05404f62019-07-24 14:11:53 +02003478 lysp_action_inout_free(st->ctx, &inout);
3479 memset(&inout, 0, sizeof inout);
3480
3481 /* max subelements */
3482 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3483 data = ELEMENT_WRAPPER_START
3484 "<output>"
3485 "<anydata name=\"anyd\"/>"
3486 "<anyxml name=\"anyx\"/>"
3487 "<choice name=\"choice\"/>"
3488 "<container name=\"subcont\"/>"
3489 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003490 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3491 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák05404f62019-07-24 14:11:53 +02003492 "<list name=\"list\"/>"
3493 "<must condition=\"cond\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003494 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák05404f62019-07-24 14:11:53 +02003495 "<uses name=\"uses-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003496 EXT_SUBELEM
David Sedlák05404f62019-07-24 14:11:53 +02003497 "</output>"
3498 ELEMENT_WRAPPER_END;
3499 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3500 assert_null(inout.parent);
3501 assert_int_equal(inout.nodetype, LYS_OUTPUT);
3502 assert_string_equal(inout.musts->arg, "cond");
3503 assert_string_equal(inout.typedefs->name, "tpdf");
3504 assert_string_equal(inout.groupings->name, "sub-grp");
3505 assert_string_equal(inout.data->name, "anyd");
3506 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
3507 assert_string_equal(inout.data->next->name, "anyx");
3508 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
3509 assert_string_equal(inout.data->next->next->name, "choice");
3510 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
3511 assert_string_equal(inout.data->next->next->next->name, "subcont");
3512 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
3513 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
3514 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
3515 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
3516 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3517 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
3518 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
3519 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
3520 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3521 assert_null(inout.data->next->next->next->next->next->next->next->next);
David Sedlákd1144562019-08-06 12:36:14 +02003522 assert_string_equal(inout.exts[0].name, "myext:c-define");
3523 assert_int_equal(inout.exts[0].insubstmt_index, 0);
3524 assert_int_equal(inout.exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák05404f62019-07-24 14:11:53 +02003525 lysp_action_inout_free(st->ctx, &inout);
3526 memset(&inout, 0, sizeof inout);
3527
3528 /* min subelems */
3529 data = ELEMENT_WRAPPER_START "<input />" ELEMENT_WRAPPER_END;
3530 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3531 memset(&inout, 0, sizeof inout);
3532
3533 data = ELEMENT_WRAPPER_START "<output />" ELEMENT_WRAPPER_END;
3534 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3535 memset(&inout, 0, sizeof inout);
3536
3537 /* invalid combinations */
3538 data = ELEMENT_WRAPPER_START "<input name=\"test\"/>" ELEMENT_WRAPPER_END;
3539 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02003540 logbuf_assert("Unexpected attribute \"name\" of \"input\" element. Line number 1.");
David Sedlák05404f62019-07-24 14:11:53 +02003541 memset(&inout, 0, sizeof inout);
3542
3543 st->finished_correctly = true;
3544}
3545
David Sedlák85d0eca2019-07-24 15:15:21 +02003546static void
3547test_action_elem(void **state)
3548{
3549 struct state *st = *state;
3550 const char *data;
3551 struct lysp_action *actions = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003552 struct tree_node_meta act_meta = {NULL, (struct lysp_node **)&actions};
David Sedlák85d0eca2019-07-24 15:15:21 +02003553
3554 /* max subelems */
3555 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3556 data = ELEMENT_WRAPPER_START
3557 "<action name=\"act\">"
3558 "<description><text>desc</text></description>"
3559 "<grouping name=\"grouping\"/>"
3560 "<if-feature name=\"iff\"/>"
3561 "<input><uses name=\"uses-name\"/></input>"
3562 "<output><must condition=\"cond\"/></output>"
3563 "<reference><text>ref</text></reference>"
3564 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003565 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákd1144562019-08-06 12:36:14 +02003566 EXT_SUBELEM
David Sedlák85d0eca2019-07-24 15:15:21 +02003567 "</action>"
3568 ELEMENT_WRAPPER_END;
3569 assert_int_equal(test_element_helper(st, &data, &act_meta, NULL, NULL, true), LY_SUCCESS);
3570 assert_null(actions->parent);
3571 assert_int_equal(actions->nodetype, LYS_ACTION);
3572 assert_true(actions->flags & LYS_STATUS_DEPRC);
3573 assert_string_equal(actions->name, "act");
3574 assert_string_equal(actions->dsc, "desc");
3575 assert_string_equal(actions->ref, "ref");
3576 assert_string_equal(*actions->iffeatures, "iff");
3577 assert_string_equal(actions->typedefs->name, "tpdf");
3578 assert_string_equal(actions->groupings->name, "grouping");
3579 assert_string_equal(actions->input.data->name, "uses-name");
3580 assert_string_equal(actions->output.musts->arg, "cond");
David Sedlákd1144562019-08-06 12:36:14 +02003581 assert_string_equal(actions->exts[0].name, "myext:c-define");
3582 assert_int_equal(actions->exts[0].insubstmt_index, 0);
3583 assert_int_equal(actions->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák85d0eca2019-07-24 15:15:21 +02003584 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3585 actions = NULL;
3586
David Sedlákeaa45792019-07-24 15:25:01 +02003587 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3588 data = ELEMENT_WRAPPER_START
3589 "<rpc name=\"act\">"
3590 "<description><text>desc</text></description>"
3591 "<grouping name=\"grouping\"/>"
3592 "<if-feature name=\"iff\"/>"
3593 "<input><uses name=\"uses-name\"/></input>"
3594 "<output><must condition=\"cond\"/></output>"
3595 "<reference><text>ref</text></reference>"
3596 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003597 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákd1144562019-08-06 12:36:14 +02003598 EXT_SUBELEM
David Sedlákeaa45792019-07-24 15:25:01 +02003599 "</rpc>"
3600 ELEMENT_WRAPPER_END;
3601 assert_int_equal(test_element_helper(st, &data, &act_meta, NULL, NULL, true), LY_SUCCESS);
3602 assert_null(actions->parent);
3603 assert_int_equal(actions->nodetype, LYS_ACTION);
3604 assert_true(actions->flags & LYS_STATUS_DEPRC);
3605 assert_string_equal(actions->name, "act");
3606 assert_string_equal(actions->dsc, "desc");
3607 assert_string_equal(actions->ref, "ref");
3608 assert_string_equal(*actions->iffeatures, "iff");
3609 assert_string_equal(actions->typedefs->name, "tpdf");
3610 assert_string_equal(actions->groupings->name, "grouping");
3611 assert_string_equal(actions->input.data->name, "uses-name");
3612 assert_string_equal(actions->output.musts->arg, "cond");
David Sedlákd1144562019-08-06 12:36:14 +02003613 assert_string_equal(actions->exts[0].name, "myext:c-define");
3614 assert_int_equal(actions->exts[0].insubstmt_index, 0);
3615 assert_int_equal(actions->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákeaa45792019-07-24 15:25:01 +02003616 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3617 actions = NULL;
3618
David Sedlák85d0eca2019-07-24 15:15:21 +02003619 /* min subelems */
3620 data = ELEMENT_WRAPPER_START "<action name=\"act\" />" ELEMENT_WRAPPER_END;
3621 assert_int_equal(test_element_helper(st, &data, &act_meta, NULL, NULL, true), LY_SUCCESS);
3622 assert_string_equal(actions->name, "act");
3623 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3624 actions = NULL;
3625
3626 st->finished_correctly = true;
3627}
3628
David Sedlák992fb7c2019-07-24 16:51:01 +02003629static void
3630test_augment_elem(void **state)
3631{
3632 struct state *st = *state;
3633 const char *data;
3634 struct lysp_augment *augments = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003635 struct tree_node_meta aug_meta = {NULL, (struct lysp_node **)&augments};
David Sedlák992fb7c2019-07-24 16:51:01 +02003636
3637 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3638 data = ELEMENT_WRAPPER_START
3639 "<augment target-node=\"target\">"
3640 "<action name=\"action\"/>"
3641 "<anydata name=\"anyd\"/>"
3642 "<anyxml name=\"anyx\"/>"
3643 "<case name=\"case\"/>"
3644 "<choice name=\"choice\"/>"
3645 "<container name=\"subcont\"/>"
3646 "<description><text>desc</text></description>"
3647 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003648 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3649 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák992fb7c2019-07-24 16:51:01 +02003650 "<list name=\"list\"/>"
3651 "<notification name=\"notif\"/>"
3652 "<reference><text>ref</text></reference>"
3653 "<status value=\"current\"/>"
3654 "<uses name=\"uses\"/>"
3655 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003656 EXT_SUBELEM
David Sedlák992fb7c2019-07-24 16:51:01 +02003657 "</augment>"
3658 ELEMENT_WRAPPER_END;
3659 assert_int_equal(test_element_helper(st, &data, &aug_meta, NULL, NULL, true), LY_SUCCESS);
3660 assert_string_equal(augments->nodeid, "target");
3661 assert_null(augments->parent);
3662 assert_int_equal(augments->nodetype, LYS_AUGMENT);
3663 assert_true(augments->flags & LYS_STATUS_CURR);
3664 assert_string_equal(augments->dsc, "desc");
3665 assert_string_equal(augments->ref, "ref");
3666 assert_string_equal(augments->when->cond, "when-cond");
3667 assert_string_equal(*augments->iffeatures, "iff");
3668 assert_string_equal(augments->child->name, "anyd");
3669 assert_int_equal(augments->child->nodetype, LYS_ANYDATA);
3670 assert_string_equal(augments->child->next->name, "anyx");
3671 assert_int_equal(augments->child->next->nodetype, LYS_ANYXML);
3672 assert_string_equal(augments->child->next->next->name, "case");
3673 assert_int_equal(augments->child->next->next->nodetype, LYS_CASE);
3674 assert_string_equal(augments->child->next->next->next->name, "choice");
3675 assert_int_equal(augments->child->next->next->next->nodetype, LYS_CHOICE);
3676 assert_string_equal(augments->child->next->next->next->next->name, "subcont");
3677 assert_int_equal(augments->child->next->next->next->next->nodetype, LYS_CONTAINER);
3678 assert_string_equal(augments->child->next->next->next->next->next->name, "leaf");
3679 assert_int_equal(augments->child->next->next->next->next->next->nodetype, LYS_LEAF);
3680 assert_string_equal(augments->child->next->next->next->next->next->next->name, "llist");
3681 assert_int_equal(augments->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3682 assert_string_equal(augments->child->next->next->next->next->next->next->next->name, "list");
3683 assert_int_equal(augments->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
3684 assert_string_equal(augments->child->next->next->next->next->next->next->next->next->name, "uses");
3685 assert_int_equal(augments->child->next->next->next->next->next->next->next->next->nodetype, LYS_USES);
3686 assert_null(augments->child->next->next->next->next->next->next->next->next->next);
3687 assert_string_equal(augments->actions->name, "action");
3688 assert_string_equal(augments->notifs->name, "notif");
David Sedlákd1144562019-08-06 12:36:14 +02003689 assert_string_equal(augments->exts[0].name, "myext:c-define");
3690 assert_int_equal(augments->exts[0].insubstmt_index, 0);
3691 assert_int_equal(augments->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák992fb7c2019-07-24 16:51:01 +02003692 FREE_ARRAY(st->ctx, augments, lysp_augment_free)
3693 augments = NULL;
3694
3695 data = ELEMENT_WRAPPER_START "<augment target-node=\"target\" />" ELEMENT_WRAPPER_END;
3696 assert_int_equal(test_element_helper(st, &data, &aug_meta, NULL, NULL, true), LY_SUCCESS);
3697 assert_string_equal(augments->nodeid, "target");
3698 FREE_ARRAY(st->ctx, augments, lysp_augment_free)
3699 augments = NULL;
3700
3701 st->finished_correctly = true;
3702}
3703
David Sedlák4ffcec82019-07-25 15:10:21 +02003704static void
3705test_deviate_elem(void **state)
3706{
3707 struct state *st = *state;
3708 const char *data;
3709 struct lysp_deviate *deviates = NULL;
3710 struct lysp_deviate_add *d_add;
3711 struct lysp_deviate_rpl *d_rpl;
3712 struct lysp_deviate_del *d_del;
3713
3714 /* all valid arguments with min subelems */
3715 data = ELEMENT_WRAPPER_START "<deviate value=\"not-supported\" />" ELEMENT_WRAPPER_END;
3716 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3717 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
3718 lysp_deviate_free(st->ctx, deviates);
3719 free(deviates);
3720 deviates = NULL;
3721
3722 data = ELEMENT_WRAPPER_START "<deviate value=\"add\" />" ELEMENT_WRAPPER_END;
3723 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3724 assert_int_equal(deviates->mod, LYS_DEV_ADD);
3725 lysp_deviate_free(st->ctx, deviates);
3726 free(deviates);
3727 deviates = NULL;
3728
3729 data = ELEMENT_WRAPPER_START "<deviate value=\"replace\" />" ELEMENT_WRAPPER_END;
3730 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3731 assert_int_equal(deviates->mod, LYS_DEV_REPLACE);
3732 lysp_deviate_free(st->ctx, deviates);
3733 free(deviates);
3734 deviates = NULL;
3735
3736 data = ELEMENT_WRAPPER_START "<deviate value=\"delete\" />" ELEMENT_WRAPPER_END;
3737 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3738 assert_int_equal(deviates->mod, LYS_DEV_DELETE);
3739 lysp_deviate_free(st->ctx, deviates);
3740 free(deviates);
3741 deviates = NULL;
3742
3743 /* max subelems and valid arguments */
3744 data = ELEMENT_WRAPPER_START
3745 "<deviate value=\"not-supported\">"
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 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
David Sedlákd1144562019-08-06 12:36:14 +02003751 assert_string_equal(deviates->exts[0].name, "myext:c-define");
3752 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3753 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003754 lysp_deviate_free(st->ctx, deviates);
3755 free(deviates);
3756 deviates = NULL;
3757
3758 data = ELEMENT_WRAPPER_START
3759 "<deviate value=\"add\">"
3760 "<units name=\"units\"/>"
3761 "<must condition=\"cond\"/>"
3762 "<unique tag=\"utag\"/>"
3763 "<default value=\"def\"/>"
3764 "<config value=\"true\"/>"
3765 "<mandatory value=\"true\"/>"
3766 "<min-elements value=\"5\"/>"
3767 "<max-elements value=\"15\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003768 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003769 "</deviate>"
3770 ELEMENT_WRAPPER_END;
3771 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3772 d_add = (struct lysp_deviate_add *)deviates;
3773 assert_int_equal(d_add->mod, LYS_DEV_ADD);
3774 assert_null(d_add->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003775 assert_string_equal(d_add->units, "units");
3776 assert_string_equal(d_add->musts->arg, "cond");
3777 assert_string_equal(*d_add->uniques, "utag");
3778 assert_string_equal(*d_add->dflts, "def");
3779 assert_true(d_add->flags & LYS_MAND_TRUE && d_add->flags & LYS_CONFIG_W);
3780 assert_int_equal(d_add->min, 5);
3781 assert_int_equal(d_add->max, 15);
David Sedlákd1144562019-08-06 12:36:14 +02003782 assert_string_equal(deviates->exts[0].name, "myext:c-define");
3783 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3784 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003785 lysp_deviate_free(st->ctx, deviates);
3786 free(deviates);
3787 deviates = NULL;
3788
3789 data = ELEMENT_WRAPPER_START
3790 "<deviate value=\"replace\">"
3791 "<type name=\"newtype\"/>"
3792 "<units name=\"uni\"/>"
3793 "<default value=\"def\"/>"
3794 "<config value=\"true\"/>"
3795 "<mandatory value=\"true\"/>"
3796 "<min-elements value=\"5\"/>"
3797 "<max-elements value=\"15\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003798 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003799 "</deviate>"
3800 ELEMENT_WRAPPER_END;
3801 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3802 d_rpl = (struct lysp_deviate_rpl *)deviates;
3803 assert_int_equal(d_rpl->mod, LYS_DEV_REPLACE);
3804 assert_null(d_rpl->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003805 assert_string_equal(d_rpl->type->name, "newtype");
3806 assert_string_equal(d_rpl->units, "uni");
3807 assert_string_equal(d_rpl->dflt, "def");
3808 assert_true(d_rpl->flags & LYS_MAND_TRUE && d_rpl->flags & LYS_CONFIG_W);
3809 assert_int_equal(d_rpl->min, 5);
3810 assert_int_equal(d_rpl->max, 15);
David Sedlákd1144562019-08-06 12:36:14 +02003811 assert_string_equal(deviates->exts[0].name, "myext:c-define");
3812 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3813 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003814 lysp_deviate_free(st->ctx, deviates);
3815 free(deviates);
3816 deviates = NULL;
3817
3818 data = ELEMENT_WRAPPER_START
3819 "<deviate value=\"delete\">"
3820 "<units name=\"u\"/>"
3821 "<must condition=\"c\"/>"
3822 "<unique tag=\"tag\"/>"
3823 "<default value=\"default\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003824 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003825 "</deviate>"
3826 ELEMENT_WRAPPER_END;
3827 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3828 d_del = (struct lysp_deviate_del *)deviates;
3829 assert_int_equal(d_del->mod, LYS_DEV_DELETE);
3830 assert_null(d_del->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003831 assert_string_equal(d_del->units, "u");
3832 assert_string_equal(d_del->musts->arg, "c");
3833 assert_string_equal(*d_del->uniques, "tag");
3834 assert_string_equal(*d_del->dflts, "default");
David Sedlákd1144562019-08-06 12:36:14 +02003835 assert_string_equal(deviates->exts[0].name, "myext:c-define");
3836 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3837 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003838 lysp_deviate_free(st->ctx, deviates);
3839 free(deviates);
3840 deviates = NULL;
3841
3842 /* invalid arguments */
3843 data = ELEMENT_WRAPPER_START "<deviate value=\"\" />" ELEMENT_WRAPPER_END;
3844 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003845 logbuf_assert("Invalid value \"\" of \"value\" attribute in \"deviate\" element. Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\". Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003846 deviates = NULL;
3847
3848 data = ELEMENT_WRAPPER_START "<deviate value=\"invalid\" />" ELEMENT_WRAPPER_END;
3849 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003850 logbuf_assert("Invalid value \"invalid\" of \"value\" attribute in \"deviate\" element. Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\". Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003851 deviates = NULL;
3852
3853 data = ELEMENT_WRAPPER_START "<deviate value=\"ad\" />" ELEMENT_WRAPPER_END;
3854 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003855 logbuf_assert("Invalid value \"ad\" of \"value\" attribute in \"deviate\" element. Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\". Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003856 deviates = NULL;
3857
3858 data = ELEMENT_WRAPPER_START "<deviate value=\"adds\" />" ELEMENT_WRAPPER_END;
3859 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003860 logbuf_assert("Invalid value \"adds\" of \"value\" attribute in \"deviate\" element. Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\". Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003861 deviates = NULL;
3862
3863 data = ELEMENT_WRAPPER_START
3864 "<deviate value=\"not-supported\">"
3865 "<must condition=\"c\"/>"
3866 "</deviate>"
3867 ELEMENT_WRAPPER_END;
3868 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
3869 logbuf_assert("Deviate of this type doesn't allow \"must\" as it's sub-element. Line number 1.");
3870
3871 st->finished_correctly = true;
3872}
3873
David Sedlák8b754462019-07-25 16:22:13 +02003874static void
3875test_deviation_elem(void **state)
3876{
3877 struct state *st = *state;
3878 const char *data;
3879 struct lysp_deviation *deviations = NULL;
3880
3881 /* min subelems */
3882 data = ELEMENT_WRAPPER_START
3883 "<deviation target-node=\"target\">"
3884 "<deviate value=\"not-supported\"/>"
3885 "</deviation>"
3886 ELEMENT_WRAPPER_END;
3887 assert_int_equal(test_element_helper(st, &data, &deviations, NULL, NULL, true), LY_SUCCESS);
3888 assert_string_equal(deviations->nodeid, "target");
3889 assert_int_equal(deviations->deviates->mod, LYS_DEV_NOT_SUPPORTED);
3890 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3891 deviations = NULL;
3892
3893 /* max subelems */
3894 data = ELEMENT_WRAPPER_START
3895 "<deviation target-node=\"target\">"
3896 "<reference><text>ref</text></reference>"
3897 "<description><text>desc</text></description>"
3898 "<deviate value=\"add\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003899 EXT_SUBELEM
David Sedlák8b754462019-07-25 16:22:13 +02003900 "</deviation>"
3901 ELEMENT_WRAPPER_END;
3902 assert_int_equal(test_element_helper(st, &data, &deviations, NULL, NULL, true), LY_SUCCESS);
3903 assert_string_equal(deviations->nodeid, "target");
3904 assert_int_equal(deviations->deviates->mod, LYS_DEV_ADD);
3905 assert_string_equal(deviations->ref, "ref");
3906 assert_string_equal(deviations->dsc, "desc");
David Sedlákd1144562019-08-06 12:36:14 +02003907 assert_string_equal(deviations->exts[0].name, "myext:c-define");
3908 assert_int_equal(deviations->exts[0].insubstmt_index, 0);
3909 assert_int_equal(deviations->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8b754462019-07-25 16:22:13 +02003910 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3911 deviations = NULL;
3912
3913 /* invalid */
3914 data = ELEMENT_WRAPPER_START "<deviation target-node=\"target\"/>" ELEMENT_WRAPPER_END;
3915 assert_int_equal(test_element_helper(st, &data, &deviations, NULL, NULL, false), LY_EVALID);
3916 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3917 deviations = NULL;
David Sedlák1538a842019-08-08 15:38:51 +02003918 logbuf_assert("Missing mandatory sub-element \"deviate\" of \"deviation\" element. Line number 1.");
3919 /* TODO */
David Sedlák8b754462019-07-25 16:22:13 +02003920 st->finished_correctly = true;
3921}
3922
David Sedlák4f03b932019-07-26 13:01:47 +02003923static void
3924test_module_elem(void **state)
3925{
3926 struct state *st = *state;
David Sedlákc5b20842019-08-13 10:18:31 +02003927 const char *data, *name, *prefix;
3928 size_t name_len, prefix_len;
David Sedlák4f03b932019-07-26 13:01:47 +02003929 struct yin_arg_record *attrs = NULL;
David Sedlák4f03b932019-07-26 13:01:47 +02003930 struct lys_module *lys_mod = NULL;
3931 struct lysp_module *lysp_mod = NULL;
3932
3933 /* max subelems */
3934 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
3935 lys_mod = calloc(1, sizeof *lys_mod);
3936 lysp_mod = calloc(1, sizeof *lysp_mod);
3937 lys_mod->ctx = st->ctx;
3938 lysp_mod->mod = lys_mod;
3939 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
3940 "<yang-version value=\"1.1\"/>\n"
3941 "<namespace uri=\"ns\"/>\n"
3942 "<prefix value=\"pref\"/>\n"
3943 "<include module=\"b-mod\"/>\n"
3944 "<import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
3945 "<organization><text>org</text></organization>\n"
3946 "<contact><text>contact</text></contact>\n"
3947 "<description><text>desc</text></description>"
3948 "<reference><text>ref</text></reference>\n"
3949 "<revision date=\"2019-02-02\"/>\n"
3950 "<anydata name=\"anyd\"/>\n"
3951 "<anyxml name=\"anyx\"/>\n"
3952 "<choice name=\"choice\"/>\n"
3953 "<container name=\"cont\"/>\n"
3954 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3955 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3956 "<list name=\"sub-list\"/>\n"
3957 "<uses name=\"uses-name\"/>\n"
3958 "<augment target-node=\"target\"/>\n"
3959 "<deviation target-node=\"target\">""<deviate value=\"not-supported\"/>""</deviation>\n"
3960 "<extension name=\"ext\"/>\n"
3961 "<feature name=\"feature\"/>\n"
3962 "<grouping name=\"grp\"/>\n"
3963 "<identity name=\"ident-name\"/>\n"
3964 "<notification name=\"notf\"/>\n"
3965 "<rpc name=\"rpc-name\"/>\n"
3966 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
David Sedlákd1144562019-08-06 12:36:14 +02003967 EXT_SUBELEM"\n"
David Sedlák4f03b932019-07-26 13:01:47 +02003968 "</module>\n";
David Sedlákc5b20842019-08-13 10:18:31 +02003969 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 +02003970 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
3971 assert_int_equal(yin_parse_mod(st->yin_ctx, attrs, &data, lysp_mod), LY_SUCCESS);
3972 assert_string_equal(lysp_mod->mod->name, "mod");
3973 assert_string_equal(lysp_mod->revs, "2019-02-02");
3974 assert_string_equal(lysp_mod->mod->ns, "ns");
3975 assert_string_equal(lysp_mod->mod->prefix, "pref");
3976 assert_null(lysp_mod->mod->filepath);
3977 assert_string_equal(lysp_mod->mod->org, "org");
3978 assert_string_equal(lysp_mod->mod->contact, "contact");
3979 assert_string_equal(lysp_mod->mod->dsc, "desc");
3980 assert_string_equal(lysp_mod->mod->ref, "ref");
3981 assert_int_equal(lysp_mod->mod->version, LYS_VERSION_1_1);
3982 assert_string_equal(lysp_mod->imports->name, "a-mod");
3983 assert_string_equal(lysp_mod->includes->name, "b-mod");
3984 assert_string_equal(lysp_mod->extensions->name, "ext");
3985 assert_string_equal(lysp_mod->features->name, "feature");
3986 assert_string_equal(lysp_mod->identities->name, "ident-name");
3987 assert_string_equal(lysp_mod->typedefs->name, "tpdf");
3988 assert_string_equal(lysp_mod->groupings->name, "grp");
3989 assert_string_equal(lysp_mod->data->name, "anyd");
3990 assert_int_equal(lysp_mod->data->nodetype, LYS_ANYDATA);
3991 assert_string_equal(lysp_mod->data->next->name, "anyx");
3992 assert_int_equal(lysp_mod->data->next->nodetype, LYS_ANYXML);
3993 assert_string_equal(lysp_mod->data->next->next->name, "choice");
3994 assert_int_equal(lysp_mod->data->next->next->nodetype, LYS_CHOICE);
3995 assert_string_equal(lysp_mod->data->next->next->next->name, "cont");
3996 assert_int_equal(lysp_mod->data->next->next->next->nodetype, LYS_CONTAINER);
3997 assert_string_equal(lysp_mod->data->next->next->next->next->name, "leaf");
3998 assert_int_equal(lysp_mod->data->next->next->next->next->nodetype, LYS_LEAF);
3999 assert_string_equal(lysp_mod->data->next->next->next->next->next->name, "llist");
4000 assert_int_equal(lysp_mod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
4001 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->name, "sub-list");
4002 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
4003 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->next->name, "uses-name");
4004 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
4005 assert_null(lysp_mod->data->next->next->next->next->next->next->next->next);
4006 assert_string_equal(lysp_mod->augments->nodeid, "target");
4007 assert_string_equal(lysp_mod->rpcs->name, "rpc-name");
4008 assert_string_equal(lysp_mod->notifs->name, "notf");
4009 assert_string_equal(lysp_mod->deviations->nodeid, "target");
David Sedlákd1144562019-08-06 12:36:14 +02004010 assert_string_equal(lysp_mod->exts[0].name, "myext:c-define");
4011 assert_int_equal(lysp_mod->exts[0].insubstmt_index, 0);
4012 assert_int_equal(lysp_mod->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4f03b932019-07-26 13:01:47 +02004013 lysp_module_free(lysp_mod);
4014 lys_module_free(lys_mod, NULL);
4015 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
4016 attrs = NULL;
4017
4018 /* min subelems */
4019 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
4020 lys_mod = calloc(1, sizeof *lys_mod);
4021 lysp_mod = calloc(1, sizeof *lysp_mod);
4022 lys_mod->ctx = st->ctx;
4023 lysp_mod->mod = lys_mod;
4024 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">"
4025 "<namespace uri=\"ns\"/>"
4026 "<prefix value=\"pref\"/>"
4027 "<yang-version value=\"1.1\"/>"
4028 "</module>";
David Sedlákc5b20842019-08-13 10:18:31 +02004029 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 +02004030 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
4031 assert_int_equal(yin_parse_mod(st->yin_ctx, attrs, &data, lysp_mod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02004032 assert_string_equal(lysp_mod->mod->name, "mod");
David Sedlák4f03b932019-07-26 13:01:47 +02004033 lysp_module_free(lysp_mod);
4034 lys_module_free(lys_mod, NULL);
4035 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
4036 attrs = NULL;
David Sedlák298ff6d2019-07-26 14:29:03 +02004037
David Sedláke6cd89e2019-08-07 12:46:02 +02004038 /* incorrect subelem order */
4039 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
4040 lys_mod = calloc(1, sizeof *lys_mod);
4041 lysp_mod = calloc(1, sizeof *lysp_mod);
4042 lys_mod->ctx = st->ctx;
4043 lysp_mod->mod = lys_mod;
4044 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">"
4045 "<feature name=\"feature\"/>\n"
4046 "<namespace uri=\"ns\"/>"
4047 "<prefix value=\"pref\"/>"
4048 "<yang-version value=\"1.1\"/>"
4049 "</module>";
David Sedlákc5b20842019-08-13 10:18:31 +02004050 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 +02004051 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
4052 assert_int_equal(yin_parse_mod(st->yin_ctx, attrs, &data, lysp_mod), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02004053 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 +02004054 lysp_module_free(lysp_mod);
4055 lys_module_free(lys_mod, NULL);
4056 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
4057 attrs = NULL;
4058
David Sedlák298ff6d2019-07-26 14:29:03 +02004059 st->finished_correctly = true;
4060}
4061
4062static void
4063test_submodule_elem(void **state)
4064{
4065 struct state *st = *state;
David Sedlákc5b20842019-08-13 10:18:31 +02004066 const char *data, *name, *prefix;
4067 size_t name_len, prefix_len;
David Sedlák298ff6d2019-07-26 14:29:03 +02004068 struct yin_arg_record *attrs = NULL;
David Sedlák298ff6d2019-07-26 14:29:03 +02004069 struct lysp_submodule *lysp_submod = NULL;
4070
4071 /* max subelements */
4072 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
4073 lysp_submod = calloc(1, sizeof *lysp_submod);
4074 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
4075 "<yang-version value=\"1.1\"/>\n"
4076 "<belongs-to module=\"mod-name\"><prefix value=\"pref\"/></belongs-to>"
4077 "<include module=\"b-mod\"/>\n"
4078 "<import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
4079 "<organization><text>org</text></organization>\n"
4080 "<contact><text>contact</text></contact>\n"
4081 "<description><text>desc</text></description>"
4082 "<reference><text>ref</text></reference>\n"
4083 "<revision date=\"2019-02-02\"/>\n"
4084 "<anydata name=\"anyd\"/>\n"
4085 "<anyxml name=\"anyx\"/>\n"
4086 "<choice name=\"choice\"/>\n"
4087 "<container name=\"cont\"/>\n"
4088 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
4089 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
4090 "<list name=\"sub-list\"/>\n"
4091 "<uses name=\"uses-name\"/>\n"
4092 "<augment target-node=\"target\"/>\n"
4093 "<deviation target-node=\"target\">""<deviate value=\"not-supported\"/>""</deviation>\n"
4094 "<extension name=\"ext\"/>\n"
4095 "<feature name=\"feature\"/>\n"
4096 "<grouping name=\"grp\"/>\n"
4097 "<identity name=\"ident-name\"/>\n"
4098 "<notification name=\"notf\"/>\n"
4099 "<rpc name=\"rpc-name\"/>\n"
4100 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
David Sedlákd1144562019-08-06 12:36:14 +02004101 EXT_SUBELEM"\n"
David Sedlák298ff6d2019-07-26 14:29:03 +02004102 "</submodule>\n";
David Sedlákc5b20842019-08-13 10:18:31 +02004103 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 +02004104 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
4105 assert_int_equal(yin_parse_submod(st->yin_ctx, attrs, &data, lysp_submod), LY_SUCCESS);
4106
4107 assert_string_equal(lysp_submod->name, "mod");
4108 assert_string_equal(lysp_submod->revs, "2019-02-02");
David Sedlák298ff6d2019-07-26 14:29:03 +02004109 assert_string_equal(lysp_submod->prefix, "pref");
4110 assert_null(lysp_submod->filepath);
4111 assert_string_equal(lysp_submod->org, "org");
4112 assert_string_equal(lysp_submod->contact, "contact");
4113 assert_string_equal(lysp_submod->dsc, "desc");
4114 assert_string_equal(lysp_submod->ref, "ref");
4115 assert_int_equal(lysp_submod->version, LYS_VERSION_1_1);
4116 assert_string_equal(lysp_submod->imports->name, "a-mod");
4117 assert_string_equal(lysp_submod->includes->name, "b-mod");
4118 assert_string_equal(lysp_submod->extensions->name, "ext");
4119 assert_string_equal(lysp_submod->features->name, "feature");
4120 assert_string_equal(lysp_submod->identities->name, "ident-name");
4121 assert_string_equal(lysp_submod->typedefs->name, "tpdf");
4122 assert_string_equal(lysp_submod->groupings->name, "grp");
4123 assert_string_equal(lysp_submod->data->name, "anyd");
4124 assert_int_equal(lysp_submod->data->nodetype, LYS_ANYDATA);
4125 assert_string_equal(lysp_submod->data->next->name, "anyx");
4126 assert_int_equal(lysp_submod->data->next->nodetype, LYS_ANYXML);
4127 assert_string_equal(lysp_submod->data->next->next->name, "choice");
4128 assert_int_equal(lysp_submod->data->next->next->nodetype, LYS_CHOICE);
4129 assert_string_equal(lysp_submod->data->next->next->next->name, "cont");
4130 assert_int_equal(lysp_submod->data->next->next->next->nodetype, LYS_CONTAINER);
4131 assert_string_equal(lysp_submod->data->next->next->next->next->name, "leaf");
4132 assert_int_equal(lysp_submod->data->next->next->next->next->nodetype, LYS_LEAF);
4133 assert_string_equal(lysp_submod->data->next->next->next->next->next->name, "llist");
4134 assert_int_equal(lysp_submod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
4135 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->name, "sub-list");
4136 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
4137 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->next->name, "uses-name");
4138 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
4139 assert_null(lysp_submod->data->next->next->next->next->next->next->next->next);
4140 assert_string_equal(lysp_submod->augments->nodeid, "target");
4141 assert_string_equal(lysp_submod->rpcs->name, "rpc-name");
4142 assert_string_equal(lysp_submod->notifs->name, "notf");
4143 assert_string_equal(lysp_submod->deviations->nodeid, "target");
David Sedlákd1144562019-08-06 12:36:14 +02004144 assert_string_equal(lysp_submod->exts[0].name, "myext:c-define");
4145 assert_int_equal(lysp_submod->exts[0].insubstmt_index, 0);
4146 assert_int_equal(lysp_submod->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák298ff6d2019-07-26 14:29:03 +02004147
4148 lysp_submodule_free(st->ctx, lysp_submod);
4149 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
4150 attrs = NULL;
4151
4152 /* min subelemnts */
4153 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
4154 lysp_submod = calloc(1, sizeof *lysp_submod);
4155 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">"
4156 "<yang-version value=\"1.0\"/>"
4157 "<belongs-to module=\"mod-name\"><prefix value=\"pref\"/></belongs-to>"
4158 "</submodule>";
David Sedlákc5b20842019-08-13 10:18:31 +02004159 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 +02004160 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
4161 assert_int_equal(yin_parse_submod(st->yin_ctx, attrs, &data, lysp_submod), LY_SUCCESS);
4162 assert_string_equal(lysp_submod->prefix, "pref");
4163 assert_string_equal(lysp_submod->belongsto, "mod-name");
4164 assert_int_equal(lysp_submod->version, LYS_VERSION_1_0);
4165 lysp_submodule_free(st->ctx, lysp_submod);
4166 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
4167 attrs = NULL;
4168
David Sedláke6cd89e2019-08-07 12:46:02 +02004169 /* incorrect subelem order */
4170 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
4171 lysp_submod = calloc(1, sizeof *lysp_submod);
4172 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">"
4173 "<yang-version value=\"1.0\"/>"
4174 "<reference><text>ref</text></reference>\n"
4175 "<belongs-to module=\"mod-name\"><prefix value=\"pref\"/></belongs-to>"
4176 "</submodule>";
David Sedlákc5b20842019-08-13 10:18:31 +02004177 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 +02004178 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
4179 assert_int_equal(yin_parse_submod(st->yin_ctx, attrs, &data, lysp_submod), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02004180 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 +02004181 lysp_submodule_free(st->ctx, lysp_submod);
4182 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
4183 attrs = NULL;
4184
David Sedlák298ff6d2019-07-26 14:29:03 +02004185 st->finished_correctly = true;
David Sedlák4f03b932019-07-26 13:01:47 +02004186}
4187
David Sedlák8985a142019-07-31 16:43:06 +02004188static void
4189test_yin_parse_module(void **state)
4190{
4191 struct state *st = *state;
4192 const char *data;
4193 struct lys_module *mod;
4194 struct yin_parser_ctx *yin_ctx = NULL;
4195
4196 mod = calloc(1, sizeof *mod);
4197 mod->ctx = st->ctx;
4198 data = "<module name=\"example-foo\""
4199 "xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
4200 "xmlns:foo=\"urn:example:foo\""
4201 "xmlns:myext=\"urn:example:extensions\">\n"
4202
4203 "<yang-version value=\"1.0\"/>\n"
4204
4205 "<namespace uri=\"urn:example:foo\"/>\n"
4206 "<prefix value=\"foo\"/>\n"
4207
4208 "<import module=\"example-extensions\">\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004209 "<prefix value=\"myext\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02004210 "</import>\n"
4211
4212 "<list name=\"interface\">\n"
4213 "<key value=\"name\"/>\n"
4214 "<leaf name=\"name\">\n"
4215 "<type name=\"string\"/>\n"
4216 "</leaf>\n"
4217 "<leaf name=\"mtu\">\n"
4218 "<type name=\"uint32\"/>\n"
4219 "<description>\n"
4220 "<text>The MTU of the interface.</text>\n"
4221 "</description>\n"
4222 "<myext:c-define name=\"MY_MTU\"/>\n"
4223 "</leaf>\n"
4224 "</list>\n"
4225 "</module>\n";
4226 assert_int_equal(yin_parse_module(&yin_ctx, data, mod), LY_SUCCESS);
4227 lys_module_free(mod, NULL);
4228 yin_parser_ctx_free(yin_ctx);
4229 mod = NULL;
4230 yin_ctx = NULL;
4231
4232 mod = calloc(1, sizeof *mod);
4233 mod->ctx = st->ctx;
David Sedlák6d781b62019-08-02 15:22:52 +02004234 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
4235 "<yang-version value=\"1.0\"/>\n"
4236 "<namespace uri=\"urn:example:foo\"/>\n"
4237 "<prefix value=\"foo\"/>\n"
4238 "</module>\n";
4239 assert_int_equal(yin_parse_module(&yin_ctx, data, mod), LY_SUCCESS);
4240 lys_module_free(mod, NULL);
4241 yin_parser_ctx_free(yin_ctx);
4242 mod = NULL;
4243 yin_ctx = NULL;
4244
4245
4246 mod = calloc(1, sizeof *mod);
4247 mod->ctx = st->ctx;
David Sedlák8985a142019-07-31 16:43:06 +02004248 data = "<submodule name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
4249 "</submodule>\n";
4250 assert_int_equal(yin_parse_module(&yin_ctx, data, mod), LY_EINVAL);
4251 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
4252 lys_module_free(mod, NULL);
4253 yin_parser_ctx_free(yin_ctx);
4254
David Sedlák6d781b62019-08-02 15:22:52 +02004255 mod = calloc(1, sizeof *mod);
4256 mod->ctx = st->ctx;
4257 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
4258 "<yang-version value=\"1.0\"/>\n"
4259 "<namespace uri=\"urn:example:foo\"/>\n"
4260 "<prefix value=\"foo\"/>\n"
4261 "</module>"
4262 "<module>";
4263 assert_int_equal(yin_parse_module(&yin_ctx, data, mod), LY_EVALID);
4264 logbuf_assert("Trailing garbage \"<module>\" after module, expected end-of-input. Line number 5.");
4265 lys_module_free(mod, NULL);
4266 yin_parser_ctx_free(yin_ctx);
4267 mod = NULL;
4268 yin_ctx = NULL;
4269
David Sedlák8985a142019-07-31 16:43:06 +02004270 st->finished_correctly = true;
4271}
4272
4273static void
4274test_yin_parse_submodule(void **state)
4275{
4276 struct state *st = *state;
4277 const char *data;
4278 struct yin_parser_ctx *yin_ctx = NULL;
4279 struct lysp_submodule *submod = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02004280 struct lys_parser_ctx main_ctx = {};
David Sedlák8985a142019-07-31 16:43:06 +02004281
4282 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
4283 "<submodule name=\"asub\""
4284 "xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
4285 "xmlns:a=\"urn:a\">"
4286 "<yang-version value=\"1.0\"/>\n"
4287 "<belongs-to module=\"a\">"
4288 "<prefix value=\"a_pref\"/>"
4289 "</belongs-to>"
4290 "<include module=\"atop\"/>"
4291 "<feature name=\"fox\"/>"
4292 "<notification name=\"bar-notif\">"
4293 "<if-feature name=\"bar\"/>"
4294 "</notification>"
4295 "<notification name=\"fox-notif\">"
4296 "<if-feature name=\"fox\"/>"
4297 "</notification>"
4298 "<augment target-node=\"/a_pref:top\">"
4299 "<if-feature name=\"bar\"/>"
4300 "<container name=\"bar-sub\"/>"
4301 "</augment>"
4302 "<augment target-node=\"/top\">"
4303 "<container name=\"bar-sub2\"/>"
4304 "</augment>"
4305 "</submodule>";
David Sedlák1b623122019-08-05 15:27:49 +02004306 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, &main_ctx, data, &submod), LY_SUCCESS);
David Sedlák8985a142019-07-31 16:43:06 +02004307 lysp_submodule_free(st->ctx, submod);
4308 yin_parser_ctx_free(yin_ctx);
4309 yin_ctx = NULL;
4310 submod = NULL;
4311
4312 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
David Sedlák6d781b62019-08-02 15:22:52 +02004313 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
4314 "<yang-version value=\"1.0\"/>\n"
4315 "<belongs-to module=\"a\">"
4316 "<prefix value=\"a_pref\"/>"
4317 "</belongs-to>"
4318 "</submodule>";
David Sedlák1b623122019-08-05 15:27:49 +02004319 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, &main_ctx, data, &submod), LY_SUCCESS);
David Sedlák6d781b62019-08-02 15:22:52 +02004320 lysp_submodule_free(st->ctx, submod);
4321 yin_parser_ctx_free(yin_ctx);
4322 yin_ctx = NULL;
4323 submod = NULL;
4324
4325 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
David Sedlák8985a142019-07-31 16:43:06 +02004326 "<module name=\"inval\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
4327 "</module>";
David Sedlák1b623122019-08-05 15:27:49 +02004328 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, &main_ctx, data, &submod), LY_EINVAL);
David Sedlák8985a142019-07-31 16:43:06 +02004329 logbuf_assert("Input data contains module in situation when a submodule is expected.");
4330 lysp_submodule_free(st->ctx, submod);
4331 yin_parser_ctx_free(yin_ctx);
4332 yin_ctx = NULL;
4333 submod = NULL;
4334
David Sedlák6d781b62019-08-02 15:22:52 +02004335 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
4336 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
4337 "<yang-version value=\"1.0\"/>\n"
4338 "<belongs-to module=\"a\">"
4339 "<prefix value=\"a_pref\"/>"
4340 "</belongs-to>"
4341 "</submodule>"
4342 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
4343 "<yang-version value=\"1.0\"/>\n"
4344 "<belongs-to module=\"a\">"
4345 "<prefix value=\"a_pref\"/>"
4346 "</belongs-to>"
4347 "</submodule>";
David Sedlák1b623122019-08-05 15:27:49 +02004348 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, &main_ctx, data, &submod), LY_EVALID);
David Sedlák6d781b62019-08-02 15:22:52 +02004349 logbuf_assert("Trailing garbage \"<submodule name...\" after submodule, expected end-of-input. Line number 2.");
4350 lysp_submodule_free(st->ctx, submod);
4351 yin_parser_ctx_free(yin_ctx);
4352 yin_ctx = NULL;
4353 submod = NULL;
4354
David Sedlák8985a142019-07-31 16:43:06 +02004355 st->finished_correctly = true;
4356}
4357
David Sedlák3b4db242018-10-19 16:11:01 +02004358int
4359main(void)
4360{
4361
4362 const struct CMUnitTest tests[] = {
David Sedlák8f7a1172019-06-20 14:42:18 +02004363 cmocka_unit_test_setup_teardown(test_yin_match_keyword, setup_f, teardown_f),
David Sedlákb1a78352019-06-28 16:16:29 +02004364 cmocka_unit_test_setup_teardown(test_yin_parse_element_generic, setup_f, teardown_f),
4365 cmocka_unit_test_setup_teardown(test_yin_parse_extension_instance, setup_f, teardown_f),
David Sedlák555c7202019-07-04 12:14:12 +02004366 cmocka_unit_test_setup_teardown(test_yin_parse_content, setup_f, teardown_f),
David Sedlák4a650532019-07-10 11:55:18 +02004367 cmocka_unit_test_setup_teardown(test_validate_value, setup_f, teardown_f),
David Sedlák32488102019-07-15 17:44:10 +02004368
David Sedlák8e7bda82019-07-16 17:57:50 +02004369 cmocka_unit_test(test_yin_match_argument_name),
David Sedlák43801c92019-08-05 15:58:54 +02004370 cmocka_unit_test_setup_teardown(test_enum_elem, setup_element_test, teardown_element_test),
4371 cmocka_unit_test_setup_teardown(test_bit_elem, setup_element_test, teardown_element_test),
David Sedlák32488102019-07-15 17:44:10 +02004372 cmocka_unit_test_setup_teardown(test_meta_elem, setup_element_test, teardown_element_test),
4373 cmocka_unit_test_setup_teardown(test_import_elem, setup_element_test, teardown_element_test),
4374 cmocka_unit_test_setup_teardown(test_status_elem, setup_element_test, teardown_element_test),
4375 cmocka_unit_test_setup_teardown(test_ext_elem, setup_element_test, teardown_element_test),
4376 cmocka_unit_test_setup_teardown(test_yin_element_elem, setup_element_test, teardown_element_test),
4377 cmocka_unit_test_setup_teardown(test_yangversion_elem, setup_element_test, teardown_element_test),
4378 cmocka_unit_test_setup_teardown(test_mandatory_elem, setup_element_test, teardown_element_test),
David Sedlák8e7bda82019-07-16 17:57:50 +02004379 cmocka_unit_test_setup_teardown(test_argument_elem, setup_element_test, teardown_element_test),
4380 cmocka_unit_test_setup_teardown(test_base_elem, setup_element_test, teardown_element_test),
4381 cmocka_unit_test_setup_teardown(test_belongsto_elem, setup_element_test, teardown_element_test),
4382 cmocka_unit_test_setup_teardown(test_config_elem, setup_element_test, teardown_element_test),
4383 cmocka_unit_test_setup_teardown(test_default_elem, setup_element_test, teardown_element_test),
4384 cmocka_unit_test_setup_teardown(test_err_app_tag_elem, setup_element_test, teardown_element_test),
4385 cmocka_unit_test_setup_teardown(test_err_msg_elem, setup_element_test, teardown_element_test),
4386 cmocka_unit_test_setup_teardown(test_fracdigits_elem, setup_element_test, teardown_element_test),
4387 cmocka_unit_test_setup_teardown(test_iffeature_elem, setup_element_test, teardown_element_test),
4388 cmocka_unit_test_setup_teardown(test_length_elem, setup_element_test, teardown_element_test),
4389 cmocka_unit_test_setup_teardown(test_modifier_elem, setup_element_test, teardown_element_test),
4390 cmocka_unit_test_setup_teardown(test_namespace_elem, setup_element_test, teardown_element_test),
4391 cmocka_unit_test_setup_teardown(test_path_elem, setup_element_test, teardown_element_test),
4392 cmocka_unit_test_setup_teardown(test_pattern_elem, setup_element_test, teardown_element_test),
4393 cmocka_unit_test_setup_teardown(test_value_position_elem, setup_element_test, teardown_element_test),
David Sedlák69f01612019-07-17 11:41:08 +02004394 cmocka_unit_test_setup_teardown(test_prefix_elem, setup_element_test, teardown_element_test),
4395 cmocka_unit_test_setup_teardown(test_range_elem, setup_element_test, teardown_element_test),
4396 cmocka_unit_test_setup_teardown(test_reqinstance_elem, setup_element_test, teardown_element_test),
4397 cmocka_unit_test_setup_teardown(test_revision_date_elem, setup_element_test, teardown_element_test),
4398 cmocka_unit_test_setup_teardown(test_unique_elem, setup_element_test, teardown_element_test),
4399 cmocka_unit_test_setup_teardown(test_units_elem, setup_element_test, teardown_element_test),
4400 cmocka_unit_test_setup_teardown(test_when_elem, setup_element_test, teardown_element_test),
4401 cmocka_unit_test_setup_teardown(test_yin_text_value_elem, setup_element_test, teardown_element_test),
David Sedlák374d2b32019-07-17 15:06:55 +02004402 cmocka_unit_test_setup_teardown(test_type_elem, setup_element_test, teardown_element_test),
David Sedlák1af868e2019-07-17 17:03:14 +02004403 cmocka_unit_test_setup_teardown(test_max_elems_elem, setup_element_test, teardown_element_test),
David Sedlák09e18c92019-07-18 11:17:11 +02004404 cmocka_unit_test_setup_teardown(test_min_elems_elem, setup_element_test, teardown_element_test),
David Sedláka2dad212019-07-18 12:45:19 +02004405 cmocka_unit_test_setup_teardown(test_ordby_elem, setup_element_test, teardown_element_test),
David Sedlák8a83bbb2019-07-18 14:46:00 +02004406 cmocka_unit_test_setup_teardown(test_any_elem, setup_element_test, teardown_element_test),
David Sedlák203ca3a2019-07-18 15:26:25 +02004407 cmocka_unit_test_setup_teardown(test_leaf_elem, setup_element_test, teardown_element_test),
David Sedlákc3da3ef2019-07-19 12:56:08 +02004408 cmocka_unit_test_setup_teardown(test_leaf_list_elem, setup_element_test, teardown_element_test),
David Sedlákcb39f642019-07-19 13:19:55 +02004409 cmocka_unit_test_setup_teardown(test_presence_elem, setup_element_test, teardown_element_test),
David Sedlák12470a82019-07-19 13:44:36 +02004410 cmocka_unit_test_setup_teardown(test_key_elem, setup_element_test, teardown_element_test),
David Sedlák04e17b22019-07-19 15:29:48 +02004411 cmocka_unit_test_setup_teardown(test_typedef_elem, setup_element_test, teardown_element_test),
David Sedlákd2d676a2019-07-22 11:28:19 +02004412 cmocka_unit_test_setup_teardown(test_refine_elem, setup_element_test, teardown_element_test),
David Sedlák0d6de5a2019-07-22 13:25:44 +02004413 cmocka_unit_test_setup_teardown(test_uses_elem, setup_element_test, teardown_element_test),
David Sedlákaa854b02019-07-22 14:17:10 +02004414 cmocka_unit_test_setup_teardown(test_revision_elem, setup_element_test, teardown_element_test),
David Sedlák0c2bab92019-07-22 15:33:19 +02004415 cmocka_unit_test_setup_teardown(test_include_elem, setup_element_test, teardown_element_test),
David Sedlák5e13dea2019-07-22 16:06:45 +02004416 cmocka_unit_test_setup_teardown(test_feature_elem, setup_element_test, teardown_element_test),
David Sedlák28794f22019-07-22 16:45:00 +02004417 cmocka_unit_test_setup_teardown(test_identity_elem, setup_element_test, teardown_element_test),
David Sedlákaf536aa2019-07-23 13:42:23 +02004418 cmocka_unit_test_setup_teardown(test_list_elem, setup_element_test, teardown_element_test),
David Sedlák031b9e72019-07-23 15:19:37 +02004419 cmocka_unit_test_setup_teardown(test_notification_elem, setup_element_test, teardown_element_test),
David Sedláke3ce9ef2019-07-23 16:34:30 +02004420 cmocka_unit_test_setup_teardown(test_grouping_elem, setup_element_test, teardown_element_test),
David Sedlákf111bcb2019-07-23 17:15:51 +02004421 cmocka_unit_test_setup_teardown(test_container_elem, setup_element_test, teardown_element_test),
David Sedlák5379d392019-07-24 10:42:03 +02004422 cmocka_unit_test_setup_teardown(test_case_elem, setup_element_test, teardown_element_test),
David Sedlákb7abcfa2019-07-24 12:33:35 +02004423 cmocka_unit_test_setup_teardown(test_choice_elem, setup_element_test, teardown_element_test),
David Sedlák05404f62019-07-24 14:11:53 +02004424 cmocka_unit_test_setup_teardown(test_inout_elem, setup_element_test, teardown_element_test),
David Sedlák85d0eca2019-07-24 15:15:21 +02004425 cmocka_unit_test_setup_teardown(test_action_elem, setup_element_test, teardown_element_test),
David Sedlák992fb7c2019-07-24 16:51:01 +02004426 cmocka_unit_test_setup_teardown(test_augment_elem, setup_element_test, teardown_element_test),
David Sedlák4ffcec82019-07-25 15:10:21 +02004427 cmocka_unit_test_setup_teardown(test_deviate_elem, setup_element_test, teardown_element_test),
David Sedlák8b754462019-07-25 16:22:13 +02004428 cmocka_unit_test_setup_teardown(test_deviation_elem, setup_element_test, teardown_element_test),
David Sedlák4f03b932019-07-26 13:01:47 +02004429 cmocka_unit_test_setup_teardown(test_module_elem, setup_element_test, teardown_element_test),
David Sedlák298ff6d2019-07-26 14:29:03 +02004430 cmocka_unit_test_setup_teardown(test_submodule_elem, setup_element_test, teardown_element_test),
David Sedlák8985a142019-07-31 16:43:06 +02004431
4432 cmocka_unit_test_setup_teardown(test_yin_parse_module, setup_logger, teardown_logger),
4433 cmocka_unit_test_setup_teardown(test_yin_parse_submodule, setup_logger, teardown_logger),
David Sedlák3b4db242018-10-19 16:11:01 +02004434 };
4435
David Sedlák8e7bda82019-07-16 17:57:50 +02004436 return cmocka_run_group_tests(tests, setup_ly_ctx, destroy_ly_ctx);
David Sedlák3b4db242018-10-19 16:11:01 +02004437}