blob: e88d7f8e2ffbc801558a3029a237e00837f774e7 [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
Radek Krejci70593c12020-06-13 20:48:09 +020024#include "common.h"
25#include "parser_internal.h"
26#include "parser_yin.h"
27#include "tree_schema.h"
28#include "tree_schema_internal.h"
29#include "xml.h"
30#include "xpath.h"
David Sedlák3b4db242018-10-19 16:11:01 +020031
David Sedlák555c7202019-07-04 12:14:12 +020032/* prototypes of static functions */
33void lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext);
David Sedlák986cb412019-07-04 13:10:11 +020034void lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext);
David Sedlák32eee7b2019-07-09 12:38:44 +020035void lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when);
David Sedlák32488102019-07-15 17:44:10 +020036void lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type);
David Sedlák8a83bbb2019-07-18 14:46:00 +020037void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
David Sedlák04e17b22019-07-19 15:29:48 +020038void lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf);
David Sedlákd2d676a2019-07-22 11:28:19 +020039void lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref);
David Sedlákaa854b02019-07-22 14:17:10 +020040void lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev);
David Sedlák0c2bab92019-07-22 15:33:19 +020041void lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include);
David Sedlák5e13dea2019-07-22 16:06:45 +020042void lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat);
David Sedlák28794f22019-07-22 16:45:00 +020043void lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident);
David Sedlák031b9e72019-07-23 15:19:37 +020044void lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif);
David Sedláke3ce9ef2019-07-23 16:34:30 +020045void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp);
David Sedlák05404f62019-07-24 14:11:53 +020046void lysp_action_inout_free(struct ly_ctx *ctx, struct lysp_action_inout *inout);
David Sedlák85d0eca2019-07-24 15:15:21 +020047void lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action);
David Sedlák992fb7c2019-07-24 16:51:01 +020048void lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment);
David Sedlák4ffcec82019-07-25 15:10:21 +020049void lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d);
David Sedlák8b754462019-07-25 16:22:13 +020050void lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev);
David Sedlák298ff6d2019-07-26 14:29:03 +020051void lysp_submodule_free(struct ly_ctx *ctx, struct lysp_submodule *submod);
52void lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import);
David Sedlák555c7202019-07-04 12:14:12 +020053
David Sedláke6cd89e2019-08-07 12:46:02 +020054/* wrapping element used for mocking has nothing to do with real module structure */
55#define ELEMENT_WRAPPER_START "<status xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
56#define ELEMENT_WRAPPER_END "</status>"
57
Radek Krejci3a4889a2020-05-19 17:01:58 +020058struct test_parser_yin_state {
David Sedlák3b4db242018-10-19 16:11:01 +020059 struct ly_ctx *ctx;
David Sedlák3017da42019-02-15 09:48:04 +010060 struct lys_module *mod;
David Sedlák619db942019-07-03 14:47:30 +020061 struct lysp_module *lysp_mod;
Michal Vaskob36053d2020-03-26 15:49:30 +010062 struct lys_yin_parser_ctx *yin_ctx;
Michal Vasko63f3d842020-07-08 10:10:14 +020063 struct ly_in *in;
David Sedlák79e50cb2019-06-05 16:33:09 +020064 bool finished_correctly;
David Sedlák68a1af12019-03-08 13:46:54 +010065};
David Sedlák872c7b42018-10-26 13:15:20 +020066
David Sedlák79e50cb2019-06-05 16:33:09 +020067#define BUFSIZE 1024
68char logbuf[BUFSIZE] = {0};
69int store = -1; /* negative for infinite logging, positive for limited logging */
70
71/* set to 0 to printing error messages to stderr instead of checking them in code */
David Sedláke0ef1c62019-09-13 10:05:55 +020072#define ENABLE_LOGGER_CHECKING 1
David Sedlák79e50cb2019-06-05 16:33:09 +020073
74#if ENABLE_LOGGER_CHECKING
75static void
76logger(LY_LOG_LEVEL level, const char *msg, const char *path)
77{
78 (void) level; /* unused */
79 if (store) {
80 if (path && path[0]) {
81 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
82 } else {
83 strncpy(logbuf, msg, BUFSIZE - 1);
84 }
85 if (store > 0) {
86 --store;
87 }
88 }
89}
90#endif
91
92#if ENABLE_LOGGER_CHECKING
93# define logbuf_assert(str) assert_string_equal(logbuf, str)
94#else
95# define logbuf_assert(str)
96#endif
97
98#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
99 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
100 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
101 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
102 CLEANUP
103
David Sedlák8e7bda82019-07-16 17:57:50 +0200104int
105setup_ly_ctx(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +0100106{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200107 struct test_parser_yin_state *st = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +0200108
David Sedlák68a1af12019-03-08 13:46:54 +0100109 /* allocate state variable */
110 (*state) = st = calloc(1, sizeof(*st));
111 if (!st) {
112 fprintf(stderr, "Memmory allocation failed");
113 return EXIT_FAILURE;
114 }
David Sedlák872c7b42018-10-26 13:15:20 +0200115
David Sedlák68a1af12019-03-08 13:46:54 +0100116 /* create new libyang context */
117 ly_ctx_new(NULL, 0, &st->ctx);
David Sedlák872c7b42018-10-26 13:15:20 +0200118
David Sedlák8e7bda82019-07-16 17:57:50 +0200119 return EXIT_SUCCESS;
120}
121
122int
123destroy_ly_ctx(void **state)
124{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200125 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +0200126 ly_ctx_destroy(st->ctx, NULL);
127 free(st);
128
129 return EXIT_SUCCESS;
130}
131
132static int
133setup_f(void **state)
134{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200135 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +0200136
137#if ENABLE_LOGGER_CHECKING
138 /* setup logger */
139 ly_set_log_clb(logger, 1);
140#endif
141
David Sedlák68a1af12019-03-08 13:46:54 +0100142 /* allocate new module */
143 st->mod = calloc(1, sizeof(*st->mod));
144 st->mod->ctx = st->ctx;
145
David Sedlák619db942019-07-03 14:47:30 +0200146 /* allocate new parsed module */
147 st->lysp_mod = calloc(1, sizeof(*st->lysp_mod));
148 st->lysp_mod->mod = calloc(1, sizeof(*st->lysp_mod->mod));
149 st->lysp_mod->mod->ctx = st->ctx;
150
151 /* allocate parser context */
David Sedlákda8ffa32019-07-08 14:17:10 +0200152 st->yin_ctx = calloc(1, sizeof(*st->yin_ctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100153 st->yin_ctx->format = LYS_IN_YIN;
David Sedlák8f5bce02019-06-03 16:41:08 +0200154
Michal Vasko63f3d842020-07-08 10:10:14 +0200155 st->in = NULL;
156
David Sedlák68a1af12019-03-08 13:46:54 +0100157 return EXIT_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +0200158}
159
160static int
David Sedlák68a1af12019-03-08 13:46:54 +0100161teardown_f(void **state)
162{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200163 struct test_parser_yin_state *st = *(struct test_parser_yin_state **)state;
David Sedlák619db942019-07-03 14:47:30 +0200164 struct lys_module *temp;
David Sedlák68a1af12019-03-08 13:46:54 +0100165
David Sedlák79e50cb2019-06-05 16:33:09 +0200166#if ENABLE_LOGGER_CHECKING
167 /* teardown logger */
168 if (!st->finished_correctly && logbuf[0] != '\0') {
169 fprintf(stderr, "%s\n", logbuf);
170 }
171#endif
172
David Sedlák619db942019-07-03 14:47:30 +0200173 temp = st->lysp_mod->mod;
174
Michal Vaskob36053d2020-03-26 15:49:30 +0100175 lyxml_ctx_free(st->yin_ctx->xmlctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100176 lys_module_free(st->mod, NULL);
David Sedlák619db942019-07-03 14:47:30 +0200177 lysp_module_free(st->lysp_mod);
178 lys_module_free(temp, NULL);
David Sedlákda8ffa32019-07-08 14:17:10 +0200179 free(st->yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200180 ly_in_free(st->in, 0);
David Sedlák68a1af12019-03-08 13:46:54 +0100181
182 return EXIT_SUCCESS;
183}
184
Radek Krejci3a4889a2020-05-19 17:01:58 +0200185static struct test_parser_yin_state*
David Sedlák392af4f2019-06-04 16:02:42 +0200186reset_state(void **state)
187{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200188 ((struct test_parser_yin_state *)*state)->finished_correctly = true;
David Sedlák555c7202019-07-04 12:14:12 +0200189 logbuf[0] = '\0';
David Sedlák392af4f2019-06-04 16:02:42 +0200190 teardown_f(state);
191 setup_f(state);
192
193 return *state;
194}
195
David Sedlák79e50cb2019-06-05 16:33:09 +0200196void
197logbuf_clean(void)
198{
199 logbuf[0] = '\0';
200}
201
David Sedlák8985a142019-07-31 16:43:06 +0200202static int
203setup_logger(void **state)
204{
205 (void)state; /* unused */
206#if ENABLE_LOGGER_CHECKING
207 /* setup logger */
208 ly_set_log_clb(logger, 1);
209#endif
210
211 logbuf[0] = '\0';
212
213 return EXIT_SUCCESS;
214}
215
216static int
217teardown_logger(void **state)
218{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200219 struct test_parser_yin_state *st = *state;
David Sedlák8985a142019-07-31 16:43:06 +0200220
221#if ENABLE_LOGGER_CHECKING
222 /* teardown logger */
223 if (!st->finished_correctly && logbuf[0] != '\0') {
224 fprintf(stderr, "%s\n", logbuf);
225 }
226#endif
227
228 return EXIT_SUCCESS;
229}
230
231static int
232setup_element_test(void **state)
233{
234 setup_logger(state);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200235 struct test_parser_yin_state *st = *state;
David Sedlák8985a142019-07-31 16:43:06 +0200236
Michal Vaskoc3781c32020-10-06 14:04:08 +0200237 st->yin_ctx = calloc(1, sizeof *st->yin_ctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100238 st->yin_ctx->format = LYS_IN_YIN;
Michal Vaskoc3781c32020-10-06 14:04:08 +0200239 st->yin_ctx->main_mod = calloc(1, sizeof *st->yin_ctx->main_mod);
240 lydict_insert(st->ctx, "module-name", 0, &st->yin_ctx->main_mod->name);
David Sedlák8985a142019-07-31 16:43:06 +0200241
242 return EXIT_SUCCESS;
243}
244
245static int
246teardown_element_test(void **state)
247{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200248 struct test_parser_yin_state *st = *(struct test_parser_yin_state **)state;
David Sedlák8985a142019-07-31 16:43:06 +0200249
Michal Vaskoc3781c32020-10-06 14:04:08 +0200250 lydict_remove(st->ctx, st->yin_ctx->main_mod->name);
251 free(st->yin_ctx->main_mod);
Michal Vaskob36053d2020-03-26 15:49:30 +0100252 lyxml_ctx_free(st->yin_ctx->xmlctx);
David Sedlák8985a142019-07-31 16:43:06 +0200253 free(st->yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200254 ly_in_free(st->in, 0);
David Sedlák8985a142019-07-31 16:43:06 +0200255
256 teardown_logger(state);
257
258 return EXIT_SUCCESS;
259}
260
David Sedlák68a1af12019-03-08 13:46:54 +0100261static void
David Sedlák1bccdfa2019-06-17 15:55:27 +0200262test_yin_match_keyword(void **state)
David Sedlák3b4db242018-10-19 16:11:01 +0200263{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200264 struct test_parser_yin_state *st = *state;
David Sedlák3b4db242018-10-19 16:11:01 +0200265
Michal Vaskob36053d2020-03-26 15:49:30 +0100266 const char *prefix;
267 size_t prefix_len;
David Sedlák8f7a1172019-06-20 14:42:18 +0200268 /* create mock yin namespace in xml context */
269 const char *data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" />";
Michal Vasko63f3d842020-07-08 10:10:14 +0200270 ly_in_new_memory(data, &st->in);
271 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100272 prefix = st->yin_ctx->xmlctx->prefix;
273 prefix_len = st->yin_ctx->xmlctx->prefix_len;
David Sedlák8f7a1172019-06-20 14:42:18 +0200274
Radek Krejcid6b76452019-09-03 17:03:03 +0200275 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydatax", strlen("anydatax"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
276 assert_int_equal(yin_match_keyword(st->yin_ctx, "asdasd", strlen("asdasd"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
277 assert_int_equal(yin_match_keyword(st->yin_ctx, "", 0, prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
278 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydata", strlen("anydata"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ANYDATA);
279 assert_int_equal(yin_match_keyword(st->yin_ctx, "anyxml", strlen("anyxml"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ANYXML);
280 assert_int_equal(yin_match_keyword(st->yin_ctx, "argument", strlen("argument"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ARGUMENT);
281 assert_int_equal(yin_match_keyword(st->yin_ctx, "augment", strlen("augment"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_AUGMENT);
282 assert_int_equal(yin_match_keyword(st->yin_ctx, "base", strlen("base"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BASE);
283 assert_int_equal(yin_match_keyword(st->yin_ctx, "belongs-to", strlen("belongs-to"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BELONGS_TO);
284 assert_int_equal(yin_match_keyword(st->yin_ctx, "bit", strlen("bit"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BIT);
285 assert_int_equal(yin_match_keyword(st->yin_ctx, "case", strlen("case"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CASE);
286 assert_int_equal(yin_match_keyword(st->yin_ctx, "choice", strlen("choice"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CHOICE);
287 assert_int_equal(yin_match_keyword(st->yin_ctx, "config", strlen("config"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONFIG);
288 assert_int_equal(yin_match_keyword(st->yin_ctx, "contact", strlen("contact"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONTACT);
289 assert_int_equal(yin_match_keyword(st->yin_ctx, "container", strlen("container"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONTAINER);
290 assert_int_equal(yin_match_keyword(st->yin_ctx, "default", strlen("default"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEFAULT);
291 assert_int_equal(yin_match_keyword(st->yin_ctx, "description", strlen("description"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DESCRIPTION);
292 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviate", strlen("deviate"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEVIATE);
293 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviation", strlen("deviation"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEVIATION);
294 assert_int_equal(yin_match_keyword(st->yin_ctx, "enum", strlen("enum"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ENUM);
295 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);
296 assert_int_equal(yin_match_keyword(st->yin_ctx, "error-message", strlen("error-message"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ERROR_MESSAGE);
297 assert_int_equal(yin_match_keyword(st->yin_ctx, "extension", strlen("extension"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_EXTENSION);
298 assert_int_equal(yin_match_keyword(st->yin_ctx, "feature", strlen("feature"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_FEATURE);
299 assert_int_equal(yin_match_keyword(st->yin_ctx, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_FRACTION_DIGITS);
300 assert_int_equal(yin_match_keyword(st->yin_ctx, "grouping", strlen("grouping"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_GROUPING);
301 assert_int_equal(yin_match_keyword(st->yin_ctx, "identity", strlen("identity"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IDENTITY);
302 assert_int_equal(yin_match_keyword(st->yin_ctx, "if-feature", strlen("if-feature"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IF_FEATURE);
303 assert_int_equal(yin_match_keyword(st->yin_ctx, "import", strlen("import"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IMPORT);
304 assert_int_equal(yin_match_keyword(st->yin_ctx, "include", strlen("include"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_INCLUDE);
305 assert_int_equal(yin_match_keyword(st->yin_ctx, "input", strlen("input"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_INPUT);
306 assert_int_equal(yin_match_keyword(st->yin_ctx, "key", strlen("key"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_KEY);
307 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf", strlen("leaf"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LEAF);
308 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf-list", strlen("leaf-list"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LEAF_LIST);
309 assert_int_equal(yin_match_keyword(st->yin_ctx, "length", strlen("length"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LENGTH);
310 assert_int_equal(yin_match_keyword(st->yin_ctx, "list", strlen("list"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LIST);
311 assert_int_equal(yin_match_keyword(st->yin_ctx, "mandatory", strlen("mandatory"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MANDATORY);
312 assert_int_equal(yin_match_keyword(st->yin_ctx, "max-elements", strlen("max-elements"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MAX_ELEMENTS);
313 assert_int_equal(yin_match_keyword(st->yin_ctx, "min-elements", strlen("min-elements"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MIN_ELEMENTS);
314 assert_int_equal(yin_match_keyword(st->yin_ctx, "modifier", strlen("modifier"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MODIFIER);
315 assert_int_equal(yin_match_keyword(st->yin_ctx, "module", strlen("module"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MODULE);
316 assert_int_equal(yin_match_keyword(st->yin_ctx, "must", strlen("must"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MUST);
317 assert_int_equal(yin_match_keyword(st->yin_ctx, "namespace", strlen("namespace"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NAMESPACE);
318 assert_int_equal(yin_match_keyword(st->yin_ctx, "notification", strlen("notification"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NOTIFICATION);
319 assert_int_equal(yin_match_keyword(st->yin_ctx, "ordered-by", strlen("ordered-by"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ORDERED_BY);
320 assert_int_equal(yin_match_keyword(st->yin_ctx, "organization", strlen("organization"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ORGANIZATION);
321 assert_int_equal(yin_match_keyword(st->yin_ctx, "output", strlen("output"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_OUTPUT);
322 assert_int_equal(yin_match_keyword(st->yin_ctx, "path", strlen("path"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PATH);
323 assert_int_equal(yin_match_keyword(st->yin_ctx, "pattern", strlen("pattern"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PATTERN);
324 assert_int_equal(yin_match_keyword(st->yin_ctx, "position", strlen("position"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_POSITION);
325 assert_int_equal(yin_match_keyword(st->yin_ctx, "prefix", strlen("prefix"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PREFIX);
326 assert_int_equal(yin_match_keyword(st->yin_ctx, "presence", strlen("presence"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PRESENCE);
327 assert_int_equal(yin_match_keyword(st->yin_ctx, "range", strlen("range"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_RANGE);
328 assert_int_equal(yin_match_keyword(st->yin_ctx, "reference", strlen("reference"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REFERENCE);
329 assert_int_equal(yin_match_keyword(st->yin_ctx, "refine", strlen("refine"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REFINE);
330 assert_int_equal(yin_match_keyword(st->yin_ctx, "require-instance", strlen("require-instance"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REQUIRE_INSTANCE);
331 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision", strlen("revision"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REVISION);
332 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision-date", strlen("revision-date"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REVISION_DATE);
333 assert_int_equal(yin_match_keyword(st->yin_ctx, "rpc", strlen("rpc"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_RPC);
334 assert_int_equal(yin_match_keyword(st->yin_ctx, "status", strlen("status"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_STATUS);
335 assert_int_equal(yin_match_keyword(st->yin_ctx, "submodule", strlen("submodule"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_SUBMODULE);
336 assert_int_equal(yin_match_keyword(st->yin_ctx, "type", strlen("type"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_TYPE);
337 assert_int_equal(yin_match_keyword(st->yin_ctx, "typedef", strlen("typedef"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_TYPEDEF);
338 assert_int_equal(yin_match_keyword(st->yin_ctx, "unique", strlen("unique"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_UNIQUE);
339 assert_int_equal(yin_match_keyword(st->yin_ctx, "units", strlen("units"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_UNITS);
340 assert_int_equal(yin_match_keyword(st->yin_ctx, "uses", strlen("uses"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_USES);
341 assert_int_equal(yin_match_keyword(st->yin_ctx, "value", strlen("value"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_VALUE);
342 assert_int_equal(yin_match_keyword(st->yin_ctx, "when", strlen("when"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_WHEN);
343 assert_int_equal(yin_match_keyword(st->yin_ctx, "yang-version", strlen("yang-version"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_YANG_VERSION);
344 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 +0200345
346 st->finished_correctly = true;
David Sedlák872c7b42018-10-26 13:15:20 +0200347}
David Sedlák3b4db242018-10-19 16:11:01 +0200348
David Sedlák872c7b42018-10-26 13:15:20 +0200349static void
David Sedlák060b00e2019-06-19 11:12:06 +0200350test_yin_match_argument_name(void **state)
David Sedlák872c7b42018-10-26 13:15:20 +0200351{
David Sedlák68a1af12019-03-08 13:46:54 +0100352 (void)state; /* unused */
David Sedlák872c7b42018-10-26 13:15:20 +0200353
David Sedlák060b00e2019-06-19 11:12:06 +0200354 assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN);
355 assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN);
356 assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN);
357 assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION);
358 assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE);
359 assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE);
360 assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME);
361 assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG);
362 assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
363 assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT);
364 assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI);
365 assert_int_equal(yin_match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200366}
367
David Sedlák68a1af12019-03-08 13:46:54 +0100368static void
David Sedlákb1a78352019-06-28 16:16:29 +0200369test_yin_parse_element_generic(void **state)
370{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200371 struct test_parser_yin_state *st = *state;
David Sedlákb1a78352019-06-28 16:16:29 +0200372 struct lysp_ext_instance exts;
David Sedlákb1a78352019-06-28 16:16:29 +0200373 LY_ERR ret;
374
375 memset(&exts, 0, sizeof(exts));
376
David Sedlákb0ca07d2019-09-11 11:54:05 +0200377 const char *data = "<myext:elem attr=\"value\" xmlns:myext=\"urn:example:extensions\">text_value</myext:elem>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200378 ly_in_new_memory(data, &st->in);
379 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100380
381 ret = yin_parse_element_generic(st->yin_ctx, LY_STMT_EXTENSION_INSTANCE, &exts.child);
David Sedlákb1a78352019-06-28 16:16:29 +0200382 assert_int_equal(ret, LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +0100383 assert_int_equal(st->yin_ctx->xmlctx->status, LYXML_ELEM_CLOSE);
David Sedláke0ef1c62019-09-13 10:05:55 +0200384 assert_string_equal(exts.child->stmt, "urn:example:extensions:elem");
David Sedlákb1a78352019-06-28 16:16:29 +0200385 assert_string_equal(exts.child->arg, "text_value");
David Sedlákb1a78352019-06-28 16:16:29 +0200386 assert_string_equal(exts.child->child->stmt, "attr");
387 assert_string_equal(exts.child->child->arg, "value");
388 assert_true(exts.child->child->flags & LYS_YIN_ATTR);
David Sedlákb1a78352019-06-28 16:16:29 +0200389 lysp_ext_instance_free(st->ctx, &exts);
David Sedlák5392a212019-07-01 09:19:10 +0200390 st = reset_state(state);
391
David Sedlákb0ca07d2019-09-11 11:54:05 +0200392 data = "<myext:elem xmlns:myext=\"urn:example:extensions\"></myext:elem>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200393 ly_in_new_memory(data, &st->in);
394 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100395
396 ret = yin_parse_element_generic(st->yin_ctx, LY_STMT_EXTENSION_INSTANCE, &exts.child);
David Sedlák5392a212019-07-01 09:19:10 +0200397 assert_int_equal(ret, LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +0100398 assert_int_equal(st->yin_ctx->xmlctx->status, LYXML_ELEM_CLOSE);
David Sedláke0ef1c62019-09-13 10:05:55 +0200399 assert_string_equal(exts.child->stmt, "urn:example:extensions:elem");
David Sedlák5392a212019-07-01 09:19:10 +0200400 assert_null(exts.child->child);
401 assert_null(exts.child->arg);
402 lysp_ext_instance_free(st->ctx, &exts);
403
David Sedlákb1a78352019-06-28 16:16:29 +0200404 st->finished_correctly = true;
405}
406
407static void
408test_yin_parse_extension_instance(void **state)
409{
410 LY_ERR ret;
Radek Krejci3a4889a2020-05-19 17:01:58 +0200411 struct test_parser_yin_state *st = *state;
David Sedlákb1a78352019-06-28 16:16:29 +0200412 struct lysp_ext_instance *exts = NULL;
David Sedlákb0ca07d2019-09-11 11:54:05 +0200413 const char *data = "<myext:ext value1=\"test\" value=\"test2\" xmlns:myext=\"urn:example:extensions\"><myext:subelem>text</myext:subelem></myext:ext>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200414 ly_in_new_memory(data, &st->in);
415 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100416
417 ret = yin_parse_extension_instance(st->yin_ctx, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200418 assert_int_equal(ret, LY_SUCCESS);
David Sedláke0ef1c62019-09-13 10:05:55 +0200419 assert_string_equal(exts->name, "urn:example:extensions:ext");
David Sedlákb1a78352019-06-28 16:16:29 +0200420 assert_int_equal(exts->insubstmt_index, 0);
421 assert_true(exts->insubstmt == LYEXT_SUBSTMT_CONTACT);
422 assert_true(exts->yin & LYS_YIN);
423 assert_string_equal(exts->child->stmt, "value1");
424 assert_string_equal(exts->child->arg, "test");
425 assert_null(exts->child->child);
426 assert_true(exts->child->flags & LYS_YIN_ATTR);
427 assert_string_equal(exts->child->next->stmt, "value");
428 assert_string_equal(exts->child->next->arg, "test2");
429 assert_null(exts->child->next->child);
430 assert_true(exts->child->next->flags & LYS_YIN_ATTR);
431
David Sedláke0ef1c62019-09-13 10:05:55 +0200432 assert_string_equal(exts->child->next->next->stmt, "urn:example:extensions:subelem");
David Sedlákb1a78352019-06-28 16:16:29 +0200433 assert_string_equal(exts->child->next->next->arg, "text");
434 assert_null(exts->child->next->next->child);
435 assert_null(exts->child->next->next->next);
436 assert_false(exts->child->next->next->flags & LYS_YIN_ATTR);
David Sedlákb1a78352019-06-28 16:16:29 +0200437 lysp_ext_instance_free(st->ctx, exts);
438 LY_ARRAY_FREE(exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200439 exts = NULL;
David Sedlákf250ecf2019-07-01 11:02:05 +0200440 st = reset_state(state);
441
David Sedlákb0ca07d2019-09-11 11:54:05 +0200442 data = "<myext:extension-elem xmlns:myext=\"urn:example:extensions\" />";
Michal Vasko63f3d842020-07-08 10:10:14 +0200443 ly_in_new_memory(data, &st->in);
444 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100445
446 ret = yin_parse_extension_instance(st->yin_ctx, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200447 assert_int_equal(ret, LY_SUCCESS);
David Sedláke0ef1c62019-09-13 10:05:55 +0200448 assert_string_equal(exts->name, "urn:example:extensions:extension-elem");
David Sedlákf250ecf2019-07-01 11:02:05 +0200449 assert_null(exts->argument);
450 assert_null(exts->child);
451 assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT);
452 assert_int_equal(exts->insubstmt_index, 0);
453 assert_true(exts->yin & LYS_YIN);
David Sedlákf250ecf2019-07-01 11:02:05 +0200454 lysp_ext_instance_free(st->ctx, exts);
455 LY_ARRAY_FREE(exts);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200456 exts = NULL;
David Sedlákadd0c2e2019-08-16 10:49:12 +0200457 st = reset_state(state);
458
David Sedlákb0ca07d2019-09-11 11:54:05 +0200459 data = "<myext:ext attr1=\"text1\" attr2=\"text2\" xmlns:myext=\"urn:example:extensions\">"
460 "<myext:ext-sub1/>"
461 "<myext:ext-sub2 sattr1=\"stext2\">"
462 "<myext:ext-sub21>"
463 "<myext:ext-sub211 sattr21=\"text21\"/>"
464 "</myext:ext-sub21>"
465 "</myext:ext-sub2>"
466 "<myext:ext-sub3 attr3=\"text3\"></myext:ext-sub3>"
467 "</myext:ext>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200468 ly_in_new_memory(data, &st->in);
469 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100470
471 ret = yin_parse_extension_instance(st->yin_ctx, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200472 assert_int_equal(ret, LY_SUCCESS);
473
David Sedláke0ef1c62019-09-13 10:05:55 +0200474 assert_string_equal(exts->name, "urn:example:extensions:ext");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200475 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
David Sedláke0ef1c62019-09-13 10:05:55 +0200489 assert_string_equal(exts->child->next->next->stmt, "urn:example:extensions:ext-sub1");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200490 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
David Sedláke0ef1c62019-09-13 10:05:55 +0200494 assert_string_equal(exts->child->next->next->next->stmt, "urn:example:extensions:ext-sub2");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200495 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
David Sedláke0ef1c62019-09-13 10:05:55 +0200502 assert_string_equal(exts->child->next->next->next->child->next->stmt, "urn:example:extensions:ext-sub21");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200503 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
David Sedláke0ef1c62019-09-13 10:05:55 +0200507 assert_string_equal(exts->child->next->next->next->child->next->child->stmt, "urn:example:extensions:ext-sub211");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200508 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
David Sedláke0ef1c62019-09-13 10:05:55 +0200518 assert_string_equal(exts->child->next->next->next->next->stmt, "urn:example:extensions:ext-sub3");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200519 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
David Sedlákadd0c2e2019-08-16 10:49:12 +0200529 lysp_ext_instance_free(st->ctx, exts);
530 LY_ARRAY_FREE(exts);
531 exts = NULL;
David Sedlákaa98bba2019-09-12 11:52:14 +0200532 st = reset_state(state);
533
534 data = "<myext:extension-elem xmlns:myext=\"urn:example:extensions\" xmlns:yin=\"urn:ietf:params:xml:ns:yang:yin:1\">"
535 "<yin:action name=\"act-name\" pre:prefixed=\"ignored\"/>"
536 "<yin:augment target-node=\"target\"/>"
537 "<yin:status value=\"value\"/>"
538 "<yin:include module=\"mod\"/>"
539 "<yin:input />"
540 "<yin:must condition=\"cond\"/>"
541 "<yin:namespace uri=\"uri\"/>"
542 "<yin:revision date=\"data\"/>"
543 "<yin:unique tag=\"tag\"/>"
David Sedlákd2844882019-09-13 16:01:22 +0200544 "<yin:description><yin:text>contact-val</yin:text></yin:description>"
545 "<yin:error-message><yin:value>err-msg</yin:value></yin:error-message>"
David Sedlákaa98bba2019-09-12 11:52:14 +0200546 "</myext:extension-elem>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200547 ly_in_new_memory(data, &st->in);
548 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100549
550 ret = yin_parse_extension_instance(st->yin_ctx, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákaa98bba2019-09-12 11:52:14 +0200551 assert_int_equal(ret, LY_SUCCESS);
552 assert_string_equal(exts->child->arg, "act-name");
553 assert_string_equal(exts->child->next->arg, "target");
554 assert_string_equal(exts->child->next->next->arg, "value");
555 assert_string_equal(exts->child->next->next->next->arg, "mod");
556 assert_null(exts->child->next->next->next->next->arg);
557 assert_string_equal(exts->child->next->next->next->next->next->arg, "cond");
558 assert_string_equal(exts->child->next->next->next->next->next->next->arg, "uri");
559 assert_string_equal(exts->child->next->next->next->next->next->next->next->arg, "data");
560 assert_string_equal(exts->child->next->next->next->next->next->next->next->next->arg, "tag");
561 assert_string_equal(exts->child->next->next->next->next->next->next->next->next->next->arg, "contact-val");
David Sedlákaa98bba2019-09-12 11:52:14 +0200562 lysp_ext_instance_free(st->ctx, exts);
563 LY_ARRAY_FREE(exts);
564 exts = NULL;
David Sedlákaa98bba2019-09-12 11:52:14 +0200565 st = reset_state(state);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200566
David Sedlákb1a78352019-06-28 16:16:29 +0200567 st->finished_correctly = true;
568}
569
David Sedlák555c7202019-07-04 12:14:12 +0200570static void
571test_yin_parse_content(void **state)
572{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200573 struct test_parser_yin_state *st = *state;
David Sedlák555c7202019-07-04 12:14:12 +0200574 LY_ERR ret = LY_SUCCESS;
David Sedlák555c7202019-07-04 12:14:12 +0200575 const char *data = "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlákb0ca07d2019-09-11 11:54:05 +0200576 "<myext:custom xmlns:myext=\"urn:example:extensions\">"
David Sedlák555c7202019-07-04 12:14:12 +0200577 "totally amazing extension"
David Sedlákb0ca07d2019-09-11 11:54:05 +0200578 "</myext:custom>"
David Sedlákaa98bba2019-09-12 11:52:14 +0200579 "<extension name=\"ext\">"
David Sedlák986cb412019-07-04 13:10:11 +0200580 "<argument name=\"argname\"></argument>"
581 "<description><text>desc</text></description>"
582 "<reference><text>ref</text></reference>"
583 "<status value=\"deprecated\"></status>"
David Sedlákaa98bba2019-09-12 11:52:14 +0200584 "</extension>"
David Sedlák555c7202019-07-04 12:14:12 +0200585 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedlák32488102019-07-15 17:44:10 +0200586 "<if-feature name=\"foo\"></if-feature>"
David Sedlák32eee7b2019-07-09 12:38:44 +0200587 "<when condition=\"condition...\">"
588 "<reference><text>when_ref</text></reference>"
589 "<description><text>when_desc</text></description>"
590 "</when>"
David Sedláke1a30302019-07-10 13:49:38 +0200591 "<config value=\"true\"/>"
David Sedlákc1771b12019-07-10 15:55:46 +0200592 "<error-message>"
593 "<value>error-msg</value>"
594 "</error-message>"
David Sedlák2ce1be62019-07-10 16:15:09 +0200595 "<error-app-tag value=\"err-app-tag\"/>"
David Sedláka5b1d382019-07-10 16:31:09 +0200596 "<units name=\"radians\"></units>"
David Sedláke7084ce2019-07-10 16:44:15 +0200597 "<default value=\"default-value\"/>"
David Sedlák5545f5d2019-07-11 11:55:16 +0200598 "<position value=\"25\"></position>"
599 "<value value=\"-5\"/>"
David Sedlákcf5569a2019-07-11 13:31:34 +0200600 "<require-instance value=\"true\"></require-instance>"
David Sedlákb7296dd2019-07-11 14:58:38 +0200601 "<range value=\"5..10\" />"
David Sedlák438ae432019-07-11 15:36:54 +0200602 "<length value=\"baf\"/>"
David Sedlákd3983112019-07-12 11:20:56 +0200603 "<pattern value='pattern'>"
604 "<modifier value='invert-match'/>"
605 "</pattern>"
David Sedlákfd5b9c32019-07-12 15:33:13 +0200606 "<enum name=\"yay\">"
607 "</enum>"
David Sedlák555c7202019-07-04 12:14:12 +0200608 "</prefix>";
609 struct lysp_ext_instance *exts = NULL;
David Sedlák5f8191e2019-07-08 16:35:52 +0200610 const char **if_features = NULL;
Michal Vasko7f45cf22020-10-01 12:49:44 +0200611 const char *value, *err_msg, *app_tag, *units;
612 struct lysp_qname def = {0};
David Sedlák986cb412019-07-04 13:10:11 +0200613 struct lysp_ext *ext_def = NULL;
David Sedlák32eee7b2019-07-09 12:38:44 +0200614 struct lysp_when *when_p = NULL;
David Sedlákcf5569a2019-07-11 13:31:34 +0200615 struct lysp_type_enum pos_enum = {}, val_enum = {};
David Sedlákfd5b9c32019-07-12 15:33:13 +0200616 struct lysp_type req_type = {}, range_type = {}, len_type = {}, patter_type = {}, enum_type = {};
David Sedláke1a30302019-07-10 13:49:38 +0200617 uint8_t config = 0;
David Sedlák555c7202019-07-04 12:14:12 +0200618
Michal Vasko63f3d842020-07-08 10:10:14 +0200619 ly_in_new_memory(data, &st->in);
620 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100621 lyxml_ctx_next(st->yin_ctx->xmlctx);
622 lyxml_ctx_next(st->yin_ctx->xmlctx);
623 lyxml_ctx_next(st->yin_ctx->xmlctx);
David Sedlák555c7202019-07-04 12:14:12 +0200624
David Sedlákfd5b9c32019-07-12 15:33:13 +0200625 struct yin_subelement subelems[17] = {
Radek Krejcid6b76452019-09-03 17:03:03 +0200626 {LY_STMT_CONFIG, &config, 0},
627 {LY_STMT_DEFAULT, &def, YIN_SUBELEM_UNIQUE},
628 {LY_STMT_ENUM, &enum_type, 0},
629 {LY_STMT_ERROR_APP_TAG, &app_tag, YIN_SUBELEM_UNIQUE},
630 {LY_STMT_ERROR_MESSAGE, &err_msg, 0},
631 {LY_STMT_EXTENSION, &ext_def, 0},
632 {LY_STMT_IF_FEATURE, &if_features, 0},
633 {LY_STMT_LENGTH, &len_type, 0},
634 {LY_STMT_PATTERN, &patter_type, 0},
635 {LY_STMT_POSITION, &pos_enum, 0},
636 {LY_STMT_RANGE, &range_type, 0},
637 {LY_STMT_REQUIRE_INSTANCE, &req_type, 0},
638 {LY_STMT_UNITS, &units, YIN_SUBELEM_UNIQUE},
639 {LY_STMT_VALUE, &val_enum, 0},
640 {LY_STMT_WHEN, &when_p, 0},
641 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
642 {LY_STMT_ARG_TEXT, &value, 0}
David Sedlákd3983112019-07-12 11:20:56 +0200643 };
Michal Vaskob36053d2020-03-26 15:49:30 +0100644 ret = yin_parse_content(st->yin_ctx, subelems, 17, LY_STMT_PREFIX, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200645 assert_int_equal(ret, LY_SUCCESS);
David Sedlák2ce1be62019-07-10 16:15:09 +0200646 /* check parsed values */
Michal Vasko7f45cf22020-10-01 12:49:44 +0200647 assert_string_equal(def.str, "default-value");
David Sedláke0ef1c62019-09-13 10:05:55 +0200648 assert_string_equal(exts->name, "urn:example:extensions:custom");
David Sedlák555c7202019-07-04 12:14:12 +0200649 assert_string_equal(exts->argument, "totally amazing extension");
650 assert_string_equal(value, "wsefsdf");
David Sedláka5b1d382019-07-10 16:31:09 +0200651 assert_string_equal(units, "radians");
David Sedlák32eee7b2019-07-09 12:38:44 +0200652 assert_string_equal(when_p->cond, "condition...");
653 assert_string_equal(when_p->dsc, "when_desc");
654 assert_string_equal(when_p->ref, "when_ref");
David Sedláke1a30302019-07-10 13:49:38 +0200655 assert_int_equal(config, LYS_CONFIG_W);
David Sedlák5545f5d2019-07-11 11:55:16 +0200656 assert_int_equal(pos_enum.value, 25);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200657 assert_true(pos_enum.flags & LYS_SET_VALUE);
David Sedlák5545f5d2019-07-11 11:55:16 +0200658 assert_int_equal(val_enum.value, -5);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200659 assert_true(val_enum.flags & LYS_SET_VALUE);
David Sedlákcf5569a2019-07-11 13:31:34 +0200660 assert_int_equal(req_type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200661 assert_true(req_type.flags &= LYS_SET_REQINST);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200662 assert_string_equal(range_type.range->arg.str, "5..10");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200663 assert_true(range_type.flags & LYS_SET_RANGE);
David Sedlákc1771b12019-07-10 15:55:46 +0200664 assert_string_equal(err_msg, "error-msg");
David Sedlák2ce1be62019-07-10 16:15:09 +0200665 assert_string_equal(app_tag, "err-app-tag");
David Sedlákfd5b9c32019-07-12 15:33:13 +0200666 assert_string_equal(enum_type.enums->name, "yay");
Michal Vasko7f45cf22020-10-01 12:49:44 +0200667 assert_string_equal(len_type.length->arg.str, "baf");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200668 assert_true(len_type.flags & LYS_SET_LENGTH);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200669 assert_string_equal(patter_type.patterns->arg.str, "\x015pattern");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200670 assert_true(patter_type.flags & LYS_SET_PATTERN);
David Sedlák2ce1be62019-07-10 16:15:09 +0200671 /* cleanup */
David Sedlák555c7202019-07-04 12:14:12 +0200672 lysp_ext_instance_free(st->ctx, exts);
David Sedlák32eee7b2019-07-09 12:38:44 +0200673 lysp_when_free(st->ctx, when_p);
David Sedlák986cb412019-07-04 13:10:11 +0200674 lysp_ext_free(st->ctx, ext_def);
David Sedlák5f8191e2019-07-08 16:35:52 +0200675 FREE_STRING(st->ctx, *if_features);
David Sedlákc1771b12019-07-10 15:55:46 +0200676 FREE_STRING(st->ctx, err_msg);
David Sedlák2ce1be62019-07-10 16:15:09 +0200677 FREE_STRING(st->ctx, app_tag);
David Sedláka5b1d382019-07-10 16:31:09 +0200678 FREE_STRING(st->ctx, units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200679 FREE_STRING(st->ctx, patter_type.patterns->arg.str);
680 FREE_STRING(st->ctx, def.str);
681 FREE_STRING(st->ctx, range_type.range->arg.str);
682 FREE_STRING(st->ctx, len_type.length->arg.str);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200683 FREE_STRING(st->ctx, enum_type.enums->name);
David Sedlákb7296dd2019-07-11 14:58:38 +0200684 FREE_STRING(st->ctx, value);
David Sedlák5f8191e2019-07-08 16:35:52 +0200685 LY_ARRAY_FREE(if_features);
David Sedlák555c7202019-07-04 12:14:12 +0200686 LY_ARRAY_FREE(exts);
David Sedlák986cb412019-07-04 13:10:11 +0200687 LY_ARRAY_FREE(ext_def);
David Sedlákd3983112019-07-12 11:20:56 +0200688 LY_ARRAY_FREE(patter_type.patterns);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200689 LY_ARRAY_FREE(enum_type.enums);
David Sedlák32eee7b2019-07-09 12:38:44 +0200690 free(when_p);
David Sedlákb7296dd2019-07-11 14:58:38 +0200691 free(range_type.range);
David Sedlák438ae432019-07-11 15:36:54 +0200692 free(len_type.length);
David Sedlák555c7202019-07-04 12:14:12 +0200693 st = reset_state(state);
694
695 /* test unique subelem */
696 const char *prefix_value;
Radek Krejcid6b76452019-09-03 17:03:03 +0200697 struct yin_subelement subelems2[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE},
698 {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_UNIQUE}};
David Sedláke6cd89e2019-08-07 12:46:02 +0200699 data = ELEMENT_WRAPPER_START
David Sedlák555c7202019-07-04 12:14:12 +0200700 "<prefix value=\"inv_mod\" />"
701 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
702 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedláke6cd89e2019-08-07 12:46:02 +0200703 ELEMENT_WRAPPER_END;
Michal Vasko63f3d842020-07-08 10:10:14 +0200704 ly_in_new_memory(data, &st->in);
705 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100706 lyxml_ctx_next(st->yin_ctx->xmlctx);
707
708 ret = yin_parse_content(st->yin_ctx, subelems2, 2, 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("Redefinition of \"text\" sub-element in \"status\" element. Line number 1.");
David Sedlák555c7202019-07-04 12:14:12 +0200711 lydict_remove(st->ctx, prefix_value);
712 lydict_remove(st->ctx, value);
713 st = reset_state(state);
David Sedlák555c7202019-07-04 12:14:12 +0200714
715 /* test first subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200716 data = ELEMENT_WRAPPER_START
David Sedlák555c7202019-07-04 12:14:12 +0200717 "<prefix value=\"inv_mod\" />"
718 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
719 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedláke6cd89e2019-08-07 12:46:02 +0200720 ELEMENT_WRAPPER_END;
Radek Krejcid6b76452019-09-03 17:03:03 +0200721 struct yin_subelement subelems3[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE},
722 {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_FIRST}};
Michal Vasko63f3d842020-07-08 10:10:14 +0200723 ly_in_new_memory(data, &st->in);
724 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100725 lyxml_ctx_next(st->yin_ctx->xmlctx);
726
727 ret = yin_parse_content(st->yin_ctx, subelems3, 2, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200728 assert_int_equal(ret, LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200729 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 +0200730 lydict_remove(st->ctx, prefix_value);
731 st = reset_state(state);
David Sedlák555c7202019-07-04 12:14:12 +0200732
733 /* test mandatory subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200734 data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END;
Radek Krejcid6b76452019-09-03 17:03:03 +0200735 struct yin_subelement subelems4[1] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}};
Michal Vasko63f3d842020-07-08 10:10:14 +0200736 ly_in_new_memory(data, &st->in);
737 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100738 lyxml_ctx_next(st->yin_ctx->xmlctx);
739
740 ret = yin_parse_content(st->yin_ctx, subelems4, 1, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200741 assert_int_equal(ret, LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200742 logbuf_assert("Missing mandatory sub-element \"prefix\" of \"status\" element. Line number 1.");
David Sedlák555c7202019-07-04 12:14:12 +0200743
744 st->finished_correctly = true;
745}
746
David Sedlák92147b02019-07-09 14:01:01 +0200747static void
David Sedlák4a650532019-07-10 11:55:18 +0200748test_validate_value(void **state)
749{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200750 struct test_parser_yin_state *st = *state;
Michal Vaskob36053d2020-03-26 15:49:30 +0100751 const char *data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END;
752
753 /* create some XML context */
Michal Vasko63f3d842020-07-08 10:10:14 +0200754 ly_in_new_memory(data, &st->in);
755 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100756 st->yin_ctx->xmlctx->status = LYXML_ELEM_CONTENT;
757 st->yin_ctx->xmlctx->dynamic = 0;
758
759 st->yin_ctx->xmlctx->value = "#invalid";
760 st->yin_ctx->xmlctx->value_len = 8;
761 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG), LY_EVALID);
Radek Krejci1deb5be2020-08-26 16:43:36 +0200762 logbuf_assert("Invalid identifier character '#' (0x0023). Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100763
764 st->yin_ctx->xmlctx->value = "";
765 st->yin_ctx->xmlctx->value_len = 0;
766 assert_int_equal(yin_validate_value(st->yin_ctx, Y_STR_ARG), LY_SUCCESS);
767
768 st->yin_ctx->xmlctx->value = "pre:b";
769 st->yin_ctx->xmlctx->value_len = 5;
770 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG), LY_EVALID);
771 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG), LY_SUCCESS);
772
773 st->yin_ctx->xmlctx->value = "pre:pre:b";
774 st->yin_ctx->xmlctx->value_len = 9;
775 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG), LY_EVALID);
David Sedlák4a650532019-07-10 11:55:18 +0200776
777 st->finished_correctly = true;
778}
779
David Sedlák32488102019-07-15 17:44:10 +0200780/* helper function to simplify unit test of each element using parse_content function */
781LY_ERR
Radek Krejci3a4889a2020-05-19 17:01:58 +0200782test_element_helper(struct test_parser_yin_state *st, const char *data, void *dest, const char **text, struct lysp_ext_instance **exts)
David Sedlák32488102019-07-15 17:44:10 +0200783{
David Sedlákc5b20842019-08-13 10:18:31 +0200784 const char *name, *prefix;
785 size_t name_len, prefix_len;
David Sedlák32488102019-07-15 17:44:10 +0200786 LY_ERR ret = LY_SUCCESS;
787 struct yin_subelement subelems[71] = {
Radek Krejcid6b76452019-09-03 17:03:03 +0200788 {LY_STMT_ACTION, dest, 0},
789 {LY_STMT_ANYDATA, dest, 0},
790 {LY_STMT_ANYXML, dest, 0},
791 {LY_STMT_ARGUMENT,dest, 0},
792 {LY_STMT_AUGMENT, dest, 0},
793 {LY_STMT_BASE, dest, 0},
794 {LY_STMT_BELONGS_TO, dest, 0},
795 {LY_STMT_BIT, dest, 0},
796 {LY_STMT_CASE, dest, 0},
797 {LY_STMT_CHOICE, dest, 0},
798 {LY_STMT_CONFIG, dest, 0},
799 {LY_STMT_CONTACT, dest, 0},
800 {LY_STMT_CONTAINER, dest, 0},
801 {LY_STMT_DEFAULT, dest, YIN_SUBELEM_UNIQUE},
802 {LY_STMT_DESCRIPTION, dest, 0},
803 {LY_STMT_DEVIATE, dest, 0},
804 {LY_STMT_DEVIATION, dest, 0},
805 {LY_STMT_ENUM, dest, 0},
806 {LY_STMT_ERROR_APP_TAG, dest, YIN_SUBELEM_UNIQUE},
807 {LY_STMT_ERROR_MESSAGE, dest, 0},
808 {LY_STMT_EXTENSION, dest, 0},
809 {LY_STMT_FEATURE, dest, 0},
810 {LY_STMT_FRACTION_DIGITS, dest, 0},
811 {LY_STMT_GROUPING, dest, 0},
812 {LY_STMT_IDENTITY, dest, 0},
813 {LY_STMT_IF_FEATURE, dest, 0},
814 {LY_STMT_IMPORT, dest, 0},
815 {LY_STMT_INCLUDE, dest, 0},
816 {LY_STMT_INPUT, dest, 0},
817 {LY_STMT_KEY, dest, YIN_SUBELEM_UNIQUE},
818 {LY_STMT_LEAF, dest, 0},
819 {LY_STMT_LEAF_LIST, dest, 0},
820 {LY_STMT_LENGTH, dest, 0},
821 {LY_STMT_LIST, dest, 0},
822 {LY_STMT_MANDATORY, dest, 0},
823 {LY_STMT_MAX_ELEMENTS, dest, 0},
824 {LY_STMT_MIN_ELEMENTS, dest, 0},
825 {LY_STMT_MODIFIER, dest, 0},
826 {LY_STMT_MODULE, dest, 0},
827 {LY_STMT_MUST, dest, 0},
828 {LY_STMT_NAMESPACE, dest, YIN_SUBELEM_UNIQUE},
829 {LY_STMT_NOTIFICATION, dest, 0},
830 {LY_STMT_ORDERED_BY, dest, 0},
831 {LY_STMT_ORGANIZATION, dest, 0},
832 {LY_STMT_OUTPUT, dest, 0},
833 {LY_STMT_PATH, dest, 0},
834 {LY_STMT_PATTERN, dest, 0},
835 {LY_STMT_POSITION, dest, 0},
836 {LY_STMT_PREFIX, dest, YIN_SUBELEM_UNIQUE},
837 {LY_STMT_PRESENCE, dest, YIN_SUBELEM_UNIQUE},
838 {LY_STMT_RANGE, dest, 0},
839 {LY_STMT_REFERENCE, dest, 0},
840 {LY_STMT_REFINE, dest, 0},
841 {LY_STMT_REQUIRE_INSTANCE, dest, 0},
842 {LY_STMT_REVISION, dest, 0},
843 {LY_STMT_REVISION_DATE, dest, 0},
844 {LY_STMT_RPC, dest, 0},
845 {LY_STMT_STATUS, dest, 0},
846 {LY_STMT_SUBMODULE, dest, 0},
847 {LY_STMT_TYPE, dest, 0},
848 {LY_STMT_TYPEDEF, dest, 0},
849 {LY_STMT_UNIQUE, dest, 0},
850 {LY_STMT_UNITS, dest, YIN_SUBELEM_UNIQUE},
851 {LY_STMT_USES, dest, 0},
852 {LY_STMT_VALUE, dest, 0},
853 {LY_STMT_WHEN, dest, 0},
854 {LY_STMT_YANG_VERSION, dest, 0},
855 {LY_STMT_YIN_ELEMENT, dest, 0},
856 {LY_STMT_EXTENSION_INSTANCE, dest, 0},
857 {LY_STMT_ARG_TEXT, dest, 0},
858 {LY_STMT_ARG_VALUE, dest, 0}
David Sedlák32488102019-07-15 17:44:10 +0200859 };
Michal Vasko63f3d842020-07-08 10:10:14 +0200860 ly_in_new_memory(data, &st->in);
861 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100862 prefix = st->yin_ctx->xmlctx->prefix;
863 prefix_len = st->yin_ctx->xmlctx->prefix_len;
864 name = st->yin_ctx->xmlctx->name;
865 name_len = st->yin_ctx->xmlctx->name_len;
866 lyxml_ctx_next(st->yin_ctx->xmlctx);
867
868 ret = yin_parse_content(st->yin_ctx, subelems, 71, yin_match_keyword(st->yin_ctx, name, name_len, prefix, prefix_len, LY_STMT_NONE), text, exts);
869
Michal Vasko63f3d842020-07-08 10:10:14 +0200870 /* free parser and input */
Michal Vaskob36053d2020-03-26 15:49:30 +0100871 lyxml_ctx_free(st->yin_ctx->xmlctx);
872 st->yin_ctx->xmlctx = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200873 ly_in_free(st->in, 0);
874 st->in = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200875 return ret;
876}
877
David Sedlákd1144562019-08-06 12:36:14 +0200878#define EXT_SUBELEM "<myext:c-define name=\"MY_MTU\" xmlns:myext=\"urn:example:extensions\"/>"
879
David Sedlák32488102019-07-15 17:44:10 +0200880static void
David Sedlák43801c92019-08-05 15:58:54 +0200881test_enum_elem(void **state)
David Sedlák32488102019-07-15 17:44:10 +0200882{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200883 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +0200884 struct lysp_type type = {};
885 const char *data;
886 data = ELEMENT_WRAPPER_START
887 "<enum name=\"enum-name\">"
888 "<if-feature name=\"feature\" />"
889 "<value value=\"55\" />"
890 "<status value=\"deprecated\" />"
891 "<description><text>desc...</text></description>"
892 "<reference><text>ref...</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +0200893 EXT_SUBELEM
David Sedlák32488102019-07-15 17:44:10 +0200894 "</enum>"
895 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100896 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200897 assert_string_equal(type.enums->name, "enum-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +0200898 assert_string_equal(type.enums->iffeatures[0].str, "feature");
David Sedlák32488102019-07-15 17:44:10 +0200899 assert_int_equal(type.enums->value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200900 assert_true((type.enums->flags & LYS_STATUS_DEPRC) && (type.enums->flags & LYS_SET_VALUE));
David Sedlák32488102019-07-15 17:44:10 +0200901 assert_string_equal(type.enums->dsc, "desc...");
902 assert_string_equal(type.enums->ref, "ref...");
David Sedláke0ef1c62019-09-13 10:05:55 +0200903 assert_string_equal(type.enums->exts->name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +0200904 assert_int_equal(type.enums->exts->insubstmt_index, 0);
905 assert_int_equal(type.enums->exts->insubstmt, LYEXT_SUBSTMT_SELF);
906 lysp_type_free(st->ctx, &type);
907 memset(&type, 0, sizeof type);
908
909 data = ELEMENT_WRAPPER_START
910 "<enum name=\"enum-name\"></enum>"
911 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100912 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200913 assert_string_equal(type.enums->name, "enum-name");
David Sedlák32488102019-07-15 17:44:10 +0200914 lysp_type_free(st->ctx, &type);
915 memset(&type, 0, sizeof type);
916
David Sedlák43801c92019-08-05 15:58:54 +0200917 st->finished_correctly = true;
918}
919
920static void
921test_bit_elem(void **state)
922{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200923 struct test_parser_yin_state *st = *state;
David Sedlák43801c92019-08-05 15:58:54 +0200924 struct lysp_type type = {};
925 const char *data;
926 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200927 "<bit name=\"bit-name\">"
David Sedlák43801c92019-08-05 15:58:54 +0200928 "<if-feature name=\"feature\" />"
929 "<position value=\"55\" />"
930 "<status value=\"deprecated\" />"
931 "<description><text>desc...</text></description>"
932 "<reference><text>ref...</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +0200933 EXT_SUBELEM
David Sedlák43801c92019-08-05 15:58:54 +0200934 "</bit>"
935 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100936 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200937 assert_string_equal(type.bits->name, "bit-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +0200938 assert_string_equal(type.bits->iffeatures[0].str, "feature");
David Sedlák43801c92019-08-05 15:58:54 +0200939 assert_int_equal(type.bits->value, 55);
940 assert_true((type.bits->flags & LYS_STATUS_DEPRC) && (type.bits->flags & LYS_SET_VALUE));
941 assert_string_equal(type.bits->dsc, "desc...");
942 assert_string_equal(type.bits->ref, "ref...");
David Sedláke0ef1c62019-09-13 10:05:55 +0200943 assert_string_equal(type.bits->exts->name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +0200944 assert_int_equal(type.bits->exts->insubstmt_index, 0);
945 assert_int_equal(type.bits->exts->insubstmt, LYEXT_SUBSTMT_SELF);
946 lysp_type_free(st->ctx, &type);
947 memset(&type, 0, sizeof type);
948
949 data = ELEMENT_WRAPPER_START
950 "<bit name=\"bit-name\"> </bit>"
951 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100952 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200953 assert_string_equal(type.bits->name, "bit-name");
David Sedlák43801c92019-08-05 15:58:54 +0200954 lysp_type_free(st->ctx, &type);
955 memset(&type, 0, sizeof type);
956
David Sedlák32488102019-07-15 17:44:10 +0200957 st->finished_correctly = true;
958}
959
960static void
961test_meta_elem(void **state)
962{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200963 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +0200964 char *value = NULL;
965 const char *data;
David Sedlákd1144562019-08-06 12:36:14 +0200966 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200967
968 /* organization element */
969 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200970 "<organization><text>organization...</text>" EXT_SUBELEM EXT_SUBELEM "</organization>"
David Sedlák32488102019-07-15 17:44:10 +0200971 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100972 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_SUCCESS);
David Sedláke0ef1c62019-09-13 10:05:55 +0200973 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +0200974 assert_int_equal(exts[0].insubstmt_index, 0);
975 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ORGANIZATION);
David Sedláke0ef1c62019-09-13 10:05:55 +0200976 assert_string_equal(exts[1].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +0200977 assert_int_equal(exts[1].insubstmt_index, 0);
978 assert_int_equal(exts[1].insubstmt, LYEXT_SUBSTMT_ORGANIZATION);
David Sedlák32488102019-07-15 17:44:10 +0200979 assert_string_equal(value, "organization...");
980 FREE_STRING(st->ctx, value);
David Sedlákd1144562019-08-06 12:36:14 +0200981 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +0200982 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +0200983 exts = NULL;
984
David Sedlák32488102019-07-15 17:44:10 +0200985 /* contact element */
986 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200987 "<contact><text>contact...</text>" EXT_SUBELEM "</contact>"
David Sedlák32488102019-07-15 17:44:10 +0200988 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100989 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_SUCCESS);
David Sedláke0ef1c62019-09-13 10:05:55 +0200990 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +0200991 assert_int_equal(exts[0].insubstmt_index, 0);
992 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_CONTACT);
David Sedlák32488102019-07-15 17:44:10 +0200993 assert_string_equal(value, "contact...");
994 FREE_STRING(st->ctx, value);
David Sedlákd1144562019-08-06 12:36:14 +0200995 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
996 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200997 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +0200998
David Sedlák32488102019-07-15 17:44:10 +0200999 /* description element */
1000 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +02001001 "<description><text>description...</text>" EXT_SUBELEM "</description>"
David Sedlák32488102019-07-15 17:44:10 +02001002 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001003 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001004 assert_string_equal(value, "description...");
David Sedláke0ef1c62019-09-13 10:05:55 +02001005 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001006 assert_int_equal(exts[0].insubstmt_index, 0);
1007 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_DESCRIPTION);
David Sedlák32488102019-07-15 17:44:10 +02001008 FREE_STRING(st->ctx, value);
1009 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001010 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1011 exts = NULL;
1012
David Sedlák32488102019-07-15 17:44:10 +02001013 /* reference element */
1014 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +02001015 "<reference><text>reference...</text>" EXT_SUBELEM "</reference>"
David Sedlák32488102019-07-15 17:44:10 +02001016 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001017 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001018 assert_string_equal(value, "reference...");
David Sedláke0ef1c62019-09-13 10:05:55 +02001019 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001020 assert_int_equal(exts[0].insubstmt_index, 0);
1021 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_REFERENCE);
David Sedlák32488102019-07-15 17:44:10 +02001022 FREE_STRING(st->ctx, value);
1023 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001024 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1025 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001026
David Sedlákdf2a9732019-08-07 13:23:16 +02001027 /* reference element */
1028 data = ELEMENT_WRAPPER_START
1029 "<reference invalid=\"text\"><text>reference...</text>""</reference>"
1030 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001031 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001032 logbuf_assert("Unexpected attribute \"invalid\" of \"reference\" element. Line number 1.");
David Sedlákdf2a9732019-08-07 13:23:16 +02001033 FREE_STRING(st->ctx, value);
1034 value = NULL;
1035 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1036 exts = NULL;
1037
David Sedlák32488102019-07-15 17:44:10 +02001038 /* missing text subelement */
1039 data = ELEMENT_WRAPPER_START
1040 "<reference>reference...</reference>"
1041 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001042 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001043 logbuf_assert("Missing mandatory sub-element \"text\" of \"reference\" element. Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001044
David Sedlákd1144562019-08-06 12:36:14 +02001045 /* reference element */
1046 data = ELEMENT_WRAPPER_START
1047 "<reference>" EXT_SUBELEM "<text>reference...</text></reference>"
1048 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001049 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001050 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 +02001051 FREE_STRING(st->ctx, value);
1052 value = NULL;
1053 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1054 exts = NULL;
1055
David Sedlák32488102019-07-15 17:44:10 +02001056 st->finished_correctly = true;
1057}
1058
1059static void
1060test_import_elem(void **state)
1061{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001062 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001063 const char *data;
David Sedlák298ff6d2019-07-26 14:29:03 +02001064 struct lysp_import *imports = NULL;
1065 struct import_meta imp_meta = {"prefix", &imports};
David Sedlák32488102019-07-15 17:44:10 +02001066
1067 /* max subelems */
1068 data = ELEMENT_WRAPPER_START
1069 "<import module=\"a\">"
David Sedlákd1144562019-08-06 12:36:14 +02001070 EXT_SUBELEM
David Sedlák32488102019-07-15 17:44:10 +02001071 "<prefix value=\"a_mod\"/>"
1072 "<revision-date date=\"2015-01-01\"></revision-date>"
1073 "<description><text>import description</text></description>"
1074 "<reference><text>import reference</text></reference>"
1075 "</import>"
1076 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001077 assert_int_equal(test_element_helper(st, data, &imp_meta, NULL, NULL), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02001078 assert_string_equal(imports->name, "a");
1079 assert_string_equal(imports->prefix, "a_mod");
1080 assert_string_equal(imports->rev, "2015-01-01");
1081 assert_string_equal(imports->dsc, "import description");
1082 assert_string_equal(imports->ref, "import reference");
David Sedláke0ef1c62019-09-13 10:05:55 +02001083 assert_string_equal(imports->exts->name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001084 assert_int_equal(imports->exts->insubstmt, LYEXT_SUBSTMT_SELF);
1085 assert_int_equal(imports->exts->insubstmt_index, 0);
David Sedlák298ff6d2019-07-26 14:29:03 +02001086 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1087 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001088
1089 /* min subelems */
David Sedlák32488102019-07-15 17:44:10 +02001090 data = ELEMENT_WRAPPER_START
1091 "<import module=\"a\">"
1092 "<prefix value=\"a_mod\"/>"
1093 "</import>"
1094 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001095 assert_int_equal(test_element_helper(st, data, &imp_meta, NULL, NULL), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02001096 assert_string_equal(imports->prefix, "a_mod");
1097 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1098 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001099
1100 /* invalid (missing prefix) */
David Sedlák298ff6d2019-07-26 14:29:03 +02001101 data = ELEMENT_WRAPPER_START "<import module=\"a\"></import>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001102 assert_int_equal(test_element_helper(st, data, &imp_meta, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001103 logbuf_assert("Missing mandatory sub-element \"prefix\" of \"import\" element. Line number 1.");
David Sedlák298ff6d2019-07-26 14:29:03 +02001104 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1105 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001106
1107 /* invalid reused prefix */
David Sedlák32488102019-07-15 17:44:10 +02001108 data = ELEMENT_WRAPPER_START
1109 "<import module=\"a\">"
David Sedlák298ff6d2019-07-26 14:29:03 +02001110 "<prefix value=\"prefix\"/>"
David Sedlák32488102019-07-15 17:44:10 +02001111 "</import>"
1112 "<import module=\"a\">"
David Sedlák298ff6d2019-07-26 14:29:03 +02001113 "<prefix value=\"prefix\"/>"
David Sedlák32488102019-07-15 17:44:10 +02001114 "</import>"
1115 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001116 assert_int_equal(test_element_helper(st, data, &imp_meta, NULL, NULL), LY_EVALID);
David Sedlák298ff6d2019-07-26 14:29:03 +02001117 logbuf_assert("Prefix \"prefix\" already used as module prefix. Line number 1.");
1118 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1119 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001120
1121 st->finished_correctly = true;
1122}
1123
1124static void
1125test_status_elem(void **state)
1126{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001127 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001128 const char *data;
1129 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001130 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001131
1132 /* test valid values */
1133 data = ELEMENT_WRAPPER_START "<status value=\"current\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001134 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001135 assert_true(flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +02001136
1137 data = ELEMENT_WRAPPER_START "<status value=\"deprecated\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001138 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001139 assert_true(flags & LYS_STATUS_DEPRC);
David Sedlák32488102019-07-15 17:44:10 +02001140
David Sedlákd1144562019-08-06 12:36:14 +02001141 data = ELEMENT_WRAPPER_START "<status value=\"obsolete\">"EXT_SUBELEM"</status>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001142 assert_int_equal(test_element_helper(st, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001143 assert_true(flags & LYS_STATUS_OBSLT);
David Sedláke0ef1c62019-09-13 10:05:55 +02001144 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001145 assert_int_equal(exts[0].insubstmt_index, 0);
1146 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_STATUS);
1147 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1148 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001149
1150 /* test invalid value */
1151 data = ELEMENT_WRAPPER_START "<status value=\"invalid\"></status>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001152 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001153 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 +02001154 st->finished_correctly = true;
1155}
1156
1157static void
1158test_ext_elem(void **state)
1159{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001160 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001161 const char *data;
1162 struct lysp_ext *ext = NULL;
1163
1164 /* max subelems */
1165 data = ELEMENT_WRAPPER_START
1166 "<extension name=\"ext_name\">"
1167 "<argument name=\"arg\"></argument>"
1168 "<status value=\"current\"/>"
1169 "<description><text>ext_desc</text></description>"
1170 "<reference><text>ext_ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001171 EXT_SUBELEM
David Sedlák32488102019-07-15 17:44:10 +02001172 "</extension>"
1173 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001174 assert_int_equal(test_element_helper(st, data, &ext, NULL, NULL), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001175 assert_string_equal(ext->name, "ext_name");
1176 assert_string_equal(ext->argument, "arg");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001177 assert_true(ext->flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +02001178 assert_string_equal(ext->dsc, "ext_desc");
1179 assert_string_equal(ext->ref, "ext_ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02001180 assert_string_equal(ext->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001181 assert_int_equal(ext->exts[0].insubstmt_index, 0);
1182 assert_int_equal(ext->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák32488102019-07-15 17:44:10 +02001183 lysp_ext_free(st->ctx, ext);
1184 LY_ARRAY_FREE(ext);
1185 ext = NULL;
1186
1187 /* min subelems */
1188 data = ELEMENT_WRAPPER_START "<extension name=\"ext_name\"></extension>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001189 assert_int_equal(test_element_helper(st, data, &ext, NULL, NULL), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001190 assert_string_equal(ext->name, "ext_name");
1191 lysp_ext_free(st->ctx, ext);
1192 LY_ARRAY_FREE(ext);
1193 ext = NULL;
1194
1195 st->finished_correctly = true;
1196}
1197
1198static void
1199test_yin_element_elem(void **state)
1200{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001201 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001202 const char *data;
1203 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001204 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001205
1206 data = ELEMENT_WRAPPER_START "<yin-element value=\"true\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001207 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001208 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +02001209
David Sedlákd1144562019-08-06 12:36:14 +02001210 data = ELEMENT_WRAPPER_START "<yin-element value=\"false\">" EXT_SUBELEM "</yin-element>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001211 assert_int_equal(test_element_helper(st, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001212 assert_true(flags & LYS_YINELEM_TRUE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001213 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001214 assert_int_equal(exts[0].insubstmt_index, 0);
1215 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_YINELEM);
1216 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001217
1218 data = ELEMENT_WRAPPER_START "<yin-element value=\"invalid\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001219 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_EVALID);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001220 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák26ea1432019-08-14 13:42:23 +02001221 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 +02001222 st->finished_correctly = true;
1223}
1224
1225static void
1226test_yangversion_elem(void **state)
1227{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001228 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001229 const char *data;
1230 uint8_t version = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001231 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001232
1233 /* valid values */
Radek Krejci96e48da2020-09-04 13:18:06 +02001234 data = ELEMENT_WRAPPER_START "<yang-version value=\"1\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001235 assert_int_equal(test_element_helper(st, data, &version, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001236 assert_true(version & LYS_VERSION_1_0);
David Sedlák32488102019-07-15 17:44:10 +02001237 assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_0);
1238
David Sedlákd1144562019-08-06 12:36:14 +02001239 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.1\">" EXT_SUBELEM "</yang-version>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001240 assert_int_equal(test_element_helper(st, data, &version, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001241 assert_true(version & LYS_VERSION_1_1);
David Sedlák32488102019-07-15 17:44:10 +02001242 assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_1);
David Sedláke0ef1c62019-09-13 10:05:55 +02001243 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001244 assert_int_equal(exts[0].insubstmt_index, 0);
1245 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_VERSION);
1246 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001247
1248 /* invalid value */
1249 data = ELEMENT_WRAPPER_START "<yang-version value=\"version\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001250 assert_int_equal(test_element_helper(st, data, &version, NULL, NULL), LY_EVALID);
Radek Krejci96e48da2020-09-04 13:18:06 +02001251 logbuf_assert("Invalid value \"version\" of \"value\" attribute in \"yang-version\" element. Valid values are \"1\" and \"1.1\". Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001252
1253 st->finished_correctly = true;
1254}
1255
1256static void
1257test_mandatory_elem(void **state)
1258{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001259 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001260 const char *data;
1261 uint16_t man = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001262 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001263
1264 /* valid values */
1265 data = ELEMENT_WRAPPER_START "<mandatory value=\"true\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001266 assert_int_equal(test_element_helper(st, data, &man, NULL, NULL), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001267 assert_int_equal(man, LYS_MAND_TRUE);
1268 man = 0;
1269
David Sedlákd1144562019-08-06 12:36:14 +02001270 data = ELEMENT_WRAPPER_START "<mandatory value=\"false\">" EXT_SUBELEM "</mandatory>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001271 assert_int_equal(test_element_helper(st, data, &man, NULL, &exts), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001272 assert_int_equal(man, LYS_MAND_FALSE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001273 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001274 assert_int_equal(exts[0].insubstmt_index, 0);
1275 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_MANDATORY);
1276 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001277
1278 data = ELEMENT_WRAPPER_START "<mandatory value=\"invalid\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001279 assert_int_equal(test_element_helper(st, data, &man, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001280 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 +02001281
1282 st->finished_correctly = true;
1283}
1284
David Sedlák8e7bda82019-07-16 17:57:50 +02001285static void
1286test_argument_elem(void **state)
1287{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001288 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001289 const char *data;
1290 uint16_t flags = 0;
1291 const char *arg;
1292 struct yin_argument_meta arg_meta = {&flags, &arg};
David Sedlákd1144562019-08-06 12:36:14 +02001293 struct lysp_ext_instance *exts = NULL;
1294
David Sedlák8e7bda82019-07-16 17:57:50 +02001295 /* max subelems */
1296 data = ELEMENT_WRAPPER_START
1297 "<argument name=\"arg-name\">"
1298 "<yin-element value=\"true\" />"
David Sedlákd1144562019-08-06 12:36:14 +02001299 EXT_SUBELEM
David Sedlák8e7bda82019-07-16 17:57:50 +02001300 "</argument>"
1301 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001302 assert_int_equal(test_element_helper(st, data, &arg_meta, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001303 assert_string_equal(arg, "arg-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001304 assert_true(flags & LYS_YINELEM_TRUE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001305 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001306 assert_int_equal(exts[0].insubstmt_index, 0);
1307 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ARGUMENT);
1308 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1309 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001310 flags = 0;
1311 FREE_STRING(st->ctx, arg);
1312 arg = NULL;
1313
1314 /* min subelems */
1315 data = ELEMENT_WRAPPER_START
1316 "<argument name=\"arg\">"
1317 "</argument>"
1318 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001319 assert_int_equal(test_element_helper(st, data, &arg_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001320 assert_string_equal(arg, "arg");
1321 assert_true(flags == 0);
1322 FREE_STRING(st->ctx, arg);
1323
1324 st->finished_correctly = true;
1325}
1326
1327static void
1328test_base_elem(void **state)
1329{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001330 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001331 const char *data;
1332 const char **bases = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001333 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001334 struct lysp_type type = {};
1335
1336 /* as identity subelement */
1337 data = "<identity xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlákd1144562019-08-06 12:36:14 +02001338 "<base name=\"base-name\">"
1339 EXT_SUBELEM
1340 "</base>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001341 "</identity>";
Michal Vaskob36053d2020-03-26 15:49:30 +01001342 assert_int_equal(test_element_helper(st, data, &bases, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001343 assert_string_equal(*bases, "base-name");
David Sedláke0ef1c62019-09-13 10:05:55 +02001344 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001345 assert_int_equal(exts[0].insubstmt_index, 0);
1346 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_BASE);
1347 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1348 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001349 FREE_STRING(st->ctx, *bases);
1350 LY_ARRAY_FREE(bases);
1351
1352 /* as type subelement */
1353 data = "<type xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlákd1144562019-08-06 12:36:14 +02001354 "<base name=\"base-name\">"
1355 EXT_SUBELEM
1356 "</base>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001357 "</type>";
Michal Vaskob36053d2020-03-26 15:49:30 +01001358 assert_int_equal(test_element_helper(st, data, &type, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001359 assert_string_equal(*type.bases, "base-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001360 assert_true(type.flags & LYS_SET_BASE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001361 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001362 assert_int_equal(exts[0].insubstmt_index, 0);
1363 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_BASE);
1364 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1365 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001366 FREE_STRING(st->ctx, *type.bases);
1367 LY_ARRAY_FREE(type.bases);
1368
1369 st->finished_correctly = true;
1370}
1371
1372static void
1373test_belongsto_elem(void **state)
1374{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001375 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001376 const char *data;
1377 struct lysp_submodule submod;
David Sedlákd1144562019-08-06 12:36:14 +02001378 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001379
1380 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +02001381 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/>"EXT_SUBELEM"</belongs-to>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001382 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001383 assert_int_equal(test_element_helper(st, data, &submod, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001384 assert_string_equal(submod.prefix, "pref");
David Sedláke0ef1c62019-09-13 10:05:55 +02001385 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001386 assert_int_equal(exts[0].insubstmt_index, 0);
1387 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_BELONGSTO);
1388 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1389 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001390 FREE_STRING(st->ctx, submod.prefix);
1391
1392 data = ELEMENT_WRAPPER_START "<belongs-to module=\"module-name\"></belongs-to>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001393 assert_int_equal(test_element_helper(st, data, &submod, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001394 logbuf_assert("Missing mandatory sub-element \"prefix\" of \"belongs-to\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001395
1396 st->finished_correctly = true;
1397}
1398
1399static void
1400test_config_elem(void **state)
1401{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001402 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001403 const char *data;
1404 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001405 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001406
David Sedlákd1144562019-08-06 12:36:14 +02001407 data = ELEMENT_WRAPPER_START "<config value=\"true\">" EXT_SUBELEM "</config>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001408 assert_int_equal(test_element_helper(st, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001409 assert_true(flags & LYS_CONFIG_W);
David Sedláke0ef1c62019-09-13 10:05:55 +02001410 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001411 assert_int_equal(exts[0].insubstmt_index, 0);
1412 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_CONFIG);
1413 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1414 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001415 flags = 0;
1416
1417 data = ELEMENT_WRAPPER_START "<config value=\"false\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001418 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001419 assert_true(flags & LYS_CONFIG_R);
David Sedlák8e7bda82019-07-16 17:57:50 +02001420 flags = 0;
1421
1422 data = ELEMENT_WRAPPER_START "<config value=\"invalid\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001423 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001424 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 +02001425
1426 st->finished_correctly = true;
1427}
1428
1429static void
1430test_default_elem(void **state)
1431{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001432 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001433 const char *data;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001434 struct lysp_qname val = {0};
David Sedlákd1144562019-08-06 12:36:14 +02001435 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001436
David Sedlákd1144562019-08-06 12:36:14 +02001437 data = ELEMENT_WRAPPER_START "<default value=\"defaul-value\">"EXT_SUBELEM"</default>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001438 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001439 assert_string_equal(val.str, "defaul-value");
David Sedláke0ef1c62019-09-13 10:05:55 +02001440 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001441 assert_int_equal(exts[0].insubstmt_index, 0);
1442 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_DEFAULT);
1443 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1444 exts = NULL;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001445 FREE_STRING(st->ctx, val.str);
1446 val.str = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001447
1448 data = ELEMENT_WRAPPER_START "<default/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001449 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001450 logbuf_assert("Missing mandatory attribute value of default element. Line number 1.");
1451
1452 st->finished_correctly = true;
1453}
1454
1455static void
1456test_err_app_tag_elem(void **state)
1457{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001458 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001459 const char *data;
1460 const char *val = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001461 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001462
David Sedlákd1144562019-08-06 12:36:14 +02001463 data = ELEMENT_WRAPPER_START "<error-app-tag value=\"val\">"EXT_SUBELEM"</error-app-tag>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001464 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001465 assert_string_equal(val, "val");
David Sedláke0ef1c62019-09-13 10:05:55 +02001466 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001467 assert_int_equal(exts[0].insubstmt_index, 0);
1468 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ERRTAG);
1469 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1470 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001471 FREE_STRING(st->ctx, val);
1472 val = NULL;
1473
1474 data = ELEMENT_WRAPPER_START "<error-app-tag/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001475 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001476 logbuf_assert("Missing mandatory attribute value of error-app-tag element. Line number 1.");
1477
1478 st->finished_correctly = true;
1479}
1480
1481static void
1482test_err_msg_elem(void **state)
1483{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001484 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001485 const char *data;
1486 const char *val = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001487 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001488
David Sedlákd1144562019-08-06 12:36:14 +02001489 data = ELEMENT_WRAPPER_START "<error-message><value>val</value>"EXT_SUBELEM"</error-message>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001490 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001491 assert_string_equal(val, "val");
David Sedláke0ef1c62019-09-13 10:05:55 +02001492 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001493 assert_int_equal(exts[0].insubstmt_index, 0);
1494 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ERRMSG);
1495 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1496 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001497 FREE_STRING(st->ctx, val);
1498
1499 data = ELEMENT_WRAPPER_START "<error-message></error-message>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001500 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001501 logbuf_assert("Missing mandatory sub-element \"value\" of \"error-message\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001502
David Sedlákdf2a9732019-08-07 13:23:16 +02001503 data = ELEMENT_WRAPPER_START "<error-message invalid=\"text\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001504 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001505 logbuf_assert("Unexpected attribute \"invalid\" of \"error-message\" element. Line number 1.");
David Sedlákdf2a9732019-08-07 13:23:16 +02001506
David Sedlák8e7bda82019-07-16 17:57:50 +02001507 st->finished_correctly = true;
1508}
1509
1510static void
1511test_fracdigits_elem(void **state)
1512{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001513 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001514 const char *data;
1515 struct lysp_type type = {};
1516
1517 /* valid value */
David Sedlákd1144562019-08-06 12:36:14 +02001518 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"10\">"EXT_SUBELEM"</fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001519 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedláke0ef1c62019-09-13 10:05:55 +02001520 assert_string_equal(type.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001521 assert_int_equal(type.exts[0].insubstmt_index, 0);
1522 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_FRACDIGITS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001523 assert_int_equal(type.fraction_digits, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001524 assert_true(type.flags & LYS_SET_FRDIGITS);
David Sedlákd1144562019-08-06 12:36:14 +02001525 FREE_ARRAY(st->ctx, type.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001526
1527 /* invalid values */
1528 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"-1\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001529 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001530 logbuf_assert("Invalid value \"-1\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001531
1532 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"02\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001533 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001534 logbuf_assert("Invalid value \"02\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001535
1536 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"1p\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001537 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001538 logbuf_assert("Invalid value \"1p\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001539
1540 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"19\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001541 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001542 logbuf_assert("Invalid value \"19\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001543
1544 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"999999999999999999\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001545 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001546 logbuf_assert("Invalid value \"999999999999999999\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001547
1548 st->finished_correctly = true;
1549}
1550
1551static void
1552test_iffeature_elem(void **state)
1553{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001554 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001555 const char *data;
1556 const char **iffeatures = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001557 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001558
David Sedlákd1144562019-08-06 12:36:14 +02001559 data = ELEMENT_WRAPPER_START "<if-feature name=\"local-storage\">"EXT_SUBELEM"</if-feature>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001560 assert_int_equal(test_element_helper(st, data, &iffeatures, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001561 assert_string_equal(*iffeatures, "local-storage");
David Sedláke0ef1c62019-09-13 10:05:55 +02001562 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001563 assert_int_equal(exts[0].insubstmt_index, 0);
1564 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_IFFEATURE);
1565 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1566 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001567 FREE_STRING(st->ctx, *iffeatures);
1568 LY_ARRAY_FREE(iffeatures);
1569 iffeatures = NULL;
1570
1571 data = ELEMENT_WRAPPER_START "<if-feature/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001572 assert_int_equal(test_element_helper(st, data, &iffeatures, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001573 logbuf_assert("Missing mandatory attribute name of if-feature element. Line number 1.");
1574 LY_ARRAY_FREE(iffeatures);
1575 iffeatures = NULL;
1576
1577 st->finished_correctly = true;
1578}
1579
1580static void
1581test_length_elem(void **state)
1582{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001583 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001584 const char *data;
1585 struct lysp_type type = {};
1586
1587 /* max subelems */
1588 data = ELEMENT_WRAPPER_START
1589 "<length value=\"length-str\">"
1590 "<error-message><value>err-msg</value></error-message>"
1591 "<error-app-tag value=\"err-app-tag\"/>"
1592 "<description><text>desc</text></description>"
1593 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001594 EXT_SUBELEM
David Sedlák8e7bda82019-07-16 17:57:50 +02001595 "</length>"
1596 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001597 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001598 assert_string_equal(type.length->arg.str, "length-str");
David Sedlák8e7bda82019-07-16 17:57:50 +02001599 assert_string_equal(type.length->emsg, "err-msg");
1600 assert_string_equal(type.length->eapptag, "err-app-tag");
1601 assert_string_equal(type.length->dsc, "desc");
1602 assert_string_equal(type.length->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001603 assert_true(type.flags & LYS_SET_LENGTH);
David Sedláke0ef1c62019-09-13 10:05:55 +02001604 assert_string_equal(type.length->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001605 assert_int_equal(type.length->exts[0].insubstmt_index, 0);
1606 assert_int_equal(type.length->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8e7bda82019-07-16 17:57:50 +02001607 lysp_type_free(st->ctx, &type);
1608 memset(&type, 0, sizeof(type));
1609
1610 /* min subelems */
1611 data = ELEMENT_WRAPPER_START
1612 "<length value=\"length-str\">"
1613 "</length>"
1614 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001615 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001616 assert_string_equal(type.length->arg.str, "length-str");
David Sedlák8e7bda82019-07-16 17:57:50 +02001617 lysp_type_free(st->ctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001618 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001619 memset(&type, 0, sizeof(type));
1620
1621 data = ELEMENT_WRAPPER_START "<length></length>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001622 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001623 logbuf_assert("Missing mandatory attribute value of length element. Line number 1.");
1624 lysp_type_free(st->ctx, &type);
1625 memset(&type, 0, sizeof(type));
1626
1627 st->finished_correctly = true;
1628}
1629
1630static void
1631test_modifier_elem(void **state)
1632{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001633 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001634 const char *data;
Radek Krejci011e4aa2020-09-04 15:22:31 +02001635 const char *pat;
David Sedlákd1144562019-08-06 12:36:14 +02001636 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001637
Radek Krejci011e4aa2020-09-04 15:22:31 +02001638 assert_int_equal(LY_SUCCESS, lydict_insert(st->ctx, "\006pattern", 8, &pat));
David Sedlákd1144562019-08-06 12:36:14 +02001639 data = ELEMENT_WRAPPER_START "<modifier value=\"invert-match\">" EXT_SUBELEM "</modifier>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001640 assert_int_equal(test_element_helper(st, data, &pat, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001641 assert_string_equal(pat, "\x015pattern");
David Sedláke0ef1c62019-09-13 10:05:55 +02001642 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001643 assert_int_equal(exts[0].insubstmt_index, 0);
1644 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_MODIFIER);
1645 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1646 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001647 FREE_STRING(st->ctx, pat);
1648
Radek Krejci011e4aa2020-09-04 15:22:31 +02001649 assert_int_equal(LY_SUCCESS, lydict_insert(st->ctx, "\006pattern", 8, &pat));
David Sedlák8e7bda82019-07-16 17:57:50 +02001650 data = ELEMENT_WRAPPER_START "<modifier value=\"invert\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001651 assert_int_equal(test_element_helper(st, data, &pat, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001652 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 +02001653 FREE_STRING(st->ctx, pat);
1654
1655 st->finished_correctly = true;
1656}
1657
1658static void
1659test_namespace_elem(void **state)
1660{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001661 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001662 const char *data;
1663 const char *ns;
David Sedlákd1144562019-08-06 12:36:14 +02001664 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001665
David Sedlákd1144562019-08-06 12:36:14 +02001666 data = ELEMENT_WRAPPER_START "<namespace uri=\"ns\">" EXT_SUBELEM "</namespace>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001667 assert_int_equal(test_element_helper(st, data, &ns, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001668 assert_string_equal(ns, "ns");
David Sedláke0ef1c62019-09-13 10:05:55 +02001669 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001670 assert_int_equal(exts[0].insubstmt_index, 0);
1671 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_NAMESPACE);
1672 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1673 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001674 FREE_STRING(st->ctx, ns);
1675
1676 data = ELEMENT_WRAPPER_START "<namespace/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001677 assert_int_equal(test_element_helper(st, data, &ns, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001678 logbuf_assert("Missing mandatory attribute uri of namespace element. Line number 1.");
1679
1680 st->finished_correctly = true;
1681}
1682
1683static void
David Sedlák8e7bda82019-07-16 17:57:50 +02001684test_pattern_elem(void **state)
1685{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001686 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001687 const char *data;
1688 struct lysp_type type = {};
1689
1690 /* max subelems */
1691 data = ELEMENT_WRAPPER_START
1692 "<pattern value=\"super_pattern\">"
1693 "<modifier value=\"invert-match\"/>"
1694 "<error-message><value>err-msg-value</value></error-message>"
1695 "<error-app-tag value=\"err-app-tag-value\"/>"
David Sedlák169cc522019-08-15 13:23:45 +02001696 "<description><text>&quot;pattern-desc&quot;</text></description>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001697 "<reference><text>pattern-ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001698 EXT_SUBELEM
David Sedlák8e7bda82019-07-16 17:57:50 +02001699 "</pattern>"
1700 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001701 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001702 assert_true(type.flags & LYS_SET_PATTERN);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001703 assert_string_equal(type.patterns->arg.str, "\x015super_pattern");
David Sedlák169cc522019-08-15 13:23:45 +02001704 assert_string_equal(type.patterns->dsc, "\"pattern-desc\"");
David Sedlák8e7bda82019-07-16 17:57:50 +02001705 assert_string_equal(type.patterns->eapptag, "err-app-tag-value");
1706 assert_string_equal(type.patterns->emsg, "err-msg-value");
David Sedlák8e7bda82019-07-16 17:57:50 +02001707 assert_string_equal(type.patterns->ref, "pattern-ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02001708 assert_string_equal(type.patterns->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001709 assert_int_equal(type.patterns->exts[0].insubstmt_index, 0);
1710 assert_int_equal(type.patterns->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8e7bda82019-07-16 17:57:50 +02001711 lysp_type_free(st->ctx, &type);
1712 memset(&type, 0, sizeof(type));
1713
1714 /* min subelems */
1715 data = ELEMENT_WRAPPER_START "<pattern value=\"pattern\"> </pattern>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001716 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001717 assert_string_equal(type.patterns->arg.str, "\x006pattern");
David Sedlák8e7bda82019-07-16 17:57:50 +02001718 lysp_type_free(st->ctx, &type);
1719 memset(&type, 0, sizeof(type));
1720
1721 st->finished_correctly = true;
1722}
1723
1724static void
1725test_value_position_elem(void **state)
1726{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001727 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001728 const char *data;
1729 struct lysp_type_enum en = {};
1730
1731 /* valid values */
David Sedlákd1144562019-08-06 12:36:14 +02001732 data = ELEMENT_WRAPPER_START "<value value=\"55\">" EXT_SUBELEM "</value>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001733 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001734 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001735 assert_true(en.flags & LYS_SET_VALUE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001736 assert_string_equal(en.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001737 assert_int_equal(en.exts[0].insubstmt_index, 0);
1738 assert_int_equal(en.exts[0].insubstmt, LYEXT_SUBSTMT_VALUE);
1739 FREE_ARRAY(st->ctx, en.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001740 memset(&en, 0, sizeof(en));
1741
1742 data = ELEMENT_WRAPPER_START "<value value=\"-55\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001743 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001744 assert_int_equal(en.value, -55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001745 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001746 memset(&en, 0, sizeof(en));
1747
1748 data = ELEMENT_WRAPPER_START "<value value=\"0\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001749 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001750 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001751 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001752 memset(&en, 0, sizeof(en));
1753
1754 data = ELEMENT_WRAPPER_START "<value value=\"-0\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001755 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001756 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001757 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001758 memset(&en, 0, sizeof(en));
1759
1760 /* valid positions */
David Sedlák8d552d62019-08-06 15:29:05 +02001761 data = ELEMENT_WRAPPER_START "<position value=\"55\">" EXT_SUBELEM "</position>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001762 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001763 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001764 assert_true(en.flags & LYS_SET_VALUE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001765 assert_string_equal(en.exts[0].name, "urn:example:extensions:c-define");
David Sedlák8d552d62019-08-06 15:29:05 +02001766 assert_int_equal(en.exts[0].insubstmt_index, 0);
1767 assert_int_equal(en.exts[0].insubstmt, LYEXT_SUBSTMT_POSITION);
1768 FREE_ARRAY(st->ctx, en.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001769 memset(&en, 0, sizeof(en));
1770
1771 data = ELEMENT_WRAPPER_START "<position value=\"0\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001772 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001773 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001774 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001775 memset(&en, 0, sizeof(en));
1776
1777 /* invalid values */
1778 data = ELEMENT_WRAPPER_START "<value value=\"99999999999999999999999\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001779 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001780 logbuf_assert("Invalid value \"99999999999999999999999\" of \"value\" attribute in \"value\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001781
1782 data = ELEMENT_WRAPPER_START "<value value=\"1k\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001783 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001784 logbuf_assert("Invalid value \"1k\" of \"value\" attribute in \"value\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001785
David Sedlák69f01612019-07-17 11:41:08 +02001786 data = ELEMENT_WRAPPER_START "<value value=\"\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001787 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001788 logbuf_assert("Invalid value \"\" of \"value\" attribute in \"value\" element. Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001789
David Sedlák8e7bda82019-07-16 17:57:50 +02001790 /*invalid positions */
1791 data = ELEMENT_WRAPPER_START "<position value=\"-5\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001792 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001793 logbuf_assert("Invalid value \"-5\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001794
1795 data = ELEMENT_WRAPPER_START "<position value=\"-0\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001796 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001797 logbuf_assert("Invalid value \"-0\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001798
1799 data = ELEMENT_WRAPPER_START "<position value=\"99999999999999999999\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001800 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001801 logbuf_assert("Invalid value \"99999999999999999999\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001802
David Sedlák69f01612019-07-17 11:41:08 +02001803 data = ELEMENT_WRAPPER_START "<position value=\"\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001804 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001805 logbuf_assert("Invalid value \"\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001806
1807 st->finished_correctly = true;
1808}
1809
1810static void
1811test_prefix_elem(void **state)
1812{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001813 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001814 const char *data;
1815 const char *value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001816 struct lysp_ext_instance *exts = NULL;
1817
1818 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\">" EXT_SUBELEM "</prefix>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001819 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001820 assert_string_equal(value, "pref");
David Sedláke0ef1c62019-09-13 10:05:55 +02001821 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001822 assert_int_equal(exts[0].insubstmt_index, 0);
1823 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_PREFIX);
1824 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1825 exts = NULL;
1826 FREE_STRING(st->ctx, value);
David Sedlák69f01612019-07-17 11:41:08 +02001827
1828 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001829 assert_int_equal(test_element_helper(st, data, &value, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001830 assert_string_equal(value, "pref");
1831 FREE_STRING(st->ctx, value);
1832
1833 st->finished_correctly = true;
1834}
1835
1836static void
1837test_range_elem(void **state)
1838{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001839 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001840 const char *data;
1841 struct lysp_type type = {};
1842
1843 /* max subelems */
1844 data = ELEMENT_WRAPPER_START
1845 "<range value=\"range-str\">"
1846 "<error-message><value>err-msg</value></error-message>"
1847 "<error-app-tag value=\"err-app-tag\" />"
1848 "<description><text>desc</text></description>"
1849 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001850 EXT_SUBELEM
David Sedlák69f01612019-07-17 11:41:08 +02001851 "</range>"
1852 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001853 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001854 assert_string_equal(type.range->arg.str, "range-str");
David Sedlák69f01612019-07-17 11:41:08 +02001855 assert_string_equal(type.range->dsc, "desc");
1856 assert_string_equal(type.range->eapptag, "err-app-tag");
1857 assert_string_equal(type.range->emsg, "err-msg");
1858 assert_string_equal(type.range->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001859 assert_true(type.flags & LYS_SET_RANGE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001860 assert_string_equal(type.range->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001861 assert_int_equal(type.range->exts[0].insubstmt_index, 0);
1862 assert_int_equal(type.range->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák69f01612019-07-17 11:41:08 +02001863 lysp_type_free(st->ctx, &type);
1864 memset(&type, 0, sizeof(type));
1865
1866 /* min subelems */
1867 data = ELEMENT_WRAPPER_START "<range value=\"range-str\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001868 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001869 assert_string_equal(type.range->arg.str, "range-str");
David Sedlák69f01612019-07-17 11:41:08 +02001870 lysp_type_free(st->ctx, &type);
1871 memset(&type, 0, sizeof(type));
1872
1873 st->finished_correctly = true;
1874}
1875
1876static void
1877test_reqinstance_elem(void **state)
1878{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001879 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001880 const char *data;
1881 struct lysp_type type = {};
1882
David Sedlákd1144562019-08-06 12:36:14 +02001883 data = ELEMENT_WRAPPER_START "<require-instance value=\"true\">" EXT_SUBELEM "</require-instance>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001884 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001885 assert_int_equal(type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001886 assert_true(type.flags & LYS_SET_REQINST);
David Sedláke0ef1c62019-09-13 10:05:55 +02001887 assert_string_equal(type.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001888 assert_int_equal(type.exts[0].insubstmt_index, 0);
1889 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_REQINSTANCE);
1890 lysp_type_free(st->ctx, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001891 memset(&type, 0, sizeof(type));
1892
1893 data = ELEMENT_WRAPPER_START "<require-instance value=\"false\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001894 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001895 assert_int_equal(type.require_instance, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001896 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001897 memset(&type, 0, sizeof(type));
1898
1899 data = ELEMENT_WRAPPER_START "<require-instance value=\"invalid\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001900 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák69f01612019-07-17 11:41:08 +02001901 memset(&type, 0, sizeof(type));
David Sedlák26ea1432019-08-14 13:42:23 +02001902 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 +02001903
1904 st->finished_correctly = true;
1905}
1906
1907static void
1908test_revision_date_elem(void **state)
1909{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001910 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001911 const char *data;
1912 char rev[LY_REV_SIZE];
David Sedlákd1144562019-08-06 12:36:14 +02001913 struct lysp_ext_instance *exts = NULL;
1914
1915 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\">"EXT_SUBELEM"</revision-date>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001916 assert_int_equal(test_element_helper(st, data, rev, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001917 assert_string_equal(rev, "2000-01-01");
David Sedláke0ef1c62019-09-13 10:05:55 +02001918 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001919 assert_int_equal(exts[0].insubstmt_index, 0);
1920 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_REVISIONDATE);
1921 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák69f01612019-07-17 11:41:08 +02001922
1923 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001924 assert_int_equal(test_element_helper(st, data, rev, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001925 assert_string_equal(rev, "2000-01-01");
1926
1927 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-50-05\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001928 assert_int_equal(test_element_helper(st, data, rev, NULL, NULL), LY_EVALID);
David Sedlák69f01612019-07-17 11:41:08 +02001929 logbuf_assert("Invalid value \"2000-50-05\" of \"revision-date\". Line number 1.");
1930
1931 st->finished_correctly = true;
1932}
1933
1934static void
1935test_unique_elem(void **state)
1936{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001937 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001938 const char *data;
1939 const char **values = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001940 struct lysp_ext_instance *exts = NULL;
1941
1942 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\">"EXT_SUBELEM"</unique>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001943 assert_int_equal(test_element_helper(st, data, &values, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001944 assert_string_equal(*values, "tag");
David Sedláke0ef1c62019-09-13 10:05:55 +02001945 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001946 assert_int_equal(exts[0].insubstmt_index, 0);
1947 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_UNIQUE);
1948 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1949 FREE_STRING(st->ctx, *values);
1950 LY_ARRAY_FREE(values);
1951 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001952
1953 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001954 assert_int_equal(test_element_helper(st, data, &values, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001955 assert_string_equal(*values, "tag");
1956 FREE_STRING(st->ctx, *values);
1957 LY_ARRAY_FREE(values);
David Sedlákd1144562019-08-06 12:36:14 +02001958 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001959
1960 st->finished_correctly = true;
1961}
1962
1963static void
1964test_units_elem(void **state)
1965{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001966 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001967 const char *data;
1968 const char *values = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001969 struct lysp_ext_instance *exts = NULL;
1970
1971 data = ELEMENT_WRAPPER_START "<units name=\"name\">"EXT_SUBELEM"</units>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001972 assert_int_equal(test_element_helper(st, data, &values, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001973 assert_string_equal(values, "name");
David Sedláke0ef1c62019-09-13 10:05:55 +02001974 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001975 assert_int_equal(exts[0].insubstmt_index, 0);
1976 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_UNITS);
1977 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1978 FREE_STRING(st->ctx, values);
1979 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001980
1981 data = ELEMENT_WRAPPER_START "<units name=\"name\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001982 assert_int_equal(test_element_helper(st, data, &values, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001983 assert_string_equal(values, "name");
1984 FREE_STRING(st->ctx, values);
David Sedlákd1144562019-08-06 12:36:14 +02001985 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001986
1987 st->finished_correctly = true;
1988}
1989
1990static void
1991test_when_elem(void **state)
1992{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001993 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001994 const char *data;
1995 struct lysp_when *when = NULL;
1996
1997 data = ELEMENT_WRAPPER_START
1998 "<when condition=\"cond\">"
1999 "<description><text>desc</text></description>"
2000 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02002001 EXT_SUBELEM
David Sedlák69f01612019-07-17 11:41:08 +02002002 "</when>"
2003 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002004 assert_int_equal(test_element_helper(st, data, &when, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002005 assert_string_equal(when->cond, "cond");
2006 assert_string_equal(when->dsc, "desc");
2007 assert_string_equal(when->ref, "ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02002008 assert_string_equal(when->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002009 assert_int_equal(when->exts[0].insubstmt_index, 0);
2010 assert_int_equal(when->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák69f01612019-07-17 11:41:08 +02002011 lysp_when_free(st->ctx, when);
2012 free(when);
2013 when = NULL;
2014
2015 data = ELEMENT_WRAPPER_START "<when condition=\"cond\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002016 assert_int_equal(test_element_helper(st, data, &when, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002017 assert_string_equal(when->cond, "cond");
2018 lysp_when_free(st->ctx, when);
2019 free(when);
2020 when = NULL;
2021
2022 st->finished_correctly = true;
2023}
2024
2025static void
2026test_yin_text_value_elem(void **state)
2027{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002028 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02002029 const char *data;
2030 const char *val;
2031
2032 data = ELEMENT_WRAPPER_START "<text>text</text>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002033 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002034 assert_string_equal(val, "text");
2035 FREE_STRING(st->ctx, val);
2036
2037 data = "<error-message xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <value>text</value> </error-message>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002038 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002039 assert_string_equal(val, "text");
2040 FREE_STRING(st->ctx, val);
2041
2042 data = ELEMENT_WRAPPER_START "<text></text>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002043 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002044 assert_string_equal("", val);
2045 FREE_STRING(st->ctx, val);
2046
David Sedlák8e7bda82019-07-16 17:57:50 +02002047 st->finished_correctly = true;
2048}
David Sedlák32488102019-07-15 17:44:10 +02002049
David Sedlák374d2b32019-07-17 15:06:55 +02002050static void
2051test_type_elem(void **state)
2052{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002053 struct test_parser_yin_state *st = *state;
David Sedlák374d2b32019-07-17 15:06:55 +02002054 const char *data;
2055 struct lysp_type type = {};
2056
2057 /* max subelems */
2058 data = ELEMENT_WRAPPER_START
2059 "<type name=\"type-name\">"
2060 "<base name=\"base-name\"/>"
2061 "<bit name=\"bit\"/>"
2062 "<enum name=\"enum\"/>"
2063 "<fraction-digits value=\"2\"/>"
2064 "<length value=\"length\"/>"
2065 "<path value=\"path\"/>"
2066 "<pattern value=\"pattern\"/>"
2067 "<range value=\"range\" />"
2068 "<require-instance value=\"true\"/>"
2069 "<type name=\"sub-type-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002070 EXT_SUBELEM
David Sedlák374d2b32019-07-17 15:06:55 +02002071 "</type>"
2072 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002073 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák374d2b32019-07-17 15:06:55 +02002074 assert_string_equal(type.name, "type-name");
2075 assert_string_equal(*type.bases, "base-name");
2076 assert_string_equal(type.bits->name, "bit");
2077 assert_string_equal(type.enums->name, "enum");
2078 assert_int_equal(type.fraction_digits, 2);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002079 assert_string_equal(type.length->arg.str, "length");
Michal Vasko004d3152020-06-11 19:59:22 +02002080 assert_string_equal(type.path->expr, "path");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002081 assert_string_equal(type.patterns->arg.str, "\006pattern");
2082 assert_string_equal(type.range->arg.str, "range");
David Sedlák374d2b32019-07-17 15:06:55 +02002083 assert_int_equal(type.require_instance, 1);
2084 assert_string_equal(type.types->name, "sub-type-name");
David Sedláke0ef1c62019-09-13 10:05:55 +02002085 assert_string_equal(type.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002086 assert_int_equal(type.exts[0].insubstmt_index, 0);
2087 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002088 assert_true(type.flags & LYS_SET_BASE);
2089 assert_true(type.flags & LYS_SET_BIT);
2090 assert_true(type.flags & LYS_SET_ENUM);
2091 assert_true(type.flags & LYS_SET_FRDIGITS);
2092 assert_true(type.flags & LYS_SET_LENGTH);
2093 assert_true(type.flags & LYS_SET_PATH);
2094 assert_true(type.flags & LYS_SET_PATTERN);
2095 assert_true(type.flags & LYS_SET_RANGE);
2096 assert_true(type.flags & LYS_SET_REQINST);
2097 assert_true(type.flags & LYS_SET_TYPE);
David Sedlákd1144562019-08-06 12:36:14 +02002098 lysp_type_free(st->ctx, &type);
David Sedlák374d2b32019-07-17 15:06:55 +02002099 memset(&type, 0, sizeof(type));
2100
2101 /* min subelems */
2102 data = ELEMENT_WRAPPER_START "<type name=\"type-name\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002103 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák374d2b32019-07-17 15:06:55 +02002104 lysp_type_free(st->ctx, &type);
2105 memset(&type, 0, sizeof(type));
2106
2107 st->finished_correctly = true;
2108}
2109
David Sedlák1af868e2019-07-17 17:03:14 +02002110static void
2111test_max_elems_elem(void **state)
2112{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002113 struct test_parser_yin_state *st = *state;
David Sedlák1af868e2019-07-17 17:03:14 +02002114 const char *data;
2115 struct lysp_node_list list = {};
2116 struct lysp_node_leaflist llist = {};
2117 struct lysp_refine refine = {};
2118
David Sedlákd1144562019-08-06 12:36:14 +02002119 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"unbounded\">"EXT_SUBELEM"</max-elements> </refine>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002120 assert_int_equal(test_element_helper(st, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02002121 assert_int_equal(refine.max, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002122 assert_true(refine.flags & LYS_SET_MAX);
David Sedláke0ef1c62019-09-13 10:05:55 +02002123 assert_string_equal(refine.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002124 assert_int_equal(refine.exts[0].insubstmt_index, 0);
2125 assert_int_equal(refine.exts[0].insubstmt, LYEXT_SUBSTMT_MAX);
2126 FREE_ARRAY(st->ctx, refine.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02002127
David Sedlákd1144562019-08-06 12:36:14 +02002128 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"5\">"EXT_SUBELEM"</max-elements> </list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002129 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02002130 assert_int_equal(list.max, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002131 assert_true(list.flags & LYS_SET_MAX);
David Sedláke0ef1c62019-09-13 10:05:55 +02002132 assert_string_equal(list.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002133 assert_int_equal(list.exts[0].insubstmt_index, 0);
2134 assert_int_equal(list.exts[0].insubstmt, LYEXT_SUBSTMT_MAX);
2135 FREE_ARRAY(st->ctx, list.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02002136
David Sedlákd1144562019-08-06 12:36:14 +02002137 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"85\">"EXT_SUBELEM"</max-elements> </leaf-list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002138 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02002139 assert_int_equal(llist.max, 85);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002140 assert_true(llist.flags & LYS_SET_MAX);
David Sedláke0ef1c62019-09-13 10:05:55 +02002141 assert_string_equal(llist.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002142 assert_int_equal(llist.exts[0].insubstmt_index, 0);
2143 assert_int_equal(llist.exts[0].insubstmt, LYEXT_SUBSTMT_MAX);
2144 FREE_ARRAY(st->ctx, llist.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02002145
2146 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"10\"/> </refine>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002147 assert_int_equal(test_element_helper(st, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02002148 assert_int_equal(refine.max, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002149 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02002150
2151 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"0\"/> </list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002152 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002153 logbuf_assert("Invalid value \"0\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002154
2155 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"-10\"/> </list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002156 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002157 logbuf_assert("Invalid value \"-10\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002158
2159 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"k\"/> </list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002160 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002161 logbuf_assert("Invalid value \"k\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002162
2163 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"u12\"/> </list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002164 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002165 logbuf_assert("Invalid value \"u12\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002166
2167 st->finished_correctly = true;
2168}
2169
David Sedlák09e18c92019-07-18 11:17:11 +02002170static void
2171test_min_elems_elem(void **state)
2172{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002173 struct test_parser_yin_state *st = *state;
David Sedlák09e18c92019-07-18 11:17:11 +02002174 const char *data;
2175 struct lysp_node_list list = {};
2176 struct lysp_node_leaflist llist = {};
2177 struct lysp_refine refine = {};
2178
David Sedlákd1144562019-08-06 12:36:14 +02002179 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"0\">"EXT_SUBELEM"</min-elements> </refine>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002180 assert_int_equal(test_element_helper(st, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák09e18c92019-07-18 11:17:11 +02002181 assert_int_equal(refine.min, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002182 assert_true(refine.flags & LYS_SET_MIN);
David Sedláke0ef1c62019-09-13 10:05:55 +02002183 assert_string_equal(refine.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002184 assert_int_equal(refine.exts[0].insubstmt_index, 0);
2185 assert_int_equal(refine.exts[0].insubstmt, LYEXT_SUBSTMT_MIN);
2186 FREE_ARRAY(st->ctx, refine.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02002187
David Sedlákd1144562019-08-06 12:36:14 +02002188 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"41\">"EXT_SUBELEM"</min-elements> </list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002189 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_SUCCESS);
David Sedlák09e18c92019-07-18 11:17:11 +02002190 assert_int_equal(list.min, 41);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002191 assert_true(list.flags & LYS_SET_MIN);
David Sedláke0ef1c62019-09-13 10:05:55 +02002192 assert_string_equal(list.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002193 assert_int_equal(list.exts[0].insubstmt_index, 0);
2194 assert_int_equal(list.exts[0].insubstmt, LYEXT_SUBSTMT_MIN);
2195 FREE_ARRAY(st->ctx, list.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02002196
David Sedlákd1144562019-08-06 12:36:14 +02002197 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"50\">"EXT_SUBELEM"</min-elements> </leaf-list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002198 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_SUCCESS);
David Sedlák09e18c92019-07-18 11:17:11 +02002199 assert_int_equal(llist.min, 50);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002200 assert_true(llist.flags & LYS_SET_MIN);
David Sedláke0ef1c62019-09-13 10:05:55 +02002201 assert_string_equal(llist.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002202 assert_int_equal(llist.exts[0].insubstmt_index, 0);
2203 assert_int_equal(llist.exts[0].insubstmt, LYEXT_SUBSTMT_MIN);
2204 FREE_ARRAY(st->ctx, llist.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02002205
2206 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"-5\"/> </leaf-list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002207 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002208 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 +02002209
2210 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"99999999999999999\"/> </leaf-list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002211 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002212 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 +02002213
2214 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"5k\"/> </leaf-list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002215 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002216 logbuf_assert("Invalid value \"5k\" of \"value\" attribute in \"min-elements\" element. Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02002217
2218 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"05\"/> </leaf-list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002219 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002220 logbuf_assert("Invalid value \"05\" of \"value\" attribute in \"min-elements\" element. Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02002221
2222 st->finished_correctly = true;
2223}
2224
David Sedláka2dad212019-07-18 12:45:19 +02002225static void
2226test_ordby_elem(void **state)
2227{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002228 struct test_parser_yin_state *st = *state;
David Sedláka2dad212019-07-18 12:45:19 +02002229 const char *data;
2230 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02002231 struct lysp_ext_instance *exts = NULL;
David Sedláka2dad212019-07-18 12:45:19 +02002232
David Sedlákd1144562019-08-06 12:36:14 +02002233 data = ELEMENT_WRAPPER_START "<ordered-by value=\"system\">"EXT_SUBELEM"</ordered-by>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002234 assert_int_equal(test_element_helper(st, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002235 assert_true(flags & LYS_ORDBY_SYSTEM);
David Sedláke0ef1c62019-09-13 10:05:55 +02002236 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002237 assert_int_equal(exts[0].insubstmt_index, 0);
2238 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ORDEREDBY);
2239 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedláka2dad212019-07-18 12:45:19 +02002240
2241 data = ELEMENT_WRAPPER_START "<ordered-by value=\"user\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002242 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002243 assert_true(flags & LYS_ORDBY_USER);
David Sedláka2dad212019-07-18 12:45:19 +02002244
2245 data = ELEMENT_WRAPPER_START "<ordered-by value=\"inv\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002246 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02002247 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 +02002248
2249 st->finished_correctly = true;
2250}
2251
David Sedlák8a83bbb2019-07-18 14:46:00 +02002252static void
2253test_any_elem(void **state)
2254{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002255 struct test_parser_yin_state *st = *state;
David Sedlák8a83bbb2019-07-18 14:46:00 +02002256 const char *data;
2257 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002258 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák8a83bbb2019-07-18 14:46:00 +02002259 struct lysp_node_anydata *parsed = NULL;
2260
2261 /* anyxml max subelems */
2262 data = ELEMENT_WRAPPER_START
2263 "<anyxml 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 "</anyxml>"
2274 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002275 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002276 parsed = (struct lysp_node_anydata *)siblings;
2277 assert_null(parsed->parent);
2278 assert_int_equal(parsed->nodetype, LYS_ANYXML);
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");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002287 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedláke0ef1c62019-09-13 10:05:55 +02002288 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002289 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 /* anydata max subelems */
2295 data = ELEMENT_WRAPPER_START
2296 "<anydata name=\"any-name\">"
2297 "<config value=\"true\" />"
2298 "<description><text>desc</text></description>"
2299 "<if-feature name=\"feature\" />"
2300 "<mandatory value=\"true\" />"
2301 "<must condition=\"must-cond\" />"
2302 "<reference><text>ref</text></reference>"
2303 "<status value=\"deprecated\"/>"
2304 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002305 EXT_SUBELEM
David Sedlák8a83bbb2019-07-18 14:46:00 +02002306 "</anydata>"
2307 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002308 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002309 parsed = (struct lysp_node_anydata *)siblings;
2310 assert_null(parsed->parent);
2311 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002312 assert_true(parsed->flags & LYS_CONFIG_W);
2313 assert_true(parsed->flags & LYS_MAND_TRUE);
2314 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002315 assert_null(parsed->next);
2316 assert_string_equal(parsed->name, "any-name");
2317 assert_string_equal(parsed->dsc, "desc");
2318 assert_string_equal(parsed->ref, "ref");
2319 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002320 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedláke0ef1c62019-09-13 10:05:55 +02002321 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002322 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2323 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002324 lysp_node_free(st->ctx, siblings);
2325 siblings = NULL;
2326
2327 /* min subelems */
2328 node_meta.parent = (void *)0x10;
2329 data = ELEMENT_WRAPPER_START "<anydata name=\"any-name\"> </anydata>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002330 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002331 parsed = (struct lysp_node_anydata *)siblings;
2332 assert_ptr_equal(parsed->parent, node_meta.parent);
2333 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
2334 assert_null(parsed->next);
2335 assert_null(parsed->exts);
2336 lysp_node_free(st->ctx, siblings);
2337
2338 st->finished_correctly = true;
2339}
2340
David Sedlák203ca3a2019-07-18 15:26:25 +02002341static void
2342test_leaf_elem(void **state)
2343{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002344 struct test_parser_yin_state *st = *state;
David Sedlák203ca3a2019-07-18 15:26:25 +02002345 const char *data;
2346 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002347 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák203ca3a2019-07-18 15:26:25 +02002348 struct lysp_node_leaf *parsed = NULL;
2349
2350 /* max elements */
2351 data = ELEMENT_WRAPPER_START
2352 "<leaf name=\"leaf\">"
2353 "<config value=\"true\" />"
2354 "<default value=\"def-val\"/>"
2355 "<description><text>desc</text></description>"
2356 "<if-feature name=\"feature\" />"
2357 "<mandatory value=\"true\" />"
2358 "<must condition=\"must-cond\" />"
2359 "<reference><text>ref</text></reference>"
2360 "<status value=\"deprecated\"/>"
2361 "<type name=\"type\"/>"
2362 "<units name=\"uni\"/>"
2363 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002364 EXT_SUBELEM
David Sedlák203ca3a2019-07-18 15:26:25 +02002365 "</leaf>"
2366 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002367 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák203ca3a2019-07-18 15:26:25 +02002368 parsed = (struct lysp_node_leaf *)siblings;
2369 assert_null(parsed->parent);
2370 assert_int_equal(parsed->nodetype, LYS_LEAF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002371 assert_true(parsed->flags & LYS_CONFIG_W);
2372 assert_true(parsed->flags & LYS_MAND_TRUE);
2373 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák203ca3a2019-07-18 15:26:25 +02002374 assert_null(parsed->next);
2375 assert_string_equal(parsed->name, "leaf");
2376 assert_string_equal(parsed->dsc, "desc");
2377 assert_string_equal(parsed->ref, "ref");
2378 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002379 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedláke0ef1c62019-09-13 10:05:55 +02002380 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002381 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2382 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002383 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlák203ca3a2019-07-18 15:26:25 +02002384 assert_string_equal(parsed->type.name, "type");
2385 assert_string_equal(parsed->units, "uni");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002386 assert_string_equal(parsed->dflt.str, "def-val");
David Sedlák203ca3a2019-07-18 15:26:25 +02002387 lysp_node_free(st->ctx, siblings);
2388 siblings = NULL;
2389
2390 /* min elements */
2391 data = ELEMENT_WRAPPER_START "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002392 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák203ca3a2019-07-18 15:26:25 +02002393 parsed = (struct lysp_node_leaf *)siblings;
2394 assert_string_equal(parsed->name, "leaf");
2395 assert_string_equal(parsed->type.name, "type");
2396 lysp_node_free(st->ctx, siblings);
2397 siblings = NULL;
2398
2399 st->finished_correctly = true;
2400}
2401
David Sedlákc3da3ef2019-07-19 12:56:08 +02002402static void
2403test_leaf_list_elem(void **state)
2404{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002405 struct test_parser_yin_state *st = *state;
David Sedlákc3da3ef2019-07-19 12:56:08 +02002406 const char *data;
2407 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002408 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlákc3da3ef2019-07-19 12:56:08 +02002409 struct lysp_node_leaflist *parsed = NULL;
2410
2411 data = ELEMENT_WRAPPER_START
2412 "<leaf-list name=\"llist\">"
2413 "<config value=\"true\" />"
2414 "<default value=\"def-val0\"/>"
2415 "<default value=\"def-val1\"/>"
2416 "<description><text>desc</text></description>"
2417 "<if-feature name=\"feature\"/>"
2418 "<max-elements value=\"5\"/>"
2419 "<must condition=\"must-cond\"/>"
2420 "<ordered-by value=\"user\" />"
2421 "<reference><text>ref</text></reference>"
2422 "<status value=\"current\"/>"
2423 "<type name=\"type\"/>"
2424 "<units name=\"uni\"/>"
2425 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002426 EXT_SUBELEM
David Sedlákc3da3ef2019-07-19 12:56:08 +02002427 "</leaf-list>"
2428 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002429 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002430 parsed = (struct lysp_node_leaflist *)siblings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02002431 assert_string_equal(parsed->dflts[0].str, "def-val0");
2432 assert_string_equal(parsed->dflts[1].str, "def-val1");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002433 assert_string_equal(parsed->dsc, "desc");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002434 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002435 assert_int_equal(parsed->max, 5);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002436 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002437 assert_string_equal(parsed->name, "llist");
2438 assert_null(parsed->next);
2439 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2440 assert_null(parsed->parent);
2441 assert_string_equal(parsed->ref, "ref");
2442 assert_string_equal(parsed->type.name, "type");
2443 assert_string_equal(parsed->units, "uni");
2444 assert_string_equal(parsed->when->cond, "when-cond");
2445 assert_true(parsed->flags & LYS_CONFIG_W);
2446 assert_true(parsed->flags & LYS_ORDBY_USER);
2447 assert_true(parsed->flags & LYS_STATUS_CURR);
David Sedláke0ef1c62019-09-13 10:05:55 +02002448 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002449 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2450 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002451 lysp_node_free(st->ctx, siblings);
2452 siblings = NULL;
2453
2454 data = ELEMENT_WRAPPER_START
2455 "<leaf-list name=\"llist\">"
2456 "<config value=\"true\" />"
2457 "<description><text>desc</text></description>"
2458 "<if-feature name=\"feature\"/>"
2459 "<min-elements value=\"5\"/>"
2460 "<must condition=\"must-cond\"/>"
2461 "<ordered-by value=\"user\" />"
2462 "<reference><text>ref</text></reference>"
2463 "<status value=\"current\"/>"
2464 "<type name=\"type\"/>"
2465 "<units name=\"uni\"/>"
2466 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002467 EXT_SUBELEM
David Sedlákc3da3ef2019-07-19 12:56:08 +02002468 "</leaf-list>"
2469 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002470 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002471 parsed = (struct lysp_node_leaflist *)siblings;
2472 assert_string_equal(parsed->dsc, "desc");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002473 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002474 assert_int_equal(parsed->min, 5);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002475 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002476 assert_string_equal(parsed->name, "llist");
2477 assert_null(parsed->next);
2478 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2479 assert_null(parsed->parent);
2480 assert_string_equal(parsed->ref, "ref");
2481 assert_string_equal(parsed->type.name, "type");
2482 assert_string_equal(parsed->units, "uni");
2483 assert_string_equal(parsed->when->cond, "when-cond");
2484 assert_true(parsed->flags & LYS_CONFIG_W);
2485 assert_true(parsed->flags & LYS_ORDBY_USER);
2486 assert_true(parsed->flags & LYS_STATUS_CURR);
David Sedláke0ef1c62019-09-13 10:05:55 +02002487 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002488 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2489 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002490 lysp_node_free(st->ctx, siblings);
2491 siblings = NULL;
2492
2493 data = ELEMENT_WRAPPER_START
2494 "<leaf-list name=\"llist\">"
2495 "<config value=\"true\" />"
2496 "<description><text>desc</text></description>"
2497 "<if-feature name=\"feature\"/>"
2498 "<max-elements value=\"15\"/>"
2499 "<min-elements value=\"5\"/>"
2500 "<must condition=\"must-cond\"/>"
2501 "<ordered-by value=\"user\" />"
2502 "<reference><text>ref</text></reference>"
2503 "<status value=\"current\"/>"
2504 "<type name=\"type\"/>"
2505 "<units name=\"uni\"/>"
2506 "<when condition=\"when-cond\"/>"
2507 "</leaf-list>"
2508 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002509 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002510 parsed = (struct lysp_node_leaflist *)siblings;
2511 assert_string_equal(parsed->dsc, "desc");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002512 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002513 assert_int_equal(parsed->min, 5);
2514 assert_int_equal(parsed->max, 15);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002515 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002516 assert_string_equal(parsed->name, "llist");
2517 assert_null(parsed->next);
2518 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2519 assert_null(parsed->parent);
2520 assert_string_equal(parsed->ref, "ref");
2521 assert_string_equal(parsed->type.name, "type");
2522 assert_string_equal(parsed->units, "uni");
2523 assert_string_equal(parsed->when->cond, "when-cond");
2524 assert_true(parsed->flags & LYS_CONFIG_W);
2525 assert_true(parsed->flags & LYS_ORDBY_USER);
2526 assert_true(parsed->flags & LYS_STATUS_CURR);
2527 lysp_node_free(st->ctx, siblings);
2528 siblings = NULL;
2529
2530 data = ELEMENT_WRAPPER_START
2531 "<leaf-list name=\"llist\">"
2532 "<type name=\"type\"/>"
2533 "</leaf-list>"
2534 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002535 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002536 parsed = (struct lysp_node_leaflist *)siblings;
2537 assert_string_equal(parsed->name, "llist");
2538 assert_string_equal(parsed->type.name, "type");
2539 lysp_node_free(st->ctx, siblings);
2540 siblings = NULL;
2541
2542 /* invalid combinations */
2543 data = ELEMENT_WRAPPER_START
2544 "<leaf-list name=\"llist\">"
2545 "<max-elements value=\"5\"/>"
2546 "<min-elements value=\"15\"/>"
2547 "<type name=\"type\"/>"
2548 "</leaf-list>"
2549 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002550 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_EVALID);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002551 logbuf_assert("Invalid combination of min-elements and max-elements: min value 15 is bigger than the max value 5. Line number 1.");
2552 lysp_node_free(st->ctx, siblings);
2553 siblings = NULL;
2554
2555 data = ELEMENT_WRAPPER_START
2556 "<leaf-list name=\"llist\">"
2557 "<default value=\"def-val1\"/>"
2558 "<min-elements value=\"15\"/>"
2559 "<type name=\"type\"/>"
2560 "</leaf-list>"
2561 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002562 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002563 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 +02002564 lysp_node_free(st->ctx, siblings);
2565 siblings = NULL;
2566
2567 data = ELEMENT_WRAPPER_START
2568 "<leaf-list name=\"llist\">"
2569 "</leaf-list>"
2570 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002571 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002572 logbuf_assert("Missing mandatory sub-element \"type\" of \"leaf-list\" element. Line number 1.");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002573 lysp_node_free(st->ctx, siblings);
2574 siblings = NULL;
2575
2576 st->finished_correctly = true;
2577}
2578
David Sedlákcb39f642019-07-19 13:19:55 +02002579static void
2580test_presence_elem(void **state)
2581{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002582 struct test_parser_yin_state *st = *state;
David Sedlákcb39f642019-07-19 13:19:55 +02002583 const char *data;
2584 const char *val;
David Sedlákd1144562019-08-06 12:36:14 +02002585 struct lysp_ext_instance *exts = NULL;
2586
2587 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\">"EXT_SUBELEM"</presence>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002588 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02002589 assert_string_equal(val, "presence-val");
David Sedláke0ef1c62019-09-13 10:05:55 +02002590 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002591 assert_int_equal(exts[0].insubstmt_index, 0);
2592 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_PRESENCE);
2593 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
2594 FREE_STRING(st->ctx, val);
David Sedlákcb39f642019-07-19 13:19:55 +02002595
2596 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002597 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlákcb39f642019-07-19 13:19:55 +02002598 assert_string_equal(val, "presence-val");
2599 FREE_STRING(st->ctx, val);
2600
2601 data = ELEMENT_WRAPPER_START "<presence/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002602 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlákcb39f642019-07-19 13:19:55 +02002603 logbuf_assert("Missing mandatory attribute value of presence element. Line number 1.");
2604
2605 st->finished_correctly = true;
2606}
2607
David Sedlák12470a82019-07-19 13:44:36 +02002608static void
2609test_key_elem(void **state)
2610{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002611 struct test_parser_yin_state *st = *state;
David Sedlák12470a82019-07-19 13:44:36 +02002612 const char *data;
2613 const char *val;
David Sedlákd1144562019-08-06 12:36:14 +02002614 struct lysp_ext_instance *exts = NULL;
2615
2616 data = ELEMENT_WRAPPER_START "<key value=\"key-value\">"EXT_SUBELEM"</key>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002617 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02002618 assert_string_equal(val, "key-value");
David Sedláke0ef1c62019-09-13 10:05:55 +02002619 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002620 assert_int_equal(exts[0].insubstmt_index, 0);
2621 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_KEY);
2622 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
2623 FREE_STRING(st->ctx, val);
David Sedlák12470a82019-07-19 13:44:36 +02002624
2625 data = ELEMENT_WRAPPER_START "<key value=\"key-value\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002626 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák12470a82019-07-19 13:44:36 +02002627 assert_string_equal(val, "key-value");
2628 FREE_STRING(st->ctx, val);
2629
2630 data = ELEMENT_WRAPPER_START "<key/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002631 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák12470a82019-07-19 13:44:36 +02002632 logbuf_assert("Missing mandatory attribute value of key element. Line number 1.");
2633
2634 st->finished_correctly = true;
2635}
2636
David Sedlák04e17b22019-07-19 15:29:48 +02002637static void
2638test_typedef_elem(void **state)
2639{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002640 struct test_parser_yin_state *st = *state;
David Sedlák04e17b22019-07-19 15:29:48 +02002641 const char *data;
2642 struct lysp_tpdf *tpdfs = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002643 struct tree_node_meta typdef_meta = {NULL, (struct lysp_node **)&tpdfs};
David Sedlák04e17b22019-07-19 15:29:48 +02002644
2645 data = ELEMENT_WRAPPER_START
2646 "<typedef name=\"tpdf-name\">"
2647 "<default value=\"def-val\"/>"
2648 "<description><text>desc-text</text></description>"
2649 "<reference><text>ref-text</text></reference>"
2650 "<status value=\"current\"/>"
2651 "<type name=\"type\"/>"
2652 "<units name=\"uni\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002653 EXT_SUBELEM
David Sedlák04e17b22019-07-19 15:29:48 +02002654 "</typedef>"
2655 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002656 assert_int_equal(test_element_helper(st, data, &typdef_meta, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002657 assert_string_equal(tpdfs[0].dflt.str, "def-val");
David Sedlák04e17b22019-07-19 15:29:48 +02002658 assert_string_equal(tpdfs[0].dsc, "desc-text");
David Sedlák04e17b22019-07-19 15:29:48 +02002659 assert_string_equal(tpdfs[0].name, "tpdf-name");
2660 assert_string_equal(tpdfs[0].ref, "ref-text");
2661 assert_string_equal(tpdfs[0].type.name, "type");
2662 assert_string_equal(tpdfs[0].units, "uni");
2663 assert_true(tpdfs[0].flags & LYS_STATUS_CURR);
David Sedláke0ef1c62019-09-13 10:05:55 +02002664 assert_string_equal(tpdfs[0].exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002665 assert_int_equal(tpdfs[0].exts[0].insubstmt_index, 0);
2666 assert_int_equal(tpdfs[0].exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák04e17b22019-07-19 15:29:48 +02002667 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2668 tpdfs = NULL;
2669
2670 data = ELEMENT_WRAPPER_START
2671 "<typedef name=\"tpdf-name\">"
2672 "<type name=\"type\"/>"
2673 "</typedef>"
2674 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002675 assert_int_equal(test_element_helper(st, data, &typdef_meta, NULL, NULL), LY_SUCCESS);
David Sedlák04e17b22019-07-19 15:29:48 +02002676 assert_string_equal(tpdfs[0].name, "tpdf-name");
2677 assert_string_equal(tpdfs[0].type.name, "type");
2678 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2679 tpdfs = NULL;
2680
2681 st->finished_correctly = true;
2682}
2683
David Sedlákd2d676a2019-07-22 11:28:19 +02002684static void
2685test_refine_elem(void **state)
2686{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002687 struct test_parser_yin_state *st = *state;
David Sedlákd2d676a2019-07-22 11:28:19 +02002688 const char *data;
2689 struct lysp_refine *refines = NULL;
2690
2691 /* max subelems */
2692 data = ELEMENT_WRAPPER_START
2693 "<refine target-node=\"target\">"
2694 "<if-feature name=\"feature\" />"
2695 "<must condition=\"cond\" />"
2696 "<presence value=\"presence\" />"
2697 "<default value=\"def\" />"
2698 "<config value=\"true\" />"
2699 "<mandatory value=\"true\" />"
2700 "<min-elements value=\"10\" />"
2701 "<max-elements value=\"20\" />"
2702 "<description><text>desc</text></description>"
2703 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02002704 EXT_SUBELEM
David Sedlákd2d676a2019-07-22 11:28:19 +02002705 "</refine>"
2706 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002707 assert_int_equal(test_element_helper(st, data, &refines, NULL, NULL), LY_SUCCESS);
David Sedlákd2d676a2019-07-22 11:28:19 +02002708 assert_string_equal(refines->nodeid, "target");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002709 assert_string_equal(refines->dflts[0], "def");
David Sedlákd2d676a2019-07-22 11:28:19 +02002710 assert_string_equal(refines->dsc, "desc");
David Sedlákd2d676a2019-07-22 11:28:19 +02002711 assert_true(refines->flags & LYS_CONFIG_W);
2712 assert_true(refines->flags & LYS_MAND_TRUE);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002713 assert_string_equal(refines->iffeatures[0].str, "feature");
David Sedlákd2d676a2019-07-22 11:28:19 +02002714 assert_int_equal(refines->max, 20);
2715 assert_int_equal(refines->min, 10);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002716 assert_string_equal(refines->musts->arg.str, "cond");
David Sedlákd2d676a2019-07-22 11:28:19 +02002717 assert_string_equal(refines->presence, "presence");
2718 assert_string_equal(refines->ref, "ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02002719 assert_string_equal(refines->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002720 assert_int_equal(refines->exts[0].insubstmt_index, 0);
2721 assert_int_equal(refines->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákd2d676a2019-07-22 11:28:19 +02002722 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2723 refines = NULL;
2724
2725 /* min subelems */
2726 data = ELEMENT_WRAPPER_START "<refine target-node=\"target\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002727 assert_int_equal(test_element_helper(st, data, &refines, NULL, NULL), LY_SUCCESS);
David Sedlákd2d676a2019-07-22 11:28:19 +02002728 assert_string_equal(refines->nodeid, "target");
2729 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2730 refines = NULL;
2731
2732 st->finished_correctly = true;
2733}
2734
David Sedlák0d6de5a2019-07-22 13:25:44 +02002735static void
2736test_uses_elem(void **state)
2737{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002738 struct test_parser_yin_state *st = *state;
David Sedlák0d6de5a2019-07-22 13:25:44 +02002739 const char *data;
2740 struct lysp_node *siblings = NULL;
2741 struct tree_node_meta node_meta = {NULL, &siblings};
2742 struct lysp_node_uses *parsed = NULL;
2743
2744 /* max subelems */
2745 data = ELEMENT_WRAPPER_START
2746 "<uses name=\"uses-name\">"
2747 "<when condition=\"cond\" />"
2748 "<if-feature name=\"feature\" />"
2749 "<status value=\"obsolete\" />"
2750 "<description><text>desc</text></description>"
2751 "<reference><text>ref</text></reference>"
2752 "<refine target-node=\"target\"/>"
David Sedlák992fb7c2019-07-24 16:51:01 +02002753 "<augment target-node=\"target\" />"
David Sedlákd1144562019-08-06 12:36:14 +02002754 EXT_SUBELEM
David Sedlák0d6de5a2019-07-22 13:25:44 +02002755 "</uses>"
2756 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002757 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002758 parsed = (struct lysp_node_uses *)&siblings[0];
2759 assert_string_equal(parsed->name, "uses-name");
2760 assert_string_equal(parsed->dsc, "desc");
David Sedlák0d6de5a2019-07-22 13:25:44 +02002761 assert_true(parsed->flags & LYS_STATUS_OBSLT);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002762 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlák0d6de5a2019-07-22 13:25:44 +02002763 assert_null(parsed->next);
2764 assert_int_equal(parsed->nodetype, LYS_USES);
2765 assert_null(parsed->parent);
2766 assert_string_equal(parsed->ref, "ref");
2767 assert_string_equal(parsed->refines->nodeid, "target");
2768 assert_string_equal(parsed->when->cond, "cond");
David Sedlák992fb7c2019-07-24 16:51:01 +02002769 assert_string_equal(parsed->augments->nodeid, "target");
David Sedláke0ef1c62019-09-13 10:05:55 +02002770 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002771 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2772 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002773 lysp_node_free(st->ctx, siblings);
2774 siblings = NULL;
2775
2776 /* min subelems */
2777 data = ELEMENT_WRAPPER_START "<uses name=\"uses-name\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002778 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002779 assert_string_equal(siblings[0].name, "uses-name");
2780 lysp_node_free(st->ctx, siblings);
2781 siblings = NULL;
2782
2783 st->finished_correctly = true;
2784}
2785
David Sedlákaa854b02019-07-22 14:17:10 +02002786static void
2787test_revision_elem(void **state)
2788{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002789 struct test_parser_yin_state *st = *state;
David Sedlákaa854b02019-07-22 14:17:10 +02002790 const char *data;
2791 struct lysp_revision *revs = NULL;
2792
2793 /* max subelems */
2794 data = ELEMENT_WRAPPER_START
2795 "<revision date=\"2018-12-25\">"
2796 "<description><text>desc</text></description>"
2797 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02002798 EXT_SUBELEM
David Sedlákaa854b02019-07-22 14:17:10 +02002799 "</revision>"
2800 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002801 assert_int_equal(test_element_helper(st, data, &revs, NULL, NULL), LY_SUCCESS);
David Sedlákaa854b02019-07-22 14:17:10 +02002802 assert_string_equal(revs->date, "2018-12-25");
2803 assert_string_equal(revs->dsc, "desc");
2804 assert_string_equal(revs->ref, "ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02002805 assert_string_equal(revs->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002806 assert_int_equal(revs->exts[0].insubstmt_index, 0);
2807 assert_int_equal(revs->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákaa854b02019-07-22 14:17:10 +02002808 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2809 revs = NULL;
2810
2811 /* min subelems */
2812 data = ELEMENT_WRAPPER_START "<revision date=\"2005-05-05\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002813 assert_int_equal(test_element_helper(st, data, &revs, NULL, NULL), LY_SUCCESS);
David Sedlákaa854b02019-07-22 14:17:10 +02002814 assert_string_equal(revs->date, "2005-05-05");
2815 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2816 revs = NULL;
2817
2818 /* invalid value */
2819 data = ELEMENT_WRAPPER_START "<revision date=\"05-05-2005\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002820 assert_int_equal(test_element_helper(st, data, &revs, NULL, NULL), LY_EVALID);
David Sedlákaa854b02019-07-22 14:17:10 +02002821 logbuf_assert("Invalid value \"05-05-2005\" of \"revision\". Line number 1.");
2822 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2823 revs = NULL;
2824
2825 st->finished_correctly = true;
2826}
2827
David Sedlák0c2bab92019-07-22 15:33:19 +02002828static void
2829test_include_elem(void **state)
2830{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002831 struct test_parser_yin_state *st = *state;
David Sedlák0c2bab92019-07-22 15:33:19 +02002832 const char *data;
2833 struct lysp_include *includes = NULL;
2834 struct include_meta inc_meta = {"module-name", &includes};
2835
2836 /* max subelems */
2837 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2838 data = ELEMENT_WRAPPER_START
2839 "<include module=\"mod\">"
2840 "<description><text>desc</text></description>"
2841 "<reference><text>ref</text></reference>"
2842 "<revision-date date=\"1999-09-09\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002843 EXT_SUBELEM
David Sedlák0c2bab92019-07-22 15:33:19 +02002844 "</include>"
2845 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002846 assert_int_equal(test_element_helper(st, data, &inc_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0c2bab92019-07-22 15:33:19 +02002847 assert_string_equal(includes->name, "mod");
2848 assert_string_equal(includes->dsc, "desc");
2849 assert_string_equal(includes->ref, "ref");
David Sedlák0c2bab92019-07-22 15:33:19 +02002850 assert_string_equal(includes->rev, "1999-09-09");
David Sedláke0ef1c62019-09-13 10:05:55 +02002851 assert_string_equal(includes->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002852 assert_int_equal(includes->exts[0].insubstmt_index, 0);
2853 assert_int_equal(includes->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák0c2bab92019-07-22 15:33:19 +02002854 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2855 includes = NULL;
2856
2857 /* min subelems */
2858 data = ELEMENT_WRAPPER_START "<include module=\"mod\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002859 assert_int_equal(test_element_helper(st, data, &inc_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0c2bab92019-07-22 15:33:19 +02002860 assert_string_equal(includes->name, "mod");
2861 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2862 includes = NULL;
2863
2864 /* invalid combinations */
2865 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2866 data = ELEMENT_WRAPPER_START
2867 "<include module=\"mod\">"
2868 "<description><text>desc</text></description>"
2869 "<revision-date date=\"1999-09-09\"/>"
2870 "</include>"
2871 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002872 assert_int_equal(test_element_helper(st, data, &inc_meta, NULL, NULL), LY_EVALID);
David Sedlák0c2bab92019-07-22 15:33:19 +02002873 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.");
2874 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2875 includes = NULL;
2876
2877 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2878 data = ELEMENT_WRAPPER_START
2879 "<include module=\"mod\">"
2880 "<reference><text>ref</text></reference>"
2881 "<revision-date date=\"1999-09-09\"/>"
2882 "</include>"
2883 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002884 assert_int_equal(test_element_helper(st, data, &inc_meta, NULL, NULL), LY_EVALID);
David Sedlák0c2bab92019-07-22 15:33:19 +02002885 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.");
2886 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2887 includes = NULL;
2888
2889 st->finished_correctly = true;
2890}
2891
David Sedlák5e13dea2019-07-22 16:06:45 +02002892static void
David Sedlákaf536aa2019-07-23 13:42:23 +02002893test_list_elem(void **state)
2894{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002895 struct test_parser_yin_state *st = *state;
David Sedlákaf536aa2019-07-23 13:42:23 +02002896 const char *data;
2897 struct lysp_node *siblings = NULL;
2898 struct tree_node_meta node_meta = {NULL, &siblings};
2899 struct lysp_node_list *parsed = NULL;
2900
2901 /* max subelems */
2902 data = ELEMENT_WRAPPER_START
2903 "<list name=\"list-name\">"
2904 "<when condition=\"when\"/>"
2905 "<if-feature name=\"iff\"/>"
2906 "<must condition=\"must-cond\"/>"
2907 "<key value=\"key\"/>"
2908 "<unique tag=\"utag\"/>"
2909 "<config value=\"true\"/>"
2910 "<min-elements value=\"10\"/>"
2911 "<ordered-by value=\"user\"/>"
2912 "<status value=\"deprecated\"/>"
2913 "<description><text>desc</text></description>"
2914 "<reference><text>ref</text></reference>"
2915 "<anydata name=\"anyd\"/>"
2916 "<anyxml name=\"anyx\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02002917 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002918 "<choice name=\"choice\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02002919 "<action name=\"action\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02002920 "<grouping name=\"grp\"/>"
David Sedlák031b9e72019-07-23 15:19:37 +02002921 "<notification name=\"notf\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002922 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
2923 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlákaf536aa2019-07-23 13:42:23 +02002924 "<list name=\"sub-list\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002925 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákaf536aa2019-07-23 13:42:23 +02002926 "<uses name=\"uses-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002927 EXT_SUBELEM
David Sedlákaf536aa2019-07-23 13:42:23 +02002928 "</list>"
2929 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002930 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákaf536aa2019-07-23 13:42:23 +02002931 parsed = (struct lysp_node_list *)&siblings[0];
2932 assert_string_equal(parsed->dsc, "desc");
2933 assert_string_equal(parsed->child->name, "anyd");
2934 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2935 assert_string_equal(parsed->child->next->name, "anyx");
2936 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
David Sedlákf111bcb2019-07-23 17:15:51 +02002937 assert_string_equal(parsed->child->next->next->name, "cont");
2938 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002939 assert_string_equal(parsed->child->next->next->next->name, "choice");
2940 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
David Sedlák85d0eca2019-07-24 15:15:21 +02002941 assert_string_equal(parsed->child->next->next->next->next->name, "leaf");
2942 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAF);
2943 assert_string_equal(parsed->child->next->next->next->next->next->name, "llist");
2944 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2945 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "sub-list");
2946 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LIST);
2947 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "uses-name");
2948 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_USES);
2949 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002950 assert_string_equal(parsed->groupings->name, "grp");
David Sedlák85d0eca2019-07-24 15:15:21 +02002951 assert_string_equal(parsed->actions->name, "action");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002952 assert_int_equal(parsed->groupings->nodetype, LYS_GROUPING);
David Sedlák031b9e72019-07-23 15:19:37 +02002953 assert_string_equal(parsed->notifs->name, "notf");
David Sedlákaf536aa2019-07-23 13:42:23 +02002954 assert_true(parsed->flags & LYS_ORDBY_USER);
2955 assert_true(parsed->flags & LYS_STATUS_DEPRC);
2956 assert_true(parsed->flags & LYS_CONFIG_W);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002957 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlákaf536aa2019-07-23 13:42:23 +02002958 assert_string_equal(parsed->key, "key");
2959 assert_int_equal(parsed->min, 10);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002960 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlákaf536aa2019-07-23 13:42:23 +02002961 assert_string_equal(parsed->name, "list-name");
2962 assert_null(parsed->next);
2963 assert_int_equal(parsed->nodetype, LYS_LIST);
2964 assert_null(parsed->parent);
2965 assert_string_equal(parsed->ref, "ref");
2966 assert_string_equal(parsed->typedefs->name, "tpdf");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002967 assert_string_equal(parsed->uniques->str, "utag");
David Sedlákaf536aa2019-07-23 13:42:23 +02002968 assert_string_equal(parsed->when->cond, "when");
David Sedláke0ef1c62019-09-13 10:05:55 +02002969 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002970 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2971 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákaf536aa2019-07-23 13:42:23 +02002972 lysp_node_free(st->ctx, siblings);
2973 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
2974 siblings = NULL;
2975
2976 /* min subelems */
2977 data = ELEMENT_WRAPPER_START "<list name=\"list-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002978 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákaf536aa2019-07-23 13:42:23 +02002979 parsed = (struct lysp_node_list *)&siblings[0];
2980 assert_string_equal(parsed->name, "list-name");
2981 lysp_node_free(st->ctx, siblings);
2982 siblings = NULL;
2983
2984 st->finished_correctly = true;
2985}
2986
David Sedlák031b9e72019-07-23 15:19:37 +02002987static void
2988test_notification_elem(void **state)
2989{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002990 struct test_parser_yin_state *st = *state;
David Sedlák031b9e72019-07-23 15:19:37 +02002991 const char *data;
2992 struct lysp_notif *notifs = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002993 struct tree_node_meta notif_meta = {NULL, (struct lysp_node **)&notifs};
David Sedlák031b9e72019-07-23 15:19:37 +02002994
2995 /* max subelems */
2996 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2997 data = ELEMENT_WRAPPER_START
2998 "<notification name=\"notif-name\">"
2999 "<anydata name=\"anyd\"/>"
3000 "<anyxml name=\"anyx\"/>"
3001 "<description><text>desc</text></description>"
3002 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003003 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3004 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák031b9e72019-07-23 15:19:37 +02003005 "<list name=\"sub-list\"/>"
3006 "<must condition=\"cond\"/>"
3007 "<reference><text>ref</text></reference>"
3008 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003009 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák031b9e72019-07-23 15:19:37 +02003010 "<uses name=\"uses-name\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02003011 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003012 "<choice name=\"choice\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02003013 "<grouping name=\"grp\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003014 EXT_SUBELEM
David Sedlák031b9e72019-07-23 15:19:37 +02003015 "</notification>"
3016 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003017 assert_int_equal(test_element_helper(st, data, &notif_meta, NULL, NULL), LY_SUCCESS);
David Sedlák031b9e72019-07-23 15:19:37 +02003018 assert_string_equal(notifs->name, "notif-name");
3019 assert_string_equal(notifs->data->name, "anyd");
3020 assert_int_equal(notifs->data->nodetype, LYS_ANYDATA);
3021 assert_string_equal(notifs->data->next->name, "anyx");
3022 assert_int_equal(notifs->data->next->nodetype, LYS_ANYXML);
3023 assert_string_equal(notifs->data->next->next->name, "leaf");
3024 assert_int_equal(notifs->data->next->next->nodetype, LYS_LEAF);
3025 assert_string_equal(notifs->data->next->next->next->name, "llist");
3026 assert_int_equal(notifs->data->next->next->next->nodetype, LYS_LEAFLIST);
3027 assert_string_equal(notifs->data->next->next->next->next->name, "sub-list");
3028 assert_int_equal(notifs->data->next->next->next->next->nodetype, LYS_LIST);
David Sedlák031b9e72019-07-23 15:19:37 +02003029 assert_true(notifs->flags & LYS_STATUS_DEPRC);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003030 assert_string_equal(notifs->groupings->name, "grp");
3031 assert_int_equal(notifs->groupings->nodetype, LYS_GROUPING);
David Sedlákf111bcb2019-07-23 17:15:51 +02003032 assert_string_equal(notifs->data->next->next->next->next->next->name, "uses-name");
3033 assert_int_equal(notifs->data->next->next->next->next->next->nodetype, LYS_USES);
3034 assert_string_equal(notifs->data->next->next->next->next->next->next->name, "cont");
3035 assert_int_equal(notifs->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003036 assert_int_equal(notifs->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
3037 assert_string_equal(notifs->data->next->next->next->next->next->next->next->name, "choice");
3038 assert_null(notifs->data->next->next->next->next->next->next->next->next);
Michal Vasko7f45cf22020-10-01 12:49:44 +02003039 assert_string_equal(notifs->iffeatures[0].str, "iff");
3040 assert_string_equal(notifs->musts->arg.str, "cond");
David Sedlák031b9e72019-07-23 15:19:37 +02003041 assert_int_equal(notifs->nodetype, LYS_NOTIF);
3042 assert_null(notifs->parent);
3043 assert_string_equal(notifs->ref, "ref");
3044 assert_string_equal(notifs->typedefs->name, "tpdf");
David Sedláke0ef1c62019-09-13 10:05:55 +02003045 assert_string_equal(notifs->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003046 assert_int_equal(notifs->exts[0].insubstmt_index, 0);
3047 assert_int_equal(notifs->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák031b9e72019-07-23 15:19:37 +02003048 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
3049 notifs = NULL;
3050
3051 /* min subelems */
3052 data = ELEMENT_WRAPPER_START "<notification name=\"notif-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003053 assert_int_equal(test_element_helper(st, data, &notif_meta, NULL, NULL), LY_SUCCESS);
David Sedlák031b9e72019-07-23 15:19:37 +02003054 assert_string_equal(notifs->name, "notif-name");
3055 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003056 notifs = NULL;
3057
3058 st->finished_correctly = true;
3059}
3060
3061static void
3062test_grouping_elem(void **state)
3063{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003064 struct test_parser_yin_state *st = *state;
David Sedláke3ce9ef2019-07-23 16:34:30 +02003065 const char *data;
3066 struct lysp_grp *grps = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003067 struct tree_node_meta grp_meta = {NULL, (struct lysp_node **)&grps};
David Sedláke3ce9ef2019-07-23 16:34:30 +02003068
3069 /* max subelems */
3070 data = ELEMENT_WRAPPER_START
3071 "<grouping name=\"grp-name\">"
3072 "<anydata name=\"anyd\"/>"
3073 "<anyxml name=\"anyx\"/>"
3074 "<description><text>desc</text></description>"
3075 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003076 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3077 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02003078 "<list name=\"list\"/>"
3079 "<notification name=\"notf\"/>"
3080 "<reference><text>ref</text></reference>"
3081 "<status value=\"current\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003082 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02003083 "<uses name=\"uses-name\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02003084 "<action name=\"act\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02003085 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003086 "<choice name=\"choice\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003087 EXT_SUBELEM
David Sedláke3ce9ef2019-07-23 16:34:30 +02003088 "</grouping>"
3089 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003090 assert_int_equal(test_element_helper(st, data, &grp_meta, NULL, NULL), LY_SUCCESS);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003091 assert_string_equal(grps->name, "grp-name");
David Sedláke3ce9ef2019-07-23 16:34:30 +02003092 assert_string_equal(grps->data->name, "anyd");
3093 assert_string_equal(grps->data->next->name, "anyx");
3094 assert_string_equal(grps->data->next->next->name, "leaf");
3095 assert_string_equal(grps->data->next->next->next->name, "llist");
3096 assert_string_equal(grps->data->next->next->next->next->name, "list");
3097 assert_string_equal(grps->dsc, "desc");
David Sedláke3ce9ef2019-07-23 16:34:30 +02003098 assert_true(grps->flags & LYS_STATUS_CURR);
3099 assert_string_equal(grps->groupings->name, "sub-grp");
3100 assert_int_equal(grps->nodetype, LYS_GROUPING);
3101 assert_string_equal(grps->notifs->name, "notf");
3102 assert_null(grps->parent);
3103 assert_string_equal(grps->ref, "ref");
3104 assert_string_equal(grps->typedefs->name, "tpdf");
David Sedlák85d0eca2019-07-24 15:15:21 +02003105 assert_string_equal(grps->actions->name, "act");
David Sedlákf111bcb2019-07-23 17:15:51 +02003106 assert_string_equal(grps->data->next->next->next->next->next->name, "uses-name");
David Sedlákb7abcfa2019-07-24 12:33:35 +02003107 assert_int_equal(grps->data->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákf111bcb2019-07-23 17:15:51 +02003108 assert_string_equal(grps->data->next->next->next->next->next->next->name, "cont");
David Sedlákb7abcfa2019-07-24 12:33:35 +02003109 assert_int_equal(grps->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
3110 assert_string_equal(grps->data->next->next->next->next->next->next->next->name, "choice");
3111 assert_int_equal(grps->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
David Sedláke0ef1c62019-09-13 10:05:55 +02003112 assert_string_equal(grps->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003113 assert_int_equal(grps->exts[0].insubstmt_index, 0);
3114 assert_int_equal(grps->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003115 FREE_ARRAY(st->ctx, grps, lysp_grp_free);
3116 grps = NULL;
3117
3118 /* min subelems */
3119 data = ELEMENT_WRAPPER_START "<grouping name=\"grp-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003120 assert_int_equal(test_element_helper(st, data, &grp_meta, NULL, NULL), LY_SUCCESS);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003121 assert_string_equal(grps->name, "grp-name");
3122 FREE_ARRAY(st->ctx, grps, lysp_grp_free);
3123 grps = NULL;
David Sedlák031b9e72019-07-23 15:19:37 +02003124
3125 st->finished_correctly = true;
3126}
3127
David Sedlákf111bcb2019-07-23 17:15:51 +02003128static void
3129test_container_elem(void **state)
3130{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003131 struct test_parser_yin_state *st = *state;
David Sedlákf111bcb2019-07-23 17:15:51 +02003132 const char *data;
3133 struct lysp_node *siblings = NULL;
3134 struct tree_node_meta node_meta = {NULL, &siblings};
3135 struct lysp_node_container *parsed = NULL;
3136
3137 /* max subelems */
David Sedláke2dc9e92019-07-24 09:59:21 +02003138 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3139 data = ELEMENT_WRAPPER_START
3140 "<container name=\"cont-name\">"
3141 "<anydata name=\"anyd\"/>"
3142 "<anyxml name=\"anyx\"/>"
3143 "<config value=\"true\"/>"
3144 "<container name=\"subcont\"/>"
3145 "<description><text>desc</text></description>"
3146 "<grouping name=\"sub-grp\"/>"
3147 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003148 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3149 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedláke2dc9e92019-07-24 09:59:21 +02003150 "<list name=\"list\"/>"
3151 "<must condition=\"cond\"/>"
3152 "<notification name=\"notf\"/>"
3153 "<presence value=\"presence\"/>"
3154 "<reference><text>ref</text></reference>"
3155 "<status value=\"current\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003156 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedláke2dc9e92019-07-24 09:59:21 +02003157 "<uses name=\"uses-name\"/>"
3158 "<when condition=\"when-cond\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02003159 "<action name=\"act\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003160 "<choice name=\"choice\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003161 EXT_SUBELEM
David Sedláke2dc9e92019-07-24 09:59:21 +02003162 "</container>"
3163 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003164 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedláke2dc9e92019-07-24 09:59:21 +02003165 parsed = (struct lysp_node_container *)siblings;
3166 assert_string_equal(parsed->name, "cont-name");
3167 assert_null(parsed->parent);
3168 assert_int_equal(parsed->nodetype, LYS_CONTAINER);
3169 assert_true(parsed->flags & LYS_CONFIG_W);
3170 assert_true(parsed->flags & LYS_STATUS_CURR);
3171 assert_null(parsed->next);
3172 assert_string_equal(parsed->dsc, "desc");
3173 assert_string_equal(parsed->ref, "ref");
3174 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003175 assert_string_equal(parsed->iffeatures[0].str, "iff");
3176 assert_string_equal(parsed->musts->arg.str, "cond");
David Sedláke2dc9e92019-07-24 09:59:21 +02003177 assert_string_equal(parsed->presence, "presence");
3178 assert_string_equal(parsed->typedefs->name, "tpdf");
3179 assert_string_equal(parsed->groupings->name, "sub-grp");
3180 assert_string_equal(parsed->child->name, "anyd");
3181 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
3182 assert_string_equal(parsed->child->next->name, "anyx");
3183 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
3184 assert_string_equal(parsed->child->next->next->name, "subcont");
3185 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
3186 assert_string_equal(parsed->child->next->next->next->name, "leaf");
3187 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
3188 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
3189 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
3190 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
3191 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
3192 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
3193 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003194 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
3195 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
3196 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke2dc9e92019-07-24 09:59:21 +02003197 assert_string_equal(parsed->notifs->name, "notf");
David Sedlák85d0eca2019-07-24 15:15:21 +02003198 assert_string_equal(parsed->actions->name, "act");
David Sedláke0ef1c62019-09-13 10:05:55 +02003199 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003200 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3201 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedláke2dc9e92019-07-24 09:59:21 +02003202 lysp_node_free(st->ctx, siblings);
3203 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
3204 siblings = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02003205
3206 /* min subelems */
3207 data = ELEMENT_WRAPPER_START "<container name=\"cont-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003208 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákf111bcb2019-07-23 17:15:51 +02003209 parsed = (struct lysp_node_container *)siblings;
3210 assert_string_equal(parsed->name, "cont-name");
3211 lysp_node_free(st->ctx, siblings);
3212 siblings = NULL;
3213
3214 st->finished_correctly = true;
3215}
3216
David Sedlák5379d392019-07-24 10:42:03 +02003217static void
3218test_case_elem(void **state)
3219{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003220 struct test_parser_yin_state *st = *state;
David Sedlák5379d392019-07-24 10:42:03 +02003221 const char *data;
3222 struct lysp_node *siblings = NULL;
3223 struct tree_node_meta node_meta = {NULL, &siblings};
3224 struct lysp_node_case *parsed = NULL;
3225
3226 /* max subelems */
3227 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3228 data = ELEMENT_WRAPPER_START
3229 "<case name=\"case-name\">"
3230 "<anydata name=\"anyd\"/>"
3231 "<anyxml name=\"anyx\"/>"
3232 "<container name=\"subcont\"/>"
3233 "<description><text>desc</text></description>"
3234 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003235 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3236 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák5379d392019-07-24 10:42:03 +02003237 "<list name=\"list\"/>"
3238 "<reference><text>ref</text></reference>"
3239 "<status value=\"current\"/>"
3240 "<uses name=\"uses-name\"/>"
3241 "<when condition=\"when-cond\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003242 "<choice name=\"choice\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003243 EXT_SUBELEM
David Sedlák5379d392019-07-24 10:42:03 +02003244 "</case>"
3245 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003246 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák5379d392019-07-24 10:42:03 +02003247 parsed = (struct lysp_node_case *)siblings;
3248 assert_string_equal(parsed->name, "case-name");
3249 assert_null(parsed->parent);
3250 assert_int_equal(parsed->nodetype, LYS_CASE);
3251 assert_true(parsed->flags & LYS_STATUS_CURR);
3252 assert_null(parsed->next);
3253 assert_string_equal(parsed->dsc, "desc");
3254 assert_string_equal(parsed->ref, "ref");
3255 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003256 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlák5379d392019-07-24 10:42:03 +02003257 assert_string_equal(parsed->child->name, "anyd");
3258 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
3259 assert_string_equal(parsed->child->next->name, "anyx");
3260 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
3261 assert_string_equal(parsed->child->next->next->name, "subcont");
3262 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
3263 assert_string_equal(parsed->child->next->next->next->name, "leaf");
3264 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
3265 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
3266 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
3267 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
3268 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
3269 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
3270 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003271 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
3272 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
3273 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke0ef1c62019-09-13 10:05:55 +02003274 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003275 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3276 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák5379d392019-07-24 10:42:03 +02003277 lysp_node_free(st->ctx, siblings);
3278 siblings = NULL;
3279
3280 /* min subelems */
3281 data = ELEMENT_WRAPPER_START "<case name=\"case-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003282 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák5379d392019-07-24 10:42:03 +02003283 parsed = (struct lysp_node_case *)siblings;
3284 assert_string_equal(parsed->name, "case-name");
3285 lysp_node_free(st->ctx, siblings);
3286 siblings = NULL;
3287
3288 st->finished_correctly = true;
3289}
3290
David Sedlákb7abcfa2019-07-24 12:33:35 +02003291static void
3292test_choice_elem(void **state)
3293{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003294 struct test_parser_yin_state *st = *state;
David Sedlákb7abcfa2019-07-24 12:33:35 +02003295 const char *data;
3296 struct lysp_node *siblings = NULL;
3297 struct tree_node_meta node_meta = {NULL, &siblings};
3298 struct lysp_node_choice *parsed = NULL;
3299
3300 /* max subelems */
3301 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3302 data = ELEMENT_WRAPPER_START
3303 "<choice name=\"choice-name\">"
3304 "<anydata name=\"anyd\"/>"
3305 "<anyxml name=\"anyx\"/>"
3306 "<case name=\"sub-case\"/>"
3307 "<choice name=\"choice\"/>"
3308 "<config value=\"true\"/>"
3309 "<container name=\"subcont\"/>"
3310 "<default value=\"def\"/>"
3311 "<description><text>desc</text></description>"
3312 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003313 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3314 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003315 "<list name=\"list\"/>"
3316 "<mandatory value=\"true\" />"
3317 "<reference><text>ref</text></reference>"
3318 "<status value=\"current\"/>"
3319 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003320 EXT_SUBELEM
David Sedlákb7abcfa2019-07-24 12:33:35 +02003321 "</choice>"
3322 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003323 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003324 parsed = (struct lysp_node_choice *)siblings;
3325 assert_string_equal(parsed->name, "choice-name");
3326 assert_null(parsed->parent);
3327 assert_int_equal(parsed->nodetype, LYS_CHOICE);
3328 assert_true(parsed->flags & LYS_CONFIG_W && parsed->flags & LYS_MAND_TRUE && parsed->flags & LYS_STATUS_CURR);
3329 assert_null(parsed->next);
3330 assert_string_equal(parsed->dsc, "desc");
3331 assert_string_equal(parsed->ref, "ref");
3332 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003333 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlákb7abcfa2019-07-24 12:33:35 +02003334 assert_string_equal(parsed->child->name, "anyd");
3335 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
3336 assert_string_equal(parsed->child->next->name, "anyx");
3337 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
3338 assert_string_equal(parsed->child->next->next->name, "sub-case");
3339 assert_int_equal(parsed->child->next->next->nodetype, LYS_CASE);
3340 assert_string_equal(parsed->child->next->next->next->name, "choice");
3341 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
3342 assert_string_equal(parsed->child->next->next->next->next->name, "subcont");
3343 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_CONTAINER);
3344 assert_string_equal(parsed->child->next->next->next->next->next->name, "leaf");
3345 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAF);
3346 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "llist");
3347 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3348 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "list");
3349 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
3350 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke0ef1c62019-09-13 10:05:55 +02003351 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003352 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3353 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003354 lysp_node_free(st->ctx, siblings);
3355 siblings = NULL;
3356
3357 /* min subelems */
3358 data = ELEMENT_WRAPPER_START "<choice name=\"choice-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003359 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003360 parsed = (struct lysp_node_choice *)siblings;
3361 assert_string_equal(parsed->name, "choice-name");
3362 lysp_node_free(st->ctx, siblings);
3363 siblings = NULL;
3364
3365 st->finished_correctly = true;
3366}
3367
David Sedlák05404f62019-07-24 14:11:53 +02003368static void
3369test_inout_elem(void **state)
3370{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003371 struct test_parser_yin_state *st = *state;
David Sedlák05404f62019-07-24 14:11:53 +02003372 const char *data;
3373 struct lysp_action_inout inout = {};
3374 struct inout_meta inout_meta = {NULL, &inout};
3375
3376 /* max subelements */
3377 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3378 data = ELEMENT_WRAPPER_START
3379 "<input>"
3380 "<anydata name=\"anyd\"/>"
3381 "<anyxml name=\"anyx\"/>"
3382 "<choice name=\"choice\"/>"
3383 "<container name=\"subcont\"/>"
3384 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003385 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3386 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák05404f62019-07-24 14:11:53 +02003387 "<list name=\"list\"/>"
3388 "<must condition=\"cond\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003389 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák05404f62019-07-24 14:11:53 +02003390 "<uses name=\"uses-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003391 EXT_SUBELEM
David Sedlák05404f62019-07-24 14:11:53 +02003392 "</input>"
3393 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003394 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_SUCCESS);
David Sedlák05404f62019-07-24 14:11:53 +02003395 assert_null(inout.parent);
3396 assert_int_equal(inout.nodetype, LYS_INPUT);
Michal Vasko7f45cf22020-10-01 12:49:44 +02003397 assert_string_equal(inout.musts->arg.str, "cond");
David Sedlák05404f62019-07-24 14:11:53 +02003398 assert_string_equal(inout.typedefs->name, "tpdf");
3399 assert_string_equal(inout.groupings->name, "sub-grp");
3400 assert_string_equal(inout.data->name, "anyd");
3401 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
3402 assert_string_equal(inout.data->next->name, "anyx");
3403 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
3404 assert_string_equal(inout.data->next->next->name, "choice");
3405 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
3406 assert_string_equal(inout.data->next->next->next->name, "subcont");
3407 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
3408 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
3409 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
3410 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
3411 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3412 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
3413 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
3414 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
3415 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3416 assert_null(inout.data->next->next->next->next->next->next->next->next);
David Sedláke0ef1c62019-09-13 10:05:55 +02003417 assert_string_equal(inout.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003418 assert_int_equal(inout.exts[0].insubstmt_index, 0);
3419 assert_int_equal(inout.exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák05404f62019-07-24 14:11:53 +02003420 lysp_action_inout_free(st->ctx, &inout);
3421 memset(&inout, 0, sizeof inout);
3422
3423 /* max subelements */
3424 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3425 data = ELEMENT_WRAPPER_START
3426 "<output>"
3427 "<anydata name=\"anyd\"/>"
3428 "<anyxml name=\"anyx\"/>"
3429 "<choice name=\"choice\"/>"
3430 "<container name=\"subcont\"/>"
3431 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003432 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3433 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák05404f62019-07-24 14:11:53 +02003434 "<list name=\"list\"/>"
3435 "<must condition=\"cond\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003436 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák05404f62019-07-24 14:11:53 +02003437 "<uses name=\"uses-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003438 EXT_SUBELEM
David Sedlák05404f62019-07-24 14:11:53 +02003439 "</output>"
3440 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003441 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_SUCCESS);
David Sedlák05404f62019-07-24 14:11:53 +02003442 assert_null(inout.parent);
3443 assert_int_equal(inout.nodetype, LYS_OUTPUT);
Michal Vasko7f45cf22020-10-01 12:49:44 +02003444 assert_string_equal(inout.musts->arg.str, "cond");
David Sedlák05404f62019-07-24 14:11:53 +02003445 assert_string_equal(inout.typedefs->name, "tpdf");
3446 assert_string_equal(inout.groupings->name, "sub-grp");
3447 assert_string_equal(inout.data->name, "anyd");
3448 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
3449 assert_string_equal(inout.data->next->name, "anyx");
3450 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
3451 assert_string_equal(inout.data->next->next->name, "choice");
3452 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
3453 assert_string_equal(inout.data->next->next->next->name, "subcont");
3454 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
3455 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
3456 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
3457 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
3458 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3459 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
3460 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
3461 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
3462 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3463 assert_null(inout.data->next->next->next->next->next->next->next->next);
David Sedláke0ef1c62019-09-13 10:05:55 +02003464 assert_string_equal(inout.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003465 assert_int_equal(inout.exts[0].insubstmt_index, 0);
3466 assert_int_equal(inout.exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák05404f62019-07-24 14:11:53 +02003467 lysp_action_inout_free(st->ctx, &inout);
3468 memset(&inout, 0, sizeof inout);
3469
3470 /* min subelems */
Michal Vaskob83af8a2020-01-06 09:49:22 +01003471 data = ELEMENT_WRAPPER_START "<input><leaf name=\"l\"><type name=\"empty\"/></leaf></input>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003472 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_SUCCESS);
Michal Vaskob83af8a2020-01-06 09:49:22 +01003473 lysp_action_inout_free(st->ctx, &inout);
David Sedlák05404f62019-07-24 14:11:53 +02003474 memset(&inout, 0, sizeof inout);
3475
Michal Vaskob83af8a2020-01-06 09:49:22 +01003476 data = ELEMENT_WRAPPER_START "<output><leaf name=\"l\"><type name=\"empty\"/></leaf></output>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003477 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_SUCCESS);
Michal Vaskob83af8a2020-01-06 09:49:22 +01003478 lysp_action_inout_free(st->ctx, &inout);
David Sedlák05404f62019-07-24 14:11:53 +02003479 memset(&inout, 0, sizeof inout);
3480
3481 /* invalid combinations */
3482 data = ELEMENT_WRAPPER_START "<input name=\"test\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003483 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02003484 logbuf_assert("Unexpected attribute \"name\" of \"input\" element. Line number 1.");
David Sedlák05404f62019-07-24 14:11:53 +02003485 memset(&inout, 0, sizeof inout);
3486
3487 st->finished_correctly = true;
3488}
3489
David Sedlák85d0eca2019-07-24 15:15:21 +02003490static void
3491test_action_elem(void **state)
3492{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003493 struct test_parser_yin_state *st = *state;
David Sedlák85d0eca2019-07-24 15:15:21 +02003494 const char *data;
3495 struct lysp_action *actions = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003496 struct tree_node_meta act_meta = {NULL, (struct lysp_node **)&actions};
David Sedlák85d0eca2019-07-24 15:15:21 +02003497
3498 /* max subelems */
3499 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3500 data = ELEMENT_WRAPPER_START
3501 "<action name=\"act\">"
3502 "<description><text>desc</text></description>"
3503 "<grouping name=\"grouping\"/>"
3504 "<if-feature name=\"iff\"/>"
3505 "<input><uses name=\"uses-name\"/></input>"
Michal Vaskob83af8a2020-01-06 09:49:22 +01003506 "<output><must condition=\"cond\"/><leaf name=\"l\"><type name=\"type\"/></leaf></output>"
David Sedlák85d0eca2019-07-24 15:15:21 +02003507 "<reference><text>ref</text></reference>"
3508 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003509 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákd1144562019-08-06 12:36:14 +02003510 EXT_SUBELEM
David Sedlák85d0eca2019-07-24 15:15:21 +02003511 "</action>"
3512 ELEMENT_WRAPPER_END;
Michal Vasko1bf09392020-03-27 12:38:10 +01003513 /* there must be parent for action */
Radek Krejcif482abc2020-08-27 15:29:05 +02003514 act_meta.parent = (void*)1;
Michal Vaskob36053d2020-03-26 15:49:30 +01003515 assert_int_equal(test_element_helper(st, data, &act_meta, NULL, NULL), LY_SUCCESS);
Michal Vasko1bf09392020-03-27 12:38:10 +01003516 act_meta.parent = NULL;
3517 assert_non_null(actions->parent);
David Sedlák85d0eca2019-07-24 15:15:21 +02003518 assert_int_equal(actions->nodetype, LYS_ACTION);
3519 assert_true(actions->flags & LYS_STATUS_DEPRC);
3520 assert_string_equal(actions->name, "act");
3521 assert_string_equal(actions->dsc, "desc");
3522 assert_string_equal(actions->ref, "ref");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003523 assert_string_equal(actions->iffeatures[0].str, "iff");
David Sedlák85d0eca2019-07-24 15:15:21 +02003524 assert_string_equal(actions->typedefs->name, "tpdf");
3525 assert_string_equal(actions->groupings->name, "grouping");
3526 assert_string_equal(actions->input.data->name, "uses-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003527 assert_string_equal(actions->output.musts->arg.str, "cond");
David Sedláke0ef1c62019-09-13 10:05:55 +02003528 assert_string_equal(actions->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003529 assert_int_equal(actions->exts[0].insubstmt_index, 0);
3530 assert_int_equal(actions->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák85d0eca2019-07-24 15:15:21 +02003531 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3532 actions = NULL;
3533
David Sedlákeaa45792019-07-24 15:25:01 +02003534 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3535 data = ELEMENT_WRAPPER_START
3536 "<rpc name=\"act\">"
3537 "<description><text>desc</text></description>"
3538 "<grouping name=\"grouping\"/>"
3539 "<if-feature name=\"iff\"/>"
3540 "<input><uses name=\"uses-name\"/></input>"
Michal Vaskob83af8a2020-01-06 09:49:22 +01003541 "<output><must condition=\"cond\"/><leaf name=\"l\"><type name=\"type\"/></leaf></output>"
David Sedlákeaa45792019-07-24 15:25:01 +02003542 "<reference><text>ref</text></reference>"
3543 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003544 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákd1144562019-08-06 12:36:14 +02003545 EXT_SUBELEM
David Sedlákeaa45792019-07-24 15:25:01 +02003546 "</rpc>"
3547 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003548 assert_int_equal(test_element_helper(st, data, &act_meta, NULL, NULL), LY_SUCCESS);
David Sedlákeaa45792019-07-24 15:25:01 +02003549 assert_null(actions->parent);
Michal Vasko1bf09392020-03-27 12:38:10 +01003550 assert_int_equal(actions->nodetype, LYS_RPC);
David Sedlákeaa45792019-07-24 15:25:01 +02003551 assert_true(actions->flags & LYS_STATUS_DEPRC);
3552 assert_string_equal(actions->name, "act");
3553 assert_string_equal(actions->dsc, "desc");
3554 assert_string_equal(actions->ref, "ref");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003555 assert_string_equal(actions->iffeatures[0].str, "iff");
David Sedlákeaa45792019-07-24 15:25:01 +02003556 assert_string_equal(actions->typedefs->name, "tpdf");
3557 assert_string_equal(actions->groupings->name, "grouping");
3558 assert_string_equal(actions->input.data->name, "uses-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003559 assert_string_equal(actions->output.musts->arg.str, "cond");
David Sedláke0ef1c62019-09-13 10:05:55 +02003560 assert_string_equal(actions->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003561 assert_int_equal(actions->exts[0].insubstmt_index, 0);
3562 assert_int_equal(actions->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákeaa45792019-07-24 15:25:01 +02003563 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3564 actions = NULL;
3565
David Sedlák85d0eca2019-07-24 15:15:21 +02003566 /* min subelems */
3567 data = ELEMENT_WRAPPER_START "<action name=\"act\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003568 assert_int_equal(test_element_helper(st, data, &act_meta, NULL, NULL), LY_SUCCESS);
David Sedlák85d0eca2019-07-24 15:15:21 +02003569 assert_string_equal(actions->name, "act");
3570 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3571 actions = NULL;
3572
3573 st->finished_correctly = true;
3574}
3575
David Sedlák992fb7c2019-07-24 16:51:01 +02003576static void
3577test_augment_elem(void **state)
3578{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003579 struct test_parser_yin_state *st = *state;
David Sedlák992fb7c2019-07-24 16:51:01 +02003580 const char *data;
3581 struct lysp_augment *augments = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003582 struct tree_node_meta aug_meta = {NULL, (struct lysp_node **)&augments};
David Sedlák992fb7c2019-07-24 16:51:01 +02003583
3584 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3585 data = ELEMENT_WRAPPER_START
3586 "<augment target-node=\"target\">"
3587 "<action name=\"action\"/>"
3588 "<anydata name=\"anyd\"/>"
3589 "<anyxml name=\"anyx\"/>"
3590 "<case name=\"case\"/>"
3591 "<choice name=\"choice\"/>"
3592 "<container name=\"subcont\"/>"
3593 "<description><text>desc</text></description>"
3594 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003595 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3596 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák992fb7c2019-07-24 16:51:01 +02003597 "<list name=\"list\"/>"
3598 "<notification name=\"notif\"/>"
3599 "<reference><text>ref</text></reference>"
3600 "<status value=\"current\"/>"
3601 "<uses name=\"uses\"/>"
3602 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003603 EXT_SUBELEM
David Sedlák992fb7c2019-07-24 16:51:01 +02003604 "</augment>"
3605 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003606 assert_int_equal(test_element_helper(st, data, &aug_meta, NULL, NULL), LY_SUCCESS);
David Sedlák992fb7c2019-07-24 16:51:01 +02003607 assert_string_equal(augments->nodeid, "target");
3608 assert_null(augments->parent);
3609 assert_int_equal(augments->nodetype, LYS_AUGMENT);
3610 assert_true(augments->flags & LYS_STATUS_CURR);
3611 assert_string_equal(augments->dsc, "desc");
3612 assert_string_equal(augments->ref, "ref");
3613 assert_string_equal(augments->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003614 assert_string_equal(augments->iffeatures[0].str, "iff");
David Sedlák992fb7c2019-07-24 16:51:01 +02003615 assert_string_equal(augments->child->name, "anyd");
3616 assert_int_equal(augments->child->nodetype, LYS_ANYDATA);
3617 assert_string_equal(augments->child->next->name, "anyx");
3618 assert_int_equal(augments->child->next->nodetype, LYS_ANYXML);
3619 assert_string_equal(augments->child->next->next->name, "case");
3620 assert_int_equal(augments->child->next->next->nodetype, LYS_CASE);
3621 assert_string_equal(augments->child->next->next->next->name, "choice");
3622 assert_int_equal(augments->child->next->next->next->nodetype, LYS_CHOICE);
3623 assert_string_equal(augments->child->next->next->next->next->name, "subcont");
3624 assert_int_equal(augments->child->next->next->next->next->nodetype, LYS_CONTAINER);
3625 assert_string_equal(augments->child->next->next->next->next->next->name, "leaf");
3626 assert_int_equal(augments->child->next->next->next->next->next->nodetype, LYS_LEAF);
3627 assert_string_equal(augments->child->next->next->next->next->next->next->name, "llist");
3628 assert_int_equal(augments->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3629 assert_string_equal(augments->child->next->next->next->next->next->next->next->name, "list");
3630 assert_int_equal(augments->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
3631 assert_string_equal(augments->child->next->next->next->next->next->next->next->next->name, "uses");
3632 assert_int_equal(augments->child->next->next->next->next->next->next->next->next->nodetype, LYS_USES);
3633 assert_null(augments->child->next->next->next->next->next->next->next->next->next);
3634 assert_string_equal(augments->actions->name, "action");
3635 assert_string_equal(augments->notifs->name, "notif");
David Sedláke0ef1c62019-09-13 10:05:55 +02003636 assert_string_equal(augments->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003637 assert_int_equal(augments->exts[0].insubstmt_index, 0);
3638 assert_int_equal(augments->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák992fb7c2019-07-24 16:51:01 +02003639 FREE_ARRAY(st->ctx, augments, lysp_augment_free)
3640 augments = NULL;
3641
3642 data = ELEMENT_WRAPPER_START "<augment target-node=\"target\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003643 assert_int_equal(test_element_helper(st, data, &aug_meta, NULL, NULL), LY_SUCCESS);
David Sedlák992fb7c2019-07-24 16:51:01 +02003644 assert_string_equal(augments->nodeid, "target");
3645 FREE_ARRAY(st->ctx, augments, lysp_augment_free)
3646 augments = NULL;
3647
3648 st->finished_correctly = true;
3649}
3650
David Sedlák4ffcec82019-07-25 15:10:21 +02003651static void
3652test_deviate_elem(void **state)
3653{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003654 struct test_parser_yin_state *st = *state;
David Sedlák4ffcec82019-07-25 15:10:21 +02003655 const char *data;
3656 struct lysp_deviate *deviates = NULL;
3657 struct lysp_deviate_add *d_add;
3658 struct lysp_deviate_rpl *d_rpl;
3659 struct lysp_deviate_del *d_del;
3660
3661 /* all valid arguments with min subelems */
3662 data = ELEMENT_WRAPPER_START "<deviate value=\"not-supported\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003663 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003664 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
3665 lysp_deviate_free(st->ctx, deviates);
3666 free(deviates);
3667 deviates = NULL;
3668
3669 data = ELEMENT_WRAPPER_START "<deviate value=\"add\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003670 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003671 assert_int_equal(deviates->mod, LYS_DEV_ADD);
3672 lysp_deviate_free(st->ctx, deviates);
3673 free(deviates);
3674 deviates = NULL;
3675
3676 data = ELEMENT_WRAPPER_START "<deviate value=\"replace\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003677 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003678 assert_int_equal(deviates->mod, LYS_DEV_REPLACE);
3679 lysp_deviate_free(st->ctx, deviates);
3680 free(deviates);
3681 deviates = NULL;
3682
3683 data = ELEMENT_WRAPPER_START "<deviate value=\"delete\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003684 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003685 assert_int_equal(deviates->mod, LYS_DEV_DELETE);
3686 lysp_deviate_free(st->ctx, deviates);
3687 free(deviates);
3688 deviates = NULL;
3689
3690 /* max subelems and valid arguments */
3691 data = ELEMENT_WRAPPER_START
3692 "<deviate value=\"not-supported\">"
David Sedlákd1144562019-08-06 12:36:14 +02003693 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003694 "</deviate>"
3695 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003696 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003697 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
David Sedláke0ef1c62019-09-13 10:05:55 +02003698 assert_string_equal(deviates->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003699 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3700 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003701 lysp_deviate_free(st->ctx, deviates);
3702 free(deviates);
3703 deviates = NULL;
3704
3705 data = ELEMENT_WRAPPER_START
3706 "<deviate value=\"add\">"
3707 "<units name=\"units\"/>"
3708 "<must condition=\"cond\"/>"
3709 "<unique tag=\"utag\"/>"
3710 "<default value=\"def\"/>"
3711 "<config value=\"true\"/>"
3712 "<mandatory value=\"true\"/>"
3713 "<min-elements value=\"5\"/>"
3714 "<max-elements value=\"15\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003715 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003716 "</deviate>"
3717 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003718 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003719 d_add = (struct lysp_deviate_add *)deviates;
3720 assert_int_equal(d_add->mod, LYS_DEV_ADD);
3721 assert_null(d_add->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003722 assert_string_equal(d_add->units, "units");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003723 assert_string_equal(d_add->musts->arg.str, "cond");
David Sedlák4ffcec82019-07-25 15:10:21 +02003724 assert_string_equal(*d_add->uniques, "utag");
3725 assert_string_equal(*d_add->dflts, "def");
3726 assert_true(d_add->flags & LYS_MAND_TRUE && d_add->flags & LYS_CONFIG_W);
3727 assert_int_equal(d_add->min, 5);
3728 assert_int_equal(d_add->max, 15);
David Sedláke0ef1c62019-09-13 10:05:55 +02003729 assert_string_equal(deviates->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003730 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3731 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003732 lysp_deviate_free(st->ctx, deviates);
3733 free(deviates);
3734 deviates = NULL;
3735
3736 data = ELEMENT_WRAPPER_START
3737 "<deviate value=\"replace\">"
3738 "<type name=\"newtype\"/>"
3739 "<units name=\"uni\"/>"
3740 "<default value=\"def\"/>"
3741 "<config value=\"true\"/>"
3742 "<mandatory value=\"true\"/>"
3743 "<min-elements value=\"5\"/>"
3744 "<max-elements value=\"15\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003745 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003746 "</deviate>"
3747 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003748 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003749 d_rpl = (struct lysp_deviate_rpl *)deviates;
3750 assert_int_equal(d_rpl->mod, LYS_DEV_REPLACE);
3751 assert_null(d_rpl->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003752 assert_string_equal(d_rpl->type->name, "newtype");
3753 assert_string_equal(d_rpl->units, "uni");
3754 assert_string_equal(d_rpl->dflt, "def");
3755 assert_true(d_rpl->flags & LYS_MAND_TRUE && d_rpl->flags & LYS_CONFIG_W);
3756 assert_int_equal(d_rpl->min, 5);
3757 assert_int_equal(d_rpl->max, 15);
David Sedláke0ef1c62019-09-13 10:05:55 +02003758 assert_string_equal(deviates->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003759 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3760 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003761 lysp_deviate_free(st->ctx, deviates);
3762 free(deviates);
3763 deviates = NULL;
3764
3765 data = ELEMENT_WRAPPER_START
3766 "<deviate value=\"delete\">"
3767 "<units name=\"u\"/>"
3768 "<must condition=\"c\"/>"
3769 "<unique tag=\"tag\"/>"
3770 "<default value=\"default\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003771 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003772 "</deviate>"
3773 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003774 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003775 d_del = (struct lysp_deviate_del *)deviates;
3776 assert_int_equal(d_del->mod, LYS_DEV_DELETE);
3777 assert_null(d_del->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003778 assert_string_equal(d_del->units, "u");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003779 assert_string_equal(d_del->musts->arg.str, "c");
David Sedlák4ffcec82019-07-25 15:10:21 +02003780 assert_string_equal(*d_del->uniques, "tag");
3781 assert_string_equal(*d_del->dflts, "default");
David Sedláke0ef1c62019-09-13 10:05:55 +02003782 assert_string_equal(deviates->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003783 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3784 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003785 lysp_deviate_free(st->ctx, deviates);
3786 free(deviates);
3787 deviates = NULL;
3788
3789 /* invalid arguments */
3790 data = ELEMENT_WRAPPER_START "<deviate value=\"\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003791 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003792 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 +02003793 deviates = NULL;
3794
3795 data = ELEMENT_WRAPPER_START "<deviate value=\"invalid\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003796 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003797 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 +02003798 deviates = NULL;
3799
3800 data = ELEMENT_WRAPPER_START "<deviate value=\"ad\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003801 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003802 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 +02003803 deviates = NULL;
3804
3805 data = ELEMENT_WRAPPER_START "<deviate value=\"adds\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003806 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003807 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 +02003808 deviates = NULL;
3809
3810 data = ELEMENT_WRAPPER_START
3811 "<deviate value=\"not-supported\">"
3812 "<must condition=\"c\"/>"
3813 "</deviate>"
3814 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003815 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_EVALID);
David Sedlák4ffcec82019-07-25 15:10:21 +02003816 logbuf_assert("Deviate of this type doesn't allow \"must\" as it's sub-element. Line number 1.");
3817
3818 st->finished_correctly = true;
3819}
3820
David Sedlák8b754462019-07-25 16:22:13 +02003821static void
3822test_deviation_elem(void **state)
3823{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003824 struct test_parser_yin_state *st = *state;
David Sedlák8b754462019-07-25 16:22:13 +02003825 const char *data;
3826 struct lysp_deviation *deviations = NULL;
3827
3828 /* min subelems */
3829 data = ELEMENT_WRAPPER_START
3830 "<deviation target-node=\"target\">"
3831 "<deviate value=\"not-supported\"/>"
3832 "</deviation>"
3833 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003834 assert_int_equal(test_element_helper(st, data, &deviations, NULL, NULL), LY_SUCCESS);
David Sedlák8b754462019-07-25 16:22:13 +02003835 assert_string_equal(deviations->nodeid, "target");
3836 assert_int_equal(deviations->deviates->mod, LYS_DEV_NOT_SUPPORTED);
3837 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3838 deviations = NULL;
3839
3840 /* max subelems */
3841 data = ELEMENT_WRAPPER_START
3842 "<deviation target-node=\"target\">"
3843 "<reference><text>ref</text></reference>"
3844 "<description><text>desc</text></description>"
3845 "<deviate value=\"add\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003846 EXT_SUBELEM
David Sedlák8b754462019-07-25 16:22:13 +02003847 "</deviation>"
3848 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003849 assert_int_equal(test_element_helper(st, data, &deviations, NULL, NULL), LY_SUCCESS);
David Sedlák8b754462019-07-25 16:22:13 +02003850 assert_string_equal(deviations->nodeid, "target");
3851 assert_int_equal(deviations->deviates->mod, LYS_DEV_ADD);
3852 assert_string_equal(deviations->ref, "ref");
3853 assert_string_equal(deviations->dsc, "desc");
David Sedláke0ef1c62019-09-13 10:05:55 +02003854 assert_string_equal(deviations->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003855 assert_int_equal(deviations->exts[0].insubstmt_index, 0);
3856 assert_int_equal(deviations->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8b754462019-07-25 16:22:13 +02003857 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3858 deviations = NULL;
3859
3860 /* invalid */
3861 data = ELEMENT_WRAPPER_START "<deviation target-node=\"target\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003862 assert_int_equal(test_element_helper(st, data, &deviations, NULL, NULL), LY_EVALID);
David Sedlák8b754462019-07-25 16:22:13 +02003863 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3864 deviations = NULL;
David Sedlák1538a842019-08-08 15:38:51 +02003865 logbuf_assert("Missing mandatory sub-element \"deviate\" of \"deviation\" element. Line number 1.");
3866 /* TODO */
David Sedlák8b754462019-07-25 16:22:13 +02003867 st->finished_correctly = true;
3868}
3869
David Sedlák4f03b932019-07-26 13:01:47 +02003870static void
3871test_module_elem(void **state)
3872{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003873 struct test_parser_yin_state *st = *state;
Michal Vaskob36053d2020-03-26 15:49:30 +01003874 const char *data;
David Sedlák4f03b932019-07-26 13:01:47 +02003875 struct lys_module *lys_mod = NULL;
3876 struct lysp_module *lysp_mod = NULL;
3877
3878 /* max subelems */
David Sedlák4f03b932019-07-26 13:01:47 +02003879 lys_mod = calloc(1, sizeof *lys_mod);
3880 lysp_mod = calloc(1, sizeof *lysp_mod);
3881 lys_mod->ctx = st->ctx;
3882 lysp_mod->mod = lys_mod;
3883 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
3884 "<yang-version value=\"1.1\"/>\n"
3885 "<namespace uri=\"ns\"/>\n"
3886 "<prefix value=\"pref\"/>\n"
3887 "<include module=\"b-mod\"/>\n"
3888 "<import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
3889 "<organization><text>org</text></organization>\n"
3890 "<contact><text>contact</text></contact>\n"
3891 "<description><text>desc</text></description>"
3892 "<reference><text>ref</text></reference>\n"
3893 "<revision date=\"2019-02-02\"/>\n"
3894 "<anydata name=\"anyd\"/>\n"
3895 "<anyxml name=\"anyx\"/>\n"
3896 "<choice name=\"choice\"/>\n"
3897 "<container name=\"cont\"/>\n"
3898 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3899 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3900 "<list name=\"sub-list\"/>\n"
3901 "<uses name=\"uses-name\"/>\n"
3902 "<augment target-node=\"target\"/>\n"
3903 "<deviation target-node=\"target\">""<deviate value=\"not-supported\"/>""</deviation>\n"
3904 "<extension name=\"ext\"/>\n"
3905 "<feature name=\"feature\"/>\n"
3906 "<grouping name=\"grp\"/>\n"
3907 "<identity name=\"ident-name\"/>\n"
3908 "<notification name=\"notf\"/>\n"
3909 "<rpc name=\"rpc-name\"/>\n"
3910 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
David Sedlákd1144562019-08-06 12:36:14 +02003911 EXT_SUBELEM"\n"
David Sedlák4f03b932019-07-26 13:01:47 +02003912 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003913 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
3914 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01003915
3916 assert_int_equal(yin_parse_mod(st->yin_ctx, lysp_mod), LY_SUCCESS);
David Sedlák4f03b932019-07-26 13:01:47 +02003917 assert_string_equal(lysp_mod->mod->name, "mod");
3918 assert_string_equal(lysp_mod->revs, "2019-02-02");
3919 assert_string_equal(lysp_mod->mod->ns, "ns");
3920 assert_string_equal(lysp_mod->mod->prefix, "pref");
3921 assert_null(lysp_mod->mod->filepath);
3922 assert_string_equal(lysp_mod->mod->org, "org");
3923 assert_string_equal(lysp_mod->mod->contact, "contact");
3924 assert_string_equal(lysp_mod->mod->dsc, "desc");
3925 assert_string_equal(lysp_mod->mod->ref, "ref");
3926 assert_int_equal(lysp_mod->mod->version, LYS_VERSION_1_1);
3927 assert_string_equal(lysp_mod->imports->name, "a-mod");
3928 assert_string_equal(lysp_mod->includes->name, "b-mod");
3929 assert_string_equal(lysp_mod->extensions->name, "ext");
3930 assert_string_equal(lysp_mod->features->name, "feature");
3931 assert_string_equal(lysp_mod->identities->name, "ident-name");
3932 assert_string_equal(lysp_mod->typedefs->name, "tpdf");
3933 assert_string_equal(lysp_mod->groupings->name, "grp");
3934 assert_string_equal(lysp_mod->data->name, "anyd");
3935 assert_int_equal(lysp_mod->data->nodetype, LYS_ANYDATA);
3936 assert_string_equal(lysp_mod->data->next->name, "anyx");
3937 assert_int_equal(lysp_mod->data->next->nodetype, LYS_ANYXML);
3938 assert_string_equal(lysp_mod->data->next->next->name, "choice");
3939 assert_int_equal(lysp_mod->data->next->next->nodetype, LYS_CHOICE);
3940 assert_string_equal(lysp_mod->data->next->next->next->name, "cont");
3941 assert_int_equal(lysp_mod->data->next->next->next->nodetype, LYS_CONTAINER);
3942 assert_string_equal(lysp_mod->data->next->next->next->next->name, "leaf");
3943 assert_int_equal(lysp_mod->data->next->next->next->next->nodetype, LYS_LEAF);
3944 assert_string_equal(lysp_mod->data->next->next->next->next->next->name, "llist");
3945 assert_int_equal(lysp_mod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3946 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->name, "sub-list");
3947 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
3948 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->next->name, "uses-name");
3949 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3950 assert_null(lysp_mod->data->next->next->next->next->next->next->next->next);
3951 assert_string_equal(lysp_mod->augments->nodeid, "target");
3952 assert_string_equal(lysp_mod->rpcs->name, "rpc-name");
3953 assert_string_equal(lysp_mod->notifs->name, "notf");
3954 assert_string_equal(lysp_mod->deviations->nodeid, "target");
David Sedláke0ef1c62019-09-13 10:05:55 +02003955 assert_string_equal(lysp_mod->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003956 assert_int_equal(lysp_mod->exts[0].insubstmt_index, 0);
3957 assert_int_equal(lysp_mod->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4f03b932019-07-26 13:01:47 +02003958 lysp_module_free(lysp_mod);
3959 lys_module_free(lys_mod, NULL);
David Sedlák4f03b932019-07-26 13:01:47 +02003960
3961 /* min subelems */
Michal Vasko63f3d842020-07-08 10:10:14 +02003962 ly_in_free(st->in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +01003963 lyxml_ctx_free(st->yin_ctx->xmlctx);
David Sedlák4f03b932019-07-26 13:01:47 +02003964 lys_mod = calloc(1, sizeof *lys_mod);
3965 lysp_mod = calloc(1, sizeof *lysp_mod);
3966 lys_mod->ctx = st->ctx;
3967 lysp_mod->mod = lys_mod;
3968 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">"
3969 "<namespace uri=\"ns\"/>"
3970 "<prefix value=\"pref\"/>"
3971 "<yang-version value=\"1.1\"/>"
3972 "</module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003973 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
3974 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01003975 assert_int_equal(yin_parse_mod(st->yin_ctx, lysp_mod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003976 assert_string_equal(lysp_mod->mod->name, "mod");
David Sedlák4f03b932019-07-26 13:01:47 +02003977 lysp_module_free(lysp_mod);
3978 lys_module_free(lys_mod, NULL);
David Sedlák298ff6d2019-07-26 14:29:03 +02003979
David Sedláke6cd89e2019-08-07 12:46:02 +02003980 /* incorrect subelem order */
Michal Vasko63f3d842020-07-08 10:10:14 +02003981 ly_in_free(st->in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +01003982 lyxml_ctx_free(st->yin_ctx->xmlctx);
David Sedláke6cd89e2019-08-07 12:46:02 +02003983 lys_mod = calloc(1, sizeof *lys_mod);
3984 lysp_mod = calloc(1, sizeof *lysp_mod);
3985 lys_mod->ctx = st->ctx;
3986 lysp_mod->mod = lys_mod;
3987 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">"
3988 "<feature name=\"feature\"/>\n"
3989 "<namespace uri=\"ns\"/>"
3990 "<prefix value=\"pref\"/>"
3991 "<yang-version value=\"1.1\"/>"
3992 "</module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003993 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
3994 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01003995 assert_int_equal(yin_parse_mod(st->yin_ctx, lysp_mod), LY_EVALID);
3996 logbuf_assert("Invalid order of module\'s sub-elements \"namespace\" can\'t appear after \"feature\". Line number 2.");
David Sedláke6cd89e2019-08-07 12:46:02 +02003997 lysp_module_free(lysp_mod);
3998 lys_module_free(lys_mod, NULL);
David Sedláke6cd89e2019-08-07 12:46:02 +02003999
David Sedlák298ff6d2019-07-26 14:29:03 +02004000 st->finished_correctly = true;
4001}
4002
4003static void
4004test_submodule_elem(void **state)
4005{
Radek Krejci3a4889a2020-05-19 17:01:58 +02004006 struct test_parser_yin_state *st = *state;
Michal Vaskob36053d2020-03-26 15:49:30 +01004007 const char *data;
David Sedlák298ff6d2019-07-26 14:29:03 +02004008 struct lysp_submodule *lysp_submod = NULL;
4009
4010 /* max subelements */
David Sedlák298ff6d2019-07-26 14:29:03 +02004011 lysp_submod = calloc(1, sizeof *lysp_submod);
4012 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
4013 "<yang-version value=\"1.1\"/>\n"
Michal Vaskoc3781c32020-10-06 14:04:08 +02004014 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>"
David Sedlák298ff6d2019-07-26 14:29:03 +02004015 "<include module=\"b-mod\"/>\n"
4016 "<import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
4017 "<organization><text>org</text></organization>\n"
4018 "<contact><text>contact</text></contact>\n"
4019 "<description><text>desc</text></description>"
4020 "<reference><text>ref</text></reference>\n"
4021 "<revision date=\"2019-02-02\"/>\n"
4022 "<anydata name=\"anyd\"/>\n"
4023 "<anyxml name=\"anyx\"/>\n"
4024 "<choice name=\"choice\"/>\n"
4025 "<container name=\"cont\"/>\n"
4026 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
4027 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
4028 "<list name=\"sub-list\"/>\n"
4029 "<uses name=\"uses-name\"/>\n"
4030 "<augment target-node=\"target\"/>\n"
4031 "<deviation target-node=\"target\">""<deviate value=\"not-supported\"/>""</deviation>\n"
4032 "<extension name=\"ext\"/>\n"
4033 "<feature name=\"feature\"/>\n"
4034 "<grouping name=\"grp\"/>\n"
4035 "<identity name=\"ident-name\"/>\n"
4036 "<notification name=\"notf\"/>\n"
4037 "<rpc name=\"rpc-name\"/>\n"
4038 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
David Sedlákd1144562019-08-06 12:36:14 +02004039 EXT_SUBELEM"\n"
David Sedlák298ff6d2019-07-26 14:29:03 +02004040 "</submodule>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004041 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
4042 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01004043 assert_int_equal(yin_parse_submod(st->yin_ctx, lysp_submod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02004044
4045 assert_string_equal(lysp_submod->name, "mod");
4046 assert_string_equal(lysp_submod->revs, "2019-02-02");
David Sedlák298ff6d2019-07-26 14:29:03 +02004047 assert_string_equal(lysp_submod->prefix, "pref");
4048 assert_null(lysp_submod->filepath);
4049 assert_string_equal(lysp_submod->org, "org");
4050 assert_string_equal(lysp_submod->contact, "contact");
4051 assert_string_equal(lysp_submod->dsc, "desc");
4052 assert_string_equal(lysp_submod->ref, "ref");
4053 assert_int_equal(lysp_submod->version, LYS_VERSION_1_1);
4054 assert_string_equal(lysp_submod->imports->name, "a-mod");
4055 assert_string_equal(lysp_submod->includes->name, "b-mod");
4056 assert_string_equal(lysp_submod->extensions->name, "ext");
4057 assert_string_equal(lysp_submod->features->name, "feature");
4058 assert_string_equal(lysp_submod->identities->name, "ident-name");
4059 assert_string_equal(lysp_submod->typedefs->name, "tpdf");
4060 assert_string_equal(lysp_submod->groupings->name, "grp");
4061 assert_string_equal(lysp_submod->data->name, "anyd");
4062 assert_int_equal(lysp_submod->data->nodetype, LYS_ANYDATA);
4063 assert_string_equal(lysp_submod->data->next->name, "anyx");
4064 assert_int_equal(lysp_submod->data->next->nodetype, LYS_ANYXML);
4065 assert_string_equal(lysp_submod->data->next->next->name, "choice");
4066 assert_int_equal(lysp_submod->data->next->next->nodetype, LYS_CHOICE);
4067 assert_string_equal(lysp_submod->data->next->next->next->name, "cont");
4068 assert_int_equal(lysp_submod->data->next->next->next->nodetype, LYS_CONTAINER);
4069 assert_string_equal(lysp_submod->data->next->next->next->next->name, "leaf");
4070 assert_int_equal(lysp_submod->data->next->next->next->next->nodetype, LYS_LEAF);
4071 assert_string_equal(lysp_submod->data->next->next->next->next->next->name, "llist");
4072 assert_int_equal(lysp_submod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
4073 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->name, "sub-list");
4074 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
4075 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->next->name, "uses-name");
4076 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
4077 assert_null(lysp_submod->data->next->next->next->next->next->next->next->next);
4078 assert_string_equal(lysp_submod->augments->nodeid, "target");
4079 assert_string_equal(lysp_submod->rpcs->name, "rpc-name");
4080 assert_string_equal(lysp_submod->notifs->name, "notf");
4081 assert_string_equal(lysp_submod->deviations->nodeid, "target");
David Sedláke0ef1c62019-09-13 10:05:55 +02004082 assert_string_equal(lysp_submod->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02004083 assert_int_equal(lysp_submod->exts[0].insubstmt_index, 0);
4084 assert_int_equal(lysp_submod->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák298ff6d2019-07-26 14:29:03 +02004085
4086 lysp_submodule_free(st->ctx, lysp_submod);
David Sedlák298ff6d2019-07-26 14:29:03 +02004087
4088 /* min subelemnts */
Michal Vasko63f3d842020-07-08 10:10:14 +02004089 ly_in_free(st->in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +01004090 lyxml_ctx_free(st->yin_ctx->xmlctx);
David Sedlák298ff6d2019-07-26 14:29:03 +02004091 lysp_submod = calloc(1, sizeof *lysp_submod);
4092 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">"
Radek Krejci96e48da2020-09-04 13:18:06 +02004093 "<yang-version value=\"1\"/>"
Michal Vaskoc3781c32020-10-06 14:04:08 +02004094 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>"
David Sedlák298ff6d2019-07-26 14:29:03 +02004095 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004096 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
4097 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01004098 assert_int_equal(yin_parse_submod(st->yin_ctx, lysp_submod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02004099 assert_string_equal(lysp_submod->prefix, "pref");
David Sedlák298ff6d2019-07-26 14:29:03 +02004100 assert_int_equal(lysp_submod->version, LYS_VERSION_1_0);
4101 lysp_submodule_free(st->ctx, lysp_submod);
David Sedlák298ff6d2019-07-26 14:29:03 +02004102
David Sedláke6cd89e2019-08-07 12:46:02 +02004103 /* incorrect subelem order */
Michal Vasko63f3d842020-07-08 10:10:14 +02004104 ly_in_free(st->in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +01004105 lyxml_ctx_free(st->yin_ctx->xmlctx);
David Sedláke6cd89e2019-08-07 12:46:02 +02004106 lysp_submod = calloc(1, sizeof *lysp_submod);
4107 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">"
Radek Krejci96e48da2020-09-04 13:18:06 +02004108 "<yang-version value=\"1\"/>"
David Sedláke6cd89e2019-08-07 12:46:02 +02004109 "<reference><text>ref</text></reference>\n"
Michal Vaskoc3781c32020-10-06 14:04:08 +02004110 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>"
David Sedláke6cd89e2019-08-07 12:46:02 +02004111 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004112 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
4113 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01004114 assert_int_equal(yin_parse_submod(st->yin_ctx, lysp_submod), LY_EVALID);
4115 logbuf_assert("Invalid order of submodule's sub-elements \"belongs-to\" can't appear after \"reference\". Line number 2.");
David Sedláke6cd89e2019-08-07 12:46:02 +02004116 lysp_submodule_free(st->ctx, lysp_submod);
David Sedláke6cd89e2019-08-07 12:46:02 +02004117
David Sedlák298ff6d2019-07-26 14:29:03 +02004118 st->finished_correctly = true;
David Sedlák4f03b932019-07-26 13:01:47 +02004119}
4120
David Sedlák8985a142019-07-31 16:43:06 +02004121static void
4122test_yin_parse_module(void **state)
4123{
Radek Krejci3a4889a2020-05-19 17:01:58 +02004124 struct test_parser_yin_state *st = *state;
David Sedlák8985a142019-07-31 16:43:06 +02004125 const char *data;
4126 struct lys_module *mod;
Michal Vaskob36053d2020-03-26 15:49:30 +01004127 struct lys_yin_parser_ctx *yin_ctx = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02004128 struct ly_in *in = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02004129
4130 mod = calloc(1, sizeof *mod);
4131 mod->ctx = st->ctx;
David Sedlákd2844882019-09-13 16:01:22 +02004132 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\" name=\"a\"> \n"
4133 "<yang-version value=\"1.1\"/>\n"
4134 "<namespace uri=\"urn:tests:extensions:metadata:a\"/>\n"
4135 "<prefix value=\"a\"/>\n"
4136 "<import module=\"ietf-yang-metadata\">\n"
4137 "<prefix value=\"md\"/>\n"
4138 "</import>\n"
4139 "<feature name=\"f\"/>\n"
4140 "<md:annotation name=\"x\">\n"
4141 "<description>\n"
4142 "<text>test</text>\n"
4143 "</description>\n"
4144 "<reference>\n"
4145 "<text>test</text>\n"
4146 "</reference>\n"
4147 "<if-feature name=\"f\"/>\n"
4148 "<status value=\"current\"/>\n"
4149 "<type name=\"uint8\"/>\n"
4150 "<units name=\"meters\"/>\n"
4151 "</md:annotation>\n"
4152 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004153 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
4154 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS);
David Sedlákd2844882019-09-13 16:01:22 +02004155 assert_null(mod->parsed->exts->child->next->child);
4156 assert_string_equal(mod->parsed->exts->child->next->arg, "test");
4157 lys_module_free(mod, NULL);
4158 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004159 ly_in_free(in, 0);
David Sedlákd2844882019-09-13 16:01:22 +02004160 mod = NULL;
4161 yin_ctx = NULL;
4162
4163 mod = calloc(1, sizeof *mod);
4164 mod->ctx = st->ctx;
David Sedlák8985a142019-07-31 16:43:06 +02004165 data = "<module name=\"example-foo\""
4166 "xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
4167 "xmlns:foo=\"urn:example:foo\""
4168 "xmlns:myext=\"urn:example:extensions\">\n"
4169
Radek Krejci96e48da2020-09-04 13:18:06 +02004170 "<yang-version value=\"1\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02004171
4172 "<namespace uri=\"urn:example:foo\"/>\n"
4173 "<prefix value=\"foo\"/>\n"
4174
4175 "<import module=\"example-extensions\">\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004176 "<prefix value=\"myext\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02004177 "</import>\n"
4178
4179 "<list name=\"interface\">\n"
4180 "<key value=\"name\"/>\n"
4181 "<leaf name=\"name\">\n"
4182 "<type name=\"string\"/>\n"
4183 "</leaf>\n"
4184 "<leaf name=\"mtu\">\n"
4185 "<type name=\"uint32\"/>\n"
4186 "<description>\n"
4187 "<text>The MTU of the interface.</text>\n"
4188 "</description>\n"
4189 "<myext:c-define name=\"MY_MTU\"/>\n"
4190 "</leaf>\n"
4191 "</list>\n"
4192 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004193 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
4194 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS);
David Sedlák8985a142019-07-31 16:43:06 +02004195 lys_module_free(mod, NULL);
4196 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004197 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02004198 mod = NULL;
4199 yin_ctx = NULL;
4200
4201 mod = calloc(1, sizeof *mod);
4202 mod->ctx = st->ctx;
David Sedlák6d781b62019-08-02 15:22:52 +02004203 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
Radek Krejci96e48da2020-09-04 13:18:06 +02004204 "<yang-version value=\"1\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004205 "<namespace uri=\"urn:example:foo\"/>\n"
4206 "<prefix value=\"foo\"/>\n"
4207 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004208 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
4209 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS);
David Sedlák6d781b62019-08-02 15:22:52 +02004210 lys_module_free(mod, NULL);
4211 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004212 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02004213 mod = NULL;
4214 yin_ctx = NULL;
4215
4216
4217 mod = calloc(1, sizeof *mod);
4218 mod->ctx = st->ctx;
David Sedlák8985a142019-07-31 16:43:06 +02004219 data = "<submodule name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
4220 "</submodule>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004221 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
4222 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_EINVAL);
David Sedlák8985a142019-07-31 16:43:06 +02004223 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
4224 lys_module_free(mod, NULL);
4225 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004226 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02004227
David Sedlák6d781b62019-08-02 15:22:52 +02004228 mod = calloc(1, sizeof *mod);
4229 mod->ctx = st->ctx;
4230 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
Radek Krejci96e48da2020-09-04 13:18:06 +02004231 "<yang-version value=\"1\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004232 "<namespace uri=\"urn:example:foo\"/>\n"
4233 "<prefix value=\"foo\"/>\n"
4234 "</module>"
4235 "<module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004236 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
4237 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_EVALID);
David Sedlák6d781b62019-08-02 15:22:52 +02004238 logbuf_assert("Trailing garbage \"<module>\" after module, expected end-of-input. Line number 5.");
4239 lys_module_free(mod, NULL);
4240 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004241 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02004242 mod = NULL;
4243 yin_ctx = NULL;
4244
David Sedlák8985a142019-07-31 16:43:06 +02004245 st->finished_correctly = true;
4246}
4247
4248static void
4249test_yin_parse_submodule(void **state)
4250{
Radek Krejci3a4889a2020-05-19 17:01:58 +02004251 struct test_parser_yin_state *st = *state;
David Sedlák8985a142019-07-31 16:43:06 +02004252 const char *data;
Michal Vaskob36053d2020-03-26 15:49:30 +01004253 struct lys_yin_parser_ctx *yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02004254 struct lysp_submodule *submod = NULL;
Michal Vaskoc3781c32020-10-06 14:04:08 +02004255 struct lys_module mod = {0};
4256 struct lys_parser_ctx main_ctx = {.main_mod = &mod};
Michal Vasko63f3d842020-07-08 10:10:14 +02004257 struct ly_in *in;
David Sedlák8985a142019-07-31 16:43:06 +02004258
Michal Vaskoc3781c32020-10-06 14:04:08 +02004259 lydict_insert(st->ctx, "a", 0, &mod.name);
4260
David Sedlák8985a142019-07-31 16:43:06 +02004261 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
4262 "<submodule name=\"asub\""
4263 "xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
4264 "xmlns:a=\"urn:a\">"
Radek Krejci96e48da2020-09-04 13:18:06 +02004265 "<yang-version value=\"1\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02004266 "<belongs-to module=\"a\">"
4267 "<prefix value=\"a_pref\"/>"
4268 "</belongs-to>"
4269 "<include module=\"atop\"/>"
4270 "<feature name=\"fox\"/>"
4271 "<notification name=\"bar-notif\">"
4272 "<if-feature name=\"bar\"/>"
4273 "</notification>"
4274 "<notification name=\"fox-notif\">"
4275 "<if-feature name=\"fox\"/>"
4276 "</notification>"
4277 "<augment target-node=\"/a_pref:top\">"
4278 "<if-feature name=\"bar\"/>"
4279 "<container name=\"bar-sub\"/>"
4280 "</augment>"
4281 "<augment target-node=\"/top\">"
4282 "<container name=\"bar-sub2\"/>"
4283 "</augment>"
4284 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004285 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
4286 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, &main_ctx, in, &submod), LY_SUCCESS);
David Sedlák8985a142019-07-31 16:43:06 +02004287 lysp_submodule_free(st->ctx, submod);
4288 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004289 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02004290 yin_ctx = NULL;
4291 submod = NULL;
4292
4293 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
David Sedlák6d781b62019-08-02 15:22:52 +02004294 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
Radek Krejci96e48da2020-09-04 13:18:06 +02004295 "<yang-version value=\"1\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004296 "<belongs-to module=\"a\">"
4297 "<prefix value=\"a_pref\"/>"
4298 "</belongs-to>"
4299 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004300 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
4301 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, &main_ctx, in, &submod), LY_SUCCESS);
David Sedlák6d781b62019-08-02 15:22:52 +02004302 lysp_submodule_free(st->ctx, submod);
4303 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004304 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02004305 yin_ctx = NULL;
4306 submod = NULL;
4307
4308 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
David Sedlák8985a142019-07-31 16:43:06 +02004309 "<module name=\"inval\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
4310 "</module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004311 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
4312 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, &main_ctx, in, &submod), LY_EINVAL);
David Sedlák8985a142019-07-31 16:43:06 +02004313 logbuf_assert("Input data contains module in situation when a submodule is expected.");
4314 lysp_submodule_free(st->ctx, submod);
4315 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004316 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02004317 yin_ctx = NULL;
4318 submod = NULL;
4319
David Sedlák6d781b62019-08-02 15:22:52 +02004320 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
4321 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
Radek Krejci96e48da2020-09-04 13:18:06 +02004322 "<yang-version value=\"1\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004323 "<belongs-to module=\"a\">"
4324 "<prefix value=\"a_pref\"/>"
4325 "</belongs-to>"
4326 "</submodule>"
4327 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
Radek Krejci96e48da2020-09-04 13:18:06 +02004328 "<yang-version value=\"1\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004329 "<belongs-to module=\"a\">"
4330 "<prefix value=\"a_pref\"/>"
4331 "</belongs-to>"
4332 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004333 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
4334 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, &main_ctx, in, &submod), LY_EVALID);
David Sedlák6d781b62019-08-02 15:22:52 +02004335 logbuf_assert("Trailing garbage \"<submodule name...\" after submodule, expected end-of-input. Line number 2.");
4336 lysp_submodule_free(st->ctx, submod);
4337 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004338 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02004339 yin_ctx = NULL;
4340 submod = NULL;
4341
Michal Vaskoc3781c32020-10-06 14:04:08 +02004342 lydict_remove(st->ctx, mod.name);
David Sedlák8985a142019-07-31 16:43:06 +02004343 st->finished_correctly = true;
4344}
4345
David Sedlák3b4db242018-10-19 16:11:01 +02004346int
4347main(void)
4348{
4349
4350 const struct CMUnitTest tests[] = {
David Sedlák8f7a1172019-06-20 14:42:18 +02004351 cmocka_unit_test_setup_teardown(test_yin_match_keyword, setup_f, teardown_f),
David Sedlákb1a78352019-06-28 16:16:29 +02004352 cmocka_unit_test_setup_teardown(test_yin_parse_element_generic, setup_f, teardown_f),
4353 cmocka_unit_test_setup_teardown(test_yin_parse_extension_instance, setup_f, teardown_f),
David Sedlák071f7662019-09-12 02:02:51 +02004354 cmocka_unit_test_setup_teardown(test_yin_parse_content, setup_f, teardown_f),
David Sedlák4a650532019-07-10 11:55:18 +02004355 cmocka_unit_test_setup_teardown(test_validate_value, setup_f, teardown_f),
David Sedlák32488102019-07-15 17:44:10 +02004356
David Sedlák8e7bda82019-07-16 17:57:50 +02004357 cmocka_unit_test(test_yin_match_argument_name),
David Sedlák43801c92019-08-05 15:58:54 +02004358 cmocka_unit_test_setup_teardown(test_enum_elem, setup_element_test, teardown_element_test),
4359 cmocka_unit_test_setup_teardown(test_bit_elem, setup_element_test, teardown_element_test),
David Sedlák32488102019-07-15 17:44:10 +02004360 cmocka_unit_test_setup_teardown(test_meta_elem, setup_element_test, teardown_element_test),
4361 cmocka_unit_test_setup_teardown(test_import_elem, setup_element_test, teardown_element_test),
4362 cmocka_unit_test_setup_teardown(test_status_elem, setup_element_test, teardown_element_test),
4363 cmocka_unit_test_setup_teardown(test_ext_elem, setup_element_test, teardown_element_test),
4364 cmocka_unit_test_setup_teardown(test_yin_element_elem, setup_element_test, teardown_element_test),
4365 cmocka_unit_test_setup_teardown(test_yangversion_elem, setup_element_test, teardown_element_test),
4366 cmocka_unit_test_setup_teardown(test_mandatory_elem, setup_element_test, teardown_element_test),
David Sedlák8e7bda82019-07-16 17:57:50 +02004367 cmocka_unit_test_setup_teardown(test_argument_elem, setup_element_test, teardown_element_test),
4368 cmocka_unit_test_setup_teardown(test_base_elem, setup_element_test, teardown_element_test),
4369 cmocka_unit_test_setup_teardown(test_belongsto_elem, setup_element_test, teardown_element_test),
4370 cmocka_unit_test_setup_teardown(test_config_elem, setup_element_test, teardown_element_test),
4371 cmocka_unit_test_setup_teardown(test_default_elem, setup_element_test, teardown_element_test),
4372 cmocka_unit_test_setup_teardown(test_err_app_tag_elem, setup_element_test, teardown_element_test),
4373 cmocka_unit_test_setup_teardown(test_err_msg_elem, setup_element_test, teardown_element_test),
4374 cmocka_unit_test_setup_teardown(test_fracdigits_elem, setup_element_test, teardown_element_test),
4375 cmocka_unit_test_setup_teardown(test_iffeature_elem, setup_element_test, teardown_element_test),
4376 cmocka_unit_test_setup_teardown(test_length_elem, setup_element_test, teardown_element_test),
4377 cmocka_unit_test_setup_teardown(test_modifier_elem, setup_element_test, teardown_element_test),
4378 cmocka_unit_test_setup_teardown(test_namespace_elem, setup_element_test, teardown_element_test),
David Sedlák8e7bda82019-07-16 17:57:50 +02004379 cmocka_unit_test_setup_teardown(test_pattern_elem, setup_element_test, teardown_element_test),
4380 cmocka_unit_test_setup_teardown(test_value_position_elem, setup_element_test, teardown_element_test),
David Sedlák69f01612019-07-17 11:41:08 +02004381 cmocka_unit_test_setup_teardown(test_prefix_elem, setup_element_test, teardown_element_test),
4382 cmocka_unit_test_setup_teardown(test_range_elem, setup_element_test, teardown_element_test),
4383 cmocka_unit_test_setup_teardown(test_reqinstance_elem, setup_element_test, teardown_element_test),
4384 cmocka_unit_test_setup_teardown(test_revision_date_elem, setup_element_test, teardown_element_test),
4385 cmocka_unit_test_setup_teardown(test_unique_elem, setup_element_test, teardown_element_test),
4386 cmocka_unit_test_setup_teardown(test_units_elem, setup_element_test, teardown_element_test),
4387 cmocka_unit_test_setup_teardown(test_when_elem, setup_element_test, teardown_element_test),
4388 cmocka_unit_test_setup_teardown(test_yin_text_value_elem, setup_element_test, teardown_element_test),
David Sedlák374d2b32019-07-17 15:06:55 +02004389 cmocka_unit_test_setup_teardown(test_type_elem, setup_element_test, teardown_element_test),
David Sedlák1af868e2019-07-17 17:03:14 +02004390 cmocka_unit_test_setup_teardown(test_max_elems_elem, setup_element_test, teardown_element_test),
David Sedlák09e18c92019-07-18 11:17:11 +02004391 cmocka_unit_test_setup_teardown(test_min_elems_elem, setup_element_test, teardown_element_test),
David Sedláka2dad212019-07-18 12:45:19 +02004392 cmocka_unit_test_setup_teardown(test_ordby_elem, setup_element_test, teardown_element_test),
David Sedlák8a83bbb2019-07-18 14:46:00 +02004393 cmocka_unit_test_setup_teardown(test_any_elem, setup_element_test, teardown_element_test),
David Sedlák203ca3a2019-07-18 15:26:25 +02004394 cmocka_unit_test_setup_teardown(test_leaf_elem, setup_element_test, teardown_element_test),
David Sedlákc3da3ef2019-07-19 12:56:08 +02004395 cmocka_unit_test_setup_teardown(test_leaf_list_elem, setup_element_test, teardown_element_test),
David Sedlákcb39f642019-07-19 13:19:55 +02004396 cmocka_unit_test_setup_teardown(test_presence_elem, setup_element_test, teardown_element_test),
David Sedlák12470a82019-07-19 13:44:36 +02004397 cmocka_unit_test_setup_teardown(test_key_elem, setup_element_test, teardown_element_test),
David Sedlák04e17b22019-07-19 15:29:48 +02004398 cmocka_unit_test_setup_teardown(test_typedef_elem, setup_element_test, teardown_element_test),
David Sedlákd2d676a2019-07-22 11:28:19 +02004399 cmocka_unit_test_setup_teardown(test_refine_elem, setup_element_test, teardown_element_test),
David Sedlák0d6de5a2019-07-22 13:25:44 +02004400 cmocka_unit_test_setup_teardown(test_uses_elem, setup_element_test, teardown_element_test),
David Sedlákaa854b02019-07-22 14:17:10 +02004401 cmocka_unit_test_setup_teardown(test_revision_elem, setup_element_test, teardown_element_test),
David Sedlák0c2bab92019-07-22 15:33:19 +02004402 cmocka_unit_test_setup_teardown(test_include_elem, setup_element_test, teardown_element_test),
David Sedlákaf536aa2019-07-23 13:42:23 +02004403 cmocka_unit_test_setup_teardown(test_list_elem, setup_element_test, teardown_element_test),
David Sedlák031b9e72019-07-23 15:19:37 +02004404 cmocka_unit_test_setup_teardown(test_notification_elem, setup_element_test, teardown_element_test),
David Sedláke3ce9ef2019-07-23 16:34:30 +02004405 cmocka_unit_test_setup_teardown(test_grouping_elem, setup_element_test, teardown_element_test),
David Sedlákf111bcb2019-07-23 17:15:51 +02004406 cmocka_unit_test_setup_teardown(test_container_elem, setup_element_test, teardown_element_test),
David Sedlák5379d392019-07-24 10:42:03 +02004407 cmocka_unit_test_setup_teardown(test_case_elem, setup_element_test, teardown_element_test),
David Sedlákb7abcfa2019-07-24 12:33:35 +02004408 cmocka_unit_test_setup_teardown(test_choice_elem, setup_element_test, teardown_element_test),
David Sedlák05404f62019-07-24 14:11:53 +02004409 cmocka_unit_test_setup_teardown(test_inout_elem, setup_element_test, teardown_element_test),
David Sedlák85d0eca2019-07-24 15:15:21 +02004410 cmocka_unit_test_setup_teardown(test_action_elem, setup_element_test, teardown_element_test),
David Sedlák992fb7c2019-07-24 16:51:01 +02004411 cmocka_unit_test_setup_teardown(test_augment_elem, setup_element_test, teardown_element_test),
David Sedlák4ffcec82019-07-25 15:10:21 +02004412 cmocka_unit_test_setup_teardown(test_deviate_elem, setup_element_test, teardown_element_test),
David Sedlák8b754462019-07-25 16:22:13 +02004413 cmocka_unit_test_setup_teardown(test_deviation_elem, setup_element_test, teardown_element_test),
David Sedlák4f03b932019-07-26 13:01:47 +02004414 cmocka_unit_test_setup_teardown(test_module_elem, setup_element_test, teardown_element_test),
David Sedlák298ff6d2019-07-26 14:29:03 +02004415 cmocka_unit_test_setup_teardown(test_submodule_elem, setup_element_test, teardown_element_test),
David Sedlák8985a142019-07-31 16:43:06 +02004416
4417 cmocka_unit_test_setup_teardown(test_yin_parse_module, setup_logger, teardown_logger),
4418 cmocka_unit_test_setup_teardown(test_yin_parse_submodule, setup_logger, teardown_logger),
David Sedlák3b4db242018-10-19 16:11:01 +02004419 };
4420
David Sedlák8e7bda82019-07-16 17:57:50 +02004421 return cmocka_run_group_tests(tests, setup_ly_ctx, destroy_ly_ctx);
David Sedlák3b4db242018-10-19 16:11:01 +02004422}