blob: a4d0c2848847c15d6ec2ba933ad0b1c65c87e587 [file] [log] [blame]
David Sedlákb1ce3f82019-06-05 14:37:26 +02001/**
2 * @file test_parser_yin.c
3 * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz>
4 * @brief unit tests for functions from parser_yin.c
5 *
6 * Copyright (c) 2015 - 2019 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
David Sedlák3b4db242018-10-19 16:11:01 +020015#include <stdarg.h>
16#include <stddef.h>
17#include <setjmp.h>
18#include <cmocka.h>
19
20#include <stdio.h>
21#include <string.h>
David Sedlák79e50cb2019-06-05 16:33:09 +020022#include <stdbool.h>
David Sedlák3b4db242018-10-19 16:11:01 +020023
Radek Krejci70593c12020-06-13 20:48:09 +020024#include "common.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020025#include "in.h"
Radek Krejci70593c12020-06-13 20:48:09 +020026#include "parser_internal.h"
Radek Krejci70593c12020-06-13 20:48:09 +020027#include "tree_schema.h"
28#include "tree_schema_internal.h"
29#include "xml.h"
30#include "xpath.h"
David Sedlák3b4db242018-10-19 16:11:01 +020031
Michal Vaskoafac7822020-10-20 14:22:26 +020032/* copied from parser_yin.c */
33enum yin_argument {
34 YIN_ARG_UNKNOWN = 0, /**< parsed argument can not be matched with any supported yin argument keyword */
35 YIN_ARG_NAME, /**< argument name */
36 YIN_ARG_TARGET_NODE, /**< argument target-node */
37 YIN_ARG_MODULE, /**< argument module */
38 YIN_ARG_VALUE, /**< argument value */
39 YIN_ARG_TEXT, /**< argument text */
40 YIN_ARG_CONDITION, /**< argument condition */
41 YIN_ARG_URI, /**< argument uri */
42 YIN_ARG_DATE, /**< argument data */
43 YIN_ARG_TAG, /**< argument tag */
44 YIN_ARG_NONE /**< empty (special value) */
45};
46
47struct yin_subelement {
48 enum ly_stmt type; /**< type of keyword */
49 void *dest; /**< meta infromation passed to responsible function (mostly information about where parsed subelement should be stored) */
50 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 */
51};
52
53struct import_meta {
54 const char *prefix; /**< module prefix. */
55 struct lysp_import **imports; /**< imports to add to. */
56};
57
58struct yin_argument_meta {
59 uint16_t *flags; /**< Argument flags */
60 const char **argument; /**< Argument value */
61};
62
63struct tree_node_meta {
64 struct lysp_node *parent; /**< parent node */
65 struct lysp_node **nodes; /**< linked list of siblings */
66};
67
68struct include_meta {
69 const char *name; /**< Module/submodule name. */
70 struct lysp_include **includes; /**< [Sized array](@ref sizedarrays) of parsed includes to add to. */
71};
72
73struct inout_meta {
74 struct lysp_node *parent; /**< Parent node. */
75 struct lysp_action_inout *inout_p; /**< inout_p Input/output pointer to write to. */
76};
77
78struct minmax_dev_meta {
79 uint32_t *lim; /**< min/max value to write to. */
80 uint16_t *flags; /**< min/max flags to write to. */
81 struct lysp_ext_instance **exts; /**< extension instances to add to. */
82};
83
84#define YIN_SUBELEM_MANDATORY 0x01
85#define YIN_SUBELEM_UNIQUE 0x02
86#define YIN_SUBELEM_FIRST 0x04
87#define YIN_SUBELEM_VER2 0x08
88
89#define YIN_SUBELEM_PARSED 0x80
90
David Sedlák555c7202019-07-04 12:14:12 +020091/* prototypes of static functions */
Michal Vaskoafac7822020-10-20 14:22:26 +020092enum yin_argument yin_match_argument_name(const char *name, size_t len);
93LY_ERR yin_parse_content(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size,
94 enum ly_stmt current_element, const char **text_content, struct lysp_ext_instance **exts);
95LY_ERR yin_validate_value(struct lys_yin_parser_ctx *ctx, enum yang_arg val_type);
96enum ly_stmt yin_match_keyword(struct lys_yin_parser_ctx *ctx, const char *name, size_t name_len,
97 const char *prefix, size_t prefix_len, enum ly_stmt parrent);
98LY_ERR yin_parse_extension_instance(struct lys_yin_parser_ctx *ctx, LYEXT_SUBSTMT subelem, LY_ARRAY_COUNT_TYPE subelem_index,
99 struct lysp_ext_instance **exts);
100LY_ERR yin_parse_element_generic(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, struct lysp_stmt **element);
101LY_ERR yin_parse_mod(struct lys_yin_parser_ctx *ctx, struct lysp_module *mod);
102LY_ERR yin_parse_submod(struct lys_yin_parser_ctx *ctx, struct lysp_submodule *submod);
103
David Sedlák555c7202019-07-04 12:14:12 +0200104void lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext);
David Sedlák986cb412019-07-04 13:10:11 +0200105void lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext);
David Sedlák32eee7b2019-07-09 12:38:44 +0200106void lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when);
David Sedlák32488102019-07-15 17:44:10 +0200107void lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type);
David Sedlák8a83bbb2019-07-18 14:46:00 +0200108void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
David Sedlák04e17b22019-07-19 15:29:48 +0200109void lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf);
David Sedlákd2d676a2019-07-22 11:28:19 +0200110void lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref);
David Sedlákaa854b02019-07-22 14:17:10 +0200111void lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev);
David Sedlák0c2bab92019-07-22 15:33:19 +0200112void lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include);
David Sedlák5e13dea2019-07-22 16:06:45 +0200113void lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat);
David Sedlák28794f22019-07-22 16:45:00 +0200114void lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident);
David Sedlák031b9e72019-07-23 15:19:37 +0200115void lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif);
David Sedláke3ce9ef2019-07-23 16:34:30 +0200116void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp);
David Sedlák05404f62019-07-24 14:11:53 +0200117void lysp_action_inout_free(struct ly_ctx *ctx, struct lysp_action_inout *inout);
David Sedlák85d0eca2019-07-24 15:15:21 +0200118void lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action);
David Sedlák992fb7c2019-07-24 16:51:01 +0200119void lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment);
David Sedlák4ffcec82019-07-25 15:10:21 +0200120void lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d);
David Sedlák8b754462019-07-25 16:22:13 +0200121void lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev);
David Sedlák298ff6d2019-07-26 14:29:03 +0200122void lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import);
David Sedlák555c7202019-07-04 12:14:12 +0200123
David Sedláke6cd89e2019-08-07 12:46:02 +0200124/* wrapping element used for mocking has nothing to do with real module structure */
125#define ELEMENT_WRAPPER_START "<status xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
126#define ELEMENT_WRAPPER_END "</status>"
127
Radek Krejci3a4889a2020-05-19 17:01:58 +0200128struct test_parser_yin_state {
David Sedlák3b4db242018-10-19 16:11:01 +0200129 struct ly_ctx *ctx;
Michal Vaskob36053d2020-03-26 15:49:30 +0100130 struct lys_yin_parser_ctx *yin_ctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200131 struct ly_in *in;
David Sedlák79e50cb2019-06-05 16:33:09 +0200132 bool finished_correctly;
David Sedlák68a1af12019-03-08 13:46:54 +0100133};
David Sedlák872c7b42018-10-26 13:15:20 +0200134
David Sedlák79e50cb2019-06-05 16:33:09 +0200135#define BUFSIZE 1024
136char logbuf[BUFSIZE] = {0};
137int store = -1; /* negative for infinite logging, positive for limited logging */
138
139/* set to 0 to printing error messages to stderr instead of checking them in code */
David Sedláke0ef1c62019-09-13 10:05:55 +0200140#define ENABLE_LOGGER_CHECKING 1
David Sedlák79e50cb2019-06-05 16:33:09 +0200141
142#if ENABLE_LOGGER_CHECKING
143static void
144logger(LY_LOG_LEVEL level, const char *msg, const char *path)
145{
146 (void) level; /* unused */
147 if (store) {
148 if (path && path[0]) {
149 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
150 } else {
151 strncpy(logbuf, msg, BUFSIZE - 1);
152 }
153 if (store > 0) {
154 --store;
155 }
156 }
157}
158#endif
159
160#if ENABLE_LOGGER_CHECKING
161# define logbuf_assert(str) assert_string_equal(logbuf, str)
162#else
163# define logbuf_assert(str)
164#endif
165
166#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
167 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
168 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
169 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
170 CLEANUP
171
David Sedlák8e7bda82019-07-16 17:57:50 +0200172int
173setup_ly_ctx(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +0100174{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200175 struct test_parser_yin_state *st = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +0200176
David Sedlák68a1af12019-03-08 13:46:54 +0100177 /* allocate state variable */
178 (*state) = st = calloc(1, sizeof(*st));
179 if (!st) {
180 fprintf(stderr, "Memmory allocation failed");
181 return EXIT_FAILURE;
182 }
David Sedlák872c7b42018-10-26 13:15:20 +0200183
David Sedlák68a1af12019-03-08 13:46:54 +0100184 /* create new libyang context */
185 ly_ctx_new(NULL, 0, &st->ctx);
David Sedlák872c7b42018-10-26 13:15:20 +0200186
David Sedlák8e7bda82019-07-16 17:57:50 +0200187 return EXIT_SUCCESS;
188}
189
190int
191destroy_ly_ctx(void **state)
192{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200193 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +0200194 ly_ctx_destroy(st->ctx, NULL);
195 free(st);
196
197 return EXIT_SUCCESS;
198}
199
200static int
201setup_f(void **state)
202{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200203 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +0200204
205#if ENABLE_LOGGER_CHECKING
206 /* setup logger */
207 ly_set_log_clb(logger, 1);
208#endif
209
David Sedlák619db942019-07-03 14:47:30 +0200210 /* allocate parser context */
David Sedlákda8ffa32019-07-08 14:17:10 +0200211 st->yin_ctx = calloc(1, sizeof(*st->yin_ctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100212 st->yin_ctx->format = LYS_IN_YIN;
David Sedlák8f5bce02019-06-03 16:41:08 +0200213
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200214 /* allocate new parsed module */
215 st->yin_ctx->parsed_mod = calloc(1, sizeof *st->yin_ctx->parsed_mod);
216
217 /* allocate new module */
218 st->yin_ctx->parsed_mod->mod = calloc(1, sizeof *st->yin_ctx->parsed_mod->mod);
219 st->yin_ctx->parsed_mod->mod->ctx = st->ctx;
220 st->yin_ctx->parsed_mod->mod->parsed = st->yin_ctx->parsed_mod;
221
Michal Vasko63f3d842020-07-08 10:10:14 +0200222 st->in = NULL;
223
David Sedlák68a1af12019-03-08 13:46:54 +0100224 return EXIT_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +0200225}
226
227static int
David Sedlák68a1af12019-03-08 13:46:54 +0100228teardown_f(void **state)
229{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200230 struct test_parser_yin_state *st = *(struct test_parser_yin_state **)state;
David Sedlák68a1af12019-03-08 13:46:54 +0100231
David Sedlák79e50cb2019-06-05 16:33:09 +0200232#if ENABLE_LOGGER_CHECKING
233 /* teardown logger */
234 if (!st->finished_correctly && logbuf[0] != '\0') {
235 fprintf(stderr, "%s\n", logbuf);
236 }
237#endif
238
Michal Vaskob36053d2020-03-26 15:49:30 +0100239 lyxml_ctx_free(st->yin_ctx->xmlctx);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200240 lys_module_free(st->yin_ctx->parsed_mod->mod, NULL);
David Sedlákda8ffa32019-07-08 14:17:10 +0200241 free(st->yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200242 ly_in_free(st->in, 0);
David Sedlák68a1af12019-03-08 13:46:54 +0100243
244 return EXIT_SUCCESS;
245}
246
Radek Krejci3a4889a2020-05-19 17:01:58 +0200247static struct test_parser_yin_state*
David Sedlák392af4f2019-06-04 16:02:42 +0200248reset_state(void **state)
249{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200250 ((struct test_parser_yin_state *)*state)->finished_correctly = true;
David Sedlák555c7202019-07-04 12:14:12 +0200251 logbuf[0] = '\0';
David Sedlák392af4f2019-06-04 16:02:42 +0200252 teardown_f(state);
253 setup_f(state);
254
255 return *state;
256}
257
David Sedlák79e50cb2019-06-05 16:33:09 +0200258void
259logbuf_clean(void)
260{
261 logbuf[0] = '\0';
262}
263
David Sedlák8985a142019-07-31 16:43:06 +0200264static int
David Sedlák8985a142019-07-31 16:43:06 +0200265setup_element_test(void **state)
266{
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200267 struct test_parser_yin_state *st;
David Sedlák8985a142019-07-31 16:43:06 +0200268
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200269 setup_f(state);
270 st = *state;
David Sedlák8985a142019-07-31 16:43:06 +0200271
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200272 lydict_insert(st->ctx, "module-name", 0, &st->yin_ctx->parsed_mod->mod->name);
David Sedlák8985a142019-07-31 16:43:06 +0200273
274 return EXIT_SUCCESS;
275}
276
David Sedlák68a1af12019-03-08 13:46:54 +0100277static void
David Sedlák1bccdfa2019-06-17 15:55:27 +0200278test_yin_match_keyword(void **state)
David Sedlák3b4db242018-10-19 16:11:01 +0200279{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200280 struct test_parser_yin_state *st = *state;
David Sedlák3b4db242018-10-19 16:11:01 +0200281
Michal Vaskob36053d2020-03-26 15:49:30 +0100282 const char *prefix;
283 size_t prefix_len;
David Sedlák8f7a1172019-06-20 14:42:18 +0200284 /* create mock yin namespace in xml context */
285 const char *data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" />";
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>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200394 ly_in_new_memory(data, &st->in);
395 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100396
397 ret = yin_parse_element_generic(st->yin_ctx, LY_STMT_EXTENSION_INSTANCE, &exts.child);
David Sedlákb1a78352019-06-28 16:16:29 +0200398 assert_int_equal(ret, LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +0100399 assert_int_equal(st->yin_ctx->xmlctx->status, LYXML_ELEM_CLOSE);
David Sedláke0ef1c62019-09-13 10:05:55 +0200400 assert_string_equal(exts.child->stmt, "urn:example:extensions:elem");
David Sedlákb1a78352019-06-28 16:16:29 +0200401 assert_string_equal(exts.child->arg, "text_value");
David Sedlákb1a78352019-06-28 16:16:29 +0200402 assert_string_equal(exts.child->child->stmt, "attr");
403 assert_string_equal(exts.child->child->arg, "value");
404 assert_true(exts.child->child->flags & LYS_YIN_ATTR);
David Sedlákb1a78352019-06-28 16:16:29 +0200405 lysp_ext_instance_free(st->ctx, &exts);
David Sedlák5392a212019-07-01 09:19:10 +0200406 st = reset_state(state);
407
David Sedlákb0ca07d2019-09-11 11:54:05 +0200408 data = "<myext:elem xmlns:myext=\"urn:example:extensions\"></myext:elem>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200409 ly_in_new_memory(data, &st->in);
410 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100411
412 ret = yin_parse_element_generic(st->yin_ctx, LY_STMT_EXTENSION_INSTANCE, &exts.child);
David Sedlák5392a212019-07-01 09:19:10 +0200413 assert_int_equal(ret, LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +0100414 assert_int_equal(st->yin_ctx->xmlctx->status, LYXML_ELEM_CLOSE);
David Sedláke0ef1c62019-09-13 10:05:55 +0200415 assert_string_equal(exts.child->stmt, "urn:example:extensions:elem");
David Sedlák5392a212019-07-01 09:19:10 +0200416 assert_null(exts.child->child);
417 assert_null(exts.child->arg);
418 lysp_ext_instance_free(st->ctx, &exts);
419
David Sedlákb1a78352019-06-28 16:16:29 +0200420 st->finished_correctly = true;
421}
422
423static void
424test_yin_parse_extension_instance(void **state)
425{
426 LY_ERR ret;
Radek Krejci3a4889a2020-05-19 17:01:58 +0200427 struct test_parser_yin_state *st = *state;
David Sedlákb1a78352019-06-28 16:16:29 +0200428 struct lysp_ext_instance *exts = NULL;
David Sedlákb0ca07d2019-09-11 11:54:05 +0200429 const char *data = "<myext:ext value1=\"test\" value=\"test2\" xmlns:myext=\"urn:example:extensions\"><myext:subelem>text</myext:subelem></myext:ext>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200430 ly_in_new_memory(data, &st->in);
431 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100432
433 ret = yin_parse_extension_instance(st->yin_ctx, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200434 assert_int_equal(ret, LY_SUCCESS);
David Sedláke0ef1c62019-09-13 10:05:55 +0200435 assert_string_equal(exts->name, "urn:example:extensions:ext");
David Sedlákb1a78352019-06-28 16:16:29 +0200436 assert_int_equal(exts->insubstmt_index, 0);
437 assert_true(exts->insubstmt == LYEXT_SUBSTMT_CONTACT);
438 assert_true(exts->yin & LYS_YIN);
439 assert_string_equal(exts->child->stmt, "value1");
440 assert_string_equal(exts->child->arg, "test");
441 assert_null(exts->child->child);
442 assert_true(exts->child->flags & LYS_YIN_ATTR);
443 assert_string_equal(exts->child->next->stmt, "value");
444 assert_string_equal(exts->child->next->arg, "test2");
445 assert_null(exts->child->next->child);
446 assert_true(exts->child->next->flags & LYS_YIN_ATTR);
447
David Sedláke0ef1c62019-09-13 10:05:55 +0200448 assert_string_equal(exts->child->next->next->stmt, "urn:example:extensions:subelem");
David Sedlákb1a78352019-06-28 16:16:29 +0200449 assert_string_equal(exts->child->next->next->arg, "text");
450 assert_null(exts->child->next->next->child);
451 assert_null(exts->child->next->next->next);
452 assert_false(exts->child->next->next->flags & LYS_YIN_ATTR);
David Sedlákb1a78352019-06-28 16:16:29 +0200453 lysp_ext_instance_free(st->ctx, exts);
454 LY_ARRAY_FREE(exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200455 exts = NULL;
David Sedlákf250ecf2019-07-01 11:02:05 +0200456 st = reset_state(state);
457
David Sedlákb0ca07d2019-09-11 11:54:05 +0200458 data = "<myext:extension-elem xmlns:myext=\"urn:example:extensions\" />";
Michal Vasko63f3d842020-07-08 10:10:14 +0200459 ly_in_new_memory(data, &st->in);
460 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100461
462 ret = yin_parse_extension_instance(st->yin_ctx, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200463 assert_int_equal(ret, LY_SUCCESS);
David Sedláke0ef1c62019-09-13 10:05:55 +0200464 assert_string_equal(exts->name, "urn:example:extensions:extension-elem");
David Sedlákf250ecf2019-07-01 11:02:05 +0200465 assert_null(exts->argument);
466 assert_null(exts->child);
467 assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT);
468 assert_int_equal(exts->insubstmt_index, 0);
469 assert_true(exts->yin & LYS_YIN);
David Sedlákf250ecf2019-07-01 11:02:05 +0200470 lysp_ext_instance_free(st->ctx, exts);
471 LY_ARRAY_FREE(exts);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200472 exts = NULL;
David Sedlákadd0c2e2019-08-16 10:49:12 +0200473 st = reset_state(state);
474
David Sedlákb0ca07d2019-09-11 11:54:05 +0200475 data = "<myext:ext attr1=\"text1\" attr2=\"text2\" xmlns:myext=\"urn:example:extensions\">"
476 "<myext:ext-sub1/>"
477 "<myext:ext-sub2 sattr1=\"stext2\">"
478 "<myext:ext-sub21>"
479 "<myext:ext-sub211 sattr21=\"text21\"/>"
480 "</myext:ext-sub21>"
481 "</myext:ext-sub2>"
482 "<myext:ext-sub3 attr3=\"text3\"></myext:ext-sub3>"
483 "</myext:ext>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200484 ly_in_new_memory(data, &st->in);
485 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100486
487 ret = yin_parse_extension_instance(st->yin_ctx, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200488 assert_int_equal(ret, LY_SUCCESS);
489
David Sedláke0ef1c62019-09-13 10:05:55 +0200490 assert_string_equal(exts->name, "urn:example:extensions:ext");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200491 assert_null(exts->argument);
492 assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT);
493 assert_int_equal(exts->insubstmt_index, 0);
494 assert_true(exts->yin & LYS_YIN);
495 assert_string_equal(exts->child->stmt, "attr1");
496 assert_string_equal(exts->child->arg, "text1");
497 assert_null(exts->child->child);
498 assert_true(exts->child->flags & LYS_YIN_ATTR);
499
500 assert_string_equal(exts->child->next->stmt, "attr2");
501 assert_string_equal(exts->child->next->arg, "text2");
502 assert_null(exts->child->next->child);
503 assert_true(exts->child->next->flags & LYS_YIN_ATTR);
504
David Sedláke0ef1c62019-09-13 10:05:55 +0200505 assert_string_equal(exts->child->next->next->stmt, "urn:example:extensions:ext-sub1");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200506 assert_null(exts->child->next->next->arg);
507 assert_null(exts->child->next->next->child);
508 assert_int_equal(exts->child->next->next->flags, 0);
509
David Sedláke0ef1c62019-09-13 10:05:55 +0200510 assert_string_equal(exts->child->next->next->next->stmt, "urn:example:extensions:ext-sub2");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200511 assert_null(exts->child->next->next->next->arg);
512 assert_int_equal(exts->child->next->next->next->flags, 0);
513 assert_string_equal(exts->child->next->next->next->child->stmt, "sattr1");
514 assert_string_equal(exts->child->next->next->next->child->arg, "stext2");
515 assert_null(exts->child->next->next->next->child->child);
516 assert_true(exts->child->next->next->next->child->flags & LYS_YIN_ATTR);
517
David Sedláke0ef1c62019-09-13 10:05:55 +0200518 assert_string_equal(exts->child->next->next->next->child->next->stmt, "urn:example:extensions:ext-sub21");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200519 assert_null(exts->child->next->next->next->child->next->arg);
520 assert_null(exts->child->next->next->next->child->next->next);
521 assert_int_equal(exts->child->next->next->next->child->next->flags, 0);
522
David Sedláke0ef1c62019-09-13 10:05:55 +0200523 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 +0200524 assert_null(exts->child->next->next->next->child->next->child->arg);
525 assert_int_equal(exts->child->next->next->next->child->next->child->flags, 0);
526 assert_null(exts->child->next->next->next->child->next->child->next);
527
528 assert_string_equal(exts->child->next->next->next->child->next->child->child->stmt, "sattr21");
529 assert_string_equal(exts->child->next->next->next->child->next->child->child->arg, "text21");
530 assert_null(exts->child->next->next->next->child->next->child->child->next);
531 assert_null(exts->child->next->next->next->child->next->child->child->child);
532 assert_true(exts->child->next->next->next->child->next->child->child->flags & LYS_YIN_ATTR);
533
David Sedláke0ef1c62019-09-13 10:05:55 +0200534 assert_string_equal(exts->child->next->next->next->next->stmt, "urn:example:extensions:ext-sub3");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200535 assert_null(exts->child->next->next->next->next->arg);
536 assert_null(exts->child->next->next->next->next->next);
537 assert_int_equal(exts->child->next->next->next->next->flags, 0);
538
539 assert_string_equal(exts->child->next->next->next->next->child->stmt, "attr3");
540 assert_string_equal(exts->child->next->next->next->next->child->arg, "text3");
541 assert_null(exts->child->next->next->next->next->child->next);
542 assert_null(exts->child->next->next->next->next->child->child);
543 assert_true(exts->child->next->next->next->next->child->flags & LYS_YIN_ATTR);
544
David Sedlákadd0c2e2019-08-16 10:49:12 +0200545 lysp_ext_instance_free(st->ctx, exts);
546 LY_ARRAY_FREE(exts);
547 exts = NULL;
David Sedlákaa98bba2019-09-12 11:52:14 +0200548 st = reset_state(state);
549
550 data = "<myext:extension-elem xmlns:myext=\"urn:example:extensions\" xmlns:yin=\"urn:ietf:params:xml:ns:yang:yin:1\">"
551 "<yin:action name=\"act-name\" pre:prefixed=\"ignored\"/>"
552 "<yin:augment target-node=\"target\"/>"
553 "<yin:status value=\"value\"/>"
554 "<yin:include module=\"mod\"/>"
555 "<yin:input />"
556 "<yin:must condition=\"cond\"/>"
557 "<yin:namespace uri=\"uri\"/>"
558 "<yin:revision date=\"data\"/>"
559 "<yin:unique tag=\"tag\"/>"
David Sedlákd2844882019-09-13 16:01:22 +0200560 "<yin:description><yin:text>contact-val</yin:text></yin:description>"
561 "<yin:error-message><yin:value>err-msg</yin:value></yin:error-message>"
David Sedlákaa98bba2019-09-12 11:52:14 +0200562 "</myext:extension-elem>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200563 ly_in_new_memory(data, &st->in);
564 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100565
566 ret = yin_parse_extension_instance(st->yin_ctx, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákaa98bba2019-09-12 11:52:14 +0200567 assert_int_equal(ret, LY_SUCCESS);
568 assert_string_equal(exts->child->arg, "act-name");
569 assert_string_equal(exts->child->next->arg, "target");
570 assert_string_equal(exts->child->next->next->arg, "value");
571 assert_string_equal(exts->child->next->next->next->arg, "mod");
572 assert_null(exts->child->next->next->next->next->arg);
573 assert_string_equal(exts->child->next->next->next->next->next->arg, "cond");
574 assert_string_equal(exts->child->next->next->next->next->next->next->arg, "uri");
575 assert_string_equal(exts->child->next->next->next->next->next->next->next->arg, "data");
576 assert_string_equal(exts->child->next->next->next->next->next->next->next->next->arg, "tag");
577 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 +0200578 lysp_ext_instance_free(st->ctx, exts);
579 LY_ARRAY_FREE(exts);
580 exts = NULL;
David Sedlákaa98bba2019-09-12 11:52:14 +0200581 st = reset_state(state);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200582
David Sedlákb1a78352019-06-28 16:16:29 +0200583 st->finished_correctly = true;
584}
585
David Sedlák555c7202019-07-04 12:14:12 +0200586static void
587test_yin_parse_content(void **state)
588{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200589 struct test_parser_yin_state *st = *state;
David Sedlák555c7202019-07-04 12:14:12 +0200590 LY_ERR ret = LY_SUCCESS;
David Sedlák555c7202019-07-04 12:14:12 +0200591 const char *data = "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlákb0ca07d2019-09-11 11:54:05 +0200592 "<myext:custom xmlns:myext=\"urn:example:extensions\">"
David Sedlák555c7202019-07-04 12:14:12 +0200593 "totally amazing extension"
David Sedlákb0ca07d2019-09-11 11:54:05 +0200594 "</myext:custom>"
David Sedlákaa98bba2019-09-12 11:52:14 +0200595 "<extension name=\"ext\">"
David Sedlák986cb412019-07-04 13:10:11 +0200596 "<argument name=\"argname\"></argument>"
597 "<description><text>desc</text></description>"
598 "<reference><text>ref</text></reference>"
599 "<status value=\"deprecated\"></status>"
David Sedlákaa98bba2019-09-12 11:52:14 +0200600 "</extension>"
David Sedlák555c7202019-07-04 12:14:12 +0200601 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedlák32488102019-07-15 17:44:10 +0200602 "<if-feature name=\"foo\"></if-feature>"
David Sedlák32eee7b2019-07-09 12:38:44 +0200603 "<when condition=\"condition...\">"
604 "<reference><text>when_ref</text></reference>"
605 "<description><text>when_desc</text></description>"
606 "</when>"
David Sedláke1a30302019-07-10 13:49:38 +0200607 "<config value=\"true\"/>"
David Sedlákc1771b12019-07-10 15:55:46 +0200608 "<error-message>"
609 "<value>error-msg</value>"
610 "</error-message>"
David Sedlák2ce1be62019-07-10 16:15:09 +0200611 "<error-app-tag value=\"err-app-tag\"/>"
David Sedláka5b1d382019-07-10 16:31:09 +0200612 "<units name=\"radians\"></units>"
David Sedláke7084ce2019-07-10 16:44:15 +0200613 "<default value=\"default-value\"/>"
David Sedlák5545f5d2019-07-11 11:55:16 +0200614 "<position value=\"25\"></position>"
615 "<value value=\"-5\"/>"
David Sedlákcf5569a2019-07-11 13:31:34 +0200616 "<require-instance value=\"true\"></require-instance>"
David Sedlákb7296dd2019-07-11 14:58:38 +0200617 "<range value=\"5..10\" />"
David Sedlák438ae432019-07-11 15:36:54 +0200618 "<length value=\"baf\"/>"
David Sedlákd3983112019-07-12 11:20:56 +0200619 "<pattern value='pattern'>"
620 "<modifier value='invert-match'/>"
621 "</pattern>"
David Sedlákfd5b9c32019-07-12 15:33:13 +0200622 "<enum name=\"yay\">"
623 "</enum>"
David Sedlák555c7202019-07-04 12:14:12 +0200624 "</prefix>";
625 struct lysp_ext_instance *exts = NULL;
David Sedlák5f8191e2019-07-08 16:35:52 +0200626 const char **if_features = NULL;
Michal Vasko7f45cf22020-10-01 12:49:44 +0200627 const char *value, *err_msg, *app_tag, *units;
628 struct lysp_qname def = {0};
David Sedlák986cb412019-07-04 13:10:11 +0200629 struct lysp_ext *ext_def = NULL;
David Sedlák32eee7b2019-07-09 12:38:44 +0200630 struct lysp_when *when_p = NULL;
David Sedlákcf5569a2019-07-11 13:31:34 +0200631 struct lysp_type_enum pos_enum = {}, val_enum = {};
David Sedlákfd5b9c32019-07-12 15:33:13 +0200632 struct lysp_type req_type = {}, range_type = {}, len_type = {}, patter_type = {}, enum_type = {};
David Sedláke1a30302019-07-10 13:49:38 +0200633 uint8_t config = 0;
David Sedlák555c7202019-07-04 12:14:12 +0200634
Michal Vasko63f3d842020-07-08 10:10:14 +0200635 ly_in_new_memory(data, &st->in);
636 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100637 lyxml_ctx_next(st->yin_ctx->xmlctx);
638 lyxml_ctx_next(st->yin_ctx->xmlctx);
639 lyxml_ctx_next(st->yin_ctx->xmlctx);
David Sedlák555c7202019-07-04 12:14:12 +0200640
David Sedlákfd5b9c32019-07-12 15:33:13 +0200641 struct yin_subelement subelems[17] = {
Radek Krejcid6b76452019-09-03 17:03:03 +0200642 {LY_STMT_CONFIG, &config, 0},
643 {LY_STMT_DEFAULT, &def, YIN_SUBELEM_UNIQUE},
644 {LY_STMT_ENUM, &enum_type, 0},
645 {LY_STMT_ERROR_APP_TAG, &app_tag, YIN_SUBELEM_UNIQUE},
646 {LY_STMT_ERROR_MESSAGE, &err_msg, 0},
647 {LY_STMT_EXTENSION, &ext_def, 0},
648 {LY_STMT_IF_FEATURE, &if_features, 0},
649 {LY_STMT_LENGTH, &len_type, 0},
650 {LY_STMT_PATTERN, &patter_type, 0},
651 {LY_STMT_POSITION, &pos_enum, 0},
652 {LY_STMT_RANGE, &range_type, 0},
653 {LY_STMT_REQUIRE_INSTANCE, &req_type, 0},
654 {LY_STMT_UNITS, &units, YIN_SUBELEM_UNIQUE},
655 {LY_STMT_VALUE, &val_enum, 0},
656 {LY_STMT_WHEN, &when_p, 0},
657 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
658 {LY_STMT_ARG_TEXT, &value, 0}
David Sedlákd3983112019-07-12 11:20:56 +0200659 };
Michal Vaskob36053d2020-03-26 15:49:30 +0100660 ret = yin_parse_content(st->yin_ctx, subelems, 17, LY_STMT_PREFIX, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200661 assert_int_equal(ret, LY_SUCCESS);
David Sedlák2ce1be62019-07-10 16:15:09 +0200662 /* check parsed values */
Michal Vasko7f45cf22020-10-01 12:49:44 +0200663 assert_string_equal(def.str, "default-value");
David Sedláke0ef1c62019-09-13 10:05:55 +0200664 assert_string_equal(exts->name, "urn:example:extensions:custom");
David Sedlák555c7202019-07-04 12:14:12 +0200665 assert_string_equal(exts->argument, "totally amazing extension");
666 assert_string_equal(value, "wsefsdf");
David Sedláka5b1d382019-07-10 16:31:09 +0200667 assert_string_equal(units, "radians");
David Sedlák32eee7b2019-07-09 12:38:44 +0200668 assert_string_equal(when_p->cond, "condition...");
669 assert_string_equal(when_p->dsc, "when_desc");
670 assert_string_equal(when_p->ref, "when_ref");
David Sedláke1a30302019-07-10 13:49:38 +0200671 assert_int_equal(config, LYS_CONFIG_W);
David Sedlák5545f5d2019-07-11 11:55:16 +0200672 assert_int_equal(pos_enum.value, 25);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200673 assert_true(pos_enum.flags & LYS_SET_VALUE);
David Sedlák5545f5d2019-07-11 11:55:16 +0200674 assert_int_equal(val_enum.value, -5);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200675 assert_true(val_enum.flags & LYS_SET_VALUE);
David Sedlákcf5569a2019-07-11 13:31:34 +0200676 assert_int_equal(req_type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200677 assert_true(req_type.flags &= LYS_SET_REQINST);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200678 assert_string_equal(range_type.range->arg.str, "5..10");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200679 assert_true(range_type.flags & LYS_SET_RANGE);
David Sedlákc1771b12019-07-10 15:55:46 +0200680 assert_string_equal(err_msg, "error-msg");
David Sedlák2ce1be62019-07-10 16:15:09 +0200681 assert_string_equal(app_tag, "err-app-tag");
David Sedlákfd5b9c32019-07-12 15:33:13 +0200682 assert_string_equal(enum_type.enums->name, "yay");
Michal Vasko7f45cf22020-10-01 12:49:44 +0200683 assert_string_equal(len_type.length->arg.str, "baf");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200684 assert_true(len_type.flags & LYS_SET_LENGTH);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200685 assert_string_equal(patter_type.patterns->arg.str, "\x015pattern");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200686 assert_true(patter_type.flags & LYS_SET_PATTERN);
David Sedlák2ce1be62019-07-10 16:15:09 +0200687 /* cleanup */
David Sedlák555c7202019-07-04 12:14:12 +0200688 lysp_ext_instance_free(st->ctx, exts);
David Sedlák32eee7b2019-07-09 12:38:44 +0200689 lysp_when_free(st->ctx, when_p);
David Sedlák986cb412019-07-04 13:10:11 +0200690 lysp_ext_free(st->ctx, ext_def);
David Sedlák5f8191e2019-07-08 16:35:52 +0200691 FREE_STRING(st->ctx, *if_features);
David Sedlákc1771b12019-07-10 15:55:46 +0200692 FREE_STRING(st->ctx, err_msg);
David Sedlák2ce1be62019-07-10 16:15:09 +0200693 FREE_STRING(st->ctx, app_tag);
David Sedláka5b1d382019-07-10 16:31:09 +0200694 FREE_STRING(st->ctx, units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200695 FREE_STRING(st->ctx, patter_type.patterns->arg.str);
696 FREE_STRING(st->ctx, def.str);
697 FREE_STRING(st->ctx, range_type.range->arg.str);
698 FREE_STRING(st->ctx, len_type.length->arg.str);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200699 FREE_STRING(st->ctx, enum_type.enums->name);
David Sedlákb7296dd2019-07-11 14:58:38 +0200700 FREE_STRING(st->ctx, value);
David Sedlák5f8191e2019-07-08 16:35:52 +0200701 LY_ARRAY_FREE(if_features);
David Sedlák555c7202019-07-04 12:14:12 +0200702 LY_ARRAY_FREE(exts);
David Sedlák986cb412019-07-04 13:10:11 +0200703 LY_ARRAY_FREE(ext_def);
David Sedlákd3983112019-07-12 11:20:56 +0200704 LY_ARRAY_FREE(patter_type.patterns);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200705 LY_ARRAY_FREE(enum_type.enums);
David Sedlák32eee7b2019-07-09 12:38:44 +0200706 free(when_p);
David Sedlákb7296dd2019-07-11 14:58:38 +0200707 free(range_type.range);
David Sedlák438ae432019-07-11 15:36:54 +0200708 free(len_type.length);
David Sedlák555c7202019-07-04 12:14:12 +0200709 st = reset_state(state);
710
711 /* test unique subelem */
712 const char *prefix_value;
Radek Krejcid6b76452019-09-03 17:03:03 +0200713 struct yin_subelement subelems2[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE},
714 {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_UNIQUE}};
David Sedláke6cd89e2019-08-07 12:46:02 +0200715 data = ELEMENT_WRAPPER_START
David Sedlák555c7202019-07-04 12:14:12 +0200716 "<prefix value=\"inv_mod\" />"
717 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
718 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedláke6cd89e2019-08-07 12:46:02 +0200719 ELEMENT_WRAPPER_END;
Michal Vasko63f3d842020-07-08 10:10:14 +0200720 ly_in_new_memory(data, &st->in);
721 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100722 lyxml_ctx_next(st->yin_ctx->xmlctx);
723
724 ret = yin_parse_content(st->yin_ctx, subelems2, 2, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200725 assert_int_equal(ret, LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200726 logbuf_assert("Redefinition of \"text\" sub-element in \"status\" element. Line number 1.");
David Sedlák555c7202019-07-04 12:14:12 +0200727 lydict_remove(st->ctx, prefix_value);
728 lydict_remove(st->ctx, value);
729 st = reset_state(state);
David Sedlák555c7202019-07-04 12:14:12 +0200730
731 /* test first subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200732 data = ELEMENT_WRAPPER_START
David Sedlák555c7202019-07-04 12:14:12 +0200733 "<prefix value=\"inv_mod\" />"
734 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
735 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedláke6cd89e2019-08-07 12:46:02 +0200736 ELEMENT_WRAPPER_END;
Radek Krejcid6b76452019-09-03 17:03:03 +0200737 struct yin_subelement subelems3[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE},
738 {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_FIRST}};
Michal Vasko63f3d842020-07-08 10:10:14 +0200739 ly_in_new_memory(data, &st->in);
740 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100741 lyxml_ctx_next(st->yin_ctx->xmlctx);
742
743 ret = yin_parse_content(st->yin_ctx, subelems3, 2, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200744 assert_int_equal(ret, LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200745 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 +0200746 lydict_remove(st->ctx, prefix_value);
747 st = reset_state(state);
David Sedlák555c7202019-07-04 12:14:12 +0200748
749 /* test mandatory subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200750 data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END;
Radek Krejcid6b76452019-09-03 17:03:03 +0200751 struct yin_subelement subelems4[1] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}};
Michal Vasko63f3d842020-07-08 10:10:14 +0200752 ly_in_new_memory(data, &st->in);
753 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100754 lyxml_ctx_next(st->yin_ctx->xmlctx);
755
756 ret = yin_parse_content(st->yin_ctx, subelems4, 1, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200757 assert_int_equal(ret, LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200758 logbuf_assert("Missing mandatory sub-element \"prefix\" of \"status\" element. Line number 1.");
David Sedlák555c7202019-07-04 12:14:12 +0200759
760 st->finished_correctly = true;
761}
762
David Sedlák92147b02019-07-09 14:01:01 +0200763static void
David Sedlák4a650532019-07-10 11:55:18 +0200764test_validate_value(void **state)
765{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200766 struct test_parser_yin_state *st = *state;
Michal Vaskob36053d2020-03-26 15:49:30 +0100767 const char *data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END;
768
769 /* create some XML context */
Michal Vasko63f3d842020-07-08 10:10:14 +0200770 ly_in_new_memory(data, &st->in);
771 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100772 st->yin_ctx->xmlctx->status = LYXML_ELEM_CONTENT;
773 st->yin_ctx->xmlctx->dynamic = 0;
774
775 st->yin_ctx->xmlctx->value = "#invalid";
776 st->yin_ctx->xmlctx->value_len = 8;
777 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG), LY_EVALID);
Radek Krejci1deb5be2020-08-26 16:43:36 +0200778 logbuf_assert("Invalid identifier character '#' (0x0023). Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100779
780 st->yin_ctx->xmlctx->value = "";
781 st->yin_ctx->xmlctx->value_len = 0;
782 assert_int_equal(yin_validate_value(st->yin_ctx, Y_STR_ARG), LY_SUCCESS);
783
784 st->yin_ctx->xmlctx->value = "pre:b";
785 st->yin_ctx->xmlctx->value_len = 5;
786 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG), LY_EVALID);
787 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG), LY_SUCCESS);
788
789 st->yin_ctx->xmlctx->value = "pre:pre:b";
790 st->yin_ctx->xmlctx->value_len = 9;
791 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG), LY_EVALID);
David Sedlák4a650532019-07-10 11:55:18 +0200792
793 st->finished_correctly = true;
794}
795
David Sedlák32488102019-07-15 17:44:10 +0200796/* helper function to simplify unit test of each element using parse_content function */
797LY_ERR
Radek Krejci3a4889a2020-05-19 17:01:58 +0200798test_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 +0200799{
David Sedlákc5b20842019-08-13 10:18:31 +0200800 const char *name, *prefix;
801 size_t name_len, prefix_len;
David Sedlák32488102019-07-15 17:44:10 +0200802 LY_ERR ret = LY_SUCCESS;
803 struct yin_subelement subelems[71] = {
Radek Krejcid6b76452019-09-03 17:03:03 +0200804 {LY_STMT_ACTION, dest, 0},
805 {LY_STMT_ANYDATA, dest, 0},
806 {LY_STMT_ANYXML, dest, 0},
807 {LY_STMT_ARGUMENT,dest, 0},
808 {LY_STMT_AUGMENT, dest, 0},
809 {LY_STMT_BASE, dest, 0},
810 {LY_STMT_BELONGS_TO, dest, 0},
811 {LY_STMT_BIT, dest, 0},
812 {LY_STMT_CASE, dest, 0},
813 {LY_STMT_CHOICE, dest, 0},
814 {LY_STMT_CONFIG, dest, 0},
815 {LY_STMT_CONTACT, dest, 0},
816 {LY_STMT_CONTAINER, dest, 0},
817 {LY_STMT_DEFAULT, dest, YIN_SUBELEM_UNIQUE},
818 {LY_STMT_DESCRIPTION, dest, 0},
819 {LY_STMT_DEVIATE, dest, 0},
820 {LY_STMT_DEVIATION, dest, 0},
821 {LY_STMT_ENUM, dest, 0},
822 {LY_STMT_ERROR_APP_TAG, dest, YIN_SUBELEM_UNIQUE},
823 {LY_STMT_ERROR_MESSAGE, dest, 0},
824 {LY_STMT_EXTENSION, dest, 0},
825 {LY_STMT_FEATURE, dest, 0},
826 {LY_STMT_FRACTION_DIGITS, dest, 0},
827 {LY_STMT_GROUPING, dest, 0},
828 {LY_STMT_IDENTITY, dest, 0},
829 {LY_STMT_IF_FEATURE, dest, 0},
830 {LY_STMT_IMPORT, dest, 0},
831 {LY_STMT_INCLUDE, dest, 0},
832 {LY_STMT_INPUT, dest, 0},
833 {LY_STMT_KEY, dest, YIN_SUBELEM_UNIQUE},
834 {LY_STMT_LEAF, dest, 0},
835 {LY_STMT_LEAF_LIST, dest, 0},
836 {LY_STMT_LENGTH, dest, 0},
837 {LY_STMT_LIST, dest, 0},
838 {LY_STMT_MANDATORY, dest, 0},
839 {LY_STMT_MAX_ELEMENTS, dest, 0},
840 {LY_STMT_MIN_ELEMENTS, dest, 0},
841 {LY_STMT_MODIFIER, dest, 0},
842 {LY_STMT_MODULE, dest, 0},
843 {LY_STMT_MUST, dest, 0},
844 {LY_STMT_NAMESPACE, dest, YIN_SUBELEM_UNIQUE},
845 {LY_STMT_NOTIFICATION, dest, 0},
846 {LY_STMT_ORDERED_BY, dest, 0},
847 {LY_STMT_ORGANIZATION, dest, 0},
848 {LY_STMT_OUTPUT, dest, 0},
849 {LY_STMT_PATH, dest, 0},
850 {LY_STMT_PATTERN, dest, 0},
851 {LY_STMT_POSITION, dest, 0},
852 {LY_STMT_PREFIX, dest, YIN_SUBELEM_UNIQUE},
853 {LY_STMT_PRESENCE, dest, YIN_SUBELEM_UNIQUE},
854 {LY_STMT_RANGE, dest, 0},
855 {LY_STMT_REFERENCE, dest, 0},
856 {LY_STMT_REFINE, dest, 0},
857 {LY_STMT_REQUIRE_INSTANCE, dest, 0},
858 {LY_STMT_REVISION, dest, 0},
859 {LY_STMT_REVISION_DATE, dest, 0},
860 {LY_STMT_RPC, dest, 0},
861 {LY_STMT_STATUS, dest, 0},
862 {LY_STMT_SUBMODULE, dest, 0},
863 {LY_STMT_TYPE, dest, 0},
864 {LY_STMT_TYPEDEF, dest, 0},
865 {LY_STMT_UNIQUE, dest, 0},
866 {LY_STMT_UNITS, dest, YIN_SUBELEM_UNIQUE},
867 {LY_STMT_USES, dest, 0},
868 {LY_STMT_VALUE, dest, 0},
869 {LY_STMT_WHEN, dest, 0},
870 {LY_STMT_YANG_VERSION, dest, 0},
871 {LY_STMT_YIN_ELEMENT, dest, 0},
872 {LY_STMT_EXTENSION_INSTANCE, dest, 0},
873 {LY_STMT_ARG_TEXT, dest, 0},
874 {LY_STMT_ARG_VALUE, dest, 0}
David Sedlák32488102019-07-15 17:44:10 +0200875 };
Michal Vasko63f3d842020-07-08 10:10:14 +0200876 ly_in_new_memory(data, &st->in);
877 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100878 prefix = st->yin_ctx->xmlctx->prefix;
879 prefix_len = st->yin_ctx->xmlctx->prefix_len;
880 name = st->yin_ctx->xmlctx->name;
881 name_len = st->yin_ctx->xmlctx->name_len;
882 lyxml_ctx_next(st->yin_ctx->xmlctx);
883
884 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);
885
Michal Vasko63f3d842020-07-08 10:10:14 +0200886 /* free parser and input */
Michal Vaskob36053d2020-03-26 15:49:30 +0100887 lyxml_ctx_free(st->yin_ctx->xmlctx);
888 st->yin_ctx->xmlctx = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200889 ly_in_free(st->in, 0);
890 st->in = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200891 return ret;
892}
893
David Sedlákd1144562019-08-06 12:36:14 +0200894#define EXT_SUBELEM "<myext:c-define name=\"MY_MTU\" xmlns:myext=\"urn:example:extensions\"/>"
895
David Sedlák32488102019-07-15 17:44:10 +0200896static void
David Sedlák43801c92019-08-05 15:58:54 +0200897test_enum_elem(void **state)
David Sedlák32488102019-07-15 17:44:10 +0200898{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200899 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +0200900 struct lysp_type type = {};
901 const char *data;
902 data = ELEMENT_WRAPPER_START
903 "<enum name=\"enum-name\">"
904 "<if-feature name=\"feature\" />"
905 "<value value=\"55\" />"
906 "<status value=\"deprecated\" />"
907 "<description><text>desc...</text></description>"
908 "<reference><text>ref...</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +0200909 EXT_SUBELEM
David Sedlák32488102019-07-15 17:44:10 +0200910 "</enum>"
911 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100912 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200913 assert_string_equal(type.enums->name, "enum-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +0200914 assert_string_equal(type.enums->iffeatures[0].str, "feature");
David Sedlák32488102019-07-15 17:44:10 +0200915 assert_int_equal(type.enums->value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200916 assert_true((type.enums->flags & LYS_STATUS_DEPRC) && (type.enums->flags & LYS_SET_VALUE));
David Sedlák32488102019-07-15 17:44:10 +0200917 assert_string_equal(type.enums->dsc, "desc...");
918 assert_string_equal(type.enums->ref, "ref...");
David Sedláke0ef1c62019-09-13 10:05:55 +0200919 assert_string_equal(type.enums->exts->name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +0200920 assert_int_equal(type.enums->exts->insubstmt_index, 0);
921 assert_int_equal(type.enums->exts->insubstmt, LYEXT_SUBSTMT_SELF);
922 lysp_type_free(st->ctx, &type);
923 memset(&type, 0, sizeof type);
924
925 data = ELEMENT_WRAPPER_START
926 "<enum name=\"enum-name\"></enum>"
927 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100928 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200929 assert_string_equal(type.enums->name, "enum-name");
David Sedlák32488102019-07-15 17:44:10 +0200930 lysp_type_free(st->ctx, &type);
931 memset(&type, 0, sizeof type);
932
David Sedlák43801c92019-08-05 15:58:54 +0200933 st->finished_correctly = true;
934}
935
936static void
937test_bit_elem(void **state)
938{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200939 struct test_parser_yin_state *st = *state;
David Sedlák43801c92019-08-05 15:58:54 +0200940 struct lysp_type type = {};
941 const char *data;
942 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200943 "<bit name=\"bit-name\">"
David Sedlák43801c92019-08-05 15:58:54 +0200944 "<if-feature name=\"feature\" />"
945 "<position value=\"55\" />"
946 "<status value=\"deprecated\" />"
947 "<description><text>desc...</text></description>"
948 "<reference><text>ref...</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +0200949 EXT_SUBELEM
David Sedlák43801c92019-08-05 15:58:54 +0200950 "</bit>"
951 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100952 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200953 assert_string_equal(type.bits->name, "bit-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +0200954 assert_string_equal(type.bits->iffeatures[0].str, "feature");
David Sedlák43801c92019-08-05 15:58:54 +0200955 assert_int_equal(type.bits->value, 55);
956 assert_true((type.bits->flags & LYS_STATUS_DEPRC) && (type.bits->flags & LYS_SET_VALUE));
957 assert_string_equal(type.bits->dsc, "desc...");
958 assert_string_equal(type.bits->ref, "ref...");
David Sedláke0ef1c62019-09-13 10:05:55 +0200959 assert_string_equal(type.bits->exts->name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +0200960 assert_int_equal(type.bits->exts->insubstmt_index, 0);
961 assert_int_equal(type.bits->exts->insubstmt, LYEXT_SUBSTMT_SELF);
962 lysp_type_free(st->ctx, &type);
963 memset(&type, 0, sizeof type);
964
965 data = ELEMENT_WRAPPER_START
966 "<bit name=\"bit-name\"> </bit>"
967 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100968 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200969 assert_string_equal(type.bits->name, "bit-name");
David Sedlák43801c92019-08-05 15:58:54 +0200970 lysp_type_free(st->ctx, &type);
971 memset(&type, 0, sizeof type);
972
David Sedlák32488102019-07-15 17:44:10 +0200973 st->finished_correctly = true;
974}
975
976static void
977test_meta_elem(void **state)
978{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200979 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +0200980 char *value = NULL;
981 const char *data;
David Sedlákd1144562019-08-06 12:36:14 +0200982 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200983
984 /* organization element */
985 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200986 "<organization><text>organization...</text>" EXT_SUBELEM EXT_SUBELEM "</organization>"
David Sedlák32488102019-07-15 17:44:10 +0200987 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100988 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_SUCCESS);
David Sedláke0ef1c62019-09-13 10:05:55 +0200989 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +0200990 assert_int_equal(exts[0].insubstmt_index, 0);
991 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ORGANIZATION);
David Sedláke0ef1c62019-09-13 10:05:55 +0200992 assert_string_equal(exts[1].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +0200993 assert_int_equal(exts[1].insubstmt_index, 0);
994 assert_int_equal(exts[1].insubstmt, LYEXT_SUBSTMT_ORGANIZATION);
David Sedlák32488102019-07-15 17:44:10 +0200995 assert_string_equal(value, "organization...");
996 FREE_STRING(st->ctx, value);
David Sedlákd1144562019-08-06 12:36:14 +0200997 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +0200998 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +0200999 exts = NULL;
1000
David Sedlák32488102019-07-15 17:44:10 +02001001 /* contact element */
1002 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +02001003 "<contact><text>contact...</text>" EXT_SUBELEM "</contact>"
David Sedlák32488102019-07-15 17:44:10 +02001004 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001005 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_SUCCESS);
David Sedláke0ef1c62019-09-13 10:05:55 +02001006 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001007 assert_int_equal(exts[0].insubstmt_index, 0);
1008 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_CONTACT);
David Sedlák32488102019-07-15 17:44:10 +02001009 assert_string_equal(value, "contact...");
1010 FREE_STRING(st->ctx, value);
David Sedlákd1144562019-08-06 12:36:14 +02001011 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1012 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001013 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001014
David Sedlák32488102019-07-15 17:44:10 +02001015 /* description element */
1016 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +02001017 "<description><text>description...</text>" EXT_SUBELEM "</description>"
David Sedlák32488102019-07-15 17:44:10 +02001018 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001019 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001020 assert_string_equal(value, "description...");
David Sedláke0ef1c62019-09-13 10:05:55 +02001021 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001022 assert_int_equal(exts[0].insubstmt_index, 0);
1023 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_DESCRIPTION);
David Sedlák32488102019-07-15 17:44:10 +02001024 FREE_STRING(st->ctx, value);
1025 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001026 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1027 exts = NULL;
1028
David Sedlák32488102019-07-15 17:44:10 +02001029 /* reference element */
1030 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +02001031 "<reference><text>reference...</text>" EXT_SUBELEM "</reference>"
David Sedlák32488102019-07-15 17:44:10 +02001032 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001033 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001034 assert_string_equal(value, "reference...");
David Sedláke0ef1c62019-09-13 10:05:55 +02001035 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001036 assert_int_equal(exts[0].insubstmt_index, 0);
1037 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_REFERENCE);
David Sedlák32488102019-07-15 17:44:10 +02001038 FREE_STRING(st->ctx, value);
1039 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001040 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1041 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001042
David Sedlákdf2a9732019-08-07 13:23:16 +02001043 /* reference element */
1044 data = ELEMENT_WRAPPER_START
1045 "<reference invalid=\"text\"><text>reference...</text>""</reference>"
1046 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001047 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001048 logbuf_assert("Unexpected attribute \"invalid\" of \"reference\" element. Line number 1.");
David Sedlákdf2a9732019-08-07 13:23:16 +02001049 FREE_STRING(st->ctx, value);
1050 value = NULL;
1051 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1052 exts = NULL;
1053
David Sedlák32488102019-07-15 17:44:10 +02001054 /* missing text subelement */
1055 data = ELEMENT_WRAPPER_START
1056 "<reference>reference...</reference>"
1057 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001058 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001059 logbuf_assert("Missing mandatory sub-element \"text\" of \"reference\" element. Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001060
David Sedlákd1144562019-08-06 12:36:14 +02001061 /* reference element */
1062 data = ELEMENT_WRAPPER_START
1063 "<reference>" EXT_SUBELEM "<text>reference...</text></reference>"
1064 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001065 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001066 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 +02001067 FREE_STRING(st->ctx, value);
1068 value = NULL;
1069 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1070 exts = NULL;
1071
David Sedlák32488102019-07-15 17:44:10 +02001072 st->finished_correctly = true;
1073}
1074
1075static void
1076test_import_elem(void **state)
1077{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001078 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001079 const char *data;
David Sedlák298ff6d2019-07-26 14:29:03 +02001080 struct lysp_import *imports = NULL;
1081 struct import_meta imp_meta = {"prefix", &imports};
David Sedlák32488102019-07-15 17:44:10 +02001082
1083 /* max subelems */
1084 data = ELEMENT_WRAPPER_START
1085 "<import module=\"a\">"
David Sedlákd1144562019-08-06 12:36:14 +02001086 EXT_SUBELEM
David Sedlák32488102019-07-15 17:44:10 +02001087 "<prefix value=\"a_mod\"/>"
1088 "<revision-date date=\"2015-01-01\"></revision-date>"
1089 "<description><text>import description</text></description>"
1090 "<reference><text>import reference</text></reference>"
1091 "</import>"
1092 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001093 assert_int_equal(test_element_helper(st, data, &imp_meta, NULL, NULL), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02001094 assert_string_equal(imports->name, "a");
1095 assert_string_equal(imports->prefix, "a_mod");
1096 assert_string_equal(imports->rev, "2015-01-01");
1097 assert_string_equal(imports->dsc, "import description");
1098 assert_string_equal(imports->ref, "import reference");
David Sedláke0ef1c62019-09-13 10:05:55 +02001099 assert_string_equal(imports->exts->name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001100 assert_int_equal(imports->exts->insubstmt, LYEXT_SUBSTMT_SELF);
1101 assert_int_equal(imports->exts->insubstmt_index, 0);
David Sedlák298ff6d2019-07-26 14:29:03 +02001102 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1103 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001104
1105 /* min subelems */
David Sedlák32488102019-07-15 17:44:10 +02001106 data = ELEMENT_WRAPPER_START
1107 "<import module=\"a\">"
1108 "<prefix value=\"a_mod\"/>"
1109 "</import>"
1110 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001111 assert_int_equal(test_element_helper(st, data, &imp_meta, NULL, NULL), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02001112 assert_string_equal(imports->prefix, "a_mod");
1113 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1114 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001115
1116 /* invalid (missing prefix) */
David Sedlák298ff6d2019-07-26 14:29:03 +02001117 data = ELEMENT_WRAPPER_START "<import module=\"a\"></import>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001118 assert_int_equal(test_element_helper(st, data, &imp_meta, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001119 logbuf_assert("Missing mandatory sub-element \"prefix\" of \"import\" element. Line number 1.");
David Sedlák298ff6d2019-07-26 14:29:03 +02001120 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1121 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001122
1123 /* invalid reused prefix */
David Sedlák32488102019-07-15 17:44:10 +02001124 data = ELEMENT_WRAPPER_START
1125 "<import module=\"a\">"
David Sedlák298ff6d2019-07-26 14:29:03 +02001126 "<prefix value=\"prefix\"/>"
David Sedlák32488102019-07-15 17:44:10 +02001127 "</import>"
1128 "<import module=\"a\">"
David Sedlák298ff6d2019-07-26 14:29:03 +02001129 "<prefix value=\"prefix\"/>"
David Sedlák32488102019-07-15 17:44:10 +02001130 "</import>"
1131 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001132 assert_int_equal(test_element_helper(st, data, &imp_meta, NULL, NULL), LY_EVALID);
David Sedlák298ff6d2019-07-26 14:29:03 +02001133 logbuf_assert("Prefix \"prefix\" already used as module prefix. Line number 1.");
1134 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1135 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001136
1137 st->finished_correctly = true;
1138}
1139
1140static void
1141test_status_elem(void **state)
1142{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001143 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001144 const char *data;
1145 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001146 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001147
1148 /* test valid values */
1149 data = ELEMENT_WRAPPER_START "<status value=\"current\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001150 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001151 assert_true(flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +02001152
1153 data = ELEMENT_WRAPPER_START "<status value=\"deprecated\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001154 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001155 assert_true(flags & LYS_STATUS_DEPRC);
David Sedlák32488102019-07-15 17:44:10 +02001156
David Sedlákd1144562019-08-06 12:36:14 +02001157 data = ELEMENT_WRAPPER_START "<status value=\"obsolete\">"EXT_SUBELEM"</status>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001158 assert_int_equal(test_element_helper(st, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001159 assert_true(flags & LYS_STATUS_OBSLT);
David Sedláke0ef1c62019-09-13 10:05:55 +02001160 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001161 assert_int_equal(exts[0].insubstmt_index, 0);
1162 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_STATUS);
1163 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1164 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001165
1166 /* test invalid value */
1167 data = ELEMENT_WRAPPER_START "<status value=\"invalid\"></status>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001168 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001169 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 +02001170 st->finished_correctly = true;
1171}
1172
1173static void
1174test_ext_elem(void **state)
1175{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001176 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001177 const char *data;
1178 struct lysp_ext *ext = NULL;
1179
1180 /* max subelems */
1181 data = ELEMENT_WRAPPER_START
1182 "<extension name=\"ext_name\">"
1183 "<argument name=\"arg\"></argument>"
1184 "<status value=\"current\"/>"
1185 "<description><text>ext_desc</text></description>"
1186 "<reference><text>ext_ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001187 EXT_SUBELEM
David Sedlák32488102019-07-15 17:44:10 +02001188 "</extension>"
1189 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001190 assert_int_equal(test_element_helper(st, data, &ext, NULL, NULL), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001191 assert_string_equal(ext->name, "ext_name");
1192 assert_string_equal(ext->argument, "arg");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001193 assert_true(ext->flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +02001194 assert_string_equal(ext->dsc, "ext_desc");
1195 assert_string_equal(ext->ref, "ext_ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02001196 assert_string_equal(ext->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001197 assert_int_equal(ext->exts[0].insubstmt_index, 0);
1198 assert_int_equal(ext->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák32488102019-07-15 17:44:10 +02001199 lysp_ext_free(st->ctx, ext);
1200 LY_ARRAY_FREE(ext);
1201 ext = NULL;
1202
1203 /* min subelems */
1204 data = ELEMENT_WRAPPER_START "<extension name=\"ext_name\"></extension>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001205 assert_int_equal(test_element_helper(st, data, &ext, NULL, NULL), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001206 assert_string_equal(ext->name, "ext_name");
1207 lysp_ext_free(st->ctx, ext);
1208 LY_ARRAY_FREE(ext);
1209 ext = NULL;
1210
1211 st->finished_correctly = true;
1212}
1213
1214static void
1215test_yin_element_elem(void **state)
1216{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001217 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001218 const char *data;
1219 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001220 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001221
1222 data = ELEMENT_WRAPPER_START "<yin-element value=\"true\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001223 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001224 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +02001225
David Sedlákd1144562019-08-06 12:36:14 +02001226 data = ELEMENT_WRAPPER_START "<yin-element value=\"false\">" EXT_SUBELEM "</yin-element>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001227 assert_int_equal(test_element_helper(st, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001228 assert_true(flags & LYS_YINELEM_TRUE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001229 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001230 assert_int_equal(exts[0].insubstmt_index, 0);
1231 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_YINELEM);
1232 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001233
1234 data = ELEMENT_WRAPPER_START "<yin-element value=\"invalid\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001235 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_EVALID);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001236 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák26ea1432019-08-14 13:42:23 +02001237 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 +02001238 st->finished_correctly = true;
1239}
1240
1241static void
1242test_yangversion_elem(void **state)
1243{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001244 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001245 const char *data;
1246 uint8_t version = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001247 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001248
1249 /* valid values */
Radek Krejci96e48da2020-09-04 13:18:06 +02001250 data = ELEMENT_WRAPPER_START "<yang-version value=\"1\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001251 assert_int_equal(test_element_helper(st, data, &version, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001252 assert_true(version & LYS_VERSION_1_0);
David Sedlák32488102019-07-15 17:44:10 +02001253
David Sedlákd1144562019-08-06 12:36:14 +02001254 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.1\">" EXT_SUBELEM "</yang-version>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001255 assert_int_equal(test_element_helper(st, data, &version, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001256 assert_true(version & LYS_VERSION_1_1);
David Sedláke0ef1c62019-09-13 10:05:55 +02001257 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001258 assert_int_equal(exts[0].insubstmt_index, 0);
1259 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_VERSION);
1260 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001261
1262 /* invalid value */
1263 data = ELEMENT_WRAPPER_START "<yang-version value=\"version\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001264 assert_int_equal(test_element_helper(st, data, &version, NULL, NULL), LY_EVALID);
Radek Krejci96e48da2020-09-04 13:18:06 +02001265 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 +02001266
1267 st->finished_correctly = true;
1268}
1269
1270static void
1271test_mandatory_elem(void **state)
1272{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001273 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001274 const char *data;
1275 uint16_t man = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001276 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001277
1278 /* valid values */
1279 data = ELEMENT_WRAPPER_START "<mandatory value=\"true\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001280 assert_int_equal(test_element_helper(st, data, &man, NULL, NULL), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001281 assert_int_equal(man, LYS_MAND_TRUE);
1282 man = 0;
1283
David Sedlákd1144562019-08-06 12:36:14 +02001284 data = ELEMENT_WRAPPER_START "<mandatory value=\"false\">" EXT_SUBELEM "</mandatory>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001285 assert_int_equal(test_element_helper(st, data, &man, NULL, &exts), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001286 assert_int_equal(man, LYS_MAND_FALSE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001287 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001288 assert_int_equal(exts[0].insubstmt_index, 0);
1289 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_MANDATORY);
1290 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001291
1292 data = ELEMENT_WRAPPER_START "<mandatory value=\"invalid\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001293 assert_int_equal(test_element_helper(st, data, &man, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001294 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 +02001295
1296 st->finished_correctly = true;
1297}
1298
David Sedlák8e7bda82019-07-16 17:57:50 +02001299static void
1300test_argument_elem(void **state)
1301{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001302 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001303 const char *data;
1304 uint16_t flags = 0;
1305 const char *arg;
1306 struct yin_argument_meta arg_meta = {&flags, &arg};
David Sedlákd1144562019-08-06 12:36:14 +02001307 struct lysp_ext_instance *exts = NULL;
1308
David Sedlák8e7bda82019-07-16 17:57:50 +02001309 /* max subelems */
1310 data = ELEMENT_WRAPPER_START
1311 "<argument name=\"arg-name\">"
1312 "<yin-element value=\"true\" />"
David Sedlákd1144562019-08-06 12:36:14 +02001313 EXT_SUBELEM
David Sedlák8e7bda82019-07-16 17:57:50 +02001314 "</argument>"
1315 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001316 assert_int_equal(test_element_helper(st, data, &arg_meta, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001317 assert_string_equal(arg, "arg-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001318 assert_true(flags & LYS_YINELEM_TRUE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001319 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001320 assert_int_equal(exts[0].insubstmt_index, 0);
1321 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ARGUMENT);
1322 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1323 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001324 flags = 0;
1325 FREE_STRING(st->ctx, arg);
1326 arg = NULL;
1327
1328 /* min subelems */
1329 data = ELEMENT_WRAPPER_START
1330 "<argument name=\"arg\">"
1331 "</argument>"
1332 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001333 assert_int_equal(test_element_helper(st, data, &arg_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001334 assert_string_equal(arg, "arg");
1335 assert_true(flags == 0);
1336 FREE_STRING(st->ctx, arg);
1337
1338 st->finished_correctly = true;
1339}
1340
1341static void
1342test_base_elem(void **state)
1343{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001344 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001345 const char *data;
1346 const char **bases = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001347 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001348 struct lysp_type type = {};
1349
1350 /* as identity subelement */
1351 data = "<identity xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlákd1144562019-08-06 12:36:14 +02001352 "<base name=\"base-name\">"
1353 EXT_SUBELEM
1354 "</base>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001355 "</identity>";
Michal Vaskob36053d2020-03-26 15:49:30 +01001356 assert_int_equal(test_element_helper(st, data, &bases, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001357 assert_string_equal(*bases, "base-name");
David Sedláke0ef1c62019-09-13 10:05:55 +02001358 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001359 assert_int_equal(exts[0].insubstmt_index, 0);
1360 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_BASE);
1361 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1362 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001363 FREE_STRING(st->ctx, *bases);
1364 LY_ARRAY_FREE(bases);
1365
1366 /* as type subelement */
1367 data = "<type xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlákd1144562019-08-06 12:36:14 +02001368 "<base name=\"base-name\">"
1369 EXT_SUBELEM
1370 "</base>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001371 "</type>";
Michal Vaskob36053d2020-03-26 15:49:30 +01001372 assert_int_equal(test_element_helper(st, data, &type, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001373 assert_string_equal(*type.bases, "base-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001374 assert_true(type.flags & LYS_SET_BASE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001375 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001376 assert_int_equal(exts[0].insubstmt_index, 0);
1377 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_BASE);
1378 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1379 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001380 FREE_STRING(st->ctx, *type.bases);
1381 LY_ARRAY_FREE(type.bases);
1382
1383 st->finished_correctly = true;
1384}
1385
1386static void
1387test_belongsto_elem(void **state)
1388{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001389 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001390 const char *data;
1391 struct lysp_submodule submod;
David Sedlákd1144562019-08-06 12:36:14 +02001392 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001393
1394 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +02001395 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/>"EXT_SUBELEM"</belongs-to>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001396 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001397 assert_int_equal(test_element_helper(st, data, &submod, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001398 assert_string_equal(submod.prefix, "pref");
David Sedláke0ef1c62019-09-13 10:05:55 +02001399 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001400 assert_int_equal(exts[0].insubstmt_index, 0);
1401 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_BELONGSTO);
1402 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1403 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001404 FREE_STRING(st->ctx, submod.prefix);
1405
1406 data = ELEMENT_WRAPPER_START "<belongs-to module=\"module-name\"></belongs-to>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001407 assert_int_equal(test_element_helper(st, data, &submod, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001408 logbuf_assert("Missing mandatory sub-element \"prefix\" of \"belongs-to\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001409
1410 st->finished_correctly = true;
1411}
1412
1413static void
1414test_config_elem(void **state)
1415{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001416 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001417 const char *data;
1418 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001419 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001420
David Sedlákd1144562019-08-06 12:36:14 +02001421 data = ELEMENT_WRAPPER_START "<config value=\"true\">" EXT_SUBELEM "</config>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001422 assert_int_equal(test_element_helper(st, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001423 assert_true(flags & LYS_CONFIG_W);
David Sedláke0ef1c62019-09-13 10:05:55 +02001424 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001425 assert_int_equal(exts[0].insubstmt_index, 0);
1426 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_CONFIG);
1427 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1428 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001429 flags = 0;
1430
1431 data = ELEMENT_WRAPPER_START "<config value=\"false\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001432 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001433 assert_true(flags & LYS_CONFIG_R);
David Sedlák8e7bda82019-07-16 17:57:50 +02001434 flags = 0;
1435
1436 data = ELEMENT_WRAPPER_START "<config value=\"invalid\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001437 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001438 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 +02001439
1440 st->finished_correctly = true;
1441}
1442
1443static void
1444test_default_elem(void **state)
1445{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001446 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001447 const char *data;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001448 struct lysp_qname val = {0};
David Sedlákd1144562019-08-06 12:36:14 +02001449 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001450
David Sedlákd1144562019-08-06 12:36:14 +02001451 data = ELEMENT_WRAPPER_START "<default value=\"defaul-value\">"EXT_SUBELEM"</default>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001452 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001453 assert_string_equal(val.str, "defaul-value");
David Sedláke0ef1c62019-09-13 10:05:55 +02001454 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001455 assert_int_equal(exts[0].insubstmt_index, 0);
1456 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_DEFAULT);
1457 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1458 exts = NULL;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001459 FREE_STRING(st->ctx, val.str);
1460 val.str = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001461
1462 data = ELEMENT_WRAPPER_START "<default/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001463 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001464 logbuf_assert("Missing mandatory attribute value of default element. Line number 1.");
1465
1466 st->finished_correctly = true;
1467}
1468
1469static void
1470test_err_app_tag_elem(void **state)
1471{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001472 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001473 const char *data;
1474 const char *val = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001475 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001476
David Sedlákd1144562019-08-06 12:36:14 +02001477 data = ELEMENT_WRAPPER_START "<error-app-tag value=\"val\">"EXT_SUBELEM"</error-app-tag>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001478 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001479 assert_string_equal(val, "val");
David Sedláke0ef1c62019-09-13 10:05:55 +02001480 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001481 assert_int_equal(exts[0].insubstmt_index, 0);
1482 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ERRTAG);
1483 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1484 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001485 FREE_STRING(st->ctx, val);
1486 val = NULL;
1487
1488 data = ELEMENT_WRAPPER_START "<error-app-tag/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001489 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001490 logbuf_assert("Missing mandatory attribute value of error-app-tag element. Line number 1.");
1491
1492 st->finished_correctly = true;
1493}
1494
1495static void
1496test_err_msg_elem(void **state)
1497{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001498 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001499 const char *data;
1500 const char *val = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001501 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001502
David Sedlákd1144562019-08-06 12:36:14 +02001503 data = ELEMENT_WRAPPER_START "<error-message><value>val</value>"EXT_SUBELEM"</error-message>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001504 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001505 assert_string_equal(val, "val");
David Sedláke0ef1c62019-09-13 10:05:55 +02001506 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001507 assert_int_equal(exts[0].insubstmt_index, 0);
1508 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ERRMSG);
1509 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1510 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001511 FREE_STRING(st->ctx, val);
1512
1513 data = ELEMENT_WRAPPER_START "<error-message></error-message>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001514 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001515 logbuf_assert("Missing mandatory sub-element \"value\" of \"error-message\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001516
David Sedlákdf2a9732019-08-07 13:23:16 +02001517 data = ELEMENT_WRAPPER_START "<error-message invalid=\"text\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001518 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001519 logbuf_assert("Unexpected attribute \"invalid\" of \"error-message\" element. Line number 1.");
David Sedlákdf2a9732019-08-07 13:23:16 +02001520
David Sedlák8e7bda82019-07-16 17:57:50 +02001521 st->finished_correctly = true;
1522}
1523
1524static void
1525test_fracdigits_elem(void **state)
1526{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001527 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001528 const char *data;
1529 struct lysp_type type = {};
1530
1531 /* valid value */
David Sedlákd1144562019-08-06 12:36:14 +02001532 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"10\">"EXT_SUBELEM"</fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001533 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedláke0ef1c62019-09-13 10:05:55 +02001534 assert_string_equal(type.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001535 assert_int_equal(type.exts[0].insubstmt_index, 0);
1536 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_FRACDIGITS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001537 assert_int_equal(type.fraction_digits, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001538 assert_true(type.flags & LYS_SET_FRDIGITS);
David Sedlákd1144562019-08-06 12:36:14 +02001539 FREE_ARRAY(st->ctx, type.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001540
1541 /* invalid values */
1542 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"-1\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001543 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001544 logbuf_assert("Invalid value \"-1\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001545
1546 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"02\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001547 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001548 logbuf_assert("Invalid value \"02\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001549
1550 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"1p\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001551 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001552 logbuf_assert("Invalid value \"1p\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001553
1554 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"19\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001555 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001556 logbuf_assert("Invalid value \"19\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001557
1558 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"999999999999999999\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001559 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001560 logbuf_assert("Invalid value \"999999999999999999\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001561
1562 st->finished_correctly = true;
1563}
1564
1565static void
1566test_iffeature_elem(void **state)
1567{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001568 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001569 const char *data;
1570 const char **iffeatures = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001571 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001572
David Sedlákd1144562019-08-06 12:36:14 +02001573 data = ELEMENT_WRAPPER_START "<if-feature name=\"local-storage\">"EXT_SUBELEM"</if-feature>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001574 assert_int_equal(test_element_helper(st, data, &iffeatures, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001575 assert_string_equal(*iffeatures, "local-storage");
David Sedláke0ef1c62019-09-13 10:05:55 +02001576 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001577 assert_int_equal(exts[0].insubstmt_index, 0);
1578 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_IFFEATURE);
1579 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1580 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001581 FREE_STRING(st->ctx, *iffeatures);
1582 LY_ARRAY_FREE(iffeatures);
1583 iffeatures = NULL;
1584
1585 data = ELEMENT_WRAPPER_START "<if-feature/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001586 assert_int_equal(test_element_helper(st, data, &iffeatures, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001587 logbuf_assert("Missing mandatory attribute name of if-feature element. Line number 1.");
1588 LY_ARRAY_FREE(iffeatures);
1589 iffeatures = NULL;
1590
1591 st->finished_correctly = true;
1592}
1593
1594static void
1595test_length_elem(void **state)
1596{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001597 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001598 const char *data;
1599 struct lysp_type type = {};
1600
1601 /* max subelems */
1602 data = ELEMENT_WRAPPER_START
1603 "<length value=\"length-str\">"
1604 "<error-message><value>err-msg</value></error-message>"
1605 "<error-app-tag value=\"err-app-tag\"/>"
1606 "<description><text>desc</text></description>"
1607 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001608 EXT_SUBELEM
David Sedlák8e7bda82019-07-16 17:57:50 +02001609 "</length>"
1610 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001611 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001612 assert_string_equal(type.length->arg.str, "length-str");
David Sedlák8e7bda82019-07-16 17:57:50 +02001613 assert_string_equal(type.length->emsg, "err-msg");
1614 assert_string_equal(type.length->eapptag, "err-app-tag");
1615 assert_string_equal(type.length->dsc, "desc");
1616 assert_string_equal(type.length->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001617 assert_true(type.flags & LYS_SET_LENGTH);
David Sedláke0ef1c62019-09-13 10:05:55 +02001618 assert_string_equal(type.length->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001619 assert_int_equal(type.length->exts[0].insubstmt_index, 0);
1620 assert_int_equal(type.length->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8e7bda82019-07-16 17:57:50 +02001621 lysp_type_free(st->ctx, &type);
1622 memset(&type, 0, sizeof(type));
1623
1624 /* min subelems */
1625 data = ELEMENT_WRAPPER_START
1626 "<length value=\"length-str\">"
1627 "</length>"
1628 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001629 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001630 assert_string_equal(type.length->arg.str, "length-str");
David Sedlák8e7bda82019-07-16 17:57:50 +02001631 lysp_type_free(st->ctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001632 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001633 memset(&type, 0, sizeof(type));
1634
1635 data = ELEMENT_WRAPPER_START "<length></length>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001636 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001637 logbuf_assert("Missing mandatory attribute value of length element. Line number 1.");
1638 lysp_type_free(st->ctx, &type);
1639 memset(&type, 0, sizeof(type));
1640
1641 st->finished_correctly = true;
1642}
1643
1644static void
1645test_modifier_elem(void **state)
1646{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001647 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001648 const char *data;
Radek Krejci011e4aa2020-09-04 15:22:31 +02001649 const char *pat;
David Sedlákd1144562019-08-06 12:36:14 +02001650 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001651
Radek Krejci011e4aa2020-09-04 15:22:31 +02001652 assert_int_equal(LY_SUCCESS, lydict_insert(st->ctx, "\006pattern", 8, &pat));
David Sedlákd1144562019-08-06 12:36:14 +02001653 data = ELEMENT_WRAPPER_START "<modifier value=\"invert-match\">" EXT_SUBELEM "</modifier>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001654 assert_int_equal(test_element_helper(st, data, &pat, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001655 assert_string_equal(pat, "\x015pattern");
David Sedláke0ef1c62019-09-13 10:05:55 +02001656 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001657 assert_int_equal(exts[0].insubstmt_index, 0);
1658 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_MODIFIER);
1659 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1660 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001661 FREE_STRING(st->ctx, pat);
1662
Radek Krejci011e4aa2020-09-04 15:22:31 +02001663 assert_int_equal(LY_SUCCESS, lydict_insert(st->ctx, "\006pattern", 8, &pat));
David Sedlák8e7bda82019-07-16 17:57:50 +02001664 data = ELEMENT_WRAPPER_START "<modifier value=\"invert\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001665 assert_int_equal(test_element_helper(st, data, &pat, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001666 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 +02001667 FREE_STRING(st->ctx, pat);
1668
1669 st->finished_correctly = true;
1670}
1671
1672static void
1673test_namespace_elem(void **state)
1674{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001675 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001676 const char *data;
1677 const char *ns;
David Sedlákd1144562019-08-06 12:36:14 +02001678 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001679
David Sedlákd1144562019-08-06 12:36:14 +02001680 data = ELEMENT_WRAPPER_START "<namespace uri=\"ns\">" EXT_SUBELEM "</namespace>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001681 assert_int_equal(test_element_helper(st, data, &ns, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001682 assert_string_equal(ns, "ns");
David Sedláke0ef1c62019-09-13 10:05:55 +02001683 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001684 assert_int_equal(exts[0].insubstmt_index, 0);
1685 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_NAMESPACE);
1686 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1687 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001688 FREE_STRING(st->ctx, ns);
1689
1690 data = ELEMENT_WRAPPER_START "<namespace/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001691 assert_int_equal(test_element_helper(st, data, &ns, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001692 logbuf_assert("Missing mandatory attribute uri of namespace element. Line number 1.");
1693
1694 st->finished_correctly = true;
1695}
1696
1697static void
David Sedlák8e7bda82019-07-16 17:57:50 +02001698test_pattern_elem(void **state)
1699{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001700 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001701 const char *data;
1702 struct lysp_type type = {};
1703
1704 /* max subelems */
1705 data = ELEMENT_WRAPPER_START
1706 "<pattern value=\"super_pattern\">"
1707 "<modifier value=\"invert-match\"/>"
1708 "<error-message><value>err-msg-value</value></error-message>"
1709 "<error-app-tag value=\"err-app-tag-value\"/>"
David Sedlák169cc522019-08-15 13:23:45 +02001710 "<description><text>&quot;pattern-desc&quot;</text></description>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001711 "<reference><text>pattern-ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001712 EXT_SUBELEM
David Sedlák8e7bda82019-07-16 17:57:50 +02001713 "</pattern>"
1714 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001715 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001716 assert_true(type.flags & LYS_SET_PATTERN);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001717 assert_string_equal(type.patterns->arg.str, "\x015super_pattern");
David Sedlák169cc522019-08-15 13:23:45 +02001718 assert_string_equal(type.patterns->dsc, "\"pattern-desc\"");
David Sedlák8e7bda82019-07-16 17:57:50 +02001719 assert_string_equal(type.patterns->eapptag, "err-app-tag-value");
1720 assert_string_equal(type.patterns->emsg, "err-msg-value");
David Sedlák8e7bda82019-07-16 17:57:50 +02001721 assert_string_equal(type.patterns->ref, "pattern-ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02001722 assert_string_equal(type.patterns->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001723 assert_int_equal(type.patterns->exts[0].insubstmt_index, 0);
1724 assert_int_equal(type.patterns->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8e7bda82019-07-16 17:57:50 +02001725 lysp_type_free(st->ctx, &type);
1726 memset(&type, 0, sizeof(type));
1727
1728 /* min subelems */
1729 data = ELEMENT_WRAPPER_START "<pattern value=\"pattern\"> </pattern>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001730 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001731 assert_string_equal(type.patterns->arg.str, "\x006pattern");
David Sedlák8e7bda82019-07-16 17:57:50 +02001732 lysp_type_free(st->ctx, &type);
1733 memset(&type, 0, sizeof(type));
1734
1735 st->finished_correctly = true;
1736}
1737
1738static void
1739test_value_position_elem(void **state)
1740{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001741 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001742 const char *data;
1743 struct lysp_type_enum en = {};
1744
1745 /* valid values */
David Sedlákd1144562019-08-06 12:36:14 +02001746 data = ELEMENT_WRAPPER_START "<value value=\"55\">" EXT_SUBELEM "</value>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001747 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001748 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001749 assert_true(en.flags & LYS_SET_VALUE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001750 assert_string_equal(en.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001751 assert_int_equal(en.exts[0].insubstmt_index, 0);
1752 assert_int_equal(en.exts[0].insubstmt, LYEXT_SUBSTMT_VALUE);
1753 FREE_ARRAY(st->ctx, en.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001754 memset(&en, 0, sizeof(en));
1755
1756 data = ELEMENT_WRAPPER_START "<value value=\"-55\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001757 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001758 assert_int_equal(en.value, -55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001759 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001760 memset(&en, 0, sizeof(en));
1761
1762 data = ELEMENT_WRAPPER_START "<value value=\"0\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001763 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001764 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001765 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001766 memset(&en, 0, sizeof(en));
1767
1768 data = ELEMENT_WRAPPER_START "<value value=\"-0\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001769 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001770 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001771 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001772 memset(&en, 0, sizeof(en));
1773
1774 /* valid positions */
David Sedlák8d552d62019-08-06 15:29:05 +02001775 data = ELEMENT_WRAPPER_START "<position value=\"55\">" EXT_SUBELEM "</position>" 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áke0ef1c62019-09-13 10:05:55 +02001779 assert_string_equal(en.exts[0].name, "urn:example:extensions:c-define");
David Sedlák8d552d62019-08-06 15:29:05 +02001780 assert_int_equal(en.exts[0].insubstmt_index, 0);
1781 assert_int_equal(en.exts[0].insubstmt, LYEXT_SUBSTMT_POSITION);
1782 FREE_ARRAY(st->ctx, en.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001783 memset(&en, 0, sizeof(en));
1784
1785 data = ELEMENT_WRAPPER_START "<position value=\"0\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001786 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001787 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001788 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001789 memset(&en, 0, sizeof(en));
1790
1791 /* invalid values */
1792 data = ELEMENT_WRAPPER_START "<value value=\"99999999999999999999999\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001793 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001794 logbuf_assert("Invalid value \"99999999999999999999999\" of \"value\" attribute in \"value\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001795
1796 data = ELEMENT_WRAPPER_START "<value value=\"1k\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001797 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001798 logbuf_assert("Invalid value \"1k\" of \"value\" attribute in \"value\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001799
David Sedlák69f01612019-07-17 11:41:08 +02001800 data = ELEMENT_WRAPPER_START "<value value=\"\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001801 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001802 logbuf_assert("Invalid value \"\" of \"value\" attribute in \"value\" element. Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001803
David Sedlák8e7bda82019-07-16 17:57:50 +02001804 /*invalid positions */
1805 data = ELEMENT_WRAPPER_START "<position value=\"-5\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001806 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001807 logbuf_assert("Invalid value \"-5\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001808
1809 data = ELEMENT_WRAPPER_START "<position value=\"-0\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001810 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001811 logbuf_assert("Invalid value \"-0\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001812
1813 data = ELEMENT_WRAPPER_START "<position value=\"99999999999999999999\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001814 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001815 logbuf_assert("Invalid value \"99999999999999999999\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001816
David Sedlák69f01612019-07-17 11:41:08 +02001817 data = ELEMENT_WRAPPER_START "<position value=\"\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001818 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001819 logbuf_assert("Invalid value \"\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001820
1821 st->finished_correctly = true;
1822}
1823
1824static void
1825test_prefix_elem(void **state)
1826{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001827 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001828 const char *data;
1829 const char *value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001830 struct lysp_ext_instance *exts = NULL;
1831
1832 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\">" EXT_SUBELEM "</prefix>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001833 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001834 assert_string_equal(value, "pref");
David Sedláke0ef1c62019-09-13 10:05:55 +02001835 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001836 assert_int_equal(exts[0].insubstmt_index, 0);
1837 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_PREFIX);
1838 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1839 exts = NULL;
1840 FREE_STRING(st->ctx, value);
David Sedlák69f01612019-07-17 11:41:08 +02001841
1842 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001843 assert_int_equal(test_element_helper(st, data, &value, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001844 assert_string_equal(value, "pref");
1845 FREE_STRING(st->ctx, value);
1846
1847 st->finished_correctly = true;
1848}
1849
1850static void
1851test_range_elem(void **state)
1852{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001853 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001854 const char *data;
1855 struct lysp_type type = {};
1856
1857 /* max subelems */
1858 data = ELEMENT_WRAPPER_START
1859 "<range value=\"range-str\">"
1860 "<error-message><value>err-msg</value></error-message>"
1861 "<error-app-tag value=\"err-app-tag\" />"
1862 "<description><text>desc</text></description>"
1863 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001864 EXT_SUBELEM
David Sedlák69f01612019-07-17 11:41:08 +02001865 "</range>"
1866 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001867 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001868 assert_string_equal(type.range->arg.str, "range-str");
David Sedlák69f01612019-07-17 11:41:08 +02001869 assert_string_equal(type.range->dsc, "desc");
1870 assert_string_equal(type.range->eapptag, "err-app-tag");
1871 assert_string_equal(type.range->emsg, "err-msg");
1872 assert_string_equal(type.range->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001873 assert_true(type.flags & LYS_SET_RANGE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001874 assert_string_equal(type.range->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001875 assert_int_equal(type.range->exts[0].insubstmt_index, 0);
1876 assert_int_equal(type.range->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák69f01612019-07-17 11:41:08 +02001877 lysp_type_free(st->ctx, &type);
1878 memset(&type, 0, sizeof(type));
1879
1880 /* min subelems */
1881 data = ELEMENT_WRAPPER_START "<range value=\"range-str\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001882 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001883 assert_string_equal(type.range->arg.str, "range-str");
David Sedlák69f01612019-07-17 11:41:08 +02001884 lysp_type_free(st->ctx, &type);
1885 memset(&type, 0, sizeof(type));
1886
1887 st->finished_correctly = true;
1888}
1889
1890static void
1891test_reqinstance_elem(void **state)
1892{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001893 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001894 const char *data;
1895 struct lysp_type type = {};
1896
David Sedlákd1144562019-08-06 12:36:14 +02001897 data = ELEMENT_WRAPPER_START "<require-instance value=\"true\">" EXT_SUBELEM "</require-instance>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001898 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001899 assert_int_equal(type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001900 assert_true(type.flags & LYS_SET_REQINST);
David Sedláke0ef1c62019-09-13 10:05:55 +02001901 assert_string_equal(type.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001902 assert_int_equal(type.exts[0].insubstmt_index, 0);
1903 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_REQINSTANCE);
1904 lysp_type_free(st->ctx, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001905 memset(&type, 0, sizeof(type));
1906
1907 data = ELEMENT_WRAPPER_START "<require-instance value=\"false\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001908 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001909 assert_int_equal(type.require_instance, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001910 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001911 memset(&type, 0, sizeof(type));
1912
1913 data = ELEMENT_WRAPPER_START "<require-instance value=\"invalid\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001914 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák69f01612019-07-17 11:41:08 +02001915 memset(&type, 0, sizeof(type));
David Sedlák26ea1432019-08-14 13:42:23 +02001916 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 +02001917
1918 st->finished_correctly = true;
1919}
1920
1921static void
1922test_revision_date_elem(void **state)
1923{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001924 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001925 const char *data;
1926 char rev[LY_REV_SIZE];
David Sedlákd1144562019-08-06 12:36:14 +02001927 struct lysp_ext_instance *exts = NULL;
1928
1929 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\">"EXT_SUBELEM"</revision-date>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001930 assert_int_equal(test_element_helper(st, data, rev, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001931 assert_string_equal(rev, "2000-01-01");
David Sedláke0ef1c62019-09-13 10:05:55 +02001932 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001933 assert_int_equal(exts[0].insubstmt_index, 0);
1934 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_REVISIONDATE);
1935 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák69f01612019-07-17 11:41:08 +02001936
1937 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001938 assert_int_equal(test_element_helper(st, data, rev, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001939 assert_string_equal(rev, "2000-01-01");
1940
1941 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-50-05\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001942 assert_int_equal(test_element_helper(st, data, rev, NULL, NULL), LY_EVALID);
David Sedlák69f01612019-07-17 11:41:08 +02001943 logbuf_assert("Invalid value \"2000-50-05\" of \"revision-date\". Line number 1.");
1944
1945 st->finished_correctly = true;
1946}
1947
1948static void
1949test_unique_elem(void **state)
1950{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001951 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001952 const char *data;
1953 const char **values = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001954 struct lysp_ext_instance *exts = NULL;
1955
1956 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\">"EXT_SUBELEM"</unique>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001957 assert_int_equal(test_element_helper(st, data, &values, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001958 assert_string_equal(*values, "tag");
David Sedláke0ef1c62019-09-13 10:05:55 +02001959 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001960 assert_int_equal(exts[0].insubstmt_index, 0);
1961 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_UNIQUE);
1962 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1963 FREE_STRING(st->ctx, *values);
1964 LY_ARRAY_FREE(values);
1965 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001966
1967 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001968 assert_int_equal(test_element_helper(st, data, &values, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001969 assert_string_equal(*values, "tag");
1970 FREE_STRING(st->ctx, *values);
1971 LY_ARRAY_FREE(values);
David Sedlákd1144562019-08-06 12:36:14 +02001972 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001973
1974 st->finished_correctly = true;
1975}
1976
1977static void
1978test_units_elem(void **state)
1979{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001980 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001981 const char *data;
1982 const char *values = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001983 struct lysp_ext_instance *exts = NULL;
1984
1985 data = ELEMENT_WRAPPER_START "<units name=\"name\">"EXT_SUBELEM"</units>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001986 assert_int_equal(test_element_helper(st, data, &values, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001987 assert_string_equal(values, "name");
David Sedláke0ef1c62019-09-13 10:05:55 +02001988 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001989 assert_int_equal(exts[0].insubstmt_index, 0);
1990 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_UNITS);
1991 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1992 FREE_STRING(st->ctx, values);
1993 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001994
1995 data = ELEMENT_WRAPPER_START "<units name=\"name\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001996 assert_int_equal(test_element_helper(st, data, &values, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001997 assert_string_equal(values, "name");
1998 FREE_STRING(st->ctx, values);
David Sedlákd1144562019-08-06 12:36:14 +02001999 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02002000
2001 st->finished_correctly = true;
2002}
2003
2004static void
2005test_when_elem(void **state)
2006{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002007 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02002008 const char *data;
2009 struct lysp_when *when = NULL;
2010
2011 data = ELEMENT_WRAPPER_START
2012 "<when condition=\"cond\">"
2013 "<description><text>desc</text></description>"
2014 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02002015 EXT_SUBELEM
David Sedlák69f01612019-07-17 11:41:08 +02002016 "</when>"
2017 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002018 assert_int_equal(test_element_helper(st, data, &when, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002019 assert_string_equal(when->cond, "cond");
2020 assert_string_equal(when->dsc, "desc");
2021 assert_string_equal(when->ref, "ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02002022 assert_string_equal(when->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002023 assert_int_equal(when->exts[0].insubstmt_index, 0);
2024 assert_int_equal(when->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák69f01612019-07-17 11:41:08 +02002025 lysp_when_free(st->ctx, when);
2026 free(when);
2027 when = NULL;
2028
2029 data = ELEMENT_WRAPPER_START "<when condition=\"cond\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002030 assert_int_equal(test_element_helper(st, data, &when, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002031 assert_string_equal(when->cond, "cond");
2032 lysp_when_free(st->ctx, when);
2033 free(when);
2034 when = NULL;
2035
2036 st->finished_correctly = true;
2037}
2038
2039static void
2040test_yin_text_value_elem(void **state)
2041{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002042 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02002043 const char *data;
2044 const char *val;
2045
2046 data = ELEMENT_WRAPPER_START "<text>text</text>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002047 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002048 assert_string_equal(val, "text");
2049 FREE_STRING(st->ctx, val);
2050
2051 data = "<error-message xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <value>text</value> </error-message>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002052 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002053 assert_string_equal(val, "text");
2054 FREE_STRING(st->ctx, val);
2055
2056 data = ELEMENT_WRAPPER_START "<text></text>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002057 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002058 assert_string_equal("", val);
2059 FREE_STRING(st->ctx, val);
2060
David Sedlák8e7bda82019-07-16 17:57:50 +02002061 st->finished_correctly = true;
2062}
David Sedlák32488102019-07-15 17:44:10 +02002063
David Sedlák374d2b32019-07-17 15:06:55 +02002064static void
2065test_type_elem(void **state)
2066{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002067 struct test_parser_yin_state *st = *state;
David Sedlák374d2b32019-07-17 15:06:55 +02002068 const char *data;
2069 struct lysp_type type = {};
2070
2071 /* max subelems */
2072 data = ELEMENT_WRAPPER_START
2073 "<type name=\"type-name\">"
2074 "<base name=\"base-name\"/>"
2075 "<bit name=\"bit\"/>"
2076 "<enum name=\"enum\"/>"
2077 "<fraction-digits value=\"2\"/>"
2078 "<length value=\"length\"/>"
Michal Vaskocb8c6d42020-10-16 11:58:30 +02002079 "<path value=\"/path\"/>"
David Sedlák374d2b32019-07-17 15:06:55 +02002080 "<pattern value=\"pattern\"/>"
2081 "<range value=\"range\" />"
2082 "<require-instance value=\"true\"/>"
2083 "<type name=\"sub-type-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002084 EXT_SUBELEM
David Sedlák374d2b32019-07-17 15:06:55 +02002085 "</type>"
2086 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002087 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák374d2b32019-07-17 15:06:55 +02002088 assert_string_equal(type.name, "type-name");
2089 assert_string_equal(*type.bases, "base-name");
2090 assert_string_equal(type.bits->name, "bit");
2091 assert_string_equal(type.enums->name, "enum");
2092 assert_int_equal(type.fraction_digits, 2);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002093 assert_string_equal(type.length->arg.str, "length");
Michal Vaskocb8c6d42020-10-16 11:58:30 +02002094 assert_string_equal(type.path->expr, "/path");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002095 assert_string_equal(type.patterns->arg.str, "\006pattern");
2096 assert_string_equal(type.range->arg.str, "range");
David Sedlák374d2b32019-07-17 15:06:55 +02002097 assert_int_equal(type.require_instance, 1);
2098 assert_string_equal(type.types->name, "sub-type-name");
David Sedláke0ef1c62019-09-13 10:05:55 +02002099 assert_string_equal(type.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002100 assert_int_equal(type.exts[0].insubstmt_index, 0);
2101 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002102 assert_true(type.flags & LYS_SET_BASE);
2103 assert_true(type.flags & LYS_SET_BIT);
2104 assert_true(type.flags & LYS_SET_ENUM);
2105 assert_true(type.flags & LYS_SET_FRDIGITS);
2106 assert_true(type.flags & LYS_SET_LENGTH);
2107 assert_true(type.flags & LYS_SET_PATH);
2108 assert_true(type.flags & LYS_SET_PATTERN);
2109 assert_true(type.flags & LYS_SET_RANGE);
2110 assert_true(type.flags & LYS_SET_REQINST);
2111 assert_true(type.flags & LYS_SET_TYPE);
David Sedlákd1144562019-08-06 12:36:14 +02002112 lysp_type_free(st->ctx, &type);
David Sedlák374d2b32019-07-17 15:06:55 +02002113 memset(&type, 0, sizeof(type));
2114
2115 /* min subelems */
2116 data = ELEMENT_WRAPPER_START "<type name=\"type-name\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002117 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák374d2b32019-07-17 15:06:55 +02002118 lysp_type_free(st->ctx, &type);
2119 memset(&type, 0, sizeof(type));
2120
2121 st->finished_correctly = true;
2122}
2123
David Sedlák1af868e2019-07-17 17:03:14 +02002124static void
2125test_max_elems_elem(void **state)
2126{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002127 struct test_parser_yin_state *st = *state;
David Sedlák1af868e2019-07-17 17:03:14 +02002128 const char *data;
2129 struct lysp_node_list list = {};
2130 struct lysp_node_leaflist llist = {};
2131 struct lysp_refine refine = {};
2132
David Sedlákd1144562019-08-06 12:36:14 +02002133 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 +01002134 assert_int_equal(test_element_helper(st, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02002135 assert_int_equal(refine.max, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002136 assert_true(refine.flags & LYS_SET_MAX);
David Sedláke0ef1c62019-09-13 10:05:55 +02002137 assert_string_equal(refine.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002138 assert_int_equal(refine.exts[0].insubstmt_index, 0);
2139 assert_int_equal(refine.exts[0].insubstmt, LYEXT_SUBSTMT_MAX);
2140 FREE_ARRAY(st->ctx, refine.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02002141
David Sedlákd1144562019-08-06 12:36:14 +02002142 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 +01002143 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02002144 assert_int_equal(list.max, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002145 assert_true(list.flags & LYS_SET_MAX);
David Sedláke0ef1c62019-09-13 10:05:55 +02002146 assert_string_equal(list.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002147 assert_int_equal(list.exts[0].insubstmt_index, 0);
2148 assert_int_equal(list.exts[0].insubstmt, LYEXT_SUBSTMT_MAX);
2149 FREE_ARRAY(st->ctx, list.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02002150
David Sedlákd1144562019-08-06 12:36:14 +02002151 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 +01002152 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02002153 assert_int_equal(llist.max, 85);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002154 assert_true(llist.flags & LYS_SET_MAX);
David Sedláke0ef1c62019-09-13 10:05:55 +02002155 assert_string_equal(llist.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002156 assert_int_equal(llist.exts[0].insubstmt_index, 0);
2157 assert_int_equal(llist.exts[0].insubstmt, LYEXT_SUBSTMT_MAX);
2158 FREE_ARRAY(st->ctx, llist.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02002159
2160 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"10\"/> </refine>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002161 assert_int_equal(test_element_helper(st, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02002162 assert_int_equal(refine.max, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002163 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02002164
2165 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"0\"/> </list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002166 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002167 logbuf_assert("Invalid value \"0\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002168
2169 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"-10\"/> </list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002170 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002171 logbuf_assert("Invalid value \"-10\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002172
2173 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"k\"/> </list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002174 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002175 logbuf_assert("Invalid value \"k\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002176
2177 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"u12\"/> </list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002178 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002179 logbuf_assert("Invalid value \"u12\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002180
2181 st->finished_correctly = true;
2182}
2183
David Sedlák09e18c92019-07-18 11:17:11 +02002184static void
2185test_min_elems_elem(void **state)
2186{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002187 struct test_parser_yin_state *st = *state;
David Sedlák09e18c92019-07-18 11:17:11 +02002188 const char *data;
2189 struct lysp_node_list list = {};
2190 struct lysp_node_leaflist llist = {};
2191 struct lysp_refine refine = {};
2192
David Sedlákd1144562019-08-06 12:36:14 +02002193 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 +01002194 assert_int_equal(test_element_helper(st, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák09e18c92019-07-18 11:17:11 +02002195 assert_int_equal(refine.min, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002196 assert_true(refine.flags & LYS_SET_MIN);
David Sedláke0ef1c62019-09-13 10:05:55 +02002197 assert_string_equal(refine.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002198 assert_int_equal(refine.exts[0].insubstmt_index, 0);
2199 assert_int_equal(refine.exts[0].insubstmt, LYEXT_SUBSTMT_MIN);
2200 FREE_ARRAY(st->ctx, refine.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02002201
David Sedlákd1144562019-08-06 12:36:14 +02002202 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 +01002203 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_SUCCESS);
David Sedlák09e18c92019-07-18 11:17:11 +02002204 assert_int_equal(list.min, 41);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002205 assert_true(list.flags & LYS_SET_MIN);
David Sedláke0ef1c62019-09-13 10:05:55 +02002206 assert_string_equal(list.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002207 assert_int_equal(list.exts[0].insubstmt_index, 0);
2208 assert_int_equal(list.exts[0].insubstmt, LYEXT_SUBSTMT_MIN);
2209 FREE_ARRAY(st->ctx, list.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02002210
David Sedlákd1144562019-08-06 12:36:14 +02002211 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 +01002212 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_SUCCESS);
David Sedlák09e18c92019-07-18 11:17:11 +02002213 assert_int_equal(llist.min, 50);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002214 assert_true(llist.flags & LYS_SET_MIN);
David Sedláke0ef1c62019-09-13 10:05:55 +02002215 assert_string_equal(llist.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002216 assert_int_equal(llist.exts[0].insubstmt_index, 0);
2217 assert_int_equal(llist.exts[0].insubstmt, LYEXT_SUBSTMT_MIN);
2218 FREE_ARRAY(st->ctx, llist.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02002219
2220 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 +01002221 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002222 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 +02002223
2224 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 +01002225 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002226 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 +02002227
2228 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 +01002229 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002230 logbuf_assert("Invalid value \"5k\" of \"value\" attribute in \"min-elements\" element. Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02002231
2232 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 +01002233 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002234 logbuf_assert("Invalid value \"05\" of \"value\" attribute in \"min-elements\" element. Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02002235
2236 st->finished_correctly = true;
2237}
2238
David Sedláka2dad212019-07-18 12:45:19 +02002239static void
2240test_ordby_elem(void **state)
2241{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002242 struct test_parser_yin_state *st = *state;
David Sedláka2dad212019-07-18 12:45:19 +02002243 const char *data;
2244 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02002245 struct lysp_ext_instance *exts = NULL;
David Sedláka2dad212019-07-18 12:45:19 +02002246
David Sedlákd1144562019-08-06 12:36:14 +02002247 data = ELEMENT_WRAPPER_START "<ordered-by value=\"system\">"EXT_SUBELEM"</ordered-by>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002248 assert_int_equal(test_element_helper(st, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002249 assert_true(flags & LYS_ORDBY_SYSTEM);
David Sedláke0ef1c62019-09-13 10:05:55 +02002250 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002251 assert_int_equal(exts[0].insubstmt_index, 0);
2252 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ORDEREDBY);
2253 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedláka2dad212019-07-18 12:45:19 +02002254
2255 data = ELEMENT_WRAPPER_START "<ordered-by value=\"user\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002256 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002257 assert_true(flags & LYS_ORDBY_USER);
David Sedláka2dad212019-07-18 12:45:19 +02002258
2259 data = ELEMENT_WRAPPER_START "<ordered-by value=\"inv\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002260 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02002261 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 +02002262
2263 st->finished_correctly = true;
2264}
2265
David Sedlák8a83bbb2019-07-18 14:46:00 +02002266static void
2267test_any_elem(void **state)
2268{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002269 struct test_parser_yin_state *st = *state;
David Sedlák8a83bbb2019-07-18 14:46:00 +02002270 const char *data;
2271 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002272 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák8a83bbb2019-07-18 14:46:00 +02002273 struct lysp_node_anydata *parsed = NULL;
2274
2275 /* anyxml max subelems */
2276 data = ELEMENT_WRAPPER_START
2277 "<anyxml name=\"any-name\">"
2278 "<config value=\"true\" />"
2279 "<description><text>desc</text></description>"
2280 "<if-feature name=\"feature\" />"
2281 "<mandatory value=\"true\" />"
2282 "<must condition=\"must-cond\" />"
2283 "<reference><text>ref</text></reference>"
2284 "<status value=\"deprecated\"/>"
2285 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002286 EXT_SUBELEM
David Sedlák8a83bbb2019-07-18 14:46:00 +02002287 "</anyxml>"
2288 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002289 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002290 parsed = (struct lysp_node_anydata *)siblings;
2291 assert_null(parsed->parent);
2292 assert_int_equal(parsed->nodetype, LYS_ANYXML);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002293 assert_true(parsed->flags & LYS_CONFIG_W);
2294 assert_true(parsed->flags & LYS_MAND_TRUE);
2295 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002296 assert_null(parsed->next);
2297 assert_string_equal(parsed->name, "any-name");
2298 assert_string_equal(parsed->dsc, "desc");
2299 assert_string_equal(parsed->ref, "ref");
2300 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002301 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedláke0ef1c62019-09-13 10:05:55 +02002302 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002303 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2304 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002305 lysp_node_free(st->ctx, siblings);
2306 siblings = NULL;
2307
2308 /* anydata max subelems */
2309 data = ELEMENT_WRAPPER_START
2310 "<anydata name=\"any-name\">"
2311 "<config value=\"true\" />"
2312 "<description><text>desc</text></description>"
2313 "<if-feature name=\"feature\" />"
2314 "<mandatory value=\"true\" />"
2315 "<must condition=\"must-cond\" />"
2316 "<reference><text>ref</text></reference>"
2317 "<status value=\"deprecated\"/>"
2318 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002319 EXT_SUBELEM
David Sedlák8a83bbb2019-07-18 14:46:00 +02002320 "</anydata>"
2321 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002322 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002323 parsed = (struct lysp_node_anydata *)siblings;
2324 assert_null(parsed->parent);
2325 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002326 assert_true(parsed->flags & LYS_CONFIG_W);
2327 assert_true(parsed->flags & LYS_MAND_TRUE);
2328 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002329 assert_null(parsed->next);
2330 assert_string_equal(parsed->name, "any-name");
2331 assert_string_equal(parsed->dsc, "desc");
2332 assert_string_equal(parsed->ref, "ref");
2333 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002334 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedláke0ef1c62019-09-13 10:05:55 +02002335 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002336 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2337 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002338 lysp_node_free(st->ctx, siblings);
2339 siblings = NULL;
2340
2341 /* min subelems */
2342 node_meta.parent = (void *)0x10;
2343 data = ELEMENT_WRAPPER_START "<anydata name=\"any-name\"> </anydata>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002344 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002345 parsed = (struct lysp_node_anydata *)siblings;
2346 assert_ptr_equal(parsed->parent, node_meta.parent);
2347 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
2348 assert_null(parsed->next);
2349 assert_null(parsed->exts);
2350 lysp_node_free(st->ctx, siblings);
2351
2352 st->finished_correctly = true;
2353}
2354
David Sedlák203ca3a2019-07-18 15:26:25 +02002355static void
2356test_leaf_elem(void **state)
2357{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002358 struct test_parser_yin_state *st = *state;
David Sedlák203ca3a2019-07-18 15:26:25 +02002359 const char *data;
2360 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002361 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák203ca3a2019-07-18 15:26:25 +02002362 struct lysp_node_leaf *parsed = NULL;
2363
2364 /* max elements */
2365 data = ELEMENT_WRAPPER_START
2366 "<leaf name=\"leaf\">"
2367 "<config value=\"true\" />"
2368 "<default value=\"def-val\"/>"
2369 "<description><text>desc</text></description>"
2370 "<if-feature name=\"feature\" />"
2371 "<mandatory value=\"true\" />"
2372 "<must condition=\"must-cond\" />"
2373 "<reference><text>ref</text></reference>"
2374 "<status value=\"deprecated\"/>"
2375 "<type name=\"type\"/>"
2376 "<units name=\"uni\"/>"
2377 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002378 EXT_SUBELEM
David Sedlák203ca3a2019-07-18 15:26:25 +02002379 "</leaf>"
2380 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002381 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák203ca3a2019-07-18 15:26:25 +02002382 parsed = (struct lysp_node_leaf *)siblings;
2383 assert_null(parsed->parent);
2384 assert_int_equal(parsed->nodetype, LYS_LEAF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002385 assert_true(parsed->flags & LYS_CONFIG_W);
2386 assert_true(parsed->flags & LYS_MAND_TRUE);
2387 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák203ca3a2019-07-18 15:26:25 +02002388 assert_null(parsed->next);
2389 assert_string_equal(parsed->name, "leaf");
2390 assert_string_equal(parsed->dsc, "desc");
2391 assert_string_equal(parsed->ref, "ref");
2392 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002393 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedláke0ef1c62019-09-13 10:05:55 +02002394 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002395 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2396 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002397 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlák203ca3a2019-07-18 15:26:25 +02002398 assert_string_equal(parsed->type.name, "type");
2399 assert_string_equal(parsed->units, "uni");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002400 assert_string_equal(parsed->dflt.str, "def-val");
David Sedlák203ca3a2019-07-18 15:26:25 +02002401 lysp_node_free(st->ctx, siblings);
2402 siblings = NULL;
2403
2404 /* min elements */
2405 data = ELEMENT_WRAPPER_START "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002406 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák203ca3a2019-07-18 15:26:25 +02002407 parsed = (struct lysp_node_leaf *)siblings;
2408 assert_string_equal(parsed->name, "leaf");
2409 assert_string_equal(parsed->type.name, "type");
2410 lysp_node_free(st->ctx, siblings);
2411 siblings = NULL;
2412
2413 st->finished_correctly = true;
2414}
2415
David Sedlákc3da3ef2019-07-19 12:56:08 +02002416static void
2417test_leaf_list_elem(void **state)
2418{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002419 struct test_parser_yin_state *st = *state;
David Sedlákc3da3ef2019-07-19 12:56:08 +02002420 const char *data;
2421 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002422 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlákc3da3ef2019-07-19 12:56:08 +02002423 struct lysp_node_leaflist *parsed = NULL;
2424
2425 data = ELEMENT_WRAPPER_START
2426 "<leaf-list name=\"llist\">"
2427 "<config value=\"true\" />"
2428 "<default value=\"def-val0\"/>"
2429 "<default value=\"def-val1\"/>"
2430 "<description><text>desc</text></description>"
2431 "<if-feature name=\"feature\"/>"
2432 "<max-elements value=\"5\"/>"
2433 "<must condition=\"must-cond\"/>"
2434 "<ordered-by value=\"user\" />"
2435 "<reference><text>ref</text></reference>"
2436 "<status value=\"current\"/>"
2437 "<type name=\"type\"/>"
2438 "<units name=\"uni\"/>"
2439 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002440 EXT_SUBELEM
David Sedlákc3da3ef2019-07-19 12:56:08 +02002441 "</leaf-list>"
2442 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002443 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002444 parsed = (struct lysp_node_leaflist *)siblings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02002445 assert_string_equal(parsed->dflts[0].str, "def-val0");
2446 assert_string_equal(parsed->dflts[1].str, "def-val1");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002447 assert_string_equal(parsed->dsc, "desc");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002448 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002449 assert_int_equal(parsed->max, 5);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002450 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002451 assert_string_equal(parsed->name, "llist");
2452 assert_null(parsed->next);
2453 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2454 assert_null(parsed->parent);
2455 assert_string_equal(parsed->ref, "ref");
2456 assert_string_equal(parsed->type.name, "type");
2457 assert_string_equal(parsed->units, "uni");
2458 assert_string_equal(parsed->when->cond, "when-cond");
2459 assert_true(parsed->flags & LYS_CONFIG_W);
2460 assert_true(parsed->flags & LYS_ORDBY_USER);
2461 assert_true(parsed->flags & LYS_STATUS_CURR);
David Sedláke0ef1c62019-09-13 10:05:55 +02002462 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002463 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2464 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002465 lysp_node_free(st->ctx, siblings);
2466 siblings = NULL;
2467
2468 data = ELEMENT_WRAPPER_START
2469 "<leaf-list name=\"llist\">"
2470 "<config value=\"true\" />"
2471 "<description><text>desc</text></description>"
2472 "<if-feature name=\"feature\"/>"
2473 "<min-elements value=\"5\"/>"
2474 "<must condition=\"must-cond\"/>"
2475 "<ordered-by value=\"user\" />"
2476 "<reference><text>ref</text></reference>"
2477 "<status value=\"current\"/>"
2478 "<type name=\"type\"/>"
2479 "<units name=\"uni\"/>"
2480 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002481 EXT_SUBELEM
David Sedlákc3da3ef2019-07-19 12:56:08 +02002482 "</leaf-list>"
2483 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002484 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002485 parsed = (struct lysp_node_leaflist *)siblings;
2486 assert_string_equal(parsed->dsc, "desc");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002487 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002488 assert_int_equal(parsed->min, 5);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002489 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002490 assert_string_equal(parsed->name, "llist");
2491 assert_null(parsed->next);
2492 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2493 assert_null(parsed->parent);
2494 assert_string_equal(parsed->ref, "ref");
2495 assert_string_equal(parsed->type.name, "type");
2496 assert_string_equal(parsed->units, "uni");
2497 assert_string_equal(parsed->when->cond, "when-cond");
2498 assert_true(parsed->flags & LYS_CONFIG_W);
2499 assert_true(parsed->flags & LYS_ORDBY_USER);
2500 assert_true(parsed->flags & LYS_STATUS_CURR);
David Sedláke0ef1c62019-09-13 10:05:55 +02002501 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002502 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2503 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002504 lysp_node_free(st->ctx, siblings);
2505 siblings = NULL;
2506
2507 data = ELEMENT_WRAPPER_START
2508 "<leaf-list name=\"llist\">"
2509 "<config value=\"true\" />"
2510 "<description><text>desc</text></description>"
2511 "<if-feature name=\"feature\"/>"
2512 "<max-elements value=\"15\"/>"
2513 "<min-elements value=\"5\"/>"
2514 "<must condition=\"must-cond\"/>"
2515 "<ordered-by value=\"user\" />"
2516 "<reference><text>ref</text></reference>"
2517 "<status value=\"current\"/>"
2518 "<type name=\"type\"/>"
2519 "<units name=\"uni\"/>"
2520 "<when condition=\"when-cond\"/>"
2521 "</leaf-list>"
2522 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002523 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002524 parsed = (struct lysp_node_leaflist *)siblings;
2525 assert_string_equal(parsed->dsc, "desc");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002526 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002527 assert_int_equal(parsed->min, 5);
2528 assert_int_equal(parsed->max, 15);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002529 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002530 assert_string_equal(parsed->name, "llist");
2531 assert_null(parsed->next);
2532 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2533 assert_null(parsed->parent);
2534 assert_string_equal(parsed->ref, "ref");
2535 assert_string_equal(parsed->type.name, "type");
2536 assert_string_equal(parsed->units, "uni");
2537 assert_string_equal(parsed->when->cond, "when-cond");
2538 assert_true(parsed->flags & LYS_CONFIG_W);
2539 assert_true(parsed->flags & LYS_ORDBY_USER);
2540 assert_true(parsed->flags & LYS_STATUS_CURR);
2541 lysp_node_free(st->ctx, siblings);
2542 siblings = NULL;
2543
2544 data = ELEMENT_WRAPPER_START
2545 "<leaf-list name=\"llist\">"
2546 "<type name=\"type\"/>"
2547 "</leaf-list>"
2548 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002549 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002550 parsed = (struct lysp_node_leaflist *)siblings;
2551 assert_string_equal(parsed->name, "llist");
2552 assert_string_equal(parsed->type.name, "type");
2553 lysp_node_free(st->ctx, siblings);
2554 siblings = NULL;
2555
2556 /* invalid combinations */
2557 data = ELEMENT_WRAPPER_START
2558 "<leaf-list name=\"llist\">"
2559 "<max-elements value=\"5\"/>"
2560 "<min-elements value=\"15\"/>"
2561 "<type name=\"type\"/>"
2562 "</leaf-list>"
2563 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002564 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_EVALID);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002565 logbuf_assert("Invalid combination of min-elements and max-elements: min value 15 is bigger than the max value 5. Line number 1.");
2566 lysp_node_free(st->ctx, siblings);
2567 siblings = NULL;
2568
2569 data = ELEMENT_WRAPPER_START
2570 "<leaf-list name=\"llist\">"
2571 "<default value=\"def-val1\"/>"
2572 "<min-elements value=\"15\"/>"
2573 "<type name=\"type\"/>"
2574 "</leaf-list>"
2575 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002576 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002577 logbuf_assert("Invalid combination of sub-elemnts \"min-elements\" and \"default\" in \"leaf-list\" element. Line number 1.");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002578 lysp_node_free(st->ctx, siblings);
2579 siblings = NULL;
2580
2581 data = ELEMENT_WRAPPER_START
2582 "<leaf-list name=\"llist\">"
2583 "</leaf-list>"
2584 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002585 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002586 logbuf_assert("Missing mandatory sub-element \"type\" of \"leaf-list\" element. Line number 1.");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002587 lysp_node_free(st->ctx, siblings);
2588 siblings = NULL;
2589
2590 st->finished_correctly = true;
2591}
2592
David Sedlákcb39f642019-07-19 13:19:55 +02002593static void
2594test_presence_elem(void **state)
2595{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002596 struct test_parser_yin_state *st = *state;
David Sedlákcb39f642019-07-19 13:19:55 +02002597 const char *data;
2598 const char *val;
David Sedlákd1144562019-08-06 12:36:14 +02002599 struct lysp_ext_instance *exts = NULL;
2600
2601 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\">"EXT_SUBELEM"</presence>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002602 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02002603 assert_string_equal(val, "presence-val");
David Sedláke0ef1c62019-09-13 10:05:55 +02002604 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002605 assert_int_equal(exts[0].insubstmt_index, 0);
2606 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_PRESENCE);
2607 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
2608 FREE_STRING(st->ctx, val);
David Sedlákcb39f642019-07-19 13:19:55 +02002609
2610 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002611 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlákcb39f642019-07-19 13:19:55 +02002612 assert_string_equal(val, "presence-val");
2613 FREE_STRING(st->ctx, val);
2614
2615 data = ELEMENT_WRAPPER_START "<presence/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002616 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlákcb39f642019-07-19 13:19:55 +02002617 logbuf_assert("Missing mandatory attribute value of presence element. Line number 1.");
2618
2619 st->finished_correctly = true;
2620}
2621
David Sedlák12470a82019-07-19 13:44:36 +02002622static void
2623test_key_elem(void **state)
2624{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002625 struct test_parser_yin_state *st = *state;
David Sedlák12470a82019-07-19 13:44:36 +02002626 const char *data;
2627 const char *val;
David Sedlákd1144562019-08-06 12:36:14 +02002628 struct lysp_ext_instance *exts = NULL;
2629
2630 data = ELEMENT_WRAPPER_START "<key value=\"key-value\">"EXT_SUBELEM"</key>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002631 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02002632 assert_string_equal(val, "key-value");
David Sedláke0ef1c62019-09-13 10:05:55 +02002633 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002634 assert_int_equal(exts[0].insubstmt_index, 0);
2635 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_KEY);
2636 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
2637 FREE_STRING(st->ctx, val);
David Sedlák12470a82019-07-19 13:44:36 +02002638
2639 data = ELEMENT_WRAPPER_START "<key value=\"key-value\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002640 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák12470a82019-07-19 13:44:36 +02002641 assert_string_equal(val, "key-value");
2642 FREE_STRING(st->ctx, val);
2643
2644 data = ELEMENT_WRAPPER_START "<key/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002645 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák12470a82019-07-19 13:44:36 +02002646 logbuf_assert("Missing mandatory attribute value of key element. Line number 1.");
2647
2648 st->finished_correctly = true;
2649}
2650
David Sedlák04e17b22019-07-19 15:29:48 +02002651static void
2652test_typedef_elem(void **state)
2653{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002654 struct test_parser_yin_state *st = *state;
David Sedlák04e17b22019-07-19 15:29:48 +02002655 const char *data;
2656 struct lysp_tpdf *tpdfs = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002657 struct tree_node_meta typdef_meta = {NULL, (struct lysp_node **)&tpdfs};
David Sedlák04e17b22019-07-19 15:29:48 +02002658
2659 data = ELEMENT_WRAPPER_START
2660 "<typedef name=\"tpdf-name\">"
2661 "<default value=\"def-val\"/>"
2662 "<description><text>desc-text</text></description>"
2663 "<reference><text>ref-text</text></reference>"
2664 "<status value=\"current\"/>"
2665 "<type name=\"type\"/>"
2666 "<units name=\"uni\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002667 EXT_SUBELEM
David Sedlák04e17b22019-07-19 15:29:48 +02002668 "</typedef>"
2669 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002670 assert_int_equal(test_element_helper(st, data, &typdef_meta, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002671 assert_string_equal(tpdfs[0].dflt.str, "def-val");
David Sedlák04e17b22019-07-19 15:29:48 +02002672 assert_string_equal(tpdfs[0].dsc, "desc-text");
David Sedlák04e17b22019-07-19 15:29:48 +02002673 assert_string_equal(tpdfs[0].name, "tpdf-name");
2674 assert_string_equal(tpdfs[0].ref, "ref-text");
2675 assert_string_equal(tpdfs[0].type.name, "type");
2676 assert_string_equal(tpdfs[0].units, "uni");
2677 assert_true(tpdfs[0].flags & LYS_STATUS_CURR);
David Sedláke0ef1c62019-09-13 10:05:55 +02002678 assert_string_equal(tpdfs[0].exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002679 assert_int_equal(tpdfs[0].exts[0].insubstmt_index, 0);
2680 assert_int_equal(tpdfs[0].exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák04e17b22019-07-19 15:29:48 +02002681 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2682 tpdfs = NULL;
2683
2684 data = ELEMENT_WRAPPER_START
2685 "<typedef name=\"tpdf-name\">"
2686 "<type name=\"type\"/>"
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);
David Sedlák04e17b22019-07-19 15:29:48 +02002690 assert_string_equal(tpdfs[0].name, "tpdf-name");
2691 assert_string_equal(tpdfs[0].type.name, "type");
2692 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2693 tpdfs = NULL;
2694
2695 st->finished_correctly = true;
2696}
2697
David Sedlákd2d676a2019-07-22 11:28:19 +02002698static void
2699test_refine_elem(void **state)
2700{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002701 struct test_parser_yin_state *st = *state;
David Sedlákd2d676a2019-07-22 11:28:19 +02002702 const char *data;
2703 struct lysp_refine *refines = NULL;
2704
2705 /* max subelems */
2706 data = ELEMENT_WRAPPER_START
2707 "<refine target-node=\"target\">"
2708 "<if-feature name=\"feature\" />"
2709 "<must condition=\"cond\" />"
2710 "<presence value=\"presence\" />"
2711 "<default value=\"def\" />"
2712 "<config value=\"true\" />"
2713 "<mandatory value=\"true\" />"
2714 "<min-elements value=\"10\" />"
2715 "<max-elements value=\"20\" />"
2716 "<description><text>desc</text></description>"
2717 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02002718 EXT_SUBELEM
David Sedlákd2d676a2019-07-22 11:28:19 +02002719 "</refine>"
2720 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002721 assert_int_equal(test_element_helper(st, data, &refines, NULL, NULL), LY_SUCCESS);
David Sedlákd2d676a2019-07-22 11:28:19 +02002722 assert_string_equal(refines->nodeid, "target");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002723 assert_string_equal(refines->dflts[0].str, "def");
David Sedlákd2d676a2019-07-22 11:28:19 +02002724 assert_string_equal(refines->dsc, "desc");
David Sedlákd2d676a2019-07-22 11:28:19 +02002725 assert_true(refines->flags & LYS_CONFIG_W);
2726 assert_true(refines->flags & LYS_MAND_TRUE);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002727 assert_string_equal(refines->iffeatures[0].str, "feature");
David Sedlákd2d676a2019-07-22 11:28:19 +02002728 assert_int_equal(refines->max, 20);
2729 assert_int_equal(refines->min, 10);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002730 assert_string_equal(refines->musts->arg.str, "cond");
David Sedlákd2d676a2019-07-22 11:28:19 +02002731 assert_string_equal(refines->presence, "presence");
2732 assert_string_equal(refines->ref, "ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02002733 assert_string_equal(refines->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002734 assert_int_equal(refines->exts[0].insubstmt_index, 0);
2735 assert_int_equal(refines->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákd2d676a2019-07-22 11:28:19 +02002736 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2737 refines = NULL;
2738
2739 /* min subelems */
2740 data = ELEMENT_WRAPPER_START "<refine target-node=\"target\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002741 assert_int_equal(test_element_helper(st, data, &refines, NULL, NULL), LY_SUCCESS);
David Sedlákd2d676a2019-07-22 11:28:19 +02002742 assert_string_equal(refines->nodeid, "target");
2743 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2744 refines = NULL;
2745
2746 st->finished_correctly = true;
2747}
2748
David Sedlák0d6de5a2019-07-22 13:25:44 +02002749static void
2750test_uses_elem(void **state)
2751{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002752 struct test_parser_yin_state *st = *state;
David Sedlák0d6de5a2019-07-22 13:25:44 +02002753 const char *data;
2754 struct lysp_node *siblings = NULL;
2755 struct tree_node_meta node_meta = {NULL, &siblings};
2756 struct lysp_node_uses *parsed = NULL;
2757
2758 /* max subelems */
2759 data = ELEMENT_WRAPPER_START
2760 "<uses name=\"uses-name\">"
2761 "<when condition=\"cond\" />"
2762 "<if-feature name=\"feature\" />"
2763 "<status value=\"obsolete\" />"
2764 "<description><text>desc</text></description>"
2765 "<reference><text>ref</text></reference>"
2766 "<refine target-node=\"target\"/>"
David Sedlák992fb7c2019-07-24 16:51:01 +02002767 "<augment target-node=\"target\" />"
David Sedlákd1144562019-08-06 12:36:14 +02002768 EXT_SUBELEM
David Sedlák0d6de5a2019-07-22 13:25:44 +02002769 "</uses>"
2770 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002771 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002772 parsed = (struct lysp_node_uses *)&siblings[0];
2773 assert_string_equal(parsed->name, "uses-name");
2774 assert_string_equal(parsed->dsc, "desc");
David Sedlák0d6de5a2019-07-22 13:25:44 +02002775 assert_true(parsed->flags & LYS_STATUS_OBSLT);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002776 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlák0d6de5a2019-07-22 13:25:44 +02002777 assert_null(parsed->next);
2778 assert_int_equal(parsed->nodetype, LYS_USES);
2779 assert_null(parsed->parent);
2780 assert_string_equal(parsed->ref, "ref");
2781 assert_string_equal(parsed->refines->nodeid, "target");
2782 assert_string_equal(parsed->when->cond, "cond");
David Sedlák992fb7c2019-07-24 16:51:01 +02002783 assert_string_equal(parsed->augments->nodeid, "target");
David Sedláke0ef1c62019-09-13 10:05:55 +02002784 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002785 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2786 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002787 lysp_node_free(st->ctx, siblings);
2788 siblings = NULL;
2789
2790 /* min subelems */
2791 data = ELEMENT_WRAPPER_START "<uses name=\"uses-name\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002792 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002793 assert_string_equal(siblings[0].name, "uses-name");
2794 lysp_node_free(st->ctx, siblings);
2795 siblings = NULL;
2796
2797 st->finished_correctly = true;
2798}
2799
David Sedlákaa854b02019-07-22 14:17:10 +02002800static void
2801test_revision_elem(void **state)
2802{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002803 struct test_parser_yin_state *st = *state;
David Sedlákaa854b02019-07-22 14:17:10 +02002804 const char *data;
2805 struct lysp_revision *revs = NULL;
2806
2807 /* max subelems */
2808 data = ELEMENT_WRAPPER_START
2809 "<revision date=\"2018-12-25\">"
2810 "<description><text>desc</text></description>"
2811 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02002812 EXT_SUBELEM
David Sedlákaa854b02019-07-22 14:17:10 +02002813 "</revision>"
2814 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002815 assert_int_equal(test_element_helper(st, data, &revs, NULL, NULL), LY_SUCCESS);
David Sedlákaa854b02019-07-22 14:17:10 +02002816 assert_string_equal(revs->date, "2018-12-25");
2817 assert_string_equal(revs->dsc, "desc");
2818 assert_string_equal(revs->ref, "ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02002819 assert_string_equal(revs->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002820 assert_int_equal(revs->exts[0].insubstmt_index, 0);
2821 assert_int_equal(revs->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákaa854b02019-07-22 14:17:10 +02002822 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2823 revs = NULL;
2824
2825 /* min subelems */
2826 data = ELEMENT_WRAPPER_START "<revision date=\"2005-05-05\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002827 assert_int_equal(test_element_helper(st, data, &revs, NULL, NULL), LY_SUCCESS);
David Sedlákaa854b02019-07-22 14:17:10 +02002828 assert_string_equal(revs->date, "2005-05-05");
2829 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2830 revs = NULL;
2831
2832 /* invalid value */
2833 data = ELEMENT_WRAPPER_START "<revision date=\"05-05-2005\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002834 assert_int_equal(test_element_helper(st, data, &revs, NULL, NULL), LY_EVALID);
David Sedlákaa854b02019-07-22 14:17:10 +02002835 logbuf_assert("Invalid value \"05-05-2005\" of \"revision\". Line number 1.");
2836 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2837 revs = NULL;
2838
2839 st->finished_correctly = true;
2840}
2841
David Sedlák0c2bab92019-07-22 15:33:19 +02002842static void
2843test_include_elem(void **state)
2844{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002845 struct test_parser_yin_state *st = *state;
David Sedlák0c2bab92019-07-22 15:33:19 +02002846 const char *data;
2847 struct lysp_include *includes = NULL;
2848 struct include_meta inc_meta = {"module-name", &includes};
2849
2850 /* max subelems */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002851 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák0c2bab92019-07-22 15:33:19 +02002852 data = ELEMENT_WRAPPER_START
2853 "<include module=\"mod\">"
2854 "<description><text>desc</text></description>"
2855 "<reference><text>ref</text></reference>"
2856 "<revision-date date=\"1999-09-09\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002857 EXT_SUBELEM
David Sedlák0c2bab92019-07-22 15:33:19 +02002858 "</include>"
2859 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002860 assert_int_equal(test_element_helper(st, data, &inc_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0c2bab92019-07-22 15:33:19 +02002861 assert_string_equal(includes->name, "mod");
2862 assert_string_equal(includes->dsc, "desc");
2863 assert_string_equal(includes->ref, "ref");
David Sedlák0c2bab92019-07-22 15:33:19 +02002864 assert_string_equal(includes->rev, "1999-09-09");
David Sedláke0ef1c62019-09-13 10:05:55 +02002865 assert_string_equal(includes->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002866 assert_int_equal(includes->exts[0].insubstmt_index, 0);
2867 assert_int_equal(includes->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák0c2bab92019-07-22 15:33:19 +02002868 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2869 includes = NULL;
2870
2871 /* min subelems */
2872 data = ELEMENT_WRAPPER_START "<include module=\"mod\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002873 assert_int_equal(test_element_helper(st, data, &inc_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0c2bab92019-07-22 15:33:19 +02002874 assert_string_equal(includes->name, "mod");
2875 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2876 includes = NULL;
2877
2878 /* invalid combinations */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002879 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_0;
David Sedlák0c2bab92019-07-22 15:33:19 +02002880 data = ELEMENT_WRAPPER_START
2881 "<include module=\"mod\">"
2882 "<description><text>desc</text></description>"
2883 "<revision-date date=\"1999-09-09\"/>"
2884 "</include>"
2885 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002886 assert_int_equal(test_element_helper(st, data, &inc_meta, NULL, NULL), LY_EVALID);
David Sedlák0c2bab92019-07-22 15:33:19 +02002887 logbuf_assert("Invalid sub-elemnt \"description\" of \"include\" element - this sub-element is allowed only in modules with version 1.1 or newer. Line number 1.");
2888 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2889 includes = NULL;
2890
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002891 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_0;
David Sedlák0c2bab92019-07-22 15:33:19 +02002892 data = ELEMENT_WRAPPER_START
2893 "<include module=\"mod\">"
2894 "<reference><text>ref</text></reference>"
2895 "<revision-date date=\"1999-09-09\"/>"
2896 "</include>"
2897 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002898 assert_int_equal(test_element_helper(st, data, &inc_meta, NULL, NULL), LY_EVALID);
David Sedlák0c2bab92019-07-22 15:33:19 +02002899 logbuf_assert("Invalid sub-elemnt \"reference\" of \"include\" element - this sub-element is allowed only in modules with version 1.1 or newer. Line number 1.");
2900 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2901 includes = NULL;
2902
2903 st->finished_correctly = true;
2904}
2905
David Sedlák5e13dea2019-07-22 16:06:45 +02002906static void
David Sedlákaf536aa2019-07-23 13:42:23 +02002907test_list_elem(void **state)
2908{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002909 struct test_parser_yin_state *st = *state;
David Sedlákaf536aa2019-07-23 13:42:23 +02002910 const char *data;
2911 struct lysp_node *siblings = NULL;
2912 struct tree_node_meta node_meta = {NULL, &siblings};
2913 struct lysp_node_list *parsed = NULL;
2914
2915 /* max subelems */
2916 data = ELEMENT_WRAPPER_START
2917 "<list name=\"list-name\">"
2918 "<when condition=\"when\"/>"
2919 "<if-feature name=\"iff\"/>"
2920 "<must condition=\"must-cond\"/>"
2921 "<key value=\"key\"/>"
2922 "<unique tag=\"utag\"/>"
2923 "<config value=\"true\"/>"
2924 "<min-elements value=\"10\"/>"
2925 "<ordered-by value=\"user\"/>"
2926 "<status value=\"deprecated\"/>"
2927 "<description><text>desc</text></description>"
2928 "<reference><text>ref</text></reference>"
2929 "<anydata name=\"anyd\"/>"
2930 "<anyxml name=\"anyx\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02002931 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002932 "<choice name=\"choice\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02002933 "<action name=\"action\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02002934 "<grouping name=\"grp\"/>"
David Sedlák031b9e72019-07-23 15:19:37 +02002935 "<notification name=\"notf\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002936 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
2937 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlákaf536aa2019-07-23 13:42:23 +02002938 "<list name=\"sub-list\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002939 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákaf536aa2019-07-23 13:42:23 +02002940 "<uses name=\"uses-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002941 EXT_SUBELEM
David Sedlákaf536aa2019-07-23 13:42:23 +02002942 "</list>"
2943 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002944 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákaf536aa2019-07-23 13:42:23 +02002945 parsed = (struct lysp_node_list *)&siblings[0];
2946 assert_string_equal(parsed->dsc, "desc");
2947 assert_string_equal(parsed->child->name, "anyd");
2948 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2949 assert_string_equal(parsed->child->next->name, "anyx");
2950 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
David Sedlákf111bcb2019-07-23 17:15:51 +02002951 assert_string_equal(parsed->child->next->next->name, "cont");
2952 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002953 assert_string_equal(parsed->child->next->next->next->name, "choice");
2954 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
David Sedlák85d0eca2019-07-24 15:15:21 +02002955 assert_string_equal(parsed->child->next->next->next->next->name, "leaf");
2956 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAF);
2957 assert_string_equal(parsed->child->next->next->next->next->next->name, "llist");
2958 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2959 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "sub-list");
2960 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LIST);
2961 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "uses-name");
2962 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_USES);
2963 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002964 assert_string_equal(parsed->groupings->name, "grp");
David Sedlák85d0eca2019-07-24 15:15:21 +02002965 assert_string_equal(parsed->actions->name, "action");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002966 assert_int_equal(parsed->groupings->nodetype, LYS_GROUPING);
David Sedlák031b9e72019-07-23 15:19:37 +02002967 assert_string_equal(parsed->notifs->name, "notf");
David Sedlákaf536aa2019-07-23 13:42:23 +02002968 assert_true(parsed->flags & LYS_ORDBY_USER);
2969 assert_true(parsed->flags & LYS_STATUS_DEPRC);
2970 assert_true(parsed->flags & LYS_CONFIG_W);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002971 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlákaf536aa2019-07-23 13:42:23 +02002972 assert_string_equal(parsed->key, "key");
2973 assert_int_equal(parsed->min, 10);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002974 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlákaf536aa2019-07-23 13:42:23 +02002975 assert_string_equal(parsed->name, "list-name");
2976 assert_null(parsed->next);
2977 assert_int_equal(parsed->nodetype, LYS_LIST);
2978 assert_null(parsed->parent);
2979 assert_string_equal(parsed->ref, "ref");
2980 assert_string_equal(parsed->typedefs->name, "tpdf");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002981 assert_string_equal(parsed->uniques->str, "utag");
David Sedlákaf536aa2019-07-23 13:42:23 +02002982 assert_string_equal(parsed->when->cond, "when");
David Sedláke0ef1c62019-09-13 10:05:55 +02002983 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002984 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2985 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákaf536aa2019-07-23 13:42:23 +02002986 lysp_node_free(st->ctx, siblings);
2987 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
2988 siblings = NULL;
2989
2990 /* min subelems */
2991 data = ELEMENT_WRAPPER_START "<list name=\"list-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002992 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákaf536aa2019-07-23 13:42:23 +02002993 parsed = (struct lysp_node_list *)&siblings[0];
2994 assert_string_equal(parsed->name, "list-name");
2995 lysp_node_free(st->ctx, siblings);
2996 siblings = NULL;
2997
2998 st->finished_correctly = true;
2999}
3000
David Sedlák031b9e72019-07-23 15:19:37 +02003001static void
3002test_notification_elem(void **state)
3003{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003004 struct test_parser_yin_state *st = *state;
David Sedlák031b9e72019-07-23 15:19:37 +02003005 const char *data;
3006 struct lysp_notif *notifs = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003007 struct tree_node_meta notif_meta = {NULL, (struct lysp_node **)&notifs};
David Sedlák031b9e72019-07-23 15:19:37 +02003008
3009 /* max subelems */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003010 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák031b9e72019-07-23 15:19:37 +02003011 data = ELEMENT_WRAPPER_START
3012 "<notification name=\"notif-name\">"
3013 "<anydata name=\"anyd\"/>"
3014 "<anyxml name=\"anyx\"/>"
3015 "<description><text>desc</text></description>"
3016 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003017 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3018 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák031b9e72019-07-23 15:19:37 +02003019 "<list name=\"sub-list\"/>"
3020 "<must condition=\"cond\"/>"
3021 "<reference><text>ref</text></reference>"
3022 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003023 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák031b9e72019-07-23 15:19:37 +02003024 "<uses name=\"uses-name\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02003025 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003026 "<choice name=\"choice\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02003027 "<grouping name=\"grp\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003028 EXT_SUBELEM
David Sedlák031b9e72019-07-23 15:19:37 +02003029 "</notification>"
3030 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003031 assert_int_equal(test_element_helper(st, data, &notif_meta, NULL, NULL), LY_SUCCESS);
David Sedlák031b9e72019-07-23 15:19:37 +02003032 assert_string_equal(notifs->name, "notif-name");
3033 assert_string_equal(notifs->data->name, "anyd");
3034 assert_int_equal(notifs->data->nodetype, LYS_ANYDATA);
3035 assert_string_equal(notifs->data->next->name, "anyx");
3036 assert_int_equal(notifs->data->next->nodetype, LYS_ANYXML);
3037 assert_string_equal(notifs->data->next->next->name, "leaf");
3038 assert_int_equal(notifs->data->next->next->nodetype, LYS_LEAF);
3039 assert_string_equal(notifs->data->next->next->next->name, "llist");
3040 assert_int_equal(notifs->data->next->next->next->nodetype, LYS_LEAFLIST);
3041 assert_string_equal(notifs->data->next->next->next->next->name, "sub-list");
3042 assert_int_equal(notifs->data->next->next->next->next->nodetype, LYS_LIST);
David Sedlák031b9e72019-07-23 15:19:37 +02003043 assert_true(notifs->flags & LYS_STATUS_DEPRC);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003044 assert_string_equal(notifs->groupings->name, "grp");
3045 assert_int_equal(notifs->groupings->nodetype, LYS_GROUPING);
David Sedlákf111bcb2019-07-23 17:15:51 +02003046 assert_string_equal(notifs->data->next->next->next->next->next->name, "uses-name");
3047 assert_int_equal(notifs->data->next->next->next->next->next->nodetype, LYS_USES);
3048 assert_string_equal(notifs->data->next->next->next->next->next->next->name, "cont");
3049 assert_int_equal(notifs->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003050 assert_int_equal(notifs->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
3051 assert_string_equal(notifs->data->next->next->next->next->next->next->next->name, "choice");
3052 assert_null(notifs->data->next->next->next->next->next->next->next->next);
Michal Vasko7f45cf22020-10-01 12:49:44 +02003053 assert_string_equal(notifs->iffeatures[0].str, "iff");
3054 assert_string_equal(notifs->musts->arg.str, "cond");
David Sedlák031b9e72019-07-23 15:19:37 +02003055 assert_int_equal(notifs->nodetype, LYS_NOTIF);
3056 assert_null(notifs->parent);
3057 assert_string_equal(notifs->ref, "ref");
3058 assert_string_equal(notifs->typedefs->name, "tpdf");
David Sedláke0ef1c62019-09-13 10:05:55 +02003059 assert_string_equal(notifs->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003060 assert_int_equal(notifs->exts[0].insubstmt_index, 0);
3061 assert_int_equal(notifs->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák031b9e72019-07-23 15:19:37 +02003062 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
3063 notifs = NULL;
3064
3065 /* min subelems */
3066 data = ELEMENT_WRAPPER_START "<notification name=\"notif-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003067 assert_int_equal(test_element_helper(st, data, &notif_meta, NULL, NULL), LY_SUCCESS);
David Sedlák031b9e72019-07-23 15:19:37 +02003068 assert_string_equal(notifs->name, "notif-name");
3069 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003070 notifs = NULL;
3071
3072 st->finished_correctly = true;
3073}
3074
3075static void
3076test_grouping_elem(void **state)
3077{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003078 struct test_parser_yin_state *st = *state;
David Sedláke3ce9ef2019-07-23 16:34:30 +02003079 const char *data;
3080 struct lysp_grp *grps = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003081 struct tree_node_meta grp_meta = {NULL, (struct lysp_node **)&grps};
David Sedláke3ce9ef2019-07-23 16:34:30 +02003082
3083 /* max subelems */
3084 data = ELEMENT_WRAPPER_START
3085 "<grouping name=\"grp-name\">"
3086 "<anydata name=\"anyd\"/>"
3087 "<anyxml name=\"anyx\"/>"
3088 "<description><text>desc</text></description>"
3089 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003090 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3091 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02003092 "<list name=\"list\"/>"
3093 "<notification name=\"notf\"/>"
3094 "<reference><text>ref</text></reference>"
3095 "<status value=\"current\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003096 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02003097 "<uses name=\"uses-name\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02003098 "<action name=\"act\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02003099 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003100 "<choice name=\"choice\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003101 EXT_SUBELEM
David Sedláke3ce9ef2019-07-23 16:34:30 +02003102 "</grouping>"
3103 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003104 assert_int_equal(test_element_helper(st, data, &grp_meta, NULL, NULL), LY_SUCCESS);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003105 assert_string_equal(grps->name, "grp-name");
David Sedláke3ce9ef2019-07-23 16:34:30 +02003106 assert_string_equal(grps->data->name, "anyd");
3107 assert_string_equal(grps->data->next->name, "anyx");
3108 assert_string_equal(grps->data->next->next->name, "leaf");
3109 assert_string_equal(grps->data->next->next->next->name, "llist");
3110 assert_string_equal(grps->data->next->next->next->next->name, "list");
3111 assert_string_equal(grps->dsc, "desc");
David Sedláke3ce9ef2019-07-23 16:34:30 +02003112 assert_true(grps->flags & LYS_STATUS_CURR);
3113 assert_string_equal(grps->groupings->name, "sub-grp");
3114 assert_int_equal(grps->nodetype, LYS_GROUPING);
3115 assert_string_equal(grps->notifs->name, "notf");
3116 assert_null(grps->parent);
3117 assert_string_equal(grps->ref, "ref");
3118 assert_string_equal(grps->typedefs->name, "tpdf");
David Sedlák85d0eca2019-07-24 15:15:21 +02003119 assert_string_equal(grps->actions->name, "act");
David Sedlákf111bcb2019-07-23 17:15:51 +02003120 assert_string_equal(grps->data->next->next->next->next->next->name, "uses-name");
David Sedlákb7abcfa2019-07-24 12:33:35 +02003121 assert_int_equal(grps->data->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákf111bcb2019-07-23 17:15:51 +02003122 assert_string_equal(grps->data->next->next->next->next->next->next->name, "cont");
David Sedlákb7abcfa2019-07-24 12:33:35 +02003123 assert_int_equal(grps->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
3124 assert_string_equal(grps->data->next->next->next->next->next->next->next->name, "choice");
3125 assert_int_equal(grps->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
David Sedláke0ef1c62019-09-13 10:05:55 +02003126 assert_string_equal(grps->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003127 assert_int_equal(grps->exts[0].insubstmt_index, 0);
3128 assert_int_equal(grps->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003129 FREE_ARRAY(st->ctx, grps, lysp_grp_free);
3130 grps = NULL;
3131
3132 /* min subelems */
3133 data = ELEMENT_WRAPPER_START "<grouping name=\"grp-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003134 assert_int_equal(test_element_helper(st, data, &grp_meta, NULL, NULL), LY_SUCCESS);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003135 assert_string_equal(grps->name, "grp-name");
3136 FREE_ARRAY(st->ctx, grps, lysp_grp_free);
3137 grps = NULL;
David Sedlák031b9e72019-07-23 15:19:37 +02003138
3139 st->finished_correctly = true;
3140}
3141
David Sedlákf111bcb2019-07-23 17:15:51 +02003142static void
3143test_container_elem(void **state)
3144{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003145 struct test_parser_yin_state *st = *state;
David Sedlákf111bcb2019-07-23 17:15:51 +02003146 const char *data;
3147 struct lysp_node *siblings = NULL;
3148 struct tree_node_meta node_meta = {NULL, &siblings};
3149 struct lysp_node_container *parsed = NULL;
3150
3151 /* max subelems */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003152 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedláke2dc9e92019-07-24 09:59:21 +02003153 data = ELEMENT_WRAPPER_START
3154 "<container name=\"cont-name\">"
3155 "<anydata name=\"anyd\"/>"
3156 "<anyxml name=\"anyx\"/>"
3157 "<config value=\"true\"/>"
3158 "<container name=\"subcont\"/>"
3159 "<description><text>desc</text></description>"
3160 "<grouping name=\"sub-grp\"/>"
3161 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003162 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3163 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedláke2dc9e92019-07-24 09:59:21 +02003164 "<list name=\"list\"/>"
3165 "<must condition=\"cond\"/>"
3166 "<notification name=\"notf\"/>"
3167 "<presence value=\"presence\"/>"
3168 "<reference><text>ref</text></reference>"
3169 "<status value=\"current\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003170 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedláke2dc9e92019-07-24 09:59:21 +02003171 "<uses name=\"uses-name\"/>"
3172 "<when condition=\"when-cond\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02003173 "<action name=\"act\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003174 "<choice name=\"choice\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003175 EXT_SUBELEM
David Sedláke2dc9e92019-07-24 09:59:21 +02003176 "</container>"
3177 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003178 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedláke2dc9e92019-07-24 09:59:21 +02003179 parsed = (struct lysp_node_container *)siblings;
3180 assert_string_equal(parsed->name, "cont-name");
3181 assert_null(parsed->parent);
3182 assert_int_equal(parsed->nodetype, LYS_CONTAINER);
3183 assert_true(parsed->flags & LYS_CONFIG_W);
3184 assert_true(parsed->flags & LYS_STATUS_CURR);
3185 assert_null(parsed->next);
3186 assert_string_equal(parsed->dsc, "desc");
3187 assert_string_equal(parsed->ref, "ref");
3188 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003189 assert_string_equal(parsed->iffeatures[0].str, "iff");
3190 assert_string_equal(parsed->musts->arg.str, "cond");
David Sedláke2dc9e92019-07-24 09:59:21 +02003191 assert_string_equal(parsed->presence, "presence");
3192 assert_string_equal(parsed->typedefs->name, "tpdf");
3193 assert_string_equal(parsed->groupings->name, "sub-grp");
3194 assert_string_equal(parsed->child->name, "anyd");
3195 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
3196 assert_string_equal(parsed->child->next->name, "anyx");
3197 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
3198 assert_string_equal(parsed->child->next->next->name, "subcont");
3199 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
3200 assert_string_equal(parsed->child->next->next->next->name, "leaf");
3201 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
3202 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
3203 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
3204 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
3205 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
3206 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
3207 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003208 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
3209 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
3210 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke2dc9e92019-07-24 09:59:21 +02003211 assert_string_equal(parsed->notifs->name, "notf");
David Sedlák85d0eca2019-07-24 15:15:21 +02003212 assert_string_equal(parsed->actions->name, "act");
David Sedláke0ef1c62019-09-13 10:05:55 +02003213 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003214 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3215 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedláke2dc9e92019-07-24 09:59:21 +02003216 lysp_node_free(st->ctx, siblings);
3217 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
3218 siblings = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02003219
3220 /* min subelems */
3221 data = ELEMENT_WRAPPER_START "<container name=\"cont-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003222 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákf111bcb2019-07-23 17:15:51 +02003223 parsed = (struct lysp_node_container *)siblings;
3224 assert_string_equal(parsed->name, "cont-name");
3225 lysp_node_free(st->ctx, siblings);
3226 siblings = NULL;
3227
3228 st->finished_correctly = true;
3229}
3230
David Sedlák5379d392019-07-24 10:42:03 +02003231static void
3232test_case_elem(void **state)
3233{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003234 struct test_parser_yin_state *st = *state;
David Sedlák5379d392019-07-24 10:42:03 +02003235 const char *data;
3236 struct lysp_node *siblings = NULL;
3237 struct tree_node_meta node_meta = {NULL, &siblings};
3238 struct lysp_node_case *parsed = NULL;
3239
3240 /* max subelems */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003241 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák5379d392019-07-24 10:42:03 +02003242 data = ELEMENT_WRAPPER_START
3243 "<case name=\"case-name\">"
3244 "<anydata name=\"anyd\"/>"
3245 "<anyxml name=\"anyx\"/>"
3246 "<container name=\"subcont\"/>"
3247 "<description><text>desc</text></description>"
3248 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003249 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3250 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák5379d392019-07-24 10:42:03 +02003251 "<list name=\"list\"/>"
3252 "<reference><text>ref</text></reference>"
3253 "<status value=\"current\"/>"
3254 "<uses name=\"uses-name\"/>"
3255 "<when condition=\"when-cond\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003256 "<choice name=\"choice\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003257 EXT_SUBELEM
David Sedlák5379d392019-07-24 10:42:03 +02003258 "</case>"
3259 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003260 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák5379d392019-07-24 10:42:03 +02003261 parsed = (struct lysp_node_case *)siblings;
3262 assert_string_equal(parsed->name, "case-name");
3263 assert_null(parsed->parent);
3264 assert_int_equal(parsed->nodetype, LYS_CASE);
3265 assert_true(parsed->flags & LYS_STATUS_CURR);
3266 assert_null(parsed->next);
3267 assert_string_equal(parsed->dsc, "desc");
3268 assert_string_equal(parsed->ref, "ref");
3269 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003270 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlák5379d392019-07-24 10:42:03 +02003271 assert_string_equal(parsed->child->name, "anyd");
3272 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
3273 assert_string_equal(parsed->child->next->name, "anyx");
3274 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
3275 assert_string_equal(parsed->child->next->next->name, "subcont");
3276 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
3277 assert_string_equal(parsed->child->next->next->next->name, "leaf");
3278 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
3279 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
3280 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
3281 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
3282 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
3283 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
3284 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003285 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
3286 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
3287 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke0ef1c62019-09-13 10:05:55 +02003288 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003289 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3290 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák5379d392019-07-24 10:42:03 +02003291 lysp_node_free(st->ctx, siblings);
3292 siblings = NULL;
3293
3294 /* min subelems */
3295 data = ELEMENT_WRAPPER_START "<case name=\"case-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003296 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák5379d392019-07-24 10:42:03 +02003297 parsed = (struct lysp_node_case *)siblings;
3298 assert_string_equal(parsed->name, "case-name");
3299 lysp_node_free(st->ctx, siblings);
3300 siblings = NULL;
3301
3302 st->finished_correctly = true;
3303}
3304
David Sedlákb7abcfa2019-07-24 12:33:35 +02003305static void
3306test_choice_elem(void **state)
3307{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003308 struct test_parser_yin_state *st = *state;
David Sedlákb7abcfa2019-07-24 12:33:35 +02003309 const char *data;
3310 struct lysp_node *siblings = NULL;
3311 struct tree_node_meta node_meta = {NULL, &siblings};
3312 struct lysp_node_choice *parsed = NULL;
3313
3314 /* max subelems */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003315 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlákb7abcfa2019-07-24 12:33:35 +02003316 data = ELEMENT_WRAPPER_START
3317 "<choice name=\"choice-name\">"
3318 "<anydata name=\"anyd\"/>"
3319 "<anyxml name=\"anyx\"/>"
3320 "<case name=\"sub-case\"/>"
3321 "<choice name=\"choice\"/>"
3322 "<config value=\"true\"/>"
3323 "<container name=\"subcont\"/>"
3324 "<default value=\"def\"/>"
3325 "<description><text>desc</text></description>"
3326 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003327 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3328 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003329 "<list name=\"list\"/>"
3330 "<mandatory value=\"true\" />"
3331 "<reference><text>ref</text></reference>"
3332 "<status value=\"current\"/>"
3333 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003334 EXT_SUBELEM
David Sedlákb7abcfa2019-07-24 12:33:35 +02003335 "</choice>"
3336 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003337 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003338 parsed = (struct lysp_node_choice *)siblings;
3339 assert_string_equal(parsed->name, "choice-name");
3340 assert_null(parsed->parent);
3341 assert_int_equal(parsed->nodetype, LYS_CHOICE);
3342 assert_true(parsed->flags & LYS_CONFIG_W && parsed->flags & LYS_MAND_TRUE && parsed->flags & LYS_STATUS_CURR);
3343 assert_null(parsed->next);
3344 assert_string_equal(parsed->dsc, "desc");
3345 assert_string_equal(parsed->ref, "ref");
3346 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003347 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlákb7abcfa2019-07-24 12:33:35 +02003348 assert_string_equal(parsed->child->name, "anyd");
3349 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
3350 assert_string_equal(parsed->child->next->name, "anyx");
3351 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
3352 assert_string_equal(parsed->child->next->next->name, "sub-case");
3353 assert_int_equal(parsed->child->next->next->nodetype, LYS_CASE);
3354 assert_string_equal(parsed->child->next->next->next->name, "choice");
3355 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
3356 assert_string_equal(parsed->child->next->next->next->next->name, "subcont");
3357 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_CONTAINER);
3358 assert_string_equal(parsed->child->next->next->next->next->next->name, "leaf");
3359 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAF);
3360 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "llist");
3361 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3362 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "list");
3363 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
3364 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke0ef1c62019-09-13 10:05:55 +02003365 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003366 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3367 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003368 lysp_node_free(st->ctx, siblings);
3369 siblings = NULL;
3370
3371 /* min subelems */
3372 data = ELEMENT_WRAPPER_START "<choice name=\"choice-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003373 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003374 parsed = (struct lysp_node_choice *)siblings;
3375 assert_string_equal(parsed->name, "choice-name");
3376 lysp_node_free(st->ctx, siblings);
3377 siblings = NULL;
3378
3379 st->finished_correctly = true;
3380}
3381
David Sedlák05404f62019-07-24 14:11:53 +02003382static void
3383test_inout_elem(void **state)
3384{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003385 struct test_parser_yin_state *st = *state;
David Sedlák05404f62019-07-24 14:11:53 +02003386 const char *data;
3387 struct lysp_action_inout inout = {};
3388 struct inout_meta inout_meta = {NULL, &inout};
3389
3390 /* max subelements */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003391 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák05404f62019-07-24 14:11:53 +02003392 data = ELEMENT_WRAPPER_START
3393 "<input>"
3394 "<anydata name=\"anyd\"/>"
3395 "<anyxml name=\"anyx\"/>"
3396 "<choice name=\"choice\"/>"
3397 "<container name=\"subcont\"/>"
3398 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003399 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3400 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák05404f62019-07-24 14:11:53 +02003401 "<list name=\"list\"/>"
3402 "<must condition=\"cond\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003403 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák05404f62019-07-24 14:11:53 +02003404 "<uses name=\"uses-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003405 EXT_SUBELEM
David Sedlák05404f62019-07-24 14:11:53 +02003406 "</input>"
3407 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003408 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_SUCCESS);
David Sedlák05404f62019-07-24 14:11:53 +02003409 assert_null(inout.parent);
3410 assert_int_equal(inout.nodetype, LYS_INPUT);
Michal Vasko7f45cf22020-10-01 12:49:44 +02003411 assert_string_equal(inout.musts->arg.str, "cond");
David Sedlák05404f62019-07-24 14:11:53 +02003412 assert_string_equal(inout.typedefs->name, "tpdf");
3413 assert_string_equal(inout.groupings->name, "sub-grp");
3414 assert_string_equal(inout.data->name, "anyd");
3415 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
3416 assert_string_equal(inout.data->next->name, "anyx");
3417 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
3418 assert_string_equal(inout.data->next->next->name, "choice");
3419 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
3420 assert_string_equal(inout.data->next->next->next->name, "subcont");
3421 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
3422 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
3423 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
3424 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
3425 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3426 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
3427 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
3428 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
3429 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3430 assert_null(inout.data->next->next->next->next->next->next->next->next);
David Sedláke0ef1c62019-09-13 10:05:55 +02003431 assert_string_equal(inout.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003432 assert_int_equal(inout.exts[0].insubstmt_index, 0);
3433 assert_int_equal(inout.exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák05404f62019-07-24 14:11:53 +02003434 lysp_action_inout_free(st->ctx, &inout);
3435 memset(&inout, 0, sizeof inout);
3436
3437 /* max subelements */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003438 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák05404f62019-07-24 14:11:53 +02003439 data = ELEMENT_WRAPPER_START
3440 "<output>"
3441 "<anydata name=\"anyd\"/>"
3442 "<anyxml name=\"anyx\"/>"
3443 "<choice name=\"choice\"/>"
3444 "<container name=\"subcont\"/>"
3445 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003446 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3447 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák05404f62019-07-24 14:11:53 +02003448 "<list name=\"list\"/>"
3449 "<must condition=\"cond\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003450 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák05404f62019-07-24 14:11:53 +02003451 "<uses name=\"uses-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003452 EXT_SUBELEM
David Sedlák05404f62019-07-24 14:11:53 +02003453 "</output>"
3454 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003455 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_SUCCESS);
David Sedlák05404f62019-07-24 14:11:53 +02003456 assert_null(inout.parent);
3457 assert_int_equal(inout.nodetype, LYS_OUTPUT);
Michal Vasko7f45cf22020-10-01 12:49:44 +02003458 assert_string_equal(inout.musts->arg.str, "cond");
David Sedlák05404f62019-07-24 14:11:53 +02003459 assert_string_equal(inout.typedefs->name, "tpdf");
3460 assert_string_equal(inout.groupings->name, "sub-grp");
3461 assert_string_equal(inout.data->name, "anyd");
3462 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
3463 assert_string_equal(inout.data->next->name, "anyx");
3464 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
3465 assert_string_equal(inout.data->next->next->name, "choice");
3466 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
3467 assert_string_equal(inout.data->next->next->next->name, "subcont");
3468 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
3469 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
3470 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
3471 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
3472 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3473 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
3474 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
3475 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
3476 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3477 assert_null(inout.data->next->next->next->next->next->next->next->next);
David Sedláke0ef1c62019-09-13 10:05:55 +02003478 assert_string_equal(inout.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003479 assert_int_equal(inout.exts[0].insubstmt_index, 0);
3480 assert_int_equal(inout.exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák05404f62019-07-24 14:11:53 +02003481 lysp_action_inout_free(st->ctx, &inout);
3482 memset(&inout, 0, sizeof inout);
3483
3484 /* min subelems */
Michal Vaskob83af8a2020-01-06 09:49:22 +01003485 data = ELEMENT_WRAPPER_START "<input><leaf name=\"l\"><type name=\"empty\"/></leaf></input>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003486 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_SUCCESS);
Michal Vaskob83af8a2020-01-06 09:49:22 +01003487 lysp_action_inout_free(st->ctx, &inout);
David Sedlák05404f62019-07-24 14:11:53 +02003488 memset(&inout, 0, sizeof inout);
3489
Michal Vaskob83af8a2020-01-06 09:49:22 +01003490 data = ELEMENT_WRAPPER_START "<output><leaf name=\"l\"><type name=\"empty\"/></leaf></output>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003491 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_SUCCESS);
Michal Vaskob83af8a2020-01-06 09:49:22 +01003492 lysp_action_inout_free(st->ctx, &inout);
David Sedlák05404f62019-07-24 14:11:53 +02003493 memset(&inout, 0, sizeof inout);
3494
3495 /* invalid combinations */
3496 data = ELEMENT_WRAPPER_START "<input name=\"test\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003497 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02003498 logbuf_assert("Unexpected attribute \"name\" of \"input\" element. Line number 1.");
David Sedlák05404f62019-07-24 14:11:53 +02003499 memset(&inout, 0, sizeof inout);
3500
3501 st->finished_correctly = true;
3502}
3503
David Sedlák85d0eca2019-07-24 15:15:21 +02003504static void
3505test_action_elem(void **state)
3506{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003507 struct test_parser_yin_state *st = *state;
David Sedlák85d0eca2019-07-24 15:15:21 +02003508 const char *data;
3509 struct lysp_action *actions = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003510 struct tree_node_meta act_meta = {NULL, (struct lysp_node **)&actions};
David Sedlák85d0eca2019-07-24 15:15:21 +02003511
3512 /* max subelems */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003513 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák85d0eca2019-07-24 15:15:21 +02003514 data = ELEMENT_WRAPPER_START
3515 "<action name=\"act\">"
3516 "<description><text>desc</text></description>"
3517 "<grouping name=\"grouping\"/>"
3518 "<if-feature name=\"iff\"/>"
3519 "<input><uses name=\"uses-name\"/></input>"
Michal Vaskob83af8a2020-01-06 09:49:22 +01003520 "<output><must condition=\"cond\"/><leaf name=\"l\"><type name=\"type\"/></leaf></output>"
David Sedlák85d0eca2019-07-24 15:15:21 +02003521 "<reference><text>ref</text></reference>"
3522 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003523 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákd1144562019-08-06 12:36:14 +02003524 EXT_SUBELEM
David Sedlák85d0eca2019-07-24 15:15:21 +02003525 "</action>"
3526 ELEMENT_WRAPPER_END;
Michal Vasko1bf09392020-03-27 12:38:10 +01003527 /* there must be parent for action */
Radek Krejcif482abc2020-08-27 15:29:05 +02003528 act_meta.parent = (void*)1;
Michal Vaskob36053d2020-03-26 15:49:30 +01003529 assert_int_equal(test_element_helper(st, data, &act_meta, NULL, NULL), LY_SUCCESS);
Michal Vasko1bf09392020-03-27 12:38:10 +01003530 act_meta.parent = NULL;
3531 assert_non_null(actions->parent);
David Sedlák85d0eca2019-07-24 15:15:21 +02003532 assert_int_equal(actions->nodetype, LYS_ACTION);
3533 assert_true(actions->flags & LYS_STATUS_DEPRC);
3534 assert_string_equal(actions->name, "act");
3535 assert_string_equal(actions->dsc, "desc");
3536 assert_string_equal(actions->ref, "ref");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003537 assert_string_equal(actions->iffeatures[0].str, "iff");
David Sedlák85d0eca2019-07-24 15:15:21 +02003538 assert_string_equal(actions->typedefs->name, "tpdf");
3539 assert_string_equal(actions->groupings->name, "grouping");
3540 assert_string_equal(actions->input.data->name, "uses-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003541 assert_string_equal(actions->output.musts->arg.str, "cond");
David Sedláke0ef1c62019-09-13 10:05:55 +02003542 assert_string_equal(actions->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003543 assert_int_equal(actions->exts[0].insubstmt_index, 0);
3544 assert_int_equal(actions->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák85d0eca2019-07-24 15:15:21 +02003545 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3546 actions = NULL;
3547
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003548 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlákeaa45792019-07-24 15:25:01 +02003549 data = ELEMENT_WRAPPER_START
3550 "<rpc name=\"act\">"
3551 "<description><text>desc</text></description>"
3552 "<grouping name=\"grouping\"/>"
3553 "<if-feature name=\"iff\"/>"
3554 "<input><uses name=\"uses-name\"/></input>"
Michal Vaskob83af8a2020-01-06 09:49:22 +01003555 "<output><must condition=\"cond\"/><leaf name=\"l\"><type name=\"type\"/></leaf></output>"
David Sedlákeaa45792019-07-24 15:25:01 +02003556 "<reference><text>ref</text></reference>"
3557 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003558 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákd1144562019-08-06 12:36:14 +02003559 EXT_SUBELEM
David Sedlákeaa45792019-07-24 15:25:01 +02003560 "</rpc>"
3561 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003562 assert_int_equal(test_element_helper(st, data, &act_meta, NULL, NULL), LY_SUCCESS);
David Sedlákeaa45792019-07-24 15:25:01 +02003563 assert_null(actions->parent);
Michal Vasko1bf09392020-03-27 12:38:10 +01003564 assert_int_equal(actions->nodetype, LYS_RPC);
David Sedlákeaa45792019-07-24 15:25:01 +02003565 assert_true(actions->flags & LYS_STATUS_DEPRC);
3566 assert_string_equal(actions->name, "act");
3567 assert_string_equal(actions->dsc, "desc");
3568 assert_string_equal(actions->ref, "ref");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003569 assert_string_equal(actions->iffeatures[0].str, "iff");
David Sedlákeaa45792019-07-24 15:25:01 +02003570 assert_string_equal(actions->typedefs->name, "tpdf");
3571 assert_string_equal(actions->groupings->name, "grouping");
3572 assert_string_equal(actions->input.data->name, "uses-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003573 assert_string_equal(actions->output.musts->arg.str, "cond");
David Sedláke0ef1c62019-09-13 10:05:55 +02003574 assert_string_equal(actions->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003575 assert_int_equal(actions->exts[0].insubstmt_index, 0);
3576 assert_int_equal(actions->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákeaa45792019-07-24 15:25:01 +02003577 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3578 actions = NULL;
3579
David Sedlák85d0eca2019-07-24 15:15:21 +02003580 /* min subelems */
3581 data = ELEMENT_WRAPPER_START "<action name=\"act\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003582 assert_int_equal(test_element_helper(st, data, &act_meta, NULL, NULL), LY_SUCCESS);
David Sedlák85d0eca2019-07-24 15:15:21 +02003583 assert_string_equal(actions->name, "act");
3584 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3585 actions = NULL;
3586
3587 st->finished_correctly = true;
3588}
3589
David Sedlák992fb7c2019-07-24 16:51:01 +02003590static void
3591test_augment_elem(void **state)
3592{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003593 struct test_parser_yin_state *st = *state;
David Sedlák992fb7c2019-07-24 16:51:01 +02003594 const char *data;
3595 struct lysp_augment *augments = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003596 struct tree_node_meta aug_meta = {NULL, (struct lysp_node **)&augments};
David Sedlák992fb7c2019-07-24 16:51:01 +02003597
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003598 st->yin_ctx->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák992fb7c2019-07-24 16:51:01 +02003599 data = ELEMENT_WRAPPER_START
3600 "<augment target-node=\"target\">"
3601 "<action name=\"action\"/>"
3602 "<anydata name=\"anyd\"/>"
3603 "<anyxml name=\"anyx\"/>"
3604 "<case name=\"case\"/>"
3605 "<choice name=\"choice\"/>"
3606 "<container name=\"subcont\"/>"
3607 "<description><text>desc</text></description>"
3608 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003609 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3610 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák992fb7c2019-07-24 16:51:01 +02003611 "<list name=\"list\"/>"
3612 "<notification name=\"notif\"/>"
3613 "<reference><text>ref</text></reference>"
3614 "<status value=\"current\"/>"
3615 "<uses name=\"uses\"/>"
3616 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003617 EXT_SUBELEM
David Sedlák992fb7c2019-07-24 16:51:01 +02003618 "</augment>"
3619 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003620 assert_int_equal(test_element_helper(st, data, &aug_meta, NULL, NULL), LY_SUCCESS);
David Sedlák992fb7c2019-07-24 16:51:01 +02003621 assert_string_equal(augments->nodeid, "target");
3622 assert_null(augments->parent);
3623 assert_int_equal(augments->nodetype, LYS_AUGMENT);
3624 assert_true(augments->flags & LYS_STATUS_CURR);
3625 assert_string_equal(augments->dsc, "desc");
3626 assert_string_equal(augments->ref, "ref");
3627 assert_string_equal(augments->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003628 assert_string_equal(augments->iffeatures[0].str, "iff");
David Sedlák992fb7c2019-07-24 16:51:01 +02003629 assert_string_equal(augments->child->name, "anyd");
3630 assert_int_equal(augments->child->nodetype, LYS_ANYDATA);
3631 assert_string_equal(augments->child->next->name, "anyx");
3632 assert_int_equal(augments->child->next->nodetype, LYS_ANYXML);
3633 assert_string_equal(augments->child->next->next->name, "case");
3634 assert_int_equal(augments->child->next->next->nodetype, LYS_CASE);
3635 assert_string_equal(augments->child->next->next->next->name, "choice");
3636 assert_int_equal(augments->child->next->next->next->nodetype, LYS_CHOICE);
3637 assert_string_equal(augments->child->next->next->next->next->name, "subcont");
3638 assert_int_equal(augments->child->next->next->next->next->nodetype, LYS_CONTAINER);
3639 assert_string_equal(augments->child->next->next->next->next->next->name, "leaf");
3640 assert_int_equal(augments->child->next->next->next->next->next->nodetype, LYS_LEAF);
3641 assert_string_equal(augments->child->next->next->next->next->next->next->name, "llist");
3642 assert_int_equal(augments->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3643 assert_string_equal(augments->child->next->next->next->next->next->next->next->name, "list");
3644 assert_int_equal(augments->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
3645 assert_string_equal(augments->child->next->next->next->next->next->next->next->next->name, "uses");
3646 assert_int_equal(augments->child->next->next->next->next->next->next->next->next->nodetype, LYS_USES);
3647 assert_null(augments->child->next->next->next->next->next->next->next->next->next);
3648 assert_string_equal(augments->actions->name, "action");
3649 assert_string_equal(augments->notifs->name, "notif");
David Sedláke0ef1c62019-09-13 10:05:55 +02003650 assert_string_equal(augments->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003651 assert_int_equal(augments->exts[0].insubstmt_index, 0);
3652 assert_int_equal(augments->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák992fb7c2019-07-24 16:51:01 +02003653 FREE_ARRAY(st->ctx, augments, lysp_augment_free)
3654 augments = NULL;
3655
3656 data = ELEMENT_WRAPPER_START "<augment target-node=\"target\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003657 assert_int_equal(test_element_helper(st, data, &aug_meta, NULL, NULL), LY_SUCCESS);
David Sedlák992fb7c2019-07-24 16:51:01 +02003658 assert_string_equal(augments->nodeid, "target");
3659 FREE_ARRAY(st->ctx, augments, lysp_augment_free)
3660 augments = NULL;
3661
3662 st->finished_correctly = true;
3663}
3664
David Sedlák4ffcec82019-07-25 15:10:21 +02003665static void
3666test_deviate_elem(void **state)
3667{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003668 struct test_parser_yin_state *st = *state;
David Sedlák4ffcec82019-07-25 15:10:21 +02003669 const char *data;
3670 struct lysp_deviate *deviates = NULL;
3671 struct lysp_deviate_add *d_add;
3672 struct lysp_deviate_rpl *d_rpl;
3673 struct lysp_deviate_del *d_del;
3674
3675 /* all valid arguments with min subelems */
3676 data = ELEMENT_WRAPPER_START "<deviate value=\"not-supported\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003677 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003678 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
3679 lysp_deviate_free(st->ctx, deviates);
3680 free(deviates);
3681 deviates = NULL;
3682
3683 data = ELEMENT_WRAPPER_START "<deviate value=\"add\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003684 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003685 assert_int_equal(deviates->mod, LYS_DEV_ADD);
3686 lysp_deviate_free(st->ctx, deviates);
3687 free(deviates);
3688 deviates = NULL;
3689
3690 data = ELEMENT_WRAPPER_START "<deviate value=\"replace\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003691 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003692 assert_int_equal(deviates->mod, LYS_DEV_REPLACE);
3693 lysp_deviate_free(st->ctx, deviates);
3694 free(deviates);
3695 deviates = NULL;
3696
3697 data = ELEMENT_WRAPPER_START "<deviate value=\"delete\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003698 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003699 assert_int_equal(deviates->mod, LYS_DEV_DELETE);
3700 lysp_deviate_free(st->ctx, deviates);
3701 free(deviates);
3702 deviates = NULL;
3703
3704 /* max subelems and valid arguments */
3705 data = ELEMENT_WRAPPER_START
3706 "<deviate value=\"not-supported\">"
David Sedlákd1144562019-08-06 12:36:14 +02003707 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003708 "</deviate>"
3709 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_NOT_SUPPORTED);
David Sedláke0ef1c62019-09-13 10:05:55 +02003712 assert_string_equal(deviates->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003713 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3714 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003715 lysp_deviate_free(st->ctx, deviates);
3716 free(deviates);
3717 deviates = NULL;
3718
3719 data = ELEMENT_WRAPPER_START
3720 "<deviate value=\"add\">"
3721 "<units name=\"units\"/>"
3722 "<must condition=\"cond\"/>"
3723 "<unique tag=\"utag\"/>"
3724 "<default value=\"def\"/>"
3725 "<config value=\"true\"/>"
3726 "<mandatory value=\"true\"/>"
3727 "<min-elements value=\"5\"/>"
3728 "<max-elements value=\"15\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003729 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003730 "</deviate>"
3731 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003732 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003733 d_add = (struct lysp_deviate_add *)deviates;
3734 assert_int_equal(d_add->mod, LYS_DEV_ADD);
3735 assert_null(d_add->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003736 assert_string_equal(d_add->units, "units");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003737 assert_string_equal(d_add->musts->arg.str, "cond");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003738 assert_string_equal(d_add->uniques[0].str, "utag");
3739 assert_string_equal(d_add->dflts[0].str, "def");
David Sedlák4ffcec82019-07-25 15:10:21 +02003740 assert_true(d_add->flags & LYS_MAND_TRUE && d_add->flags & LYS_CONFIG_W);
3741 assert_int_equal(d_add->min, 5);
3742 assert_int_equal(d_add->max, 15);
David Sedláke0ef1c62019-09-13 10:05:55 +02003743 assert_string_equal(deviates->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003744 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3745 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003746 lysp_deviate_free(st->ctx, deviates);
3747 free(deviates);
3748 deviates = NULL;
3749
3750 data = ELEMENT_WRAPPER_START
3751 "<deviate value=\"replace\">"
3752 "<type name=\"newtype\"/>"
3753 "<units name=\"uni\"/>"
3754 "<default value=\"def\"/>"
3755 "<config value=\"true\"/>"
3756 "<mandatory value=\"true\"/>"
3757 "<min-elements value=\"5\"/>"
3758 "<max-elements value=\"15\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003759 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003760 "</deviate>"
3761 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003762 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003763 d_rpl = (struct lysp_deviate_rpl *)deviates;
3764 assert_int_equal(d_rpl->mod, LYS_DEV_REPLACE);
3765 assert_null(d_rpl->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003766 assert_string_equal(d_rpl->type->name, "newtype");
3767 assert_string_equal(d_rpl->units, "uni");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003768 assert_string_equal(d_rpl->dflt.str, "def");
David Sedlák4ffcec82019-07-25 15:10:21 +02003769 assert_true(d_rpl->flags & LYS_MAND_TRUE && d_rpl->flags & LYS_CONFIG_W);
3770 assert_int_equal(d_rpl->min, 5);
3771 assert_int_equal(d_rpl->max, 15);
David Sedláke0ef1c62019-09-13 10:05:55 +02003772 assert_string_equal(deviates->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003773 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3774 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003775 lysp_deviate_free(st->ctx, deviates);
3776 free(deviates);
3777 deviates = NULL;
3778
3779 data = ELEMENT_WRAPPER_START
3780 "<deviate value=\"delete\">"
3781 "<units name=\"u\"/>"
3782 "<must condition=\"c\"/>"
3783 "<unique tag=\"tag\"/>"
3784 "<default value=\"default\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003785 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003786 "</deviate>"
3787 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003788 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003789 d_del = (struct lysp_deviate_del *)deviates;
3790 assert_int_equal(d_del->mod, LYS_DEV_DELETE);
3791 assert_null(d_del->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003792 assert_string_equal(d_del->units, "u");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003793 assert_string_equal(d_del->musts->arg.str, "c");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003794 assert_string_equal(d_del->uniques[0].str, "tag");
3795 assert_string_equal(d_del->dflts[0].str, "default");
David Sedláke0ef1c62019-09-13 10:05:55 +02003796 assert_string_equal(deviates->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003797 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3798 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003799 lysp_deviate_free(st->ctx, deviates);
3800 free(deviates);
3801 deviates = NULL;
3802
3803 /* invalid arguments */
3804 data = ELEMENT_WRAPPER_START "<deviate value=\"\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003805 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003806 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 +02003807 deviates = NULL;
3808
3809 data = ELEMENT_WRAPPER_START "<deviate value=\"invalid\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003810 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003811 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 +02003812 deviates = NULL;
3813
3814 data = ELEMENT_WRAPPER_START "<deviate value=\"ad\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003815 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003816 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 +02003817 deviates = NULL;
3818
3819 data = ELEMENT_WRAPPER_START "<deviate value=\"adds\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003820 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003821 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 +02003822 deviates = NULL;
3823
3824 data = ELEMENT_WRAPPER_START
3825 "<deviate value=\"not-supported\">"
3826 "<must condition=\"c\"/>"
3827 "</deviate>"
3828 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ák4ffcec82019-07-25 15:10:21 +02003830 logbuf_assert("Deviate of this type doesn't allow \"must\" as it's sub-element. Line number 1.");
3831
3832 st->finished_correctly = true;
3833}
3834
David Sedlák8b754462019-07-25 16:22:13 +02003835static void
3836test_deviation_elem(void **state)
3837{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003838 struct test_parser_yin_state *st = *state;
David Sedlák8b754462019-07-25 16:22:13 +02003839 const char *data;
3840 struct lysp_deviation *deviations = NULL;
3841
3842 /* min subelems */
3843 data = ELEMENT_WRAPPER_START
3844 "<deviation target-node=\"target\">"
3845 "<deviate value=\"not-supported\"/>"
3846 "</deviation>"
3847 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003848 assert_int_equal(test_element_helper(st, data, &deviations, NULL, NULL), LY_SUCCESS);
David Sedlák8b754462019-07-25 16:22:13 +02003849 assert_string_equal(deviations->nodeid, "target");
3850 assert_int_equal(deviations->deviates->mod, LYS_DEV_NOT_SUPPORTED);
3851 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3852 deviations = NULL;
3853
3854 /* max subelems */
3855 data = ELEMENT_WRAPPER_START
3856 "<deviation target-node=\"target\">"
3857 "<reference><text>ref</text></reference>"
3858 "<description><text>desc</text></description>"
3859 "<deviate value=\"add\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003860 EXT_SUBELEM
David Sedlák8b754462019-07-25 16:22:13 +02003861 "</deviation>"
3862 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003863 assert_int_equal(test_element_helper(st, data, &deviations, NULL, NULL), LY_SUCCESS);
David Sedlák8b754462019-07-25 16:22:13 +02003864 assert_string_equal(deviations->nodeid, "target");
3865 assert_int_equal(deviations->deviates->mod, LYS_DEV_ADD);
3866 assert_string_equal(deviations->ref, "ref");
3867 assert_string_equal(deviations->dsc, "desc");
David Sedláke0ef1c62019-09-13 10:05:55 +02003868 assert_string_equal(deviations->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003869 assert_int_equal(deviations->exts[0].insubstmt_index, 0);
3870 assert_int_equal(deviations->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8b754462019-07-25 16:22:13 +02003871 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3872 deviations = NULL;
3873
3874 /* invalid */
3875 data = ELEMENT_WRAPPER_START "<deviation target-node=\"target\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003876 assert_int_equal(test_element_helper(st, data, &deviations, NULL, NULL), LY_EVALID);
David Sedlák8b754462019-07-25 16:22:13 +02003877 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3878 deviations = NULL;
David Sedlák1538a842019-08-08 15:38:51 +02003879 logbuf_assert("Missing mandatory sub-element \"deviate\" of \"deviation\" element. Line number 1.");
3880 /* TODO */
David Sedlák8b754462019-07-25 16:22:13 +02003881 st->finished_correctly = true;
3882}
3883
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003884static struct lysp_module *
3885mod_renew(struct lys_yin_parser_ctx *ctx)
3886{
3887 struct ly_ctx *ly_ctx = ctx->parsed_mod->mod->ctx;
3888
3889 lys_module_free(ctx->parsed_mod->mod, NULL);
3890 ctx->parsed_mod = calloc(1, sizeof *ctx->parsed_mod);
3891 ctx->parsed_mod->mod = calloc(1, sizeof *ctx->parsed_mod->mod);
3892 ctx->parsed_mod->mod->parsed = ctx->parsed_mod;
3893 ctx->parsed_mod->mod->ctx = ly_ctx;
3894
3895 return ctx->parsed_mod;
3896}
3897
David Sedlák4f03b932019-07-26 13:01:47 +02003898static void
3899test_module_elem(void **state)
3900{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003901 struct test_parser_yin_state *st = *state;
Michal Vaskob36053d2020-03-26 15:49:30 +01003902 const char *data;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003903 struct lysp_module *lysp_mod = mod_renew(st->yin_ctx);
David Sedlák4f03b932019-07-26 13:01:47 +02003904
3905 /* max subelems */
David Sedlák4f03b932019-07-26 13:01:47 +02003906 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
3907 "<yang-version value=\"1.1\"/>\n"
3908 "<namespace uri=\"ns\"/>\n"
3909 "<prefix value=\"pref\"/>\n"
3910 "<include module=\"b-mod\"/>\n"
3911 "<import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
3912 "<organization><text>org</text></organization>\n"
3913 "<contact><text>contact</text></contact>\n"
3914 "<description><text>desc</text></description>"
3915 "<reference><text>ref</text></reference>\n"
3916 "<revision date=\"2019-02-02\"/>\n"
3917 "<anydata name=\"anyd\"/>\n"
3918 "<anyxml name=\"anyx\"/>\n"
3919 "<choice name=\"choice\"/>\n"
3920 "<container name=\"cont\"/>\n"
3921 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3922 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3923 "<list name=\"sub-list\"/>\n"
3924 "<uses name=\"uses-name\"/>\n"
3925 "<augment target-node=\"target\"/>\n"
3926 "<deviation target-node=\"target\">""<deviate value=\"not-supported\"/>""</deviation>\n"
3927 "<extension name=\"ext\"/>\n"
3928 "<feature name=\"feature\"/>\n"
3929 "<grouping name=\"grp\"/>\n"
3930 "<identity name=\"ident-name\"/>\n"
3931 "<notification name=\"notf\"/>\n"
3932 "<rpc name=\"rpc-name\"/>\n"
3933 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
David Sedlákd1144562019-08-06 12:36:14 +02003934 EXT_SUBELEM"\n"
David Sedlák4f03b932019-07-26 13:01:47 +02003935 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003936 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
3937 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01003938
3939 assert_int_equal(yin_parse_mod(st->yin_ctx, lysp_mod), LY_SUCCESS);
David Sedlák4f03b932019-07-26 13:01:47 +02003940 assert_string_equal(lysp_mod->mod->name, "mod");
3941 assert_string_equal(lysp_mod->revs, "2019-02-02");
3942 assert_string_equal(lysp_mod->mod->ns, "ns");
3943 assert_string_equal(lysp_mod->mod->prefix, "pref");
3944 assert_null(lysp_mod->mod->filepath);
3945 assert_string_equal(lysp_mod->mod->org, "org");
3946 assert_string_equal(lysp_mod->mod->contact, "contact");
3947 assert_string_equal(lysp_mod->mod->dsc, "desc");
3948 assert_string_equal(lysp_mod->mod->ref, "ref");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003949 assert_int_equal(lysp_mod->version, LYS_VERSION_1_1);
David Sedlák4f03b932019-07-26 13:01:47 +02003950 assert_string_equal(lysp_mod->imports->name, "a-mod");
3951 assert_string_equal(lysp_mod->includes->name, "b-mod");
3952 assert_string_equal(lysp_mod->extensions->name, "ext");
3953 assert_string_equal(lysp_mod->features->name, "feature");
3954 assert_string_equal(lysp_mod->identities->name, "ident-name");
3955 assert_string_equal(lysp_mod->typedefs->name, "tpdf");
3956 assert_string_equal(lysp_mod->groupings->name, "grp");
3957 assert_string_equal(lysp_mod->data->name, "anyd");
3958 assert_int_equal(lysp_mod->data->nodetype, LYS_ANYDATA);
3959 assert_string_equal(lysp_mod->data->next->name, "anyx");
3960 assert_int_equal(lysp_mod->data->next->nodetype, LYS_ANYXML);
3961 assert_string_equal(lysp_mod->data->next->next->name, "choice");
3962 assert_int_equal(lysp_mod->data->next->next->nodetype, LYS_CHOICE);
3963 assert_string_equal(lysp_mod->data->next->next->next->name, "cont");
3964 assert_int_equal(lysp_mod->data->next->next->next->nodetype, LYS_CONTAINER);
3965 assert_string_equal(lysp_mod->data->next->next->next->next->name, "leaf");
3966 assert_int_equal(lysp_mod->data->next->next->next->next->nodetype, LYS_LEAF);
3967 assert_string_equal(lysp_mod->data->next->next->next->next->next->name, "llist");
3968 assert_int_equal(lysp_mod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3969 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->name, "sub-list");
3970 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
3971 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->next->name, "uses-name");
3972 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3973 assert_null(lysp_mod->data->next->next->next->next->next->next->next->next);
3974 assert_string_equal(lysp_mod->augments->nodeid, "target");
3975 assert_string_equal(lysp_mod->rpcs->name, "rpc-name");
3976 assert_string_equal(lysp_mod->notifs->name, "notf");
3977 assert_string_equal(lysp_mod->deviations->nodeid, "target");
David Sedláke0ef1c62019-09-13 10:05:55 +02003978 assert_string_equal(lysp_mod->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003979 assert_int_equal(lysp_mod->exts[0].insubstmt_index, 0);
3980 assert_int_equal(lysp_mod->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4f03b932019-07-26 13:01:47 +02003981
3982 /* min subelems */
Michal Vasko63f3d842020-07-08 10:10:14 +02003983 ly_in_free(st->in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +01003984 lyxml_ctx_free(st->yin_ctx->xmlctx);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003985 lysp_mod = mod_renew(st->yin_ctx);
David Sedlák4f03b932019-07-26 13:01:47 +02003986 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">"
3987 "<namespace uri=\"ns\"/>"
3988 "<prefix value=\"pref\"/>"
3989 "<yang-version value=\"1.1\"/>"
3990 "</module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003991 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
3992 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01003993 assert_int_equal(yin_parse_mod(st->yin_ctx, lysp_mod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003994 assert_string_equal(lysp_mod->mod->name, "mod");
David Sedlák298ff6d2019-07-26 14:29:03 +02003995
David Sedláke6cd89e2019-08-07 12:46:02 +02003996 /* incorrect subelem order */
Michal Vasko63f3d842020-07-08 10:10:14 +02003997 ly_in_free(st->in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +01003998 lyxml_ctx_free(st->yin_ctx->xmlctx);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003999 lysp_mod = mod_renew(st->yin_ctx);
David Sedláke6cd89e2019-08-07 12:46:02 +02004000 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">"
4001 "<feature name=\"feature\"/>\n"
4002 "<namespace uri=\"ns\"/>"
4003 "<prefix value=\"pref\"/>"
4004 "<yang-version value=\"1.1\"/>"
4005 "</module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004006 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
4007 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01004008 assert_int_equal(yin_parse_mod(st->yin_ctx, lysp_mod), LY_EVALID);
4009 logbuf_assert("Invalid order of module\'s sub-elements \"namespace\" can\'t appear after \"feature\". Line number 2.");
David Sedláke6cd89e2019-08-07 12:46:02 +02004010
David Sedlák298ff6d2019-07-26 14:29:03 +02004011 st->finished_correctly = true;
4012}
4013
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004014static struct lysp_submodule *
4015submod_renew(struct lys_yin_parser_ctx *ctx, const char *belongs_to)
4016{
4017 struct ly_ctx *ly_ctx = ctx->parsed_mod->mod->ctx;
4018
4019 lys_module_free(ctx->parsed_mod->mod, NULL);
4020 ctx->parsed_mod = calloc(1, sizeof(struct lysp_submodule));
4021 ctx->parsed_mod->mod = calloc(1, sizeof *ctx->parsed_mod->mod);
4022 lydict_insert(ly_ctx, belongs_to, 0, &ctx->parsed_mod->mod->name);
4023 ctx->parsed_mod->mod->parsed = ctx->parsed_mod;
4024 ctx->parsed_mod->mod->ctx = ly_ctx;
4025
4026 return (struct lysp_submodule *)ctx->parsed_mod;
4027}
4028
David Sedlák298ff6d2019-07-26 14:29:03 +02004029static void
4030test_submodule_elem(void **state)
4031{
Radek Krejci3a4889a2020-05-19 17:01:58 +02004032 struct test_parser_yin_state *st = *state;
Michal Vaskob36053d2020-03-26 15:49:30 +01004033 const char *data;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004034 struct lysp_submodule *lysp_submod = submod_renew(st->yin_ctx, "module-name");
David Sedlák298ff6d2019-07-26 14:29:03 +02004035
4036 /* max subelements */
David Sedlák298ff6d2019-07-26 14:29:03 +02004037 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
4038 "<yang-version value=\"1.1\"/>\n"
Michal Vaskoc3781c32020-10-06 14:04:08 +02004039 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>"
David Sedlák298ff6d2019-07-26 14:29:03 +02004040 "<include module=\"b-mod\"/>\n"
4041 "<import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
4042 "<organization><text>org</text></organization>\n"
4043 "<contact><text>contact</text></contact>\n"
4044 "<description><text>desc</text></description>"
4045 "<reference><text>ref</text></reference>\n"
4046 "<revision date=\"2019-02-02\"/>\n"
4047 "<anydata name=\"anyd\"/>\n"
4048 "<anyxml name=\"anyx\"/>\n"
4049 "<choice name=\"choice\"/>\n"
4050 "<container name=\"cont\"/>\n"
4051 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
4052 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
4053 "<list name=\"sub-list\"/>\n"
4054 "<uses name=\"uses-name\"/>\n"
4055 "<augment target-node=\"target\"/>\n"
4056 "<deviation target-node=\"target\">""<deviate value=\"not-supported\"/>""</deviation>\n"
4057 "<extension name=\"ext\"/>\n"
4058 "<feature name=\"feature\"/>\n"
4059 "<grouping name=\"grp\"/>\n"
4060 "<identity name=\"ident-name\"/>\n"
4061 "<notification name=\"notf\"/>\n"
4062 "<rpc name=\"rpc-name\"/>\n"
4063 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
David Sedlákd1144562019-08-06 12:36:14 +02004064 EXT_SUBELEM"\n"
David Sedlák298ff6d2019-07-26 14:29:03 +02004065 "</submodule>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004066 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
4067 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02004068
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004069 assert_int_equal(yin_parse_submod(st->yin_ctx, lysp_submod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02004070 assert_string_equal(lysp_submod->name, "mod");
4071 assert_string_equal(lysp_submod->revs, "2019-02-02");
David Sedlák298ff6d2019-07-26 14:29:03 +02004072 assert_string_equal(lysp_submod->prefix, "pref");
4073 assert_null(lysp_submod->filepath);
4074 assert_string_equal(lysp_submod->org, "org");
4075 assert_string_equal(lysp_submod->contact, "contact");
4076 assert_string_equal(lysp_submod->dsc, "desc");
4077 assert_string_equal(lysp_submod->ref, "ref");
4078 assert_int_equal(lysp_submod->version, LYS_VERSION_1_1);
4079 assert_string_equal(lysp_submod->imports->name, "a-mod");
4080 assert_string_equal(lysp_submod->includes->name, "b-mod");
4081 assert_string_equal(lysp_submod->extensions->name, "ext");
4082 assert_string_equal(lysp_submod->features->name, "feature");
4083 assert_string_equal(lysp_submod->identities->name, "ident-name");
4084 assert_string_equal(lysp_submod->typedefs->name, "tpdf");
4085 assert_string_equal(lysp_submod->groupings->name, "grp");
4086 assert_string_equal(lysp_submod->data->name, "anyd");
4087 assert_int_equal(lysp_submod->data->nodetype, LYS_ANYDATA);
4088 assert_string_equal(lysp_submod->data->next->name, "anyx");
4089 assert_int_equal(lysp_submod->data->next->nodetype, LYS_ANYXML);
4090 assert_string_equal(lysp_submod->data->next->next->name, "choice");
4091 assert_int_equal(lysp_submod->data->next->next->nodetype, LYS_CHOICE);
4092 assert_string_equal(lysp_submod->data->next->next->next->name, "cont");
4093 assert_int_equal(lysp_submod->data->next->next->next->nodetype, LYS_CONTAINER);
4094 assert_string_equal(lysp_submod->data->next->next->next->next->name, "leaf");
4095 assert_int_equal(lysp_submod->data->next->next->next->next->nodetype, LYS_LEAF);
4096 assert_string_equal(lysp_submod->data->next->next->next->next->next->name, "llist");
4097 assert_int_equal(lysp_submod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
4098 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->name, "sub-list");
4099 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
4100 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->next->name, "uses-name");
4101 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
4102 assert_null(lysp_submod->data->next->next->next->next->next->next->next->next);
4103 assert_string_equal(lysp_submod->augments->nodeid, "target");
4104 assert_string_equal(lysp_submod->rpcs->name, "rpc-name");
4105 assert_string_equal(lysp_submod->notifs->name, "notf");
4106 assert_string_equal(lysp_submod->deviations->nodeid, "target");
David Sedláke0ef1c62019-09-13 10:05:55 +02004107 assert_string_equal(lysp_submod->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02004108 assert_int_equal(lysp_submod->exts[0].insubstmt_index, 0);
4109 assert_int_equal(lysp_submod->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák298ff6d2019-07-26 14:29:03 +02004110
David Sedlák298ff6d2019-07-26 14:29:03 +02004111 /* min subelemnts */
Michal Vasko63f3d842020-07-08 10:10:14 +02004112 ly_in_free(st->in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +01004113 lyxml_ctx_free(st->yin_ctx->xmlctx);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004114 lysp_submod = submod_renew(st->yin_ctx, "module-name");
David Sedlák298ff6d2019-07-26 14:29:03 +02004115 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">"
Radek Krejci96e48da2020-09-04 13:18:06 +02004116 "<yang-version value=\"1\"/>"
Michal Vaskoc3781c32020-10-06 14:04:08 +02004117 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>"
David Sedlák298ff6d2019-07-26 14:29:03 +02004118 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004119 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
4120 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01004121 assert_int_equal(yin_parse_submod(st->yin_ctx, lysp_submod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02004122 assert_string_equal(lysp_submod->prefix, "pref");
David Sedlák298ff6d2019-07-26 14:29:03 +02004123 assert_int_equal(lysp_submod->version, LYS_VERSION_1_0);
David Sedlák298ff6d2019-07-26 14:29:03 +02004124
David Sedláke6cd89e2019-08-07 12:46:02 +02004125 /* incorrect subelem order */
Michal Vasko63f3d842020-07-08 10:10:14 +02004126 ly_in_free(st->in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +01004127 lyxml_ctx_free(st->yin_ctx->xmlctx);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004128 lysp_submod = submod_renew(st->yin_ctx, "module-name");
David Sedláke6cd89e2019-08-07 12:46:02 +02004129 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">"
Radek Krejci96e48da2020-09-04 13:18:06 +02004130 "<yang-version value=\"1\"/>"
David Sedláke6cd89e2019-08-07 12:46:02 +02004131 "<reference><text>ref</text></reference>\n"
Michal Vaskoc3781c32020-10-06 14:04:08 +02004132 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>"
David Sedláke6cd89e2019-08-07 12:46:02 +02004133 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004134 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
4135 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01004136 assert_int_equal(yin_parse_submod(st->yin_ctx, lysp_submod), LY_EVALID);
4137 logbuf_assert("Invalid order of submodule's sub-elements \"belongs-to\" can't appear after \"reference\". Line number 2.");
David Sedláke6cd89e2019-08-07 12:46:02 +02004138
David Sedlák298ff6d2019-07-26 14:29:03 +02004139 st->finished_correctly = true;
David Sedlák4f03b932019-07-26 13:01:47 +02004140}
4141
David Sedlák8985a142019-07-31 16:43:06 +02004142static void
4143test_yin_parse_module(void **state)
4144{
Radek Krejci3a4889a2020-05-19 17:01:58 +02004145 struct test_parser_yin_state *st = *state;
David Sedlák8985a142019-07-31 16:43:06 +02004146 const char *data;
4147 struct lys_module *mod;
Michal Vaskob36053d2020-03-26 15:49:30 +01004148 struct lys_yin_parser_ctx *yin_ctx = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02004149 struct ly_in *in = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02004150
4151 mod = calloc(1, sizeof *mod);
4152 mod->ctx = st->ctx;
David Sedlákd2844882019-09-13 16:01:22 +02004153 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"
4154 "<yang-version value=\"1.1\"/>\n"
4155 "<namespace uri=\"urn:tests:extensions:metadata:a\"/>\n"
4156 "<prefix value=\"a\"/>\n"
4157 "<import module=\"ietf-yang-metadata\">\n"
4158 "<prefix value=\"md\"/>\n"
4159 "</import>\n"
4160 "<feature name=\"f\"/>\n"
4161 "<md:annotation name=\"x\">\n"
4162 "<description>\n"
4163 "<text>test</text>\n"
4164 "</description>\n"
4165 "<reference>\n"
4166 "<text>test</text>\n"
4167 "</reference>\n"
4168 "<if-feature name=\"f\"/>\n"
4169 "<status value=\"current\"/>\n"
4170 "<type name=\"uint8\"/>\n"
4171 "<units name=\"meters\"/>\n"
4172 "</md:annotation>\n"
4173 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004174 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
4175 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS);
David Sedlákd2844882019-09-13 16:01:22 +02004176 assert_null(mod->parsed->exts->child->next->child);
4177 assert_string_equal(mod->parsed->exts->child->next->arg, "test");
4178 lys_module_free(mod, NULL);
4179 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004180 ly_in_free(in, 0);
David Sedlákd2844882019-09-13 16:01:22 +02004181 mod = NULL;
4182 yin_ctx = NULL;
4183
4184 mod = calloc(1, sizeof *mod);
4185 mod->ctx = st->ctx;
David Sedlák8985a142019-07-31 16:43:06 +02004186 data = "<module name=\"example-foo\""
4187 "xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
4188 "xmlns:foo=\"urn:example:foo\""
4189 "xmlns:myext=\"urn:example:extensions\">\n"
4190
Radek Krejci96e48da2020-09-04 13:18:06 +02004191 "<yang-version value=\"1\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02004192
4193 "<namespace uri=\"urn:example:foo\"/>\n"
4194 "<prefix value=\"foo\"/>\n"
4195
4196 "<import module=\"example-extensions\">\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004197 "<prefix value=\"myext\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02004198 "</import>\n"
4199
4200 "<list name=\"interface\">\n"
4201 "<key value=\"name\"/>\n"
4202 "<leaf name=\"name\">\n"
4203 "<type name=\"string\"/>\n"
4204 "</leaf>\n"
4205 "<leaf name=\"mtu\">\n"
4206 "<type name=\"uint32\"/>\n"
4207 "<description>\n"
4208 "<text>The MTU of the interface.</text>\n"
4209 "</description>\n"
4210 "<myext:c-define name=\"MY_MTU\"/>\n"
4211 "</leaf>\n"
4212 "</list>\n"
4213 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004214 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
4215 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS);
David Sedlák8985a142019-07-31 16:43:06 +02004216 lys_module_free(mod, NULL);
4217 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004218 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02004219 mod = NULL;
4220 yin_ctx = NULL;
4221
4222 mod = calloc(1, sizeof *mod);
4223 mod->ctx = st->ctx;
David Sedlák6d781b62019-08-02 15:22:52 +02004224 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
Radek Krejci96e48da2020-09-04 13:18:06 +02004225 "<yang-version value=\"1\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004226 "<namespace uri=\"urn:example:foo\"/>\n"
4227 "<prefix value=\"foo\"/>\n"
4228 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004229 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
4230 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS);
David Sedlák6d781b62019-08-02 15:22:52 +02004231 lys_module_free(mod, NULL);
4232 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004233 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02004234 mod = NULL;
4235 yin_ctx = NULL;
4236
4237
4238 mod = calloc(1, sizeof *mod);
4239 mod->ctx = st->ctx;
David Sedlák8985a142019-07-31 16:43:06 +02004240 data = "<submodule name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
4241 "</submodule>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004242 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
4243 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_EINVAL);
David Sedlák8985a142019-07-31 16:43:06 +02004244 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
4245 lys_module_free(mod, NULL);
4246 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004247 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02004248
David Sedlák6d781b62019-08-02 15:22:52 +02004249 mod = calloc(1, sizeof *mod);
4250 mod->ctx = st->ctx;
4251 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
Radek Krejci96e48da2020-09-04 13:18:06 +02004252 "<yang-version value=\"1\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004253 "<namespace uri=\"urn:example:foo\"/>\n"
4254 "<prefix value=\"foo\"/>\n"
4255 "</module>"
4256 "<module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004257 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
4258 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_EVALID);
David Sedlák6d781b62019-08-02 15:22:52 +02004259 logbuf_assert("Trailing garbage \"<module>\" after module, expected end-of-input. Line number 5.");
4260 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ák8985a142019-07-31 16:43:06 +02004266 st->finished_correctly = true;
4267}
4268
4269static void
4270test_yin_parse_submodule(void **state)
4271{
Radek Krejci3a4889a2020-05-19 17:01:58 +02004272 struct test_parser_yin_state *st = *state;
David Sedlák8985a142019-07-31 16:43:06 +02004273 const char *data;
Michal Vaskob36053d2020-03-26 15:49:30 +01004274 struct lys_yin_parser_ctx *yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02004275 struct lysp_submodule *submod = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02004276 struct ly_in *in;
David Sedlák8985a142019-07-31 16:43:06 +02004277
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004278 lydict_insert(st->ctx, "a", 0, &st->yin_ctx->parsed_mod->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02004279
David Sedlák8985a142019-07-31 16:43:06 +02004280 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
4281 "<submodule name=\"asub\""
4282 "xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
4283 "xmlns:a=\"urn:a\">"
Radek Krejci96e48da2020-09-04 13:18:06 +02004284 "<yang-version value=\"1\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02004285 "<belongs-to module=\"a\">"
4286 "<prefix value=\"a_pref\"/>"
4287 "</belongs-to>"
4288 "<include module=\"atop\"/>"
4289 "<feature name=\"fox\"/>"
4290 "<notification name=\"bar-notif\">"
4291 "<if-feature name=\"bar\"/>"
4292 "</notification>"
4293 "<notification name=\"fox-notif\">"
4294 "<if-feature name=\"fox\"/>"
4295 "</notification>"
4296 "<augment target-node=\"/a_pref:top\">"
4297 "<if-feature name=\"bar\"/>"
4298 "<container name=\"bar-sub\"/>"
4299 "</augment>"
4300 "<augment target-node=\"/top\">"
4301 "<container name=\"bar-sub2\"/>"
4302 "</augment>"
4303 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004304 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004305 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, (struct lys_parser_ctx *)st->yin_ctx, in, &submod), LY_SUCCESS);
4306 lysp_module_free((struct lysp_module *)submod);
David Sedlák8985a142019-07-31 16:43:06 +02004307 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004308 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02004309 yin_ctx = NULL;
4310 submod = NULL;
4311
4312 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
David Sedlák6d781b62019-08-02 15:22:52 +02004313 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
Radek Krejci96e48da2020-09-04 13:18:06 +02004314 "<yang-version value=\"1\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004315 "<belongs-to module=\"a\">"
4316 "<prefix value=\"a_pref\"/>"
4317 "</belongs-to>"
4318 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004319 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004320 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, (struct lys_parser_ctx *)st->yin_ctx, in, &submod), LY_SUCCESS);
4321 lysp_module_free((struct lysp_module *)submod);
David Sedlák6d781b62019-08-02 15:22:52 +02004322 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004323 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02004324 yin_ctx = NULL;
4325 submod = NULL;
4326
4327 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
David Sedlák8985a142019-07-31 16:43:06 +02004328 "<module name=\"inval\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
4329 "</module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004330 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004331 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 +02004332 logbuf_assert("Input data contains module in situation when a submodule is expected.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004333 lysp_module_free((struct lysp_module *)submod);
David Sedlák8985a142019-07-31 16:43:06 +02004334 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004335 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02004336 yin_ctx = NULL;
4337 submod = NULL;
4338
David Sedlák6d781b62019-08-02 15:22:52 +02004339 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
4340 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
Radek Krejci96e48da2020-09-04 13:18:06 +02004341 "<yang-version value=\"1\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004342 "<belongs-to module=\"a\">"
4343 "<prefix value=\"a_pref\"/>"
4344 "</belongs-to>"
4345 "</submodule>"
4346 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
Radek Krejci96e48da2020-09-04 13:18:06 +02004347 "<yang-version value=\"1\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004348 "<belongs-to module=\"a\">"
4349 "<prefix value=\"a_pref\"/>"
4350 "</belongs-to>"
4351 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004352 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004353 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, (struct lys_parser_ctx *)st->yin_ctx, in, &submod), LY_EVALID);
David Sedlák6d781b62019-08-02 15:22:52 +02004354 logbuf_assert("Trailing garbage \"<submodule name...\" after submodule, expected end-of-input. Line number 2.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004355 lysp_module_free((struct lysp_module *)submod);
David Sedlák6d781b62019-08-02 15:22:52 +02004356 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004357 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02004358 yin_ctx = NULL;
4359 submod = NULL;
4360
David Sedlák8985a142019-07-31 16:43:06 +02004361 st->finished_correctly = true;
4362}
4363
David Sedlák3b4db242018-10-19 16:11:01 +02004364int
4365main(void)
4366{
4367
4368 const struct CMUnitTest tests[] = {
David Sedlák8f7a1172019-06-20 14:42:18 +02004369 cmocka_unit_test_setup_teardown(test_yin_match_keyword, setup_f, teardown_f),
David Sedlákb1a78352019-06-28 16:16:29 +02004370 cmocka_unit_test_setup_teardown(test_yin_parse_element_generic, setup_f, teardown_f),
4371 cmocka_unit_test_setup_teardown(test_yin_parse_extension_instance, setup_f, teardown_f),
David Sedlák071f7662019-09-12 02:02:51 +02004372 cmocka_unit_test_setup_teardown(test_yin_parse_content, setup_f, teardown_f),
David Sedlák4a650532019-07-10 11:55:18 +02004373 cmocka_unit_test_setup_teardown(test_validate_value, setup_f, teardown_f),
David Sedlák32488102019-07-15 17:44:10 +02004374
David Sedlák8e7bda82019-07-16 17:57:50 +02004375 cmocka_unit_test(test_yin_match_argument_name),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004376 cmocka_unit_test_setup_teardown(test_enum_elem, setup_element_test, teardown_f),
4377 cmocka_unit_test_setup_teardown(test_bit_elem, setup_element_test, teardown_f),
4378 cmocka_unit_test_setup_teardown(test_meta_elem, setup_element_test, teardown_f),
4379 cmocka_unit_test_setup_teardown(test_import_elem, setup_element_test, teardown_f),
4380 cmocka_unit_test_setup_teardown(test_status_elem, setup_element_test, teardown_f),
4381 cmocka_unit_test_setup_teardown(test_ext_elem, setup_element_test, teardown_f),
4382 cmocka_unit_test_setup_teardown(test_yin_element_elem, setup_element_test, teardown_f),
4383 cmocka_unit_test_setup_teardown(test_yangversion_elem, setup_element_test, teardown_f),
4384 cmocka_unit_test_setup_teardown(test_mandatory_elem, setup_element_test, teardown_f),
4385 cmocka_unit_test_setup_teardown(test_argument_elem, setup_element_test, teardown_f),
4386 cmocka_unit_test_setup_teardown(test_base_elem, setup_element_test, teardown_f),
4387 cmocka_unit_test_setup_teardown(test_belongsto_elem, setup_element_test, teardown_f),
4388 cmocka_unit_test_setup_teardown(test_config_elem, setup_element_test, teardown_f),
4389 cmocka_unit_test_setup_teardown(test_default_elem, setup_element_test, teardown_f),
4390 cmocka_unit_test_setup_teardown(test_err_app_tag_elem, setup_element_test, teardown_f),
4391 cmocka_unit_test_setup_teardown(test_err_msg_elem, setup_element_test, teardown_f),
4392 cmocka_unit_test_setup_teardown(test_fracdigits_elem, setup_element_test, teardown_f),
4393 cmocka_unit_test_setup_teardown(test_iffeature_elem, setup_element_test, teardown_f),
4394 cmocka_unit_test_setup_teardown(test_length_elem, setup_element_test, teardown_f),
4395 cmocka_unit_test_setup_teardown(test_modifier_elem, setup_element_test, teardown_f),
4396 cmocka_unit_test_setup_teardown(test_namespace_elem, setup_element_test, teardown_f),
4397 cmocka_unit_test_setup_teardown(test_pattern_elem, setup_element_test, teardown_f),
4398 cmocka_unit_test_setup_teardown(test_value_position_elem, setup_element_test, teardown_f),
4399 cmocka_unit_test_setup_teardown(test_prefix_elem, setup_element_test, teardown_f),
4400 cmocka_unit_test_setup_teardown(test_range_elem, setup_element_test, teardown_f),
4401 cmocka_unit_test_setup_teardown(test_reqinstance_elem, setup_element_test, teardown_f),
4402 cmocka_unit_test_setup_teardown(test_revision_date_elem, setup_element_test, teardown_f),
4403 cmocka_unit_test_setup_teardown(test_unique_elem, setup_element_test, teardown_f),
4404 cmocka_unit_test_setup_teardown(test_units_elem, setup_element_test, teardown_f),
4405 cmocka_unit_test_setup_teardown(test_when_elem, setup_element_test, teardown_f),
4406 cmocka_unit_test_setup_teardown(test_yin_text_value_elem, setup_element_test, teardown_f),
4407 cmocka_unit_test_setup_teardown(test_type_elem, setup_element_test, teardown_f),
4408 cmocka_unit_test_setup_teardown(test_max_elems_elem, setup_element_test, teardown_f),
4409 cmocka_unit_test_setup_teardown(test_min_elems_elem, setup_element_test, teardown_f),
4410 cmocka_unit_test_setup_teardown(test_ordby_elem, setup_element_test, teardown_f),
4411 cmocka_unit_test_setup_teardown(test_any_elem, setup_element_test, teardown_f),
4412 cmocka_unit_test_setup_teardown(test_leaf_elem, setup_element_test, teardown_f),
4413 cmocka_unit_test_setup_teardown(test_leaf_list_elem, setup_element_test, teardown_f),
4414 cmocka_unit_test_setup_teardown(test_presence_elem, setup_element_test, teardown_f),
4415 cmocka_unit_test_setup_teardown(test_key_elem, setup_element_test, teardown_f),
4416 cmocka_unit_test_setup_teardown(test_typedef_elem, setup_element_test, teardown_f),
4417 cmocka_unit_test_setup_teardown(test_refine_elem, setup_element_test, teardown_f),
4418 cmocka_unit_test_setup_teardown(test_uses_elem, setup_element_test, teardown_f),
4419 cmocka_unit_test_setup_teardown(test_revision_elem, setup_element_test, teardown_f),
4420 cmocka_unit_test_setup_teardown(test_include_elem, setup_element_test, teardown_f),
4421 cmocka_unit_test_setup_teardown(test_list_elem, setup_element_test, teardown_f),
4422 cmocka_unit_test_setup_teardown(test_notification_elem, setup_element_test, teardown_f),
4423 cmocka_unit_test_setup_teardown(test_grouping_elem, setup_element_test, teardown_f),
4424 cmocka_unit_test_setup_teardown(test_container_elem, setup_element_test, teardown_f),
4425 cmocka_unit_test_setup_teardown(test_case_elem, setup_element_test, teardown_f),
4426 cmocka_unit_test_setup_teardown(test_choice_elem, setup_element_test, teardown_f),
4427 cmocka_unit_test_setup_teardown(test_inout_elem, setup_element_test, teardown_f),
4428 cmocka_unit_test_setup_teardown(test_action_elem, setup_element_test, teardown_f),
4429 cmocka_unit_test_setup_teardown(test_augment_elem, setup_element_test, teardown_f),
4430 cmocka_unit_test_setup_teardown(test_deviate_elem, setup_element_test, teardown_f),
4431 cmocka_unit_test_setup_teardown(test_deviation_elem, setup_element_test, teardown_f),
4432 cmocka_unit_test_setup_teardown(test_module_elem, setup_element_test, teardown_f),
4433 cmocka_unit_test_setup_teardown(test_submodule_elem, setup_element_test, teardown_f),
David Sedlák8985a142019-07-31 16:43:06 +02004434
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004435 cmocka_unit_test_setup_teardown(test_yin_parse_module, setup_f, teardown_f),
4436 cmocka_unit_test_setup_teardown(test_yin_parse_submodule, setup_f, teardown_f),
David Sedlák3b4db242018-10-19 16:11:01 +02004437 };
4438
David Sedlák8e7bda82019-07-16 17:57:50 +02004439 return cmocka_run_group_tests(tests, setup_ly_ctx, destroy_ly_ctx);
David Sedlák3b4db242018-10-19 16:11:01 +02004440}