blob: 728fe36e83306a3737d63e8edaa8f6e032d65d5a [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"
Michal Vaskoafac7822020-10-20 14:22:26 +020025#include "in.h"
Radek Krejci70593c12020-06-13 20:48:09 +020026#include "parser_internal.h"
Michal Vasko405cc9e2020-12-01 12:01:27 +010027#include "schema_compile.h"
Radek Krejci70593c12020-06-13 20:48:09 +020028#include "tree_schema.h"
29#include "tree_schema_internal.h"
30#include "xml.h"
31#include "xpath.h"
David Sedlák3b4db242018-10-19 16:11:01 +020032
Michal Vaskoafac7822020-10-20 14:22:26 +020033/* copied from parser_yin.c */
34enum yin_argument {
35 YIN_ARG_UNKNOWN = 0, /**< parsed argument can not be matched with any supported yin argument keyword */
36 YIN_ARG_NAME, /**< argument name */
37 YIN_ARG_TARGET_NODE, /**< argument target-node */
38 YIN_ARG_MODULE, /**< argument module */
39 YIN_ARG_VALUE, /**< argument value */
40 YIN_ARG_TEXT, /**< argument text */
41 YIN_ARG_CONDITION, /**< argument condition */
42 YIN_ARG_URI, /**< argument uri */
43 YIN_ARG_DATE, /**< argument data */
44 YIN_ARG_TAG, /**< argument tag */
45 YIN_ARG_NONE /**< empty (special value) */
46};
47
48struct yin_subelement {
49 enum ly_stmt type; /**< type of keyword */
50 void *dest; /**< meta infromation passed to responsible function (mostly information about where parsed subelement should be stored) */
51 uint16_t flags; /**< describes constraints of subelement can be set to YIN_SUBELEM_MANDATORY, YIN_SUBELEM_UNIQUE, YIN_SUBELEM_FIRST, YIN_SUBELEM_VER2, and YIN_SUBELEM_DEFAULT_TEXT */
52};
53
54struct import_meta {
55 const char *prefix; /**< module prefix. */
56 struct lysp_import **imports; /**< imports to add to. */
57};
58
59struct yin_argument_meta {
60 uint16_t *flags; /**< Argument flags */
61 const char **argument; /**< Argument value */
62};
63
64struct tree_node_meta {
65 struct lysp_node *parent; /**< parent node */
66 struct lysp_node **nodes; /**< linked list of siblings */
67};
68
69struct include_meta {
70 const char *name; /**< Module/submodule name. */
71 struct lysp_include **includes; /**< [Sized array](@ref sizedarrays) of parsed includes to add to. */
72};
73
74struct inout_meta {
75 struct lysp_node *parent; /**< Parent node. */
76 struct lysp_action_inout *inout_p; /**< inout_p Input/output pointer to write to. */
77};
78
79struct minmax_dev_meta {
80 uint32_t *lim; /**< min/max value to write to. */
81 uint16_t *flags; /**< min/max flags to write to. */
82 struct lysp_ext_instance **exts; /**< extension instances to add to. */
83};
84
85#define YIN_SUBELEM_MANDATORY 0x01
86#define YIN_SUBELEM_UNIQUE 0x02
87#define YIN_SUBELEM_FIRST 0x04
88#define YIN_SUBELEM_VER2 0x08
89
90#define YIN_SUBELEM_PARSED 0x80
91
David Sedlák555c7202019-07-04 12:14:12 +020092/* prototypes of static functions */
Michal Vaskoafac7822020-10-20 14:22:26 +020093enum yin_argument yin_match_argument_name(const char *name, size_t len);
94LY_ERR yin_parse_content(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size,
95 enum ly_stmt current_element, const char **text_content, struct lysp_ext_instance **exts);
96LY_ERR yin_validate_value(struct lys_yin_parser_ctx *ctx, enum yang_arg val_type);
97enum ly_stmt yin_match_keyword(struct lys_yin_parser_ctx *ctx, const char *name, size_t name_len,
98 const char *prefix, size_t prefix_len, enum ly_stmt parrent);
99LY_ERR yin_parse_extension_instance(struct lys_yin_parser_ctx *ctx, LYEXT_SUBSTMT subelem, LY_ARRAY_COUNT_TYPE subelem_index,
100 struct lysp_ext_instance **exts);
101LY_ERR yin_parse_element_generic(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, struct lysp_stmt **element);
102LY_ERR yin_parse_mod(struct lys_yin_parser_ctx *ctx, struct lysp_module *mod);
103LY_ERR yin_parse_submod(struct lys_yin_parser_ctx *ctx, struct lysp_submodule *submod);
104
David Sedlák555c7202019-07-04 12:14:12 +0200105void lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext);
David Sedlák986cb412019-07-04 13:10:11 +0200106void lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext);
David Sedlák32eee7b2019-07-09 12:38:44 +0200107void lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when);
David Sedlák32488102019-07-15 17:44:10 +0200108void lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type);
David Sedlák8a83bbb2019-07-18 14:46:00 +0200109void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
David Sedlák04e17b22019-07-19 15:29:48 +0200110void lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf);
David Sedlákd2d676a2019-07-22 11:28:19 +0200111void lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref);
David Sedlákaa854b02019-07-22 14:17:10 +0200112void lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev);
David Sedlák0c2bab92019-07-22 15:33:19 +0200113void lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include);
David Sedlák5e13dea2019-07-22 16:06:45 +0200114void lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat);
David Sedlák28794f22019-07-22 16:45:00 +0200115void lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident);
David Sedlák031b9e72019-07-23 15:19:37 +0200116void lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif);
David Sedláke3ce9ef2019-07-23 16:34:30 +0200117void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp);
David Sedlák05404f62019-07-24 14:11:53 +0200118void lysp_action_inout_free(struct ly_ctx *ctx, struct lysp_action_inout *inout);
David Sedlák85d0eca2019-07-24 15:15:21 +0200119void lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action);
David Sedlák992fb7c2019-07-24 16:51:01 +0200120void lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment);
David Sedlák4ffcec82019-07-25 15:10:21 +0200121void lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d);
David Sedlák8b754462019-07-25 16:22:13 +0200122void lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev);
David Sedlák298ff6d2019-07-26 14:29:03 +0200123void lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import);
David Sedlák555c7202019-07-04 12:14:12 +0200124
David Sedláke6cd89e2019-08-07 12:46:02 +0200125/* wrapping element used for mocking has nothing to do with real module structure */
126#define ELEMENT_WRAPPER_START "<status xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
127#define ELEMENT_WRAPPER_END "</status>"
128
Radek Krejci3a4889a2020-05-19 17:01:58 +0200129struct test_parser_yin_state {
David Sedlák3b4db242018-10-19 16:11:01 +0200130 struct ly_ctx *ctx;
Michal Vaskob36053d2020-03-26 15:49:30 +0100131 struct lys_yin_parser_ctx *yin_ctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200132 struct ly_in *in;
David Sedlák79e50cb2019-06-05 16:33:09 +0200133 bool finished_correctly;
David Sedlák68a1af12019-03-08 13:46:54 +0100134};
David Sedlák872c7b42018-10-26 13:15:20 +0200135
David Sedlák79e50cb2019-06-05 16:33:09 +0200136#define BUFSIZE 1024
137char logbuf[BUFSIZE] = {0};
138int store = -1; /* negative for infinite logging, positive for limited logging */
139
140/* set to 0 to printing error messages to stderr instead of checking them in code */
David Sedláke0ef1c62019-09-13 10:05:55 +0200141#define ENABLE_LOGGER_CHECKING 1
David Sedlák79e50cb2019-06-05 16:33:09 +0200142
143#if ENABLE_LOGGER_CHECKING
144static void
145logger(LY_LOG_LEVEL level, const char *msg, const char *path)
146{
147 (void) level; /* unused */
148 if (store) {
149 if (path && path[0]) {
150 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
151 } else {
152 strncpy(logbuf, msg, BUFSIZE - 1);
153 }
154 if (store > 0) {
155 --store;
156 }
157 }
158}
159#endif
160
161#if ENABLE_LOGGER_CHECKING
162# define logbuf_assert(str) assert_string_equal(logbuf, str)
163#else
164# define logbuf_assert(str)
165#endif
166
167#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
168 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
169 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
170 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
171 CLEANUP
172
David Sedlák8e7bda82019-07-16 17:57:50 +0200173int
174setup_ly_ctx(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +0100175{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200176 struct test_parser_yin_state *st = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +0200177
David Sedlák68a1af12019-03-08 13:46:54 +0100178 /* allocate state variable */
179 (*state) = st = calloc(1, sizeof(*st));
180 if (!st) {
181 fprintf(stderr, "Memmory allocation failed");
182 return EXIT_FAILURE;
183 }
David Sedlák872c7b42018-10-26 13:15:20 +0200184
David Sedlák68a1af12019-03-08 13:46:54 +0100185 /* create new libyang context */
186 ly_ctx_new(NULL, 0, &st->ctx);
David Sedlák872c7b42018-10-26 13:15:20 +0200187
David Sedlák8e7bda82019-07-16 17:57:50 +0200188 return EXIT_SUCCESS;
189}
190
191int
192destroy_ly_ctx(void **state)
193{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200194 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +0200195 ly_ctx_destroy(st->ctx, NULL);
196 free(st);
197
198 return EXIT_SUCCESS;
199}
200
201static int
202setup_f(void **state)
203{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200204 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +0200205
206#if ENABLE_LOGGER_CHECKING
207 /* setup logger */
208 ly_set_log_clb(logger, 1);
209#endif
210
David Sedlák619db942019-07-03 14:47:30 +0200211 /* allocate parser context */
David Sedlákda8ffa32019-07-08 14:17:10 +0200212 st->yin_ctx = calloc(1, sizeof(*st->yin_ctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100213 st->yin_ctx->format = LYS_IN_YIN;
David Sedlák8f5bce02019-06-03 16:41:08 +0200214
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200215 /* allocate new parsed module */
216 st->yin_ctx->parsed_mod = calloc(1, sizeof *st->yin_ctx->parsed_mod);
217
218 /* allocate new module */
219 st->yin_ctx->parsed_mod->mod = calloc(1, sizeof *st->yin_ctx->parsed_mod->mod);
220 st->yin_ctx->parsed_mod->mod->ctx = st->ctx;
221 st->yin_ctx->parsed_mod->mod->parsed = st->yin_ctx->parsed_mod;
222
Michal Vasko63f3d842020-07-08 10:10:14 +0200223 st->in = NULL;
224
David Sedlák68a1af12019-03-08 13:46:54 +0100225 return EXIT_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +0200226}
227
228static int
David Sedlák68a1af12019-03-08 13:46:54 +0100229teardown_f(void **state)
230{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200231 struct test_parser_yin_state *st = *(struct test_parser_yin_state **)state;
David Sedlák68a1af12019-03-08 13:46:54 +0100232
David Sedlák79e50cb2019-06-05 16:33:09 +0200233#if ENABLE_LOGGER_CHECKING
234 /* teardown logger */
235 if (!st->finished_correctly && logbuf[0] != '\0') {
236 fprintf(stderr, "%s\n", logbuf);
237 }
238#endif
239
Michal Vaskob36053d2020-03-26 15:49:30 +0100240 lyxml_ctx_free(st->yin_ctx->xmlctx);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200241 lys_module_free(st->yin_ctx->parsed_mod->mod, NULL);
David Sedlákda8ffa32019-07-08 14:17:10 +0200242 free(st->yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200243 ly_in_free(st->in, 0);
David Sedlák68a1af12019-03-08 13:46:54 +0100244
245 return EXIT_SUCCESS;
246}
247
Radek Krejci3a4889a2020-05-19 17:01:58 +0200248static struct test_parser_yin_state*
David Sedlák392af4f2019-06-04 16:02:42 +0200249reset_state(void **state)
250{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200251 ((struct test_parser_yin_state *)*state)->finished_correctly = true;
David Sedlák555c7202019-07-04 12:14:12 +0200252 logbuf[0] = '\0';
David Sedlák392af4f2019-06-04 16:02:42 +0200253 teardown_f(state);
254 setup_f(state);
255
256 return *state;
257}
258
David Sedlák79e50cb2019-06-05 16:33:09 +0200259void
260logbuf_clean(void)
261{
262 logbuf[0] = '\0';
263}
264
David Sedlák8985a142019-07-31 16:43:06 +0200265static int
David Sedlák8985a142019-07-31 16:43:06 +0200266setup_element_test(void **state)
267{
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200268 struct test_parser_yin_state *st;
David Sedlák8985a142019-07-31 16:43:06 +0200269
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200270 setup_f(state);
271 st = *state;
David Sedlák8985a142019-07-31 16:43:06 +0200272
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200273 lydict_insert(st->ctx, "module-name", 0, &st->yin_ctx->parsed_mod->mod->name);
David Sedlák8985a142019-07-31 16:43:06 +0200274
275 return EXIT_SUCCESS;
276}
277
David Sedlák68a1af12019-03-08 13:46:54 +0100278static void
David Sedlák1bccdfa2019-06-17 15:55:27 +0200279test_yin_match_keyword(void **state)
David Sedlák3b4db242018-10-19 16:11:01 +0200280{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200281 struct test_parser_yin_state *st = *state;
David Sedlák3b4db242018-10-19 16:11:01 +0200282
Michal Vaskob36053d2020-03-26 15:49:30 +0100283 const char *prefix;
284 size_t prefix_len;
David Sedlák8f7a1172019-06-20 14:42:18 +0200285 /* create mock yin namespace in xml context */
286 const char *data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" />";
Michal Vasko63f3d842020-07-08 10:10:14 +0200287 ly_in_new_memory(data, &st->in);
288 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100289 prefix = st->yin_ctx->xmlctx->prefix;
290 prefix_len = st->yin_ctx->xmlctx->prefix_len;
David Sedlák8f7a1172019-06-20 14:42:18 +0200291
Radek Krejcid6b76452019-09-03 17:03:03 +0200292 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydatax", strlen("anydatax"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
293 assert_int_equal(yin_match_keyword(st->yin_ctx, "asdasd", strlen("asdasd"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
294 assert_int_equal(yin_match_keyword(st->yin_ctx, "", 0, prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
295 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydata", strlen("anydata"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ANYDATA);
296 assert_int_equal(yin_match_keyword(st->yin_ctx, "anyxml", strlen("anyxml"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ANYXML);
297 assert_int_equal(yin_match_keyword(st->yin_ctx, "argument", strlen("argument"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ARGUMENT);
298 assert_int_equal(yin_match_keyword(st->yin_ctx, "augment", strlen("augment"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_AUGMENT);
299 assert_int_equal(yin_match_keyword(st->yin_ctx, "base", strlen("base"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BASE);
300 assert_int_equal(yin_match_keyword(st->yin_ctx, "belongs-to", strlen("belongs-to"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BELONGS_TO);
301 assert_int_equal(yin_match_keyword(st->yin_ctx, "bit", strlen("bit"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BIT);
302 assert_int_equal(yin_match_keyword(st->yin_ctx, "case", strlen("case"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CASE);
303 assert_int_equal(yin_match_keyword(st->yin_ctx, "choice", strlen("choice"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CHOICE);
304 assert_int_equal(yin_match_keyword(st->yin_ctx, "config", strlen("config"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONFIG);
305 assert_int_equal(yin_match_keyword(st->yin_ctx, "contact", strlen("contact"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONTACT);
306 assert_int_equal(yin_match_keyword(st->yin_ctx, "container", strlen("container"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONTAINER);
307 assert_int_equal(yin_match_keyword(st->yin_ctx, "default", strlen("default"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEFAULT);
308 assert_int_equal(yin_match_keyword(st->yin_ctx, "description", strlen("description"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DESCRIPTION);
309 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviate", strlen("deviate"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEVIATE);
310 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviation", strlen("deviation"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEVIATION);
311 assert_int_equal(yin_match_keyword(st->yin_ctx, "enum", strlen("enum"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ENUM);
312 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);
313 assert_int_equal(yin_match_keyword(st->yin_ctx, "error-message", strlen("error-message"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ERROR_MESSAGE);
314 assert_int_equal(yin_match_keyword(st->yin_ctx, "extension", strlen("extension"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_EXTENSION);
315 assert_int_equal(yin_match_keyword(st->yin_ctx, "feature", strlen("feature"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_FEATURE);
316 assert_int_equal(yin_match_keyword(st->yin_ctx, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_FRACTION_DIGITS);
317 assert_int_equal(yin_match_keyword(st->yin_ctx, "grouping", strlen("grouping"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_GROUPING);
318 assert_int_equal(yin_match_keyword(st->yin_ctx, "identity", strlen("identity"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IDENTITY);
319 assert_int_equal(yin_match_keyword(st->yin_ctx, "if-feature", strlen("if-feature"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IF_FEATURE);
320 assert_int_equal(yin_match_keyword(st->yin_ctx, "import", strlen("import"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IMPORT);
321 assert_int_equal(yin_match_keyword(st->yin_ctx, "include", strlen("include"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_INCLUDE);
322 assert_int_equal(yin_match_keyword(st->yin_ctx, "input", strlen("input"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_INPUT);
323 assert_int_equal(yin_match_keyword(st->yin_ctx, "key", strlen("key"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_KEY);
324 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf", strlen("leaf"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LEAF);
325 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf-list", strlen("leaf-list"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LEAF_LIST);
326 assert_int_equal(yin_match_keyword(st->yin_ctx, "length", strlen("length"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LENGTH);
327 assert_int_equal(yin_match_keyword(st->yin_ctx, "list", strlen("list"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LIST);
328 assert_int_equal(yin_match_keyword(st->yin_ctx, "mandatory", strlen("mandatory"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MANDATORY);
329 assert_int_equal(yin_match_keyword(st->yin_ctx, "max-elements", strlen("max-elements"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MAX_ELEMENTS);
330 assert_int_equal(yin_match_keyword(st->yin_ctx, "min-elements", strlen("min-elements"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MIN_ELEMENTS);
331 assert_int_equal(yin_match_keyword(st->yin_ctx, "modifier", strlen("modifier"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MODIFIER);
332 assert_int_equal(yin_match_keyword(st->yin_ctx, "module", strlen("module"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MODULE);
333 assert_int_equal(yin_match_keyword(st->yin_ctx, "must", strlen("must"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MUST);
334 assert_int_equal(yin_match_keyword(st->yin_ctx, "namespace", strlen("namespace"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NAMESPACE);
335 assert_int_equal(yin_match_keyword(st->yin_ctx, "notification", strlen("notification"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NOTIFICATION);
336 assert_int_equal(yin_match_keyword(st->yin_ctx, "ordered-by", strlen("ordered-by"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ORDERED_BY);
337 assert_int_equal(yin_match_keyword(st->yin_ctx, "organization", strlen("organization"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ORGANIZATION);
338 assert_int_equal(yin_match_keyword(st->yin_ctx, "output", strlen("output"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_OUTPUT);
339 assert_int_equal(yin_match_keyword(st->yin_ctx, "path", strlen("path"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PATH);
340 assert_int_equal(yin_match_keyword(st->yin_ctx, "pattern", strlen("pattern"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PATTERN);
341 assert_int_equal(yin_match_keyword(st->yin_ctx, "position", strlen("position"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_POSITION);
342 assert_int_equal(yin_match_keyword(st->yin_ctx, "prefix", strlen("prefix"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PREFIX);
343 assert_int_equal(yin_match_keyword(st->yin_ctx, "presence", strlen("presence"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PRESENCE);
344 assert_int_equal(yin_match_keyword(st->yin_ctx, "range", strlen("range"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_RANGE);
345 assert_int_equal(yin_match_keyword(st->yin_ctx, "reference", strlen("reference"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REFERENCE);
346 assert_int_equal(yin_match_keyword(st->yin_ctx, "refine", strlen("refine"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REFINE);
347 assert_int_equal(yin_match_keyword(st->yin_ctx, "require-instance", strlen("require-instance"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REQUIRE_INSTANCE);
348 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision", strlen("revision"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REVISION);
349 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision-date", strlen("revision-date"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REVISION_DATE);
350 assert_int_equal(yin_match_keyword(st->yin_ctx, "rpc", strlen("rpc"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_RPC);
351 assert_int_equal(yin_match_keyword(st->yin_ctx, "status", strlen("status"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_STATUS);
352 assert_int_equal(yin_match_keyword(st->yin_ctx, "submodule", strlen("submodule"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_SUBMODULE);
353 assert_int_equal(yin_match_keyword(st->yin_ctx, "type", strlen("type"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_TYPE);
354 assert_int_equal(yin_match_keyword(st->yin_ctx, "typedef", strlen("typedef"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_TYPEDEF);
355 assert_int_equal(yin_match_keyword(st->yin_ctx, "unique", strlen("unique"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_UNIQUE);
356 assert_int_equal(yin_match_keyword(st->yin_ctx, "units", strlen("units"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_UNITS);
357 assert_int_equal(yin_match_keyword(st->yin_ctx, "uses", strlen("uses"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_USES);
358 assert_int_equal(yin_match_keyword(st->yin_ctx, "value", strlen("value"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_VALUE);
359 assert_int_equal(yin_match_keyword(st->yin_ctx, "when", strlen("when"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_WHEN);
360 assert_int_equal(yin_match_keyword(st->yin_ctx, "yang-version", strlen("yang-version"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_YANG_VERSION);
361 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 +0200362
363 st->finished_correctly = true;
David Sedlák872c7b42018-10-26 13:15:20 +0200364}
David Sedlák3b4db242018-10-19 16:11:01 +0200365
David Sedlák872c7b42018-10-26 13:15:20 +0200366static void
David Sedlák060b00e2019-06-19 11:12:06 +0200367test_yin_match_argument_name(void **state)
David Sedlák872c7b42018-10-26 13:15:20 +0200368{
David Sedlák68a1af12019-03-08 13:46:54 +0100369 (void)state; /* unused */
David Sedlák872c7b42018-10-26 13:15:20 +0200370
David Sedlák060b00e2019-06-19 11:12:06 +0200371 assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN);
372 assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN);
373 assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN);
374 assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION);
375 assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE);
376 assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE);
377 assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME);
378 assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG);
379 assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
380 assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT);
381 assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI);
382 assert_int_equal(yin_match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200383}
384
David Sedlák68a1af12019-03-08 13:46:54 +0100385static void
David Sedlákb1a78352019-06-28 16:16:29 +0200386test_yin_parse_element_generic(void **state)
387{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200388 struct test_parser_yin_state *st = *state;
David Sedlákb1a78352019-06-28 16:16:29 +0200389 struct lysp_ext_instance exts;
David Sedlákb1a78352019-06-28 16:16:29 +0200390 LY_ERR ret;
391
392 memset(&exts, 0, sizeof(exts));
393
David Sedlákb0ca07d2019-09-11 11:54:05 +0200394 const char *data = "<myext:elem attr=\"value\" xmlns:myext=\"urn:example:extensions\">text_value</myext:elem>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200395 ly_in_new_memory(data, &st->in);
396 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100397
398 ret = yin_parse_element_generic(st->yin_ctx, LY_STMT_EXTENSION_INSTANCE, &exts.child);
David Sedlákb1a78352019-06-28 16:16:29 +0200399 assert_int_equal(ret, LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +0100400 assert_int_equal(st->yin_ctx->xmlctx->status, LYXML_ELEM_CLOSE);
David Sedláke0ef1c62019-09-13 10:05:55 +0200401 assert_string_equal(exts.child->stmt, "urn:example:extensions:elem");
David Sedlákb1a78352019-06-28 16:16:29 +0200402 assert_string_equal(exts.child->arg, "text_value");
David Sedlákb1a78352019-06-28 16:16:29 +0200403 assert_string_equal(exts.child->child->stmt, "attr");
404 assert_string_equal(exts.child->child->arg, "value");
405 assert_true(exts.child->child->flags & LYS_YIN_ATTR);
David Sedlákb1a78352019-06-28 16:16:29 +0200406 lysp_ext_instance_free(st->ctx, &exts);
David Sedlák5392a212019-07-01 09:19:10 +0200407 st = reset_state(state);
408
David Sedlákb0ca07d2019-09-11 11:54:05 +0200409 data = "<myext:elem xmlns:myext=\"urn:example:extensions\"></myext:elem>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200410 ly_in_new_memory(data, &st->in);
411 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100412
413 ret = yin_parse_element_generic(st->yin_ctx, LY_STMT_EXTENSION_INSTANCE, &exts.child);
David Sedlák5392a212019-07-01 09:19:10 +0200414 assert_int_equal(ret, LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +0100415 assert_int_equal(st->yin_ctx->xmlctx->status, LYXML_ELEM_CLOSE);
David Sedláke0ef1c62019-09-13 10:05:55 +0200416 assert_string_equal(exts.child->stmt, "urn:example:extensions:elem");
David Sedlák5392a212019-07-01 09:19:10 +0200417 assert_null(exts.child->child);
418 assert_null(exts.child->arg);
419 lysp_ext_instance_free(st->ctx, &exts);
420
David Sedlákb1a78352019-06-28 16:16:29 +0200421 st->finished_correctly = true;
422}
423
424static void
425test_yin_parse_extension_instance(void **state)
426{
427 LY_ERR ret;
Radek Krejci3a4889a2020-05-19 17:01:58 +0200428 struct test_parser_yin_state *st = *state;
David Sedlákb1a78352019-06-28 16:16:29 +0200429 struct lysp_ext_instance *exts = NULL;
David Sedlákb0ca07d2019-09-11 11:54:05 +0200430 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 +0200431 ly_in_new_memory(data, &st->in);
432 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100433
434 ret = yin_parse_extension_instance(st->yin_ctx, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200435 assert_int_equal(ret, LY_SUCCESS);
David Sedláke0ef1c62019-09-13 10:05:55 +0200436 assert_string_equal(exts->name, "urn:example:extensions:ext");
David Sedlákb1a78352019-06-28 16:16:29 +0200437 assert_int_equal(exts->insubstmt_index, 0);
438 assert_true(exts->insubstmt == LYEXT_SUBSTMT_CONTACT);
439 assert_true(exts->yin & LYS_YIN);
440 assert_string_equal(exts->child->stmt, "value1");
441 assert_string_equal(exts->child->arg, "test");
442 assert_null(exts->child->child);
443 assert_true(exts->child->flags & LYS_YIN_ATTR);
444 assert_string_equal(exts->child->next->stmt, "value");
445 assert_string_equal(exts->child->next->arg, "test2");
446 assert_null(exts->child->next->child);
447 assert_true(exts->child->next->flags & LYS_YIN_ATTR);
448
David Sedláke0ef1c62019-09-13 10:05:55 +0200449 assert_string_equal(exts->child->next->next->stmt, "urn:example:extensions:subelem");
David Sedlákb1a78352019-06-28 16:16:29 +0200450 assert_string_equal(exts->child->next->next->arg, "text");
451 assert_null(exts->child->next->next->child);
452 assert_null(exts->child->next->next->next);
453 assert_false(exts->child->next->next->flags & LYS_YIN_ATTR);
David Sedlákb1a78352019-06-28 16:16:29 +0200454 lysp_ext_instance_free(st->ctx, exts);
455 LY_ARRAY_FREE(exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200456 exts = NULL;
David Sedlákf250ecf2019-07-01 11:02:05 +0200457 st = reset_state(state);
458
David Sedlákb0ca07d2019-09-11 11:54:05 +0200459 data = "<myext:extension-elem xmlns:myext=\"urn:example:extensions\" />";
Michal Vasko63f3d842020-07-08 10:10:14 +0200460 ly_in_new_memory(data, &st->in);
461 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100462
463 ret = yin_parse_extension_instance(st->yin_ctx, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200464 assert_int_equal(ret, LY_SUCCESS);
David Sedláke0ef1c62019-09-13 10:05:55 +0200465 assert_string_equal(exts->name, "urn:example:extensions:extension-elem");
David Sedlákf250ecf2019-07-01 11:02:05 +0200466 assert_null(exts->argument);
467 assert_null(exts->child);
468 assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT);
469 assert_int_equal(exts->insubstmt_index, 0);
470 assert_true(exts->yin & LYS_YIN);
David Sedlákf250ecf2019-07-01 11:02:05 +0200471 lysp_ext_instance_free(st->ctx, exts);
472 LY_ARRAY_FREE(exts);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200473 exts = NULL;
David Sedlákadd0c2e2019-08-16 10:49:12 +0200474 st = reset_state(state);
475
David Sedlákb0ca07d2019-09-11 11:54:05 +0200476 data = "<myext:ext attr1=\"text1\" attr2=\"text2\" xmlns:myext=\"urn:example:extensions\">"
477 "<myext:ext-sub1/>"
478 "<myext:ext-sub2 sattr1=\"stext2\">"
479 "<myext:ext-sub21>"
480 "<myext:ext-sub211 sattr21=\"text21\"/>"
481 "</myext:ext-sub21>"
482 "</myext:ext-sub2>"
483 "<myext:ext-sub3 attr3=\"text3\"></myext:ext-sub3>"
484 "</myext:ext>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200485 ly_in_new_memory(data, &st->in);
486 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100487
488 ret = yin_parse_extension_instance(st->yin_ctx, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200489 assert_int_equal(ret, LY_SUCCESS);
490
David Sedláke0ef1c62019-09-13 10:05:55 +0200491 assert_string_equal(exts->name, "urn:example:extensions:ext");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200492 assert_null(exts->argument);
493 assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT);
494 assert_int_equal(exts->insubstmt_index, 0);
495 assert_true(exts->yin & LYS_YIN);
496 assert_string_equal(exts->child->stmt, "attr1");
497 assert_string_equal(exts->child->arg, "text1");
498 assert_null(exts->child->child);
499 assert_true(exts->child->flags & LYS_YIN_ATTR);
500
501 assert_string_equal(exts->child->next->stmt, "attr2");
502 assert_string_equal(exts->child->next->arg, "text2");
503 assert_null(exts->child->next->child);
504 assert_true(exts->child->next->flags & LYS_YIN_ATTR);
505
David Sedláke0ef1c62019-09-13 10:05:55 +0200506 assert_string_equal(exts->child->next->next->stmt, "urn:example:extensions:ext-sub1");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200507 assert_null(exts->child->next->next->arg);
508 assert_null(exts->child->next->next->child);
509 assert_int_equal(exts->child->next->next->flags, 0);
510
David Sedláke0ef1c62019-09-13 10:05:55 +0200511 assert_string_equal(exts->child->next->next->next->stmt, "urn:example:extensions:ext-sub2");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200512 assert_null(exts->child->next->next->next->arg);
513 assert_int_equal(exts->child->next->next->next->flags, 0);
514 assert_string_equal(exts->child->next->next->next->child->stmt, "sattr1");
515 assert_string_equal(exts->child->next->next->next->child->arg, "stext2");
516 assert_null(exts->child->next->next->next->child->child);
517 assert_true(exts->child->next->next->next->child->flags & LYS_YIN_ATTR);
518
David Sedláke0ef1c62019-09-13 10:05:55 +0200519 assert_string_equal(exts->child->next->next->next->child->next->stmt, "urn:example:extensions:ext-sub21");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200520 assert_null(exts->child->next->next->next->child->next->arg);
521 assert_null(exts->child->next->next->next->child->next->next);
522 assert_int_equal(exts->child->next->next->next->child->next->flags, 0);
523
David Sedláke0ef1c62019-09-13 10:05:55 +0200524 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 +0200525 assert_null(exts->child->next->next->next->child->next->child->arg);
526 assert_int_equal(exts->child->next->next->next->child->next->child->flags, 0);
527 assert_null(exts->child->next->next->next->child->next->child->next);
528
529 assert_string_equal(exts->child->next->next->next->child->next->child->child->stmt, "sattr21");
530 assert_string_equal(exts->child->next->next->next->child->next->child->child->arg, "text21");
531 assert_null(exts->child->next->next->next->child->next->child->child->next);
532 assert_null(exts->child->next->next->next->child->next->child->child->child);
533 assert_true(exts->child->next->next->next->child->next->child->child->flags & LYS_YIN_ATTR);
534
David Sedláke0ef1c62019-09-13 10:05:55 +0200535 assert_string_equal(exts->child->next->next->next->next->stmt, "urn:example:extensions:ext-sub3");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200536 assert_null(exts->child->next->next->next->next->arg);
537 assert_null(exts->child->next->next->next->next->next);
538 assert_int_equal(exts->child->next->next->next->next->flags, 0);
539
540 assert_string_equal(exts->child->next->next->next->next->child->stmt, "attr3");
541 assert_string_equal(exts->child->next->next->next->next->child->arg, "text3");
542 assert_null(exts->child->next->next->next->next->child->next);
543 assert_null(exts->child->next->next->next->next->child->child);
544 assert_true(exts->child->next->next->next->next->child->flags & LYS_YIN_ATTR);
545
David Sedlákadd0c2e2019-08-16 10:49:12 +0200546 lysp_ext_instance_free(st->ctx, exts);
547 LY_ARRAY_FREE(exts);
548 exts = NULL;
David Sedlákaa98bba2019-09-12 11:52:14 +0200549 st = reset_state(state);
550
551 data = "<myext:extension-elem xmlns:myext=\"urn:example:extensions\" xmlns:yin=\"urn:ietf:params:xml:ns:yang:yin:1\">"
552 "<yin:action name=\"act-name\" pre:prefixed=\"ignored\"/>"
553 "<yin:augment target-node=\"target\"/>"
554 "<yin:status value=\"value\"/>"
555 "<yin:include module=\"mod\"/>"
556 "<yin:input />"
557 "<yin:must condition=\"cond\"/>"
558 "<yin:namespace uri=\"uri\"/>"
559 "<yin:revision date=\"data\"/>"
560 "<yin:unique tag=\"tag\"/>"
David Sedlákd2844882019-09-13 16:01:22 +0200561 "<yin:description><yin:text>contact-val</yin:text></yin:description>"
562 "<yin:error-message><yin:value>err-msg</yin:value></yin:error-message>"
David Sedlákaa98bba2019-09-12 11:52:14 +0200563 "</myext:extension-elem>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200564 ly_in_new_memory(data, &st->in);
565 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100566
567 ret = yin_parse_extension_instance(st->yin_ctx, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákaa98bba2019-09-12 11:52:14 +0200568 assert_int_equal(ret, LY_SUCCESS);
569 assert_string_equal(exts->child->arg, "act-name");
570 assert_string_equal(exts->child->next->arg, "target");
571 assert_string_equal(exts->child->next->next->arg, "value");
572 assert_string_equal(exts->child->next->next->next->arg, "mod");
573 assert_null(exts->child->next->next->next->next->arg);
574 assert_string_equal(exts->child->next->next->next->next->next->arg, "cond");
575 assert_string_equal(exts->child->next->next->next->next->next->next->arg, "uri");
576 assert_string_equal(exts->child->next->next->next->next->next->next->next->arg, "data");
577 assert_string_equal(exts->child->next->next->next->next->next->next->next->next->arg, "tag");
578 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 +0200579 lysp_ext_instance_free(st->ctx, exts);
580 LY_ARRAY_FREE(exts);
581 exts = NULL;
David Sedlákaa98bba2019-09-12 11:52:14 +0200582 st = reset_state(state);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200583
David Sedlákb1a78352019-06-28 16:16:29 +0200584 st->finished_correctly = true;
585}
586
David Sedlák555c7202019-07-04 12:14:12 +0200587static void
588test_yin_parse_content(void **state)
589{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200590 struct test_parser_yin_state *st = *state;
David Sedlák555c7202019-07-04 12:14:12 +0200591 LY_ERR ret = LY_SUCCESS;
David Sedlák555c7202019-07-04 12:14:12 +0200592 const char *data = "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlákb0ca07d2019-09-11 11:54:05 +0200593 "<myext:custom xmlns:myext=\"urn:example:extensions\">"
David Sedlák555c7202019-07-04 12:14:12 +0200594 "totally amazing extension"
David Sedlákb0ca07d2019-09-11 11:54:05 +0200595 "</myext:custom>"
David Sedlákaa98bba2019-09-12 11:52:14 +0200596 "<extension name=\"ext\">"
David Sedlák986cb412019-07-04 13:10:11 +0200597 "<argument name=\"argname\"></argument>"
598 "<description><text>desc</text></description>"
599 "<reference><text>ref</text></reference>"
600 "<status value=\"deprecated\"></status>"
David Sedlákaa98bba2019-09-12 11:52:14 +0200601 "</extension>"
David Sedlák555c7202019-07-04 12:14:12 +0200602 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedlák32488102019-07-15 17:44:10 +0200603 "<if-feature name=\"foo\"></if-feature>"
David Sedlák32eee7b2019-07-09 12:38:44 +0200604 "<when condition=\"condition...\">"
605 "<reference><text>when_ref</text></reference>"
606 "<description><text>when_desc</text></description>"
607 "</when>"
David Sedláke1a30302019-07-10 13:49:38 +0200608 "<config value=\"true\"/>"
David Sedlákc1771b12019-07-10 15:55:46 +0200609 "<error-message>"
610 "<value>error-msg</value>"
611 "</error-message>"
David Sedlák2ce1be62019-07-10 16:15:09 +0200612 "<error-app-tag value=\"err-app-tag\"/>"
David Sedláka5b1d382019-07-10 16:31:09 +0200613 "<units name=\"radians\"></units>"
David Sedláke7084ce2019-07-10 16:44:15 +0200614 "<default value=\"default-value\"/>"
David Sedlák5545f5d2019-07-11 11:55:16 +0200615 "<position value=\"25\"></position>"
616 "<value value=\"-5\"/>"
David Sedlákcf5569a2019-07-11 13:31:34 +0200617 "<require-instance value=\"true\"></require-instance>"
David Sedlákb7296dd2019-07-11 14:58:38 +0200618 "<range value=\"5..10\" />"
David Sedlák438ae432019-07-11 15:36:54 +0200619 "<length value=\"baf\"/>"
David Sedlákd3983112019-07-12 11:20:56 +0200620 "<pattern value='pattern'>"
621 "<modifier value='invert-match'/>"
622 "</pattern>"
David Sedlákfd5b9c32019-07-12 15:33:13 +0200623 "<enum name=\"yay\">"
624 "</enum>"
David Sedlák555c7202019-07-04 12:14:12 +0200625 "</prefix>";
626 struct lysp_ext_instance *exts = NULL;
David Sedlák5f8191e2019-07-08 16:35:52 +0200627 const char **if_features = NULL;
Michal Vasko7f45cf22020-10-01 12:49:44 +0200628 const char *value, *err_msg, *app_tag, *units;
629 struct lysp_qname def = {0};
David Sedlák986cb412019-07-04 13:10:11 +0200630 struct lysp_ext *ext_def = NULL;
David Sedlák32eee7b2019-07-09 12:38:44 +0200631 struct lysp_when *when_p = NULL;
David Sedlákcf5569a2019-07-11 13:31:34 +0200632 struct lysp_type_enum pos_enum = {}, val_enum = {};
David Sedlákfd5b9c32019-07-12 15:33:13 +0200633 struct lysp_type req_type = {}, range_type = {}, len_type = {}, patter_type = {}, enum_type = {};
Juraj Vijtiukf6a07372020-10-29 21:13:33 +0100634 uint16_t config = 0;
David Sedlák555c7202019-07-04 12:14:12 +0200635
Michal Vasko63f3d842020-07-08 10:10:14 +0200636 ly_in_new_memory(data, &st->in);
637 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100638 lyxml_ctx_next(st->yin_ctx->xmlctx);
639 lyxml_ctx_next(st->yin_ctx->xmlctx);
640 lyxml_ctx_next(st->yin_ctx->xmlctx);
David Sedlák555c7202019-07-04 12:14:12 +0200641
David Sedlákfd5b9c32019-07-12 15:33:13 +0200642 struct yin_subelement subelems[17] = {
Radek Krejcid6b76452019-09-03 17:03:03 +0200643 {LY_STMT_CONFIG, &config, 0},
644 {LY_STMT_DEFAULT, &def, YIN_SUBELEM_UNIQUE},
645 {LY_STMT_ENUM, &enum_type, 0},
646 {LY_STMT_ERROR_APP_TAG, &app_tag, YIN_SUBELEM_UNIQUE},
647 {LY_STMT_ERROR_MESSAGE, &err_msg, 0},
648 {LY_STMT_EXTENSION, &ext_def, 0},
649 {LY_STMT_IF_FEATURE, &if_features, 0},
650 {LY_STMT_LENGTH, &len_type, 0},
651 {LY_STMT_PATTERN, &patter_type, 0},
652 {LY_STMT_POSITION, &pos_enum, 0},
653 {LY_STMT_RANGE, &range_type, 0},
654 {LY_STMT_REQUIRE_INSTANCE, &req_type, 0},
655 {LY_STMT_UNITS, &units, YIN_SUBELEM_UNIQUE},
656 {LY_STMT_VALUE, &val_enum, 0},
657 {LY_STMT_WHEN, &when_p, 0},
658 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
659 {LY_STMT_ARG_TEXT, &value, 0}
David Sedlákd3983112019-07-12 11:20:56 +0200660 };
Michal Vaskob36053d2020-03-26 15:49:30 +0100661 ret = yin_parse_content(st->yin_ctx, subelems, 17, LY_STMT_PREFIX, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200662 assert_int_equal(ret, LY_SUCCESS);
David Sedlák2ce1be62019-07-10 16:15:09 +0200663 /* check parsed values */
Michal Vasko7f45cf22020-10-01 12:49:44 +0200664 assert_string_equal(def.str, "default-value");
David Sedláke0ef1c62019-09-13 10:05:55 +0200665 assert_string_equal(exts->name, "urn:example:extensions:custom");
David Sedlák555c7202019-07-04 12:14:12 +0200666 assert_string_equal(exts->argument, "totally amazing extension");
667 assert_string_equal(value, "wsefsdf");
David Sedláka5b1d382019-07-10 16:31:09 +0200668 assert_string_equal(units, "radians");
David Sedlák32eee7b2019-07-09 12:38:44 +0200669 assert_string_equal(when_p->cond, "condition...");
670 assert_string_equal(when_p->dsc, "when_desc");
671 assert_string_equal(when_p->ref, "when_ref");
David Sedláke1a30302019-07-10 13:49:38 +0200672 assert_int_equal(config, LYS_CONFIG_W);
David Sedlák5545f5d2019-07-11 11:55:16 +0200673 assert_int_equal(pos_enum.value, 25);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200674 assert_true(pos_enum.flags & LYS_SET_VALUE);
David Sedlák5545f5d2019-07-11 11:55:16 +0200675 assert_int_equal(val_enum.value, -5);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200676 assert_true(val_enum.flags & LYS_SET_VALUE);
David Sedlákcf5569a2019-07-11 13:31:34 +0200677 assert_int_equal(req_type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200678 assert_true(req_type.flags &= LYS_SET_REQINST);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200679 assert_string_equal(range_type.range->arg.str, "5..10");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200680 assert_true(range_type.flags & LYS_SET_RANGE);
David Sedlákc1771b12019-07-10 15:55:46 +0200681 assert_string_equal(err_msg, "error-msg");
David Sedlák2ce1be62019-07-10 16:15:09 +0200682 assert_string_equal(app_tag, "err-app-tag");
David Sedlákfd5b9c32019-07-12 15:33:13 +0200683 assert_string_equal(enum_type.enums->name, "yay");
Michal Vasko7f45cf22020-10-01 12:49:44 +0200684 assert_string_equal(len_type.length->arg.str, "baf");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200685 assert_true(len_type.flags & LYS_SET_LENGTH);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200686 assert_string_equal(patter_type.patterns->arg.str, "\x015pattern");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200687 assert_true(patter_type.flags & LYS_SET_PATTERN);
David Sedlák2ce1be62019-07-10 16:15:09 +0200688 /* cleanup */
David Sedlák555c7202019-07-04 12:14:12 +0200689 lysp_ext_instance_free(st->ctx, exts);
David Sedlák32eee7b2019-07-09 12:38:44 +0200690 lysp_when_free(st->ctx, when_p);
David Sedlák986cb412019-07-04 13:10:11 +0200691 lysp_ext_free(st->ctx, ext_def);
David Sedlák5f8191e2019-07-08 16:35:52 +0200692 FREE_STRING(st->ctx, *if_features);
David Sedlákc1771b12019-07-10 15:55:46 +0200693 FREE_STRING(st->ctx, err_msg);
David Sedlák2ce1be62019-07-10 16:15:09 +0200694 FREE_STRING(st->ctx, app_tag);
David Sedláka5b1d382019-07-10 16:31:09 +0200695 FREE_STRING(st->ctx, units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200696 FREE_STRING(st->ctx, patter_type.patterns->arg.str);
697 FREE_STRING(st->ctx, def.str);
698 FREE_STRING(st->ctx, range_type.range->arg.str);
699 FREE_STRING(st->ctx, len_type.length->arg.str);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200700 FREE_STRING(st->ctx, enum_type.enums->name);
David Sedlákb7296dd2019-07-11 14:58:38 +0200701 FREE_STRING(st->ctx, value);
David Sedlák5f8191e2019-07-08 16:35:52 +0200702 LY_ARRAY_FREE(if_features);
David Sedlák555c7202019-07-04 12:14:12 +0200703 LY_ARRAY_FREE(exts);
David Sedlák986cb412019-07-04 13:10:11 +0200704 LY_ARRAY_FREE(ext_def);
David Sedlákd3983112019-07-12 11:20:56 +0200705 LY_ARRAY_FREE(patter_type.patterns);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200706 LY_ARRAY_FREE(enum_type.enums);
David Sedlák32eee7b2019-07-09 12:38:44 +0200707 free(when_p);
David Sedlákb7296dd2019-07-11 14:58:38 +0200708 free(range_type.range);
David Sedlák438ae432019-07-11 15:36:54 +0200709 free(len_type.length);
David Sedlák555c7202019-07-04 12:14:12 +0200710 st = reset_state(state);
711
712 /* test unique subelem */
713 const char *prefix_value;
Radek Krejcid6b76452019-09-03 17:03:03 +0200714 struct yin_subelement subelems2[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE},
715 {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_UNIQUE}};
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;
Michal Vasko63f3d842020-07-08 10:10:14 +0200721 ly_in_new_memory(data, &st->in);
722 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100723 lyxml_ctx_next(st->yin_ctx->xmlctx);
724
725 ret = yin_parse_content(st->yin_ctx, subelems2, 2, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200726 assert_int_equal(ret, LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200727 logbuf_assert("Redefinition of \"text\" sub-element in \"status\" element. Line number 1.");
David Sedlák555c7202019-07-04 12:14:12 +0200728 lydict_remove(st->ctx, prefix_value);
729 lydict_remove(st->ctx, value);
730 st = reset_state(state);
David Sedlák555c7202019-07-04 12:14:12 +0200731
732 /* test first subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200733 data = ELEMENT_WRAPPER_START
David Sedlák555c7202019-07-04 12:14:12 +0200734 "<prefix value=\"inv_mod\" />"
735 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
736 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedláke6cd89e2019-08-07 12:46:02 +0200737 ELEMENT_WRAPPER_END;
Radek Krejcid6b76452019-09-03 17:03:03 +0200738 struct yin_subelement subelems3[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE},
739 {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_FIRST}};
Michal Vasko63f3d842020-07-08 10:10:14 +0200740 ly_in_new_memory(data, &st->in);
741 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100742 lyxml_ctx_next(st->yin_ctx->xmlctx);
743
744 ret = yin_parse_content(st->yin_ctx, subelems3, 2, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200745 assert_int_equal(ret, LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200746 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 +0200747 lydict_remove(st->ctx, prefix_value);
748 st = reset_state(state);
David Sedlák555c7202019-07-04 12:14:12 +0200749
750 /* test mandatory subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200751 data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END;
Radek Krejcid6b76452019-09-03 17:03:03 +0200752 struct yin_subelement subelems4[1] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}};
Michal Vasko63f3d842020-07-08 10:10:14 +0200753 ly_in_new_memory(data, &st->in);
754 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100755 lyxml_ctx_next(st->yin_ctx->xmlctx);
756
757 ret = yin_parse_content(st->yin_ctx, subelems4, 1, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200758 assert_int_equal(ret, LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200759 logbuf_assert("Missing mandatory sub-element \"prefix\" of \"status\" element. Line number 1.");
David Sedlák555c7202019-07-04 12:14:12 +0200760
761 st->finished_correctly = true;
762}
763
David Sedlák92147b02019-07-09 14:01:01 +0200764static void
David Sedlák4a650532019-07-10 11:55:18 +0200765test_validate_value(void **state)
766{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200767 struct test_parser_yin_state *st = *state;
Michal Vaskob36053d2020-03-26 15:49:30 +0100768 const char *data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END;
769
770 /* create some XML context */
Michal Vasko63f3d842020-07-08 10:10:14 +0200771 ly_in_new_memory(data, &st->in);
772 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100773 st->yin_ctx->xmlctx->status = LYXML_ELEM_CONTENT;
774 st->yin_ctx->xmlctx->dynamic = 0;
775
776 st->yin_ctx->xmlctx->value = "#invalid";
777 st->yin_ctx->xmlctx->value_len = 8;
778 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG), LY_EVALID);
Radek Krejci1deb5be2020-08-26 16:43:36 +0200779 logbuf_assert("Invalid identifier character '#' (0x0023). Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100780
781 st->yin_ctx->xmlctx->value = "";
782 st->yin_ctx->xmlctx->value_len = 0;
783 assert_int_equal(yin_validate_value(st->yin_ctx, Y_STR_ARG), LY_SUCCESS);
784
785 st->yin_ctx->xmlctx->value = "pre:b";
786 st->yin_ctx->xmlctx->value_len = 5;
787 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG), LY_EVALID);
788 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG), LY_SUCCESS);
789
790 st->yin_ctx->xmlctx->value = "pre:pre:b";
791 st->yin_ctx->xmlctx->value_len = 9;
792 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG), LY_EVALID);
David Sedlák4a650532019-07-10 11:55:18 +0200793
794 st->finished_correctly = true;
795}
796
David Sedlák32488102019-07-15 17:44:10 +0200797/* helper function to simplify unit test of each element using parse_content function */
798LY_ERR
Radek Krejci3a4889a2020-05-19 17:01:58 +0200799test_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 +0200800{
David Sedlákc5b20842019-08-13 10:18:31 +0200801 const char *name, *prefix;
802 size_t name_len, prefix_len;
David Sedlák32488102019-07-15 17:44:10 +0200803 LY_ERR ret = LY_SUCCESS;
804 struct yin_subelement subelems[71] = {
Radek Krejcid6b76452019-09-03 17:03:03 +0200805 {LY_STMT_ACTION, dest, 0},
806 {LY_STMT_ANYDATA, dest, 0},
807 {LY_STMT_ANYXML, dest, 0},
808 {LY_STMT_ARGUMENT,dest, 0},
809 {LY_STMT_AUGMENT, dest, 0},
810 {LY_STMT_BASE, dest, 0},
811 {LY_STMT_BELONGS_TO, dest, 0},
812 {LY_STMT_BIT, dest, 0},
813 {LY_STMT_CASE, dest, 0},
814 {LY_STMT_CHOICE, dest, 0},
815 {LY_STMT_CONFIG, dest, 0},
816 {LY_STMT_CONTACT, dest, 0},
817 {LY_STMT_CONTAINER, dest, 0},
818 {LY_STMT_DEFAULT, dest, YIN_SUBELEM_UNIQUE},
819 {LY_STMT_DESCRIPTION, dest, 0},
820 {LY_STMT_DEVIATE, dest, 0},
821 {LY_STMT_DEVIATION, dest, 0},
822 {LY_STMT_ENUM, dest, 0},
823 {LY_STMT_ERROR_APP_TAG, dest, YIN_SUBELEM_UNIQUE},
824 {LY_STMT_ERROR_MESSAGE, dest, 0},
825 {LY_STMT_EXTENSION, dest, 0},
826 {LY_STMT_FEATURE, dest, 0},
827 {LY_STMT_FRACTION_DIGITS, dest, 0},
828 {LY_STMT_GROUPING, dest, 0},
829 {LY_STMT_IDENTITY, dest, 0},
830 {LY_STMT_IF_FEATURE, dest, 0},
831 {LY_STMT_IMPORT, dest, 0},
832 {LY_STMT_INCLUDE, dest, 0},
833 {LY_STMT_INPUT, dest, 0},
834 {LY_STMT_KEY, dest, YIN_SUBELEM_UNIQUE},
835 {LY_STMT_LEAF, dest, 0},
836 {LY_STMT_LEAF_LIST, dest, 0},
837 {LY_STMT_LENGTH, dest, 0},
838 {LY_STMT_LIST, dest, 0},
839 {LY_STMT_MANDATORY, dest, 0},
840 {LY_STMT_MAX_ELEMENTS, dest, 0},
841 {LY_STMT_MIN_ELEMENTS, dest, 0},
842 {LY_STMT_MODIFIER, dest, 0},
843 {LY_STMT_MODULE, dest, 0},
844 {LY_STMT_MUST, dest, 0},
845 {LY_STMT_NAMESPACE, dest, YIN_SUBELEM_UNIQUE},
846 {LY_STMT_NOTIFICATION, dest, 0},
847 {LY_STMT_ORDERED_BY, dest, 0},
848 {LY_STMT_ORGANIZATION, dest, 0},
849 {LY_STMT_OUTPUT, dest, 0},
850 {LY_STMT_PATH, dest, 0},
851 {LY_STMT_PATTERN, dest, 0},
852 {LY_STMT_POSITION, dest, 0},
853 {LY_STMT_PREFIX, dest, YIN_SUBELEM_UNIQUE},
854 {LY_STMT_PRESENCE, dest, YIN_SUBELEM_UNIQUE},
855 {LY_STMT_RANGE, dest, 0},
856 {LY_STMT_REFERENCE, dest, 0},
857 {LY_STMT_REFINE, dest, 0},
858 {LY_STMT_REQUIRE_INSTANCE, dest, 0},
859 {LY_STMT_REVISION, dest, 0},
860 {LY_STMT_REVISION_DATE, dest, 0},
861 {LY_STMT_RPC, dest, 0},
862 {LY_STMT_STATUS, dest, 0},
863 {LY_STMT_SUBMODULE, dest, 0},
864 {LY_STMT_TYPE, dest, 0},
865 {LY_STMT_TYPEDEF, dest, 0},
866 {LY_STMT_UNIQUE, dest, 0},
867 {LY_STMT_UNITS, dest, YIN_SUBELEM_UNIQUE},
868 {LY_STMT_USES, dest, 0},
869 {LY_STMT_VALUE, dest, 0},
870 {LY_STMT_WHEN, dest, 0},
871 {LY_STMT_YANG_VERSION, dest, 0},
872 {LY_STMT_YIN_ELEMENT, dest, 0},
873 {LY_STMT_EXTENSION_INSTANCE, dest, 0},
874 {LY_STMT_ARG_TEXT, dest, 0},
875 {LY_STMT_ARG_VALUE, dest, 0}
David Sedlák32488102019-07-15 17:44:10 +0200876 };
Michal Vasko63f3d842020-07-08 10:10:14 +0200877 ly_in_new_memory(data, &st->in);
878 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100879 prefix = st->yin_ctx->xmlctx->prefix;
880 prefix_len = st->yin_ctx->xmlctx->prefix_len;
881 name = st->yin_ctx->xmlctx->name;
882 name_len = st->yin_ctx->xmlctx->name_len;
883 lyxml_ctx_next(st->yin_ctx->xmlctx);
884
885 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);
886
Michal Vasko63f3d842020-07-08 10:10:14 +0200887 /* free parser and input */
Michal Vaskob36053d2020-03-26 15:49:30 +0100888 lyxml_ctx_free(st->yin_ctx->xmlctx);
889 st->yin_ctx->xmlctx = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200890 ly_in_free(st->in, 0);
891 st->in = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200892 return ret;
893}
894
David Sedlákd1144562019-08-06 12:36:14 +0200895#define EXT_SUBELEM "<myext:c-define name=\"MY_MTU\" xmlns:myext=\"urn:example:extensions\"/>"
896
David Sedlák32488102019-07-15 17:44:10 +0200897static void
David Sedlák43801c92019-08-05 15:58:54 +0200898test_enum_elem(void **state)
David Sedlák32488102019-07-15 17:44:10 +0200899{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200900 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +0200901 struct lysp_type type = {};
902 const char *data;
903 data = ELEMENT_WRAPPER_START
904 "<enum name=\"enum-name\">"
905 "<if-feature name=\"feature\" />"
906 "<value value=\"55\" />"
907 "<status value=\"deprecated\" />"
908 "<description><text>desc...</text></description>"
909 "<reference><text>ref...</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +0200910 EXT_SUBELEM
David Sedlák32488102019-07-15 17:44:10 +0200911 "</enum>"
912 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100913 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200914 assert_string_equal(type.enums->name, "enum-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +0200915 assert_string_equal(type.enums->iffeatures[0].str, "feature");
David Sedlák32488102019-07-15 17:44:10 +0200916 assert_int_equal(type.enums->value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200917 assert_true((type.enums->flags & LYS_STATUS_DEPRC) && (type.enums->flags & LYS_SET_VALUE));
David Sedlák32488102019-07-15 17:44:10 +0200918 assert_string_equal(type.enums->dsc, "desc...");
919 assert_string_equal(type.enums->ref, "ref...");
David Sedláke0ef1c62019-09-13 10:05:55 +0200920 assert_string_equal(type.enums->exts->name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +0200921 assert_int_equal(type.enums->exts->insubstmt_index, 0);
922 assert_int_equal(type.enums->exts->insubstmt, LYEXT_SUBSTMT_SELF);
923 lysp_type_free(st->ctx, &type);
924 memset(&type, 0, sizeof type);
925
926 data = ELEMENT_WRAPPER_START
927 "<enum name=\"enum-name\"></enum>"
928 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100929 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200930 assert_string_equal(type.enums->name, "enum-name");
David Sedlák32488102019-07-15 17:44:10 +0200931 lysp_type_free(st->ctx, &type);
932 memset(&type, 0, sizeof type);
933
David Sedlák43801c92019-08-05 15:58:54 +0200934 st->finished_correctly = true;
935}
936
937static void
938test_bit_elem(void **state)
939{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200940 struct test_parser_yin_state *st = *state;
David Sedlák43801c92019-08-05 15:58:54 +0200941 struct lysp_type type = {};
942 const char *data;
943 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200944 "<bit name=\"bit-name\">"
David Sedlák43801c92019-08-05 15:58:54 +0200945 "<if-feature name=\"feature\" />"
946 "<position value=\"55\" />"
947 "<status value=\"deprecated\" />"
948 "<description><text>desc...</text></description>"
949 "<reference><text>ref...</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +0200950 EXT_SUBELEM
David Sedlák43801c92019-08-05 15:58:54 +0200951 "</bit>"
952 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100953 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200954 assert_string_equal(type.bits->name, "bit-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +0200955 assert_string_equal(type.bits->iffeatures[0].str, "feature");
David Sedlák43801c92019-08-05 15:58:54 +0200956 assert_int_equal(type.bits->value, 55);
957 assert_true((type.bits->flags & LYS_STATUS_DEPRC) && (type.bits->flags & LYS_SET_VALUE));
958 assert_string_equal(type.bits->dsc, "desc...");
959 assert_string_equal(type.bits->ref, "ref...");
David Sedláke0ef1c62019-09-13 10:05:55 +0200960 assert_string_equal(type.bits->exts->name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +0200961 assert_int_equal(type.bits->exts->insubstmt_index, 0);
962 assert_int_equal(type.bits->exts->insubstmt, LYEXT_SUBSTMT_SELF);
963 lysp_type_free(st->ctx, &type);
964 memset(&type, 0, sizeof type);
965
966 data = ELEMENT_WRAPPER_START
967 "<bit name=\"bit-name\"> </bit>"
968 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100969 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200970 assert_string_equal(type.bits->name, "bit-name");
David Sedlák43801c92019-08-05 15:58:54 +0200971 lysp_type_free(st->ctx, &type);
972 memset(&type, 0, sizeof type);
973
David Sedlák32488102019-07-15 17:44:10 +0200974 st->finished_correctly = true;
975}
976
977static void
978test_meta_elem(void **state)
979{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200980 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +0200981 char *value = NULL;
982 const char *data;
David Sedlákd1144562019-08-06 12:36:14 +0200983 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200984
985 /* organization element */
986 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200987 "<organization><text>organization...</text>" EXT_SUBELEM EXT_SUBELEM "</organization>"
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_ORGANIZATION);
David Sedláke0ef1c62019-09-13 10:05:55 +0200993 assert_string_equal(exts[1].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +0200994 assert_int_equal(exts[1].insubstmt_index, 0);
995 assert_int_equal(exts[1].insubstmt, LYEXT_SUBSTMT_ORGANIZATION);
David Sedlák32488102019-07-15 17:44:10 +0200996 assert_string_equal(value, "organization...");
997 FREE_STRING(st->ctx, value);
David Sedlákd1144562019-08-06 12:36:14 +0200998 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +0200999 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001000 exts = NULL;
1001
David Sedlák32488102019-07-15 17:44:10 +02001002 /* contact element */
1003 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +02001004 "<contact><text>contact...</text>" EXT_SUBELEM "</contact>"
David Sedlák32488102019-07-15 17:44:10 +02001005 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001006 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_SUCCESS);
David Sedláke0ef1c62019-09-13 10:05:55 +02001007 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001008 assert_int_equal(exts[0].insubstmt_index, 0);
1009 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_CONTACT);
David Sedlák32488102019-07-15 17:44:10 +02001010 assert_string_equal(value, "contact...");
1011 FREE_STRING(st->ctx, value);
David Sedlákd1144562019-08-06 12:36:14 +02001012 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1013 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001014 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001015
David Sedlák32488102019-07-15 17:44:10 +02001016 /* description element */
1017 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +02001018 "<description><text>description...</text>" EXT_SUBELEM "</description>"
David Sedlák32488102019-07-15 17:44:10 +02001019 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001020 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001021 assert_string_equal(value, "description...");
David Sedláke0ef1c62019-09-13 10:05:55 +02001022 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001023 assert_int_equal(exts[0].insubstmt_index, 0);
1024 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_DESCRIPTION);
David Sedlák32488102019-07-15 17:44:10 +02001025 FREE_STRING(st->ctx, value);
1026 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001027 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1028 exts = NULL;
1029
David Sedlák32488102019-07-15 17:44:10 +02001030 /* reference element */
1031 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +02001032 "<reference><text>reference...</text>" EXT_SUBELEM "</reference>"
David Sedlák32488102019-07-15 17:44:10 +02001033 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001034 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001035 assert_string_equal(value, "reference...");
David Sedláke0ef1c62019-09-13 10:05:55 +02001036 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001037 assert_int_equal(exts[0].insubstmt_index, 0);
1038 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_REFERENCE);
David Sedlák32488102019-07-15 17:44:10 +02001039 FREE_STRING(st->ctx, value);
1040 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001041 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1042 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001043
David Sedlákdf2a9732019-08-07 13:23:16 +02001044 /* reference element */
1045 data = ELEMENT_WRAPPER_START
1046 "<reference invalid=\"text\"><text>reference...</text>""</reference>"
1047 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001048 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001049 logbuf_assert("Unexpected attribute \"invalid\" of \"reference\" element. Line number 1.");
David Sedlákdf2a9732019-08-07 13:23:16 +02001050 FREE_STRING(st->ctx, value);
1051 value = NULL;
1052 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1053 exts = NULL;
1054
David Sedlák32488102019-07-15 17:44:10 +02001055 /* missing text subelement */
1056 data = ELEMENT_WRAPPER_START
1057 "<reference>reference...</reference>"
1058 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001059 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001060 logbuf_assert("Missing mandatory sub-element \"text\" of \"reference\" element. Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001061
David Sedlákd1144562019-08-06 12:36:14 +02001062 /* reference element */
1063 data = ELEMENT_WRAPPER_START
1064 "<reference>" EXT_SUBELEM "<text>reference...</text></reference>"
1065 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001066 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001067 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 +02001068 FREE_STRING(st->ctx, value);
1069 value = NULL;
1070 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1071 exts = NULL;
1072
David Sedlák32488102019-07-15 17:44:10 +02001073 st->finished_correctly = true;
1074}
1075
1076static void
1077test_import_elem(void **state)
1078{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001079 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001080 const char *data;
David Sedlák298ff6d2019-07-26 14:29:03 +02001081 struct lysp_import *imports = NULL;
1082 struct import_meta imp_meta = {"prefix", &imports};
David Sedlák32488102019-07-15 17:44:10 +02001083
1084 /* max subelems */
1085 data = ELEMENT_WRAPPER_START
1086 "<import module=\"a\">"
David Sedlákd1144562019-08-06 12:36:14 +02001087 EXT_SUBELEM
David Sedlák32488102019-07-15 17:44:10 +02001088 "<prefix value=\"a_mod\"/>"
1089 "<revision-date date=\"2015-01-01\"></revision-date>"
1090 "<description><text>import description</text></description>"
1091 "<reference><text>import reference</text></reference>"
1092 "</import>"
1093 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001094 assert_int_equal(test_element_helper(st, data, &imp_meta, NULL, NULL), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02001095 assert_string_equal(imports->name, "a");
1096 assert_string_equal(imports->prefix, "a_mod");
1097 assert_string_equal(imports->rev, "2015-01-01");
1098 assert_string_equal(imports->dsc, "import description");
1099 assert_string_equal(imports->ref, "import reference");
David Sedláke0ef1c62019-09-13 10:05:55 +02001100 assert_string_equal(imports->exts->name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001101 assert_int_equal(imports->exts->insubstmt, LYEXT_SUBSTMT_SELF);
1102 assert_int_equal(imports->exts->insubstmt_index, 0);
David Sedlák298ff6d2019-07-26 14:29:03 +02001103 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1104 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001105
1106 /* min subelems */
David Sedlák32488102019-07-15 17:44:10 +02001107 data = ELEMENT_WRAPPER_START
1108 "<import module=\"a\">"
1109 "<prefix value=\"a_mod\"/>"
1110 "</import>"
1111 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001112 assert_int_equal(test_element_helper(st, data, &imp_meta, NULL, NULL), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02001113 assert_string_equal(imports->prefix, "a_mod");
1114 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1115 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001116
1117 /* invalid (missing prefix) */
David Sedlák298ff6d2019-07-26 14:29:03 +02001118 data = ELEMENT_WRAPPER_START "<import module=\"a\"></import>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001119 assert_int_equal(test_element_helper(st, data, &imp_meta, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001120 logbuf_assert("Missing mandatory sub-element \"prefix\" of \"import\" element. Line number 1.");
David Sedlák298ff6d2019-07-26 14:29:03 +02001121 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1122 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001123
1124 /* invalid reused prefix */
David Sedlák32488102019-07-15 17:44:10 +02001125 data = ELEMENT_WRAPPER_START
1126 "<import module=\"a\">"
David Sedlák298ff6d2019-07-26 14:29:03 +02001127 "<prefix value=\"prefix\"/>"
David Sedlák32488102019-07-15 17:44:10 +02001128 "</import>"
1129 "<import module=\"a\">"
David Sedlák298ff6d2019-07-26 14:29:03 +02001130 "<prefix value=\"prefix\"/>"
David Sedlák32488102019-07-15 17:44:10 +02001131 "</import>"
1132 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001133 assert_int_equal(test_element_helper(st, data, &imp_meta, NULL, NULL), LY_EVALID);
David Sedlák298ff6d2019-07-26 14:29:03 +02001134 logbuf_assert("Prefix \"prefix\" already used as module prefix. Line number 1.");
1135 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1136 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001137
1138 st->finished_correctly = true;
1139}
1140
1141static void
1142test_status_elem(void **state)
1143{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001144 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001145 const char *data;
1146 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001147 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001148
1149 /* test valid values */
1150 data = ELEMENT_WRAPPER_START "<status value=\"current\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001151 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001152 assert_true(flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +02001153
1154 data = ELEMENT_WRAPPER_START "<status value=\"deprecated\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001155 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001156 assert_true(flags & LYS_STATUS_DEPRC);
David Sedlák32488102019-07-15 17:44:10 +02001157
David Sedlákd1144562019-08-06 12:36:14 +02001158 data = ELEMENT_WRAPPER_START "<status value=\"obsolete\">"EXT_SUBELEM"</status>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001159 assert_int_equal(test_element_helper(st, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001160 assert_true(flags & LYS_STATUS_OBSLT);
David Sedláke0ef1c62019-09-13 10:05:55 +02001161 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001162 assert_int_equal(exts[0].insubstmt_index, 0);
1163 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_STATUS);
1164 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1165 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001166
1167 /* test invalid value */
1168 data = ELEMENT_WRAPPER_START "<status value=\"invalid\"></status>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001169 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001170 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 +02001171 st->finished_correctly = true;
1172}
1173
1174static void
1175test_ext_elem(void **state)
1176{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001177 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001178 const char *data;
1179 struct lysp_ext *ext = NULL;
1180
1181 /* max subelems */
1182 data = ELEMENT_WRAPPER_START
1183 "<extension name=\"ext_name\">"
1184 "<argument name=\"arg\"></argument>"
1185 "<status value=\"current\"/>"
1186 "<description><text>ext_desc</text></description>"
1187 "<reference><text>ext_ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001188 EXT_SUBELEM
David Sedlák32488102019-07-15 17:44:10 +02001189 "</extension>"
1190 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001191 assert_int_equal(test_element_helper(st, data, &ext, NULL, NULL), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001192 assert_string_equal(ext->name, "ext_name");
1193 assert_string_equal(ext->argument, "arg");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001194 assert_true(ext->flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +02001195 assert_string_equal(ext->dsc, "ext_desc");
1196 assert_string_equal(ext->ref, "ext_ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02001197 assert_string_equal(ext->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001198 assert_int_equal(ext->exts[0].insubstmt_index, 0);
1199 assert_int_equal(ext->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák32488102019-07-15 17:44:10 +02001200 lysp_ext_free(st->ctx, ext);
1201 LY_ARRAY_FREE(ext);
1202 ext = NULL;
1203
1204 /* min subelems */
1205 data = ELEMENT_WRAPPER_START "<extension name=\"ext_name\"></extension>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001206 assert_int_equal(test_element_helper(st, data, &ext, NULL, NULL), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001207 assert_string_equal(ext->name, "ext_name");
1208 lysp_ext_free(st->ctx, ext);
1209 LY_ARRAY_FREE(ext);
1210 ext = NULL;
1211
1212 st->finished_correctly = true;
1213}
1214
1215static void
1216test_yin_element_elem(void **state)
1217{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001218 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001219 const char *data;
1220 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001221 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001222
1223 data = ELEMENT_WRAPPER_START "<yin-element value=\"true\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001224 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001225 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +02001226
David Sedlákd1144562019-08-06 12:36:14 +02001227 data = ELEMENT_WRAPPER_START "<yin-element value=\"false\">" EXT_SUBELEM "</yin-element>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001228 assert_int_equal(test_element_helper(st, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001229 assert_true(flags & LYS_YINELEM_TRUE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001230 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001231 assert_int_equal(exts[0].insubstmt_index, 0);
1232 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_YINELEM);
1233 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001234
1235 data = ELEMENT_WRAPPER_START "<yin-element value=\"invalid\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001236 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_EVALID);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001237 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák26ea1432019-08-14 13:42:23 +02001238 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 +02001239 st->finished_correctly = true;
1240}
1241
1242static void
1243test_yangversion_elem(void **state)
1244{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001245 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001246 const char *data;
1247 uint8_t version = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001248 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001249
1250 /* valid values */
Radek Krejci96e48da2020-09-04 13:18:06 +02001251 data = ELEMENT_WRAPPER_START "<yang-version value=\"1\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001252 assert_int_equal(test_element_helper(st, data, &version, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001253 assert_true(version & LYS_VERSION_1_0);
David Sedlák32488102019-07-15 17:44:10 +02001254
David Sedlákd1144562019-08-06 12:36:14 +02001255 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.1\">" EXT_SUBELEM "</yang-version>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001256 assert_int_equal(test_element_helper(st, data, &version, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001257 assert_true(version & LYS_VERSION_1_1);
David Sedláke0ef1c62019-09-13 10:05:55 +02001258 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001259 assert_int_equal(exts[0].insubstmt_index, 0);
1260 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_VERSION);
1261 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001262
1263 /* invalid value */
1264 data = ELEMENT_WRAPPER_START "<yang-version value=\"version\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001265 assert_int_equal(test_element_helper(st, data, &version, NULL, NULL), LY_EVALID);
Radek Krejci96e48da2020-09-04 13:18:06 +02001266 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 +02001267
1268 st->finished_correctly = true;
1269}
1270
1271static void
1272test_mandatory_elem(void **state)
1273{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001274 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001275 const char *data;
1276 uint16_t man = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001277 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001278
1279 /* valid values */
1280 data = ELEMENT_WRAPPER_START "<mandatory value=\"true\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001281 assert_int_equal(test_element_helper(st, data, &man, NULL, NULL), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001282 assert_int_equal(man, LYS_MAND_TRUE);
1283 man = 0;
1284
David Sedlákd1144562019-08-06 12:36:14 +02001285 data = ELEMENT_WRAPPER_START "<mandatory value=\"false\">" EXT_SUBELEM "</mandatory>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001286 assert_int_equal(test_element_helper(st, data, &man, NULL, &exts), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001287 assert_int_equal(man, LYS_MAND_FALSE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001288 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001289 assert_int_equal(exts[0].insubstmt_index, 0);
1290 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_MANDATORY);
1291 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001292
1293 data = ELEMENT_WRAPPER_START "<mandatory value=\"invalid\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001294 assert_int_equal(test_element_helper(st, data, &man, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001295 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 +02001296
1297 st->finished_correctly = true;
1298}
1299
David Sedlák8e7bda82019-07-16 17:57:50 +02001300static void
1301test_argument_elem(void **state)
1302{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001303 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001304 const char *data;
1305 uint16_t flags = 0;
1306 const char *arg;
1307 struct yin_argument_meta arg_meta = {&flags, &arg};
David Sedlákd1144562019-08-06 12:36:14 +02001308 struct lysp_ext_instance *exts = NULL;
1309
David Sedlák8e7bda82019-07-16 17:57:50 +02001310 /* max subelems */
1311 data = ELEMENT_WRAPPER_START
1312 "<argument name=\"arg-name\">"
1313 "<yin-element value=\"true\" />"
David Sedlákd1144562019-08-06 12:36:14 +02001314 EXT_SUBELEM
David Sedlák8e7bda82019-07-16 17:57:50 +02001315 "</argument>"
1316 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001317 assert_int_equal(test_element_helper(st, data, &arg_meta, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001318 assert_string_equal(arg, "arg-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001319 assert_true(flags & LYS_YINELEM_TRUE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001320 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001321 assert_int_equal(exts[0].insubstmt_index, 0);
1322 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ARGUMENT);
1323 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1324 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001325 flags = 0;
1326 FREE_STRING(st->ctx, arg);
1327 arg = NULL;
1328
1329 /* min subelems */
1330 data = ELEMENT_WRAPPER_START
1331 "<argument name=\"arg\">"
1332 "</argument>"
1333 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001334 assert_int_equal(test_element_helper(st, data, &arg_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001335 assert_string_equal(arg, "arg");
1336 assert_true(flags == 0);
1337 FREE_STRING(st->ctx, arg);
1338
1339 st->finished_correctly = true;
1340}
1341
1342static void
1343test_base_elem(void **state)
1344{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001345 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001346 const char *data;
1347 const char **bases = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001348 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001349 struct lysp_type type = {};
1350
1351 /* as identity subelement */
1352 data = "<identity xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlákd1144562019-08-06 12:36:14 +02001353 "<base name=\"base-name\">"
1354 EXT_SUBELEM
1355 "</base>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001356 "</identity>";
Michal Vaskob36053d2020-03-26 15:49:30 +01001357 assert_int_equal(test_element_helper(st, data, &bases, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001358 assert_string_equal(*bases, "base-name");
David Sedláke0ef1c62019-09-13 10:05:55 +02001359 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001360 assert_int_equal(exts[0].insubstmt_index, 0);
1361 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_BASE);
1362 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1363 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001364 FREE_STRING(st->ctx, *bases);
1365 LY_ARRAY_FREE(bases);
1366
1367 /* as type subelement */
1368 data = "<type xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlákd1144562019-08-06 12:36:14 +02001369 "<base name=\"base-name\">"
1370 EXT_SUBELEM
1371 "</base>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001372 "</type>";
Michal Vaskob36053d2020-03-26 15:49:30 +01001373 assert_int_equal(test_element_helper(st, data, &type, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001374 assert_string_equal(*type.bases, "base-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001375 assert_true(type.flags & LYS_SET_BASE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001376 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001377 assert_int_equal(exts[0].insubstmt_index, 0);
1378 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_BASE);
1379 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1380 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001381 FREE_STRING(st->ctx, *type.bases);
1382 LY_ARRAY_FREE(type.bases);
1383
1384 st->finished_correctly = true;
1385}
1386
1387static void
1388test_belongsto_elem(void **state)
1389{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001390 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001391 const char *data;
1392 struct lysp_submodule submod;
David Sedlákd1144562019-08-06 12:36:14 +02001393 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001394
1395 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +02001396 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/>"EXT_SUBELEM"</belongs-to>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001397 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001398 assert_int_equal(test_element_helper(st, data, &submod, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001399 assert_string_equal(submod.prefix, "pref");
David Sedláke0ef1c62019-09-13 10:05:55 +02001400 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001401 assert_int_equal(exts[0].insubstmt_index, 0);
1402 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_BELONGSTO);
1403 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1404 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001405 FREE_STRING(st->ctx, submod.prefix);
1406
1407 data = ELEMENT_WRAPPER_START "<belongs-to module=\"module-name\"></belongs-to>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001408 assert_int_equal(test_element_helper(st, data, &submod, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001409 logbuf_assert("Missing mandatory sub-element \"prefix\" of \"belongs-to\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001410
1411 st->finished_correctly = true;
1412}
1413
1414static void
1415test_config_elem(void **state)
1416{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001417 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001418 const char *data;
1419 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001420 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001421
David Sedlákd1144562019-08-06 12:36:14 +02001422 data = ELEMENT_WRAPPER_START "<config value=\"true\">" EXT_SUBELEM "</config>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001423 assert_int_equal(test_element_helper(st, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001424 assert_true(flags & LYS_CONFIG_W);
David Sedláke0ef1c62019-09-13 10:05:55 +02001425 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001426 assert_int_equal(exts[0].insubstmt_index, 0);
1427 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_CONFIG);
1428 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1429 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001430 flags = 0;
1431
1432 data = ELEMENT_WRAPPER_START "<config value=\"false\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001433 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001434 assert_true(flags & LYS_CONFIG_R);
David Sedlák8e7bda82019-07-16 17:57:50 +02001435 flags = 0;
1436
1437 data = ELEMENT_WRAPPER_START "<config value=\"invalid\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001438 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001439 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 +02001440
1441 st->finished_correctly = true;
1442}
1443
1444static void
1445test_default_elem(void **state)
1446{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001447 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001448 const char *data;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001449 struct lysp_qname val = {0};
David Sedlákd1144562019-08-06 12:36:14 +02001450 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001451
David Sedlákd1144562019-08-06 12:36:14 +02001452 data = ELEMENT_WRAPPER_START "<default value=\"defaul-value\">"EXT_SUBELEM"</default>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001453 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001454 assert_string_equal(val.str, "defaul-value");
David Sedláke0ef1c62019-09-13 10:05:55 +02001455 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001456 assert_int_equal(exts[0].insubstmt_index, 0);
1457 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_DEFAULT);
1458 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1459 exts = NULL;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001460 FREE_STRING(st->ctx, val.str);
1461 val.str = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001462
1463 data = ELEMENT_WRAPPER_START "<default/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001464 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001465 logbuf_assert("Missing mandatory attribute value of default element. Line number 1.");
1466
1467 st->finished_correctly = true;
1468}
1469
1470static void
1471test_err_app_tag_elem(void **state)
1472{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001473 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001474 const char *data;
1475 const char *val = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001476 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001477
David Sedlákd1144562019-08-06 12:36:14 +02001478 data = ELEMENT_WRAPPER_START "<error-app-tag value=\"val\">"EXT_SUBELEM"</error-app-tag>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001479 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001480 assert_string_equal(val, "val");
David Sedláke0ef1c62019-09-13 10:05:55 +02001481 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001482 assert_int_equal(exts[0].insubstmt_index, 0);
1483 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ERRTAG);
1484 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1485 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001486 FREE_STRING(st->ctx, val);
1487 val = NULL;
1488
1489 data = ELEMENT_WRAPPER_START "<error-app-tag/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001490 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001491 logbuf_assert("Missing mandatory attribute value of error-app-tag element. Line number 1.");
1492
1493 st->finished_correctly = true;
1494}
1495
1496static void
1497test_err_msg_elem(void **state)
1498{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001499 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001500 const char *data;
1501 const char *val = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001502 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001503
David Sedlákd1144562019-08-06 12:36:14 +02001504 data = ELEMENT_WRAPPER_START "<error-message><value>val</value>"EXT_SUBELEM"</error-message>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001505 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001506 assert_string_equal(val, "val");
David Sedláke0ef1c62019-09-13 10:05:55 +02001507 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001508 assert_int_equal(exts[0].insubstmt_index, 0);
1509 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ERRMSG);
1510 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1511 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001512 FREE_STRING(st->ctx, val);
1513
1514 data = ELEMENT_WRAPPER_START "<error-message></error-message>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001515 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001516 logbuf_assert("Missing mandatory sub-element \"value\" of \"error-message\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001517
David Sedlákdf2a9732019-08-07 13:23:16 +02001518 data = ELEMENT_WRAPPER_START "<error-message invalid=\"text\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001519 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001520 logbuf_assert("Unexpected attribute \"invalid\" of \"error-message\" element. Line number 1.");
David Sedlákdf2a9732019-08-07 13:23:16 +02001521
David Sedlák8e7bda82019-07-16 17:57:50 +02001522 st->finished_correctly = true;
1523}
1524
1525static void
1526test_fracdigits_elem(void **state)
1527{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001528 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001529 const char *data;
1530 struct lysp_type type = {};
1531
1532 /* valid value */
David Sedlákd1144562019-08-06 12:36:14 +02001533 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"10\">"EXT_SUBELEM"</fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001534 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedláke0ef1c62019-09-13 10:05:55 +02001535 assert_string_equal(type.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001536 assert_int_equal(type.exts[0].insubstmt_index, 0);
1537 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_FRACDIGITS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001538 assert_int_equal(type.fraction_digits, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001539 assert_true(type.flags & LYS_SET_FRDIGITS);
David Sedlákd1144562019-08-06 12:36:14 +02001540 FREE_ARRAY(st->ctx, type.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001541
1542 /* invalid values */
1543 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"-1\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001544 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001545 logbuf_assert("Invalid value \"-1\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001546
1547 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"02\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001548 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001549 logbuf_assert("Invalid value \"02\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001550
1551 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"1p\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001552 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001553 logbuf_assert("Invalid value \"1p\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001554
1555 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"19\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001556 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001557 logbuf_assert("Invalid value \"19\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001558
1559 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"999999999999999999\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001560 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001561 logbuf_assert("Invalid value \"999999999999999999\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001562
1563 st->finished_correctly = true;
1564}
1565
1566static void
1567test_iffeature_elem(void **state)
1568{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001569 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001570 const char *data;
1571 const char **iffeatures = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001572 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001573
David Sedlákd1144562019-08-06 12:36:14 +02001574 data = ELEMENT_WRAPPER_START "<if-feature name=\"local-storage\">"EXT_SUBELEM"</if-feature>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001575 assert_int_equal(test_element_helper(st, data, &iffeatures, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001576 assert_string_equal(*iffeatures, "local-storage");
David Sedláke0ef1c62019-09-13 10:05:55 +02001577 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001578 assert_int_equal(exts[0].insubstmt_index, 0);
1579 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_IFFEATURE);
1580 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1581 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001582 FREE_STRING(st->ctx, *iffeatures);
1583 LY_ARRAY_FREE(iffeatures);
1584 iffeatures = NULL;
1585
1586 data = ELEMENT_WRAPPER_START "<if-feature/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001587 assert_int_equal(test_element_helper(st, data, &iffeatures, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001588 logbuf_assert("Missing mandatory attribute name of if-feature element. Line number 1.");
1589 LY_ARRAY_FREE(iffeatures);
1590 iffeatures = NULL;
1591
1592 st->finished_correctly = true;
1593}
1594
1595static void
1596test_length_elem(void **state)
1597{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001598 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001599 const char *data;
1600 struct lysp_type type = {};
1601
1602 /* max subelems */
1603 data = ELEMENT_WRAPPER_START
1604 "<length value=\"length-str\">"
1605 "<error-message><value>err-msg</value></error-message>"
1606 "<error-app-tag value=\"err-app-tag\"/>"
1607 "<description><text>desc</text></description>"
1608 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001609 EXT_SUBELEM
David Sedlák8e7bda82019-07-16 17:57:50 +02001610 "</length>"
1611 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001612 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001613 assert_string_equal(type.length->arg.str, "length-str");
David Sedlák8e7bda82019-07-16 17:57:50 +02001614 assert_string_equal(type.length->emsg, "err-msg");
1615 assert_string_equal(type.length->eapptag, "err-app-tag");
1616 assert_string_equal(type.length->dsc, "desc");
1617 assert_string_equal(type.length->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001618 assert_true(type.flags & LYS_SET_LENGTH);
David Sedláke0ef1c62019-09-13 10:05:55 +02001619 assert_string_equal(type.length->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001620 assert_int_equal(type.length->exts[0].insubstmt_index, 0);
1621 assert_int_equal(type.length->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8e7bda82019-07-16 17:57:50 +02001622 lysp_type_free(st->ctx, &type);
1623 memset(&type, 0, sizeof(type));
1624
1625 /* min subelems */
1626 data = ELEMENT_WRAPPER_START
1627 "<length value=\"length-str\">"
1628 "</length>"
1629 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001630 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001631 assert_string_equal(type.length->arg.str, "length-str");
David Sedlák8e7bda82019-07-16 17:57:50 +02001632 lysp_type_free(st->ctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001633 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001634 memset(&type, 0, sizeof(type));
1635
1636 data = ELEMENT_WRAPPER_START "<length></length>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001637 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001638 logbuf_assert("Missing mandatory attribute value of length element. Line number 1.");
1639 lysp_type_free(st->ctx, &type);
1640 memset(&type, 0, sizeof(type));
1641
1642 st->finished_correctly = true;
1643}
1644
1645static void
1646test_modifier_elem(void **state)
1647{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001648 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001649 const char *data;
Radek Krejci011e4aa2020-09-04 15:22:31 +02001650 const char *pat;
David Sedlákd1144562019-08-06 12:36:14 +02001651 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001652
Radek Krejci011e4aa2020-09-04 15:22:31 +02001653 assert_int_equal(LY_SUCCESS, lydict_insert(st->ctx, "\006pattern", 8, &pat));
David Sedlákd1144562019-08-06 12:36:14 +02001654 data = ELEMENT_WRAPPER_START "<modifier value=\"invert-match\">" EXT_SUBELEM "</modifier>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001655 assert_int_equal(test_element_helper(st, data, &pat, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001656 assert_string_equal(pat, "\x015pattern");
David Sedláke0ef1c62019-09-13 10:05:55 +02001657 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001658 assert_int_equal(exts[0].insubstmt_index, 0);
1659 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_MODIFIER);
1660 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1661 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001662 FREE_STRING(st->ctx, pat);
1663
Radek Krejci011e4aa2020-09-04 15:22:31 +02001664 assert_int_equal(LY_SUCCESS, lydict_insert(st->ctx, "\006pattern", 8, &pat));
David Sedlák8e7bda82019-07-16 17:57:50 +02001665 data = ELEMENT_WRAPPER_START "<modifier value=\"invert\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001666 assert_int_equal(test_element_helper(st, data, &pat, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001667 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 +02001668 FREE_STRING(st->ctx, pat);
1669
1670 st->finished_correctly = true;
1671}
1672
1673static void
1674test_namespace_elem(void **state)
1675{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001676 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001677 const char *data;
1678 const char *ns;
David Sedlákd1144562019-08-06 12:36:14 +02001679 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001680
David Sedlákd1144562019-08-06 12:36:14 +02001681 data = ELEMENT_WRAPPER_START "<namespace uri=\"ns\">" EXT_SUBELEM "</namespace>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001682 assert_int_equal(test_element_helper(st, data, &ns, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001683 assert_string_equal(ns, "ns");
David Sedláke0ef1c62019-09-13 10:05:55 +02001684 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001685 assert_int_equal(exts[0].insubstmt_index, 0);
1686 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_NAMESPACE);
1687 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1688 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001689 FREE_STRING(st->ctx, ns);
1690
1691 data = ELEMENT_WRAPPER_START "<namespace/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001692 assert_int_equal(test_element_helper(st, data, &ns, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001693 logbuf_assert("Missing mandatory attribute uri of namespace element. Line number 1.");
1694
1695 st->finished_correctly = true;
1696}
1697
1698static void
David Sedlák8e7bda82019-07-16 17:57:50 +02001699test_pattern_elem(void **state)
1700{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001701 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001702 const char *data;
1703 struct lysp_type type = {};
1704
1705 /* max subelems */
1706 data = ELEMENT_WRAPPER_START
1707 "<pattern value=\"super_pattern\">"
1708 "<modifier value=\"invert-match\"/>"
1709 "<error-message><value>err-msg-value</value></error-message>"
1710 "<error-app-tag value=\"err-app-tag-value\"/>"
David Sedlák169cc522019-08-15 13:23:45 +02001711 "<description><text>&quot;pattern-desc&quot;</text></description>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001712 "<reference><text>pattern-ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001713 EXT_SUBELEM
David Sedlák8e7bda82019-07-16 17:57:50 +02001714 "</pattern>"
1715 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001716 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001717 assert_true(type.flags & LYS_SET_PATTERN);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001718 assert_string_equal(type.patterns->arg.str, "\x015super_pattern");
David Sedlák169cc522019-08-15 13:23:45 +02001719 assert_string_equal(type.patterns->dsc, "\"pattern-desc\"");
David Sedlák8e7bda82019-07-16 17:57:50 +02001720 assert_string_equal(type.patterns->eapptag, "err-app-tag-value");
1721 assert_string_equal(type.patterns->emsg, "err-msg-value");
David Sedlák8e7bda82019-07-16 17:57:50 +02001722 assert_string_equal(type.patterns->ref, "pattern-ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02001723 assert_string_equal(type.patterns->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001724 assert_int_equal(type.patterns->exts[0].insubstmt_index, 0);
1725 assert_int_equal(type.patterns->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8e7bda82019-07-16 17:57:50 +02001726 lysp_type_free(st->ctx, &type);
1727 memset(&type, 0, sizeof(type));
1728
1729 /* min subelems */
1730 data = ELEMENT_WRAPPER_START "<pattern value=\"pattern\"> </pattern>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001731 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001732 assert_string_equal(type.patterns->arg.str, "\x006pattern");
David Sedlák8e7bda82019-07-16 17:57:50 +02001733 lysp_type_free(st->ctx, &type);
1734 memset(&type, 0, sizeof(type));
1735
1736 st->finished_correctly = true;
1737}
1738
1739static void
1740test_value_position_elem(void **state)
1741{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001742 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001743 const char *data;
1744 struct lysp_type_enum en = {};
1745
1746 /* valid values */
David Sedlákd1144562019-08-06 12:36:14 +02001747 data = ELEMENT_WRAPPER_START "<value value=\"55\">" EXT_SUBELEM "</value>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001748 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001749 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001750 assert_true(en.flags & LYS_SET_VALUE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001751 assert_string_equal(en.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001752 assert_int_equal(en.exts[0].insubstmt_index, 0);
1753 assert_int_equal(en.exts[0].insubstmt, LYEXT_SUBSTMT_VALUE);
1754 FREE_ARRAY(st->ctx, en.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001755 memset(&en, 0, sizeof(en));
1756
1757 data = ELEMENT_WRAPPER_START "<value value=\"-55\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001758 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001759 assert_int_equal(en.value, -55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001760 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001761 memset(&en, 0, sizeof(en));
1762
1763 data = ELEMENT_WRAPPER_START "<value value=\"0\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001764 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001765 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001766 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001767 memset(&en, 0, sizeof(en));
1768
1769 data = ELEMENT_WRAPPER_START "<value value=\"-0\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001770 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001771 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001772 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001773 memset(&en, 0, sizeof(en));
1774
1775 /* valid positions */
David Sedlák8d552d62019-08-06 15:29:05 +02001776 data = ELEMENT_WRAPPER_START "<position value=\"55\">" EXT_SUBELEM "</position>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001777 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001778 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001779 assert_true(en.flags & LYS_SET_VALUE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001780 assert_string_equal(en.exts[0].name, "urn:example:extensions:c-define");
David Sedlák8d552d62019-08-06 15:29:05 +02001781 assert_int_equal(en.exts[0].insubstmt_index, 0);
1782 assert_int_equal(en.exts[0].insubstmt, LYEXT_SUBSTMT_POSITION);
1783 FREE_ARRAY(st->ctx, en.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001784 memset(&en, 0, sizeof(en));
1785
1786 data = ELEMENT_WRAPPER_START "<position value=\"0\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001787 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001788 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001789 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001790 memset(&en, 0, sizeof(en));
1791
1792 /* invalid values */
1793 data = ELEMENT_WRAPPER_START "<value value=\"99999999999999999999999\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001794 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001795 logbuf_assert("Invalid value \"99999999999999999999999\" of \"value\" attribute in \"value\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001796
1797 data = ELEMENT_WRAPPER_START "<value value=\"1k\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001798 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001799 logbuf_assert("Invalid value \"1k\" of \"value\" attribute in \"value\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001800
David Sedlák69f01612019-07-17 11:41:08 +02001801 data = ELEMENT_WRAPPER_START "<value value=\"\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001802 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001803 logbuf_assert("Invalid value \"\" of \"value\" attribute in \"value\" element. Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001804
David Sedlák8e7bda82019-07-16 17:57:50 +02001805 /*invalid positions */
1806 data = ELEMENT_WRAPPER_START "<position value=\"-5\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001807 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001808 logbuf_assert("Invalid value \"-5\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001809
1810 data = ELEMENT_WRAPPER_START "<position value=\"-0\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001811 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001812 logbuf_assert("Invalid value \"-0\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001813
1814 data = ELEMENT_WRAPPER_START "<position value=\"99999999999999999999\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001815 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001816 logbuf_assert("Invalid value \"99999999999999999999\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001817
David Sedlák69f01612019-07-17 11:41:08 +02001818 data = ELEMENT_WRAPPER_START "<position value=\"\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001819 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001820 logbuf_assert("Invalid value \"\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001821
1822 st->finished_correctly = true;
1823}
1824
1825static void
1826test_prefix_elem(void **state)
1827{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001828 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001829 const char *data;
1830 const char *value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001831 struct lysp_ext_instance *exts = NULL;
1832
1833 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\">" EXT_SUBELEM "</prefix>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001834 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001835 assert_string_equal(value, "pref");
David Sedláke0ef1c62019-09-13 10:05:55 +02001836 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001837 assert_int_equal(exts[0].insubstmt_index, 0);
1838 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_PREFIX);
1839 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1840 exts = NULL;
1841 FREE_STRING(st->ctx, value);
David Sedlák69f01612019-07-17 11:41:08 +02001842
1843 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001844 assert_int_equal(test_element_helper(st, data, &value, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001845 assert_string_equal(value, "pref");
1846 FREE_STRING(st->ctx, value);
1847
1848 st->finished_correctly = true;
1849}
1850
1851static void
1852test_range_elem(void **state)
1853{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001854 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001855 const char *data;
1856 struct lysp_type type = {};
1857
1858 /* max subelems */
1859 data = ELEMENT_WRAPPER_START
1860 "<range value=\"range-str\">"
1861 "<error-message><value>err-msg</value></error-message>"
1862 "<error-app-tag value=\"err-app-tag\" />"
1863 "<description><text>desc</text></description>"
1864 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001865 EXT_SUBELEM
David Sedlák69f01612019-07-17 11:41:08 +02001866 "</range>"
1867 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 assert_string_equal(type.range->dsc, "desc");
1871 assert_string_equal(type.range->eapptag, "err-app-tag");
1872 assert_string_equal(type.range->emsg, "err-msg");
1873 assert_string_equal(type.range->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001874 assert_true(type.flags & LYS_SET_RANGE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001875 assert_string_equal(type.range->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001876 assert_int_equal(type.range->exts[0].insubstmt_index, 0);
1877 assert_int_equal(type.range->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák69f01612019-07-17 11:41:08 +02001878 lysp_type_free(st->ctx, &type);
1879 memset(&type, 0, sizeof(type));
1880
1881 /* min subelems */
1882 data = ELEMENT_WRAPPER_START "<range value=\"range-str\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001883 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001884 assert_string_equal(type.range->arg.str, "range-str");
David Sedlák69f01612019-07-17 11:41:08 +02001885 lysp_type_free(st->ctx, &type);
1886 memset(&type, 0, sizeof(type));
1887
1888 st->finished_correctly = true;
1889}
1890
1891static void
1892test_reqinstance_elem(void **state)
1893{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001894 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001895 const char *data;
1896 struct lysp_type type = {};
1897
David Sedlákd1144562019-08-06 12:36:14 +02001898 data = ELEMENT_WRAPPER_START "<require-instance value=\"true\">" EXT_SUBELEM "</require-instance>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001899 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001900 assert_int_equal(type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001901 assert_true(type.flags & LYS_SET_REQINST);
David Sedláke0ef1c62019-09-13 10:05:55 +02001902 assert_string_equal(type.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001903 assert_int_equal(type.exts[0].insubstmt_index, 0);
1904 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_REQINSTANCE);
1905 lysp_type_free(st->ctx, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001906 memset(&type, 0, sizeof(type));
1907
1908 data = ELEMENT_WRAPPER_START "<require-instance value=\"false\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001909 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001910 assert_int_equal(type.require_instance, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001911 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001912 memset(&type, 0, sizeof(type));
1913
1914 data = ELEMENT_WRAPPER_START "<require-instance value=\"invalid\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001915 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák69f01612019-07-17 11:41:08 +02001916 memset(&type, 0, sizeof(type));
David Sedlák26ea1432019-08-14 13:42:23 +02001917 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 +02001918
1919 st->finished_correctly = true;
1920}
1921
1922static void
1923test_revision_date_elem(void **state)
1924{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001925 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001926 const char *data;
1927 char rev[LY_REV_SIZE];
David Sedlákd1144562019-08-06 12:36:14 +02001928 struct lysp_ext_instance *exts = NULL;
1929
1930 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\">"EXT_SUBELEM"</revision-date>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001931 assert_int_equal(test_element_helper(st, data, rev, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001932 assert_string_equal(rev, "2000-01-01");
David Sedláke0ef1c62019-09-13 10:05:55 +02001933 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001934 assert_int_equal(exts[0].insubstmt_index, 0);
1935 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_REVISIONDATE);
1936 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák69f01612019-07-17 11:41:08 +02001937
1938 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001939 assert_int_equal(test_element_helper(st, data, rev, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001940 assert_string_equal(rev, "2000-01-01");
1941
1942 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-50-05\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001943 assert_int_equal(test_element_helper(st, data, rev, NULL, NULL), LY_EVALID);
David Sedlák69f01612019-07-17 11:41:08 +02001944 logbuf_assert("Invalid value \"2000-50-05\" of \"revision-date\". Line number 1.");
1945
1946 st->finished_correctly = true;
1947}
1948
1949static void
1950test_unique_elem(void **state)
1951{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001952 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001953 const char *data;
1954 const char **values = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001955 struct lysp_ext_instance *exts = NULL;
1956
1957 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\">"EXT_SUBELEM"</unique>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001958 assert_int_equal(test_element_helper(st, data, &values, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001959 assert_string_equal(*values, "tag");
David Sedláke0ef1c62019-09-13 10:05:55 +02001960 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001961 assert_int_equal(exts[0].insubstmt_index, 0);
1962 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_UNIQUE);
1963 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1964 FREE_STRING(st->ctx, *values);
1965 LY_ARRAY_FREE(values);
1966 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001967
1968 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001969 assert_int_equal(test_element_helper(st, data, &values, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001970 assert_string_equal(*values, "tag");
1971 FREE_STRING(st->ctx, *values);
1972 LY_ARRAY_FREE(values);
David Sedlákd1144562019-08-06 12:36:14 +02001973 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001974
1975 st->finished_correctly = true;
1976}
1977
1978static void
1979test_units_elem(void **state)
1980{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001981 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001982 const char *data;
1983 const char *values = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001984 struct lysp_ext_instance *exts = NULL;
1985
1986 data = ELEMENT_WRAPPER_START "<units name=\"name\">"EXT_SUBELEM"</units>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001987 assert_int_equal(test_element_helper(st, data, &values, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001988 assert_string_equal(values, "name");
David Sedláke0ef1c62019-09-13 10:05:55 +02001989 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001990 assert_int_equal(exts[0].insubstmt_index, 0);
1991 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_UNITS);
1992 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1993 FREE_STRING(st->ctx, values);
1994 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001995
1996 data = ELEMENT_WRAPPER_START "<units name=\"name\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001997 assert_int_equal(test_element_helper(st, data, &values, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001998 assert_string_equal(values, "name");
1999 FREE_STRING(st->ctx, values);
David Sedlákd1144562019-08-06 12:36:14 +02002000 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02002001
2002 st->finished_correctly = true;
2003}
2004
2005static void
2006test_when_elem(void **state)
2007{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002008 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02002009 const char *data;
2010 struct lysp_when *when = NULL;
2011
2012 data = ELEMENT_WRAPPER_START
2013 "<when condition=\"cond\">"
2014 "<description><text>desc</text></description>"
2015 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02002016 EXT_SUBELEM
David Sedlák69f01612019-07-17 11:41:08 +02002017 "</when>"
2018 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002019 assert_int_equal(test_element_helper(st, data, &when, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002020 assert_string_equal(when->cond, "cond");
2021 assert_string_equal(when->dsc, "desc");
2022 assert_string_equal(when->ref, "ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02002023 assert_string_equal(when->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002024 assert_int_equal(when->exts[0].insubstmt_index, 0);
2025 assert_int_equal(when->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák69f01612019-07-17 11:41:08 +02002026 lysp_when_free(st->ctx, when);
2027 free(when);
2028 when = NULL;
2029
2030 data = ELEMENT_WRAPPER_START "<when condition=\"cond\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002031 assert_int_equal(test_element_helper(st, data, &when, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002032 assert_string_equal(when->cond, "cond");
2033 lysp_when_free(st->ctx, when);
2034 free(when);
2035 when = NULL;
2036
2037 st->finished_correctly = true;
2038}
2039
2040static void
2041test_yin_text_value_elem(void **state)
2042{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002043 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02002044 const char *data;
2045 const char *val;
2046
2047 data = ELEMENT_WRAPPER_START "<text>text</text>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002048 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002049 assert_string_equal(val, "text");
2050 FREE_STRING(st->ctx, val);
2051
2052 data = "<error-message xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <value>text</value> </error-message>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002053 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002054 assert_string_equal(val, "text");
2055 FREE_STRING(st->ctx, val);
2056
2057 data = ELEMENT_WRAPPER_START "<text></text>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002058 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002059 assert_string_equal("", val);
2060 FREE_STRING(st->ctx, val);
2061
David Sedlák8e7bda82019-07-16 17:57:50 +02002062 st->finished_correctly = true;
2063}
David Sedlák32488102019-07-15 17:44:10 +02002064
David Sedlák374d2b32019-07-17 15:06:55 +02002065static void
2066test_type_elem(void **state)
2067{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002068 struct test_parser_yin_state *st = *state;
David Sedlák374d2b32019-07-17 15:06:55 +02002069 const char *data;
2070 struct lysp_type type = {};
2071
2072 /* max subelems */
2073 data = ELEMENT_WRAPPER_START
2074 "<type name=\"type-name\">"
2075 "<base name=\"base-name\"/>"
2076 "<bit name=\"bit\"/>"
2077 "<enum name=\"enum\"/>"
2078 "<fraction-digits value=\"2\"/>"
2079 "<length value=\"length\"/>"
Michal Vaskocb8c6d42020-10-16 11:58:30 +02002080 "<path value=\"/path\"/>"
David Sedlák374d2b32019-07-17 15:06:55 +02002081 "<pattern value=\"pattern\"/>"
2082 "<range value=\"range\" />"
2083 "<require-instance value=\"true\"/>"
2084 "<type name=\"sub-type-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002085 EXT_SUBELEM
David Sedlák374d2b32019-07-17 15:06:55 +02002086 "</type>"
2087 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002088 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák374d2b32019-07-17 15:06:55 +02002089 assert_string_equal(type.name, "type-name");
2090 assert_string_equal(*type.bases, "base-name");
2091 assert_string_equal(type.bits->name, "bit");
2092 assert_string_equal(type.enums->name, "enum");
2093 assert_int_equal(type.fraction_digits, 2);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002094 assert_string_equal(type.length->arg.str, "length");
Michal Vaskocb8c6d42020-10-16 11:58:30 +02002095 assert_string_equal(type.path->expr, "/path");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002096 assert_string_equal(type.patterns->arg.str, "\006pattern");
2097 assert_string_equal(type.range->arg.str, "range");
David Sedlák374d2b32019-07-17 15:06:55 +02002098 assert_int_equal(type.require_instance, 1);
2099 assert_string_equal(type.types->name, "sub-type-name");
David Sedláke0ef1c62019-09-13 10:05:55 +02002100 assert_string_equal(type.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002101 assert_int_equal(type.exts[0].insubstmt_index, 0);
2102 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002103 assert_true(type.flags & LYS_SET_BASE);
2104 assert_true(type.flags & LYS_SET_BIT);
2105 assert_true(type.flags & LYS_SET_ENUM);
2106 assert_true(type.flags & LYS_SET_FRDIGITS);
2107 assert_true(type.flags & LYS_SET_LENGTH);
2108 assert_true(type.flags & LYS_SET_PATH);
2109 assert_true(type.flags & LYS_SET_PATTERN);
2110 assert_true(type.flags & LYS_SET_RANGE);
2111 assert_true(type.flags & LYS_SET_REQINST);
2112 assert_true(type.flags & LYS_SET_TYPE);
David Sedlákd1144562019-08-06 12:36:14 +02002113 lysp_type_free(st->ctx, &type);
David Sedlák374d2b32019-07-17 15:06:55 +02002114 memset(&type, 0, sizeof(type));
2115
2116 /* min subelems */
2117 data = ELEMENT_WRAPPER_START "<type name=\"type-name\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002118 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák374d2b32019-07-17 15:06:55 +02002119 lysp_type_free(st->ctx, &type);
2120 memset(&type, 0, sizeof(type));
2121
2122 st->finished_correctly = true;
2123}
2124
David Sedlák1af868e2019-07-17 17:03:14 +02002125static void
2126test_max_elems_elem(void **state)
2127{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002128 struct test_parser_yin_state *st = *state;
David Sedlák1af868e2019-07-17 17:03:14 +02002129 const char *data;
2130 struct lysp_node_list list = {};
2131 struct lysp_node_leaflist llist = {};
2132 struct lysp_refine refine = {};
2133
David Sedlákd1144562019-08-06 12:36:14 +02002134 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 +01002135 assert_int_equal(test_element_helper(st, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02002136 assert_int_equal(refine.max, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002137 assert_true(refine.flags & LYS_SET_MAX);
David Sedláke0ef1c62019-09-13 10:05:55 +02002138 assert_string_equal(refine.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002139 assert_int_equal(refine.exts[0].insubstmt_index, 0);
2140 assert_int_equal(refine.exts[0].insubstmt, LYEXT_SUBSTMT_MAX);
2141 FREE_ARRAY(st->ctx, refine.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02002142
David Sedlákd1144562019-08-06 12:36:14 +02002143 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 +01002144 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02002145 assert_int_equal(list.max, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002146 assert_true(list.flags & LYS_SET_MAX);
David Sedláke0ef1c62019-09-13 10:05:55 +02002147 assert_string_equal(list.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002148 assert_int_equal(list.exts[0].insubstmt_index, 0);
2149 assert_int_equal(list.exts[0].insubstmt, LYEXT_SUBSTMT_MAX);
2150 FREE_ARRAY(st->ctx, list.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02002151
David Sedlákd1144562019-08-06 12:36:14 +02002152 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 +01002153 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02002154 assert_int_equal(llist.max, 85);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002155 assert_true(llist.flags & LYS_SET_MAX);
David Sedláke0ef1c62019-09-13 10:05:55 +02002156 assert_string_equal(llist.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002157 assert_int_equal(llist.exts[0].insubstmt_index, 0);
2158 assert_int_equal(llist.exts[0].insubstmt, LYEXT_SUBSTMT_MAX);
2159 FREE_ARRAY(st->ctx, llist.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02002160
2161 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"10\"/> </refine>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002162 assert_int_equal(test_element_helper(st, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02002163 assert_int_equal(refine.max, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002164 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02002165
2166 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"0\"/> </list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002167 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002168 logbuf_assert("Invalid value \"0\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002169
2170 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"-10\"/> </list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002171 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002172 logbuf_assert("Invalid value \"-10\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002173
2174 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"k\"/> </list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002175 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002176 logbuf_assert("Invalid value \"k\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002177
2178 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"u12\"/> </list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002179 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002180 logbuf_assert("Invalid value \"u12\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002181
2182 st->finished_correctly = true;
2183}
2184
David Sedlák09e18c92019-07-18 11:17:11 +02002185static void
2186test_min_elems_elem(void **state)
2187{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002188 struct test_parser_yin_state *st = *state;
David Sedlák09e18c92019-07-18 11:17:11 +02002189 const char *data;
2190 struct lysp_node_list list = {};
2191 struct lysp_node_leaflist llist = {};
2192 struct lysp_refine refine = {};
2193
David Sedlákd1144562019-08-06 12:36:14 +02002194 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 +01002195 assert_int_equal(test_element_helper(st, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák09e18c92019-07-18 11:17:11 +02002196 assert_int_equal(refine.min, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002197 assert_true(refine.flags & LYS_SET_MIN);
David Sedláke0ef1c62019-09-13 10:05:55 +02002198 assert_string_equal(refine.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002199 assert_int_equal(refine.exts[0].insubstmt_index, 0);
2200 assert_int_equal(refine.exts[0].insubstmt, LYEXT_SUBSTMT_MIN);
2201 FREE_ARRAY(st->ctx, refine.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02002202
David Sedlákd1144562019-08-06 12:36:14 +02002203 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 +01002204 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_SUCCESS);
David Sedlák09e18c92019-07-18 11:17:11 +02002205 assert_int_equal(list.min, 41);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002206 assert_true(list.flags & LYS_SET_MIN);
David Sedláke0ef1c62019-09-13 10:05:55 +02002207 assert_string_equal(list.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002208 assert_int_equal(list.exts[0].insubstmt_index, 0);
2209 assert_int_equal(list.exts[0].insubstmt, LYEXT_SUBSTMT_MIN);
2210 FREE_ARRAY(st->ctx, list.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02002211
David Sedlákd1144562019-08-06 12:36:14 +02002212 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 +01002213 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_SUCCESS);
David Sedlák09e18c92019-07-18 11:17:11 +02002214 assert_int_equal(llist.min, 50);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002215 assert_true(llist.flags & LYS_SET_MIN);
David Sedláke0ef1c62019-09-13 10:05:55 +02002216 assert_string_equal(llist.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002217 assert_int_equal(llist.exts[0].insubstmt_index, 0);
2218 assert_int_equal(llist.exts[0].insubstmt, LYEXT_SUBSTMT_MIN);
2219 FREE_ARRAY(st->ctx, llist.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02002220
2221 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 +01002222 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002223 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 +02002224
2225 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 +01002226 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002227 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 +02002228
2229 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 +01002230 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002231 logbuf_assert("Invalid value \"5k\" of \"value\" attribute in \"min-elements\" element. Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02002232
2233 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 +01002234 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002235 logbuf_assert("Invalid value \"05\" of \"value\" attribute in \"min-elements\" element. Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02002236
2237 st->finished_correctly = true;
2238}
2239
David Sedláka2dad212019-07-18 12:45:19 +02002240static void
2241test_ordby_elem(void **state)
2242{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002243 struct test_parser_yin_state *st = *state;
David Sedláka2dad212019-07-18 12:45:19 +02002244 const char *data;
2245 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02002246 struct lysp_ext_instance *exts = NULL;
David Sedláka2dad212019-07-18 12:45:19 +02002247
David Sedlákd1144562019-08-06 12:36:14 +02002248 data = ELEMENT_WRAPPER_START "<ordered-by value=\"system\">"EXT_SUBELEM"</ordered-by>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002249 assert_int_equal(test_element_helper(st, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002250 assert_true(flags & LYS_ORDBY_SYSTEM);
David Sedláke0ef1c62019-09-13 10:05:55 +02002251 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002252 assert_int_equal(exts[0].insubstmt_index, 0);
2253 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ORDEREDBY);
2254 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedláka2dad212019-07-18 12:45:19 +02002255
2256 data = ELEMENT_WRAPPER_START "<ordered-by value=\"user\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002257 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002258 assert_true(flags & LYS_ORDBY_USER);
David Sedláka2dad212019-07-18 12:45:19 +02002259
2260 data = ELEMENT_WRAPPER_START "<ordered-by value=\"inv\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002261 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02002262 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 +02002263
2264 st->finished_correctly = true;
2265}
2266
David Sedlák8a83bbb2019-07-18 14:46:00 +02002267static void
2268test_any_elem(void **state)
2269{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002270 struct test_parser_yin_state *st = *state;
David Sedlák8a83bbb2019-07-18 14:46:00 +02002271 const char *data;
2272 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002273 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák8a83bbb2019-07-18 14:46:00 +02002274 struct lysp_node_anydata *parsed = NULL;
2275
2276 /* anyxml max subelems */
2277 data = ELEMENT_WRAPPER_START
2278 "<anyxml name=\"any-name\">"
2279 "<config value=\"true\" />"
2280 "<description><text>desc</text></description>"
2281 "<if-feature name=\"feature\" />"
2282 "<mandatory value=\"true\" />"
2283 "<must condition=\"must-cond\" />"
2284 "<reference><text>ref</text></reference>"
2285 "<status value=\"deprecated\"/>"
2286 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002287 EXT_SUBELEM
David Sedlák8a83bbb2019-07-18 14:46:00 +02002288 "</anyxml>"
2289 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002290 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002291 parsed = (struct lysp_node_anydata *)siblings;
2292 assert_null(parsed->parent);
2293 assert_int_equal(parsed->nodetype, LYS_ANYXML);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002294 assert_true(parsed->flags & LYS_CONFIG_W);
2295 assert_true(parsed->flags & LYS_MAND_TRUE);
2296 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002297 assert_null(parsed->next);
2298 assert_string_equal(parsed->name, "any-name");
2299 assert_string_equal(parsed->dsc, "desc");
2300 assert_string_equal(parsed->ref, "ref");
2301 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002302 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedláke0ef1c62019-09-13 10:05:55 +02002303 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002304 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2305 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002306 lysp_node_free(st->ctx, siblings);
2307 siblings = NULL;
2308
2309 /* anydata max subelems */
2310 data = ELEMENT_WRAPPER_START
2311 "<anydata name=\"any-name\">"
2312 "<config value=\"true\" />"
2313 "<description><text>desc</text></description>"
2314 "<if-feature name=\"feature\" />"
2315 "<mandatory value=\"true\" />"
2316 "<must condition=\"must-cond\" />"
2317 "<reference><text>ref</text></reference>"
2318 "<status value=\"deprecated\"/>"
2319 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002320 EXT_SUBELEM
David Sedlák8a83bbb2019-07-18 14:46:00 +02002321 "</anydata>"
2322 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002323 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002324 parsed = (struct lysp_node_anydata *)siblings;
2325 assert_null(parsed->parent);
2326 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002327 assert_true(parsed->flags & LYS_CONFIG_W);
2328 assert_true(parsed->flags & LYS_MAND_TRUE);
2329 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002330 assert_null(parsed->next);
2331 assert_string_equal(parsed->name, "any-name");
2332 assert_string_equal(parsed->dsc, "desc");
2333 assert_string_equal(parsed->ref, "ref");
2334 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002335 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedláke0ef1c62019-09-13 10:05:55 +02002336 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002337 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2338 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002339 lysp_node_free(st->ctx, siblings);
2340 siblings = NULL;
2341
2342 /* min subelems */
2343 node_meta.parent = (void *)0x10;
2344 data = ELEMENT_WRAPPER_START "<anydata name=\"any-name\"> </anydata>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002345 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002346 parsed = (struct lysp_node_anydata *)siblings;
2347 assert_ptr_equal(parsed->parent, node_meta.parent);
2348 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
2349 assert_null(parsed->next);
2350 assert_null(parsed->exts);
2351 lysp_node_free(st->ctx, siblings);
2352
2353 st->finished_correctly = true;
2354}
2355
David Sedlák203ca3a2019-07-18 15:26:25 +02002356static void
2357test_leaf_elem(void **state)
2358{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002359 struct test_parser_yin_state *st = *state;
David Sedlák203ca3a2019-07-18 15:26:25 +02002360 const char *data;
2361 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002362 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák203ca3a2019-07-18 15:26:25 +02002363 struct lysp_node_leaf *parsed = NULL;
2364
2365 /* max elements */
2366 data = ELEMENT_WRAPPER_START
2367 "<leaf name=\"leaf\">"
2368 "<config value=\"true\" />"
2369 "<default value=\"def-val\"/>"
2370 "<description><text>desc</text></description>"
2371 "<if-feature name=\"feature\" />"
2372 "<mandatory value=\"true\" />"
2373 "<must condition=\"must-cond\" />"
2374 "<reference><text>ref</text></reference>"
2375 "<status value=\"deprecated\"/>"
2376 "<type name=\"type\"/>"
2377 "<units name=\"uni\"/>"
2378 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002379 EXT_SUBELEM
David Sedlák203ca3a2019-07-18 15:26:25 +02002380 "</leaf>"
2381 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002382 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák203ca3a2019-07-18 15:26:25 +02002383 parsed = (struct lysp_node_leaf *)siblings;
2384 assert_null(parsed->parent);
2385 assert_int_equal(parsed->nodetype, LYS_LEAF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002386 assert_true(parsed->flags & LYS_CONFIG_W);
2387 assert_true(parsed->flags & LYS_MAND_TRUE);
2388 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák203ca3a2019-07-18 15:26:25 +02002389 assert_null(parsed->next);
2390 assert_string_equal(parsed->name, "leaf");
2391 assert_string_equal(parsed->dsc, "desc");
2392 assert_string_equal(parsed->ref, "ref");
2393 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002394 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedláke0ef1c62019-09-13 10:05:55 +02002395 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002396 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2397 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002398 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlák203ca3a2019-07-18 15:26:25 +02002399 assert_string_equal(parsed->type.name, "type");
2400 assert_string_equal(parsed->units, "uni");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002401 assert_string_equal(parsed->dflt.str, "def-val");
David Sedlák203ca3a2019-07-18 15:26:25 +02002402 lysp_node_free(st->ctx, siblings);
2403 siblings = NULL;
2404
2405 /* min elements */
2406 data = ELEMENT_WRAPPER_START "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002407 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák203ca3a2019-07-18 15:26:25 +02002408 parsed = (struct lysp_node_leaf *)siblings;
2409 assert_string_equal(parsed->name, "leaf");
2410 assert_string_equal(parsed->type.name, "type");
2411 lysp_node_free(st->ctx, siblings);
2412 siblings = NULL;
2413
2414 st->finished_correctly = true;
2415}
2416
David Sedlákc3da3ef2019-07-19 12:56:08 +02002417static void
2418test_leaf_list_elem(void **state)
2419{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002420 struct test_parser_yin_state *st = *state;
David Sedlákc3da3ef2019-07-19 12:56:08 +02002421 const char *data;
2422 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002423 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlákc3da3ef2019-07-19 12:56:08 +02002424 struct lysp_node_leaflist *parsed = NULL;
2425
2426 data = ELEMENT_WRAPPER_START
2427 "<leaf-list name=\"llist\">"
2428 "<config value=\"true\" />"
2429 "<default value=\"def-val0\"/>"
2430 "<default value=\"def-val1\"/>"
2431 "<description><text>desc</text></description>"
2432 "<if-feature name=\"feature\"/>"
2433 "<max-elements value=\"5\"/>"
2434 "<must condition=\"must-cond\"/>"
2435 "<ordered-by value=\"user\" />"
2436 "<reference><text>ref</text></reference>"
2437 "<status value=\"current\"/>"
2438 "<type name=\"type\"/>"
2439 "<units name=\"uni\"/>"
2440 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002441 EXT_SUBELEM
David Sedlákc3da3ef2019-07-19 12:56:08 +02002442 "</leaf-list>"
2443 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002444 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002445 parsed = (struct lysp_node_leaflist *)siblings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02002446 assert_string_equal(parsed->dflts[0].str, "def-val0");
2447 assert_string_equal(parsed->dflts[1].str, "def-val1");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002448 assert_string_equal(parsed->dsc, "desc");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002449 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002450 assert_int_equal(parsed->max, 5);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002451 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002452 assert_string_equal(parsed->name, "llist");
2453 assert_null(parsed->next);
2454 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2455 assert_null(parsed->parent);
2456 assert_string_equal(parsed->ref, "ref");
2457 assert_string_equal(parsed->type.name, "type");
2458 assert_string_equal(parsed->units, "uni");
2459 assert_string_equal(parsed->when->cond, "when-cond");
2460 assert_true(parsed->flags & LYS_CONFIG_W);
2461 assert_true(parsed->flags & LYS_ORDBY_USER);
2462 assert_true(parsed->flags & LYS_STATUS_CURR);
David Sedláke0ef1c62019-09-13 10:05:55 +02002463 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002464 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2465 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002466 lysp_node_free(st->ctx, siblings);
2467 siblings = NULL;
2468
2469 data = ELEMENT_WRAPPER_START
2470 "<leaf-list name=\"llist\">"
2471 "<config value=\"true\" />"
2472 "<description><text>desc</text></description>"
2473 "<if-feature name=\"feature\"/>"
2474 "<min-elements value=\"5\"/>"
2475 "<must condition=\"must-cond\"/>"
2476 "<ordered-by value=\"user\" />"
2477 "<reference><text>ref</text></reference>"
2478 "<status value=\"current\"/>"
2479 "<type name=\"type\"/>"
2480 "<units name=\"uni\"/>"
2481 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002482 EXT_SUBELEM
David Sedlákc3da3ef2019-07-19 12:56:08 +02002483 "</leaf-list>"
2484 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002485 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002486 parsed = (struct lysp_node_leaflist *)siblings;
2487 assert_string_equal(parsed->dsc, "desc");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002488 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002489 assert_int_equal(parsed->min, 5);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002490 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002491 assert_string_equal(parsed->name, "llist");
2492 assert_null(parsed->next);
2493 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2494 assert_null(parsed->parent);
2495 assert_string_equal(parsed->ref, "ref");
2496 assert_string_equal(parsed->type.name, "type");
2497 assert_string_equal(parsed->units, "uni");
2498 assert_string_equal(parsed->when->cond, "when-cond");
2499 assert_true(parsed->flags & LYS_CONFIG_W);
2500 assert_true(parsed->flags & LYS_ORDBY_USER);
2501 assert_true(parsed->flags & LYS_STATUS_CURR);
David Sedláke0ef1c62019-09-13 10:05:55 +02002502 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002503 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2504 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002505 lysp_node_free(st->ctx, siblings);
2506 siblings = NULL;
2507
2508 data = ELEMENT_WRAPPER_START
2509 "<leaf-list name=\"llist\">"
2510 "<config value=\"true\" />"
2511 "<description><text>desc</text></description>"
2512 "<if-feature name=\"feature\"/>"
2513 "<max-elements value=\"15\"/>"
2514 "<min-elements value=\"5\"/>"
2515 "<must condition=\"must-cond\"/>"
2516 "<ordered-by value=\"user\" />"
2517 "<reference><text>ref</text></reference>"
2518 "<status value=\"current\"/>"
2519 "<type name=\"type\"/>"
2520 "<units name=\"uni\"/>"
2521 "<when condition=\"when-cond\"/>"
2522 "</leaf-list>"
2523 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002524 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002525 parsed = (struct lysp_node_leaflist *)siblings;
2526 assert_string_equal(parsed->dsc, "desc");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002527 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002528 assert_int_equal(parsed->min, 5);
2529 assert_int_equal(parsed->max, 15);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002530 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002531 assert_string_equal(parsed->name, "llist");
2532 assert_null(parsed->next);
2533 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2534 assert_null(parsed->parent);
2535 assert_string_equal(parsed->ref, "ref");
2536 assert_string_equal(parsed->type.name, "type");
2537 assert_string_equal(parsed->units, "uni");
2538 assert_string_equal(parsed->when->cond, "when-cond");
2539 assert_true(parsed->flags & LYS_CONFIG_W);
2540 assert_true(parsed->flags & LYS_ORDBY_USER);
2541 assert_true(parsed->flags & LYS_STATUS_CURR);
2542 lysp_node_free(st->ctx, siblings);
2543 siblings = NULL;
2544
2545 data = ELEMENT_WRAPPER_START
2546 "<leaf-list name=\"llist\">"
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_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002551 parsed = (struct lysp_node_leaflist *)siblings;
2552 assert_string_equal(parsed->name, "llist");
2553 assert_string_equal(parsed->type.name, "type");
2554 lysp_node_free(st->ctx, siblings);
2555 siblings = NULL;
2556
2557 /* invalid combinations */
2558 data = ELEMENT_WRAPPER_START
2559 "<leaf-list name=\"llist\">"
2560 "<max-elements value=\"5\"/>"
2561 "<min-elements value=\"15\"/>"
2562 "<type name=\"type\"/>"
2563 "</leaf-list>"
2564 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002565 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_EVALID);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002566 logbuf_assert("Invalid combination of min-elements and max-elements: min value 15 is bigger than the max value 5. Line number 1.");
2567 lysp_node_free(st->ctx, siblings);
2568 siblings = NULL;
2569
2570 data = ELEMENT_WRAPPER_START
2571 "<leaf-list name=\"llist\">"
2572 "<default value=\"def-val1\"/>"
2573 "<min-elements value=\"15\"/>"
2574 "<type name=\"type\"/>"
2575 "</leaf-list>"
2576 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002577 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002578 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 +02002579 lysp_node_free(st->ctx, siblings);
2580 siblings = NULL;
2581
2582 data = ELEMENT_WRAPPER_START
2583 "<leaf-list name=\"llist\">"
2584 "</leaf-list>"
2585 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002586 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002587 logbuf_assert("Missing mandatory sub-element \"type\" of \"leaf-list\" element. Line number 1.");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002588 lysp_node_free(st->ctx, siblings);
2589 siblings = NULL;
2590
2591 st->finished_correctly = true;
2592}
2593
David Sedlákcb39f642019-07-19 13:19:55 +02002594static void
2595test_presence_elem(void **state)
2596{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002597 struct test_parser_yin_state *st = *state;
David Sedlákcb39f642019-07-19 13:19:55 +02002598 const char *data;
2599 const char *val;
David Sedlákd1144562019-08-06 12:36:14 +02002600 struct lysp_ext_instance *exts = NULL;
2601
2602 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\">"EXT_SUBELEM"</presence>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002603 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02002604 assert_string_equal(val, "presence-val");
David Sedláke0ef1c62019-09-13 10:05:55 +02002605 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002606 assert_int_equal(exts[0].insubstmt_index, 0);
2607 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_PRESENCE);
2608 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
2609 FREE_STRING(st->ctx, val);
David Sedlákcb39f642019-07-19 13:19:55 +02002610
2611 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002612 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlákcb39f642019-07-19 13:19:55 +02002613 assert_string_equal(val, "presence-val");
2614 FREE_STRING(st->ctx, val);
2615
2616 data = ELEMENT_WRAPPER_START "<presence/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002617 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlákcb39f642019-07-19 13:19:55 +02002618 logbuf_assert("Missing mandatory attribute value of presence element. Line number 1.");
2619
2620 st->finished_correctly = true;
2621}
2622
David Sedlák12470a82019-07-19 13:44:36 +02002623static void
2624test_key_elem(void **state)
2625{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002626 struct test_parser_yin_state *st = *state;
David Sedlák12470a82019-07-19 13:44:36 +02002627 const char *data;
2628 const char *val;
David Sedlákd1144562019-08-06 12:36:14 +02002629 struct lysp_ext_instance *exts = NULL;
2630
2631 data = ELEMENT_WRAPPER_START "<key value=\"key-value\">"EXT_SUBELEM"</key>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002632 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02002633 assert_string_equal(val, "key-value");
David Sedláke0ef1c62019-09-13 10:05:55 +02002634 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002635 assert_int_equal(exts[0].insubstmt_index, 0);
2636 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_KEY);
2637 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
2638 FREE_STRING(st->ctx, val);
David Sedlák12470a82019-07-19 13:44:36 +02002639
2640 data = ELEMENT_WRAPPER_START "<key value=\"key-value\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002641 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák12470a82019-07-19 13:44:36 +02002642 assert_string_equal(val, "key-value");
2643 FREE_STRING(st->ctx, val);
2644
2645 data = ELEMENT_WRAPPER_START "<key/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002646 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák12470a82019-07-19 13:44:36 +02002647 logbuf_assert("Missing mandatory attribute value of key element. Line number 1.");
2648
2649 st->finished_correctly = true;
2650}
2651
David Sedlák04e17b22019-07-19 15:29:48 +02002652static void
2653test_typedef_elem(void **state)
2654{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002655 struct test_parser_yin_state *st = *state;
David Sedlák04e17b22019-07-19 15:29:48 +02002656 const char *data;
2657 struct lysp_tpdf *tpdfs = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002658 struct tree_node_meta typdef_meta = {NULL, (struct lysp_node **)&tpdfs};
David Sedlák04e17b22019-07-19 15:29:48 +02002659
2660 data = ELEMENT_WRAPPER_START
2661 "<typedef name=\"tpdf-name\">"
2662 "<default value=\"def-val\"/>"
2663 "<description><text>desc-text</text></description>"
2664 "<reference><text>ref-text</text></reference>"
2665 "<status value=\"current\"/>"
2666 "<type name=\"type\"/>"
2667 "<units name=\"uni\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002668 EXT_SUBELEM
David Sedlák04e17b22019-07-19 15:29:48 +02002669 "</typedef>"
2670 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002671 assert_int_equal(test_element_helper(st, data, &typdef_meta, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002672 assert_string_equal(tpdfs[0].dflt.str, "def-val");
David Sedlák04e17b22019-07-19 15:29:48 +02002673 assert_string_equal(tpdfs[0].dsc, "desc-text");
David Sedlák04e17b22019-07-19 15:29:48 +02002674 assert_string_equal(tpdfs[0].name, "tpdf-name");
2675 assert_string_equal(tpdfs[0].ref, "ref-text");
2676 assert_string_equal(tpdfs[0].type.name, "type");
2677 assert_string_equal(tpdfs[0].units, "uni");
2678 assert_true(tpdfs[0].flags & LYS_STATUS_CURR);
David Sedláke0ef1c62019-09-13 10:05:55 +02002679 assert_string_equal(tpdfs[0].exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002680 assert_int_equal(tpdfs[0].exts[0].insubstmt_index, 0);
2681 assert_int_equal(tpdfs[0].exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák04e17b22019-07-19 15:29:48 +02002682 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2683 tpdfs = NULL;
2684
2685 data = ELEMENT_WRAPPER_START
2686 "<typedef name=\"tpdf-name\">"
2687 "<type name=\"type\"/>"
2688 "</typedef>"
2689 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002690 assert_int_equal(test_element_helper(st, data, &typdef_meta, NULL, NULL), LY_SUCCESS);
David Sedlák04e17b22019-07-19 15:29:48 +02002691 assert_string_equal(tpdfs[0].name, "tpdf-name");
2692 assert_string_equal(tpdfs[0].type.name, "type");
2693 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2694 tpdfs = NULL;
2695
2696 st->finished_correctly = true;
2697}
2698
David Sedlákd2d676a2019-07-22 11:28:19 +02002699static void
2700test_refine_elem(void **state)
2701{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002702 struct test_parser_yin_state *st = *state;
David Sedlákd2d676a2019-07-22 11:28:19 +02002703 const char *data;
2704 struct lysp_refine *refines = NULL;
2705
2706 /* max subelems */
2707 data = ELEMENT_WRAPPER_START
2708 "<refine target-node=\"target\">"
2709 "<if-feature name=\"feature\" />"
2710 "<must condition=\"cond\" />"
2711 "<presence value=\"presence\" />"
2712 "<default value=\"def\" />"
2713 "<config value=\"true\" />"
2714 "<mandatory value=\"true\" />"
2715 "<min-elements value=\"10\" />"
2716 "<max-elements value=\"20\" />"
2717 "<description><text>desc</text></description>"
2718 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02002719 EXT_SUBELEM
David Sedlákd2d676a2019-07-22 11:28:19 +02002720 "</refine>"
2721 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002722 assert_int_equal(test_element_helper(st, data, &refines, NULL, NULL), LY_SUCCESS);
David Sedlákd2d676a2019-07-22 11:28:19 +02002723 assert_string_equal(refines->nodeid, "target");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002724 assert_string_equal(refines->dflts[0].str, "def");
David Sedlákd2d676a2019-07-22 11:28:19 +02002725 assert_string_equal(refines->dsc, "desc");
David Sedlákd2d676a2019-07-22 11:28:19 +02002726 assert_true(refines->flags & LYS_CONFIG_W);
2727 assert_true(refines->flags & LYS_MAND_TRUE);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002728 assert_string_equal(refines->iffeatures[0].str, "feature");
David Sedlákd2d676a2019-07-22 11:28:19 +02002729 assert_int_equal(refines->max, 20);
2730 assert_int_equal(refines->min, 10);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002731 assert_string_equal(refines->musts->arg.str, "cond");
David Sedlákd2d676a2019-07-22 11:28:19 +02002732 assert_string_equal(refines->presence, "presence");
2733 assert_string_equal(refines->ref, "ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02002734 assert_string_equal(refines->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002735 assert_int_equal(refines->exts[0].insubstmt_index, 0);
2736 assert_int_equal(refines->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákd2d676a2019-07-22 11:28:19 +02002737 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2738 refines = NULL;
2739
2740 /* min subelems */
2741 data = ELEMENT_WRAPPER_START "<refine target-node=\"target\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002742 assert_int_equal(test_element_helper(st, data, &refines, NULL, NULL), LY_SUCCESS);
David Sedlákd2d676a2019-07-22 11:28:19 +02002743 assert_string_equal(refines->nodeid, "target");
2744 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2745 refines = NULL;
2746
2747 st->finished_correctly = true;
2748}
2749
David Sedlák0d6de5a2019-07-22 13:25:44 +02002750static void
2751test_uses_elem(void **state)
2752{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002753 struct test_parser_yin_state *st = *state;
David Sedlák0d6de5a2019-07-22 13:25:44 +02002754 const char *data;
2755 struct lysp_node *siblings = NULL;
2756 struct tree_node_meta node_meta = {NULL, &siblings};
2757 struct lysp_node_uses *parsed = NULL;
2758
2759 /* max subelems */
2760 data = ELEMENT_WRAPPER_START
2761 "<uses name=\"uses-name\">"
2762 "<when condition=\"cond\" />"
2763 "<if-feature name=\"feature\" />"
2764 "<status value=\"obsolete\" />"
2765 "<description><text>desc</text></description>"
2766 "<reference><text>ref</text></reference>"
2767 "<refine target-node=\"target\"/>"
David Sedlák992fb7c2019-07-24 16:51:01 +02002768 "<augment target-node=\"target\" />"
David Sedlákd1144562019-08-06 12:36:14 +02002769 EXT_SUBELEM
David Sedlák0d6de5a2019-07-22 13:25:44 +02002770 "</uses>"
2771 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002772 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002773 parsed = (struct lysp_node_uses *)&siblings[0];
2774 assert_string_equal(parsed->name, "uses-name");
2775 assert_string_equal(parsed->dsc, "desc");
David Sedlák0d6de5a2019-07-22 13:25:44 +02002776 assert_true(parsed->flags & LYS_STATUS_OBSLT);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002777 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlák0d6de5a2019-07-22 13:25:44 +02002778 assert_null(parsed->next);
2779 assert_int_equal(parsed->nodetype, LYS_USES);
2780 assert_null(parsed->parent);
2781 assert_string_equal(parsed->ref, "ref");
2782 assert_string_equal(parsed->refines->nodeid, "target");
2783 assert_string_equal(parsed->when->cond, "cond");
David Sedlák992fb7c2019-07-24 16:51:01 +02002784 assert_string_equal(parsed->augments->nodeid, "target");
David Sedláke0ef1c62019-09-13 10:05:55 +02002785 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002786 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2787 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002788 lysp_node_free(st->ctx, siblings);
2789 siblings = NULL;
2790
2791 /* min subelems */
2792 data = ELEMENT_WRAPPER_START "<uses name=\"uses-name\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002793 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002794 assert_string_equal(siblings[0].name, "uses-name");
2795 lysp_node_free(st->ctx, siblings);
2796 siblings = NULL;
2797
2798 st->finished_correctly = true;
2799}
2800
David Sedlákaa854b02019-07-22 14:17:10 +02002801static void
2802test_revision_elem(void **state)
2803{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002804 struct test_parser_yin_state *st = *state;
David Sedlákaa854b02019-07-22 14:17:10 +02002805 const char *data;
2806 struct lysp_revision *revs = NULL;
2807
2808 /* max subelems */
2809 data = ELEMENT_WRAPPER_START
2810 "<revision date=\"2018-12-25\">"
2811 "<description><text>desc</text></description>"
2812 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02002813 EXT_SUBELEM
David Sedlákaa854b02019-07-22 14:17:10 +02002814 "</revision>"
2815 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002816 assert_int_equal(test_element_helper(st, data, &revs, NULL, NULL), LY_SUCCESS);
David Sedlákaa854b02019-07-22 14:17:10 +02002817 assert_string_equal(revs->date, "2018-12-25");
2818 assert_string_equal(revs->dsc, "desc");
2819 assert_string_equal(revs->ref, "ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02002820 assert_string_equal(revs->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002821 assert_int_equal(revs->exts[0].insubstmt_index, 0);
2822 assert_int_equal(revs->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákaa854b02019-07-22 14:17:10 +02002823 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2824 revs = NULL;
2825
2826 /* min subelems */
2827 data = ELEMENT_WRAPPER_START "<revision date=\"2005-05-05\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002828 assert_int_equal(test_element_helper(st, data, &revs, NULL, NULL), LY_SUCCESS);
David Sedlákaa854b02019-07-22 14:17:10 +02002829 assert_string_equal(revs->date, "2005-05-05");
2830 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2831 revs = NULL;
2832
2833 /* invalid value */
2834 data = ELEMENT_WRAPPER_START "<revision date=\"05-05-2005\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002835 assert_int_equal(test_element_helper(st, data, &revs, NULL, NULL), LY_EVALID);
David Sedlákaa854b02019-07-22 14:17:10 +02002836 logbuf_assert("Invalid value \"05-05-2005\" of \"revision\". Line number 1.");
2837 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2838 revs = NULL;
2839
2840 st->finished_correctly = true;
2841}
2842
David Sedlák0c2bab92019-07-22 15:33:19 +02002843static void
2844test_include_elem(void **state)
2845{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002846 struct test_parser_yin_state *st = *state;
David Sedlák0c2bab92019-07-22 15:33:19 +02002847 const char *data;
2848 struct lysp_include *includes = NULL;
2849 struct include_meta inc_meta = {"module-name", &includes};
2850
2851 /* max subelems */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002852 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák0c2bab92019-07-22 15:33:19 +02002853 data = ELEMENT_WRAPPER_START
2854 "<include module=\"mod\">"
2855 "<description><text>desc</text></description>"
2856 "<reference><text>ref</text></reference>"
2857 "<revision-date date=\"1999-09-09\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002858 EXT_SUBELEM
David Sedlák0c2bab92019-07-22 15:33:19 +02002859 "</include>"
2860 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002861 assert_int_equal(test_element_helper(st, data, &inc_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0c2bab92019-07-22 15:33:19 +02002862 assert_string_equal(includes->name, "mod");
2863 assert_string_equal(includes->dsc, "desc");
2864 assert_string_equal(includes->ref, "ref");
David Sedlák0c2bab92019-07-22 15:33:19 +02002865 assert_string_equal(includes->rev, "1999-09-09");
David Sedláke0ef1c62019-09-13 10:05:55 +02002866 assert_string_equal(includes->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002867 assert_int_equal(includes->exts[0].insubstmt_index, 0);
2868 assert_int_equal(includes->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák0c2bab92019-07-22 15:33:19 +02002869 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2870 includes = NULL;
2871
2872 /* min subelems */
2873 data = ELEMENT_WRAPPER_START "<include module=\"mod\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002874 assert_int_equal(test_element_helper(st, data, &inc_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0c2bab92019-07-22 15:33:19 +02002875 assert_string_equal(includes->name, "mod");
2876 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2877 includes = NULL;
2878
2879 /* invalid combinations */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002880 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_0;
David Sedlák0c2bab92019-07-22 15:33:19 +02002881 data = ELEMENT_WRAPPER_START
2882 "<include module=\"mod\">"
2883 "<description><text>desc</text></description>"
2884 "<revision-date date=\"1999-09-09\"/>"
2885 "</include>"
2886 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002887 assert_int_equal(test_element_helper(st, data, &inc_meta, NULL, NULL), LY_EVALID);
David Sedlák0c2bab92019-07-22 15:33:19 +02002888 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.");
2889 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2890 includes = NULL;
2891
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002892 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_0;
David Sedlák0c2bab92019-07-22 15:33:19 +02002893 data = ELEMENT_WRAPPER_START
2894 "<include module=\"mod\">"
2895 "<reference><text>ref</text></reference>"
2896 "<revision-date date=\"1999-09-09\"/>"
2897 "</include>"
2898 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002899 assert_int_equal(test_element_helper(st, data, &inc_meta, NULL, NULL), LY_EVALID);
David Sedlák0c2bab92019-07-22 15:33:19 +02002900 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.");
2901 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2902 includes = NULL;
2903
2904 st->finished_correctly = true;
2905}
2906
David Sedlák5e13dea2019-07-22 16:06:45 +02002907static void
David Sedlákaf536aa2019-07-23 13:42:23 +02002908test_list_elem(void **state)
2909{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002910 struct test_parser_yin_state *st = *state;
David Sedlákaf536aa2019-07-23 13:42:23 +02002911 const char *data;
2912 struct lysp_node *siblings = NULL;
2913 struct tree_node_meta node_meta = {NULL, &siblings};
2914 struct lysp_node_list *parsed = NULL;
2915
2916 /* max subelems */
2917 data = ELEMENT_WRAPPER_START
2918 "<list name=\"list-name\">"
2919 "<when condition=\"when\"/>"
2920 "<if-feature name=\"iff\"/>"
2921 "<must condition=\"must-cond\"/>"
2922 "<key value=\"key\"/>"
2923 "<unique tag=\"utag\"/>"
2924 "<config value=\"true\"/>"
2925 "<min-elements value=\"10\"/>"
2926 "<ordered-by value=\"user\"/>"
2927 "<status value=\"deprecated\"/>"
2928 "<description><text>desc</text></description>"
2929 "<reference><text>ref</text></reference>"
2930 "<anydata name=\"anyd\"/>"
2931 "<anyxml name=\"anyx\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02002932 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002933 "<choice name=\"choice\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02002934 "<action name=\"action\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02002935 "<grouping name=\"grp\"/>"
David Sedlák031b9e72019-07-23 15:19:37 +02002936 "<notification name=\"notf\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002937 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
2938 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlákaf536aa2019-07-23 13:42:23 +02002939 "<list name=\"sub-list\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002940 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákaf536aa2019-07-23 13:42:23 +02002941 "<uses name=\"uses-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002942 EXT_SUBELEM
David Sedlákaf536aa2019-07-23 13:42:23 +02002943 "</list>"
2944 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002945 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákaf536aa2019-07-23 13:42:23 +02002946 parsed = (struct lysp_node_list *)&siblings[0];
2947 assert_string_equal(parsed->dsc, "desc");
2948 assert_string_equal(parsed->child->name, "anyd");
2949 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2950 assert_string_equal(parsed->child->next->name, "anyx");
2951 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
David Sedlákf111bcb2019-07-23 17:15:51 +02002952 assert_string_equal(parsed->child->next->next->name, "cont");
2953 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002954 assert_string_equal(parsed->child->next->next->next->name, "choice");
2955 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
David Sedlák85d0eca2019-07-24 15:15:21 +02002956 assert_string_equal(parsed->child->next->next->next->next->name, "leaf");
2957 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAF);
2958 assert_string_equal(parsed->child->next->next->next->next->next->name, "llist");
2959 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2960 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "sub-list");
2961 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LIST);
2962 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "uses-name");
2963 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_USES);
2964 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002965 assert_string_equal(parsed->groupings->name, "grp");
David Sedlák85d0eca2019-07-24 15:15:21 +02002966 assert_string_equal(parsed->actions->name, "action");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002967 assert_int_equal(parsed->groupings->nodetype, LYS_GROUPING);
David Sedlák031b9e72019-07-23 15:19:37 +02002968 assert_string_equal(parsed->notifs->name, "notf");
David Sedlákaf536aa2019-07-23 13:42:23 +02002969 assert_true(parsed->flags & LYS_ORDBY_USER);
2970 assert_true(parsed->flags & LYS_STATUS_DEPRC);
2971 assert_true(parsed->flags & LYS_CONFIG_W);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002972 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlákaf536aa2019-07-23 13:42:23 +02002973 assert_string_equal(parsed->key, "key");
2974 assert_int_equal(parsed->min, 10);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002975 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlákaf536aa2019-07-23 13:42:23 +02002976 assert_string_equal(parsed->name, "list-name");
2977 assert_null(parsed->next);
2978 assert_int_equal(parsed->nodetype, LYS_LIST);
2979 assert_null(parsed->parent);
2980 assert_string_equal(parsed->ref, "ref");
2981 assert_string_equal(parsed->typedefs->name, "tpdf");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002982 assert_string_equal(parsed->uniques->str, "utag");
David Sedlákaf536aa2019-07-23 13:42:23 +02002983 assert_string_equal(parsed->when->cond, "when");
David Sedláke0ef1c62019-09-13 10:05:55 +02002984 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002985 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2986 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákaf536aa2019-07-23 13:42:23 +02002987 lysp_node_free(st->ctx, siblings);
2988 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
2989 siblings = NULL;
2990
2991 /* min subelems */
2992 data = ELEMENT_WRAPPER_START "<list name=\"list-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002993 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákaf536aa2019-07-23 13:42:23 +02002994 parsed = (struct lysp_node_list *)&siblings[0];
2995 assert_string_equal(parsed->name, "list-name");
2996 lysp_node_free(st->ctx, siblings);
2997 siblings = NULL;
2998
2999 st->finished_correctly = true;
3000}
3001
David Sedlák031b9e72019-07-23 15:19:37 +02003002static void
3003test_notification_elem(void **state)
3004{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003005 struct test_parser_yin_state *st = *state;
David Sedlák031b9e72019-07-23 15:19:37 +02003006 const char *data;
3007 struct lysp_notif *notifs = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003008 struct tree_node_meta notif_meta = {NULL, (struct lysp_node **)&notifs};
David Sedlák031b9e72019-07-23 15:19:37 +02003009
3010 /* max subelems */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003011 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák031b9e72019-07-23 15:19:37 +02003012 data = ELEMENT_WRAPPER_START
3013 "<notification name=\"notif-name\">"
3014 "<anydata name=\"anyd\"/>"
3015 "<anyxml name=\"anyx\"/>"
3016 "<description><text>desc</text></description>"
3017 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003018 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3019 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák031b9e72019-07-23 15:19:37 +02003020 "<list name=\"sub-list\"/>"
3021 "<must condition=\"cond\"/>"
3022 "<reference><text>ref</text></reference>"
3023 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003024 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák031b9e72019-07-23 15:19:37 +02003025 "<uses name=\"uses-name\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02003026 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003027 "<choice name=\"choice\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02003028 "<grouping name=\"grp\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003029 EXT_SUBELEM
David Sedlák031b9e72019-07-23 15:19:37 +02003030 "</notification>"
3031 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003032 assert_int_equal(test_element_helper(st, data, &notif_meta, NULL, NULL), LY_SUCCESS);
David Sedlák031b9e72019-07-23 15:19:37 +02003033 assert_string_equal(notifs->name, "notif-name");
3034 assert_string_equal(notifs->data->name, "anyd");
3035 assert_int_equal(notifs->data->nodetype, LYS_ANYDATA);
3036 assert_string_equal(notifs->data->next->name, "anyx");
3037 assert_int_equal(notifs->data->next->nodetype, LYS_ANYXML);
3038 assert_string_equal(notifs->data->next->next->name, "leaf");
3039 assert_int_equal(notifs->data->next->next->nodetype, LYS_LEAF);
3040 assert_string_equal(notifs->data->next->next->next->name, "llist");
3041 assert_int_equal(notifs->data->next->next->next->nodetype, LYS_LEAFLIST);
3042 assert_string_equal(notifs->data->next->next->next->next->name, "sub-list");
3043 assert_int_equal(notifs->data->next->next->next->next->nodetype, LYS_LIST);
David Sedlák031b9e72019-07-23 15:19:37 +02003044 assert_true(notifs->flags & LYS_STATUS_DEPRC);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003045 assert_string_equal(notifs->groupings->name, "grp");
3046 assert_int_equal(notifs->groupings->nodetype, LYS_GROUPING);
David Sedlákf111bcb2019-07-23 17:15:51 +02003047 assert_string_equal(notifs->data->next->next->next->next->next->name, "uses-name");
3048 assert_int_equal(notifs->data->next->next->next->next->next->nodetype, LYS_USES);
3049 assert_string_equal(notifs->data->next->next->next->next->next->next->name, "cont");
3050 assert_int_equal(notifs->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003051 assert_int_equal(notifs->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
3052 assert_string_equal(notifs->data->next->next->next->next->next->next->next->name, "choice");
3053 assert_null(notifs->data->next->next->next->next->next->next->next->next);
Michal Vasko7f45cf22020-10-01 12:49:44 +02003054 assert_string_equal(notifs->iffeatures[0].str, "iff");
3055 assert_string_equal(notifs->musts->arg.str, "cond");
David Sedlák031b9e72019-07-23 15:19:37 +02003056 assert_int_equal(notifs->nodetype, LYS_NOTIF);
3057 assert_null(notifs->parent);
3058 assert_string_equal(notifs->ref, "ref");
3059 assert_string_equal(notifs->typedefs->name, "tpdf");
David Sedláke0ef1c62019-09-13 10:05:55 +02003060 assert_string_equal(notifs->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003061 assert_int_equal(notifs->exts[0].insubstmt_index, 0);
3062 assert_int_equal(notifs->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák031b9e72019-07-23 15:19:37 +02003063 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
3064 notifs = NULL;
3065
3066 /* min subelems */
3067 data = ELEMENT_WRAPPER_START "<notification name=\"notif-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003068 assert_int_equal(test_element_helper(st, data, &notif_meta, NULL, NULL), LY_SUCCESS);
David Sedlák031b9e72019-07-23 15:19:37 +02003069 assert_string_equal(notifs->name, "notif-name");
3070 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003071 notifs = NULL;
3072
3073 st->finished_correctly = true;
3074}
3075
3076static void
3077test_grouping_elem(void **state)
3078{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003079 struct test_parser_yin_state *st = *state;
David Sedláke3ce9ef2019-07-23 16:34:30 +02003080 const char *data;
3081 struct lysp_grp *grps = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003082 struct tree_node_meta grp_meta = {NULL, (struct lysp_node **)&grps};
David Sedláke3ce9ef2019-07-23 16:34:30 +02003083
3084 /* max subelems */
3085 data = ELEMENT_WRAPPER_START
3086 "<grouping name=\"grp-name\">"
3087 "<anydata name=\"anyd\"/>"
3088 "<anyxml name=\"anyx\"/>"
3089 "<description><text>desc</text></description>"
3090 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003091 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3092 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02003093 "<list name=\"list\"/>"
3094 "<notification name=\"notf\"/>"
3095 "<reference><text>ref</text></reference>"
3096 "<status value=\"current\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003097 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02003098 "<uses name=\"uses-name\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02003099 "<action name=\"act\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02003100 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003101 "<choice name=\"choice\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003102 EXT_SUBELEM
David Sedláke3ce9ef2019-07-23 16:34:30 +02003103 "</grouping>"
3104 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003105 assert_int_equal(test_element_helper(st, data, &grp_meta, NULL, NULL), LY_SUCCESS);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003106 assert_string_equal(grps->name, "grp-name");
David Sedláke3ce9ef2019-07-23 16:34:30 +02003107 assert_string_equal(grps->data->name, "anyd");
3108 assert_string_equal(grps->data->next->name, "anyx");
3109 assert_string_equal(grps->data->next->next->name, "leaf");
3110 assert_string_equal(grps->data->next->next->next->name, "llist");
3111 assert_string_equal(grps->data->next->next->next->next->name, "list");
3112 assert_string_equal(grps->dsc, "desc");
David Sedláke3ce9ef2019-07-23 16:34:30 +02003113 assert_true(grps->flags & LYS_STATUS_CURR);
3114 assert_string_equal(grps->groupings->name, "sub-grp");
3115 assert_int_equal(grps->nodetype, LYS_GROUPING);
3116 assert_string_equal(grps->notifs->name, "notf");
3117 assert_null(grps->parent);
3118 assert_string_equal(grps->ref, "ref");
3119 assert_string_equal(grps->typedefs->name, "tpdf");
David Sedlák85d0eca2019-07-24 15:15:21 +02003120 assert_string_equal(grps->actions->name, "act");
David Sedlákf111bcb2019-07-23 17:15:51 +02003121 assert_string_equal(grps->data->next->next->next->next->next->name, "uses-name");
David Sedlákb7abcfa2019-07-24 12:33:35 +02003122 assert_int_equal(grps->data->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákf111bcb2019-07-23 17:15:51 +02003123 assert_string_equal(grps->data->next->next->next->next->next->next->name, "cont");
David Sedlákb7abcfa2019-07-24 12:33:35 +02003124 assert_int_equal(grps->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
3125 assert_string_equal(grps->data->next->next->next->next->next->next->next->name, "choice");
3126 assert_int_equal(grps->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
David Sedláke0ef1c62019-09-13 10:05:55 +02003127 assert_string_equal(grps->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003128 assert_int_equal(grps->exts[0].insubstmt_index, 0);
3129 assert_int_equal(grps->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003130 FREE_ARRAY(st->ctx, grps, lysp_grp_free);
3131 grps = NULL;
3132
3133 /* min subelems */
3134 data = ELEMENT_WRAPPER_START "<grouping name=\"grp-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003135 assert_int_equal(test_element_helper(st, data, &grp_meta, NULL, NULL), LY_SUCCESS);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003136 assert_string_equal(grps->name, "grp-name");
3137 FREE_ARRAY(st->ctx, grps, lysp_grp_free);
3138 grps = NULL;
David Sedlák031b9e72019-07-23 15:19:37 +02003139
3140 st->finished_correctly = true;
3141}
3142
David Sedlákf111bcb2019-07-23 17:15:51 +02003143static void
3144test_container_elem(void **state)
3145{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003146 struct test_parser_yin_state *st = *state;
David Sedlákf111bcb2019-07-23 17:15:51 +02003147 const char *data;
3148 struct lysp_node *siblings = NULL;
3149 struct tree_node_meta node_meta = {NULL, &siblings};
3150 struct lysp_node_container *parsed = NULL;
3151
3152 /* max subelems */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003153 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedláke2dc9e92019-07-24 09:59:21 +02003154 data = ELEMENT_WRAPPER_START
3155 "<container name=\"cont-name\">"
3156 "<anydata name=\"anyd\"/>"
3157 "<anyxml name=\"anyx\"/>"
3158 "<config value=\"true\"/>"
3159 "<container name=\"subcont\"/>"
3160 "<description><text>desc</text></description>"
3161 "<grouping name=\"sub-grp\"/>"
3162 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003163 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3164 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedláke2dc9e92019-07-24 09:59:21 +02003165 "<list name=\"list\"/>"
3166 "<must condition=\"cond\"/>"
3167 "<notification name=\"notf\"/>"
3168 "<presence value=\"presence\"/>"
3169 "<reference><text>ref</text></reference>"
3170 "<status value=\"current\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003171 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedláke2dc9e92019-07-24 09:59:21 +02003172 "<uses name=\"uses-name\"/>"
3173 "<when condition=\"when-cond\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02003174 "<action name=\"act\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003175 "<choice name=\"choice\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003176 EXT_SUBELEM
David Sedláke2dc9e92019-07-24 09:59:21 +02003177 "</container>"
3178 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003179 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedláke2dc9e92019-07-24 09:59:21 +02003180 parsed = (struct lysp_node_container *)siblings;
3181 assert_string_equal(parsed->name, "cont-name");
3182 assert_null(parsed->parent);
3183 assert_int_equal(parsed->nodetype, LYS_CONTAINER);
3184 assert_true(parsed->flags & LYS_CONFIG_W);
3185 assert_true(parsed->flags & LYS_STATUS_CURR);
3186 assert_null(parsed->next);
3187 assert_string_equal(parsed->dsc, "desc");
3188 assert_string_equal(parsed->ref, "ref");
3189 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003190 assert_string_equal(parsed->iffeatures[0].str, "iff");
3191 assert_string_equal(parsed->musts->arg.str, "cond");
David Sedláke2dc9e92019-07-24 09:59:21 +02003192 assert_string_equal(parsed->presence, "presence");
3193 assert_string_equal(parsed->typedefs->name, "tpdf");
3194 assert_string_equal(parsed->groupings->name, "sub-grp");
3195 assert_string_equal(parsed->child->name, "anyd");
3196 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
3197 assert_string_equal(parsed->child->next->name, "anyx");
3198 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
3199 assert_string_equal(parsed->child->next->next->name, "subcont");
3200 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
3201 assert_string_equal(parsed->child->next->next->next->name, "leaf");
3202 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
3203 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
3204 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
3205 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
3206 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
3207 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
3208 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003209 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
3210 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
3211 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke2dc9e92019-07-24 09:59:21 +02003212 assert_string_equal(parsed->notifs->name, "notf");
David Sedlák85d0eca2019-07-24 15:15:21 +02003213 assert_string_equal(parsed->actions->name, "act");
David Sedláke0ef1c62019-09-13 10:05:55 +02003214 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003215 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3216 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedláke2dc9e92019-07-24 09:59:21 +02003217 lysp_node_free(st->ctx, siblings);
3218 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
3219 siblings = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02003220
3221 /* min subelems */
3222 data = ELEMENT_WRAPPER_START "<container name=\"cont-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003223 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákf111bcb2019-07-23 17:15:51 +02003224 parsed = (struct lysp_node_container *)siblings;
3225 assert_string_equal(parsed->name, "cont-name");
3226 lysp_node_free(st->ctx, siblings);
3227 siblings = NULL;
3228
3229 st->finished_correctly = true;
3230}
3231
David Sedlák5379d392019-07-24 10:42:03 +02003232static void
3233test_case_elem(void **state)
3234{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003235 struct test_parser_yin_state *st = *state;
David Sedlák5379d392019-07-24 10:42:03 +02003236 const char *data;
3237 struct lysp_node *siblings = NULL;
3238 struct tree_node_meta node_meta = {NULL, &siblings};
3239 struct lysp_node_case *parsed = NULL;
3240
3241 /* max subelems */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003242 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák5379d392019-07-24 10:42:03 +02003243 data = ELEMENT_WRAPPER_START
3244 "<case name=\"case-name\">"
3245 "<anydata name=\"anyd\"/>"
3246 "<anyxml name=\"anyx\"/>"
3247 "<container name=\"subcont\"/>"
3248 "<description><text>desc</text></description>"
3249 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003250 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3251 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák5379d392019-07-24 10:42:03 +02003252 "<list name=\"list\"/>"
3253 "<reference><text>ref</text></reference>"
3254 "<status value=\"current\"/>"
3255 "<uses name=\"uses-name\"/>"
3256 "<when condition=\"when-cond\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003257 "<choice name=\"choice\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003258 EXT_SUBELEM
David Sedlák5379d392019-07-24 10:42:03 +02003259 "</case>"
3260 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003261 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák5379d392019-07-24 10:42:03 +02003262 parsed = (struct lysp_node_case *)siblings;
3263 assert_string_equal(parsed->name, "case-name");
3264 assert_null(parsed->parent);
3265 assert_int_equal(parsed->nodetype, LYS_CASE);
3266 assert_true(parsed->flags & LYS_STATUS_CURR);
3267 assert_null(parsed->next);
3268 assert_string_equal(parsed->dsc, "desc");
3269 assert_string_equal(parsed->ref, "ref");
3270 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003271 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlák5379d392019-07-24 10:42:03 +02003272 assert_string_equal(parsed->child->name, "anyd");
3273 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
3274 assert_string_equal(parsed->child->next->name, "anyx");
3275 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
3276 assert_string_equal(parsed->child->next->next->name, "subcont");
3277 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
3278 assert_string_equal(parsed->child->next->next->next->name, "leaf");
3279 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
3280 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
3281 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
3282 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
3283 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
3284 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
3285 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003286 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
3287 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
3288 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke0ef1c62019-09-13 10:05:55 +02003289 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003290 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3291 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák5379d392019-07-24 10:42:03 +02003292 lysp_node_free(st->ctx, siblings);
3293 siblings = NULL;
3294
3295 /* min subelems */
3296 data = ELEMENT_WRAPPER_START "<case name=\"case-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003297 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák5379d392019-07-24 10:42:03 +02003298 parsed = (struct lysp_node_case *)siblings;
3299 assert_string_equal(parsed->name, "case-name");
3300 lysp_node_free(st->ctx, siblings);
3301 siblings = NULL;
3302
3303 st->finished_correctly = true;
3304}
3305
David Sedlákb7abcfa2019-07-24 12:33:35 +02003306static void
3307test_choice_elem(void **state)
3308{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003309 struct test_parser_yin_state *st = *state;
David Sedlákb7abcfa2019-07-24 12:33:35 +02003310 const char *data;
3311 struct lysp_node *siblings = NULL;
3312 struct tree_node_meta node_meta = {NULL, &siblings};
3313 struct lysp_node_choice *parsed = NULL;
3314
3315 /* max subelems */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003316 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlákb7abcfa2019-07-24 12:33:35 +02003317 data = ELEMENT_WRAPPER_START
3318 "<choice name=\"choice-name\">"
3319 "<anydata name=\"anyd\"/>"
3320 "<anyxml name=\"anyx\"/>"
3321 "<case name=\"sub-case\"/>"
3322 "<choice name=\"choice\"/>"
3323 "<config value=\"true\"/>"
3324 "<container name=\"subcont\"/>"
3325 "<default value=\"def\"/>"
3326 "<description><text>desc</text></description>"
3327 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003328 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3329 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003330 "<list name=\"list\"/>"
3331 "<mandatory value=\"true\" />"
3332 "<reference><text>ref</text></reference>"
3333 "<status value=\"current\"/>"
3334 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003335 EXT_SUBELEM
David Sedlákb7abcfa2019-07-24 12:33:35 +02003336 "</choice>"
3337 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003338 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003339 parsed = (struct lysp_node_choice *)siblings;
3340 assert_string_equal(parsed->name, "choice-name");
3341 assert_null(parsed->parent);
3342 assert_int_equal(parsed->nodetype, LYS_CHOICE);
3343 assert_true(parsed->flags & LYS_CONFIG_W && parsed->flags & LYS_MAND_TRUE && parsed->flags & LYS_STATUS_CURR);
3344 assert_null(parsed->next);
3345 assert_string_equal(parsed->dsc, "desc");
3346 assert_string_equal(parsed->ref, "ref");
3347 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003348 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlákb7abcfa2019-07-24 12:33:35 +02003349 assert_string_equal(parsed->child->name, "anyd");
3350 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
3351 assert_string_equal(parsed->child->next->name, "anyx");
3352 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
3353 assert_string_equal(parsed->child->next->next->name, "sub-case");
3354 assert_int_equal(parsed->child->next->next->nodetype, LYS_CASE);
3355 assert_string_equal(parsed->child->next->next->next->name, "choice");
3356 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
3357 assert_string_equal(parsed->child->next->next->next->next->name, "subcont");
3358 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_CONTAINER);
3359 assert_string_equal(parsed->child->next->next->next->next->next->name, "leaf");
3360 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAF);
3361 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "llist");
3362 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3363 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "list");
3364 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
3365 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke0ef1c62019-09-13 10:05:55 +02003366 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003367 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3368 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003369 lysp_node_free(st->ctx, siblings);
3370 siblings = NULL;
3371
3372 /* min subelems */
3373 data = ELEMENT_WRAPPER_START "<choice name=\"choice-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003374 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003375 parsed = (struct lysp_node_choice *)siblings;
3376 assert_string_equal(parsed->name, "choice-name");
3377 lysp_node_free(st->ctx, siblings);
3378 siblings = NULL;
3379
3380 st->finished_correctly = true;
3381}
3382
David Sedlák05404f62019-07-24 14:11:53 +02003383static void
3384test_inout_elem(void **state)
3385{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003386 struct test_parser_yin_state *st = *state;
David Sedlák05404f62019-07-24 14:11:53 +02003387 const char *data;
3388 struct lysp_action_inout inout = {};
3389 struct inout_meta inout_meta = {NULL, &inout};
3390
3391 /* max subelements */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003392 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák05404f62019-07-24 14:11:53 +02003393 data = ELEMENT_WRAPPER_START
3394 "<input>"
3395 "<anydata name=\"anyd\"/>"
3396 "<anyxml name=\"anyx\"/>"
3397 "<choice name=\"choice\"/>"
3398 "<container name=\"subcont\"/>"
3399 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003400 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3401 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák05404f62019-07-24 14:11:53 +02003402 "<list name=\"list\"/>"
3403 "<must condition=\"cond\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003404 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák05404f62019-07-24 14:11:53 +02003405 "<uses name=\"uses-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003406 EXT_SUBELEM
David Sedlák05404f62019-07-24 14:11:53 +02003407 "</input>"
3408 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003409 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_SUCCESS);
David Sedlák05404f62019-07-24 14:11:53 +02003410 assert_null(inout.parent);
3411 assert_int_equal(inout.nodetype, LYS_INPUT);
Michal Vasko7f45cf22020-10-01 12:49:44 +02003412 assert_string_equal(inout.musts->arg.str, "cond");
David Sedlák05404f62019-07-24 14:11:53 +02003413 assert_string_equal(inout.typedefs->name, "tpdf");
3414 assert_string_equal(inout.groupings->name, "sub-grp");
3415 assert_string_equal(inout.data->name, "anyd");
3416 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
3417 assert_string_equal(inout.data->next->name, "anyx");
3418 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
3419 assert_string_equal(inout.data->next->next->name, "choice");
3420 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
3421 assert_string_equal(inout.data->next->next->next->name, "subcont");
3422 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
3423 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
3424 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
3425 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
3426 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3427 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
3428 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
3429 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
3430 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3431 assert_null(inout.data->next->next->next->next->next->next->next->next);
David Sedláke0ef1c62019-09-13 10:05:55 +02003432 assert_string_equal(inout.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003433 assert_int_equal(inout.exts[0].insubstmt_index, 0);
3434 assert_int_equal(inout.exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák05404f62019-07-24 14:11:53 +02003435 lysp_action_inout_free(st->ctx, &inout);
3436 memset(&inout, 0, sizeof inout);
3437
3438 /* max subelements */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003439 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák05404f62019-07-24 14:11:53 +02003440 data = ELEMENT_WRAPPER_START
3441 "<output>"
3442 "<anydata name=\"anyd\"/>"
3443 "<anyxml name=\"anyx\"/>"
3444 "<choice name=\"choice\"/>"
3445 "<container name=\"subcont\"/>"
3446 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003447 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3448 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák05404f62019-07-24 14:11:53 +02003449 "<list name=\"list\"/>"
3450 "<must condition=\"cond\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003451 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák05404f62019-07-24 14:11:53 +02003452 "<uses name=\"uses-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003453 EXT_SUBELEM
David Sedlák05404f62019-07-24 14:11:53 +02003454 "</output>"
3455 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003456 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_SUCCESS);
David Sedlák05404f62019-07-24 14:11:53 +02003457 assert_null(inout.parent);
3458 assert_int_equal(inout.nodetype, LYS_OUTPUT);
Michal Vasko7f45cf22020-10-01 12:49:44 +02003459 assert_string_equal(inout.musts->arg.str, "cond");
David Sedlák05404f62019-07-24 14:11:53 +02003460 assert_string_equal(inout.typedefs->name, "tpdf");
3461 assert_string_equal(inout.groupings->name, "sub-grp");
3462 assert_string_equal(inout.data->name, "anyd");
3463 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
3464 assert_string_equal(inout.data->next->name, "anyx");
3465 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
3466 assert_string_equal(inout.data->next->next->name, "choice");
3467 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
3468 assert_string_equal(inout.data->next->next->next->name, "subcont");
3469 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
3470 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
3471 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
3472 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
3473 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3474 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
3475 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
3476 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
3477 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3478 assert_null(inout.data->next->next->next->next->next->next->next->next);
David Sedláke0ef1c62019-09-13 10:05:55 +02003479 assert_string_equal(inout.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003480 assert_int_equal(inout.exts[0].insubstmt_index, 0);
3481 assert_int_equal(inout.exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák05404f62019-07-24 14:11:53 +02003482 lysp_action_inout_free(st->ctx, &inout);
3483 memset(&inout, 0, sizeof inout);
3484
3485 /* min subelems */
Michal Vaskob83af8a2020-01-06 09:49:22 +01003486 data = ELEMENT_WRAPPER_START "<input><leaf name=\"l\"><type name=\"empty\"/></leaf></input>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003487 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_SUCCESS);
Michal Vaskob83af8a2020-01-06 09:49:22 +01003488 lysp_action_inout_free(st->ctx, &inout);
David Sedlák05404f62019-07-24 14:11:53 +02003489 memset(&inout, 0, sizeof inout);
3490
Michal Vaskob83af8a2020-01-06 09:49:22 +01003491 data = ELEMENT_WRAPPER_START "<output><leaf name=\"l\"><type name=\"empty\"/></leaf></output>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003492 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_SUCCESS);
Michal Vaskob83af8a2020-01-06 09:49:22 +01003493 lysp_action_inout_free(st->ctx, &inout);
David Sedlák05404f62019-07-24 14:11:53 +02003494 memset(&inout, 0, sizeof inout);
3495
3496 /* invalid combinations */
3497 data = ELEMENT_WRAPPER_START "<input name=\"test\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003498 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02003499 logbuf_assert("Unexpected attribute \"name\" of \"input\" element. Line number 1.");
David Sedlák05404f62019-07-24 14:11:53 +02003500 memset(&inout, 0, sizeof inout);
3501
3502 st->finished_correctly = true;
3503}
3504
David Sedlák85d0eca2019-07-24 15:15:21 +02003505static void
3506test_action_elem(void **state)
3507{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003508 struct test_parser_yin_state *st = *state;
David Sedlák85d0eca2019-07-24 15:15:21 +02003509 const char *data;
3510 struct lysp_action *actions = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003511 struct tree_node_meta act_meta = {NULL, (struct lysp_node **)&actions};
David Sedlák85d0eca2019-07-24 15:15:21 +02003512
3513 /* max subelems */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003514 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák85d0eca2019-07-24 15:15:21 +02003515 data = ELEMENT_WRAPPER_START
3516 "<action name=\"act\">"
3517 "<description><text>desc</text></description>"
3518 "<grouping name=\"grouping\"/>"
3519 "<if-feature name=\"iff\"/>"
3520 "<input><uses name=\"uses-name\"/></input>"
Michal Vaskob83af8a2020-01-06 09:49:22 +01003521 "<output><must condition=\"cond\"/><leaf name=\"l\"><type name=\"type\"/></leaf></output>"
David Sedlák85d0eca2019-07-24 15:15:21 +02003522 "<reference><text>ref</text></reference>"
3523 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003524 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákd1144562019-08-06 12:36:14 +02003525 EXT_SUBELEM
David Sedlák85d0eca2019-07-24 15:15:21 +02003526 "</action>"
3527 ELEMENT_WRAPPER_END;
Michal Vasko1bf09392020-03-27 12:38:10 +01003528 /* there must be parent for action */
Radek Krejcif482abc2020-08-27 15:29:05 +02003529 act_meta.parent = (void*)1;
Michal Vaskob36053d2020-03-26 15:49:30 +01003530 assert_int_equal(test_element_helper(st, data, &act_meta, NULL, NULL), LY_SUCCESS);
Michal Vasko1bf09392020-03-27 12:38:10 +01003531 act_meta.parent = NULL;
3532 assert_non_null(actions->parent);
David Sedlák85d0eca2019-07-24 15:15:21 +02003533 assert_int_equal(actions->nodetype, LYS_ACTION);
3534 assert_true(actions->flags & LYS_STATUS_DEPRC);
3535 assert_string_equal(actions->name, "act");
3536 assert_string_equal(actions->dsc, "desc");
3537 assert_string_equal(actions->ref, "ref");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003538 assert_string_equal(actions->iffeatures[0].str, "iff");
David Sedlák85d0eca2019-07-24 15:15:21 +02003539 assert_string_equal(actions->typedefs->name, "tpdf");
3540 assert_string_equal(actions->groupings->name, "grouping");
3541 assert_string_equal(actions->input.data->name, "uses-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003542 assert_string_equal(actions->output.musts->arg.str, "cond");
David Sedláke0ef1c62019-09-13 10:05:55 +02003543 assert_string_equal(actions->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003544 assert_int_equal(actions->exts[0].insubstmt_index, 0);
3545 assert_int_equal(actions->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák85d0eca2019-07-24 15:15:21 +02003546 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3547 actions = NULL;
3548
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003549 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlákeaa45792019-07-24 15:25:01 +02003550 data = ELEMENT_WRAPPER_START
3551 "<rpc name=\"act\">"
3552 "<description><text>desc</text></description>"
3553 "<grouping name=\"grouping\"/>"
3554 "<if-feature name=\"iff\"/>"
3555 "<input><uses name=\"uses-name\"/></input>"
Michal Vaskob83af8a2020-01-06 09:49:22 +01003556 "<output><must condition=\"cond\"/><leaf name=\"l\"><type name=\"type\"/></leaf></output>"
David Sedlákeaa45792019-07-24 15:25:01 +02003557 "<reference><text>ref</text></reference>"
3558 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003559 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákd1144562019-08-06 12:36:14 +02003560 EXT_SUBELEM
David Sedlákeaa45792019-07-24 15:25:01 +02003561 "</rpc>"
3562 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003563 assert_int_equal(test_element_helper(st, data, &act_meta, NULL, NULL), LY_SUCCESS);
David Sedlákeaa45792019-07-24 15:25:01 +02003564 assert_null(actions->parent);
Michal Vasko1bf09392020-03-27 12:38:10 +01003565 assert_int_equal(actions->nodetype, LYS_RPC);
David Sedlákeaa45792019-07-24 15:25:01 +02003566 assert_true(actions->flags & LYS_STATUS_DEPRC);
3567 assert_string_equal(actions->name, "act");
3568 assert_string_equal(actions->dsc, "desc");
3569 assert_string_equal(actions->ref, "ref");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003570 assert_string_equal(actions->iffeatures[0].str, "iff");
David Sedlákeaa45792019-07-24 15:25:01 +02003571 assert_string_equal(actions->typedefs->name, "tpdf");
3572 assert_string_equal(actions->groupings->name, "grouping");
3573 assert_string_equal(actions->input.data->name, "uses-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003574 assert_string_equal(actions->output.musts->arg.str, "cond");
David Sedláke0ef1c62019-09-13 10:05:55 +02003575 assert_string_equal(actions->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003576 assert_int_equal(actions->exts[0].insubstmt_index, 0);
3577 assert_int_equal(actions->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákeaa45792019-07-24 15:25:01 +02003578 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3579 actions = NULL;
3580
David Sedlák85d0eca2019-07-24 15:15:21 +02003581 /* min subelems */
3582 data = ELEMENT_WRAPPER_START "<action name=\"act\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003583 assert_int_equal(test_element_helper(st, data, &act_meta, NULL, NULL), LY_SUCCESS);
David Sedlák85d0eca2019-07-24 15:15:21 +02003584 assert_string_equal(actions->name, "act");
3585 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3586 actions = NULL;
3587
3588 st->finished_correctly = true;
3589}
3590
David Sedlák992fb7c2019-07-24 16:51:01 +02003591static void
3592test_augment_elem(void **state)
3593{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003594 struct test_parser_yin_state *st = *state;
David Sedlák992fb7c2019-07-24 16:51:01 +02003595 const char *data;
3596 struct lysp_augment *augments = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003597 struct tree_node_meta aug_meta = {NULL, (struct lysp_node **)&augments};
David Sedlák992fb7c2019-07-24 16:51:01 +02003598
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003599 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák992fb7c2019-07-24 16:51:01 +02003600 data = ELEMENT_WRAPPER_START
3601 "<augment target-node=\"target\">"
3602 "<action name=\"action\"/>"
3603 "<anydata name=\"anyd\"/>"
3604 "<anyxml name=\"anyx\"/>"
3605 "<case name=\"case\"/>"
3606 "<choice name=\"choice\"/>"
3607 "<container name=\"subcont\"/>"
3608 "<description><text>desc</text></description>"
3609 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003610 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3611 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák992fb7c2019-07-24 16:51:01 +02003612 "<list name=\"list\"/>"
3613 "<notification name=\"notif\"/>"
3614 "<reference><text>ref</text></reference>"
3615 "<status value=\"current\"/>"
3616 "<uses name=\"uses\"/>"
3617 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003618 EXT_SUBELEM
David Sedlák992fb7c2019-07-24 16:51:01 +02003619 "</augment>"
3620 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003621 assert_int_equal(test_element_helper(st, data, &aug_meta, NULL, NULL), LY_SUCCESS);
David Sedlák992fb7c2019-07-24 16:51:01 +02003622 assert_string_equal(augments->nodeid, "target");
3623 assert_null(augments->parent);
3624 assert_int_equal(augments->nodetype, LYS_AUGMENT);
3625 assert_true(augments->flags & LYS_STATUS_CURR);
3626 assert_string_equal(augments->dsc, "desc");
3627 assert_string_equal(augments->ref, "ref");
3628 assert_string_equal(augments->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003629 assert_string_equal(augments->iffeatures[0].str, "iff");
David Sedlák992fb7c2019-07-24 16:51:01 +02003630 assert_string_equal(augments->child->name, "anyd");
3631 assert_int_equal(augments->child->nodetype, LYS_ANYDATA);
3632 assert_string_equal(augments->child->next->name, "anyx");
3633 assert_int_equal(augments->child->next->nodetype, LYS_ANYXML);
3634 assert_string_equal(augments->child->next->next->name, "case");
3635 assert_int_equal(augments->child->next->next->nodetype, LYS_CASE);
3636 assert_string_equal(augments->child->next->next->next->name, "choice");
3637 assert_int_equal(augments->child->next->next->next->nodetype, LYS_CHOICE);
3638 assert_string_equal(augments->child->next->next->next->next->name, "subcont");
3639 assert_int_equal(augments->child->next->next->next->next->nodetype, LYS_CONTAINER);
3640 assert_string_equal(augments->child->next->next->next->next->next->name, "leaf");
3641 assert_int_equal(augments->child->next->next->next->next->next->nodetype, LYS_LEAF);
3642 assert_string_equal(augments->child->next->next->next->next->next->next->name, "llist");
3643 assert_int_equal(augments->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3644 assert_string_equal(augments->child->next->next->next->next->next->next->next->name, "list");
3645 assert_int_equal(augments->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
3646 assert_string_equal(augments->child->next->next->next->next->next->next->next->next->name, "uses");
3647 assert_int_equal(augments->child->next->next->next->next->next->next->next->next->nodetype, LYS_USES);
3648 assert_null(augments->child->next->next->next->next->next->next->next->next->next);
3649 assert_string_equal(augments->actions->name, "action");
3650 assert_string_equal(augments->notifs->name, "notif");
David Sedláke0ef1c62019-09-13 10:05:55 +02003651 assert_string_equal(augments->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003652 assert_int_equal(augments->exts[0].insubstmt_index, 0);
3653 assert_int_equal(augments->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák992fb7c2019-07-24 16:51:01 +02003654 FREE_ARRAY(st->ctx, augments, lysp_augment_free)
3655 augments = NULL;
3656
3657 data = ELEMENT_WRAPPER_START "<augment target-node=\"target\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003658 assert_int_equal(test_element_helper(st, data, &aug_meta, NULL, NULL), LY_SUCCESS);
David Sedlák992fb7c2019-07-24 16:51:01 +02003659 assert_string_equal(augments->nodeid, "target");
3660 FREE_ARRAY(st->ctx, augments, lysp_augment_free)
3661 augments = NULL;
3662
3663 st->finished_correctly = true;
3664}
3665
David Sedlák4ffcec82019-07-25 15:10:21 +02003666static void
3667test_deviate_elem(void **state)
3668{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003669 struct test_parser_yin_state *st = *state;
David Sedlák4ffcec82019-07-25 15:10:21 +02003670 const char *data;
3671 struct lysp_deviate *deviates = NULL;
3672 struct lysp_deviate_add *d_add;
3673 struct lysp_deviate_rpl *d_rpl;
3674 struct lysp_deviate_del *d_del;
3675
3676 /* all valid arguments with min subelems */
3677 data = ELEMENT_WRAPPER_START "<deviate value=\"not-supported\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003678 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003679 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
3680 lysp_deviate_free(st->ctx, deviates);
3681 free(deviates);
3682 deviates = NULL;
3683
3684 data = ELEMENT_WRAPPER_START "<deviate value=\"add\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003685 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003686 assert_int_equal(deviates->mod, LYS_DEV_ADD);
3687 lysp_deviate_free(st->ctx, deviates);
3688 free(deviates);
3689 deviates = NULL;
3690
3691 data = ELEMENT_WRAPPER_START "<deviate value=\"replace\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003692 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003693 assert_int_equal(deviates->mod, LYS_DEV_REPLACE);
3694 lysp_deviate_free(st->ctx, deviates);
3695 free(deviates);
3696 deviates = NULL;
3697
3698 data = ELEMENT_WRAPPER_START "<deviate value=\"delete\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003699 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003700 assert_int_equal(deviates->mod, LYS_DEV_DELETE);
3701 lysp_deviate_free(st->ctx, deviates);
3702 free(deviates);
3703 deviates = NULL;
3704
3705 /* max subelems and valid arguments */
3706 data = ELEMENT_WRAPPER_START
3707 "<deviate value=\"not-supported\">"
David Sedlákd1144562019-08-06 12:36:14 +02003708 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003709 "</deviate>"
3710 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003711 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003712 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
David Sedláke0ef1c62019-09-13 10:05:55 +02003713 assert_string_equal(deviates->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003714 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3715 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003716 lysp_deviate_free(st->ctx, deviates);
3717 free(deviates);
3718 deviates = NULL;
3719
3720 data = ELEMENT_WRAPPER_START
3721 "<deviate value=\"add\">"
3722 "<units name=\"units\"/>"
3723 "<must condition=\"cond\"/>"
3724 "<unique tag=\"utag\"/>"
3725 "<default value=\"def\"/>"
3726 "<config value=\"true\"/>"
3727 "<mandatory value=\"true\"/>"
3728 "<min-elements value=\"5\"/>"
3729 "<max-elements value=\"15\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003730 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003731 "</deviate>"
3732 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003733 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003734 d_add = (struct lysp_deviate_add *)deviates;
3735 assert_int_equal(d_add->mod, LYS_DEV_ADD);
3736 assert_null(d_add->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003737 assert_string_equal(d_add->units, "units");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003738 assert_string_equal(d_add->musts->arg.str, "cond");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003739 assert_string_equal(d_add->uniques[0].str, "utag");
3740 assert_string_equal(d_add->dflts[0].str, "def");
David Sedlák4ffcec82019-07-25 15:10:21 +02003741 assert_true(d_add->flags & LYS_MAND_TRUE && d_add->flags & LYS_CONFIG_W);
3742 assert_int_equal(d_add->min, 5);
3743 assert_int_equal(d_add->max, 15);
David Sedláke0ef1c62019-09-13 10:05:55 +02003744 assert_string_equal(deviates->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003745 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3746 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003747 lysp_deviate_free(st->ctx, deviates);
3748 free(deviates);
3749 deviates = NULL;
3750
3751 data = ELEMENT_WRAPPER_START
3752 "<deviate value=\"replace\">"
3753 "<type name=\"newtype\"/>"
3754 "<units name=\"uni\"/>"
3755 "<default value=\"def\"/>"
3756 "<config value=\"true\"/>"
3757 "<mandatory value=\"true\"/>"
3758 "<min-elements value=\"5\"/>"
3759 "<max-elements value=\"15\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003760 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003761 "</deviate>"
3762 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003763 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003764 d_rpl = (struct lysp_deviate_rpl *)deviates;
3765 assert_int_equal(d_rpl->mod, LYS_DEV_REPLACE);
3766 assert_null(d_rpl->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003767 assert_string_equal(d_rpl->type->name, "newtype");
3768 assert_string_equal(d_rpl->units, "uni");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003769 assert_string_equal(d_rpl->dflt.str, "def");
David Sedlák4ffcec82019-07-25 15:10:21 +02003770 assert_true(d_rpl->flags & LYS_MAND_TRUE && d_rpl->flags & LYS_CONFIG_W);
3771 assert_int_equal(d_rpl->min, 5);
3772 assert_int_equal(d_rpl->max, 15);
David Sedláke0ef1c62019-09-13 10:05:55 +02003773 assert_string_equal(deviates->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003774 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3775 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003776 lysp_deviate_free(st->ctx, deviates);
3777 free(deviates);
3778 deviates = NULL;
3779
3780 data = ELEMENT_WRAPPER_START
3781 "<deviate value=\"delete\">"
3782 "<units name=\"u\"/>"
3783 "<must condition=\"c\"/>"
3784 "<unique tag=\"tag\"/>"
3785 "<default value=\"default\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003786 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003787 "</deviate>"
3788 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003789 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003790 d_del = (struct lysp_deviate_del *)deviates;
3791 assert_int_equal(d_del->mod, LYS_DEV_DELETE);
3792 assert_null(d_del->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003793 assert_string_equal(d_del->units, "u");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003794 assert_string_equal(d_del->musts->arg.str, "c");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003795 assert_string_equal(d_del->uniques[0].str, "tag");
3796 assert_string_equal(d_del->dflts[0].str, "default");
David Sedláke0ef1c62019-09-13 10:05:55 +02003797 assert_string_equal(deviates->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003798 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3799 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003800 lysp_deviate_free(st->ctx, deviates);
3801 free(deviates);
3802 deviates = NULL;
3803
3804 /* invalid arguments */
3805 data = ELEMENT_WRAPPER_START "<deviate value=\"\" />" 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 \"\" 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 "<deviate value=\"invalid\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003811 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003812 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 +02003813 deviates = NULL;
3814
3815 data = ELEMENT_WRAPPER_START "<deviate value=\"ad\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003816 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003817 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 +02003818 deviates = NULL;
3819
3820 data = ELEMENT_WRAPPER_START "<deviate value=\"adds\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003821 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003822 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 +02003823 deviates = NULL;
3824
3825 data = ELEMENT_WRAPPER_START
3826 "<deviate value=\"not-supported\">"
3827 "<must condition=\"c\"/>"
3828 "</deviate>"
3829 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003830 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_EVALID);
David Sedlák4ffcec82019-07-25 15:10:21 +02003831 logbuf_assert("Deviate of this type doesn't allow \"must\" as it's sub-element. Line number 1.");
3832
3833 st->finished_correctly = true;
3834}
3835
David Sedlák8b754462019-07-25 16:22:13 +02003836static void
3837test_deviation_elem(void **state)
3838{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003839 struct test_parser_yin_state *st = *state;
David Sedlák8b754462019-07-25 16:22:13 +02003840 const char *data;
3841 struct lysp_deviation *deviations = NULL;
3842
3843 /* min subelems */
3844 data = ELEMENT_WRAPPER_START
3845 "<deviation target-node=\"target\">"
3846 "<deviate value=\"not-supported\"/>"
3847 "</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_NOT_SUPPORTED);
3852 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3853 deviations = NULL;
3854
3855 /* max subelems */
3856 data = ELEMENT_WRAPPER_START
3857 "<deviation target-node=\"target\">"
3858 "<reference><text>ref</text></reference>"
3859 "<description><text>desc</text></description>"
3860 "<deviate value=\"add\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003861 EXT_SUBELEM
David Sedlák8b754462019-07-25 16:22:13 +02003862 "</deviation>"
3863 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003864 assert_int_equal(test_element_helper(st, data, &deviations, NULL, NULL), LY_SUCCESS);
David Sedlák8b754462019-07-25 16:22:13 +02003865 assert_string_equal(deviations->nodeid, "target");
3866 assert_int_equal(deviations->deviates->mod, LYS_DEV_ADD);
3867 assert_string_equal(deviations->ref, "ref");
3868 assert_string_equal(deviations->dsc, "desc");
David Sedláke0ef1c62019-09-13 10:05:55 +02003869 assert_string_equal(deviations->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003870 assert_int_equal(deviations->exts[0].insubstmt_index, 0);
3871 assert_int_equal(deviations->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8b754462019-07-25 16:22:13 +02003872 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3873 deviations = NULL;
3874
3875 /* invalid */
3876 data = ELEMENT_WRAPPER_START "<deviation target-node=\"target\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003877 assert_int_equal(test_element_helper(st, data, &deviations, NULL, NULL), LY_EVALID);
David Sedlák8b754462019-07-25 16:22:13 +02003878 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3879 deviations = NULL;
David Sedlák1538a842019-08-08 15:38:51 +02003880 logbuf_assert("Missing mandatory sub-element \"deviate\" of \"deviation\" element. Line number 1.");
3881 /* TODO */
David Sedlák8b754462019-07-25 16:22:13 +02003882 st->finished_correctly = true;
3883}
3884
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003885static struct lysp_module *
3886mod_renew(struct lys_yin_parser_ctx *ctx)
3887{
3888 struct ly_ctx *ly_ctx = ctx->parsed_mod->mod->ctx;
3889
3890 lys_module_free(ctx->parsed_mod->mod, NULL);
3891 ctx->parsed_mod = calloc(1, sizeof *ctx->parsed_mod);
3892 ctx->parsed_mod->mod = calloc(1, sizeof *ctx->parsed_mod->mod);
3893 ctx->parsed_mod->mod->parsed = ctx->parsed_mod;
3894 ctx->parsed_mod->mod->ctx = ly_ctx;
3895
3896 return ctx->parsed_mod;
3897}
3898
David Sedlák4f03b932019-07-26 13:01:47 +02003899static void
3900test_module_elem(void **state)
3901{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003902 struct test_parser_yin_state *st = *state;
Michal Vaskob36053d2020-03-26 15:49:30 +01003903 const char *data;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003904 struct lysp_module *lysp_mod = mod_renew(st->yin_ctx);
David Sedlák4f03b932019-07-26 13:01:47 +02003905
3906 /* max subelems */
David Sedlák4f03b932019-07-26 13:01:47 +02003907 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
3908 "<yang-version value=\"1.1\"/>\n"
3909 "<namespace uri=\"ns\"/>\n"
3910 "<prefix value=\"pref\"/>\n"
3911 "<include module=\"b-mod\"/>\n"
3912 "<import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
3913 "<organization><text>org</text></organization>\n"
3914 "<contact><text>contact</text></contact>\n"
3915 "<description><text>desc</text></description>"
3916 "<reference><text>ref</text></reference>\n"
3917 "<revision date=\"2019-02-02\"/>\n"
3918 "<anydata name=\"anyd\"/>\n"
3919 "<anyxml name=\"anyx\"/>\n"
3920 "<choice name=\"choice\"/>\n"
3921 "<container name=\"cont\"/>\n"
3922 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3923 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3924 "<list name=\"sub-list\"/>\n"
3925 "<uses name=\"uses-name\"/>\n"
3926 "<augment target-node=\"target\"/>\n"
3927 "<deviation target-node=\"target\">""<deviate value=\"not-supported\"/>""</deviation>\n"
3928 "<extension name=\"ext\"/>\n"
3929 "<feature name=\"feature\"/>\n"
3930 "<grouping name=\"grp\"/>\n"
3931 "<identity name=\"ident-name\"/>\n"
3932 "<notification name=\"notf\"/>\n"
3933 "<rpc name=\"rpc-name\"/>\n"
3934 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
David Sedlákd1144562019-08-06 12:36:14 +02003935 EXT_SUBELEM"\n"
David Sedlák4f03b932019-07-26 13:01:47 +02003936 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003937 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
3938 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01003939
3940 assert_int_equal(yin_parse_mod(st->yin_ctx, lysp_mod), LY_SUCCESS);
David Sedlák4f03b932019-07-26 13:01:47 +02003941 assert_string_equal(lysp_mod->mod->name, "mod");
3942 assert_string_equal(lysp_mod->revs, "2019-02-02");
3943 assert_string_equal(lysp_mod->mod->ns, "ns");
3944 assert_string_equal(lysp_mod->mod->prefix, "pref");
3945 assert_null(lysp_mod->mod->filepath);
3946 assert_string_equal(lysp_mod->mod->org, "org");
3947 assert_string_equal(lysp_mod->mod->contact, "contact");
3948 assert_string_equal(lysp_mod->mod->dsc, "desc");
3949 assert_string_equal(lysp_mod->mod->ref, "ref");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003950 assert_int_equal(lysp_mod->version, LYS_VERSION_1_1);
David Sedlák4f03b932019-07-26 13:01:47 +02003951 assert_string_equal(lysp_mod->imports->name, "a-mod");
3952 assert_string_equal(lysp_mod->includes->name, "b-mod");
3953 assert_string_equal(lysp_mod->extensions->name, "ext");
3954 assert_string_equal(lysp_mod->features->name, "feature");
3955 assert_string_equal(lysp_mod->identities->name, "ident-name");
3956 assert_string_equal(lysp_mod->typedefs->name, "tpdf");
3957 assert_string_equal(lysp_mod->groupings->name, "grp");
3958 assert_string_equal(lysp_mod->data->name, "anyd");
3959 assert_int_equal(lysp_mod->data->nodetype, LYS_ANYDATA);
3960 assert_string_equal(lysp_mod->data->next->name, "anyx");
3961 assert_int_equal(lysp_mod->data->next->nodetype, LYS_ANYXML);
3962 assert_string_equal(lysp_mod->data->next->next->name, "choice");
3963 assert_int_equal(lysp_mod->data->next->next->nodetype, LYS_CHOICE);
3964 assert_string_equal(lysp_mod->data->next->next->next->name, "cont");
3965 assert_int_equal(lysp_mod->data->next->next->next->nodetype, LYS_CONTAINER);
3966 assert_string_equal(lysp_mod->data->next->next->next->next->name, "leaf");
3967 assert_int_equal(lysp_mod->data->next->next->next->next->nodetype, LYS_LEAF);
3968 assert_string_equal(lysp_mod->data->next->next->next->next->next->name, "llist");
3969 assert_int_equal(lysp_mod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3970 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->name, "sub-list");
3971 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
3972 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->next->name, "uses-name");
3973 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3974 assert_null(lysp_mod->data->next->next->next->next->next->next->next->next);
3975 assert_string_equal(lysp_mod->augments->nodeid, "target");
3976 assert_string_equal(lysp_mod->rpcs->name, "rpc-name");
3977 assert_string_equal(lysp_mod->notifs->name, "notf");
3978 assert_string_equal(lysp_mod->deviations->nodeid, "target");
David Sedláke0ef1c62019-09-13 10:05:55 +02003979 assert_string_equal(lysp_mod->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003980 assert_int_equal(lysp_mod->exts[0].insubstmt_index, 0);
3981 assert_int_equal(lysp_mod->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4f03b932019-07-26 13:01:47 +02003982
3983 /* min subelems */
Michal Vasko63f3d842020-07-08 10:10:14 +02003984 ly_in_free(st->in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +01003985 lyxml_ctx_free(st->yin_ctx->xmlctx);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003986 lysp_mod = mod_renew(st->yin_ctx);
David Sedlák4f03b932019-07-26 13:01:47 +02003987 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">"
3988 "<namespace uri=\"ns\"/>"
3989 "<prefix value=\"pref\"/>"
3990 "<yang-version value=\"1.1\"/>"
3991 "</module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003992 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
3993 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01003994 assert_int_equal(yin_parse_mod(st->yin_ctx, lysp_mod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003995 assert_string_equal(lysp_mod->mod->name, "mod");
David Sedlák298ff6d2019-07-26 14:29:03 +02003996
David Sedláke6cd89e2019-08-07 12:46:02 +02003997 /* incorrect subelem order */
Michal Vasko63f3d842020-07-08 10:10:14 +02003998 ly_in_free(st->in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +01003999 lyxml_ctx_free(st->yin_ctx->xmlctx);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004000 lysp_mod = mod_renew(st->yin_ctx);
David Sedláke6cd89e2019-08-07 12:46:02 +02004001 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">"
4002 "<feature name=\"feature\"/>\n"
4003 "<namespace uri=\"ns\"/>"
4004 "<prefix value=\"pref\"/>"
4005 "<yang-version value=\"1.1\"/>"
4006 "</module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004007 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
4008 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01004009 assert_int_equal(yin_parse_mod(st->yin_ctx, lysp_mod), LY_EVALID);
4010 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 +02004011
David Sedlák298ff6d2019-07-26 14:29:03 +02004012 st->finished_correctly = true;
4013}
4014
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004015static struct lysp_submodule *
4016submod_renew(struct lys_yin_parser_ctx *ctx, const char *belongs_to)
4017{
4018 struct ly_ctx *ly_ctx = ctx->parsed_mod->mod->ctx;
4019
4020 lys_module_free(ctx->parsed_mod->mod, NULL);
4021 ctx->parsed_mod = calloc(1, sizeof(struct lysp_submodule));
4022 ctx->parsed_mod->mod = calloc(1, sizeof *ctx->parsed_mod->mod);
4023 lydict_insert(ly_ctx, belongs_to, 0, &ctx->parsed_mod->mod->name);
4024 ctx->parsed_mod->mod->parsed = ctx->parsed_mod;
4025 ctx->parsed_mod->mod->ctx = ly_ctx;
4026
4027 return (struct lysp_submodule *)ctx->parsed_mod;
4028}
4029
David Sedlák298ff6d2019-07-26 14:29:03 +02004030static void
4031test_submodule_elem(void **state)
4032{
Radek Krejci3a4889a2020-05-19 17:01:58 +02004033 struct test_parser_yin_state *st = *state;
Michal Vaskob36053d2020-03-26 15:49:30 +01004034 const char *data;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004035 struct lysp_submodule *lysp_submod = submod_renew(st->yin_ctx, "module-name");
David Sedlák298ff6d2019-07-26 14:29:03 +02004036
4037 /* max subelements */
David Sedlák298ff6d2019-07-26 14:29:03 +02004038 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
4039 "<yang-version value=\"1.1\"/>\n"
Michal Vaskoc3781c32020-10-06 14:04:08 +02004040 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>"
David Sedlák298ff6d2019-07-26 14:29:03 +02004041 "<include module=\"b-mod\"/>\n"
4042 "<import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
4043 "<organization><text>org</text></organization>\n"
4044 "<contact><text>contact</text></contact>\n"
4045 "<description><text>desc</text></description>"
4046 "<reference><text>ref</text></reference>\n"
4047 "<revision date=\"2019-02-02\"/>\n"
4048 "<anydata name=\"anyd\"/>\n"
4049 "<anyxml name=\"anyx\"/>\n"
4050 "<choice name=\"choice\"/>\n"
4051 "<container name=\"cont\"/>\n"
4052 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
4053 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
4054 "<list name=\"sub-list\"/>\n"
4055 "<uses name=\"uses-name\"/>\n"
4056 "<augment target-node=\"target\"/>\n"
4057 "<deviation target-node=\"target\">""<deviate value=\"not-supported\"/>""</deviation>\n"
4058 "<extension name=\"ext\"/>\n"
4059 "<feature name=\"feature\"/>\n"
4060 "<grouping name=\"grp\"/>\n"
4061 "<identity name=\"ident-name\"/>\n"
4062 "<notification name=\"notf\"/>\n"
4063 "<rpc name=\"rpc-name\"/>\n"
4064 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
David Sedlákd1144562019-08-06 12:36:14 +02004065 EXT_SUBELEM"\n"
David Sedlák298ff6d2019-07-26 14:29:03 +02004066 "</submodule>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004067 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
4068 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02004069
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004070 assert_int_equal(yin_parse_submod(st->yin_ctx, lysp_submod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02004071 assert_string_equal(lysp_submod->name, "mod");
4072 assert_string_equal(lysp_submod->revs, "2019-02-02");
David Sedlák298ff6d2019-07-26 14:29:03 +02004073 assert_string_equal(lysp_submod->prefix, "pref");
4074 assert_null(lysp_submod->filepath);
4075 assert_string_equal(lysp_submod->org, "org");
4076 assert_string_equal(lysp_submod->contact, "contact");
4077 assert_string_equal(lysp_submod->dsc, "desc");
4078 assert_string_equal(lysp_submod->ref, "ref");
4079 assert_int_equal(lysp_submod->version, LYS_VERSION_1_1);
4080 assert_string_equal(lysp_submod->imports->name, "a-mod");
4081 assert_string_equal(lysp_submod->includes->name, "b-mod");
4082 assert_string_equal(lysp_submod->extensions->name, "ext");
4083 assert_string_equal(lysp_submod->features->name, "feature");
4084 assert_string_equal(lysp_submod->identities->name, "ident-name");
4085 assert_string_equal(lysp_submod->typedefs->name, "tpdf");
4086 assert_string_equal(lysp_submod->groupings->name, "grp");
4087 assert_string_equal(lysp_submod->data->name, "anyd");
4088 assert_int_equal(lysp_submod->data->nodetype, LYS_ANYDATA);
4089 assert_string_equal(lysp_submod->data->next->name, "anyx");
4090 assert_int_equal(lysp_submod->data->next->nodetype, LYS_ANYXML);
4091 assert_string_equal(lysp_submod->data->next->next->name, "choice");
4092 assert_int_equal(lysp_submod->data->next->next->nodetype, LYS_CHOICE);
4093 assert_string_equal(lysp_submod->data->next->next->next->name, "cont");
4094 assert_int_equal(lysp_submod->data->next->next->next->nodetype, LYS_CONTAINER);
4095 assert_string_equal(lysp_submod->data->next->next->next->next->name, "leaf");
4096 assert_int_equal(lysp_submod->data->next->next->next->next->nodetype, LYS_LEAF);
4097 assert_string_equal(lysp_submod->data->next->next->next->next->next->name, "llist");
4098 assert_int_equal(lysp_submod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
4099 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->name, "sub-list");
4100 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
4101 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->next->name, "uses-name");
4102 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
4103 assert_null(lysp_submod->data->next->next->next->next->next->next->next->next);
4104 assert_string_equal(lysp_submod->augments->nodeid, "target");
4105 assert_string_equal(lysp_submod->rpcs->name, "rpc-name");
4106 assert_string_equal(lysp_submod->notifs->name, "notf");
4107 assert_string_equal(lysp_submod->deviations->nodeid, "target");
David Sedláke0ef1c62019-09-13 10:05:55 +02004108 assert_string_equal(lysp_submod->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02004109 assert_int_equal(lysp_submod->exts[0].insubstmt_index, 0);
4110 assert_int_equal(lysp_submod->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák298ff6d2019-07-26 14:29:03 +02004111
David Sedlák298ff6d2019-07-26 14:29:03 +02004112 /* min subelemnts */
Michal Vasko63f3d842020-07-08 10:10:14 +02004113 ly_in_free(st->in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +01004114 lyxml_ctx_free(st->yin_ctx->xmlctx);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004115 lysp_submod = submod_renew(st->yin_ctx, "module-name");
David Sedlák298ff6d2019-07-26 14:29:03 +02004116 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">"
Radek Krejci96e48da2020-09-04 13:18:06 +02004117 "<yang-version value=\"1\"/>"
Michal Vaskoc3781c32020-10-06 14:04:08 +02004118 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>"
David Sedlák298ff6d2019-07-26 14:29:03 +02004119 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004120 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
4121 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01004122 assert_int_equal(yin_parse_submod(st->yin_ctx, lysp_submod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02004123 assert_string_equal(lysp_submod->prefix, "pref");
David Sedlák298ff6d2019-07-26 14:29:03 +02004124 assert_int_equal(lysp_submod->version, LYS_VERSION_1_0);
David Sedlák298ff6d2019-07-26 14:29:03 +02004125
David Sedláke6cd89e2019-08-07 12:46:02 +02004126 /* incorrect subelem order */
Michal Vasko63f3d842020-07-08 10:10:14 +02004127 ly_in_free(st->in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +01004128 lyxml_ctx_free(st->yin_ctx->xmlctx);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004129 lysp_submod = submod_renew(st->yin_ctx, "module-name");
David Sedláke6cd89e2019-08-07 12:46:02 +02004130 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">"
Radek Krejci96e48da2020-09-04 13:18:06 +02004131 "<yang-version value=\"1\"/>"
David Sedláke6cd89e2019-08-07 12:46:02 +02004132 "<reference><text>ref</text></reference>\n"
Michal Vaskoc3781c32020-10-06 14:04:08 +02004133 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>"
David Sedláke6cd89e2019-08-07 12:46:02 +02004134 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004135 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
4136 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01004137 assert_int_equal(yin_parse_submod(st->yin_ctx, lysp_submod), LY_EVALID);
4138 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 +02004139
David Sedlák298ff6d2019-07-26 14:29:03 +02004140 st->finished_correctly = true;
David Sedlák4f03b932019-07-26 13:01:47 +02004141}
4142
David Sedlák8985a142019-07-31 16:43:06 +02004143static void
4144test_yin_parse_module(void **state)
4145{
Radek Krejci3a4889a2020-05-19 17:01:58 +02004146 struct test_parser_yin_state *st = *state;
David Sedlák8985a142019-07-31 16:43:06 +02004147 const char *data;
4148 struct lys_module *mod;
Michal Vaskob36053d2020-03-26 15:49:30 +01004149 struct lys_yin_parser_ctx *yin_ctx = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02004150 struct ly_in *in = NULL;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004151 struct lys_glob_unres unres = {0};
David Sedlák8985a142019-07-31 16:43:06 +02004152
4153 mod = calloc(1, sizeof *mod);
4154 mod->ctx = st->ctx;
David Sedlákd2844882019-09-13 16:01:22 +02004155 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"
4156 "<yang-version value=\"1.1\"/>\n"
4157 "<namespace uri=\"urn:tests:extensions:metadata:a\"/>\n"
4158 "<prefix value=\"a\"/>\n"
4159 "<import module=\"ietf-yang-metadata\">\n"
4160 "<prefix value=\"md\"/>\n"
4161 "</import>\n"
4162 "<feature name=\"f\"/>\n"
4163 "<md:annotation name=\"x\">\n"
4164 "<description>\n"
4165 "<text>test</text>\n"
4166 "</description>\n"
4167 "<reference>\n"
4168 "<text>test</text>\n"
4169 "</reference>\n"
4170 "<if-feature name=\"f\"/>\n"
4171 "<status value=\"current\"/>\n"
4172 "<type name=\"uint8\"/>\n"
4173 "<units name=\"meters\"/>\n"
4174 "</md:annotation>\n"
4175 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004176 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko405cc9e2020-12-01 12:01:27 +01004177 assert_int_equal(yin_parse_module(&yin_ctx, in, mod, &unres), LY_SUCCESS);
David Sedlákd2844882019-09-13 16:01:22 +02004178 assert_null(mod->parsed->exts->child->next->child);
4179 assert_string_equal(mod->parsed->exts->child->next->arg, "test");
Michal Vasko405cc9e2020-12-01 12:01:27 +01004180 lys_compile_unres_glob_erase(st->ctx, &unres);
David Sedlákd2844882019-09-13 16:01:22 +02004181 lys_module_free(mod, NULL);
4182 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004183 ly_in_free(in, 0);
David Sedlákd2844882019-09-13 16:01:22 +02004184 mod = NULL;
4185 yin_ctx = NULL;
4186
4187 mod = calloc(1, sizeof *mod);
4188 mod->ctx = st->ctx;
David Sedlák8985a142019-07-31 16:43:06 +02004189 data = "<module name=\"example-foo\""
4190 "xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
4191 "xmlns:foo=\"urn:example:foo\""
4192 "xmlns:myext=\"urn:example:extensions\">\n"
4193
Radek Krejci96e48da2020-09-04 13:18:06 +02004194 "<yang-version value=\"1\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02004195
4196 "<namespace uri=\"urn:example:foo\"/>\n"
4197 "<prefix value=\"foo\"/>\n"
4198
4199 "<import module=\"example-extensions\">\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004200 "<prefix value=\"myext\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02004201 "</import>\n"
4202
4203 "<list name=\"interface\">\n"
4204 "<key value=\"name\"/>\n"
4205 "<leaf name=\"name\">\n"
4206 "<type name=\"string\"/>\n"
4207 "</leaf>\n"
4208 "<leaf name=\"mtu\">\n"
4209 "<type name=\"uint32\"/>\n"
4210 "<description>\n"
4211 "<text>The MTU of the interface.</text>\n"
4212 "</description>\n"
4213 "<myext:c-define name=\"MY_MTU\"/>\n"
4214 "</leaf>\n"
4215 "</list>\n"
4216 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004217 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko405cc9e2020-12-01 12:01:27 +01004218 assert_int_equal(yin_parse_module(&yin_ctx, in, mod, &unres), LY_SUCCESS);
4219 lys_compile_unres_glob_erase(st->ctx, &unres);
David Sedlák8985a142019-07-31 16:43:06 +02004220 lys_module_free(mod, NULL);
4221 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004222 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02004223 mod = NULL;
4224 yin_ctx = NULL;
4225
4226 mod = calloc(1, sizeof *mod);
4227 mod->ctx = st->ctx;
David Sedlák6d781b62019-08-02 15:22:52 +02004228 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
Radek Krejci96e48da2020-09-04 13:18:06 +02004229 "<yang-version value=\"1\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004230 "<namespace uri=\"urn:example:foo\"/>\n"
4231 "<prefix value=\"foo\"/>\n"
4232 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004233 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko405cc9e2020-12-01 12:01:27 +01004234 assert_int_equal(yin_parse_module(&yin_ctx, in, mod, &unres), LY_SUCCESS);
4235 lys_compile_unres_glob_erase(st->ctx, &unres);
David Sedlák6d781b62019-08-02 15:22:52 +02004236 lys_module_free(mod, NULL);
4237 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004238 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02004239 mod = NULL;
4240 yin_ctx = NULL;
4241
4242
4243 mod = calloc(1, sizeof *mod);
4244 mod->ctx = st->ctx;
David Sedlák8985a142019-07-31 16:43:06 +02004245 data = "<submodule name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
4246 "</submodule>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004247 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko405cc9e2020-12-01 12:01:27 +01004248 assert_int_equal(yin_parse_module(&yin_ctx, in, mod, &unres), LY_EINVAL);
David Sedlák8985a142019-07-31 16:43:06 +02004249 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
4250 lys_module_free(mod, NULL);
4251 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004252 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02004253
David Sedlák6d781b62019-08-02 15:22:52 +02004254 mod = calloc(1, sizeof *mod);
4255 mod->ctx = st->ctx;
4256 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
Radek Krejci96e48da2020-09-04 13:18:06 +02004257 "<yang-version value=\"1\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004258 "<namespace uri=\"urn:example:foo\"/>\n"
4259 "<prefix value=\"foo\"/>\n"
4260 "</module>"
4261 "<module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004262 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko405cc9e2020-12-01 12:01:27 +01004263 assert_int_equal(yin_parse_module(&yin_ctx, in, mod, &unres), LY_EVALID);
David Sedlák6d781b62019-08-02 15:22:52 +02004264 logbuf_assert("Trailing garbage \"<module>\" after module, expected end-of-input. Line number 5.");
4265 lys_module_free(mod, NULL);
4266 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004267 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02004268 mod = NULL;
4269 yin_ctx = NULL;
4270
David Sedlák8985a142019-07-31 16:43:06 +02004271 st->finished_correctly = true;
4272}
4273
4274static void
4275test_yin_parse_submodule(void **state)
4276{
Radek Krejci3a4889a2020-05-19 17:01:58 +02004277 struct test_parser_yin_state *st = *state;
David Sedlák8985a142019-07-31 16:43:06 +02004278 const char *data;
Michal Vaskob36053d2020-03-26 15:49:30 +01004279 struct lys_yin_parser_ctx *yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02004280 struct lysp_submodule *submod = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02004281 struct ly_in *in;
David Sedlák8985a142019-07-31 16:43:06 +02004282
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004283 lydict_insert(st->ctx, "a", 0, &st->yin_ctx->parsed_mod->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004284
David Sedlák8985a142019-07-31 16:43:06 +02004285 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
4286 "<submodule name=\"asub\""
4287 "xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
4288 "xmlns:a=\"urn:a\">"
Radek Krejci96e48da2020-09-04 13:18:06 +02004289 "<yang-version value=\"1\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02004290 "<belongs-to module=\"a\">"
4291 "<prefix value=\"a_pref\"/>"
4292 "</belongs-to>"
4293 "<include module=\"atop\"/>"
4294 "<feature name=\"fox\"/>"
4295 "<notification name=\"bar-notif\">"
4296 "<if-feature name=\"bar\"/>"
4297 "</notification>"
4298 "<notification name=\"fox-notif\">"
4299 "<if-feature name=\"fox\"/>"
4300 "</notification>"
4301 "<augment target-node=\"/a_pref:top\">"
4302 "<if-feature name=\"bar\"/>"
4303 "<container name=\"bar-sub\"/>"
4304 "</augment>"
4305 "<augment target-node=\"/top\">"
4306 "<container name=\"bar-sub2\"/>"
4307 "</augment>"
4308 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004309 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004310 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, (struct lys_parser_ctx *)st->yin_ctx, in, &submod), LY_SUCCESS);
4311 lysp_module_free((struct lysp_module *)submod);
David Sedlák8985a142019-07-31 16:43:06 +02004312 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004313 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02004314 yin_ctx = NULL;
4315 submod = NULL;
4316
4317 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
David Sedlák6d781b62019-08-02 15:22:52 +02004318 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
Radek Krejci96e48da2020-09-04 13:18:06 +02004319 "<yang-version value=\"1\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004320 "<belongs-to module=\"a\">"
4321 "<prefix value=\"a_pref\"/>"
4322 "</belongs-to>"
4323 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004324 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004325 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, (struct lys_parser_ctx *)st->yin_ctx, in, &submod), LY_SUCCESS);
4326 lysp_module_free((struct lysp_module *)submod);
David Sedlák6d781b62019-08-02 15:22:52 +02004327 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004328 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02004329 yin_ctx = NULL;
4330 submod = NULL;
4331
4332 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
David Sedlák8985a142019-07-31 16:43:06 +02004333 "<module name=\"inval\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
4334 "</module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004335 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004336 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, (struct lys_parser_ctx *)st->yin_ctx, in, &submod), LY_EINVAL);
David Sedlák8985a142019-07-31 16:43:06 +02004337 logbuf_assert("Input data contains module in situation when a submodule is expected.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004338 lysp_module_free((struct lysp_module *)submod);
David Sedlák8985a142019-07-31 16:43:06 +02004339 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004340 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02004341 yin_ctx = NULL;
4342 submod = NULL;
4343
David Sedlák6d781b62019-08-02 15:22:52 +02004344 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
4345 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
Radek Krejci96e48da2020-09-04 13:18:06 +02004346 "<yang-version value=\"1\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004347 "<belongs-to module=\"a\">"
4348 "<prefix value=\"a_pref\"/>"
4349 "</belongs-to>"
4350 "</submodule>"
4351 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
Radek Krejci96e48da2020-09-04 13:18:06 +02004352 "<yang-version value=\"1\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004353 "<belongs-to module=\"a\">"
4354 "<prefix value=\"a_pref\"/>"
4355 "</belongs-to>"
4356 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004357 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004358 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, (struct lys_parser_ctx *)st->yin_ctx, in, &submod), LY_EVALID);
David Sedlák6d781b62019-08-02 15:22:52 +02004359 logbuf_assert("Trailing garbage \"<submodule name...\" after submodule, expected end-of-input. Line number 2.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004360 lysp_module_free((struct lysp_module *)submod);
David Sedlák6d781b62019-08-02 15:22:52 +02004361 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004362 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02004363 yin_ctx = NULL;
4364 submod = NULL;
4365
David Sedlák8985a142019-07-31 16:43:06 +02004366 st->finished_correctly = true;
4367}
4368
David Sedlák3b4db242018-10-19 16:11:01 +02004369int
4370main(void)
4371{
4372
4373 const struct CMUnitTest tests[] = {
David Sedlák8f7a1172019-06-20 14:42:18 +02004374 cmocka_unit_test_setup_teardown(test_yin_match_keyword, setup_f, teardown_f),
David Sedlákb1a78352019-06-28 16:16:29 +02004375 cmocka_unit_test_setup_teardown(test_yin_parse_element_generic, setup_f, teardown_f),
4376 cmocka_unit_test_setup_teardown(test_yin_parse_extension_instance, setup_f, teardown_f),
David Sedlák071f7662019-09-12 02:02:51 +02004377 cmocka_unit_test_setup_teardown(test_yin_parse_content, setup_f, teardown_f),
David Sedlák4a650532019-07-10 11:55:18 +02004378 cmocka_unit_test_setup_teardown(test_validate_value, setup_f, teardown_f),
David Sedlák32488102019-07-15 17:44:10 +02004379
David Sedlák8e7bda82019-07-16 17:57:50 +02004380 cmocka_unit_test(test_yin_match_argument_name),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004381 cmocka_unit_test_setup_teardown(test_enum_elem, setup_element_test, teardown_f),
4382 cmocka_unit_test_setup_teardown(test_bit_elem, setup_element_test, teardown_f),
4383 cmocka_unit_test_setup_teardown(test_meta_elem, setup_element_test, teardown_f),
4384 cmocka_unit_test_setup_teardown(test_import_elem, setup_element_test, teardown_f),
4385 cmocka_unit_test_setup_teardown(test_status_elem, setup_element_test, teardown_f),
4386 cmocka_unit_test_setup_teardown(test_ext_elem, setup_element_test, teardown_f),
4387 cmocka_unit_test_setup_teardown(test_yin_element_elem, setup_element_test, teardown_f),
4388 cmocka_unit_test_setup_teardown(test_yangversion_elem, setup_element_test, teardown_f),
4389 cmocka_unit_test_setup_teardown(test_mandatory_elem, setup_element_test, teardown_f),
4390 cmocka_unit_test_setup_teardown(test_argument_elem, setup_element_test, teardown_f),
4391 cmocka_unit_test_setup_teardown(test_base_elem, setup_element_test, teardown_f),
4392 cmocka_unit_test_setup_teardown(test_belongsto_elem, setup_element_test, teardown_f),
4393 cmocka_unit_test_setup_teardown(test_config_elem, setup_element_test, teardown_f),
4394 cmocka_unit_test_setup_teardown(test_default_elem, setup_element_test, teardown_f),
4395 cmocka_unit_test_setup_teardown(test_err_app_tag_elem, setup_element_test, teardown_f),
4396 cmocka_unit_test_setup_teardown(test_err_msg_elem, setup_element_test, teardown_f),
4397 cmocka_unit_test_setup_teardown(test_fracdigits_elem, setup_element_test, teardown_f),
4398 cmocka_unit_test_setup_teardown(test_iffeature_elem, setup_element_test, teardown_f),
4399 cmocka_unit_test_setup_teardown(test_length_elem, setup_element_test, teardown_f),
4400 cmocka_unit_test_setup_teardown(test_modifier_elem, setup_element_test, teardown_f),
4401 cmocka_unit_test_setup_teardown(test_namespace_elem, setup_element_test, teardown_f),
4402 cmocka_unit_test_setup_teardown(test_pattern_elem, setup_element_test, teardown_f),
4403 cmocka_unit_test_setup_teardown(test_value_position_elem, setup_element_test, teardown_f),
4404 cmocka_unit_test_setup_teardown(test_prefix_elem, setup_element_test, teardown_f),
4405 cmocka_unit_test_setup_teardown(test_range_elem, setup_element_test, teardown_f),
4406 cmocka_unit_test_setup_teardown(test_reqinstance_elem, setup_element_test, teardown_f),
4407 cmocka_unit_test_setup_teardown(test_revision_date_elem, setup_element_test, teardown_f),
4408 cmocka_unit_test_setup_teardown(test_unique_elem, setup_element_test, teardown_f),
4409 cmocka_unit_test_setup_teardown(test_units_elem, setup_element_test, teardown_f),
4410 cmocka_unit_test_setup_teardown(test_when_elem, setup_element_test, teardown_f),
4411 cmocka_unit_test_setup_teardown(test_yin_text_value_elem, setup_element_test, teardown_f),
4412 cmocka_unit_test_setup_teardown(test_type_elem, setup_element_test, teardown_f),
4413 cmocka_unit_test_setup_teardown(test_max_elems_elem, setup_element_test, teardown_f),
4414 cmocka_unit_test_setup_teardown(test_min_elems_elem, setup_element_test, teardown_f),
4415 cmocka_unit_test_setup_teardown(test_ordby_elem, setup_element_test, teardown_f),
4416 cmocka_unit_test_setup_teardown(test_any_elem, setup_element_test, teardown_f),
4417 cmocka_unit_test_setup_teardown(test_leaf_elem, setup_element_test, teardown_f),
4418 cmocka_unit_test_setup_teardown(test_leaf_list_elem, setup_element_test, teardown_f),
4419 cmocka_unit_test_setup_teardown(test_presence_elem, setup_element_test, teardown_f),
4420 cmocka_unit_test_setup_teardown(test_key_elem, setup_element_test, teardown_f),
4421 cmocka_unit_test_setup_teardown(test_typedef_elem, setup_element_test, teardown_f),
4422 cmocka_unit_test_setup_teardown(test_refine_elem, setup_element_test, teardown_f),
4423 cmocka_unit_test_setup_teardown(test_uses_elem, setup_element_test, teardown_f),
4424 cmocka_unit_test_setup_teardown(test_revision_elem, setup_element_test, teardown_f),
4425 cmocka_unit_test_setup_teardown(test_include_elem, setup_element_test, teardown_f),
4426 cmocka_unit_test_setup_teardown(test_list_elem, setup_element_test, teardown_f),
4427 cmocka_unit_test_setup_teardown(test_notification_elem, setup_element_test, teardown_f),
4428 cmocka_unit_test_setup_teardown(test_grouping_elem, setup_element_test, teardown_f),
4429 cmocka_unit_test_setup_teardown(test_container_elem, setup_element_test, teardown_f),
4430 cmocka_unit_test_setup_teardown(test_case_elem, setup_element_test, teardown_f),
4431 cmocka_unit_test_setup_teardown(test_choice_elem, setup_element_test, teardown_f),
4432 cmocka_unit_test_setup_teardown(test_inout_elem, setup_element_test, teardown_f),
4433 cmocka_unit_test_setup_teardown(test_action_elem, setup_element_test, teardown_f),
4434 cmocka_unit_test_setup_teardown(test_augment_elem, setup_element_test, teardown_f),
4435 cmocka_unit_test_setup_teardown(test_deviate_elem, setup_element_test, teardown_f),
4436 cmocka_unit_test_setup_teardown(test_deviation_elem, setup_element_test, teardown_f),
4437 cmocka_unit_test_setup_teardown(test_module_elem, setup_element_test, teardown_f),
4438 cmocka_unit_test_setup_teardown(test_submodule_elem, setup_element_test, teardown_f),
David Sedlák8985a142019-07-31 16:43:06 +02004439
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004440 cmocka_unit_test_setup_teardown(test_yin_parse_module, setup_f, teardown_f),
4441 cmocka_unit_test_setup_teardown(test_yin_parse_submodule, setup_f, teardown_f),
David Sedlák3b4db242018-10-19 16:11:01 +02004442 };
4443
David Sedlák8e7bda82019-07-16 17:57:50 +02004444 return cmocka_run_group_tests(tests, setup_ly_ctx, destroy_ly_ctx);
David Sedlák3b4db242018-10-19 16:11:01 +02004445}