blob: ee7e960fb38c8fcec17a42aae4f768aaa2ecc9f8 [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 *
Radek Iša56ca9e42020-09-08 18:42:00 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
David Sedlákb1ce3f82019-06-05 14:37:26 +02007 *
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 */
Radek Iša56ca9e42020-09-08 18:42:00 +020014#define _UTEST_MAIN_
15#include "utests.h"
David Sedlákb1ce3f82019-06-05 14:37:26 +020016
Radek Krejcib4ac5a92020-11-23 17:54:33 +010017#include <stdbool.h>
David Sedlák3b4db242018-10-19 16:11:01 +020018#include <stdio.h>
19#include <string.h>
20
Radek Krejci70593c12020-06-13 20:48:09 +020021#include "common.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020022#include "in.h"
Radek Krejci70593c12020-06-13 20:48:09 +020023#include "parser_internal.h"
Michal Vasko405cc9e2020-12-01 12:01:27 +010024#include "schema_compile.h"
Radek Krejci70593c12020-06-13 20:48:09 +020025#include "tree_schema.h"
26#include "tree_schema_internal.h"
27#include "xml.h"
28#include "xpath.h"
David Sedlák3b4db242018-10-19 16:11:01 +020029
Michal Vaskoafac7822020-10-20 14:22:26 +020030/* copied from parser_yin.c */
31enum yin_argument {
32 YIN_ARG_UNKNOWN = 0, /**< parsed argument can not be matched with any supported yin argument keyword */
33 YIN_ARG_NAME, /**< argument name */
34 YIN_ARG_TARGET_NODE, /**< argument target-node */
35 YIN_ARG_MODULE, /**< argument module */
36 YIN_ARG_VALUE, /**< argument value */
37 YIN_ARG_TEXT, /**< argument text */
38 YIN_ARG_CONDITION, /**< argument condition */
39 YIN_ARG_URI, /**< argument uri */
40 YIN_ARG_DATE, /**< argument data */
41 YIN_ARG_TAG, /**< argument tag */
42 YIN_ARG_NONE /**< empty (special value) */
43};
44
45struct yin_subelement {
46 enum ly_stmt type; /**< type of keyword */
47 void *dest; /**< meta infromation passed to responsible function (mostly information about where parsed subelement should be stored) */
48 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 */
49};
50
51struct import_meta {
52 const char *prefix; /**< module prefix. */
53 struct lysp_import **imports; /**< imports to add to. */
54};
55
56struct yin_argument_meta {
57 uint16_t *flags; /**< Argument flags */
58 const char **argument; /**< Argument value */
59};
60
61struct tree_node_meta {
62 struct lysp_node *parent; /**< parent node */
63 struct lysp_node **nodes; /**< linked list of siblings */
64};
65
66struct include_meta {
67 const char *name; /**< Module/submodule name. */
68 struct lysp_include **includes; /**< [Sized array](@ref sizedarrays) of parsed includes to add to. */
69};
70
71struct inout_meta {
72 struct lysp_node *parent; /**< Parent node. */
Radek Krejci2a9fc652021-01-22 17:44:34 +010073 struct lysp_node_action_inout *inout_p; /**< inout_p Input/output pointer to write to. */
Michal Vaskoafac7822020-10-20 14:22:26 +020074};
75
76struct minmax_dev_meta {
77 uint32_t *lim; /**< min/max value to write to. */
78 uint16_t *flags; /**< min/max flags to write to. */
79 struct lysp_ext_instance **exts; /**< extension instances to add to. */
80};
81
82#define YIN_SUBELEM_MANDATORY 0x01
83#define YIN_SUBELEM_UNIQUE 0x02
84#define YIN_SUBELEM_FIRST 0x04
85#define YIN_SUBELEM_VER2 0x08
86
87#define YIN_SUBELEM_PARSED 0x80
88
David Sedlák555c7202019-07-04 12:14:12 +020089/* prototypes of static functions */
Michal Vaskoafac7822020-10-20 14:22:26 +020090enum yin_argument yin_match_argument_name(const char *name, size_t len);
91LY_ERR yin_parse_content(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size,
92 enum ly_stmt current_element, const char **text_content, struct lysp_ext_instance **exts);
93LY_ERR yin_validate_value(struct lys_yin_parser_ctx *ctx, enum yang_arg val_type);
94enum ly_stmt yin_match_keyword(struct lys_yin_parser_ctx *ctx, const char *name, size_t name_len,
95 const char *prefix, size_t prefix_len, enum ly_stmt parrent);
96LY_ERR yin_parse_extension_instance(struct lys_yin_parser_ctx *ctx, LYEXT_SUBSTMT subelem, LY_ARRAY_COUNT_TYPE subelem_index,
97 struct lysp_ext_instance **exts);
98LY_ERR yin_parse_element_generic(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, struct lysp_stmt **element);
99LY_ERR yin_parse_mod(struct lys_yin_parser_ctx *ctx, struct lysp_module *mod);
100LY_ERR yin_parse_submod(struct lys_yin_parser_ctx *ctx, struct lysp_submodule *submod);
101
David Sedlák555c7202019-07-04 12:14:12 +0200102void lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext);
David Sedlák986cb412019-07-04 13:10:11 +0200103void lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext);
David Sedlák32eee7b2019-07-09 12:38:44 +0200104void lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when);
David Sedlák32488102019-07-15 17:44:10 +0200105void lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type);
David Sedlák8a83bbb2019-07-18 14:46:00 +0200106void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
David Sedlák04e17b22019-07-19 15:29:48 +0200107void lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf);
David Sedlákd2d676a2019-07-22 11:28:19 +0200108void lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref);
David Sedlákaa854b02019-07-22 14:17:10 +0200109void lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev);
David Sedlák0c2bab92019-07-22 15:33:19 +0200110void lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include);
David Sedlák5e13dea2019-07-22 16:06:45 +0200111void lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat);
David Sedlák28794f22019-07-22 16:45:00 +0200112void lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100113void lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp);
114void lysp_augment_free(struct ly_ctx *ctx, struct lysp_node_augment *augment);
David Sedlák4ffcec82019-07-25 15:10:21 +0200115void lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d);
David Sedlák8b754462019-07-25 16:22:13 +0200116void lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev);
David Sedlák298ff6d2019-07-26 14:29:03 +0200117void lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import);
David Sedlák555c7202019-07-04 12:14:12 +0200118
David Sedláke6cd89e2019-08-07 12:46:02 +0200119/* wrapping element used for mocking has nothing to do with real module structure */
120#define ELEMENT_WRAPPER_START "<status xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
121#define ELEMENT_WRAPPER_END "</status>"
122
Radek Iša56ca9e42020-09-08 18:42:00 +0200123#define TEST_1_CHECK_LYSP_EXT_INSTANCE(NODE, INSUBSTMT)\
124 CHECK_LYSP_EXT_INSTANCE((NODE), NULL, 1, NULL, INSUBSTMT, 0, "urn:example:extensions:c-define", 0, 0x2, 0x1)
David Sedlák872c7b42018-10-26 13:15:20 +0200125
Radek Iša56ca9e42020-09-08 18:42:00 +0200126struct lys_yin_parser_ctx *YCTX;
David Sedlák8e7bda82019-07-16 17:57:50 +0200127
128static int
Radek Iša56ca9e42020-09-08 18:42:00 +0200129setup_ctx(void **state)
David Sedlák8e7bda82019-07-16 17:57:50 +0200130{
David Sedlák619db942019-07-03 14:47:30 +0200131 /* allocate parser context */
Radek Iša56ca9e42020-09-08 18:42:00 +0200132 YCTX = calloc(1, sizeof(*YCTX));
133 YCTX->format = LYS_IN_YIN;
David Sedlák8f5bce02019-06-03 16:41:08 +0200134
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200135 /* allocate new parsed module */
Radek Iša56ca9e42020-09-08 18:42:00 +0200136 YCTX->parsed_mod = calloc(1, sizeof *YCTX->parsed_mod);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200137
138 /* allocate new module */
Radek Iša56ca9e42020-09-08 18:42:00 +0200139 YCTX->parsed_mod->mod = calloc(1, sizeof *YCTX->parsed_mod->mod);
140 YCTX->parsed_mod->mod->ctx = UTEST_LYCTX;
141 YCTX->parsed_mod->mod->parsed = YCTX->parsed_mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200142
Radek Iša56ca9e42020-09-08 18:42:00 +0200143 return 0;
David Sedlák3b4db242018-10-19 16:11:01 +0200144}
145
146static int
Radek Iša56ca9e42020-09-08 18:42:00 +0200147setup(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +0100148{
Radek Iša56ca9e42020-09-08 18:42:00 +0200149 UTEST_SETUP;
David Sedlák68a1af12019-03-08 13:46:54 +0100150
Radek Iša56ca9e42020-09-08 18:42:00 +0200151 setup_ctx(state);
David Sedlák79e50cb2019-06-05 16:33:09 +0200152
Radek Iša56ca9e42020-09-08 18:42:00 +0200153 return 0;
David Sedlák79e50cb2019-06-05 16:33:09 +0200154}
155
David Sedlák8985a142019-07-31 16:43:06 +0200156static int
Radek Iša56ca9e42020-09-08 18:42:00 +0200157teardown_ctx(void **UNUSED(state))
David Sedlák8985a142019-07-31 16:43:06 +0200158{
Radek Iša56ca9e42020-09-08 18:42:00 +0200159 lyxml_ctx_free(YCTX->xmlctx);
160 lys_module_free(YCTX->parsed_mod->mod, NULL);
161 free(YCTX);
162 YCTX = NULL;
David Sedlák8985a142019-07-31 16:43:06 +0200163
Radek Iša56ca9e42020-09-08 18:42:00 +0200164 return 0;
David Sedlák8985a142019-07-31 16:43:06 +0200165}
166
Radek Iša56ca9e42020-09-08 18:42:00 +0200167static int
168teardown(void **state)
169{
170 teardown_ctx(state);
171
172 UTEST_TEARDOWN;
173
174 return 0;
175}
176
177#define RESET_STATE \
178 ly_in_free(UTEST_IN, 0); \
179 UTEST_IN = NULL; \
180 teardown_ctx(state); \
181 setup_ctx(state)
182
David Sedlák68a1af12019-03-08 13:46:54 +0100183static void
David Sedlák1bccdfa2019-06-17 15:55:27 +0200184test_yin_match_keyword(void **state)
David Sedlák3b4db242018-10-19 16:11:01 +0200185{
Michal Vaskob36053d2020-03-26 15:49:30 +0100186 const char *prefix;
187 size_t prefix_len;
Radek Iša56ca9e42020-09-08 18:42:00 +0200188
David Sedlák8f7a1172019-06-20 14:42:18 +0200189 /* create mock yin namespace in xml context */
Radek Iša56ca9e42020-09-08 18:42:00 +0200190 ly_in_new_memory("<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" />", &UTEST_IN);
191 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
192 prefix = YCTX->xmlctx->prefix;
193 prefix_len = YCTX->xmlctx->prefix_len;
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100194
Radek Iša56ca9e42020-09-08 18:42:00 +0200195 assert_int_equal(yin_match_keyword(YCTX, "anydatax", strlen("anydatax"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
196 assert_int_equal(yin_match_keyword(YCTX, "asdasd", strlen("asdasd"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
197 assert_int_equal(yin_match_keyword(YCTX, "", 0, prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
198 assert_int_equal(yin_match_keyword(YCTX, "anydata", strlen("anydata"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ANYDATA);
199 assert_int_equal(yin_match_keyword(YCTX, "anyxml", strlen("anyxml"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ANYXML);
200 assert_int_equal(yin_match_keyword(YCTX, "argument", strlen("argument"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ARGUMENT);
201 assert_int_equal(yin_match_keyword(YCTX, "augment", strlen("augment"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_AUGMENT);
202 assert_int_equal(yin_match_keyword(YCTX, "base", strlen("base"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BASE);
203 assert_int_equal(yin_match_keyword(YCTX, "belongs-to", strlen("belongs-to"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BELONGS_TO);
204 assert_int_equal(yin_match_keyword(YCTX, "bit", strlen("bit"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BIT);
205 assert_int_equal(yin_match_keyword(YCTX, "case", strlen("case"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CASE);
206 assert_int_equal(yin_match_keyword(YCTX, "choice", strlen("choice"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CHOICE);
207 assert_int_equal(yin_match_keyword(YCTX, "config", strlen("config"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONFIG);
208 assert_int_equal(yin_match_keyword(YCTX, "contact", strlen("contact"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONTACT);
209 assert_int_equal(yin_match_keyword(YCTX, "container", strlen("container"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONTAINER);
210 assert_int_equal(yin_match_keyword(YCTX, "default", strlen("default"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEFAULT);
211 assert_int_equal(yin_match_keyword(YCTX, "description", strlen("description"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DESCRIPTION);
212 assert_int_equal(yin_match_keyword(YCTX, "deviate", strlen("deviate"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEVIATE);
213 assert_int_equal(yin_match_keyword(YCTX, "deviation", strlen("deviation"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEVIATION);
214 assert_int_equal(yin_match_keyword(YCTX, "enum", strlen("enum"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ENUM);
215 assert_int_equal(yin_match_keyword(YCTX, "error-app-tag", strlen("error-app-tag"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ERROR_APP_TAG);
216 assert_int_equal(yin_match_keyword(YCTX, "error-message", strlen("error-message"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ERROR_MESSAGE);
217 assert_int_equal(yin_match_keyword(YCTX, "extension", strlen("extension"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_EXTENSION);
218 assert_int_equal(yin_match_keyword(YCTX, "feature", strlen("feature"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_FEATURE);
219 assert_int_equal(yin_match_keyword(YCTX, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_FRACTION_DIGITS);
220 assert_int_equal(yin_match_keyword(YCTX, "grouping", strlen("grouping"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_GROUPING);
221 assert_int_equal(yin_match_keyword(YCTX, "identity", strlen("identity"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IDENTITY);
222 assert_int_equal(yin_match_keyword(YCTX, "if-feature", strlen("if-feature"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IF_FEATURE);
223 assert_int_equal(yin_match_keyword(YCTX, "import", strlen("import"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IMPORT);
224 assert_int_equal(yin_match_keyword(YCTX, "include", strlen("include"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_INCLUDE);
225 assert_int_equal(yin_match_keyword(YCTX, "input", strlen("input"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_INPUT);
226 assert_int_equal(yin_match_keyword(YCTX, "key", strlen("key"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_KEY);
227 assert_int_equal(yin_match_keyword(YCTX, "leaf", strlen("leaf"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LEAF);
228 assert_int_equal(yin_match_keyword(YCTX, "leaf-list", strlen("leaf-list"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LEAF_LIST);
229 assert_int_equal(yin_match_keyword(YCTX, "length", strlen("length"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LENGTH);
230 assert_int_equal(yin_match_keyword(YCTX, "list", strlen("list"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LIST);
231 assert_int_equal(yin_match_keyword(YCTX, "mandatory", strlen("mandatory"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MANDATORY);
232 assert_int_equal(yin_match_keyword(YCTX, "max-elements", strlen("max-elements"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MAX_ELEMENTS);
233 assert_int_equal(yin_match_keyword(YCTX, "min-elements", strlen("min-elements"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MIN_ELEMENTS);
234 assert_int_equal(yin_match_keyword(YCTX, "modifier", strlen("modifier"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MODIFIER);
235 assert_int_equal(yin_match_keyword(YCTX, "module", strlen("module"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MODULE);
236 assert_int_equal(yin_match_keyword(YCTX, "must", strlen("must"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MUST);
237 assert_int_equal(yin_match_keyword(YCTX, "namespace", strlen("namespace"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NAMESPACE);
238 assert_int_equal(yin_match_keyword(YCTX, "notification", strlen("notification"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NOTIFICATION);
239 assert_int_equal(yin_match_keyword(YCTX, "ordered-by", strlen("ordered-by"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ORDERED_BY);
240 assert_int_equal(yin_match_keyword(YCTX, "organization", strlen("organization"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ORGANIZATION);
241 assert_int_equal(yin_match_keyword(YCTX, "output", strlen("output"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_OUTPUT);
242 assert_int_equal(yin_match_keyword(YCTX, "path", strlen("path"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PATH);
243 assert_int_equal(yin_match_keyword(YCTX, "pattern", strlen("pattern"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PATTERN);
244 assert_int_equal(yin_match_keyword(YCTX, "position", strlen("position"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_POSITION);
245 assert_int_equal(yin_match_keyword(YCTX, "prefix", strlen("prefix"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PREFIX);
246 assert_int_equal(yin_match_keyword(YCTX, "presence", strlen("presence"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PRESENCE);
247 assert_int_equal(yin_match_keyword(YCTX, "range", strlen("range"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_RANGE);
248 assert_int_equal(yin_match_keyword(YCTX, "reference", strlen("reference"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REFERENCE);
249 assert_int_equal(yin_match_keyword(YCTX, "refine", strlen("refine"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REFINE);
250 assert_int_equal(yin_match_keyword(YCTX, "require-instance", strlen("require-instance"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REQUIRE_INSTANCE);
251 assert_int_equal(yin_match_keyword(YCTX, "revision", strlen("revision"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REVISION);
252 assert_int_equal(yin_match_keyword(YCTX, "revision-date", strlen("revision-date"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REVISION_DATE);
253 assert_int_equal(yin_match_keyword(YCTX, "rpc", strlen("rpc"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_RPC);
254 assert_int_equal(yin_match_keyword(YCTX, "status", strlen("status"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_STATUS);
255 assert_int_equal(yin_match_keyword(YCTX, "submodule", strlen("submodule"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_SUBMODULE);
256 assert_int_equal(yin_match_keyword(YCTX, "type", strlen("type"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_TYPE);
257 assert_int_equal(yin_match_keyword(YCTX, "typedef", strlen("typedef"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_TYPEDEF);
258 assert_int_equal(yin_match_keyword(YCTX, "unique", strlen("unique"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_UNIQUE);
259 assert_int_equal(yin_match_keyword(YCTX, "units", strlen("units"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_UNITS);
260 assert_int_equal(yin_match_keyword(YCTX, "uses", strlen("uses"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_USES);
261 assert_int_equal(yin_match_keyword(YCTX, "value", strlen("value"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_VALUE);
262 assert_int_equal(yin_match_keyword(YCTX, "when", strlen("when"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_WHEN);
263 assert_int_equal(yin_match_keyword(YCTX, "yang-version", strlen("yang-version"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_YANG_VERSION);
264 assert_int_equal(yin_match_keyword(YCTX, "yin-element", strlen("yin-element"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_YIN_ELEMENT);
David Sedlák872c7b42018-10-26 13:15:20 +0200265}
David Sedlák3b4db242018-10-19 16:11:01 +0200266
David Sedlák872c7b42018-10-26 13:15:20 +0200267static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200268test_yin_match_argument_name(void **UNUSED(state))
David Sedlák872c7b42018-10-26 13:15:20 +0200269{
David Sedlák060b00e2019-06-19 11:12:06 +0200270 assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN);
271 assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN);
272 assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN);
273 assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION);
274 assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE);
275 assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE);
276 assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME);
277 assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG);
278 assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
279 assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT);
280 assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI);
281 assert_int_equal(yin_match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200282}
283
David Sedlák68a1af12019-03-08 13:46:54 +0100284static void
David Sedlákb1a78352019-06-28 16:16:29 +0200285test_yin_parse_element_generic(void **state)
286{
David Sedlákb1a78352019-06-28 16:16:29 +0200287 struct lysp_ext_instance exts;
David Sedlákb1a78352019-06-28 16:16:29 +0200288 LY_ERR ret;
Radek Iša56ca9e42020-09-08 18:42:00 +0200289 const char *arg;
290 const char *stmt;
291 const char *data;
David Sedlákb1a78352019-06-28 16:16:29 +0200292
293 memset(&exts, 0, sizeof(exts));
294
Radek Iša56ca9e42020-09-08 18:42:00 +0200295 data = "<myext:elem attr=\"value\" xmlns:myext=\"urn:example:extensions\">text_value</myext:elem>";
296 ly_in_new_memory(data, &UTEST_IN);
297 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100298
Radek Iša56ca9e42020-09-08 18:42:00 +0200299 ret = yin_parse_element_generic(YCTX, LY_STMT_EXTENSION_INSTANCE, &exts.child);
David Sedlákb1a78352019-06-28 16:16:29 +0200300 assert_int_equal(ret, LY_SUCCESS);
Radek Iša56ca9e42020-09-08 18:42:00 +0200301 assert_int_equal(YCTX->xmlctx->status, LYXML_ELEM_CLOSE);
302 stmt = "urn:example:extensions:elem";
303 arg = "text_value";
Radek Krejci996f0772021-02-05 19:39:41 +0100304 CHECK_LYSP_STMT(exts.child, arg, 1, 0, LY_STMT_EXTENSION_INSTANCE, 0, stmt);
Radek Iša56ca9e42020-09-08 18:42:00 +0200305 stmt = "attr";
306 arg = "value";
307 CHECK_LYSP_STMT(exts.child->child, arg, 0, 0x400, 0, 0, stmt);
308 lysp_ext_instance_free(UTEST_LYCTX, &exts);
309 RESET_STATE;
David Sedlák5392a212019-07-01 09:19:10 +0200310
David Sedlákb0ca07d2019-09-11 11:54:05 +0200311 data = "<myext:elem xmlns:myext=\"urn:example:extensions\"></myext:elem>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200312 ly_in_new_memory(data, &UTEST_IN);
313 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100314
Radek Iša56ca9e42020-09-08 18:42:00 +0200315 ret = yin_parse_element_generic(YCTX, LY_STMT_EXTENSION_INSTANCE, &exts.child);
David Sedlák5392a212019-07-01 09:19:10 +0200316 assert_int_equal(ret, LY_SUCCESS);
Radek Iša56ca9e42020-09-08 18:42:00 +0200317 assert_int_equal(YCTX->xmlctx->status, LYXML_ELEM_CLOSE);
318 stmt = "urn:example:extensions:elem";
Radek Krejci996f0772021-02-05 19:39:41 +0100319 CHECK_LYSP_STMT(exts.child, NULL, 0, 0x0, LY_STMT_EXTENSION_INSTANCE, 0, stmt);
Radek Iša56ca9e42020-09-08 18:42:00 +0200320 lysp_ext_instance_free(UTEST_LYCTX, &exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200321}
322
323static void
324test_yin_parse_extension_instance(void **state)
325{
326 LY_ERR ret;
David Sedlákb1a78352019-06-28 16:16:29 +0200327 struct lysp_ext_instance *exts = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +0200328 struct lysp_stmt *act_child;
David Sedlákb0ca07d2019-09-11 11:54:05 +0200329 const char *data = "<myext:ext value1=\"test\" value=\"test2\" xmlns:myext=\"urn:example:extensions\"><myext:subelem>text</myext:subelem></myext:ext>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200330 const char *exts_name;
331 const char *stmt = "value1";
332 const char *arg = "test";
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100333
Radek Iša56ca9e42020-09-08 18:42:00 +0200334 ly_in_new_memory(data, &UTEST_IN);
335 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100336
Radek Iša56ca9e42020-09-08 18:42:00 +0200337 ret = yin_parse_extension_instance(YCTX, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200338 assert_int_equal(ret, LY_SUCCESS);
Radek Iša56ca9e42020-09-08 18:42:00 +0200339 exts_name = "urn:example:extensions:ext";
340 CHECK_LYSP_EXT_INSTANCE(exts, NULL, 1, NULL,
Radek Krejci996f0772021-02-05 19:39:41 +0100341 LYEXT_SUBSTMT_CONTACT, 0, exts_name, 0, LYS_CHOICE, LYS_YIN);
David Sedlákb1a78352019-06-28 16:16:29 +0200342
Radek Iša56ca9e42020-09-08 18:42:00 +0200343 CHECK_LYSP_STMT(exts->child, arg, 0, LYS_YIN_ATTR, 0, 1, stmt);
344 stmt = "value";
345 arg = "test2";
346 CHECK_LYSP_STMT(exts->child->next, arg, 0, LYS_YIN_ATTR, 0, 1, stmt);
347 stmt = "urn:example:extensions:subelem";
348 arg = "text";
Radek Krejci996f0772021-02-05 19:39:41 +0100349 CHECK_LYSP_STMT(exts->child->next->next, arg, 0, 0, LY_STMT_EXTENSION_INSTANCE, 0, stmt);
Radek Iša56ca9e42020-09-08 18:42:00 +0200350 lysp_ext_instance_free(UTEST_LYCTX, exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200351 LY_ARRAY_FREE(exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200352 exts = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +0200353 RESET_STATE;
David Sedlákf250ecf2019-07-01 11:02:05 +0200354
David Sedlákb0ca07d2019-09-11 11:54:05 +0200355 data = "<myext:extension-elem xmlns:myext=\"urn:example:extensions\" />";
Radek Iša56ca9e42020-09-08 18:42:00 +0200356 ly_in_new_memory(data, &UTEST_IN);
357 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100358
Radek Iša56ca9e42020-09-08 18:42:00 +0200359 ret = yin_parse_extension_instance(YCTX, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200360 assert_int_equal(ret, LY_SUCCESS);
Radek Iša56ca9e42020-09-08 18:42:00 +0200361 exts_name = "urn:example:extensions:extension-elem";
362 CHECK_LYSP_EXT_INSTANCE(exts, NULL, 0, NULL,
Radek Krejci996f0772021-02-05 19:39:41 +0100363 LYEXT_SUBSTMT_CONTACT, 0, exts_name, 0, LYS_CHOICE, LYS_YIN);
Radek Iša56ca9e42020-09-08 18:42:00 +0200364 lysp_ext_instance_free(UTEST_LYCTX, exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200365 LY_ARRAY_FREE(exts);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200366 exts = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +0200367 RESET_STATE;
David Sedlákadd0c2e2019-08-16 10:49:12 +0200368
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100369 data =
370 "<myext:ext attr1=\"text1\" attr2=\"text2\" xmlns:myext=\"urn:example:extensions\">\n"
371 " <myext:ext-sub1/>\n"
372 " <myext:ext-sub2 sattr1=\"stext2\">\n"
373 " <myext:ext-sub21>\n"
374 " <myext:ext-sub211 sattr21=\"text21\"/>\n"
375 " </myext:ext-sub21>\n"
376 " </myext:ext-sub2>\n"
377 " <myext:ext-sub3 attr3=\"text3\"></myext:ext-sub3>\n"
378 "</myext:ext>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200379 ly_in_new_memory(data, &UTEST_IN);
380 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100381
Radek Iša56ca9e42020-09-08 18:42:00 +0200382 ret = yin_parse_extension_instance(YCTX, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200383 assert_int_equal(ret, LY_SUCCESS);
384
Radek Iša56ca9e42020-09-08 18:42:00 +0200385 exts_name = "urn:example:extensions:ext";
386 CHECK_LYSP_EXT_INSTANCE(exts, NULL, 1, NULL,
Radek Krejci996f0772021-02-05 19:39:41 +0100387 LYEXT_SUBSTMT_CONTACT, 0, exts_name, 0, LYS_CHOICE, LYS_YIN);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200388
Radek Iša56ca9e42020-09-08 18:42:00 +0200389 stmt = "attr1";
390 arg = "text1";
391 act_child = exts->child;
392 CHECK_LYSP_STMT(act_child, arg, NULL, LYS_YIN_ATTR, 0x0, 1, stmt);
393 stmt = "attr2";
394 arg = "text2";
395 act_child = act_child->next;
396 CHECK_LYSP_STMT(act_child, arg, NULL, LYS_YIN_ATTR, 0x0, 1, stmt);
397 stmt = "urn:example:extensions:ext-sub1";
398 arg = NULL;
399 act_child = act_child->next;
Radek Krejci996f0772021-02-05 19:39:41 +0100400 CHECK_LYSP_STMT(act_child, arg, NULL, 0, LY_STMT_EXTENSION_INSTANCE, 1, stmt);
Radek Iša56ca9e42020-09-08 18:42:00 +0200401 stmt = "urn:example:extensions:ext-sub2";
402 arg = NULL;
403 act_child = act_child->next;
Radek Krejci996f0772021-02-05 19:39:41 +0100404 CHECK_LYSP_STMT(act_child, arg, 1, 0, LY_STMT_EXTENSION_INSTANCE, 1, stmt);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200405
Radek Iša56ca9e42020-09-08 18:42:00 +0200406 stmt = "sattr1";
407 arg = "stext2";
408 act_child = act_child->child;
409 CHECK_LYSP_STMT(act_child, arg, NULL, LYS_YIN_ATTR, 0, 1, stmt);
410 stmt = "urn:example:extensions:ext-sub21";
411 arg = NULL;
412 act_child = act_child->next;
Radek Krejci996f0772021-02-05 19:39:41 +0100413 CHECK_LYSP_STMT(act_child, arg, 1, 0, LY_STMT_EXTENSION_INSTANCE, 0, stmt);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200414
Radek Iša56ca9e42020-09-08 18:42:00 +0200415 stmt = "urn:example:extensions:ext-sub211";
416 arg = NULL;
417 act_child = act_child->child;
Radek Krejci996f0772021-02-05 19:39:41 +0100418 CHECK_LYSP_STMT(act_child, arg, 1, 0, LY_STMT_EXTENSION_INSTANCE, 0, stmt);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200419
Radek Iša56ca9e42020-09-08 18:42:00 +0200420 stmt = "sattr21";
421 arg = "text21";
422 act_child = act_child->child;
423 CHECK_LYSP_STMT(act_child, arg, 0, LYS_YIN_ATTR, 0, 0, stmt);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200424
Radek Iša56ca9e42020-09-08 18:42:00 +0200425 stmt = "urn:example:extensions:ext-sub3";
426 arg = NULL;
427 act_child = exts->child->next->next->next->next;
Radek Krejci996f0772021-02-05 19:39:41 +0100428 CHECK_LYSP_STMT(act_child, arg, 1, 0, LY_STMT_EXTENSION_INSTANCE, 0, stmt);
Radek Iša56ca9e42020-09-08 18:42:00 +0200429 stmt = "attr3";
430 arg = "text3";
431 act_child = act_child->child;
432 CHECK_LYSP_STMT(act_child, arg, 0, LYS_YIN_ATTR, 0, 0, stmt);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200433
Radek Iša56ca9e42020-09-08 18:42:00 +0200434 lysp_ext_instance_free(UTEST_LYCTX, exts);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200435 LY_ARRAY_FREE(exts);
436 exts = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +0200437 RESET_STATE;
David Sedlákaa98bba2019-09-12 11:52:14 +0200438
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100439 data =
440 "<myext:extension-elem xmlns:myext=\"urn:example:extensions\" xmlns:yin=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
441 " <yin:action name=\"act-name\" pre:prefixed=\"ignored\"/>\n"
442 " <yin:augment target-node=\"target\"/>\n"
443 " <yin:status value=\"value\"/>\n"
444 " <yin:include module=\"mod\"/>\n"
445 " <yin:input />\n"
446 " <yin:must condition=\"cond\"/>\n"
447 " <yin:namespace uri=\"uri\"/>\n"
448 " <yin:revision date=\"data\"/>\n"
449 " <yin:unique tag=\"tag\"/>\n"
450 " <yin:description><yin:text>contact-val</yin:text></yin:description>\n"
451 " <yin:error-message><yin:value>err-msg</yin:value></yin:error-message>\n"
452 "</myext:extension-elem>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200453 ly_in_new_memory(data, &UTEST_IN);
454 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100455
Radek Iša56ca9e42020-09-08 18:42:00 +0200456 ret = yin_parse_extension_instance(YCTX, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákaa98bba2019-09-12 11:52:14 +0200457 assert_int_equal(ret, LY_SUCCESS);
458 assert_string_equal(exts->child->arg, "act-name");
459 assert_string_equal(exts->child->next->arg, "target");
460 assert_string_equal(exts->child->next->next->arg, "value");
461 assert_string_equal(exts->child->next->next->next->arg, "mod");
462 assert_null(exts->child->next->next->next->next->arg);
463 assert_string_equal(exts->child->next->next->next->next->next->arg, "cond");
464 assert_string_equal(exts->child->next->next->next->next->next->next->arg, "uri");
465 assert_string_equal(exts->child->next->next->next->next->next->next->next->arg, "data");
466 assert_string_equal(exts->child->next->next->next->next->next->next->next->next->arg, "tag");
467 assert_string_equal(exts->child->next->next->next->next->next->next->next->next->next->arg, "contact-val");
Radek Iša56ca9e42020-09-08 18:42:00 +0200468 lysp_ext_instance_free(UTEST_LYCTX, exts);
David Sedlákaa98bba2019-09-12 11:52:14 +0200469 LY_ARRAY_FREE(exts);
470 exts = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +0200471 RESET_STATE;
David Sedlákb1a78352019-06-28 16:16:29 +0200472}
473
David Sedlák555c7202019-07-04 12:14:12 +0200474static void
475test_yin_parse_content(void **state)
476{
David Sedlák555c7202019-07-04 12:14:12 +0200477 LY_ERR ret = LY_SUCCESS;
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100478 const char *data =
479 "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
480 " <myext:custom xmlns:myext=\"urn:example:extensions\">totally amazing extension</myext:custom>\n"
481 " <extension name=\"ext\">\n"
482 " <argument name=\"argname\"></argument>\n"
483 " <description><text>desc</text></description>\n"
484 " <reference><text>ref</text></reference>\n"
485 " <status value=\"deprecated\"></status>\n"
486 " </extension>\n"
487 " <text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>\n"
488 " <if-feature name=\"foo\"></if-feature>\n"
489 " <when condition=\"condition...\">\n"
490 " <reference><text>when_ref</text></reference>\n"
491 " <description><text>when_desc</text></description>\n"
492 " </when>\n"
493 " <config value=\"true\"/>\n"
494 " <error-message>\n"
495 " <value>error-msg</value>\n"
496 " </error-message>\n"
497 " <error-app-tag value=\"err-app-tag\"/>\n"
498 " <units name=\"radians\"></units>\n"
499 " <default value=\"default-value\"/>\n"
500 " <position value=\"25\"></position>\n"
501 " <value value=\"-5\"/>\n"
502 " <require-instance value=\"true\"></require-instance>\n"
503 " <range value=\"5..10\" />\n"
504 " <length value=\"baf\"/>\n"
505 " <pattern value='pattern'>\n"
506 " <modifier value='invert-match'/>\n"
507 " </pattern>\n"
508 " <enum name=\"yay\">\n"
509 " </enum>\n"
510 "</prefix>";
David Sedlák555c7202019-07-04 12:14:12 +0200511 struct lysp_ext_instance *exts = NULL;
David Sedlák5f8191e2019-07-08 16:35:52 +0200512 const char **if_features = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +0200513 const char *value, *error_message, *app_tag, *units;
Michal Vasko7f45cf22020-10-01 12:49:44 +0200514 struct lysp_qname def = {0};
David Sedlák986cb412019-07-04 13:10:11 +0200515 struct lysp_ext *ext_def = NULL;
David Sedlák32eee7b2019-07-09 12:38:44 +0200516 struct lysp_when *when_p = NULL;
David Sedlákcf5569a2019-07-11 13:31:34 +0200517 struct lysp_type_enum pos_enum = {}, val_enum = {};
David Sedlákfd5b9c32019-07-12 15:33:13 +0200518 struct lysp_type req_type = {}, range_type = {}, len_type = {}, patter_type = {}, enum_type = {};
Juraj Vijtiukf6a07372020-10-29 21:13:33 +0100519 uint16_t config = 0;
David Sedlák555c7202019-07-04 12:14:12 +0200520
Radek Iša56ca9e42020-09-08 18:42:00 +0200521 ly_in_new_memory(data, &UTEST_IN);
522 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
523 lyxml_ctx_next(YCTX->xmlctx);
524 lyxml_ctx_next(YCTX->xmlctx);
525 lyxml_ctx_next(YCTX->xmlctx);
David Sedlák555c7202019-07-04 12:14:12 +0200526
David Sedlákfd5b9c32019-07-12 15:33:13 +0200527 struct yin_subelement subelems[17] = {
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100528 {LY_STMT_CONFIG, &config, 0},
529 {LY_STMT_DEFAULT, &def, YIN_SUBELEM_UNIQUE},
530 {LY_STMT_ENUM, &enum_type, 0},
531 {LY_STMT_ERROR_APP_TAG, &app_tag, YIN_SUBELEM_UNIQUE},
Radek Iša56ca9e42020-09-08 18:42:00 +0200532 {LY_STMT_ERROR_MESSAGE, &error_message, 0},
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100533 {LY_STMT_EXTENSION, &ext_def, 0},
534 {LY_STMT_IF_FEATURE, &if_features, 0},
535 {LY_STMT_LENGTH, &len_type, 0},
536 {LY_STMT_PATTERN, &patter_type, 0},
537 {LY_STMT_POSITION, &pos_enum, 0},
538 {LY_STMT_RANGE, &range_type, 0},
539 {LY_STMT_REQUIRE_INSTANCE, &req_type, 0},
540 {LY_STMT_UNITS, &units, YIN_SUBELEM_UNIQUE},
541 {LY_STMT_VALUE, &val_enum, 0},
542 {LY_STMT_WHEN, &when_p, 0},
543 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
544 {LY_STMT_ARG_TEXT, &value, 0}
545 };
546
Radek Iša56ca9e42020-09-08 18:42:00 +0200547 ret = yin_parse_content(YCTX, subelems, 17, LY_STMT_PREFIX, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200548 assert_int_equal(ret, LY_SUCCESS);
David Sedlák2ce1be62019-07-10 16:15:09 +0200549 /* check parsed values */
Michal Vasko7f45cf22020-10-01 12:49:44 +0200550 assert_string_equal(def.str, "default-value");
Radek Iša56ca9e42020-09-08 18:42:00 +0200551 const char *exts_name = "urn:example:extensions:custom";
552 const char *exts_arg = "totally amazing extension";
553
554 CHECK_LYSP_EXT_INSTANCE(exts, exts_arg, 0, NULL,
555 LYEXT_SUBSTMT_PREFIX, 0, exts_name, 0, 0x1, 0x1);
David Sedlák555c7202019-07-04 12:14:12 +0200556 assert_string_equal(value, "wsefsdf");
David Sedláka5b1d382019-07-10 16:31:09 +0200557 assert_string_equal(units, "radians");
David Sedlák32eee7b2019-07-09 12:38:44 +0200558 assert_string_equal(when_p->cond, "condition...");
559 assert_string_equal(when_p->dsc, "when_desc");
560 assert_string_equal(when_p->ref, "when_ref");
David Sedláke1a30302019-07-10 13:49:38 +0200561 assert_int_equal(config, LYS_CONFIG_W);
Radek Iša56ca9e42020-09-08 18:42:00 +0200562 CHECK_LYSP_TYPE_ENUM(&pos_enum, NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, 25);
563 CHECK_LYSP_TYPE_ENUM(&val_enum, NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, -5);
David Sedlákcf5569a2019-07-11 13:31:34 +0200564 assert_int_equal(req_type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200565 assert_true(req_type.flags &= LYS_SET_REQINST);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200566 assert_string_equal(range_type.range->arg.str, "5..10");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200567 assert_true(range_type.flags & LYS_SET_RANGE);
Radek Iša56ca9e42020-09-08 18:42:00 +0200568 assert_string_equal(error_message, "error-msg");
David Sedlák2ce1be62019-07-10 16:15:09 +0200569 assert_string_equal(app_tag, "err-app-tag");
David Sedlákfd5b9c32019-07-12 15:33:13 +0200570 assert_string_equal(enum_type.enums->name, "yay");
Radek Iša56ca9e42020-09-08 18:42:00 +0200571 CHECK_LYSP_RESTR(len_type.length, "baf", NULL,
572 NULL, NULL, 0, NULL);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200573 assert_true(len_type.flags & LYS_SET_LENGTH);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200574 assert_string_equal(patter_type.patterns->arg.str, "\x015pattern");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200575 assert_true(patter_type.flags & LYS_SET_PATTERN);
David Sedlák2ce1be62019-07-10 16:15:09 +0200576 /* cleanup */
Radek Iša56ca9e42020-09-08 18:42:00 +0200577 lysp_ext_instance_free(UTEST_LYCTX, exts);
578 lysp_when_free(UTEST_LYCTX, when_p);
579 lysp_ext_free(UTEST_LYCTX, ext_def);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100580 lydict_remove(UTEST_LYCTX, *if_features);
581 lydict_remove(UTEST_LYCTX, error_message);
582 lydict_remove(UTEST_LYCTX, app_tag);
583 lydict_remove(UTEST_LYCTX, units);
584 lydict_remove(UTEST_LYCTX, patter_type.patterns->arg.str);
585 lydict_remove(UTEST_LYCTX, def.str);
586 lydict_remove(UTEST_LYCTX, range_type.range->arg.str);
587 lydict_remove(UTEST_LYCTX, len_type.length->arg.str);
588 lydict_remove(UTEST_LYCTX, enum_type.enums->name);
589 lydict_remove(UTEST_LYCTX, value);
David Sedlák5f8191e2019-07-08 16:35:52 +0200590 LY_ARRAY_FREE(if_features);
David Sedlák555c7202019-07-04 12:14:12 +0200591 LY_ARRAY_FREE(exts);
David Sedlák986cb412019-07-04 13:10:11 +0200592 LY_ARRAY_FREE(ext_def);
David Sedlákd3983112019-07-12 11:20:56 +0200593 LY_ARRAY_FREE(patter_type.patterns);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200594 LY_ARRAY_FREE(enum_type.enums);
David Sedlák32eee7b2019-07-09 12:38:44 +0200595 free(when_p);
David Sedlákb7296dd2019-07-11 14:58:38 +0200596 free(range_type.range);
David Sedlák438ae432019-07-11 15:36:54 +0200597 free(len_type.length);
Radek Iša56ca9e42020-09-08 18:42:00 +0200598 RESET_STATE;
David Sedlák555c7202019-07-04 12:14:12 +0200599
600 /* test unique subelem */
601 const char *prefix_value;
Radek Krejcid6b76452019-09-03 17:03:03 +0200602 struct yin_subelement subelems2[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE},
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100603 {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_UNIQUE}};
604
David Sedláke6cd89e2019-08-07 12:46:02 +0200605 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100606 "<prefix value=\"inv_mod\" />"
607 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
608 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
609 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200610 ly_in_new_memory(data, &UTEST_IN);
611 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
612 lyxml_ctx_next(YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100613
Radek Iša56ca9e42020-09-08 18:42:00 +0200614 ret = yin_parse_content(YCTX, subelems2, 2, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200615 assert_int_equal(ret, LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +0200616 CHECK_LOG_CTX("Redefinition of \"text\" sub-element in \"status\" element.", "Line number 1.");
617 lydict_remove(UTEST_LYCTX, prefix_value);
618 lydict_remove(UTEST_LYCTX, value);
619 RESET_STATE;
David Sedlák555c7202019-07-04 12:14:12 +0200620
621 /* test first subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200622 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100623 "<prefix value=\"inv_mod\" />"
624 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
625 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
626 ELEMENT_WRAPPER_END;
Radek Krejcid6b76452019-09-03 17:03:03 +0200627 struct yin_subelement subelems3[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE},
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100628 {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_FIRST}};
629
Radek Iša56ca9e42020-09-08 18:42:00 +0200630 ly_in_new_memory(data, &UTEST_IN);
631 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
632 lyxml_ctx_next(YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100633
Radek Iša56ca9e42020-09-08 18:42:00 +0200634 ret = yin_parse_content(YCTX, subelems3, 2, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200635 assert_int_equal(ret, LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +0200636 CHECK_LOG_CTX("Sub-element \"text\" of \"status\" element must be defined as it's first sub-element.", "Line number 1.");
637 lydict_remove(UTEST_LYCTX, prefix_value);
638 RESET_STATE;
David Sedlák555c7202019-07-04 12:14:12 +0200639
640 /* test mandatory subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200641 data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END;
Radek Krejcid6b76452019-09-03 17:03:03 +0200642 struct yin_subelement subelems4[1] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}};
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100643
Radek Iša56ca9e42020-09-08 18:42:00 +0200644 ly_in_new_memory(data, &UTEST_IN);
645 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
646 lyxml_ctx_next(YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100647
Radek Iša56ca9e42020-09-08 18:42:00 +0200648 ret = yin_parse_content(YCTX, subelems4, 1, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200649 assert_int_equal(ret, LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +0200650 CHECK_LOG_CTX("Missing mandatory sub-element \"prefix\" of \"status\" element.", "Line number 1.");
David Sedlák555c7202019-07-04 12:14:12 +0200651}
652
David Sedlák92147b02019-07-09 14:01:01 +0200653static void
David Sedlák4a650532019-07-10 11:55:18 +0200654test_validate_value(void **state)
655{
Michal Vaskob36053d2020-03-26 15:49:30 +0100656 const char *data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END;
657
658 /* create some XML context */
Radek Iša56ca9e42020-09-08 18:42:00 +0200659 ly_in_new_memory(data, &UTEST_IN);
660 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
661 YCTX->xmlctx->status = LYXML_ELEM_CONTENT;
662 YCTX->xmlctx->dynamic = 0;
Michal Vaskob36053d2020-03-26 15:49:30 +0100663
Radek Iša56ca9e42020-09-08 18:42:00 +0200664 YCTX->xmlctx->value = "#invalid";
665 YCTX->xmlctx->value_len = 8;
666 assert_int_equal(yin_validate_value(YCTX, Y_IDENTIF_ARG), LY_EVALID);
667 CHECK_LOG_CTX("Invalid identifier character '#' (0x0023).", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100668
Radek Iša56ca9e42020-09-08 18:42:00 +0200669 YCTX->xmlctx->value = "";
670 YCTX->xmlctx->value_len = 0;
671 assert_int_equal(yin_validate_value(YCTX, Y_STR_ARG), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +0100672
Radek Iša56ca9e42020-09-08 18:42:00 +0200673 YCTX->xmlctx->value = "pre:b";
674 YCTX->xmlctx->value_len = 5;
675 assert_int_equal(yin_validate_value(YCTX, Y_IDENTIF_ARG), LY_EVALID);
676 assert_int_equal(yin_validate_value(YCTX, Y_PREF_IDENTIF_ARG), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +0100677
Radek Iša56ca9e42020-09-08 18:42:00 +0200678 YCTX->xmlctx->value = "pre:pre:b";
679 YCTX->xmlctx->value_len = 9;
680 assert_int_equal(yin_validate_value(YCTX, Y_PREF_IDENTIF_ARG), LY_EVALID);
David Sedlák4a650532019-07-10 11:55:18 +0200681}
682
David Sedlák32488102019-07-15 17:44:10 +0200683/* helper function to simplify unit test of each element using parse_content function */
684LY_ERR
Radek Iša56ca9e42020-09-08 18:42:00 +0200685test_element_helper(void **state, const char *data, void *dest, const char **text, struct lysp_ext_instance **exts)
David Sedlák32488102019-07-15 17:44:10 +0200686{
David Sedlákc5b20842019-08-13 10:18:31 +0200687 const char *name, *prefix;
688 size_t name_len, prefix_len;
David Sedlák32488102019-07-15 17:44:10 +0200689 LY_ERR ret = LY_SUCCESS;
690 struct yin_subelement subelems[71] = {
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100691 {LY_STMT_ACTION, dest, 0},
692 {LY_STMT_ANYDATA, dest, 0},
693 {LY_STMT_ANYXML, dest, 0},
694 {LY_STMT_ARGUMENT, dest, 0},
695 {LY_STMT_AUGMENT, dest, 0},
696 {LY_STMT_BASE, dest, 0},
697 {LY_STMT_BELONGS_TO, dest, 0},
698 {LY_STMT_BIT, dest, 0},
699 {LY_STMT_CASE, dest, 0},
700 {LY_STMT_CHOICE, dest, 0},
701 {LY_STMT_CONFIG, dest, 0},
702 {LY_STMT_CONTACT, dest, 0},
703 {LY_STMT_CONTAINER, dest, 0},
704 {LY_STMT_DEFAULT, dest, YIN_SUBELEM_UNIQUE},
705 {LY_STMT_DESCRIPTION, dest, 0},
706 {LY_STMT_DEVIATE, dest, 0},
707 {LY_STMT_DEVIATION, dest, 0},
708 {LY_STMT_ENUM, dest, 0},
709 {LY_STMT_ERROR_APP_TAG, dest, YIN_SUBELEM_UNIQUE},
710 {LY_STMT_ERROR_MESSAGE, dest, 0},
711 {LY_STMT_EXTENSION, dest, 0},
712 {LY_STMT_FEATURE, dest, 0},
713 {LY_STMT_FRACTION_DIGITS, dest, 0},
714 {LY_STMT_GROUPING, dest, 0},
715 {LY_STMT_IDENTITY, dest, 0},
716 {LY_STMT_IF_FEATURE, dest, 0},
717 {LY_STMT_IMPORT, dest, 0},
718 {LY_STMT_INCLUDE, dest, 0},
719 {LY_STMT_INPUT, dest, 0},
720 {LY_STMT_KEY, dest, YIN_SUBELEM_UNIQUE},
721 {LY_STMT_LEAF, dest, 0},
722 {LY_STMT_LEAF_LIST, dest, 0},
723 {LY_STMT_LENGTH, dest, 0},
724 {LY_STMT_LIST, dest, 0},
725 {LY_STMT_MANDATORY, dest, 0},
726 {LY_STMT_MAX_ELEMENTS, dest, 0},
727 {LY_STMT_MIN_ELEMENTS, dest, 0},
728 {LY_STMT_MODIFIER, dest, 0},
729 {LY_STMT_MODULE, dest, 0},
730 {LY_STMT_MUST, dest, 0},
731 {LY_STMT_NAMESPACE, dest, YIN_SUBELEM_UNIQUE},
732 {LY_STMT_NOTIFICATION, dest, 0},
733 {LY_STMT_ORDERED_BY, dest, 0},
734 {LY_STMT_ORGANIZATION, dest, 0},
735 {LY_STMT_OUTPUT, dest, 0},
736 {LY_STMT_PATH, dest, 0},
737 {LY_STMT_PATTERN, dest, 0},
738 {LY_STMT_POSITION, dest, 0},
739 {LY_STMT_PREFIX, dest, YIN_SUBELEM_UNIQUE},
740 {LY_STMT_PRESENCE, dest, YIN_SUBELEM_UNIQUE},
741 {LY_STMT_RANGE, dest, 0},
742 {LY_STMT_REFERENCE, dest, 0},
743 {LY_STMT_REFINE, dest, 0},
744 {LY_STMT_REQUIRE_INSTANCE, dest, 0},
745 {LY_STMT_REVISION, dest, 0},
746 {LY_STMT_REVISION_DATE, dest, 0},
747 {LY_STMT_RPC, dest, 0},
748 {LY_STMT_STATUS, dest, 0},
749 {LY_STMT_SUBMODULE, dest, 0},
750 {LY_STMT_TYPE, dest, 0},
751 {LY_STMT_TYPEDEF, dest, 0},
752 {LY_STMT_UNIQUE, dest, 0},
753 {LY_STMT_UNITS, dest, YIN_SUBELEM_UNIQUE},
754 {LY_STMT_USES, dest, 0},
755 {LY_STMT_VALUE, dest, 0},
756 {LY_STMT_WHEN, dest, 0},
757 {LY_STMT_YANG_VERSION, dest, 0},
758 {LY_STMT_YIN_ELEMENT, dest, 0},
759 {LY_STMT_EXTENSION_INSTANCE, dest, 0},
760 {LY_STMT_ARG_TEXT, dest, 0},
761 {LY_STMT_ARG_VALUE, dest, 0}
762 };
763
Radek Iša56ca9e42020-09-08 18:42:00 +0200764 ly_in_new_memory(data, &UTEST_IN);
765 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
766 prefix = YCTX->xmlctx->prefix;
767 prefix_len = YCTX->xmlctx->prefix_len;
768 name = YCTX->xmlctx->name;
769 name_len = YCTX->xmlctx->name_len;
770 lyxml_ctx_next(YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100771
Radek Iša56ca9e42020-09-08 18:42:00 +0200772 ret = yin_parse_content(YCTX, subelems, 71, yin_match_keyword(YCTX, name, name_len, prefix, prefix_len, LY_STMT_NONE), text, exts);
Michal Vaskob36053d2020-03-26 15:49:30 +0100773
Michal Vasko63f3d842020-07-08 10:10:14 +0200774 /* free parser and input */
Radek Iša56ca9e42020-09-08 18:42:00 +0200775 lyxml_ctx_free(YCTX->xmlctx);
776 YCTX->xmlctx = NULL;
777 ly_in_free(UTEST_IN, 0);
778 UTEST_IN = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200779 return ret;
780}
781
David Sedlákd1144562019-08-06 12:36:14 +0200782#define EXT_SUBELEM "<myext:c-define name=\"MY_MTU\" xmlns:myext=\"urn:example:extensions\"/>"
783
David Sedlák32488102019-07-15 17:44:10 +0200784static void
David Sedlák43801c92019-08-05 15:58:54 +0200785test_enum_elem(void **state)
David Sedlák32488102019-07-15 17:44:10 +0200786{
David Sedlák32488102019-07-15 17:44:10 +0200787 struct lysp_type type = {};
788 const char *data;
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100789
David Sedlák32488102019-07-15 17:44:10 +0200790 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100791 "<enum name=\"enum-name\">\n"
792 " <if-feature name=\"feature\" />\n"
793 " <value value=\"55\" />\n"
794 " <status value=\"deprecated\" />\n"
795 " <description><text>desc...</text></description>\n"
796 " <reference><text>ref...</text></reference>\n"
797 " " EXT_SUBELEM "\n"
798 "</enum>"
799 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200800 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
801 uint16_t flags = LYS_STATUS_DEPRC | LYS_SET_VALUE;
802
803 CHECK_LYSP_TYPE_ENUM(type.enums, "desc...", 1, flags, 1, "enum-name", "ref...", 55);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200804 assert_string_equal(type.enums->iffeatures[0].str, "feature");
Radek Iša56ca9e42020-09-08 18:42:00 +0200805 TEST_1_CHECK_LYSP_EXT_INSTANCE(type.enums->exts, LYEXT_SUBSTMT_SELF);
806 lysp_type_free(UTEST_LYCTX, &type);
David Sedlákd1144562019-08-06 12:36:14 +0200807 memset(&type, 0, sizeof type);
808
809 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100810 "<enum name=\"enum-name\"></enum>"
811 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200812 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200813 assert_string_equal(type.enums->name, "enum-name");
Radek Iša56ca9e42020-09-08 18:42:00 +0200814 lysp_type_free(UTEST_LYCTX, &type);
David Sedlák32488102019-07-15 17:44:10 +0200815 memset(&type, 0, sizeof type);
David Sedlák43801c92019-08-05 15:58:54 +0200816}
817
818static void
819test_bit_elem(void **state)
820{
David Sedlák43801c92019-08-05 15:58:54 +0200821 struct lysp_type type = {};
822 const char *data;
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100823
David Sedlák43801c92019-08-05 15:58:54 +0200824 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100825 "<bit name=\"bit-name\">\n"
826 " <if-feature name=\"feature\" />\n"
827 " <position value=\"55\" />\n"
828 " <status value=\"deprecated\" />\n"
829 " <description><text>desc...</text></description>\n"
830 " <reference><text>ref...</text></reference>\n"
831 EXT_SUBELEM
832 "</bit>"
833 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200834 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
835 uint16_t flags = LYS_STATUS_DEPRC | LYS_SET_VALUE;
836
837 CHECK_LYSP_TYPE_ENUM(type.bits, "desc...", 1, flags, 1, "bit-name", "ref...", 55);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200838 assert_string_equal(type.bits->iffeatures[0].str, "feature");
Radek Iša56ca9e42020-09-08 18:42:00 +0200839 TEST_1_CHECK_LYSP_EXT_INSTANCE(type.bits->exts, LYEXT_SUBSTMT_SELF);
840 lysp_type_free(UTEST_LYCTX, &type);
David Sedlákd1144562019-08-06 12:36:14 +0200841 memset(&type, 0, sizeof type);
842
843 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100844 "<bit name=\"bit-name\"> </bit>"
845 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200846 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
847 CHECK_LYSP_TYPE_ENUM(type.bits, NULL, 0, 0, 0, "bit-name", NULL, 0);
848 lysp_type_free(UTEST_LYCTX, &type);
David Sedlák43801c92019-08-05 15:58:54 +0200849 memset(&type, 0, sizeof type);
David Sedlák32488102019-07-15 17:44:10 +0200850}
851
852static void
853test_meta_elem(void **state)
854{
David Sedlák32488102019-07-15 17:44:10 +0200855 char *value = NULL;
856 const char *data;
David Sedlákd1144562019-08-06 12:36:14 +0200857 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200858
859 /* organization element */
860 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100861 "<organization><text>organization...</text>" EXT_SUBELEM EXT_SUBELEM "</organization>"
862 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200863 assert_int_equal(test_element_helper(state, data, &value, NULL, &exts), LY_SUCCESS);
864 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_ORGANIZATION);
865 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[1]), LYEXT_SUBSTMT_ORGANIZATION);
866
David Sedlák32488102019-07-15 17:44:10 +0200867 assert_string_equal(value, "organization...");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100868 lydict_remove(UTEST_LYCTX, value);
Radek Iša56ca9e42020-09-08 18:42:00 +0200869 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +0200870 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +0200871 exts = NULL;
872
David Sedlák32488102019-07-15 17:44:10 +0200873 /* contact element */
874 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100875 "<contact><text>contact...</text>" EXT_SUBELEM "</contact>"
876 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200877 assert_int_equal(test_element_helper(state, data, &value, NULL, &exts), LY_SUCCESS);
878 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_CONTACT);
David Sedlák32488102019-07-15 17:44:10 +0200879 assert_string_equal(value, "contact...");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100880 lydict_remove(UTEST_LYCTX, value);
Radek Iša56ca9e42020-09-08 18:42:00 +0200881 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +0200882 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200883 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +0200884
David Sedlák32488102019-07-15 17:44:10 +0200885 /* description element */
886 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100887 "<description><text>description...</text>" EXT_SUBELEM "</description>"
888 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200889 assert_int_equal(test_element_helper(state, data, &value, NULL, &exts), LY_SUCCESS);
890 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_DESCRIPTION);
David Sedlák32488102019-07-15 17:44:10 +0200891 assert_string_equal(value, "description...");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100892 lydict_remove(UTEST_LYCTX, value);
David Sedlák32488102019-07-15 17:44:10 +0200893 value = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +0200894 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +0200895 exts = NULL;
896
David Sedlák32488102019-07-15 17:44:10 +0200897 /* reference element */
898 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100899 "<reference><text>reference...</text>" EXT_SUBELEM "</reference>"
900 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200901 assert_int_equal(test_element_helper(state, data, &value, NULL, &exts), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +0200902 assert_string_equal(value, "reference...");
Radek Iša56ca9e42020-09-08 18:42:00 +0200903 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_REFERENCE);
Michal Vaskoe180ed02021-02-05 16:31:20 +0100904 lydict_remove(UTEST_LYCTX, value);
David Sedlák32488102019-07-15 17:44:10 +0200905 value = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +0200906 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +0200907 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200908
David Sedlákdf2a9732019-08-07 13:23:16 +0200909 /* reference element */
910 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100911 "<reference invalid=\"text\"><text>reference...</text>" "</reference>"
912 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200913 assert_int_equal(test_element_helper(state, data, &value, NULL, &exts), LY_EVALID);
914 CHECK_LOG_CTX("Unexpected attribute \"invalid\" of \"reference\" element.", "Line number 1.");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100915 lydict_remove(UTEST_LYCTX, value);
David Sedlákdf2a9732019-08-07 13:23:16 +0200916 value = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +0200917 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákdf2a9732019-08-07 13:23:16 +0200918 exts = NULL;
919
David Sedlák32488102019-07-15 17:44:10 +0200920 /* missing text subelement */
921 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100922 "<reference>reference...</reference>"
923 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200924 assert_int_equal(test_element_helper(state, data, &value, NULL, &exts), LY_EVALID);
925 CHECK_LOG_CTX("Missing mandatory sub-element \"text\" of \"reference\" element.", "Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +0200926
David Sedlákd1144562019-08-06 12:36:14 +0200927 /* reference element */
928 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100929 "<reference>" EXT_SUBELEM "<text>reference...</text></reference>"
930 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200931 assert_int_equal(test_element_helper(state, data, &value, NULL, &exts), LY_EVALID);
932 CHECK_LOG_CTX("Sub-element \"text\" of \"reference\" element must be defined as it's first sub-element.", "Line number 1.");
Michal Vaskoe180ed02021-02-05 16:31:20 +0100933 lydict_remove(UTEST_LYCTX, value);
David Sedlákd1144562019-08-06 12:36:14 +0200934 value = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +0200935 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +0200936 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200937}
938
939static void
940test_import_elem(void **state)
941{
David Sedlák32488102019-07-15 17:44:10 +0200942 const char *data;
David Sedlák298ff6d2019-07-26 14:29:03 +0200943 struct lysp_import *imports = NULL;
944 struct import_meta imp_meta = {"prefix", &imports};
David Sedlák32488102019-07-15 17:44:10 +0200945
946 /* max subelems */
947 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100948 "<import module=\"a\">\n"
949 EXT_SUBELEM
950 " <prefix value=\"a_mod\"/>\n"
951 " <revision-date date=\"2015-01-01\"></revision-date>\n"
952 " <description><text>import description</text></description>\n"
953 " <reference><text>import reference</text></reference>\n"
954 "</import>"
955 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200956 assert_int_equal(test_element_helper(state, data, &imp_meta, NULL, NULL), LY_SUCCESS);
957 CHECK_LYSP_IMPORT(imports, "import description", 1, "a",
958 "a_mod", "import reference", "2015-01-01");
959 TEST_1_CHECK_LYSP_EXT_INSTANCE(imports->exts, LYEXT_SUBSTMT_SELF);
960 FREE_ARRAY(UTEST_LYCTX, imports, lysp_import_free);
David Sedlák298ff6d2019-07-26 14:29:03 +0200961 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200962
963 /* min subelems */
David Sedlák32488102019-07-15 17:44:10 +0200964 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100965 "<import module=\"a\">\n"
966 " <prefix value=\"a_mod\"/>\n"
967 "</import>"
968 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200969 assert_int_equal(test_element_helper(state, data, &imp_meta, NULL, NULL), LY_SUCCESS);
970 CHECK_LYSP_IMPORT(imports, NULL, 0, "a",
971 "a_mod", NULL, "");
972 FREE_ARRAY(UTEST_LYCTX, imports, lysp_import_free);
David Sedlák298ff6d2019-07-26 14:29:03 +0200973 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200974
975 /* invalid (missing prefix) */
David Sedlák298ff6d2019-07-26 14:29:03 +0200976 data = ELEMENT_WRAPPER_START "<import module=\"a\"></import>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200977 assert_int_equal(test_element_helper(state, data, &imp_meta, NULL, NULL), LY_EVALID);
978 CHECK_LOG_CTX("Missing mandatory sub-element \"prefix\" of \"import\" element.", "Line number 1.");
979 FREE_ARRAY(UTEST_LYCTX, imports, lysp_import_free);
David Sedlák298ff6d2019-07-26 14:29:03 +0200980 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200981
982 /* invalid reused prefix */
David Sedlák32488102019-07-15 17:44:10 +0200983 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100984 "<import module=\"a\">\n"
985 " <prefix value=\"prefix\"/>\n"
986 "</import>"
987 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200988 assert_int_equal(test_element_helper(state, data, &imp_meta, NULL, NULL), LY_EVALID);
989 CHECK_LOG_CTX("Prefix \"prefix\" already used as module prefix.", "Line number 3.");
990 FREE_ARRAY(UTEST_LYCTX, imports, lysp_import_free);
David Sedlák298ff6d2019-07-26 14:29:03 +0200991 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200992
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100993 data = ELEMENT_WRAPPER_START
994 "<import module=\"a\">\n"
995 " <prefix value=\"a\"/>\n"
996 "</import>\n"
997 "<import module=\"a\">\n"
998 " <prefix value=\"a\"/>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +0200999 "</import>\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001000 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001001 assert_int_equal(test_element_helper(state, data, &imp_meta, NULL, NULL), LY_EVALID);
1002 CHECK_LOG_CTX("Prefix \"a\" already used to import \"a\" module.", "Line number 6.");
1003 FREE_ARRAY(UTEST_LYCTX, imports, lysp_import_free);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001004 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001005}
1006
1007static void
1008test_status_elem(void **state)
1009{
David Sedlák32488102019-07-15 17:44:10 +02001010 const char *data;
1011 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001012 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001013
1014 /* test valid values */
1015 data = ELEMENT_WRAPPER_START "<status value=\"current\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001016 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001017 assert_true(flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +02001018
1019 data = ELEMENT_WRAPPER_START "<status value=\"deprecated\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001020 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001021 assert_true(flags & LYS_STATUS_DEPRC);
David Sedlák32488102019-07-15 17:44:10 +02001022
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001023 data = ELEMENT_WRAPPER_START "<status value=\"obsolete\">"EXT_SUBELEM "</status>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001024 assert_int_equal(test_element_helper(state, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001025 assert_true(flags & LYS_STATUS_OBSLT);
Radek Iša56ca9e42020-09-08 18:42:00 +02001026 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_STATUS);
1027 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001028 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001029
1030 /* test invalid value */
1031 data = ELEMENT_WRAPPER_START "<status value=\"invalid\"></status>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001032 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID);
1033 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"status\" element. "
1034 "Valid values are \"current\", \"deprecated\" and \"obsolete\".", "Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001035}
1036
1037static void
1038test_ext_elem(void **state)
1039{
David Sedlák32488102019-07-15 17:44:10 +02001040 const char *data;
1041 struct lysp_ext *ext = NULL;
1042
1043 /* max subelems */
1044 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001045 "<extension name=\"ext_name\">\n"
1046 " <argument name=\"arg\"></argument>\n"
1047 " <status value=\"current\"/>\n"
1048 " <description><text>ext_desc</text></description>\n"
1049 " <reference><text>ext_ref</text></reference>\n"
1050 EXT_SUBELEM
1051 "</extension>"
1052 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001053 assert_int_equal(test_element_helper(state, data, &ext, NULL, NULL), LY_SUCCESS);
1054 CHECK_LYSP_EXT(ext, "arg", 0, "ext_desc", 1, LYS_STATUS_CURR, "ext_name", "ext_ref");
1055 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(ext->exts[0]), LYEXT_SUBSTMT_SELF);
1056 lysp_ext_free(UTEST_LYCTX, ext);
David Sedlák32488102019-07-15 17:44:10 +02001057 LY_ARRAY_FREE(ext);
1058 ext = NULL;
1059
1060 /* min subelems */
1061 data = ELEMENT_WRAPPER_START "<extension name=\"ext_name\"></extension>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001062 assert_int_equal(test_element_helper(state, data, &ext, NULL, NULL), LY_SUCCESS);
1063 CHECK_LYSP_EXT(ext, NULL, 0, NULL, 0, 0, "ext_name", NULL);
1064 lysp_ext_free(UTEST_LYCTX, ext);
David Sedlák32488102019-07-15 17:44:10 +02001065 LY_ARRAY_FREE(ext);
1066 ext = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001067}
1068
1069static void
1070test_yin_element_elem(void **state)
1071{
David Sedlák32488102019-07-15 17:44:10 +02001072 const char *data;
1073 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001074 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001075
1076 data = ELEMENT_WRAPPER_START "<yin-element value=\"true\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001077 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001078 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +02001079
David Sedlákd1144562019-08-06 12:36:14 +02001080 data = ELEMENT_WRAPPER_START "<yin-element value=\"false\">" EXT_SUBELEM "</yin-element>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001081 assert_int_equal(test_element_helper(state, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001082 assert_true(flags & LYS_YINELEM_TRUE);
Radek Krejcieccf6602021-02-05 19:42:54 +01001083 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_YIN_ELEMENT);
Radek Iša56ca9e42020-09-08 18:42:00 +02001084 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001085
1086 data = ELEMENT_WRAPPER_START "<yin-element value=\"invalid\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001087 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001088 assert_true(flags & LYS_YINELEM_TRUE);
Radek Iša56ca9e42020-09-08 18:42:00 +02001089 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"yin-element\" element. "
1090 "Valid values are \"true\" and \"false\".", "Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001091}
1092
1093static void
1094test_yangversion_elem(void **state)
1095{
David Sedlák32488102019-07-15 17:44:10 +02001096 const char *data;
1097 uint8_t version = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001098 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001099
1100 /* valid values */
Radek Krejci96e48da2020-09-04 13:18:06 +02001101 data = ELEMENT_WRAPPER_START "<yang-version value=\"1\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001102 assert_int_equal(test_element_helper(state, data, &version, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001103 assert_true(version & LYS_VERSION_1_0);
David Sedlák32488102019-07-15 17:44:10 +02001104
David Sedlákd1144562019-08-06 12:36:14 +02001105 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.1\">" EXT_SUBELEM "</yang-version>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001106 assert_int_equal(test_element_helper(state, data, &version, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001107 assert_true(version & LYS_VERSION_1_1);
Radek Krejcieccf6602021-02-05 19:42:54 +01001108 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_YANG_VERSION);
Radek Iša56ca9e42020-09-08 18:42:00 +02001109 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001110
1111 /* invalid value */
1112 data = ELEMENT_WRAPPER_START "<yang-version value=\"version\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001113 assert_int_equal(test_element_helper(state, data, &version, NULL, NULL), LY_EVALID);
1114 CHECK_LOG_CTX("Invalid value \"version\" of \"value\" attribute in \"yang-version\" element. "
1115 "Valid values are \"1\" and \"1.1\".", "Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001116}
1117
1118static void
1119test_mandatory_elem(void **state)
1120{
David Sedlák32488102019-07-15 17:44:10 +02001121 const char *data;
1122 uint16_t man = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001123 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001124
1125 /* valid values */
1126 data = ELEMENT_WRAPPER_START "<mandatory value=\"true\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001127 assert_int_equal(test_element_helper(state, data, &man, NULL, NULL), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001128 assert_int_equal(man, LYS_MAND_TRUE);
1129 man = 0;
1130
David Sedlákd1144562019-08-06 12:36:14 +02001131 data = ELEMENT_WRAPPER_START "<mandatory value=\"false\">" EXT_SUBELEM "</mandatory>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001132 assert_int_equal(test_element_helper(state, data, &man, NULL, &exts), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001133 assert_int_equal(man, LYS_MAND_FALSE);
Radek Iša56ca9e42020-09-08 18:42:00 +02001134 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_MANDATORY);
1135 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001136
1137 data = ELEMENT_WRAPPER_START "<mandatory value=\"invalid\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001138 assert_int_equal(test_element_helper(state, data, &man, NULL, NULL), LY_EVALID);
1139 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"mandatory\" element. "
1140 "Valid values are \"true\" and \"false\".", "Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001141}
1142
David Sedlák8e7bda82019-07-16 17:57:50 +02001143static void
1144test_argument_elem(void **state)
1145{
David Sedlák8e7bda82019-07-16 17:57:50 +02001146 const char *data;
1147 uint16_t flags = 0;
1148 const char *arg;
1149 struct yin_argument_meta arg_meta = {&flags, &arg};
David Sedlákd1144562019-08-06 12:36:14 +02001150 struct lysp_ext_instance *exts = NULL;
1151
David Sedlák8e7bda82019-07-16 17:57:50 +02001152 /* max subelems */
1153 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001154 "<argument name=\"arg-name\">\n"
1155 " <yin-element value=\"true\" />\n"
1156 EXT_SUBELEM
1157 "</argument>"
1158 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001159 assert_int_equal(test_element_helper(state, data, &arg_meta, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001160 assert_string_equal(arg, "arg-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001161 assert_true(flags & LYS_YINELEM_TRUE);
Radek Iša56ca9e42020-09-08 18:42:00 +02001162 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_ARGUMENT);
1163 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001164 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001165 flags = 0;
Michal Vaskoe180ed02021-02-05 16:31:20 +01001166 lydict_remove(UTEST_LYCTX, arg);
David Sedlák8e7bda82019-07-16 17:57:50 +02001167 arg = NULL;
1168
1169 /* min subelems */
1170 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001171 "<argument name=\"arg\">"
1172 "</argument>"
1173 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001174 assert_int_equal(test_element_helper(state, data, &arg_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001175 assert_string_equal(arg, "arg");
1176 assert_true(flags == 0);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001177 lydict_remove(UTEST_LYCTX, arg);
David Sedlák8e7bda82019-07-16 17:57:50 +02001178}
1179
1180static void
1181test_base_elem(void **state)
1182{
David Sedlák8e7bda82019-07-16 17:57:50 +02001183 const char *data;
1184 const char **bases = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001185 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001186 struct lysp_type type = {};
1187
1188 /* as identity subelement */
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001189 data = "<identity xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
1190 " <base name=\"base-name\">\n"
1191 EXT_SUBELEM
1192 " </base>\n"
1193 "</identity>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001194 assert_int_equal(test_element_helper(state, data, &bases, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001195 assert_string_equal(*bases, "base-name");
Radek Iša56ca9e42020-09-08 18:42:00 +02001196 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_BASE);
1197 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001198 exts = NULL;
Michal Vaskoe180ed02021-02-05 16:31:20 +01001199 lydict_remove(UTEST_LYCTX, *bases);
David Sedlák8e7bda82019-07-16 17:57:50 +02001200 LY_ARRAY_FREE(bases);
1201
1202 /* as type subelement */
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001203 data = "<type xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
1204 " <base name=\"base-name\">\n"
1205 EXT_SUBELEM
1206 " </base>\n"
1207 "</type>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001208 assert_int_equal(test_element_helper(state, data, &type, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001209 assert_string_equal(*type.bases, "base-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001210 assert_true(type.flags & LYS_SET_BASE);
Radek Iša56ca9e42020-09-08 18:42:00 +02001211 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_BASE);
1212 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001213 exts = NULL;
Michal Vaskoe180ed02021-02-05 16:31:20 +01001214 lydict_remove(UTEST_LYCTX, *type.bases);
David Sedlák8e7bda82019-07-16 17:57:50 +02001215 LY_ARRAY_FREE(type.bases);
David Sedlák8e7bda82019-07-16 17:57:50 +02001216}
1217
1218static void
1219test_belongsto_elem(void **state)
1220{
David Sedlák8e7bda82019-07-16 17:57:50 +02001221 const char *data;
1222 struct lysp_submodule submod;
David Sedlákd1144562019-08-06 12:36:14 +02001223 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001224
Radek Iša56ca9e42020-09-08 18:42:00 +02001225 lydict_insert(UTEST_LYCTX, "module-name", 0, &YCTX->parsed_mod->mod->name);
1226
David Sedlák8e7bda82019-07-16 17:57:50 +02001227 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001228 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/>"EXT_SUBELEM "</belongs-to>"
1229 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001230 assert_int_equal(test_element_helper(state, data, &submod, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001231 assert_string_equal(submod.prefix, "pref");
Radek Krejcieccf6602021-02-05 19:42:54 +01001232 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_BELONGS_TO);
Radek Iša56ca9e42020-09-08 18:42:00 +02001233 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001234 exts = NULL;
Michal Vaskoe180ed02021-02-05 16:31:20 +01001235 lydict_remove(UTEST_LYCTX, submod.prefix);
David Sedlák8e7bda82019-07-16 17:57:50 +02001236
1237 data = ELEMENT_WRAPPER_START "<belongs-to module=\"module-name\"></belongs-to>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001238 assert_int_equal(test_element_helper(state, data, &submod, NULL, NULL), LY_EVALID);
1239 CHECK_LOG_CTX("Missing mandatory sub-element \"prefix\" of \"belongs-to\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001240}
1241
1242static void
1243test_config_elem(void **state)
1244{
David Sedlák8e7bda82019-07-16 17:57:50 +02001245 const char *data;
1246 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001247 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001248
David Sedlákd1144562019-08-06 12:36:14 +02001249 data = ELEMENT_WRAPPER_START "<config value=\"true\">" EXT_SUBELEM "</config>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001250 assert_int_equal(test_element_helper(state, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001251 assert_true(flags & LYS_CONFIG_W);
Radek Iša56ca9e42020-09-08 18:42:00 +02001252 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_CONFIG);
1253 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001254 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001255 flags = 0;
1256
1257 data = ELEMENT_WRAPPER_START "<config value=\"false\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001258 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001259 assert_true(flags & LYS_CONFIG_R);
David Sedlák8e7bda82019-07-16 17:57:50 +02001260 flags = 0;
1261
1262 data = ELEMENT_WRAPPER_START "<config value=\"invalid\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001263 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID);
1264 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"config\" element. "
1265 "Valid values are \"true\" and \"false\".", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001266}
1267
1268static void
1269test_default_elem(void **state)
1270{
David Sedlák8e7bda82019-07-16 17:57:50 +02001271 const char *data;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001272 struct lysp_qname val = {0};
David Sedlákd1144562019-08-06 12:36:14 +02001273 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001274
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001275 data = ELEMENT_WRAPPER_START "<default value=\"defaul-value\">"EXT_SUBELEM "</default>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001276 assert_int_equal(test_element_helper(state, data, &val, NULL, &exts), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001277 assert_string_equal(val.str, "defaul-value");
Radek Iša56ca9e42020-09-08 18:42:00 +02001278 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_DEFAULT);
1279 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001280 exts = NULL;
Michal Vaskoe180ed02021-02-05 16:31:20 +01001281 lydict_remove(UTEST_LYCTX, val.str);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001282 val.str = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001283
1284 data = ELEMENT_WRAPPER_START "<default/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001285 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
1286 CHECK_LOG_CTX("Missing mandatory attribute value of default element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001287}
1288
1289static void
1290test_err_app_tag_elem(void **state)
1291{
David Sedlák8e7bda82019-07-16 17:57:50 +02001292 const char *data;
1293 const char *val = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001294 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001295
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001296 data = ELEMENT_WRAPPER_START "<error-app-tag value=\"val\">"EXT_SUBELEM "</error-app-tag>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001297 assert_int_equal(test_element_helper(state, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001298 assert_string_equal(val, "val");
Radek Krejcieccf6602021-02-05 19:42:54 +01001299 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_ERROR_APP_TAG);
Radek Iša56ca9e42020-09-08 18:42:00 +02001300 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001301 exts = NULL;
Michal Vaskoe180ed02021-02-05 16:31:20 +01001302 lydict_remove(UTEST_LYCTX, val);
David Sedlák8e7bda82019-07-16 17:57:50 +02001303 val = NULL;
1304
1305 data = ELEMENT_WRAPPER_START "<error-app-tag/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001306 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
1307 CHECK_LOG_CTX("Missing mandatory attribute value of error-app-tag element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001308}
1309
1310static void
1311test_err_msg_elem(void **state)
1312{
David Sedlák8e7bda82019-07-16 17:57:50 +02001313 const char *data;
1314 const char *val = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001315 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001316
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001317 data = ELEMENT_WRAPPER_START "<error-message><value>val</value>"EXT_SUBELEM "</error-message>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001318 assert_int_equal(test_element_helper(state, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001319 assert_string_equal(val, "val");
Radek Krejcieccf6602021-02-05 19:42:54 +01001320 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_ERROR_MESSAGE);
Radek Iša56ca9e42020-09-08 18:42:00 +02001321 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001322 exts = NULL;
Michal Vaskoe180ed02021-02-05 16:31:20 +01001323 lydict_remove(UTEST_LYCTX, val);
David Sedlák8e7bda82019-07-16 17:57:50 +02001324
1325 data = ELEMENT_WRAPPER_START "<error-message></error-message>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001326 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
1327 CHECK_LOG_CTX("Missing mandatory sub-element \"value\" of \"error-message\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001328
David Sedlákdf2a9732019-08-07 13:23:16 +02001329 data = ELEMENT_WRAPPER_START "<error-message invalid=\"text\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001330 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
1331 CHECK_LOG_CTX("Unexpected attribute \"invalid\" of \"error-message\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001332}
1333
1334static void
1335test_fracdigits_elem(void **state)
1336{
David Sedlák8e7bda82019-07-16 17:57:50 +02001337 const char *data;
1338 struct lysp_type type = {};
1339
1340 /* valid value */
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001341 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"10\">"EXT_SUBELEM "</fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001342 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
Radek Krejcieccf6602021-02-05 19:42:54 +01001343 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.exts[0]), LYEXT_SUBSTMT_FRACTION_DIGITS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001344 assert_int_equal(type.fraction_digits, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001345 assert_true(type.flags & LYS_SET_FRDIGITS);
Radek Iša56ca9e42020-09-08 18:42:00 +02001346 FREE_ARRAY(UTEST_LYCTX, type.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001347
1348 /* invalid values */
1349 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"-1\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001350 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1351 CHECK_LOG_CTX("Invalid value \"-1\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001352
1353 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"02\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001354 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1355 CHECK_LOG_CTX("Invalid value \"02\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001356
1357 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"1p\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001358 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1359 CHECK_LOG_CTX("Invalid value \"1p\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001360
1361 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"19\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001362 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1363 CHECK_LOG_CTX("Invalid value \"19\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001364
1365 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"999999999999999999\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001366 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1367 CHECK_LOG_CTX("Invalid value \"999999999999999999\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001368}
1369
1370static void
1371test_iffeature_elem(void **state)
1372{
David Sedlák8e7bda82019-07-16 17:57:50 +02001373 const char *data;
1374 const char **iffeatures = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001375 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001376
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001377 data = ELEMENT_WRAPPER_START "<if-feature name=\"local-storage\">"EXT_SUBELEM "</if-feature>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001378 assert_int_equal(test_element_helper(state, data, &iffeatures, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001379 assert_string_equal(*iffeatures, "local-storage");
Radek Krejcieccf6602021-02-05 19:42:54 +01001380 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_IF_FEATURE);
Radek Iša56ca9e42020-09-08 18:42:00 +02001381 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001382 exts = NULL;
Michal Vaskoe180ed02021-02-05 16:31:20 +01001383 lydict_remove(UTEST_LYCTX, *iffeatures);
David Sedlák8e7bda82019-07-16 17:57:50 +02001384 LY_ARRAY_FREE(iffeatures);
1385 iffeatures = NULL;
1386
1387 data = ELEMENT_WRAPPER_START "<if-feature/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001388 assert_int_equal(test_element_helper(state, data, &iffeatures, NULL, NULL), LY_EVALID);
1389 CHECK_LOG_CTX("Missing mandatory attribute name of if-feature element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001390 LY_ARRAY_FREE(iffeatures);
1391 iffeatures = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001392}
1393
1394static void
1395test_length_elem(void **state)
1396{
David Sedlák8e7bda82019-07-16 17:57:50 +02001397 const char *data;
1398 struct lysp_type type = {};
1399
1400 /* max subelems */
1401 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001402 "<length value=\"length-str\">\n"
1403 " <error-message><value>err-msg</value></error-message>\n"
1404 " <error-app-tag value=\"err-app-tag\"/>\n"
1405 " <description><text>desc</text></description>\n"
1406 " <reference><text>ref</text></reference>\n"
1407 EXT_SUBELEM
1408 "</length>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001409 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001410 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1411 CHECK_LYSP_RESTR(type.length, "length-str", "desc",
1412 "err-app-tag", "err-msg", 1, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001413 assert_true(type.flags & LYS_SET_LENGTH);
Radek Iša56ca9e42020-09-08 18:42:00 +02001414 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.length->exts[0]), LYEXT_SUBSTMT_SELF);
1415 lysp_type_free(UTEST_LYCTX, &type);
David Sedlák8e7bda82019-07-16 17:57:50 +02001416 memset(&type, 0, sizeof(type));
1417
1418 /* min subelems */
1419 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001420 "<length value=\"length-str\">"
1421 "</length>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001422 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001423 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1424 CHECK_LYSP_RESTR(type.length, "length-str", NULL,
1425 NULL, NULL, 0, NULL);
1426 lysp_type_free(UTEST_LYCTX, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001427 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001428 memset(&type, 0, sizeof(type));
1429
1430 data = ELEMENT_WRAPPER_START "<length></length>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001431 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1432 CHECK_LOG_CTX("Missing mandatory attribute value of length element.", "Line number 1.");
1433 lysp_type_free(UTEST_LYCTX, &type);
David Sedlák8e7bda82019-07-16 17:57:50 +02001434 memset(&type, 0, sizeof(type));
David Sedlák8e7bda82019-07-16 17:57:50 +02001435}
1436
1437static void
1438test_modifier_elem(void **state)
1439{
David Sedlák8e7bda82019-07-16 17:57:50 +02001440 const char *data;
Radek Krejci011e4aa2020-09-04 15:22:31 +02001441 const char *pat;
David Sedlákd1144562019-08-06 12:36:14 +02001442 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001443
Radek Iša56ca9e42020-09-08 18:42:00 +02001444 assert_int_equal(LY_SUCCESS, lydict_insert(UTEST_LYCTX, "\006pattern", 8, &pat));
David Sedlákd1144562019-08-06 12:36:14 +02001445 data = ELEMENT_WRAPPER_START "<modifier value=\"invert-match\">" EXT_SUBELEM "</modifier>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001446 assert_int_equal(test_element_helper(state, data, &pat, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001447 assert_string_equal(pat, "\x015pattern");
Radek Iša56ca9e42020-09-08 18:42:00 +02001448 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_MODIFIER);
1449 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001450 exts = NULL;
Michal Vaskoe180ed02021-02-05 16:31:20 +01001451 lydict_remove(UTEST_LYCTX, pat);
David Sedlák8e7bda82019-07-16 17:57:50 +02001452
Radek Iša56ca9e42020-09-08 18:42:00 +02001453 assert_int_equal(LY_SUCCESS, lydict_insert(UTEST_LYCTX, "\006pattern", 8, &pat));
David Sedlák8e7bda82019-07-16 17:57:50 +02001454 data = ELEMENT_WRAPPER_START "<modifier value=\"invert\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001455 assert_int_equal(test_element_helper(state, data, &pat, NULL, NULL), LY_EVALID);
1456 CHECK_LOG_CTX("Invalid value \"invert\" of \"value\" attribute in \"modifier\" element. "
1457 "Only valid value is \"invert-match\".", "Line number 1.");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001458 lydict_remove(UTEST_LYCTX, pat);
David Sedlák8e7bda82019-07-16 17:57:50 +02001459}
1460
1461static void
1462test_namespace_elem(void **state)
1463{
David Sedlák8e7bda82019-07-16 17:57:50 +02001464 const char *data;
1465 const char *ns;
David Sedlákd1144562019-08-06 12:36:14 +02001466 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001467
David Sedlákd1144562019-08-06 12:36:14 +02001468 data = ELEMENT_WRAPPER_START "<namespace uri=\"ns\">" EXT_SUBELEM "</namespace>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001469 assert_int_equal(test_element_helper(state, data, &ns, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001470 assert_string_equal(ns, "ns");
Radek Iša56ca9e42020-09-08 18:42:00 +02001471 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_NAMESPACE);
1472 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001473 exts = NULL;
Michal Vaskoe180ed02021-02-05 16:31:20 +01001474 lydict_remove(UTEST_LYCTX, ns);
David Sedlák8e7bda82019-07-16 17:57:50 +02001475
1476 data = ELEMENT_WRAPPER_START "<namespace/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001477 assert_int_equal(test_element_helper(state, data, &ns, NULL, NULL), LY_EVALID);
1478 CHECK_LOG_CTX("Missing mandatory attribute uri of namespace element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001479}
1480
1481static void
David Sedlák8e7bda82019-07-16 17:57:50 +02001482test_pattern_elem(void **state)
1483{
David Sedlák8e7bda82019-07-16 17:57:50 +02001484 const char *data;
1485 struct lysp_type type = {};
1486
1487 /* max subelems */
1488 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001489 "<pattern value=\"super_pattern\">\n"
1490 " <modifier value=\"invert-match\"/>\n"
1491 " <error-message><value>err-msg-value</value></error-message>\n"
1492 " <error-app-tag value=\"err-app-tag-value\"/>\n"
1493 " <description><text>&quot;pattern-desc&quot;</text></description>\n"
1494 " <reference><text>pattern-ref</text></reference>\n"
1495 EXT_SUBELEM
1496 "</pattern>"
1497 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001498 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001499 assert_true(type.flags & LYS_SET_PATTERN);
Radek Iša56ca9e42020-09-08 18:42:00 +02001500 CHECK_LYSP_RESTR(type.patterns, "\x015super_pattern", "\"pattern-desc\"",
1501 "err-app-tag-value", "err-msg-value", 1, "pattern-ref");
1502 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.patterns->exts[0]), LYEXT_SUBSTMT_SELF);
1503 lysp_type_free(UTEST_LYCTX, &type);
David Sedlák8e7bda82019-07-16 17:57:50 +02001504 memset(&type, 0, sizeof(type));
1505
1506 /* min subelems */
1507 data = ELEMENT_WRAPPER_START "<pattern value=\"pattern\"> </pattern>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001508 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1509 CHECK_LYSP_RESTR(type.patterns, "\x006pattern", NULL, NULL, NULL, 0, NULL);
1510 lysp_type_free(UTEST_LYCTX, &type);
David Sedlák8e7bda82019-07-16 17:57:50 +02001511 memset(&type, 0, sizeof(type));
David Sedlák8e7bda82019-07-16 17:57:50 +02001512}
1513
1514static void
1515test_value_position_elem(void **state)
1516{
David Sedlák8e7bda82019-07-16 17:57:50 +02001517 const char *data;
1518 struct lysp_type_enum en = {};
1519
1520 /* valid values */
David Sedlákd1144562019-08-06 12:36:14 +02001521 data = ELEMENT_WRAPPER_START "<value value=\"55\">" EXT_SUBELEM "</value>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001522 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1523 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 1, LYS_SET_VALUE, 0, NULL, NULL, 55);
1524 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(en.exts[0]), LYEXT_SUBSTMT_VALUE);
1525 FREE_ARRAY(UTEST_LYCTX, en.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001526 memset(&en, 0, sizeof(en));
1527
1528 data = ELEMENT_WRAPPER_START "<value value=\"-55\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001529 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1530 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, -55);
David Sedlák8e7bda82019-07-16 17:57:50 +02001531 memset(&en, 0, sizeof(en));
1532
1533 data = ELEMENT_WRAPPER_START "<value value=\"0\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001534 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1535 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, 0);
David Sedlák8e7bda82019-07-16 17:57:50 +02001536 memset(&en, 0, sizeof(en));
1537
1538 data = ELEMENT_WRAPPER_START "<value value=\"-0\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001539 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1540 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, 0);
David Sedlák8e7bda82019-07-16 17:57:50 +02001541 memset(&en, 0, sizeof(en));
1542
1543 /* valid positions */
David Sedlák8d552d62019-08-06 15:29:05 +02001544 data = ELEMENT_WRAPPER_START "<position value=\"55\">" EXT_SUBELEM "</position>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001545 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1546 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 1, LYS_SET_VALUE, 0, NULL, NULL, 55);
1547 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(en.exts[0]), LYEXT_SUBSTMT_POSITION);
1548 FREE_ARRAY(UTEST_LYCTX, en.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001549 memset(&en, 0, sizeof(en));
1550
1551 data = ELEMENT_WRAPPER_START "<position value=\"0\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001552 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1553 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, 0);
David Sedlák8e7bda82019-07-16 17:57:50 +02001554 memset(&en, 0, sizeof(en));
1555
1556 /* invalid values */
1557 data = ELEMENT_WRAPPER_START "<value value=\"99999999999999999999999\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001558 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1559 CHECK_LOG_CTX("Invalid value \"99999999999999999999999\" of \"value\" attribute in \"value\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001560
1561 data = ELEMENT_WRAPPER_START "<value value=\"1k\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001562 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1563 CHECK_LOG_CTX("Invalid value \"1k\" of \"value\" attribute in \"value\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001564
David Sedlák69f01612019-07-17 11:41:08 +02001565 data = ELEMENT_WRAPPER_START "<value value=\"\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001566 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1567 CHECK_LOG_CTX("Invalid value \"\" of \"value\" attribute in \"value\" element.", "Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001568
David Sedlák8e7bda82019-07-16 17:57:50 +02001569 /*invalid positions */
1570 data = ELEMENT_WRAPPER_START "<position value=\"-5\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001571 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1572 CHECK_LOG_CTX("Invalid value \"-5\" of \"value\" attribute in \"position\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001573
1574 data = ELEMENT_WRAPPER_START "<position value=\"-0\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001575 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1576 CHECK_LOG_CTX("Invalid value \"-0\" of \"value\" attribute in \"position\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001577
1578 data = ELEMENT_WRAPPER_START "<position value=\"99999999999999999999\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001579 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1580 CHECK_LOG_CTX("Invalid value \"99999999999999999999\" of \"value\" attribute in \"position\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001581
David Sedlák69f01612019-07-17 11:41:08 +02001582 data = ELEMENT_WRAPPER_START "<position value=\"\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001583 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1584 CHECK_LOG_CTX("Invalid value \"\" of \"value\" attribute in \"position\" element.", "Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001585}
1586
1587static void
1588test_prefix_elem(void **state)
1589{
David Sedlák69f01612019-07-17 11:41:08 +02001590 const char *data;
1591 const char *value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001592 struct lysp_ext_instance *exts = NULL;
1593
1594 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\">" EXT_SUBELEM "</prefix>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001595 assert_int_equal(test_element_helper(state, data, &value, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001596 assert_string_equal(value, "pref");
Radek Iša56ca9e42020-09-08 18:42:00 +02001597 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_PREFIX);
1598 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001599 exts = NULL;
Michal Vaskoe180ed02021-02-05 16:31:20 +01001600 lydict_remove(UTEST_LYCTX, value);
David Sedlák69f01612019-07-17 11:41:08 +02001601
1602 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001603 assert_int_equal(test_element_helper(state, data, &value, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001604 assert_string_equal(value, "pref");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001605 lydict_remove(UTEST_LYCTX, value);
David Sedlák69f01612019-07-17 11:41:08 +02001606}
1607
1608static void
1609test_range_elem(void **state)
1610{
David Sedlák69f01612019-07-17 11:41:08 +02001611 const char *data;
1612 struct lysp_type type = {};
1613
1614 /* max subelems */
1615 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001616 "<range value=\"range-str\">\n"
1617 " <error-message><value>err-msg</value></error-message>\n"
1618 " <error-app-tag value=\"err-app-tag\" />\n"
1619 " <description><text>desc</text></description>\n"
1620 " <reference><text>ref</text></reference>\n"
1621 EXT_SUBELEM
1622 "</range>"
1623 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001624 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1625 CHECK_LYSP_RESTR(type.range, "range-str", "desc",
1626 "err-app-tag", "err-msg", 1, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001627 assert_true(type.flags & LYS_SET_RANGE);
Radek Iša56ca9e42020-09-08 18:42:00 +02001628 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.range->exts[0]), LYEXT_SUBSTMT_SELF);
1629 lysp_type_free(UTEST_LYCTX, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001630 memset(&type, 0, sizeof(type));
1631
1632 /* min subelems */
1633 data = ELEMENT_WRAPPER_START "<range value=\"range-str\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001634 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1635 CHECK_LYSP_RESTR(type.range, "range-str", NULL,
1636 NULL, NULL, 0, NULL);
1637 lysp_type_free(UTEST_LYCTX, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001638 memset(&type, 0, sizeof(type));
David Sedlák69f01612019-07-17 11:41:08 +02001639}
1640
1641static void
1642test_reqinstance_elem(void **state)
1643{
David Sedlák69f01612019-07-17 11:41:08 +02001644 const char *data;
1645 struct lysp_type type = {};
1646
David Sedlákd1144562019-08-06 12:36:14 +02001647 data = ELEMENT_WRAPPER_START "<require-instance value=\"true\">" EXT_SUBELEM "</require-instance>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001648 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001649 assert_int_equal(type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001650 assert_true(type.flags & LYS_SET_REQINST);
Radek Krejcieccf6602021-02-05 19:42:54 +01001651 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.exts[0]), LYEXT_SUBSTMT_REQUIRE_INSTANCE);
Radek Iša56ca9e42020-09-08 18:42:00 +02001652 lysp_type_free(UTEST_LYCTX, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001653 memset(&type, 0, sizeof(type));
1654
1655 data = ELEMENT_WRAPPER_START "<require-instance value=\"false\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001656 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001657 assert_int_equal(type.require_instance, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001658 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001659 memset(&type, 0, sizeof(type));
1660
1661 data = ELEMENT_WRAPPER_START "<require-instance value=\"invalid\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001662 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
David Sedlák69f01612019-07-17 11:41:08 +02001663 memset(&type, 0, sizeof(type));
Radek Iša56ca9e42020-09-08 18:42:00 +02001664 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"require-instance\" element. "
1665 "Valid values are \"true\" and \"false\".", "Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001666}
1667
1668static void
1669test_revision_date_elem(void **state)
1670{
David Sedlák69f01612019-07-17 11:41:08 +02001671 const char *data;
1672 char rev[LY_REV_SIZE];
David Sedlákd1144562019-08-06 12:36:14 +02001673 struct lysp_ext_instance *exts = NULL;
1674
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001675 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\">"EXT_SUBELEM "</revision-date>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001676 assert_int_equal(test_element_helper(state, data, rev, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001677 assert_string_equal(rev, "2000-01-01");
Radek Krejcieccf6602021-02-05 19:42:54 +01001678 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_REVISION_DATE);
Radek Iša56ca9e42020-09-08 18:42:00 +02001679 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlák69f01612019-07-17 11:41:08 +02001680
1681 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001682 assert_int_equal(test_element_helper(state, data, rev, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001683 assert_string_equal(rev, "2000-01-01");
1684
1685 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-50-05\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001686 assert_int_equal(test_element_helper(state, data, rev, NULL, NULL), LY_EVALID);
1687 CHECK_LOG_CTX("Invalid value \"2000-50-05\" of \"revision-date\".", "Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001688}
1689
1690static void
1691test_unique_elem(void **state)
1692{
David Sedlák69f01612019-07-17 11:41:08 +02001693 const char *data;
1694 const char **values = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001695 struct lysp_ext_instance *exts = NULL;
1696
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001697 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\">"EXT_SUBELEM "</unique>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001698 assert_int_equal(test_element_helper(state, data, &values, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001699 assert_string_equal(*values, "tag");
Radek Iša56ca9e42020-09-08 18:42:00 +02001700 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_UNIQUE);
1701 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001702 lydict_remove(UTEST_LYCTX, *values);
David Sedlákd1144562019-08-06 12:36:14 +02001703 LY_ARRAY_FREE(values);
1704 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001705
1706 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001707 assert_int_equal(test_element_helper(state, data, &values, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001708 assert_string_equal(*values, "tag");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001709 lydict_remove(UTEST_LYCTX, *values);
David Sedlák69f01612019-07-17 11:41:08 +02001710 LY_ARRAY_FREE(values);
David Sedlákd1144562019-08-06 12:36:14 +02001711 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001712}
1713
1714static void
1715test_units_elem(void **state)
1716{
David Sedlák69f01612019-07-17 11:41:08 +02001717 const char *data;
1718 const char *values = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001719 struct lysp_ext_instance *exts = NULL;
1720
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001721 data = ELEMENT_WRAPPER_START "<units name=\"name\">"EXT_SUBELEM "</units>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001722 assert_int_equal(test_element_helper(state, data, &values, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001723 assert_string_equal(values, "name");
Radek Iša56ca9e42020-09-08 18:42:00 +02001724 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_UNITS);
1725 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001726 lydict_remove(UTEST_LYCTX, values);
David Sedlákd1144562019-08-06 12:36:14 +02001727 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001728
1729 data = ELEMENT_WRAPPER_START "<units name=\"name\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001730 assert_int_equal(test_element_helper(state, data, &values, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001731 assert_string_equal(values, "name");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001732 lydict_remove(UTEST_LYCTX, values);
David Sedlákd1144562019-08-06 12:36:14 +02001733 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001734}
1735
1736static void
1737test_when_elem(void **state)
1738{
David Sedlák69f01612019-07-17 11:41:08 +02001739 const char *data;
1740 struct lysp_when *when = NULL;
1741
1742 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001743 "<when condition=\"cond\">\n"
1744 " <description><text>desc</text></description>\n"
1745 " <reference><text>ref</text></reference>\n"
1746 EXT_SUBELEM
1747 "</when>"
1748 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001749 assert_int_equal(test_element_helper(state, data, &when, NULL, NULL), LY_SUCCESS);
1750 CHECK_LYSP_WHEN(when, "cond", "desc", 1, "ref");
1751 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(when->exts[0]), LYEXT_SUBSTMT_SELF);
1752 lysp_when_free(UTEST_LYCTX, when);
David Sedlák69f01612019-07-17 11:41:08 +02001753 free(when);
1754 when = NULL;
1755
1756 data = ELEMENT_WRAPPER_START "<when condition=\"cond\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001757 assert_int_equal(test_element_helper(state, data, &when, NULL, NULL), LY_SUCCESS);
1758 CHECK_LYSP_WHEN(when, "cond", NULL, 0, NULL);
1759 lysp_when_free(UTEST_LYCTX, when);
David Sedlák69f01612019-07-17 11:41:08 +02001760 free(when);
1761 when = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001762}
1763
1764static void
1765test_yin_text_value_elem(void **state)
1766{
David Sedlák69f01612019-07-17 11:41:08 +02001767 const char *data;
1768 const char *val;
1769
1770 data = ELEMENT_WRAPPER_START "<text>text</text>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001771 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001772 assert_string_equal(val, "text");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001773 lydict_remove(UTEST_LYCTX, val);
David Sedlák69f01612019-07-17 11:41:08 +02001774
1775 data = "<error-message xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <value>text</value> </error-message>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001776 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001777 assert_string_equal(val, "text");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001778 lydict_remove(UTEST_LYCTX, val);
David Sedlák69f01612019-07-17 11:41:08 +02001779
1780 data = ELEMENT_WRAPPER_START "<text></text>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001781 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001782 assert_string_equal("", val);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001783 lydict_remove(UTEST_LYCTX, val);
David Sedlák8e7bda82019-07-16 17:57:50 +02001784}
David Sedlák32488102019-07-15 17:44:10 +02001785
David Sedlák374d2b32019-07-17 15:06:55 +02001786static void
1787test_type_elem(void **state)
1788{
David Sedlák374d2b32019-07-17 15:06:55 +02001789 const char *data;
1790 struct lysp_type type = {};
1791
1792 /* max subelems */
1793 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001794 "<type name=\"type-name\">\n"
1795 " <base name=\"base-name\"/>\n"
1796 " <bit name=\"bit\"/>\n"
1797 " <enum name=\"enum\"/>\n"
1798 " <fraction-digits value=\"2\"/>\n"
1799 " <length value=\"length\"/>\n"
1800 " <path value=\"/path\"/>\n"
1801 " <pattern value=\"pattern\"/>\n"
1802 " <range value=\"range\" />\n"
1803 " <require-instance value=\"true\"/>\n"
1804 " <type name=\"sub-type-name\"/>\n"
1805 EXT_SUBELEM
1806 "</type>"
1807 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001808 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák374d2b32019-07-17 15:06:55 +02001809 assert_string_equal(type.name, "type-name");
1810 assert_string_equal(*type.bases, "base-name");
1811 assert_string_equal(type.bits->name, "bit");
1812 assert_string_equal(type.enums->name, "enum");
1813 assert_int_equal(type.fraction_digits, 2);
Radek Iša56ca9e42020-09-08 18:42:00 +02001814 CHECK_LYSP_RESTR(type.length, "length", NULL,
1815 NULL, NULL, 0, NULL);
Michal Vaskocb8c6d42020-10-16 11:58:30 +02001816 assert_string_equal(type.path->expr, "/path");
Radek Iša56ca9e42020-09-08 18:42:00 +02001817 CHECK_LYSP_RESTR(type.patterns, "\006pattern", NULL,
1818 NULL, NULL, 0, NULL);
1819 CHECK_LYSP_RESTR(type.range, "range", NULL,
1820 NULL, NULL, 0, NULL);
David Sedlák374d2b32019-07-17 15:06:55 +02001821 assert_int_equal(type.require_instance, 1);
1822 assert_string_equal(type.types->name, "sub-type-name");
Radek Iša56ca9e42020-09-08 18:42:00 +02001823 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.exts[0]), LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001824 assert_true(type.flags & LYS_SET_BASE);
1825 assert_true(type.flags & LYS_SET_BIT);
1826 assert_true(type.flags & LYS_SET_ENUM);
1827 assert_true(type.flags & LYS_SET_FRDIGITS);
1828 assert_true(type.flags & LYS_SET_LENGTH);
1829 assert_true(type.flags & LYS_SET_PATH);
1830 assert_true(type.flags & LYS_SET_PATTERN);
1831 assert_true(type.flags & LYS_SET_RANGE);
1832 assert_true(type.flags & LYS_SET_REQINST);
1833 assert_true(type.flags & LYS_SET_TYPE);
Radek Iša56ca9e42020-09-08 18:42:00 +02001834 lysp_type_free(UTEST_LYCTX, &type);
David Sedlák374d2b32019-07-17 15:06:55 +02001835 memset(&type, 0, sizeof(type));
1836
1837 /* min subelems */
1838 data = ELEMENT_WRAPPER_START "<type name=\"type-name\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001839 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1840 lysp_type_free(UTEST_LYCTX, &type);
David Sedlák374d2b32019-07-17 15:06:55 +02001841 memset(&type, 0, sizeof(type));
David Sedlák374d2b32019-07-17 15:06:55 +02001842}
1843
David Sedlák1af868e2019-07-17 17:03:14 +02001844static void
1845test_max_elems_elem(void **state)
1846{
David Sedlák1af868e2019-07-17 17:03:14 +02001847 const char *data;
1848 struct lysp_node_list list = {};
1849 struct lysp_node_leaflist llist = {};
1850 struct lysp_refine refine = {};
1851
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001852 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"unbounded\">"EXT_SUBELEM "</max-elements> </refine>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001853 assert_int_equal(test_element_helper(state, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02001854 assert_int_equal(refine.max, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001855 assert_true(refine.flags & LYS_SET_MAX);
Radek Krejcieccf6602021-02-05 19:42:54 +01001856 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(refine.exts[0]), LYEXT_SUBSTMT_MAX_ELEMENTS);
Radek Iša56ca9e42020-09-08 18:42:00 +02001857 FREE_ARRAY(UTEST_LYCTX, refine.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02001858
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001859 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"5\">"EXT_SUBELEM "</max-elements> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001860 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02001861 assert_int_equal(list.max, 5);
Radek Iša56ca9e42020-09-08 18:42:00 +02001862 CHECK_LYSP_NODE(&list, NULL, 1, LYS_SET_MAX, 0, NULL, 0, LYS_UNKNOWN, NULL, NULL, 0);
Radek Krejcieccf6602021-02-05 19:42:54 +01001863 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(list.exts[0]), LYEXT_SUBSTMT_MAX_ELEMENTS);
Radek Iša56ca9e42020-09-08 18:42:00 +02001864 FREE_ARRAY(UTEST_LYCTX, list.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02001865
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001866 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"85\">"EXT_SUBELEM "</max-elements> </leaf-list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001867 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02001868 assert_int_equal(llist.max, 85);
Radek Iša56ca9e42020-09-08 18:42:00 +02001869 CHECK_LYSP_NODE(&llist, NULL, 1, LYS_SET_MAX, 0, NULL, 0, LYS_UNKNOWN, NULL, NULL, 0);
Radek Krejcieccf6602021-02-05 19:42:54 +01001870 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(llist.exts[0]), LYEXT_SUBSTMT_MAX_ELEMENTS);
Radek Iša56ca9e42020-09-08 18:42:00 +02001871 FREE_ARRAY(UTEST_LYCTX, llist.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02001872
1873 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"10\"/> </refine>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001874 assert_int_equal(test_element_helper(state, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02001875 assert_int_equal(refine.max, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001876 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001877
1878 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"0\"/> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001879 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID);
1880 CHECK_LOG_CTX("Invalid value \"0\" of \"value\" attribute in \"max-elements\" element.", "Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02001881
1882 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"-10\"/> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001883 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID);
1884 CHECK_LOG_CTX("Invalid value \"-10\" of \"value\" attribute in \"max-elements\" element.", "Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02001885
1886 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"k\"/> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001887 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID);
1888 CHECK_LOG_CTX("Invalid value \"k\" of \"value\" attribute in \"max-elements\" element.", "Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02001889
1890 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"u12\"/> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001891 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID);
1892 CHECK_LOG_CTX("Invalid value \"u12\" of \"value\" attribute in \"max-elements\" element.", "Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02001893}
1894
David Sedlák09e18c92019-07-18 11:17:11 +02001895static void
1896test_min_elems_elem(void **state)
1897{
David Sedlák09e18c92019-07-18 11:17:11 +02001898 const char *data;
1899 struct lysp_node_list list = {};
1900 struct lysp_node_leaflist llist = {};
1901 struct lysp_refine refine = {};
1902
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001903 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"0\">"EXT_SUBELEM "</min-elements> </refine>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001904 assert_int_equal(test_element_helper(state, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák09e18c92019-07-18 11:17:11 +02001905 assert_int_equal(refine.min, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001906 assert_true(refine.flags & LYS_SET_MIN);
Radek Krejcieccf6602021-02-05 19:42:54 +01001907 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(refine.exts[0]), LYEXT_SUBSTMT_MIN_ELEMENTS);
Radek Iša56ca9e42020-09-08 18:42:00 +02001908 FREE_ARRAY(UTEST_LYCTX, refine.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02001909
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001910 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"41\">"EXT_SUBELEM "</min-elements> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001911 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_SUCCESS);
David Sedlák09e18c92019-07-18 11:17:11 +02001912 assert_int_equal(list.min, 41);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001913 assert_true(list.flags & LYS_SET_MIN);
Radek Krejcieccf6602021-02-05 19:42:54 +01001914 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(list.exts[0]), LYEXT_SUBSTMT_MIN_ELEMENTS);
Radek Iša56ca9e42020-09-08 18:42:00 +02001915 FREE_ARRAY(UTEST_LYCTX, list.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02001916
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001917 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"50\">"EXT_SUBELEM "</min-elements> </leaf-list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001918 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_SUCCESS);
David Sedlák09e18c92019-07-18 11:17:11 +02001919 assert_int_equal(llist.min, 50);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001920 assert_true(llist.flags & LYS_SET_MIN);
Radek Krejcieccf6602021-02-05 19:42:54 +01001921 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(llist.exts[0]), LYEXT_SUBSTMT_MIN_ELEMENTS);
Radek Iša56ca9e42020-09-08 18:42:00 +02001922 FREE_ARRAY(UTEST_LYCTX, llist.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02001923
1924 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"-5\"/> </leaf-list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001925 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID);
1926 CHECK_LOG_CTX("Value \"-5\" of \"value\" attribute in \"min-elements\" element is out of bounds.", "Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02001927
1928 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"99999999999999999\"/> </leaf-list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001929 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID);
1930 CHECK_LOG_CTX("Value \"99999999999999999\" of \"value\" attribute in \"min-elements\" element is out of bounds.", "Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02001931
1932 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"5k\"/> </leaf-list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001933 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID);
1934 CHECK_LOG_CTX("Invalid value \"5k\" of \"value\" attribute in \"min-elements\" element.", "Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02001935
1936 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"05\"/> </leaf-list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001937 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID);
1938 CHECK_LOG_CTX("Invalid value \"05\" of \"value\" attribute in \"min-elements\" element.", "Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02001939}
1940
David Sedláka2dad212019-07-18 12:45:19 +02001941static void
1942test_ordby_elem(void **state)
1943{
David Sedláka2dad212019-07-18 12:45:19 +02001944 const char *data;
1945 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001946 struct lysp_ext_instance *exts = NULL;
David Sedláka2dad212019-07-18 12:45:19 +02001947
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001948 data = ELEMENT_WRAPPER_START "<ordered-by value=\"system\">"EXT_SUBELEM "</ordered-by>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001949 assert_int_equal(test_element_helper(state, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001950 assert_true(flags & LYS_ORDBY_SYSTEM);
Radek Krejcieccf6602021-02-05 19:42:54 +01001951 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_ORDERED_BY);
Radek Iša56ca9e42020-09-08 18:42:00 +02001952 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedláka2dad212019-07-18 12:45:19 +02001953
1954 data = ELEMENT_WRAPPER_START "<ordered-by value=\"user\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001955 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001956 assert_true(flags & LYS_ORDBY_USER);
David Sedláka2dad212019-07-18 12:45:19 +02001957
1958 data = ELEMENT_WRAPPER_START "<ordered-by value=\"inv\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001959 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID);
1960 CHECK_LOG_CTX("Invalid value \"inv\" of \"value\" attribute in \"ordered-by\" element. "
1961 "Valid values are \"system\" and \"user\".", "Line number 1.");
David Sedláka2dad212019-07-18 12:45:19 +02001962}
1963
David Sedlák8a83bbb2019-07-18 14:46:00 +02001964static void
1965test_any_elem(void **state)
1966{
David Sedlák8a83bbb2019-07-18 14:46:00 +02001967 const char *data;
1968 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02001969 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák8a83bbb2019-07-18 14:46:00 +02001970 struct lysp_node_anydata *parsed = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02001971 uint16_t flags;
David Sedlák8a83bbb2019-07-18 14:46:00 +02001972
1973 /* anyxml max subelems */
1974 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001975 "<anyxml name=\"any-name\">\n"
1976 " <config value=\"true\" />\n"
1977 " <description><text>desc</text></description>\n"
1978 " <if-feature name=\"feature\" />\n"
1979 " <mandatory value=\"true\" />\n"
1980 " <must condition=\"must-cond\" />\n"
1981 " <reference><text>ref</text></reference>\n"
1982 " <status value=\"deprecated\"/>\n"
1983 " <when condition=\"when-cond\"/>\n"
1984 EXT_SUBELEM
1985 "</anyxml>"
1986 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001987 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001988 parsed = (struct lysp_node_anydata *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02001989 flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_DEPRC;
1990 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
1991 "any-name", 0, LYS_ANYXML, 0, "ref", 1);
1992 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001993 assert_string_equal(parsed->iffeatures[0].str, "feature");
Radek Iša56ca9e42020-09-08 18:42:00 +02001994 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LYEXT_SUBSTMT_SELF);
1995 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001996 siblings = NULL;
1997
1998 /* anydata max subelems */
1999 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002000 "<anydata name=\"any-name\">\n"
2001 " <config value=\"true\" />\n"
2002 " <description><text>desc</text></description>\n"
2003 " <if-feature name=\"feature\" />\n"
2004 " <mandatory value=\"true\" />\n"
2005 " <must condition=\"must-cond\" />\n"
2006 " <reference><text>ref</text></reference>\n"
2007 " <status value=\"deprecated\"/>\n"
2008 " <when condition=\"when-cond\"/>\n"
2009 EXT_SUBELEM
2010 "</anydata>"
2011 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002012 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002013 parsed = (struct lysp_node_anydata *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002014 flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_DEPRC;
2015 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2016 "any-name", 0, LYS_ANYDATA, 0, "ref", 1);
2017 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002018 assert_string_equal(parsed->iffeatures[0].str, "feature");
Radek Iša56ca9e42020-09-08 18:42:00 +02002019 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LYEXT_SUBSTMT_SELF);
2020 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002021 siblings = NULL;
2022
2023 /* min subelems */
2024 node_meta.parent = (void *)0x10;
2025 data = ELEMENT_WRAPPER_START "<anydata name=\"any-name\"> </anydata>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002026 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002027 parsed = (struct lysp_node_anydata *)siblings;
2028 assert_ptr_equal(parsed->parent, node_meta.parent);
Radek Iša56ca9e42020-09-08 18:42:00 +02002029 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2030 "any-name", 0, LYS_ANYDATA, 1, NULL, 0);
2031 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002032}
2033
David Sedlák203ca3a2019-07-18 15:26:25 +02002034static void
2035test_leaf_elem(void **state)
2036{
David Sedlák203ca3a2019-07-18 15:26:25 +02002037 const char *data;
2038 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002039 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák203ca3a2019-07-18 15:26:25 +02002040 struct lysp_node_leaf *parsed = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02002041 uint16_t flags;
David Sedlák203ca3a2019-07-18 15:26:25 +02002042
2043 /* max elements */
2044 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002045 "<leaf name=\"leaf\">\n"
2046 " <config value=\"true\" />\n"
2047 " <default value=\"def-val\"/>\n"
2048 " <description><text>desc</text></description>\n"
2049 " <if-feature name=\"feature\" />\n"
2050 " <mandatory value=\"true\" />\n"
2051 " <must condition=\"must-cond\" />\n"
2052 " <reference><text>ref</text></reference>\n"
2053 " <status value=\"deprecated\"/>\n"
2054 " <type name=\"type\"/>\n"
2055 " <units name=\"uni\"/>\n"
2056 " <when condition=\"when-cond\"/>\n"
2057 EXT_SUBELEM
2058 "</leaf>"
David Sedlák203ca3a2019-07-18 15:26:25 +02002059 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002060 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák203ca3a2019-07-18 15:26:25 +02002061 parsed = (struct lysp_node_leaf *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002062 flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_DEPRC;
2063 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2064 "leaf", 0, LYS_LEAF, 0, "ref", 1);
2065 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002066 assert_string_equal(parsed->iffeatures[0].str, "feature");
Radek Iša56ca9e42020-09-08 18:42:00 +02002067 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LYEXT_SUBSTMT_SELF);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002068 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlák203ca3a2019-07-18 15:26:25 +02002069 assert_string_equal(parsed->type.name, "type");
2070 assert_string_equal(parsed->units, "uni");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002071 assert_string_equal(parsed->dflt.str, "def-val");
Radek Iša56ca9e42020-09-08 18:42:00 +02002072 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlák203ca3a2019-07-18 15:26:25 +02002073 siblings = NULL;
2074
2075 /* min elements */
2076 data = ELEMENT_WRAPPER_START "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002077 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák203ca3a2019-07-18 15:26:25 +02002078 parsed = (struct lysp_node_leaf *)siblings;
2079 assert_string_equal(parsed->name, "leaf");
2080 assert_string_equal(parsed->type.name, "type");
Radek Iša56ca9e42020-09-08 18:42:00 +02002081 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlák203ca3a2019-07-18 15:26:25 +02002082 siblings = NULL;
David Sedlák203ca3a2019-07-18 15:26:25 +02002083}
2084
David Sedlákc3da3ef2019-07-19 12:56:08 +02002085static void
2086test_leaf_list_elem(void **state)
2087{
David Sedlákc3da3ef2019-07-19 12:56:08 +02002088 const char *data;
2089 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002090 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlákc3da3ef2019-07-19 12:56:08 +02002091 struct lysp_node_leaflist *parsed = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02002092 uint16_t flags;
David Sedlákc3da3ef2019-07-19 12:56:08 +02002093
2094 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002095 "<leaf-list name=\"llist\">\n"
2096 " <config value=\"true\" />\n"
2097 " <default value=\"def-val0\"/>\n"
2098 " <default value=\"def-val1\"/>\n"
2099 " <description><text>desc</text></description>\n"
2100 " <if-feature name=\"feature\"/>\n"
2101 " <max-elements value=\"5\"/>\n"
2102 " <must condition=\"must-cond\"/>\n"
2103 " <ordered-by value=\"user\" />\n"
2104 " <reference><text>ref</text></reference>\n"
2105 " <status value=\"current\"/>\n"
2106 " <type name=\"type\"/>\n"
2107 " <units name=\"uni\"/>\n"
2108 " <when condition=\"when-cond\"/>\n"
2109 EXT_SUBELEM
2110 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002111 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002112 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002113 parsed = (struct lysp_node_leaflist *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002114 flags = LYS_CONFIG_W | LYS_ORDBY_USER | LYS_STATUS_CURR | LYS_SET_MAX;
2115 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2116 "llist", 0, LYS_LEAFLIST, 0, "ref", 1);
2117 CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002118 assert_string_equal(parsed->dflts[0].str, "def-val0");
2119 assert_string_equal(parsed->dflts[1].str, "def-val1");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002120 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002121 assert_int_equal(parsed->max, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002122 assert_string_equal(parsed->type.name, "type");
2123 assert_string_equal(parsed->units, "uni");
Radek Iša56ca9e42020-09-08 18:42:00 +02002124 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
2125 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LYEXT_SUBSTMT_SELF);
2126 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002127 siblings = NULL;
2128
2129 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002130 "<leaf-list name=\"llist\">\n"
2131 " <config value=\"true\" />\n"
2132 " <description><text>desc</text></description>\n"
2133 " <if-feature name=\"feature\"/>\n"
2134 " <min-elements value=\"5\"/>\n"
2135 " <must condition=\"must-cond\"/>\n"
2136 " <ordered-by value=\"user\" />\n"
2137 " <reference><text>ref</text></reference>\n"
2138 " <status value=\"current\"/>\n"
2139 " <type name=\"type\"/>\n"
2140 " <units name=\"uni\"/>\n"
2141 " <when condition=\"when-cond\"/>\n"
2142 EXT_SUBELEM
2143 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002144 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002145 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002146 parsed = (struct lysp_node_leaflist *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002147 flags = LYS_CONFIG_W | LYS_ORDBY_USER | LYS_STATUS_CURR | LYS_SET_MIN;
2148 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2149 "llist", 0, LYS_LEAFLIST, 0, "ref", 1);
2150 CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL);
2151 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002152 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002153 assert_int_equal(parsed->min, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002154 assert_string_equal(parsed->type.name, "type");
2155 assert_string_equal(parsed->units, "uni");
Radek Iša56ca9e42020-09-08 18:42:00 +02002156 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LYEXT_SUBSTMT_SELF);
2157 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002158 siblings = NULL;
2159
2160 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002161 "<leaf-list name=\"llist\">\n"
2162 " <config value=\"true\" />\n"
2163 " <description><text>desc</text></description>\n"
2164 " <if-feature name=\"feature\"/>\n"
2165 " <max-elements value=\"15\"/>\n"
2166 " <min-elements value=\"5\"/>\n"
2167 " <must condition=\"must-cond\"/>\n"
2168 " <ordered-by value=\"user\" />\n"
2169 " <reference><text>ref</text></reference>\n"
2170 " <status value=\"current\"/>\n"
2171 " <type name=\"type\"/>\n"
2172 " <units name=\"uni\"/>\n"
2173 " <when condition=\"when-cond\"/>\n"
2174 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002175 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002176 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002177 parsed = (struct lysp_node_leaflist *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002178 flags = LYS_CONFIG_W | LYS_ORDBY_USER | LYS_STATUS_CURR | LYS_SET_MIN | LYS_SET_MAX;
2179 CHECK_LYSP_NODE(parsed, "desc", 0, flags, 1,
2180 "llist", 0, LYS_LEAFLIST, 0, "ref", 1);
2181 CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL);
2182 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002183 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002184 assert_int_equal(parsed->min, 5);
2185 assert_int_equal(parsed->max, 15);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002186 assert_string_equal(parsed->type.name, "type");
2187 assert_string_equal(parsed->units, "uni");
Radek Iša56ca9e42020-09-08 18:42:00 +02002188 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002189 siblings = NULL;
2190
2191 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002192 "<leaf-list name=\"llist\">\n"
2193 " <type name=\"type\"/>\n"
2194 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002195 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002196 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002197 parsed = (struct lysp_node_leaflist *)siblings;
2198 assert_string_equal(parsed->name, "llist");
2199 assert_string_equal(parsed->type.name, "type");
Radek Iša56ca9e42020-09-08 18:42:00 +02002200 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002201 siblings = NULL;
2202
2203 /* invalid combinations */
2204 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002205 "<leaf-list name=\"llist\">\n"
2206 " <max-elements value=\"5\"/>\n"
2207 " <min-elements value=\"15\"/>\n"
2208 " <type name=\"type\"/>"
2209 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002210 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002211 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_EVALID);
2212 CHECK_LOG_CTX("Invalid combination of min-elements and max-elements: min value 15 is bigger than the max value 5.", "Line number 4.");
2213 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002214 siblings = NULL;
2215
2216 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002217 "<leaf-list name=\"llist\">\n"
2218 " <default value=\"def-val1\"/>\n"
2219 " <min-elements value=\"15\"/>\n"
2220 " <type name=\"type\"/>\n"
2221 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002222 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002223 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_EVALID);
2224 CHECK_LOG_CTX("Invalid combination of sub-elemnts \"min-elements\" and \"default\" in \"leaf-list\" element.", "Line number 5.");
2225 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002226 siblings = NULL;
2227
2228 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002229 "<leaf-list name=\"llist\">"
2230 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002231 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002232 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_EVALID);
2233 CHECK_LOG_CTX("Missing mandatory sub-element \"type\" of \"leaf-list\" element.", "Line number 1.");
2234 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002235 siblings = NULL;
David Sedlákc3da3ef2019-07-19 12:56:08 +02002236}
2237
David Sedlákcb39f642019-07-19 13:19:55 +02002238static void
2239test_presence_elem(void **state)
2240{
David Sedlákcb39f642019-07-19 13:19:55 +02002241 const char *data;
2242 const char *val;
David Sedlákd1144562019-08-06 12:36:14 +02002243 struct lysp_ext_instance *exts = NULL;
2244
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002245 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\">"EXT_SUBELEM "</presence>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002246 assert_int_equal(test_element_helper(state, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02002247 assert_string_equal(val, "presence-val");
Radek Iša56ca9e42020-09-08 18:42:00 +02002248 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_PRESENCE);
2249 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
Michal Vaskoe180ed02021-02-05 16:31:20 +01002250 lydict_remove(UTEST_LYCTX, val);
David Sedlákcb39f642019-07-19 13:19:55 +02002251
2252 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002253 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlákcb39f642019-07-19 13:19:55 +02002254 assert_string_equal(val, "presence-val");
Michal Vaskoe180ed02021-02-05 16:31:20 +01002255 lydict_remove(UTEST_LYCTX, val);
David Sedlákcb39f642019-07-19 13:19:55 +02002256
2257 data = ELEMENT_WRAPPER_START "<presence/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002258 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
2259 CHECK_LOG_CTX("Missing mandatory attribute value of presence element.", "Line number 1.");
David Sedlákcb39f642019-07-19 13:19:55 +02002260}
2261
David Sedlák12470a82019-07-19 13:44:36 +02002262static void
2263test_key_elem(void **state)
2264{
David Sedlák12470a82019-07-19 13:44:36 +02002265 const char *data;
2266 const char *val;
David Sedlákd1144562019-08-06 12:36:14 +02002267 struct lysp_ext_instance *exts = NULL;
2268
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002269 data = ELEMENT_WRAPPER_START "<key value=\"key-value\">"EXT_SUBELEM "</key>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002270 assert_int_equal(test_element_helper(state, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02002271 assert_string_equal(val, "key-value");
Radek Iša56ca9e42020-09-08 18:42:00 +02002272 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_KEY);
2273 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
Michal Vaskoe180ed02021-02-05 16:31:20 +01002274 lydict_remove(UTEST_LYCTX, val);
David Sedlák12470a82019-07-19 13:44:36 +02002275
2276 data = ELEMENT_WRAPPER_START "<key value=\"key-value\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002277 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák12470a82019-07-19 13:44:36 +02002278 assert_string_equal(val, "key-value");
Michal Vaskoe180ed02021-02-05 16:31:20 +01002279 lydict_remove(UTEST_LYCTX, val);
David Sedlák12470a82019-07-19 13:44:36 +02002280
2281 data = ELEMENT_WRAPPER_START "<key/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002282 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
2283 CHECK_LOG_CTX("Missing mandatory attribute value of key element.", "Line number 1.");
David Sedlák12470a82019-07-19 13:44:36 +02002284}
2285
David Sedlák04e17b22019-07-19 15:29:48 +02002286static void
2287test_typedef_elem(void **state)
2288{
David Sedlák04e17b22019-07-19 15:29:48 +02002289 const char *data;
2290 struct lysp_tpdf *tpdfs = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002291 struct tree_node_meta typdef_meta = {NULL, (struct lysp_node **)&tpdfs};
David Sedlák04e17b22019-07-19 15:29:48 +02002292
2293 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002294 "<typedef name=\"tpdf-name\">\n"
2295 " <default value=\"def-val\"/>\n"
2296 " <description><text>desc-text</text></description>\n"
2297 " <reference><text>ref-text</text></reference>\n"
2298 " <status value=\"current\"/>\n"
2299 " <type name=\"type\"/>\n"
2300 " <units name=\"uni\"/>\n"
2301 EXT_SUBELEM
2302 "</typedef>"
2303 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002304 assert_int_equal(test_element_helper(state, data, &typdef_meta, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002305 assert_string_equal(tpdfs[0].dflt.str, "def-val");
David Sedlák04e17b22019-07-19 15:29:48 +02002306 assert_string_equal(tpdfs[0].dsc, "desc-text");
David Sedlák04e17b22019-07-19 15:29:48 +02002307 assert_string_equal(tpdfs[0].name, "tpdf-name");
2308 assert_string_equal(tpdfs[0].ref, "ref-text");
2309 assert_string_equal(tpdfs[0].type.name, "type");
2310 assert_string_equal(tpdfs[0].units, "uni");
2311 assert_true(tpdfs[0].flags & LYS_STATUS_CURR);
Radek Iša56ca9e42020-09-08 18:42:00 +02002312 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(tpdfs[0].exts[0]), LYEXT_SUBSTMT_SELF);
2313 FREE_ARRAY(UTEST_LYCTX, tpdfs, lysp_tpdf_free);
David Sedlák04e17b22019-07-19 15:29:48 +02002314 tpdfs = NULL;
2315
2316 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002317 "<typedef name=\"tpdf-name\">\n"
2318 " <type name=\"type\"/>\n"
2319 "</typedef>"
2320 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002321 assert_int_equal(test_element_helper(state, data, &typdef_meta, NULL, NULL), LY_SUCCESS);
David Sedlák04e17b22019-07-19 15:29:48 +02002322 assert_string_equal(tpdfs[0].name, "tpdf-name");
2323 assert_string_equal(tpdfs[0].type.name, "type");
Radek Iša56ca9e42020-09-08 18:42:00 +02002324 FREE_ARRAY(UTEST_LYCTX, tpdfs, lysp_tpdf_free);
David Sedlák04e17b22019-07-19 15:29:48 +02002325 tpdfs = NULL;
David Sedlák04e17b22019-07-19 15:29:48 +02002326}
2327
David Sedlákd2d676a2019-07-22 11:28:19 +02002328static void
2329test_refine_elem(void **state)
2330{
David Sedlákd2d676a2019-07-22 11:28:19 +02002331 const char *data;
2332 struct lysp_refine *refines = NULL;
2333
2334 /* max subelems */
2335 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002336 "<refine target-node=\"target\">\n"
2337 " <if-feature name=\"feature\" />\n"
2338 " <must condition=\"cond\" />\n"
2339 " <presence value=\"presence\" />\n"
2340 " <default value=\"def\" />\n"
2341 " <config value=\"true\" />\n"
2342 " <mandatory value=\"true\" />\n"
2343 " <min-elements value=\"10\" />\n"
2344 " <max-elements value=\"20\" />\n"
2345 " <description><text>desc</text></description>\n"
2346 " <reference><text>ref</text></reference>\n"
2347 EXT_SUBELEM
2348 "</refine>"
2349 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002350 assert_int_equal(test_element_helper(state, data, &refines, NULL, NULL), LY_SUCCESS);
David Sedlákd2d676a2019-07-22 11:28:19 +02002351 assert_string_equal(refines->nodeid, "target");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002352 assert_string_equal(refines->dflts[0].str, "def");
David Sedlákd2d676a2019-07-22 11:28:19 +02002353 assert_string_equal(refines->dsc, "desc");
David Sedlákd2d676a2019-07-22 11:28:19 +02002354 assert_true(refines->flags & LYS_CONFIG_W);
2355 assert_true(refines->flags & LYS_MAND_TRUE);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002356 assert_string_equal(refines->iffeatures[0].str, "feature");
David Sedlákd2d676a2019-07-22 11:28:19 +02002357 assert_int_equal(refines->max, 20);
2358 assert_int_equal(refines->min, 10);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002359 assert_string_equal(refines->musts->arg.str, "cond");
David Sedlákd2d676a2019-07-22 11:28:19 +02002360 assert_string_equal(refines->presence, "presence");
2361 assert_string_equal(refines->ref, "ref");
Radek Iša56ca9e42020-09-08 18:42:00 +02002362 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(refines->exts[0]), LYEXT_SUBSTMT_SELF);
2363 FREE_ARRAY(UTEST_LYCTX, refines, lysp_refine_free);
David Sedlákd2d676a2019-07-22 11:28:19 +02002364 refines = NULL;
2365
2366 /* min subelems */
2367 data = ELEMENT_WRAPPER_START "<refine target-node=\"target\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002368 assert_int_equal(test_element_helper(state, data, &refines, NULL, NULL), LY_SUCCESS);
David Sedlákd2d676a2019-07-22 11:28:19 +02002369 assert_string_equal(refines->nodeid, "target");
Radek Iša56ca9e42020-09-08 18:42:00 +02002370 FREE_ARRAY(UTEST_LYCTX, refines, lysp_refine_free);
David Sedlákd2d676a2019-07-22 11:28:19 +02002371 refines = NULL;
David Sedlákd2d676a2019-07-22 11:28:19 +02002372}
2373
David Sedlák0d6de5a2019-07-22 13:25:44 +02002374static void
2375test_uses_elem(void **state)
2376{
David Sedlák0d6de5a2019-07-22 13:25:44 +02002377 const char *data;
2378 struct lysp_node *siblings = NULL;
2379 struct tree_node_meta node_meta = {NULL, &siblings};
2380 struct lysp_node_uses *parsed = NULL;
2381
2382 /* max subelems */
2383 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002384 "<uses name=\"uses-name\">\n"
2385 " <when condition=\"cond\" />\n"
2386 " <if-feature name=\"feature\" />\n"
2387 " <status value=\"obsolete\" />\n"
2388 " <description><text>desc</text></description>\n"
2389 " <reference><text>ref</text></reference>\n"
2390 " <refine target-node=\"target\"/>\n"
2391 " <augment target-node=\"target\" />\n"
2392 EXT_SUBELEM
2393 "</uses>"
2394 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002395 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002396 parsed = (struct lysp_node_uses *)&siblings[0];
Radek Iša56ca9e42020-09-08 18:42:00 +02002397 CHECK_LYSP_NODE(parsed, "desc", 1, LYS_STATUS_OBSLT, 1,
2398 "uses-name", 0, LYS_USES, 0, "ref", 1);
2399 CHECK_LYSP_WHEN(parsed->when, "cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002400 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlák0d6de5a2019-07-22 13:25:44 +02002401 assert_string_equal(parsed->refines->nodeid, "target");
David Sedlák992fb7c2019-07-24 16:51:01 +02002402 assert_string_equal(parsed->augments->nodeid, "target");
Radek Iša56ca9e42020-09-08 18:42:00 +02002403 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LYEXT_SUBSTMT_SELF);
2404 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002405 siblings = NULL;
2406
2407 /* min subelems */
2408 data = ELEMENT_WRAPPER_START "<uses name=\"uses-name\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002409 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002410 assert_string_equal(siblings[0].name, "uses-name");
Radek Iša56ca9e42020-09-08 18:42:00 +02002411 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002412 siblings = NULL;
David Sedlák0d6de5a2019-07-22 13:25:44 +02002413}
2414
David Sedlákaa854b02019-07-22 14:17:10 +02002415static void
2416test_revision_elem(void **state)
2417{
David Sedlákaa854b02019-07-22 14:17:10 +02002418 const char *data;
2419 struct lysp_revision *revs = NULL;
2420
2421 /* max subelems */
2422 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002423 "<revision date=\"2018-12-25\">\n"
2424 " <description><text>desc</text></description>\n"
2425 " <reference><text>ref</text></reference>\n"
2426 EXT_SUBELEM
2427 "</revision>"
2428 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002429 assert_int_equal(test_element_helper(state, data, &revs, NULL, NULL), LY_SUCCESS);
David Sedlákaa854b02019-07-22 14:17:10 +02002430 assert_string_equal(revs->date, "2018-12-25");
2431 assert_string_equal(revs->dsc, "desc");
2432 assert_string_equal(revs->ref, "ref");
Radek Iša56ca9e42020-09-08 18:42:00 +02002433 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(revs->exts[0]), LYEXT_SUBSTMT_SELF);
2434 FREE_ARRAY(UTEST_LYCTX, revs, lysp_revision_free);
David Sedlákaa854b02019-07-22 14:17:10 +02002435 revs = NULL;
2436
2437 /* min subelems */
2438 data = ELEMENT_WRAPPER_START "<revision date=\"2005-05-05\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002439 assert_int_equal(test_element_helper(state, data, &revs, NULL, NULL), LY_SUCCESS);
David Sedlákaa854b02019-07-22 14:17:10 +02002440 assert_string_equal(revs->date, "2005-05-05");
Radek Iša56ca9e42020-09-08 18:42:00 +02002441 FREE_ARRAY(UTEST_LYCTX, revs, lysp_revision_free);
David Sedlákaa854b02019-07-22 14:17:10 +02002442 revs = NULL;
2443
2444 /* invalid value */
2445 data = ELEMENT_WRAPPER_START "<revision date=\"05-05-2005\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002446 assert_int_equal(test_element_helper(state, data, &revs, NULL, NULL), LY_EVALID);
2447 CHECK_LOG_CTX("Invalid value \"05-05-2005\" of \"revision\".", "Line number 1.");
2448 FREE_ARRAY(UTEST_LYCTX, revs, lysp_revision_free);
David Sedlákaa854b02019-07-22 14:17:10 +02002449 revs = NULL;
David Sedlákaa854b02019-07-22 14:17:10 +02002450}
2451
David Sedlák0c2bab92019-07-22 15:33:19 +02002452static void
2453test_include_elem(void **state)
2454{
David Sedlák0c2bab92019-07-22 15:33:19 +02002455 const char *data;
2456 struct lysp_include *includes = NULL;
2457 struct include_meta inc_meta = {"module-name", &includes};
2458
2459 /* max subelems */
Radek Iša56ca9e42020-09-08 18:42:00 +02002460 YCTX->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák0c2bab92019-07-22 15:33:19 +02002461 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002462 "<include module=\"mod\">\n"
2463 " <description><text>desc</text></description>\n"
2464 " <reference><text>ref</text></reference>\n"
2465 " <revision-date date=\"1999-09-09\"/>\n"
2466 EXT_SUBELEM
2467 "</include>"
2468 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002469 assert_int_equal(test_element_helper(state, data, &inc_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0c2bab92019-07-22 15:33:19 +02002470 assert_string_equal(includes->name, "mod");
2471 assert_string_equal(includes->dsc, "desc");
2472 assert_string_equal(includes->ref, "ref");
David Sedlák0c2bab92019-07-22 15:33:19 +02002473 assert_string_equal(includes->rev, "1999-09-09");
Radek Iša56ca9e42020-09-08 18:42:00 +02002474 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(includes->exts[0]), LYEXT_SUBSTMT_SELF);
2475 FREE_ARRAY(UTEST_LYCTX, includes, lysp_include_free);
David Sedlák0c2bab92019-07-22 15:33:19 +02002476 includes = NULL;
2477
2478 /* min subelems */
2479 data = ELEMENT_WRAPPER_START "<include module=\"mod\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002480 assert_int_equal(test_element_helper(state, data, &inc_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0c2bab92019-07-22 15:33:19 +02002481 assert_string_equal(includes->name, "mod");
Radek Iša56ca9e42020-09-08 18:42:00 +02002482 FREE_ARRAY(UTEST_LYCTX, includes, lysp_include_free);
David Sedlák0c2bab92019-07-22 15:33:19 +02002483 includes = NULL;
2484
2485 /* invalid combinations */
Radek Iša56ca9e42020-09-08 18:42:00 +02002486 YCTX->parsed_mod->version = LYS_VERSION_1_0;
David Sedlák0c2bab92019-07-22 15:33:19 +02002487 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002488 "<include module=\"mod\">\n"
2489 " <description><text>desc</text></description>\n"
2490 " <revision-date date=\"1999-09-09\"/>\n"
2491 "</include>"
2492 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002493 assert_int_equal(test_element_helper(state, data, &inc_meta, NULL, NULL), LY_EVALID);
2494 CHECK_LOG_CTX("Invalid sub-elemnt \"description\" of \"include\" element - this sub-element is allowed only in modules with version 1.1 or newer.",
2495 "Line number 2.");
2496 FREE_ARRAY(UTEST_LYCTX, includes, lysp_include_free);
David Sedlák0c2bab92019-07-22 15:33:19 +02002497 includes = NULL;
2498
Radek Iša56ca9e42020-09-08 18:42:00 +02002499 YCTX->parsed_mod->version = LYS_VERSION_1_0;
David Sedlák0c2bab92019-07-22 15:33:19 +02002500 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002501 "<include module=\"mod\">\n"
2502 " <reference><text>ref</text></reference>\n"
2503 " <revision-date date=\"1999-09-09\"/>\n"
2504 "</include>"
2505 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002506 assert_int_equal(test_element_helper(state, data, &inc_meta, NULL, NULL), LY_EVALID);
2507 CHECK_LOG_CTX("Invalid sub-elemnt \"reference\" of \"include\" element - this sub-element is allowed only in modules with version 1.1 or newer.",
2508 "Line number 2.");
2509 FREE_ARRAY(UTEST_LYCTX, includes, lysp_include_free);
David Sedlák0c2bab92019-07-22 15:33:19 +02002510 includes = NULL;
David Sedlák0c2bab92019-07-22 15:33:19 +02002511}
2512
David Sedlák5e13dea2019-07-22 16:06:45 +02002513static void
David Sedlákaf536aa2019-07-23 13:42:23 +02002514test_list_elem(void **state)
2515{
David Sedlákaf536aa2019-07-23 13:42:23 +02002516 const char *data;
2517 struct lysp_node *siblings = NULL;
2518 struct tree_node_meta node_meta = {NULL, &siblings};
2519 struct lysp_node_list *parsed = NULL;
2520
2521 /* max subelems */
2522 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002523 "<list name=\"list-name\">\n"
2524 " <when condition=\"when\"/>\n"
2525 " <if-feature name=\"iff\"/>\n"
2526 " <must condition=\"must-cond\"/>\n"
2527 " <key value=\"key\"/>\n"
2528 " <unique tag=\"utag\"/>\n"
2529 " <config value=\"true\"/>\n"
2530 " <min-elements value=\"10\"/>\n"
2531 " <ordered-by value=\"user\"/>\n"
2532 " <status value=\"deprecated\"/>\n"
2533 " <description><text>desc</text></description>\n"
2534 " <reference><text>ref</text></reference>\n"
2535 " <anydata name=\"anyd\"/>\n"
2536 " <anyxml name=\"anyx\"/>\n"
2537 " <container name=\"cont\"/>\n"
2538 " <choice name=\"choice\"/>\n"
2539 " <action name=\"action\"/>\n"
2540 " <grouping name=\"grp\"/>\n"
2541 " <notification name=\"notf\"/>\n"
2542 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2543 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2544 " <list name=\"sub-list\"/>\n"
2545 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2546 " <uses name=\"uses-name\"/>\n"
2547 EXT_SUBELEM
2548 "</list>"
2549 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002550 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákaf536aa2019-07-23 13:42:23 +02002551 parsed = (struct lysp_node_list *)&siblings[0];
David Sedlákaf536aa2019-07-23 13:42:23 +02002552 assert_string_equal(parsed->child->name, "anyd");
2553 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2554 assert_string_equal(parsed->child->next->name, "anyx");
2555 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
David Sedlákf111bcb2019-07-23 17:15:51 +02002556 assert_string_equal(parsed->child->next->next->name, "cont");
2557 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002558 assert_string_equal(parsed->child->next->next->next->name, "choice");
2559 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
David Sedlák85d0eca2019-07-24 15:15:21 +02002560 assert_string_equal(parsed->child->next->next->next->next->name, "leaf");
2561 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAF);
2562 assert_string_equal(parsed->child->next->next->next->next->next->name, "llist");
2563 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2564 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "sub-list");
2565 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LIST);
2566 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "uses-name");
2567 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_USES);
2568 assert_null(parsed->child->next->next->next->next->next->next->next->next);
Radek Iša56ca9e42020-09-08 18:42:00 +02002569 uint16_t flags = LYS_ORDBY_USER | LYS_STATUS_DEPRC | LYS_CONFIG_W | LYS_SET_MIN;
2570
2571 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2572 "list-name", 0, LYS_LIST, 0, "ref", 1);
2573 CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL);
2574 CHECK_LYSP_WHEN(parsed->when, "when", NULL, 0, NULL);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002575 assert_string_equal(parsed->groupings->name, "grp");
David Sedlák85d0eca2019-07-24 15:15:21 +02002576 assert_string_equal(parsed->actions->name, "action");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002577 assert_int_equal(parsed->groupings->nodetype, LYS_GROUPING);
David Sedlák031b9e72019-07-23 15:19:37 +02002578 assert_string_equal(parsed->notifs->name, "notf");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002579 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlákaf536aa2019-07-23 13:42:23 +02002580 assert_string_equal(parsed->key, "key");
2581 assert_int_equal(parsed->min, 10);
David Sedlákaf536aa2019-07-23 13:42:23 +02002582 assert_string_equal(parsed->typedefs->name, "tpdf");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002583 assert_string_equal(parsed->uniques->str, "utag");
Radek Iša56ca9e42020-09-08 18:42:00 +02002584 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LYEXT_SUBSTMT_SELF);
2585 lysp_node_free(UTEST_LYCTX, siblings);
2586 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
David Sedlákaf536aa2019-07-23 13:42:23 +02002587 siblings = NULL;
2588
2589 /* min subelems */
2590 data = ELEMENT_WRAPPER_START "<list name=\"list-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002591 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákaf536aa2019-07-23 13:42:23 +02002592 parsed = (struct lysp_node_list *)&siblings[0];
Radek Iša56ca9e42020-09-08 18:42:00 +02002593 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2594 "list-name", 0, LYS_LIST, 0, NULL, 0);
2595 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlákaf536aa2019-07-23 13:42:23 +02002596 siblings = NULL;
David Sedlákaf536aa2019-07-23 13:42:23 +02002597}
2598
David Sedlák031b9e72019-07-23 15:19:37 +02002599static void
2600test_notification_elem(void **state)
2601{
David Sedlák031b9e72019-07-23 15:19:37 +02002602 const char *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002603 struct lysp_node_notif *notifs = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002604 struct tree_node_meta notif_meta = {NULL, (struct lysp_node **)&notifs};
David Sedlák031b9e72019-07-23 15:19:37 +02002605
2606 /* max subelems */
Radek Iša56ca9e42020-09-08 18:42:00 +02002607 YCTX->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák031b9e72019-07-23 15:19:37 +02002608 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002609 "<notification name=\"notif-name\">\n"
2610 " <anydata name=\"anyd\"/>\n"
2611 " <anyxml name=\"anyx\"/>\n"
2612 " <description><text>desc</text></description>\n"
2613 " <if-feature name=\"iff\"/>\n"
2614 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2615 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2616 " <list name=\"sub-list\"/>\n"
2617 " <must condition=\"cond\"/>\n"
2618 " <reference><text>ref</text></reference>\n"
2619 " <status value=\"deprecated\"/>\n"
2620 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2621 " <uses name=\"uses-name\"/>\n"
2622 " <container name=\"cont\"/>\n"
2623 " <choice name=\"choice\"/>\n"
2624 " <grouping name=\"grp\"/>\n"
2625 EXT_SUBELEM
2626 "</notification>"
2627 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002628 assert_int_equal(test_element_helper(state, data, &notif_meta, NULL, NULL), LY_SUCCESS);
David Sedlák031b9e72019-07-23 15:19:37 +02002629 assert_string_equal(notifs->name, "notif-name");
Radek Krejci01180ac2021-01-27 08:48:22 +01002630 assert_string_equal(notifs->child->name, "anyd");
2631 assert_int_equal(notifs->child->nodetype, LYS_ANYDATA);
2632 assert_string_equal(notifs->child->next->name, "anyx");
2633 assert_int_equal(notifs->child->next->nodetype, LYS_ANYXML);
2634 assert_string_equal(notifs->child->next->next->name, "leaf");
2635 assert_int_equal(notifs->child->next->next->nodetype, LYS_LEAF);
2636 assert_string_equal(notifs->child->next->next->next->name, "llist");
2637 assert_int_equal(notifs->child->next->next->next->nodetype, LYS_LEAFLIST);
2638 assert_string_equal(notifs->child->next->next->next->next->name, "sub-list");
2639 assert_int_equal(notifs->child->next->next->next->next->nodetype, LYS_LIST);
David Sedlák031b9e72019-07-23 15:19:37 +02002640 assert_true(notifs->flags & LYS_STATUS_DEPRC);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002641 assert_string_equal(notifs->groupings->name, "grp");
2642 assert_int_equal(notifs->groupings->nodetype, LYS_GROUPING);
Radek Krejci01180ac2021-01-27 08:48:22 +01002643 assert_string_equal(notifs->child->next->next->next->next->next->name, "uses-name");
2644 assert_int_equal(notifs->child->next->next->next->next->next->nodetype, LYS_USES);
2645 assert_string_equal(notifs->child->next->next->next->next->next->next->name, "cont");
2646 assert_int_equal(notifs->child->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
2647 assert_int_equal(notifs->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2648 assert_string_equal(notifs->child->next->next->next->next->next->next->next->name, "choice");
2649 assert_null(notifs->child->next->next->next->next->next->next->next->next);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002650 assert_string_equal(notifs->iffeatures[0].str, "iff");
2651 assert_string_equal(notifs->musts->arg.str, "cond");
David Sedlák031b9e72019-07-23 15:19:37 +02002652 assert_int_equal(notifs->nodetype, LYS_NOTIF);
2653 assert_null(notifs->parent);
2654 assert_string_equal(notifs->ref, "ref");
2655 assert_string_equal(notifs->typedefs->name, "tpdf");
Radek Iša56ca9e42020-09-08 18:42:00 +02002656 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(notifs->exts[0]), LYEXT_SUBSTMT_SELF);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002657 lysp_node_free(UTEST_LYCTX, (struct lysp_node *)notifs);
David Sedlák031b9e72019-07-23 15:19:37 +02002658 notifs = NULL;
2659
2660 /* min subelems */
2661 data = ELEMENT_WRAPPER_START "<notification name=\"notif-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002662 assert_int_equal(test_element_helper(state, data, &notif_meta, NULL, NULL), LY_SUCCESS);
David Sedlák031b9e72019-07-23 15:19:37 +02002663 assert_string_equal(notifs->name, "notif-name");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002664 lysp_node_free(UTEST_LYCTX, (struct lysp_node *)notifs);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002665 notifs = NULL;
David Sedláke3ce9ef2019-07-23 16:34:30 +02002666}
2667
2668static void
2669test_grouping_elem(void **state)
2670{
David Sedláke3ce9ef2019-07-23 16:34:30 +02002671 const char *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002672 struct lysp_node_grp *grps = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002673 struct tree_node_meta grp_meta = {NULL, (struct lysp_node **)&grps};
David Sedláke3ce9ef2019-07-23 16:34:30 +02002674
2675 /* max subelems */
2676 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002677 "<grouping name=\"grp-name\">\n"
2678 " <anydata name=\"anyd\"/>\n"
2679 " <anyxml name=\"anyx\"/>\n"
2680 " <description><text>desc</text></description>\n"
2681 " <grouping name=\"sub-grp\"/>\n"
2682 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2683 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2684 " <list name=\"list\"/>\n"
2685 " <notification name=\"notf\"/>\n"
2686 " <reference><text>ref</text></reference>\n"
2687 " <status value=\"current\"/>\n"
2688 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2689 " <uses name=\"uses-name\"/>\n"
2690 " <action name=\"act\"/>\n"
2691 " <container name=\"cont\"/>\n"
2692 " <choice name=\"choice\"/>\n"
2693 EXT_SUBELEM
2694 "</grouping>"
2695 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002696 assert_int_equal(test_element_helper(state, data, &grp_meta, NULL, NULL), LY_SUCCESS);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002697 assert_string_equal(grps->name, "grp-name");
Radek Krejci01180ac2021-01-27 08:48:22 +01002698 assert_string_equal(grps->child->name, "anyd");
2699 assert_string_equal(grps->child->next->name, "anyx");
2700 assert_string_equal(grps->child->next->next->name, "leaf");
2701 assert_string_equal(grps->child->next->next->next->name, "llist");
2702 assert_string_equal(grps->child->next->next->next->next->name, "list");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002703 assert_string_equal(grps->dsc, "desc");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002704 assert_true(grps->flags & LYS_STATUS_CURR);
2705 assert_string_equal(grps->groupings->name, "sub-grp");
2706 assert_int_equal(grps->nodetype, LYS_GROUPING);
2707 assert_string_equal(grps->notifs->name, "notf");
2708 assert_null(grps->parent);
2709 assert_string_equal(grps->ref, "ref");
2710 assert_string_equal(grps->typedefs->name, "tpdf");
David Sedlák85d0eca2019-07-24 15:15:21 +02002711 assert_string_equal(grps->actions->name, "act");
Radek Krejci01180ac2021-01-27 08:48:22 +01002712 assert_string_equal(grps->child->next->next->next->next->next->name, "uses-name");
2713 assert_int_equal(grps->child->next->next->next->next->next->nodetype, LYS_USES);
2714 assert_string_equal(grps->child->next->next->next->next->next->next->name, "cont");
2715 assert_int_equal(grps->child->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
2716 assert_string_equal(grps->child->next->next->next->next->next->next->next->name, "choice");
2717 assert_int_equal(grps->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
Radek Iša56ca9e42020-09-08 18:42:00 +02002718 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(grps->exts[0]), LYEXT_SUBSTMT_SELF);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002719 lysp_node_free(UTEST_LYCTX, &grps->node);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002720 grps = NULL;
2721
2722 /* min subelems */
2723 data = ELEMENT_WRAPPER_START "<grouping name=\"grp-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002724 assert_int_equal(test_element_helper(state, data, &grp_meta, NULL, NULL), LY_SUCCESS);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002725 assert_string_equal(grps->name, "grp-name");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002726 lysp_node_free(UTEST_LYCTX, &grps->node);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002727 grps = NULL;
David Sedlák031b9e72019-07-23 15:19:37 +02002728}
2729
David Sedlákf111bcb2019-07-23 17:15:51 +02002730static void
2731test_container_elem(void **state)
2732{
David Sedlákf111bcb2019-07-23 17:15:51 +02002733 const char *data;
2734 struct lysp_node *siblings = NULL;
2735 struct tree_node_meta node_meta = {NULL, &siblings};
2736 struct lysp_node_container *parsed = NULL;
2737
2738 /* max subelems */
Radek Iša56ca9e42020-09-08 18:42:00 +02002739 YCTX->parsed_mod->version = LYS_VERSION_1_1;
David Sedláke2dc9e92019-07-24 09:59:21 +02002740 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002741 "<container name=\"cont-name\">\n"
2742 " <anydata name=\"anyd\"/>\n"
2743 " <anyxml name=\"anyx\"/>\n"
2744 " <config value=\"true\"/>\n"
2745 " <container name=\"subcont\"/>\n"
2746 " <description><text>desc</text></description>\n"
2747 " <grouping name=\"sub-grp\"/>\n"
2748 " <if-feature name=\"iff\"/>\n"
2749 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2750 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2751 " <list name=\"list\"/>\n"
2752 " <must condition=\"cond\"/>\n"
2753 " <notification name=\"notf\"/>\n"
2754 " <presence value=\"presence\"/>\n"
2755 " <reference><text>ref</text></reference>\n"
2756 " <status value=\"current\"/>\n"
2757 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2758 " <uses name=\"uses-name\"/>\n"
2759 " <when condition=\"when-cond\"/>\n"
2760 " <action name=\"act\"/>\n"
2761 " <choice name=\"choice\"/>\n"
2762 EXT_SUBELEM
2763 "</container>"
2764 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002765 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedláke2dc9e92019-07-24 09:59:21 +02002766 parsed = (struct lysp_node_container *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002767 uint16_t flags = LYS_CONFIG_W | LYS_STATUS_CURR;
2768
2769 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2770 "cont-name", 0, LYS_CONTAINER, 0, "ref", 1);
2771 CHECK_LYSP_RESTR(parsed->musts, "cond", NULL, NULL, NULL, 0, NULL);
2772 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
2773
Michal Vasko7f45cf22020-10-01 12:49:44 +02002774 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedláke2dc9e92019-07-24 09:59:21 +02002775 assert_string_equal(parsed->presence, "presence");
2776 assert_string_equal(parsed->typedefs->name, "tpdf");
2777 assert_string_equal(parsed->groupings->name, "sub-grp");
2778 assert_string_equal(parsed->child->name, "anyd");
2779 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2780 assert_string_equal(parsed->child->next->name, "anyx");
2781 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2782 assert_string_equal(parsed->child->next->next->name, "subcont");
2783 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
2784 assert_string_equal(parsed->child->next->next->next->name, "leaf");
2785 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
2786 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
2787 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
2788 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
2789 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
2790 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
2791 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002792 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
2793 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2794 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke2dc9e92019-07-24 09:59:21 +02002795 assert_string_equal(parsed->notifs->name, "notf");
David Sedlák85d0eca2019-07-24 15:15:21 +02002796 assert_string_equal(parsed->actions->name, "act");
Radek Iša56ca9e42020-09-08 18:42:00 +02002797 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LYEXT_SUBSTMT_SELF);
2798 lysp_node_free(UTEST_LYCTX, siblings);
2799 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
David Sedláke2dc9e92019-07-24 09:59:21 +02002800 siblings = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02002801
2802 /* min subelems */
2803 data = ELEMENT_WRAPPER_START "<container name=\"cont-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002804 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákf111bcb2019-07-23 17:15:51 +02002805 parsed = (struct lysp_node_container *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002806 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2807 "cont-name", 0, LYS_CONTAINER, 0, NULL, 0);
2808 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlákf111bcb2019-07-23 17:15:51 +02002809 siblings = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02002810}
2811
David Sedlák5379d392019-07-24 10:42:03 +02002812static void
2813test_case_elem(void **state)
2814{
David Sedlák5379d392019-07-24 10:42:03 +02002815 const char *data;
2816 struct lysp_node *siblings = NULL;
2817 struct tree_node_meta node_meta = {NULL, &siblings};
2818 struct lysp_node_case *parsed = NULL;
2819
2820 /* max subelems */
Radek Iša56ca9e42020-09-08 18:42:00 +02002821 YCTX->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák5379d392019-07-24 10:42:03 +02002822 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002823 "<case name=\"case-name\">\n"
2824 " <anydata name=\"anyd\"/>\n"
2825 " <anyxml name=\"anyx\"/>\n"
2826 " <container name=\"subcont\"/>\n"
2827 " <description><text>desc</text></description>\n"
2828 " <if-feature name=\"iff\"/>\n"
2829 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2830 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2831 " <list name=\"list\"/>\n"
2832 " <reference><text>ref</text></reference>\n"
2833 " <status value=\"current\"/>\n"
2834 " <uses name=\"uses-name\"/>\n"
2835 " <when condition=\"when-cond\"/>\n"
2836 " <choice name=\"choice\"/>\n"
2837 EXT_SUBELEM
2838 "</case>"
2839 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002840 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák5379d392019-07-24 10:42:03 +02002841 parsed = (struct lysp_node_case *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002842 uint16_t flags = LYS_STATUS_CURR;
2843
2844 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2845 "case-name", 0, LYS_CASE, 0, "ref", 1);
2846 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002847 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlák5379d392019-07-24 10:42:03 +02002848 assert_string_equal(parsed->child->name, "anyd");
2849 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2850 assert_string_equal(parsed->child->next->name, "anyx");
2851 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2852 assert_string_equal(parsed->child->next->next->name, "subcont");
2853 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
2854 assert_string_equal(parsed->child->next->next->next->name, "leaf");
2855 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
2856 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
2857 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
2858 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
2859 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
2860 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
2861 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002862 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
2863 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2864 assert_null(parsed->child->next->next->next->next->next->next->next->next);
Radek Iša56ca9e42020-09-08 18:42:00 +02002865 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LYEXT_SUBSTMT_SELF);
2866 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlák5379d392019-07-24 10:42:03 +02002867 siblings = NULL;
2868
2869 /* min subelems */
2870 data = ELEMENT_WRAPPER_START "<case name=\"case-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002871 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák5379d392019-07-24 10:42:03 +02002872 parsed = (struct lysp_node_case *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002873 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2874 "case-name", 0, LYS_CASE, 0, NULL, 0);
2875 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlák5379d392019-07-24 10:42:03 +02002876 siblings = NULL;
David Sedlák5379d392019-07-24 10:42:03 +02002877}
2878
David Sedlákb7abcfa2019-07-24 12:33:35 +02002879static void
2880test_choice_elem(void **state)
2881{
David Sedlákb7abcfa2019-07-24 12:33:35 +02002882 const char *data;
2883 struct lysp_node *siblings = NULL;
2884 struct tree_node_meta node_meta = {NULL, &siblings};
2885 struct lysp_node_choice *parsed = NULL;
2886
2887 /* max subelems */
Radek Iša56ca9e42020-09-08 18:42:00 +02002888 YCTX->parsed_mod->version = LYS_VERSION_1_1;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002889 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002890 "<choice name=\"choice-name\">\n"
2891 " <anydata name=\"anyd\"/>\n"
2892 " <anyxml name=\"anyx\"/>\n"
2893 " <case name=\"sub-case\"/>\n"
2894 " <choice name=\"choice\"/>\n"
2895 " <config value=\"true\"/>\n"
2896 " <container name=\"subcont\"/>\n"
2897 " <default value=\"def\"/>\n"
2898 " <description><text>desc</text></description>\n"
2899 " <if-feature name=\"iff\"/>\n"
2900 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2901 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2902 " <list name=\"list\"/>\n"
2903 " <mandatory value=\"true\" />\n"
2904 " <reference><text>ref</text></reference>\n"
2905 " <status value=\"current\"/>\n"
2906 " <when condition=\"when-cond\"/>\n"
2907 EXT_SUBELEM
2908 "</choice>"
2909 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002910 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002911 parsed = (struct lysp_node_choice *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002912 uint16_t flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_CURR;
2913
2914 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2915 "choice-name", 0, LYS_CHOICE, 0, "ref", 1);
2916 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002917 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlákb7abcfa2019-07-24 12:33:35 +02002918 assert_string_equal(parsed->child->name, "anyd");
2919 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2920 assert_string_equal(parsed->child->next->name, "anyx");
2921 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2922 assert_string_equal(parsed->child->next->next->name, "sub-case");
2923 assert_int_equal(parsed->child->next->next->nodetype, LYS_CASE);
2924 assert_string_equal(parsed->child->next->next->next->name, "choice");
2925 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
2926 assert_string_equal(parsed->child->next->next->next->next->name, "subcont");
2927 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_CONTAINER);
2928 assert_string_equal(parsed->child->next->next->next->next->next->name, "leaf");
2929 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAF);
2930 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "llist");
2931 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2932 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "list");
2933 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
2934 assert_null(parsed->child->next->next->next->next->next->next->next->next);
Radek Iša56ca9e42020-09-08 18:42:00 +02002935 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LYEXT_SUBSTMT_SELF);
2936 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002937 siblings = NULL;
2938
2939 /* min subelems */
2940 data = ELEMENT_WRAPPER_START "<choice name=\"choice-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002941 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002942 parsed = (struct lysp_node_choice *)siblings;
2943 assert_string_equal(parsed->name, "choice-name");
Radek Iša56ca9e42020-09-08 18:42:00 +02002944 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2945 "choice-name", 0, LYS_CHOICE, 0, NULL, 0);
2946 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002947 siblings = NULL;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002948}
2949
David Sedlák05404f62019-07-24 14:11:53 +02002950static void
2951test_inout_elem(void **state)
2952{
David Sedlák05404f62019-07-24 14:11:53 +02002953 const char *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002954 struct lysp_node_action_inout inout = {};
David Sedlák05404f62019-07-24 14:11:53 +02002955 struct inout_meta inout_meta = {NULL, &inout};
2956
2957 /* max subelements */
Radek Iša56ca9e42020-09-08 18:42:00 +02002958 YCTX->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák05404f62019-07-24 14:11:53 +02002959 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002960 "<input>\n"
2961 " <anydata name=\"anyd\"/>\n"
2962 " <anyxml name=\"anyx\"/>\n"
2963 " <choice name=\"choice\"/>\n"
2964 " <container name=\"subcont\"/>\n"
2965 " <grouping name=\"sub-grp\"/>\n"
2966 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2967 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2968 " <list name=\"list\"/>\n"
2969 " <must condition=\"cond\"/>\n"
2970 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2971 " <uses name=\"uses-name\"/>\n"
2972 EXT_SUBELEM
2973 "</input>"
2974 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002975 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
2976 CHECK_LYSP_ACTION_INOUT(&(inout), 1, 1, 1, 1, LYS_INPUT, 0, 1);
2977 CHECK_LYSP_RESTR(inout.musts, "cond", NULL, NULL, NULL, 0, NULL);
David Sedlák05404f62019-07-24 14:11:53 +02002978 assert_string_equal(inout.typedefs->name, "tpdf");
2979 assert_string_equal(inout.groupings->name, "sub-grp");
Radek Krejci01180ac2021-01-27 08:48:22 +01002980 assert_string_equal(inout.child->name, "anyd");
2981 assert_int_equal(inout.child->nodetype, LYS_ANYDATA);
2982 assert_string_equal(inout.child->next->name, "anyx");
2983 assert_int_equal(inout.child->next->nodetype, LYS_ANYXML);
2984 assert_string_equal(inout.child->next->next->name, "choice");
2985 assert_int_equal(inout.child->next->next->nodetype, LYS_CHOICE);
2986 assert_string_equal(inout.child->next->next->next->name, "subcont");
2987 assert_int_equal(inout.child->next->next->next->nodetype, LYS_CONTAINER);
2988 assert_string_equal(inout.child->next->next->next->next->name, "leaf");
2989 assert_int_equal(inout.child->next->next->next->next->nodetype, LYS_LEAF);
2990 assert_string_equal(inout.child->next->next->next->next->next->name, "llist");
2991 assert_int_equal(inout.child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2992 assert_string_equal(inout.child->next->next->next->next->next->next->name, "list");
2993 assert_int_equal(inout.child->next->next->next->next->next->next->nodetype, LYS_LIST);
2994 assert_string_equal(inout.child->next->next->next->next->next->next->next->name, "uses-name");
2995 assert_int_equal(inout.child->next->next->next->next->next->next->next->nodetype, LYS_USES);
2996 assert_null(inout.child->next->next->next->next->next->next->next->next);
Radek Iša56ca9e42020-09-08 18:42:00 +02002997 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(inout.exts[0]), LYEXT_SUBSTMT_SELF);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002998 lysp_node_free(UTEST_LYCTX, (struct lysp_node *)&inout);
David Sedlák05404f62019-07-24 14:11:53 +02002999 memset(&inout, 0, sizeof inout);
3000
3001 /* max subelements */
Radek Iša56ca9e42020-09-08 18:42:00 +02003002 YCTX->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák05404f62019-07-24 14:11:53 +02003003 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003004 "<output>\n"
3005 " <anydata name=\"anyd\"/>\n"
3006 " <anyxml name=\"anyx\"/>\n"
3007 " <choice name=\"choice\"/>\n"
3008 " <container name=\"subcont\"/>\n"
3009 " <grouping name=\"sub-grp\"/>\n"
3010 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3011 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3012 " <list name=\"list\"/>\n"
3013 " <must condition=\"cond\"/>\n"
3014 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3015 " <uses name=\"uses-name\"/>\n"
3016 EXT_SUBELEM
3017 "</output>"
3018 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003019 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
3020 CHECK_LYSP_ACTION_INOUT(&(inout), 1, 1, 1, 1, LYS_OUTPUT, 0, 1);
Michal Vasko7f45cf22020-10-01 12:49:44 +02003021 assert_string_equal(inout.musts->arg.str, "cond");
David Sedlák05404f62019-07-24 14:11:53 +02003022 assert_string_equal(inout.typedefs->name, "tpdf");
3023 assert_string_equal(inout.groupings->name, "sub-grp");
Radek Krejci01180ac2021-01-27 08:48:22 +01003024 assert_string_equal(inout.child->name, "anyd");
3025 assert_int_equal(inout.child->nodetype, LYS_ANYDATA);
3026 assert_string_equal(inout.child->next->name, "anyx");
3027 assert_int_equal(inout.child->next->nodetype, LYS_ANYXML);
3028 assert_string_equal(inout.child->next->next->name, "choice");
3029 assert_int_equal(inout.child->next->next->nodetype, LYS_CHOICE);
3030 assert_string_equal(inout.child->next->next->next->name, "subcont");
3031 assert_int_equal(inout.child->next->next->next->nodetype, LYS_CONTAINER);
3032 assert_string_equal(inout.child->next->next->next->next->name, "leaf");
3033 assert_int_equal(inout.child->next->next->next->next->nodetype, LYS_LEAF);
3034 assert_string_equal(inout.child->next->next->next->next->next->name, "llist");
3035 assert_int_equal(inout.child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3036 assert_string_equal(inout.child->next->next->next->next->next->next->name, "list");
3037 assert_int_equal(inout.child->next->next->next->next->next->next->nodetype, LYS_LIST);
3038 assert_string_equal(inout.child->next->next->next->next->next->next->next->name, "uses-name");
3039 assert_int_equal(inout.child->next->next->next->next->next->next->next->nodetype, LYS_USES);
3040 assert_null(inout.child->next->next->next->next->next->next->next->next);
Radek Iša56ca9e42020-09-08 18:42:00 +02003041 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(inout.exts[0]), LYEXT_SUBSTMT_SELF);
Radek Krejci2a9fc652021-01-22 17:44:34 +01003042 lysp_node_free(UTEST_LYCTX, (struct lysp_node *)&inout);
David Sedlák05404f62019-07-24 14:11:53 +02003043 memset(&inout, 0, sizeof inout);
3044
3045 /* min subelems */
Michal Vaskob83af8a2020-01-06 09:49:22 +01003046 data = ELEMENT_WRAPPER_START "<input><leaf name=\"l\"><type name=\"empty\"/></leaf></input>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003047 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
Radek Krejci2a9fc652021-01-22 17:44:34 +01003048 lysp_node_free(UTEST_LYCTX, (struct lysp_node *)&inout);
David Sedlák05404f62019-07-24 14:11:53 +02003049 memset(&inout, 0, sizeof inout);
3050
Michal Vaskob83af8a2020-01-06 09:49:22 +01003051 data = ELEMENT_WRAPPER_START "<output><leaf name=\"l\"><type name=\"empty\"/></leaf></output>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003052 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
Radek Krejci2a9fc652021-01-22 17:44:34 +01003053 lysp_node_free(UTEST_LYCTX, (struct lysp_node *)&inout);
David Sedlák05404f62019-07-24 14:11:53 +02003054 memset(&inout, 0, sizeof inout);
3055
3056 /* invalid combinations */
3057 data = ELEMENT_WRAPPER_START "<input name=\"test\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003058 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_EVALID);
Radek Krejci9a3823e2021-01-27 20:26:46 +01003059 lysp_node_free(UTEST_LYCTX, (struct lysp_node *)&inout);
Radek Iša56ca9e42020-09-08 18:42:00 +02003060 CHECK_LOG_CTX("Unexpected attribute \"name\" of \"input\" element.", "Line number 1.");
David Sedlák05404f62019-07-24 14:11:53 +02003061 memset(&inout, 0, sizeof inout);
David Sedlák05404f62019-07-24 14:11:53 +02003062}
3063
David Sedlák85d0eca2019-07-24 15:15:21 +02003064static void
3065test_action_elem(void **state)
3066{
David Sedlák85d0eca2019-07-24 15:15:21 +02003067 const char *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003068 struct lysp_node_action *actions = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003069 struct tree_node_meta act_meta = {NULL, (struct lysp_node **)&actions};
Radek Iša56ca9e42020-09-08 18:42:00 +02003070 uint16_t flags;
David Sedlák85d0eca2019-07-24 15:15:21 +02003071
3072 /* max subelems */
Radek Iša56ca9e42020-09-08 18:42:00 +02003073 YCTX->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák85d0eca2019-07-24 15:15:21 +02003074 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003075 "<action name=\"act\">\n"
3076 " <description><text>desc</text></description>\n"
3077 " <grouping name=\"grouping\"/>\n"
3078 " <if-feature name=\"iff\"/>\n"
3079 " <input><uses name=\"uses-name\"/></input>\n"
3080 " <output><must condition=\"cond\"/><leaf name=\"l\"><type name=\"type\"/></leaf></output>\n"
3081 " <reference><text>ref</text></reference>\n"
3082 " <status value=\"deprecated\"/>\n"
3083 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3084 EXT_SUBELEM
3085 "</action>"
3086 ELEMENT_WRAPPER_END;
Michal Vasko1bf09392020-03-27 12:38:10 +01003087 /* there must be parent for action */
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003088 act_meta.parent = (void *)1;
Radek Iša56ca9e42020-09-08 18:42:00 +02003089 assert_int_equal(test_element_helper(state, data, &act_meta, NULL, NULL), LY_SUCCESS);
Michal Vasko1bf09392020-03-27 12:38:10 +01003090 act_meta.parent = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02003091 flags = LYS_STATUS_DEPRC;
3092 CHECK_LYSP_ACTION(actions, "desc", 1, flags, 1, 1,\
3093 1, 0, 0, 0,\
3094 1, 0,\
3095 "act", LYS_ACTION, \
3096 1, 0, 0, 1,\
3097 1, 0,\
3098 1, "ref", 1);
3099
Michal Vasko7f45cf22020-10-01 12:49:44 +02003100 assert_string_equal(actions->iffeatures[0].str, "iff");
David Sedlák85d0eca2019-07-24 15:15:21 +02003101 assert_string_equal(actions->typedefs->name, "tpdf");
3102 assert_string_equal(actions->groupings->name, "grouping");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003103 assert_string_equal(actions->output.musts->arg.str, "cond");
Radek Krejci01180ac2021-01-27 08:48:22 +01003104 assert_string_equal(actions->input.child->name, "uses-name");
Radek Iša56ca9e42020-09-08 18:42:00 +02003105 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(actions->exts[0]), LYEXT_SUBSTMT_SELF);
Radek Krejci2a9fc652021-01-22 17:44:34 +01003106 lysp_node_free(UTEST_LYCTX, (struct lysp_node *)actions);
David Sedlák85d0eca2019-07-24 15:15:21 +02003107 actions = NULL;
3108
Radek Iša56ca9e42020-09-08 18:42:00 +02003109 YCTX->parsed_mod->version = LYS_VERSION_1_1;
David Sedlákeaa45792019-07-24 15:25:01 +02003110 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003111 "<rpc name=\"act\">\n"
3112 " <description><text>desc</text></description>\n"
3113 " <grouping name=\"grouping\"/>\n"
3114 " <if-feature name=\"iff\"/>\n"
3115 " <input><uses name=\"uses-name\"/></input>\n"
3116 " <output><must condition=\"cond\"/><leaf name=\"l\"><type name=\"type\"/></leaf></output>\n"
3117 " <reference><text>ref</text></reference>\n"
3118 " <status value=\"deprecated\"/>\n"
3119 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3120 EXT_SUBELEM
3121 "</rpc>"
3122 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003123 assert_int_equal(test_element_helper(state, data, &act_meta, NULL, NULL), LY_SUCCESS);
3124 flags = LYS_STATUS_DEPRC;
3125 CHECK_LYSP_ACTION(actions, "desc", 1, flags, 1, 1,\
3126 1, 0, 0, 0,\
3127 1, 0,\
3128 "act", LYS_RPC, \
3129 1, 0, 0, 1,\
3130 1, 0,\
3131 0, "ref", 1);
3132
Michal Vasko7f45cf22020-10-01 12:49:44 +02003133 assert_string_equal(actions->iffeatures[0].str, "iff");
David Sedlákeaa45792019-07-24 15:25:01 +02003134 assert_string_equal(actions->typedefs->name, "tpdf");
3135 assert_string_equal(actions->groupings->name, "grouping");
Radek Krejci01180ac2021-01-27 08:48:22 +01003136 assert_string_equal(actions->input.child->name, "uses-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003137 assert_string_equal(actions->output.musts->arg.str, "cond");
Radek Iša56ca9e42020-09-08 18:42:00 +02003138 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(actions->exts[0]), LYEXT_SUBSTMT_SELF);
Radek Krejci2a9fc652021-01-22 17:44:34 +01003139 lysp_node_free(UTEST_LYCTX, (struct lysp_node *)actions);
David Sedlákeaa45792019-07-24 15:25:01 +02003140 actions = NULL;
3141
David Sedlák85d0eca2019-07-24 15:15:21 +02003142 /* min subelems */
3143 data = ELEMENT_WRAPPER_START "<action name=\"act\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003144 assert_int_equal(test_element_helper(state, data, &act_meta, NULL, NULL), LY_SUCCESS);
David Sedlák85d0eca2019-07-24 15:15:21 +02003145 assert_string_equal(actions->name, "act");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003146 lysp_node_free(UTEST_LYCTX, (struct lysp_node *)actions);
David Sedlák85d0eca2019-07-24 15:15:21 +02003147 actions = NULL;
David Sedlák85d0eca2019-07-24 15:15:21 +02003148}
3149
David Sedlák992fb7c2019-07-24 16:51:01 +02003150static void
3151test_augment_elem(void **state)
3152{
David Sedlák992fb7c2019-07-24 16:51:01 +02003153 const char *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003154 struct lysp_node_augment *augments = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003155 struct tree_node_meta aug_meta = {NULL, (struct lysp_node **)&augments};
David Sedlák992fb7c2019-07-24 16:51:01 +02003156
Radek Iša56ca9e42020-09-08 18:42:00 +02003157 YCTX->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák992fb7c2019-07-24 16:51:01 +02003158 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003159 "<augment target-node=\"target\">\n"
3160 " <action name=\"action\"/>\n"
3161 " <anydata name=\"anyd\"/>\n"
3162 " <anyxml name=\"anyx\"/>\n"
3163 " <case name=\"case\"/>\n"
3164 " <choice name=\"choice\"/>\n"
3165 " <container name=\"subcont\"/>\n"
3166 " <description><text>desc</text></description>\n"
3167 " <if-feature name=\"iff\"/>\n"
3168 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3169 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3170 " <list name=\"list\"/>\n"
3171 " <notification name=\"notif\"/>\n"
3172 " <reference><text>ref</text></reference>\n"
3173 " <status value=\"current\"/>\n"
3174 " <uses name=\"uses\"/>\n"
3175 " <when condition=\"when-cond\"/>\n"
3176 EXT_SUBELEM
3177 "</augment>"
3178 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003179 assert_int_equal(test_element_helper(state, data, &aug_meta, NULL, NULL), LY_SUCCESS);
David Sedlák992fb7c2019-07-24 16:51:01 +02003180 assert_string_equal(augments->nodeid, "target");
3181 assert_null(augments->parent);
3182 assert_int_equal(augments->nodetype, LYS_AUGMENT);
3183 assert_true(augments->flags & LYS_STATUS_CURR);
3184 assert_string_equal(augments->dsc, "desc");
3185 assert_string_equal(augments->ref, "ref");
3186 assert_string_equal(augments->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003187 assert_string_equal(augments->iffeatures[0].str, "iff");
David Sedlák992fb7c2019-07-24 16:51:01 +02003188 assert_string_equal(augments->child->name, "anyd");
3189 assert_int_equal(augments->child->nodetype, LYS_ANYDATA);
3190 assert_string_equal(augments->child->next->name, "anyx");
3191 assert_int_equal(augments->child->next->nodetype, LYS_ANYXML);
3192 assert_string_equal(augments->child->next->next->name, "case");
3193 assert_int_equal(augments->child->next->next->nodetype, LYS_CASE);
3194 assert_string_equal(augments->child->next->next->next->name, "choice");
3195 assert_int_equal(augments->child->next->next->next->nodetype, LYS_CHOICE);
3196 assert_string_equal(augments->child->next->next->next->next->name, "subcont");
3197 assert_int_equal(augments->child->next->next->next->next->nodetype, LYS_CONTAINER);
3198 assert_string_equal(augments->child->next->next->next->next->next->name, "leaf");
3199 assert_int_equal(augments->child->next->next->next->next->next->nodetype, LYS_LEAF);
3200 assert_string_equal(augments->child->next->next->next->next->next->next->name, "llist");
3201 assert_int_equal(augments->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3202 assert_string_equal(augments->child->next->next->next->next->next->next->next->name, "list");
3203 assert_int_equal(augments->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
3204 assert_string_equal(augments->child->next->next->next->next->next->next->next->next->name, "uses");
3205 assert_int_equal(augments->child->next->next->next->next->next->next->next->next->nodetype, LYS_USES);
3206 assert_null(augments->child->next->next->next->next->next->next->next->next->next);
3207 assert_string_equal(augments->actions->name, "action");
3208 assert_string_equal(augments->notifs->name, "notif");
Radek Iša56ca9e42020-09-08 18:42:00 +02003209 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(augments->exts[0]), LYEXT_SUBSTMT_SELF);
Radek Krejci2a9fc652021-01-22 17:44:34 +01003210 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)augments);
David Sedlák992fb7c2019-07-24 16:51:01 +02003211 augments = NULL;
3212
3213 data = ELEMENT_WRAPPER_START "<augment target-node=\"target\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003214 assert_int_equal(test_element_helper(state, data, &aug_meta, NULL, NULL), LY_SUCCESS);
David Sedlák992fb7c2019-07-24 16:51:01 +02003215 assert_string_equal(augments->nodeid, "target");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003216 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)augments);
David Sedlák992fb7c2019-07-24 16:51:01 +02003217 augments = NULL;
David Sedlák992fb7c2019-07-24 16:51:01 +02003218}
3219
David Sedlák4ffcec82019-07-25 15:10:21 +02003220static void
3221test_deviate_elem(void **state)
3222{
David Sedlák4ffcec82019-07-25 15:10:21 +02003223 const char *data;
3224 struct lysp_deviate *deviates = NULL;
3225 struct lysp_deviate_add *d_add;
3226 struct lysp_deviate_rpl *d_rpl;
3227 struct lysp_deviate_del *d_del;
3228
3229 /* all valid arguments with min subelems */
3230 data = ELEMENT_WRAPPER_START "<deviate value=\"not-supported\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003231 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003232 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
Radek Iša56ca9e42020-09-08 18:42:00 +02003233 lysp_deviate_free(UTEST_LYCTX, deviates);
David Sedlák4ffcec82019-07-25 15:10:21 +02003234 free(deviates);
3235 deviates = NULL;
3236
3237 data = ELEMENT_WRAPPER_START "<deviate value=\"add\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003238 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003239 assert_int_equal(deviates->mod, LYS_DEV_ADD);
Radek Iša56ca9e42020-09-08 18:42:00 +02003240 lysp_deviate_free(UTEST_LYCTX, deviates);
David Sedlák4ffcec82019-07-25 15:10:21 +02003241 free(deviates);
3242 deviates = NULL;
3243
3244 data = ELEMENT_WRAPPER_START "<deviate value=\"replace\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003245 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003246 assert_int_equal(deviates->mod, LYS_DEV_REPLACE);
Radek Iša56ca9e42020-09-08 18:42:00 +02003247 lysp_deviate_free(UTEST_LYCTX, deviates);
David Sedlák4ffcec82019-07-25 15:10:21 +02003248 free(deviates);
3249 deviates = NULL;
3250
3251 data = ELEMENT_WRAPPER_START "<deviate value=\"delete\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003252 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003253 assert_int_equal(deviates->mod, LYS_DEV_DELETE);
Radek Iša56ca9e42020-09-08 18:42:00 +02003254 lysp_deviate_free(UTEST_LYCTX, deviates);
David Sedlák4ffcec82019-07-25 15:10:21 +02003255 free(deviates);
3256 deviates = NULL;
3257
3258 /* max subelems and valid arguments */
3259 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003260 "<deviate value=\"not-supported\">"
3261 EXT_SUBELEM
3262 "</deviate>"
3263 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003264 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003265 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
Radek Iša56ca9e42020-09-08 18:42:00 +02003266 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(deviates->exts[0]), LYEXT_SUBSTMT_SELF);
3267 lysp_deviate_free(UTEST_LYCTX, deviates);
David Sedlák4ffcec82019-07-25 15:10:21 +02003268 free(deviates);
3269 deviates = NULL;
3270
3271 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003272 "<deviate value=\"add\">\n"
3273 " <units name=\"units\"/>\n"
3274 " <must condition=\"cond\"/>\n"
3275 " <unique tag=\"utag\"/>\n"
3276 " <default value=\"def\"/>\n"
3277 " <config value=\"true\"/>\n"
3278 " <mandatory value=\"true\"/>\n"
3279 " <min-elements value=\"5\"/>\n"
3280 " <max-elements value=\"15\"/>\n"
3281 EXT_SUBELEM
3282 "</deviate>"
3283 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003284 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003285 d_add = (struct lysp_deviate_add *)deviates;
3286 assert_int_equal(d_add->mod, LYS_DEV_ADD);
3287 assert_null(d_add->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003288 assert_string_equal(d_add->units, "units");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003289 assert_string_equal(d_add->musts->arg.str, "cond");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003290 assert_string_equal(d_add->uniques[0].str, "utag");
3291 assert_string_equal(d_add->dflts[0].str, "def");
Radek Iša56ca9e42020-09-08 18:42:00 +02003292 assert_true((d_add->flags & LYS_MAND_TRUE) && (d_add->flags & LYS_CONFIG_W));
David Sedlák4ffcec82019-07-25 15:10:21 +02003293 assert_int_equal(d_add->min, 5);
3294 assert_int_equal(d_add->max, 15);
Radek Iša56ca9e42020-09-08 18:42:00 +02003295 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(deviates->exts[0]), LYEXT_SUBSTMT_SELF);
3296 lysp_deviate_free(UTEST_LYCTX, deviates);
David Sedlák4ffcec82019-07-25 15:10:21 +02003297 free(deviates);
3298 deviates = NULL;
3299
3300 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003301 "<deviate value=\"replace\">\n"
3302 " <type name=\"newtype\"/>\n"
3303 " <units name=\"uni\"/>\n"
3304 " <default value=\"def\"/>\n"
3305 " <config value=\"true\"/>\n"
3306 " <mandatory value=\"true\"/>\n"
3307 " <min-elements value=\"5\"/>\n"
3308 " <max-elements value=\"15\"/>\n"
3309 EXT_SUBELEM
3310 "</deviate>"
3311 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003312 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003313 d_rpl = (struct lysp_deviate_rpl *)deviates;
3314 assert_int_equal(d_rpl->mod, LYS_DEV_REPLACE);
3315 assert_null(d_rpl->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003316 assert_string_equal(d_rpl->type->name, "newtype");
3317 assert_string_equal(d_rpl->units, "uni");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003318 assert_string_equal(d_rpl->dflt.str, "def");
Radek Iša56ca9e42020-09-08 18:42:00 +02003319 assert_true((d_rpl->flags & LYS_MAND_TRUE) && (d_rpl->flags & LYS_CONFIG_W));
David Sedlák4ffcec82019-07-25 15:10:21 +02003320 assert_int_equal(d_rpl->min, 5);
3321 assert_int_equal(d_rpl->max, 15);
Radek Iša56ca9e42020-09-08 18:42:00 +02003322 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(deviates->exts[0]), LYEXT_SUBSTMT_SELF);
3323 lysp_deviate_free(UTEST_LYCTX, deviates);
David Sedlák4ffcec82019-07-25 15:10:21 +02003324 free(deviates);
3325 deviates = NULL;
3326
3327 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003328 "<deviate value=\"delete\">\n"
3329 " <units name=\"u\"/>\n"
3330 " <must condition=\"c\"/>\n"
3331 " <unique tag=\"tag\"/>\n"
3332 " <default value=\"default\"/>\n"
3333 EXT_SUBELEM
3334 "</deviate>"
3335 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003336 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003337 d_del = (struct lysp_deviate_del *)deviates;
3338 assert_int_equal(d_del->mod, LYS_DEV_DELETE);
3339 assert_null(d_del->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003340 assert_string_equal(d_del->units, "u");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003341 assert_string_equal(d_del->musts->arg.str, "c");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003342 assert_string_equal(d_del->uniques[0].str, "tag");
3343 assert_string_equal(d_del->dflts[0].str, "default");
Radek Iša56ca9e42020-09-08 18:42:00 +02003344 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(deviates->exts[0]), LYEXT_SUBSTMT_SELF);
3345 lysp_deviate_free(UTEST_LYCTX, deviates);
David Sedlák4ffcec82019-07-25 15:10:21 +02003346 free(deviates);
3347 deviates = NULL;
3348
3349 /* invalid arguments */
3350 data = ELEMENT_WRAPPER_START "<deviate value=\"\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003351 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
3352 CHECK_LOG_CTX("Invalid value \"\" of \"value\" attribute in \"deviate\" element. "
3353 "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", "Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003354 deviates = NULL;
3355
3356 data = ELEMENT_WRAPPER_START "<deviate value=\"invalid\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003357 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
3358 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"deviate\" element. "
3359 "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", "Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003360 deviates = NULL;
3361
3362 data = ELEMENT_WRAPPER_START "<deviate value=\"ad\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003363 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
3364 CHECK_LOG_CTX("Invalid value \"ad\" of \"value\" attribute in \"deviate\" element. "
3365 "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", "Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003366 deviates = NULL;
3367
3368 data = ELEMENT_WRAPPER_START "<deviate value=\"adds\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003369 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
3370 CHECK_LOG_CTX("Invalid value \"adds\" of \"value\" attribute in \"deviate\" element. "
3371 "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", "Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003372 deviates = NULL;
3373
3374 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003375 "<deviate value=\"not-supported\">\n"
3376 " <must condition=\"c\"/>\n"
3377 "</deviate>"
3378 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003379 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
3380 CHECK_LOG_CTX("Deviate of this type doesn't allow \"must\" as it's sub-element.", "Line number 2.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003381}
3382
David Sedlák8b754462019-07-25 16:22:13 +02003383static void
3384test_deviation_elem(void **state)
3385{
David Sedlák8b754462019-07-25 16:22:13 +02003386 const char *data;
3387 struct lysp_deviation *deviations = NULL;
3388
3389 /* min subelems */
3390 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003391 "<deviation target-node=\"target\">\n"
3392 " <deviate value=\"not-supported\"/>\n"
3393 "</deviation>"
3394 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003395 assert_int_equal(test_element_helper(state, data, &deviations, NULL, NULL), LY_SUCCESS);
David Sedlák8b754462019-07-25 16:22:13 +02003396 assert_string_equal(deviations->nodeid, "target");
3397 assert_int_equal(deviations->deviates->mod, LYS_DEV_NOT_SUPPORTED);
Radek Iša56ca9e42020-09-08 18:42:00 +02003398 FREE_ARRAY(UTEST_LYCTX, deviations, lysp_deviation_free);
David Sedlák8b754462019-07-25 16:22:13 +02003399 deviations = NULL;
3400
3401 /* max subelems */
3402 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003403 "<deviation target-node=\"target\">\n"
3404 " <reference><text>ref</text></reference>\n"
3405 " <description><text>desc</text></description>\n"
3406 " <deviate value=\"add\"/>\n"
3407 EXT_SUBELEM
3408 "</deviation>"
3409 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003410 assert_int_equal(test_element_helper(state, data, &deviations, NULL, NULL), LY_SUCCESS);
David Sedlák8b754462019-07-25 16:22:13 +02003411 assert_string_equal(deviations->nodeid, "target");
3412 assert_int_equal(deviations->deviates->mod, LYS_DEV_ADD);
3413 assert_string_equal(deviations->ref, "ref");
3414 assert_string_equal(deviations->dsc, "desc");
Radek Iša56ca9e42020-09-08 18:42:00 +02003415 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(deviations->exts[0]), LYEXT_SUBSTMT_SELF);
3416 FREE_ARRAY(UTEST_LYCTX, deviations, lysp_deviation_free);
David Sedlák8b754462019-07-25 16:22:13 +02003417 deviations = NULL;
3418
3419 /* invalid */
3420 data = ELEMENT_WRAPPER_START "<deviation target-node=\"target\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003421 assert_int_equal(test_element_helper(state, data, &deviations, NULL, NULL), LY_EVALID);
3422 FREE_ARRAY(UTEST_LYCTX, deviations, lysp_deviation_free);
David Sedlák8b754462019-07-25 16:22:13 +02003423 deviations = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02003424 CHECK_LOG_CTX("Missing mandatory sub-element \"deviate\" of \"deviation\" element.", "Line number 1.");
3425 /* TODO */}
David Sedlák8b754462019-07-25 16:22:13 +02003426
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003427static struct lysp_module *
3428mod_renew(struct lys_yin_parser_ctx *ctx)
3429{
3430 struct ly_ctx *ly_ctx = ctx->parsed_mod->mod->ctx;
3431
3432 lys_module_free(ctx->parsed_mod->mod, NULL);
3433 ctx->parsed_mod = calloc(1, sizeof *ctx->parsed_mod);
3434 ctx->parsed_mod->mod = calloc(1, sizeof *ctx->parsed_mod->mod);
3435 ctx->parsed_mod->mod->parsed = ctx->parsed_mod;
3436 ctx->parsed_mod->mod->ctx = ly_ctx;
3437
3438 return ctx->parsed_mod;
3439}
3440
David Sedlák4f03b932019-07-26 13:01:47 +02003441static void
3442test_module_elem(void **state)
3443{
Michal Vaskob36053d2020-03-26 15:49:30 +01003444 const char *data;
Radek Iša56ca9e42020-09-08 18:42:00 +02003445 struct lysp_module *lysp_mod = mod_renew(YCTX);
David Sedlák4f03b932019-07-26 13:01:47 +02003446
3447 /* max subelems */
David Sedlák4f03b932019-07-26 13:01:47 +02003448 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003449 " <yang-version value=\"1.1\"/>\n"
3450 " <namespace uri=\"ns\"/>\n"
3451 " <prefix value=\"pref\"/>\n"
3452 " <include module=\"b-mod\"/>\n"
3453 " <import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
3454 " <organization><text>org</text></organization>\n"
3455 " <contact><text>contact</text></contact>\n"
3456 " <description><text>desc</text></description>\n"
3457 " <reference><text>ref</text></reference>\n"
3458 " <revision date=\"2019-02-02\"/>\n"
3459 " <anydata name=\"anyd\"/>\n"
3460 " <anyxml name=\"anyx\"/>\n"
3461 " <choice name=\"choice\"/>\n"
3462 " <container name=\"cont\"/>\n"
3463 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3464 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3465 " <list name=\"sub-list\"/>\n"
3466 " <uses name=\"uses-name\"/>\n"
3467 " <augment target-node=\"target\"/>\n"
3468 " <deviation target-node=\"target\">\n"
3469 " <deviate value=\"not-supported\"/>\n"
3470 " </deviation>\n"
3471 " <extension name=\"ext\"/>\n"
3472 " <feature name=\"feature\"/>\n"
3473 " <grouping name=\"grp\"/>\n"
3474 " <identity name=\"ident-name\"/>\n"
3475 " <notification name=\"notf\"/>\n"
3476 " <rpc name=\"rpc-name\"/>\n"
3477 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3478 EXT_SUBELEM "\n"
3479 "</module>\n";
Radek Iša56ca9e42020-09-08 18:42:00 +02003480 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3481 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01003482
Radek Iša56ca9e42020-09-08 18:42:00 +02003483 assert_int_equal(yin_parse_mod(YCTX, lysp_mod), LY_SUCCESS);
David Sedlák4f03b932019-07-26 13:01:47 +02003484 assert_string_equal(lysp_mod->mod->name, "mod");
3485 assert_string_equal(lysp_mod->revs, "2019-02-02");
3486 assert_string_equal(lysp_mod->mod->ns, "ns");
3487 assert_string_equal(lysp_mod->mod->prefix, "pref");
3488 assert_null(lysp_mod->mod->filepath);
3489 assert_string_equal(lysp_mod->mod->org, "org");
3490 assert_string_equal(lysp_mod->mod->contact, "contact");
3491 assert_string_equal(lysp_mod->mod->dsc, "desc");
3492 assert_string_equal(lysp_mod->mod->ref, "ref");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003493 assert_int_equal(lysp_mod->version, LYS_VERSION_1_1);
Radek Iša56ca9e42020-09-08 18:42:00 +02003494 CHECK_LYSP_IMPORT(lysp_mod->imports, NULL, 0, "a-mod",
3495 "imp-pref", NULL, "");
David Sedlák4f03b932019-07-26 13:01:47 +02003496 assert_string_equal(lysp_mod->includes->name, "b-mod");
3497 assert_string_equal(lysp_mod->extensions->name, "ext");
3498 assert_string_equal(lysp_mod->features->name, "feature");
3499 assert_string_equal(lysp_mod->identities->name, "ident-name");
3500 assert_string_equal(lysp_mod->typedefs->name, "tpdf");
3501 assert_string_equal(lysp_mod->groupings->name, "grp");
3502 assert_string_equal(lysp_mod->data->name, "anyd");
3503 assert_int_equal(lysp_mod->data->nodetype, LYS_ANYDATA);
3504 assert_string_equal(lysp_mod->data->next->name, "anyx");
3505 assert_int_equal(lysp_mod->data->next->nodetype, LYS_ANYXML);
3506 assert_string_equal(lysp_mod->data->next->next->name, "choice");
3507 assert_int_equal(lysp_mod->data->next->next->nodetype, LYS_CHOICE);
3508 assert_string_equal(lysp_mod->data->next->next->next->name, "cont");
3509 assert_int_equal(lysp_mod->data->next->next->next->nodetype, LYS_CONTAINER);
3510 assert_string_equal(lysp_mod->data->next->next->next->next->name, "leaf");
3511 assert_int_equal(lysp_mod->data->next->next->next->next->nodetype, LYS_LEAF);
3512 assert_string_equal(lysp_mod->data->next->next->next->next->next->name, "llist");
3513 assert_int_equal(lysp_mod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3514 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->name, "sub-list");
3515 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
3516 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->next->name, "uses-name");
3517 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3518 assert_null(lysp_mod->data->next->next->next->next->next->next->next->next);
3519 assert_string_equal(lysp_mod->augments->nodeid, "target");
3520 assert_string_equal(lysp_mod->rpcs->name, "rpc-name");
3521 assert_string_equal(lysp_mod->notifs->name, "notf");
3522 assert_string_equal(lysp_mod->deviations->nodeid, "target");
Radek Iša56ca9e42020-09-08 18:42:00 +02003523 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(lysp_mod->exts[0]), LYEXT_SUBSTMT_SELF);
David Sedlák4f03b932019-07-26 13:01:47 +02003524
3525 /* min subelems */
Radek Iša56ca9e42020-09-08 18:42:00 +02003526 ly_in_free(UTEST_IN, 0);
3527 lyxml_ctx_free(YCTX->xmlctx);
3528 lysp_mod = mod_renew(YCTX);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003529 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
3530 " <namespace uri=\"ns\"/>\n"
3531 " <prefix value=\"pref\"/>\n"
3532 " <yang-version value=\"1.1\"/>\n"
3533 "</module>";
Radek Iša56ca9e42020-09-08 18:42:00 +02003534 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3535 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
3536 assert_int_equal(yin_parse_mod(YCTX, lysp_mod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003537 assert_string_equal(lysp_mod->mod->name, "mod");
David Sedlák298ff6d2019-07-26 14:29:03 +02003538
David Sedláke6cd89e2019-08-07 12:46:02 +02003539 /* incorrect subelem order */
Radek Iša56ca9e42020-09-08 18:42:00 +02003540 ly_in_free(UTEST_IN, 0);
3541 lyxml_ctx_free(YCTX->xmlctx);
3542 lysp_mod = mod_renew(YCTX);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003543 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
3544 " <feature name=\"feature\"/>\n"
3545 " <namespace uri=\"ns\"/>\n"
3546 " <prefix value=\"pref\"/>\n"
3547 " <yang-version value=\"1.1\"/>\n"
3548 "</module>";
Radek Iša56ca9e42020-09-08 18:42:00 +02003549 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3550 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
3551 assert_int_equal(yin_parse_mod(YCTX, lysp_mod), LY_EVALID);
3552 CHECK_LOG_CTX("Invalid order of module\'s sub-elements \"namespace\" can\'t appear after \"feature\".", "Line number 3.");
David Sedlák298ff6d2019-07-26 14:29:03 +02003553}
3554
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003555static struct lysp_submodule *
3556submod_renew(struct lys_yin_parser_ctx *ctx, const char *belongs_to)
3557{
3558 struct ly_ctx *ly_ctx = ctx->parsed_mod->mod->ctx;
3559
3560 lys_module_free(ctx->parsed_mod->mod, NULL);
3561 ctx->parsed_mod = calloc(1, sizeof(struct lysp_submodule));
3562 ctx->parsed_mod->mod = calloc(1, sizeof *ctx->parsed_mod->mod);
3563 lydict_insert(ly_ctx, belongs_to, 0, &ctx->parsed_mod->mod->name);
3564 ctx->parsed_mod->mod->parsed = ctx->parsed_mod;
3565 ctx->parsed_mod->mod->ctx = ly_ctx;
3566
3567 return (struct lysp_submodule *)ctx->parsed_mod;
3568}
3569
David Sedlák298ff6d2019-07-26 14:29:03 +02003570static void
3571test_submodule_elem(void **state)
3572{
Michal Vaskob36053d2020-03-26 15:49:30 +01003573 const char *data;
Radek Iša56ca9e42020-09-08 18:42:00 +02003574 struct lysp_submodule *lysp_submod = submod_renew(YCTX, "module-name");
David Sedlák298ff6d2019-07-26 14:29:03 +02003575
3576 /* max subelements */
David Sedlák298ff6d2019-07-26 14:29:03 +02003577 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003578 " <yang-version value=\"1.1\"/>\n"
3579 " <belongs-to module=\"module-name\">\n"
3580 " <prefix value=\"pref\"/>\n"
3581 " </belongs-to>\n"
3582 " <include module=\"b-mod\"/>\n"
3583 " <import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
3584 " <organization><text>org</text></organization>\n"
3585 " <contact><text>contact</text></contact>\n"
3586 " <description><text>desc</text></description>\n"
3587 " <reference><text>ref</text></reference>\n"
3588 " <revision date=\"2019-02-02\"/>\n"
3589 " <anydata name=\"anyd\"/>\n"
3590 " <anyxml name=\"anyx\"/>\n"
3591 " <choice name=\"choice\"/>\n"
3592 " <container name=\"cont\"/>\n"
3593 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3594 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3595 " <list name=\"sub-list\"/>\n"
3596 " <uses name=\"uses-name\"/>\n"
3597 " <augment target-node=\"target\"/>\n"
3598 " <deviation target-node=\"target\">\n"
3599 " <deviate value=\"not-supported\"/>\n"
3600 " </deviation>\n"
3601 " <extension name=\"ext\"/>\n"
3602 " <feature name=\"feature\"/>\n"
3603 " <grouping name=\"grp\"/>\n"
3604 " <identity name=\"ident-name\"/>\n"
3605 " <notification name=\"notf\"/>\n"
3606 " <rpc name=\"rpc-name\"/>\n"
3607 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3608 EXT_SUBELEM "\n"
3609 "</submodule>\n";
Radek Iša56ca9e42020-09-08 18:42:00 +02003610 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3611 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003612
Radek Iša56ca9e42020-09-08 18:42:00 +02003613 assert_int_equal(yin_parse_submod(YCTX, lysp_submod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003614 assert_string_equal(lysp_submod->name, "mod");
3615 assert_string_equal(lysp_submod->revs, "2019-02-02");
David Sedlák298ff6d2019-07-26 14:29:03 +02003616 assert_string_equal(lysp_submod->prefix, "pref");
3617 assert_null(lysp_submod->filepath);
3618 assert_string_equal(lysp_submod->org, "org");
3619 assert_string_equal(lysp_submod->contact, "contact");
3620 assert_string_equal(lysp_submod->dsc, "desc");
3621 assert_string_equal(lysp_submod->ref, "ref");
3622 assert_int_equal(lysp_submod->version, LYS_VERSION_1_1);
Radek Iša56ca9e42020-09-08 18:42:00 +02003623 CHECK_LYSP_IMPORT(lysp_submod->imports, NULL, 0, "a-mod",
3624 "imp-pref", NULL, "");
David Sedlák298ff6d2019-07-26 14:29:03 +02003625 assert_string_equal(lysp_submod->includes->name, "b-mod");
3626 assert_string_equal(lysp_submod->extensions->name, "ext");
3627 assert_string_equal(lysp_submod->features->name, "feature");
3628 assert_string_equal(lysp_submod->identities->name, "ident-name");
3629 assert_string_equal(lysp_submod->typedefs->name, "tpdf");
3630 assert_string_equal(lysp_submod->groupings->name, "grp");
3631 assert_string_equal(lysp_submod->data->name, "anyd");
3632 assert_int_equal(lysp_submod->data->nodetype, LYS_ANYDATA);
3633 assert_string_equal(lysp_submod->data->next->name, "anyx");
3634 assert_int_equal(lysp_submod->data->next->nodetype, LYS_ANYXML);
3635 assert_string_equal(lysp_submod->data->next->next->name, "choice");
3636 assert_int_equal(lysp_submod->data->next->next->nodetype, LYS_CHOICE);
3637 assert_string_equal(lysp_submod->data->next->next->next->name, "cont");
3638 assert_int_equal(lysp_submod->data->next->next->next->nodetype, LYS_CONTAINER);
3639 assert_string_equal(lysp_submod->data->next->next->next->next->name, "leaf");
3640 assert_int_equal(lysp_submod->data->next->next->next->next->nodetype, LYS_LEAF);
3641 assert_string_equal(lysp_submod->data->next->next->next->next->next->name, "llist");
3642 assert_int_equal(lysp_submod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3643 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->name, "sub-list");
3644 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
3645 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->next->name, "uses-name");
3646 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3647 assert_null(lysp_submod->data->next->next->next->next->next->next->next->next);
3648 assert_string_equal(lysp_submod->augments->nodeid, "target");
3649 assert_string_equal(lysp_submod->rpcs->name, "rpc-name");
3650 assert_string_equal(lysp_submod->notifs->name, "notf");
3651 assert_string_equal(lysp_submod->deviations->nodeid, "target");
Radek Iša56ca9e42020-09-08 18:42:00 +02003652 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(lysp_submod->exts[0]), LYEXT_SUBSTMT_SELF);
David Sedlák298ff6d2019-07-26 14:29:03 +02003653
David Sedlák298ff6d2019-07-26 14:29:03 +02003654 /* min subelemnts */
Radek Iša56ca9e42020-09-08 18:42:00 +02003655 ly_in_free(UTEST_IN, 0);
3656 lyxml_ctx_free(YCTX->xmlctx);
3657 lysp_submod = submod_renew(YCTX, "module-name");
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003658 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">\n"
3659 " <yang-version value=\"1\"/>\n"
3660 " <belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>\n"
3661 "</submodule>";
Radek Iša56ca9e42020-09-08 18:42:00 +02003662 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3663 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
3664 assert_int_equal(yin_parse_submod(YCTX, lysp_submod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003665 assert_string_equal(lysp_submod->prefix, "pref");
David Sedlák298ff6d2019-07-26 14:29:03 +02003666 assert_int_equal(lysp_submod->version, LYS_VERSION_1_0);
David Sedlák298ff6d2019-07-26 14:29:03 +02003667
David Sedláke6cd89e2019-08-07 12:46:02 +02003668 /* incorrect subelem order */
Radek Iša56ca9e42020-09-08 18:42:00 +02003669 ly_in_free(UTEST_IN, 0);
3670 lyxml_ctx_free(YCTX->xmlctx);
3671 lysp_submod = submod_renew(YCTX, "module-name");
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003672 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">\n"
3673 " <yang-version value=\"1\"/>\n"
3674 " <reference><text>ref</text></reference>\n"
3675 " <belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>\n"
3676 "</submodule>";
Radek Iša56ca9e42020-09-08 18:42:00 +02003677 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3678 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
3679 assert_int_equal(yin_parse_submod(YCTX, lysp_submod), LY_EVALID);
3680 CHECK_LOG_CTX("Invalid order of submodule's sub-elements \"belongs-to\" can't appear after \"reference\".", "Line number 4.");
David Sedlák4f03b932019-07-26 13:01:47 +02003681}
3682
David Sedlák8985a142019-07-31 16:43:06 +02003683static void
3684test_yin_parse_module(void **state)
3685{
David Sedlák8985a142019-07-31 16:43:06 +02003686 const char *data;
3687 struct lys_module *mod;
Michal Vaskob36053d2020-03-26 15:49:30 +01003688 struct lys_yin_parser_ctx *yin_ctx = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02003689 struct ly_in *in = NULL;
Michal Vasko405cc9e2020-12-01 12:01:27 +01003690 struct lys_glob_unres unres = {0};
David Sedlák8985a142019-07-31 16:43:06 +02003691
3692 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003693 mod->ctx = UTEST_LYCTX;
David Sedlákd2844882019-09-13 16:01:22 +02003694 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\" name=\"a\"> \n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003695 " <yang-version value=\"1.1\"/>\n"
3696 " <namespace uri=\"urn:tests:extensions:metadata:a\"/>\n"
3697 " <prefix value=\"a\"/>\n"
3698 " <import module=\"ietf-yang-metadata\">\n"
3699 " <prefix value=\"md\"/>\n"
3700 " </import>\n"
3701 " <feature name=\"f\"/>\n"
3702 " <md:annotation name=\"x\">\n"
3703 " <description>\n"
3704 " <text>test</text>\n"
3705 " </description>\n"
3706 " <reference>\n"
3707 " <text>test</text>\n"
3708 " </reference>\n"
3709 " <if-feature name=\"f\"/>\n"
3710 " <status value=\"current\"/>\n"
3711 " <type name=\"uint8\"/>\n"
3712 " <units name=\"meters\"/>\n"
3713 " </md:annotation>\n"
3714 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003715 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko405cc9e2020-12-01 12:01:27 +01003716 assert_int_equal(yin_parse_module(&yin_ctx, in, mod, &unres), LY_SUCCESS);
David Sedlákd2844882019-09-13 16:01:22 +02003717 assert_null(mod->parsed->exts->child->next->child);
3718 assert_string_equal(mod->parsed->exts->child->next->arg, "test");
Radek Iša56ca9e42020-09-08 18:42:00 +02003719 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
David Sedlákd2844882019-09-13 16:01:22 +02003720 lys_module_free(mod, NULL);
3721 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003722 ly_in_free(in, 0);
David Sedlákd2844882019-09-13 16:01:22 +02003723 mod = NULL;
3724 yin_ctx = NULL;
3725
3726 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003727 mod->ctx = UTEST_LYCTX;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003728 data = "<module name=\"example-foo\""
3729 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
3730 " xmlns:foo=\"urn:example:foo\""
3731 " xmlns:myext=\"urn:example:extensions\">\n"
David Sedlák8985a142019-07-31 16:43:06 +02003732
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003733 " <yang-version value=\"1\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003734
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003735 " <namespace uri=\"urn:example:foo\"/>\n"
3736 " <prefix value=\"foo\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003737
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003738 " <import module=\"example-extensions\">\n"
3739 " <prefix value=\"myext\"/>\n"
3740 " </import>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003741
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003742 " <list name=\"interface\">\n"
3743 " <key value=\"name\"/>\n"
3744 " <leaf name=\"name\">\n"
3745 " <type name=\"string\"/>\n"
3746 " </leaf>\n"
3747 " <leaf name=\"mtu\">\n"
3748 " <type name=\"uint32\"/>\n"
3749 " <description>\n"
3750 " <text>The MTU of the interface.</text>\n"
3751 " </description>\n"
3752 " <myext:c-define name=\"MY_MTU\"/>\n"
3753 " </leaf>\n"
3754 " </list>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003755 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003756 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko405cc9e2020-12-01 12:01:27 +01003757 assert_int_equal(yin_parse_module(&yin_ctx, in, mod, &unres), LY_SUCCESS);
Radek Iša56ca9e42020-09-08 18:42:00 +02003758 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
David Sedlák8985a142019-07-31 16:43:06 +02003759 lys_module_free(mod, NULL);
3760 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003761 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02003762 mod = NULL;
3763 yin_ctx = NULL;
3764
3765 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003766 mod->ctx = UTEST_LYCTX;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003767 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3768 " <yang-version value=\"1\"/>\n"
3769 " <namespace uri=\"urn:example:foo\"/>\n"
3770 " <prefix value=\"foo\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003771 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003772 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko405cc9e2020-12-01 12:01:27 +01003773 assert_int_equal(yin_parse_module(&yin_ctx, in, mod, &unres), LY_SUCCESS);
Radek Iša56ca9e42020-09-08 18:42:00 +02003774 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
David Sedlák6d781b62019-08-02 15:22:52 +02003775 lys_module_free(mod, NULL);
3776 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003777 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02003778 mod = NULL;
3779 yin_ctx = NULL;
3780
David Sedlák6d781b62019-08-02 15:22:52 +02003781 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003782 mod->ctx = UTEST_LYCTX;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003783 data = "<submodule name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlák8985a142019-07-31 16:43:06 +02003784 "</submodule>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003785 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko405cc9e2020-12-01 12:01:27 +01003786 assert_int_equal(yin_parse_module(&yin_ctx, in, mod, &unres), LY_EINVAL);
Radek Iša56ca9e42020-09-08 18:42:00 +02003787 CHECK_LOG_CTX("Input data contains submodule which cannot be parsed directly without its main module.", NULL);
David Sedlák8985a142019-07-31 16:43:06 +02003788 lys_module_free(mod, NULL);
3789 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003790 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02003791
David Sedlák6d781b62019-08-02 15:22:52 +02003792 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003793 mod->ctx = UTEST_LYCTX;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003794 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3795 " <yang-version value=\"1\"/>\n"
3796 " <namespace uri=\"urn:example:foo\"/>\n"
3797 " <prefix value=\"foo\"/>\n"
3798 "</module>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003799 "<module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003800 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko405cc9e2020-12-01 12:01:27 +01003801 assert_int_equal(yin_parse_module(&yin_ctx, in, mod, &unres), LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +02003802 CHECK_LOG_CTX("Trailing garbage \"<module>\" after module, expected end-of-input.", "Line number 6.");
David Sedlák6d781b62019-08-02 15:22:52 +02003803 lys_module_free(mod, NULL);
3804 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003805 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02003806 mod = NULL;
3807 yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003808}
3809
3810static void
3811test_yin_parse_submodule(void **state)
3812{
David Sedlák8985a142019-07-31 16:43:06 +02003813 const char *data;
Michal Vaskob36053d2020-03-26 15:49:30 +01003814 struct lys_yin_parser_ctx *yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003815 struct lysp_submodule *submod = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02003816 struct ly_in *in;
David Sedlák8985a142019-07-31 16:43:06 +02003817
Radek Iša56ca9e42020-09-08 18:42:00 +02003818 lydict_insert(UTEST_LYCTX, "a", 0, &YCTX->parsed_mod->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02003819
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003820 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003821 "<submodule name=\"asub\""
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003822 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
3823 " xmlns:a=\"urn:a\">\n"
3824 " <yang-version value=\"1\"/>\n"
3825 " <belongs-to module=\"a\">\n"
3826 " <prefix value=\"a_pref\"/>\n"
3827 " </belongs-to>\n"
3828 " <include module=\"atop\"/>\n"
3829 " <feature name=\"fox\"/>\n"
3830 " <notification name=\"bar-notif\">\n"
3831 " <if-feature name=\"bar\"/>\n"
3832 " </notification>\n"
3833 " <notification name=\"fox-notif\">\n"
3834 " <if-feature name=\"fox\"/>\n"
3835 " </notification>\n"
3836 " <augment target-node=\"/a_pref:top\">\n"
3837 " <if-feature name=\"bar\"/>\n"
3838 " <container name=\"bar-sub\"/>\n"
3839 " </augment>\n"
3840 " <augment target-node=\"/top\">\n"
3841 " <container name=\"bar-sub2\"/>\n"
3842 " </augment>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003843 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003844 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Radek Iša56ca9e42020-09-08 18:42:00 +02003845 assert_int_equal(yin_parse_submodule(&yin_ctx, UTEST_LYCTX, (struct lys_parser_ctx *)YCTX, in, &submod), LY_SUCCESS);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003846 lysp_module_free((struct lysp_module *)submod);
David Sedlák8985a142019-07-31 16:43:06 +02003847 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003848 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02003849 yin_ctx = NULL;
3850 submod = NULL;
3851
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003852 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3853 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3854 " <yang-version value=\"1\"/>\n"
3855 " <belongs-to module=\"a\">\n"
3856 " <prefix value=\"a_pref\"/>\n"
3857 " </belongs-to>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003858 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003859 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Radek Iša56ca9e42020-09-08 18:42:00 +02003860 assert_int_equal(yin_parse_submodule(&yin_ctx, UTEST_LYCTX, (struct lys_parser_ctx *)YCTX, in, &submod), LY_SUCCESS);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003861 lysp_module_free((struct lysp_module *)submod);
David Sedlák6d781b62019-08-02 15:22:52 +02003862 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003863 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02003864 yin_ctx = NULL;
3865 submod = NULL;
3866
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003867 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003868 "<module name=\"inval\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
3869 "</module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003870 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Radek Iša56ca9e42020-09-08 18:42:00 +02003871 assert_int_equal(yin_parse_submodule(&yin_ctx, UTEST_LYCTX, (struct lys_parser_ctx *)YCTX, in, &submod), LY_EINVAL);
3872 CHECK_LOG_CTX("Input data contains module in situation when a submodule is expected.", NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003873 lysp_module_free((struct lysp_module *)submod);
David Sedlák8985a142019-07-31 16:43:06 +02003874 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003875 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02003876 yin_ctx = NULL;
3877 submod = NULL;
3878
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003879 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3880 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3881 " <yang-version value=\"1\"/>\n"
3882 " <belongs-to module=\"a\">\n"
3883 " <prefix value=\"a_pref\"/>\n"
3884 " </belongs-to>\n"
3885 "</submodule>\n"
3886 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3887 " <yang-version value=\"1\"/>\n"
3888 " <belongs-to module=\"a\">\n"
3889 " <prefix value=\"a_pref\"/>\n"
3890 " </belongs-to>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003891 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003892 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Radek Iša56ca9e42020-09-08 18:42:00 +02003893 assert_int_equal(yin_parse_submodule(&yin_ctx, UTEST_LYCTX, (struct lys_parser_ctx *)YCTX, in, &submod), LY_EVALID);
3894 CHECK_LOG_CTX("Trailing garbage \"<submodule name...\" after submodule, expected end-of-input.", "Line number 8.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003895 lysp_module_free((struct lysp_module *)submod);
David Sedlák6d781b62019-08-02 15:22:52 +02003896 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003897 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02003898 yin_ctx = NULL;
3899 submod = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003900}
3901
David Sedlák3b4db242018-10-19 16:11:01 +02003902int
3903main(void)
3904{
3905
3906 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +02003907 UTEST(test_yin_match_keyword, setup, teardown),
3908 UTEST(test_yin_parse_element_generic, setup, teardown),
3909 UTEST(test_yin_parse_extension_instance, setup, teardown),
3910 UTEST(test_yin_parse_content, setup, teardown),
3911 UTEST(test_validate_value, setup, teardown),
David Sedlák32488102019-07-15 17:44:10 +02003912
Radek Iša56ca9e42020-09-08 18:42:00 +02003913 UTEST(test_yin_match_argument_name),
3914 cmocka_unit_test_setup_teardown(test_enum_elem, setup, teardown),
3915 cmocka_unit_test_setup_teardown(test_bit_elem, setup, teardown),
3916 cmocka_unit_test_setup_teardown(test_meta_elem, setup, teardown),
3917 cmocka_unit_test_setup_teardown(test_import_elem, setup, teardown),
3918 cmocka_unit_test_setup_teardown(test_status_elem, setup, teardown),
3919 cmocka_unit_test_setup_teardown(test_ext_elem, setup, teardown),
3920 cmocka_unit_test_setup_teardown(test_yin_element_elem, setup, teardown),
3921 cmocka_unit_test_setup_teardown(test_yangversion_elem, setup, teardown),
3922 cmocka_unit_test_setup_teardown(test_mandatory_elem, setup, teardown),
3923 cmocka_unit_test_setup_teardown(test_argument_elem, setup, teardown),
3924 cmocka_unit_test_setup_teardown(test_base_elem, setup, teardown),
3925 cmocka_unit_test_setup_teardown(test_belongsto_elem, setup, teardown),
3926 cmocka_unit_test_setup_teardown(test_config_elem, setup, teardown),
3927 cmocka_unit_test_setup_teardown(test_default_elem, setup, teardown),
3928 cmocka_unit_test_setup_teardown(test_err_app_tag_elem, setup, teardown),
3929 cmocka_unit_test_setup_teardown(test_err_msg_elem, setup, teardown),
3930 cmocka_unit_test_setup_teardown(test_fracdigits_elem, setup, teardown),
3931 cmocka_unit_test_setup_teardown(test_iffeature_elem, setup, teardown),
3932 cmocka_unit_test_setup_teardown(test_length_elem, setup, teardown),
3933 cmocka_unit_test_setup_teardown(test_modifier_elem, setup, teardown),
3934 cmocka_unit_test_setup_teardown(test_namespace_elem, setup, teardown),
3935 cmocka_unit_test_setup_teardown(test_pattern_elem, setup, teardown),
3936 cmocka_unit_test_setup_teardown(test_value_position_elem, setup, teardown),
3937 cmocka_unit_test_setup_teardown(test_prefix_elem, setup, teardown),
3938 cmocka_unit_test_setup_teardown(test_range_elem, setup, teardown),
3939 cmocka_unit_test_setup_teardown(test_reqinstance_elem, setup, teardown),
3940 cmocka_unit_test_setup_teardown(test_revision_date_elem, setup, teardown),
3941 cmocka_unit_test_setup_teardown(test_unique_elem, setup, teardown),
3942 cmocka_unit_test_setup_teardown(test_units_elem, setup, teardown),
3943 cmocka_unit_test_setup_teardown(test_when_elem, setup, teardown),
3944 cmocka_unit_test_setup_teardown(test_yin_text_value_elem, setup, teardown),
3945 cmocka_unit_test_setup_teardown(test_type_elem, setup, teardown),
3946 cmocka_unit_test_setup_teardown(test_max_elems_elem, setup, teardown),
3947 cmocka_unit_test_setup_teardown(test_min_elems_elem, setup, teardown),
3948 cmocka_unit_test_setup_teardown(test_ordby_elem, setup, teardown),
3949 cmocka_unit_test_setup_teardown(test_any_elem, setup, teardown),
3950 cmocka_unit_test_setup_teardown(test_leaf_elem, setup, teardown),
3951 cmocka_unit_test_setup_teardown(test_leaf_list_elem, setup, teardown),
3952 cmocka_unit_test_setup_teardown(test_presence_elem, setup, teardown),
3953 cmocka_unit_test_setup_teardown(test_key_elem, setup, teardown),
3954 cmocka_unit_test_setup_teardown(test_typedef_elem, setup, teardown),
3955 cmocka_unit_test_setup_teardown(test_refine_elem, setup, teardown),
3956 cmocka_unit_test_setup_teardown(test_uses_elem, setup, teardown),
3957 cmocka_unit_test_setup_teardown(test_revision_elem, setup, teardown),
3958 cmocka_unit_test_setup_teardown(test_include_elem, setup, teardown),
3959 cmocka_unit_test_setup_teardown(test_list_elem, setup, teardown),
3960 cmocka_unit_test_setup_teardown(test_notification_elem, setup, teardown),
3961 cmocka_unit_test_setup_teardown(test_grouping_elem, setup, teardown),
3962 cmocka_unit_test_setup_teardown(test_container_elem, setup, teardown),
3963 cmocka_unit_test_setup_teardown(test_case_elem, setup, teardown),
3964 cmocka_unit_test_setup_teardown(test_choice_elem, setup, teardown),
3965 cmocka_unit_test_setup_teardown(test_inout_elem, setup, teardown),
3966 cmocka_unit_test_setup_teardown(test_action_elem, setup, teardown),
3967 cmocka_unit_test_setup_teardown(test_augment_elem, setup, teardown),
3968 cmocka_unit_test_setup_teardown(test_deviate_elem, setup, teardown),
3969 cmocka_unit_test_setup_teardown(test_deviation_elem, setup, teardown),
3970 cmocka_unit_test_setup_teardown(test_module_elem, setup, teardown),
3971 cmocka_unit_test_setup_teardown(test_submodule_elem, setup, teardown),
David Sedlák8985a142019-07-31 16:43:06 +02003972
Radek Iša56ca9e42020-09-08 18:42:00 +02003973 cmocka_unit_test_setup_teardown(test_yin_parse_module, setup, teardown),
3974 cmocka_unit_test_setup_teardown(test_yin_parse_submodule, setup, teardown),
David Sedlák3b4db242018-10-19 16:11:01 +02003975 };
3976
Radek Iša56ca9e42020-09-08 18:42:00 +02003977 return cmocka_run_group_tests(tests, NULL, NULL);
David Sedlák3b4db242018-10-19 16:11:01 +02003978}