blob: 1d00e4dcb421eee2ebd078d31cacd8a9085d5f8d [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;
Radek Krejci99435242019-09-05 16:19:15 +0200151 st->yin_ctx->pos_type = LY_VLOG_LINE;
David Sedlákda8ffa32019-07-08 14:17:10 +0200152 st->yin_ctx->xml_ctx.line = 1;
David Sedlák8f5bce02019-06-03 16:41:08 +0200153
David Sedlák68a1af12019-03-08 13:46:54 +0100154 return EXIT_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +0200155}
156
157static int
David Sedlák68a1af12019-03-08 13:46:54 +0100158teardown_f(void **state)
159{
160 struct state *st = *(struct state **)state;
David Sedlák619db942019-07-03 14:47:30 +0200161 struct lys_module *temp;
David Sedlák68a1af12019-03-08 13:46:54 +0100162
David Sedlák79e50cb2019-06-05 16:33:09 +0200163#if ENABLE_LOGGER_CHECKING
164 /* teardown logger */
165 if (!st->finished_correctly && logbuf[0] != '\0') {
166 fprintf(stderr, "%s\n", logbuf);
167 }
168#endif
169
David Sedlák619db942019-07-03 14:47:30 +0200170 temp = st->lysp_mod->mod;
171
David Sedlákda8ffa32019-07-08 14:17:10 +0200172 lyxml_context_clear(&st->yin_ctx->xml_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100173 lys_module_free(st->mod, NULL);
David Sedlák619db942019-07-03 14:47:30 +0200174 lysp_module_free(st->lysp_mod);
175 lys_module_free(temp, NULL);
David Sedlákda8ffa32019-07-08 14:17:10 +0200176 free(st->yin_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100177
178 return EXIT_SUCCESS;
179}
180
David Sedlák392af4f2019-06-04 16:02:42 +0200181static struct state*
182reset_state(void **state)
183{
David Sedlák79e50cb2019-06-05 16:33:09 +0200184 ((struct state *)*state)->finished_correctly = true;
David Sedlák555c7202019-07-04 12:14:12 +0200185 logbuf[0] = '\0';
David Sedlák392af4f2019-06-04 16:02:42 +0200186 teardown_f(state);
187 setup_f(state);
188
189 return *state;
190}
191
David Sedlák79e50cb2019-06-05 16:33:09 +0200192void
193logbuf_clean(void)
194{
195 logbuf[0] = '\0';
196}
197
David Sedlák8985a142019-07-31 16:43:06 +0200198static int
199setup_logger(void **state)
200{
201 (void)state; /* unused */
202#if ENABLE_LOGGER_CHECKING
203 /* setup logger */
204 ly_set_log_clb(logger, 1);
205#endif
206
207 logbuf[0] = '\0';
208
209 return EXIT_SUCCESS;
210}
211
212static int
213teardown_logger(void **state)
214{
215 struct state *st = *state;
216
217#if ENABLE_LOGGER_CHECKING
218 /* teardown logger */
219 if (!st->finished_correctly && logbuf[0] != '\0') {
220 fprintf(stderr, "%s\n", logbuf);
221 }
222#endif
223
224 return EXIT_SUCCESS;
225}
226
227static int
228setup_element_test(void **state)
229{
230 setup_logger(state);
231 struct state *st = *state;
232
233 st->yin_ctx = calloc(1, sizeof(*st->yin_ctx));
234
235 /* allocate parser context */
236 st->yin_ctx->xml_ctx.ctx = st->ctx;
Radek Krejci99435242019-09-05 16:19:15 +0200237 st->yin_ctx->pos_type = LY_VLOG_LINE;
David Sedlák8985a142019-07-31 16:43:06 +0200238 st->yin_ctx->xml_ctx.line = 1;
239
240 return EXIT_SUCCESS;
241}
242
243static int
244teardown_element_test(void **state)
245{
246 struct state *st = *(struct state **)state;
247
248 lyxml_context_clear(&st->yin_ctx->xml_ctx);
249 free(st->yin_ctx);
250
251 teardown_logger(state);
252
253 return EXIT_SUCCESS;
254}
255
David Sedlák68a1af12019-03-08 13:46:54 +0100256static void
David Sedlák1bccdfa2019-06-17 15:55:27 +0200257test_yin_match_keyword(void **state)
David Sedlák3b4db242018-10-19 16:11:01 +0200258{
David Sedlák8f7a1172019-06-20 14:42:18 +0200259 struct state *st = *state;
David Sedlák3b4db242018-10-19 16:11:01 +0200260
David Sedlák8f7a1172019-06-20 14:42:18 +0200261 const char *prefix, *name;
262 struct yin_arg_record *args = NULL;
263 size_t prefix_len, name_len;
264 /* create mock yin namespace in xml context */
265 const char *data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" />";
David Sedlákda8ffa32019-07-08 14:17:10 +0200266 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
267 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák8f7a1172019-06-20 14:42:18 +0200268 LY_ARRAY_FREE(args);
269
Radek Krejcid6b76452019-09-03 17:03:03 +0200270 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydatax", strlen("anydatax"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
271 assert_int_equal(yin_match_keyword(st->yin_ctx, "asdasd", strlen("asdasd"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
272 assert_int_equal(yin_match_keyword(st->yin_ctx, "", 0, prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
273 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydata", strlen("anydata"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ANYDATA);
274 assert_int_equal(yin_match_keyword(st->yin_ctx, "anyxml", strlen("anyxml"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ANYXML);
275 assert_int_equal(yin_match_keyword(st->yin_ctx, "argument", strlen("argument"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ARGUMENT);
276 assert_int_equal(yin_match_keyword(st->yin_ctx, "augment", strlen("augment"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_AUGMENT);
277 assert_int_equal(yin_match_keyword(st->yin_ctx, "base", strlen("base"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BASE);
278 assert_int_equal(yin_match_keyword(st->yin_ctx, "belongs-to", strlen("belongs-to"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BELONGS_TO);
279 assert_int_equal(yin_match_keyword(st->yin_ctx, "bit", strlen("bit"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BIT);
280 assert_int_equal(yin_match_keyword(st->yin_ctx, "case", strlen("case"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CASE);
281 assert_int_equal(yin_match_keyword(st->yin_ctx, "choice", strlen("choice"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CHOICE);
282 assert_int_equal(yin_match_keyword(st->yin_ctx, "config", strlen("config"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONFIG);
283 assert_int_equal(yin_match_keyword(st->yin_ctx, "contact", strlen("contact"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONTACT);
284 assert_int_equal(yin_match_keyword(st->yin_ctx, "container", strlen("container"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONTAINER);
285 assert_int_equal(yin_match_keyword(st->yin_ctx, "default", strlen("default"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEFAULT);
286 assert_int_equal(yin_match_keyword(st->yin_ctx, "description", strlen("description"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DESCRIPTION);
287 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviate", strlen("deviate"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEVIATE);
288 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviation", strlen("deviation"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEVIATION);
289 assert_int_equal(yin_match_keyword(st->yin_ctx, "enum", strlen("enum"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ENUM);
290 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);
291 assert_int_equal(yin_match_keyword(st->yin_ctx, "error-message", strlen("error-message"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ERROR_MESSAGE);
292 assert_int_equal(yin_match_keyword(st->yin_ctx, "extension", strlen("extension"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_EXTENSION);
293 assert_int_equal(yin_match_keyword(st->yin_ctx, "feature", strlen("feature"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_FEATURE);
294 assert_int_equal(yin_match_keyword(st->yin_ctx, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_FRACTION_DIGITS);
295 assert_int_equal(yin_match_keyword(st->yin_ctx, "grouping", strlen("grouping"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_GROUPING);
296 assert_int_equal(yin_match_keyword(st->yin_ctx, "identity", strlen("identity"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IDENTITY);
297 assert_int_equal(yin_match_keyword(st->yin_ctx, "if-feature", strlen("if-feature"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IF_FEATURE);
298 assert_int_equal(yin_match_keyword(st->yin_ctx, "import", strlen("import"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IMPORT);
299 assert_int_equal(yin_match_keyword(st->yin_ctx, "include", strlen("include"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_INCLUDE);
300 assert_int_equal(yin_match_keyword(st->yin_ctx, "input", strlen("input"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_INPUT);
301 assert_int_equal(yin_match_keyword(st->yin_ctx, "key", strlen("key"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_KEY);
302 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf", strlen("leaf"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LEAF);
303 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf-list", strlen("leaf-list"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LEAF_LIST);
304 assert_int_equal(yin_match_keyword(st->yin_ctx, "length", strlen("length"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LENGTH);
305 assert_int_equal(yin_match_keyword(st->yin_ctx, "list", strlen("list"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LIST);
306 assert_int_equal(yin_match_keyword(st->yin_ctx, "mandatory", strlen("mandatory"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MANDATORY);
307 assert_int_equal(yin_match_keyword(st->yin_ctx, "max-elements", strlen("max-elements"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MAX_ELEMENTS);
308 assert_int_equal(yin_match_keyword(st->yin_ctx, "min-elements", strlen("min-elements"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MIN_ELEMENTS);
309 assert_int_equal(yin_match_keyword(st->yin_ctx, "modifier", strlen("modifier"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MODIFIER);
310 assert_int_equal(yin_match_keyword(st->yin_ctx, "module", strlen("module"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MODULE);
311 assert_int_equal(yin_match_keyword(st->yin_ctx, "must", strlen("must"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MUST);
312 assert_int_equal(yin_match_keyword(st->yin_ctx, "namespace", strlen("namespace"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NAMESPACE);
313 assert_int_equal(yin_match_keyword(st->yin_ctx, "notification", strlen("notification"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NOTIFICATION);
314 assert_int_equal(yin_match_keyword(st->yin_ctx, "ordered-by", strlen("ordered-by"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ORDERED_BY);
315 assert_int_equal(yin_match_keyword(st->yin_ctx, "organization", strlen("organization"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ORGANIZATION);
316 assert_int_equal(yin_match_keyword(st->yin_ctx, "output", strlen("output"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_OUTPUT);
317 assert_int_equal(yin_match_keyword(st->yin_ctx, "path", strlen("path"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PATH);
318 assert_int_equal(yin_match_keyword(st->yin_ctx, "pattern", strlen("pattern"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PATTERN);
319 assert_int_equal(yin_match_keyword(st->yin_ctx, "position", strlen("position"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_POSITION);
320 assert_int_equal(yin_match_keyword(st->yin_ctx, "prefix", strlen("prefix"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PREFIX);
321 assert_int_equal(yin_match_keyword(st->yin_ctx, "presence", strlen("presence"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PRESENCE);
322 assert_int_equal(yin_match_keyword(st->yin_ctx, "range", strlen("range"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_RANGE);
323 assert_int_equal(yin_match_keyword(st->yin_ctx, "reference", strlen("reference"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REFERENCE);
324 assert_int_equal(yin_match_keyword(st->yin_ctx, "refine", strlen("refine"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REFINE);
325 assert_int_equal(yin_match_keyword(st->yin_ctx, "require-instance", strlen("require-instance"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REQUIRE_INSTANCE);
326 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision", strlen("revision"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REVISION);
327 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision-date", strlen("revision-date"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REVISION_DATE);
328 assert_int_equal(yin_match_keyword(st->yin_ctx, "rpc", strlen("rpc"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_RPC);
329 assert_int_equal(yin_match_keyword(st->yin_ctx, "status", strlen("status"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_STATUS);
330 assert_int_equal(yin_match_keyword(st->yin_ctx, "submodule", strlen("submodule"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_SUBMODULE);
331 assert_int_equal(yin_match_keyword(st->yin_ctx, "type", strlen("type"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_TYPE);
332 assert_int_equal(yin_match_keyword(st->yin_ctx, "typedef", strlen("typedef"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_TYPEDEF);
333 assert_int_equal(yin_match_keyword(st->yin_ctx, "unique", strlen("unique"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_UNIQUE);
334 assert_int_equal(yin_match_keyword(st->yin_ctx, "units", strlen("units"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_UNITS);
335 assert_int_equal(yin_match_keyword(st->yin_ctx, "uses", strlen("uses"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_USES);
336 assert_int_equal(yin_match_keyword(st->yin_ctx, "value", strlen("value"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_VALUE);
337 assert_int_equal(yin_match_keyword(st->yin_ctx, "when", strlen("when"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_WHEN);
338 assert_int_equal(yin_match_keyword(st->yin_ctx, "yang-version", strlen("yang-version"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_YANG_VERSION);
339 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 +0200340
341 st->finished_correctly = true;
David Sedlák872c7b42018-10-26 13:15:20 +0200342}
David Sedlák3b4db242018-10-19 16:11:01 +0200343
David Sedlák872c7b42018-10-26 13:15:20 +0200344static void
David Sedlák060b00e2019-06-19 11:12:06 +0200345test_yin_match_argument_name(void **state)
David Sedlák872c7b42018-10-26 13:15:20 +0200346{
David Sedlák68a1af12019-03-08 13:46:54 +0100347 (void)state; /* unused */
David Sedlák872c7b42018-10-26 13:15:20 +0200348
David Sedlák060b00e2019-06-19 11:12:06 +0200349 assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN);
350 assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN);
351 assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN);
352 assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION);
353 assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE);
354 assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE);
355 assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME);
356 assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG);
357 assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
358 assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT);
359 assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI);
360 assert_int_equal(yin_match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200361}
362
David Sedlák68a1af12019-03-08 13:46:54 +0100363static void
David Sedlákb1a78352019-06-28 16:16:29 +0200364test_yin_parse_element_generic(void **state)
365{
366 const char *prefix, *name;
367 struct state *st = *state;
368 struct lysp_ext_instance exts;
369 size_t prefix_len, name_len;
370 LY_ERR ret;
371
372 memset(&exts, 0, sizeof(exts));
373
374 const char *data = "<elem attr=\"value\">text_value</elem>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200375 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák4ffcec82019-07-25 15:10:21 +0200376 ret = yin_parse_element_generic(st->yin_ctx, name, name_len, &data, &exts.child);
David Sedlákb1a78352019-06-28 16:16:29 +0200377 assert_int_equal(ret, LY_SUCCESS);
David Sedlák1fdb2522019-07-09 16:22:57 +0200378 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákb1a78352019-06-28 16:16:29 +0200379 assert_string_equal(exts.child->stmt, "elem");
380 assert_string_equal(exts.child->arg, "text_value");
David Sedlákb1a78352019-06-28 16:16:29 +0200381 assert_string_equal(exts.child->child->stmt, "attr");
382 assert_string_equal(exts.child->child->arg, "value");
383 assert_true(exts.child->child->flags & LYS_YIN_ATTR);
David Sedlákb1a78352019-06-28 16:16:29 +0200384 lysp_ext_instance_free(st->ctx, &exts);
David Sedlák5392a212019-07-01 09:19:10 +0200385 st = reset_state(state);
386
387 data = "<elem></elem>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200388 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák4ffcec82019-07-25 15:10:21 +0200389 ret = yin_parse_element_generic(st->yin_ctx, name, name_len, &data, &exts.child);
David Sedlák5392a212019-07-01 09:19:10 +0200390 assert_int_equal(ret, LY_SUCCESS);
391 assert_string_equal(exts.child->stmt, "elem");
392 assert_null(exts.child->child);
393 assert_null(exts.child->arg);
David Sedlákda8ffa32019-07-08 14:17:10 +0200394 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlák5392a212019-07-01 09:19:10 +0200395 lysp_ext_instance_free(st->ctx, &exts);
396
David Sedlákb1a78352019-06-28 16:16:29 +0200397 st->finished_correctly = true;
398}
399
400static void
401test_yin_parse_extension_instance(void **state)
402{
403 LY_ERR ret;
404 struct state *st = *state;
405 const char *prefix, *name;
406 size_t prefix_len, name_len;
407 struct yin_arg_record *args = NULL;
408 struct lysp_ext_instance *exts = NULL;
David Sedlákb1a78352019-06-28 16:16:29 +0200409 const char *data = "<ext value1=\"test\" value=\"test2\"><subelem>text</subelem></ext>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200410 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
411 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200412 ret = yin_parse_extension_instance(st->yin_ctx, args, &data, name2fname(name, prefix_len),
413 len2flen(name_len, prefix_len), LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200414 assert_int_equal(ret, LY_SUCCESS);
415 assert_string_equal(exts->name, "ext");
416 assert_int_equal(exts->insubstmt_index, 0);
417 assert_true(exts->insubstmt == LYEXT_SUBSTMT_CONTACT);
418 assert_true(exts->yin & LYS_YIN);
419 assert_string_equal(exts->child->stmt, "value1");
420 assert_string_equal(exts->child->arg, "test");
421 assert_null(exts->child->child);
422 assert_true(exts->child->flags & LYS_YIN_ATTR);
423 assert_string_equal(exts->child->next->stmt, "value");
424 assert_string_equal(exts->child->next->arg, "test2");
425 assert_null(exts->child->next->child);
426 assert_true(exts->child->next->flags & LYS_YIN_ATTR);
427
428 assert_string_equal(exts->child->next->next->stmt, "subelem");
429 assert_string_equal(exts->child->next->next->arg, "text");
430 assert_null(exts->child->next->next->child);
431 assert_null(exts->child->next->next->next);
432 assert_false(exts->child->next->next->flags & LYS_YIN_ATTR);
David Sedlákda8ffa32019-07-08 14:17:10 +0200433 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákb1a78352019-06-28 16:16:29 +0200434 LY_ARRAY_FREE(args);
435 lysp_ext_instance_free(st->ctx, exts);
436 LY_ARRAY_FREE(exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200437 exts = NULL;
438 args = NULL;
439 st = reset_state(state);
440
441 data = "<extension-elem />";
David Sedlákda8ffa32019-07-08 14:17:10 +0200442 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
443 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák1f90d252019-07-10 17:09:32 +0200444 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 +0200445 assert_int_equal(ret, LY_SUCCESS);
446 assert_string_equal(exts->name, "extension-elem");
447 assert_null(exts->argument);
448 assert_null(exts->child);
449 assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT);
450 assert_int_equal(exts->insubstmt_index, 0);
451 assert_true(exts->yin & LYS_YIN);
David Sedlákda8ffa32019-07-08 14:17:10 +0200452 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákf250ecf2019-07-01 11:02:05 +0200453 LY_ARRAY_FREE(args);
454 lysp_ext_instance_free(st->ctx, exts);
455 LY_ARRAY_FREE(exts);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200456 exts = NULL;
457 args = NULL;
458 st = reset_state(state);
459
460 data = "<ext attr1=\"text1\" attr2=\"text2\">"
461 "<ext-sub1/>"
462 "<ext-sub2 sattr1=\"stext2\">"
463 "<ext-sub21>"
464 "<ext-sub211 sattr21=\"text21\"/>"
465 "</ext-sub21>"
466 "</ext-sub2>"
467 "<ext-sub3 attr3=\"text3\"></ext-sub3>"
468 "</ext>";
469 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
470 yin_load_attributes(st->yin_ctx, &data, &args);
471 ret = yin_parse_extension_instance(st->yin_ctx, args, &data, name, name_len, LYEXT_SUBSTMT_CONTACT, 0, &exts);
472 assert_int_equal(ret, LY_SUCCESS);
473
474 assert_string_equal(exts->name, "ext");
475 assert_null(exts->argument);
476 assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT);
477 assert_int_equal(exts->insubstmt_index, 0);
478 assert_true(exts->yin & LYS_YIN);
479 assert_string_equal(exts->child->stmt, "attr1");
480 assert_string_equal(exts->child->arg, "text1");
481 assert_null(exts->child->child);
482 assert_true(exts->child->flags & LYS_YIN_ATTR);
483
484 assert_string_equal(exts->child->next->stmt, "attr2");
485 assert_string_equal(exts->child->next->arg, "text2");
486 assert_null(exts->child->next->child);
487 assert_true(exts->child->next->flags & LYS_YIN_ATTR);
488
489 assert_string_equal(exts->child->next->next->stmt, "ext-sub1");
490 assert_null(exts->child->next->next->arg);
491 assert_null(exts->child->next->next->child);
492 assert_int_equal(exts->child->next->next->flags, 0);
493
494 assert_string_equal(exts->child->next->next->next->stmt, "ext-sub2");
495 assert_null(exts->child->next->next->next->arg);
496 assert_int_equal(exts->child->next->next->next->flags, 0);
497 assert_string_equal(exts->child->next->next->next->child->stmt, "sattr1");
498 assert_string_equal(exts->child->next->next->next->child->arg, "stext2");
499 assert_null(exts->child->next->next->next->child->child);
500 assert_true(exts->child->next->next->next->child->flags & LYS_YIN_ATTR);
501
502 assert_string_equal(exts->child->next->next->next->child->next->stmt, "ext-sub21");
503 assert_null(exts->child->next->next->next->child->next->arg);
504 assert_null(exts->child->next->next->next->child->next->next);
505 assert_int_equal(exts->child->next->next->next->child->next->flags, 0);
506
507 assert_string_equal(exts->child->next->next->next->child->next->child->stmt, "ext-sub211");
508 assert_null(exts->child->next->next->next->child->next->child->arg);
509 assert_int_equal(exts->child->next->next->next->child->next->child->flags, 0);
510 assert_null(exts->child->next->next->next->child->next->child->next);
511
512 assert_string_equal(exts->child->next->next->next->child->next->child->child->stmt, "sattr21");
513 assert_string_equal(exts->child->next->next->next->child->next->child->child->arg, "text21");
514 assert_null(exts->child->next->next->next->child->next->child->child->next);
515 assert_null(exts->child->next->next->next->child->next->child->child->child);
516 assert_true(exts->child->next->next->next->child->next->child->child->flags & LYS_YIN_ATTR);
517
518 assert_string_equal(exts->child->next->next->next->next->stmt, "ext-sub3");
519 assert_null(exts->child->next->next->next->next->arg);
520 assert_null(exts->child->next->next->next->next->next);
521 assert_int_equal(exts->child->next->next->next->next->flags, 0);
522
523 assert_string_equal(exts->child->next->next->next->next->child->stmt, "attr3");
524 assert_string_equal(exts->child->next->next->next->next->child->arg, "text3");
525 assert_null(exts->child->next->next->next->next->child->next);
526 assert_null(exts->child->next->next->next->next->child->child);
527 assert_true(exts->child->next->next->next->next->child->flags & LYS_YIN_ATTR);
528
529 LY_ARRAY_FREE(args);
530 lysp_ext_instance_free(st->ctx, exts);
531 LY_ARRAY_FREE(exts);
532 exts = NULL;
533 args = NULL;
534
David Sedlákb1a78352019-06-28 16:16:29 +0200535 st->finished_correctly = true;
536}
537
David Sedlák555c7202019-07-04 12:14:12 +0200538static void
539test_yin_parse_content(void **state)
540{
541 struct state *st = *state;
542 LY_ERR ret = LY_SUCCESS;
David Sedlákc5b20842019-08-13 10:18:31 +0200543 const char *name, *prefix;
544 size_t name_len, prefix_len;
David Sedlák555c7202019-07-04 12:14:12 +0200545 const char *data = "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
546 "<custom xmlns=\"my-ext\">"
547 "totally amazing extension"
548 "</custom>"
David Sedlák986cb412019-07-04 13:10:11 +0200549 "<extension name=\"ext\">"
550 "<argument name=\"argname\"></argument>"
551 "<description><text>desc</text></description>"
552 "<reference><text>ref</text></reference>"
553 "<status value=\"deprecated\"></status>"
554 "</extension>"
David Sedlák555c7202019-07-04 12:14:12 +0200555 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedlák32488102019-07-15 17:44:10 +0200556 "<if-feature name=\"foo\"></if-feature>"
David Sedlák32eee7b2019-07-09 12:38:44 +0200557 "<when condition=\"condition...\">"
558 "<reference><text>when_ref</text></reference>"
559 "<description><text>when_desc</text></description>"
560 "</when>"
David Sedláke1a30302019-07-10 13:49:38 +0200561 "<config value=\"true\"/>"
David Sedlákc1771b12019-07-10 15:55:46 +0200562 "<error-message>"
563 "<value>error-msg</value>"
564 "</error-message>"
David Sedlák2ce1be62019-07-10 16:15:09 +0200565 "<error-app-tag value=\"err-app-tag\"/>"
David Sedláka5b1d382019-07-10 16:31:09 +0200566 "<units name=\"radians\"></units>"
David Sedláke7084ce2019-07-10 16:44:15 +0200567 "<default value=\"default-value\"/>"
David Sedlák5545f5d2019-07-11 11:55:16 +0200568 "<position value=\"25\"></position>"
569 "<value value=\"-5\"/>"
David Sedlákcf5569a2019-07-11 13:31:34 +0200570 "<require-instance value=\"true\"></require-instance>"
David Sedlákb7296dd2019-07-11 14:58:38 +0200571 "<range value=\"5..10\" />"
David Sedlák438ae432019-07-11 15:36:54 +0200572 "<length value=\"baf\"/>"
David Sedlákd3983112019-07-12 11:20:56 +0200573 "<pattern value='pattern'>"
574 "<modifier value='invert-match'/>"
575 "</pattern>"
David Sedlákfd5b9c32019-07-12 15:33:13 +0200576 "<enum name=\"yay\">"
577 "</enum>"
David Sedlák555c7202019-07-04 12:14:12 +0200578 "</prefix>";
579 struct lysp_ext_instance *exts = NULL;
David Sedlák5f8191e2019-07-08 16:35:52 +0200580 const char **if_features = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200581 struct yin_arg_record *attrs = NULL;
David Sedláke7084ce2019-07-10 16:44:15 +0200582 const char *value, *err_msg, *app_tag, *units, *def;
David Sedlák986cb412019-07-04 13:10:11 +0200583 struct lysp_ext *ext_def = NULL;
David Sedlák32eee7b2019-07-09 12:38:44 +0200584 struct lysp_when *when_p = NULL;
David Sedlákcf5569a2019-07-11 13:31:34 +0200585 struct lysp_type_enum pos_enum = {}, val_enum = {};
David Sedlákfd5b9c32019-07-12 15:33:13 +0200586 struct lysp_type req_type = {}, range_type = {}, len_type = {}, patter_type = {}, enum_type = {};
David Sedláke1a30302019-07-10 13:49:38 +0200587 uint8_t config = 0;
David Sedlák555c7202019-07-04 12:14:12 +0200588
David Sedlákc5b20842019-08-13 10:18:31 +0200589 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlákda8ffa32019-07-08 14:17:10 +0200590 yin_load_attributes(st->yin_ctx, &data, &attrs);
David Sedlák555c7202019-07-04 12:14:12 +0200591
David Sedlákfd5b9c32019-07-12 15:33:13 +0200592 struct yin_subelement subelems[17] = {
Radek Krejcid6b76452019-09-03 17:03:03 +0200593 {LY_STMT_CONFIG, &config, 0},
594 {LY_STMT_DEFAULT, &def, YIN_SUBELEM_UNIQUE},
595 {LY_STMT_ENUM, &enum_type, 0},
596 {LY_STMT_ERROR_APP_TAG, &app_tag, YIN_SUBELEM_UNIQUE},
597 {LY_STMT_ERROR_MESSAGE, &err_msg, 0},
598 {LY_STMT_EXTENSION, &ext_def, 0},
599 {LY_STMT_IF_FEATURE, &if_features, 0},
600 {LY_STMT_LENGTH, &len_type, 0},
601 {LY_STMT_PATTERN, &patter_type, 0},
602 {LY_STMT_POSITION, &pos_enum, 0},
603 {LY_STMT_RANGE, &range_type, 0},
604 {LY_STMT_REQUIRE_INSTANCE, &req_type, 0},
605 {LY_STMT_UNITS, &units, YIN_SUBELEM_UNIQUE},
606 {LY_STMT_VALUE, &val_enum, 0},
607 {LY_STMT_WHEN, &when_p, 0},
608 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
609 {LY_STMT_ARG_TEXT, &value, 0}
David Sedlákd3983112019-07-12 11:20:56 +0200610 };
Radek Krejcid6b76452019-09-03 17:03:03 +0200611 ret = yin_parse_content(st->yin_ctx, subelems, 17, &data, LY_STMT_PREFIX, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200612 assert_int_equal(ret, LY_SUCCESS);
David Sedlák1fdb2522019-07-09 16:22:57 +0200613 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlák2ce1be62019-07-10 16:15:09 +0200614 /* check parsed values */
David Sedláke7084ce2019-07-10 16:44:15 +0200615 assert_string_equal(def, "default-value");
David Sedlák555c7202019-07-04 12:14:12 +0200616 assert_string_equal(exts->name, "custom");
617 assert_string_equal(exts->argument, "totally amazing extension");
618 assert_string_equal(value, "wsefsdf");
David Sedláka5b1d382019-07-10 16:31:09 +0200619 assert_string_equal(units, "radians");
David Sedlák32eee7b2019-07-09 12:38:44 +0200620 assert_string_equal(when_p->cond, "condition...");
621 assert_string_equal(when_p->dsc, "when_desc");
622 assert_string_equal(when_p->ref, "when_ref");
David Sedláke1a30302019-07-10 13:49:38 +0200623 assert_int_equal(config, LYS_CONFIG_W);
David Sedlák5545f5d2019-07-11 11:55:16 +0200624 assert_int_equal(pos_enum.value, 25);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200625 assert_true(pos_enum.flags & LYS_SET_VALUE);
David Sedlák5545f5d2019-07-11 11:55:16 +0200626 assert_int_equal(val_enum.value, -5);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200627 assert_true(val_enum.flags & LYS_SET_VALUE);
David Sedlákcf5569a2019-07-11 13:31:34 +0200628 assert_int_equal(req_type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200629 assert_true(req_type.flags &= LYS_SET_REQINST);
David Sedlákb7296dd2019-07-11 14:58:38 +0200630 assert_string_equal(range_type.range->arg, "5..10");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200631 assert_true(range_type.flags & LYS_SET_RANGE);
David Sedlákc1771b12019-07-10 15:55:46 +0200632 assert_string_equal(err_msg, "error-msg");
David Sedlák2ce1be62019-07-10 16:15:09 +0200633 assert_string_equal(app_tag, "err-app-tag");
David Sedlákfd5b9c32019-07-12 15:33:13 +0200634 assert_string_equal(enum_type.enums->name, "yay");
David Sedlák438ae432019-07-11 15:36:54 +0200635 assert_string_equal(len_type.length->arg, "baf");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200636 assert_true(len_type.flags & LYS_SET_LENGTH);
David Sedlákd3983112019-07-12 11:20:56 +0200637 assert_string_equal(patter_type.patterns->arg, "\x015pattern");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200638 assert_true(patter_type.flags & LYS_SET_PATTERN);
David Sedlák2ce1be62019-07-10 16:15:09 +0200639 /* cleanup */
David Sedlák555c7202019-07-04 12:14:12 +0200640 lysp_ext_instance_free(st->ctx, exts);
David Sedlák32eee7b2019-07-09 12:38:44 +0200641 lysp_when_free(st->ctx, when_p);
David Sedlák986cb412019-07-04 13:10:11 +0200642 lysp_ext_free(st->ctx, ext_def);
David Sedlák5f8191e2019-07-08 16:35:52 +0200643 FREE_STRING(st->ctx, *if_features);
David Sedlákc1771b12019-07-10 15:55:46 +0200644 FREE_STRING(st->ctx, err_msg);
David Sedlák2ce1be62019-07-10 16:15:09 +0200645 FREE_STRING(st->ctx, app_tag);
David Sedláka5b1d382019-07-10 16:31:09 +0200646 FREE_STRING(st->ctx, units);
David Sedlákd3983112019-07-12 11:20:56 +0200647 FREE_STRING(st->ctx, patter_type.patterns->arg);
David Sedláke7084ce2019-07-10 16:44:15 +0200648 FREE_STRING(st->ctx, def);
David Sedlákb7296dd2019-07-11 14:58:38 +0200649 FREE_STRING(st->ctx, range_type.range->arg);
David Sedlák438ae432019-07-11 15:36:54 +0200650 FREE_STRING(st->ctx, len_type.length->arg);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200651 FREE_STRING(st->ctx, enum_type.enums->name);
David Sedlákb7296dd2019-07-11 14:58:38 +0200652 FREE_STRING(st->ctx, value);
David Sedlák5f8191e2019-07-08 16:35:52 +0200653 LY_ARRAY_FREE(if_features);
David Sedlák555c7202019-07-04 12:14:12 +0200654 LY_ARRAY_FREE(exts);
David Sedlák986cb412019-07-04 13:10:11 +0200655 LY_ARRAY_FREE(ext_def);
David Sedlák555c7202019-07-04 12:14:12 +0200656 LY_ARRAY_FREE(attrs);
David Sedlákd3983112019-07-12 11:20:56 +0200657 LY_ARRAY_FREE(patter_type.patterns);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200658 LY_ARRAY_FREE(enum_type.enums);
David Sedlák32eee7b2019-07-09 12:38:44 +0200659 free(when_p);
David Sedlákb7296dd2019-07-11 14:58:38 +0200660 free(range_type.range);
David Sedlák438ae432019-07-11 15:36:54 +0200661 free(len_type.length);
David Sedlák555c7202019-07-04 12:14:12 +0200662 attrs = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200663 st = reset_state(state);
664
665 /* test unique subelem */
666 const char *prefix_value;
Radek Krejcid6b76452019-09-03 17:03:03 +0200667 struct yin_subelement subelems2[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE},
668 {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_UNIQUE}};
David Sedláke6cd89e2019-08-07 12:46:02 +0200669 data = ELEMENT_WRAPPER_START
David Sedlák555c7202019-07-04 12:14:12 +0200670 "<prefix value=\"inv_mod\" />"
671 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
672 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedláke6cd89e2019-08-07 12:46:02 +0200673 ELEMENT_WRAPPER_END;
David Sedlákc5b20842019-08-13 10:18:31 +0200674 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlákda8ffa32019-07-08 14:17:10 +0200675 yin_load_attributes(st->yin_ctx, &data, &attrs);
Radek Krejcid6b76452019-09-03 17:03:03 +0200676 ret = yin_parse_content(st->yin_ctx, subelems2, 2, &data, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200677 assert_int_equal(ret, LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200678 logbuf_assert("Redefinition of \"text\" sub-element in \"status\" element. Line number 1.");
David Sedlák555c7202019-07-04 12:14:12 +0200679 lydict_remove(st->ctx, prefix_value);
680 lydict_remove(st->ctx, value);
681 st = reset_state(state);
682 LY_ARRAY_FREE(attrs);
683 attrs = NULL;
684
685 /* test first subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200686 data = ELEMENT_WRAPPER_START
David Sedlák555c7202019-07-04 12:14:12 +0200687 "<prefix value=\"inv_mod\" />"
688 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
689 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedláke6cd89e2019-08-07 12:46:02 +0200690 ELEMENT_WRAPPER_END;
Radek Krejcid6b76452019-09-03 17:03:03 +0200691 struct yin_subelement subelems3[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE},
692 {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_FIRST}};
David Sedlákc5b20842019-08-13 10:18:31 +0200693 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlákda8ffa32019-07-08 14:17:10 +0200694 yin_load_attributes(st->yin_ctx, &data, &attrs);
Radek Krejcid6b76452019-09-03 17:03:03 +0200695 ret = yin_parse_content(st->yin_ctx, subelems3, 2, &data, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200696 assert_int_equal(ret, LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200697 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 +0200698 lydict_remove(st->ctx, prefix_value);
699 st = reset_state(state);
700 LY_ARRAY_FREE(attrs);
701 attrs = NULL;
702
703 /* test mandatory subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200704 data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END;
Radek Krejcid6b76452019-09-03 17:03:03 +0200705 struct yin_subelement subelems4[1] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}};
David Sedlákc5b20842019-08-13 10:18:31 +0200706 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlákda8ffa32019-07-08 14:17:10 +0200707 yin_load_attributes(st->yin_ctx, &data, &attrs);
Radek Krejcid6b76452019-09-03 17:03:03 +0200708 ret = yin_parse_content(st->yin_ctx, subelems4, 1, &data, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200709 assert_int_equal(ret, LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200710 logbuf_assert("Missing mandatory sub-element \"prefix\" of \"status\" element. Line number 1.");
David Sedlák555c7202019-07-04 12:14:12 +0200711 LY_ARRAY_FREE(attrs);
712
713 st->finished_correctly = true;
714}
715
David Sedlák92147b02019-07-09 14:01:01 +0200716static void
David Sedlák4a650532019-07-10 11:55:18 +0200717test_validate_value(void **state)
718{
719 struct state *st = *state;
720 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "#invalid", 8), LY_EVALID);
721 logbuf_assert("Invalid identifier character '#'. Line number 1.");
722 assert_int_equal(yin_validate_value(st->yin_ctx, Y_STR_ARG, "", 0), LY_SUCCESS);
723 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "pre:b", 5), LY_EVALID);
724 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:b", 5), LY_SUCCESS);
725 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:pre:b", 9), LY_EVALID);
726
727 st->finished_correctly = true;
728}
729
David Sedlák32488102019-07-15 17:44:10 +0200730/* helper function to simplify unit test of each element using parse_content function */
731LY_ERR
732test_element_helper(struct state *st, const char **data, void *dest, const char **text,
733 struct lysp_ext_instance **exts, bool valid)
734{
735 struct yin_arg_record *attrs = NULL;
David Sedlákc5b20842019-08-13 10:18:31 +0200736 const char *name, *prefix;
737 size_t name_len, prefix_len;
David Sedlák32488102019-07-15 17:44:10 +0200738 LY_ERR ret = LY_SUCCESS;
739 struct yin_subelement subelems[71] = {
Radek Krejcid6b76452019-09-03 17:03:03 +0200740 {LY_STMT_ACTION, dest, 0},
741 {LY_STMT_ANYDATA, dest, 0},
742 {LY_STMT_ANYXML, dest, 0},
743 {LY_STMT_ARGUMENT,dest, 0},
744 {LY_STMT_AUGMENT, dest, 0},
745 {LY_STMT_BASE, dest, 0},
746 {LY_STMT_BELONGS_TO, dest, 0},
747 {LY_STMT_BIT, dest, 0},
748 {LY_STMT_CASE, dest, 0},
749 {LY_STMT_CHOICE, dest, 0},
750 {LY_STMT_CONFIG, dest, 0},
751 {LY_STMT_CONTACT, dest, 0},
752 {LY_STMT_CONTAINER, dest, 0},
753 {LY_STMT_DEFAULT, dest, YIN_SUBELEM_UNIQUE},
754 {LY_STMT_DESCRIPTION, dest, 0},
755 {LY_STMT_DEVIATE, dest, 0},
756 {LY_STMT_DEVIATION, dest, 0},
757 {LY_STMT_ENUM, dest, 0},
758 {LY_STMT_ERROR_APP_TAG, dest, YIN_SUBELEM_UNIQUE},
759 {LY_STMT_ERROR_MESSAGE, dest, 0},
760 {LY_STMT_EXTENSION, dest, 0},
761 {LY_STMT_FEATURE, dest, 0},
762 {LY_STMT_FRACTION_DIGITS, dest, 0},
763 {LY_STMT_GROUPING, dest, 0},
764 {LY_STMT_IDENTITY, dest, 0},
765 {LY_STMT_IF_FEATURE, dest, 0},
766 {LY_STMT_IMPORT, dest, 0},
767 {LY_STMT_INCLUDE, dest, 0},
768 {LY_STMT_INPUT, dest, 0},
769 {LY_STMT_KEY, dest, YIN_SUBELEM_UNIQUE},
770 {LY_STMT_LEAF, dest, 0},
771 {LY_STMT_LEAF_LIST, dest, 0},
772 {LY_STMT_LENGTH, dest, 0},
773 {LY_STMT_LIST, dest, 0},
774 {LY_STMT_MANDATORY, dest, 0},
775 {LY_STMT_MAX_ELEMENTS, dest, 0},
776 {LY_STMT_MIN_ELEMENTS, dest, 0},
777 {LY_STMT_MODIFIER, dest, 0},
778 {LY_STMT_MODULE, dest, 0},
779 {LY_STMT_MUST, dest, 0},
780 {LY_STMT_NAMESPACE, dest, YIN_SUBELEM_UNIQUE},
781 {LY_STMT_NOTIFICATION, dest, 0},
782 {LY_STMT_ORDERED_BY, dest, 0},
783 {LY_STMT_ORGANIZATION, dest, 0},
784 {LY_STMT_OUTPUT, dest, 0},
785 {LY_STMT_PATH, dest, 0},
786 {LY_STMT_PATTERN, dest, 0},
787 {LY_STMT_POSITION, dest, 0},
788 {LY_STMT_PREFIX, dest, YIN_SUBELEM_UNIQUE},
789 {LY_STMT_PRESENCE, dest, YIN_SUBELEM_UNIQUE},
790 {LY_STMT_RANGE, dest, 0},
791 {LY_STMT_REFERENCE, dest, 0},
792 {LY_STMT_REFINE, dest, 0},
793 {LY_STMT_REQUIRE_INSTANCE, dest, 0},
794 {LY_STMT_REVISION, dest, 0},
795 {LY_STMT_REVISION_DATE, dest, 0},
796 {LY_STMT_RPC, dest, 0},
797 {LY_STMT_STATUS, dest, 0},
798 {LY_STMT_SUBMODULE, dest, 0},
799 {LY_STMT_TYPE, dest, 0},
800 {LY_STMT_TYPEDEF, dest, 0},
801 {LY_STMT_UNIQUE, dest, 0},
802 {LY_STMT_UNITS, dest, YIN_SUBELEM_UNIQUE},
803 {LY_STMT_USES, dest, 0},
804 {LY_STMT_VALUE, dest, 0},
805 {LY_STMT_WHEN, dest, 0},
806 {LY_STMT_YANG_VERSION, dest, 0},
807 {LY_STMT_YIN_ELEMENT, dest, 0},
808 {LY_STMT_EXTENSION_INSTANCE, dest, 0},
809 {LY_STMT_ARG_TEXT, dest, 0},
810 {LY_STMT_ARG_VALUE, dest, 0}
David Sedlák32488102019-07-15 17:44:10 +0200811 };
David Sedlákc5b20842019-08-13 10:18:31 +0200812 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 +0200813 LY_CHECK_RET(yin_load_attributes(st->yin_ctx, data, &attrs));
Radek Krejcid6b76452019-09-03 17:03:03 +0200814 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 +0200815 LY_ARRAY_FREE(attrs);
816 if (valid) {
817 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
818 }
819 /* reset status */
820 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
821 return ret;
822}
823
David Sedlákd1144562019-08-06 12:36:14 +0200824#define EXT_SUBELEM "<myext:c-define name=\"MY_MTU\" xmlns:myext=\"urn:example:extensions\"/>"
825
David Sedlák32488102019-07-15 17:44:10 +0200826static void
David Sedlák43801c92019-08-05 15:58:54 +0200827test_enum_elem(void **state)
David Sedlák32488102019-07-15 17:44:10 +0200828{
David Sedlák32488102019-07-15 17:44:10 +0200829 struct state *st = *state;
830 struct lysp_type type = {};
831 const char *data;
832 data = ELEMENT_WRAPPER_START
833 "<enum name=\"enum-name\">"
834 "<if-feature name=\"feature\" />"
835 "<value value=\"55\" />"
836 "<status value=\"deprecated\" />"
837 "<description><text>desc...</text></description>"
838 "<reference><text>ref...</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +0200839 EXT_SUBELEM
David Sedlák32488102019-07-15 17:44:10 +0200840 "</enum>"
841 ELEMENT_WRAPPER_END;
842 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200843 assert_string_equal(type.enums->name, "enum-name");
David Sedlák32488102019-07-15 17:44:10 +0200844 assert_string_equal(*type.enums->iffeatures, "feature");
845 assert_int_equal(type.enums->value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200846 assert_true((type.enums->flags & LYS_STATUS_DEPRC) && (type.enums->flags & LYS_SET_VALUE));
David Sedlák32488102019-07-15 17:44:10 +0200847 assert_string_equal(type.enums->dsc, "desc...");
848 assert_string_equal(type.enums->ref, "ref...");
David Sedlákd1144562019-08-06 12:36:14 +0200849 assert_string_equal(type.enums->exts->name, "myext:c-define");
850 assert_int_equal(type.enums->exts->insubstmt_index, 0);
851 assert_int_equal(type.enums->exts->insubstmt, LYEXT_SUBSTMT_SELF);
852 lysp_type_free(st->ctx, &type);
853 memset(&type, 0, sizeof type);
854
855 data = ELEMENT_WRAPPER_START
856 "<enum name=\"enum-name\"></enum>"
857 ELEMENT_WRAPPER_END;
858 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
859 assert_string_equal(type.enums->name, "enum-name");
David Sedlák32488102019-07-15 17:44:10 +0200860 lysp_type_free(st->ctx, &type);
861 memset(&type, 0, sizeof type);
862
David Sedlák43801c92019-08-05 15:58:54 +0200863 st->finished_correctly = true;
864}
865
866static void
867test_bit_elem(void **state)
868{
869 struct state *st = *state;
870 struct lysp_type type = {};
871 const char *data;
872 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200873 "<bit name=\"bit-name\">"
David Sedlák43801c92019-08-05 15:58:54 +0200874 "<if-feature name=\"feature\" />"
875 "<position value=\"55\" />"
876 "<status value=\"deprecated\" />"
877 "<description><text>desc...</text></description>"
878 "<reference><text>ref...</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +0200879 EXT_SUBELEM
David Sedlák43801c92019-08-05 15:58:54 +0200880 "</bit>"
881 ELEMENT_WRAPPER_END;
882 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200883 assert_string_equal(type.bits->name, "bit-name");
David Sedlák43801c92019-08-05 15:58:54 +0200884 assert_string_equal(*type.bits->iffeatures, "feature");
885 assert_int_equal(type.bits->value, 55);
886 assert_true((type.bits->flags & LYS_STATUS_DEPRC) && (type.bits->flags & LYS_SET_VALUE));
887 assert_string_equal(type.bits->dsc, "desc...");
888 assert_string_equal(type.bits->ref, "ref...");
David Sedlákd1144562019-08-06 12:36:14 +0200889 assert_string_equal(type.bits->exts->name, "myext:c-define");
890 assert_int_equal(type.bits->exts->insubstmt_index, 0);
891 assert_int_equal(type.bits->exts->insubstmt, LYEXT_SUBSTMT_SELF);
892 lysp_type_free(st->ctx, &type);
893 memset(&type, 0, sizeof type);
894
895 data = ELEMENT_WRAPPER_START
896 "<bit name=\"bit-name\"> </bit>"
897 ELEMENT_WRAPPER_END;
898 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
899 assert_string_equal(type.bits->name, "bit-name");
David Sedlák43801c92019-08-05 15:58:54 +0200900 lysp_type_free(st->ctx, &type);
901 memset(&type, 0, sizeof type);
902
David Sedlák32488102019-07-15 17:44:10 +0200903 st->finished_correctly = true;
904}
905
906static void
907test_meta_elem(void **state)
908{
909 struct state *st = *state;
910 char *value = NULL;
911 const char *data;
David Sedlákd1144562019-08-06 12:36:14 +0200912 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200913
914 /* organization element */
915 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200916 "<organization><text>organization...</text>" EXT_SUBELEM EXT_SUBELEM "</organization>"
David Sedlák32488102019-07-15 17:44:10 +0200917 ELEMENT_WRAPPER_END;
David Sedlákd1144562019-08-06 12:36:14 +0200918 assert_int_equal(test_element_helper(st, &data, &value, NULL, &exts, true), LY_SUCCESS);
919 assert_string_equal(exts[0].name, "myext:c-define");
920 assert_int_equal(exts[0].insubstmt_index, 0);
921 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ORGANIZATION);
922 assert_string_equal(exts[1].name, "myext:c-define");
923 assert_int_equal(exts[1].insubstmt_index, 0);
924 assert_int_equal(exts[1].insubstmt, LYEXT_SUBSTMT_ORGANIZATION);
David Sedlák32488102019-07-15 17:44:10 +0200925 assert_string_equal(value, "organization...");
926 FREE_STRING(st->ctx, value);
David Sedlákd1144562019-08-06 12:36:14 +0200927 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +0200928 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +0200929 exts = NULL;
930
David Sedlák32488102019-07-15 17:44:10 +0200931 /* contact element */
932 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200933 "<contact><text>contact...</text>" EXT_SUBELEM "</contact>"
David Sedlák32488102019-07-15 17:44:10 +0200934 ELEMENT_WRAPPER_END;
David Sedlákd1144562019-08-06 12:36:14 +0200935 assert_int_equal(test_element_helper(st, &data, &value, NULL, &exts, true), LY_SUCCESS);
936 assert_string_equal(exts[0].name, "myext:c-define");
937 assert_int_equal(exts[0].insubstmt_index, 0);
938 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_CONTACT);
David Sedlák32488102019-07-15 17:44:10 +0200939 assert_string_equal(value, "contact...");
940 FREE_STRING(st->ctx, value);
David Sedlákd1144562019-08-06 12:36:14 +0200941 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
942 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200943 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +0200944
David Sedlák32488102019-07-15 17:44:10 +0200945 /* description element */
946 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200947 "<description><text>description...</text>" EXT_SUBELEM "</description>"
David Sedlák32488102019-07-15 17:44:10 +0200948 ELEMENT_WRAPPER_END;
David Sedlákd1144562019-08-06 12:36:14 +0200949 assert_int_equal(test_element_helper(st, &data, &value, NULL, &exts, true), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +0200950 assert_string_equal(value, "description...");
David Sedlákd1144562019-08-06 12:36:14 +0200951 assert_string_equal(exts[0].name, "myext:c-define");
952 assert_int_equal(exts[0].insubstmt_index, 0);
953 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_DESCRIPTION);
David Sedlák32488102019-07-15 17:44:10 +0200954 FREE_STRING(st->ctx, value);
955 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +0200956 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
957 exts = NULL;
958
David Sedlák32488102019-07-15 17:44:10 +0200959 /* reference element */
960 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200961 "<reference><text>reference...</text>" EXT_SUBELEM "</reference>"
David Sedlák32488102019-07-15 17:44:10 +0200962 ELEMENT_WRAPPER_END;
David Sedlákd1144562019-08-06 12:36:14 +0200963 assert_int_equal(test_element_helper(st, &data, &value, NULL, &exts, true), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +0200964 assert_string_equal(value, "reference...");
David Sedlákd1144562019-08-06 12:36:14 +0200965 assert_string_equal(exts[0].name, "myext:c-define");
966 assert_int_equal(exts[0].insubstmt_index, 0);
967 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_REFERENCE);
David Sedlák32488102019-07-15 17:44:10 +0200968 FREE_STRING(st->ctx, value);
969 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +0200970 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
971 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200972
David Sedlákdf2a9732019-08-07 13:23:16 +0200973 /* reference element */
974 data = ELEMENT_WRAPPER_START
975 "<reference invalid=\"text\"><text>reference...</text>""</reference>"
976 ELEMENT_WRAPPER_END;
977 assert_int_equal(test_element_helper(st, &data, &value, NULL, &exts, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200978 logbuf_assert("Unexpected attribute \"invalid\" of \"reference\" element. Line number 1.");
David Sedlákdf2a9732019-08-07 13:23:16 +0200979 FREE_STRING(st->ctx, value);
980 value = NULL;
981 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
982 exts = NULL;
983
David Sedlák32488102019-07-15 17:44:10 +0200984 /* missing text subelement */
985 data = ELEMENT_WRAPPER_START
986 "<reference>reference...</reference>"
987 ELEMENT_WRAPPER_END;
David Sedlákd1144562019-08-06 12:36:14 +0200988 assert_int_equal(test_element_helper(st, &data, &value, NULL, &exts, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200989 logbuf_assert("Missing mandatory sub-element \"text\" of \"reference\" element. Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +0200990
David Sedlákd1144562019-08-06 12:36:14 +0200991 /* reference element */
992 data = ELEMENT_WRAPPER_START
993 "<reference>" EXT_SUBELEM "<text>reference...</text></reference>"
994 ELEMENT_WRAPPER_END;
995 assert_int_equal(test_element_helper(st, &data, &value, NULL, &exts, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200996 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 +0200997 FREE_STRING(st->ctx, value);
998 value = NULL;
999 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1000 exts = NULL;
1001
David Sedlák32488102019-07-15 17:44:10 +02001002 st->finished_correctly = true;
1003}
1004
1005static void
1006test_import_elem(void **state)
1007{
1008 struct state *st = *state;
1009 const char *data;
David Sedlák298ff6d2019-07-26 14:29:03 +02001010 struct lysp_import *imports = NULL;
1011 struct import_meta imp_meta = {"prefix", &imports};
David Sedlák32488102019-07-15 17:44:10 +02001012
1013 /* max subelems */
1014 data = ELEMENT_WRAPPER_START
1015 "<import module=\"a\">"
David Sedlákd1144562019-08-06 12:36:14 +02001016 EXT_SUBELEM
David Sedlák32488102019-07-15 17:44:10 +02001017 "<prefix value=\"a_mod\"/>"
1018 "<revision-date date=\"2015-01-01\"></revision-date>"
1019 "<description><text>import description</text></description>"
1020 "<reference><text>import reference</text></reference>"
1021 "</import>"
1022 ELEMENT_WRAPPER_END;
David Sedlák298ff6d2019-07-26 14:29:03 +02001023 assert_int_equal(test_element_helper(st, &data, &imp_meta, NULL, NULL, true), LY_SUCCESS);
1024 assert_string_equal(imports->name, "a");
1025 assert_string_equal(imports->prefix, "a_mod");
1026 assert_string_equal(imports->rev, "2015-01-01");
1027 assert_string_equal(imports->dsc, "import description");
1028 assert_string_equal(imports->ref, "import reference");
David Sedlákd1144562019-08-06 12:36:14 +02001029 assert_string_equal(imports->exts->name, "myext:c-define");
1030 assert_int_equal(imports->exts->insubstmt, LYEXT_SUBSTMT_SELF);
1031 assert_int_equal(imports->exts->insubstmt_index, 0);
David Sedlák298ff6d2019-07-26 14:29:03 +02001032 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1033 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001034
1035 /* min subelems */
David Sedlák32488102019-07-15 17:44:10 +02001036 data = ELEMENT_WRAPPER_START
1037 "<import module=\"a\">"
1038 "<prefix value=\"a_mod\"/>"
1039 "</import>"
1040 ELEMENT_WRAPPER_END;
David Sedlák298ff6d2019-07-26 14:29:03 +02001041 assert_int_equal(test_element_helper(st, &data, &imp_meta, NULL, NULL, true), LY_SUCCESS);
1042 assert_string_equal(imports->prefix, "a_mod");
1043 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1044 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001045
1046 /* invalid (missing prefix) */
David Sedlák298ff6d2019-07-26 14:29:03 +02001047 data = ELEMENT_WRAPPER_START "<import module=\"a\"></import>" ELEMENT_WRAPPER_END;
1048 assert_int_equal(test_element_helper(st, &data, &imp_meta, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001049 logbuf_assert("Missing mandatory sub-element \"prefix\" of \"import\" element. Line number 1.");
David Sedlák298ff6d2019-07-26 14:29:03 +02001050 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1051 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001052
1053 /* invalid reused prefix */
David Sedlák32488102019-07-15 17:44:10 +02001054 data = ELEMENT_WRAPPER_START
1055 "<import module=\"a\">"
David Sedlák298ff6d2019-07-26 14:29:03 +02001056 "<prefix value=\"prefix\"/>"
David Sedlák32488102019-07-15 17:44:10 +02001057 "</import>"
1058 "<import module=\"a\">"
David Sedlák298ff6d2019-07-26 14:29:03 +02001059 "<prefix value=\"prefix\"/>"
David Sedlák32488102019-07-15 17:44:10 +02001060 "</import>"
1061 ELEMENT_WRAPPER_END;
David Sedlák298ff6d2019-07-26 14:29:03 +02001062 assert_int_equal(test_element_helper(st, &data, &imp_meta, NULL, NULL, false), LY_EVALID);
1063 logbuf_assert("Prefix \"prefix\" already used as module prefix. Line number 1.");
1064 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1065 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001066
1067 st->finished_correctly = true;
1068}
1069
1070static void
1071test_status_elem(void **state)
1072{
1073 struct state *st = *state;
1074 const char *data;
1075 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001076 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001077
1078 /* test valid values */
1079 data = ELEMENT_WRAPPER_START "<status value=\"current\" />" ELEMENT_WRAPPER_END;
1080 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001081 assert_true(flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +02001082
1083 data = ELEMENT_WRAPPER_START "<status value=\"deprecated\" />" ELEMENT_WRAPPER_END;
1084 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001085 assert_true(flags & LYS_STATUS_DEPRC);
David Sedlák32488102019-07-15 17:44:10 +02001086
David Sedlákd1144562019-08-06 12:36:14 +02001087 data = ELEMENT_WRAPPER_START "<status value=\"obsolete\">"EXT_SUBELEM"</status>" ELEMENT_WRAPPER_END;
1088 assert_int_equal(test_element_helper(st, &data, &flags, NULL, &exts, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001089 assert_true(flags & LYS_STATUS_OBSLT);
David Sedlákd1144562019-08-06 12:36:14 +02001090 assert_string_equal(exts[0].name, "myext:c-define");
1091 assert_int_equal(exts[0].insubstmt_index, 0);
1092 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_STATUS);
1093 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1094 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001095
1096 /* test invalid value */
1097 data = ELEMENT_WRAPPER_START "<status value=\"invalid\"></status>" ELEMENT_WRAPPER_END;
1098 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001099 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 +02001100 st->finished_correctly = true;
1101}
1102
1103static void
1104test_ext_elem(void **state)
1105{
1106 struct state *st = *state;
1107 const char *data;
1108 struct lysp_ext *ext = NULL;
1109
1110 /* max subelems */
1111 data = ELEMENT_WRAPPER_START
1112 "<extension name=\"ext_name\">"
1113 "<argument name=\"arg\"></argument>"
1114 "<status value=\"current\"/>"
1115 "<description><text>ext_desc</text></description>"
1116 "<reference><text>ext_ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001117 EXT_SUBELEM
David Sedlák32488102019-07-15 17:44:10 +02001118 "</extension>"
1119 ELEMENT_WRAPPER_END;
1120 assert_int_equal(test_element_helper(st, &data, &ext, NULL, NULL, true), LY_SUCCESS);
1121 assert_string_equal(ext->name, "ext_name");
1122 assert_string_equal(ext->argument, "arg");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001123 assert_true(ext->flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +02001124 assert_string_equal(ext->dsc, "ext_desc");
1125 assert_string_equal(ext->ref, "ext_ref");
David Sedlákd1144562019-08-06 12:36:14 +02001126 assert_string_equal(ext->exts[0].name, "myext:c-define");
1127 assert_int_equal(ext->exts[0].insubstmt_index, 0);
1128 assert_int_equal(ext->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák32488102019-07-15 17:44:10 +02001129 lysp_ext_free(st->ctx, ext);
1130 LY_ARRAY_FREE(ext);
1131 ext = NULL;
1132
1133 /* min subelems */
1134 data = ELEMENT_WRAPPER_START "<extension name=\"ext_name\"></extension>" ELEMENT_WRAPPER_END;
1135 assert_int_equal(test_element_helper(st, &data, &ext, NULL, NULL, true), LY_SUCCESS);
1136 assert_string_equal(ext->name, "ext_name");
1137 lysp_ext_free(st->ctx, ext);
1138 LY_ARRAY_FREE(ext);
1139 ext = NULL;
1140
1141 st->finished_correctly = true;
1142}
1143
1144static void
1145test_yin_element_elem(void **state)
1146{
1147 struct state *st = *state;
1148 const char *data;
1149 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001150 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001151
1152 data = ELEMENT_WRAPPER_START "<yin-element value=\"true\" />" ELEMENT_WRAPPER_END;
1153 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001154 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +02001155
David Sedlákd1144562019-08-06 12:36:14 +02001156 data = ELEMENT_WRAPPER_START "<yin-element value=\"false\">" EXT_SUBELEM "</yin-element>" ELEMENT_WRAPPER_END;
1157 assert_int_equal(test_element_helper(st, &data, &flags, NULL, &exts, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001158 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlákd1144562019-08-06 12:36:14 +02001159 assert_string_equal(exts[0].name, "myext:c-define");
1160 assert_int_equal(exts[0].insubstmt_index, 0);
1161 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_YINELEM);
1162 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001163
1164 data = ELEMENT_WRAPPER_START "<yin-element value=\"invalid\" />" ELEMENT_WRAPPER_END;
1165 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001166 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák26ea1432019-08-14 13:42:23 +02001167 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 +02001168 st->finished_correctly = true;
1169}
1170
1171static void
1172test_yangversion_elem(void **state)
1173{
1174 struct state *st = *state;
1175 const char *data;
1176 uint8_t version = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001177 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001178
1179 /* valid values */
1180 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.0\" />" ELEMENT_WRAPPER_END;
1181 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001182 assert_true(version & LYS_VERSION_1_0);
David Sedlák32488102019-07-15 17:44:10 +02001183 assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_0);
1184
David Sedlákd1144562019-08-06 12:36:14 +02001185 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.1\">" EXT_SUBELEM "</yang-version>" ELEMENT_WRAPPER_END;
1186 assert_int_equal(test_element_helper(st, &data, &version, NULL, &exts, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001187 assert_true(version & LYS_VERSION_1_1);
David Sedlák32488102019-07-15 17:44:10 +02001188 assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_1);
David Sedlákd1144562019-08-06 12:36:14 +02001189 assert_string_equal(exts[0].name, "myext:c-define");
1190 assert_int_equal(exts[0].insubstmt_index, 0);
1191 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_VERSION);
1192 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001193
1194 /* invalid value */
1195 data = ELEMENT_WRAPPER_START "<yang-version value=\"version\" />" ELEMENT_WRAPPER_END;
1196 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, false), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001197 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 +02001198
1199 st->finished_correctly = true;
1200}
1201
1202static void
1203test_mandatory_elem(void **state)
1204{
1205 struct state *st = *state;
1206 const char *data;
1207 uint16_t man = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001208 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001209
1210 /* valid values */
1211 data = ELEMENT_WRAPPER_START "<mandatory value=\"true\" />" ELEMENT_WRAPPER_END;
1212 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, true), LY_SUCCESS);
1213 assert_int_equal(man, LYS_MAND_TRUE);
1214 man = 0;
1215
David Sedlákd1144562019-08-06 12:36:14 +02001216 data = ELEMENT_WRAPPER_START "<mandatory value=\"false\">" EXT_SUBELEM "</mandatory>" ELEMENT_WRAPPER_END;
1217 assert_int_equal(test_element_helper(st, &data, &man, NULL, &exts, true), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001218 assert_int_equal(man, LYS_MAND_FALSE);
David Sedlákd1144562019-08-06 12:36:14 +02001219 assert_string_equal(exts[0].name, "myext:c-define");
1220 assert_int_equal(exts[0].insubstmt_index, 0);
1221 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_MANDATORY);
1222 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001223
1224 data = ELEMENT_WRAPPER_START "<mandatory value=\"invalid\" />" ELEMENT_WRAPPER_END;
1225 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, false), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001226 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 +02001227
1228 st->finished_correctly = true;
1229}
1230
David Sedlák8e7bda82019-07-16 17:57:50 +02001231static void
1232test_argument_elem(void **state)
1233{
1234 struct state *st = *state;
1235 const char *data;
1236 uint16_t flags = 0;
1237 const char *arg;
1238 struct yin_argument_meta arg_meta = {&flags, &arg};
David Sedlákd1144562019-08-06 12:36:14 +02001239 struct lysp_ext_instance *exts = NULL;
1240
David Sedlák8e7bda82019-07-16 17:57:50 +02001241 /* max subelems */
1242 data = ELEMENT_WRAPPER_START
1243 "<argument name=\"arg-name\">"
1244 "<yin-element value=\"true\" />"
David Sedlákd1144562019-08-06 12:36:14 +02001245 EXT_SUBELEM
David Sedlák8e7bda82019-07-16 17:57:50 +02001246 "</argument>"
1247 ELEMENT_WRAPPER_END;
David Sedlákd1144562019-08-06 12:36:14 +02001248 assert_int_equal(test_element_helper(st, &data, &arg_meta, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001249 assert_string_equal(arg, "arg-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001250 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlákd1144562019-08-06 12:36:14 +02001251 assert_string_equal(exts[0].name, "myext:c-define");
1252 assert_int_equal(exts[0].insubstmt_index, 0);
1253 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ARGUMENT);
1254 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1255 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001256 flags = 0;
1257 FREE_STRING(st->ctx, arg);
1258 arg = NULL;
1259
1260 /* min subelems */
1261 data = ELEMENT_WRAPPER_START
1262 "<argument name=\"arg\">"
1263 "</argument>"
1264 ELEMENT_WRAPPER_END;
1265 assert_int_equal(test_element_helper(st, &data, &arg_meta, NULL, NULL, true), LY_SUCCESS);
1266 assert_string_equal(arg, "arg");
1267 assert_true(flags == 0);
1268 FREE_STRING(st->ctx, arg);
1269
1270 st->finished_correctly = true;
1271}
1272
1273static void
1274test_base_elem(void **state)
1275{
1276 struct state *st = *state;
1277 const char *data;
1278 const char **bases = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001279 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001280 struct lysp_type type = {};
1281
1282 /* as identity subelement */
1283 data = "<identity xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlákd1144562019-08-06 12:36:14 +02001284 "<base name=\"base-name\">"
1285 EXT_SUBELEM
1286 "</base>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001287 "</identity>";
David Sedlákd1144562019-08-06 12:36:14 +02001288 assert_int_equal(test_element_helper(st, &data, &bases, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001289 assert_string_equal(*bases, "base-name");
David Sedlákd1144562019-08-06 12:36:14 +02001290 assert_string_equal(exts[0].name, "myext:c-define");
1291 assert_int_equal(exts[0].insubstmt_index, 0);
1292 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_BASE);
1293 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1294 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001295 FREE_STRING(st->ctx, *bases);
1296 LY_ARRAY_FREE(bases);
1297
1298 /* as type subelement */
1299 data = "<type xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlákd1144562019-08-06 12:36:14 +02001300 "<base name=\"base-name\">"
1301 EXT_SUBELEM
1302 "</base>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001303 "</type>";
David Sedlákd1144562019-08-06 12:36:14 +02001304 assert_int_equal(test_element_helper(st, &data, &type, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001305 assert_string_equal(*type.bases, "base-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001306 assert_true(type.flags & LYS_SET_BASE);
David Sedlákd1144562019-08-06 12:36:14 +02001307 assert_string_equal(exts[0].name, "myext:c-define");
1308 assert_int_equal(exts[0].insubstmt_index, 0);
1309 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_BASE);
1310 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1311 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001312 FREE_STRING(st->ctx, *type.bases);
1313 LY_ARRAY_FREE(type.bases);
1314
1315 st->finished_correctly = true;
1316}
1317
1318static void
1319test_belongsto_elem(void **state)
1320{
1321 struct state *st = *state;
1322 const char *data;
1323 struct lysp_submodule submod;
David Sedlákd1144562019-08-06 12:36:14 +02001324 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001325
1326 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +02001327 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/>"EXT_SUBELEM"</belongs-to>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001328 ELEMENT_WRAPPER_END;
David Sedlákd1144562019-08-06 12:36:14 +02001329 assert_int_equal(test_element_helper(st, &data, &submod, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001330 assert_string_equal(submod.belongsto, "module-name");
1331 assert_string_equal(submod.prefix, "pref");
David Sedlákd1144562019-08-06 12:36:14 +02001332 assert_string_equal(exts[0].name, "myext:c-define");
1333 assert_int_equal(exts[0].insubstmt_index, 0);
1334 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_BELONGSTO);
1335 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1336 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001337 FREE_STRING(st->ctx, submod.belongsto);
1338 FREE_STRING(st->ctx, submod.prefix);
1339
1340 data = ELEMENT_WRAPPER_START "<belongs-to module=\"module-name\"></belongs-to>" ELEMENT_WRAPPER_END;
1341 assert_int_equal(test_element_helper(st, &data, &submod, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001342 logbuf_assert("Missing mandatory sub-element \"prefix\" of \"belongs-to\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001343 FREE_STRING(st->ctx, submod.belongsto);
1344
1345 st->finished_correctly = true;
1346}
1347
1348static void
1349test_config_elem(void **state)
1350{
1351 struct state *st = *state;
1352 const char *data;
1353 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001354 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001355
David Sedlákd1144562019-08-06 12:36:14 +02001356 data = ELEMENT_WRAPPER_START "<config value=\"true\">" EXT_SUBELEM "</config>" ELEMENT_WRAPPER_END;
1357 assert_int_equal(test_element_helper(st, &data, &flags, NULL, &exts, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001358 assert_true(flags & LYS_CONFIG_W);
David Sedlákd1144562019-08-06 12:36:14 +02001359 assert_string_equal(exts[0].name, "myext:c-define");
1360 assert_int_equal(exts[0].insubstmt_index, 0);
1361 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_CONFIG);
1362 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1363 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001364 flags = 0;
1365
1366 data = ELEMENT_WRAPPER_START "<config value=\"false\"/>" ELEMENT_WRAPPER_END;
1367 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001368 assert_true(flags & LYS_CONFIG_R);
David Sedlák8e7bda82019-07-16 17:57:50 +02001369 flags = 0;
1370
1371 data = ELEMENT_WRAPPER_START "<config value=\"invalid\"/>" ELEMENT_WRAPPER_END;
1372 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001373 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 +02001374
1375 st->finished_correctly = true;
1376}
1377
1378static void
1379test_default_elem(void **state)
1380{
1381 struct state *st = *state;
1382 const char *data;
1383 const char *val = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001384 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001385
David Sedlákd1144562019-08-06 12:36:14 +02001386 data = ELEMENT_WRAPPER_START "<default value=\"defaul-value\">"EXT_SUBELEM"</default>" ELEMENT_WRAPPER_END;
1387 assert_int_equal(test_element_helper(st, &data, &val, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001388 assert_string_equal(val, "defaul-value");
David Sedlákd1144562019-08-06 12:36:14 +02001389 assert_string_equal(exts[0].name, "myext:c-define");
1390 assert_int_equal(exts[0].insubstmt_index, 0);
1391 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_DEFAULT);
1392 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1393 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001394 FREE_STRING(st->ctx, val);
1395 val = NULL;
1396
1397 data = ELEMENT_WRAPPER_START "<default/>" ELEMENT_WRAPPER_END;
1398 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1399 logbuf_assert("Missing mandatory attribute value of default element. Line number 1.");
1400
1401 st->finished_correctly = true;
1402}
1403
1404static void
1405test_err_app_tag_elem(void **state)
1406{
1407 struct state *st = *state;
1408 const char *data;
1409 const char *val = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001410 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001411
David Sedlákd1144562019-08-06 12:36:14 +02001412 data = ELEMENT_WRAPPER_START "<error-app-tag value=\"val\">"EXT_SUBELEM"</error-app-tag>" ELEMENT_WRAPPER_END;
1413 assert_int_equal(test_element_helper(st, &data, &val, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001414 assert_string_equal(val, "val");
David Sedlákd1144562019-08-06 12:36:14 +02001415 assert_string_equal(exts[0].name, "myext:c-define");
1416 assert_int_equal(exts[0].insubstmt_index, 0);
1417 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ERRTAG);
1418 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1419 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001420 FREE_STRING(st->ctx, val);
1421 val = NULL;
1422
1423 data = ELEMENT_WRAPPER_START "<error-app-tag/>" ELEMENT_WRAPPER_END;
1424 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1425 logbuf_assert("Missing mandatory attribute value of error-app-tag element. Line number 1.");
1426
1427 st->finished_correctly = true;
1428}
1429
1430static void
1431test_err_msg_elem(void **state)
1432{
1433 struct state *st = *state;
1434 const char *data;
1435 const char *val = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001436 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001437
David Sedlákd1144562019-08-06 12:36:14 +02001438 data = ELEMENT_WRAPPER_START "<error-message><value>val</value>"EXT_SUBELEM"</error-message>" ELEMENT_WRAPPER_END;
1439 assert_int_equal(test_element_helper(st, &data, &val, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001440 assert_string_equal(val, "val");
David Sedlákd1144562019-08-06 12:36:14 +02001441 assert_string_equal(exts[0].name, "myext:c-define");
1442 assert_int_equal(exts[0].insubstmt_index, 0);
1443 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ERRMSG);
1444 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1445 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001446 FREE_STRING(st->ctx, val);
1447
1448 data = ELEMENT_WRAPPER_START "<error-message></error-message>" ELEMENT_WRAPPER_END;
1449 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001450 logbuf_assert("Missing mandatory sub-element \"value\" of \"error-message\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001451
David Sedlákdf2a9732019-08-07 13:23:16 +02001452 data = ELEMENT_WRAPPER_START "<error-message invalid=\"text\"/>" ELEMENT_WRAPPER_END;
1453 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001454 logbuf_assert("Unexpected attribute \"invalid\" of \"error-message\" element. Line number 1.");
David Sedlákdf2a9732019-08-07 13:23:16 +02001455
David Sedlák8e7bda82019-07-16 17:57:50 +02001456 st->finished_correctly = true;
1457}
1458
1459static void
1460test_fracdigits_elem(void **state)
1461{
1462 struct state *st = *state;
1463 const char *data;
1464 struct lysp_type type = {};
1465
1466 /* valid value */
David Sedlákd1144562019-08-06 12:36:14 +02001467 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"10\">"EXT_SUBELEM"</fraction-digits>" ELEMENT_WRAPPER_END;
David Sedlák8e7bda82019-07-16 17:57:50 +02001468 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001469 assert_string_equal(type.exts[0].name, "myext:c-define");
1470 assert_int_equal(type.exts[0].insubstmt_index, 0);
1471 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_FRACDIGITS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001472 assert_int_equal(type.fraction_digits, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001473 assert_true(type.flags & LYS_SET_FRDIGITS);
David Sedlákd1144562019-08-06 12:36:14 +02001474 FREE_ARRAY(st->ctx, type.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001475
1476 /* invalid values */
1477 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"-1\"></fraction-digits>" ELEMENT_WRAPPER_END;
1478 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001479 logbuf_assert("Invalid value \"-1\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001480
1481 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"02\"></fraction-digits>" ELEMENT_WRAPPER_END;
1482 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001483 logbuf_assert("Invalid value \"02\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001484
1485 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"1p\"></fraction-digits>" ELEMENT_WRAPPER_END;
1486 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001487 logbuf_assert("Invalid value \"1p\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001488
1489 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"19\"></fraction-digits>" ELEMENT_WRAPPER_END;
1490 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001491 logbuf_assert("Invalid value \"19\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001492
1493 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"999999999999999999\"></fraction-digits>" ELEMENT_WRAPPER_END;
1494 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001495 logbuf_assert("Invalid value \"999999999999999999\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001496
1497 st->finished_correctly = true;
1498}
1499
1500static void
1501test_iffeature_elem(void **state)
1502{
1503 struct state *st = *state;
1504 const char *data;
1505 const char **iffeatures = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001506 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001507
David Sedlákd1144562019-08-06 12:36:14 +02001508 data = ELEMENT_WRAPPER_START "<if-feature name=\"local-storage\">"EXT_SUBELEM"</if-feature>" ELEMENT_WRAPPER_END;
1509 assert_int_equal(test_element_helper(st, &data, &iffeatures, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001510 assert_string_equal(*iffeatures, "local-storage");
David Sedlákd1144562019-08-06 12:36:14 +02001511 assert_string_equal(exts[0].name, "myext:c-define");
1512 assert_int_equal(exts[0].insubstmt_index, 0);
1513 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_IFFEATURE);
1514 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1515 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001516 FREE_STRING(st->ctx, *iffeatures);
1517 LY_ARRAY_FREE(iffeatures);
1518 iffeatures = NULL;
1519
1520 data = ELEMENT_WRAPPER_START "<if-feature/>" ELEMENT_WRAPPER_END;
1521 assert_int_equal(test_element_helper(st, &data, &iffeatures, NULL, NULL, false), LY_EVALID);
1522 logbuf_assert("Missing mandatory attribute name of if-feature element. Line number 1.");
1523 LY_ARRAY_FREE(iffeatures);
1524 iffeatures = NULL;
1525
1526 st->finished_correctly = true;
1527}
1528
1529static void
1530test_length_elem(void **state)
1531{
1532 struct state *st = *state;
1533 const char *data;
1534 struct lysp_type type = {};
1535
1536 /* max subelems */
1537 data = ELEMENT_WRAPPER_START
1538 "<length value=\"length-str\">"
1539 "<error-message><value>err-msg</value></error-message>"
1540 "<error-app-tag value=\"err-app-tag\"/>"
1541 "<description><text>desc</text></description>"
1542 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001543 EXT_SUBELEM
David Sedlák8e7bda82019-07-16 17:57:50 +02001544 "</length>"
1545 ELEMENT_WRAPPER_END;
1546 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1547 assert_string_equal(type.length->arg, "length-str");
1548 assert_string_equal(type.length->emsg, "err-msg");
1549 assert_string_equal(type.length->eapptag, "err-app-tag");
1550 assert_string_equal(type.length->dsc, "desc");
1551 assert_string_equal(type.length->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001552 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlákd1144562019-08-06 12:36:14 +02001553 assert_string_equal(type.length->exts[0].name, "myext:c-define");
1554 assert_int_equal(type.length->exts[0].insubstmt_index, 0);
1555 assert_int_equal(type.length->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8e7bda82019-07-16 17:57:50 +02001556 lysp_type_free(st->ctx, &type);
1557 memset(&type, 0, sizeof(type));
1558
1559 /* min subelems */
1560 data = ELEMENT_WRAPPER_START
1561 "<length value=\"length-str\">"
1562 "</length>"
1563 ELEMENT_WRAPPER_END;
1564 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1565 assert_string_equal(type.length->arg, "length-str");
1566 lysp_type_free(st->ctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001567 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001568 memset(&type, 0, sizeof(type));
1569
1570 data = ELEMENT_WRAPPER_START "<length></length>" ELEMENT_WRAPPER_END;
1571 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1572 logbuf_assert("Missing mandatory attribute value of length element. Line number 1.");
1573 lysp_type_free(st->ctx, &type);
1574 memset(&type, 0, sizeof(type));
1575
1576 st->finished_correctly = true;
1577}
1578
1579static void
1580test_modifier_elem(void **state)
1581{
1582 struct state *st = *state;
1583 const char *data;
1584 const char *pat = lydict_insert(st->ctx, "\006pattern", 8);
David Sedlákd1144562019-08-06 12:36:14 +02001585 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001586
David Sedlákd1144562019-08-06 12:36:14 +02001587 data = ELEMENT_WRAPPER_START "<modifier value=\"invert-match\">" EXT_SUBELEM "</modifier>" ELEMENT_WRAPPER_END;
1588 assert_int_equal(test_element_helper(st, &data, &pat, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001589 assert_string_equal(pat, "\x015pattern");
David Sedlákd1144562019-08-06 12:36:14 +02001590 assert_string_equal(exts[0].name, "myext:c-define");
1591 assert_int_equal(exts[0].insubstmt_index, 0);
1592 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_MODIFIER);
1593 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1594 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001595 FREE_STRING(st->ctx, pat);
1596
1597 pat = lydict_insert(st->ctx, "\006pattern", 8);
1598 data = ELEMENT_WRAPPER_START "<modifier value=\"invert\" />" ELEMENT_WRAPPER_END;
1599 assert_int_equal(test_element_helper(st, &data, &pat, NULL, NULL, false), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001600 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 +02001601 FREE_STRING(st->ctx, pat);
1602
1603 st->finished_correctly = true;
1604}
1605
1606static void
1607test_namespace_elem(void **state)
1608{
1609 struct state *st = *state;
1610 const char *data;
1611 const char *ns;
David Sedlákd1144562019-08-06 12:36:14 +02001612 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001613
David Sedlákd1144562019-08-06 12:36:14 +02001614 data = ELEMENT_WRAPPER_START "<namespace uri=\"ns\">" EXT_SUBELEM "</namespace>" ELEMENT_WRAPPER_END;
1615 assert_int_equal(test_element_helper(st, &data, &ns, NULL, &exts, true), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001616 assert_string_equal(ns, "ns");
David Sedlákd1144562019-08-06 12:36:14 +02001617 assert_string_equal(exts[0].name, "myext:c-define");
1618 assert_int_equal(exts[0].insubstmt_index, 0);
1619 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_NAMESPACE);
1620 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1621 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001622 FREE_STRING(st->ctx, ns);
1623
1624 data = ELEMENT_WRAPPER_START "<namespace/>" ELEMENT_WRAPPER_END;
1625 assert_int_equal(test_element_helper(st, &data, &ns, NULL, NULL, false), LY_EVALID);
1626 logbuf_assert("Missing mandatory attribute uri of namespace element. Line number 1.");
1627
1628 st->finished_correctly = true;
1629}
1630
1631static void
1632test_path_elem(void **state)
1633{
1634 struct state *st = *state;
1635 const char *data;
1636 struct lysp_type type = {};
1637
David Sedlák169cc522019-08-15 13:23:45 +02001638 data = ELEMENT_WRAPPER_START "<path value=\"p&amp;th-val\">" EXT_SUBELEM "</path>" ELEMENT_WRAPPER_END;
David Sedlák8e7bda82019-07-16 17:57:50 +02001639 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
David Sedlák169cc522019-08-15 13:23:45 +02001640 assert_string_equal("p&th-val", type.path);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001641 assert_true(type.flags & LYS_SET_PATH);
David Sedlákd1144562019-08-06 12:36:14 +02001642 assert_string_equal(type.exts[0].name, "myext:c-define");
1643 assert_int_equal(type.exts[0].insubstmt_index, 0);
1644 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_PATH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001645 lysp_type_free(st->ctx, &type);
1646
1647 st->finished_correctly = true;
1648}
1649
1650static void
1651test_pattern_elem(void **state)
1652{
1653 struct state *st = *state;
1654 const char *data;
1655 struct lysp_type type = {};
1656
1657 /* max subelems */
1658 data = ELEMENT_WRAPPER_START
1659 "<pattern value=\"super_pattern\">"
1660 "<modifier value=\"invert-match\"/>"
1661 "<error-message><value>err-msg-value</value></error-message>"
1662 "<error-app-tag value=\"err-app-tag-value\"/>"
David Sedlák169cc522019-08-15 13:23:45 +02001663 "<description><text>&quot;pattern-desc&quot;</text></description>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001664 "<reference><text>pattern-ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001665 EXT_SUBELEM
David Sedlák8e7bda82019-07-16 17:57:50 +02001666 "</pattern>"
1667 ELEMENT_WRAPPER_END;
1668 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001669 assert_true(type.flags & LYS_SET_PATTERN);
David Sedlák8e7bda82019-07-16 17:57:50 +02001670 assert_string_equal(type.patterns->arg, "\x015super_pattern");
David Sedlák169cc522019-08-15 13:23:45 +02001671 assert_string_equal(type.patterns->dsc, "\"pattern-desc\"");
David Sedlák8e7bda82019-07-16 17:57:50 +02001672 assert_string_equal(type.patterns->eapptag, "err-app-tag-value");
1673 assert_string_equal(type.patterns->emsg, "err-msg-value");
David Sedlák8e7bda82019-07-16 17:57:50 +02001674 assert_string_equal(type.patterns->ref, "pattern-ref");
David Sedlákd1144562019-08-06 12:36:14 +02001675 assert_string_equal(type.patterns->exts[0].name, "myext:c-define");
1676 assert_int_equal(type.patterns->exts[0].insubstmt_index, 0);
1677 assert_int_equal(type.patterns->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8e7bda82019-07-16 17:57:50 +02001678 lysp_type_free(st->ctx, &type);
1679 memset(&type, 0, sizeof(type));
1680
1681 /* min subelems */
1682 data = ELEMENT_WRAPPER_START "<pattern value=\"pattern\"> </pattern>" ELEMENT_WRAPPER_END;
1683 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1684 assert_string_equal(type.patterns->arg, "\x006pattern");
1685 lysp_type_free(st->ctx, &type);
1686 memset(&type, 0, sizeof(type));
1687
1688 st->finished_correctly = true;
1689}
1690
1691static void
1692test_value_position_elem(void **state)
1693{
1694 struct state *st = *state;
1695 const char *data;
1696 struct lysp_type_enum en = {};
1697
1698 /* valid values */
David Sedlákd1144562019-08-06 12:36:14 +02001699 data = ELEMENT_WRAPPER_START "<value value=\"55\">" EXT_SUBELEM "</value>" ELEMENT_WRAPPER_END;
David Sedlák8e7bda82019-07-16 17:57:50 +02001700 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1701 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001702 assert_true(en.flags & LYS_SET_VALUE);
David Sedlákd1144562019-08-06 12:36:14 +02001703 assert_string_equal(en.exts[0].name, "myext:c-define");
1704 assert_int_equal(en.exts[0].insubstmt_index, 0);
1705 assert_int_equal(en.exts[0].insubstmt, LYEXT_SUBSTMT_VALUE);
1706 FREE_ARRAY(st->ctx, en.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001707 memset(&en, 0, sizeof(en));
1708
1709 data = ELEMENT_WRAPPER_START "<value value=\"-55\"/>" ELEMENT_WRAPPER_END;
1710 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1711 assert_int_equal(en.value, -55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001712 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001713 memset(&en, 0, sizeof(en));
1714
1715 data = ELEMENT_WRAPPER_START "<value value=\"0\"/>" ELEMENT_WRAPPER_END;
1716 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1717 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001718 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001719 memset(&en, 0, sizeof(en));
1720
1721 data = ELEMENT_WRAPPER_START "<value value=\"-0\"/>" ELEMENT_WRAPPER_END;
1722 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1723 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001724 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001725 memset(&en, 0, sizeof(en));
1726
1727 /* valid positions */
David Sedlák8d552d62019-08-06 15:29:05 +02001728 data = ELEMENT_WRAPPER_START "<position value=\"55\">" EXT_SUBELEM "</position>" ELEMENT_WRAPPER_END;
David Sedlák8e7bda82019-07-16 17:57:50 +02001729 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1730 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001731 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8d552d62019-08-06 15:29:05 +02001732 assert_string_equal(en.exts[0].name, "myext:c-define");
1733 assert_int_equal(en.exts[0].insubstmt_index, 0);
1734 assert_int_equal(en.exts[0].insubstmt, LYEXT_SUBSTMT_POSITION);
1735 FREE_ARRAY(st->ctx, en.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001736 memset(&en, 0, sizeof(en));
1737
1738 data = ELEMENT_WRAPPER_START "<position value=\"0\" />" ELEMENT_WRAPPER_END;
1739 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1740 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001741 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001742 memset(&en, 0, sizeof(en));
1743
1744 /* invalid values */
1745 data = ELEMENT_WRAPPER_START "<value value=\"99999999999999999999999\"/>" ELEMENT_WRAPPER_END;
1746 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001747 logbuf_assert("Invalid value \"99999999999999999999999\" of \"value\" attribute in \"value\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001748
1749 data = ELEMENT_WRAPPER_START "<value value=\"1k\"/>" ELEMENT_WRAPPER_END;
1750 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001751 logbuf_assert("Invalid value \"1k\" of \"value\" attribute in \"value\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001752
David Sedlák69f01612019-07-17 11:41:08 +02001753 data = ELEMENT_WRAPPER_START "<value value=\"\"/>" ELEMENT_WRAPPER_END;
1754 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001755 logbuf_assert("Invalid value \"\" of \"value\" attribute in \"value\" element. Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001756
David Sedlák8e7bda82019-07-16 17:57:50 +02001757 /*invalid positions */
1758 data = ELEMENT_WRAPPER_START "<position value=\"-5\"/>" ELEMENT_WRAPPER_END;
1759 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001760 logbuf_assert("Invalid value \"-5\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001761
1762 data = ELEMENT_WRAPPER_START "<position value=\"-0\"/>" ELEMENT_WRAPPER_END;
1763 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001764 logbuf_assert("Invalid value \"-0\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001765
1766 data = ELEMENT_WRAPPER_START "<position value=\"99999999999999999999\"/>" ELEMENT_WRAPPER_END;
1767 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001768 logbuf_assert("Invalid value \"99999999999999999999\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001769
David Sedlák69f01612019-07-17 11:41:08 +02001770 data = ELEMENT_WRAPPER_START "<position value=\"\"/>" ELEMENT_WRAPPER_END;
1771 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001772 logbuf_assert("Invalid value \"\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001773
1774 st->finished_correctly = true;
1775}
1776
1777static void
1778test_prefix_elem(void **state)
1779{
1780 struct state *st = *state;
1781 const char *data;
1782 const char *value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001783 struct lysp_ext_instance *exts = NULL;
1784
1785 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\">" EXT_SUBELEM "</prefix>" ELEMENT_WRAPPER_END;
1786 assert_int_equal(test_element_helper(st, &data, &value, NULL, &exts, true), LY_SUCCESS);
1787 assert_string_equal(value, "pref");
1788 assert_string_equal(exts[0].name, "myext:c-define");
1789 assert_int_equal(exts[0].insubstmt_index, 0);
1790 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_PREFIX);
1791 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1792 exts = NULL;
1793 FREE_STRING(st->ctx, value);
David Sedlák69f01612019-07-17 11:41:08 +02001794
1795 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\"/>" ELEMENT_WRAPPER_END;
1796 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
1797 assert_string_equal(value, "pref");
1798 FREE_STRING(st->ctx, value);
1799
1800 st->finished_correctly = true;
1801}
1802
1803static void
1804test_range_elem(void **state)
1805{
1806 struct state *st = *state;
1807 const char *data;
1808 struct lysp_type type = {};
1809
1810 /* max subelems */
1811 data = ELEMENT_WRAPPER_START
1812 "<range value=\"range-str\">"
1813 "<error-message><value>err-msg</value></error-message>"
1814 "<error-app-tag value=\"err-app-tag\" />"
1815 "<description><text>desc</text></description>"
1816 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001817 EXT_SUBELEM
David Sedlák69f01612019-07-17 11:41:08 +02001818 "</range>"
1819 ELEMENT_WRAPPER_END;
1820 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1821 assert_string_equal(type.range->arg, "range-str");
1822 assert_string_equal(type.range->dsc, "desc");
1823 assert_string_equal(type.range->eapptag, "err-app-tag");
1824 assert_string_equal(type.range->emsg, "err-msg");
1825 assert_string_equal(type.range->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001826 assert_true(type.flags & LYS_SET_RANGE);
David Sedlákd1144562019-08-06 12:36:14 +02001827 assert_string_equal(type.range->exts[0].name, "myext:c-define");
1828 assert_int_equal(type.range->exts[0].insubstmt_index, 0);
1829 assert_int_equal(type.range->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák69f01612019-07-17 11:41:08 +02001830 lysp_type_free(st->ctx, &type);
1831 memset(&type, 0, sizeof(type));
1832
1833 /* min subelems */
1834 data = ELEMENT_WRAPPER_START "<range value=\"range-str\"/>" ELEMENT_WRAPPER_END;
1835 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1836 assert_string_equal(type.range->arg, "range-str");
1837 lysp_type_free(st->ctx, &type);
1838 memset(&type, 0, sizeof(type));
1839
1840 st->finished_correctly = true;
1841}
1842
1843static void
1844test_reqinstance_elem(void **state)
1845{
1846 struct state *st = *state;
1847 const char *data;
1848 struct lysp_type type = {};
1849
David Sedlákd1144562019-08-06 12:36:14 +02001850 data = ELEMENT_WRAPPER_START "<require-instance value=\"true\">" EXT_SUBELEM "</require-instance>" ELEMENT_WRAPPER_END;
David Sedlák69f01612019-07-17 11:41:08 +02001851 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1852 assert_int_equal(type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001853 assert_true(type.flags & LYS_SET_REQINST);
David Sedlákd1144562019-08-06 12:36:14 +02001854 assert_string_equal(type.exts[0].name, "myext:c-define");
1855 assert_int_equal(type.exts[0].insubstmt_index, 0);
1856 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_REQINSTANCE);
1857 lysp_type_free(st->ctx, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001858 memset(&type, 0, sizeof(type));
1859
1860 data = ELEMENT_WRAPPER_START "<require-instance value=\"false\"/>" ELEMENT_WRAPPER_END;
1861 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1862 assert_int_equal(type.require_instance, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001863 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001864 memset(&type, 0, sizeof(type));
1865
1866 data = ELEMENT_WRAPPER_START "<require-instance value=\"invalid\"/>" ELEMENT_WRAPPER_END;
1867 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1868 memset(&type, 0, sizeof(type));
David Sedlák26ea1432019-08-14 13:42:23 +02001869 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 +02001870
1871 st->finished_correctly = true;
1872}
1873
1874static void
1875test_revision_date_elem(void **state)
1876{
1877 struct state *st = *state;
1878 const char *data;
1879 char rev[LY_REV_SIZE];
David Sedlákd1144562019-08-06 12:36:14 +02001880 struct lysp_ext_instance *exts = NULL;
1881
1882 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\">"EXT_SUBELEM"</revision-date>" ELEMENT_WRAPPER_END;
1883 assert_int_equal(test_element_helper(st, &data, rev, NULL, &exts, true), LY_SUCCESS);
1884 assert_string_equal(rev, "2000-01-01");
1885 assert_string_equal(exts[0].name, "myext:c-define");
1886 assert_int_equal(exts[0].insubstmt_index, 0);
1887 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_REVISIONDATE);
1888 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák69f01612019-07-17 11:41:08 +02001889
1890 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\"/>" ELEMENT_WRAPPER_END;
1891 assert_int_equal(test_element_helper(st, &data, rev, NULL, NULL, true), LY_SUCCESS);
1892 assert_string_equal(rev, "2000-01-01");
1893
1894 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-50-05\"/>" ELEMENT_WRAPPER_END;
1895 assert_int_equal(test_element_helper(st, &data, rev, NULL, NULL, false), LY_EVALID);
1896 logbuf_assert("Invalid value \"2000-50-05\" of \"revision-date\". Line number 1.");
1897
1898 st->finished_correctly = true;
1899}
1900
1901static void
1902test_unique_elem(void **state)
1903{
1904 struct state *st = *state;
1905 const char *data;
1906 const char **values = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001907 struct lysp_ext_instance *exts = NULL;
1908
1909 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\">"EXT_SUBELEM"</unique>" ELEMENT_WRAPPER_END;
1910 assert_int_equal(test_element_helper(st, &data, &values, NULL, &exts, true), LY_SUCCESS);
1911 assert_string_equal(*values, "tag");
1912 assert_string_equal(exts[0].name, "myext:c-define");
1913 assert_int_equal(exts[0].insubstmt_index, 0);
1914 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_UNIQUE);
1915 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1916 FREE_STRING(st->ctx, *values);
1917 LY_ARRAY_FREE(values);
1918 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001919
1920 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\"/>" ELEMENT_WRAPPER_END;
1921 assert_int_equal(test_element_helper(st, &data, &values, NULL, NULL, true), LY_SUCCESS);
1922 assert_string_equal(*values, "tag");
1923 FREE_STRING(st->ctx, *values);
1924 LY_ARRAY_FREE(values);
David Sedlákd1144562019-08-06 12:36:14 +02001925 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001926
1927 st->finished_correctly = true;
1928}
1929
1930static void
1931test_units_elem(void **state)
1932{
1933 struct state *st = *state;
1934 const char *data;
1935 const char *values = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001936 struct lysp_ext_instance *exts = NULL;
1937
1938 data = ELEMENT_WRAPPER_START "<units name=\"name\">"EXT_SUBELEM"</units>" ELEMENT_WRAPPER_END;
1939 assert_int_equal(test_element_helper(st, &data, &values, NULL, &exts, true), LY_SUCCESS);
1940 assert_string_equal(values, "name");
1941 assert_string_equal(exts[0].name, "myext:c-define");
1942 assert_int_equal(exts[0].insubstmt_index, 0);
1943 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_UNITS);
1944 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1945 FREE_STRING(st->ctx, values);
1946 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001947
1948 data = ELEMENT_WRAPPER_START "<units name=\"name\"/>" ELEMENT_WRAPPER_END;
1949 assert_int_equal(test_element_helper(st, &data, &values, NULL, NULL, true), LY_SUCCESS);
1950 assert_string_equal(values, "name");
1951 FREE_STRING(st->ctx, values);
David Sedlákd1144562019-08-06 12:36:14 +02001952 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001953
1954 st->finished_correctly = true;
1955}
1956
1957static void
1958test_when_elem(void **state)
1959{
1960 struct state *st = *state;
1961 const char *data;
1962 struct lysp_when *when = NULL;
1963
1964 data = ELEMENT_WRAPPER_START
1965 "<when condition=\"cond\">"
1966 "<description><text>desc</text></description>"
1967 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001968 EXT_SUBELEM
David Sedlák69f01612019-07-17 11:41:08 +02001969 "</when>"
1970 ELEMENT_WRAPPER_END;
1971 assert_int_equal(test_element_helper(st, &data, &when, NULL, NULL, true), LY_SUCCESS);
1972 assert_string_equal(when->cond, "cond");
1973 assert_string_equal(when->dsc, "desc");
1974 assert_string_equal(when->ref, "ref");
David Sedlákd1144562019-08-06 12:36:14 +02001975 assert_string_equal(when->exts[0].name, "myext:c-define");
1976 assert_int_equal(when->exts[0].insubstmt_index, 0);
1977 assert_int_equal(when->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák69f01612019-07-17 11:41:08 +02001978 lysp_when_free(st->ctx, when);
1979 free(when);
1980 when = NULL;
1981
1982 data = ELEMENT_WRAPPER_START "<when condition=\"cond\" />" ELEMENT_WRAPPER_END;
1983 assert_int_equal(test_element_helper(st, &data, &when, NULL, NULL, true), LY_SUCCESS);
1984 assert_string_equal(when->cond, "cond");
1985 lysp_when_free(st->ctx, when);
1986 free(when);
1987 when = NULL;
1988
1989 st->finished_correctly = true;
1990}
1991
1992static void
1993test_yin_text_value_elem(void **state)
1994{
1995 struct state *st = *state;
1996 const char *data;
1997 const char *val;
1998
1999 data = ELEMENT_WRAPPER_START "<text>text</text>" ELEMENT_WRAPPER_END;
2000 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2001 assert_string_equal(val, "text");
2002 FREE_STRING(st->ctx, val);
2003
2004 data = "<error-message xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <value>text</value> </error-message>";
2005 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2006 assert_string_equal(val, "text");
2007 FREE_STRING(st->ctx, val);
2008
2009 data = ELEMENT_WRAPPER_START "<text></text>" ELEMENT_WRAPPER_END;
2010 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2011 assert_string_equal("", val);
2012 FREE_STRING(st->ctx, val);
2013
David Sedlák8e7bda82019-07-16 17:57:50 +02002014 st->finished_correctly = true;
2015}
David Sedlák32488102019-07-15 17:44:10 +02002016
David Sedlák374d2b32019-07-17 15:06:55 +02002017static void
2018test_type_elem(void **state)
2019{
2020 struct state *st = *state;
2021 const char *data;
2022 struct lysp_type type = {};
2023
2024 /* max subelems */
2025 data = ELEMENT_WRAPPER_START
2026 "<type name=\"type-name\">"
2027 "<base name=\"base-name\"/>"
2028 "<bit name=\"bit\"/>"
2029 "<enum name=\"enum\"/>"
2030 "<fraction-digits value=\"2\"/>"
2031 "<length value=\"length\"/>"
2032 "<path value=\"path\"/>"
2033 "<pattern value=\"pattern\"/>"
2034 "<range value=\"range\" />"
2035 "<require-instance value=\"true\"/>"
2036 "<type name=\"sub-type-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002037 EXT_SUBELEM
David Sedlák374d2b32019-07-17 15:06:55 +02002038 "</type>"
2039 ELEMENT_WRAPPER_END;
2040 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
2041 assert_string_equal(type.name, "type-name");
2042 assert_string_equal(*type.bases, "base-name");
2043 assert_string_equal(type.bits->name, "bit");
2044 assert_string_equal(type.enums->name, "enum");
2045 assert_int_equal(type.fraction_digits, 2);
2046 assert_string_equal(type.length->arg, "length");
2047 assert_string_equal(type.path, "path");
2048 assert_string_equal(type.patterns->arg, "\006pattern");
2049 assert_string_equal(type.range->arg, "range");
2050 assert_int_equal(type.require_instance, 1);
2051 assert_string_equal(type.types->name, "sub-type-name");
David Sedlákd1144562019-08-06 12:36:14 +02002052 assert_string_equal(type.exts[0].name, "myext:c-define");
2053 assert_int_equal(type.exts[0].insubstmt_index, 0);
2054 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002055 assert_true(type.flags & LYS_SET_BASE);
2056 assert_true(type.flags & LYS_SET_BIT);
2057 assert_true(type.flags & LYS_SET_ENUM);
2058 assert_true(type.flags & LYS_SET_FRDIGITS);
2059 assert_true(type.flags & LYS_SET_LENGTH);
2060 assert_true(type.flags & LYS_SET_PATH);
2061 assert_true(type.flags & LYS_SET_PATTERN);
2062 assert_true(type.flags & LYS_SET_RANGE);
2063 assert_true(type.flags & LYS_SET_REQINST);
2064 assert_true(type.flags & LYS_SET_TYPE);
David Sedlákd1144562019-08-06 12:36:14 +02002065 lysp_type_free(st->ctx, &type);
David Sedlák374d2b32019-07-17 15:06:55 +02002066 memset(&type, 0, sizeof(type));
2067
2068 /* min subelems */
2069 data = ELEMENT_WRAPPER_START "<type name=\"type-name\"/>" ELEMENT_WRAPPER_END;
2070 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
2071 lysp_type_free(st->ctx, &type);
2072 memset(&type, 0, sizeof(type));
2073
2074 st->finished_correctly = true;
2075}
2076
David Sedlák1af868e2019-07-17 17:03:14 +02002077static void
2078test_max_elems_elem(void **state)
2079{
2080 struct state *st = *state;
2081 const char *data;
2082 struct lysp_node_list list = {};
2083 struct lysp_node_leaflist llist = {};
2084 struct lysp_refine refine = {};
2085
David Sedlákd1144562019-08-06 12:36:14 +02002086 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 +02002087 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
2088 assert_int_equal(refine.max, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002089 assert_true(refine.flags & LYS_SET_MAX);
David Sedlákd1144562019-08-06 12:36:14 +02002090 assert_string_equal(refine.exts[0].name, "myext:c-define");
2091 assert_int_equal(refine.exts[0].insubstmt_index, 0);
2092 assert_int_equal(refine.exts[0].insubstmt, LYEXT_SUBSTMT_MAX);
2093 FREE_ARRAY(st->ctx, refine.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02002094
David Sedlákd1144562019-08-06 12:36:14 +02002095 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 +02002096 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, true), LY_SUCCESS);
2097 assert_int_equal(list.max, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002098 assert_true(list.flags & LYS_SET_MAX);
David Sedlákd1144562019-08-06 12:36:14 +02002099 assert_string_equal(list.exts[0].name, "myext:c-define");
2100 assert_int_equal(list.exts[0].insubstmt_index, 0);
2101 assert_int_equal(list.exts[0].insubstmt, LYEXT_SUBSTMT_MAX);
2102 FREE_ARRAY(st->ctx, list.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02002103
David Sedlákd1144562019-08-06 12:36:14 +02002104 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 +02002105 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, true), LY_SUCCESS);
2106 assert_int_equal(llist.max, 85);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002107 assert_true(llist.flags & LYS_SET_MAX);
David Sedlákd1144562019-08-06 12:36:14 +02002108 assert_string_equal(llist.exts[0].name, "myext:c-define");
2109 assert_int_equal(llist.exts[0].insubstmt_index, 0);
2110 assert_int_equal(llist.exts[0].insubstmt, LYEXT_SUBSTMT_MAX);
2111 FREE_ARRAY(st->ctx, llist.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02002112
2113 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"10\"/> </refine>";
2114 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
2115 assert_int_equal(refine.max, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002116 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02002117
2118 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"0\"/> </list>";
2119 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002120 logbuf_assert("Invalid value \"0\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002121
2122 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"-10\"/> </list>";
2123 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002124 logbuf_assert("Invalid value \"-10\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002125
2126 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"k\"/> </list>";
2127 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002128 logbuf_assert("Invalid value \"k\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002129
2130 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"u12\"/> </list>";
2131 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002132 logbuf_assert("Invalid value \"u12\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002133
2134 st->finished_correctly = true;
2135}
2136
David Sedlák09e18c92019-07-18 11:17:11 +02002137static void
2138test_min_elems_elem(void **state)
2139{
2140 struct state *st = *state;
2141 const char *data;
2142 struct lysp_node_list list = {};
2143 struct lysp_node_leaflist llist = {};
2144 struct lysp_refine refine = {};
2145
David Sedlákd1144562019-08-06 12:36:14 +02002146 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 +02002147 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
2148 assert_int_equal(refine.min, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002149 assert_true(refine.flags & LYS_SET_MIN);
David Sedlákd1144562019-08-06 12:36:14 +02002150 assert_string_equal(refine.exts[0].name, "myext:c-define");
2151 assert_int_equal(refine.exts[0].insubstmt_index, 0);
2152 assert_int_equal(refine.exts[0].insubstmt, LYEXT_SUBSTMT_MIN);
2153 FREE_ARRAY(st->ctx, refine.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02002154
David Sedlákd1144562019-08-06 12:36:14 +02002155 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 +02002156 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, true), LY_SUCCESS);
2157 assert_int_equal(list.min, 41);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002158 assert_true(list.flags & LYS_SET_MIN);
David Sedlákd1144562019-08-06 12:36:14 +02002159 assert_string_equal(list.exts[0].name, "myext:c-define");
2160 assert_int_equal(list.exts[0].insubstmt_index, 0);
2161 assert_int_equal(list.exts[0].insubstmt, LYEXT_SUBSTMT_MIN);
2162 FREE_ARRAY(st->ctx, list.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02002163
David Sedlákd1144562019-08-06 12:36:14 +02002164 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 +02002165 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, true), LY_SUCCESS);
2166 assert_int_equal(llist.min, 50);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002167 assert_true(llist.flags & LYS_SET_MIN);
David Sedlákd1144562019-08-06 12:36:14 +02002168 assert_string_equal(llist.exts[0].name, "myext:c-define");
2169 assert_int_equal(llist.exts[0].insubstmt_index, 0);
2170 assert_int_equal(llist.exts[0].insubstmt, LYEXT_SUBSTMT_MIN);
2171 FREE_ARRAY(st->ctx, llist.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02002172
2173 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"-5\"/> </leaf-list>";
2174 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002175 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 +02002176
2177 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"99999999999999999\"/> </leaf-list>";
2178 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002179 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 +02002180
2181 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"5k\"/> </leaf-list>";
2182 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002183 logbuf_assert("Invalid value \"5k\" of \"value\" attribute in \"min-elements\" element. Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02002184
2185 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"05\"/> </leaf-list>";
2186 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002187 logbuf_assert("Invalid value \"05\" of \"value\" attribute in \"min-elements\" element. Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02002188
2189 st->finished_correctly = true;
2190}
2191
David Sedláka2dad212019-07-18 12:45:19 +02002192static void
2193test_ordby_elem(void **state)
2194{
2195 struct state *st = *state;
2196 const char *data;
2197 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02002198 struct lysp_ext_instance *exts = NULL;
David Sedláka2dad212019-07-18 12:45:19 +02002199
David Sedlákd1144562019-08-06 12:36:14 +02002200 data = ELEMENT_WRAPPER_START "<ordered-by value=\"system\">"EXT_SUBELEM"</ordered-by>" ELEMENT_WRAPPER_END;
2201 assert_int_equal(test_element_helper(st, &data, &flags, NULL, &exts, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002202 assert_true(flags & LYS_ORDBY_SYSTEM);
David Sedlákd1144562019-08-06 12:36:14 +02002203 assert_string_equal(exts[0].name, "myext:c-define");
2204 assert_int_equal(exts[0].insubstmt_index, 0);
2205 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ORDEREDBY);
2206 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedláka2dad212019-07-18 12:45:19 +02002207
2208 data = ELEMENT_WRAPPER_START "<ordered-by value=\"user\"/>" ELEMENT_WRAPPER_END;
2209 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002210 assert_true(flags & LYS_ORDBY_USER);
David Sedláka2dad212019-07-18 12:45:19 +02002211
2212 data = ELEMENT_WRAPPER_START "<ordered-by value=\"inv\"/>" ELEMENT_WRAPPER_END;
2213 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02002214 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 +02002215
2216 st->finished_correctly = true;
2217}
2218
David Sedlák8a83bbb2019-07-18 14:46:00 +02002219static void
2220test_any_elem(void **state)
2221{
2222 struct state *st = *state;
2223 const char *data;
2224 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002225 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák8a83bbb2019-07-18 14:46:00 +02002226 struct lysp_node_anydata *parsed = NULL;
2227
2228 /* anyxml max subelems */
2229 data = ELEMENT_WRAPPER_START
2230 "<anyxml name=\"any-name\">"
2231 "<config value=\"true\" />"
2232 "<description><text>desc</text></description>"
2233 "<if-feature name=\"feature\" />"
2234 "<mandatory value=\"true\" />"
2235 "<must condition=\"must-cond\" />"
2236 "<reference><text>ref</text></reference>"
2237 "<status value=\"deprecated\"/>"
2238 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002239 EXT_SUBELEM
David Sedlák8a83bbb2019-07-18 14:46:00 +02002240 "</anyxml>"
2241 ELEMENT_WRAPPER_END;
2242 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2243 parsed = (struct lysp_node_anydata *)siblings;
2244 assert_null(parsed->parent);
2245 assert_int_equal(parsed->nodetype, LYS_ANYXML);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002246 assert_true(parsed->flags & LYS_CONFIG_W);
2247 assert_true(parsed->flags & LYS_MAND_TRUE);
2248 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002249 assert_null(parsed->next);
2250 assert_string_equal(parsed->name, "any-name");
2251 assert_string_equal(parsed->dsc, "desc");
2252 assert_string_equal(parsed->ref, "ref");
2253 assert_string_equal(parsed->when->cond, "when-cond");
2254 assert_string_equal(*parsed->iffeatures, "feature");
David Sedlákd1144562019-08-06 12:36:14 +02002255 assert_string_equal(parsed->exts[0].name, "myext:c-define");
2256 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2257 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002258 lysp_node_free(st->ctx, siblings);
2259 siblings = NULL;
2260
2261 /* anydata max subelems */
2262 data = ELEMENT_WRAPPER_START
2263 "<anydata name=\"any-name\">"
2264 "<config value=\"true\" />"
2265 "<description><text>desc</text></description>"
2266 "<if-feature name=\"feature\" />"
2267 "<mandatory value=\"true\" />"
2268 "<must condition=\"must-cond\" />"
2269 "<reference><text>ref</text></reference>"
2270 "<status value=\"deprecated\"/>"
2271 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002272 EXT_SUBELEM
David Sedlák8a83bbb2019-07-18 14:46:00 +02002273 "</anydata>"
2274 ELEMENT_WRAPPER_END;
2275 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2276 parsed = (struct lysp_node_anydata *)siblings;
2277 assert_null(parsed->parent);
2278 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002279 assert_true(parsed->flags & LYS_CONFIG_W);
2280 assert_true(parsed->flags & LYS_MAND_TRUE);
2281 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002282 assert_null(parsed->next);
2283 assert_string_equal(parsed->name, "any-name");
2284 assert_string_equal(parsed->dsc, "desc");
2285 assert_string_equal(parsed->ref, "ref");
2286 assert_string_equal(parsed->when->cond, "when-cond");
2287 assert_string_equal(*parsed->iffeatures, "feature");
David Sedlákd1144562019-08-06 12:36:14 +02002288 assert_string_equal(parsed->exts[0].name, "myext:c-define");
2289 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2290 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002291 lysp_node_free(st->ctx, siblings);
2292 siblings = NULL;
2293
2294 /* min subelems */
2295 node_meta.parent = (void *)0x10;
2296 data = ELEMENT_WRAPPER_START "<anydata name=\"any-name\"> </anydata>" ELEMENT_WRAPPER_END;
2297 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2298 parsed = (struct lysp_node_anydata *)siblings;
2299 assert_ptr_equal(parsed->parent, node_meta.parent);
2300 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
2301 assert_null(parsed->next);
2302 assert_null(parsed->exts);
2303 lysp_node_free(st->ctx, siblings);
2304
2305 st->finished_correctly = true;
2306}
2307
David Sedlák203ca3a2019-07-18 15:26:25 +02002308static void
2309test_leaf_elem(void **state)
2310{
2311 struct state *st = *state;
2312 const char *data;
2313 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002314 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák203ca3a2019-07-18 15:26:25 +02002315 struct lysp_node_leaf *parsed = NULL;
2316
2317 /* max elements */
2318 data = ELEMENT_WRAPPER_START
2319 "<leaf name=\"leaf\">"
2320 "<config value=\"true\" />"
2321 "<default value=\"def-val\"/>"
2322 "<description><text>desc</text></description>"
2323 "<if-feature name=\"feature\" />"
2324 "<mandatory value=\"true\" />"
2325 "<must condition=\"must-cond\" />"
2326 "<reference><text>ref</text></reference>"
2327 "<status value=\"deprecated\"/>"
2328 "<type name=\"type\"/>"
2329 "<units name=\"uni\"/>"
2330 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002331 EXT_SUBELEM
David Sedlák203ca3a2019-07-18 15:26:25 +02002332 "</leaf>"
2333 ELEMENT_WRAPPER_END;
2334 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2335 parsed = (struct lysp_node_leaf *)siblings;
2336 assert_null(parsed->parent);
2337 assert_int_equal(parsed->nodetype, LYS_LEAF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002338 assert_true(parsed->flags & LYS_CONFIG_W);
2339 assert_true(parsed->flags & LYS_MAND_TRUE);
2340 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák203ca3a2019-07-18 15:26:25 +02002341 assert_null(parsed->next);
2342 assert_string_equal(parsed->name, "leaf");
2343 assert_string_equal(parsed->dsc, "desc");
2344 assert_string_equal(parsed->ref, "ref");
2345 assert_string_equal(parsed->when->cond, "when-cond");
2346 assert_string_equal(*parsed->iffeatures, "feature");
David Sedlákd1144562019-08-06 12:36:14 +02002347 assert_string_equal(parsed->exts[0].name, "myext:c-define");
2348 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2349 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák203ca3a2019-07-18 15:26:25 +02002350 assert_string_equal(parsed->musts->arg, "must-cond");
2351 assert_string_equal(parsed->type.name, "type");
2352 assert_string_equal(parsed->units, "uni");
2353 assert_string_equal(parsed->dflt, "def-val");
2354 lysp_node_free(st->ctx, siblings);
2355 siblings = NULL;
2356
2357 /* min elements */
2358 data = ELEMENT_WRAPPER_START "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>" ELEMENT_WRAPPER_END;
2359 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2360 parsed = (struct lysp_node_leaf *)siblings;
2361 assert_string_equal(parsed->name, "leaf");
2362 assert_string_equal(parsed->type.name, "type");
2363 lysp_node_free(st->ctx, siblings);
2364 siblings = NULL;
2365
2366 st->finished_correctly = true;
2367}
2368
David Sedlákc3da3ef2019-07-19 12:56:08 +02002369static void
2370test_leaf_list_elem(void **state)
2371{
2372 struct state *st = *state;
2373 const char *data;
2374 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002375 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlákc3da3ef2019-07-19 12:56:08 +02002376 struct lysp_node_leaflist *parsed = NULL;
2377
2378 data = ELEMENT_WRAPPER_START
2379 "<leaf-list name=\"llist\">"
2380 "<config value=\"true\" />"
2381 "<default value=\"def-val0\"/>"
2382 "<default value=\"def-val1\"/>"
2383 "<description><text>desc</text></description>"
2384 "<if-feature name=\"feature\"/>"
2385 "<max-elements value=\"5\"/>"
2386 "<must condition=\"must-cond\"/>"
2387 "<ordered-by value=\"user\" />"
2388 "<reference><text>ref</text></reference>"
2389 "<status value=\"current\"/>"
2390 "<type name=\"type\"/>"
2391 "<units name=\"uni\"/>"
2392 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002393 EXT_SUBELEM
David Sedlákc3da3ef2019-07-19 12:56:08 +02002394 "</leaf-list>"
2395 ELEMENT_WRAPPER_END;
2396 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2397 parsed = (struct lysp_node_leaflist *)siblings;
2398 assert_string_equal(parsed->dflts[0], "def-val0");
2399 assert_string_equal(parsed->dflts[1], "def-val1");
2400 assert_string_equal(parsed->dsc, "desc");
2401 assert_string_equal(*parsed->iffeatures, "feature");
2402 assert_int_equal(parsed->max, 5);
2403 assert_string_equal(parsed->musts->arg, "must-cond");
2404 assert_string_equal(parsed->name, "llist");
2405 assert_null(parsed->next);
2406 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2407 assert_null(parsed->parent);
2408 assert_string_equal(parsed->ref, "ref");
2409 assert_string_equal(parsed->type.name, "type");
2410 assert_string_equal(parsed->units, "uni");
2411 assert_string_equal(parsed->when->cond, "when-cond");
2412 assert_true(parsed->flags & LYS_CONFIG_W);
2413 assert_true(parsed->flags & LYS_ORDBY_USER);
2414 assert_true(parsed->flags & LYS_STATUS_CURR);
David Sedlákd1144562019-08-06 12:36:14 +02002415 assert_string_equal(parsed->exts[0].name, "myext:c-define");
2416 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2417 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002418 lysp_node_free(st->ctx, siblings);
2419 siblings = NULL;
2420
2421 data = ELEMENT_WRAPPER_START
2422 "<leaf-list name=\"llist\">"
2423 "<config value=\"true\" />"
2424 "<description><text>desc</text></description>"
2425 "<if-feature name=\"feature\"/>"
2426 "<min-elements value=\"5\"/>"
2427 "<must condition=\"must-cond\"/>"
2428 "<ordered-by value=\"user\" />"
2429 "<reference><text>ref</text></reference>"
2430 "<status value=\"current\"/>"
2431 "<type name=\"type\"/>"
2432 "<units name=\"uni\"/>"
2433 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002434 EXT_SUBELEM
David Sedlákc3da3ef2019-07-19 12:56:08 +02002435 "</leaf-list>"
2436 ELEMENT_WRAPPER_END;
2437 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2438 parsed = (struct lysp_node_leaflist *)siblings;
2439 assert_string_equal(parsed->dsc, "desc");
2440 assert_string_equal(*parsed->iffeatures, "feature");
2441 assert_int_equal(parsed->min, 5);
2442 assert_string_equal(parsed->musts->arg, "must-cond");
2443 assert_string_equal(parsed->name, "llist");
2444 assert_null(parsed->next);
2445 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2446 assert_null(parsed->parent);
2447 assert_string_equal(parsed->ref, "ref");
2448 assert_string_equal(parsed->type.name, "type");
2449 assert_string_equal(parsed->units, "uni");
2450 assert_string_equal(parsed->when->cond, "when-cond");
2451 assert_true(parsed->flags & LYS_CONFIG_W);
2452 assert_true(parsed->flags & LYS_ORDBY_USER);
2453 assert_true(parsed->flags & LYS_STATUS_CURR);
David Sedlákd1144562019-08-06 12:36:14 +02002454 assert_string_equal(parsed->exts[0].name, "myext:c-define");
2455 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2456 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002457 lysp_node_free(st->ctx, siblings);
2458 siblings = NULL;
2459
2460 data = ELEMENT_WRAPPER_START
2461 "<leaf-list name=\"llist\">"
2462 "<config value=\"true\" />"
2463 "<description><text>desc</text></description>"
2464 "<if-feature name=\"feature\"/>"
2465 "<max-elements value=\"15\"/>"
2466 "<min-elements value=\"5\"/>"
2467 "<must condition=\"must-cond\"/>"
2468 "<ordered-by value=\"user\" />"
2469 "<reference><text>ref</text></reference>"
2470 "<status value=\"current\"/>"
2471 "<type name=\"type\"/>"
2472 "<units name=\"uni\"/>"
2473 "<when condition=\"when-cond\"/>"
2474 "</leaf-list>"
2475 ELEMENT_WRAPPER_END;
2476 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2477 parsed = (struct lysp_node_leaflist *)siblings;
2478 assert_string_equal(parsed->dsc, "desc");
2479 assert_string_equal(*parsed->iffeatures, "feature");
2480 assert_int_equal(parsed->min, 5);
2481 assert_int_equal(parsed->max, 15);
2482 assert_string_equal(parsed->musts->arg, "must-cond");
2483 assert_string_equal(parsed->name, "llist");
2484 assert_null(parsed->next);
2485 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2486 assert_null(parsed->parent);
2487 assert_string_equal(parsed->ref, "ref");
2488 assert_string_equal(parsed->type.name, "type");
2489 assert_string_equal(parsed->units, "uni");
2490 assert_string_equal(parsed->when->cond, "when-cond");
2491 assert_true(parsed->flags & LYS_CONFIG_W);
2492 assert_true(parsed->flags & LYS_ORDBY_USER);
2493 assert_true(parsed->flags & LYS_STATUS_CURR);
2494 lysp_node_free(st->ctx, siblings);
2495 siblings = NULL;
2496
2497 data = ELEMENT_WRAPPER_START
2498 "<leaf-list name=\"llist\">"
2499 "<type name=\"type\"/>"
2500 "</leaf-list>"
2501 ELEMENT_WRAPPER_END;
2502 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2503 parsed = (struct lysp_node_leaflist *)siblings;
2504 assert_string_equal(parsed->name, "llist");
2505 assert_string_equal(parsed->type.name, "type");
2506 lysp_node_free(st->ctx, siblings);
2507 siblings = NULL;
2508
2509 /* invalid combinations */
2510 data = ELEMENT_WRAPPER_START
2511 "<leaf-list name=\"llist\">"
2512 "<max-elements value=\"5\"/>"
2513 "<min-elements value=\"15\"/>"
2514 "<type name=\"type\"/>"
2515 "</leaf-list>"
2516 ELEMENT_WRAPPER_END;
2517 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
2518 logbuf_assert("Invalid combination of min-elements and max-elements: min value 15 is bigger than the max value 5. Line number 1.");
2519 lysp_node_free(st->ctx, siblings);
2520 siblings = NULL;
2521
2522 data = ELEMENT_WRAPPER_START
2523 "<leaf-list name=\"llist\">"
2524 "<default value=\"def-val1\"/>"
2525 "<min-elements value=\"15\"/>"
2526 "<type name=\"type\"/>"
2527 "</leaf-list>"
2528 ELEMENT_WRAPPER_END;
2529 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002530 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 +02002531 lysp_node_free(st->ctx, siblings);
2532 siblings = NULL;
2533
2534 data = ELEMENT_WRAPPER_START
2535 "<leaf-list name=\"llist\">"
2536 "</leaf-list>"
2537 ELEMENT_WRAPPER_END;
2538 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002539 logbuf_assert("Missing mandatory sub-element \"type\" of \"leaf-list\" element. Line number 1.");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002540 lysp_node_free(st->ctx, siblings);
2541 siblings = NULL;
2542
2543 st->finished_correctly = true;
2544}
2545
David Sedlákcb39f642019-07-19 13:19:55 +02002546static void
2547test_presence_elem(void **state)
2548{
2549 struct state *st = *state;
2550 const char *data;
2551 const char *val;
David Sedlákd1144562019-08-06 12:36:14 +02002552 struct lysp_ext_instance *exts = NULL;
2553
2554 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\">"EXT_SUBELEM"</presence>" ELEMENT_WRAPPER_END;
2555 assert_int_equal(test_element_helper(st, &data, &val, NULL, &exts, true), LY_SUCCESS);
2556 assert_string_equal(val, "presence-val");
2557 assert_string_equal(exts[0].name, "myext:c-define");
2558 assert_int_equal(exts[0].insubstmt_index, 0);
2559 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_PRESENCE);
2560 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
2561 FREE_STRING(st->ctx, val);
David Sedlákcb39f642019-07-19 13:19:55 +02002562
2563 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\"/>" ELEMENT_WRAPPER_END;
2564 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2565 assert_string_equal(val, "presence-val");
2566 FREE_STRING(st->ctx, val);
2567
2568 data = ELEMENT_WRAPPER_START "<presence/>" ELEMENT_WRAPPER_END;
2569 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
2570 logbuf_assert("Missing mandatory attribute value of presence element. Line number 1.");
2571
2572 st->finished_correctly = true;
2573}
2574
David Sedlák12470a82019-07-19 13:44:36 +02002575static void
2576test_key_elem(void **state)
2577{
2578 struct state *st = *state;
2579 const char *data;
2580 const char *val;
David Sedlákd1144562019-08-06 12:36:14 +02002581 struct lysp_ext_instance *exts = NULL;
2582
2583 data = ELEMENT_WRAPPER_START "<key value=\"key-value\">"EXT_SUBELEM"</key>" ELEMENT_WRAPPER_END;
2584 assert_int_equal(test_element_helper(st, &data, &val, NULL, &exts, true), LY_SUCCESS);
2585 assert_string_equal(val, "key-value");
2586 assert_string_equal(exts[0].name, "myext:c-define");
2587 assert_int_equal(exts[0].insubstmt_index, 0);
2588 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_KEY);
2589 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
2590 FREE_STRING(st->ctx, val);
David Sedlák12470a82019-07-19 13:44:36 +02002591
2592 data = ELEMENT_WRAPPER_START "<key value=\"key-value\"/>" ELEMENT_WRAPPER_END;
2593 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2594 assert_string_equal(val, "key-value");
2595 FREE_STRING(st->ctx, val);
2596
2597 data = ELEMENT_WRAPPER_START "<key/>" ELEMENT_WRAPPER_END;
2598 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
2599 logbuf_assert("Missing mandatory attribute value of key element. Line number 1.");
2600
2601 st->finished_correctly = true;
2602}
2603
David Sedlák04e17b22019-07-19 15:29:48 +02002604static void
2605test_typedef_elem(void **state)
2606{
2607 struct state *st = *state;
2608 const char *data;
2609 struct lysp_tpdf *tpdfs = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002610 struct tree_node_meta typdef_meta = {NULL, (struct lysp_node **)&tpdfs};
David Sedlák04e17b22019-07-19 15:29:48 +02002611
2612 data = ELEMENT_WRAPPER_START
2613 "<typedef name=\"tpdf-name\">"
2614 "<default value=\"def-val\"/>"
2615 "<description><text>desc-text</text></description>"
2616 "<reference><text>ref-text</text></reference>"
2617 "<status value=\"current\"/>"
2618 "<type name=\"type\"/>"
2619 "<units name=\"uni\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002620 EXT_SUBELEM
David Sedlák04e17b22019-07-19 15:29:48 +02002621 "</typedef>"
2622 ELEMENT_WRAPPER_END;
2623 assert_int_equal(test_element_helper(st, &data, &typdef_meta, NULL, NULL, true), LY_SUCCESS);
2624 assert_string_equal(tpdfs[0].dflt, "def-val");
2625 assert_string_equal(tpdfs[0].dsc, "desc-text");
David Sedlák04e17b22019-07-19 15:29:48 +02002626 assert_string_equal(tpdfs[0].name, "tpdf-name");
2627 assert_string_equal(tpdfs[0].ref, "ref-text");
2628 assert_string_equal(tpdfs[0].type.name, "type");
2629 assert_string_equal(tpdfs[0].units, "uni");
2630 assert_true(tpdfs[0].flags & LYS_STATUS_CURR);
David Sedlákd1144562019-08-06 12:36:14 +02002631 assert_string_equal(tpdfs[0].exts[0].name, "myext:c-define");
2632 assert_int_equal(tpdfs[0].exts[0].insubstmt_index, 0);
2633 assert_int_equal(tpdfs[0].exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák04e17b22019-07-19 15:29:48 +02002634 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2635 tpdfs = NULL;
2636
2637 data = ELEMENT_WRAPPER_START
2638 "<typedef name=\"tpdf-name\">"
2639 "<type name=\"type\"/>"
2640 "</typedef>"
2641 ELEMENT_WRAPPER_END;
2642 assert_int_equal(test_element_helper(st, &data, &typdef_meta, NULL, NULL, true), LY_SUCCESS);
2643 assert_string_equal(tpdfs[0].name, "tpdf-name");
2644 assert_string_equal(tpdfs[0].type.name, "type");
2645 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2646 tpdfs = NULL;
2647
2648 st->finished_correctly = true;
2649}
2650
David Sedlákd2d676a2019-07-22 11:28:19 +02002651static void
2652test_refine_elem(void **state)
2653{
2654 struct state *st = *state;
2655 const char *data;
2656 struct lysp_refine *refines = NULL;
2657
2658 /* max subelems */
2659 data = ELEMENT_WRAPPER_START
2660 "<refine target-node=\"target\">"
2661 "<if-feature name=\"feature\" />"
2662 "<must condition=\"cond\" />"
2663 "<presence value=\"presence\" />"
2664 "<default value=\"def\" />"
2665 "<config value=\"true\" />"
2666 "<mandatory value=\"true\" />"
2667 "<min-elements value=\"10\" />"
2668 "<max-elements value=\"20\" />"
2669 "<description><text>desc</text></description>"
2670 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02002671 EXT_SUBELEM
David Sedlákd2d676a2019-07-22 11:28:19 +02002672 "</refine>"
2673 ELEMENT_WRAPPER_END;
2674 assert_int_equal(test_element_helper(st, &data, &refines, NULL, NULL, true), LY_SUCCESS);
2675 assert_string_equal(refines->nodeid, "target");
2676 assert_string_equal(*refines->dflts, "def");
2677 assert_string_equal(refines->dsc, "desc");
David Sedlákd2d676a2019-07-22 11:28:19 +02002678 assert_true(refines->flags & LYS_CONFIG_W);
2679 assert_true(refines->flags & LYS_MAND_TRUE);
2680 assert_string_equal(*refines->iffeatures, "feature");
2681 assert_int_equal(refines->max, 20);
2682 assert_int_equal(refines->min, 10);
2683 assert_string_equal(refines->musts->arg, "cond");
2684 assert_string_equal(refines->presence, "presence");
2685 assert_string_equal(refines->ref, "ref");
David Sedlákd1144562019-08-06 12:36:14 +02002686 assert_string_equal(refines->exts[0].name, "myext:c-define");
2687 assert_int_equal(refines->exts[0].insubstmt_index, 0);
2688 assert_int_equal(refines->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákd2d676a2019-07-22 11:28:19 +02002689 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2690 refines = NULL;
2691
2692 /* min subelems */
2693 data = ELEMENT_WRAPPER_START "<refine target-node=\"target\" />" ELEMENT_WRAPPER_END;
2694 assert_int_equal(test_element_helper(st, &data, &refines, NULL, NULL, true), LY_SUCCESS);
2695 assert_string_equal(refines->nodeid, "target");
2696 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2697 refines = NULL;
2698
2699 st->finished_correctly = true;
2700}
2701
David Sedlák0d6de5a2019-07-22 13:25:44 +02002702static void
2703test_uses_elem(void **state)
2704{
2705 struct state *st = *state;
2706 const char *data;
2707 struct lysp_node *siblings = NULL;
2708 struct tree_node_meta node_meta = {NULL, &siblings};
2709 struct lysp_node_uses *parsed = NULL;
2710
2711 /* max subelems */
2712 data = ELEMENT_WRAPPER_START
2713 "<uses name=\"uses-name\">"
2714 "<when condition=\"cond\" />"
2715 "<if-feature name=\"feature\" />"
2716 "<status value=\"obsolete\" />"
2717 "<description><text>desc</text></description>"
2718 "<reference><text>ref</text></reference>"
2719 "<refine target-node=\"target\"/>"
David Sedlák992fb7c2019-07-24 16:51:01 +02002720 "<augment target-node=\"target\" />"
David Sedlákd1144562019-08-06 12:36:14 +02002721 EXT_SUBELEM
David Sedlák0d6de5a2019-07-22 13:25:44 +02002722 "</uses>"
2723 ELEMENT_WRAPPER_END;
2724 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2725 parsed = (struct lysp_node_uses *)&siblings[0];
2726 assert_string_equal(parsed->name, "uses-name");
2727 assert_string_equal(parsed->dsc, "desc");
David Sedlák0d6de5a2019-07-22 13:25:44 +02002728 assert_true(parsed->flags & LYS_STATUS_OBSLT);
2729 assert_string_equal(*parsed->iffeatures, "feature");
2730 assert_null(parsed->next);
2731 assert_int_equal(parsed->nodetype, LYS_USES);
2732 assert_null(parsed->parent);
2733 assert_string_equal(parsed->ref, "ref");
2734 assert_string_equal(parsed->refines->nodeid, "target");
2735 assert_string_equal(parsed->when->cond, "cond");
David Sedlák992fb7c2019-07-24 16:51:01 +02002736 assert_string_equal(parsed->augments->nodeid, "target");
David Sedlákd1144562019-08-06 12:36:14 +02002737 assert_string_equal(parsed->exts[0].name, "myext:c-define");
2738 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2739 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002740 lysp_node_free(st->ctx, siblings);
2741 siblings = NULL;
2742
2743 /* min subelems */
2744 data = ELEMENT_WRAPPER_START "<uses name=\"uses-name\"/>" ELEMENT_WRAPPER_END;
2745 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2746 assert_string_equal(siblings[0].name, "uses-name");
2747 lysp_node_free(st->ctx, siblings);
2748 siblings = NULL;
2749
2750 st->finished_correctly = true;
2751}
2752
David Sedlákaa854b02019-07-22 14:17:10 +02002753static void
2754test_revision_elem(void **state)
2755{
2756 struct state *st = *state;
2757 const char *data;
2758 struct lysp_revision *revs = NULL;
2759
2760 /* max subelems */
2761 data = ELEMENT_WRAPPER_START
2762 "<revision date=\"2018-12-25\">"
2763 "<description><text>desc</text></description>"
2764 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02002765 EXT_SUBELEM
David Sedlákaa854b02019-07-22 14:17:10 +02002766 "</revision>"
2767 ELEMENT_WRAPPER_END;
2768 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, true), LY_SUCCESS);
2769 assert_string_equal(revs->date, "2018-12-25");
2770 assert_string_equal(revs->dsc, "desc");
2771 assert_string_equal(revs->ref, "ref");
David Sedlákd1144562019-08-06 12:36:14 +02002772 assert_string_equal(revs->exts[0].name, "myext:c-define");
2773 assert_int_equal(revs->exts[0].insubstmt_index, 0);
2774 assert_int_equal(revs->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákaa854b02019-07-22 14:17:10 +02002775 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2776 revs = NULL;
2777
2778 /* min subelems */
2779 data = ELEMENT_WRAPPER_START "<revision date=\"2005-05-05\" />" ELEMENT_WRAPPER_END;
2780 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, true), LY_SUCCESS);
2781 assert_string_equal(revs->date, "2005-05-05");
2782 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2783 revs = NULL;
2784
2785 /* invalid value */
2786 data = ELEMENT_WRAPPER_START "<revision date=\"05-05-2005\" />" ELEMENT_WRAPPER_END;
2787 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, false), LY_EVALID);
2788 logbuf_assert("Invalid value \"05-05-2005\" of \"revision\". Line number 1.");
2789 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2790 revs = NULL;
2791
2792 st->finished_correctly = true;
2793}
2794
David Sedlák0c2bab92019-07-22 15:33:19 +02002795static void
2796test_include_elem(void **state)
2797{
2798 struct state *st = *state;
2799 const char *data;
2800 struct lysp_include *includes = NULL;
2801 struct include_meta inc_meta = {"module-name", &includes};
2802
2803 /* max subelems */
2804 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2805 data = ELEMENT_WRAPPER_START
2806 "<include module=\"mod\">"
2807 "<description><text>desc</text></description>"
2808 "<reference><text>ref</text></reference>"
2809 "<revision-date date=\"1999-09-09\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002810 EXT_SUBELEM
David Sedlák0c2bab92019-07-22 15:33:19 +02002811 "</include>"
2812 ELEMENT_WRAPPER_END;
2813 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, true), LY_SUCCESS);
2814 assert_string_equal(includes->name, "mod");
2815 assert_string_equal(includes->dsc, "desc");
2816 assert_string_equal(includes->ref, "ref");
David Sedlák0c2bab92019-07-22 15:33:19 +02002817 assert_string_equal(includes->rev, "1999-09-09");
David Sedlákd1144562019-08-06 12:36:14 +02002818 assert_string_equal(includes->exts[0].name, "myext:c-define");
2819 assert_int_equal(includes->exts[0].insubstmt_index, 0);
2820 assert_int_equal(includes->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák0c2bab92019-07-22 15:33:19 +02002821 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2822 includes = NULL;
2823
2824 /* min subelems */
2825 data = ELEMENT_WRAPPER_START "<include module=\"mod\"/>" ELEMENT_WRAPPER_END;
2826 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, true), LY_SUCCESS);
2827 assert_string_equal(includes->name, "mod");
2828 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2829 includes = NULL;
2830
2831 /* invalid combinations */
2832 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2833 data = ELEMENT_WRAPPER_START
2834 "<include module=\"mod\">"
2835 "<description><text>desc</text></description>"
2836 "<revision-date date=\"1999-09-09\"/>"
2837 "</include>"
2838 ELEMENT_WRAPPER_END;
2839 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, false), LY_EVALID);
2840 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.");
2841 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2842 includes = NULL;
2843
2844 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2845 data = ELEMENT_WRAPPER_START
2846 "<include module=\"mod\">"
2847 "<reference><text>ref</text></reference>"
2848 "<revision-date date=\"1999-09-09\"/>"
2849 "</include>"
2850 ELEMENT_WRAPPER_END;
2851 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, false), LY_EVALID);
2852 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.");
2853 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2854 includes = NULL;
2855
2856 st->finished_correctly = true;
2857}
2858
David Sedlák5e13dea2019-07-22 16:06:45 +02002859static void
2860test_feature_elem(void **state)
2861{
2862 struct state *st = *state;
2863 const char *data;
2864 struct lysp_feature *features = NULL;
2865
2866 /* max subelems */
2867 data = ELEMENT_WRAPPER_START
2868 "<feature name=\"feature-name\">"
2869 "<if-feature name=\"iff\"/>"
2870 "<status value=\"deprecated\"/>"
2871 "<description><text>desc</text></description>"
2872 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02002873 EXT_SUBELEM
David Sedlák5e13dea2019-07-22 16:06:45 +02002874 "</feature>"
2875 ELEMENT_WRAPPER_END;
2876 assert_int_equal(test_element_helper(st, &data, &features, NULL, NULL, true), LY_SUCCESS);
2877 assert_string_equal(features->name, "feature-name");
2878 assert_string_equal(features->dsc, "desc");
David Sedlák5e13dea2019-07-22 16:06:45 +02002879 assert_true(features->flags & LYS_STATUS_DEPRC);
2880 assert_string_equal(*features->iffeatures, "iff");
2881 assert_string_equal(features->ref, "ref");
David Sedlákd1144562019-08-06 12:36:14 +02002882 assert_string_equal(features->exts[0].name, "myext:c-define");
2883 assert_int_equal(features->exts[0].insubstmt_index, 0);
2884 assert_int_equal(features->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák5e13dea2019-07-22 16:06:45 +02002885 FREE_ARRAY(st->ctx, features, lysp_feature_free);
2886 features = NULL;
2887
2888 /* min subelems */
2889 data = ELEMENT_WRAPPER_START "<feature name=\"feature-name\"/>" ELEMENT_WRAPPER_END;
2890 assert_int_equal(test_element_helper(st, &data, &features, NULL, NULL, true), LY_SUCCESS);
2891 assert_string_equal(features->name, "feature-name");
2892 FREE_ARRAY(st->ctx, features, lysp_feature_free);
2893 features = NULL;
2894
2895 st->finished_correctly = true;
2896}
2897
David Sedlák28794f22019-07-22 16:45:00 +02002898static void
2899test_identity_elem(void **state)
2900{
2901 struct state *st = *state;
2902 const char *data;
2903 struct lysp_ident *identities = NULL;
2904
2905 /* max subelems */
2906 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2907 data = ELEMENT_WRAPPER_START
2908 "<identity name=\"ident-name\">"
2909 "<if-feature name=\"iff\"/>"
2910 "<base name=\"base-name\"/>"
2911 "<status value=\"deprecated\"/>"
2912 "<description><text>desc</text></description>"
2913 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02002914 EXT_SUBELEM
David Sedlák28794f22019-07-22 16:45:00 +02002915 "</identity>"
2916 ELEMENT_WRAPPER_END;
2917 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, true), LY_SUCCESS);
2918 assert_string_equal(identities->name, "ident-name");
2919 assert_string_equal(*identities->bases, "base-name");
2920 assert_string_equal(*identities->iffeatures, "iff");
2921 assert_string_equal(identities->dsc, "desc");
2922 assert_string_equal(identities->ref, "ref");
2923 assert_true(identities->flags & LYS_STATUS_DEPRC);
David Sedlákd1144562019-08-06 12:36:14 +02002924 assert_string_equal(identities->exts[0].name, "myext:c-define");
2925 assert_int_equal(identities->exts[0].insubstmt_index, 0);
2926 assert_int_equal(identities->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák28794f22019-07-22 16:45:00 +02002927 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2928 identities = NULL;
2929
2930 /* min subelems */
2931 data = ELEMENT_WRAPPER_START "<identity name=\"ident-name\" />" ELEMENT_WRAPPER_END;
2932 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, true), LY_SUCCESS);
2933 assert_string_equal(identities->name, "ident-name");
2934 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2935 identities = NULL;
2936
2937 /* invalid */
2938 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2939 data = ELEMENT_WRAPPER_START
2940 "<identity name=\"ident-name\">"
2941 "<if-feature name=\"iff\"/>"
2942 "</identity>"
2943 ELEMENT_WRAPPER_END;
2944 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, false), LY_EVALID);
2945 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.");
2946 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2947 identities = NULL;
2948
2949 st->finished_correctly = true;
2950}
2951
David Sedlákaf536aa2019-07-23 13:42:23 +02002952static void
2953test_list_elem(void **state)
2954{
2955 struct state *st = *state;
2956 const char *data;
2957 struct lysp_node *siblings = NULL;
2958 struct tree_node_meta node_meta = {NULL, &siblings};
2959 struct lysp_node_list *parsed = NULL;
2960
2961 /* max subelems */
2962 data = ELEMENT_WRAPPER_START
2963 "<list name=\"list-name\">"
2964 "<when condition=\"when\"/>"
2965 "<if-feature name=\"iff\"/>"
2966 "<must condition=\"must-cond\"/>"
2967 "<key value=\"key\"/>"
2968 "<unique tag=\"utag\"/>"
2969 "<config value=\"true\"/>"
2970 "<min-elements value=\"10\"/>"
2971 "<ordered-by value=\"user\"/>"
2972 "<status value=\"deprecated\"/>"
2973 "<description><text>desc</text></description>"
2974 "<reference><text>ref</text></reference>"
2975 "<anydata name=\"anyd\"/>"
2976 "<anyxml name=\"anyx\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02002977 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002978 "<choice name=\"choice\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02002979 "<action name=\"action\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02002980 "<grouping name=\"grp\"/>"
David Sedlák031b9e72019-07-23 15:19:37 +02002981 "<notification name=\"notf\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002982 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
2983 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlákaf536aa2019-07-23 13:42:23 +02002984 "<list name=\"sub-list\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002985 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákaf536aa2019-07-23 13:42:23 +02002986 "<uses name=\"uses-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002987 EXT_SUBELEM
David Sedlákaf536aa2019-07-23 13:42:23 +02002988 "</list>"
2989 ELEMENT_WRAPPER_END;
2990 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2991 parsed = (struct lysp_node_list *)&siblings[0];
2992 assert_string_equal(parsed->dsc, "desc");
2993 assert_string_equal(parsed->child->name, "anyd");
2994 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2995 assert_string_equal(parsed->child->next->name, "anyx");
2996 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
David Sedlákf111bcb2019-07-23 17:15:51 +02002997 assert_string_equal(parsed->child->next->next->name, "cont");
2998 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002999 assert_string_equal(parsed->child->next->next->next->name, "choice");
3000 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
David Sedlák85d0eca2019-07-24 15:15:21 +02003001 assert_string_equal(parsed->child->next->next->next->next->name, "leaf");
3002 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAF);
3003 assert_string_equal(parsed->child->next->next->next->next->next->name, "llist");
3004 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3005 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "sub-list");
3006 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LIST);
3007 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "uses-name");
3008 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_USES);
3009 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003010 assert_string_equal(parsed->groupings->name, "grp");
David Sedlák85d0eca2019-07-24 15:15:21 +02003011 assert_string_equal(parsed->actions->name, "action");
David Sedláke3ce9ef2019-07-23 16:34:30 +02003012 assert_int_equal(parsed->groupings->nodetype, LYS_GROUPING);
David Sedlák031b9e72019-07-23 15:19:37 +02003013 assert_string_equal(parsed->notifs->name, "notf");
David Sedlákaf536aa2019-07-23 13:42:23 +02003014 assert_true(parsed->flags & LYS_ORDBY_USER);
3015 assert_true(parsed->flags & LYS_STATUS_DEPRC);
3016 assert_true(parsed->flags & LYS_CONFIG_W);
3017 assert_string_equal(*parsed->iffeatures, "iff");
3018 assert_string_equal(parsed->key, "key");
3019 assert_int_equal(parsed->min, 10);
3020 assert_string_equal(parsed->musts->arg, "must-cond");
3021 assert_string_equal(parsed->name, "list-name");
3022 assert_null(parsed->next);
3023 assert_int_equal(parsed->nodetype, LYS_LIST);
3024 assert_null(parsed->parent);
3025 assert_string_equal(parsed->ref, "ref");
3026 assert_string_equal(parsed->typedefs->name, "tpdf");
3027 assert_string_equal(*parsed->uniques, "utag");
3028 assert_string_equal(parsed->when->cond, "when");
David Sedlákd1144562019-08-06 12:36:14 +02003029 assert_string_equal(parsed->exts[0].name, "myext:c-define");
3030 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3031 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákaf536aa2019-07-23 13:42:23 +02003032 lysp_node_free(st->ctx, siblings);
3033 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
3034 siblings = NULL;
3035
3036 /* min subelems */
3037 data = ELEMENT_WRAPPER_START "<list name=\"list-name\" />" ELEMENT_WRAPPER_END;
3038 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
3039 parsed = (struct lysp_node_list *)&siblings[0];
3040 assert_string_equal(parsed->name, "list-name");
3041 lysp_node_free(st->ctx, siblings);
3042 siblings = NULL;
3043
3044 st->finished_correctly = true;
3045}
3046
David Sedlák031b9e72019-07-23 15:19:37 +02003047static void
3048test_notification_elem(void **state)
3049{
3050 struct state *st = *state;
3051 const char *data;
3052 struct lysp_notif *notifs = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003053 struct tree_node_meta notif_meta = {NULL, (struct lysp_node **)&notifs};
David Sedlák031b9e72019-07-23 15:19:37 +02003054
3055 /* max subelems */
3056 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3057 data = ELEMENT_WRAPPER_START
3058 "<notification name=\"notif-name\">"
3059 "<anydata name=\"anyd\"/>"
3060 "<anyxml name=\"anyx\"/>"
3061 "<description><text>desc</text></description>"
3062 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003063 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3064 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák031b9e72019-07-23 15:19:37 +02003065 "<list name=\"sub-list\"/>"
3066 "<must condition=\"cond\"/>"
3067 "<reference><text>ref</text></reference>"
3068 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003069 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák031b9e72019-07-23 15:19:37 +02003070 "<uses name=\"uses-name\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02003071 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003072 "<choice name=\"choice\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02003073 "<grouping name=\"grp\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003074 EXT_SUBELEM
David Sedlák031b9e72019-07-23 15:19:37 +02003075 "</notification>"
3076 ELEMENT_WRAPPER_END;
3077 assert_int_equal(test_element_helper(st, &data, &notif_meta, NULL, NULL, true), LY_SUCCESS);
3078 assert_string_equal(notifs->name, "notif-name");
3079 assert_string_equal(notifs->data->name, "anyd");
3080 assert_int_equal(notifs->data->nodetype, LYS_ANYDATA);
3081 assert_string_equal(notifs->data->next->name, "anyx");
3082 assert_int_equal(notifs->data->next->nodetype, LYS_ANYXML);
3083 assert_string_equal(notifs->data->next->next->name, "leaf");
3084 assert_int_equal(notifs->data->next->next->nodetype, LYS_LEAF);
3085 assert_string_equal(notifs->data->next->next->next->name, "llist");
3086 assert_int_equal(notifs->data->next->next->next->nodetype, LYS_LEAFLIST);
3087 assert_string_equal(notifs->data->next->next->next->next->name, "sub-list");
3088 assert_int_equal(notifs->data->next->next->next->next->nodetype, LYS_LIST);
David Sedlák031b9e72019-07-23 15:19:37 +02003089 assert_true(notifs->flags & LYS_STATUS_DEPRC);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003090 assert_string_equal(notifs->groupings->name, "grp");
3091 assert_int_equal(notifs->groupings->nodetype, LYS_GROUPING);
David Sedlákf111bcb2019-07-23 17:15:51 +02003092 assert_string_equal(notifs->data->next->next->next->next->next->name, "uses-name");
3093 assert_int_equal(notifs->data->next->next->next->next->next->nodetype, LYS_USES);
3094 assert_string_equal(notifs->data->next->next->next->next->next->next->name, "cont");
3095 assert_int_equal(notifs->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003096 assert_int_equal(notifs->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
3097 assert_string_equal(notifs->data->next->next->next->next->next->next->next->name, "choice");
3098 assert_null(notifs->data->next->next->next->next->next->next->next->next);
David Sedlák031b9e72019-07-23 15:19:37 +02003099 assert_string_equal(*notifs->iffeatures, "iff");
3100 assert_string_equal(notifs->musts->arg, "cond");
3101 assert_int_equal(notifs->nodetype, LYS_NOTIF);
3102 assert_null(notifs->parent);
3103 assert_string_equal(notifs->ref, "ref");
3104 assert_string_equal(notifs->typedefs->name, "tpdf");
David Sedlákd1144562019-08-06 12:36:14 +02003105 assert_string_equal(notifs->exts[0].name, "myext:c-define");
3106 assert_int_equal(notifs->exts[0].insubstmt_index, 0);
3107 assert_int_equal(notifs->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák031b9e72019-07-23 15:19:37 +02003108 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
3109 notifs = NULL;
3110
3111 /* min subelems */
3112 data = ELEMENT_WRAPPER_START "<notification name=\"notif-name\" />" ELEMENT_WRAPPER_END;
3113 assert_int_equal(test_element_helper(st, &data, &notif_meta, NULL, NULL, true), LY_SUCCESS);
3114 assert_string_equal(notifs->name, "notif-name");
3115 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003116 notifs = NULL;
3117
3118 st->finished_correctly = true;
3119}
3120
3121static void
3122test_grouping_elem(void **state)
3123{
3124 struct state *st = *state;
3125 const char *data;
3126 struct lysp_grp *grps = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003127 struct tree_node_meta grp_meta = {NULL, (struct lysp_node **)&grps};
David Sedláke3ce9ef2019-07-23 16:34:30 +02003128
3129 /* max subelems */
3130 data = ELEMENT_WRAPPER_START
3131 "<grouping name=\"grp-name\">"
3132 "<anydata name=\"anyd\"/>"
3133 "<anyxml name=\"anyx\"/>"
3134 "<description><text>desc</text></description>"
3135 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003136 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3137 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02003138 "<list name=\"list\"/>"
3139 "<notification name=\"notf\"/>"
3140 "<reference><text>ref</text></reference>"
3141 "<status value=\"current\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003142 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02003143 "<uses name=\"uses-name\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02003144 "<action name=\"act\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02003145 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003146 "<choice name=\"choice\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003147 EXT_SUBELEM
David Sedláke3ce9ef2019-07-23 16:34:30 +02003148 "</grouping>"
3149 ELEMENT_WRAPPER_END;
3150 assert_int_equal(test_element_helper(st, &data, &grp_meta, NULL, NULL, true), LY_SUCCESS);
3151 assert_string_equal(grps->name, "grp-name");
David Sedláke3ce9ef2019-07-23 16:34:30 +02003152 assert_string_equal(grps->data->name, "anyd");
3153 assert_string_equal(grps->data->next->name, "anyx");
3154 assert_string_equal(grps->data->next->next->name, "leaf");
3155 assert_string_equal(grps->data->next->next->next->name, "llist");
3156 assert_string_equal(grps->data->next->next->next->next->name, "list");
3157 assert_string_equal(grps->dsc, "desc");
David Sedláke3ce9ef2019-07-23 16:34:30 +02003158 assert_true(grps->flags & LYS_STATUS_CURR);
3159 assert_string_equal(grps->groupings->name, "sub-grp");
3160 assert_int_equal(grps->nodetype, LYS_GROUPING);
3161 assert_string_equal(grps->notifs->name, "notf");
3162 assert_null(grps->parent);
3163 assert_string_equal(grps->ref, "ref");
3164 assert_string_equal(grps->typedefs->name, "tpdf");
David Sedlák85d0eca2019-07-24 15:15:21 +02003165 assert_string_equal(grps->actions->name, "act");
David Sedlákf111bcb2019-07-23 17:15:51 +02003166 assert_string_equal(grps->data->next->next->next->next->next->name, "uses-name");
David Sedlákb7abcfa2019-07-24 12:33:35 +02003167 assert_int_equal(grps->data->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákf111bcb2019-07-23 17:15:51 +02003168 assert_string_equal(grps->data->next->next->next->next->next->next->name, "cont");
David Sedlákb7abcfa2019-07-24 12:33:35 +02003169 assert_int_equal(grps->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
3170 assert_string_equal(grps->data->next->next->next->next->next->next->next->name, "choice");
3171 assert_int_equal(grps->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
David Sedlákd1144562019-08-06 12:36:14 +02003172 assert_string_equal(grps->exts[0].name, "myext:c-define");
3173 assert_int_equal(grps->exts[0].insubstmt_index, 0);
3174 assert_int_equal(grps->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003175 FREE_ARRAY(st->ctx, grps, lysp_grp_free);
3176 grps = NULL;
3177
3178 /* min subelems */
3179 data = ELEMENT_WRAPPER_START "<grouping name=\"grp-name\" />" ELEMENT_WRAPPER_END;
3180 assert_int_equal(test_element_helper(st, &data, &grp_meta, NULL, NULL, true), LY_SUCCESS);
3181 assert_string_equal(grps->name, "grp-name");
3182 FREE_ARRAY(st->ctx, grps, lysp_grp_free);
3183 grps = NULL;
David Sedlák031b9e72019-07-23 15:19:37 +02003184
3185 st->finished_correctly = true;
3186}
3187
David Sedlákf111bcb2019-07-23 17:15:51 +02003188static void
3189test_container_elem(void **state)
3190{
3191 struct state *st = *state;
3192 const char *data;
3193 struct lysp_node *siblings = NULL;
3194 struct tree_node_meta node_meta = {NULL, &siblings};
3195 struct lysp_node_container *parsed = NULL;
3196
3197 /* max subelems */
David Sedláke2dc9e92019-07-24 09:59:21 +02003198 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3199 data = ELEMENT_WRAPPER_START
3200 "<container name=\"cont-name\">"
3201 "<anydata name=\"anyd\"/>"
3202 "<anyxml name=\"anyx\"/>"
3203 "<config value=\"true\"/>"
3204 "<container name=\"subcont\"/>"
3205 "<description><text>desc</text></description>"
3206 "<grouping name=\"sub-grp\"/>"
3207 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003208 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3209 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedláke2dc9e92019-07-24 09:59:21 +02003210 "<list name=\"list\"/>"
3211 "<must condition=\"cond\"/>"
3212 "<notification name=\"notf\"/>"
3213 "<presence value=\"presence\"/>"
3214 "<reference><text>ref</text></reference>"
3215 "<status value=\"current\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003216 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedláke2dc9e92019-07-24 09:59:21 +02003217 "<uses name=\"uses-name\"/>"
3218 "<when condition=\"when-cond\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02003219 "<action name=\"act\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003220 "<choice name=\"choice\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003221 EXT_SUBELEM
David Sedláke2dc9e92019-07-24 09:59:21 +02003222 "</container>"
3223 ELEMENT_WRAPPER_END;
3224 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
3225 parsed = (struct lysp_node_container *)siblings;
3226 assert_string_equal(parsed->name, "cont-name");
3227 assert_null(parsed->parent);
3228 assert_int_equal(parsed->nodetype, LYS_CONTAINER);
3229 assert_true(parsed->flags & LYS_CONFIG_W);
3230 assert_true(parsed->flags & LYS_STATUS_CURR);
3231 assert_null(parsed->next);
3232 assert_string_equal(parsed->dsc, "desc");
3233 assert_string_equal(parsed->ref, "ref");
3234 assert_string_equal(parsed->when->cond, "when-cond");
3235 assert_string_equal(*parsed->iffeatures, "iff");
David Sedláke2dc9e92019-07-24 09:59:21 +02003236 assert_string_equal(parsed->musts->arg, "cond");
3237 assert_string_equal(parsed->presence, "presence");
3238 assert_string_equal(parsed->typedefs->name, "tpdf");
3239 assert_string_equal(parsed->groupings->name, "sub-grp");
3240 assert_string_equal(parsed->child->name, "anyd");
3241 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
3242 assert_string_equal(parsed->child->next->name, "anyx");
3243 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
3244 assert_string_equal(parsed->child->next->next->name, "subcont");
3245 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
3246 assert_string_equal(parsed->child->next->next->next->name, "leaf");
3247 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
3248 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
3249 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
3250 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
3251 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
3252 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
3253 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003254 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
3255 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
3256 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke2dc9e92019-07-24 09:59:21 +02003257 assert_string_equal(parsed->notifs->name, "notf");
David Sedlák85d0eca2019-07-24 15:15:21 +02003258 assert_string_equal(parsed->actions->name, "act");
David Sedlákd1144562019-08-06 12:36:14 +02003259 assert_string_equal(parsed->exts[0].name, "myext:c-define");
3260 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3261 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedláke2dc9e92019-07-24 09:59:21 +02003262 lysp_node_free(st->ctx, siblings);
3263 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
3264 siblings = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02003265
3266 /* min subelems */
3267 data = ELEMENT_WRAPPER_START "<container name=\"cont-name\" />" ELEMENT_WRAPPER_END;
3268 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
3269 parsed = (struct lysp_node_container *)siblings;
3270 assert_string_equal(parsed->name, "cont-name");
3271 lysp_node_free(st->ctx, siblings);
3272 siblings = NULL;
3273
3274 st->finished_correctly = true;
3275}
3276
David Sedlák5379d392019-07-24 10:42:03 +02003277static void
3278test_case_elem(void **state)
3279{
3280 struct state *st = *state;
3281 const char *data;
3282 struct lysp_node *siblings = NULL;
3283 struct tree_node_meta node_meta = {NULL, &siblings};
3284 struct lysp_node_case *parsed = NULL;
3285
3286 /* max subelems */
3287 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3288 data = ELEMENT_WRAPPER_START
3289 "<case name=\"case-name\">"
3290 "<anydata name=\"anyd\"/>"
3291 "<anyxml name=\"anyx\"/>"
3292 "<container name=\"subcont\"/>"
3293 "<description><text>desc</text></description>"
3294 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003295 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3296 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák5379d392019-07-24 10:42:03 +02003297 "<list name=\"list\"/>"
3298 "<reference><text>ref</text></reference>"
3299 "<status value=\"current\"/>"
3300 "<uses name=\"uses-name\"/>"
3301 "<when condition=\"when-cond\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003302 "<choice name=\"choice\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003303 EXT_SUBELEM
David Sedlák5379d392019-07-24 10:42:03 +02003304 "</case>"
3305 ELEMENT_WRAPPER_END;
3306 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
3307 parsed = (struct lysp_node_case *)siblings;
3308 assert_string_equal(parsed->name, "case-name");
3309 assert_null(parsed->parent);
3310 assert_int_equal(parsed->nodetype, LYS_CASE);
3311 assert_true(parsed->flags & LYS_STATUS_CURR);
3312 assert_null(parsed->next);
3313 assert_string_equal(parsed->dsc, "desc");
3314 assert_string_equal(parsed->ref, "ref");
3315 assert_string_equal(parsed->when->cond, "when-cond");
3316 assert_string_equal(*parsed->iffeatures, "iff");
David Sedlák5379d392019-07-24 10:42:03 +02003317 assert_string_equal(parsed->child->name, "anyd");
3318 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
3319 assert_string_equal(parsed->child->next->name, "anyx");
3320 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
3321 assert_string_equal(parsed->child->next->next->name, "subcont");
3322 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
3323 assert_string_equal(parsed->child->next->next->next->name, "leaf");
3324 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
3325 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
3326 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
3327 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
3328 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
3329 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
3330 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003331 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
3332 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
3333 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedlákd1144562019-08-06 12:36:14 +02003334 assert_string_equal(parsed->exts[0].name, "myext:c-define");
3335 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3336 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák5379d392019-07-24 10:42:03 +02003337 lysp_node_free(st->ctx, siblings);
3338 siblings = NULL;
3339
3340 /* min subelems */
3341 data = ELEMENT_WRAPPER_START "<case name=\"case-name\" />" ELEMENT_WRAPPER_END;
3342 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
3343 parsed = (struct lysp_node_case *)siblings;
3344 assert_string_equal(parsed->name, "case-name");
3345 lysp_node_free(st->ctx, siblings);
3346 siblings = NULL;
3347
3348 st->finished_correctly = true;
3349}
3350
David Sedlákb7abcfa2019-07-24 12:33:35 +02003351static void
3352test_choice_elem(void **state)
3353{
3354 struct state *st = *state;
3355 const char *data;
3356 struct lysp_node *siblings = NULL;
3357 struct tree_node_meta node_meta = {NULL, &siblings};
3358 struct lysp_node_choice *parsed = NULL;
3359
3360 /* max subelems */
3361 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3362 data = ELEMENT_WRAPPER_START
3363 "<choice name=\"choice-name\">"
3364 "<anydata name=\"anyd\"/>"
3365 "<anyxml name=\"anyx\"/>"
3366 "<case name=\"sub-case\"/>"
3367 "<choice name=\"choice\"/>"
3368 "<config value=\"true\"/>"
3369 "<container name=\"subcont\"/>"
3370 "<default value=\"def\"/>"
3371 "<description><text>desc</text></description>"
3372 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003373 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3374 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003375 "<list name=\"list\"/>"
3376 "<mandatory value=\"true\" />"
3377 "<reference><text>ref</text></reference>"
3378 "<status value=\"current\"/>"
3379 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003380 EXT_SUBELEM
David Sedlákb7abcfa2019-07-24 12:33:35 +02003381 "</choice>"
3382 ELEMENT_WRAPPER_END;
3383 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
3384 parsed = (struct lysp_node_choice *)siblings;
3385 assert_string_equal(parsed->name, "choice-name");
3386 assert_null(parsed->parent);
3387 assert_int_equal(parsed->nodetype, LYS_CHOICE);
3388 assert_true(parsed->flags & LYS_CONFIG_W && parsed->flags & LYS_MAND_TRUE && parsed->flags & LYS_STATUS_CURR);
3389 assert_null(parsed->next);
3390 assert_string_equal(parsed->dsc, "desc");
3391 assert_string_equal(parsed->ref, "ref");
3392 assert_string_equal(parsed->when->cond, "when-cond");
3393 assert_string_equal(*parsed->iffeatures, "iff");
David Sedlákb7abcfa2019-07-24 12:33:35 +02003394 assert_string_equal(parsed->child->name, "anyd");
3395 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
3396 assert_string_equal(parsed->child->next->name, "anyx");
3397 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
3398 assert_string_equal(parsed->child->next->next->name, "sub-case");
3399 assert_int_equal(parsed->child->next->next->nodetype, LYS_CASE);
3400 assert_string_equal(parsed->child->next->next->next->name, "choice");
3401 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
3402 assert_string_equal(parsed->child->next->next->next->next->name, "subcont");
3403 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_CONTAINER);
3404 assert_string_equal(parsed->child->next->next->next->next->next->name, "leaf");
3405 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAF);
3406 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "llist");
3407 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3408 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "list");
3409 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
3410 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedlákd1144562019-08-06 12:36:14 +02003411 assert_string_equal(parsed->exts[0].name, "myext:c-define");
3412 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3413 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003414 lysp_node_free(st->ctx, siblings);
3415 siblings = NULL;
3416
3417 /* min subelems */
3418 data = ELEMENT_WRAPPER_START "<choice name=\"choice-name\" />" ELEMENT_WRAPPER_END;
3419 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
3420 parsed = (struct lysp_node_choice *)siblings;
3421 assert_string_equal(parsed->name, "choice-name");
3422 lysp_node_free(st->ctx, siblings);
3423 siblings = NULL;
3424
3425 st->finished_correctly = true;
3426}
3427
David Sedlák05404f62019-07-24 14:11:53 +02003428static void
3429test_inout_elem(void **state)
3430{
3431 struct state *st = *state;
3432 const char *data;
3433 struct lysp_action_inout inout = {};
3434 struct inout_meta inout_meta = {NULL, &inout};
3435
3436 /* max subelements */
3437 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3438 data = ELEMENT_WRAPPER_START
3439 "<input>"
3440 "<anydata name=\"anyd\"/>"
3441 "<anyxml name=\"anyx\"/>"
3442 "<choice name=\"choice\"/>"
3443 "<container name=\"subcont\"/>"
3444 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003445 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3446 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák05404f62019-07-24 14:11:53 +02003447 "<list name=\"list\"/>"
3448 "<must condition=\"cond\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003449 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák05404f62019-07-24 14:11:53 +02003450 "<uses name=\"uses-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003451 EXT_SUBELEM
David Sedlák05404f62019-07-24 14:11:53 +02003452 "</input>"
3453 ELEMENT_WRAPPER_END;
3454 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3455 assert_null(inout.parent);
3456 assert_int_equal(inout.nodetype, LYS_INPUT);
3457 assert_string_equal(inout.musts->arg, "cond");
3458 assert_string_equal(inout.typedefs->name, "tpdf");
3459 assert_string_equal(inout.groupings->name, "sub-grp");
3460 assert_string_equal(inout.data->name, "anyd");
3461 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
3462 assert_string_equal(inout.data->next->name, "anyx");
3463 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
3464 assert_string_equal(inout.data->next->next->name, "choice");
3465 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
3466 assert_string_equal(inout.data->next->next->next->name, "subcont");
3467 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
3468 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
3469 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
3470 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
3471 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3472 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
3473 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
3474 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
3475 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3476 assert_null(inout.data->next->next->next->next->next->next->next->next);
David Sedlákd1144562019-08-06 12:36:14 +02003477 assert_string_equal(inout.exts[0].name, "myext:c-define");
3478 assert_int_equal(inout.exts[0].insubstmt_index, 0);
3479 assert_int_equal(inout.exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák05404f62019-07-24 14:11:53 +02003480 lysp_action_inout_free(st->ctx, &inout);
3481 memset(&inout, 0, sizeof inout);
3482
3483 /* max subelements */
3484 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3485 data = ELEMENT_WRAPPER_START
3486 "<output>"
3487 "<anydata name=\"anyd\"/>"
3488 "<anyxml name=\"anyx\"/>"
3489 "<choice name=\"choice\"/>"
3490 "<container name=\"subcont\"/>"
3491 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003492 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3493 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák05404f62019-07-24 14:11:53 +02003494 "<list name=\"list\"/>"
3495 "<must condition=\"cond\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003496 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák05404f62019-07-24 14:11:53 +02003497 "<uses name=\"uses-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003498 EXT_SUBELEM
David Sedlák05404f62019-07-24 14:11:53 +02003499 "</output>"
3500 ELEMENT_WRAPPER_END;
3501 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3502 assert_null(inout.parent);
3503 assert_int_equal(inout.nodetype, LYS_OUTPUT);
3504 assert_string_equal(inout.musts->arg, "cond");
3505 assert_string_equal(inout.typedefs->name, "tpdf");
3506 assert_string_equal(inout.groupings->name, "sub-grp");
3507 assert_string_equal(inout.data->name, "anyd");
3508 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
3509 assert_string_equal(inout.data->next->name, "anyx");
3510 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
3511 assert_string_equal(inout.data->next->next->name, "choice");
3512 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
3513 assert_string_equal(inout.data->next->next->next->name, "subcont");
3514 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
3515 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
3516 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
3517 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
3518 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3519 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
3520 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
3521 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
3522 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3523 assert_null(inout.data->next->next->next->next->next->next->next->next);
David Sedlákd1144562019-08-06 12:36:14 +02003524 assert_string_equal(inout.exts[0].name, "myext:c-define");
3525 assert_int_equal(inout.exts[0].insubstmt_index, 0);
3526 assert_int_equal(inout.exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák05404f62019-07-24 14:11:53 +02003527 lysp_action_inout_free(st->ctx, &inout);
3528 memset(&inout, 0, sizeof inout);
3529
3530 /* min subelems */
3531 data = ELEMENT_WRAPPER_START "<input />" ELEMENT_WRAPPER_END;
3532 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3533 memset(&inout, 0, sizeof inout);
3534
3535 data = ELEMENT_WRAPPER_START "<output />" ELEMENT_WRAPPER_END;
3536 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3537 memset(&inout, 0, sizeof inout);
3538
3539 /* invalid combinations */
3540 data = ELEMENT_WRAPPER_START "<input name=\"test\"/>" ELEMENT_WRAPPER_END;
3541 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, false), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02003542 logbuf_assert("Unexpected attribute \"name\" of \"input\" element. Line number 1.");
David Sedlák05404f62019-07-24 14:11:53 +02003543 memset(&inout, 0, sizeof inout);
3544
3545 st->finished_correctly = true;
3546}
3547
David Sedlák85d0eca2019-07-24 15:15:21 +02003548static void
3549test_action_elem(void **state)
3550{
3551 struct state *st = *state;
3552 const char *data;
3553 struct lysp_action *actions = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003554 struct tree_node_meta act_meta = {NULL, (struct lysp_node **)&actions};
David Sedlák85d0eca2019-07-24 15:15:21 +02003555
3556 /* max subelems */
3557 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3558 data = ELEMENT_WRAPPER_START
3559 "<action name=\"act\">"
3560 "<description><text>desc</text></description>"
3561 "<grouping name=\"grouping\"/>"
3562 "<if-feature name=\"iff\"/>"
3563 "<input><uses name=\"uses-name\"/></input>"
3564 "<output><must condition=\"cond\"/></output>"
3565 "<reference><text>ref</text></reference>"
3566 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003567 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákd1144562019-08-06 12:36:14 +02003568 EXT_SUBELEM
David Sedlák85d0eca2019-07-24 15:15:21 +02003569 "</action>"
3570 ELEMENT_WRAPPER_END;
3571 assert_int_equal(test_element_helper(st, &data, &act_meta, NULL, NULL, true), LY_SUCCESS);
3572 assert_null(actions->parent);
3573 assert_int_equal(actions->nodetype, LYS_ACTION);
3574 assert_true(actions->flags & LYS_STATUS_DEPRC);
3575 assert_string_equal(actions->name, "act");
3576 assert_string_equal(actions->dsc, "desc");
3577 assert_string_equal(actions->ref, "ref");
3578 assert_string_equal(*actions->iffeatures, "iff");
3579 assert_string_equal(actions->typedefs->name, "tpdf");
3580 assert_string_equal(actions->groupings->name, "grouping");
3581 assert_string_equal(actions->input.data->name, "uses-name");
3582 assert_string_equal(actions->output.musts->arg, "cond");
David Sedlákd1144562019-08-06 12:36:14 +02003583 assert_string_equal(actions->exts[0].name, "myext:c-define");
3584 assert_int_equal(actions->exts[0].insubstmt_index, 0);
3585 assert_int_equal(actions->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák85d0eca2019-07-24 15:15:21 +02003586 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3587 actions = NULL;
3588
David Sedlákeaa45792019-07-24 15:25:01 +02003589 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3590 data = ELEMENT_WRAPPER_START
3591 "<rpc name=\"act\">"
3592 "<description><text>desc</text></description>"
3593 "<grouping name=\"grouping\"/>"
3594 "<if-feature name=\"iff\"/>"
3595 "<input><uses name=\"uses-name\"/></input>"
3596 "<output><must condition=\"cond\"/></output>"
3597 "<reference><text>ref</text></reference>"
3598 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003599 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákd1144562019-08-06 12:36:14 +02003600 EXT_SUBELEM
David Sedlákeaa45792019-07-24 15:25:01 +02003601 "</rpc>"
3602 ELEMENT_WRAPPER_END;
3603 assert_int_equal(test_element_helper(st, &data, &act_meta, NULL, NULL, true), LY_SUCCESS);
3604 assert_null(actions->parent);
3605 assert_int_equal(actions->nodetype, LYS_ACTION);
3606 assert_true(actions->flags & LYS_STATUS_DEPRC);
3607 assert_string_equal(actions->name, "act");
3608 assert_string_equal(actions->dsc, "desc");
3609 assert_string_equal(actions->ref, "ref");
3610 assert_string_equal(*actions->iffeatures, "iff");
3611 assert_string_equal(actions->typedefs->name, "tpdf");
3612 assert_string_equal(actions->groupings->name, "grouping");
3613 assert_string_equal(actions->input.data->name, "uses-name");
3614 assert_string_equal(actions->output.musts->arg, "cond");
David Sedlákd1144562019-08-06 12:36:14 +02003615 assert_string_equal(actions->exts[0].name, "myext:c-define");
3616 assert_int_equal(actions->exts[0].insubstmt_index, 0);
3617 assert_int_equal(actions->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákeaa45792019-07-24 15:25:01 +02003618 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3619 actions = NULL;
3620
David Sedlák85d0eca2019-07-24 15:15:21 +02003621 /* min subelems */
3622 data = ELEMENT_WRAPPER_START "<action name=\"act\" />" ELEMENT_WRAPPER_END;
3623 assert_int_equal(test_element_helper(st, &data, &act_meta, NULL, NULL, true), LY_SUCCESS);
3624 assert_string_equal(actions->name, "act");
3625 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3626 actions = NULL;
3627
3628 st->finished_correctly = true;
3629}
3630
David Sedlák992fb7c2019-07-24 16:51:01 +02003631static void
3632test_augment_elem(void **state)
3633{
3634 struct state *st = *state;
3635 const char *data;
3636 struct lysp_augment *augments = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003637 struct tree_node_meta aug_meta = {NULL, (struct lysp_node **)&augments};
David Sedlák992fb7c2019-07-24 16:51:01 +02003638
3639 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3640 data = ELEMENT_WRAPPER_START
3641 "<augment target-node=\"target\">"
3642 "<action name=\"action\"/>"
3643 "<anydata name=\"anyd\"/>"
3644 "<anyxml name=\"anyx\"/>"
3645 "<case name=\"case\"/>"
3646 "<choice name=\"choice\"/>"
3647 "<container name=\"subcont\"/>"
3648 "<description><text>desc</text></description>"
3649 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003650 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3651 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák992fb7c2019-07-24 16:51:01 +02003652 "<list name=\"list\"/>"
3653 "<notification name=\"notif\"/>"
3654 "<reference><text>ref</text></reference>"
3655 "<status value=\"current\"/>"
3656 "<uses name=\"uses\"/>"
3657 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003658 EXT_SUBELEM
David Sedlák992fb7c2019-07-24 16:51:01 +02003659 "</augment>"
3660 ELEMENT_WRAPPER_END;
3661 assert_int_equal(test_element_helper(st, &data, &aug_meta, NULL, NULL, true), LY_SUCCESS);
3662 assert_string_equal(augments->nodeid, "target");
3663 assert_null(augments->parent);
3664 assert_int_equal(augments->nodetype, LYS_AUGMENT);
3665 assert_true(augments->flags & LYS_STATUS_CURR);
3666 assert_string_equal(augments->dsc, "desc");
3667 assert_string_equal(augments->ref, "ref");
3668 assert_string_equal(augments->when->cond, "when-cond");
3669 assert_string_equal(*augments->iffeatures, "iff");
3670 assert_string_equal(augments->child->name, "anyd");
3671 assert_int_equal(augments->child->nodetype, LYS_ANYDATA);
3672 assert_string_equal(augments->child->next->name, "anyx");
3673 assert_int_equal(augments->child->next->nodetype, LYS_ANYXML);
3674 assert_string_equal(augments->child->next->next->name, "case");
3675 assert_int_equal(augments->child->next->next->nodetype, LYS_CASE);
3676 assert_string_equal(augments->child->next->next->next->name, "choice");
3677 assert_int_equal(augments->child->next->next->next->nodetype, LYS_CHOICE);
3678 assert_string_equal(augments->child->next->next->next->next->name, "subcont");
3679 assert_int_equal(augments->child->next->next->next->next->nodetype, LYS_CONTAINER);
3680 assert_string_equal(augments->child->next->next->next->next->next->name, "leaf");
3681 assert_int_equal(augments->child->next->next->next->next->next->nodetype, LYS_LEAF);
3682 assert_string_equal(augments->child->next->next->next->next->next->next->name, "llist");
3683 assert_int_equal(augments->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3684 assert_string_equal(augments->child->next->next->next->next->next->next->next->name, "list");
3685 assert_int_equal(augments->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
3686 assert_string_equal(augments->child->next->next->next->next->next->next->next->next->name, "uses");
3687 assert_int_equal(augments->child->next->next->next->next->next->next->next->next->nodetype, LYS_USES);
3688 assert_null(augments->child->next->next->next->next->next->next->next->next->next);
3689 assert_string_equal(augments->actions->name, "action");
3690 assert_string_equal(augments->notifs->name, "notif");
David Sedlákd1144562019-08-06 12:36:14 +02003691 assert_string_equal(augments->exts[0].name, "myext:c-define");
3692 assert_int_equal(augments->exts[0].insubstmt_index, 0);
3693 assert_int_equal(augments->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák992fb7c2019-07-24 16:51:01 +02003694 FREE_ARRAY(st->ctx, augments, lysp_augment_free)
3695 augments = NULL;
3696
3697 data = ELEMENT_WRAPPER_START "<augment target-node=\"target\" />" ELEMENT_WRAPPER_END;
3698 assert_int_equal(test_element_helper(st, &data, &aug_meta, NULL, NULL, true), LY_SUCCESS);
3699 assert_string_equal(augments->nodeid, "target");
3700 FREE_ARRAY(st->ctx, augments, lysp_augment_free)
3701 augments = NULL;
3702
3703 st->finished_correctly = true;
3704}
3705
David Sedlák4ffcec82019-07-25 15:10:21 +02003706static void
3707test_deviate_elem(void **state)
3708{
3709 struct state *st = *state;
3710 const char *data;
3711 struct lysp_deviate *deviates = NULL;
3712 struct lysp_deviate_add *d_add;
3713 struct lysp_deviate_rpl *d_rpl;
3714 struct lysp_deviate_del *d_del;
3715
3716 /* all valid arguments with min subelems */
3717 data = ELEMENT_WRAPPER_START "<deviate value=\"not-supported\" />" ELEMENT_WRAPPER_END;
3718 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3719 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
3720 lysp_deviate_free(st->ctx, deviates);
3721 free(deviates);
3722 deviates = NULL;
3723
3724 data = ELEMENT_WRAPPER_START "<deviate value=\"add\" />" ELEMENT_WRAPPER_END;
3725 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3726 assert_int_equal(deviates->mod, LYS_DEV_ADD);
3727 lysp_deviate_free(st->ctx, deviates);
3728 free(deviates);
3729 deviates = NULL;
3730
3731 data = ELEMENT_WRAPPER_START "<deviate value=\"replace\" />" ELEMENT_WRAPPER_END;
3732 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3733 assert_int_equal(deviates->mod, LYS_DEV_REPLACE);
3734 lysp_deviate_free(st->ctx, deviates);
3735 free(deviates);
3736 deviates = NULL;
3737
3738 data = ELEMENT_WRAPPER_START "<deviate value=\"delete\" />" ELEMENT_WRAPPER_END;
3739 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3740 assert_int_equal(deviates->mod, LYS_DEV_DELETE);
3741 lysp_deviate_free(st->ctx, deviates);
3742 free(deviates);
3743 deviates = NULL;
3744
3745 /* max subelems and valid arguments */
3746 data = ELEMENT_WRAPPER_START
3747 "<deviate value=\"not-supported\">"
David Sedlákd1144562019-08-06 12:36:14 +02003748 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003749 "</deviate>"
3750 ELEMENT_WRAPPER_END;
3751 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3752 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
David Sedlákd1144562019-08-06 12:36:14 +02003753 assert_string_equal(deviates->exts[0].name, "myext:c-define");
3754 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3755 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003756 lysp_deviate_free(st->ctx, deviates);
3757 free(deviates);
3758 deviates = NULL;
3759
3760 data = ELEMENT_WRAPPER_START
3761 "<deviate value=\"add\">"
3762 "<units name=\"units\"/>"
3763 "<must condition=\"cond\"/>"
3764 "<unique tag=\"utag\"/>"
3765 "<default value=\"def\"/>"
3766 "<config value=\"true\"/>"
3767 "<mandatory value=\"true\"/>"
3768 "<min-elements value=\"5\"/>"
3769 "<max-elements value=\"15\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003770 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003771 "</deviate>"
3772 ELEMENT_WRAPPER_END;
3773 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3774 d_add = (struct lysp_deviate_add *)deviates;
3775 assert_int_equal(d_add->mod, LYS_DEV_ADD);
3776 assert_null(d_add->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003777 assert_string_equal(d_add->units, "units");
3778 assert_string_equal(d_add->musts->arg, "cond");
3779 assert_string_equal(*d_add->uniques, "utag");
3780 assert_string_equal(*d_add->dflts, "def");
3781 assert_true(d_add->flags & LYS_MAND_TRUE && d_add->flags & LYS_CONFIG_W);
3782 assert_int_equal(d_add->min, 5);
3783 assert_int_equal(d_add->max, 15);
David Sedlákd1144562019-08-06 12:36:14 +02003784 assert_string_equal(deviates->exts[0].name, "myext:c-define");
3785 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3786 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003787 lysp_deviate_free(st->ctx, deviates);
3788 free(deviates);
3789 deviates = NULL;
3790
3791 data = ELEMENT_WRAPPER_START
3792 "<deviate value=\"replace\">"
3793 "<type name=\"newtype\"/>"
3794 "<units name=\"uni\"/>"
3795 "<default value=\"def\"/>"
3796 "<config value=\"true\"/>"
3797 "<mandatory value=\"true\"/>"
3798 "<min-elements value=\"5\"/>"
3799 "<max-elements value=\"15\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003800 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003801 "</deviate>"
3802 ELEMENT_WRAPPER_END;
3803 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3804 d_rpl = (struct lysp_deviate_rpl *)deviates;
3805 assert_int_equal(d_rpl->mod, LYS_DEV_REPLACE);
3806 assert_null(d_rpl->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003807 assert_string_equal(d_rpl->type->name, "newtype");
3808 assert_string_equal(d_rpl->units, "uni");
3809 assert_string_equal(d_rpl->dflt, "def");
3810 assert_true(d_rpl->flags & LYS_MAND_TRUE && d_rpl->flags & LYS_CONFIG_W);
3811 assert_int_equal(d_rpl->min, 5);
3812 assert_int_equal(d_rpl->max, 15);
David Sedlákd1144562019-08-06 12:36:14 +02003813 assert_string_equal(deviates->exts[0].name, "myext:c-define");
3814 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3815 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003816 lysp_deviate_free(st->ctx, deviates);
3817 free(deviates);
3818 deviates = NULL;
3819
3820 data = ELEMENT_WRAPPER_START
3821 "<deviate value=\"delete\">"
3822 "<units name=\"u\"/>"
3823 "<must condition=\"c\"/>"
3824 "<unique tag=\"tag\"/>"
3825 "<default value=\"default\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003826 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003827 "</deviate>"
3828 ELEMENT_WRAPPER_END;
3829 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3830 d_del = (struct lysp_deviate_del *)deviates;
3831 assert_int_equal(d_del->mod, LYS_DEV_DELETE);
3832 assert_null(d_del->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003833 assert_string_equal(d_del->units, "u");
3834 assert_string_equal(d_del->musts->arg, "c");
3835 assert_string_equal(*d_del->uniques, "tag");
3836 assert_string_equal(*d_del->dflts, "default");
David Sedlákd1144562019-08-06 12:36:14 +02003837 assert_string_equal(deviates->exts[0].name, "myext:c-define");
3838 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3839 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003840 lysp_deviate_free(st->ctx, deviates);
3841 free(deviates);
3842 deviates = NULL;
3843
3844 /* invalid arguments */
3845 data = ELEMENT_WRAPPER_START "<deviate value=\"\" />" ELEMENT_WRAPPER_END;
3846 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003847 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 +02003848 deviates = NULL;
3849
3850 data = ELEMENT_WRAPPER_START "<deviate value=\"invalid\" />" ELEMENT_WRAPPER_END;
3851 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003852 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 +02003853 deviates = NULL;
3854
3855 data = ELEMENT_WRAPPER_START "<deviate value=\"ad\" />" ELEMENT_WRAPPER_END;
3856 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003857 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 +02003858 deviates = NULL;
3859
3860 data = ELEMENT_WRAPPER_START "<deviate value=\"adds\" />" ELEMENT_WRAPPER_END;
3861 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003862 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 +02003863 deviates = NULL;
3864
3865 data = ELEMENT_WRAPPER_START
3866 "<deviate value=\"not-supported\">"
3867 "<must condition=\"c\"/>"
3868 "</deviate>"
3869 ELEMENT_WRAPPER_END;
3870 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
3871 logbuf_assert("Deviate of this type doesn't allow \"must\" as it's sub-element. Line number 1.");
3872
3873 st->finished_correctly = true;
3874}
3875
David Sedlák8b754462019-07-25 16:22:13 +02003876static void
3877test_deviation_elem(void **state)
3878{
3879 struct state *st = *state;
3880 const char *data;
3881 struct lysp_deviation *deviations = NULL;
3882
3883 /* min subelems */
3884 data = ELEMENT_WRAPPER_START
3885 "<deviation target-node=\"target\">"
3886 "<deviate value=\"not-supported\"/>"
3887 "</deviation>"
3888 ELEMENT_WRAPPER_END;
3889 assert_int_equal(test_element_helper(st, &data, &deviations, NULL, NULL, true), LY_SUCCESS);
3890 assert_string_equal(deviations->nodeid, "target");
3891 assert_int_equal(deviations->deviates->mod, LYS_DEV_NOT_SUPPORTED);
3892 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3893 deviations = NULL;
3894
3895 /* max subelems */
3896 data = ELEMENT_WRAPPER_START
3897 "<deviation target-node=\"target\">"
3898 "<reference><text>ref</text></reference>"
3899 "<description><text>desc</text></description>"
3900 "<deviate value=\"add\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003901 EXT_SUBELEM
David Sedlák8b754462019-07-25 16:22:13 +02003902 "</deviation>"
3903 ELEMENT_WRAPPER_END;
3904 assert_int_equal(test_element_helper(st, &data, &deviations, NULL, NULL, true), LY_SUCCESS);
3905 assert_string_equal(deviations->nodeid, "target");
3906 assert_int_equal(deviations->deviates->mod, LYS_DEV_ADD);
3907 assert_string_equal(deviations->ref, "ref");
3908 assert_string_equal(deviations->dsc, "desc");
David Sedlákd1144562019-08-06 12:36:14 +02003909 assert_string_equal(deviations->exts[0].name, "myext:c-define");
3910 assert_int_equal(deviations->exts[0].insubstmt_index, 0);
3911 assert_int_equal(deviations->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8b754462019-07-25 16:22:13 +02003912 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3913 deviations = NULL;
3914
3915 /* invalid */
3916 data = ELEMENT_WRAPPER_START "<deviation target-node=\"target\"/>" ELEMENT_WRAPPER_END;
3917 assert_int_equal(test_element_helper(st, &data, &deviations, NULL, NULL, false), LY_EVALID);
3918 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3919 deviations = NULL;
David Sedlák1538a842019-08-08 15:38:51 +02003920 logbuf_assert("Missing mandatory sub-element \"deviate\" of \"deviation\" element. Line number 1.");
3921 /* TODO */
David Sedlák8b754462019-07-25 16:22:13 +02003922 st->finished_correctly = true;
3923}
3924
David Sedlák4f03b932019-07-26 13:01:47 +02003925static void
3926test_module_elem(void **state)
3927{
3928 struct state *st = *state;
David Sedlákc5b20842019-08-13 10:18:31 +02003929 const char *data, *name, *prefix;
3930 size_t name_len, prefix_len;
David Sedlák4f03b932019-07-26 13:01:47 +02003931 struct yin_arg_record *attrs = NULL;
David Sedlák4f03b932019-07-26 13:01:47 +02003932 struct lys_module *lys_mod = NULL;
3933 struct lysp_module *lysp_mod = NULL;
3934
3935 /* max subelems */
3936 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
3937 lys_mod = calloc(1, sizeof *lys_mod);
3938 lysp_mod = calloc(1, sizeof *lysp_mod);
3939 lys_mod->ctx = st->ctx;
3940 lysp_mod->mod = lys_mod;
3941 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
3942 "<yang-version value=\"1.1\"/>\n"
3943 "<namespace uri=\"ns\"/>\n"
3944 "<prefix value=\"pref\"/>\n"
3945 "<include module=\"b-mod\"/>\n"
3946 "<import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
3947 "<organization><text>org</text></organization>\n"
3948 "<contact><text>contact</text></contact>\n"
3949 "<description><text>desc</text></description>"
3950 "<reference><text>ref</text></reference>\n"
3951 "<revision date=\"2019-02-02\"/>\n"
3952 "<anydata name=\"anyd\"/>\n"
3953 "<anyxml name=\"anyx\"/>\n"
3954 "<choice name=\"choice\"/>\n"
3955 "<container name=\"cont\"/>\n"
3956 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3957 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3958 "<list name=\"sub-list\"/>\n"
3959 "<uses name=\"uses-name\"/>\n"
3960 "<augment target-node=\"target\"/>\n"
3961 "<deviation target-node=\"target\">""<deviate value=\"not-supported\"/>""</deviation>\n"
3962 "<extension name=\"ext\"/>\n"
3963 "<feature name=\"feature\"/>\n"
3964 "<grouping name=\"grp\"/>\n"
3965 "<identity name=\"ident-name\"/>\n"
3966 "<notification name=\"notf\"/>\n"
3967 "<rpc name=\"rpc-name\"/>\n"
3968 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
David Sedlákd1144562019-08-06 12:36:14 +02003969 EXT_SUBELEM"\n"
David Sedlák4f03b932019-07-26 13:01:47 +02003970 "</module>\n";
David Sedlákc5b20842019-08-13 10:18:31 +02003971 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 +02003972 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
3973 assert_int_equal(yin_parse_mod(st->yin_ctx, attrs, &data, lysp_mod), LY_SUCCESS);
3974 assert_string_equal(lysp_mod->mod->name, "mod");
3975 assert_string_equal(lysp_mod->revs, "2019-02-02");
3976 assert_string_equal(lysp_mod->mod->ns, "ns");
3977 assert_string_equal(lysp_mod->mod->prefix, "pref");
3978 assert_null(lysp_mod->mod->filepath);
3979 assert_string_equal(lysp_mod->mod->org, "org");
3980 assert_string_equal(lysp_mod->mod->contact, "contact");
3981 assert_string_equal(lysp_mod->mod->dsc, "desc");
3982 assert_string_equal(lysp_mod->mod->ref, "ref");
3983 assert_int_equal(lysp_mod->mod->version, LYS_VERSION_1_1);
3984 assert_string_equal(lysp_mod->imports->name, "a-mod");
3985 assert_string_equal(lysp_mod->includes->name, "b-mod");
3986 assert_string_equal(lysp_mod->extensions->name, "ext");
3987 assert_string_equal(lysp_mod->features->name, "feature");
3988 assert_string_equal(lysp_mod->identities->name, "ident-name");
3989 assert_string_equal(lysp_mod->typedefs->name, "tpdf");
3990 assert_string_equal(lysp_mod->groupings->name, "grp");
3991 assert_string_equal(lysp_mod->data->name, "anyd");
3992 assert_int_equal(lysp_mod->data->nodetype, LYS_ANYDATA);
3993 assert_string_equal(lysp_mod->data->next->name, "anyx");
3994 assert_int_equal(lysp_mod->data->next->nodetype, LYS_ANYXML);
3995 assert_string_equal(lysp_mod->data->next->next->name, "choice");
3996 assert_int_equal(lysp_mod->data->next->next->nodetype, LYS_CHOICE);
3997 assert_string_equal(lysp_mod->data->next->next->next->name, "cont");
3998 assert_int_equal(lysp_mod->data->next->next->next->nodetype, LYS_CONTAINER);
3999 assert_string_equal(lysp_mod->data->next->next->next->next->name, "leaf");
4000 assert_int_equal(lysp_mod->data->next->next->next->next->nodetype, LYS_LEAF);
4001 assert_string_equal(lysp_mod->data->next->next->next->next->next->name, "llist");
4002 assert_int_equal(lysp_mod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
4003 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->name, "sub-list");
4004 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
4005 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->next->name, "uses-name");
4006 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
4007 assert_null(lysp_mod->data->next->next->next->next->next->next->next->next);
4008 assert_string_equal(lysp_mod->augments->nodeid, "target");
4009 assert_string_equal(lysp_mod->rpcs->name, "rpc-name");
4010 assert_string_equal(lysp_mod->notifs->name, "notf");
4011 assert_string_equal(lysp_mod->deviations->nodeid, "target");
David Sedlákd1144562019-08-06 12:36:14 +02004012 assert_string_equal(lysp_mod->exts[0].name, "myext:c-define");
4013 assert_int_equal(lysp_mod->exts[0].insubstmt_index, 0);
4014 assert_int_equal(lysp_mod->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4f03b932019-07-26 13:01:47 +02004015 lysp_module_free(lysp_mod);
4016 lys_module_free(lys_mod, NULL);
4017 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
4018 attrs = NULL;
4019
4020 /* min subelems */
4021 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
4022 lys_mod = calloc(1, sizeof *lys_mod);
4023 lysp_mod = calloc(1, sizeof *lysp_mod);
4024 lys_mod->ctx = st->ctx;
4025 lysp_mod->mod = lys_mod;
4026 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">"
4027 "<namespace uri=\"ns\"/>"
4028 "<prefix value=\"pref\"/>"
4029 "<yang-version value=\"1.1\"/>"
4030 "</module>";
David Sedlákc5b20842019-08-13 10:18:31 +02004031 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 +02004032 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
4033 assert_int_equal(yin_parse_mod(st->yin_ctx, attrs, &data, lysp_mod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02004034 assert_string_equal(lysp_mod->mod->name, "mod");
David Sedlák4f03b932019-07-26 13:01:47 +02004035 lysp_module_free(lysp_mod);
4036 lys_module_free(lys_mod, NULL);
4037 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
4038 attrs = NULL;
David Sedlák298ff6d2019-07-26 14:29:03 +02004039
David Sedláke6cd89e2019-08-07 12:46:02 +02004040 /* incorrect subelem order */
4041 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
4042 lys_mod = calloc(1, sizeof *lys_mod);
4043 lysp_mod = calloc(1, sizeof *lysp_mod);
4044 lys_mod->ctx = st->ctx;
4045 lysp_mod->mod = lys_mod;
4046 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">"
4047 "<feature name=\"feature\"/>\n"
4048 "<namespace uri=\"ns\"/>"
4049 "<prefix value=\"pref\"/>"
4050 "<yang-version value=\"1.1\"/>"
4051 "</module>";
David Sedlákc5b20842019-08-13 10:18:31 +02004052 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 +02004053 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
4054 assert_int_equal(yin_parse_mod(st->yin_ctx, attrs, &data, lysp_mod), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02004055 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 +02004056 lysp_module_free(lysp_mod);
4057 lys_module_free(lys_mod, NULL);
4058 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
4059 attrs = NULL;
4060
David Sedlák298ff6d2019-07-26 14:29:03 +02004061 st->finished_correctly = true;
4062}
4063
4064static void
4065test_submodule_elem(void **state)
4066{
4067 struct state *st = *state;
David Sedlákc5b20842019-08-13 10:18:31 +02004068 const char *data, *name, *prefix;
4069 size_t name_len, prefix_len;
David Sedlák298ff6d2019-07-26 14:29:03 +02004070 struct yin_arg_record *attrs = NULL;
David Sedlák298ff6d2019-07-26 14:29:03 +02004071 struct lysp_submodule *lysp_submod = NULL;
4072
4073 /* max subelements */
4074 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
4075 lysp_submod = calloc(1, sizeof *lysp_submod);
4076 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
4077 "<yang-version value=\"1.1\"/>\n"
4078 "<belongs-to module=\"mod-name\"><prefix value=\"pref\"/></belongs-to>"
4079 "<include module=\"b-mod\"/>\n"
4080 "<import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
4081 "<organization><text>org</text></organization>\n"
4082 "<contact><text>contact</text></contact>\n"
4083 "<description><text>desc</text></description>"
4084 "<reference><text>ref</text></reference>\n"
4085 "<revision date=\"2019-02-02\"/>\n"
4086 "<anydata name=\"anyd\"/>\n"
4087 "<anyxml name=\"anyx\"/>\n"
4088 "<choice name=\"choice\"/>\n"
4089 "<container name=\"cont\"/>\n"
4090 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
4091 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
4092 "<list name=\"sub-list\"/>\n"
4093 "<uses name=\"uses-name\"/>\n"
4094 "<augment target-node=\"target\"/>\n"
4095 "<deviation target-node=\"target\">""<deviate value=\"not-supported\"/>""</deviation>\n"
4096 "<extension name=\"ext\"/>\n"
4097 "<feature name=\"feature\"/>\n"
4098 "<grouping name=\"grp\"/>\n"
4099 "<identity name=\"ident-name\"/>\n"
4100 "<notification name=\"notf\"/>\n"
4101 "<rpc name=\"rpc-name\"/>\n"
4102 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
David Sedlákd1144562019-08-06 12:36:14 +02004103 EXT_SUBELEM"\n"
David Sedlák298ff6d2019-07-26 14:29:03 +02004104 "</submodule>\n";
David Sedlákc5b20842019-08-13 10:18:31 +02004105 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 +02004106 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
4107 assert_int_equal(yin_parse_submod(st->yin_ctx, attrs, &data, lysp_submod), LY_SUCCESS);
4108
4109 assert_string_equal(lysp_submod->name, "mod");
4110 assert_string_equal(lysp_submod->revs, "2019-02-02");
David Sedlák298ff6d2019-07-26 14:29:03 +02004111 assert_string_equal(lysp_submod->prefix, "pref");
4112 assert_null(lysp_submod->filepath);
4113 assert_string_equal(lysp_submod->org, "org");
4114 assert_string_equal(lysp_submod->contact, "contact");
4115 assert_string_equal(lysp_submod->dsc, "desc");
4116 assert_string_equal(lysp_submod->ref, "ref");
4117 assert_int_equal(lysp_submod->version, LYS_VERSION_1_1);
4118 assert_string_equal(lysp_submod->imports->name, "a-mod");
4119 assert_string_equal(lysp_submod->includes->name, "b-mod");
4120 assert_string_equal(lysp_submod->extensions->name, "ext");
4121 assert_string_equal(lysp_submod->features->name, "feature");
4122 assert_string_equal(lysp_submod->identities->name, "ident-name");
4123 assert_string_equal(lysp_submod->typedefs->name, "tpdf");
4124 assert_string_equal(lysp_submod->groupings->name, "grp");
4125 assert_string_equal(lysp_submod->data->name, "anyd");
4126 assert_int_equal(lysp_submod->data->nodetype, LYS_ANYDATA);
4127 assert_string_equal(lysp_submod->data->next->name, "anyx");
4128 assert_int_equal(lysp_submod->data->next->nodetype, LYS_ANYXML);
4129 assert_string_equal(lysp_submod->data->next->next->name, "choice");
4130 assert_int_equal(lysp_submod->data->next->next->nodetype, LYS_CHOICE);
4131 assert_string_equal(lysp_submod->data->next->next->next->name, "cont");
4132 assert_int_equal(lysp_submod->data->next->next->next->nodetype, LYS_CONTAINER);
4133 assert_string_equal(lysp_submod->data->next->next->next->next->name, "leaf");
4134 assert_int_equal(lysp_submod->data->next->next->next->next->nodetype, LYS_LEAF);
4135 assert_string_equal(lysp_submod->data->next->next->next->next->next->name, "llist");
4136 assert_int_equal(lysp_submod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
4137 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->name, "sub-list");
4138 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
4139 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->next->name, "uses-name");
4140 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
4141 assert_null(lysp_submod->data->next->next->next->next->next->next->next->next);
4142 assert_string_equal(lysp_submod->augments->nodeid, "target");
4143 assert_string_equal(lysp_submod->rpcs->name, "rpc-name");
4144 assert_string_equal(lysp_submod->notifs->name, "notf");
4145 assert_string_equal(lysp_submod->deviations->nodeid, "target");
David Sedlákd1144562019-08-06 12:36:14 +02004146 assert_string_equal(lysp_submod->exts[0].name, "myext:c-define");
4147 assert_int_equal(lysp_submod->exts[0].insubstmt_index, 0);
4148 assert_int_equal(lysp_submod->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák298ff6d2019-07-26 14:29:03 +02004149
4150 lysp_submodule_free(st->ctx, lysp_submod);
4151 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
4152 attrs = NULL;
4153
4154 /* min subelemnts */
4155 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
4156 lysp_submod = calloc(1, sizeof *lysp_submod);
4157 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">"
4158 "<yang-version value=\"1.0\"/>"
4159 "<belongs-to module=\"mod-name\"><prefix value=\"pref\"/></belongs-to>"
4160 "</submodule>";
David Sedlákc5b20842019-08-13 10:18:31 +02004161 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 +02004162 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
4163 assert_int_equal(yin_parse_submod(st->yin_ctx, attrs, &data, lysp_submod), LY_SUCCESS);
4164 assert_string_equal(lysp_submod->prefix, "pref");
4165 assert_string_equal(lysp_submod->belongsto, "mod-name");
4166 assert_int_equal(lysp_submod->version, LYS_VERSION_1_0);
4167 lysp_submodule_free(st->ctx, lysp_submod);
4168 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
4169 attrs = NULL;
4170
David Sedláke6cd89e2019-08-07 12:46:02 +02004171 /* incorrect subelem order */
4172 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
4173 lysp_submod = calloc(1, sizeof *lysp_submod);
4174 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">"
4175 "<yang-version value=\"1.0\"/>"
4176 "<reference><text>ref</text></reference>\n"
4177 "<belongs-to module=\"mod-name\"><prefix value=\"pref\"/></belongs-to>"
4178 "</submodule>";
David Sedlákc5b20842019-08-13 10:18:31 +02004179 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 +02004180 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
4181 assert_int_equal(yin_parse_submod(st->yin_ctx, attrs, &data, lysp_submod), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02004182 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 +02004183 lysp_submodule_free(st->ctx, lysp_submod);
4184 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
4185 attrs = NULL;
4186
David Sedlák298ff6d2019-07-26 14:29:03 +02004187 st->finished_correctly = true;
David Sedlák4f03b932019-07-26 13:01:47 +02004188}
4189
David Sedlák8985a142019-07-31 16:43:06 +02004190static void
4191test_yin_parse_module(void **state)
4192{
4193 struct state *st = *state;
4194 const char *data;
4195 struct lys_module *mod;
4196 struct yin_parser_ctx *yin_ctx = NULL;
4197
4198 mod = calloc(1, sizeof *mod);
4199 mod->ctx = st->ctx;
4200 data = "<module name=\"example-foo\""
4201 "xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
4202 "xmlns:foo=\"urn:example:foo\""
4203 "xmlns:myext=\"urn:example:extensions\">\n"
4204
4205 "<yang-version value=\"1.0\"/>\n"
4206
4207 "<namespace uri=\"urn:example:foo\"/>\n"
4208 "<prefix value=\"foo\"/>\n"
4209
4210 "<import module=\"example-extensions\">\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004211 "<prefix value=\"myext\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02004212 "</import>\n"
4213
4214 "<list name=\"interface\">\n"
4215 "<key value=\"name\"/>\n"
4216 "<leaf name=\"name\">\n"
4217 "<type name=\"string\"/>\n"
4218 "</leaf>\n"
4219 "<leaf name=\"mtu\">\n"
4220 "<type name=\"uint32\"/>\n"
4221 "<description>\n"
4222 "<text>The MTU of the interface.</text>\n"
4223 "</description>\n"
4224 "<myext:c-define name=\"MY_MTU\"/>\n"
4225 "</leaf>\n"
4226 "</list>\n"
4227 "</module>\n";
4228 assert_int_equal(yin_parse_module(&yin_ctx, data, mod), LY_SUCCESS);
4229 lys_module_free(mod, NULL);
4230 yin_parser_ctx_free(yin_ctx);
4231 mod = NULL;
4232 yin_ctx = NULL;
4233
4234 mod = calloc(1, sizeof *mod);
4235 mod->ctx = st->ctx;
David Sedlák6d781b62019-08-02 15:22:52 +02004236 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
4237 "<yang-version value=\"1.0\"/>\n"
4238 "<namespace uri=\"urn:example:foo\"/>\n"
4239 "<prefix value=\"foo\"/>\n"
4240 "</module>\n";
4241 assert_int_equal(yin_parse_module(&yin_ctx, data, mod), LY_SUCCESS);
4242 lys_module_free(mod, NULL);
4243 yin_parser_ctx_free(yin_ctx);
4244 mod = NULL;
4245 yin_ctx = NULL;
4246
4247
4248 mod = calloc(1, sizeof *mod);
4249 mod->ctx = st->ctx;
David Sedlák8985a142019-07-31 16:43:06 +02004250 data = "<submodule name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
4251 "</submodule>\n";
4252 assert_int_equal(yin_parse_module(&yin_ctx, data, mod), LY_EINVAL);
4253 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
4254 lys_module_free(mod, NULL);
4255 yin_parser_ctx_free(yin_ctx);
4256
David Sedlák6d781b62019-08-02 15:22:52 +02004257 mod = calloc(1, sizeof *mod);
4258 mod->ctx = st->ctx;
4259 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
4260 "<yang-version value=\"1.0\"/>\n"
4261 "<namespace uri=\"urn:example:foo\"/>\n"
4262 "<prefix value=\"foo\"/>\n"
4263 "</module>"
4264 "<module>";
4265 assert_int_equal(yin_parse_module(&yin_ctx, data, mod), LY_EVALID);
4266 logbuf_assert("Trailing garbage \"<module>\" after module, expected end-of-input. Line number 5.");
4267 lys_module_free(mod, NULL);
4268 yin_parser_ctx_free(yin_ctx);
4269 mod = NULL;
4270 yin_ctx = NULL;
4271
David Sedlák8985a142019-07-31 16:43:06 +02004272 st->finished_correctly = true;
4273}
4274
4275static void
4276test_yin_parse_submodule(void **state)
4277{
4278 struct state *st = *state;
4279 const char *data;
4280 struct yin_parser_ctx *yin_ctx = NULL;
4281 struct lysp_submodule *submod = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02004282 struct lys_parser_ctx main_ctx = {};
David Sedlák8985a142019-07-31 16:43:06 +02004283
4284 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
4285 "<submodule name=\"asub\""
4286 "xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
4287 "xmlns:a=\"urn:a\">"
4288 "<yang-version value=\"1.0\"/>\n"
4289 "<belongs-to module=\"a\">"
4290 "<prefix value=\"a_pref\"/>"
4291 "</belongs-to>"
4292 "<include module=\"atop\"/>"
4293 "<feature name=\"fox\"/>"
4294 "<notification name=\"bar-notif\">"
4295 "<if-feature name=\"bar\"/>"
4296 "</notification>"
4297 "<notification name=\"fox-notif\">"
4298 "<if-feature name=\"fox\"/>"
4299 "</notification>"
4300 "<augment target-node=\"/a_pref:top\">"
4301 "<if-feature name=\"bar\"/>"
4302 "<container name=\"bar-sub\"/>"
4303 "</augment>"
4304 "<augment target-node=\"/top\">"
4305 "<container name=\"bar-sub2\"/>"
4306 "</augment>"
4307 "</submodule>";
David Sedlák1b623122019-08-05 15:27:49 +02004308 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, &main_ctx, data, &submod), LY_SUCCESS);
David Sedlák8985a142019-07-31 16:43:06 +02004309 lysp_submodule_free(st->ctx, submod);
4310 yin_parser_ctx_free(yin_ctx);
4311 yin_ctx = NULL;
4312 submod = NULL;
4313
4314 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
David Sedlák6d781b62019-08-02 15:22:52 +02004315 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
4316 "<yang-version value=\"1.0\"/>\n"
4317 "<belongs-to module=\"a\">"
4318 "<prefix value=\"a_pref\"/>"
4319 "</belongs-to>"
4320 "</submodule>";
David Sedlák1b623122019-08-05 15:27:49 +02004321 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, &main_ctx, data, &submod), LY_SUCCESS);
David Sedlák6d781b62019-08-02 15:22:52 +02004322 lysp_submodule_free(st->ctx, submod);
4323 yin_parser_ctx_free(yin_ctx);
4324 yin_ctx = NULL;
4325 submod = NULL;
4326
4327 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
David Sedlák8985a142019-07-31 16:43:06 +02004328 "<module name=\"inval\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
4329 "</module>";
David Sedlák1b623122019-08-05 15:27:49 +02004330 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, &main_ctx, data, &submod), LY_EINVAL);
David Sedlák8985a142019-07-31 16:43:06 +02004331 logbuf_assert("Input data contains module in situation when a submodule is expected.");
4332 lysp_submodule_free(st->ctx, submod);
4333 yin_parser_ctx_free(yin_ctx);
4334 yin_ctx = NULL;
4335 submod = NULL;
4336
David Sedlák6d781b62019-08-02 15:22:52 +02004337 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
4338 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
4339 "<yang-version value=\"1.0\"/>\n"
4340 "<belongs-to module=\"a\">"
4341 "<prefix value=\"a_pref\"/>"
4342 "</belongs-to>"
4343 "</submodule>"
4344 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
4345 "<yang-version value=\"1.0\"/>\n"
4346 "<belongs-to module=\"a\">"
4347 "<prefix value=\"a_pref\"/>"
4348 "</belongs-to>"
4349 "</submodule>";
David Sedlák1b623122019-08-05 15:27:49 +02004350 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, &main_ctx, data, &submod), LY_EVALID);
David Sedlák6d781b62019-08-02 15:22:52 +02004351 logbuf_assert("Trailing garbage \"<submodule name...\" after submodule, expected end-of-input. Line number 2.");
4352 lysp_submodule_free(st->ctx, submod);
4353 yin_parser_ctx_free(yin_ctx);
4354 yin_ctx = NULL;
4355 submod = NULL;
4356
David Sedlák8985a142019-07-31 16:43:06 +02004357 st->finished_correctly = true;
4358}
4359
David Sedlák3b4db242018-10-19 16:11:01 +02004360int
4361main(void)
4362{
4363
4364 const struct CMUnitTest tests[] = {
David Sedlák8f7a1172019-06-20 14:42:18 +02004365 cmocka_unit_test_setup_teardown(test_yin_match_keyword, setup_f, teardown_f),
David Sedlákb1a78352019-06-28 16:16:29 +02004366 cmocka_unit_test_setup_teardown(test_yin_parse_element_generic, setup_f, teardown_f),
4367 cmocka_unit_test_setup_teardown(test_yin_parse_extension_instance, setup_f, teardown_f),
David Sedlák555c7202019-07-04 12:14:12 +02004368 cmocka_unit_test_setup_teardown(test_yin_parse_content, setup_f, teardown_f),
David Sedlák4a650532019-07-10 11:55:18 +02004369 cmocka_unit_test_setup_teardown(test_validate_value, setup_f, teardown_f),
David Sedlák32488102019-07-15 17:44:10 +02004370
David Sedlák8e7bda82019-07-16 17:57:50 +02004371 cmocka_unit_test(test_yin_match_argument_name),
David Sedlák43801c92019-08-05 15:58:54 +02004372 cmocka_unit_test_setup_teardown(test_enum_elem, setup_element_test, teardown_element_test),
4373 cmocka_unit_test_setup_teardown(test_bit_elem, setup_element_test, teardown_element_test),
David Sedlák32488102019-07-15 17:44:10 +02004374 cmocka_unit_test_setup_teardown(test_meta_elem, setup_element_test, teardown_element_test),
4375 cmocka_unit_test_setup_teardown(test_import_elem, setup_element_test, teardown_element_test),
4376 cmocka_unit_test_setup_teardown(test_status_elem, setup_element_test, teardown_element_test),
4377 cmocka_unit_test_setup_teardown(test_ext_elem, setup_element_test, teardown_element_test),
4378 cmocka_unit_test_setup_teardown(test_yin_element_elem, setup_element_test, teardown_element_test),
4379 cmocka_unit_test_setup_teardown(test_yangversion_elem, setup_element_test, teardown_element_test),
4380 cmocka_unit_test_setup_teardown(test_mandatory_elem, setup_element_test, teardown_element_test),
David Sedlák8e7bda82019-07-16 17:57:50 +02004381 cmocka_unit_test_setup_teardown(test_argument_elem, setup_element_test, teardown_element_test),
4382 cmocka_unit_test_setup_teardown(test_base_elem, setup_element_test, teardown_element_test),
4383 cmocka_unit_test_setup_teardown(test_belongsto_elem, setup_element_test, teardown_element_test),
4384 cmocka_unit_test_setup_teardown(test_config_elem, setup_element_test, teardown_element_test),
4385 cmocka_unit_test_setup_teardown(test_default_elem, setup_element_test, teardown_element_test),
4386 cmocka_unit_test_setup_teardown(test_err_app_tag_elem, setup_element_test, teardown_element_test),
4387 cmocka_unit_test_setup_teardown(test_err_msg_elem, setup_element_test, teardown_element_test),
4388 cmocka_unit_test_setup_teardown(test_fracdigits_elem, setup_element_test, teardown_element_test),
4389 cmocka_unit_test_setup_teardown(test_iffeature_elem, setup_element_test, teardown_element_test),
4390 cmocka_unit_test_setup_teardown(test_length_elem, setup_element_test, teardown_element_test),
4391 cmocka_unit_test_setup_teardown(test_modifier_elem, setup_element_test, teardown_element_test),
4392 cmocka_unit_test_setup_teardown(test_namespace_elem, setup_element_test, teardown_element_test),
4393 cmocka_unit_test_setup_teardown(test_path_elem, setup_element_test, teardown_element_test),
4394 cmocka_unit_test_setup_teardown(test_pattern_elem, setup_element_test, teardown_element_test),
4395 cmocka_unit_test_setup_teardown(test_value_position_elem, setup_element_test, teardown_element_test),
David Sedlák69f01612019-07-17 11:41:08 +02004396 cmocka_unit_test_setup_teardown(test_prefix_elem, setup_element_test, teardown_element_test),
4397 cmocka_unit_test_setup_teardown(test_range_elem, setup_element_test, teardown_element_test),
4398 cmocka_unit_test_setup_teardown(test_reqinstance_elem, setup_element_test, teardown_element_test),
4399 cmocka_unit_test_setup_teardown(test_revision_date_elem, setup_element_test, teardown_element_test),
4400 cmocka_unit_test_setup_teardown(test_unique_elem, setup_element_test, teardown_element_test),
4401 cmocka_unit_test_setup_teardown(test_units_elem, setup_element_test, teardown_element_test),
4402 cmocka_unit_test_setup_teardown(test_when_elem, setup_element_test, teardown_element_test),
4403 cmocka_unit_test_setup_teardown(test_yin_text_value_elem, setup_element_test, teardown_element_test),
David Sedlák374d2b32019-07-17 15:06:55 +02004404 cmocka_unit_test_setup_teardown(test_type_elem, setup_element_test, teardown_element_test),
David Sedlák1af868e2019-07-17 17:03:14 +02004405 cmocka_unit_test_setup_teardown(test_max_elems_elem, setup_element_test, teardown_element_test),
David Sedlák09e18c92019-07-18 11:17:11 +02004406 cmocka_unit_test_setup_teardown(test_min_elems_elem, setup_element_test, teardown_element_test),
David Sedláka2dad212019-07-18 12:45:19 +02004407 cmocka_unit_test_setup_teardown(test_ordby_elem, setup_element_test, teardown_element_test),
David Sedlák8a83bbb2019-07-18 14:46:00 +02004408 cmocka_unit_test_setup_teardown(test_any_elem, setup_element_test, teardown_element_test),
David Sedlák203ca3a2019-07-18 15:26:25 +02004409 cmocka_unit_test_setup_teardown(test_leaf_elem, setup_element_test, teardown_element_test),
David Sedlákc3da3ef2019-07-19 12:56:08 +02004410 cmocka_unit_test_setup_teardown(test_leaf_list_elem, setup_element_test, teardown_element_test),
David Sedlákcb39f642019-07-19 13:19:55 +02004411 cmocka_unit_test_setup_teardown(test_presence_elem, setup_element_test, teardown_element_test),
David Sedlák12470a82019-07-19 13:44:36 +02004412 cmocka_unit_test_setup_teardown(test_key_elem, setup_element_test, teardown_element_test),
David Sedlák04e17b22019-07-19 15:29:48 +02004413 cmocka_unit_test_setup_teardown(test_typedef_elem, setup_element_test, teardown_element_test),
David Sedlákd2d676a2019-07-22 11:28:19 +02004414 cmocka_unit_test_setup_teardown(test_refine_elem, setup_element_test, teardown_element_test),
David Sedlák0d6de5a2019-07-22 13:25:44 +02004415 cmocka_unit_test_setup_teardown(test_uses_elem, setup_element_test, teardown_element_test),
David Sedlákaa854b02019-07-22 14:17:10 +02004416 cmocka_unit_test_setup_teardown(test_revision_elem, setup_element_test, teardown_element_test),
David Sedlák0c2bab92019-07-22 15:33:19 +02004417 cmocka_unit_test_setup_teardown(test_include_elem, setup_element_test, teardown_element_test),
David Sedlák5e13dea2019-07-22 16:06:45 +02004418 cmocka_unit_test_setup_teardown(test_feature_elem, setup_element_test, teardown_element_test),
David Sedlák28794f22019-07-22 16:45:00 +02004419 cmocka_unit_test_setup_teardown(test_identity_elem, setup_element_test, teardown_element_test),
David Sedlákaf536aa2019-07-23 13:42:23 +02004420 cmocka_unit_test_setup_teardown(test_list_elem, setup_element_test, teardown_element_test),
David Sedlák031b9e72019-07-23 15:19:37 +02004421 cmocka_unit_test_setup_teardown(test_notification_elem, setup_element_test, teardown_element_test),
David Sedláke3ce9ef2019-07-23 16:34:30 +02004422 cmocka_unit_test_setup_teardown(test_grouping_elem, setup_element_test, teardown_element_test),
David Sedlákf111bcb2019-07-23 17:15:51 +02004423 cmocka_unit_test_setup_teardown(test_container_elem, setup_element_test, teardown_element_test),
David Sedlák5379d392019-07-24 10:42:03 +02004424 cmocka_unit_test_setup_teardown(test_case_elem, setup_element_test, teardown_element_test),
David Sedlákb7abcfa2019-07-24 12:33:35 +02004425 cmocka_unit_test_setup_teardown(test_choice_elem, setup_element_test, teardown_element_test),
David Sedlák05404f62019-07-24 14:11:53 +02004426 cmocka_unit_test_setup_teardown(test_inout_elem, setup_element_test, teardown_element_test),
David Sedlák85d0eca2019-07-24 15:15:21 +02004427 cmocka_unit_test_setup_teardown(test_action_elem, setup_element_test, teardown_element_test),
David Sedlák992fb7c2019-07-24 16:51:01 +02004428 cmocka_unit_test_setup_teardown(test_augment_elem, setup_element_test, teardown_element_test),
David Sedlák4ffcec82019-07-25 15:10:21 +02004429 cmocka_unit_test_setup_teardown(test_deviate_elem, setup_element_test, teardown_element_test),
David Sedlák8b754462019-07-25 16:22:13 +02004430 cmocka_unit_test_setup_teardown(test_deviation_elem, setup_element_test, teardown_element_test),
David Sedlák4f03b932019-07-26 13:01:47 +02004431 cmocka_unit_test_setup_teardown(test_module_elem, setup_element_test, teardown_element_test),
David Sedlák298ff6d2019-07-26 14:29:03 +02004432 cmocka_unit_test_setup_teardown(test_submodule_elem, setup_element_test, teardown_element_test),
David Sedlák8985a142019-07-31 16:43:06 +02004433
4434 cmocka_unit_test_setup_teardown(test_yin_parse_module, setup_logger, teardown_logger),
4435 cmocka_unit_test_setup_teardown(test_yin_parse_submodule, setup_logger, teardown_logger),
David Sedlák3b4db242018-10-19 16:11:01 +02004436 };
4437
David Sedlák8e7bda82019-07-16 17:57:50 +02004438 return cmocka_run_group_tests(tests, setup_ly_ctx, destroy_ly_ctx);
David Sedlák3b4db242018-10-19 16:11:01 +02004439}