blob: 5adcf3ad2d67e346e5b0b6127fcc25736135da08 [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. */
73 struct lysp_action_inout *inout_p; /**< inout_p Input/output pointer to write to. */
74};
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);
David Sedlák031b9e72019-07-23 15:19:37 +0200113void lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif);
David Sedláke3ce9ef2019-07-23 16:34:30 +0200114void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp);
David Sedlák05404f62019-07-24 14:11:53 +0200115void lysp_action_inout_free(struct ly_ctx *ctx, struct lysp_action_inout *inout);
David Sedlák85d0eca2019-07-24 15:15:21 +0200116void lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action);
David Sedlák992fb7c2019-07-24 16:51:01 +0200117void lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment);
David Sedlák4ffcec82019-07-25 15:10:21 +0200118void lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d);
David Sedlák8b754462019-07-25 16:22:13 +0200119void lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev);
David Sedlák298ff6d2019-07-26 14:29:03 +0200120void lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import);
David Sedlák555c7202019-07-04 12:14:12 +0200121
David Sedláke6cd89e2019-08-07 12:46:02 +0200122/* wrapping element used for mocking has nothing to do with real module structure */
123#define ELEMENT_WRAPPER_START "<status xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
124#define ELEMENT_WRAPPER_END "</status>"
125
Radek Iša56ca9e42020-09-08 18:42:00 +0200126#define TEST_1_CHECK_LYSP_EXT_INSTANCE(NODE, INSUBSTMT)\
127 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 +0200128
Radek Iša56ca9e42020-09-08 18:42:00 +0200129struct lys_yin_parser_ctx *YCTX;
David Sedlák8e7bda82019-07-16 17:57:50 +0200130
131static int
Radek Iša56ca9e42020-09-08 18:42:00 +0200132setup_ctx(void **state)
David Sedlák8e7bda82019-07-16 17:57:50 +0200133{
David Sedlák619db942019-07-03 14:47:30 +0200134 /* allocate parser context */
Radek Iša56ca9e42020-09-08 18:42:00 +0200135 YCTX = calloc(1, sizeof(*YCTX));
136 YCTX->format = LYS_IN_YIN;
David Sedlák8f5bce02019-06-03 16:41:08 +0200137
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200138 /* allocate new parsed module */
Radek Iša56ca9e42020-09-08 18:42:00 +0200139 YCTX->parsed_mod = calloc(1, sizeof *YCTX->parsed_mod);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200140
141 /* allocate new module */
Radek Iša56ca9e42020-09-08 18:42:00 +0200142 YCTX->parsed_mod->mod = calloc(1, sizeof *YCTX->parsed_mod->mod);
143 YCTX->parsed_mod->mod->ctx = UTEST_LYCTX;
144 YCTX->parsed_mod->mod->parsed = YCTX->parsed_mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200145
Radek Iša56ca9e42020-09-08 18:42:00 +0200146 return 0;
David Sedlák3b4db242018-10-19 16:11:01 +0200147}
148
149static int
Radek Iša56ca9e42020-09-08 18:42:00 +0200150setup(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +0100151{
Radek Iša56ca9e42020-09-08 18:42:00 +0200152 UTEST_SETUP;
David Sedlák68a1af12019-03-08 13:46:54 +0100153
Radek Iša56ca9e42020-09-08 18:42:00 +0200154 setup_ctx(state);
David Sedlák79e50cb2019-06-05 16:33:09 +0200155
Radek Iša56ca9e42020-09-08 18:42:00 +0200156 return 0;
David Sedlák79e50cb2019-06-05 16:33:09 +0200157}
158
David Sedlák8985a142019-07-31 16:43:06 +0200159static int
Radek Iša56ca9e42020-09-08 18:42:00 +0200160teardown_ctx(void **UNUSED(state))
David Sedlák8985a142019-07-31 16:43:06 +0200161{
Radek Iša56ca9e42020-09-08 18:42:00 +0200162 lyxml_ctx_free(YCTX->xmlctx);
163 lys_module_free(YCTX->parsed_mod->mod, NULL);
164 free(YCTX);
165 YCTX = NULL;
David Sedlák8985a142019-07-31 16:43:06 +0200166
Radek Iša56ca9e42020-09-08 18:42:00 +0200167 return 0;
David Sedlák8985a142019-07-31 16:43:06 +0200168}
169
Radek Iša56ca9e42020-09-08 18:42:00 +0200170static int
171teardown(void **state)
172{
173 teardown_ctx(state);
174
175 UTEST_TEARDOWN;
176
177 return 0;
178}
179
180#define RESET_STATE \
181 ly_in_free(UTEST_IN, 0); \
182 UTEST_IN = NULL; \
183 teardown_ctx(state); \
184 setup_ctx(state)
185
David Sedlák68a1af12019-03-08 13:46:54 +0100186static void
David Sedlák1bccdfa2019-06-17 15:55:27 +0200187test_yin_match_keyword(void **state)
David Sedlák3b4db242018-10-19 16:11:01 +0200188{
Michal Vaskob36053d2020-03-26 15:49:30 +0100189 const char *prefix;
190 size_t prefix_len;
Radek Iša56ca9e42020-09-08 18:42:00 +0200191
David Sedlák8f7a1172019-06-20 14:42:18 +0200192 /* create mock yin namespace in xml context */
Radek Iša56ca9e42020-09-08 18:42:00 +0200193 ly_in_new_memory("<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" />", &UTEST_IN);
194 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
195 prefix = YCTX->xmlctx->prefix;
196 prefix_len = YCTX->xmlctx->prefix_len;
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100197
Radek Iša56ca9e42020-09-08 18:42:00 +0200198 assert_int_equal(yin_match_keyword(YCTX, "anydatax", strlen("anydatax"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
199 assert_int_equal(yin_match_keyword(YCTX, "asdasd", strlen("asdasd"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
200 assert_int_equal(yin_match_keyword(YCTX, "", 0, prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
201 assert_int_equal(yin_match_keyword(YCTX, "anydata", strlen("anydata"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ANYDATA);
202 assert_int_equal(yin_match_keyword(YCTX, "anyxml", strlen("anyxml"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ANYXML);
203 assert_int_equal(yin_match_keyword(YCTX, "argument", strlen("argument"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ARGUMENT);
204 assert_int_equal(yin_match_keyword(YCTX, "augment", strlen("augment"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_AUGMENT);
205 assert_int_equal(yin_match_keyword(YCTX, "base", strlen("base"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BASE);
206 assert_int_equal(yin_match_keyword(YCTX, "belongs-to", strlen("belongs-to"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BELONGS_TO);
207 assert_int_equal(yin_match_keyword(YCTX, "bit", strlen("bit"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BIT);
208 assert_int_equal(yin_match_keyword(YCTX, "case", strlen("case"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CASE);
209 assert_int_equal(yin_match_keyword(YCTX, "choice", strlen("choice"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CHOICE);
210 assert_int_equal(yin_match_keyword(YCTX, "config", strlen("config"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONFIG);
211 assert_int_equal(yin_match_keyword(YCTX, "contact", strlen("contact"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONTACT);
212 assert_int_equal(yin_match_keyword(YCTX, "container", strlen("container"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONTAINER);
213 assert_int_equal(yin_match_keyword(YCTX, "default", strlen("default"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEFAULT);
214 assert_int_equal(yin_match_keyword(YCTX, "description", strlen("description"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DESCRIPTION);
215 assert_int_equal(yin_match_keyword(YCTX, "deviate", strlen("deviate"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEVIATE);
216 assert_int_equal(yin_match_keyword(YCTX, "deviation", strlen("deviation"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEVIATION);
217 assert_int_equal(yin_match_keyword(YCTX, "enum", strlen("enum"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ENUM);
218 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);
219 assert_int_equal(yin_match_keyword(YCTX, "error-message", strlen("error-message"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ERROR_MESSAGE);
220 assert_int_equal(yin_match_keyword(YCTX, "extension", strlen("extension"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_EXTENSION);
221 assert_int_equal(yin_match_keyword(YCTX, "feature", strlen("feature"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_FEATURE);
222 assert_int_equal(yin_match_keyword(YCTX, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_FRACTION_DIGITS);
223 assert_int_equal(yin_match_keyword(YCTX, "grouping", strlen("grouping"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_GROUPING);
224 assert_int_equal(yin_match_keyword(YCTX, "identity", strlen("identity"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IDENTITY);
225 assert_int_equal(yin_match_keyword(YCTX, "if-feature", strlen("if-feature"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IF_FEATURE);
226 assert_int_equal(yin_match_keyword(YCTX, "import", strlen("import"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IMPORT);
227 assert_int_equal(yin_match_keyword(YCTX, "include", strlen("include"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_INCLUDE);
228 assert_int_equal(yin_match_keyword(YCTX, "input", strlen("input"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_INPUT);
229 assert_int_equal(yin_match_keyword(YCTX, "key", strlen("key"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_KEY);
230 assert_int_equal(yin_match_keyword(YCTX, "leaf", strlen("leaf"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LEAF);
231 assert_int_equal(yin_match_keyword(YCTX, "leaf-list", strlen("leaf-list"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LEAF_LIST);
232 assert_int_equal(yin_match_keyword(YCTX, "length", strlen("length"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LENGTH);
233 assert_int_equal(yin_match_keyword(YCTX, "list", strlen("list"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LIST);
234 assert_int_equal(yin_match_keyword(YCTX, "mandatory", strlen("mandatory"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MANDATORY);
235 assert_int_equal(yin_match_keyword(YCTX, "max-elements", strlen("max-elements"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MAX_ELEMENTS);
236 assert_int_equal(yin_match_keyword(YCTX, "min-elements", strlen("min-elements"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MIN_ELEMENTS);
237 assert_int_equal(yin_match_keyword(YCTX, "modifier", strlen("modifier"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MODIFIER);
238 assert_int_equal(yin_match_keyword(YCTX, "module", strlen("module"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MODULE);
239 assert_int_equal(yin_match_keyword(YCTX, "must", strlen("must"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MUST);
240 assert_int_equal(yin_match_keyword(YCTX, "namespace", strlen("namespace"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NAMESPACE);
241 assert_int_equal(yin_match_keyword(YCTX, "notification", strlen("notification"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NOTIFICATION);
242 assert_int_equal(yin_match_keyword(YCTX, "ordered-by", strlen("ordered-by"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ORDERED_BY);
243 assert_int_equal(yin_match_keyword(YCTX, "organization", strlen("organization"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ORGANIZATION);
244 assert_int_equal(yin_match_keyword(YCTX, "output", strlen("output"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_OUTPUT);
245 assert_int_equal(yin_match_keyword(YCTX, "path", strlen("path"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PATH);
246 assert_int_equal(yin_match_keyword(YCTX, "pattern", strlen("pattern"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PATTERN);
247 assert_int_equal(yin_match_keyword(YCTX, "position", strlen("position"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_POSITION);
248 assert_int_equal(yin_match_keyword(YCTX, "prefix", strlen("prefix"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PREFIX);
249 assert_int_equal(yin_match_keyword(YCTX, "presence", strlen("presence"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PRESENCE);
250 assert_int_equal(yin_match_keyword(YCTX, "range", strlen("range"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_RANGE);
251 assert_int_equal(yin_match_keyword(YCTX, "reference", strlen("reference"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REFERENCE);
252 assert_int_equal(yin_match_keyword(YCTX, "refine", strlen("refine"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REFINE);
253 assert_int_equal(yin_match_keyword(YCTX, "require-instance", strlen("require-instance"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REQUIRE_INSTANCE);
254 assert_int_equal(yin_match_keyword(YCTX, "revision", strlen("revision"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REVISION);
255 assert_int_equal(yin_match_keyword(YCTX, "revision-date", strlen("revision-date"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REVISION_DATE);
256 assert_int_equal(yin_match_keyword(YCTX, "rpc", strlen("rpc"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_RPC);
257 assert_int_equal(yin_match_keyword(YCTX, "status", strlen("status"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_STATUS);
258 assert_int_equal(yin_match_keyword(YCTX, "submodule", strlen("submodule"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_SUBMODULE);
259 assert_int_equal(yin_match_keyword(YCTX, "type", strlen("type"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_TYPE);
260 assert_int_equal(yin_match_keyword(YCTX, "typedef", strlen("typedef"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_TYPEDEF);
261 assert_int_equal(yin_match_keyword(YCTX, "unique", strlen("unique"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_UNIQUE);
262 assert_int_equal(yin_match_keyword(YCTX, "units", strlen("units"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_UNITS);
263 assert_int_equal(yin_match_keyword(YCTX, "uses", strlen("uses"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_USES);
264 assert_int_equal(yin_match_keyword(YCTX, "value", strlen("value"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_VALUE);
265 assert_int_equal(yin_match_keyword(YCTX, "when", strlen("when"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_WHEN);
266 assert_int_equal(yin_match_keyword(YCTX, "yang-version", strlen("yang-version"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_YANG_VERSION);
267 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 +0200268}
David Sedlák3b4db242018-10-19 16:11:01 +0200269
David Sedlák872c7b42018-10-26 13:15:20 +0200270static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200271test_yin_match_argument_name(void **UNUSED(state))
David Sedlák872c7b42018-10-26 13:15:20 +0200272{
David Sedlák060b00e2019-06-19 11:12:06 +0200273 assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN);
274 assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN);
275 assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN);
276 assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION);
277 assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE);
278 assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE);
279 assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME);
280 assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG);
281 assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
282 assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT);
283 assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI);
284 assert_int_equal(yin_match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200285}
286
David Sedlák68a1af12019-03-08 13:46:54 +0100287static void
David Sedlákb1a78352019-06-28 16:16:29 +0200288test_yin_parse_element_generic(void **state)
289{
David Sedlákb1a78352019-06-28 16:16:29 +0200290 struct lysp_ext_instance exts;
David Sedlákb1a78352019-06-28 16:16:29 +0200291 LY_ERR ret;
Radek Iša56ca9e42020-09-08 18:42:00 +0200292 const char *arg;
293 const char *stmt;
294 const char *data;
David Sedlákb1a78352019-06-28 16:16:29 +0200295
296 memset(&exts, 0, sizeof(exts));
297
Radek Iša56ca9e42020-09-08 18:42:00 +0200298 data = "<myext:elem attr=\"value\" xmlns:myext=\"urn:example:extensions\">text_value</myext:elem>";
299 ly_in_new_memory(data, &UTEST_IN);
300 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100301
Radek Iša56ca9e42020-09-08 18:42:00 +0200302 ret = yin_parse_element_generic(YCTX, LY_STMT_EXTENSION_INSTANCE, &exts.child);
David Sedlákb1a78352019-06-28 16:16:29 +0200303 assert_int_equal(ret, LY_SUCCESS);
Radek Iša56ca9e42020-09-08 18:42:00 +0200304 assert_int_equal(YCTX->xmlctx->status, LYXML_ELEM_CLOSE);
305 stmt = "urn:example:extensions:elem";
306 arg = "text_value";
307 CHECK_LYSP_STMT(exts.child, arg, 1, 0, 0x45, 0, stmt);
308 stmt = "attr";
309 arg = "value";
310 CHECK_LYSP_STMT(exts.child->child, arg, 0, 0x400, 0, 0, stmt);
311 lysp_ext_instance_free(UTEST_LYCTX, &exts);
312 RESET_STATE;
David Sedlák5392a212019-07-01 09:19:10 +0200313
David Sedlákb0ca07d2019-09-11 11:54:05 +0200314 data = "<myext:elem xmlns:myext=\"urn:example:extensions\"></myext:elem>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200315 ly_in_new_memory(data, &UTEST_IN);
316 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100317
Radek Iša56ca9e42020-09-08 18:42:00 +0200318 ret = yin_parse_element_generic(YCTX, LY_STMT_EXTENSION_INSTANCE, &exts.child);
David Sedlák5392a212019-07-01 09:19:10 +0200319 assert_int_equal(ret, LY_SUCCESS);
Radek Iša56ca9e42020-09-08 18:42:00 +0200320 assert_int_equal(YCTX->xmlctx->status, LYXML_ELEM_CLOSE);
321 stmt = "urn:example:extensions:elem";
322 CHECK_LYSP_STMT(exts.child, NULL, 0, 0x0, 0x45, 0, stmt);
323 lysp_ext_instance_free(UTEST_LYCTX, &exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200324}
325
326static void
327test_yin_parse_extension_instance(void **state)
328{
329 LY_ERR ret;
David Sedlákb1a78352019-06-28 16:16:29 +0200330 struct lysp_ext_instance *exts = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +0200331 struct lysp_stmt *act_child;
David Sedlákb0ca07d2019-09-11 11:54:05 +0200332 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 +0200333 const char *exts_name;
334 const char *stmt = "value1";
335 const char *arg = "test";
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100336
Radek Iša56ca9e42020-09-08 18:42:00 +0200337 ly_in_new_memory(data, &UTEST_IN);
338 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100339
Radek Iša56ca9e42020-09-08 18:42:00 +0200340 ret = yin_parse_extension_instance(YCTX, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200341 assert_int_equal(ret, LY_SUCCESS);
Radek Iša56ca9e42020-09-08 18:42:00 +0200342 exts_name = "urn:example:extensions:ext";
343 CHECK_LYSP_EXT_INSTANCE(exts, NULL, 1, NULL,
344 LYEXT_SUBSTMT_CONTACT, 0, exts_name, 0, 0x2, LYS_YIN);
David Sedlákb1a78352019-06-28 16:16:29 +0200345
Radek Iša56ca9e42020-09-08 18:42:00 +0200346 CHECK_LYSP_STMT(exts->child, arg, 0, LYS_YIN_ATTR, 0, 1, stmt);
347 stmt = "value";
348 arg = "test2";
349 CHECK_LYSP_STMT(exts->child->next, arg, 0, LYS_YIN_ATTR, 0, 1, stmt);
350 stmt = "urn:example:extensions:subelem";
351 arg = "text";
352 CHECK_LYSP_STMT(exts->child->next->next, arg, 0, 0, 0x45, 0, stmt);
353 lysp_ext_instance_free(UTEST_LYCTX, exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200354 LY_ARRAY_FREE(exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200355 exts = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +0200356 RESET_STATE;
David Sedlákf250ecf2019-07-01 11:02:05 +0200357
David Sedlákb0ca07d2019-09-11 11:54:05 +0200358 data = "<myext:extension-elem xmlns:myext=\"urn:example:extensions\" />";
Radek Iša56ca9e42020-09-08 18:42:00 +0200359 ly_in_new_memory(data, &UTEST_IN);
360 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100361
Radek Iša56ca9e42020-09-08 18:42:00 +0200362 ret = yin_parse_extension_instance(YCTX, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200363 assert_int_equal(ret, LY_SUCCESS);
Radek Iša56ca9e42020-09-08 18:42:00 +0200364 exts_name = "urn:example:extensions:extension-elem";
365 CHECK_LYSP_EXT_INSTANCE(exts, NULL, 0, NULL,
366 LYEXT_SUBSTMT_CONTACT, 0, exts_name, 0, 0x2, LYS_YIN);
367 lysp_ext_instance_free(UTEST_LYCTX, exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200368 LY_ARRAY_FREE(exts);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200369 exts = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +0200370 RESET_STATE;
David Sedlákadd0c2e2019-08-16 10:49:12 +0200371
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100372 data =
373 "<myext:ext attr1=\"text1\" attr2=\"text2\" xmlns:myext=\"urn:example:extensions\">\n"
374 " <myext:ext-sub1/>\n"
375 " <myext:ext-sub2 sattr1=\"stext2\">\n"
376 " <myext:ext-sub21>\n"
377 " <myext:ext-sub211 sattr21=\"text21\"/>\n"
378 " </myext:ext-sub21>\n"
379 " </myext:ext-sub2>\n"
380 " <myext:ext-sub3 attr3=\"text3\"></myext:ext-sub3>\n"
381 "</myext:ext>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200382 ly_in_new_memory(data, &UTEST_IN);
383 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100384
Radek Iša56ca9e42020-09-08 18:42:00 +0200385 ret = yin_parse_extension_instance(YCTX, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200386 assert_int_equal(ret, LY_SUCCESS);
387
Radek Iša56ca9e42020-09-08 18:42:00 +0200388 exts_name = "urn:example:extensions:ext";
389 CHECK_LYSP_EXT_INSTANCE(exts, NULL, 1, NULL,
390 LYEXT_SUBSTMT_CONTACT, 0, exts_name, 0, 0x2, LYS_YIN);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200391
Radek Iša56ca9e42020-09-08 18:42:00 +0200392 stmt = "attr1";
393 arg = "text1";
394 act_child = exts->child;
395 CHECK_LYSP_STMT(act_child, arg, NULL, LYS_YIN_ATTR, 0x0, 1, stmt);
396 stmt = "attr2";
397 arg = "text2";
398 act_child = act_child->next;
399 CHECK_LYSP_STMT(act_child, arg, NULL, LYS_YIN_ATTR, 0x0, 1, stmt);
400 stmt = "urn:example:extensions:ext-sub1";
401 arg = NULL;
402 act_child = act_child->next;
403 CHECK_LYSP_STMT(act_child, arg, NULL, 0, 0x45, 1, stmt);
404 stmt = "urn:example:extensions:ext-sub2";
405 arg = NULL;
406 act_child = act_child->next;
407 CHECK_LYSP_STMT(act_child, arg, 1, 0, 0x45, 1, stmt);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200408
Radek Iša56ca9e42020-09-08 18:42:00 +0200409 stmt = "sattr1";
410 arg = "stext2";
411 act_child = act_child->child;
412 CHECK_LYSP_STMT(act_child, arg, NULL, LYS_YIN_ATTR, 0, 1, stmt);
413 stmt = "urn:example:extensions:ext-sub21";
414 arg = NULL;
415 act_child = act_child->next;
416 CHECK_LYSP_STMT(act_child, arg, 1, 0, 0x45, 0, stmt);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200417
Radek Iša56ca9e42020-09-08 18:42:00 +0200418 stmt = "urn:example:extensions:ext-sub211";
419 arg = NULL;
420 act_child = act_child->child;
421 CHECK_LYSP_STMT(act_child, arg, 1, 0, 0x45, 0, stmt);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200422
Radek Iša56ca9e42020-09-08 18:42:00 +0200423 stmt = "sattr21";
424 arg = "text21";
425 act_child = act_child->child;
426 CHECK_LYSP_STMT(act_child, arg, 0, LYS_YIN_ATTR, 0, 0, stmt);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200427
Radek Iša56ca9e42020-09-08 18:42:00 +0200428 stmt = "urn:example:extensions:ext-sub3";
429 arg = NULL;
430 act_child = exts->child->next->next->next->next;
431 CHECK_LYSP_STMT(act_child, arg, 1, 0, 0x45, 0, stmt);
432 stmt = "attr3";
433 arg = "text3";
434 act_child = act_child->child;
435 CHECK_LYSP_STMT(act_child, arg, 0, LYS_YIN_ATTR, 0, 0, stmt);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200436
Radek Iša56ca9e42020-09-08 18:42:00 +0200437 lysp_ext_instance_free(UTEST_LYCTX, exts);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200438 LY_ARRAY_FREE(exts);
439 exts = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +0200440 RESET_STATE;
David Sedlákaa98bba2019-09-12 11:52:14 +0200441
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100442 data =
443 "<myext:extension-elem xmlns:myext=\"urn:example:extensions\" xmlns:yin=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
444 " <yin:action name=\"act-name\" pre:prefixed=\"ignored\"/>\n"
445 " <yin:augment target-node=\"target\"/>\n"
446 " <yin:status value=\"value\"/>\n"
447 " <yin:include module=\"mod\"/>\n"
448 " <yin:input />\n"
449 " <yin:must condition=\"cond\"/>\n"
450 " <yin:namespace uri=\"uri\"/>\n"
451 " <yin:revision date=\"data\"/>\n"
452 " <yin:unique tag=\"tag\"/>\n"
453 " <yin:description><yin:text>contact-val</yin:text></yin:description>\n"
454 " <yin:error-message><yin:value>err-msg</yin:value></yin:error-message>\n"
455 "</myext:extension-elem>";
Radek Iša56ca9e42020-09-08 18:42:00 +0200456 ly_in_new_memory(data, &UTEST_IN);
457 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100458
Radek Iša56ca9e42020-09-08 18:42:00 +0200459 ret = yin_parse_extension_instance(YCTX, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákaa98bba2019-09-12 11:52:14 +0200460 assert_int_equal(ret, LY_SUCCESS);
461 assert_string_equal(exts->child->arg, "act-name");
462 assert_string_equal(exts->child->next->arg, "target");
463 assert_string_equal(exts->child->next->next->arg, "value");
464 assert_string_equal(exts->child->next->next->next->arg, "mod");
465 assert_null(exts->child->next->next->next->next->arg);
466 assert_string_equal(exts->child->next->next->next->next->next->arg, "cond");
467 assert_string_equal(exts->child->next->next->next->next->next->next->arg, "uri");
468 assert_string_equal(exts->child->next->next->next->next->next->next->next->arg, "data");
469 assert_string_equal(exts->child->next->next->next->next->next->next->next->next->arg, "tag");
470 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 +0200471 lysp_ext_instance_free(UTEST_LYCTX, exts);
David Sedlákaa98bba2019-09-12 11:52:14 +0200472 LY_ARRAY_FREE(exts);
473 exts = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +0200474 RESET_STATE;
David Sedlákb1a78352019-06-28 16:16:29 +0200475}
476
David Sedlák555c7202019-07-04 12:14:12 +0200477static void
478test_yin_parse_content(void **state)
479{
David Sedlák555c7202019-07-04 12:14:12 +0200480 LY_ERR ret = LY_SUCCESS;
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100481 const char *data =
482 "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
483 " <myext:custom xmlns:myext=\"urn:example:extensions\">totally amazing extension</myext:custom>\n"
484 " <extension name=\"ext\">\n"
485 " <argument name=\"argname\"></argument>\n"
486 " <description><text>desc</text></description>\n"
487 " <reference><text>ref</text></reference>\n"
488 " <status value=\"deprecated\"></status>\n"
489 " </extension>\n"
490 " <text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>\n"
491 " <if-feature name=\"foo\"></if-feature>\n"
492 " <when condition=\"condition...\">\n"
493 " <reference><text>when_ref</text></reference>\n"
494 " <description><text>when_desc</text></description>\n"
495 " </when>\n"
496 " <config value=\"true\"/>\n"
497 " <error-message>\n"
498 " <value>error-msg</value>\n"
499 " </error-message>\n"
500 " <error-app-tag value=\"err-app-tag\"/>\n"
501 " <units name=\"radians\"></units>\n"
502 " <default value=\"default-value\"/>\n"
503 " <position value=\"25\"></position>\n"
504 " <value value=\"-5\"/>\n"
505 " <require-instance value=\"true\"></require-instance>\n"
506 " <range value=\"5..10\" />\n"
507 " <length value=\"baf\"/>\n"
508 " <pattern value='pattern'>\n"
509 " <modifier value='invert-match'/>\n"
510 " </pattern>\n"
511 " <enum name=\"yay\">\n"
512 " </enum>\n"
513 "</prefix>";
David Sedlák555c7202019-07-04 12:14:12 +0200514 struct lysp_ext_instance *exts = NULL;
David Sedlák5f8191e2019-07-08 16:35:52 +0200515 const char **if_features = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +0200516 const char *value, *error_message, *app_tag, *units;
Michal Vasko7f45cf22020-10-01 12:49:44 +0200517 struct lysp_qname def = {0};
David Sedlák986cb412019-07-04 13:10:11 +0200518 struct lysp_ext *ext_def = NULL;
David Sedlák32eee7b2019-07-09 12:38:44 +0200519 struct lysp_when *when_p = NULL;
David Sedlákcf5569a2019-07-11 13:31:34 +0200520 struct lysp_type_enum pos_enum = {}, val_enum = {};
David Sedlákfd5b9c32019-07-12 15:33:13 +0200521 struct lysp_type req_type = {}, range_type = {}, len_type = {}, patter_type = {}, enum_type = {};
Juraj Vijtiukf6a07372020-10-29 21:13:33 +0100522 uint16_t config = 0;
David Sedlák555c7202019-07-04 12:14:12 +0200523
Radek Iša56ca9e42020-09-08 18:42:00 +0200524 ly_in_new_memory(data, &UTEST_IN);
525 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
526 lyxml_ctx_next(YCTX->xmlctx);
527 lyxml_ctx_next(YCTX->xmlctx);
528 lyxml_ctx_next(YCTX->xmlctx);
David Sedlák555c7202019-07-04 12:14:12 +0200529
David Sedlákfd5b9c32019-07-12 15:33:13 +0200530 struct yin_subelement subelems[17] = {
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100531 {LY_STMT_CONFIG, &config, 0},
532 {LY_STMT_DEFAULT, &def, YIN_SUBELEM_UNIQUE},
533 {LY_STMT_ENUM, &enum_type, 0},
534 {LY_STMT_ERROR_APP_TAG, &app_tag, YIN_SUBELEM_UNIQUE},
Radek Iša56ca9e42020-09-08 18:42:00 +0200535 {LY_STMT_ERROR_MESSAGE, &error_message, 0},
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100536 {LY_STMT_EXTENSION, &ext_def, 0},
537 {LY_STMT_IF_FEATURE, &if_features, 0},
538 {LY_STMT_LENGTH, &len_type, 0},
539 {LY_STMT_PATTERN, &patter_type, 0},
540 {LY_STMT_POSITION, &pos_enum, 0},
541 {LY_STMT_RANGE, &range_type, 0},
542 {LY_STMT_REQUIRE_INSTANCE, &req_type, 0},
543 {LY_STMT_UNITS, &units, YIN_SUBELEM_UNIQUE},
544 {LY_STMT_VALUE, &val_enum, 0},
545 {LY_STMT_WHEN, &when_p, 0},
546 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
547 {LY_STMT_ARG_TEXT, &value, 0}
548 };
549
Radek Iša56ca9e42020-09-08 18:42:00 +0200550 ret = yin_parse_content(YCTX, subelems, 17, LY_STMT_PREFIX, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200551 assert_int_equal(ret, LY_SUCCESS);
David Sedlák2ce1be62019-07-10 16:15:09 +0200552 /* check parsed values */
Michal Vasko7f45cf22020-10-01 12:49:44 +0200553 assert_string_equal(def.str, "default-value");
Radek Iša56ca9e42020-09-08 18:42:00 +0200554 const char *exts_name = "urn:example:extensions:custom";
555 const char *exts_arg = "totally amazing extension";
556
557 CHECK_LYSP_EXT_INSTANCE(exts, exts_arg, 0, NULL,
558 LYEXT_SUBSTMT_PREFIX, 0, exts_name, 0, 0x1, 0x1);
David Sedlák555c7202019-07-04 12:14:12 +0200559 assert_string_equal(value, "wsefsdf");
David Sedláka5b1d382019-07-10 16:31:09 +0200560 assert_string_equal(units, "radians");
David Sedlák32eee7b2019-07-09 12:38:44 +0200561 assert_string_equal(when_p->cond, "condition...");
562 assert_string_equal(when_p->dsc, "when_desc");
563 assert_string_equal(when_p->ref, "when_ref");
David Sedláke1a30302019-07-10 13:49:38 +0200564 assert_int_equal(config, LYS_CONFIG_W);
Radek Iša56ca9e42020-09-08 18:42:00 +0200565 CHECK_LYSP_TYPE_ENUM(&pos_enum, NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, 25);
566 CHECK_LYSP_TYPE_ENUM(&val_enum, NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, -5);
David Sedlákcf5569a2019-07-11 13:31:34 +0200567 assert_int_equal(req_type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200568 assert_true(req_type.flags &= LYS_SET_REQINST);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200569 assert_string_equal(range_type.range->arg.str, "5..10");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200570 assert_true(range_type.flags & LYS_SET_RANGE);
Radek Iša56ca9e42020-09-08 18:42:00 +0200571 assert_string_equal(error_message, "error-msg");
David Sedlák2ce1be62019-07-10 16:15:09 +0200572 assert_string_equal(app_tag, "err-app-tag");
David Sedlákfd5b9c32019-07-12 15:33:13 +0200573 assert_string_equal(enum_type.enums->name, "yay");
Radek Iša56ca9e42020-09-08 18:42:00 +0200574 CHECK_LYSP_RESTR(len_type.length, "baf", NULL,
575 NULL, NULL, 0, NULL);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200576 assert_true(len_type.flags & LYS_SET_LENGTH);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200577 assert_string_equal(patter_type.patterns->arg.str, "\x015pattern");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200578 assert_true(patter_type.flags & LYS_SET_PATTERN);
David Sedlák2ce1be62019-07-10 16:15:09 +0200579 /* cleanup */
Radek Iša56ca9e42020-09-08 18:42:00 +0200580 lysp_ext_instance_free(UTEST_LYCTX, exts);
581 lysp_when_free(UTEST_LYCTX, when_p);
582 lysp_ext_free(UTEST_LYCTX, ext_def);
583 FREE_STRING(UTEST_LYCTX, *if_features);
584 FREE_STRING(UTEST_LYCTX, error_message);
585 FREE_STRING(UTEST_LYCTX, app_tag);
586 FREE_STRING(UTEST_LYCTX, units);
587 FREE_STRING(UTEST_LYCTX, patter_type.patterns->arg.str);
588 FREE_STRING(UTEST_LYCTX, def.str);
589 FREE_STRING(UTEST_LYCTX, range_type.range->arg.str);
590 FREE_STRING(UTEST_LYCTX, len_type.length->arg.str);
591 FREE_STRING(UTEST_LYCTX, enum_type.enums->name);
592 FREE_STRING(UTEST_LYCTX, value);
David Sedlák5f8191e2019-07-08 16:35:52 +0200593 LY_ARRAY_FREE(if_features);
David Sedlák555c7202019-07-04 12:14:12 +0200594 LY_ARRAY_FREE(exts);
David Sedlák986cb412019-07-04 13:10:11 +0200595 LY_ARRAY_FREE(ext_def);
David Sedlákd3983112019-07-12 11:20:56 +0200596 LY_ARRAY_FREE(patter_type.patterns);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200597 LY_ARRAY_FREE(enum_type.enums);
David Sedlák32eee7b2019-07-09 12:38:44 +0200598 free(when_p);
David Sedlákb7296dd2019-07-11 14:58:38 +0200599 free(range_type.range);
David Sedlák438ae432019-07-11 15:36:54 +0200600 free(len_type.length);
Radek Iša56ca9e42020-09-08 18:42:00 +0200601 RESET_STATE;
David Sedlák555c7202019-07-04 12:14:12 +0200602
603 /* test unique subelem */
604 const char *prefix_value;
Radek Krejcid6b76452019-09-03 17:03:03 +0200605 struct yin_subelement subelems2[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE},
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100606 {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_UNIQUE}};
607
David Sedláke6cd89e2019-08-07 12:46:02 +0200608 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100609 "<prefix value=\"inv_mod\" />"
610 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
611 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
612 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200613 ly_in_new_memory(data, &UTEST_IN);
614 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
615 lyxml_ctx_next(YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100616
Radek Iša56ca9e42020-09-08 18:42:00 +0200617 ret = yin_parse_content(YCTX, subelems2, 2, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200618 assert_int_equal(ret, LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +0200619 CHECK_LOG_CTX("Redefinition of \"text\" sub-element in \"status\" element.", "Line number 1.");
620 lydict_remove(UTEST_LYCTX, prefix_value);
621 lydict_remove(UTEST_LYCTX, value);
622 RESET_STATE;
David Sedlák555c7202019-07-04 12:14:12 +0200623
624 /* test first subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200625 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100626 "<prefix value=\"inv_mod\" />"
627 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
628 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
629 ELEMENT_WRAPPER_END;
Radek Krejcid6b76452019-09-03 17:03:03 +0200630 struct yin_subelement subelems3[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE},
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100631 {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_FIRST}};
632
Radek Iša56ca9e42020-09-08 18:42:00 +0200633 ly_in_new_memory(data, &UTEST_IN);
634 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
635 lyxml_ctx_next(YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100636
Radek Iša56ca9e42020-09-08 18:42:00 +0200637 ret = yin_parse_content(YCTX, subelems3, 2, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200638 assert_int_equal(ret, LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +0200639 CHECK_LOG_CTX("Sub-element \"text\" of \"status\" element must be defined as it's first sub-element.", "Line number 1.");
640 lydict_remove(UTEST_LYCTX, prefix_value);
641 RESET_STATE;
David Sedlák555c7202019-07-04 12:14:12 +0200642
643 /* test mandatory subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200644 data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END;
Radek Krejcid6b76452019-09-03 17:03:03 +0200645 struct yin_subelement subelems4[1] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}};
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100646
Radek Iša56ca9e42020-09-08 18:42:00 +0200647 ly_in_new_memory(data, &UTEST_IN);
648 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
649 lyxml_ctx_next(YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100650
Radek Iša56ca9e42020-09-08 18:42:00 +0200651 ret = yin_parse_content(YCTX, subelems4, 1, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200652 assert_int_equal(ret, LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +0200653 CHECK_LOG_CTX("Missing mandatory sub-element \"prefix\" of \"status\" element.", "Line number 1.");
David Sedlák555c7202019-07-04 12:14:12 +0200654}
655
David Sedlák92147b02019-07-09 14:01:01 +0200656static void
David Sedlák4a650532019-07-10 11:55:18 +0200657test_validate_value(void **state)
658{
Michal Vaskob36053d2020-03-26 15:49:30 +0100659 const char *data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END;
660
661 /* create some XML context */
Radek Iša56ca9e42020-09-08 18:42:00 +0200662 ly_in_new_memory(data, &UTEST_IN);
663 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
664 YCTX->xmlctx->status = LYXML_ELEM_CONTENT;
665 YCTX->xmlctx->dynamic = 0;
Michal Vaskob36053d2020-03-26 15:49:30 +0100666
Radek Iša56ca9e42020-09-08 18:42:00 +0200667 YCTX->xmlctx->value = "#invalid";
668 YCTX->xmlctx->value_len = 8;
669 assert_int_equal(yin_validate_value(YCTX, Y_IDENTIF_ARG), LY_EVALID);
670 CHECK_LOG_CTX("Invalid identifier character '#' (0x0023).", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100671
Radek Iša56ca9e42020-09-08 18:42:00 +0200672 YCTX->xmlctx->value = "";
673 YCTX->xmlctx->value_len = 0;
674 assert_int_equal(yin_validate_value(YCTX, Y_STR_ARG), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +0100675
Radek Iša56ca9e42020-09-08 18:42:00 +0200676 YCTX->xmlctx->value = "pre:b";
677 YCTX->xmlctx->value_len = 5;
678 assert_int_equal(yin_validate_value(YCTX, Y_IDENTIF_ARG), LY_EVALID);
679 assert_int_equal(yin_validate_value(YCTX, Y_PREF_IDENTIF_ARG), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +0100680
Radek Iša56ca9e42020-09-08 18:42:00 +0200681 YCTX->xmlctx->value = "pre:pre:b";
682 YCTX->xmlctx->value_len = 9;
683 assert_int_equal(yin_validate_value(YCTX, Y_PREF_IDENTIF_ARG), LY_EVALID);
David Sedlák4a650532019-07-10 11:55:18 +0200684}
685
David Sedlák32488102019-07-15 17:44:10 +0200686/* helper function to simplify unit test of each element using parse_content function */
687LY_ERR
Radek Iša56ca9e42020-09-08 18:42:00 +0200688test_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 +0200689{
David Sedlákc5b20842019-08-13 10:18:31 +0200690 const char *name, *prefix;
691 size_t name_len, prefix_len;
David Sedlák32488102019-07-15 17:44:10 +0200692 LY_ERR ret = LY_SUCCESS;
693 struct yin_subelement subelems[71] = {
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100694 {LY_STMT_ACTION, dest, 0},
695 {LY_STMT_ANYDATA, dest, 0},
696 {LY_STMT_ANYXML, dest, 0},
697 {LY_STMT_ARGUMENT, dest, 0},
698 {LY_STMT_AUGMENT, dest, 0},
699 {LY_STMT_BASE, dest, 0},
700 {LY_STMT_BELONGS_TO, dest, 0},
701 {LY_STMT_BIT, dest, 0},
702 {LY_STMT_CASE, dest, 0},
703 {LY_STMT_CHOICE, dest, 0},
704 {LY_STMT_CONFIG, dest, 0},
705 {LY_STMT_CONTACT, dest, 0},
706 {LY_STMT_CONTAINER, dest, 0},
707 {LY_STMT_DEFAULT, dest, YIN_SUBELEM_UNIQUE},
708 {LY_STMT_DESCRIPTION, dest, 0},
709 {LY_STMT_DEVIATE, dest, 0},
710 {LY_STMT_DEVIATION, dest, 0},
711 {LY_STMT_ENUM, dest, 0},
712 {LY_STMT_ERROR_APP_TAG, dest, YIN_SUBELEM_UNIQUE},
713 {LY_STMT_ERROR_MESSAGE, dest, 0},
714 {LY_STMT_EXTENSION, dest, 0},
715 {LY_STMT_FEATURE, dest, 0},
716 {LY_STMT_FRACTION_DIGITS, dest, 0},
717 {LY_STMT_GROUPING, dest, 0},
718 {LY_STMT_IDENTITY, dest, 0},
719 {LY_STMT_IF_FEATURE, dest, 0},
720 {LY_STMT_IMPORT, dest, 0},
721 {LY_STMT_INCLUDE, dest, 0},
722 {LY_STMT_INPUT, dest, 0},
723 {LY_STMT_KEY, dest, YIN_SUBELEM_UNIQUE},
724 {LY_STMT_LEAF, dest, 0},
725 {LY_STMT_LEAF_LIST, dest, 0},
726 {LY_STMT_LENGTH, dest, 0},
727 {LY_STMT_LIST, dest, 0},
728 {LY_STMT_MANDATORY, dest, 0},
729 {LY_STMT_MAX_ELEMENTS, dest, 0},
730 {LY_STMT_MIN_ELEMENTS, dest, 0},
731 {LY_STMT_MODIFIER, dest, 0},
732 {LY_STMT_MODULE, dest, 0},
733 {LY_STMT_MUST, dest, 0},
734 {LY_STMT_NAMESPACE, dest, YIN_SUBELEM_UNIQUE},
735 {LY_STMT_NOTIFICATION, dest, 0},
736 {LY_STMT_ORDERED_BY, dest, 0},
737 {LY_STMT_ORGANIZATION, dest, 0},
738 {LY_STMT_OUTPUT, dest, 0},
739 {LY_STMT_PATH, dest, 0},
740 {LY_STMT_PATTERN, dest, 0},
741 {LY_STMT_POSITION, dest, 0},
742 {LY_STMT_PREFIX, dest, YIN_SUBELEM_UNIQUE},
743 {LY_STMT_PRESENCE, dest, YIN_SUBELEM_UNIQUE},
744 {LY_STMT_RANGE, dest, 0},
745 {LY_STMT_REFERENCE, dest, 0},
746 {LY_STMT_REFINE, dest, 0},
747 {LY_STMT_REQUIRE_INSTANCE, dest, 0},
748 {LY_STMT_REVISION, dest, 0},
749 {LY_STMT_REVISION_DATE, dest, 0},
750 {LY_STMT_RPC, dest, 0},
751 {LY_STMT_STATUS, dest, 0},
752 {LY_STMT_SUBMODULE, dest, 0},
753 {LY_STMT_TYPE, dest, 0},
754 {LY_STMT_TYPEDEF, dest, 0},
755 {LY_STMT_UNIQUE, dest, 0},
756 {LY_STMT_UNITS, dest, YIN_SUBELEM_UNIQUE},
757 {LY_STMT_USES, dest, 0},
758 {LY_STMT_VALUE, dest, 0},
759 {LY_STMT_WHEN, dest, 0},
760 {LY_STMT_YANG_VERSION, dest, 0},
761 {LY_STMT_YIN_ELEMENT, dest, 0},
762 {LY_STMT_EXTENSION_INSTANCE, dest, 0},
763 {LY_STMT_ARG_TEXT, dest, 0},
764 {LY_STMT_ARG_VALUE, dest, 0}
765 };
766
Radek Iša56ca9e42020-09-08 18:42:00 +0200767 ly_in_new_memory(data, &UTEST_IN);
768 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
769 prefix = YCTX->xmlctx->prefix;
770 prefix_len = YCTX->xmlctx->prefix_len;
771 name = YCTX->xmlctx->name;
772 name_len = YCTX->xmlctx->name_len;
773 lyxml_ctx_next(YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100774
Radek Iša56ca9e42020-09-08 18:42:00 +0200775 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 +0100776
Michal Vasko63f3d842020-07-08 10:10:14 +0200777 /* free parser and input */
Radek Iša56ca9e42020-09-08 18:42:00 +0200778 lyxml_ctx_free(YCTX->xmlctx);
779 YCTX->xmlctx = NULL;
780 ly_in_free(UTEST_IN, 0);
781 UTEST_IN = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200782 return ret;
783}
784
David Sedlákd1144562019-08-06 12:36:14 +0200785#define EXT_SUBELEM "<myext:c-define name=\"MY_MTU\" xmlns:myext=\"urn:example:extensions\"/>"
786
David Sedlák32488102019-07-15 17:44:10 +0200787static void
David Sedlák43801c92019-08-05 15:58:54 +0200788test_enum_elem(void **state)
David Sedlák32488102019-07-15 17:44:10 +0200789{
David Sedlák32488102019-07-15 17:44:10 +0200790 struct lysp_type type = {};
791 const char *data;
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100792
David Sedlák32488102019-07-15 17:44:10 +0200793 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100794 "<enum name=\"enum-name\">\n"
795 " <if-feature name=\"feature\" />\n"
796 " <value value=\"55\" />\n"
797 " <status value=\"deprecated\" />\n"
798 " <description><text>desc...</text></description>\n"
799 " <reference><text>ref...</text></reference>\n"
800 " " EXT_SUBELEM "\n"
801 "</enum>"
802 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200803 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
804 uint16_t flags = LYS_STATUS_DEPRC | LYS_SET_VALUE;
805
806 CHECK_LYSP_TYPE_ENUM(type.enums, "desc...", 1, flags, 1, "enum-name", "ref...", 55);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200807 assert_string_equal(type.enums->iffeatures[0].str, "feature");
Radek Iša56ca9e42020-09-08 18:42:00 +0200808 TEST_1_CHECK_LYSP_EXT_INSTANCE(type.enums->exts, LYEXT_SUBSTMT_SELF);
809 lysp_type_free(UTEST_LYCTX, &type);
David Sedlákd1144562019-08-06 12:36:14 +0200810 memset(&type, 0, sizeof type);
811
812 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100813 "<enum name=\"enum-name\"></enum>"
814 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200815 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200816 assert_string_equal(type.enums->name, "enum-name");
Radek Iša56ca9e42020-09-08 18:42:00 +0200817 lysp_type_free(UTEST_LYCTX, &type);
David Sedlák32488102019-07-15 17:44:10 +0200818 memset(&type, 0, sizeof type);
David Sedlák43801c92019-08-05 15:58:54 +0200819}
820
821static void
822test_bit_elem(void **state)
823{
David Sedlák43801c92019-08-05 15:58:54 +0200824 struct lysp_type type = {};
825 const char *data;
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100826
David Sedlák43801c92019-08-05 15:58:54 +0200827 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100828 "<bit name=\"bit-name\">\n"
829 " <if-feature name=\"feature\" />\n"
830 " <position value=\"55\" />\n"
831 " <status value=\"deprecated\" />\n"
832 " <description><text>desc...</text></description>\n"
833 " <reference><text>ref...</text></reference>\n"
834 EXT_SUBELEM
835 "</bit>"
836 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200837 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
838 uint16_t flags = LYS_STATUS_DEPRC | LYS_SET_VALUE;
839
840 CHECK_LYSP_TYPE_ENUM(type.bits, "desc...", 1, flags, 1, "bit-name", "ref...", 55);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200841 assert_string_equal(type.bits->iffeatures[0].str, "feature");
Radek Iša56ca9e42020-09-08 18:42:00 +0200842 TEST_1_CHECK_LYSP_EXT_INSTANCE(type.bits->exts, LYEXT_SUBSTMT_SELF);
843 lysp_type_free(UTEST_LYCTX, &type);
David Sedlákd1144562019-08-06 12:36:14 +0200844 memset(&type, 0, sizeof type);
845
846 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100847 "<bit name=\"bit-name\"> </bit>"
848 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200849 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
850 CHECK_LYSP_TYPE_ENUM(type.bits, NULL, 0, 0, 0, "bit-name", NULL, 0);
851 lysp_type_free(UTEST_LYCTX, &type);
David Sedlák43801c92019-08-05 15:58:54 +0200852 memset(&type, 0, sizeof type);
David Sedlák32488102019-07-15 17:44:10 +0200853}
854
855static void
856test_meta_elem(void **state)
857{
David Sedlák32488102019-07-15 17:44:10 +0200858 char *value = NULL;
859 const char *data;
David Sedlákd1144562019-08-06 12:36:14 +0200860 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200861
862 /* organization element */
863 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100864 "<organization><text>organization...</text>" EXT_SUBELEM EXT_SUBELEM "</organization>"
865 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200866 assert_int_equal(test_element_helper(state, data, &value, NULL, &exts), LY_SUCCESS);
867 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_ORGANIZATION);
868 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[1]), LYEXT_SUBSTMT_ORGANIZATION);
869
David Sedlák32488102019-07-15 17:44:10 +0200870 assert_string_equal(value, "organization...");
Radek Iša56ca9e42020-09-08 18:42:00 +0200871 FREE_STRING(UTEST_LYCTX, value);
872 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +0200873 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +0200874 exts = NULL;
875
David Sedlák32488102019-07-15 17:44:10 +0200876 /* contact element */
877 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100878 "<contact><text>contact...</text>" EXT_SUBELEM "</contact>"
879 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200880 assert_int_equal(test_element_helper(state, data, &value, NULL, &exts), LY_SUCCESS);
881 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_CONTACT);
David Sedlák32488102019-07-15 17:44:10 +0200882 assert_string_equal(value, "contact...");
Radek Iša56ca9e42020-09-08 18:42:00 +0200883 FREE_STRING(UTEST_LYCTX, value);
884 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +0200885 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200886 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +0200887
David Sedlák32488102019-07-15 17:44:10 +0200888 /* description element */
889 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100890 "<description><text>description...</text>" EXT_SUBELEM "</description>"
891 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200892 assert_int_equal(test_element_helper(state, data, &value, NULL, &exts), LY_SUCCESS);
893 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_DESCRIPTION);
David Sedlák32488102019-07-15 17:44:10 +0200894 assert_string_equal(value, "description...");
Radek Iša56ca9e42020-09-08 18:42:00 +0200895 FREE_STRING(UTEST_LYCTX, value);
David Sedlák32488102019-07-15 17:44:10 +0200896 value = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +0200897 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +0200898 exts = NULL;
899
David Sedlák32488102019-07-15 17:44:10 +0200900 /* reference element */
901 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100902 "<reference><text>reference...</text>" EXT_SUBELEM "</reference>"
903 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200904 assert_int_equal(test_element_helper(state, data, &value, NULL, &exts), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +0200905 assert_string_equal(value, "reference...");
Radek Iša56ca9e42020-09-08 18:42:00 +0200906 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_REFERENCE);
907 FREE_STRING(UTEST_LYCTX, value);
David Sedlák32488102019-07-15 17:44:10 +0200908 value = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +0200909 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +0200910 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200911
David Sedlákdf2a9732019-08-07 13:23:16 +0200912 /* reference element */
913 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100914 "<reference invalid=\"text\"><text>reference...</text>" "</reference>"
915 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200916 assert_int_equal(test_element_helper(state, data, &value, NULL, &exts), LY_EVALID);
917 CHECK_LOG_CTX("Unexpected attribute \"invalid\" of \"reference\" element.", "Line number 1.");
918 FREE_STRING(UTEST_LYCTX, value);
David Sedlákdf2a9732019-08-07 13:23:16 +0200919 value = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +0200920 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákdf2a9732019-08-07 13:23:16 +0200921 exts = NULL;
922
David Sedlák32488102019-07-15 17:44:10 +0200923 /* missing text subelement */
924 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100925 "<reference>reference...</reference>"
926 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200927 assert_int_equal(test_element_helper(state, data, &value, NULL, &exts), LY_EVALID);
928 CHECK_LOG_CTX("Missing mandatory sub-element \"text\" of \"reference\" element.", "Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +0200929
David Sedlákd1144562019-08-06 12:36:14 +0200930 /* reference element */
931 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100932 "<reference>" EXT_SUBELEM "<text>reference...</text></reference>"
933 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200934 assert_int_equal(test_element_helper(state, data, &value, NULL, &exts), LY_EVALID);
935 CHECK_LOG_CTX("Sub-element \"text\" of \"reference\" element must be defined as it's first sub-element.", "Line number 1.");
936 FREE_STRING(UTEST_LYCTX, value);
David Sedlákd1144562019-08-06 12:36:14 +0200937 value = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +0200938 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +0200939 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200940}
941
942static void
943test_import_elem(void **state)
944{
David Sedlák32488102019-07-15 17:44:10 +0200945 const char *data;
David Sedlák298ff6d2019-07-26 14:29:03 +0200946 struct lysp_import *imports = NULL;
947 struct import_meta imp_meta = {"prefix", &imports};
David Sedlák32488102019-07-15 17:44:10 +0200948
949 /* max subelems */
950 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100951 "<import module=\"a\">\n"
952 EXT_SUBELEM
953 " <prefix value=\"a_mod\"/>\n"
954 " <revision-date date=\"2015-01-01\"></revision-date>\n"
955 " <description><text>import description</text></description>\n"
956 " <reference><text>import reference</text></reference>\n"
957 "</import>"
958 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200959 assert_int_equal(test_element_helper(state, data, &imp_meta, NULL, NULL), LY_SUCCESS);
960 CHECK_LYSP_IMPORT(imports, "import description", 1, "a",
961 "a_mod", "import reference", "2015-01-01");
962 TEST_1_CHECK_LYSP_EXT_INSTANCE(imports->exts, LYEXT_SUBSTMT_SELF);
963 FREE_ARRAY(UTEST_LYCTX, imports, lysp_import_free);
David Sedlák298ff6d2019-07-26 14:29:03 +0200964 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200965
966 /* min subelems */
David Sedlák32488102019-07-15 17:44:10 +0200967 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100968 "<import module=\"a\">\n"
969 " <prefix value=\"a_mod\"/>\n"
970 "</import>"
971 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200972 assert_int_equal(test_element_helper(state, data, &imp_meta, NULL, NULL), LY_SUCCESS);
973 CHECK_LYSP_IMPORT(imports, NULL, 0, "a",
974 "a_mod", NULL, "");
975 FREE_ARRAY(UTEST_LYCTX, imports, lysp_import_free);
David Sedlák298ff6d2019-07-26 14:29:03 +0200976 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200977
978 /* invalid (missing prefix) */
David Sedlák298ff6d2019-07-26 14:29:03 +0200979 data = ELEMENT_WRAPPER_START "<import module=\"a\"></import>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200980 assert_int_equal(test_element_helper(state, data, &imp_meta, NULL, NULL), LY_EVALID);
981 CHECK_LOG_CTX("Missing mandatory sub-element \"prefix\" of \"import\" element.", "Line number 1.");
982 FREE_ARRAY(UTEST_LYCTX, imports, lysp_import_free);
David Sedlák298ff6d2019-07-26 14:29:03 +0200983 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200984
985 /* invalid reused prefix */
David Sedlák32488102019-07-15 17:44:10 +0200986 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100987 "<import module=\"a\">\n"
988 " <prefix value=\"prefix\"/>\n"
989 "</import>"
990 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200991 assert_int_equal(test_element_helper(state, data, &imp_meta, NULL, NULL), LY_EVALID);
992 CHECK_LOG_CTX("Prefix \"prefix\" already used as module prefix.", "Line number 3.");
993 FREE_ARRAY(UTEST_LYCTX, imports, lysp_import_free);
David Sedlák298ff6d2019-07-26 14:29:03 +0200994 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200995
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100996 data = ELEMENT_WRAPPER_START
997 "<import module=\"a\">\n"
998 " <prefix value=\"a\"/>\n"
999 "</import>\n"
1000 "<import module=\"a\">\n"
1001 " <prefix value=\"a\"/>\n"
Radek Iša56ca9e42020-09-08 18:42:00 +02001002 "</import>\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001003 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001004 assert_int_equal(test_element_helper(state, data, &imp_meta, NULL, NULL), LY_EVALID);
1005 CHECK_LOG_CTX("Prefix \"a\" already used to import \"a\" module.", "Line number 6.");
1006 FREE_ARRAY(UTEST_LYCTX, imports, lysp_import_free);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001007 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001008}
1009
1010static void
1011test_status_elem(void **state)
1012{
David Sedlák32488102019-07-15 17:44:10 +02001013 const char *data;
1014 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001015 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001016
1017 /* test valid values */
1018 data = ELEMENT_WRAPPER_START "<status value=\"current\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001019 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001020 assert_true(flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +02001021
1022 data = ELEMENT_WRAPPER_START "<status value=\"deprecated\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001023 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001024 assert_true(flags & LYS_STATUS_DEPRC);
David Sedlák32488102019-07-15 17:44:10 +02001025
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001026 data = ELEMENT_WRAPPER_START "<status value=\"obsolete\">"EXT_SUBELEM "</status>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001027 assert_int_equal(test_element_helper(state, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001028 assert_true(flags & LYS_STATUS_OBSLT);
Radek Iša56ca9e42020-09-08 18:42:00 +02001029 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_STATUS);
1030 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001031 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001032
1033 /* test invalid value */
1034 data = ELEMENT_WRAPPER_START "<status value=\"invalid\"></status>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001035 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID);
1036 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"status\" element. "
1037 "Valid values are \"current\", \"deprecated\" and \"obsolete\".", "Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001038}
1039
1040static void
1041test_ext_elem(void **state)
1042{
David Sedlák32488102019-07-15 17:44:10 +02001043 const char *data;
1044 struct lysp_ext *ext = NULL;
1045
1046 /* max subelems */
1047 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001048 "<extension name=\"ext_name\">\n"
1049 " <argument name=\"arg\"></argument>\n"
1050 " <status value=\"current\"/>\n"
1051 " <description><text>ext_desc</text></description>\n"
1052 " <reference><text>ext_ref</text></reference>\n"
1053 EXT_SUBELEM
1054 "</extension>"
1055 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001056 assert_int_equal(test_element_helper(state, data, &ext, NULL, NULL), LY_SUCCESS);
1057 CHECK_LYSP_EXT(ext, "arg", 0, "ext_desc", 1, LYS_STATUS_CURR, "ext_name", "ext_ref");
1058 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(ext->exts[0]), LYEXT_SUBSTMT_SELF);
1059 lysp_ext_free(UTEST_LYCTX, ext);
David Sedlák32488102019-07-15 17:44:10 +02001060 LY_ARRAY_FREE(ext);
1061 ext = NULL;
1062
1063 /* min subelems */
1064 data = ELEMENT_WRAPPER_START "<extension name=\"ext_name\"></extension>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001065 assert_int_equal(test_element_helper(state, data, &ext, NULL, NULL), LY_SUCCESS);
1066 CHECK_LYSP_EXT(ext, NULL, 0, NULL, 0, 0, "ext_name", NULL);
1067 lysp_ext_free(UTEST_LYCTX, ext);
David Sedlák32488102019-07-15 17:44:10 +02001068 LY_ARRAY_FREE(ext);
1069 ext = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001070}
1071
1072static void
1073test_yin_element_elem(void **state)
1074{
David Sedlák32488102019-07-15 17:44:10 +02001075 const char *data;
1076 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001077 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001078
1079 data = ELEMENT_WRAPPER_START "<yin-element value=\"true\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001080 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001081 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +02001082
David Sedlákd1144562019-08-06 12:36:14 +02001083 data = ELEMENT_WRAPPER_START "<yin-element value=\"false\">" EXT_SUBELEM "</yin-element>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001084 assert_int_equal(test_element_helper(state, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001085 assert_true(flags & LYS_YINELEM_TRUE);
Radek Iša56ca9e42020-09-08 18:42:00 +02001086 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_YINELEM);
1087 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001088
1089 data = ELEMENT_WRAPPER_START "<yin-element value=\"invalid\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001090 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001091 assert_true(flags & LYS_YINELEM_TRUE);
Radek Iša56ca9e42020-09-08 18:42:00 +02001092 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"yin-element\" element. "
1093 "Valid values are \"true\" and \"false\".", "Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001094}
1095
1096static void
1097test_yangversion_elem(void **state)
1098{
David Sedlák32488102019-07-15 17:44:10 +02001099 const char *data;
1100 uint8_t version = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001101 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001102
1103 /* valid values */
Radek Krejci96e48da2020-09-04 13:18:06 +02001104 data = ELEMENT_WRAPPER_START "<yang-version value=\"1\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001105 assert_int_equal(test_element_helper(state, data, &version, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001106 assert_true(version & LYS_VERSION_1_0);
David Sedlák32488102019-07-15 17:44:10 +02001107
David Sedlákd1144562019-08-06 12:36:14 +02001108 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.1\">" EXT_SUBELEM "</yang-version>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001109 assert_int_equal(test_element_helper(state, data, &version, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001110 assert_true(version & LYS_VERSION_1_1);
Radek Iša56ca9e42020-09-08 18:42:00 +02001111 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_VERSION);
1112 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001113
1114 /* invalid value */
1115 data = ELEMENT_WRAPPER_START "<yang-version value=\"version\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001116 assert_int_equal(test_element_helper(state, data, &version, NULL, NULL), LY_EVALID);
1117 CHECK_LOG_CTX("Invalid value \"version\" of \"value\" attribute in \"yang-version\" element. "
1118 "Valid values are \"1\" and \"1.1\".", "Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001119}
1120
1121static void
1122test_mandatory_elem(void **state)
1123{
David Sedlák32488102019-07-15 17:44:10 +02001124 const char *data;
1125 uint16_t man = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001126 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001127
1128 /* valid values */
1129 data = ELEMENT_WRAPPER_START "<mandatory value=\"true\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001130 assert_int_equal(test_element_helper(state, data, &man, NULL, NULL), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001131 assert_int_equal(man, LYS_MAND_TRUE);
1132 man = 0;
1133
David Sedlákd1144562019-08-06 12:36:14 +02001134 data = ELEMENT_WRAPPER_START "<mandatory value=\"false\">" EXT_SUBELEM "</mandatory>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001135 assert_int_equal(test_element_helper(state, data, &man, NULL, &exts), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001136 assert_int_equal(man, LYS_MAND_FALSE);
Radek Iša56ca9e42020-09-08 18:42:00 +02001137 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_MANDATORY);
1138 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001139
1140 data = ELEMENT_WRAPPER_START "<mandatory value=\"invalid\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001141 assert_int_equal(test_element_helper(state, data, &man, NULL, NULL), LY_EVALID);
1142 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"mandatory\" element. "
1143 "Valid values are \"true\" and \"false\".", "Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001144}
1145
David Sedlák8e7bda82019-07-16 17:57:50 +02001146static void
1147test_argument_elem(void **state)
1148{
David Sedlák8e7bda82019-07-16 17:57:50 +02001149 const char *data;
1150 uint16_t flags = 0;
1151 const char *arg;
1152 struct yin_argument_meta arg_meta = {&flags, &arg};
David Sedlákd1144562019-08-06 12:36:14 +02001153 struct lysp_ext_instance *exts = NULL;
1154
David Sedlák8e7bda82019-07-16 17:57:50 +02001155 /* max subelems */
1156 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001157 "<argument name=\"arg-name\">\n"
1158 " <yin-element value=\"true\" />\n"
1159 EXT_SUBELEM
1160 "</argument>"
1161 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001162 assert_int_equal(test_element_helper(state, data, &arg_meta, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001163 assert_string_equal(arg, "arg-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001164 assert_true(flags & LYS_YINELEM_TRUE);
Radek Iša56ca9e42020-09-08 18:42:00 +02001165 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_ARGUMENT);
1166 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001167 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001168 flags = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +02001169 FREE_STRING(UTEST_LYCTX, arg);
David Sedlák8e7bda82019-07-16 17:57:50 +02001170 arg = NULL;
1171
1172 /* min subelems */
1173 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001174 "<argument name=\"arg\">"
1175 "</argument>"
1176 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001177 assert_int_equal(test_element_helper(state, data, &arg_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001178 assert_string_equal(arg, "arg");
1179 assert_true(flags == 0);
Radek Iša56ca9e42020-09-08 18:42:00 +02001180 FREE_STRING(UTEST_LYCTX, arg);
David Sedlák8e7bda82019-07-16 17:57:50 +02001181}
1182
1183static void
1184test_base_elem(void **state)
1185{
David Sedlák8e7bda82019-07-16 17:57:50 +02001186 const char *data;
1187 const char **bases = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001188 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001189 struct lysp_type type = {};
1190
1191 /* as identity subelement */
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001192 data = "<identity xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
1193 " <base name=\"base-name\">\n"
1194 EXT_SUBELEM
1195 " </base>\n"
1196 "</identity>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001197 assert_int_equal(test_element_helper(state, data, &bases, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001198 assert_string_equal(*bases, "base-name");
Radek Iša56ca9e42020-09-08 18:42:00 +02001199 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_BASE);
1200 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001201 exts = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02001202 FREE_STRING(UTEST_LYCTX, *bases);
David Sedlák8e7bda82019-07-16 17:57:50 +02001203 LY_ARRAY_FREE(bases);
1204
1205 /* as type subelement */
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001206 data = "<type xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
1207 " <base name=\"base-name\">\n"
1208 EXT_SUBELEM
1209 " </base>\n"
1210 "</type>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001211 assert_int_equal(test_element_helper(state, data, &type, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001212 assert_string_equal(*type.bases, "base-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001213 assert_true(type.flags & LYS_SET_BASE);
Radek Iša56ca9e42020-09-08 18:42:00 +02001214 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_BASE);
1215 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001216 exts = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02001217 FREE_STRING(UTEST_LYCTX, *type.bases);
David Sedlák8e7bda82019-07-16 17:57:50 +02001218 LY_ARRAY_FREE(type.bases);
David Sedlák8e7bda82019-07-16 17:57:50 +02001219}
1220
1221static void
1222test_belongsto_elem(void **state)
1223{
David Sedlák8e7bda82019-07-16 17:57:50 +02001224 const char *data;
1225 struct lysp_submodule submod;
David Sedlákd1144562019-08-06 12:36:14 +02001226 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001227
Radek Iša56ca9e42020-09-08 18:42:00 +02001228 lydict_insert(UTEST_LYCTX, "module-name", 0, &YCTX->parsed_mod->mod->name);
1229
David Sedlák8e7bda82019-07-16 17:57:50 +02001230 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001231 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/>"EXT_SUBELEM "</belongs-to>"
1232 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001233 assert_int_equal(test_element_helper(state, data, &submod, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001234 assert_string_equal(submod.prefix, "pref");
Radek Iša56ca9e42020-09-08 18:42:00 +02001235 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_BELONGSTO);
1236 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001237 exts = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02001238 FREE_STRING(UTEST_LYCTX, submod.prefix);
David Sedlák8e7bda82019-07-16 17:57:50 +02001239
1240 data = ELEMENT_WRAPPER_START "<belongs-to module=\"module-name\"></belongs-to>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001241 assert_int_equal(test_element_helper(state, data, &submod, NULL, NULL), LY_EVALID);
1242 CHECK_LOG_CTX("Missing mandatory sub-element \"prefix\" of \"belongs-to\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001243}
1244
1245static void
1246test_config_elem(void **state)
1247{
David Sedlák8e7bda82019-07-16 17:57:50 +02001248 const char *data;
1249 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001250 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001251
David Sedlákd1144562019-08-06 12:36:14 +02001252 data = ELEMENT_WRAPPER_START "<config value=\"true\">" EXT_SUBELEM "</config>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001253 assert_int_equal(test_element_helper(state, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001254 assert_true(flags & LYS_CONFIG_W);
Radek Iša56ca9e42020-09-08 18:42:00 +02001255 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_CONFIG);
1256 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001257 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001258 flags = 0;
1259
1260 data = ELEMENT_WRAPPER_START "<config value=\"false\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001261 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001262 assert_true(flags & LYS_CONFIG_R);
David Sedlák8e7bda82019-07-16 17:57:50 +02001263 flags = 0;
1264
1265 data = ELEMENT_WRAPPER_START "<config value=\"invalid\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001266 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID);
1267 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"config\" element. "
1268 "Valid values are \"true\" and \"false\".", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001269}
1270
1271static void
1272test_default_elem(void **state)
1273{
David Sedlák8e7bda82019-07-16 17:57:50 +02001274 const char *data;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001275 struct lysp_qname val = {0};
David Sedlákd1144562019-08-06 12:36:14 +02001276 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001277
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001278 data = ELEMENT_WRAPPER_START "<default value=\"defaul-value\">"EXT_SUBELEM "</default>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001279 assert_int_equal(test_element_helper(state, data, &val, NULL, &exts), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001280 assert_string_equal(val.str, "defaul-value");
Radek Iša56ca9e42020-09-08 18:42:00 +02001281 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_DEFAULT);
1282 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001283 exts = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02001284 FREE_STRING(UTEST_LYCTX, val.str);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001285 val.str = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001286
1287 data = ELEMENT_WRAPPER_START "<default/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001288 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
1289 CHECK_LOG_CTX("Missing mandatory attribute value of default element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001290}
1291
1292static void
1293test_err_app_tag_elem(void **state)
1294{
David Sedlák8e7bda82019-07-16 17:57:50 +02001295 const char *data;
1296 const char *val = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001297 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001298
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001299 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 +02001300 assert_int_equal(test_element_helper(state, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001301 assert_string_equal(val, "val");
Radek Iša56ca9e42020-09-08 18:42:00 +02001302 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_ERRTAG);
1303 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001304 exts = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02001305 FREE_STRING(UTEST_LYCTX, val);
David Sedlák8e7bda82019-07-16 17:57:50 +02001306 val = NULL;
1307
1308 data = ELEMENT_WRAPPER_START "<error-app-tag/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001309 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
1310 CHECK_LOG_CTX("Missing mandatory attribute value of error-app-tag element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001311}
1312
1313static void
1314test_err_msg_elem(void **state)
1315{
David Sedlák8e7bda82019-07-16 17:57:50 +02001316 const char *data;
1317 const char *val = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001318 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001319
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001320 data = ELEMENT_WRAPPER_START "<error-message><value>val</value>"EXT_SUBELEM "</error-message>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001321 assert_int_equal(test_element_helper(state, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001322 assert_string_equal(val, "val");
Radek Iša56ca9e42020-09-08 18:42:00 +02001323 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_ERRMSG);
1324 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001325 exts = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02001326 FREE_STRING(UTEST_LYCTX, val);
David Sedlák8e7bda82019-07-16 17:57:50 +02001327
1328 data = ELEMENT_WRAPPER_START "<error-message></error-message>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001329 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
1330 CHECK_LOG_CTX("Missing mandatory sub-element \"value\" of \"error-message\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001331
David Sedlákdf2a9732019-08-07 13:23:16 +02001332 data = ELEMENT_WRAPPER_START "<error-message invalid=\"text\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001333 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
1334 CHECK_LOG_CTX("Unexpected attribute \"invalid\" of \"error-message\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001335}
1336
1337static void
1338test_fracdigits_elem(void **state)
1339{
David Sedlák8e7bda82019-07-16 17:57:50 +02001340 const char *data;
1341 struct lysp_type type = {};
1342
1343 /* valid value */
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001344 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"10\">"EXT_SUBELEM "</fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001345 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1346 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.exts[0]), LYEXT_SUBSTMT_FRACDIGITS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001347 assert_int_equal(type.fraction_digits, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001348 assert_true(type.flags & LYS_SET_FRDIGITS);
Radek Iša56ca9e42020-09-08 18:42:00 +02001349 FREE_ARRAY(UTEST_LYCTX, type.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001350
1351 /* invalid values */
1352 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"-1\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001353 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1354 CHECK_LOG_CTX("Invalid value \"-1\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001355
1356 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"02\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001357 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1358 CHECK_LOG_CTX("Invalid value \"02\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001359
1360 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"1p\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001361 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1362 CHECK_LOG_CTX("Invalid value \"1p\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001363
1364 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"19\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001365 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1366 CHECK_LOG_CTX("Invalid value \"19\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001367
1368 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"999999999999999999\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001369 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1370 CHECK_LOG_CTX("Invalid value \"999999999999999999\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001371}
1372
1373static void
1374test_iffeature_elem(void **state)
1375{
David Sedlák8e7bda82019-07-16 17:57:50 +02001376 const char *data;
1377 const char **iffeatures = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001378 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001379
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001380 data = ELEMENT_WRAPPER_START "<if-feature name=\"local-storage\">"EXT_SUBELEM "</if-feature>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001381 assert_int_equal(test_element_helper(state, data, &iffeatures, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001382 assert_string_equal(*iffeatures, "local-storage");
Radek Iša56ca9e42020-09-08 18:42:00 +02001383 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_IFFEATURE);
1384 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001385 exts = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02001386 FREE_STRING(UTEST_LYCTX, *iffeatures);
David Sedlák8e7bda82019-07-16 17:57:50 +02001387 LY_ARRAY_FREE(iffeatures);
1388 iffeatures = NULL;
1389
1390 data = ELEMENT_WRAPPER_START "<if-feature/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001391 assert_int_equal(test_element_helper(state, data, &iffeatures, NULL, NULL), LY_EVALID);
1392 CHECK_LOG_CTX("Missing mandatory attribute name of if-feature element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001393 LY_ARRAY_FREE(iffeatures);
1394 iffeatures = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001395}
1396
1397static void
1398test_length_elem(void **state)
1399{
David Sedlák8e7bda82019-07-16 17:57:50 +02001400 const char *data;
1401 struct lysp_type type = {};
1402
1403 /* max subelems */
1404 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001405 "<length value=\"length-str\">\n"
1406 " <error-message><value>err-msg</value></error-message>\n"
1407 " <error-app-tag value=\"err-app-tag\"/>\n"
1408 " <description><text>desc</text></description>\n"
1409 " <reference><text>ref</text></reference>\n"
1410 EXT_SUBELEM
1411 "</length>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001412 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001413 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1414 CHECK_LYSP_RESTR(type.length, "length-str", "desc",
1415 "err-app-tag", "err-msg", 1, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001416 assert_true(type.flags & LYS_SET_LENGTH);
Radek Iša56ca9e42020-09-08 18:42:00 +02001417 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.length->exts[0]), LYEXT_SUBSTMT_SELF);
1418 lysp_type_free(UTEST_LYCTX, &type);
David Sedlák8e7bda82019-07-16 17:57:50 +02001419 memset(&type, 0, sizeof(type));
1420
1421 /* min subelems */
1422 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001423 "<length value=\"length-str\">"
1424 "</length>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001425 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001426 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1427 CHECK_LYSP_RESTR(type.length, "length-str", NULL,
1428 NULL, NULL, 0, NULL);
1429 lysp_type_free(UTEST_LYCTX, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001430 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001431 memset(&type, 0, sizeof(type));
1432
1433 data = ELEMENT_WRAPPER_START "<length></length>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001434 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1435 CHECK_LOG_CTX("Missing mandatory attribute value of length element.", "Line number 1.");
1436 lysp_type_free(UTEST_LYCTX, &type);
David Sedlák8e7bda82019-07-16 17:57:50 +02001437 memset(&type, 0, sizeof(type));
David Sedlák8e7bda82019-07-16 17:57:50 +02001438}
1439
1440static void
1441test_modifier_elem(void **state)
1442{
David Sedlák8e7bda82019-07-16 17:57:50 +02001443 const char *data;
Radek Krejci011e4aa2020-09-04 15:22:31 +02001444 const char *pat;
David Sedlákd1144562019-08-06 12:36:14 +02001445 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001446
Radek Iša56ca9e42020-09-08 18:42:00 +02001447 assert_int_equal(LY_SUCCESS, lydict_insert(UTEST_LYCTX, "\006pattern", 8, &pat));
David Sedlákd1144562019-08-06 12:36:14 +02001448 data = ELEMENT_WRAPPER_START "<modifier value=\"invert-match\">" EXT_SUBELEM "</modifier>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001449 assert_int_equal(test_element_helper(state, data, &pat, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001450 assert_string_equal(pat, "\x015pattern");
Radek Iša56ca9e42020-09-08 18:42:00 +02001451 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_MODIFIER);
1452 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001453 exts = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02001454 FREE_STRING(UTEST_LYCTX, pat);
David Sedlák8e7bda82019-07-16 17:57:50 +02001455
Radek Iša56ca9e42020-09-08 18:42:00 +02001456 assert_int_equal(LY_SUCCESS, lydict_insert(UTEST_LYCTX, "\006pattern", 8, &pat));
David Sedlák8e7bda82019-07-16 17:57:50 +02001457 data = ELEMENT_WRAPPER_START "<modifier value=\"invert\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001458 assert_int_equal(test_element_helper(state, data, &pat, NULL, NULL), LY_EVALID);
1459 CHECK_LOG_CTX("Invalid value \"invert\" of \"value\" attribute in \"modifier\" element. "
1460 "Only valid value is \"invert-match\".", "Line number 1.");
1461 FREE_STRING(UTEST_LYCTX, pat);
David Sedlák8e7bda82019-07-16 17:57:50 +02001462}
1463
1464static void
1465test_namespace_elem(void **state)
1466{
David Sedlák8e7bda82019-07-16 17:57:50 +02001467 const char *data;
1468 const char *ns;
David Sedlákd1144562019-08-06 12:36:14 +02001469 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001470
David Sedlákd1144562019-08-06 12:36:14 +02001471 data = ELEMENT_WRAPPER_START "<namespace uri=\"ns\">" EXT_SUBELEM "</namespace>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001472 assert_int_equal(test_element_helper(state, data, &ns, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001473 assert_string_equal(ns, "ns");
Radek Iša56ca9e42020-09-08 18:42:00 +02001474 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_NAMESPACE);
1475 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001476 exts = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02001477 FREE_STRING(UTEST_LYCTX, ns);
David Sedlák8e7bda82019-07-16 17:57:50 +02001478
1479 data = ELEMENT_WRAPPER_START "<namespace/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001480 assert_int_equal(test_element_helper(state, data, &ns, NULL, NULL), LY_EVALID);
1481 CHECK_LOG_CTX("Missing mandatory attribute uri of namespace element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001482}
1483
1484static void
David Sedlák8e7bda82019-07-16 17:57:50 +02001485test_pattern_elem(void **state)
1486{
David Sedlák8e7bda82019-07-16 17:57:50 +02001487 const char *data;
1488 struct lysp_type type = {};
1489
1490 /* max subelems */
1491 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001492 "<pattern value=\"super_pattern\">\n"
1493 " <modifier value=\"invert-match\"/>\n"
1494 " <error-message><value>err-msg-value</value></error-message>\n"
1495 " <error-app-tag value=\"err-app-tag-value\"/>\n"
1496 " <description><text>&quot;pattern-desc&quot;</text></description>\n"
1497 " <reference><text>pattern-ref</text></reference>\n"
1498 EXT_SUBELEM
1499 "</pattern>"
1500 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001501 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001502 assert_true(type.flags & LYS_SET_PATTERN);
Radek Iša56ca9e42020-09-08 18:42:00 +02001503 CHECK_LYSP_RESTR(type.patterns, "\x015super_pattern", "\"pattern-desc\"",
1504 "err-app-tag-value", "err-msg-value", 1, "pattern-ref");
1505 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.patterns->exts[0]), LYEXT_SUBSTMT_SELF);
1506 lysp_type_free(UTEST_LYCTX, &type);
David Sedlák8e7bda82019-07-16 17:57:50 +02001507 memset(&type, 0, sizeof(type));
1508
1509 /* min subelems */
1510 data = ELEMENT_WRAPPER_START "<pattern value=\"pattern\"> </pattern>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001511 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1512 CHECK_LYSP_RESTR(type.patterns, "\x006pattern", NULL, NULL, NULL, 0, NULL);
1513 lysp_type_free(UTEST_LYCTX, &type);
David Sedlák8e7bda82019-07-16 17:57:50 +02001514 memset(&type, 0, sizeof(type));
David Sedlák8e7bda82019-07-16 17:57:50 +02001515}
1516
1517static void
1518test_value_position_elem(void **state)
1519{
David Sedlák8e7bda82019-07-16 17:57:50 +02001520 const char *data;
1521 struct lysp_type_enum en = {};
1522
1523 /* valid values */
David Sedlákd1144562019-08-06 12:36:14 +02001524 data = ELEMENT_WRAPPER_START "<value value=\"55\">" EXT_SUBELEM "</value>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001525 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1526 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 1, LYS_SET_VALUE, 0, NULL, NULL, 55);
1527 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(en.exts[0]), LYEXT_SUBSTMT_VALUE);
1528 FREE_ARRAY(UTEST_LYCTX, en.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001529 memset(&en, 0, sizeof(en));
1530
1531 data = ELEMENT_WRAPPER_START "<value value=\"-55\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001532 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1533 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, -55);
David Sedlák8e7bda82019-07-16 17:57:50 +02001534 memset(&en, 0, sizeof(en));
1535
1536 data = ELEMENT_WRAPPER_START "<value value=\"0\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001537 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1538 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, 0);
David Sedlák8e7bda82019-07-16 17:57:50 +02001539 memset(&en, 0, sizeof(en));
1540
1541 data = ELEMENT_WRAPPER_START "<value value=\"-0\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001542 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1543 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, 0);
David Sedlák8e7bda82019-07-16 17:57:50 +02001544 memset(&en, 0, sizeof(en));
1545
1546 /* valid positions */
David Sedlák8d552d62019-08-06 15:29:05 +02001547 data = ELEMENT_WRAPPER_START "<position value=\"55\">" EXT_SUBELEM "</position>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001548 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1549 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 1, LYS_SET_VALUE, 0, NULL, NULL, 55);
1550 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(en.exts[0]), LYEXT_SUBSTMT_POSITION);
1551 FREE_ARRAY(UTEST_LYCTX, en.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001552 memset(&en, 0, sizeof(en));
1553
1554 data = ELEMENT_WRAPPER_START "<position value=\"0\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001555 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1556 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, 0);
David Sedlák8e7bda82019-07-16 17:57:50 +02001557 memset(&en, 0, sizeof(en));
1558
1559 /* invalid values */
1560 data = ELEMENT_WRAPPER_START "<value value=\"99999999999999999999999\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001561 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1562 CHECK_LOG_CTX("Invalid value \"99999999999999999999999\" of \"value\" attribute in \"value\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001563
1564 data = ELEMENT_WRAPPER_START "<value value=\"1k\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001565 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1566 CHECK_LOG_CTX("Invalid value \"1k\" of \"value\" attribute in \"value\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001567
David Sedlák69f01612019-07-17 11:41:08 +02001568 data = ELEMENT_WRAPPER_START "<value value=\"\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001569 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1570 CHECK_LOG_CTX("Invalid value \"\" of \"value\" attribute in \"value\" element.", "Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001571
David Sedlák8e7bda82019-07-16 17:57:50 +02001572 /*invalid positions */
1573 data = ELEMENT_WRAPPER_START "<position value=\"-5\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001574 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1575 CHECK_LOG_CTX("Invalid value \"-5\" of \"value\" attribute in \"position\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001576
1577 data = ELEMENT_WRAPPER_START "<position value=\"-0\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001578 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1579 CHECK_LOG_CTX("Invalid value \"-0\" of \"value\" attribute in \"position\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001580
1581 data = ELEMENT_WRAPPER_START "<position value=\"99999999999999999999\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001582 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1583 CHECK_LOG_CTX("Invalid value \"99999999999999999999\" of \"value\" attribute in \"position\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001584
David Sedlák69f01612019-07-17 11:41:08 +02001585 data = ELEMENT_WRAPPER_START "<position value=\"\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001586 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1587 CHECK_LOG_CTX("Invalid value \"\" of \"value\" attribute in \"position\" element.", "Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001588}
1589
1590static void
1591test_prefix_elem(void **state)
1592{
David Sedlák69f01612019-07-17 11:41:08 +02001593 const char *data;
1594 const char *value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001595 struct lysp_ext_instance *exts = NULL;
1596
1597 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\">" EXT_SUBELEM "</prefix>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001598 assert_int_equal(test_element_helper(state, data, &value, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001599 assert_string_equal(value, "pref");
Radek Iša56ca9e42020-09-08 18:42:00 +02001600 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_PREFIX);
1601 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlákd1144562019-08-06 12:36:14 +02001602 exts = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02001603 FREE_STRING(UTEST_LYCTX, value);
David Sedlák69f01612019-07-17 11:41:08 +02001604
1605 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001606 assert_int_equal(test_element_helper(state, data, &value, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001607 assert_string_equal(value, "pref");
Radek Iša56ca9e42020-09-08 18:42:00 +02001608 FREE_STRING(UTEST_LYCTX, value);
David Sedlák69f01612019-07-17 11:41:08 +02001609}
1610
1611static void
1612test_range_elem(void **state)
1613{
David Sedlák69f01612019-07-17 11:41:08 +02001614 const char *data;
1615 struct lysp_type type = {};
1616
1617 /* max subelems */
1618 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001619 "<range value=\"range-str\">\n"
1620 " <error-message><value>err-msg</value></error-message>\n"
1621 " <error-app-tag value=\"err-app-tag\" />\n"
1622 " <description><text>desc</text></description>\n"
1623 " <reference><text>ref</text></reference>\n"
1624 EXT_SUBELEM
1625 "</range>"
1626 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001627 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1628 CHECK_LYSP_RESTR(type.range, "range-str", "desc",
1629 "err-app-tag", "err-msg", 1, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001630 assert_true(type.flags & LYS_SET_RANGE);
Radek Iša56ca9e42020-09-08 18:42:00 +02001631 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.range->exts[0]), LYEXT_SUBSTMT_SELF);
1632 lysp_type_free(UTEST_LYCTX, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001633 memset(&type, 0, sizeof(type));
1634
1635 /* min subelems */
1636 data = ELEMENT_WRAPPER_START "<range value=\"range-str\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001637 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1638 CHECK_LYSP_RESTR(type.range, "range-str", NULL,
1639 NULL, NULL, 0, NULL);
1640 lysp_type_free(UTEST_LYCTX, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001641 memset(&type, 0, sizeof(type));
David Sedlák69f01612019-07-17 11:41:08 +02001642}
1643
1644static void
1645test_reqinstance_elem(void **state)
1646{
David Sedlák69f01612019-07-17 11:41:08 +02001647 const char *data;
1648 struct lysp_type type = {};
1649
David Sedlákd1144562019-08-06 12:36:14 +02001650 data = ELEMENT_WRAPPER_START "<require-instance value=\"true\">" EXT_SUBELEM "</require-instance>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001651 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001652 assert_int_equal(type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001653 assert_true(type.flags & LYS_SET_REQINST);
Radek Iša56ca9e42020-09-08 18:42:00 +02001654 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.exts[0]), LYEXT_SUBSTMT_REQINSTANCE);
1655 lysp_type_free(UTEST_LYCTX, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001656 memset(&type, 0, sizeof(type));
1657
1658 data = ELEMENT_WRAPPER_START "<require-instance value=\"false\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001659 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001660 assert_int_equal(type.require_instance, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001661 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001662 memset(&type, 0, sizeof(type));
1663
1664 data = ELEMENT_WRAPPER_START "<require-instance value=\"invalid\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001665 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
David Sedlák69f01612019-07-17 11:41:08 +02001666 memset(&type, 0, sizeof(type));
Radek Iša56ca9e42020-09-08 18:42:00 +02001667 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"require-instance\" element. "
1668 "Valid values are \"true\" and \"false\".", "Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001669}
1670
1671static void
1672test_revision_date_elem(void **state)
1673{
David Sedlák69f01612019-07-17 11:41:08 +02001674 const char *data;
1675 char rev[LY_REV_SIZE];
David Sedlákd1144562019-08-06 12:36:14 +02001676 struct lysp_ext_instance *exts = NULL;
1677
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001678 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 +02001679 assert_int_equal(test_element_helper(state, data, rev, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001680 assert_string_equal(rev, "2000-01-01");
Radek Iša56ca9e42020-09-08 18:42:00 +02001681 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_REVISIONDATE);
1682 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedlák69f01612019-07-17 11:41:08 +02001683
1684 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001685 assert_int_equal(test_element_helper(state, data, rev, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001686 assert_string_equal(rev, "2000-01-01");
1687
1688 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-50-05\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001689 assert_int_equal(test_element_helper(state, data, rev, NULL, NULL), LY_EVALID);
1690 CHECK_LOG_CTX("Invalid value \"2000-50-05\" of \"revision-date\".", "Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001691}
1692
1693static void
1694test_unique_elem(void **state)
1695{
David Sedlák69f01612019-07-17 11:41:08 +02001696 const char *data;
1697 const char **values = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001698 struct lysp_ext_instance *exts = NULL;
1699
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001700 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\">"EXT_SUBELEM "</unique>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001701 assert_int_equal(test_element_helper(state, data, &values, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001702 assert_string_equal(*values, "tag");
Radek Iša56ca9e42020-09-08 18:42:00 +02001703 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_UNIQUE);
1704 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
1705 FREE_STRING(UTEST_LYCTX, *values);
David Sedlákd1144562019-08-06 12:36:14 +02001706 LY_ARRAY_FREE(values);
1707 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001708
1709 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001710 assert_int_equal(test_element_helper(state, data, &values, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001711 assert_string_equal(*values, "tag");
Radek Iša56ca9e42020-09-08 18:42:00 +02001712 FREE_STRING(UTEST_LYCTX, *values);
David Sedlák69f01612019-07-17 11:41:08 +02001713 LY_ARRAY_FREE(values);
David Sedlákd1144562019-08-06 12:36:14 +02001714 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001715}
1716
1717static void
1718test_units_elem(void **state)
1719{
David Sedlák69f01612019-07-17 11:41:08 +02001720 const char *data;
1721 const char *values = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001722 struct lysp_ext_instance *exts = NULL;
1723
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001724 data = ELEMENT_WRAPPER_START "<units name=\"name\">"EXT_SUBELEM "</units>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001725 assert_int_equal(test_element_helper(state, data, &values, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001726 assert_string_equal(values, "name");
Radek Iša56ca9e42020-09-08 18:42:00 +02001727 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_UNITS);
1728 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
1729 FREE_STRING(UTEST_LYCTX, values);
David Sedlákd1144562019-08-06 12:36:14 +02001730 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001731
1732 data = ELEMENT_WRAPPER_START "<units name=\"name\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001733 assert_int_equal(test_element_helper(state, data, &values, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001734 assert_string_equal(values, "name");
Radek Iša56ca9e42020-09-08 18:42:00 +02001735 FREE_STRING(UTEST_LYCTX, values);
David Sedlákd1144562019-08-06 12:36:14 +02001736 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001737}
1738
1739static void
1740test_when_elem(void **state)
1741{
David Sedlák69f01612019-07-17 11:41:08 +02001742 const char *data;
1743 struct lysp_when *when = NULL;
1744
1745 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001746 "<when condition=\"cond\">\n"
1747 " <description><text>desc</text></description>\n"
1748 " <reference><text>ref</text></reference>\n"
1749 EXT_SUBELEM
1750 "</when>"
1751 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001752 assert_int_equal(test_element_helper(state, data, &when, NULL, NULL), LY_SUCCESS);
1753 CHECK_LYSP_WHEN(when, "cond", "desc", 1, "ref");
1754 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(when->exts[0]), LYEXT_SUBSTMT_SELF);
1755 lysp_when_free(UTEST_LYCTX, when);
David Sedlák69f01612019-07-17 11:41:08 +02001756 free(when);
1757 when = NULL;
1758
1759 data = ELEMENT_WRAPPER_START "<when condition=\"cond\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001760 assert_int_equal(test_element_helper(state, data, &when, NULL, NULL), LY_SUCCESS);
1761 CHECK_LYSP_WHEN(when, "cond", NULL, 0, NULL);
1762 lysp_when_free(UTEST_LYCTX, when);
David Sedlák69f01612019-07-17 11:41:08 +02001763 free(when);
1764 when = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001765}
1766
1767static void
1768test_yin_text_value_elem(void **state)
1769{
David Sedlák69f01612019-07-17 11:41:08 +02001770 const char *data;
1771 const char *val;
1772
1773 data = ELEMENT_WRAPPER_START "<text>text</text>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001774 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001775 assert_string_equal(val, "text");
Radek Iša56ca9e42020-09-08 18:42:00 +02001776 FREE_STRING(UTEST_LYCTX, val);
David Sedlák69f01612019-07-17 11:41:08 +02001777
1778 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 +02001779 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001780 assert_string_equal(val, "text");
Radek Iša56ca9e42020-09-08 18:42:00 +02001781 FREE_STRING(UTEST_LYCTX, val);
David Sedlák69f01612019-07-17 11:41:08 +02001782
1783 data = ELEMENT_WRAPPER_START "<text></text>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001784 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001785 assert_string_equal("", val);
Radek Iša56ca9e42020-09-08 18:42:00 +02001786 FREE_STRING(UTEST_LYCTX, val);
David Sedlák8e7bda82019-07-16 17:57:50 +02001787}
David Sedlák32488102019-07-15 17:44:10 +02001788
David Sedlák374d2b32019-07-17 15:06:55 +02001789static void
1790test_type_elem(void **state)
1791{
David Sedlák374d2b32019-07-17 15:06:55 +02001792 const char *data;
1793 struct lysp_type type = {};
1794
1795 /* max subelems */
1796 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001797 "<type name=\"type-name\">\n"
1798 " <base name=\"base-name\"/>\n"
1799 " <bit name=\"bit\"/>\n"
1800 " <enum name=\"enum\"/>\n"
1801 " <fraction-digits value=\"2\"/>\n"
1802 " <length value=\"length\"/>\n"
1803 " <path value=\"/path\"/>\n"
1804 " <pattern value=\"pattern\"/>\n"
1805 " <range value=\"range\" />\n"
1806 " <require-instance value=\"true\"/>\n"
1807 " <type name=\"sub-type-name\"/>\n"
1808 EXT_SUBELEM
1809 "</type>"
1810 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001811 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák374d2b32019-07-17 15:06:55 +02001812 assert_string_equal(type.name, "type-name");
1813 assert_string_equal(*type.bases, "base-name");
1814 assert_string_equal(type.bits->name, "bit");
1815 assert_string_equal(type.enums->name, "enum");
1816 assert_int_equal(type.fraction_digits, 2);
Radek Iša56ca9e42020-09-08 18:42:00 +02001817 CHECK_LYSP_RESTR(type.length, "length", NULL,
1818 NULL, NULL, 0, NULL);
Michal Vaskocb8c6d42020-10-16 11:58:30 +02001819 assert_string_equal(type.path->expr, "/path");
Radek Iša56ca9e42020-09-08 18:42:00 +02001820 CHECK_LYSP_RESTR(type.patterns, "\006pattern", NULL,
1821 NULL, NULL, 0, NULL);
1822 CHECK_LYSP_RESTR(type.range, "range", NULL,
1823 NULL, NULL, 0, NULL);
David Sedlák374d2b32019-07-17 15:06:55 +02001824 assert_int_equal(type.require_instance, 1);
1825 assert_string_equal(type.types->name, "sub-type-name");
Radek Iša56ca9e42020-09-08 18:42:00 +02001826 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.exts[0]), LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001827 assert_true(type.flags & LYS_SET_BASE);
1828 assert_true(type.flags & LYS_SET_BIT);
1829 assert_true(type.flags & LYS_SET_ENUM);
1830 assert_true(type.flags & LYS_SET_FRDIGITS);
1831 assert_true(type.flags & LYS_SET_LENGTH);
1832 assert_true(type.flags & LYS_SET_PATH);
1833 assert_true(type.flags & LYS_SET_PATTERN);
1834 assert_true(type.flags & LYS_SET_RANGE);
1835 assert_true(type.flags & LYS_SET_REQINST);
1836 assert_true(type.flags & LYS_SET_TYPE);
Radek Iša56ca9e42020-09-08 18:42:00 +02001837 lysp_type_free(UTEST_LYCTX, &type);
David Sedlák374d2b32019-07-17 15:06:55 +02001838 memset(&type, 0, sizeof(type));
1839
1840 /* min subelems */
1841 data = ELEMENT_WRAPPER_START "<type name=\"type-name\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001842 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1843 lysp_type_free(UTEST_LYCTX, &type);
David Sedlák374d2b32019-07-17 15:06:55 +02001844 memset(&type, 0, sizeof(type));
David Sedlák374d2b32019-07-17 15:06:55 +02001845}
1846
David Sedlák1af868e2019-07-17 17:03:14 +02001847static void
1848test_max_elems_elem(void **state)
1849{
David Sedlák1af868e2019-07-17 17:03:14 +02001850 const char *data;
1851 struct lysp_node_list list = {};
1852 struct lysp_node_leaflist llist = {};
1853 struct lysp_refine refine = {};
1854
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001855 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 +02001856 assert_int_equal(test_element_helper(state, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02001857 assert_int_equal(refine.max, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001858 assert_true(refine.flags & LYS_SET_MAX);
Radek Iša56ca9e42020-09-08 18:42:00 +02001859 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(refine.exts[0]), LYEXT_SUBSTMT_MAX);
1860 FREE_ARRAY(UTEST_LYCTX, refine.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02001861
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001862 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 +02001863 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02001864 assert_int_equal(list.max, 5);
Radek Iša56ca9e42020-09-08 18:42:00 +02001865 CHECK_LYSP_NODE(&list, NULL, 1, LYS_SET_MAX, 0, NULL, 0, LYS_UNKNOWN, NULL, NULL, 0);
1866 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(list.exts[0]), LYEXT_SUBSTMT_MAX);
1867 FREE_ARRAY(UTEST_LYCTX, list.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02001868
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001869 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 +02001870 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02001871 assert_int_equal(llist.max, 85);
Radek Iša56ca9e42020-09-08 18:42:00 +02001872 CHECK_LYSP_NODE(&llist, NULL, 1, LYS_SET_MAX, 0, NULL, 0, LYS_UNKNOWN, NULL, NULL, 0);
1873 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(llist.exts[0]), LYEXT_SUBSTMT_MAX);
1874 FREE_ARRAY(UTEST_LYCTX, llist.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02001875
1876 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"10\"/> </refine>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001877 assert_int_equal(test_element_helper(state, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02001878 assert_int_equal(refine.max, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001879 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001880
1881 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"0\"/> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001882 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID);
1883 CHECK_LOG_CTX("Invalid value \"0\" of \"value\" attribute in \"max-elements\" element.", "Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02001884
1885 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"-10\"/> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001886 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID);
1887 CHECK_LOG_CTX("Invalid value \"-10\" of \"value\" attribute in \"max-elements\" element.", "Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02001888
1889 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"k\"/> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001890 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID);
1891 CHECK_LOG_CTX("Invalid value \"k\" of \"value\" attribute in \"max-elements\" element.", "Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02001892
1893 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"u12\"/> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001894 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID);
1895 CHECK_LOG_CTX("Invalid value \"u12\" of \"value\" attribute in \"max-elements\" element.", "Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02001896}
1897
David Sedlák09e18c92019-07-18 11:17:11 +02001898static void
1899test_min_elems_elem(void **state)
1900{
David Sedlák09e18c92019-07-18 11:17:11 +02001901 const char *data;
1902 struct lysp_node_list list = {};
1903 struct lysp_node_leaflist llist = {};
1904 struct lysp_refine refine = {};
1905
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001906 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 +02001907 assert_int_equal(test_element_helper(state, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák09e18c92019-07-18 11:17:11 +02001908 assert_int_equal(refine.min, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001909 assert_true(refine.flags & LYS_SET_MIN);
Radek Iša56ca9e42020-09-08 18:42:00 +02001910 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(refine.exts[0]), LYEXT_SUBSTMT_MIN);
1911 FREE_ARRAY(UTEST_LYCTX, refine.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02001912
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001913 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 +02001914 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_SUCCESS);
David Sedlák09e18c92019-07-18 11:17:11 +02001915 assert_int_equal(list.min, 41);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001916 assert_true(list.flags & LYS_SET_MIN);
Radek Iša56ca9e42020-09-08 18:42:00 +02001917 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(list.exts[0]), LYEXT_SUBSTMT_MIN);
1918 FREE_ARRAY(UTEST_LYCTX, list.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02001919
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001920 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 +02001921 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_SUCCESS);
David Sedlák09e18c92019-07-18 11:17:11 +02001922 assert_int_equal(llist.min, 50);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001923 assert_true(llist.flags & LYS_SET_MIN);
Radek Iša56ca9e42020-09-08 18:42:00 +02001924 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(llist.exts[0]), LYEXT_SUBSTMT_MIN);
1925 FREE_ARRAY(UTEST_LYCTX, llist.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02001926
1927 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 +02001928 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID);
1929 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 +02001930
1931 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 +02001932 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID);
1933 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 +02001934
1935 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 +02001936 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID);
1937 CHECK_LOG_CTX("Invalid value \"5k\" of \"value\" attribute in \"min-elements\" element.", "Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02001938
1939 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 +02001940 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID);
1941 CHECK_LOG_CTX("Invalid value \"05\" of \"value\" attribute in \"min-elements\" element.", "Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02001942}
1943
David Sedláka2dad212019-07-18 12:45:19 +02001944static void
1945test_ordby_elem(void **state)
1946{
David Sedláka2dad212019-07-18 12:45:19 +02001947 const char *data;
1948 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001949 struct lysp_ext_instance *exts = NULL;
David Sedláka2dad212019-07-18 12:45:19 +02001950
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001951 data = ELEMENT_WRAPPER_START "<ordered-by value=\"system\">"EXT_SUBELEM "</ordered-by>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001952 assert_int_equal(test_element_helper(state, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001953 assert_true(flags & LYS_ORDBY_SYSTEM);
Radek Iša56ca9e42020-09-08 18:42:00 +02001954 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_ORDEREDBY);
1955 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
David Sedláka2dad212019-07-18 12:45:19 +02001956
1957 data = ELEMENT_WRAPPER_START "<ordered-by value=\"user\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001958 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001959 assert_true(flags & LYS_ORDBY_USER);
David Sedláka2dad212019-07-18 12:45:19 +02001960
1961 data = ELEMENT_WRAPPER_START "<ordered-by value=\"inv\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001962 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID);
1963 CHECK_LOG_CTX("Invalid value \"inv\" of \"value\" attribute in \"ordered-by\" element. "
1964 "Valid values are \"system\" and \"user\".", "Line number 1.");
David Sedláka2dad212019-07-18 12:45:19 +02001965}
1966
David Sedlák8a83bbb2019-07-18 14:46:00 +02001967static void
1968test_any_elem(void **state)
1969{
David Sedlák8a83bbb2019-07-18 14:46:00 +02001970 const char *data;
1971 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02001972 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák8a83bbb2019-07-18 14:46:00 +02001973 struct lysp_node_anydata *parsed = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02001974 uint16_t flags;
David Sedlák8a83bbb2019-07-18 14:46:00 +02001975
1976 /* anyxml max subelems */
1977 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001978 "<anyxml name=\"any-name\">\n"
1979 " <config value=\"true\" />\n"
1980 " <description><text>desc</text></description>\n"
1981 " <if-feature name=\"feature\" />\n"
1982 " <mandatory value=\"true\" />\n"
1983 " <must condition=\"must-cond\" />\n"
1984 " <reference><text>ref</text></reference>\n"
1985 " <status value=\"deprecated\"/>\n"
1986 " <when condition=\"when-cond\"/>\n"
1987 EXT_SUBELEM
1988 "</anyxml>"
1989 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001990 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001991 parsed = (struct lysp_node_anydata *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02001992 flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_DEPRC;
1993 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
1994 "any-name", 0, LYS_ANYXML, 0, "ref", 1);
1995 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001996 assert_string_equal(parsed->iffeatures[0].str, "feature");
Radek Iša56ca9e42020-09-08 18:42:00 +02001997 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LYEXT_SUBSTMT_SELF);
1998 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001999 siblings = NULL;
2000
2001 /* anydata max subelems */
2002 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002003 "<anydata name=\"any-name\">\n"
2004 " <config value=\"true\" />\n"
2005 " <description><text>desc</text></description>\n"
2006 " <if-feature name=\"feature\" />\n"
2007 " <mandatory value=\"true\" />\n"
2008 " <must condition=\"must-cond\" />\n"
2009 " <reference><text>ref</text></reference>\n"
2010 " <status value=\"deprecated\"/>\n"
2011 " <when condition=\"when-cond\"/>\n"
2012 EXT_SUBELEM
2013 "</anydata>"
2014 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002015 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002016 parsed = (struct lysp_node_anydata *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002017 flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_DEPRC;
2018 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2019 "any-name", 0, LYS_ANYDATA, 0, "ref", 1);
2020 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002021 assert_string_equal(parsed->iffeatures[0].str, "feature");
Radek Iša56ca9e42020-09-08 18:42:00 +02002022 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LYEXT_SUBSTMT_SELF);
2023 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002024 siblings = NULL;
2025
2026 /* min subelems */
2027 node_meta.parent = (void *)0x10;
2028 data = ELEMENT_WRAPPER_START "<anydata name=\"any-name\"> </anydata>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002029 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002030 parsed = (struct lysp_node_anydata *)siblings;
2031 assert_ptr_equal(parsed->parent, node_meta.parent);
Radek Iša56ca9e42020-09-08 18:42:00 +02002032 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2033 "any-name", 0, LYS_ANYDATA, 1, NULL, 0);
2034 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002035}
2036
David Sedlák203ca3a2019-07-18 15:26:25 +02002037static void
2038test_leaf_elem(void **state)
2039{
David Sedlák203ca3a2019-07-18 15:26:25 +02002040 const char *data;
2041 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002042 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák203ca3a2019-07-18 15:26:25 +02002043 struct lysp_node_leaf *parsed = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02002044 uint16_t flags;
David Sedlák203ca3a2019-07-18 15:26:25 +02002045
2046 /* max elements */
2047 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002048 "<leaf name=\"leaf\">\n"
2049 " <config value=\"true\" />\n"
2050 " <default value=\"def-val\"/>\n"
2051 " <description><text>desc</text></description>\n"
2052 " <if-feature name=\"feature\" />\n"
2053 " <mandatory value=\"true\" />\n"
2054 " <must condition=\"must-cond\" />\n"
2055 " <reference><text>ref</text></reference>\n"
2056 " <status value=\"deprecated\"/>\n"
2057 " <type name=\"type\"/>\n"
2058 " <units name=\"uni\"/>\n"
2059 " <when condition=\"when-cond\"/>\n"
2060 EXT_SUBELEM
2061 "</leaf>"
David Sedlák203ca3a2019-07-18 15:26:25 +02002062 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002063 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák203ca3a2019-07-18 15:26:25 +02002064 parsed = (struct lysp_node_leaf *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002065 flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_DEPRC;
2066 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2067 "leaf", 0, LYS_LEAF, 0, "ref", 1);
2068 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002069 assert_string_equal(parsed->iffeatures[0].str, "feature");
Radek Iša56ca9e42020-09-08 18:42:00 +02002070 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LYEXT_SUBSTMT_SELF);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002071 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlák203ca3a2019-07-18 15:26:25 +02002072 assert_string_equal(parsed->type.name, "type");
2073 assert_string_equal(parsed->units, "uni");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002074 assert_string_equal(parsed->dflt.str, "def-val");
Radek Iša56ca9e42020-09-08 18:42:00 +02002075 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlák203ca3a2019-07-18 15:26:25 +02002076 siblings = NULL;
2077
2078 /* min elements */
2079 data = ELEMENT_WRAPPER_START "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002080 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák203ca3a2019-07-18 15:26:25 +02002081 parsed = (struct lysp_node_leaf *)siblings;
2082 assert_string_equal(parsed->name, "leaf");
2083 assert_string_equal(parsed->type.name, "type");
Radek Iša56ca9e42020-09-08 18:42:00 +02002084 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlák203ca3a2019-07-18 15:26:25 +02002085 siblings = NULL;
David Sedlák203ca3a2019-07-18 15:26:25 +02002086}
2087
David Sedlákc3da3ef2019-07-19 12:56:08 +02002088static void
2089test_leaf_list_elem(void **state)
2090{
David Sedlákc3da3ef2019-07-19 12:56:08 +02002091 const char *data;
2092 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002093 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlákc3da3ef2019-07-19 12:56:08 +02002094 struct lysp_node_leaflist *parsed = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02002095 uint16_t flags;
David Sedlákc3da3ef2019-07-19 12:56:08 +02002096
2097 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002098 "<leaf-list name=\"llist\">\n"
2099 " <config value=\"true\" />\n"
2100 " <default value=\"def-val0\"/>\n"
2101 " <default value=\"def-val1\"/>\n"
2102 " <description><text>desc</text></description>\n"
2103 " <if-feature name=\"feature\"/>\n"
2104 " <max-elements value=\"5\"/>\n"
2105 " <must condition=\"must-cond\"/>\n"
2106 " <ordered-by value=\"user\" />\n"
2107 " <reference><text>ref</text></reference>\n"
2108 " <status value=\"current\"/>\n"
2109 " <type name=\"type\"/>\n"
2110 " <units name=\"uni\"/>\n"
2111 " <when condition=\"when-cond\"/>\n"
2112 EXT_SUBELEM
2113 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002114 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002115 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002116 parsed = (struct lysp_node_leaflist *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002117 flags = LYS_CONFIG_W | LYS_ORDBY_USER | LYS_STATUS_CURR | LYS_SET_MAX;
2118 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2119 "llist", 0, LYS_LEAFLIST, 0, "ref", 1);
2120 CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002121 assert_string_equal(parsed->dflts[0].str, "def-val0");
2122 assert_string_equal(parsed->dflts[1].str, "def-val1");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002123 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002124 assert_int_equal(parsed->max, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002125 assert_string_equal(parsed->type.name, "type");
2126 assert_string_equal(parsed->units, "uni");
Radek Iša56ca9e42020-09-08 18:42:00 +02002127 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
2128 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LYEXT_SUBSTMT_SELF);
2129 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002130 siblings = NULL;
2131
2132 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002133 "<leaf-list name=\"llist\">\n"
2134 " <config value=\"true\" />\n"
2135 " <description><text>desc</text></description>\n"
2136 " <if-feature name=\"feature\"/>\n"
2137 " <min-elements value=\"5\"/>\n"
2138 " <must condition=\"must-cond\"/>\n"
2139 " <ordered-by value=\"user\" />\n"
2140 " <reference><text>ref</text></reference>\n"
2141 " <status value=\"current\"/>\n"
2142 " <type name=\"type\"/>\n"
2143 " <units name=\"uni\"/>\n"
2144 " <when condition=\"when-cond\"/>\n"
2145 EXT_SUBELEM
2146 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002147 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002148 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002149 parsed = (struct lysp_node_leaflist *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002150 flags = LYS_CONFIG_W | LYS_ORDBY_USER | LYS_STATUS_CURR | LYS_SET_MIN;
2151 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2152 "llist", 0, LYS_LEAFLIST, 0, "ref", 1);
2153 CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL);
2154 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002155 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002156 assert_int_equal(parsed->min, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002157 assert_string_equal(parsed->type.name, "type");
2158 assert_string_equal(parsed->units, "uni");
Radek Iša56ca9e42020-09-08 18:42:00 +02002159 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LYEXT_SUBSTMT_SELF);
2160 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002161 siblings = NULL;
2162
2163 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002164 "<leaf-list name=\"llist\">\n"
2165 " <config value=\"true\" />\n"
2166 " <description><text>desc</text></description>\n"
2167 " <if-feature name=\"feature\"/>\n"
2168 " <max-elements value=\"15\"/>\n"
2169 " <min-elements value=\"5\"/>\n"
2170 " <must condition=\"must-cond\"/>\n"
2171 " <ordered-by value=\"user\" />\n"
2172 " <reference><text>ref</text></reference>\n"
2173 " <status value=\"current\"/>\n"
2174 " <type name=\"type\"/>\n"
2175 " <units name=\"uni\"/>\n"
2176 " <when condition=\"when-cond\"/>\n"
2177 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002178 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002179 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002180 parsed = (struct lysp_node_leaflist *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002181 flags = LYS_CONFIG_W | LYS_ORDBY_USER | LYS_STATUS_CURR | LYS_SET_MIN | LYS_SET_MAX;
2182 CHECK_LYSP_NODE(parsed, "desc", 0, flags, 1,
2183 "llist", 0, LYS_LEAFLIST, 0, "ref", 1);
2184 CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL);
2185 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002186 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002187 assert_int_equal(parsed->min, 5);
2188 assert_int_equal(parsed->max, 15);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002189 assert_string_equal(parsed->type.name, "type");
2190 assert_string_equal(parsed->units, "uni");
Radek Iša56ca9e42020-09-08 18:42:00 +02002191 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002192 siblings = NULL;
2193
2194 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002195 "<leaf-list name=\"llist\">\n"
2196 " <type name=\"type\"/>\n"
2197 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002198 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002199 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002200 parsed = (struct lysp_node_leaflist *)siblings;
2201 assert_string_equal(parsed->name, "llist");
2202 assert_string_equal(parsed->type.name, "type");
Radek Iša56ca9e42020-09-08 18:42:00 +02002203 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002204 siblings = NULL;
2205
2206 /* invalid combinations */
2207 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002208 "<leaf-list name=\"llist\">\n"
2209 " <max-elements value=\"5\"/>\n"
2210 " <min-elements value=\"15\"/>\n"
2211 " <type name=\"type\"/>"
2212 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002213 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002214 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_EVALID);
2215 CHECK_LOG_CTX("Invalid combination of min-elements and max-elements: min value 15 is bigger than the max value 5.", "Line number 4.");
2216 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002217 siblings = NULL;
2218
2219 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002220 "<leaf-list name=\"llist\">\n"
2221 " <default value=\"def-val1\"/>\n"
2222 " <min-elements value=\"15\"/>\n"
2223 " <type name=\"type\"/>\n"
2224 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002225 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002226 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_EVALID);
2227 CHECK_LOG_CTX("Invalid combination of sub-elemnts \"min-elements\" and \"default\" in \"leaf-list\" element.", "Line number 5.");
2228 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002229 siblings = NULL;
2230
2231 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002232 "<leaf-list name=\"llist\">"
2233 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002234 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002235 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_EVALID);
2236 CHECK_LOG_CTX("Missing mandatory sub-element \"type\" of \"leaf-list\" element.", "Line number 1.");
2237 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002238 siblings = NULL;
David Sedlákc3da3ef2019-07-19 12:56:08 +02002239}
2240
David Sedlákcb39f642019-07-19 13:19:55 +02002241static void
2242test_presence_elem(void **state)
2243{
David Sedlákcb39f642019-07-19 13:19:55 +02002244 const char *data;
2245 const char *val;
David Sedlákd1144562019-08-06 12:36:14 +02002246 struct lysp_ext_instance *exts = NULL;
2247
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002248 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\">"EXT_SUBELEM "</presence>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002249 assert_int_equal(test_element_helper(state, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02002250 assert_string_equal(val, "presence-val");
Radek Iša56ca9e42020-09-08 18:42:00 +02002251 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_PRESENCE);
2252 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
2253 FREE_STRING(UTEST_LYCTX, val);
David Sedlákcb39f642019-07-19 13:19:55 +02002254
2255 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002256 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlákcb39f642019-07-19 13:19:55 +02002257 assert_string_equal(val, "presence-val");
Radek Iša56ca9e42020-09-08 18:42:00 +02002258 FREE_STRING(UTEST_LYCTX, val);
David Sedlákcb39f642019-07-19 13:19:55 +02002259
2260 data = ELEMENT_WRAPPER_START "<presence/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002261 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
2262 CHECK_LOG_CTX("Missing mandatory attribute value of presence element.", "Line number 1.");
David Sedlákcb39f642019-07-19 13:19:55 +02002263}
2264
David Sedlák12470a82019-07-19 13:44:36 +02002265static void
2266test_key_elem(void **state)
2267{
David Sedlák12470a82019-07-19 13:44:36 +02002268 const char *data;
2269 const char *val;
David Sedlákd1144562019-08-06 12:36:14 +02002270 struct lysp_ext_instance *exts = NULL;
2271
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002272 data = ELEMENT_WRAPPER_START "<key value=\"key-value\">"EXT_SUBELEM "</key>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002273 assert_int_equal(test_element_helper(state, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02002274 assert_string_equal(val, "key-value");
Radek Iša56ca9e42020-09-08 18:42:00 +02002275 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(exts[0]), LYEXT_SUBSTMT_KEY);
2276 FREE_ARRAY(UTEST_LYCTX, exts, lysp_ext_instance_free);
2277 FREE_STRING(UTEST_LYCTX, val);
David Sedlák12470a82019-07-19 13:44:36 +02002278
2279 data = ELEMENT_WRAPPER_START "<key value=\"key-value\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002280 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák12470a82019-07-19 13:44:36 +02002281 assert_string_equal(val, "key-value");
Radek Iša56ca9e42020-09-08 18:42:00 +02002282 FREE_STRING(UTEST_LYCTX, val);
David Sedlák12470a82019-07-19 13:44:36 +02002283
2284 data = ELEMENT_WRAPPER_START "<key/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002285 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
2286 CHECK_LOG_CTX("Missing mandatory attribute value of key element.", "Line number 1.");
David Sedlák12470a82019-07-19 13:44:36 +02002287}
2288
David Sedlák04e17b22019-07-19 15:29:48 +02002289static void
2290test_typedef_elem(void **state)
2291{
David Sedlák04e17b22019-07-19 15:29:48 +02002292 const char *data;
2293 struct lysp_tpdf *tpdfs = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002294 struct tree_node_meta typdef_meta = {NULL, (struct lysp_node **)&tpdfs};
David Sedlák04e17b22019-07-19 15:29:48 +02002295
2296 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002297 "<typedef name=\"tpdf-name\">\n"
2298 " <default value=\"def-val\"/>\n"
2299 " <description><text>desc-text</text></description>\n"
2300 " <reference><text>ref-text</text></reference>\n"
2301 " <status value=\"current\"/>\n"
2302 " <type name=\"type\"/>\n"
2303 " <units name=\"uni\"/>\n"
2304 EXT_SUBELEM
2305 "</typedef>"
2306 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002307 assert_int_equal(test_element_helper(state, data, &typdef_meta, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002308 assert_string_equal(tpdfs[0].dflt.str, "def-val");
David Sedlák04e17b22019-07-19 15:29:48 +02002309 assert_string_equal(tpdfs[0].dsc, "desc-text");
David Sedlák04e17b22019-07-19 15:29:48 +02002310 assert_string_equal(tpdfs[0].name, "tpdf-name");
2311 assert_string_equal(tpdfs[0].ref, "ref-text");
2312 assert_string_equal(tpdfs[0].type.name, "type");
2313 assert_string_equal(tpdfs[0].units, "uni");
2314 assert_true(tpdfs[0].flags & LYS_STATUS_CURR);
Radek Iša56ca9e42020-09-08 18:42:00 +02002315 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(tpdfs[0].exts[0]), LYEXT_SUBSTMT_SELF);
2316 FREE_ARRAY(UTEST_LYCTX, tpdfs, lysp_tpdf_free);
David Sedlák04e17b22019-07-19 15:29:48 +02002317 tpdfs = NULL;
2318
2319 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002320 "<typedef name=\"tpdf-name\">\n"
2321 " <type name=\"type\"/>\n"
2322 "</typedef>"
2323 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002324 assert_int_equal(test_element_helper(state, data, &typdef_meta, NULL, NULL), LY_SUCCESS);
David Sedlák04e17b22019-07-19 15:29:48 +02002325 assert_string_equal(tpdfs[0].name, "tpdf-name");
2326 assert_string_equal(tpdfs[0].type.name, "type");
Radek Iša56ca9e42020-09-08 18:42:00 +02002327 FREE_ARRAY(UTEST_LYCTX, tpdfs, lysp_tpdf_free);
David Sedlák04e17b22019-07-19 15:29:48 +02002328 tpdfs = NULL;
David Sedlák04e17b22019-07-19 15:29:48 +02002329}
2330
David Sedlákd2d676a2019-07-22 11:28:19 +02002331static void
2332test_refine_elem(void **state)
2333{
David Sedlákd2d676a2019-07-22 11:28:19 +02002334 const char *data;
2335 struct lysp_refine *refines = NULL;
2336
2337 /* max subelems */
2338 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002339 "<refine target-node=\"target\">\n"
2340 " <if-feature name=\"feature\" />\n"
2341 " <must condition=\"cond\" />\n"
2342 " <presence value=\"presence\" />\n"
2343 " <default value=\"def\" />\n"
2344 " <config value=\"true\" />\n"
2345 " <mandatory value=\"true\" />\n"
2346 " <min-elements value=\"10\" />\n"
2347 " <max-elements value=\"20\" />\n"
2348 " <description><text>desc</text></description>\n"
2349 " <reference><text>ref</text></reference>\n"
2350 EXT_SUBELEM
2351 "</refine>"
2352 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002353 assert_int_equal(test_element_helper(state, data, &refines, NULL, NULL), LY_SUCCESS);
David Sedlákd2d676a2019-07-22 11:28:19 +02002354 assert_string_equal(refines->nodeid, "target");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02002355 assert_string_equal(refines->dflts[0].str, "def");
David Sedlákd2d676a2019-07-22 11:28:19 +02002356 assert_string_equal(refines->dsc, "desc");
David Sedlákd2d676a2019-07-22 11:28:19 +02002357 assert_true(refines->flags & LYS_CONFIG_W);
2358 assert_true(refines->flags & LYS_MAND_TRUE);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002359 assert_string_equal(refines->iffeatures[0].str, "feature");
David Sedlákd2d676a2019-07-22 11:28:19 +02002360 assert_int_equal(refines->max, 20);
2361 assert_int_equal(refines->min, 10);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002362 assert_string_equal(refines->musts->arg.str, "cond");
David Sedlákd2d676a2019-07-22 11:28:19 +02002363 assert_string_equal(refines->presence, "presence");
2364 assert_string_equal(refines->ref, "ref");
Radek Iša56ca9e42020-09-08 18:42:00 +02002365 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(refines->exts[0]), LYEXT_SUBSTMT_SELF);
2366 FREE_ARRAY(UTEST_LYCTX, refines, lysp_refine_free);
David Sedlákd2d676a2019-07-22 11:28:19 +02002367 refines = NULL;
2368
2369 /* min subelems */
2370 data = ELEMENT_WRAPPER_START "<refine target-node=\"target\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002371 assert_int_equal(test_element_helper(state, data, &refines, NULL, NULL), LY_SUCCESS);
David Sedlákd2d676a2019-07-22 11:28:19 +02002372 assert_string_equal(refines->nodeid, "target");
Radek Iša56ca9e42020-09-08 18:42:00 +02002373 FREE_ARRAY(UTEST_LYCTX, refines, lysp_refine_free);
David Sedlákd2d676a2019-07-22 11:28:19 +02002374 refines = NULL;
David Sedlákd2d676a2019-07-22 11:28:19 +02002375}
2376
David Sedlák0d6de5a2019-07-22 13:25:44 +02002377static void
2378test_uses_elem(void **state)
2379{
David Sedlák0d6de5a2019-07-22 13:25:44 +02002380 const char *data;
2381 struct lysp_node *siblings = NULL;
2382 struct tree_node_meta node_meta = {NULL, &siblings};
2383 struct lysp_node_uses *parsed = NULL;
2384
2385 /* max subelems */
2386 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002387 "<uses name=\"uses-name\">\n"
2388 " <when condition=\"cond\" />\n"
2389 " <if-feature name=\"feature\" />\n"
2390 " <status value=\"obsolete\" />\n"
2391 " <description><text>desc</text></description>\n"
2392 " <reference><text>ref</text></reference>\n"
2393 " <refine target-node=\"target\"/>\n"
2394 " <augment target-node=\"target\" />\n"
2395 EXT_SUBELEM
2396 "</uses>"
2397 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002398 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002399 parsed = (struct lysp_node_uses *)&siblings[0];
Radek Iša56ca9e42020-09-08 18:42:00 +02002400 CHECK_LYSP_NODE(parsed, "desc", 1, LYS_STATUS_OBSLT, 1,
2401 "uses-name", 0, LYS_USES, 0, "ref", 1);
2402 CHECK_LYSP_WHEN(parsed->when, "cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002403 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlák0d6de5a2019-07-22 13:25:44 +02002404 assert_string_equal(parsed->refines->nodeid, "target");
David Sedlák992fb7c2019-07-24 16:51:01 +02002405 assert_string_equal(parsed->augments->nodeid, "target");
Radek Iša56ca9e42020-09-08 18:42:00 +02002406 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LYEXT_SUBSTMT_SELF);
2407 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002408 siblings = NULL;
2409
2410 /* min subelems */
2411 data = ELEMENT_WRAPPER_START "<uses name=\"uses-name\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002412 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002413 assert_string_equal(siblings[0].name, "uses-name");
Radek Iša56ca9e42020-09-08 18:42:00 +02002414 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002415 siblings = NULL;
David Sedlák0d6de5a2019-07-22 13:25:44 +02002416}
2417
David Sedlákaa854b02019-07-22 14:17:10 +02002418static void
2419test_revision_elem(void **state)
2420{
David Sedlákaa854b02019-07-22 14:17:10 +02002421 const char *data;
2422 struct lysp_revision *revs = NULL;
2423
2424 /* max subelems */
2425 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002426 "<revision date=\"2018-12-25\">\n"
2427 " <description><text>desc</text></description>\n"
2428 " <reference><text>ref</text></reference>\n"
2429 EXT_SUBELEM
2430 "</revision>"
2431 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002432 assert_int_equal(test_element_helper(state, data, &revs, NULL, NULL), LY_SUCCESS);
David Sedlákaa854b02019-07-22 14:17:10 +02002433 assert_string_equal(revs->date, "2018-12-25");
2434 assert_string_equal(revs->dsc, "desc");
2435 assert_string_equal(revs->ref, "ref");
Radek Iša56ca9e42020-09-08 18:42:00 +02002436 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(revs->exts[0]), LYEXT_SUBSTMT_SELF);
2437 FREE_ARRAY(UTEST_LYCTX, revs, lysp_revision_free);
David Sedlákaa854b02019-07-22 14:17:10 +02002438 revs = NULL;
2439
2440 /* min subelems */
2441 data = ELEMENT_WRAPPER_START "<revision date=\"2005-05-05\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002442 assert_int_equal(test_element_helper(state, data, &revs, NULL, NULL), LY_SUCCESS);
David Sedlákaa854b02019-07-22 14:17:10 +02002443 assert_string_equal(revs->date, "2005-05-05");
Radek Iša56ca9e42020-09-08 18:42:00 +02002444 FREE_ARRAY(UTEST_LYCTX, revs, lysp_revision_free);
David Sedlákaa854b02019-07-22 14:17:10 +02002445 revs = NULL;
2446
2447 /* invalid value */
2448 data = ELEMENT_WRAPPER_START "<revision date=\"05-05-2005\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002449 assert_int_equal(test_element_helper(state, data, &revs, NULL, NULL), LY_EVALID);
2450 CHECK_LOG_CTX("Invalid value \"05-05-2005\" of \"revision\".", "Line number 1.");
2451 FREE_ARRAY(UTEST_LYCTX, revs, lysp_revision_free);
David Sedlákaa854b02019-07-22 14:17:10 +02002452 revs = NULL;
David Sedlákaa854b02019-07-22 14:17:10 +02002453}
2454
David Sedlák0c2bab92019-07-22 15:33:19 +02002455static void
2456test_include_elem(void **state)
2457{
David Sedlák0c2bab92019-07-22 15:33:19 +02002458 const char *data;
2459 struct lysp_include *includes = NULL;
2460 struct include_meta inc_meta = {"module-name", &includes};
2461
2462 /* max subelems */
Radek Iša56ca9e42020-09-08 18:42:00 +02002463 YCTX->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák0c2bab92019-07-22 15:33:19 +02002464 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002465 "<include module=\"mod\">\n"
2466 " <description><text>desc</text></description>\n"
2467 " <reference><text>ref</text></reference>\n"
2468 " <revision-date date=\"1999-09-09\"/>\n"
2469 EXT_SUBELEM
2470 "</include>"
2471 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002472 assert_int_equal(test_element_helper(state, data, &inc_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0c2bab92019-07-22 15:33:19 +02002473 assert_string_equal(includes->name, "mod");
2474 assert_string_equal(includes->dsc, "desc");
2475 assert_string_equal(includes->ref, "ref");
David Sedlák0c2bab92019-07-22 15:33:19 +02002476 assert_string_equal(includes->rev, "1999-09-09");
Radek Iša56ca9e42020-09-08 18:42:00 +02002477 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(includes->exts[0]), LYEXT_SUBSTMT_SELF);
2478 FREE_ARRAY(UTEST_LYCTX, includes, lysp_include_free);
David Sedlák0c2bab92019-07-22 15:33:19 +02002479 includes = NULL;
2480
2481 /* min subelems */
2482 data = ELEMENT_WRAPPER_START "<include module=\"mod\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002483 assert_int_equal(test_element_helper(state, data, &inc_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0c2bab92019-07-22 15:33:19 +02002484 assert_string_equal(includes->name, "mod");
Radek Iša56ca9e42020-09-08 18:42:00 +02002485 FREE_ARRAY(UTEST_LYCTX, includes, lysp_include_free);
David Sedlák0c2bab92019-07-22 15:33:19 +02002486 includes = NULL;
2487
2488 /* invalid combinations */
Radek Iša56ca9e42020-09-08 18:42:00 +02002489 YCTX->parsed_mod->version = LYS_VERSION_1_0;
David Sedlák0c2bab92019-07-22 15:33:19 +02002490 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002491 "<include module=\"mod\">\n"
2492 " <description><text>desc</text></description>\n"
2493 " <revision-date date=\"1999-09-09\"/>\n"
2494 "</include>"
2495 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002496 assert_int_equal(test_element_helper(state, data, &inc_meta, NULL, NULL), LY_EVALID);
2497 CHECK_LOG_CTX("Invalid sub-elemnt \"description\" of \"include\" element - this sub-element is allowed only in modules with version 1.1 or newer.",
2498 "Line number 2.");
2499 FREE_ARRAY(UTEST_LYCTX, includes, lysp_include_free);
David Sedlák0c2bab92019-07-22 15:33:19 +02002500 includes = NULL;
2501
Radek Iša56ca9e42020-09-08 18:42:00 +02002502 YCTX->parsed_mod->version = LYS_VERSION_1_0;
David Sedlák0c2bab92019-07-22 15:33:19 +02002503 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002504 "<include module=\"mod\">\n"
2505 " <reference><text>ref</text></reference>\n"
2506 " <revision-date date=\"1999-09-09\"/>\n"
2507 "</include>"
2508 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002509 assert_int_equal(test_element_helper(state, data, &inc_meta, NULL, NULL), LY_EVALID);
2510 CHECK_LOG_CTX("Invalid sub-elemnt \"reference\" of \"include\" element - this sub-element is allowed only in modules with version 1.1 or newer.",
2511 "Line number 2.");
2512 FREE_ARRAY(UTEST_LYCTX, includes, lysp_include_free);
David Sedlák0c2bab92019-07-22 15:33:19 +02002513 includes = NULL;
David Sedlák0c2bab92019-07-22 15:33:19 +02002514}
2515
David Sedlák5e13dea2019-07-22 16:06:45 +02002516static void
David Sedlákaf536aa2019-07-23 13:42:23 +02002517test_list_elem(void **state)
2518{
David Sedlákaf536aa2019-07-23 13:42:23 +02002519 const char *data;
2520 struct lysp_node *siblings = NULL;
2521 struct tree_node_meta node_meta = {NULL, &siblings};
2522 struct lysp_node_list *parsed = NULL;
2523
2524 /* max subelems */
2525 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002526 "<list name=\"list-name\">\n"
2527 " <when condition=\"when\"/>\n"
2528 " <if-feature name=\"iff\"/>\n"
2529 " <must condition=\"must-cond\"/>\n"
2530 " <key value=\"key\"/>\n"
2531 " <unique tag=\"utag\"/>\n"
2532 " <config value=\"true\"/>\n"
2533 " <min-elements value=\"10\"/>\n"
2534 " <ordered-by value=\"user\"/>\n"
2535 " <status value=\"deprecated\"/>\n"
2536 " <description><text>desc</text></description>\n"
2537 " <reference><text>ref</text></reference>\n"
2538 " <anydata name=\"anyd\"/>\n"
2539 " <anyxml name=\"anyx\"/>\n"
2540 " <container name=\"cont\"/>\n"
2541 " <choice name=\"choice\"/>\n"
2542 " <action name=\"action\"/>\n"
2543 " <grouping name=\"grp\"/>\n"
2544 " <notification name=\"notf\"/>\n"
2545 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2546 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2547 " <list name=\"sub-list\"/>\n"
2548 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2549 " <uses name=\"uses-name\"/>\n"
2550 EXT_SUBELEM
2551 "</list>"
2552 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002553 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákaf536aa2019-07-23 13:42:23 +02002554 parsed = (struct lysp_node_list *)&siblings[0];
David Sedlákaf536aa2019-07-23 13:42:23 +02002555 assert_string_equal(parsed->child->name, "anyd");
2556 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2557 assert_string_equal(parsed->child->next->name, "anyx");
2558 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
David Sedlákf111bcb2019-07-23 17:15:51 +02002559 assert_string_equal(parsed->child->next->next->name, "cont");
2560 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002561 assert_string_equal(parsed->child->next->next->next->name, "choice");
2562 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
David Sedlák85d0eca2019-07-24 15:15:21 +02002563 assert_string_equal(parsed->child->next->next->next->next->name, "leaf");
2564 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAF);
2565 assert_string_equal(parsed->child->next->next->next->next->next->name, "llist");
2566 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2567 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "sub-list");
2568 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LIST);
2569 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "uses-name");
2570 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_USES);
2571 assert_null(parsed->child->next->next->next->next->next->next->next->next);
Radek Iša56ca9e42020-09-08 18:42:00 +02002572 uint16_t flags = LYS_ORDBY_USER | LYS_STATUS_DEPRC | LYS_CONFIG_W | LYS_SET_MIN;
2573
2574 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2575 "list-name", 0, LYS_LIST, 0, "ref", 1);
2576 CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL);
2577 CHECK_LYSP_WHEN(parsed->when, "when", NULL, 0, NULL);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002578 assert_string_equal(parsed->groupings->name, "grp");
David Sedlák85d0eca2019-07-24 15:15:21 +02002579 assert_string_equal(parsed->actions->name, "action");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002580 assert_int_equal(parsed->groupings->nodetype, LYS_GROUPING);
David Sedlák031b9e72019-07-23 15:19:37 +02002581 assert_string_equal(parsed->notifs->name, "notf");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002582 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlákaf536aa2019-07-23 13:42:23 +02002583 assert_string_equal(parsed->key, "key");
2584 assert_int_equal(parsed->min, 10);
David Sedlákaf536aa2019-07-23 13:42:23 +02002585 assert_string_equal(parsed->typedefs->name, "tpdf");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002586 assert_string_equal(parsed->uniques->str, "utag");
Radek Iša56ca9e42020-09-08 18:42:00 +02002587 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LYEXT_SUBSTMT_SELF);
2588 lysp_node_free(UTEST_LYCTX, siblings);
2589 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
David Sedlákaf536aa2019-07-23 13:42:23 +02002590 siblings = NULL;
2591
2592 /* min subelems */
2593 data = ELEMENT_WRAPPER_START "<list name=\"list-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002594 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákaf536aa2019-07-23 13:42:23 +02002595 parsed = (struct lysp_node_list *)&siblings[0];
Radek Iša56ca9e42020-09-08 18:42:00 +02002596 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2597 "list-name", 0, LYS_LIST, 0, NULL, 0);
2598 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlákaf536aa2019-07-23 13:42:23 +02002599 siblings = NULL;
David Sedlákaf536aa2019-07-23 13:42:23 +02002600}
2601
David Sedlák031b9e72019-07-23 15:19:37 +02002602static void
2603test_notification_elem(void **state)
2604{
David Sedlák031b9e72019-07-23 15:19:37 +02002605 const char *data;
2606 struct lysp_notif *notifs = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002607 struct tree_node_meta notif_meta = {NULL, (struct lysp_node **)&notifs};
David Sedlák031b9e72019-07-23 15:19:37 +02002608
2609 /* max subelems */
Radek Iša56ca9e42020-09-08 18:42:00 +02002610 YCTX->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák031b9e72019-07-23 15:19:37 +02002611 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002612 "<notification name=\"notif-name\">\n"
2613 " <anydata name=\"anyd\"/>\n"
2614 " <anyxml name=\"anyx\"/>\n"
2615 " <description><text>desc</text></description>\n"
2616 " <if-feature name=\"iff\"/>\n"
2617 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2618 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2619 " <list name=\"sub-list\"/>\n"
2620 " <must condition=\"cond\"/>\n"
2621 " <reference><text>ref</text></reference>\n"
2622 " <status value=\"deprecated\"/>\n"
2623 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2624 " <uses name=\"uses-name\"/>\n"
2625 " <container name=\"cont\"/>\n"
2626 " <choice name=\"choice\"/>\n"
2627 " <grouping name=\"grp\"/>\n"
2628 EXT_SUBELEM
2629 "</notification>"
2630 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002631 assert_int_equal(test_element_helper(state, data, &notif_meta, NULL, NULL), LY_SUCCESS);
David Sedlák031b9e72019-07-23 15:19:37 +02002632 assert_string_equal(notifs->name, "notif-name");
2633 assert_string_equal(notifs->data->name, "anyd");
2634 assert_int_equal(notifs->data->nodetype, LYS_ANYDATA);
2635 assert_string_equal(notifs->data->next->name, "anyx");
2636 assert_int_equal(notifs->data->next->nodetype, LYS_ANYXML);
2637 assert_string_equal(notifs->data->next->next->name, "leaf");
2638 assert_int_equal(notifs->data->next->next->nodetype, LYS_LEAF);
2639 assert_string_equal(notifs->data->next->next->next->name, "llist");
2640 assert_int_equal(notifs->data->next->next->next->nodetype, LYS_LEAFLIST);
2641 assert_string_equal(notifs->data->next->next->next->next->name, "sub-list");
2642 assert_int_equal(notifs->data->next->next->next->next->nodetype, LYS_LIST);
David Sedlák031b9e72019-07-23 15:19:37 +02002643 assert_true(notifs->flags & LYS_STATUS_DEPRC);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002644 assert_string_equal(notifs->groupings->name, "grp");
2645 assert_int_equal(notifs->groupings->nodetype, LYS_GROUPING);
David Sedlákf111bcb2019-07-23 17:15:51 +02002646 assert_string_equal(notifs->data->next->next->next->next->next->name, "uses-name");
2647 assert_int_equal(notifs->data->next->next->next->next->next->nodetype, LYS_USES);
2648 assert_string_equal(notifs->data->next->next->next->next->next->next->name, "cont");
2649 assert_int_equal(notifs->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002650 assert_int_equal(notifs->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2651 assert_string_equal(notifs->data->next->next->next->next->next->next->next->name, "choice");
2652 assert_null(notifs->data->next->next->next->next->next->next->next->next);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002653 assert_string_equal(notifs->iffeatures[0].str, "iff");
2654 assert_string_equal(notifs->musts->arg.str, "cond");
David Sedlák031b9e72019-07-23 15:19:37 +02002655 assert_int_equal(notifs->nodetype, LYS_NOTIF);
2656 assert_null(notifs->parent);
2657 assert_string_equal(notifs->ref, "ref");
2658 assert_string_equal(notifs->typedefs->name, "tpdf");
Radek Iša56ca9e42020-09-08 18:42:00 +02002659 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(notifs->exts[0]), LYEXT_SUBSTMT_SELF);
2660 FREE_ARRAY(UTEST_LYCTX, notifs, lysp_notif_free);
David Sedlák031b9e72019-07-23 15:19:37 +02002661 notifs = NULL;
2662
2663 /* min subelems */
2664 data = ELEMENT_WRAPPER_START "<notification name=\"notif-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002665 assert_int_equal(test_element_helper(state, data, &notif_meta, NULL, NULL), LY_SUCCESS);
David Sedlák031b9e72019-07-23 15:19:37 +02002666 assert_string_equal(notifs->name, "notif-name");
Radek Iša56ca9e42020-09-08 18:42:00 +02002667 FREE_ARRAY(UTEST_LYCTX, notifs, lysp_notif_free);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002668 notifs = NULL;
David Sedláke3ce9ef2019-07-23 16:34:30 +02002669}
2670
2671static void
2672test_grouping_elem(void **state)
2673{
David Sedláke3ce9ef2019-07-23 16:34:30 +02002674 const char *data;
2675 struct lysp_grp *grps = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002676 struct tree_node_meta grp_meta = {NULL, (struct lysp_node **)&grps};
David Sedláke3ce9ef2019-07-23 16:34:30 +02002677
2678 /* max subelems */
2679 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002680 "<grouping name=\"grp-name\">\n"
2681 " <anydata name=\"anyd\"/>\n"
2682 " <anyxml name=\"anyx\"/>\n"
2683 " <description><text>desc</text></description>\n"
2684 " <grouping name=\"sub-grp\"/>\n"
2685 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2686 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2687 " <list name=\"list\"/>\n"
2688 " <notification name=\"notf\"/>\n"
2689 " <reference><text>ref</text></reference>\n"
2690 " <status value=\"current\"/>\n"
2691 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2692 " <uses name=\"uses-name\"/>\n"
2693 " <action name=\"act\"/>\n"
2694 " <container name=\"cont\"/>\n"
2695 " <choice name=\"choice\"/>\n"
2696 EXT_SUBELEM
2697 "</grouping>"
2698 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002699 assert_int_equal(test_element_helper(state, data, &grp_meta, NULL, NULL), LY_SUCCESS);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002700 assert_string_equal(grps->name, "grp-name");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002701 assert_string_equal(grps->data->name, "anyd");
2702 assert_string_equal(grps->data->next->name, "anyx");
2703 assert_string_equal(grps->data->next->next->name, "leaf");
2704 assert_string_equal(grps->data->next->next->next->name, "llist");
2705 assert_string_equal(grps->data->next->next->next->next->name, "list");
2706 assert_string_equal(grps->dsc, "desc");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002707 assert_true(grps->flags & LYS_STATUS_CURR);
2708 assert_string_equal(grps->groupings->name, "sub-grp");
2709 assert_int_equal(grps->nodetype, LYS_GROUPING);
2710 assert_string_equal(grps->notifs->name, "notf");
2711 assert_null(grps->parent);
2712 assert_string_equal(grps->ref, "ref");
2713 assert_string_equal(grps->typedefs->name, "tpdf");
David Sedlák85d0eca2019-07-24 15:15:21 +02002714 assert_string_equal(grps->actions->name, "act");
David Sedlákf111bcb2019-07-23 17:15:51 +02002715 assert_string_equal(grps->data->next->next->next->next->next->name, "uses-name");
David Sedlákb7abcfa2019-07-24 12:33:35 +02002716 assert_int_equal(grps->data->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákf111bcb2019-07-23 17:15:51 +02002717 assert_string_equal(grps->data->next->next->next->next->next->next->name, "cont");
David Sedlákb7abcfa2019-07-24 12:33:35 +02002718 assert_int_equal(grps->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
2719 assert_string_equal(grps->data->next->next->next->next->next->next->next->name, "choice");
2720 assert_int_equal(grps->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
Radek Iša56ca9e42020-09-08 18:42:00 +02002721 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(grps->exts[0]), LYEXT_SUBSTMT_SELF);
2722 FREE_ARRAY(UTEST_LYCTX, grps, lysp_grp_free);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002723 grps = NULL;
2724
2725 /* min subelems */
2726 data = ELEMENT_WRAPPER_START "<grouping name=\"grp-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002727 assert_int_equal(test_element_helper(state, data, &grp_meta, NULL, NULL), LY_SUCCESS);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002728 assert_string_equal(grps->name, "grp-name");
Radek Iša56ca9e42020-09-08 18:42:00 +02002729 FREE_ARRAY(UTEST_LYCTX, grps, lysp_grp_free);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002730 grps = NULL;
David Sedlák031b9e72019-07-23 15:19:37 +02002731}
2732
David Sedlákf111bcb2019-07-23 17:15:51 +02002733static void
2734test_container_elem(void **state)
2735{
David Sedlákf111bcb2019-07-23 17:15:51 +02002736 const char *data;
2737 struct lysp_node *siblings = NULL;
2738 struct tree_node_meta node_meta = {NULL, &siblings};
2739 struct lysp_node_container *parsed = NULL;
2740
2741 /* max subelems */
Radek Iša56ca9e42020-09-08 18:42:00 +02002742 YCTX->parsed_mod->version = LYS_VERSION_1_1;
David Sedláke2dc9e92019-07-24 09:59:21 +02002743 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002744 "<container name=\"cont-name\">\n"
2745 " <anydata name=\"anyd\"/>\n"
2746 " <anyxml name=\"anyx\"/>\n"
2747 " <config value=\"true\"/>\n"
2748 " <container name=\"subcont\"/>\n"
2749 " <description><text>desc</text></description>\n"
2750 " <grouping name=\"sub-grp\"/>\n"
2751 " <if-feature name=\"iff\"/>\n"
2752 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2753 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2754 " <list name=\"list\"/>\n"
2755 " <must condition=\"cond\"/>\n"
2756 " <notification name=\"notf\"/>\n"
2757 " <presence value=\"presence\"/>\n"
2758 " <reference><text>ref</text></reference>\n"
2759 " <status value=\"current\"/>\n"
2760 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2761 " <uses name=\"uses-name\"/>\n"
2762 " <when condition=\"when-cond\"/>\n"
2763 " <action name=\"act\"/>\n"
2764 " <choice name=\"choice\"/>\n"
2765 EXT_SUBELEM
2766 "</container>"
2767 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002768 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedláke2dc9e92019-07-24 09:59:21 +02002769 parsed = (struct lysp_node_container *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002770 uint16_t flags = LYS_CONFIG_W | LYS_STATUS_CURR;
2771
2772 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2773 "cont-name", 0, LYS_CONTAINER, 0, "ref", 1);
2774 CHECK_LYSP_RESTR(parsed->musts, "cond", NULL, NULL, NULL, 0, NULL);
2775 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
2776
Michal Vasko7f45cf22020-10-01 12:49:44 +02002777 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedláke2dc9e92019-07-24 09:59:21 +02002778 assert_string_equal(parsed->presence, "presence");
2779 assert_string_equal(parsed->typedefs->name, "tpdf");
2780 assert_string_equal(parsed->groupings->name, "sub-grp");
2781 assert_string_equal(parsed->child->name, "anyd");
2782 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2783 assert_string_equal(parsed->child->next->name, "anyx");
2784 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2785 assert_string_equal(parsed->child->next->next->name, "subcont");
2786 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
2787 assert_string_equal(parsed->child->next->next->next->name, "leaf");
2788 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
2789 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
2790 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
2791 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
2792 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
2793 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
2794 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002795 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
2796 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2797 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke2dc9e92019-07-24 09:59:21 +02002798 assert_string_equal(parsed->notifs->name, "notf");
David Sedlák85d0eca2019-07-24 15:15:21 +02002799 assert_string_equal(parsed->actions->name, "act");
Radek Iša56ca9e42020-09-08 18:42:00 +02002800 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LYEXT_SUBSTMT_SELF);
2801 lysp_node_free(UTEST_LYCTX, siblings);
2802 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
David Sedláke2dc9e92019-07-24 09:59:21 +02002803 siblings = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02002804
2805 /* min subelems */
2806 data = ELEMENT_WRAPPER_START "<container name=\"cont-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002807 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákf111bcb2019-07-23 17:15:51 +02002808 parsed = (struct lysp_node_container *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002809 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2810 "cont-name", 0, LYS_CONTAINER, 0, NULL, 0);
2811 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlákf111bcb2019-07-23 17:15:51 +02002812 siblings = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02002813}
2814
David Sedlák5379d392019-07-24 10:42:03 +02002815static void
2816test_case_elem(void **state)
2817{
David Sedlák5379d392019-07-24 10:42:03 +02002818 const char *data;
2819 struct lysp_node *siblings = NULL;
2820 struct tree_node_meta node_meta = {NULL, &siblings};
2821 struct lysp_node_case *parsed = NULL;
2822
2823 /* max subelems */
Radek Iša56ca9e42020-09-08 18:42:00 +02002824 YCTX->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák5379d392019-07-24 10:42:03 +02002825 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002826 "<case name=\"case-name\">\n"
2827 " <anydata name=\"anyd\"/>\n"
2828 " <anyxml name=\"anyx\"/>\n"
2829 " <container name=\"subcont\"/>\n"
2830 " <description><text>desc</text></description>\n"
2831 " <if-feature name=\"iff\"/>\n"
2832 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2833 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2834 " <list name=\"list\"/>\n"
2835 " <reference><text>ref</text></reference>\n"
2836 " <status value=\"current\"/>\n"
2837 " <uses name=\"uses-name\"/>\n"
2838 " <when condition=\"when-cond\"/>\n"
2839 " <choice name=\"choice\"/>\n"
2840 EXT_SUBELEM
2841 "</case>"
2842 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002843 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák5379d392019-07-24 10:42:03 +02002844 parsed = (struct lysp_node_case *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002845 uint16_t flags = LYS_STATUS_CURR;
2846
2847 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2848 "case-name", 0, LYS_CASE, 0, "ref", 1);
2849 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002850 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlák5379d392019-07-24 10:42:03 +02002851 assert_string_equal(parsed->child->name, "anyd");
2852 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2853 assert_string_equal(parsed->child->next->name, "anyx");
2854 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2855 assert_string_equal(parsed->child->next->next->name, "subcont");
2856 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
2857 assert_string_equal(parsed->child->next->next->next->name, "leaf");
2858 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
2859 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
2860 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
2861 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
2862 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
2863 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
2864 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002865 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
2866 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2867 assert_null(parsed->child->next->next->next->next->next->next->next->next);
Radek Iša56ca9e42020-09-08 18:42:00 +02002868 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LYEXT_SUBSTMT_SELF);
2869 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlák5379d392019-07-24 10:42:03 +02002870 siblings = NULL;
2871
2872 /* min subelems */
2873 data = ELEMENT_WRAPPER_START "<case name=\"case-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002874 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák5379d392019-07-24 10:42:03 +02002875 parsed = (struct lysp_node_case *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002876 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2877 "case-name", 0, LYS_CASE, 0, NULL, 0);
2878 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlák5379d392019-07-24 10:42:03 +02002879 siblings = NULL;
David Sedlák5379d392019-07-24 10:42:03 +02002880}
2881
David Sedlákb7abcfa2019-07-24 12:33:35 +02002882static void
2883test_choice_elem(void **state)
2884{
David Sedlákb7abcfa2019-07-24 12:33:35 +02002885 const char *data;
2886 struct lysp_node *siblings = NULL;
2887 struct tree_node_meta node_meta = {NULL, &siblings};
2888 struct lysp_node_choice *parsed = NULL;
2889
2890 /* max subelems */
Radek Iša56ca9e42020-09-08 18:42:00 +02002891 YCTX->parsed_mod->version = LYS_VERSION_1_1;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002892 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002893 "<choice name=\"choice-name\">\n"
2894 " <anydata name=\"anyd\"/>\n"
2895 " <anyxml name=\"anyx\"/>\n"
2896 " <case name=\"sub-case\"/>\n"
2897 " <choice name=\"choice\"/>\n"
2898 " <config value=\"true\"/>\n"
2899 " <container name=\"subcont\"/>\n"
2900 " <default value=\"def\"/>\n"
2901 " <description><text>desc</text></description>\n"
2902 " <if-feature name=\"iff\"/>\n"
2903 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2904 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2905 " <list name=\"list\"/>\n"
2906 " <mandatory value=\"true\" />\n"
2907 " <reference><text>ref</text></reference>\n"
2908 " <status value=\"current\"/>\n"
2909 " <when condition=\"when-cond\"/>\n"
2910 EXT_SUBELEM
2911 "</choice>"
2912 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002913 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002914 parsed = (struct lysp_node_choice *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002915 uint16_t flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_CURR;
2916
2917 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2918 "choice-name", 0, LYS_CHOICE, 0, "ref", 1);
2919 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002920 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlákb7abcfa2019-07-24 12:33:35 +02002921 assert_string_equal(parsed->child->name, "anyd");
2922 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2923 assert_string_equal(parsed->child->next->name, "anyx");
2924 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2925 assert_string_equal(parsed->child->next->next->name, "sub-case");
2926 assert_int_equal(parsed->child->next->next->nodetype, LYS_CASE);
2927 assert_string_equal(parsed->child->next->next->next->name, "choice");
2928 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
2929 assert_string_equal(parsed->child->next->next->next->next->name, "subcont");
2930 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_CONTAINER);
2931 assert_string_equal(parsed->child->next->next->next->next->next->name, "leaf");
2932 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAF);
2933 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "llist");
2934 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2935 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "list");
2936 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
2937 assert_null(parsed->child->next->next->next->next->next->next->next->next);
Radek Iša56ca9e42020-09-08 18:42:00 +02002938 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LYEXT_SUBSTMT_SELF);
2939 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002940 siblings = NULL;
2941
2942 /* min subelems */
2943 data = ELEMENT_WRAPPER_START "<choice name=\"choice-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002944 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002945 parsed = (struct lysp_node_choice *)siblings;
2946 assert_string_equal(parsed->name, "choice-name");
Radek Iša56ca9e42020-09-08 18:42:00 +02002947 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2948 "choice-name", 0, LYS_CHOICE, 0, NULL, 0);
2949 lysp_node_free(UTEST_LYCTX, siblings);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002950 siblings = NULL;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002951}
2952
David Sedlák05404f62019-07-24 14:11:53 +02002953static void
2954test_inout_elem(void **state)
2955{
David Sedlák05404f62019-07-24 14:11:53 +02002956 const char *data;
2957 struct lysp_action_inout inout = {};
2958 struct inout_meta inout_meta = {NULL, &inout};
2959
2960 /* max subelements */
Radek Iša56ca9e42020-09-08 18:42:00 +02002961 YCTX->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák05404f62019-07-24 14:11:53 +02002962 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002963 "<input>\n"
2964 " <anydata name=\"anyd\"/>\n"
2965 " <anyxml name=\"anyx\"/>\n"
2966 " <choice name=\"choice\"/>\n"
2967 " <container name=\"subcont\"/>\n"
2968 " <grouping name=\"sub-grp\"/>\n"
2969 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2970 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2971 " <list name=\"list\"/>\n"
2972 " <must condition=\"cond\"/>\n"
2973 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2974 " <uses name=\"uses-name\"/>\n"
2975 EXT_SUBELEM
2976 "</input>"
2977 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002978 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
2979 CHECK_LYSP_ACTION_INOUT(&(inout), 1, 1, 1, 1, LYS_INPUT, 0, 1);
2980 CHECK_LYSP_RESTR(inout.musts, "cond", NULL, NULL, NULL, 0, NULL);
David Sedlák05404f62019-07-24 14:11:53 +02002981 assert_string_equal(inout.typedefs->name, "tpdf");
2982 assert_string_equal(inout.groupings->name, "sub-grp");
2983 assert_string_equal(inout.data->name, "anyd");
2984 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
2985 assert_string_equal(inout.data->next->name, "anyx");
2986 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
2987 assert_string_equal(inout.data->next->next->name, "choice");
2988 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
2989 assert_string_equal(inout.data->next->next->next->name, "subcont");
2990 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
2991 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
2992 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
2993 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
2994 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2995 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
2996 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
2997 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
2998 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
2999 assert_null(inout.data->next->next->next->next->next->next->next->next);
Radek Iša56ca9e42020-09-08 18:42:00 +02003000 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(inout.exts[0]), LYEXT_SUBSTMT_SELF);
3001 lysp_action_inout_free(UTEST_LYCTX, &inout);
David Sedlák05404f62019-07-24 14:11:53 +02003002 memset(&inout, 0, sizeof inout);
3003
3004 /* max subelements */
Radek Iša56ca9e42020-09-08 18:42:00 +02003005 YCTX->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák05404f62019-07-24 14:11:53 +02003006 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003007 "<output>\n"
3008 " <anydata name=\"anyd\"/>\n"
3009 " <anyxml name=\"anyx\"/>\n"
3010 " <choice name=\"choice\"/>\n"
3011 " <container name=\"subcont\"/>\n"
3012 " <grouping name=\"sub-grp\"/>\n"
3013 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3014 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3015 " <list name=\"list\"/>\n"
3016 " <must condition=\"cond\"/>\n"
3017 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3018 " <uses name=\"uses-name\"/>\n"
3019 EXT_SUBELEM
3020 "</output>"
3021 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003022 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
3023 CHECK_LYSP_ACTION_INOUT(&(inout), 1, 1, 1, 1, LYS_OUTPUT, 0, 1);
Michal Vasko7f45cf22020-10-01 12:49:44 +02003024 assert_string_equal(inout.musts->arg.str, "cond");
David Sedlák05404f62019-07-24 14:11:53 +02003025 assert_string_equal(inout.typedefs->name, "tpdf");
3026 assert_string_equal(inout.groupings->name, "sub-grp");
3027 assert_string_equal(inout.data->name, "anyd");
3028 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
3029 assert_string_equal(inout.data->next->name, "anyx");
3030 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
3031 assert_string_equal(inout.data->next->next->name, "choice");
3032 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
3033 assert_string_equal(inout.data->next->next->next->name, "subcont");
3034 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
3035 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
3036 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
3037 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
3038 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3039 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
3040 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
3041 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
3042 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3043 assert_null(inout.data->next->next->next->next->next->next->next->next);
Radek Iša56ca9e42020-09-08 18:42:00 +02003044 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(inout.exts[0]), LYEXT_SUBSTMT_SELF);
3045 lysp_action_inout_free(UTEST_LYCTX, &inout);
David Sedlák05404f62019-07-24 14:11:53 +02003046 memset(&inout, 0, sizeof inout);
3047
3048 /* min subelems */
Michal Vaskob83af8a2020-01-06 09:49:22 +01003049 data = ELEMENT_WRAPPER_START "<input><leaf name=\"l\"><type name=\"empty\"/></leaf></input>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003050 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
3051 lysp_action_inout_free(UTEST_LYCTX, &inout);
David Sedlák05404f62019-07-24 14:11:53 +02003052 memset(&inout, 0, sizeof inout);
3053
Michal Vaskob83af8a2020-01-06 09:49:22 +01003054 data = ELEMENT_WRAPPER_START "<output><leaf name=\"l\"><type name=\"empty\"/></leaf></output>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003055 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
3056 lysp_action_inout_free(UTEST_LYCTX, &inout);
David Sedlák05404f62019-07-24 14:11:53 +02003057 memset(&inout, 0, sizeof inout);
3058
3059 /* invalid combinations */
3060 data = ELEMENT_WRAPPER_START "<input name=\"test\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003061 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_EVALID);
3062 CHECK_LOG_CTX("Unexpected attribute \"name\" of \"input\" element.", "Line number 1.");
David Sedlák05404f62019-07-24 14:11:53 +02003063 memset(&inout, 0, sizeof inout);
David Sedlák05404f62019-07-24 14:11:53 +02003064}
3065
David Sedlák85d0eca2019-07-24 15:15:21 +02003066static void
3067test_action_elem(void **state)
3068{
David Sedlák85d0eca2019-07-24 15:15:21 +02003069 const char *data;
3070 struct lysp_action *actions = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003071 struct tree_node_meta act_meta = {NULL, (struct lysp_node **)&actions};
Radek Iša56ca9e42020-09-08 18:42:00 +02003072 uint16_t flags;
David Sedlák85d0eca2019-07-24 15:15:21 +02003073
3074 /* max subelems */
Radek Iša56ca9e42020-09-08 18:42:00 +02003075 YCTX->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák85d0eca2019-07-24 15:15:21 +02003076 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003077 "<action name=\"act\">\n"
3078 " <description><text>desc</text></description>\n"
3079 " <grouping name=\"grouping\"/>\n"
3080 " <if-feature name=\"iff\"/>\n"
3081 " <input><uses name=\"uses-name\"/></input>\n"
3082 " <output><must condition=\"cond\"/><leaf name=\"l\"><type name=\"type\"/></leaf></output>\n"
3083 " <reference><text>ref</text></reference>\n"
3084 " <status value=\"deprecated\"/>\n"
3085 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3086 EXT_SUBELEM
3087 "</action>"
3088 ELEMENT_WRAPPER_END;
Michal Vasko1bf09392020-03-27 12:38:10 +01003089 /* there must be parent for action */
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003090 act_meta.parent = (void *)1;
Radek Iša56ca9e42020-09-08 18:42:00 +02003091 assert_int_equal(test_element_helper(state, data, &act_meta, NULL, NULL), LY_SUCCESS);
Michal Vasko1bf09392020-03-27 12:38:10 +01003092 act_meta.parent = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02003093 flags = LYS_STATUS_DEPRC;
3094 CHECK_LYSP_ACTION(actions, "desc", 1, flags, 1, 1,\
3095 1, 0, 0, 0,\
3096 1, 0,\
3097 "act", LYS_ACTION, \
3098 1, 0, 0, 1,\
3099 1, 0,\
3100 1, "ref", 1);
3101
Michal Vasko7f45cf22020-10-01 12:49:44 +02003102 assert_string_equal(actions->iffeatures[0].str, "iff");
David Sedlák85d0eca2019-07-24 15:15:21 +02003103 assert_string_equal(actions->typedefs->name, "tpdf");
3104 assert_string_equal(actions->groupings->name, "grouping");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003105 assert_string_equal(actions->output.musts->arg.str, "cond");
Radek Iša56ca9e42020-09-08 18:42:00 +02003106 assert_string_equal(actions->input.data->name, "uses-name");
3107 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(actions->exts[0]), LYEXT_SUBSTMT_SELF);
3108 FREE_ARRAY(UTEST_LYCTX, actions, lysp_action_free)
David Sedlák85d0eca2019-07-24 15:15:21 +02003109 actions = NULL;
3110
Radek Iša56ca9e42020-09-08 18:42:00 +02003111 YCTX->parsed_mod->version = LYS_VERSION_1_1;
David Sedlákeaa45792019-07-24 15:25:01 +02003112 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003113 "<rpc name=\"act\">\n"
3114 " <description><text>desc</text></description>\n"
3115 " <grouping name=\"grouping\"/>\n"
3116 " <if-feature name=\"iff\"/>\n"
3117 " <input><uses name=\"uses-name\"/></input>\n"
3118 " <output><must condition=\"cond\"/><leaf name=\"l\"><type name=\"type\"/></leaf></output>\n"
3119 " <reference><text>ref</text></reference>\n"
3120 " <status value=\"deprecated\"/>\n"
3121 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3122 EXT_SUBELEM
3123 "</rpc>"
3124 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003125 assert_int_equal(test_element_helper(state, data, &act_meta, NULL, NULL), LY_SUCCESS);
3126 flags = LYS_STATUS_DEPRC;
3127 CHECK_LYSP_ACTION(actions, "desc", 1, flags, 1, 1,\
3128 1, 0, 0, 0,\
3129 1, 0,\
3130 "act", LYS_RPC, \
3131 1, 0, 0, 1,\
3132 1, 0,\
3133 0, "ref", 1);
3134
Michal Vasko7f45cf22020-10-01 12:49:44 +02003135 assert_string_equal(actions->iffeatures[0].str, "iff");
David Sedlákeaa45792019-07-24 15:25:01 +02003136 assert_string_equal(actions->typedefs->name, "tpdf");
3137 assert_string_equal(actions->groupings->name, "grouping");
3138 assert_string_equal(actions->input.data->name, "uses-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003139 assert_string_equal(actions->output.musts->arg.str, "cond");
Radek Iša56ca9e42020-09-08 18:42:00 +02003140 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(actions->exts[0]), LYEXT_SUBSTMT_SELF);
3141 FREE_ARRAY(UTEST_LYCTX, actions, lysp_action_free)
David Sedlákeaa45792019-07-24 15:25:01 +02003142 actions = NULL;
3143
David Sedlák85d0eca2019-07-24 15:15:21 +02003144 /* min subelems */
3145 data = ELEMENT_WRAPPER_START "<action name=\"act\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003146 assert_int_equal(test_element_helper(state, data, &act_meta, NULL, NULL), LY_SUCCESS);
David Sedlák85d0eca2019-07-24 15:15:21 +02003147 assert_string_equal(actions->name, "act");
Radek Iša56ca9e42020-09-08 18:42:00 +02003148 FREE_ARRAY(UTEST_LYCTX, actions, lysp_action_free)
David Sedlák85d0eca2019-07-24 15:15:21 +02003149 actions = NULL;
David Sedlák85d0eca2019-07-24 15:15:21 +02003150}
3151
David Sedlák992fb7c2019-07-24 16:51:01 +02003152static void
3153test_augment_elem(void **state)
3154{
David Sedlák992fb7c2019-07-24 16:51:01 +02003155 const char *data;
3156 struct lysp_augment *augments = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003157 struct tree_node_meta aug_meta = {NULL, (struct lysp_node **)&augments};
David Sedlák992fb7c2019-07-24 16:51:01 +02003158
Radek Iša56ca9e42020-09-08 18:42:00 +02003159 YCTX->parsed_mod->version = LYS_VERSION_1_1;
David Sedlák992fb7c2019-07-24 16:51:01 +02003160 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003161 "<augment target-node=\"target\">\n"
3162 " <action name=\"action\"/>\n"
3163 " <anydata name=\"anyd\"/>\n"
3164 " <anyxml name=\"anyx\"/>\n"
3165 " <case name=\"case\"/>\n"
3166 " <choice name=\"choice\"/>\n"
3167 " <container name=\"subcont\"/>\n"
3168 " <description><text>desc</text></description>\n"
3169 " <if-feature name=\"iff\"/>\n"
3170 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3171 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3172 " <list name=\"list\"/>\n"
3173 " <notification name=\"notif\"/>\n"
3174 " <reference><text>ref</text></reference>\n"
3175 " <status value=\"current\"/>\n"
3176 " <uses name=\"uses\"/>\n"
3177 " <when condition=\"when-cond\"/>\n"
3178 EXT_SUBELEM
3179 "</augment>"
3180 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003181 assert_int_equal(test_element_helper(state, data, &aug_meta, NULL, NULL), LY_SUCCESS);
David Sedlák992fb7c2019-07-24 16:51:01 +02003182 assert_string_equal(augments->nodeid, "target");
3183 assert_null(augments->parent);
3184 assert_int_equal(augments->nodetype, LYS_AUGMENT);
3185 assert_true(augments->flags & LYS_STATUS_CURR);
3186 assert_string_equal(augments->dsc, "desc");
3187 assert_string_equal(augments->ref, "ref");
3188 assert_string_equal(augments->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003189 assert_string_equal(augments->iffeatures[0].str, "iff");
David Sedlák992fb7c2019-07-24 16:51:01 +02003190 assert_string_equal(augments->child->name, "anyd");
3191 assert_int_equal(augments->child->nodetype, LYS_ANYDATA);
3192 assert_string_equal(augments->child->next->name, "anyx");
3193 assert_int_equal(augments->child->next->nodetype, LYS_ANYXML);
3194 assert_string_equal(augments->child->next->next->name, "case");
3195 assert_int_equal(augments->child->next->next->nodetype, LYS_CASE);
3196 assert_string_equal(augments->child->next->next->next->name, "choice");
3197 assert_int_equal(augments->child->next->next->next->nodetype, LYS_CHOICE);
3198 assert_string_equal(augments->child->next->next->next->next->name, "subcont");
3199 assert_int_equal(augments->child->next->next->next->next->nodetype, LYS_CONTAINER);
3200 assert_string_equal(augments->child->next->next->next->next->next->name, "leaf");
3201 assert_int_equal(augments->child->next->next->next->next->next->nodetype, LYS_LEAF);
3202 assert_string_equal(augments->child->next->next->next->next->next->next->name, "llist");
3203 assert_int_equal(augments->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3204 assert_string_equal(augments->child->next->next->next->next->next->next->next->name, "list");
3205 assert_int_equal(augments->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
3206 assert_string_equal(augments->child->next->next->next->next->next->next->next->next->name, "uses");
3207 assert_int_equal(augments->child->next->next->next->next->next->next->next->next->nodetype, LYS_USES);
3208 assert_null(augments->child->next->next->next->next->next->next->next->next->next);
3209 assert_string_equal(augments->actions->name, "action");
3210 assert_string_equal(augments->notifs->name, "notif");
Radek Iša56ca9e42020-09-08 18:42:00 +02003211 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(augments->exts[0]), LYEXT_SUBSTMT_SELF);
3212 FREE_ARRAY(UTEST_LYCTX, augments, lysp_augment_free)
David Sedlák992fb7c2019-07-24 16:51:01 +02003213 augments = NULL;
3214
3215 data = ELEMENT_WRAPPER_START "<augment target-node=\"target\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003216 assert_int_equal(test_element_helper(state, data, &aug_meta, NULL, NULL), LY_SUCCESS);
David Sedlák992fb7c2019-07-24 16:51:01 +02003217 assert_string_equal(augments->nodeid, "target");
Radek Iša56ca9e42020-09-08 18:42:00 +02003218 FREE_ARRAY(UTEST_LYCTX, augments, lysp_augment_free)
David Sedlák992fb7c2019-07-24 16:51:01 +02003219 augments = NULL;
David Sedlák992fb7c2019-07-24 16:51:01 +02003220}
3221
David Sedlák4ffcec82019-07-25 15:10:21 +02003222static void
3223test_deviate_elem(void **state)
3224{
David Sedlák4ffcec82019-07-25 15:10:21 +02003225 const char *data;
3226 struct lysp_deviate *deviates = NULL;
3227 struct lysp_deviate_add *d_add;
3228 struct lysp_deviate_rpl *d_rpl;
3229 struct lysp_deviate_del *d_del;
3230
3231 /* all valid arguments with min subelems */
3232 data = ELEMENT_WRAPPER_START "<deviate value=\"not-supported\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003233 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003234 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
Radek Iša56ca9e42020-09-08 18:42:00 +02003235 lysp_deviate_free(UTEST_LYCTX, deviates);
David Sedlák4ffcec82019-07-25 15:10:21 +02003236 free(deviates);
3237 deviates = NULL;
3238
3239 data = ELEMENT_WRAPPER_START "<deviate value=\"add\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003240 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003241 assert_int_equal(deviates->mod, LYS_DEV_ADD);
Radek Iša56ca9e42020-09-08 18:42:00 +02003242 lysp_deviate_free(UTEST_LYCTX, deviates);
David Sedlák4ffcec82019-07-25 15:10:21 +02003243 free(deviates);
3244 deviates = NULL;
3245
3246 data = ELEMENT_WRAPPER_START "<deviate value=\"replace\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003247 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003248 assert_int_equal(deviates->mod, LYS_DEV_REPLACE);
Radek Iša56ca9e42020-09-08 18:42:00 +02003249 lysp_deviate_free(UTEST_LYCTX, deviates);
David Sedlák4ffcec82019-07-25 15:10:21 +02003250 free(deviates);
3251 deviates = NULL;
3252
3253 data = ELEMENT_WRAPPER_START "<deviate value=\"delete\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003254 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003255 assert_int_equal(deviates->mod, LYS_DEV_DELETE);
Radek Iša56ca9e42020-09-08 18:42:00 +02003256 lysp_deviate_free(UTEST_LYCTX, deviates);
David Sedlák4ffcec82019-07-25 15:10:21 +02003257 free(deviates);
3258 deviates = NULL;
3259
3260 /* max subelems and valid arguments */
3261 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003262 "<deviate value=\"not-supported\">"
3263 EXT_SUBELEM
3264 "</deviate>"
3265 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003266 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003267 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
Radek Iša56ca9e42020-09-08 18:42:00 +02003268 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(deviates->exts[0]), LYEXT_SUBSTMT_SELF);
3269 lysp_deviate_free(UTEST_LYCTX, deviates);
David Sedlák4ffcec82019-07-25 15:10:21 +02003270 free(deviates);
3271 deviates = NULL;
3272
3273 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003274 "<deviate value=\"add\">\n"
3275 " <units name=\"units\"/>\n"
3276 " <must condition=\"cond\"/>\n"
3277 " <unique tag=\"utag\"/>\n"
3278 " <default value=\"def\"/>\n"
3279 " <config value=\"true\"/>\n"
3280 " <mandatory value=\"true\"/>\n"
3281 " <min-elements value=\"5\"/>\n"
3282 " <max-elements value=\"15\"/>\n"
3283 EXT_SUBELEM
3284 "</deviate>"
3285 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003286 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003287 d_add = (struct lysp_deviate_add *)deviates;
3288 assert_int_equal(d_add->mod, LYS_DEV_ADD);
3289 assert_null(d_add->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003290 assert_string_equal(d_add->units, "units");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003291 assert_string_equal(d_add->musts->arg.str, "cond");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003292 assert_string_equal(d_add->uniques[0].str, "utag");
3293 assert_string_equal(d_add->dflts[0].str, "def");
Radek Iša56ca9e42020-09-08 18:42:00 +02003294 assert_true((d_add->flags & LYS_MAND_TRUE) && (d_add->flags & LYS_CONFIG_W));
David Sedlák4ffcec82019-07-25 15:10:21 +02003295 assert_int_equal(d_add->min, 5);
3296 assert_int_equal(d_add->max, 15);
Radek Iša56ca9e42020-09-08 18:42:00 +02003297 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(deviates->exts[0]), LYEXT_SUBSTMT_SELF);
3298 lysp_deviate_free(UTEST_LYCTX, deviates);
David Sedlák4ffcec82019-07-25 15:10:21 +02003299 free(deviates);
3300 deviates = NULL;
3301
3302 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003303 "<deviate value=\"replace\">\n"
3304 " <type name=\"newtype\"/>\n"
3305 " <units name=\"uni\"/>\n"
3306 " <default value=\"def\"/>\n"
3307 " <config value=\"true\"/>\n"
3308 " <mandatory value=\"true\"/>\n"
3309 " <min-elements value=\"5\"/>\n"
3310 " <max-elements value=\"15\"/>\n"
3311 EXT_SUBELEM
3312 "</deviate>"
3313 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003314 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003315 d_rpl = (struct lysp_deviate_rpl *)deviates;
3316 assert_int_equal(d_rpl->mod, LYS_DEV_REPLACE);
3317 assert_null(d_rpl->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003318 assert_string_equal(d_rpl->type->name, "newtype");
3319 assert_string_equal(d_rpl->units, "uni");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003320 assert_string_equal(d_rpl->dflt.str, "def");
Radek Iša56ca9e42020-09-08 18:42:00 +02003321 assert_true((d_rpl->flags & LYS_MAND_TRUE) && (d_rpl->flags & LYS_CONFIG_W));
David Sedlák4ffcec82019-07-25 15:10:21 +02003322 assert_int_equal(d_rpl->min, 5);
3323 assert_int_equal(d_rpl->max, 15);
Radek Iša56ca9e42020-09-08 18:42:00 +02003324 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(deviates->exts[0]), LYEXT_SUBSTMT_SELF);
3325 lysp_deviate_free(UTEST_LYCTX, deviates);
David Sedlák4ffcec82019-07-25 15:10:21 +02003326 free(deviates);
3327 deviates = NULL;
3328
3329 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003330 "<deviate value=\"delete\">\n"
3331 " <units name=\"u\"/>\n"
3332 " <must condition=\"c\"/>\n"
3333 " <unique tag=\"tag\"/>\n"
3334 " <default value=\"default\"/>\n"
3335 EXT_SUBELEM
3336 "</deviate>"
3337 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003338 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003339 d_del = (struct lysp_deviate_del *)deviates;
3340 assert_int_equal(d_del->mod, LYS_DEV_DELETE);
3341 assert_null(d_del->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003342 assert_string_equal(d_del->units, "u");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003343 assert_string_equal(d_del->musts->arg.str, "c");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003344 assert_string_equal(d_del->uniques[0].str, "tag");
3345 assert_string_equal(d_del->dflts[0].str, "default");
Radek Iša56ca9e42020-09-08 18:42:00 +02003346 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(deviates->exts[0]), LYEXT_SUBSTMT_SELF);
3347 lysp_deviate_free(UTEST_LYCTX, deviates);
David Sedlák4ffcec82019-07-25 15:10:21 +02003348 free(deviates);
3349 deviates = NULL;
3350
3351 /* invalid arguments */
3352 data = ELEMENT_WRAPPER_START "<deviate value=\"\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003353 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
3354 CHECK_LOG_CTX("Invalid value \"\" of \"value\" attribute in \"deviate\" element. "
3355 "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", "Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003356 deviates = NULL;
3357
3358 data = ELEMENT_WRAPPER_START "<deviate value=\"invalid\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003359 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
3360 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"deviate\" element. "
3361 "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", "Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003362 deviates = NULL;
3363
3364 data = ELEMENT_WRAPPER_START "<deviate value=\"ad\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003365 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
3366 CHECK_LOG_CTX("Invalid value \"ad\" of \"value\" attribute in \"deviate\" element. "
3367 "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", "Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003368 deviates = NULL;
3369
3370 data = ELEMENT_WRAPPER_START "<deviate value=\"adds\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003371 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
3372 CHECK_LOG_CTX("Invalid value \"adds\" of \"value\" attribute in \"deviate\" element. "
3373 "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", "Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003374 deviates = NULL;
3375
3376 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003377 "<deviate value=\"not-supported\">\n"
3378 " <must condition=\"c\"/>\n"
3379 "</deviate>"
3380 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003381 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
3382 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 +02003383}
3384
David Sedlák8b754462019-07-25 16:22:13 +02003385static void
3386test_deviation_elem(void **state)
3387{
David Sedlák8b754462019-07-25 16:22:13 +02003388 const char *data;
3389 struct lysp_deviation *deviations = NULL;
3390
3391 /* min subelems */
3392 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003393 "<deviation target-node=\"target\">\n"
3394 " <deviate value=\"not-supported\"/>\n"
3395 "</deviation>"
3396 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003397 assert_int_equal(test_element_helper(state, data, &deviations, NULL, NULL), LY_SUCCESS);
David Sedlák8b754462019-07-25 16:22:13 +02003398 assert_string_equal(deviations->nodeid, "target");
3399 assert_int_equal(deviations->deviates->mod, LYS_DEV_NOT_SUPPORTED);
Radek Iša56ca9e42020-09-08 18:42:00 +02003400 FREE_ARRAY(UTEST_LYCTX, deviations, lysp_deviation_free);
David Sedlák8b754462019-07-25 16:22:13 +02003401 deviations = NULL;
3402
3403 /* max subelems */
3404 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003405 "<deviation target-node=\"target\">\n"
3406 " <reference><text>ref</text></reference>\n"
3407 " <description><text>desc</text></description>\n"
3408 " <deviate value=\"add\"/>\n"
3409 EXT_SUBELEM
3410 "</deviation>"
3411 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003412 assert_int_equal(test_element_helper(state, data, &deviations, NULL, NULL), LY_SUCCESS);
David Sedlák8b754462019-07-25 16:22:13 +02003413 assert_string_equal(deviations->nodeid, "target");
3414 assert_int_equal(deviations->deviates->mod, LYS_DEV_ADD);
3415 assert_string_equal(deviations->ref, "ref");
3416 assert_string_equal(deviations->dsc, "desc");
Radek Iša56ca9e42020-09-08 18:42:00 +02003417 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(deviations->exts[0]), LYEXT_SUBSTMT_SELF);
3418 FREE_ARRAY(UTEST_LYCTX, deviations, lysp_deviation_free);
David Sedlák8b754462019-07-25 16:22:13 +02003419 deviations = NULL;
3420
3421 /* invalid */
3422 data = ELEMENT_WRAPPER_START "<deviation target-node=\"target\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003423 assert_int_equal(test_element_helper(state, data, &deviations, NULL, NULL), LY_EVALID);
3424 FREE_ARRAY(UTEST_LYCTX, deviations, lysp_deviation_free);
David Sedlák8b754462019-07-25 16:22:13 +02003425 deviations = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02003426 CHECK_LOG_CTX("Missing mandatory sub-element \"deviate\" of \"deviation\" element.", "Line number 1.");
3427 /* TODO */}
David Sedlák8b754462019-07-25 16:22:13 +02003428
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003429static struct lysp_module *
3430mod_renew(struct lys_yin_parser_ctx *ctx)
3431{
3432 struct ly_ctx *ly_ctx = ctx->parsed_mod->mod->ctx;
3433
3434 lys_module_free(ctx->parsed_mod->mod, NULL);
3435 ctx->parsed_mod = calloc(1, sizeof *ctx->parsed_mod);
3436 ctx->parsed_mod->mod = calloc(1, sizeof *ctx->parsed_mod->mod);
3437 ctx->parsed_mod->mod->parsed = ctx->parsed_mod;
3438 ctx->parsed_mod->mod->ctx = ly_ctx;
3439
3440 return ctx->parsed_mod;
3441}
3442
David Sedlák4f03b932019-07-26 13:01:47 +02003443static void
3444test_module_elem(void **state)
3445{
Michal Vaskob36053d2020-03-26 15:49:30 +01003446 const char *data;
Radek Iša56ca9e42020-09-08 18:42:00 +02003447 struct lysp_module *lysp_mod = mod_renew(YCTX);
David Sedlák4f03b932019-07-26 13:01:47 +02003448
3449 /* max subelems */
David Sedlák4f03b932019-07-26 13:01:47 +02003450 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003451 " <yang-version value=\"1.1\"/>\n"
3452 " <namespace uri=\"ns\"/>\n"
3453 " <prefix value=\"pref\"/>\n"
3454 " <include module=\"b-mod\"/>\n"
3455 " <import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
3456 " <organization><text>org</text></organization>\n"
3457 " <contact><text>contact</text></contact>\n"
3458 " <description><text>desc</text></description>\n"
3459 " <reference><text>ref</text></reference>\n"
3460 " <revision date=\"2019-02-02\"/>\n"
3461 " <anydata name=\"anyd\"/>\n"
3462 " <anyxml name=\"anyx\"/>\n"
3463 " <choice name=\"choice\"/>\n"
3464 " <container name=\"cont\"/>\n"
3465 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3466 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3467 " <list name=\"sub-list\"/>\n"
3468 " <uses name=\"uses-name\"/>\n"
3469 " <augment target-node=\"target\"/>\n"
3470 " <deviation target-node=\"target\">\n"
3471 " <deviate value=\"not-supported\"/>\n"
3472 " </deviation>\n"
3473 " <extension name=\"ext\"/>\n"
3474 " <feature name=\"feature\"/>\n"
3475 " <grouping name=\"grp\"/>\n"
3476 " <identity name=\"ident-name\"/>\n"
3477 " <notification name=\"notf\"/>\n"
3478 " <rpc name=\"rpc-name\"/>\n"
3479 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3480 EXT_SUBELEM "\n"
3481 "</module>\n";
Radek Iša56ca9e42020-09-08 18:42:00 +02003482 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3483 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01003484
Radek Iša56ca9e42020-09-08 18:42:00 +02003485 assert_int_equal(yin_parse_mod(YCTX, lysp_mod), LY_SUCCESS);
David Sedlák4f03b932019-07-26 13:01:47 +02003486 assert_string_equal(lysp_mod->mod->name, "mod");
3487 assert_string_equal(lysp_mod->revs, "2019-02-02");
3488 assert_string_equal(lysp_mod->mod->ns, "ns");
3489 assert_string_equal(lysp_mod->mod->prefix, "pref");
3490 assert_null(lysp_mod->mod->filepath);
3491 assert_string_equal(lysp_mod->mod->org, "org");
3492 assert_string_equal(lysp_mod->mod->contact, "contact");
3493 assert_string_equal(lysp_mod->mod->dsc, "desc");
3494 assert_string_equal(lysp_mod->mod->ref, "ref");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003495 assert_int_equal(lysp_mod->version, LYS_VERSION_1_1);
Radek Iša56ca9e42020-09-08 18:42:00 +02003496 CHECK_LYSP_IMPORT(lysp_mod->imports, NULL, 0, "a-mod",
3497 "imp-pref", NULL, "");
David Sedlák4f03b932019-07-26 13:01:47 +02003498 assert_string_equal(lysp_mod->includes->name, "b-mod");
3499 assert_string_equal(lysp_mod->extensions->name, "ext");
3500 assert_string_equal(lysp_mod->features->name, "feature");
3501 assert_string_equal(lysp_mod->identities->name, "ident-name");
3502 assert_string_equal(lysp_mod->typedefs->name, "tpdf");
3503 assert_string_equal(lysp_mod->groupings->name, "grp");
3504 assert_string_equal(lysp_mod->data->name, "anyd");
3505 assert_int_equal(lysp_mod->data->nodetype, LYS_ANYDATA);
3506 assert_string_equal(lysp_mod->data->next->name, "anyx");
3507 assert_int_equal(lysp_mod->data->next->nodetype, LYS_ANYXML);
3508 assert_string_equal(lysp_mod->data->next->next->name, "choice");
3509 assert_int_equal(lysp_mod->data->next->next->nodetype, LYS_CHOICE);
3510 assert_string_equal(lysp_mod->data->next->next->next->name, "cont");
3511 assert_int_equal(lysp_mod->data->next->next->next->nodetype, LYS_CONTAINER);
3512 assert_string_equal(lysp_mod->data->next->next->next->next->name, "leaf");
3513 assert_int_equal(lysp_mod->data->next->next->next->next->nodetype, LYS_LEAF);
3514 assert_string_equal(lysp_mod->data->next->next->next->next->next->name, "llist");
3515 assert_int_equal(lysp_mod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3516 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->name, "sub-list");
3517 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
3518 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->next->name, "uses-name");
3519 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3520 assert_null(lysp_mod->data->next->next->next->next->next->next->next->next);
3521 assert_string_equal(lysp_mod->augments->nodeid, "target");
3522 assert_string_equal(lysp_mod->rpcs->name, "rpc-name");
3523 assert_string_equal(lysp_mod->notifs->name, "notf");
3524 assert_string_equal(lysp_mod->deviations->nodeid, "target");
Radek Iša56ca9e42020-09-08 18:42:00 +02003525 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(lysp_mod->exts[0]), LYEXT_SUBSTMT_SELF);
David Sedlák4f03b932019-07-26 13:01:47 +02003526
3527 /* min subelems */
Radek Iša56ca9e42020-09-08 18:42:00 +02003528 ly_in_free(UTEST_IN, 0);
3529 lyxml_ctx_free(YCTX->xmlctx);
3530 lysp_mod = mod_renew(YCTX);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003531 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
3532 " <namespace uri=\"ns\"/>\n"
3533 " <prefix value=\"pref\"/>\n"
3534 " <yang-version value=\"1.1\"/>\n"
3535 "</module>";
Radek Iša56ca9e42020-09-08 18:42:00 +02003536 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3537 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
3538 assert_int_equal(yin_parse_mod(YCTX, lysp_mod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003539 assert_string_equal(lysp_mod->mod->name, "mod");
David Sedlák298ff6d2019-07-26 14:29:03 +02003540
David Sedláke6cd89e2019-08-07 12:46:02 +02003541 /* incorrect subelem order */
Radek Iša56ca9e42020-09-08 18:42:00 +02003542 ly_in_free(UTEST_IN, 0);
3543 lyxml_ctx_free(YCTX->xmlctx);
3544 lysp_mod = mod_renew(YCTX);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003545 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
3546 " <feature name=\"feature\"/>\n"
3547 " <namespace uri=\"ns\"/>\n"
3548 " <prefix value=\"pref\"/>\n"
3549 " <yang-version value=\"1.1\"/>\n"
3550 "</module>";
Radek Iša56ca9e42020-09-08 18:42:00 +02003551 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3552 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
3553 assert_int_equal(yin_parse_mod(YCTX, lysp_mod), LY_EVALID);
3554 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 +02003555}
3556
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003557static struct lysp_submodule *
3558submod_renew(struct lys_yin_parser_ctx *ctx, const char *belongs_to)
3559{
3560 struct ly_ctx *ly_ctx = ctx->parsed_mod->mod->ctx;
3561
3562 lys_module_free(ctx->parsed_mod->mod, NULL);
3563 ctx->parsed_mod = calloc(1, sizeof(struct lysp_submodule));
3564 ctx->parsed_mod->mod = calloc(1, sizeof *ctx->parsed_mod->mod);
3565 lydict_insert(ly_ctx, belongs_to, 0, &ctx->parsed_mod->mod->name);
3566 ctx->parsed_mod->mod->parsed = ctx->parsed_mod;
3567 ctx->parsed_mod->mod->ctx = ly_ctx;
3568
3569 return (struct lysp_submodule *)ctx->parsed_mod;
3570}
3571
David Sedlák298ff6d2019-07-26 14:29:03 +02003572static void
3573test_submodule_elem(void **state)
3574{
Michal Vaskob36053d2020-03-26 15:49:30 +01003575 const char *data;
Radek Iša56ca9e42020-09-08 18:42:00 +02003576 struct lysp_submodule *lysp_submod = submod_renew(YCTX, "module-name");
David Sedlák298ff6d2019-07-26 14:29:03 +02003577
3578 /* max subelements */
David Sedlák298ff6d2019-07-26 14:29:03 +02003579 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003580 " <yang-version value=\"1.1\"/>\n"
3581 " <belongs-to module=\"module-name\">\n"
3582 " <prefix value=\"pref\"/>\n"
3583 " </belongs-to>\n"
3584 " <include module=\"b-mod\"/>\n"
3585 " <import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
3586 " <organization><text>org</text></organization>\n"
3587 " <contact><text>contact</text></contact>\n"
3588 " <description><text>desc</text></description>\n"
3589 " <reference><text>ref</text></reference>\n"
3590 " <revision date=\"2019-02-02\"/>\n"
3591 " <anydata name=\"anyd\"/>\n"
3592 " <anyxml name=\"anyx\"/>\n"
3593 " <choice name=\"choice\"/>\n"
3594 " <container name=\"cont\"/>\n"
3595 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3596 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3597 " <list name=\"sub-list\"/>\n"
3598 " <uses name=\"uses-name\"/>\n"
3599 " <augment target-node=\"target\"/>\n"
3600 " <deviation target-node=\"target\">\n"
3601 " <deviate value=\"not-supported\"/>\n"
3602 " </deviation>\n"
3603 " <extension name=\"ext\"/>\n"
3604 " <feature name=\"feature\"/>\n"
3605 " <grouping name=\"grp\"/>\n"
3606 " <identity name=\"ident-name\"/>\n"
3607 " <notification name=\"notf\"/>\n"
3608 " <rpc name=\"rpc-name\"/>\n"
3609 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3610 EXT_SUBELEM "\n"
3611 "</submodule>\n";
Radek Iša56ca9e42020-09-08 18:42:00 +02003612 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3613 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003614
Radek Iša56ca9e42020-09-08 18:42:00 +02003615 assert_int_equal(yin_parse_submod(YCTX, lysp_submod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003616 assert_string_equal(lysp_submod->name, "mod");
3617 assert_string_equal(lysp_submod->revs, "2019-02-02");
David Sedlák298ff6d2019-07-26 14:29:03 +02003618 assert_string_equal(lysp_submod->prefix, "pref");
3619 assert_null(lysp_submod->filepath);
3620 assert_string_equal(lysp_submod->org, "org");
3621 assert_string_equal(lysp_submod->contact, "contact");
3622 assert_string_equal(lysp_submod->dsc, "desc");
3623 assert_string_equal(lysp_submod->ref, "ref");
3624 assert_int_equal(lysp_submod->version, LYS_VERSION_1_1);
Radek Iša56ca9e42020-09-08 18:42:00 +02003625 CHECK_LYSP_IMPORT(lysp_submod->imports, NULL, 0, "a-mod",
3626 "imp-pref", NULL, "");
David Sedlák298ff6d2019-07-26 14:29:03 +02003627 assert_string_equal(lysp_submod->includes->name, "b-mod");
3628 assert_string_equal(lysp_submod->extensions->name, "ext");
3629 assert_string_equal(lysp_submod->features->name, "feature");
3630 assert_string_equal(lysp_submod->identities->name, "ident-name");
3631 assert_string_equal(lysp_submod->typedefs->name, "tpdf");
3632 assert_string_equal(lysp_submod->groupings->name, "grp");
3633 assert_string_equal(lysp_submod->data->name, "anyd");
3634 assert_int_equal(lysp_submod->data->nodetype, LYS_ANYDATA);
3635 assert_string_equal(lysp_submod->data->next->name, "anyx");
3636 assert_int_equal(lysp_submod->data->next->nodetype, LYS_ANYXML);
3637 assert_string_equal(lysp_submod->data->next->next->name, "choice");
3638 assert_int_equal(lysp_submod->data->next->next->nodetype, LYS_CHOICE);
3639 assert_string_equal(lysp_submod->data->next->next->next->name, "cont");
3640 assert_int_equal(lysp_submod->data->next->next->next->nodetype, LYS_CONTAINER);
3641 assert_string_equal(lysp_submod->data->next->next->next->next->name, "leaf");
3642 assert_int_equal(lysp_submod->data->next->next->next->next->nodetype, LYS_LEAF);
3643 assert_string_equal(lysp_submod->data->next->next->next->next->next->name, "llist");
3644 assert_int_equal(lysp_submod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3645 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->name, "sub-list");
3646 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
3647 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->next->name, "uses-name");
3648 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3649 assert_null(lysp_submod->data->next->next->next->next->next->next->next->next);
3650 assert_string_equal(lysp_submod->augments->nodeid, "target");
3651 assert_string_equal(lysp_submod->rpcs->name, "rpc-name");
3652 assert_string_equal(lysp_submod->notifs->name, "notf");
3653 assert_string_equal(lysp_submod->deviations->nodeid, "target");
Radek Iša56ca9e42020-09-08 18:42:00 +02003654 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(lysp_submod->exts[0]), LYEXT_SUBSTMT_SELF);
David Sedlák298ff6d2019-07-26 14:29:03 +02003655
David Sedlák298ff6d2019-07-26 14:29:03 +02003656 /* min subelemnts */
Radek Iša56ca9e42020-09-08 18:42:00 +02003657 ly_in_free(UTEST_IN, 0);
3658 lyxml_ctx_free(YCTX->xmlctx);
3659 lysp_submod = submod_renew(YCTX, "module-name");
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003660 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">\n"
3661 " <yang-version value=\"1\"/>\n"
3662 " <belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>\n"
3663 "</submodule>";
Radek Iša56ca9e42020-09-08 18:42:00 +02003664 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3665 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
3666 assert_int_equal(yin_parse_submod(YCTX, lysp_submod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003667 assert_string_equal(lysp_submod->prefix, "pref");
David Sedlák298ff6d2019-07-26 14:29:03 +02003668 assert_int_equal(lysp_submod->version, LYS_VERSION_1_0);
David Sedlák298ff6d2019-07-26 14:29:03 +02003669
David Sedláke6cd89e2019-08-07 12:46:02 +02003670 /* incorrect subelem order */
Radek Iša56ca9e42020-09-08 18:42:00 +02003671 ly_in_free(UTEST_IN, 0);
3672 lyxml_ctx_free(YCTX->xmlctx);
3673 lysp_submod = submod_renew(YCTX, "module-name");
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003674 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">\n"
3675 " <yang-version value=\"1\"/>\n"
3676 " <reference><text>ref</text></reference>\n"
3677 " <belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>\n"
3678 "</submodule>";
Radek Iša56ca9e42020-09-08 18:42:00 +02003679 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3680 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
3681 assert_int_equal(yin_parse_submod(YCTX, lysp_submod), LY_EVALID);
3682 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 +02003683}
3684
David Sedlák8985a142019-07-31 16:43:06 +02003685static void
3686test_yin_parse_module(void **state)
3687{
David Sedlák8985a142019-07-31 16:43:06 +02003688 const char *data;
3689 struct lys_module *mod;
Michal Vaskob36053d2020-03-26 15:49:30 +01003690 struct lys_yin_parser_ctx *yin_ctx = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02003691 struct ly_in *in = NULL;
Michal Vasko405cc9e2020-12-01 12:01:27 +01003692 struct lys_glob_unres unres = {0};
David Sedlák8985a142019-07-31 16:43:06 +02003693
3694 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003695 mod->ctx = UTEST_LYCTX;
David Sedlákd2844882019-09-13 16:01:22 +02003696 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 +01003697 " <yang-version value=\"1.1\"/>\n"
3698 " <namespace uri=\"urn:tests:extensions:metadata:a\"/>\n"
3699 " <prefix value=\"a\"/>\n"
3700 " <import module=\"ietf-yang-metadata\">\n"
3701 " <prefix value=\"md\"/>\n"
3702 " </import>\n"
3703 " <feature name=\"f\"/>\n"
3704 " <md:annotation name=\"x\">\n"
3705 " <description>\n"
3706 " <text>test</text>\n"
3707 " </description>\n"
3708 " <reference>\n"
3709 " <text>test</text>\n"
3710 " </reference>\n"
3711 " <if-feature name=\"f\"/>\n"
3712 " <status value=\"current\"/>\n"
3713 " <type name=\"uint8\"/>\n"
3714 " <units name=\"meters\"/>\n"
3715 " </md:annotation>\n"
3716 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003717 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko405cc9e2020-12-01 12:01:27 +01003718 assert_int_equal(yin_parse_module(&yin_ctx, in, mod, &unres), LY_SUCCESS);
David Sedlákd2844882019-09-13 16:01:22 +02003719 assert_null(mod->parsed->exts->child->next->child);
3720 assert_string_equal(mod->parsed->exts->child->next->arg, "test");
Radek Iša56ca9e42020-09-08 18:42:00 +02003721 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
David Sedlákd2844882019-09-13 16:01:22 +02003722 lys_module_free(mod, NULL);
3723 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003724 ly_in_free(in, 0);
David Sedlákd2844882019-09-13 16:01:22 +02003725 mod = NULL;
3726 yin_ctx = NULL;
3727
3728 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003729 mod->ctx = UTEST_LYCTX;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003730 data = "<module name=\"example-foo\""
3731 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
3732 " xmlns:foo=\"urn:example:foo\""
3733 " xmlns:myext=\"urn:example:extensions\">\n"
David Sedlák8985a142019-07-31 16:43:06 +02003734
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003735 " <yang-version value=\"1\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003736
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003737 " <namespace uri=\"urn:example:foo\"/>\n"
3738 " <prefix value=\"foo\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003739
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003740 " <import module=\"example-extensions\">\n"
3741 " <prefix value=\"myext\"/>\n"
3742 " </import>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003743
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003744 " <list name=\"interface\">\n"
3745 " <key value=\"name\"/>\n"
3746 " <leaf name=\"name\">\n"
3747 " <type name=\"string\"/>\n"
3748 " </leaf>\n"
3749 " <leaf name=\"mtu\">\n"
3750 " <type name=\"uint32\"/>\n"
3751 " <description>\n"
3752 " <text>The MTU of the interface.</text>\n"
3753 " </description>\n"
3754 " <myext:c-define name=\"MY_MTU\"/>\n"
3755 " </leaf>\n"
3756 " </list>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003757 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003758 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko405cc9e2020-12-01 12:01:27 +01003759 assert_int_equal(yin_parse_module(&yin_ctx, in, mod, &unres), LY_SUCCESS);
Radek Iša56ca9e42020-09-08 18:42:00 +02003760 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
David Sedlák8985a142019-07-31 16:43:06 +02003761 lys_module_free(mod, NULL);
3762 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003763 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02003764 mod = NULL;
3765 yin_ctx = NULL;
3766
3767 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003768 mod->ctx = UTEST_LYCTX;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003769 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3770 " <yang-version value=\"1\"/>\n"
3771 " <namespace uri=\"urn:example:foo\"/>\n"
3772 " <prefix value=\"foo\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003773 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003774 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko405cc9e2020-12-01 12:01:27 +01003775 assert_int_equal(yin_parse_module(&yin_ctx, in, mod, &unres), LY_SUCCESS);
Radek Iša56ca9e42020-09-08 18:42:00 +02003776 lys_compile_unres_glob_erase(UTEST_LYCTX, &unres);
David Sedlák6d781b62019-08-02 15:22:52 +02003777 lys_module_free(mod, NULL);
3778 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003779 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02003780 mod = NULL;
3781 yin_ctx = NULL;
3782
David Sedlák6d781b62019-08-02 15:22:52 +02003783 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003784 mod->ctx = UTEST_LYCTX;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003785 data = "<submodule name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlák8985a142019-07-31 16:43:06 +02003786 "</submodule>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003787 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko405cc9e2020-12-01 12:01:27 +01003788 assert_int_equal(yin_parse_module(&yin_ctx, in, mod, &unres), LY_EINVAL);
Radek Iša56ca9e42020-09-08 18:42:00 +02003789 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 +02003790 lys_module_free(mod, NULL);
3791 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003792 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02003793
David Sedlák6d781b62019-08-02 15:22:52 +02003794 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003795 mod->ctx = UTEST_LYCTX;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003796 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3797 " <yang-version value=\"1\"/>\n"
3798 " <namespace uri=\"urn:example:foo\"/>\n"
3799 " <prefix value=\"foo\"/>\n"
3800 "</module>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003801 "<module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003802 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vasko405cc9e2020-12-01 12:01:27 +01003803 assert_int_equal(yin_parse_module(&yin_ctx, in, mod, &unres), LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +02003804 CHECK_LOG_CTX("Trailing garbage \"<module>\" after module, expected end-of-input.", "Line number 6.");
David Sedlák6d781b62019-08-02 15:22:52 +02003805 lys_module_free(mod, NULL);
3806 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003807 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02003808 mod = NULL;
3809 yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003810}
3811
3812static void
3813test_yin_parse_submodule(void **state)
3814{
David Sedlák8985a142019-07-31 16:43:06 +02003815 const char *data;
Michal Vaskob36053d2020-03-26 15:49:30 +01003816 struct lys_yin_parser_ctx *yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003817 struct lysp_submodule *submod = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02003818 struct ly_in *in;
David Sedlák8985a142019-07-31 16:43:06 +02003819
Radek Iša56ca9e42020-09-08 18:42:00 +02003820 lydict_insert(UTEST_LYCTX, "a", 0, &YCTX->parsed_mod->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02003821
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003822 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003823 "<submodule name=\"asub\""
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003824 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
3825 " xmlns:a=\"urn:a\">\n"
3826 " <yang-version value=\"1\"/>\n"
3827 " <belongs-to module=\"a\">\n"
3828 " <prefix value=\"a_pref\"/>\n"
3829 " </belongs-to>\n"
3830 " <include module=\"atop\"/>\n"
3831 " <feature name=\"fox\"/>\n"
3832 " <notification name=\"bar-notif\">\n"
3833 " <if-feature name=\"bar\"/>\n"
3834 " </notification>\n"
3835 " <notification name=\"fox-notif\">\n"
3836 " <if-feature name=\"fox\"/>\n"
3837 " </notification>\n"
3838 " <augment target-node=\"/a_pref:top\">\n"
3839 " <if-feature name=\"bar\"/>\n"
3840 " <container name=\"bar-sub\"/>\n"
3841 " </augment>\n"
3842 " <augment target-node=\"/top\">\n"
3843 " <container name=\"bar-sub2\"/>\n"
3844 " </augment>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003845 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003846 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Radek Iša56ca9e42020-09-08 18:42:00 +02003847 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 +02003848 lysp_module_free((struct lysp_module *)submod);
David Sedlák8985a142019-07-31 16:43:06 +02003849 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003850 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02003851 yin_ctx = NULL;
3852 submod = NULL;
3853
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003854 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3855 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3856 " <yang-version value=\"1\"/>\n"
3857 " <belongs-to module=\"a\">\n"
3858 " <prefix value=\"a_pref\"/>\n"
3859 " </belongs-to>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003860 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003861 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Radek Iša56ca9e42020-09-08 18:42:00 +02003862 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 +02003863 lysp_module_free((struct lysp_module *)submod);
David Sedlák6d781b62019-08-02 15:22:52 +02003864 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003865 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02003866 yin_ctx = NULL;
3867 submod = NULL;
3868
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003869 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003870 "<module name=\"inval\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
3871 "</module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003872 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Radek Iša56ca9e42020-09-08 18:42:00 +02003873 assert_int_equal(yin_parse_submodule(&yin_ctx, UTEST_LYCTX, (struct lys_parser_ctx *)YCTX, in, &submod), LY_EINVAL);
3874 CHECK_LOG_CTX("Input data contains module in situation when a submodule is expected.", NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003875 lysp_module_free((struct lysp_module *)submod);
David Sedlák8985a142019-07-31 16:43:06 +02003876 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003877 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02003878 yin_ctx = NULL;
3879 submod = NULL;
3880
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003881 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3882 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3883 " <yang-version value=\"1\"/>\n"
3884 " <belongs-to module=\"a\">\n"
3885 " <prefix value=\"a_pref\"/>\n"
3886 " </belongs-to>\n"
3887 "</submodule>\n"
3888 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3889 " <yang-version value=\"1\"/>\n"
3890 " <belongs-to module=\"a\">\n"
3891 " <prefix value=\"a_pref\"/>\n"
3892 " </belongs-to>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003893 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003894 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Radek Iša56ca9e42020-09-08 18:42:00 +02003895 assert_int_equal(yin_parse_submodule(&yin_ctx, UTEST_LYCTX, (struct lys_parser_ctx *)YCTX, in, &submod), LY_EVALID);
3896 CHECK_LOG_CTX("Trailing garbage \"<submodule name...\" after submodule, expected end-of-input.", "Line number 8.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003897 lysp_module_free((struct lysp_module *)submod);
David Sedlák6d781b62019-08-02 15:22:52 +02003898 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003899 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02003900 yin_ctx = NULL;
3901 submod = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003902}
3903
David Sedlák3b4db242018-10-19 16:11:01 +02003904int
3905main(void)
3906{
3907
3908 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +02003909 UTEST(test_yin_match_keyword, setup, teardown),
3910 UTEST(test_yin_parse_element_generic, setup, teardown),
3911 UTEST(test_yin_parse_extension_instance, setup, teardown),
3912 UTEST(test_yin_parse_content, setup, teardown),
3913 UTEST(test_validate_value, setup, teardown),
David Sedlák32488102019-07-15 17:44:10 +02003914
Radek Iša56ca9e42020-09-08 18:42:00 +02003915 UTEST(test_yin_match_argument_name),
3916 cmocka_unit_test_setup_teardown(test_enum_elem, setup, teardown),
3917 cmocka_unit_test_setup_teardown(test_bit_elem, setup, teardown),
3918 cmocka_unit_test_setup_teardown(test_meta_elem, setup, teardown),
3919 cmocka_unit_test_setup_teardown(test_import_elem, setup, teardown),
3920 cmocka_unit_test_setup_teardown(test_status_elem, setup, teardown),
3921 cmocka_unit_test_setup_teardown(test_ext_elem, setup, teardown),
3922 cmocka_unit_test_setup_teardown(test_yin_element_elem, setup, teardown),
3923 cmocka_unit_test_setup_teardown(test_yangversion_elem, setup, teardown),
3924 cmocka_unit_test_setup_teardown(test_mandatory_elem, setup, teardown),
3925 cmocka_unit_test_setup_teardown(test_argument_elem, setup, teardown),
3926 cmocka_unit_test_setup_teardown(test_base_elem, setup, teardown),
3927 cmocka_unit_test_setup_teardown(test_belongsto_elem, setup, teardown),
3928 cmocka_unit_test_setup_teardown(test_config_elem, setup, teardown),
3929 cmocka_unit_test_setup_teardown(test_default_elem, setup, teardown),
3930 cmocka_unit_test_setup_teardown(test_err_app_tag_elem, setup, teardown),
3931 cmocka_unit_test_setup_teardown(test_err_msg_elem, setup, teardown),
3932 cmocka_unit_test_setup_teardown(test_fracdigits_elem, setup, teardown),
3933 cmocka_unit_test_setup_teardown(test_iffeature_elem, setup, teardown),
3934 cmocka_unit_test_setup_teardown(test_length_elem, setup, teardown),
3935 cmocka_unit_test_setup_teardown(test_modifier_elem, setup, teardown),
3936 cmocka_unit_test_setup_teardown(test_namespace_elem, setup, teardown),
3937 cmocka_unit_test_setup_teardown(test_pattern_elem, setup, teardown),
3938 cmocka_unit_test_setup_teardown(test_value_position_elem, setup, teardown),
3939 cmocka_unit_test_setup_teardown(test_prefix_elem, setup, teardown),
3940 cmocka_unit_test_setup_teardown(test_range_elem, setup, teardown),
3941 cmocka_unit_test_setup_teardown(test_reqinstance_elem, setup, teardown),
3942 cmocka_unit_test_setup_teardown(test_revision_date_elem, setup, teardown),
3943 cmocka_unit_test_setup_teardown(test_unique_elem, setup, teardown),
3944 cmocka_unit_test_setup_teardown(test_units_elem, setup, teardown),
3945 cmocka_unit_test_setup_teardown(test_when_elem, setup, teardown),
3946 cmocka_unit_test_setup_teardown(test_yin_text_value_elem, setup, teardown),
3947 cmocka_unit_test_setup_teardown(test_type_elem, setup, teardown),
3948 cmocka_unit_test_setup_teardown(test_max_elems_elem, setup, teardown),
3949 cmocka_unit_test_setup_teardown(test_min_elems_elem, setup, teardown),
3950 cmocka_unit_test_setup_teardown(test_ordby_elem, setup, teardown),
3951 cmocka_unit_test_setup_teardown(test_any_elem, setup, teardown),
3952 cmocka_unit_test_setup_teardown(test_leaf_elem, setup, teardown),
3953 cmocka_unit_test_setup_teardown(test_leaf_list_elem, setup, teardown),
3954 cmocka_unit_test_setup_teardown(test_presence_elem, setup, teardown),
3955 cmocka_unit_test_setup_teardown(test_key_elem, setup, teardown),
3956 cmocka_unit_test_setup_teardown(test_typedef_elem, setup, teardown),
3957 cmocka_unit_test_setup_teardown(test_refine_elem, setup, teardown),
3958 cmocka_unit_test_setup_teardown(test_uses_elem, setup, teardown),
3959 cmocka_unit_test_setup_teardown(test_revision_elem, setup, teardown),
3960 cmocka_unit_test_setup_teardown(test_include_elem, setup, teardown),
3961 cmocka_unit_test_setup_teardown(test_list_elem, setup, teardown),
3962 cmocka_unit_test_setup_teardown(test_notification_elem, setup, teardown),
3963 cmocka_unit_test_setup_teardown(test_grouping_elem, setup, teardown),
3964 cmocka_unit_test_setup_teardown(test_container_elem, setup, teardown),
3965 cmocka_unit_test_setup_teardown(test_case_elem, setup, teardown),
3966 cmocka_unit_test_setup_teardown(test_choice_elem, setup, teardown),
3967 cmocka_unit_test_setup_teardown(test_inout_elem, setup, teardown),
3968 cmocka_unit_test_setup_teardown(test_action_elem, setup, teardown),
3969 cmocka_unit_test_setup_teardown(test_augment_elem, setup, teardown),
3970 cmocka_unit_test_setup_teardown(test_deviate_elem, setup, teardown),
3971 cmocka_unit_test_setup_teardown(test_deviation_elem, setup, teardown),
3972 cmocka_unit_test_setup_teardown(test_module_elem, setup, teardown),
3973 cmocka_unit_test_setup_teardown(test_submodule_elem, setup, teardown),
David Sedlák8985a142019-07-31 16:43:06 +02003974
Radek Iša56ca9e42020-09-08 18:42:00 +02003975 cmocka_unit_test_setup_teardown(test_yin_parse_module, setup, teardown),
3976 cmocka_unit_test_setup_teardown(test_yin_parse_submodule, setup, teardown),
David Sedlák3b4db242018-10-19 16:11:01 +02003977 };
3978
Radek Iša56ca9e42020-09-08 18:42:00 +02003979 return cmocka_run_group_tests(tests, NULL, NULL);
David Sedlák3b4db242018-10-19 16:11:01 +02003980}