blob: fe33de4a618ffc92930d9d8e0b6859058f854aab [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
Radek Krejcib4ac5a92020-11-23 17:54:33 +010015#include <stdbool.h>
David Sedlák3b4db242018-10-19 16:11:01 +020016#include <stdio.h>
17#include <string.h>
18
Radek Krejci70593c12020-06-13 20:48:09 +020019#include "common.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020020#include "in.h"
Radek Krejci70593c12020-06-13 20:48:09 +020021#include "parser_internal.h"
Michal Vasko405cc9e2020-12-01 12:01:27 +010022#include "schema_compile.h"
Radek Krejci70593c12020-06-13 20:48:09 +020023#include "tree_schema.h"
24#include "tree_schema_internal.h"
Radek Krejcib4ac5a92020-11-23 17:54:33 +010025#include "utests.h"
Radek Krejci70593c12020-06-13 20:48:09 +020026#include "xml.h"
27#include "xpath.h"
David Sedlák3b4db242018-10-19 16:11:01 +020028
Michal Vaskoafac7822020-10-20 14:22:26 +020029/* copied from parser_yin.c */
30enum yin_argument {
31 YIN_ARG_UNKNOWN = 0, /**< parsed argument can not be matched with any supported yin argument keyword */
32 YIN_ARG_NAME, /**< argument name */
33 YIN_ARG_TARGET_NODE, /**< argument target-node */
34 YIN_ARG_MODULE, /**< argument module */
35 YIN_ARG_VALUE, /**< argument value */
36 YIN_ARG_TEXT, /**< argument text */
37 YIN_ARG_CONDITION, /**< argument condition */
38 YIN_ARG_URI, /**< argument uri */
39 YIN_ARG_DATE, /**< argument data */
40 YIN_ARG_TAG, /**< argument tag */
41 YIN_ARG_NONE /**< empty (special value) */
42};
43
44struct yin_subelement {
45 enum ly_stmt type; /**< type of keyword */
46 void *dest; /**< meta infromation passed to responsible function (mostly information about where parsed subelement should be stored) */
47 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 */
48};
49
50struct import_meta {
51 const char *prefix; /**< module prefix. */
52 struct lysp_import **imports; /**< imports to add to. */
53};
54
55struct yin_argument_meta {
56 uint16_t *flags; /**< Argument flags */
57 const char **argument; /**< Argument value */
58};
59
60struct tree_node_meta {
61 struct lysp_node *parent; /**< parent node */
62 struct lysp_node **nodes; /**< linked list of siblings */
63};
64
65struct include_meta {
66 const char *name; /**< Module/submodule name. */
67 struct lysp_include **includes; /**< [Sized array](@ref sizedarrays) of parsed includes to add to. */
68};
69
70struct inout_meta {
71 struct lysp_node *parent; /**< Parent node. */
72 struct lysp_action_inout *inout_p; /**< inout_p Input/output pointer to write to. */
73};
74
75struct minmax_dev_meta {
76 uint32_t *lim; /**< min/max value to write to. */
77 uint16_t *flags; /**< min/max flags to write to. */
78 struct lysp_ext_instance **exts; /**< extension instances to add to. */
79};
80
81#define YIN_SUBELEM_MANDATORY 0x01
82#define YIN_SUBELEM_UNIQUE 0x02
83#define YIN_SUBELEM_FIRST 0x04
84#define YIN_SUBELEM_VER2 0x08
85
86#define YIN_SUBELEM_PARSED 0x80
87
David Sedlák555c7202019-07-04 12:14:12 +020088/* prototypes of static functions */
Michal Vaskoafac7822020-10-20 14:22:26 +020089enum yin_argument yin_match_argument_name(const char *name, size_t len);
90LY_ERR yin_parse_content(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size,
91 enum ly_stmt current_element, const char **text_content, struct lysp_ext_instance **exts);
92LY_ERR yin_validate_value(struct lys_yin_parser_ctx *ctx, enum yang_arg val_type);
93enum ly_stmt yin_match_keyword(struct lys_yin_parser_ctx *ctx, const char *name, size_t name_len,
94 const char *prefix, size_t prefix_len, enum ly_stmt parrent);
95LY_ERR yin_parse_extension_instance(struct lys_yin_parser_ctx *ctx, LYEXT_SUBSTMT subelem, LY_ARRAY_COUNT_TYPE subelem_index,
96 struct lysp_ext_instance **exts);
97LY_ERR yin_parse_element_generic(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, struct lysp_stmt **element);
98LY_ERR yin_parse_mod(struct lys_yin_parser_ctx *ctx, struct lysp_module *mod);
99LY_ERR yin_parse_submod(struct lys_yin_parser_ctx *ctx, struct lysp_submodule *submod);
100
David Sedlák555c7202019-07-04 12:14:12 +0200101void lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext);
David Sedlák986cb412019-07-04 13:10:11 +0200102void lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext);
David Sedlák32eee7b2019-07-09 12:38:44 +0200103void lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when);
David Sedlák32488102019-07-15 17:44:10 +0200104void lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type);
David Sedlák8a83bbb2019-07-18 14:46:00 +0200105void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
David Sedlák04e17b22019-07-19 15:29:48 +0200106void lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf);
David Sedlákd2d676a2019-07-22 11:28:19 +0200107void lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref);
David Sedlákaa854b02019-07-22 14:17:10 +0200108void lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev);
David Sedlák0c2bab92019-07-22 15:33:19 +0200109void lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include);
David Sedlák5e13dea2019-07-22 16:06:45 +0200110void lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat);
David Sedlák28794f22019-07-22 16:45:00 +0200111void lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident);
David Sedlák031b9e72019-07-23 15:19:37 +0200112void lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif);
David Sedláke3ce9ef2019-07-23 16:34:30 +0200113void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp);
David Sedlák05404f62019-07-24 14:11:53 +0200114void lysp_action_inout_free(struct ly_ctx *ctx, struct lysp_action_inout *inout);
David Sedlák85d0eca2019-07-24 15:15:21 +0200115void lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action);
David Sedlák992fb7c2019-07-24 16:51:01 +0200116void lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment);
David Sedlák4ffcec82019-07-25 15:10:21 +0200117void lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d);
David Sedlák8b754462019-07-25 16:22:13 +0200118void lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev);
David Sedlák298ff6d2019-07-26 14:29:03 +0200119void lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import);
David Sedlák555c7202019-07-04 12:14:12 +0200120
David Sedláke6cd89e2019-08-07 12:46:02 +0200121/* wrapping element used for mocking has nothing to do with real module structure */
122#define ELEMENT_WRAPPER_START "<status xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
123#define ELEMENT_WRAPPER_END "</status>"
124
Radek Krejci3a4889a2020-05-19 17:01:58 +0200125struct test_parser_yin_state {
David Sedlák3b4db242018-10-19 16:11:01 +0200126 struct ly_ctx *ctx;
Michal Vaskob36053d2020-03-26 15:49:30 +0100127 struct lys_yin_parser_ctx *yin_ctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200128 struct ly_in *in;
David Sedlák79e50cb2019-06-05 16:33:09 +0200129 bool finished_correctly;
David Sedlák68a1af12019-03-08 13:46:54 +0100130};
David Sedlák872c7b42018-10-26 13:15:20 +0200131
David Sedlák79e50cb2019-06-05 16:33:09 +0200132#define BUFSIZE 1024
133char logbuf[BUFSIZE] = {0};
134int store = -1; /* negative for infinite logging, positive for limited logging */
135
136/* set to 0 to printing error messages to stderr instead of checking them in code */
David Sedláke0ef1c62019-09-13 10:05:55 +0200137#define ENABLE_LOGGER_CHECKING 1
David Sedlák79e50cb2019-06-05 16:33:09 +0200138
139#if ENABLE_LOGGER_CHECKING
140static void
141logger(LY_LOG_LEVEL level, const char *msg, const char *path)
142{
143 (void) level; /* unused */
144 if (store) {
145 if (path && path[0]) {
146 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
147 } else {
148 strncpy(logbuf, msg, BUFSIZE - 1);
149 }
150 if (store > 0) {
151 --store;
152 }
153 }
154}
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100155
David Sedlák79e50cb2019-06-05 16:33:09 +0200156#endif
157
158#if ENABLE_LOGGER_CHECKING
159# define logbuf_assert(str) assert_string_equal(logbuf, str)
160#else
161# define logbuf_assert(str)
162#endif
163
164#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
165 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
166 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
167 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
168 CLEANUP
169
David Sedlák8e7bda82019-07-16 17:57:50 +0200170int
171setup_ly_ctx(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +0100172{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200173 struct test_parser_yin_state *st = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +0200174
David Sedlák68a1af12019-03-08 13:46:54 +0100175 /* allocate state variable */
176 (*state) = st = calloc(1, sizeof(*st));
177 if (!st) {
178 fprintf(stderr, "Memmory allocation failed");
179 return EXIT_FAILURE;
180 }
David Sedlák872c7b42018-10-26 13:15:20 +0200181
David Sedlák68a1af12019-03-08 13:46:54 +0100182 /* create new libyang context */
183 ly_ctx_new(NULL, 0, &st->ctx);
David Sedlák872c7b42018-10-26 13:15:20 +0200184
David Sedlák8e7bda82019-07-16 17:57:50 +0200185 return EXIT_SUCCESS;
186}
187
188int
189destroy_ly_ctx(void **state)
190{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200191 struct test_parser_yin_state *st = *state;
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100192
David Sedlák8e7bda82019-07-16 17:57:50 +0200193 ly_ctx_destroy(st->ctx, NULL);
194 free(st);
195
196 return EXIT_SUCCESS;
197}
198
199static int
200setup_f(void **state)
201{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200202 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +0200203
204#if ENABLE_LOGGER_CHECKING
205 /* setup logger */
206 ly_set_log_clb(logger, 1);
207#endif
208
David Sedlák619db942019-07-03 14:47:30 +0200209 /* allocate parser context */
David Sedlákda8ffa32019-07-08 14:17:10 +0200210 st->yin_ctx = calloc(1, sizeof(*st->yin_ctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100211 st->yin_ctx->format = LYS_IN_YIN;
David Sedlák8f5bce02019-06-03 16:41:08 +0200212
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200213 /* allocate new parsed module */
214 st->yin_ctx->parsed_mod = calloc(1, sizeof *st->yin_ctx->parsed_mod);
215
216 /* allocate new module */
217 st->yin_ctx->parsed_mod->mod = calloc(1, sizeof *st->yin_ctx->parsed_mod->mod);
218 st->yin_ctx->parsed_mod->mod->ctx = st->ctx;
219 st->yin_ctx->parsed_mod->mod->parsed = st->yin_ctx->parsed_mod;
220
Michal Vasko63f3d842020-07-08 10:10:14 +0200221 st->in = NULL;
222
David Sedlák68a1af12019-03-08 13:46:54 +0100223 return EXIT_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +0200224}
225
226static int
David Sedlák68a1af12019-03-08 13:46:54 +0100227teardown_f(void **state)
228{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200229 struct test_parser_yin_state *st = *(struct test_parser_yin_state **)state;
David Sedlák68a1af12019-03-08 13:46:54 +0100230
David Sedlák79e50cb2019-06-05 16:33:09 +0200231#if ENABLE_LOGGER_CHECKING
232 /* teardown logger */
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100233 if (!st->finished_correctly && (logbuf[0] != '\0')) {
David Sedlák79e50cb2019-06-05 16:33:09 +0200234 fprintf(stderr, "%s\n", logbuf);
235 }
236#endif
237
Michal Vaskob36053d2020-03-26 15:49:30 +0100238 lyxml_ctx_free(st->yin_ctx->xmlctx);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200239 lys_module_free(st->yin_ctx->parsed_mod->mod, NULL);
David Sedlákda8ffa32019-07-08 14:17:10 +0200240 free(st->yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200241 ly_in_free(st->in, 0);
David Sedlák68a1af12019-03-08 13:46:54 +0100242
243 return EXIT_SUCCESS;
244}
245
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100246static struct test_parser_yin_state *
David Sedlák392af4f2019-06-04 16:02:42 +0200247reset_state(void **state)
248{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200249 ((struct test_parser_yin_state *)*state)->finished_correctly = true;
David Sedlák555c7202019-07-04 12:14:12 +0200250 logbuf[0] = '\0';
David Sedlák392af4f2019-06-04 16:02:42 +0200251 teardown_f(state);
252 setup_f(state);
253
254 return *state;
255}
256
David Sedlák79e50cb2019-06-05 16:33:09 +0200257void
258logbuf_clean(void)
259{
260 logbuf[0] = '\0';
261}
262
David Sedlák8985a142019-07-31 16:43:06 +0200263static int
David Sedlák8985a142019-07-31 16:43:06 +0200264setup_element_test(void **state)
265{
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200266 struct test_parser_yin_state *st;
David Sedlák8985a142019-07-31 16:43:06 +0200267
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200268 setup_f(state);
269 st = *state;
David Sedlák8985a142019-07-31 16:43:06 +0200270
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200271 lydict_insert(st->ctx, "module-name", 0, &st->yin_ctx->parsed_mod->mod->name);
David Sedlák8985a142019-07-31 16:43:06 +0200272
273 return EXIT_SUCCESS;
274}
275
David Sedlák68a1af12019-03-08 13:46:54 +0100276static void
David Sedlák1bccdfa2019-06-17 15:55:27 +0200277test_yin_match_keyword(void **state)
David Sedlák3b4db242018-10-19 16:11:01 +0200278{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200279 struct test_parser_yin_state *st = *state;
David Sedlák3b4db242018-10-19 16:11:01 +0200280
Michal Vaskob36053d2020-03-26 15:49:30 +0100281 const char *prefix;
282 size_t prefix_len;
David Sedlák8f7a1172019-06-20 14:42:18 +0200283 /* create mock yin namespace in xml context */
284 const char *data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" />";
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100285
Michal Vasko63f3d842020-07-08 10:10:14 +0200286 ly_in_new_memory(data, &st->in);
287 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100288 prefix = st->yin_ctx->xmlctx->prefix;
289 prefix_len = st->yin_ctx->xmlctx->prefix_len;
David Sedlák8f7a1172019-06-20 14:42:18 +0200290
Radek Krejcid6b76452019-09-03 17:03:03 +0200291 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydatax", strlen("anydatax"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
292 assert_int_equal(yin_match_keyword(st->yin_ctx, "asdasd", strlen("asdasd"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
293 assert_int_equal(yin_match_keyword(st->yin_ctx, "", 0, prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
294 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydata", strlen("anydata"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ANYDATA);
295 assert_int_equal(yin_match_keyword(st->yin_ctx, "anyxml", strlen("anyxml"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ANYXML);
296 assert_int_equal(yin_match_keyword(st->yin_ctx, "argument", strlen("argument"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ARGUMENT);
297 assert_int_equal(yin_match_keyword(st->yin_ctx, "augment", strlen("augment"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_AUGMENT);
298 assert_int_equal(yin_match_keyword(st->yin_ctx, "base", strlen("base"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BASE);
299 assert_int_equal(yin_match_keyword(st->yin_ctx, "belongs-to", strlen("belongs-to"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BELONGS_TO);
300 assert_int_equal(yin_match_keyword(st->yin_ctx, "bit", strlen("bit"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BIT);
301 assert_int_equal(yin_match_keyword(st->yin_ctx, "case", strlen("case"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CASE);
302 assert_int_equal(yin_match_keyword(st->yin_ctx, "choice", strlen("choice"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CHOICE);
303 assert_int_equal(yin_match_keyword(st->yin_ctx, "config", strlen("config"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONFIG);
304 assert_int_equal(yin_match_keyword(st->yin_ctx, "contact", strlen("contact"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONTACT);
305 assert_int_equal(yin_match_keyword(st->yin_ctx, "container", strlen("container"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONTAINER);
306 assert_int_equal(yin_match_keyword(st->yin_ctx, "default", strlen("default"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEFAULT);
307 assert_int_equal(yin_match_keyword(st->yin_ctx, "description", strlen("description"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DESCRIPTION);
308 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviate", strlen("deviate"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEVIATE);
309 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviation", strlen("deviation"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEVIATION);
310 assert_int_equal(yin_match_keyword(st->yin_ctx, "enum", strlen("enum"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ENUM);
311 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);
312 assert_int_equal(yin_match_keyword(st->yin_ctx, "error-message", strlen("error-message"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ERROR_MESSAGE);
313 assert_int_equal(yin_match_keyword(st->yin_ctx, "extension", strlen("extension"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_EXTENSION);
314 assert_int_equal(yin_match_keyword(st->yin_ctx, "feature", strlen("feature"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_FEATURE);
315 assert_int_equal(yin_match_keyword(st->yin_ctx, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_FRACTION_DIGITS);
316 assert_int_equal(yin_match_keyword(st->yin_ctx, "grouping", strlen("grouping"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_GROUPING);
317 assert_int_equal(yin_match_keyword(st->yin_ctx, "identity", strlen("identity"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IDENTITY);
318 assert_int_equal(yin_match_keyword(st->yin_ctx, "if-feature", strlen("if-feature"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IF_FEATURE);
319 assert_int_equal(yin_match_keyword(st->yin_ctx, "import", strlen("import"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IMPORT);
320 assert_int_equal(yin_match_keyword(st->yin_ctx, "include", strlen("include"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_INCLUDE);
321 assert_int_equal(yin_match_keyword(st->yin_ctx, "input", strlen("input"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_INPUT);
322 assert_int_equal(yin_match_keyword(st->yin_ctx, "key", strlen("key"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_KEY);
323 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf", strlen("leaf"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LEAF);
324 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf-list", strlen("leaf-list"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LEAF_LIST);
325 assert_int_equal(yin_match_keyword(st->yin_ctx, "length", strlen("length"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LENGTH);
326 assert_int_equal(yin_match_keyword(st->yin_ctx, "list", strlen("list"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LIST);
327 assert_int_equal(yin_match_keyword(st->yin_ctx, "mandatory", strlen("mandatory"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MANDATORY);
328 assert_int_equal(yin_match_keyword(st->yin_ctx, "max-elements", strlen("max-elements"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MAX_ELEMENTS);
329 assert_int_equal(yin_match_keyword(st->yin_ctx, "min-elements", strlen("min-elements"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MIN_ELEMENTS);
330 assert_int_equal(yin_match_keyword(st->yin_ctx, "modifier", strlen("modifier"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MODIFIER);
331 assert_int_equal(yin_match_keyword(st->yin_ctx, "module", strlen("module"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MODULE);
332 assert_int_equal(yin_match_keyword(st->yin_ctx, "must", strlen("must"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MUST);
333 assert_int_equal(yin_match_keyword(st->yin_ctx, "namespace", strlen("namespace"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NAMESPACE);
334 assert_int_equal(yin_match_keyword(st->yin_ctx, "notification", strlen("notification"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NOTIFICATION);
335 assert_int_equal(yin_match_keyword(st->yin_ctx, "ordered-by", strlen("ordered-by"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ORDERED_BY);
336 assert_int_equal(yin_match_keyword(st->yin_ctx, "organization", strlen("organization"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ORGANIZATION);
337 assert_int_equal(yin_match_keyword(st->yin_ctx, "output", strlen("output"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_OUTPUT);
338 assert_int_equal(yin_match_keyword(st->yin_ctx, "path", strlen("path"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PATH);
339 assert_int_equal(yin_match_keyword(st->yin_ctx, "pattern", strlen("pattern"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PATTERN);
340 assert_int_equal(yin_match_keyword(st->yin_ctx, "position", strlen("position"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_POSITION);
341 assert_int_equal(yin_match_keyword(st->yin_ctx, "prefix", strlen("prefix"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PREFIX);
342 assert_int_equal(yin_match_keyword(st->yin_ctx, "presence", strlen("presence"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PRESENCE);
343 assert_int_equal(yin_match_keyword(st->yin_ctx, "range", strlen("range"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_RANGE);
344 assert_int_equal(yin_match_keyword(st->yin_ctx, "reference", strlen("reference"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REFERENCE);
345 assert_int_equal(yin_match_keyword(st->yin_ctx, "refine", strlen("refine"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REFINE);
346 assert_int_equal(yin_match_keyword(st->yin_ctx, "require-instance", strlen("require-instance"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REQUIRE_INSTANCE);
347 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision", strlen("revision"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REVISION);
348 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision-date", strlen("revision-date"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REVISION_DATE);
349 assert_int_equal(yin_match_keyword(st->yin_ctx, "rpc", strlen("rpc"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_RPC);
350 assert_int_equal(yin_match_keyword(st->yin_ctx, "status", strlen("status"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_STATUS);
351 assert_int_equal(yin_match_keyword(st->yin_ctx, "submodule", strlen("submodule"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_SUBMODULE);
352 assert_int_equal(yin_match_keyword(st->yin_ctx, "type", strlen("type"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_TYPE);
353 assert_int_equal(yin_match_keyword(st->yin_ctx, "typedef", strlen("typedef"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_TYPEDEF);
354 assert_int_equal(yin_match_keyword(st->yin_ctx, "unique", strlen("unique"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_UNIQUE);
355 assert_int_equal(yin_match_keyword(st->yin_ctx, "units", strlen("units"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_UNITS);
356 assert_int_equal(yin_match_keyword(st->yin_ctx, "uses", strlen("uses"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_USES);
357 assert_int_equal(yin_match_keyword(st->yin_ctx, "value", strlen("value"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_VALUE);
358 assert_int_equal(yin_match_keyword(st->yin_ctx, "when", strlen("when"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_WHEN);
359 assert_int_equal(yin_match_keyword(st->yin_ctx, "yang-version", strlen("yang-version"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_YANG_VERSION);
360 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 +0200361
362 st->finished_correctly = true;
David Sedlák872c7b42018-10-26 13:15:20 +0200363}
David Sedlák3b4db242018-10-19 16:11:01 +0200364
David Sedlák872c7b42018-10-26 13:15:20 +0200365static void
David Sedlák060b00e2019-06-19 11:12:06 +0200366test_yin_match_argument_name(void **state)
David Sedlák872c7b42018-10-26 13:15:20 +0200367{
David Sedlák68a1af12019-03-08 13:46:54 +0100368 (void)state; /* unused */
David Sedlák872c7b42018-10-26 13:15:20 +0200369
David Sedlák060b00e2019-06-19 11:12:06 +0200370 assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN);
371 assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN);
372 assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN);
373 assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION);
374 assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE);
375 assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE);
376 assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME);
377 assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG);
378 assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
379 assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT);
380 assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI);
381 assert_int_equal(yin_match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200382}
383
David Sedlák68a1af12019-03-08 13:46:54 +0100384static void
David Sedlákb1a78352019-06-28 16:16:29 +0200385test_yin_parse_element_generic(void **state)
386{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200387 struct test_parser_yin_state *st = *state;
David Sedlákb1a78352019-06-28 16:16:29 +0200388 struct lysp_ext_instance exts;
David Sedlákb1a78352019-06-28 16:16:29 +0200389 LY_ERR ret;
390
391 memset(&exts, 0, sizeof(exts));
392
David Sedlákb0ca07d2019-09-11 11:54:05 +0200393 const char *data = "<myext:elem attr=\"value\" xmlns:myext=\"urn:example:extensions\">text_value</myext:elem>";
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100394
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>";
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100431
Michal Vasko63f3d842020-07-08 10:10:14 +0200432 ly_in_new_memory(data, &st->in);
433 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100434
435 ret = yin_parse_extension_instance(st->yin_ctx, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200436 assert_int_equal(ret, LY_SUCCESS);
David Sedláke0ef1c62019-09-13 10:05:55 +0200437 assert_string_equal(exts->name, "urn:example:extensions:ext");
David Sedlákb1a78352019-06-28 16:16:29 +0200438 assert_int_equal(exts->insubstmt_index, 0);
439 assert_true(exts->insubstmt == LYEXT_SUBSTMT_CONTACT);
440 assert_true(exts->yin & LYS_YIN);
441 assert_string_equal(exts->child->stmt, "value1");
442 assert_string_equal(exts->child->arg, "test");
443 assert_null(exts->child->child);
444 assert_true(exts->child->flags & LYS_YIN_ATTR);
445 assert_string_equal(exts->child->next->stmt, "value");
446 assert_string_equal(exts->child->next->arg, "test2");
447 assert_null(exts->child->next->child);
448 assert_true(exts->child->next->flags & LYS_YIN_ATTR);
449
David Sedláke0ef1c62019-09-13 10:05:55 +0200450 assert_string_equal(exts->child->next->next->stmt, "urn:example:extensions:subelem");
David Sedlákb1a78352019-06-28 16:16:29 +0200451 assert_string_equal(exts->child->next->next->arg, "text");
452 assert_null(exts->child->next->next->child);
453 assert_null(exts->child->next->next->next);
454 assert_false(exts->child->next->next->flags & LYS_YIN_ATTR);
David Sedlákb1a78352019-06-28 16:16:29 +0200455 lysp_ext_instance_free(st->ctx, exts);
456 LY_ARRAY_FREE(exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200457 exts = NULL;
David Sedlákf250ecf2019-07-01 11:02:05 +0200458 st = reset_state(state);
459
David Sedlákb0ca07d2019-09-11 11:54:05 +0200460 data = "<myext:extension-elem xmlns:myext=\"urn:example:extensions\" />";
Michal Vasko63f3d842020-07-08 10:10:14 +0200461 ly_in_new_memory(data, &st->in);
462 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100463
464 ret = yin_parse_extension_instance(st->yin_ctx, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200465 assert_int_equal(ret, LY_SUCCESS);
David Sedláke0ef1c62019-09-13 10:05:55 +0200466 assert_string_equal(exts->name, "urn:example:extensions:extension-elem");
David Sedlákf250ecf2019-07-01 11:02:05 +0200467 assert_null(exts->argument);
468 assert_null(exts->child);
469 assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT);
470 assert_int_equal(exts->insubstmt_index, 0);
471 assert_true(exts->yin & LYS_YIN);
David Sedlákf250ecf2019-07-01 11:02:05 +0200472 lysp_ext_instance_free(st->ctx, exts);
473 LY_ARRAY_FREE(exts);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200474 exts = NULL;
David Sedlákadd0c2e2019-08-16 10:49:12 +0200475 st = reset_state(state);
476
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100477 data =
478 "<myext:ext attr1=\"text1\" attr2=\"text2\" xmlns:myext=\"urn:example:extensions\">\n"
479 " <myext:ext-sub1/>\n"
480 " <myext:ext-sub2 sattr1=\"stext2\">\n"
481 " <myext:ext-sub21>\n"
482 " <myext:ext-sub211 sattr21=\"text21\"/>\n"
483 " </myext:ext-sub21>\n"
484 " </myext:ext-sub2>\n"
485 " <myext:ext-sub3 attr3=\"text3\"></myext:ext-sub3>\n"
486 "</myext:ext>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200487 ly_in_new_memory(data, &st->in);
488 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100489
490 ret = yin_parse_extension_instance(st->yin_ctx, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200491 assert_int_equal(ret, LY_SUCCESS);
492
David Sedláke0ef1c62019-09-13 10:05:55 +0200493 assert_string_equal(exts->name, "urn:example:extensions:ext");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200494 assert_null(exts->argument);
495 assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT);
496 assert_int_equal(exts->insubstmt_index, 0);
497 assert_true(exts->yin & LYS_YIN);
498 assert_string_equal(exts->child->stmt, "attr1");
499 assert_string_equal(exts->child->arg, "text1");
500 assert_null(exts->child->child);
501 assert_true(exts->child->flags & LYS_YIN_ATTR);
502
503 assert_string_equal(exts->child->next->stmt, "attr2");
504 assert_string_equal(exts->child->next->arg, "text2");
505 assert_null(exts->child->next->child);
506 assert_true(exts->child->next->flags & LYS_YIN_ATTR);
507
David Sedláke0ef1c62019-09-13 10:05:55 +0200508 assert_string_equal(exts->child->next->next->stmt, "urn:example:extensions:ext-sub1");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200509 assert_null(exts->child->next->next->arg);
510 assert_null(exts->child->next->next->child);
511 assert_int_equal(exts->child->next->next->flags, 0);
512
David Sedláke0ef1c62019-09-13 10:05:55 +0200513 assert_string_equal(exts->child->next->next->next->stmt, "urn:example:extensions:ext-sub2");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200514 assert_null(exts->child->next->next->next->arg);
515 assert_int_equal(exts->child->next->next->next->flags, 0);
516 assert_string_equal(exts->child->next->next->next->child->stmt, "sattr1");
517 assert_string_equal(exts->child->next->next->next->child->arg, "stext2");
518 assert_null(exts->child->next->next->next->child->child);
519 assert_true(exts->child->next->next->next->child->flags & LYS_YIN_ATTR);
520
David Sedláke0ef1c62019-09-13 10:05:55 +0200521 assert_string_equal(exts->child->next->next->next->child->next->stmt, "urn:example:extensions:ext-sub21");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200522 assert_null(exts->child->next->next->next->child->next->arg);
523 assert_null(exts->child->next->next->next->child->next->next);
524 assert_int_equal(exts->child->next->next->next->child->next->flags, 0);
525
David Sedláke0ef1c62019-09-13 10:05:55 +0200526 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 +0200527 assert_null(exts->child->next->next->next->child->next->child->arg);
528 assert_int_equal(exts->child->next->next->next->child->next->child->flags, 0);
529 assert_null(exts->child->next->next->next->child->next->child->next);
530
531 assert_string_equal(exts->child->next->next->next->child->next->child->child->stmt, "sattr21");
532 assert_string_equal(exts->child->next->next->next->child->next->child->child->arg, "text21");
533 assert_null(exts->child->next->next->next->child->next->child->child->next);
534 assert_null(exts->child->next->next->next->child->next->child->child->child);
535 assert_true(exts->child->next->next->next->child->next->child->child->flags & LYS_YIN_ATTR);
536
David Sedláke0ef1c62019-09-13 10:05:55 +0200537 assert_string_equal(exts->child->next->next->next->next->stmt, "urn:example:extensions:ext-sub3");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200538 assert_null(exts->child->next->next->next->next->arg);
539 assert_null(exts->child->next->next->next->next->next);
540 assert_int_equal(exts->child->next->next->next->next->flags, 0);
541
542 assert_string_equal(exts->child->next->next->next->next->child->stmt, "attr3");
543 assert_string_equal(exts->child->next->next->next->next->child->arg, "text3");
544 assert_null(exts->child->next->next->next->next->child->next);
545 assert_null(exts->child->next->next->next->next->child->child);
546 assert_true(exts->child->next->next->next->next->child->flags & LYS_YIN_ATTR);
547
David Sedlákadd0c2e2019-08-16 10:49:12 +0200548 lysp_ext_instance_free(st->ctx, exts);
549 LY_ARRAY_FREE(exts);
550 exts = NULL;
David Sedlákaa98bba2019-09-12 11:52:14 +0200551 st = reset_state(state);
552
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100553 data =
554 "<myext:extension-elem xmlns:myext=\"urn:example:extensions\" xmlns:yin=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
555 " <yin:action name=\"act-name\" pre:prefixed=\"ignored\"/>\n"
556 " <yin:augment target-node=\"target\"/>\n"
557 " <yin:status value=\"value\"/>\n"
558 " <yin:include module=\"mod\"/>\n"
559 " <yin:input />\n"
560 " <yin:must condition=\"cond\"/>\n"
561 " <yin:namespace uri=\"uri\"/>\n"
562 " <yin:revision date=\"data\"/>\n"
563 " <yin:unique tag=\"tag\"/>\n"
564 " <yin:description><yin:text>contact-val</yin:text></yin:description>\n"
565 " <yin:error-message><yin:value>err-msg</yin:value></yin:error-message>\n"
566 "</myext:extension-elem>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200567 ly_in_new_memory(data, &st->in);
568 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100569
570 ret = yin_parse_extension_instance(st->yin_ctx, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákaa98bba2019-09-12 11:52:14 +0200571 assert_int_equal(ret, LY_SUCCESS);
572 assert_string_equal(exts->child->arg, "act-name");
573 assert_string_equal(exts->child->next->arg, "target");
574 assert_string_equal(exts->child->next->next->arg, "value");
575 assert_string_equal(exts->child->next->next->next->arg, "mod");
576 assert_null(exts->child->next->next->next->next->arg);
577 assert_string_equal(exts->child->next->next->next->next->next->arg, "cond");
578 assert_string_equal(exts->child->next->next->next->next->next->next->arg, "uri");
579 assert_string_equal(exts->child->next->next->next->next->next->next->next->arg, "data");
580 assert_string_equal(exts->child->next->next->next->next->next->next->next->next->arg, "tag");
581 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 +0200582 lysp_ext_instance_free(st->ctx, exts);
583 LY_ARRAY_FREE(exts);
584 exts = NULL;
David Sedlákaa98bba2019-09-12 11:52:14 +0200585 st = reset_state(state);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200586
David Sedlákb1a78352019-06-28 16:16:29 +0200587 st->finished_correctly = true;
588}
589
David Sedlák555c7202019-07-04 12:14:12 +0200590static void
591test_yin_parse_content(void **state)
592{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200593 struct test_parser_yin_state *st = *state;
David Sedlák555c7202019-07-04 12:14:12 +0200594 LY_ERR ret = LY_SUCCESS;
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100595 const char *data =
596 "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
597 " <myext:custom xmlns:myext=\"urn:example:extensions\">totally amazing extension</myext:custom>\n"
598 " <extension name=\"ext\">\n"
599 " <argument name=\"argname\"></argument>\n"
600 " <description><text>desc</text></description>\n"
601 " <reference><text>ref</text></reference>\n"
602 " <status value=\"deprecated\"></status>\n"
603 " </extension>\n"
604 " <text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>\n"
605 " <if-feature name=\"foo\"></if-feature>\n"
606 " <when condition=\"condition...\">\n"
607 " <reference><text>when_ref</text></reference>\n"
608 " <description><text>when_desc</text></description>\n"
609 " </when>\n"
610 " <config value=\"true\"/>\n"
611 " <error-message>\n"
612 " <value>error-msg</value>\n"
613 " </error-message>\n"
614 " <error-app-tag value=\"err-app-tag\"/>\n"
615 " <units name=\"radians\"></units>\n"
616 " <default value=\"default-value\"/>\n"
617 " <position value=\"25\"></position>\n"
618 " <value value=\"-5\"/>\n"
619 " <require-instance value=\"true\"></require-instance>\n"
620 " <range value=\"5..10\" />\n"
621 " <length value=\"baf\"/>\n"
622 " <pattern value='pattern'>\n"
623 " <modifier value='invert-match'/>\n"
624 " </pattern>\n"
625 " <enum name=\"yay\">\n"
626 " </enum>\n"
627 "</prefix>";
David Sedlák555c7202019-07-04 12:14:12 +0200628 struct lysp_ext_instance *exts = NULL;
David Sedlák5f8191e2019-07-08 16:35:52 +0200629 const char **if_features = NULL;
Michal Vasko7f45cf22020-10-01 12:49:44 +0200630 const char *value, *err_msg, *app_tag, *units;
631 struct lysp_qname def = {0};
David Sedlák986cb412019-07-04 13:10:11 +0200632 struct lysp_ext *ext_def = NULL;
David Sedlák32eee7b2019-07-09 12:38:44 +0200633 struct lysp_when *when_p = NULL;
David Sedlákcf5569a2019-07-11 13:31:34 +0200634 struct lysp_type_enum pos_enum = {}, val_enum = {};
David Sedlákfd5b9c32019-07-12 15:33:13 +0200635 struct lysp_type req_type = {}, range_type = {}, len_type = {}, patter_type = {}, enum_type = {};
Juraj Vijtiukf6a07372020-10-29 21:13:33 +0100636 uint16_t config = 0;
David Sedlák555c7202019-07-04 12:14:12 +0200637
Michal Vasko63f3d842020-07-08 10:10:14 +0200638 ly_in_new_memory(data, &st->in);
639 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100640 lyxml_ctx_next(st->yin_ctx->xmlctx);
641 lyxml_ctx_next(st->yin_ctx->xmlctx);
642 lyxml_ctx_next(st->yin_ctx->xmlctx);
David Sedlák555c7202019-07-04 12:14:12 +0200643
David Sedlákfd5b9c32019-07-12 15:33:13 +0200644 struct yin_subelement subelems[17] = {
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100645 {LY_STMT_CONFIG, &config, 0},
646 {LY_STMT_DEFAULT, &def, YIN_SUBELEM_UNIQUE},
647 {LY_STMT_ENUM, &enum_type, 0},
648 {LY_STMT_ERROR_APP_TAG, &app_tag, YIN_SUBELEM_UNIQUE},
649 {LY_STMT_ERROR_MESSAGE, &err_msg, 0},
650 {LY_STMT_EXTENSION, &ext_def, 0},
651 {LY_STMT_IF_FEATURE, &if_features, 0},
652 {LY_STMT_LENGTH, &len_type, 0},
653 {LY_STMT_PATTERN, &patter_type, 0},
654 {LY_STMT_POSITION, &pos_enum, 0},
655 {LY_STMT_RANGE, &range_type, 0},
656 {LY_STMT_REQUIRE_INSTANCE, &req_type, 0},
657 {LY_STMT_UNITS, &units, YIN_SUBELEM_UNIQUE},
658 {LY_STMT_VALUE, &val_enum, 0},
659 {LY_STMT_WHEN, &when_p, 0},
660 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
661 {LY_STMT_ARG_TEXT, &value, 0}
662 };
663
Michal Vaskob36053d2020-03-26 15:49:30 +0100664 ret = yin_parse_content(st->yin_ctx, subelems, 17, LY_STMT_PREFIX, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200665 assert_int_equal(ret, LY_SUCCESS);
David Sedlák2ce1be62019-07-10 16:15:09 +0200666 /* check parsed values */
Michal Vasko7f45cf22020-10-01 12:49:44 +0200667 assert_string_equal(def.str, "default-value");
David Sedláke0ef1c62019-09-13 10:05:55 +0200668 assert_string_equal(exts->name, "urn:example:extensions:custom");
David Sedlák555c7202019-07-04 12:14:12 +0200669 assert_string_equal(exts->argument, "totally amazing extension");
670 assert_string_equal(value, "wsefsdf");
David Sedláka5b1d382019-07-10 16:31:09 +0200671 assert_string_equal(units, "radians");
David Sedlák32eee7b2019-07-09 12:38:44 +0200672 assert_string_equal(when_p->cond, "condition...");
673 assert_string_equal(when_p->dsc, "when_desc");
674 assert_string_equal(when_p->ref, "when_ref");
David Sedláke1a30302019-07-10 13:49:38 +0200675 assert_int_equal(config, LYS_CONFIG_W);
David Sedlák5545f5d2019-07-11 11:55:16 +0200676 assert_int_equal(pos_enum.value, 25);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200677 assert_true(pos_enum.flags & LYS_SET_VALUE);
David Sedlák5545f5d2019-07-11 11:55:16 +0200678 assert_int_equal(val_enum.value, -5);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200679 assert_true(val_enum.flags & LYS_SET_VALUE);
David Sedlákcf5569a2019-07-11 13:31:34 +0200680 assert_int_equal(req_type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200681 assert_true(req_type.flags &= LYS_SET_REQINST);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200682 assert_string_equal(range_type.range->arg.str, "5..10");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200683 assert_true(range_type.flags & LYS_SET_RANGE);
David Sedlákc1771b12019-07-10 15:55:46 +0200684 assert_string_equal(err_msg, "error-msg");
David Sedlák2ce1be62019-07-10 16:15:09 +0200685 assert_string_equal(app_tag, "err-app-tag");
David Sedlákfd5b9c32019-07-12 15:33:13 +0200686 assert_string_equal(enum_type.enums->name, "yay");
Michal Vasko7f45cf22020-10-01 12:49:44 +0200687 assert_string_equal(len_type.length->arg.str, "baf");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200688 assert_true(len_type.flags & LYS_SET_LENGTH);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200689 assert_string_equal(patter_type.patterns->arg.str, "\x015pattern");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200690 assert_true(patter_type.flags & LYS_SET_PATTERN);
David Sedlák2ce1be62019-07-10 16:15:09 +0200691 /* cleanup */
David Sedlák555c7202019-07-04 12:14:12 +0200692 lysp_ext_instance_free(st->ctx, exts);
David Sedlák32eee7b2019-07-09 12:38:44 +0200693 lysp_when_free(st->ctx, when_p);
David Sedlák986cb412019-07-04 13:10:11 +0200694 lysp_ext_free(st->ctx, ext_def);
David Sedlák5f8191e2019-07-08 16:35:52 +0200695 FREE_STRING(st->ctx, *if_features);
David Sedlákc1771b12019-07-10 15:55:46 +0200696 FREE_STRING(st->ctx, err_msg);
David Sedlák2ce1be62019-07-10 16:15:09 +0200697 FREE_STRING(st->ctx, app_tag);
David Sedláka5b1d382019-07-10 16:31:09 +0200698 FREE_STRING(st->ctx, units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200699 FREE_STRING(st->ctx, patter_type.patterns->arg.str);
700 FREE_STRING(st->ctx, def.str);
701 FREE_STRING(st->ctx, range_type.range->arg.str);
702 FREE_STRING(st->ctx, len_type.length->arg.str);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200703 FREE_STRING(st->ctx, enum_type.enums->name);
David Sedlákb7296dd2019-07-11 14:58:38 +0200704 FREE_STRING(st->ctx, value);
David Sedlák5f8191e2019-07-08 16:35:52 +0200705 LY_ARRAY_FREE(if_features);
David Sedlák555c7202019-07-04 12:14:12 +0200706 LY_ARRAY_FREE(exts);
David Sedlák986cb412019-07-04 13:10:11 +0200707 LY_ARRAY_FREE(ext_def);
David Sedlákd3983112019-07-12 11:20:56 +0200708 LY_ARRAY_FREE(patter_type.patterns);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200709 LY_ARRAY_FREE(enum_type.enums);
David Sedlák32eee7b2019-07-09 12:38:44 +0200710 free(when_p);
David Sedlákb7296dd2019-07-11 14:58:38 +0200711 free(range_type.range);
David Sedlák438ae432019-07-11 15:36:54 +0200712 free(len_type.length);
David Sedlák555c7202019-07-04 12:14:12 +0200713 st = reset_state(state);
714
715 /* test unique subelem */
716 const char *prefix_value;
Radek Krejcid6b76452019-09-03 17:03:03 +0200717 struct yin_subelement subelems2[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE},
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100718 {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_UNIQUE}};
719
David Sedláke6cd89e2019-08-07 12:46:02 +0200720 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100721 "<prefix value=\"inv_mod\" />"
722 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
723 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
724 ELEMENT_WRAPPER_END;
Michal Vasko63f3d842020-07-08 10:10:14 +0200725 ly_in_new_memory(data, &st->in);
726 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100727 lyxml_ctx_next(st->yin_ctx->xmlctx);
728
729 ret = yin_parse_content(st->yin_ctx, subelems2, 2, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200730 assert_int_equal(ret, LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200731 logbuf_assert("Redefinition of \"text\" sub-element in \"status\" element. Line number 1.");
David Sedlák555c7202019-07-04 12:14:12 +0200732 lydict_remove(st->ctx, prefix_value);
733 lydict_remove(st->ctx, value);
734 st = reset_state(state);
David Sedlák555c7202019-07-04 12:14:12 +0200735
736 /* test first subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200737 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100738 "<prefix value=\"inv_mod\" />"
739 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
740 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
741 ELEMENT_WRAPPER_END;
Radek Krejcid6b76452019-09-03 17:03:03 +0200742 struct yin_subelement subelems3[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE},
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100743 {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_FIRST}};
744
Michal Vasko63f3d842020-07-08 10:10:14 +0200745 ly_in_new_memory(data, &st->in);
746 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100747 lyxml_ctx_next(st->yin_ctx->xmlctx);
748
749 ret = yin_parse_content(st->yin_ctx, subelems3, 2, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200750 assert_int_equal(ret, LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200751 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 +0200752 lydict_remove(st->ctx, prefix_value);
753 st = reset_state(state);
David Sedlák555c7202019-07-04 12:14:12 +0200754
755 /* test mandatory subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200756 data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END;
Radek Krejcid6b76452019-09-03 17:03:03 +0200757 struct yin_subelement subelems4[1] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}};
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100758
Michal Vasko63f3d842020-07-08 10:10:14 +0200759 ly_in_new_memory(data, &st->in);
760 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100761 lyxml_ctx_next(st->yin_ctx->xmlctx);
762
763 ret = yin_parse_content(st->yin_ctx, subelems4, 1, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200764 assert_int_equal(ret, LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200765 logbuf_assert("Missing mandatory sub-element \"prefix\" of \"status\" element. Line number 1.");
David Sedlák555c7202019-07-04 12:14:12 +0200766
767 st->finished_correctly = true;
768}
769
David Sedlák92147b02019-07-09 14:01:01 +0200770static void
David Sedlák4a650532019-07-10 11:55:18 +0200771test_validate_value(void **state)
772{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200773 struct test_parser_yin_state *st = *state;
Michal Vaskob36053d2020-03-26 15:49:30 +0100774 const char *data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END;
775
776 /* create some XML context */
Michal Vasko63f3d842020-07-08 10:10:14 +0200777 ly_in_new_memory(data, &st->in);
778 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100779 st->yin_ctx->xmlctx->status = LYXML_ELEM_CONTENT;
780 st->yin_ctx->xmlctx->dynamic = 0;
781
782 st->yin_ctx->xmlctx->value = "#invalid";
783 st->yin_ctx->xmlctx->value_len = 8;
784 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG), LY_EVALID);
Radek Krejci1deb5be2020-08-26 16:43:36 +0200785 logbuf_assert("Invalid identifier character '#' (0x0023). Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100786
787 st->yin_ctx->xmlctx->value = "";
788 st->yin_ctx->xmlctx->value_len = 0;
789 assert_int_equal(yin_validate_value(st->yin_ctx, Y_STR_ARG), LY_SUCCESS);
790
791 st->yin_ctx->xmlctx->value = "pre:b";
792 st->yin_ctx->xmlctx->value_len = 5;
793 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG), LY_EVALID);
794 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG), LY_SUCCESS);
795
796 st->yin_ctx->xmlctx->value = "pre:pre:b";
797 st->yin_ctx->xmlctx->value_len = 9;
798 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG), LY_EVALID);
David Sedlák4a650532019-07-10 11:55:18 +0200799
800 st->finished_correctly = true;
801}
802
David Sedlák32488102019-07-15 17:44:10 +0200803/* helper function to simplify unit test of each element using parse_content function */
804LY_ERR
Radek Krejci3a4889a2020-05-19 17:01:58 +0200805test_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 +0200806{
David Sedlákc5b20842019-08-13 10:18:31 +0200807 const char *name, *prefix;
808 size_t name_len, prefix_len;
David Sedlák32488102019-07-15 17:44:10 +0200809 LY_ERR ret = LY_SUCCESS;
810 struct yin_subelement subelems[71] = {
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100811 {LY_STMT_ACTION, dest, 0},
812 {LY_STMT_ANYDATA, dest, 0},
813 {LY_STMT_ANYXML, dest, 0},
814 {LY_STMT_ARGUMENT, dest, 0},
815 {LY_STMT_AUGMENT, dest, 0},
816 {LY_STMT_BASE, dest, 0},
817 {LY_STMT_BELONGS_TO, dest, 0},
818 {LY_STMT_BIT, dest, 0},
819 {LY_STMT_CASE, dest, 0},
820 {LY_STMT_CHOICE, dest, 0},
821 {LY_STMT_CONFIG, dest, 0},
822 {LY_STMT_CONTACT, dest, 0},
823 {LY_STMT_CONTAINER, dest, 0},
824 {LY_STMT_DEFAULT, dest, YIN_SUBELEM_UNIQUE},
825 {LY_STMT_DESCRIPTION, dest, 0},
826 {LY_STMT_DEVIATE, dest, 0},
827 {LY_STMT_DEVIATION, dest, 0},
828 {LY_STMT_ENUM, dest, 0},
829 {LY_STMT_ERROR_APP_TAG, dest, YIN_SUBELEM_UNIQUE},
830 {LY_STMT_ERROR_MESSAGE, dest, 0},
831 {LY_STMT_EXTENSION, dest, 0},
832 {LY_STMT_FEATURE, dest, 0},
833 {LY_STMT_FRACTION_DIGITS, dest, 0},
834 {LY_STMT_GROUPING, dest, 0},
835 {LY_STMT_IDENTITY, dest, 0},
836 {LY_STMT_IF_FEATURE, dest, 0},
837 {LY_STMT_IMPORT, dest, 0},
838 {LY_STMT_INCLUDE, dest, 0},
839 {LY_STMT_INPUT, dest, 0},
840 {LY_STMT_KEY, dest, YIN_SUBELEM_UNIQUE},
841 {LY_STMT_LEAF, dest, 0},
842 {LY_STMT_LEAF_LIST, dest, 0},
843 {LY_STMT_LENGTH, dest, 0},
844 {LY_STMT_LIST, dest, 0},
845 {LY_STMT_MANDATORY, dest, 0},
846 {LY_STMT_MAX_ELEMENTS, dest, 0},
847 {LY_STMT_MIN_ELEMENTS, dest, 0},
848 {LY_STMT_MODIFIER, dest, 0},
849 {LY_STMT_MODULE, dest, 0},
850 {LY_STMT_MUST, dest, 0},
851 {LY_STMT_NAMESPACE, dest, YIN_SUBELEM_UNIQUE},
852 {LY_STMT_NOTIFICATION, dest, 0},
853 {LY_STMT_ORDERED_BY, dest, 0},
854 {LY_STMT_ORGANIZATION, dest, 0},
855 {LY_STMT_OUTPUT, dest, 0},
856 {LY_STMT_PATH, dest, 0},
857 {LY_STMT_PATTERN, dest, 0},
858 {LY_STMT_POSITION, dest, 0},
859 {LY_STMT_PREFIX, dest, YIN_SUBELEM_UNIQUE},
860 {LY_STMT_PRESENCE, dest, YIN_SUBELEM_UNIQUE},
861 {LY_STMT_RANGE, dest, 0},
862 {LY_STMT_REFERENCE, dest, 0},
863 {LY_STMT_REFINE, dest, 0},
864 {LY_STMT_REQUIRE_INSTANCE, dest, 0},
865 {LY_STMT_REVISION, dest, 0},
866 {LY_STMT_REVISION_DATE, dest, 0},
867 {LY_STMT_RPC, dest, 0},
868 {LY_STMT_STATUS, dest, 0},
869 {LY_STMT_SUBMODULE, dest, 0},
870 {LY_STMT_TYPE, dest, 0},
871 {LY_STMT_TYPEDEF, dest, 0},
872 {LY_STMT_UNIQUE, dest, 0},
873 {LY_STMT_UNITS, dest, YIN_SUBELEM_UNIQUE},
874 {LY_STMT_USES, dest, 0},
875 {LY_STMT_VALUE, dest, 0},
876 {LY_STMT_WHEN, dest, 0},
877 {LY_STMT_YANG_VERSION, dest, 0},
878 {LY_STMT_YIN_ELEMENT, dest, 0},
879 {LY_STMT_EXTENSION_INSTANCE, dest, 0},
880 {LY_STMT_ARG_TEXT, dest, 0},
881 {LY_STMT_ARG_VALUE, dest, 0}
882 };
883
Michal Vasko63f3d842020-07-08 10:10:14 +0200884 ly_in_new_memory(data, &st->in);
885 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100886 prefix = st->yin_ctx->xmlctx->prefix;
887 prefix_len = st->yin_ctx->xmlctx->prefix_len;
888 name = st->yin_ctx->xmlctx->name;
889 name_len = st->yin_ctx->xmlctx->name_len;
890 lyxml_ctx_next(st->yin_ctx->xmlctx);
891
892 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);
893
Michal Vasko63f3d842020-07-08 10:10:14 +0200894 /* free parser and input */
Michal Vaskob36053d2020-03-26 15:49:30 +0100895 lyxml_ctx_free(st->yin_ctx->xmlctx);
896 st->yin_ctx->xmlctx = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200897 ly_in_free(st->in, 0);
898 st->in = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200899 return ret;
900}
901
David Sedlákd1144562019-08-06 12:36:14 +0200902#define EXT_SUBELEM "<myext:c-define name=\"MY_MTU\" xmlns:myext=\"urn:example:extensions\"/>"
903
David Sedlák32488102019-07-15 17:44:10 +0200904static void
David Sedlák43801c92019-08-05 15:58:54 +0200905test_enum_elem(void **state)
David Sedlák32488102019-07-15 17:44:10 +0200906{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200907 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +0200908 struct lysp_type type = {};
909 const char *data;
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100910
David Sedlák32488102019-07-15 17:44:10 +0200911 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100912 "<enum name=\"enum-name\">\n"
913 " <if-feature name=\"feature\" />\n"
914 " <value value=\"55\" />\n"
915 " <status value=\"deprecated\" />\n"
916 " <description><text>desc...</text></description>\n"
917 " <reference><text>ref...</text></reference>\n"
918 " " EXT_SUBELEM "\n"
919 "</enum>"
920 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100921 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200922 assert_string_equal(type.enums->name, "enum-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +0200923 assert_string_equal(type.enums->iffeatures[0].str, "feature");
David Sedlák32488102019-07-15 17:44:10 +0200924 assert_int_equal(type.enums->value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200925 assert_true((type.enums->flags & LYS_STATUS_DEPRC) && (type.enums->flags & LYS_SET_VALUE));
David Sedlák32488102019-07-15 17:44:10 +0200926 assert_string_equal(type.enums->dsc, "desc...");
927 assert_string_equal(type.enums->ref, "ref...");
David Sedláke0ef1c62019-09-13 10:05:55 +0200928 assert_string_equal(type.enums->exts->name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +0200929 assert_int_equal(type.enums->exts->insubstmt_index, 0);
930 assert_int_equal(type.enums->exts->insubstmt, LYEXT_SUBSTMT_SELF);
931 lysp_type_free(st->ctx, &type);
932 memset(&type, 0, sizeof type);
933
934 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100935 "<enum name=\"enum-name\"></enum>"
936 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100937 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200938 assert_string_equal(type.enums->name, "enum-name");
David Sedlák32488102019-07-15 17:44:10 +0200939 lysp_type_free(st->ctx, &type);
940 memset(&type, 0, sizeof type);
941
David Sedlák43801c92019-08-05 15:58:54 +0200942 st->finished_correctly = true;
943}
944
945static void
946test_bit_elem(void **state)
947{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200948 struct test_parser_yin_state *st = *state;
David Sedlák43801c92019-08-05 15:58:54 +0200949 struct lysp_type type = {};
950 const char *data;
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100951
David Sedlák43801c92019-08-05 15:58:54 +0200952 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100953 "<bit name=\"bit-name\">\n"
954 " <if-feature name=\"feature\" />\n"
955 " <position value=\"55\" />\n"
956 " <status value=\"deprecated\" />\n"
957 " <description><text>desc...</text></description>\n"
958 " <reference><text>ref...</text></reference>\n"
959 EXT_SUBELEM
960 "</bit>"
961 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100962 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200963 assert_string_equal(type.bits->name, "bit-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +0200964 assert_string_equal(type.bits->iffeatures[0].str, "feature");
David Sedlák43801c92019-08-05 15:58:54 +0200965 assert_int_equal(type.bits->value, 55);
966 assert_true((type.bits->flags & LYS_STATUS_DEPRC) && (type.bits->flags & LYS_SET_VALUE));
967 assert_string_equal(type.bits->dsc, "desc...");
968 assert_string_equal(type.bits->ref, "ref...");
David Sedláke0ef1c62019-09-13 10:05:55 +0200969 assert_string_equal(type.bits->exts->name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +0200970 assert_int_equal(type.bits->exts->insubstmt_index, 0);
971 assert_int_equal(type.bits->exts->insubstmt, LYEXT_SUBSTMT_SELF);
972 lysp_type_free(st->ctx, &type);
973 memset(&type, 0, sizeof type);
974
975 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100976 "<bit name=\"bit-name\"> </bit>"
977 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100978 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200979 assert_string_equal(type.bits->name, "bit-name");
David Sedlák43801c92019-08-05 15:58:54 +0200980 lysp_type_free(st->ctx, &type);
981 memset(&type, 0, sizeof type);
982
David Sedlák32488102019-07-15 17:44:10 +0200983 st->finished_correctly = true;
984}
985
986static void
987test_meta_elem(void **state)
988{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200989 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +0200990 char *value = NULL;
991 const char *data;
David Sedlákd1144562019-08-06 12:36:14 +0200992 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200993
994 /* organization element */
995 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100996 "<organization><text>organization...</text>" EXT_SUBELEM EXT_SUBELEM "</organization>"
997 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100998 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_SUCCESS);
David Sedláke0ef1c62019-09-13 10:05:55 +0200999 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001000 assert_int_equal(exts[0].insubstmt_index, 0);
1001 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ORGANIZATION);
David Sedláke0ef1c62019-09-13 10:05:55 +02001002 assert_string_equal(exts[1].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001003 assert_int_equal(exts[1].insubstmt_index, 0);
1004 assert_int_equal(exts[1].insubstmt, LYEXT_SUBSTMT_ORGANIZATION);
David Sedlák32488102019-07-15 17:44:10 +02001005 assert_string_equal(value, "organization...");
1006 FREE_STRING(st->ctx, value);
David Sedlákd1144562019-08-06 12:36:14 +02001007 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001008 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001009 exts = NULL;
1010
David Sedlák32488102019-07-15 17:44:10 +02001011 /* contact element */
1012 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001013 "<contact><text>contact...</text>" EXT_SUBELEM "</contact>"
1014 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001015 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_SUCCESS);
David Sedláke0ef1c62019-09-13 10:05:55 +02001016 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001017 assert_int_equal(exts[0].insubstmt_index, 0);
1018 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_CONTACT);
David Sedlák32488102019-07-15 17:44:10 +02001019 assert_string_equal(value, "contact...");
1020 FREE_STRING(st->ctx, value);
David Sedlákd1144562019-08-06 12:36:14 +02001021 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1022 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001023 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001024
David Sedlák32488102019-07-15 17:44:10 +02001025 /* description element */
1026 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001027 "<description><text>description...</text>" EXT_SUBELEM "</description>"
1028 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001029 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001030 assert_string_equal(value, "description...");
David Sedláke0ef1c62019-09-13 10:05:55 +02001031 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001032 assert_int_equal(exts[0].insubstmt_index, 0);
1033 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_DESCRIPTION);
David Sedlák32488102019-07-15 17:44:10 +02001034 FREE_STRING(st->ctx, value);
1035 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001036 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1037 exts = NULL;
1038
David Sedlák32488102019-07-15 17:44:10 +02001039 /* reference element */
1040 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001041 "<reference><text>reference...</text>" EXT_SUBELEM "</reference>"
1042 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001043 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001044 assert_string_equal(value, "reference...");
David Sedláke0ef1c62019-09-13 10:05:55 +02001045 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001046 assert_int_equal(exts[0].insubstmt_index, 0);
1047 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_REFERENCE);
David Sedlák32488102019-07-15 17:44:10 +02001048 FREE_STRING(st->ctx, value);
1049 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001050 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1051 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001052
David Sedlákdf2a9732019-08-07 13:23:16 +02001053 /* reference element */
1054 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001055 "<reference invalid=\"text\"><text>reference...</text>" "</reference>"
1056 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001057 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001058 logbuf_assert("Unexpected attribute \"invalid\" of \"reference\" element. Line number 1.");
David Sedlákdf2a9732019-08-07 13:23:16 +02001059 FREE_STRING(st->ctx, value);
1060 value = NULL;
1061 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1062 exts = NULL;
1063
David Sedlák32488102019-07-15 17:44:10 +02001064 /* missing text subelement */
1065 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001066 "<reference>reference...</reference>"
1067 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001068 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001069 logbuf_assert("Missing mandatory sub-element \"text\" of \"reference\" element. Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001070
David Sedlákd1144562019-08-06 12:36:14 +02001071 /* reference element */
1072 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001073 "<reference>" EXT_SUBELEM "<text>reference...</text></reference>"
1074 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001075 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001076 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 +02001077 FREE_STRING(st->ctx, value);
1078 value = NULL;
1079 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1080 exts = NULL;
1081
David Sedlák32488102019-07-15 17:44:10 +02001082 st->finished_correctly = true;
1083}
1084
1085static void
1086test_import_elem(void **state)
1087{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001088 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001089 const char *data;
David Sedlák298ff6d2019-07-26 14:29:03 +02001090 struct lysp_import *imports = NULL;
1091 struct import_meta imp_meta = {"prefix", &imports};
David Sedlák32488102019-07-15 17:44:10 +02001092
1093 /* max subelems */
1094 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001095 "<import module=\"a\">\n"
1096 EXT_SUBELEM
1097 " <prefix value=\"a_mod\"/>\n"
1098 " <revision-date date=\"2015-01-01\"></revision-date>\n"
1099 " <description><text>import description</text></description>\n"
1100 " <reference><text>import reference</text></reference>\n"
1101 "</import>"
1102 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001103 assert_int_equal(test_element_helper(st, data, &imp_meta, NULL, NULL), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02001104 assert_string_equal(imports->name, "a");
1105 assert_string_equal(imports->prefix, "a_mod");
1106 assert_string_equal(imports->rev, "2015-01-01");
1107 assert_string_equal(imports->dsc, "import description");
1108 assert_string_equal(imports->ref, "import reference");
David Sedláke0ef1c62019-09-13 10:05:55 +02001109 assert_string_equal(imports->exts->name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001110 assert_int_equal(imports->exts->insubstmt, LYEXT_SUBSTMT_SELF);
1111 assert_int_equal(imports->exts->insubstmt_index, 0);
David Sedlák298ff6d2019-07-26 14:29:03 +02001112 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1113 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001114
1115 /* min subelems */
David Sedlák32488102019-07-15 17:44:10 +02001116 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001117 "<import module=\"a\">\n"
1118 " <prefix value=\"a_mod\"/>\n"
1119 "</import>"
1120 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001121 assert_int_equal(test_element_helper(st, data, &imp_meta, NULL, NULL), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02001122 assert_string_equal(imports->prefix, "a_mod");
1123 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1124 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001125
1126 /* invalid (missing prefix) */
David Sedlák298ff6d2019-07-26 14:29:03 +02001127 data = ELEMENT_WRAPPER_START "<import module=\"a\"></import>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001128 assert_int_equal(test_element_helper(st, data, &imp_meta, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001129 logbuf_assert("Missing mandatory sub-element \"prefix\" of \"import\" element. Line number 1.");
David Sedlák298ff6d2019-07-26 14:29:03 +02001130 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1131 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001132
1133 /* invalid reused prefix */
David Sedlák32488102019-07-15 17:44:10 +02001134 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001135 "<import module=\"a\">\n"
1136 " <prefix value=\"prefix\"/>\n"
1137 "</import>"
1138 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001139 assert_int_equal(test_element_helper(st, data, &imp_meta, NULL, NULL), LY_EVALID);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001140 logbuf_assert("Prefix \"prefix\" already used as module prefix. Line number 3.");
David Sedlák298ff6d2019-07-26 14:29:03 +02001141 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1142 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001143
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001144 data = ELEMENT_WRAPPER_START
1145 "<import module=\"a\">\n"
1146 " <prefix value=\"a\"/>\n"
1147 "</import>\n"
1148 "<import module=\"a\">\n"
1149 " <prefix value=\"a\"/>\n"
1150 "</import>"
1151 ELEMENT_WRAPPER_END;
1152 assert_int_equal(test_element_helper(st, data, &imp_meta, NULL, NULL), LY_EVALID);
1153 logbuf_assert("Prefix \"a\" already used to import \"a\" module. Line number 6.");
1154 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1155 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001156 st->finished_correctly = true;
1157}
1158
1159static void
1160test_status_elem(void **state)
1161{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001162 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001163 const char *data;
1164 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001165 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001166
1167 /* test valid values */
1168 data = ELEMENT_WRAPPER_START "<status value=\"current\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001169 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001170 assert_true(flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +02001171
1172 data = ELEMENT_WRAPPER_START "<status value=\"deprecated\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001173 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001174 assert_true(flags & LYS_STATUS_DEPRC);
David Sedlák32488102019-07-15 17:44:10 +02001175
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001176 data = ELEMENT_WRAPPER_START "<status value=\"obsolete\">"EXT_SUBELEM "</status>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001177 assert_int_equal(test_element_helper(st, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001178 assert_true(flags & LYS_STATUS_OBSLT);
David Sedláke0ef1c62019-09-13 10:05:55 +02001179 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001180 assert_int_equal(exts[0].insubstmt_index, 0);
1181 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_STATUS);
1182 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1183 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001184
1185 /* test invalid value */
1186 data = ELEMENT_WRAPPER_START "<status value=\"invalid\"></status>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001187 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001188 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 +02001189 st->finished_correctly = true;
1190}
1191
1192static void
1193test_ext_elem(void **state)
1194{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001195 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001196 const char *data;
1197 struct lysp_ext *ext = NULL;
1198
1199 /* max subelems */
1200 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001201 "<extension name=\"ext_name\">\n"
1202 " <argument name=\"arg\"></argument>\n"
1203 " <status value=\"current\"/>\n"
1204 " <description><text>ext_desc</text></description>\n"
1205 " <reference><text>ext_ref</text></reference>\n"
1206 EXT_SUBELEM
1207 "</extension>"
1208 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001209 assert_int_equal(test_element_helper(st, data, &ext, NULL, NULL), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001210 assert_string_equal(ext->name, "ext_name");
1211 assert_string_equal(ext->argument, "arg");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001212 assert_true(ext->flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +02001213 assert_string_equal(ext->dsc, "ext_desc");
1214 assert_string_equal(ext->ref, "ext_ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02001215 assert_string_equal(ext->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001216 assert_int_equal(ext->exts[0].insubstmt_index, 0);
1217 assert_int_equal(ext->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák32488102019-07-15 17:44:10 +02001218 lysp_ext_free(st->ctx, ext);
1219 LY_ARRAY_FREE(ext);
1220 ext = NULL;
1221
1222 /* min subelems */
1223 data = ELEMENT_WRAPPER_START "<extension name=\"ext_name\"></extension>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001224 assert_int_equal(test_element_helper(st, data, &ext, NULL, NULL), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001225 assert_string_equal(ext->name, "ext_name");
1226 lysp_ext_free(st->ctx, ext);
1227 LY_ARRAY_FREE(ext);
1228 ext = NULL;
1229
1230 st->finished_correctly = true;
1231}
1232
1233static void
1234test_yin_element_elem(void **state)
1235{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001236 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001237 const char *data;
1238 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001239 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001240
1241 data = ELEMENT_WRAPPER_START "<yin-element value=\"true\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001242 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001243 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +02001244
David Sedlákd1144562019-08-06 12:36:14 +02001245 data = ELEMENT_WRAPPER_START "<yin-element value=\"false\">" EXT_SUBELEM "</yin-element>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001246 assert_int_equal(test_element_helper(st, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001247 assert_true(flags & LYS_YINELEM_TRUE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001248 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001249 assert_int_equal(exts[0].insubstmt_index, 0);
1250 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_YINELEM);
1251 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001252
1253 data = ELEMENT_WRAPPER_START "<yin-element value=\"invalid\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001254 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_EVALID);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001255 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák26ea1432019-08-14 13:42:23 +02001256 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 +02001257 st->finished_correctly = true;
1258}
1259
1260static void
1261test_yangversion_elem(void **state)
1262{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001263 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001264 const char *data;
1265 uint8_t version = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001266 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001267
1268 /* valid values */
Radek Krejci96e48da2020-09-04 13:18:06 +02001269 data = ELEMENT_WRAPPER_START "<yang-version value=\"1\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001270 assert_int_equal(test_element_helper(st, data, &version, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001271 assert_true(version & LYS_VERSION_1_0);
David Sedlák32488102019-07-15 17:44:10 +02001272
David Sedlákd1144562019-08-06 12:36:14 +02001273 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.1\">" EXT_SUBELEM "</yang-version>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001274 assert_int_equal(test_element_helper(st, data, &version, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001275 assert_true(version & LYS_VERSION_1_1);
David Sedláke0ef1c62019-09-13 10:05:55 +02001276 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001277 assert_int_equal(exts[0].insubstmt_index, 0);
1278 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_VERSION);
1279 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001280
1281 /* invalid value */
1282 data = ELEMENT_WRAPPER_START "<yang-version value=\"version\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001283 assert_int_equal(test_element_helper(st, data, &version, NULL, NULL), LY_EVALID);
Radek Krejci96e48da2020-09-04 13:18:06 +02001284 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 +02001285
1286 st->finished_correctly = true;
1287}
1288
1289static void
1290test_mandatory_elem(void **state)
1291{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001292 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001293 const char *data;
1294 uint16_t man = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001295 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001296
1297 /* valid values */
1298 data = ELEMENT_WRAPPER_START "<mandatory value=\"true\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001299 assert_int_equal(test_element_helper(st, data, &man, NULL, NULL), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001300 assert_int_equal(man, LYS_MAND_TRUE);
1301 man = 0;
1302
David Sedlákd1144562019-08-06 12:36:14 +02001303 data = ELEMENT_WRAPPER_START "<mandatory value=\"false\">" EXT_SUBELEM "</mandatory>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001304 assert_int_equal(test_element_helper(st, data, &man, NULL, &exts), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001305 assert_int_equal(man, LYS_MAND_FALSE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001306 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001307 assert_int_equal(exts[0].insubstmt_index, 0);
1308 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_MANDATORY);
1309 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001310
1311 data = ELEMENT_WRAPPER_START "<mandatory value=\"invalid\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001312 assert_int_equal(test_element_helper(st, data, &man, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001313 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 +02001314
1315 st->finished_correctly = true;
1316}
1317
David Sedlák8e7bda82019-07-16 17:57:50 +02001318static void
1319test_argument_elem(void **state)
1320{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001321 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001322 const char *data;
1323 uint16_t flags = 0;
1324 const char *arg;
1325 struct yin_argument_meta arg_meta = {&flags, &arg};
David Sedlákd1144562019-08-06 12:36:14 +02001326 struct lysp_ext_instance *exts = NULL;
1327
David Sedlák8e7bda82019-07-16 17:57:50 +02001328 /* max subelems */
1329 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001330 "<argument name=\"arg-name\">\n"
1331 " <yin-element value=\"true\" />\n"
1332 EXT_SUBELEM
1333 "</argument>"
1334 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001335 assert_int_equal(test_element_helper(st, data, &arg_meta, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001336 assert_string_equal(arg, "arg-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001337 assert_true(flags & LYS_YINELEM_TRUE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001338 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001339 assert_int_equal(exts[0].insubstmt_index, 0);
1340 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ARGUMENT);
1341 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1342 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001343 flags = 0;
1344 FREE_STRING(st->ctx, arg);
1345 arg = NULL;
1346
1347 /* min subelems */
1348 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001349 "<argument name=\"arg\">"
1350 "</argument>"
1351 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001352 assert_int_equal(test_element_helper(st, data, &arg_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001353 assert_string_equal(arg, "arg");
1354 assert_true(flags == 0);
1355 FREE_STRING(st->ctx, arg);
1356
1357 st->finished_correctly = true;
1358}
1359
1360static void
1361test_base_elem(void **state)
1362{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001363 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001364 const char *data;
1365 const char **bases = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001366 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001367 struct lysp_type type = {};
1368
1369 /* as identity subelement */
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001370 data = "<identity xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
1371 " <base name=\"base-name\">\n"
1372 EXT_SUBELEM
1373 " </base>\n"
1374 "</identity>";
Michal Vaskob36053d2020-03-26 15:49:30 +01001375 assert_int_equal(test_element_helper(st, data, &bases, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001376 assert_string_equal(*bases, "base-name");
David Sedláke0ef1c62019-09-13 10:05:55 +02001377 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001378 assert_int_equal(exts[0].insubstmt_index, 0);
1379 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_BASE);
1380 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1381 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001382 FREE_STRING(st->ctx, *bases);
1383 LY_ARRAY_FREE(bases);
1384
1385 /* as type subelement */
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001386 data = "<type xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
1387 " <base name=\"base-name\">\n"
1388 EXT_SUBELEM
1389 " </base>\n"
1390 "</type>";
Michal Vaskob36053d2020-03-26 15:49:30 +01001391 assert_int_equal(test_element_helper(st, data, &type, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001392 assert_string_equal(*type.bases, "base-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001393 assert_true(type.flags & LYS_SET_BASE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001394 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001395 assert_int_equal(exts[0].insubstmt_index, 0);
1396 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_BASE);
1397 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1398 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001399 FREE_STRING(st->ctx, *type.bases);
1400 LY_ARRAY_FREE(type.bases);
1401
1402 st->finished_correctly = true;
1403}
1404
1405static void
1406test_belongsto_elem(void **state)
1407{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001408 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001409 const char *data;
1410 struct lysp_submodule submod;
David Sedlákd1144562019-08-06 12:36:14 +02001411 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001412
1413 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001414 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/>"EXT_SUBELEM "</belongs-to>"
1415 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001416 assert_int_equal(test_element_helper(st, data, &submod, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001417 assert_string_equal(submod.prefix, "pref");
David Sedláke0ef1c62019-09-13 10:05:55 +02001418 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001419 assert_int_equal(exts[0].insubstmt_index, 0);
1420 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_BELONGSTO);
1421 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1422 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001423 FREE_STRING(st->ctx, submod.prefix);
1424
1425 data = ELEMENT_WRAPPER_START "<belongs-to module=\"module-name\"></belongs-to>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001426 assert_int_equal(test_element_helper(st, data, &submod, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001427 logbuf_assert("Missing mandatory sub-element \"prefix\" of \"belongs-to\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001428
1429 st->finished_correctly = true;
1430}
1431
1432static void
1433test_config_elem(void **state)
1434{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001435 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001436 const char *data;
1437 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001438 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001439
David Sedlákd1144562019-08-06 12:36:14 +02001440 data = ELEMENT_WRAPPER_START "<config value=\"true\">" EXT_SUBELEM "</config>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001441 assert_int_equal(test_element_helper(st, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001442 assert_true(flags & LYS_CONFIG_W);
David Sedláke0ef1c62019-09-13 10:05:55 +02001443 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001444 assert_int_equal(exts[0].insubstmt_index, 0);
1445 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_CONFIG);
1446 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1447 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001448 flags = 0;
1449
1450 data = ELEMENT_WRAPPER_START "<config value=\"false\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001451 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001452 assert_true(flags & LYS_CONFIG_R);
David Sedlák8e7bda82019-07-16 17:57:50 +02001453 flags = 0;
1454
1455 data = ELEMENT_WRAPPER_START "<config value=\"invalid\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001456 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001457 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 +02001458
1459 st->finished_correctly = true;
1460}
1461
1462static void
1463test_default_elem(void **state)
1464{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001465 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001466 const char *data;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001467 struct lysp_qname val = {0};
David Sedlákd1144562019-08-06 12:36:14 +02001468 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001469
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001470 data = ELEMENT_WRAPPER_START "<default value=\"defaul-value\">"EXT_SUBELEM "</default>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001471 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001472 assert_string_equal(val.str, "defaul-value");
David Sedláke0ef1c62019-09-13 10:05:55 +02001473 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001474 assert_int_equal(exts[0].insubstmt_index, 0);
1475 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_DEFAULT);
1476 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1477 exts = NULL;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001478 FREE_STRING(st->ctx, val.str);
1479 val.str = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001480
1481 data = ELEMENT_WRAPPER_START "<default/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001482 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001483 logbuf_assert("Missing mandatory attribute value of default element. Line number 1.");
1484
1485 st->finished_correctly = true;
1486}
1487
1488static void
1489test_err_app_tag_elem(void **state)
1490{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001491 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001492 const char *data;
1493 const char *val = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001494 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001495
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001496 data = ELEMENT_WRAPPER_START "<error-app-tag value=\"val\">"EXT_SUBELEM "</error-app-tag>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001497 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001498 assert_string_equal(val, "val");
David Sedláke0ef1c62019-09-13 10:05:55 +02001499 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001500 assert_int_equal(exts[0].insubstmt_index, 0);
1501 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ERRTAG);
1502 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1503 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001504 FREE_STRING(st->ctx, val);
1505 val = NULL;
1506
1507 data = ELEMENT_WRAPPER_START "<error-app-tag/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001508 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001509 logbuf_assert("Missing mandatory attribute value of error-app-tag element. Line number 1.");
1510
1511 st->finished_correctly = true;
1512}
1513
1514static void
1515test_err_msg_elem(void **state)
1516{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001517 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001518 const char *data;
1519 const char *val = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001520 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001521
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001522 data = ELEMENT_WRAPPER_START "<error-message><value>val</value>"EXT_SUBELEM "</error-message>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001523 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001524 assert_string_equal(val, "val");
David Sedláke0ef1c62019-09-13 10:05:55 +02001525 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001526 assert_int_equal(exts[0].insubstmt_index, 0);
1527 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ERRMSG);
1528 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1529 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001530 FREE_STRING(st->ctx, val);
1531
1532 data = ELEMENT_WRAPPER_START "<error-message></error-message>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001533 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001534 logbuf_assert("Missing mandatory sub-element \"value\" of \"error-message\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001535
David Sedlákdf2a9732019-08-07 13:23:16 +02001536 data = ELEMENT_WRAPPER_START "<error-message invalid=\"text\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001537 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001538 logbuf_assert("Unexpected attribute \"invalid\" of \"error-message\" element. Line number 1.");
David Sedlákdf2a9732019-08-07 13:23:16 +02001539
David Sedlák8e7bda82019-07-16 17:57:50 +02001540 st->finished_correctly = true;
1541}
1542
1543static void
1544test_fracdigits_elem(void **state)
1545{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001546 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001547 const char *data;
1548 struct lysp_type type = {};
1549
1550 /* valid value */
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001551 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"10\">"EXT_SUBELEM "</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_SUCCESS);
David Sedláke0ef1c62019-09-13 10:05:55 +02001553 assert_string_equal(type.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001554 assert_int_equal(type.exts[0].insubstmt_index, 0);
1555 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_FRACDIGITS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001556 assert_int_equal(type.fraction_digits, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001557 assert_true(type.flags & LYS_SET_FRDIGITS);
David Sedlákd1144562019-08-06 12:36:14 +02001558 FREE_ARRAY(st->ctx, type.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001559
1560 /* invalid values */
1561 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"-1\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001562 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001563 logbuf_assert("Invalid value \"-1\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001564
1565 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"02\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001566 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001567 logbuf_assert("Invalid value \"02\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001568
1569 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"1p\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001570 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001571 logbuf_assert("Invalid value \"1p\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001572
1573 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"19\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001574 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001575 logbuf_assert("Invalid value \"19\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001576
1577 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"999999999999999999\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001578 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001579 logbuf_assert("Invalid value \"999999999999999999\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001580
1581 st->finished_correctly = true;
1582}
1583
1584static void
1585test_iffeature_elem(void **state)
1586{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001587 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001588 const char *data;
1589 const char **iffeatures = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001590 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001591
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001592 data = ELEMENT_WRAPPER_START "<if-feature name=\"local-storage\">"EXT_SUBELEM "</if-feature>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001593 assert_int_equal(test_element_helper(st, data, &iffeatures, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001594 assert_string_equal(*iffeatures, "local-storage");
David Sedláke0ef1c62019-09-13 10:05:55 +02001595 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001596 assert_int_equal(exts[0].insubstmt_index, 0);
1597 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_IFFEATURE);
1598 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1599 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001600 FREE_STRING(st->ctx, *iffeatures);
1601 LY_ARRAY_FREE(iffeatures);
1602 iffeatures = NULL;
1603
1604 data = ELEMENT_WRAPPER_START "<if-feature/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001605 assert_int_equal(test_element_helper(st, data, &iffeatures, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001606 logbuf_assert("Missing mandatory attribute name of if-feature element. Line number 1.");
1607 LY_ARRAY_FREE(iffeatures);
1608 iffeatures = NULL;
1609
1610 st->finished_correctly = true;
1611}
1612
1613static void
1614test_length_elem(void **state)
1615{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001616 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001617 const char *data;
1618 struct lysp_type type = {};
1619
1620 /* max subelems */
1621 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001622 "<length value=\"length-str\">\n"
1623 " <error-message><value>err-msg</value></error-message>\n"
1624 " <error-app-tag value=\"err-app-tag\"/>\n"
1625 " <description><text>desc</text></description>\n"
1626 " <reference><text>ref</text></reference>\n"
1627 EXT_SUBELEM
1628 "</length>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001629 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 assert_string_equal(type.length->emsg, "err-msg");
1633 assert_string_equal(type.length->eapptag, "err-app-tag");
1634 assert_string_equal(type.length->dsc, "desc");
1635 assert_string_equal(type.length->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001636 assert_true(type.flags & LYS_SET_LENGTH);
David Sedláke0ef1c62019-09-13 10:05:55 +02001637 assert_string_equal(type.length->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001638 assert_int_equal(type.length->exts[0].insubstmt_index, 0);
1639 assert_int_equal(type.length->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8e7bda82019-07-16 17:57:50 +02001640 lysp_type_free(st->ctx, &type);
1641 memset(&type, 0, sizeof(type));
1642
1643 /* min subelems */
1644 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001645 "<length value=\"length-str\">"
1646 "</length>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001647 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001648 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001649 assert_string_equal(type.length->arg.str, "length-str");
David Sedlák8e7bda82019-07-16 17:57:50 +02001650 lysp_type_free(st->ctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001651 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001652 memset(&type, 0, sizeof(type));
1653
1654 data = ELEMENT_WRAPPER_START "<length></length>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001655 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001656 logbuf_assert("Missing mandatory attribute value of length element. Line number 1.");
1657 lysp_type_free(st->ctx, &type);
1658 memset(&type, 0, sizeof(type));
1659
1660 st->finished_correctly = true;
1661}
1662
1663static void
1664test_modifier_elem(void **state)
1665{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001666 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001667 const char *data;
Radek Krejci011e4aa2020-09-04 15:22:31 +02001668 const char *pat;
David Sedlákd1144562019-08-06 12:36:14 +02001669 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001670
Radek Krejci011e4aa2020-09-04 15:22:31 +02001671 assert_int_equal(LY_SUCCESS, lydict_insert(st->ctx, "\006pattern", 8, &pat));
David Sedlákd1144562019-08-06 12:36:14 +02001672 data = ELEMENT_WRAPPER_START "<modifier value=\"invert-match\">" EXT_SUBELEM "</modifier>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001673 assert_int_equal(test_element_helper(st, data, &pat, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001674 assert_string_equal(pat, "\x015pattern");
David Sedláke0ef1c62019-09-13 10:05:55 +02001675 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001676 assert_int_equal(exts[0].insubstmt_index, 0);
1677 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_MODIFIER);
1678 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1679 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001680 FREE_STRING(st->ctx, pat);
1681
Radek Krejci011e4aa2020-09-04 15:22:31 +02001682 assert_int_equal(LY_SUCCESS, lydict_insert(st->ctx, "\006pattern", 8, &pat));
David Sedlák8e7bda82019-07-16 17:57:50 +02001683 data = ELEMENT_WRAPPER_START "<modifier value=\"invert\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001684 assert_int_equal(test_element_helper(st, data, &pat, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001685 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 +02001686 FREE_STRING(st->ctx, pat);
1687
1688 st->finished_correctly = true;
1689}
1690
1691static void
1692test_namespace_elem(void **state)
1693{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001694 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001695 const char *data;
1696 const char *ns;
David Sedlákd1144562019-08-06 12:36:14 +02001697 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001698
David Sedlákd1144562019-08-06 12:36:14 +02001699 data = ELEMENT_WRAPPER_START "<namespace uri=\"ns\">" EXT_SUBELEM "</namespace>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001700 assert_int_equal(test_element_helper(st, data, &ns, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001701 assert_string_equal(ns, "ns");
David Sedláke0ef1c62019-09-13 10:05:55 +02001702 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001703 assert_int_equal(exts[0].insubstmt_index, 0);
1704 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_NAMESPACE);
1705 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1706 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001707 FREE_STRING(st->ctx, ns);
1708
1709 data = ELEMENT_WRAPPER_START "<namespace/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001710 assert_int_equal(test_element_helper(st, data, &ns, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001711 logbuf_assert("Missing mandatory attribute uri of namespace element. Line number 1.");
1712
1713 st->finished_correctly = true;
1714}
1715
1716static void
David Sedlák8e7bda82019-07-16 17:57:50 +02001717test_pattern_elem(void **state)
1718{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001719 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001720 const char *data;
1721 struct lysp_type type = {};
1722
1723 /* max subelems */
1724 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001725 "<pattern value=\"super_pattern\">\n"
1726 " <modifier value=\"invert-match\"/>\n"
1727 " <error-message><value>err-msg-value</value></error-message>\n"
1728 " <error-app-tag value=\"err-app-tag-value\"/>\n"
1729 " <description><text>&quot;pattern-desc&quot;</text></description>\n"
1730 " <reference><text>pattern-ref</text></reference>\n"
1731 EXT_SUBELEM
1732 "</pattern>"
1733 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001734 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001735 assert_true(type.flags & LYS_SET_PATTERN);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001736 assert_string_equal(type.patterns->arg.str, "\x015super_pattern");
David Sedlák169cc522019-08-15 13:23:45 +02001737 assert_string_equal(type.patterns->dsc, "\"pattern-desc\"");
David Sedlák8e7bda82019-07-16 17:57:50 +02001738 assert_string_equal(type.patterns->eapptag, "err-app-tag-value");
1739 assert_string_equal(type.patterns->emsg, "err-msg-value");
David Sedlák8e7bda82019-07-16 17:57:50 +02001740 assert_string_equal(type.patterns->ref, "pattern-ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02001741 assert_string_equal(type.patterns->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001742 assert_int_equal(type.patterns->exts[0].insubstmt_index, 0);
1743 assert_int_equal(type.patterns->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8e7bda82019-07-16 17:57:50 +02001744 lysp_type_free(st->ctx, &type);
1745 memset(&type, 0, sizeof(type));
1746
1747 /* min subelems */
1748 data = ELEMENT_WRAPPER_START "<pattern value=\"pattern\"> </pattern>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001749 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001750 assert_string_equal(type.patterns->arg.str, "\x006pattern");
David Sedlák8e7bda82019-07-16 17:57:50 +02001751 lysp_type_free(st->ctx, &type);
1752 memset(&type, 0, sizeof(type));
1753
1754 st->finished_correctly = true;
1755}
1756
1757static void
1758test_value_position_elem(void **state)
1759{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001760 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001761 const char *data;
1762 struct lysp_type_enum en = {};
1763
1764 /* valid values */
David Sedlákd1144562019-08-06 12:36:14 +02001765 data = ELEMENT_WRAPPER_START "<value value=\"55\">" EXT_SUBELEM "</value>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001766 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001767 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001768 assert_true(en.flags & LYS_SET_VALUE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001769 assert_string_equal(en.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001770 assert_int_equal(en.exts[0].insubstmt_index, 0);
1771 assert_int_equal(en.exts[0].insubstmt, LYEXT_SUBSTMT_VALUE);
1772 FREE_ARRAY(st->ctx, en.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001773 memset(&en, 0, sizeof(en));
1774
1775 data = ELEMENT_WRAPPER_START "<value value=\"-55\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001776 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001777 assert_int_equal(en.value, -55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001778 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001779 memset(&en, 0, sizeof(en));
1780
1781 data = ELEMENT_WRAPPER_START "<value value=\"0\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001782 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001783 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001784 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001785 memset(&en, 0, sizeof(en));
1786
1787 data = ELEMENT_WRAPPER_START "<value value=\"-0\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001788 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001789 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001790 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001791 memset(&en, 0, sizeof(en));
1792
1793 /* valid positions */
David Sedlák8d552d62019-08-06 15:29:05 +02001794 data = ELEMENT_WRAPPER_START "<position value=\"55\">" EXT_SUBELEM "</position>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001795 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001796 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001797 assert_true(en.flags & LYS_SET_VALUE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001798 assert_string_equal(en.exts[0].name, "urn:example:extensions:c-define");
David Sedlák8d552d62019-08-06 15:29:05 +02001799 assert_int_equal(en.exts[0].insubstmt_index, 0);
1800 assert_int_equal(en.exts[0].insubstmt, LYEXT_SUBSTMT_POSITION);
1801 FREE_ARRAY(st->ctx, en.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001802 memset(&en, 0, sizeof(en));
1803
1804 data = ELEMENT_WRAPPER_START "<position value=\"0\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001805 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001806 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001807 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001808 memset(&en, 0, sizeof(en));
1809
1810 /* invalid values */
1811 data = ELEMENT_WRAPPER_START "<value value=\"99999999999999999999999\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001812 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001813 logbuf_assert("Invalid value \"99999999999999999999999\" of \"value\" attribute in \"value\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001814
1815 data = ELEMENT_WRAPPER_START "<value value=\"1k\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001816 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001817 logbuf_assert("Invalid value \"1k\" of \"value\" attribute in \"value\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001818
David Sedlák69f01612019-07-17 11:41:08 +02001819 data = ELEMENT_WRAPPER_START "<value value=\"\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001820 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001821 logbuf_assert("Invalid value \"\" of \"value\" attribute in \"value\" element. Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001822
David Sedlák8e7bda82019-07-16 17:57:50 +02001823 /*invalid positions */
1824 data = ELEMENT_WRAPPER_START "<position value=\"-5\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001825 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001826 logbuf_assert("Invalid value \"-5\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001827
1828 data = ELEMENT_WRAPPER_START "<position value=\"-0\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001829 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001830 logbuf_assert("Invalid value \"-0\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001831
1832 data = ELEMENT_WRAPPER_START "<position value=\"99999999999999999999\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001833 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001834 logbuf_assert("Invalid value \"99999999999999999999\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001835
David Sedlák69f01612019-07-17 11:41:08 +02001836 data = ELEMENT_WRAPPER_START "<position value=\"\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001837 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001838 logbuf_assert("Invalid value \"\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001839
1840 st->finished_correctly = true;
1841}
1842
1843static void
1844test_prefix_elem(void **state)
1845{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001846 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001847 const char *data;
1848 const char *value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001849 struct lysp_ext_instance *exts = NULL;
1850
1851 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\">" EXT_SUBELEM "</prefix>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001852 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001853 assert_string_equal(value, "pref");
David Sedláke0ef1c62019-09-13 10:05:55 +02001854 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001855 assert_int_equal(exts[0].insubstmt_index, 0);
1856 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_PREFIX);
1857 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1858 exts = NULL;
1859 FREE_STRING(st->ctx, value);
David Sedlák69f01612019-07-17 11:41:08 +02001860
1861 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001862 assert_int_equal(test_element_helper(st, data, &value, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001863 assert_string_equal(value, "pref");
1864 FREE_STRING(st->ctx, value);
1865
1866 st->finished_correctly = true;
1867}
1868
1869static void
1870test_range_elem(void **state)
1871{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001872 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001873 const char *data;
1874 struct lysp_type type = {};
1875
1876 /* max subelems */
1877 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001878 "<range value=\"range-str\">\n"
1879 " <error-message><value>err-msg</value></error-message>\n"
1880 " <error-app-tag value=\"err-app-tag\" />\n"
1881 " <description><text>desc</text></description>\n"
1882 " <reference><text>ref</text></reference>\n"
1883 EXT_SUBELEM
1884 "</range>"
1885 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001886 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001887 assert_string_equal(type.range->arg.str, "range-str");
David Sedlák69f01612019-07-17 11:41:08 +02001888 assert_string_equal(type.range->dsc, "desc");
1889 assert_string_equal(type.range->eapptag, "err-app-tag");
1890 assert_string_equal(type.range->emsg, "err-msg");
1891 assert_string_equal(type.range->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001892 assert_true(type.flags & LYS_SET_RANGE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001893 assert_string_equal(type.range->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001894 assert_int_equal(type.range->exts[0].insubstmt_index, 0);
1895 assert_int_equal(type.range->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák69f01612019-07-17 11:41:08 +02001896 lysp_type_free(st->ctx, &type);
1897 memset(&type, 0, sizeof(type));
1898
1899 /* min subelems */
1900 data = ELEMENT_WRAPPER_START "<range value=\"range-str\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001901 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001902 assert_string_equal(type.range->arg.str, "range-str");
David Sedlák69f01612019-07-17 11:41:08 +02001903 lysp_type_free(st->ctx, &type);
1904 memset(&type, 0, sizeof(type));
1905
1906 st->finished_correctly = true;
1907}
1908
1909static void
1910test_reqinstance_elem(void **state)
1911{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001912 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001913 const char *data;
1914 struct lysp_type type = {};
1915
David Sedlákd1144562019-08-06 12:36:14 +02001916 data = ELEMENT_WRAPPER_START "<require-instance value=\"true\">" EXT_SUBELEM "</require-instance>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001917 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001918 assert_int_equal(type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001919 assert_true(type.flags & LYS_SET_REQINST);
David Sedláke0ef1c62019-09-13 10:05:55 +02001920 assert_string_equal(type.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001921 assert_int_equal(type.exts[0].insubstmt_index, 0);
1922 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_REQINSTANCE);
1923 lysp_type_free(st->ctx, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001924 memset(&type, 0, sizeof(type));
1925
1926 data = ELEMENT_WRAPPER_START "<require-instance value=\"false\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001927 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001928 assert_int_equal(type.require_instance, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001929 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001930 memset(&type, 0, sizeof(type));
1931
1932 data = ELEMENT_WRAPPER_START "<require-instance value=\"invalid\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001933 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák69f01612019-07-17 11:41:08 +02001934 memset(&type, 0, sizeof(type));
David Sedlák26ea1432019-08-14 13:42:23 +02001935 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 +02001936
1937 st->finished_correctly = true;
1938}
1939
1940static void
1941test_revision_date_elem(void **state)
1942{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001943 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001944 const char *data;
1945 char rev[LY_REV_SIZE];
David Sedlákd1144562019-08-06 12:36:14 +02001946 struct lysp_ext_instance *exts = NULL;
1947
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001948 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\">"EXT_SUBELEM "</revision-date>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001949 assert_int_equal(test_element_helper(st, data, rev, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001950 assert_string_equal(rev, "2000-01-01");
David Sedláke0ef1c62019-09-13 10:05:55 +02001951 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001952 assert_int_equal(exts[0].insubstmt_index, 0);
1953 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_REVISIONDATE);
1954 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák69f01612019-07-17 11:41:08 +02001955
1956 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001957 assert_int_equal(test_element_helper(st, data, rev, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001958 assert_string_equal(rev, "2000-01-01");
1959
1960 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-50-05\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001961 assert_int_equal(test_element_helper(st, data, rev, NULL, NULL), LY_EVALID);
David Sedlák69f01612019-07-17 11:41:08 +02001962 logbuf_assert("Invalid value \"2000-50-05\" of \"revision-date\". Line number 1.");
1963
1964 st->finished_correctly = true;
1965}
1966
1967static void
1968test_unique_elem(void **state)
1969{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001970 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001971 const char *data;
1972 const char **values = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001973 struct lysp_ext_instance *exts = NULL;
1974
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001975 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\">"EXT_SUBELEM "</unique>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001976 assert_int_equal(test_element_helper(st, data, &values, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001977 assert_string_equal(*values, "tag");
David Sedláke0ef1c62019-09-13 10:05:55 +02001978 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001979 assert_int_equal(exts[0].insubstmt_index, 0);
1980 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_UNIQUE);
1981 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1982 FREE_STRING(st->ctx, *values);
1983 LY_ARRAY_FREE(values);
1984 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001985
1986 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001987 assert_int_equal(test_element_helper(st, data, &values, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001988 assert_string_equal(*values, "tag");
1989 FREE_STRING(st->ctx, *values);
1990 LY_ARRAY_FREE(values);
David Sedlákd1144562019-08-06 12:36:14 +02001991 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001992
1993 st->finished_correctly = true;
1994}
1995
1996static void
1997test_units_elem(void **state)
1998{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001999 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02002000 const char *data;
2001 const char *values = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02002002 struct lysp_ext_instance *exts = NULL;
2003
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002004 data = ELEMENT_WRAPPER_START "<units name=\"name\">"EXT_SUBELEM "</units>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002005 assert_int_equal(test_element_helper(st, data, &values, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02002006 assert_string_equal(values, "name");
David Sedláke0ef1c62019-09-13 10:05:55 +02002007 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002008 assert_int_equal(exts[0].insubstmt_index, 0);
2009 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_UNITS);
2010 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
2011 FREE_STRING(st->ctx, values);
2012 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02002013
2014 data = ELEMENT_WRAPPER_START "<units name=\"name\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002015 assert_int_equal(test_element_helper(st, data, &values, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002016 assert_string_equal(values, "name");
2017 FREE_STRING(st->ctx, values);
David Sedlákd1144562019-08-06 12:36:14 +02002018 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02002019
2020 st->finished_correctly = true;
2021}
2022
2023static void
2024test_when_elem(void **state)
2025{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002026 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02002027 const char *data;
2028 struct lysp_when *when = NULL;
2029
2030 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002031 "<when condition=\"cond\">\n"
2032 " <description><text>desc</text></description>\n"
2033 " <reference><text>ref</text></reference>\n"
2034 EXT_SUBELEM
2035 "</when>"
2036 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002037 assert_int_equal(test_element_helper(st, data, &when, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002038 assert_string_equal(when->cond, "cond");
2039 assert_string_equal(when->dsc, "desc");
2040 assert_string_equal(when->ref, "ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02002041 assert_string_equal(when->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002042 assert_int_equal(when->exts[0].insubstmt_index, 0);
2043 assert_int_equal(when->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák69f01612019-07-17 11:41:08 +02002044 lysp_when_free(st->ctx, when);
2045 free(when);
2046 when = NULL;
2047
2048 data = ELEMENT_WRAPPER_START "<when condition=\"cond\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002049 assert_int_equal(test_element_helper(st, data, &when, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002050 assert_string_equal(when->cond, "cond");
2051 lysp_when_free(st->ctx, when);
2052 free(when);
2053 when = NULL;
2054
2055 st->finished_correctly = true;
2056}
2057
2058static void
2059test_yin_text_value_elem(void **state)
2060{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002061 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02002062 const char *data;
2063 const char *val;
2064
2065 data = ELEMENT_WRAPPER_START "<text>text</text>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002066 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002067 assert_string_equal(val, "text");
2068 FREE_STRING(st->ctx, val);
2069
2070 data = "<error-message xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <value>text</value> </error-message>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002071 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002072 assert_string_equal(val, "text");
2073 FREE_STRING(st->ctx, val);
2074
2075 data = ELEMENT_WRAPPER_START "<text></text>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002076 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002077 assert_string_equal("", val);
2078 FREE_STRING(st->ctx, val);
2079
David Sedlák8e7bda82019-07-16 17:57:50 +02002080 st->finished_correctly = true;
2081}
David Sedlák32488102019-07-15 17:44:10 +02002082
David Sedlák374d2b32019-07-17 15:06:55 +02002083static void
2084test_type_elem(void **state)
2085{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002086 struct test_parser_yin_state *st = *state;
David Sedlák374d2b32019-07-17 15:06:55 +02002087 const char *data;
2088 struct lysp_type type = {};
2089
2090 /* max subelems */
2091 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002092 "<type name=\"type-name\">\n"
2093 " <base name=\"base-name\"/>\n"
2094 " <bit name=\"bit\"/>\n"
2095 " <enum name=\"enum\"/>\n"
2096 " <fraction-digits value=\"2\"/>\n"
2097 " <length value=\"length\"/>\n"
2098 " <path value=\"/path\"/>\n"
2099 " <pattern value=\"pattern\"/>\n"
2100 " <range value=\"range\" />\n"
2101 " <require-instance value=\"true\"/>\n"
2102 " <type name=\"sub-type-name\"/>\n"
2103 EXT_SUBELEM
2104 "</type>"
2105 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002106 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák374d2b32019-07-17 15:06:55 +02002107 assert_string_equal(type.name, "type-name");
2108 assert_string_equal(*type.bases, "base-name");
2109 assert_string_equal(type.bits->name, "bit");
2110 assert_string_equal(type.enums->name, "enum");
2111 assert_int_equal(type.fraction_digits, 2);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002112 assert_string_equal(type.length->arg.str, "length");
Michal Vaskocb8c6d42020-10-16 11:58:30 +02002113 assert_string_equal(type.path->expr, "/path");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002114 assert_string_equal(type.patterns->arg.str, "\006pattern");
2115 assert_string_equal(type.range->arg.str, "range");
David Sedlák374d2b32019-07-17 15:06:55 +02002116 assert_int_equal(type.require_instance, 1);
2117 assert_string_equal(type.types->name, "sub-type-name");
David Sedláke0ef1c62019-09-13 10:05:55 +02002118 assert_string_equal(type.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002119 assert_int_equal(type.exts[0].insubstmt_index, 0);
2120 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002121 assert_true(type.flags & LYS_SET_BASE);
2122 assert_true(type.flags & LYS_SET_BIT);
2123 assert_true(type.flags & LYS_SET_ENUM);
2124 assert_true(type.flags & LYS_SET_FRDIGITS);
2125 assert_true(type.flags & LYS_SET_LENGTH);
2126 assert_true(type.flags & LYS_SET_PATH);
2127 assert_true(type.flags & LYS_SET_PATTERN);
2128 assert_true(type.flags & LYS_SET_RANGE);
2129 assert_true(type.flags & LYS_SET_REQINST);
2130 assert_true(type.flags & LYS_SET_TYPE);
David Sedlákd1144562019-08-06 12:36:14 +02002131 lysp_type_free(st->ctx, &type);
David Sedlák374d2b32019-07-17 15:06:55 +02002132 memset(&type, 0, sizeof(type));
2133
2134 /* min subelems */
2135 data = ELEMENT_WRAPPER_START "<type name=\"type-name\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002136 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák374d2b32019-07-17 15:06:55 +02002137 lysp_type_free(st->ctx, &type);
2138 memset(&type, 0, sizeof(type));
2139
2140 st->finished_correctly = true;
2141}
2142
David Sedlák1af868e2019-07-17 17:03:14 +02002143static void
2144test_max_elems_elem(void **state)
2145{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002146 struct test_parser_yin_state *st = *state;
David Sedlák1af868e2019-07-17 17:03:14 +02002147 const char *data;
2148 struct lysp_node_list list = {};
2149 struct lysp_node_leaflist llist = {};
2150 struct lysp_refine refine = {};
2151
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002152 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 +01002153 assert_int_equal(test_element_helper(st, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02002154 assert_int_equal(refine.max, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002155 assert_true(refine.flags & LYS_SET_MAX);
David Sedláke0ef1c62019-09-13 10:05:55 +02002156 assert_string_equal(refine.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002157 assert_int_equal(refine.exts[0].insubstmt_index, 0);
2158 assert_int_equal(refine.exts[0].insubstmt, LYEXT_SUBSTMT_MAX);
2159 FREE_ARRAY(st->ctx, refine.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02002160
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002161 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 +01002162 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02002163 assert_int_equal(list.max, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002164 assert_true(list.flags & LYS_SET_MAX);
David Sedláke0ef1c62019-09-13 10:05:55 +02002165 assert_string_equal(list.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002166 assert_int_equal(list.exts[0].insubstmt_index, 0);
2167 assert_int_equal(list.exts[0].insubstmt, LYEXT_SUBSTMT_MAX);
2168 FREE_ARRAY(st->ctx, list.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02002169
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002170 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 +01002171 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02002172 assert_int_equal(llist.max, 85);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002173 assert_true(llist.flags & LYS_SET_MAX);
David Sedláke0ef1c62019-09-13 10:05:55 +02002174 assert_string_equal(llist.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002175 assert_int_equal(llist.exts[0].insubstmt_index, 0);
2176 assert_int_equal(llist.exts[0].insubstmt, LYEXT_SUBSTMT_MAX);
2177 FREE_ARRAY(st->ctx, llist.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02002178
2179 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"10\"/> </refine>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002180 assert_int_equal(test_element_helper(st, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02002181 assert_int_equal(refine.max, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002182 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02002183
2184 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"0\"/> </list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002185 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002186 logbuf_assert("Invalid value \"0\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002187
2188 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"-10\"/> </list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002189 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002190 logbuf_assert("Invalid value \"-10\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002191
2192 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"k\"/> </list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002193 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002194 logbuf_assert("Invalid value \"k\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002195
2196 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"u12\"/> </list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002197 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002198 logbuf_assert("Invalid value \"u12\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002199
2200 st->finished_correctly = true;
2201}
2202
David Sedlák09e18c92019-07-18 11:17:11 +02002203static void
2204test_min_elems_elem(void **state)
2205{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002206 struct test_parser_yin_state *st = *state;
David Sedlák09e18c92019-07-18 11:17:11 +02002207 const char *data;
2208 struct lysp_node_list list = {};
2209 struct lysp_node_leaflist llist = {};
2210 struct lysp_refine refine = {};
2211
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002212 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 +01002213 assert_int_equal(test_element_helper(st, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák09e18c92019-07-18 11:17:11 +02002214 assert_int_equal(refine.min, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002215 assert_true(refine.flags & LYS_SET_MIN);
David Sedláke0ef1c62019-09-13 10:05:55 +02002216 assert_string_equal(refine.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002217 assert_int_equal(refine.exts[0].insubstmt_index, 0);
2218 assert_int_equal(refine.exts[0].insubstmt, LYEXT_SUBSTMT_MIN);
2219 FREE_ARRAY(st->ctx, refine.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02002220
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002221 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 +01002222 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_SUCCESS);
David Sedlák09e18c92019-07-18 11:17:11 +02002223 assert_int_equal(list.min, 41);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002224 assert_true(list.flags & LYS_SET_MIN);
David Sedláke0ef1c62019-09-13 10:05:55 +02002225 assert_string_equal(list.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002226 assert_int_equal(list.exts[0].insubstmt_index, 0);
2227 assert_int_equal(list.exts[0].insubstmt, LYEXT_SUBSTMT_MIN);
2228 FREE_ARRAY(st->ctx, list.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02002229
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002230 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 +01002231 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_SUCCESS);
David Sedlák09e18c92019-07-18 11:17:11 +02002232 assert_int_equal(llist.min, 50);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002233 assert_true(llist.flags & LYS_SET_MIN);
David Sedláke0ef1c62019-09-13 10:05:55 +02002234 assert_string_equal(llist.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002235 assert_int_equal(llist.exts[0].insubstmt_index, 0);
2236 assert_int_equal(llist.exts[0].insubstmt, LYEXT_SUBSTMT_MIN);
2237 FREE_ARRAY(st->ctx, llist.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02002238
2239 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 +01002240 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002241 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 +02002242
2243 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 +01002244 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002245 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 +02002246
2247 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 +01002248 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002249 logbuf_assert("Invalid value \"5k\" of \"value\" attribute in \"min-elements\" element. Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02002250
2251 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 +01002252 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002253 logbuf_assert("Invalid value \"05\" of \"value\" attribute in \"min-elements\" element. Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02002254
2255 st->finished_correctly = true;
2256}
2257
David Sedláka2dad212019-07-18 12:45:19 +02002258static void
2259test_ordby_elem(void **state)
2260{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002261 struct test_parser_yin_state *st = *state;
David Sedláka2dad212019-07-18 12:45:19 +02002262 const char *data;
2263 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02002264 struct lysp_ext_instance *exts = NULL;
David Sedláka2dad212019-07-18 12:45:19 +02002265
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002266 data = ELEMENT_WRAPPER_START "<ordered-by value=\"system\">"EXT_SUBELEM "</ordered-by>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002267 assert_int_equal(test_element_helper(st, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002268 assert_true(flags & LYS_ORDBY_SYSTEM);
David Sedláke0ef1c62019-09-13 10:05:55 +02002269 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002270 assert_int_equal(exts[0].insubstmt_index, 0);
2271 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ORDEREDBY);
2272 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedláka2dad212019-07-18 12:45:19 +02002273
2274 data = ELEMENT_WRAPPER_START "<ordered-by value=\"user\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002275 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002276 assert_true(flags & LYS_ORDBY_USER);
David Sedláka2dad212019-07-18 12:45:19 +02002277
2278 data = ELEMENT_WRAPPER_START "<ordered-by value=\"inv\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002279 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02002280 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 +02002281
2282 st->finished_correctly = true;
2283}
2284
David Sedlák8a83bbb2019-07-18 14:46:00 +02002285static void
2286test_any_elem(void **state)
2287{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002288 struct test_parser_yin_state *st = *state;
David Sedlák8a83bbb2019-07-18 14:46:00 +02002289 const char *data;
2290 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002291 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák8a83bbb2019-07-18 14:46:00 +02002292 struct lysp_node_anydata *parsed = NULL;
2293
2294 /* anyxml max subelems */
2295 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002296 "<anyxml name=\"any-name\">\n"
2297 " <config value=\"true\" />\n"
2298 " <description><text>desc</text></description>\n"
2299 " <if-feature name=\"feature\" />\n"
2300 " <mandatory value=\"true\" />\n"
2301 " <must condition=\"must-cond\" />\n"
2302 " <reference><text>ref</text></reference>\n"
2303 " <status value=\"deprecated\"/>\n"
2304 " <when condition=\"when-cond\"/>\n"
2305 EXT_SUBELEM
2306 "</anyxml>"
2307 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002308 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002309 parsed = (struct lysp_node_anydata *)siblings;
2310 assert_null(parsed->parent);
2311 assert_int_equal(parsed->nodetype, LYS_ANYXML);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002312 assert_true(parsed->flags & LYS_CONFIG_W);
2313 assert_true(parsed->flags & LYS_MAND_TRUE);
2314 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002315 assert_null(parsed->next);
2316 assert_string_equal(parsed->name, "any-name");
2317 assert_string_equal(parsed->dsc, "desc");
2318 assert_string_equal(parsed->ref, "ref");
2319 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002320 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedláke0ef1c62019-09-13 10:05:55 +02002321 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002322 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2323 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002324 lysp_node_free(st->ctx, siblings);
2325 siblings = NULL;
2326
2327 /* anydata max subelems */
2328 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002329 "<anydata name=\"any-name\">\n"
2330 " <config value=\"true\" />\n"
2331 " <description><text>desc</text></description>\n"
2332 " <if-feature name=\"feature\" />\n"
2333 " <mandatory value=\"true\" />\n"
2334 " <must condition=\"must-cond\" />\n"
2335 " <reference><text>ref</text></reference>\n"
2336 " <status value=\"deprecated\"/>\n"
2337 " <when condition=\"when-cond\"/>\n"
2338 EXT_SUBELEM
2339 "</anydata>"
2340 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002341 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002342 parsed = (struct lysp_node_anydata *)siblings;
2343 assert_null(parsed->parent);
2344 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002345 assert_true(parsed->flags & LYS_CONFIG_W);
2346 assert_true(parsed->flags & LYS_MAND_TRUE);
2347 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002348 assert_null(parsed->next);
2349 assert_string_equal(parsed->name, "any-name");
2350 assert_string_equal(parsed->dsc, "desc");
2351 assert_string_equal(parsed->ref, "ref");
2352 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002353 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedláke0ef1c62019-09-13 10:05:55 +02002354 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002355 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2356 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002357 lysp_node_free(st->ctx, siblings);
2358 siblings = NULL;
2359
2360 /* min subelems */
2361 node_meta.parent = (void *)0x10;
2362 data = ELEMENT_WRAPPER_START "<anydata name=\"any-name\"> </anydata>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002363 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002364 parsed = (struct lysp_node_anydata *)siblings;
2365 assert_ptr_equal(parsed->parent, node_meta.parent);
2366 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
2367 assert_null(parsed->next);
2368 assert_null(parsed->exts);
2369 lysp_node_free(st->ctx, siblings);
2370
2371 st->finished_correctly = true;
2372}
2373
David Sedlák203ca3a2019-07-18 15:26:25 +02002374static void
2375test_leaf_elem(void **state)
2376{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002377 struct test_parser_yin_state *st = *state;
David Sedlák203ca3a2019-07-18 15:26:25 +02002378 const char *data;
2379 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002380 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák203ca3a2019-07-18 15:26:25 +02002381 struct lysp_node_leaf *parsed = NULL;
2382
2383 /* max elements */
2384 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002385 "<leaf name=\"leaf\">\n"
2386 " <config value=\"true\" />\n"
2387 " <default value=\"def-val\"/>\n"
2388 " <description><text>desc</text></description>\n"
2389 " <if-feature name=\"feature\" />\n"
2390 " <mandatory value=\"true\" />\n"
2391 " <must condition=\"must-cond\" />\n"
2392 " <reference><text>ref</text></reference>\n"
2393 " <status value=\"deprecated\"/>\n"
2394 " <type name=\"type\"/>\n"
2395 " <units name=\"uni\"/>\n"
2396 " <when condition=\"when-cond\"/>\n"
2397 EXT_SUBELEM
2398 "</leaf>"
David Sedlák203ca3a2019-07-18 15:26:25 +02002399 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002400 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák203ca3a2019-07-18 15:26:25 +02002401 parsed = (struct lysp_node_leaf *)siblings;
2402 assert_null(parsed->parent);
2403 assert_int_equal(parsed->nodetype, LYS_LEAF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002404 assert_true(parsed->flags & LYS_CONFIG_W);
2405 assert_true(parsed->flags & LYS_MAND_TRUE);
2406 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák203ca3a2019-07-18 15:26:25 +02002407 assert_null(parsed->next);
2408 assert_string_equal(parsed->name, "leaf");
2409 assert_string_equal(parsed->dsc, "desc");
2410 assert_string_equal(parsed->ref, "ref");
2411 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002412 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedláke0ef1c62019-09-13 10:05:55 +02002413 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002414 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2415 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002416 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlák203ca3a2019-07-18 15:26:25 +02002417 assert_string_equal(parsed->type.name, "type");
2418 assert_string_equal(parsed->units, "uni");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002419 assert_string_equal(parsed->dflt.str, "def-val");
David Sedlák203ca3a2019-07-18 15:26:25 +02002420 lysp_node_free(st->ctx, siblings);
2421 siblings = NULL;
2422
2423 /* min elements */
2424 data = ELEMENT_WRAPPER_START "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002425 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák203ca3a2019-07-18 15:26:25 +02002426 parsed = (struct lysp_node_leaf *)siblings;
2427 assert_string_equal(parsed->name, "leaf");
2428 assert_string_equal(parsed->type.name, "type");
2429 lysp_node_free(st->ctx, siblings);
2430 siblings = NULL;
2431
2432 st->finished_correctly = true;
2433}
2434
David Sedlákc3da3ef2019-07-19 12:56:08 +02002435static void
2436test_leaf_list_elem(void **state)
2437{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002438 struct test_parser_yin_state *st = *state;
David Sedlákc3da3ef2019-07-19 12:56:08 +02002439 const char *data;
2440 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002441 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlákc3da3ef2019-07-19 12:56:08 +02002442 struct lysp_node_leaflist *parsed = NULL;
2443
2444 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002445 "<leaf-list name=\"llist\">\n"
2446 " <config value=\"true\" />\n"
2447 " <default value=\"def-val0\"/>\n"
2448 " <default value=\"def-val1\"/>\n"
2449 " <description><text>desc</text></description>\n"
2450 " <if-feature name=\"feature\"/>\n"
2451 " <max-elements value=\"5\"/>\n"
2452 " <must condition=\"must-cond\"/>\n"
2453 " <ordered-by value=\"user\" />\n"
2454 " <reference><text>ref</text></reference>\n"
2455 " <status value=\"current\"/>\n"
2456 " <type name=\"type\"/>\n"
2457 " <units name=\"uni\"/>\n"
2458 " <when condition=\"when-cond\"/>\n"
2459 EXT_SUBELEM
2460 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002461 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002462 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002463 parsed = (struct lysp_node_leaflist *)siblings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02002464 assert_string_equal(parsed->dflts[0].str, "def-val0");
2465 assert_string_equal(parsed->dflts[1].str, "def-val1");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002466 assert_string_equal(parsed->dsc, "desc");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002467 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002468 assert_int_equal(parsed->max, 5);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002469 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002470 assert_string_equal(parsed->name, "llist");
2471 assert_null(parsed->next);
2472 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2473 assert_null(parsed->parent);
2474 assert_string_equal(parsed->ref, "ref");
2475 assert_string_equal(parsed->type.name, "type");
2476 assert_string_equal(parsed->units, "uni");
2477 assert_string_equal(parsed->when->cond, "when-cond");
2478 assert_true(parsed->flags & LYS_CONFIG_W);
2479 assert_true(parsed->flags & LYS_ORDBY_USER);
2480 assert_true(parsed->flags & LYS_STATUS_CURR);
David Sedláke0ef1c62019-09-13 10:05:55 +02002481 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002482 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2483 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002484 lysp_node_free(st->ctx, siblings);
2485 siblings = NULL;
2486
2487 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002488 "<leaf-list name=\"llist\">\n"
2489 " <config value=\"true\" />\n"
2490 " <description><text>desc</text></description>\n"
2491 " <if-feature name=\"feature\"/>\n"
2492 " <min-elements value=\"5\"/>\n"
2493 " <must condition=\"must-cond\"/>\n"
2494 " <ordered-by value=\"user\" />\n"
2495 " <reference><text>ref</text></reference>\n"
2496 " <status value=\"current\"/>\n"
2497 " <type name=\"type\"/>\n"
2498 " <units name=\"uni\"/>\n"
2499 " <when condition=\"when-cond\"/>\n"
2500 EXT_SUBELEM
2501 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002502 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002503 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002504 parsed = (struct lysp_node_leaflist *)siblings;
2505 assert_string_equal(parsed->dsc, "desc");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002506 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002507 assert_int_equal(parsed->min, 5);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002508 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002509 assert_string_equal(parsed->name, "llist");
2510 assert_null(parsed->next);
2511 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2512 assert_null(parsed->parent);
2513 assert_string_equal(parsed->ref, "ref");
2514 assert_string_equal(parsed->type.name, "type");
2515 assert_string_equal(parsed->units, "uni");
2516 assert_string_equal(parsed->when->cond, "when-cond");
2517 assert_true(parsed->flags & LYS_CONFIG_W);
2518 assert_true(parsed->flags & LYS_ORDBY_USER);
2519 assert_true(parsed->flags & LYS_STATUS_CURR);
David Sedláke0ef1c62019-09-13 10:05:55 +02002520 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002521 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2522 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002523 lysp_node_free(st->ctx, siblings);
2524 siblings = NULL;
2525
2526 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002527 "<leaf-list name=\"llist\">\n"
2528 " <config value=\"true\" />\n"
2529 " <description><text>desc</text></description>\n"
2530 " <if-feature name=\"feature\"/>\n"
2531 " <max-elements value=\"15\"/>\n"
2532 " <min-elements value=\"5\"/>\n"
2533 " <must condition=\"must-cond\"/>\n"
2534 " <ordered-by value=\"user\" />\n"
2535 " <reference><text>ref</text></reference>\n"
2536 " <status value=\"current\"/>\n"
2537 " <type name=\"type\"/>\n"
2538 " <units name=\"uni\"/>\n"
2539 " <when condition=\"when-cond\"/>\n"
2540 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002541 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002542 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002543 parsed = (struct lysp_node_leaflist *)siblings;
2544 assert_string_equal(parsed->dsc, "desc");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002545 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002546 assert_int_equal(parsed->min, 5);
2547 assert_int_equal(parsed->max, 15);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002548 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002549 assert_string_equal(parsed->name, "llist");
2550 assert_null(parsed->next);
2551 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2552 assert_null(parsed->parent);
2553 assert_string_equal(parsed->ref, "ref");
2554 assert_string_equal(parsed->type.name, "type");
2555 assert_string_equal(parsed->units, "uni");
2556 assert_string_equal(parsed->when->cond, "when-cond");
2557 assert_true(parsed->flags & LYS_CONFIG_W);
2558 assert_true(parsed->flags & LYS_ORDBY_USER);
2559 assert_true(parsed->flags & LYS_STATUS_CURR);
2560 lysp_node_free(st->ctx, siblings);
2561 siblings = NULL;
2562
2563 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002564 "<leaf-list name=\"llist\">\n"
2565 " <type name=\"type\"/>\n"
2566 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002567 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002568 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002569 parsed = (struct lysp_node_leaflist *)siblings;
2570 assert_string_equal(parsed->name, "llist");
2571 assert_string_equal(parsed->type.name, "type");
2572 lysp_node_free(st->ctx, siblings);
2573 siblings = NULL;
2574
2575 /* invalid combinations */
2576 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002577 "<leaf-list name=\"llist\">\n"
2578 " <max-elements value=\"5\"/>\n"
2579 " <min-elements value=\"15\"/>\n"
2580 " <type name=\"type\"/>"
2581 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002582 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002583 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_EVALID);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002584 logbuf_assert("Invalid combination of min-elements and max-elements: min value 15 is bigger than the max value 5. Line number 4.");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002585 lysp_node_free(st->ctx, siblings);
2586 siblings = NULL;
2587
2588 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002589 "<leaf-list name=\"llist\">\n"
2590 " <default value=\"def-val1\"/>\n"
2591 " <min-elements value=\"15\"/>\n"
2592 " <type name=\"type\"/>\n"
2593 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002594 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002595 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_EVALID);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002596 logbuf_assert("Invalid combination of sub-elemnts \"min-elements\" and \"default\" in \"leaf-list\" element. Line number 5.");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002597 lysp_node_free(st->ctx, siblings);
2598 siblings = NULL;
2599
2600 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002601 "<leaf-list name=\"llist\">"
2602 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002603 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002604 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002605 logbuf_assert("Missing mandatory sub-element \"type\" of \"leaf-list\" element. Line number 1.");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002606 lysp_node_free(st->ctx, siblings);
2607 siblings = NULL;
2608
2609 st->finished_correctly = true;
2610}
2611
David Sedlákcb39f642019-07-19 13:19:55 +02002612static void
2613test_presence_elem(void **state)
2614{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002615 struct test_parser_yin_state *st = *state;
David Sedlákcb39f642019-07-19 13:19:55 +02002616 const char *data;
2617 const char *val;
David Sedlákd1144562019-08-06 12:36:14 +02002618 struct lysp_ext_instance *exts = NULL;
2619
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002620 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\">"EXT_SUBELEM "</presence>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002621 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02002622 assert_string_equal(val, "presence-val");
David Sedláke0ef1c62019-09-13 10:05:55 +02002623 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002624 assert_int_equal(exts[0].insubstmt_index, 0);
2625 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_PRESENCE);
2626 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
2627 FREE_STRING(st->ctx, val);
David Sedlákcb39f642019-07-19 13:19:55 +02002628
2629 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002630 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlákcb39f642019-07-19 13:19:55 +02002631 assert_string_equal(val, "presence-val");
2632 FREE_STRING(st->ctx, val);
2633
2634 data = ELEMENT_WRAPPER_START "<presence/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002635 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlákcb39f642019-07-19 13:19:55 +02002636 logbuf_assert("Missing mandatory attribute value of presence element. Line number 1.");
2637
2638 st->finished_correctly = true;
2639}
2640
David Sedlák12470a82019-07-19 13:44:36 +02002641static void
2642test_key_elem(void **state)
2643{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002644 struct test_parser_yin_state *st = *state;
David Sedlák12470a82019-07-19 13:44:36 +02002645 const char *data;
2646 const char *val;
David Sedlákd1144562019-08-06 12:36:14 +02002647 struct lysp_ext_instance *exts = NULL;
2648
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002649 data = ELEMENT_WRAPPER_START "<key value=\"key-value\">"EXT_SUBELEM "</key>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002650 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02002651 assert_string_equal(val, "key-value");
David Sedláke0ef1c62019-09-13 10:05:55 +02002652 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002653 assert_int_equal(exts[0].insubstmt_index, 0);
2654 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_KEY);
2655 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
2656 FREE_STRING(st->ctx, val);
David Sedlák12470a82019-07-19 13:44:36 +02002657
2658 data = ELEMENT_WRAPPER_START "<key value=\"key-value\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002659 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák12470a82019-07-19 13:44:36 +02002660 assert_string_equal(val, "key-value");
2661 FREE_STRING(st->ctx, val);
2662
2663 data = ELEMENT_WRAPPER_START "<key/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002664 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák12470a82019-07-19 13:44:36 +02002665 logbuf_assert("Missing mandatory attribute value of key element. Line number 1.");
2666
2667 st->finished_correctly = true;
2668}
2669
David Sedlák04e17b22019-07-19 15:29:48 +02002670static void
2671test_typedef_elem(void **state)
2672{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002673 struct test_parser_yin_state *st = *state;
David Sedlák04e17b22019-07-19 15:29:48 +02002674 const char *data;
2675 struct lysp_tpdf *tpdfs = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002676 struct tree_node_meta typdef_meta = {NULL, (struct lysp_node **)&tpdfs};
David Sedlák04e17b22019-07-19 15:29:48 +02002677
2678 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002679 "<typedef name=\"tpdf-name\">\n"
2680 " <default value=\"def-val\"/>\n"
2681 " <description><text>desc-text</text></description>\n"
2682 " <reference><text>ref-text</text></reference>\n"
2683 " <status value=\"current\"/>\n"
2684 " <type name=\"type\"/>\n"
2685 " <units name=\"uni\"/>\n"
2686 EXT_SUBELEM
2687 "</typedef>"
2688 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002689 assert_int_equal(test_element_helper(st, data, &typdef_meta, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002690 assert_string_equal(tpdfs[0].dflt.str, "def-val");
David Sedlák04e17b22019-07-19 15:29:48 +02002691 assert_string_equal(tpdfs[0].dsc, "desc-text");
David Sedlák04e17b22019-07-19 15:29:48 +02002692 assert_string_equal(tpdfs[0].name, "tpdf-name");
2693 assert_string_equal(tpdfs[0].ref, "ref-text");
2694 assert_string_equal(tpdfs[0].type.name, "type");
2695 assert_string_equal(tpdfs[0].units, "uni");
2696 assert_true(tpdfs[0].flags & LYS_STATUS_CURR);
David Sedláke0ef1c62019-09-13 10:05:55 +02002697 assert_string_equal(tpdfs[0].exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002698 assert_int_equal(tpdfs[0].exts[0].insubstmt_index, 0);
2699 assert_int_equal(tpdfs[0].exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák04e17b22019-07-19 15:29:48 +02002700 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2701 tpdfs = NULL;
2702
2703 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002704 "<typedef name=\"tpdf-name\">\n"
2705 " <type name=\"type\"/>\n"
2706 "</typedef>"
2707 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002708 assert_int_equal(test_element_helper(st, data, &typdef_meta, NULL, NULL), LY_SUCCESS);
David Sedlák04e17b22019-07-19 15:29:48 +02002709 assert_string_equal(tpdfs[0].name, "tpdf-name");
2710 assert_string_equal(tpdfs[0].type.name, "type");
2711 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2712 tpdfs = NULL;
2713
2714 st->finished_correctly = true;
2715}
2716
David Sedlákd2d676a2019-07-22 11:28:19 +02002717static void
2718test_refine_elem(void **state)
2719{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002720 struct test_parser_yin_state *st = *state;
David Sedlákd2d676a2019-07-22 11:28:19 +02002721 const char *data;
2722 struct lysp_refine *refines = NULL;
2723
2724 /* max subelems */
2725 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002726 "<refine target-node=\"target\">\n"
2727 " <if-feature name=\"feature\" />\n"
2728 " <must condition=\"cond\" />\n"
2729 " <presence value=\"presence\" />\n"
2730 " <default value=\"def\" />\n"
2731 " <config value=\"true\" />\n"
2732 " <mandatory value=\"true\" />\n"
2733 " <min-elements value=\"10\" />\n"
2734 " <max-elements value=\"20\" />\n"
2735 " <description><text>desc</text></description>\n"
2736 " <reference><text>ref</text></reference>\n"
2737 EXT_SUBELEM
2738 "</refine>"
2739 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002740 assert_int_equal(test_element_helper(st, data, &refines, NULL, NULL), LY_SUCCESS);
David Sedlákd2d676a2019-07-22 11:28:19 +02002741 assert_string_equal(refines->nodeid, "target");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002742 assert_string_equal(refines->dflts[0].str, "def");
David Sedlákd2d676a2019-07-22 11:28:19 +02002743 assert_string_equal(refines->dsc, "desc");
David Sedlákd2d676a2019-07-22 11:28:19 +02002744 assert_true(refines->flags & LYS_CONFIG_W);
2745 assert_true(refines->flags & LYS_MAND_TRUE);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002746 assert_string_equal(refines->iffeatures[0].str, "feature");
David Sedlákd2d676a2019-07-22 11:28:19 +02002747 assert_int_equal(refines->max, 20);
2748 assert_int_equal(refines->min, 10);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002749 assert_string_equal(refines->musts->arg.str, "cond");
David Sedlákd2d676a2019-07-22 11:28:19 +02002750 assert_string_equal(refines->presence, "presence");
2751 assert_string_equal(refines->ref, "ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02002752 assert_string_equal(refines->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002753 assert_int_equal(refines->exts[0].insubstmt_index, 0);
2754 assert_int_equal(refines->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákd2d676a2019-07-22 11:28:19 +02002755 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2756 refines = NULL;
2757
2758 /* min subelems */
2759 data = ELEMENT_WRAPPER_START "<refine target-node=\"target\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002760 assert_int_equal(test_element_helper(st, data, &refines, NULL, NULL), LY_SUCCESS);
David Sedlákd2d676a2019-07-22 11:28:19 +02002761 assert_string_equal(refines->nodeid, "target");
2762 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2763 refines = NULL;
2764
2765 st->finished_correctly = true;
2766}
2767
David Sedlák0d6de5a2019-07-22 13:25:44 +02002768static void
2769test_uses_elem(void **state)
2770{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002771 struct test_parser_yin_state *st = *state;
David Sedlák0d6de5a2019-07-22 13:25:44 +02002772 const char *data;
2773 struct lysp_node *siblings = NULL;
2774 struct tree_node_meta node_meta = {NULL, &siblings};
2775 struct lysp_node_uses *parsed = NULL;
2776
2777 /* max subelems */
2778 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002779 "<uses name=\"uses-name\">\n"
2780 " <when condition=\"cond\" />\n"
2781 " <if-feature name=\"feature\" />\n"
2782 " <status value=\"obsolete\" />\n"
2783 " <description><text>desc</text></description>\n"
2784 " <reference><text>ref</text></reference>\n"
2785 " <refine target-node=\"target\"/>\n"
2786 " <augment target-node=\"target\" />\n"
2787 EXT_SUBELEM
2788 "</uses>"
2789 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002790 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002791 parsed = (struct lysp_node_uses *)&siblings[0];
2792 assert_string_equal(parsed->name, "uses-name");
2793 assert_string_equal(parsed->dsc, "desc");
David Sedlák0d6de5a2019-07-22 13:25:44 +02002794 assert_true(parsed->flags & LYS_STATUS_OBSLT);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002795 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlák0d6de5a2019-07-22 13:25:44 +02002796 assert_null(parsed->next);
2797 assert_int_equal(parsed->nodetype, LYS_USES);
2798 assert_null(parsed->parent);
2799 assert_string_equal(parsed->ref, "ref");
2800 assert_string_equal(parsed->refines->nodeid, "target");
2801 assert_string_equal(parsed->when->cond, "cond");
David Sedlák992fb7c2019-07-24 16:51:01 +02002802 assert_string_equal(parsed->augments->nodeid, "target");
David Sedláke0ef1c62019-09-13 10:05:55 +02002803 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002804 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2805 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002806 lysp_node_free(st->ctx, siblings);
2807 siblings = NULL;
2808
2809 /* min subelems */
2810 data = ELEMENT_WRAPPER_START "<uses name=\"uses-name\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002811 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002812 assert_string_equal(siblings[0].name, "uses-name");
2813 lysp_node_free(st->ctx, siblings);
2814 siblings = NULL;
2815
2816 st->finished_correctly = true;
2817}
2818
David Sedlákaa854b02019-07-22 14:17:10 +02002819static void
2820test_revision_elem(void **state)
2821{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002822 struct test_parser_yin_state *st = *state;
David Sedlákaa854b02019-07-22 14:17:10 +02002823 const char *data;
2824 struct lysp_revision *revs = NULL;
2825
2826 /* max subelems */
2827 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002828 "<revision date=\"2018-12-25\">\n"
2829 " <description><text>desc</text></description>\n"
2830 " <reference><text>ref</text></reference>\n"
2831 EXT_SUBELEM
2832 "</revision>"
2833 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002834 assert_int_equal(test_element_helper(st, data, &revs, NULL, NULL), LY_SUCCESS);
David Sedlákaa854b02019-07-22 14:17:10 +02002835 assert_string_equal(revs->date, "2018-12-25");
2836 assert_string_equal(revs->dsc, "desc");
2837 assert_string_equal(revs->ref, "ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02002838 assert_string_equal(revs->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002839 assert_int_equal(revs->exts[0].insubstmt_index, 0);
2840 assert_int_equal(revs->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákaa854b02019-07-22 14:17:10 +02002841 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2842 revs = NULL;
2843
2844 /* min subelems */
2845 data = ELEMENT_WRAPPER_START "<revision date=\"2005-05-05\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002846 assert_int_equal(test_element_helper(st, data, &revs, NULL, NULL), LY_SUCCESS);
David Sedlákaa854b02019-07-22 14:17:10 +02002847 assert_string_equal(revs->date, "2005-05-05");
2848 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2849 revs = NULL;
2850
2851 /* invalid value */
2852 data = ELEMENT_WRAPPER_START "<revision date=\"05-05-2005\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002853 assert_int_equal(test_element_helper(st, data, &revs, NULL, NULL), LY_EVALID);
David Sedlákaa854b02019-07-22 14:17:10 +02002854 logbuf_assert("Invalid value \"05-05-2005\" of \"revision\". Line number 1.");
2855 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2856 revs = NULL;
2857
2858 st->finished_correctly = true;
2859}
2860
David Sedlák0c2bab92019-07-22 15:33:19 +02002861static void
2862test_include_elem(void **state)
2863{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002864 struct test_parser_yin_state *st = *state;
David Sedlák0c2bab92019-07-22 15:33:19 +02002865 const char *data;
2866 struct lysp_include *includes = NULL;
2867 struct include_meta inc_meta = {"module-name", &includes};
2868
2869 /* max subelems */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002870 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák0c2bab92019-07-22 15:33:19 +02002871 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002872 "<include module=\"mod\">\n"
2873 " <description><text>desc</text></description>\n"
2874 " <reference><text>ref</text></reference>\n"
2875 " <revision-date date=\"1999-09-09\"/>\n"
2876 EXT_SUBELEM
2877 "</include>"
2878 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002879 assert_int_equal(test_element_helper(st, data, &inc_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0c2bab92019-07-22 15:33:19 +02002880 assert_string_equal(includes->name, "mod");
2881 assert_string_equal(includes->dsc, "desc");
2882 assert_string_equal(includes->ref, "ref");
David Sedlák0c2bab92019-07-22 15:33:19 +02002883 assert_string_equal(includes->rev, "1999-09-09");
David Sedláke0ef1c62019-09-13 10:05:55 +02002884 assert_string_equal(includes->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002885 assert_int_equal(includes->exts[0].insubstmt_index, 0);
2886 assert_int_equal(includes->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák0c2bab92019-07-22 15:33:19 +02002887 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2888 includes = NULL;
2889
2890 /* min subelems */
2891 data = ELEMENT_WRAPPER_START "<include module=\"mod\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002892 assert_int_equal(test_element_helper(st, data, &inc_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0c2bab92019-07-22 15:33:19 +02002893 assert_string_equal(includes->name, "mod");
2894 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2895 includes = NULL;
2896
2897 /* invalid combinations */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002898 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_0;
David Sedlák0c2bab92019-07-22 15:33:19 +02002899 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002900 "<include module=\"mod\">\n"
2901 " <description><text>desc</text></description>\n"
2902 " <revision-date date=\"1999-09-09\"/>\n"
2903 "</include>"
2904 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002905 assert_int_equal(test_element_helper(st, data, &inc_meta, NULL, NULL), LY_EVALID);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002906 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 2.");
David Sedlák0c2bab92019-07-22 15:33:19 +02002907 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2908 includes = NULL;
2909
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002910 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_0;
David Sedlák0c2bab92019-07-22 15:33:19 +02002911 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002912 "<include module=\"mod\">\n"
2913 " <reference><text>ref</text></reference>\n"
2914 " <revision-date date=\"1999-09-09\"/>\n"
2915 "</include>"
2916 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002917 assert_int_equal(test_element_helper(st, data, &inc_meta, NULL, NULL), LY_EVALID);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002918 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 2.");
David Sedlák0c2bab92019-07-22 15:33:19 +02002919 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2920 includes = NULL;
2921
2922 st->finished_correctly = true;
2923}
2924
David Sedlák5e13dea2019-07-22 16:06:45 +02002925static void
David Sedlákaf536aa2019-07-23 13:42:23 +02002926test_list_elem(void **state)
2927{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002928 struct test_parser_yin_state *st = *state;
David Sedlákaf536aa2019-07-23 13:42:23 +02002929 const char *data;
2930 struct lysp_node *siblings = NULL;
2931 struct tree_node_meta node_meta = {NULL, &siblings};
2932 struct lysp_node_list *parsed = NULL;
2933
2934 /* max subelems */
2935 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002936 "<list name=\"list-name\">\n"
2937 " <when condition=\"when\"/>\n"
2938 " <if-feature name=\"iff\"/>\n"
2939 " <must condition=\"must-cond\"/>\n"
2940 " <key value=\"key\"/>\n"
2941 " <unique tag=\"utag\"/>\n"
2942 " <config value=\"true\"/>\n"
2943 " <min-elements value=\"10\"/>\n"
2944 " <ordered-by value=\"user\"/>\n"
2945 " <status value=\"deprecated\"/>\n"
2946 " <description><text>desc</text></description>\n"
2947 " <reference><text>ref</text></reference>\n"
2948 " <anydata name=\"anyd\"/>\n"
2949 " <anyxml name=\"anyx\"/>\n"
2950 " <container name=\"cont\"/>\n"
2951 " <choice name=\"choice\"/>\n"
2952 " <action name=\"action\"/>\n"
2953 " <grouping name=\"grp\"/>\n"
2954 " <notification name=\"notf\"/>\n"
2955 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2956 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2957 " <list name=\"sub-list\"/>\n"
2958 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2959 " <uses name=\"uses-name\"/>\n"
2960 EXT_SUBELEM
2961 "</list>"
2962 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002963 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákaf536aa2019-07-23 13:42:23 +02002964 parsed = (struct lysp_node_list *)&siblings[0];
2965 assert_string_equal(parsed->dsc, "desc");
2966 assert_string_equal(parsed->child->name, "anyd");
2967 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2968 assert_string_equal(parsed->child->next->name, "anyx");
2969 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
David Sedlákf111bcb2019-07-23 17:15:51 +02002970 assert_string_equal(parsed->child->next->next->name, "cont");
2971 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002972 assert_string_equal(parsed->child->next->next->next->name, "choice");
2973 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
David Sedlák85d0eca2019-07-24 15:15:21 +02002974 assert_string_equal(parsed->child->next->next->next->next->name, "leaf");
2975 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAF);
2976 assert_string_equal(parsed->child->next->next->next->next->next->name, "llist");
2977 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2978 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "sub-list");
2979 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LIST);
2980 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "uses-name");
2981 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_USES);
2982 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002983 assert_string_equal(parsed->groupings->name, "grp");
David Sedlák85d0eca2019-07-24 15:15:21 +02002984 assert_string_equal(parsed->actions->name, "action");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002985 assert_int_equal(parsed->groupings->nodetype, LYS_GROUPING);
David Sedlák031b9e72019-07-23 15:19:37 +02002986 assert_string_equal(parsed->notifs->name, "notf");
David Sedlákaf536aa2019-07-23 13:42:23 +02002987 assert_true(parsed->flags & LYS_ORDBY_USER);
2988 assert_true(parsed->flags & LYS_STATUS_DEPRC);
2989 assert_true(parsed->flags & LYS_CONFIG_W);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002990 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlákaf536aa2019-07-23 13:42:23 +02002991 assert_string_equal(parsed->key, "key");
2992 assert_int_equal(parsed->min, 10);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002993 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlákaf536aa2019-07-23 13:42:23 +02002994 assert_string_equal(parsed->name, "list-name");
2995 assert_null(parsed->next);
2996 assert_int_equal(parsed->nodetype, LYS_LIST);
2997 assert_null(parsed->parent);
2998 assert_string_equal(parsed->ref, "ref");
2999 assert_string_equal(parsed->typedefs->name, "tpdf");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003000 assert_string_equal(parsed->uniques->str, "utag");
David Sedlákaf536aa2019-07-23 13:42:23 +02003001 assert_string_equal(parsed->when->cond, "when");
David Sedláke0ef1c62019-09-13 10:05:55 +02003002 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003003 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3004 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákaf536aa2019-07-23 13:42:23 +02003005 lysp_node_free(st->ctx, siblings);
3006 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
3007 siblings = NULL;
3008
3009 /* min subelems */
3010 data = ELEMENT_WRAPPER_START "<list name=\"list-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003011 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákaf536aa2019-07-23 13:42:23 +02003012 parsed = (struct lysp_node_list *)&siblings[0];
3013 assert_string_equal(parsed->name, "list-name");
3014 lysp_node_free(st->ctx, siblings);
3015 siblings = NULL;
3016
3017 st->finished_correctly = true;
3018}
3019
David Sedlák031b9e72019-07-23 15:19:37 +02003020static void
3021test_notification_elem(void **state)
3022{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003023 struct test_parser_yin_state *st = *state;
David Sedlák031b9e72019-07-23 15:19:37 +02003024 const char *data;
3025 struct lysp_notif *notifs = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003026 struct tree_node_meta notif_meta = {NULL, (struct lysp_node **)&notifs};
David Sedlák031b9e72019-07-23 15:19:37 +02003027
3028 /* max subelems */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003029 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák031b9e72019-07-23 15:19:37 +02003030 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003031 "<notification name=\"notif-name\">\n"
3032 " <anydata name=\"anyd\"/>\n"
3033 " <anyxml name=\"anyx\"/>\n"
3034 " <description><text>desc</text></description>\n"
3035 " <if-feature name=\"iff\"/>\n"
3036 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3037 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3038 " <list name=\"sub-list\"/>\n"
3039 " <must condition=\"cond\"/>\n"
3040 " <reference><text>ref</text></reference>\n"
3041 " <status value=\"deprecated\"/>\n"
3042 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3043 " <uses name=\"uses-name\"/>\n"
3044 " <container name=\"cont\"/>\n"
3045 " <choice name=\"choice\"/>\n"
3046 " <grouping name=\"grp\"/>\n"
3047 EXT_SUBELEM
3048 "</notification>"
3049 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003050 assert_int_equal(test_element_helper(st, data, &notif_meta, NULL, NULL), LY_SUCCESS);
David Sedlák031b9e72019-07-23 15:19:37 +02003051 assert_string_equal(notifs->name, "notif-name");
3052 assert_string_equal(notifs->data->name, "anyd");
3053 assert_int_equal(notifs->data->nodetype, LYS_ANYDATA);
3054 assert_string_equal(notifs->data->next->name, "anyx");
3055 assert_int_equal(notifs->data->next->nodetype, LYS_ANYXML);
3056 assert_string_equal(notifs->data->next->next->name, "leaf");
3057 assert_int_equal(notifs->data->next->next->nodetype, LYS_LEAF);
3058 assert_string_equal(notifs->data->next->next->next->name, "llist");
3059 assert_int_equal(notifs->data->next->next->next->nodetype, LYS_LEAFLIST);
3060 assert_string_equal(notifs->data->next->next->next->next->name, "sub-list");
3061 assert_int_equal(notifs->data->next->next->next->next->nodetype, LYS_LIST);
David Sedlák031b9e72019-07-23 15:19:37 +02003062 assert_true(notifs->flags & LYS_STATUS_DEPRC);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003063 assert_string_equal(notifs->groupings->name, "grp");
3064 assert_int_equal(notifs->groupings->nodetype, LYS_GROUPING);
David Sedlákf111bcb2019-07-23 17:15:51 +02003065 assert_string_equal(notifs->data->next->next->next->next->next->name, "uses-name");
3066 assert_int_equal(notifs->data->next->next->next->next->next->nodetype, LYS_USES);
3067 assert_string_equal(notifs->data->next->next->next->next->next->next->name, "cont");
3068 assert_int_equal(notifs->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003069 assert_int_equal(notifs->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
3070 assert_string_equal(notifs->data->next->next->next->next->next->next->next->name, "choice");
3071 assert_null(notifs->data->next->next->next->next->next->next->next->next);
Michal Vasko7f45cf22020-10-01 12:49:44 +02003072 assert_string_equal(notifs->iffeatures[0].str, "iff");
3073 assert_string_equal(notifs->musts->arg.str, "cond");
David Sedlák031b9e72019-07-23 15:19:37 +02003074 assert_int_equal(notifs->nodetype, LYS_NOTIF);
3075 assert_null(notifs->parent);
3076 assert_string_equal(notifs->ref, "ref");
3077 assert_string_equal(notifs->typedefs->name, "tpdf");
David Sedláke0ef1c62019-09-13 10:05:55 +02003078 assert_string_equal(notifs->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003079 assert_int_equal(notifs->exts[0].insubstmt_index, 0);
3080 assert_int_equal(notifs->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák031b9e72019-07-23 15:19:37 +02003081 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
3082 notifs = NULL;
3083
3084 /* min subelems */
3085 data = ELEMENT_WRAPPER_START "<notification name=\"notif-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003086 assert_int_equal(test_element_helper(st, data, &notif_meta, NULL, NULL), LY_SUCCESS);
David Sedlák031b9e72019-07-23 15:19:37 +02003087 assert_string_equal(notifs->name, "notif-name");
3088 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003089 notifs = NULL;
3090
3091 st->finished_correctly = true;
3092}
3093
3094static void
3095test_grouping_elem(void **state)
3096{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003097 struct test_parser_yin_state *st = *state;
David Sedláke3ce9ef2019-07-23 16:34:30 +02003098 const char *data;
3099 struct lysp_grp *grps = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003100 struct tree_node_meta grp_meta = {NULL, (struct lysp_node **)&grps};
David Sedláke3ce9ef2019-07-23 16:34:30 +02003101
3102 /* max subelems */
3103 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003104 "<grouping name=\"grp-name\">\n"
3105 " <anydata name=\"anyd\"/>\n"
3106 " <anyxml name=\"anyx\"/>\n"
3107 " <description><text>desc</text></description>\n"
3108 " <grouping name=\"sub-grp\"/>\n"
3109 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3110 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3111 " <list name=\"list\"/>\n"
3112 " <notification name=\"notf\"/>\n"
3113 " <reference><text>ref</text></reference>\n"
3114 " <status value=\"current\"/>\n"
3115 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3116 " <uses name=\"uses-name\"/>\n"
3117 " <action name=\"act\"/>\n"
3118 " <container name=\"cont\"/>\n"
3119 " <choice name=\"choice\"/>\n"
3120 EXT_SUBELEM
3121 "</grouping>"
3122 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003123 assert_int_equal(test_element_helper(st, data, &grp_meta, NULL, NULL), LY_SUCCESS);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003124 assert_string_equal(grps->name, "grp-name");
David Sedláke3ce9ef2019-07-23 16:34:30 +02003125 assert_string_equal(grps->data->name, "anyd");
3126 assert_string_equal(grps->data->next->name, "anyx");
3127 assert_string_equal(grps->data->next->next->name, "leaf");
3128 assert_string_equal(grps->data->next->next->next->name, "llist");
3129 assert_string_equal(grps->data->next->next->next->next->name, "list");
3130 assert_string_equal(grps->dsc, "desc");
David Sedláke3ce9ef2019-07-23 16:34:30 +02003131 assert_true(grps->flags & LYS_STATUS_CURR);
3132 assert_string_equal(grps->groupings->name, "sub-grp");
3133 assert_int_equal(grps->nodetype, LYS_GROUPING);
3134 assert_string_equal(grps->notifs->name, "notf");
3135 assert_null(grps->parent);
3136 assert_string_equal(grps->ref, "ref");
3137 assert_string_equal(grps->typedefs->name, "tpdf");
David Sedlák85d0eca2019-07-24 15:15:21 +02003138 assert_string_equal(grps->actions->name, "act");
David Sedlákf111bcb2019-07-23 17:15:51 +02003139 assert_string_equal(grps->data->next->next->next->next->next->name, "uses-name");
David Sedlákb7abcfa2019-07-24 12:33:35 +02003140 assert_int_equal(grps->data->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákf111bcb2019-07-23 17:15:51 +02003141 assert_string_equal(grps->data->next->next->next->next->next->next->name, "cont");
David Sedlákb7abcfa2019-07-24 12:33:35 +02003142 assert_int_equal(grps->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
3143 assert_string_equal(grps->data->next->next->next->next->next->next->next->name, "choice");
3144 assert_int_equal(grps->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
David Sedláke0ef1c62019-09-13 10:05:55 +02003145 assert_string_equal(grps->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003146 assert_int_equal(grps->exts[0].insubstmt_index, 0);
3147 assert_int_equal(grps->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003148 FREE_ARRAY(st->ctx, grps, lysp_grp_free);
3149 grps = NULL;
3150
3151 /* min subelems */
3152 data = ELEMENT_WRAPPER_START "<grouping name=\"grp-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003153 assert_int_equal(test_element_helper(st, data, &grp_meta, NULL, NULL), LY_SUCCESS);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003154 assert_string_equal(grps->name, "grp-name");
3155 FREE_ARRAY(st->ctx, grps, lysp_grp_free);
3156 grps = NULL;
David Sedlák031b9e72019-07-23 15:19:37 +02003157
3158 st->finished_correctly = true;
3159}
3160
David Sedlákf111bcb2019-07-23 17:15:51 +02003161static void
3162test_container_elem(void **state)
3163{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003164 struct test_parser_yin_state *st = *state;
David Sedlákf111bcb2019-07-23 17:15:51 +02003165 const char *data;
3166 struct lysp_node *siblings = NULL;
3167 struct tree_node_meta node_meta = {NULL, &siblings};
3168 struct lysp_node_container *parsed = NULL;
3169
3170 /* max subelems */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003171 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedláke2dc9e92019-07-24 09:59:21 +02003172 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003173 "<container name=\"cont-name\">\n"
3174 " <anydata name=\"anyd\"/>\n"
3175 " <anyxml name=\"anyx\"/>\n"
3176 " <config value=\"true\"/>\n"
3177 " <container name=\"subcont\"/>\n"
3178 " <description><text>desc</text></description>\n"
3179 " <grouping name=\"sub-grp\"/>\n"
3180 " <if-feature name=\"iff\"/>\n"
3181 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3182 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3183 " <list name=\"list\"/>\n"
3184 " <must condition=\"cond\"/>\n"
3185 " <notification name=\"notf\"/>\n"
3186 " <presence value=\"presence\"/>\n"
3187 " <reference><text>ref</text></reference>\n"
3188 " <status value=\"current\"/>\n"
3189 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3190 " <uses name=\"uses-name\"/>\n"
3191 " <when condition=\"when-cond\"/>\n"
3192 " <action name=\"act\"/>\n"
3193 " <choice name=\"choice\"/>\n"
3194 EXT_SUBELEM
3195 "</container>"
3196 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003197 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedláke2dc9e92019-07-24 09:59:21 +02003198 parsed = (struct lysp_node_container *)siblings;
3199 assert_string_equal(parsed->name, "cont-name");
3200 assert_null(parsed->parent);
3201 assert_int_equal(parsed->nodetype, LYS_CONTAINER);
3202 assert_true(parsed->flags & LYS_CONFIG_W);
3203 assert_true(parsed->flags & LYS_STATUS_CURR);
3204 assert_null(parsed->next);
3205 assert_string_equal(parsed->dsc, "desc");
3206 assert_string_equal(parsed->ref, "ref");
3207 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003208 assert_string_equal(parsed->iffeatures[0].str, "iff");
3209 assert_string_equal(parsed->musts->arg.str, "cond");
David Sedláke2dc9e92019-07-24 09:59:21 +02003210 assert_string_equal(parsed->presence, "presence");
3211 assert_string_equal(parsed->typedefs->name, "tpdf");
3212 assert_string_equal(parsed->groupings->name, "sub-grp");
3213 assert_string_equal(parsed->child->name, "anyd");
3214 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
3215 assert_string_equal(parsed->child->next->name, "anyx");
3216 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
3217 assert_string_equal(parsed->child->next->next->name, "subcont");
3218 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
3219 assert_string_equal(parsed->child->next->next->next->name, "leaf");
3220 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
3221 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
3222 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
3223 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
3224 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
3225 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
3226 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003227 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
3228 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
3229 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke2dc9e92019-07-24 09:59:21 +02003230 assert_string_equal(parsed->notifs->name, "notf");
David Sedlák85d0eca2019-07-24 15:15:21 +02003231 assert_string_equal(parsed->actions->name, "act");
David Sedláke0ef1c62019-09-13 10:05:55 +02003232 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003233 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3234 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedláke2dc9e92019-07-24 09:59:21 +02003235 lysp_node_free(st->ctx, siblings);
3236 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
3237 siblings = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02003238
3239 /* min subelems */
3240 data = ELEMENT_WRAPPER_START "<container name=\"cont-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003241 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákf111bcb2019-07-23 17:15:51 +02003242 parsed = (struct lysp_node_container *)siblings;
3243 assert_string_equal(parsed->name, "cont-name");
3244 lysp_node_free(st->ctx, siblings);
3245 siblings = NULL;
3246
3247 st->finished_correctly = true;
3248}
3249
David Sedlák5379d392019-07-24 10:42:03 +02003250static void
3251test_case_elem(void **state)
3252{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003253 struct test_parser_yin_state *st = *state;
David Sedlák5379d392019-07-24 10:42:03 +02003254 const char *data;
3255 struct lysp_node *siblings = NULL;
3256 struct tree_node_meta node_meta = {NULL, &siblings};
3257 struct lysp_node_case *parsed = NULL;
3258
3259 /* max subelems */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003260 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák5379d392019-07-24 10:42:03 +02003261 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003262 "<case name=\"case-name\">\n"
3263 " <anydata name=\"anyd\"/>\n"
3264 " <anyxml name=\"anyx\"/>\n"
3265 " <container name=\"subcont\"/>\n"
3266 " <description><text>desc</text></description>\n"
3267 " <if-feature name=\"iff\"/>\n"
3268 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3269 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3270 " <list name=\"list\"/>\n"
3271 " <reference><text>ref</text></reference>\n"
3272 " <status value=\"current\"/>\n"
3273 " <uses name=\"uses-name\"/>\n"
3274 " <when condition=\"when-cond\"/>\n"
3275 " <choice name=\"choice\"/>\n"
3276 EXT_SUBELEM
3277 "</case>"
3278 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003279 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák5379d392019-07-24 10:42:03 +02003280 parsed = (struct lysp_node_case *)siblings;
3281 assert_string_equal(parsed->name, "case-name");
3282 assert_null(parsed->parent);
3283 assert_int_equal(parsed->nodetype, LYS_CASE);
3284 assert_true(parsed->flags & LYS_STATUS_CURR);
3285 assert_null(parsed->next);
3286 assert_string_equal(parsed->dsc, "desc");
3287 assert_string_equal(parsed->ref, "ref");
3288 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003289 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlák5379d392019-07-24 10:42:03 +02003290 assert_string_equal(parsed->child->name, "anyd");
3291 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
3292 assert_string_equal(parsed->child->next->name, "anyx");
3293 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
3294 assert_string_equal(parsed->child->next->next->name, "subcont");
3295 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
3296 assert_string_equal(parsed->child->next->next->next->name, "leaf");
3297 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
3298 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
3299 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
3300 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
3301 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
3302 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
3303 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003304 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
3305 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
3306 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke0ef1c62019-09-13 10:05:55 +02003307 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003308 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3309 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák5379d392019-07-24 10:42:03 +02003310 lysp_node_free(st->ctx, siblings);
3311 siblings = NULL;
3312
3313 /* min subelems */
3314 data = ELEMENT_WRAPPER_START "<case name=\"case-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003315 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák5379d392019-07-24 10:42:03 +02003316 parsed = (struct lysp_node_case *)siblings;
3317 assert_string_equal(parsed->name, "case-name");
3318 lysp_node_free(st->ctx, siblings);
3319 siblings = NULL;
3320
3321 st->finished_correctly = true;
3322}
3323
David Sedlákb7abcfa2019-07-24 12:33:35 +02003324static void
3325test_choice_elem(void **state)
3326{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003327 struct test_parser_yin_state *st = *state;
David Sedlákb7abcfa2019-07-24 12:33:35 +02003328 const char *data;
3329 struct lysp_node *siblings = NULL;
3330 struct tree_node_meta node_meta = {NULL, &siblings};
3331 struct lysp_node_choice *parsed = NULL;
3332
3333 /* max subelems */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003334 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlákb7abcfa2019-07-24 12:33:35 +02003335 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003336 "<choice name=\"choice-name\">\n"
3337 " <anydata name=\"anyd\"/>\n"
3338 " <anyxml name=\"anyx\"/>\n"
3339 " <case name=\"sub-case\"/>\n"
3340 " <choice name=\"choice\"/>\n"
3341 " <config value=\"true\"/>\n"
3342 " <container name=\"subcont\"/>\n"
3343 " <default value=\"def\"/>\n"
3344 " <description><text>desc</text></description>\n"
3345 " <if-feature name=\"iff\"/>\n"
3346 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3347 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3348 " <list name=\"list\"/>\n"
3349 " <mandatory value=\"true\" />\n"
3350 " <reference><text>ref</text></reference>\n"
3351 " <status value=\"current\"/>\n"
3352 " <when condition=\"when-cond\"/>\n"
3353 EXT_SUBELEM
3354 "</choice>"
3355 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003356 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003357 parsed = (struct lysp_node_choice *)siblings;
3358 assert_string_equal(parsed->name, "choice-name");
3359 assert_null(parsed->parent);
3360 assert_int_equal(parsed->nodetype, LYS_CHOICE);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003361 assert_true(parsed->flags & LYS_CONFIG_W && parsed->flags & LYS_MAND_TRUE && parsed->flags & LYS_STATUS_CURR);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003362 assert_null(parsed->next);
3363 assert_string_equal(parsed->dsc, "desc");
3364 assert_string_equal(parsed->ref, "ref");
3365 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003366 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlákb7abcfa2019-07-24 12:33:35 +02003367 assert_string_equal(parsed->child->name, "anyd");
3368 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
3369 assert_string_equal(parsed->child->next->name, "anyx");
3370 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
3371 assert_string_equal(parsed->child->next->next->name, "sub-case");
3372 assert_int_equal(parsed->child->next->next->nodetype, LYS_CASE);
3373 assert_string_equal(parsed->child->next->next->next->name, "choice");
3374 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
3375 assert_string_equal(parsed->child->next->next->next->next->name, "subcont");
3376 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_CONTAINER);
3377 assert_string_equal(parsed->child->next->next->next->next->next->name, "leaf");
3378 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAF);
3379 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "llist");
3380 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3381 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "list");
3382 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
3383 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke0ef1c62019-09-13 10:05:55 +02003384 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003385 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3386 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003387 lysp_node_free(st->ctx, siblings);
3388 siblings = NULL;
3389
3390 /* min subelems */
3391 data = ELEMENT_WRAPPER_START "<choice name=\"choice-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003392 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003393 parsed = (struct lysp_node_choice *)siblings;
3394 assert_string_equal(parsed->name, "choice-name");
3395 lysp_node_free(st->ctx, siblings);
3396 siblings = NULL;
3397
3398 st->finished_correctly = true;
3399}
3400
David Sedlák05404f62019-07-24 14:11:53 +02003401static void
3402test_inout_elem(void **state)
3403{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003404 struct test_parser_yin_state *st = *state;
David Sedlák05404f62019-07-24 14:11:53 +02003405 const char *data;
3406 struct lysp_action_inout inout = {};
3407 struct inout_meta inout_meta = {NULL, &inout};
3408
3409 /* max subelements */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003410 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák05404f62019-07-24 14:11:53 +02003411 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003412 "<input>\n"
3413 " <anydata name=\"anyd\"/>\n"
3414 " <anyxml name=\"anyx\"/>\n"
3415 " <choice name=\"choice\"/>\n"
3416 " <container name=\"subcont\"/>\n"
3417 " <grouping name=\"sub-grp\"/>\n"
3418 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3419 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3420 " <list name=\"list\"/>\n"
3421 " <must condition=\"cond\"/>\n"
3422 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3423 " <uses name=\"uses-name\"/>\n"
3424 EXT_SUBELEM
3425 "</input>"
3426 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003427 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_SUCCESS);
David Sedlák05404f62019-07-24 14:11:53 +02003428 assert_null(inout.parent);
3429 assert_int_equal(inout.nodetype, LYS_INPUT);
Michal Vasko7f45cf22020-10-01 12:49:44 +02003430 assert_string_equal(inout.musts->arg.str, "cond");
David Sedlák05404f62019-07-24 14:11:53 +02003431 assert_string_equal(inout.typedefs->name, "tpdf");
3432 assert_string_equal(inout.groupings->name, "sub-grp");
3433 assert_string_equal(inout.data->name, "anyd");
3434 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
3435 assert_string_equal(inout.data->next->name, "anyx");
3436 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
3437 assert_string_equal(inout.data->next->next->name, "choice");
3438 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
3439 assert_string_equal(inout.data->next->next->next->name, "subcont");
3440 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
3441 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
3442 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
3443 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
3444 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3445 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
3446 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
3447 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
3448 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3449 assert_null(inout.data->next->next->next->next->next->next->next->next);
David Sedláke0ef1c62019-09-13 10:05:55 +02003450 assert_string_equal(inout.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003451 assert_int_equal(inout.exts[0].insubstmt_index, 0);
3452 assert_int_equal(inout.exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák05404f62019-07-24 14:11:53 +02003453 lysp_action_inout_free(st->ctx, &inout);
3454 memset(&inout, 0, sizeof inout);
3455
3456 /* max subelements */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003457 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák05404f62019-07-24 14:11:53 +02003458 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003459 "<output>\n"
3460 " <anydata name=\"anyd\"/>\n"
3461 " <anyxml name=\"anyx\"/>\n"
3462 " <choice name=\"choice\"/>\n"
3463 " <container name=\"subcont\"/>\n"
3464 " <grouping name=\"sub-grp\"/>\n"
3465 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3466 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3467 " <list name=\"list\"/>\n"
3468 " <must condition=\"cond\"/>\n"
3469 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3470 " <uses name=\"uses-name\"/>\n"
3471 EXT_SUBELEM
3472 "</output>"
3473 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003474 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_SUCCESS);
David Sedlák05404f62019-07-24 14:11:53 +02003475 assert_null(inout.parent);
3476 assert_int_equal(inout.nodetype, LYS_OUTPUT);
Michal Vasko7f45cf22020-10-01 12:49:44 +02003477 assert_string_equal(inout.musts->arg.str, "cond");
David Sedlák05404f62019-07-24 14:11:53 +02003478 assert_string_equal(inout.typedefs->name, "tpdf");
3479 assert_string_equal(inout.groupings->name, "sub-grp");
3480 assert_string_equal(inout.data->name, "anyd");
3481 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
3482 assert_string_equal(inout.data->next->name, "anyx");
3483 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
3484 assert_string_equal(inout.data->next->next->name, "choice");
3485 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
3486 assert_string_equal(inout.data->next->next->next->name, "subcont");
3487 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
3488 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
3489 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
3490 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
3491 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3492 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
3493 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
3494 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
3495 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3496 assert_null(inout.data->next->next->next->next->next->next->next->next);
David Sedláke0ef1c62019-09-13 10:05:55 +02003497 assert_string_equal(inout.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003498 assert_int_equal(inout.exts[0].insubstmt_index, 0);
3499 assert_int_equal(inout.exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák05404f62019-07-24 14:11:53 +02003500 lysp_action_inout_free(st->ctx, &inout);
3501 memset(&inout, 0, sizeof inout);
3502
3503 /* min subelems */
Michal Vaskob83af8a2020-01-06 09:49:22 +01003504 data = ELEMENT_WRAPPER_START "<input><leaf name=\"l\"><type name=\"empty\"/></leaf></input>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003505 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_SUCCESS);
Michal Vaskob83af8a2020-01-06 09:49:22 +01003506 lysp_action_inout_free(st->ctx, &inout);
David Sedlák05404f62019-07-24 14:11:53 +02003507 memset(&inout, 0, sizeof inout);
3508
Michal Vaskob83af8a2020-01-06 09:49:22 +01003509 data = ELEMENT_WRAPPER_START "<output><leaf name=\"l\"><type name=\"empty\"/></leaf></output>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003510 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_SUCCESS);
Michal Vaskob83af8a2020-01-06 09:49:22 +01003511 lysp_action_inout_free(st->ctx, &inout);
David Sedlák05404f62019-07-24 14:11:53 +02003512 memset(&inout, 0, sizeof inout);
3513
3514 /* invalid combinations */
3515 data = ELEMENT_WRAPPER_START "<input name=\"test\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003516 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02003517 logbuf_assert("Unexpected attribute \"name\" of \"input\" element. Line number 1.");
David Sedlák05404f62019-07-24 14:11:53 +02003518 memset(&inout, 0, sizeof inout);
3519
3520 st->finished_correctly = true;
3521}
3522
David Sedlák85d0eca2019-07-24 15:15:21 +02003523static void
3524test_action_elem(void **state)
3525{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003526 struct test_parser_yin_state *st = *state;
David Sedlák85d0eca2019-07-24 15:15:21 +02003527 const char *data;
3528 struct lysp_action *actions = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003529 struct tree_node_meta act_meta = {NULL, (struct lysp_node **)&actions};
David Sedlák85d0eca2019-07-24 15:15:21 +02003530
3531 /* max subelems */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003532 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák85d0eca2019-07-24 15:15:21 +02003533 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003534 "<action name=\"act\">\n"
3535 " <description><text>desc</text></description>\n"
3536 " <grouping name=\"grouping\"/>\n"
3537 " <if-feature name=\"iff\"/>\n"
3538 " <input><uses name=\"uses-name\"/></input>\n"
3539 " <output><must condition=\"cond\"/><leaf name=\"l\"><type name=\"type\"/></leaf></output>\n"
3540 " <reference><text>ref</text></reference>\n"
3541 " <status value=\"deprecated\"/>\n"
3542 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3543 EXT_SUBELEM
3544 "</action>"
3545 ELEMENT_WRAPPER_END;
Michal Vasko1bf09392020-03-27 12:38:10 +01003546 /* there must be parent for action */
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003547 act_meta.parent = (void *)1;
Michal Vaskob36053d2020-03-26 15:49:30 +01003548 assert_int_equal(test_element_helper(st, data, &act_meta, NULL, NULL), LY_SUCCESS);
Michal Vasko1bf09392020-03-27 12:38:10 +01003549 act_meta.parent = NULL;
3550 assert_non_null(actions->parent);
David Sedlák85d0eca2019-07-24 15:15:21 +02003551 assert_int_equal(actions->nodetype, LYS_ACTION);
3552 assert_true(actions->flags & LYS_STATUS_DEPRC);
3553 assert_string_equal(actions->name, "act");
3554 assert_string_equal(actions->dsc, "desc");
3555 assert_string_equal(actions->ref, "ref");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003556 assert_string_equal(actions->iffeatures[0].str, "iff");
David Sedlák85d0eca2019-07-24 15:15:21 +02003557 assert_string_equal(actions->typedefs->name, "tpdf");
3558 assert_string_equal(actions->groupings->name, "grouping");
3559 assert_string_equal(actions->input.data->name, "uses-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003560 assert_string_equal(actions->output.musts->arg.str, "cond");
David Sedláke0ef1c62019-09-13 10:05:55 +02003561 assert_string_equal(actions->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003562 assert_int_equal(actions->exts[0].insubstmt_index, 0);
3563 assert_int_equal(actions->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák85d0eca2019-07-24 15:15:21 +02003564 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3565 actions = NULL;
3566
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003567 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlákeaa45792019-07-24 15:25:01 +02003568 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003569 "<rpc name=\"act\">\n"
3570 " <description><text>desc</text></description>\n"
3571 " <grouping name=\"grouping\"/>\n"
3572 " <if-feature name=\"iff\"/>\n"
3573 " <input><uses name=\"uses-name\"/></input>\n"
3574 " <output><must condition=\"cond\"/><leaf name=\"l\"><type name=\"type\"/></leaf></output>\n"
3575 " <reference><text>ref</text></reference>\n"
3576 " <status value=\"deprecated\"/>\n"
3577 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3578 EXT_SUBELEM
3579 "</rpc>"
3580 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003581 assert_int_equal(test_element_helper(st, data, &act_meta, NULL, NULL), LY_SUCCESS);
David Sedlákeaa45792019-07-24 15:25:01 +02003582 assert_null(actions->parent);
Michal Vasko1bf09392020-03-27 12:38:10 +01003583 assert_int_equal(actions->nodetype, LYS_RPC);
David Sedlákeaa45792019-07-24 15:25:01 +02003584 assert_true(actions->flags & LYS_STATUS_DEPRC);
3585 assert_string_equal(actions->name, "act");
3586 assert_string_equal(actions->dsc, "desc");
3587 assert_string_equal(actions->ref, "ref");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003588 assert_string_equal(actions->iffeatures[0].str, "iff");
David Sedlákeaa45792019-07-24 15:25:01 +02003589 assert_string_equal(actions->typedefs->name, "tpdf");
3590 assert_string_equal(actions->groupings->name, "grouping");
3591 assert_string_equal(actions->input.data->name, "uses-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003592 assert_string_equal(actions->output.musts->arg.str, "cond");
David Sedláke0ef1c62019-09-13 10:05:55 +02003593 assert_string_equal(actions->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003594 assert_int_equal(actions->exts[0].insubstmt_index, 0);
3595 assert_int_equal(actions->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákeaa45792019-07-24 15:25:01 +02003596 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3597 actions = NULL;
3598
David Sedlák85d0eca2019-07-24 15:15:21 +02003599 /* min subelems */
3600 data = ELEMENT_WRAPPER_START "<action name=\"act\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003601 assert_int_equal(test_element_helper(st, data, &act_meta, NULL, NULL), LY_SUCCESS);
David Sedlák85d0eca2019-07-24 15:15:21 +02003602 assert_string_equal(actions->name, "act");
3603 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3604 actions = NULL;
3605
3606 st->finished_correctly = true;
3607}
3608
David Sedlák992fb7c2019-07-24 16:51:01 +02003609static void
3610test_augment_elem(void **state)
3611{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003612 struct test_parser_yin_state *st = *state;
David Sedlák992fb7c2019-07-24 16:51:01 +02003613 const char *data;
3614 struct lysp_augment *augments = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003615 struct tree_node_meta aug_meta = {NULL, (struct lysp_node **)&augments};
David Sedlák992fb7c2019-07-24 16:51:01 +02003616
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003617 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák992fb7c2019-07-24 16:51:01 +02003618 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003619 "<augment target-node=\"target\">\n"
3620 " <action name=\"action\"/>\n"
3621 " <anydata name=\"anyd\"/>\n"
3622 " <anyxml name=\"anyx\"/>\n"
3623 " <case name=\"case\"/>\n"
3624 " <choice name=\"choice\"/>\n"
3625 " <container name=\"subcont\"/>\n"
3626 " <description><text>desc</text></description>\n"
3627 " <if-feature name=\"iff\"/>\n"
3628 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3629 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3630 " <list name=\"list\"/>\n"
3631 " <notification name=\"notif\"/>\n"
3632 " <reference><text>ref</text></reference>\n"
3633 " <status value=\"current\"/>\n"
3634 " <uses name=\"uses\"/>\n"
3635 " <when condition=\"when-cond\"/>\n"
3636 EXT_SUBELEM
3637 "</augment>"
3638 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003639 assert_int_equal(test_element_helper(st, data, &aug_meta, NULL, NULL), LY_SUCCESS);
David Sedlák992fb7c2019-07-24 16:51:01 +02003640 assert_string_equal(augments->nodeid, "target");
3641 assert_null(augments->parent);
3642 assert_int_equal(augments->nodetype, LYS_AUGMENT);
3643 assert_true(augments->flags & LYS_STATUS_CURR);
3644 assert_string_equal(augments->dsc, "desc");
3645 assert_string_equal(augments->ref, "ref");
3646 assert_string_equal(augments->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003647 assert_string_equal(augments->iffeatures[0].str, "iff");
David Sedlák992fb7c2019-07-24 16:51:01 +02003648 assert_string_equal(augments->child->name, "anyd");
3649 assert_int_equal(augments->child->nodetype, LYS_ANYDATA);
3650 assert_string_equal(augments->child->next->name, "anyx");
3651 assert_int_equal(augments->child->next->nodetype, LYS_ANYXML);
3652 assert_string_equal(augments->child->next->next->name, "case");
3653 assert_int_equal(augments->child->next->next->nodetype, LYS_CASE);
3654 assert_string_equal(augments->child->next->next->next->name, "choice");
3655 assert_int_equal(augments->child->next->next->next->nodetype, LYS_CHOICE);
3656 assert_string_equal(augments->child->next->next->next->next->name, "subcont");
3657 assert_int_equal(augments->child->next->next->next->next->nodetype, LYS_CONTAINER);
3658 assert_string_equal(augments->child->next->next->next->next->next->name, "leaf");
3659 assert_int_equal(augments->child->next->next->next->next->next->nodetype, LYS_LEAF);
3660 assert_string_equal(augments->child->next->next->next->next->next->next->name, "llist");
3661 assert_int_equal(augments->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3662 assert_string_equal(augments->child->next->next->next->next->next->next->next->name, "list");
3663 assert_int_equal(augments->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
3664 assert_string_equal(augments->child->next->next->next->next->next->next->next->next->name, "uses");
3665 assert_int_equal(augments->child->next->next->next->next->next->next->next->next->nodetype, LYS_USES);
3666 assert_null(augments->child->next->next->next->next->next->next->next->next->next);
3667 assert_string_equal(augments->actions->name, "action");
3668 assert_string_equal(augments->notifs->name, "notif");
David Sedláke0ef1c62019-09-13 10:05:55 +02003669 assert_string_equal(augments->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003670 assert_int_equal(augments->exts[0].insubstmt_index, 0);
3671 assert_int_equal(augments->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák992fb7c2019-07-24 16:51:01 +02003672 FREE_ARRAY(st->ctx, augments, lysp_augment_free)
3673 augments = NULL;
3674
3675 data = ELEMENT_WRAPPER_START "<augment target-node=\"target\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003676 assert_int_equal(test_element_helper(st, data, &aug_meta, NULL, NULL), LY_SUCCESS);
David Sedlák992fb7c2019-07-24 16:51:01 +02003677 assert_string_equal(augments->nodeid, "target");
3678 FREE_ARRAY(st->ctx, augments, lysp_augment_free)
3679 augments = NULL;
3680
3681 st->finished_correctly = true;
3682}
3683
David Sedlák4ffcec82019-07-25 15:10:21 +02003684static void
3685test_deviate_elem(void **state)
3686{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003687 struct test_parser_yin_state *st = *state;
David Sedlák4ffcec82019-07-25 15:10:21 +02003688 const char *data;
3689 struct lysp_deviate *deviates = NULL;
3690 struct lysp_deviate_add *d_add;
3691 struct lysp_deviate_rpl *d_rpl;
3692 struct lysp_deviate_del *d_del;
3693
3694 /* all valid arguments with min subelems */
3695 data = ELEMENT_WRAPPER_START "<deviate value=\"not-supported\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003696 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003697 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
3698 lysp_deviate_free(st->ctx, deviates);
3699 free(deviates);
3700 deviates = NULL;
3701
3702 data = ELEMENT_WRAPPER_START "<deviate value=\"add\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003703 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003704 assert_int_equal(deviates->mod, LYS_DEV_ADD);
3705 lysp_deviate_free(st->ctx, deviates);
3706 free(deviates);
3707 deviates = NULL;
3708
3709 data = ELEMENT_WRAPPER_START "<deviate value=\"replace\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003710 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003711 assert_int_equal(deviates->mod, LYS_DEV_REPLACE);
3712 lysp_deviate_free(st->ctx, deviates);
3713 free(deviates);
3714 deviates = NULL;
3715
3716 data = ELEMENT_WRAPPER_START "<deviate value=\"delete\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003717 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003718 assert_int_equal(deviates->mod, LYS_DEV_DELETE);
3719 lysp_deviate_free(st->ctx, deviates);
3720 free(deviates);
3721 deviates = NULL;
3722
3723 /* max subelems and valid arguments */
3724 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003725 "<deviate value=\"not-supported\">"
3726 EXT_SUBELEM
3727 "</deviate>"
3728 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003729 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003730 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
David Sedláke0ef1c62019-09-13 10:05:55 +02003731 assert_string_equal(deviates->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003732 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3733 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003734 lysp_deviate_free(st->ctx, deviates);
3735 free(deviates);
3736 deviates = NULL;
3737
3738 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003739 "<deviate value=\"add\">\n"
3740 " <units name=\"units\"/>\n"
3741 " <must condition=\"cond\"/>\n"
3742 " <unique tag=\"utag\"/>\n"
3743 " <default value=\"def\"/>\n"
3744 " <config value=\"true\"/>\n"
3745 " <mandatory value=\"true\"/>\n"
3746 " <min-elements value=\"5\"/>\n"
3747 " <max-elements value=\"15\"/>\n"
3748 EXT_SUBELEM
3749 "</deviate>"
3750 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003751 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003752 d_add = (struct lysp_deviate_add *)deviates;
3753 assert_int_equal(d_add->mod, LYS_DEV_ADD);
3754 assert_null(d_add->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003755 assert_string_equal(d_add->units, "units");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003756 assert_string_equal(d_add->musts->arg.str, "cond");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003757 assert_string_equal(d_add->uniques[0].str, "utag");
3758 assert_string_equal(d_add->dflts[0].str, "def");
David Sedlák4ffcec82019-07-25 15:10:21 +02003759 assert_true(d_add->flags & LYS_MAND_TRUE && d_add->flags & LYS_CONFIG_W);
3760 assert_int_equal(d_add->min, 5);
3761 assert_int_equal(d_add->max, 15);
David Sedláke0ef1c62019-09-13 10:05:55 +02003762 assert_string_equal(deviates->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003763 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3764 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003765 lysp_deviate_free(st->ctx, deviates);
3766 free(deviates);
3767 deviates = NULL;
3768
3769 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003770 "<deviate value=\"replace\">\n"
3771 " <type name=\"newtype\"/>\n"
3772 " <units name=\"uni\"/>\n"
3773 " <default value=\"def\"/>\n"
3774 " <config value=\"true\"/>\n"
3775 " <mandatory value=\"true\"/>\n"
3776 " <min-elements value=\"5\"/>\n"
3777 " <max-elements value=\"15\"/>\n"
3778 EXT_SUBELEM
3779 "</deviate>"
3780 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003781 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003782 d_rpl = (struct lysp_deviate_rpl *)deviates;
3783 assert_int_equal(d_rpl->mod, LYS_DEV_REPLACE);
3784 assert_null(d_rpl->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003785 assert_string_equal(d_rpl->type->name, "newtype");
3786 assert_string_equal(d_rpl->units, "uni");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003787 assert_string_equal(d_rpl->dflt.str, "def");
David Sedlák4ffcec82019-07-25 15:10:21 +02003788 assert_true(d_rpl->flags & LYS_MAND_TRUE && d_rpl->flags & LYS_CONFIG_W);
3789 assert_int_equal(d_rpl->min, 5);
3790 assert_int_equal(d_rpl->max, 15);
David Sedláke0ef1c62019-09-13 10:05:55 +02003791 assert_string_equal(deviates->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003792 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3793 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003794 lysp_deviate_free(st->ctx, deviates);
3795 free(deviates);
3796 deviates = NULL;
3797
3798 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003799 "<deviate value=\"delete\">\n"
3800 " <units name=\"u\"/>\n"
3801 " <must condition=\"c\"/>\n"
3802 " <unique tag=\"tag\"/>\n"
3803 " <default value=\"default\"/>\n"
3804 EXT_SUBELEM
3805 "</deviate>"
3806 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003807 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003808 d_del = (struct lysp_deviate_del *)deviates;
3809 assert_int_equal(d_del->mod, LYS_DEV_DELETE);
3810 assert_null(d_del->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003811 assert_string_equal(d_del->units, "u");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003812 assert_string_equal(d_del->musts->arg.str, "c");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003813 assert_string_equal(d_del->uniques[0].str, "tag");
3814 assert_string_equal(d_del->dflts[0].str, "default");
David Sedláke0ef1c62019-09-13 10:05:55 +02003815 assert_string_equal(deviates->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003816 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3817 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003818 lysp_deviate_free(st->ctx, deviates);
3819 free(deviates);
3820 deviates = NULL;
3821
3822 /* invalid arguments */
3823 data = ELEMENT_WRAPPER_START "<deviate value=\"\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003824 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003825 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 +02003826 deviates = NULL;
3827
3828 data = ELEMENT_WRAPPER_START "<deviate value=\"invalid\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003829 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003830 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 +02003831 deviates = NULL;
3832
3833 data = ELEMENT_WRAPPER_START "<deviate value=\"ad\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003834 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003835 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 +02003836 deviates = NULL;
3837
3838 data = ELEMENT_WRAPPER_START "<deviate value=\"adds\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003839 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003840 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 +02003841 deviates = NULL;
3842
3843 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003844 "<deviate value=\"not-supported\">\n"
3845 " <must condition=\"c\"/>\n"
3846 "</deviate>"
3847 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003848 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_EVALID);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003849 logbuf_assert("Deviate of this type doesn't allow \"must\" as it's sub-element. Line number 2.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003850
3851 st->finished_correctly = true;
3852}
3853
David Sedlák8b754462019-07-25 16:22:13 +02003854static void
3855test_deviation_elem(void **state)
3856{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003857 struct test_parser_yin_state *st = *state;
David Sedlák8b754462019-07-25 16:22:13 +02003858 const char *data;
3859 struct lysp_deviation *deviations = NULL;
3860
3861 /* min subelems */
3862 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003863 "<deviation target-node=\"target\">\n"
3864 " <deviate value=\"not-supported\"/>\n"
3865 "</deviation>"
3866 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003867 assert_int_equal(test_element_helper(st, data, &deviations, NULL, NULL), LY_SUCCESS);
David Sedlák8b754462019-07-25 16:22:13 +02003868 assert_string_equal(deviations->nodeid, "target");
3869 assert_int_equal(deviations->deviates->mod, LYS_DEV_NOT_SUPPORTED);
3870 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3871 deviations = NULL;
3872
3873 /* max subelems */
3874 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003875 "<deviation target-node=\"target\">\n"
3876 " <reference><text>ref</text></reference>\n"
3877 " <description><text>desc</text></description>\n"
3878 " <deviate value=\"add\"/>\n"
3879 EXT_SUBELEM
3880 "</deviation>"
3881 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003882 assert_int_equal(test_element_helper(st, data, &deviations, NULL, NULL), LY_SUCCESS);
David Sedlák8b754462019-07-25 16:22:13 +02003883 assert_string_equal(deviations->nodeid, "target");
3884 assert_int_equal(deviations->deviates->mod, LYS_DEV_ADD);
3885 assert_string_equal(deviations->ref, "ref");
3886 assert_string_equal(deviations->dsc, "desc");
David Sedláke0ef1c62019-09-13 10:05:55 +02003887 assert_string_equal(deviations->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003888 assert_int_equal(deviations->exts[0].insubstmt_index, 0);
3889 assert_int_equal(deviations->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8b754462019-07-25 16:22:13 +02003890 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3891 deviations = NULL;
3892
3893 /* invalid */
3894 data = ELEMENT_WRAPPER_START "<deviation target-node=\"target\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003895 assert_int_equal(test_element_helper(st, data, &deviations, NULL, NULL), LY_EVALID);
David Sedlák8b754462019-07-25 16:22:13 +02003896 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3897 deviations = NULL;
David Sedlák1538a842019-08-08 15:38:51 +02003898 logbuf_assert("Missing mandatory sub-element \"deviate\" of \"deviation\" element. Line number 1.");
3899 /* TODO */
David Sedlák8b754462019-07-25 16:22:13 +02003900 st->finished_correctly = true;
3901}
3902
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003903static struct lysp_module *
3904mod_renew(struct lys_yin_parser_ctx *ctx)
3905{
3906 struct ly_ctx *ly_ctx = ctx->parsed_mod->mod->ctx;
3907
3908 lys_module_free(ctx->parsed_mod->mod, NULL);
3909 ctx->parsed_mod = calloc(1, sizeof *ctx->parsed_mod);
3910 ctx->parsed_mod->mod = calloc(1, sizeof *ctx->parsed_mod->mod);
3911 ctx->parsed_mod->mod->parsed = ctx->parsed_mod;
3912 ctx->parsed_mod->mod->ctx = ly_ctx;
3913
3914 return ctx->parsed_mod;
3915}
3916
David Sedlák4f03b932019-07-26 13:01:47 +02003917static void
3918test_module_elem(void **state)
3919{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003920 struct test_parser_yin_state *st = *state;
Michal Vaskob36053d2020-03-26 15:49:30 +01003921 const char *data;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003922 struct lysp_module *lysp_mod = mod_renew(st->yin_ctx);
David Sedlák4f03b932019-07-26 13:01:47 +02003923
3924 /* max subelems */
David Sedlák4f03b932019-07-26 13:01:47 +02003925 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003926 " <yang-version value=\"1.1\"/>\n"
3927 " <namespace uri=\"ns\"/>\n"
3928 " <prefix value=\"pref\"/>\n"
3929 " <include module=\"b-mod\"/>\n"
3930 " <import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
3931 " <organization><text>org</text></organization>\n"
3932 " <contact><text>contact</text></contact>\n"
3933 " <description><text>desc</text></description>\n"
3934 " <reference><text>ref</text></reference>\n"
3935 " <revision date=\"2019-02-02\"/>\n"
3936 " <anydata name=\"anyd\"/>\n"
3937 " <anyxml name=\"anyx\"/>\n"
3938 " <choice name=\"choice\"/>\n"
3939 " <container name=\"cont\"/>\n"
3940 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3941 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3942 " <list name=\"sub-list\"/>\n"
3943 " <uses name=\"uses-name\"/>\n"
3944 " <augment target-node=\"target\"/>\n"
3945 " <deviation target-node=\"target\">\n"
3946 " <deviate value=\"not-supported\"/>\n"
3947 " </deviation>\n"
3948 " <extension name=\"ext\"/>\n"
3949 " <feature name=\"feature\"/>\n"
3950 " <grouping name=\"grp\"/>\n"
3951 " <identity name=\"ident-name\"/>\n"
3952 " <notification name=\"notf\"/>\n"
3953 " <rpc name=\"rpc-name\"/>\n"
3954 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3955 EXT_SUBELEM "\n"
3956 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003957 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
3958 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01003959
3960 assert_int_equal(yin_parse_mod(st->yin_ctx, lysp_mod), LY_SUCCESS);
David Sedlák4f03b932019-07-26 13:01:47 +02003961 assert_string_equal(lysp_mod->mod->name, "mod");
3962 assert_string_equal(lysp_mod->revs, "2019-02-02");
3963 assert_string_equal(lysp_mod->mod->ns, "ns");
3964 assert_string_equal(lysp_mod->mod->prefix, "pref");
3965 assert_null(lysp_mod->mod->filepath);
3966 assert_string_equal(lysp_mod->mod->org, "org");
3967 assert_string_equal(lysp_mod->mod->contact, "contact");
3968 assert_string_equal(lysp_mod->mod->dsc, "desc");
3969 assert_string_equal(lysp_mod->mod->ref, "ref");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003970 assert_int_equal(lysp_mod->version, LYS_VERSION_1_1);
David Sedlák4f03b932019-07-26 13:01:47 +02003971 assert_string_equal(lysp_mod->imports->name, "a-mod");
3972 assert_string_equal(lysp_mod->includes->name, "b-mod");
3973 assert_string_equal(lysp_mod->extensions->name, "ext");
3974 assert_string_equal(lysp_mod->features->name, "feature");
3975 assert_string_equal(lysp_mod->identities->name, "ident-name");
3976 assert_string_equal(lysp_mod->typedefs->name, "tpdf");
3977 assert_string_equal(lysp_mod->groupings->name, "grp");
3978 assert_string_equal(lysp_mod->data->name, "anyd");
3979 assert_int_equal(lysp_mod->data->nodetype, LYS_ANYDATA);
3980 assert_string_equal(lysp_mod->data->next->name, "anyx");
3981 assert_int_equal(lysp_mod->data->next->nodetype, LYS_ANYXML);
3982 assert_string_equal(lysp_mod->data->next->next->name, "choice");
3983 assert_int_equal(lysp_mod->data->next->next->nodetype, LYS_CHOICE);
3984 assert_string_equal(lysp_mod->data->next->next->next->name, "cont");
3985 assert_int_equal(lysp_mod->data->next->next->next->nodetype, LYS_CONTAINER);
3986 assert_string_equal(lysp_mod->data->next->next->next->next->name, "leaf");
3987 assert_int_equal(lysp_mod->data->next->next->next->next->nodetype, LYS_LEAF);
3988 assert_string_equal(lysp_mod->data->next->next->next->next->next->name, "llist");
3989 assert_int_equal(lysp_mod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3990 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->name, "sub-list");
3991 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
3992 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->next->name, "uses-name");
3993 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3994 assert_null(lysp_mod->data->next->next->next->next->next->next->next->next);
3995 assert_string_equal(lysp_mod->augments->nodeid, "target");
3996 assert_string_equal(lysp_mod->rpcs->name, "rpc-name");
3997 assert_string_equal(lysp_mod->notifs->name, "notf");
3998 assert_string_equal(lysp_mod->deviations->nodeid, "target");
David Sedláke0ef1c62019-09-13 10:05:55 +02003999 assert_string_equal(lysp_mod->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02004000 assert_int_equal(lysp_mod->exts[0].insubstmt_index, 0);
4001 assert_int_equal(lysp_mod->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4f03b932019-07-26 13:01:47 +02004002
4003 /* min subelems */
Michal Vasko63f3d842020-07-08 10:10:14 +02004004 ly_in_free(st->in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +01004005 lyxml_ctx_free(st->yin_ctx->xmlctx);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004006 lysp_mod = mod_renew(st->yin_ctx);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01004007 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
4008 " <namespace uri=\"ns\"/>\n"
4009 " <prefix value=\"pref\"/>\n"
4010 " <yang-version value=\"1.1\"/>\n"
4011 "</module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004012 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
4013 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01004014 assert_int_equal(yin_parse_mod(st->yin_ctx, lysp_mod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02004015 assert_string_equal(lysp_mod->mod->name, "mod");
David Sedlák298ff6d2019-07-26 14:29:03 +02004016
David Sedláke6cd89e2019-08-07 12:46:02 +02004017 /* incorrect subelem order */
Michal Vasko63f3d842020-07-08 10:10:14 +02004018 ly_in_free(st->in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +01004019 lyxml_ctx_free(st->yin_ctx->xmlctx);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004020 lysp_mod = mod_renew(st->yin_ctx);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01004021 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
4022 " <feature name=\"feature\"/>\n"
4023 " <namespace uri=\"ns\"/>\n"
4024 " <prefix value=\"pref\"/>\n"
4025 " <yang-version value=\"1.1\"/>\n"
4026 "</module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004027 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
4028 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01004029 assert_int_equal(yin_parse_mod(st->yin_ctx, lysp_mod), LY_EVALID);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01004030 logbuf_assert("Invalid order of module\'s sub-elements \"namespace\" can\'t appear after \"feature\". Line number 3.");
David Sedláke6cd89e2019-08-07 12:46:02 +02004031
David Sedlák298ff6d2019-07-26 14:29:03 +02004032 st->finished_correctly = true;
4033}
4034
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004035static struct lysp_submodule *
4036submod_renew(struct lys_yin_parser_ctx *ctx, const char *belongs_to)
4037{
4038 struct ly_ctx *ly_ctx = ctx->parsed_mod->mod->ctx;
4039
4040 lys_module_free(ctx->parsed_mod->mod, NULL);
4041 ctx->parsed_mod = calloc(1, sizeof(struct lysp_submodule));
4042 ctx->parsed_mod->mod = calloc(1, sizeof *ctx->parsed_mod->mod);
4043 lydict_insert(ly_ctx, belongs_to, 0, &ctx->parsed_mod->mod->name);
4044 ctx->parsed_mod->mod->parsed = ctx->parsed_mod;
4045 ctx->parsed_mod->mod->ctx = ly_ctx;
4046
4047 return (struct lysp_submodule *)ctx->parsed_mod;
4048}
4049
David Sedlák298ff6d2019-07-26 14:29:03 +02004050static void
4051test_submodule_elem(void **state)
4052{
Radek Krejci3a4889a2020-05-19 17:01:58 +02004053 struct test_parser_yin_state *st = *state;
Michal Vaskob36053d2020-03-26 15:49:30 +01004054 const char *data;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004055 struct lysp_submodule *lysp_submod = submod_renew(st->yin_ctx, "module-name");
David Sedlák298ff6d2019-07-26 14:29:03 +02004056
4057 /* max subelements */
David Sedlák298ff6d2019-07-26 14:29:03 +02004058 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01004059 " <yang-version value=\"1.1\"/>\n"
4060 " <belongs-to module=\"module-name\">\n"
4061 " <prefix value=\"pref\"/>\n"
4062 " </belongs-to>\n"
4063 " <include module=\"b-mod\"/>\n"
4064 " <import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
4065 " <organization><text>org</text></organization>\n"
4066 " <contact><text>contact</text></contact>\n"
4067 " <description><text>desc</text></description>\n"
4068 " <reference><text>ref</text></reference>\n"
4069 " <revision date=\"2019-02-02\"/>\n"
4070 " <anydata name=\"anyd\"/>\n"
4071 " <anyxml name=\"anyx\"/>\n"
4072 " <choice name=\"choice\"/>\n"
4073 " <container name=\"cont\"/>\n"
4074 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
4075 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
4076 " <list name=\"sub-list\"/>\n"
4077 " <uses name=\"uses-name\"/>\n"
4078 " <augment target-node=\"target\"/>\n"
4079 " <deviation target-node=\"target\">\n"
4080 " <deviate value=\"not-supported\"/>\n"
4081 " </deviation>\n"
4082 " <extension name=\"ext\"/>\n"
4083 " <feature name=\"feature\"/>\n"
4084 " <grouping name=\"grp\"/>\n"
4085 " <identity name=\"ident-name\"/>\n"
4086 " <notification name=\"notf\"/>\n"
4087 " <rpc name=\"rpc-name\"/>\n"
4088 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
4089 EXT_SUBELEM "\n"
4090 "</submodule>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004091 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
4092 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02004093
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004094 assert_int_equal(yin_parse_submod(st->yin_ctx, lysp_submod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02004095 assert_string_equal(lysp_submod->name, "mod");
4096 assert_string_equal(lysp_submod->revs, "2019-02-02");
David Sedlák298ff6d2019-07-26 14:29:03 +02004097 assert_string_equal(lysp_submod->prefix, "pref");
4098 assert_null(lysp_submod->filepath);
4099 assert_string_equal(lysp_submod->org, "org");
4100 assert_string_equal(lysp_submod->contact, "contact");
4101 assert_string_equal(lysp_submod->dsc, "desc");
4102 assert_string_equal(lysp_submod->ref, "ref");
4103 assert_int_equal(lysp_submod->version, LYS_VERSION_1_1);
4104 assert_string_equal(lysp_submod->imports->name, "a-mod");
4105 assert_string_equal(lysp_submod->includes->name, "b-mod");
4106 assert_string_equal(lysp_submod->extensions->name, "ext");
4107 assert_string_equal(lysp_submod->features->name, "feature");
4108 assert_string_equal(lysp_submod->identities->name, "ident-name");
4109 assert_string_equal(lysp_submod->typedefs->name, "tpdf");
4110 assert_string_equal(lysp_submod->groupings->name, "grp");
4111 assert_string_equal(lysp_submod->data->name, "anyd");
4112 assert_int_equal(lysp_submod->data->nodetype, LYS_ANYDATA);
4113 assert_string_equal(lysp_submod->data->next->name, "anyx");
4114 assert_int_equal(lysp_submod->data->next->nodetype, LYS_ANYXML);
4115 assert_string_equal(lysp_submod->data->next->next->name, "choice");
4116 assert_int_equal(lysp_submod->data->next->next->nodetype, LYS_CHOICE);
4117 assert_string_equal(lysp_submod->data->next->next->next->name, "cont");
4118 assert_int_equal(lysp_submod->data->next->next->next->nodetype, LYS_CONTAINER);
4119 assert_string_equal(lysp_submod->data->next->next->next->next->name, "leaf");
4120 assert_int_equal(lysp_submod->data->next->next->next->next->nodetype, LYS_LEAF);
4121 assert_string_equal(lysp_submod->data->next->next->next->next->next->name, "llist");
4122 assert_int_equal(lysp_submod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
4123 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->name, "sub-list");
4124 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
4125 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->next->name, "uses-name");
4126 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
4127 assert_null(lysp_submod->data->next->next->next->next->next->next->next->next);
4128 assert_string_equal(lysp_submod->augments->nodeid, "target");
4129 assert_string_equal(lysp_submod->rpcs->name, "rpc-name");
4130 assert_string_equal(lysp_submod->notifs->name, "notf");
4131 assert_string_equal(lysp_submod->deviations->nodeid, "target");
David Sedláke0ef1c62019-09-13 10:05:55 +02004132 assert_string_equal(lysp_submod->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02004133 assert_int_equal(lysp_submod->exts[0].insubstmt_index, 0);
4134 assert_int_equal(lysp_submod->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák298ff6d2019-07-26 14:29:03 +02004135
David Sedlák298ff6d2019-07-26 14:29:03 +02004136 /* min subelemnts */
Michal Vasko63f3d842020-07-08 10:10:14 +02004137 ly_in_free(st->in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +01004138 lyxml_ctx_free(st->yin_ctx->xmlctx);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004139 lysp_submod = submod_renew(st->yin_ctx, "module-name");
Radek Krejcib4ac5a92020-11-23 17:54:33 +01004140 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">\n"
4141 " <yang-version value=\"1\"/>\n"
4142 " <belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>\n"
4143 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004144 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
4145 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01004146 assert_int_equal(yin_parse_submod(st->yin_ctx, lysp_submod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02004147 assert_string_equal(lysp_submod->prefix, "pref");
David Sedlák298ff6d2019-07-26 14:29:03 +02004148 assert_int_equal(lysp_submod->version, LYS_VERSION_1_0);
David Sedlák298ff6d2019-07-26 14:29:03 +02004149
David Sedláke6cd89e2019-08-07 12:46:02 +02004150 /* incorrect subelem order */
Michal Vasko63f3d842020-07-08 10:10:14 +02004151 ly_in_free(st->in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +01004152 lyxml_ctx_free(st->yin_ctx->xmlctx);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004153 lysp_submod = submod_renew(st->yin_ctx, "module-name");
Radek Krejcib4ac5a92020-11-23 17:54:33 +01004154 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">\n"
4155 " <yang-version value=\"1\"/>\n"
4156 " <reference><text>ref</text></reference>\n"
4157 " <belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>\n"
4158 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004159 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
4160 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01004161 assert_int_equal(yin_parse_submod(st->yin_ctx, lysp_submod), LY_EVALID);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01004162 logbuf_assert("Invalid order of submodule's sub-elements \"belongs-to\" can't appear after \"reference\". Line number 4.");
David Sedláke6cd89e2019-08-07 12:46:02 +02004163
David Sedlák298ff6d2019-07-26 14:29:03 +02004164 st->finished_correctly = true;
David Sedlák4f03b932019-07-26 13:01:47 +02004165}
4166
David Sedlák8985a142019-07-31 16:43:06 +02004167static void
4168test_yin_parse_module(void **state)
4169{
Radek Krejci3a4889a2020-05-19 17:01:58 +02004170 struct test_parser_yin_state *st = *state;
David Sedlák8985a142019-07-31 16:43:06 +02004171 const char *data;
4172 struct lys_module *mod;
Michal Vaskob36053d2020-03-26 15:49:30 +01004173 struct lys_yin_parser_ctx *yin_ctx = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02004174 struct ly_in *in = NULL;
Michal Vasko405cc9e2020-12-01 12:01:27 +01004175 struct lys_glob_unres unres = {0};
David Sedlák8985a142019-07-31 16:43:06 +02004176
4177 mod = calloc(1, sizeof *mod);
4178 mod->ctx = st->ctx;
David Sedlákd2844882019-09-13 16:01:22 +02004179 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"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01004180 " <yang-version value=\"1.1\"/>\n"
4181 " <namespace uri=\"urn:tests:extensions:metadata:a\"/>\n"
4182 " <prefix value=\"a\"/>\n"
4183 " <import module=\"ietf-yang-metadata\">\n"
4184 " <prefix value=\"md\"/>\n"
4185 " </import>\n"
4186 " <feature name=\"f\"/>\n"
4187 " <md:annotation name=\"x\">\n"
4188 " <description>\n"
4189 " <text>test</text>\n"
4190 " </description>\n"
4191 " <reference>\n"
4192 " <text>test</text>\n"
4193 " </reference>\n"
4194 " <if-feature name=\"f\"/>\n"
4195 " <status value=\"current\"/>\n"
4196 " <type name=\"uint8\"/>\n"
4197 " <units name=\"meters\"/>\n"
4198 " </md:annotation>\n"
4199 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004200 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko405cc9e2020-12-01 12:01:27 +01004201 assert_int_equal(yin_parse_module(&yin_ctx, in, mod, &unres), LY_SUCCESS);
David Sedlákd2844882019-09-13 16:01:22 +02004202 assert_null(mod->parsed->exts->child->next->child);
4203 assert_string_equal(mod->parsed->exts->child->next->arg, "test");
Michal Vasko405cc9e2020-12-01 12:01:27 +01004204 lys_compile_unres_glob_erase(st->ctx, &unres);
David Sedlákd2844882019-09-13 16:01:22 +02004205 lys_module_free(mod, NULL);
4206 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004207 ly_in_free(in, 0);
David Sedlákd2844882019-09-13 16:01:22 +02004208 mod = NULL;
4209 yin_ctx = NULL;
4210
4211 mod = calloc(1, sizeof *mod);
4212 mod->ctx = st->ctx;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01004213 data = "<module name=\"example-foo\""
4214 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
4215 " xmlns:foo=\"urn:example:foo\""
4216 " xmlns:myext=\"urn:example:extensions\">\n"
David Sedlák8985a142019-07-31 16:43:06 +02004217
Radek Krejcib4ac5a92020-11-23 17:54:33 +01004218 " <yang-version value=\"1\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02004219
Radek Krejcib4ac5a92020-11-23 17:54:33 +01004220 " <namespace uri=\"urn:example:foo\"/>\n"
4221 " <prefix value=\"foo\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02004222
Radek Krejcib4ac5a92020-11-23 17:54:33 +01004223 " <import module=\"example-extensions\">\n"
4224 " <prefix value=\"myext\"/>\n"
4225 " </import>\n"
David Sedlák8985a142019-07-31 16:43:06 +02004226
Radek Krejcib4ac5a92020-11-23 17:54:33 +01004227 " <list name=\"interface\">\n"
4228 " <key value=\"name\"/>\n"
4229 " <leaf name=\"name\">\n"
4230 " <type name=\"string\"/>\n"
4231 " </leaf>\n"
4232 " <leaf name=\"mtu\">\n"
4233 " <type name=\"uint32\"/>\n"
4234 " <description>\n"
4235 " <text>The MTU of the interface.</text>\n"
4236 " </description>\n"
4237 " <myext:c-define name=\"MY_MTU\"/>\n"
4238 " </leaf>\n"
4239 " </list>\n"
David Sedlák8985a142019-07-31 16:43:06 +02004240 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004241 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko405cc9e2020-12-01 12:01:27 +01004242 assert_int_equal(yin_parse_module(&yin_ctx, in, mod, &unres), LY_SUCCESS);
4243 lys_compile_unres_glob_erase(st->ctx, &unres);
David Sedlák8985a142019-07-31 16:43:06 +02004244 lys_module_free(mod, NULL);
4245 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004246 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02004247 mod = NULL;
4248 yin_ctx = NULL;
4249
4250 mod = calloc(1, sizeof *mod);
4251 mod->ctx = st->ctx;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01004252 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
4253 " <yang-version value=\"1\"/>\n"
4254 " <namespace uri=\"urn:example:foo\"/>\n"
4255 " <prefix value=\"foo\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004256 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004257 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko405cc9e2020-12-01 12:01:27 +01004258 assert_int_equal(yin_parse_module(&yin_ctx, in, mod, &unres), LY_SUCCESS);
4259 lys_compile_unres_glob_erase(st->ctx, &unres);
David Sedlák6d781b62019-08-02 15:22:52 +02004260 lys_module_free(mod, NULL);
4261 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004262 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02004263 mod = NULL;
4264 yin_ctx = NULL;
4265
David Sedlák6d781b62019-08-02 15:22:52 +02004266 mod = calloc(1, sizeof *mod);
4267 mod->ctx = st->ctx;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01004268 data = "<submodule name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlák8985a142019-07-31 16:43:06 +02004269 "</submodule>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004270 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko405cc9e2020-12-01 12:01:27 +01004271 assert_int_equal(yin_parse_module(&yin_ctx, in, mod, &unres), LY_EINVAL);
David Sedlák8985a142019-07-31 16:43:06 +02004272 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
4273 lys_module_free(mod, NULL);
4274 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004275 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02004276
David Sedlák6d781b62019-08-02 15:22:52 +02004277 mod = calloc(1, sizeof *mod);
4278 mod->ctx = st->ctx;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01004279 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
4280 " <yang-version value=\"1\"/>\n"
4281 " <namespace uri=\"urn:example:foo\"/>\n"
4282 " <prefix value=\"foo\"/>\n"
4283 "</module>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004284 "<module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004285 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko405cc9e2020-12-01 12:01:27 +01004286 assert_int_equal(yin_parse_module(&yin_ctx, in, mod, &unres), LY_EVALID);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01004287 logbuf_assert("Trailing garbage \"<module>\" after module, expected end-of-input. Line number 6.");
David Sedlák6d781b62019-08-02 15:22:52 +02004288 lys_module_free(mod, NULL);
4289 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004290 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02004291 mod = NULL;
4292 yin_ctx = NULL;
4293
David Sedlák8985a142019-07-31 16:43:06 +02004294 st->finished_correctly = true;
4295}
4296
4297static void
4298test_yin_parse_submodule(void **state)
4299{
Radek Krejci3a4889a2020-05-19 17:01:58 +02004300 struct test_parser_yin_state *st = *state;
David Sedlák8985a142019-07-31 16:43:06 +02004301 const char *data;
Michal Vaskob36053d2020-03-26 15:49:30 +01004302 struct lys_yin_parser_ctx *yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02004303 struct lysp_submodule *submod = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02004304 struct ly_in *in;
David Sedlák8985a142019-07-31 16:43:06 +02004305
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004306 lydict_insert(st->ctx, "a", 0, &st->yin_ctx->parsed_mod->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004307
Radek Krejcib4ac5a92020-11-23 17:54:33 +01004308 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
David Sedlák8985a142019-07-31 16:43:06 +02004309 "<submodule name=\"asub\""
Radek Krejcib4ac5a92020-11-23 17:54:33 +01004310 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
4311 " xmlns:a=\"urn:a\">\n"
4312 " <yang-version value=\"1\"/>\n"
4313 " <belongs-to module=\"a\">\n"
4314 " <prefix value=\"a_pref\"/>\n"
4315 " </belongs-to>\n"
4316 " <include module=\"atop\"/>\n"
4317 " <feature name=\"fox\"/>\n"
4318 " <notification name=\"bar-notif\">\n"
4319 " <if-feature name=\"bar\"/>\n"
4320 " </notification>\n"
4321 " <notification name=\"fox-notif\">\n"
4322 " <if-feature name=\"fox\"/>\n"
4323 " </notification>\n"
4324 " <augment target-node=\"/a_pref:top\">\n"
4325 " <if-feature name=\"bar\"/>\n"
4326 " <container name=\"bar-sub\"/>\n"
4327 " </augment>\n"
4328 " <augment target-node=\"/top\">\n"
4329 " <container name=\"bar-sub2\"/>\n"
4330 " </augment>\n"
David Sedlák8985a142019-07-31 16:43:06 +02004331 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004332 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004333 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, (struct lys_parser_ctx *)st->yin_ctx, in, &submod), LY_SUCCESS);
4334 lysp_module_free((struct lysp_module *)submod);
David Sedlák8985a142019-07-31 16:43:06 +02004335 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004336 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02004337 yin_ctx = NULL;
4338 submod = NULL;
4339
Radek Krejcib4ac5a92020-11-23 17:54:33 +01004340 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
4341 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
4342 " <yang-version value=\"1\"/>\n"
4343 " <belongs-to module=\"a\">\n"
4344 " <prefix value=\"a_pref\"/>\n"
4345 " </belongs-to>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004346 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004347 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004348 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, (struct lys_parser_ctx *)st->yin_ctx, in, &submod), LY_SUCCESS);
4349 lysp_module_free((struct lysp_module *)submod);
David Sedlák6d781b62019-08-02 15:22:52 +02004350 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004351 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02004352 yin_ctx = NULL;
4353 submod = NULL;
4354
Radek Krejcib4ac5a92020-11-23 17:54:33 +01004355 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
David Sedlák8985a142019-07-31 16:43:06 +02004356 "<module name=\"inval\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
4357 "</module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004358 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004359 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 +02004360 logbuf_assert("Input data contains module in situation when a submodule is expected.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004361 lysp_module_free((struct lysp_module *)submod);
David Sedlák8985a142019-07-31 16:43:06 +02004362 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004363 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02004364 yin_ctx = NULL;
4365 submod = NULL;
4366
Radek Krejcib4ac5a92020-11-23 17:54:33 +01004367 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
4368 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
4369 " <yang-version value=\"1\"/>\n"
4370 " <belongs-to module=\"a\">\n"
4371 " <prefix value=\"a_pref\"/>\n"
4372 " </belongs-to>\n"
4373 "</submodule>\n"
4374 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
4375 " <yang-version value=\"1\"/>\n"
4376 " <belongs-to module=\"a\">\n"
4377 " <prefix value=\"a_pref\"/>\n"
4378 " </belongs-to>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004379 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004380 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004381 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, (struct lys_parser_ctx *)st->yin_ctx, in, &submod), LY_EVALID);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01004382 logbuf_assert("Trailing garbage \"<submodule name...\" after submodule, expected end-of-input. Line number 8.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004383 lysp_module_free((struct lysp_module *)submod);
David Sedlák6d781b62019-08-02 15:22:52 +02004384 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004385 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02004386 yin_ctx = NULL;
4387 submod = NULL;
4388
David Sedlák8985a142019-07-31 16:43:06 +02004389 st->finished_correctly = true;
4390}
4391
David Sedlák3b4db242018-10-19 16:11:01 +02004392int
4393main(void)
4394{
4395
4396 const struct CMUnitTest tests[] = {
David Sedlák8f7a1172019-06-20 14:42:18 +02004397 cmocka_unit_test_setup_teardown(test_yin_match_keyword, setup_f, teardown_f),
David Sedlákb1a78352019-06-28 16:16:29 +02004398 cmocka_unit_test_setup_teardown(test_yin_parse_element_generic, setup_f, teardown_f),
4399 cmocka_unit_test_setup_teardown(test_yin_parse_extension_instance, setup_f, teardown_f),
David Sedlák071f7662019-09-12 02:02:51 +02004400 cmocka_unit_test_setup_teardown(test_yin_parse_content, setup_f, teardown_f),
David Sedlák4a650532019-07-10 11:55:18 +02004401 cmocka_unit_test_setup_teardown(test_validate_value, setup_f, teardown_f),
David Sedlák32488102019-07-15 17:44:10 +02004402
David Sedlák8e7bda82019-07-16 17:57:50 +02004403 cmocka_unit_test(test_yin_match_argument_name),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004404 cmocka_unit_test_setup_teardown(test_enum_elem, setup_element_test, teardown_f),
4405 cmocka_unit_test_setup_teardown(test_bit_elem, setup_element_test, teardown_f),
4406 cmocka_unit_test_setup_teardown(test_meta_elem, setup_element_test, teardown_f),
4407 cmocka_unit_test_setup_teardown(test_import_elem, setup_element_test, teardown_f),
4408 cmocka_unit_test_setup_teardown(test_status_elem, setup_element_test, teardown_f),
4409 cmocka_unit_test_setup_teardown(test_ext_elem, setup_element_test, teardown_f),
4410 cmocka_unit_test_setup_teardown(test_yin_element_elem, setup_element_test, teardown_f),
4411 cmocka_unit_test_setup_teardown(test_yangversion_elem, setup_element_test, teardown_f),
4412 cmocka_unit_test_setup_teardown(test_mandatory_elem, setup_element_test, teardown_f),
4413 cmocka_unit_test_setup_teardown(test_argument_elem, setup_element_test, teardown_f),
4414 cmocka_unit_test_setup_teardown(test_base_elem, setup_element_test, teardown_f),
4415 cmocka_unit_test_setup_teardown(test_belongsto_elem, setup_element_test, teardown_f),
4416 cmocka_unit_test_setup_teardown(test_config_elem, setup_element_test, teardown_f),
4417 cmocka_unit_test_setup_teardown(test_default_elem, setup_element_test, teardown_f),
4418 cmocka_unit_test_setup_teardown(test_err_app_tag_elem, setup_element_test, teardown_f),
4419 cmocka_unit_test_setup_teardown(test_err_msg_elem, setup_element_test, teardown_f),
4420 cmocka_unit_test_setup_teardown(test_fracdigits_elem, setup_element_test, teardown_f),
4421 cmocka_unit_test_setup_teardown(test_iffeature_elem, setup_element_test, teardown_f),
4422 cmocka_unit_test_setup_teardown(test_length_elem, setup_element_test, teardown_f),
4423 cmocka_unit_test_setup_teardown(test_modifier_elem, setup_element_test, teardown_f),
4424 cmocka_unit_test_setup_teardown(test_namespace_elem, setup_element_test, teardown_f),
4425 cmocka_unit_test_setup_teardown(test_pattern_elem, setup_element_test, teardown_f),
4426 cmocka_unit_test_setup_teardown(test_value_position_elem, setup_element_test, teardown_f),
4427 cmocka_unit_test_setup_teardown(test_prefix_elem, setup_element_test, teardown_f),
4428 cmocka_unit_test_setup_teardown(test_range_elem, setup_element_test, teardown_f),
4429 cmocka_unit_test_setup_teardown(test_reqinstance_elem, setup_element_test, teardown_f),
4430 cmocka_unit_test_setup_teardown(test_revision_date_elem, setup_element_test, teardown_f),
4431 cmocka_unit_test_setup_teardown(test_unique_elem, setup_element_test, teardown_f),
4432 cmocka_unit_test_setup_teardown(test_units_elem, setup_element_test, teardown_f),
4433 cmocka_unit_test_setup_teardown(test_when_elem, setup_element_test, teardown_f),
4434 cmocka_unit_test_setup_teardown(test_yin_text_value_elem, setup_element_test, teardown_f),
4435 cmocka_unit_test_setup_teardown(test_type_elem, setup_element_test, teardown_f),
4436 cmocka_unit_test_setup_teardown(test_max_elems_elem, setup_element_test, teardown_f),
4437 cmocka_unit_test_setup_teardown(test_min_elems_elem, setup_element_test, teardown_f),
4438 cmocka_unit_test_setup_teardown(test_ordby_elem, setup_element_test, teardown_f),
4439 cmocka_unit_test_setup_teardown(test_any_elem, setup_element_test, teardown_f),
4440 cmocka_unit_test_setup_teardown(test_leaf_elem, setup_element_test, teardown_f),
4441 cmocka_unit_test_setup_teardown(test_leaf_list_elem, setup_element_test, teardown_f),
4442 cmocka_unit_test_setup_teardown(test_presence_elem, setup_element_test, teardown_f),
4443 cmocka_unit_test_setup_teardown(test_key_elem, setup_element_test, teardown_f),
4444 cmocka_unit_test_setup_teardown(test_typedef_elem, setup_element_test, teardown_f),
4445 cmocka_unit_test_setup_teardown(test_refine_elem, setup_element_test, teardown_f),
4446 cmocka_unit_test_setup_teardown(test_uses_elem, setup_element_test, teardown_f),
4447 cmocka_unit_test_setup_teardown(test_revision_elem, setup_element_test, teardown_f),
4448 cmocka_unit_test_setup_teardown(test_include_elem, setup_element_test, teardown_f),
4449 cmocka_unit_test_setup_teardown(test_list_elem, setup_element_test, teardown_f),
4450 cmocka_unit_test_setup_teardown(test_notification_elem, setup_element_test, teardown_f),
4451 cmocka_unit_test_setup_teardown(test_grouping_elem, setup_element_test, teardown_f),
4452 cmocka_unit_test_setup_teardown(test_container_elem, setup_element_test, teardown_f),
4453 cmocka_unit_test_setup_teardown(test_case_elem, setup_element_test, teardown_f),
4454 cmocka_unit_test_setup_teardown(test_choice_elem, setup_element_test, teardown_f),
4455 cmocka_unit_test_setup_teardown(test_inout_elem, setup_element_test, teardown_f),
4456 cmocka_unit_test_setup_teardown(test_action_elem, setup_element_test, teardown_f),
4457 cmocka_unit_test_setup_teardown(test_augment_elem, setup_element_test, teardown_f),
4458 cmocka_unit_test_setup_teardown(test_deviate_elem, setup_element_test, teardown_f),
4459 cmocka_unit_test_setup_teardown(test_deviation_elem, setup_element_test, teardown_f),
4460 cmocka_unit_test_setup_teardown(test_module_elem, setup_element_test, teardown_f),
4461 cmocka_unit_test_setup_teardown(test_submodule_elem, setup_element_test, teardown_f),
David Sedlák8985a142019-07-31 16:43:06 +02004462
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004463 cmocka_unit_test_setup_teardown(test_yin_parse_module, setup_f, teardown_f),
4464 cmocka_unit_test_setup_teardown(test_yin_parse_submodule, setup_f, teardown_f),
David Sedlák3b4db242018-10-19 16:11:01 +02004465 };
4466
David Sedlák8e7bda82019-07-16 17:57:50 +02004467 return cmocka_run_group_tests(tests, setup_ly_ctx, destroy_ly_ctx);
David Sedlák3b4db242018-10-19 16:11:01 +02004468}