blob: 6b200164cf8bec48348b0374538be11922f7833e [file] [log] [blame]
David Sedlákb1ce3f82019-06-05 14:37:26 +02001/**
Michal Vasko12ef5362022-09-16 15:13:58 +02002 * @file test_yin.c
David Sedlákb1ce3f82019-06-05 14:37:26 +02003 * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz>
Michal Vasko12ef5362022-09-16 15:13:58 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
5 * @brief unit tests for YIN parser and printer
David Sedlákb1ce3f82019-06-05 14:37:26 +02006 *
Michal Vasko12ef5362022-09-16 15:13:58 +02007 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
David Sedlákb1ce3f82019-06-05 14:37:26 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
Radek Iša56ca9e42020-09-08 18:42:00 +020015#define _UTEST_MAIN_
16#include "utests.h"
David Sedlákb1ce3f82019-06-05 14:37:26 +020017
Radek Krejcib4ac5a92020-11-23 17:54:33 +010018#include <stdbool.h>
David Sedlák3b4db242018-10-19 16:11:01 +020019#include <stdio.h>
20#include <string.h>
21
Radek Krejci70593c12020-06-13 20:48:09 +020022#include "common.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020023#include "in.h"
Radek Krejci70593c12020-06-13 20:48:09 +020024#include "parser_internal.h"
Michal Vasko405cc9e2020-12-01 12:01:27 +010025#include "schema_compile.h"
Radek Krejci8df109d2021-04-23 12:19:08 +020026#include "tree.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010027#include "tree_edit.h"
Radek Krejci70593c12020-06-13 20:48:09 +020028#include "tree_schema.h"
29#include "tree_schema_internal.h"
30#include "xml.h"
31#include "xpath.h"
David Sedlák3b4db242018-10-19 16:11:01 +020032
Michal Vaskoafac7822020-10-20 14:22:26 +020033/* copied from parser_yin.c */
34enum yin_argument {
35 YIN_ARG_UNKNOWN = 0, /**< parsed argument can not be matched with any supported yin argument keyword */
36 YIN_ARG_NAME, /**< argument name */
37 YIN_ARG_TARGET_NODE, /**< argument target-node */
38 YIN_ARG_MODULE, /**< argument module */
39 YIN_ARG_VALUE, /**< argument value */
40 YIN_ARG_TEXT, /**< argument text */
41 YIN_ARG_CONDITION, /**< argument condition */
42 YIN_ARG_URI, /**< argument uri */
43 YIN_ARG_DATE, /**< argument data */
44 YIN_ARG_TAG, /**< argument tag */
45 YIN_ARG_NONE /**< empty (special value) */
46};
47
48struct yin_subelement {
49 enum ly_stmt type; /**< type of keyword */
50 void *dest; /**< meta infromation passed to responsible function (mostly information about where parsed subelement should be stored) */
51 uint16_t flags; /**< describes constraints of subelement can be set to YIN_SUBELEM_MANDATORY, YIN_SUBELEM_UNIQUE, YIN_SUBELEM_FIRST, YIN_SUBELEM_VER2, and YIN_SUBELEM_DEFAULT_TEXT */
52};
53
54struct import_meta {
55 const char *prefix; /**< module prefix. */
56 struct lysp_import **imports; /**< imports to add to. */
57};
58
59struct yin_argument_meta {
60 uint16_t *flags; /**< Argument flags */
61 const char **argument; /**< Argument value */
62};
63
64struct tree_node_meta {
65 struct lysp_node *parent; /**< parent node */
66 struct lysp_node **nodes; /**< linked list of siblings */
67};
68
69struct include_meta {
70 const char *name; /**< Module/submodule name. */
71 struct lysp_include **includes; /**< [Sized array](@ref sizedarrays) of parsed includes to add to. */
72};
73
74struct inout_meta {
75 struct lysp_node *parent; /**< Parent node. */
Radek Krejci2a9fc652021-01-22 17:44:34 +010076 struct lysp_node_action_inout *inout_p; /**< inout_p Input/output pointer to write to. */
Michal Vaskoafac7822020-10-20 14:22:26 +020077};
78
79struct minmax_dev_meta {
80 uint32_t *lim; /**< min/max value to write to. */
81 uint16_t *flags; /**< min/max flags to write to. */
82 struct lysp_ext_instance **exts; /**< extension instances to add to. */
83};
84
85#define YIN_SUBELEM_MANDATORY 0x01
86#define YIN_SUBELEM_UNIQUE 0x02
87#define YIN_SUBELEM_FIRST 0x04
88#define YIN_SUBELEM_VER2 0x08
89
90#define YIN_SUBELEM_PARSED 0x80
91
David Sedlák555c7202019-07-04 12:14:12 +020092/* prototypes of static functions */
Michal Vaskoafac7822020-10-20 14:22:26 +020093enum yin_argument yin_match_argument_name(const char *name, size_t len);
Michal Vaskod0625d72022-10-06 15:02:50 +020094LY_ERR yin_parse_content(struct lysp_yin_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size,
Michal Vaskoafac7822020-10-20 14:22:26 +020095 enum ly_stmt current_element, const char **text_content, struct lysp_ext_instance **exts);
Michal Vaskod0625d72022-10-06 15:02:50 +020096LY_ERR yin_validate_value(struct lysp_yin_ctx *ctx, enum yang_arg val_type);
97enum ly_stmt yin_match_keyword(struct lysp_yin_ctx *ctx, const char *name, size_t name_len,
Michal Vaskoafac7822020-10-20 14:22:26 +020098 const char *prefix, size_t prefix_len, enum ly_stmt parrent);
Michal Vaskod0625d72022-10-06 15:02:50 +020099LY_ERR yin_parse_extension_instance(struct lysp_yin_ctx *ctx, enum ly_stmt subelem, LY_ARRAY_COUNT_TYPE subelem_index,
Michal Vaskoafac7822020-10-20 14:22:26 +0200100 struct lysp_ext_instance **exts);
Michal Vaskod0625d72022-10-06 15:02:50 +0200101LY_ERR yin_parse_element_generic(struct lysp_yin_ctx *ctx, enum ly_stmt parent, struct lysp_stmt **element);
102LY_ERR yin_parse_mod(struct lysp_yin_ctx *ctx, struct lysp_module *mod);
103LY_ERR yin_parse_submod(struct lysp_yin_ctx *ctx, struct lysp_submodule *submod);
Michal Vaskoafac7822020-10-20 14:22:26 +0200104
David Sedláke6cd89e2019-08-07 12:46:02 +0200105/* wrapping element used for mocking has nothing to do with real module structure */
106#define ELEMENT_WRAPPER_START "<status xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
107#define ELEMENT_WRAPPER_END "</status>"
108
Radek Iša56ca9e42020-09-08 18:42:00 +0200109#define TEST_1_CHECK_LYSP_EXT_INSTANCE(NODE, INSUBSTMT)\
Radek Krejci8df109d2021-04-23 12:19:08 +0200110 CHECK_LYSP_EXT_INSTANCE((NODE), NULL, 1, INSUBSTMT, 0, "myext:c-define", LY_VALUE_XML)
David Sedlák872c7b42018-10-26 13:15:20 +0200111
Michal Vaskod0625d72022-10-06 15:02:50 +0200112struct lysp_yin_ctx *YCTX;
Michal Vaskoc636ea42022-09-16 10:20:31 +0200113struct lysf_ctx fctx;
David Sedlák8e7bda82019-07-16 17:57:50 +0200114
115static int
Radek Iša56ca9e42020-09-08 18:42:00 +0200116setup_ctx(void **state)
David Sedlák8e7bda82019-07-16 17:57:50 +0200117{
Michal Vasko8a67eff2021-12-07 14:04:47 +0100118 struct lysp_module *pmod;
119
David Sedlák619db942019-07-03 14:47:30 +0200120 /* allocate parser context */
Radek Iša56ca9e42020-09-08 18:42:00 +0200121 YCTX = calloc(1, sizeof(*YCTX));
122 YCTX->format = LYS_IN_YIN;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100123 ly_set_new(&YCTX->parsed_mods);
David Sedlák8f5bce02019-06-03 16:41:08 +0200124
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200125 /* allocate new parsed module */
Michal Vasko8a67eff2021-12-07 14:04:47 +0100126 pmod = calloc(1, sizeof *pmod);
127 ly_set_add(YCTX->parsed_mods, pmod, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200128
129 /* allocate new module */
Michal Vasko8a67eff2021-12-07 14:04:47 +0100130 pmod->mod = calloc(1, sizeof *pmod->mod);
131 pmod->mod->ctx = UTEST_LYCTX;
132 pmod->mod->parsed = pmod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200133
Radek Iša56ca9e42020-09-08 18:42:00 +0200134 return 0;
David Sedlák3b4db242018-10-19 16:11:01 +0200135}
136
137static int
Radek Iša56ca9e42020-09-08 18:42:00 +0200138setup(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +0100139{
Radek Iša56ca9e42020-09-08 18:42:00 +0200140 UTEST_SETUP;
David Sedlák68a1af12019-03-08 13:46:54 +0100141
Radek Iša56ca9e42020-09-08 18:42:00 +0200142 setup_ctx(state);
David Sedlák79e50cb2019-06-05 16:33:09 +0200143
Michal Vaskoc636ea42022-09-16 10:20:31 +0200144 fctx.ctx = UTEST_LYCTX;
145 fctx.mod = PARSER_CUR_PMOD(YCTX)->mod;
146
Radek Iša56ca9e42020-09-08 18:42:00 +0200147 return 0;
David Sedlák79e50cb2019-06-05 16:33:09 +0200148}
149
David Sedlák8985a142019-07-31 16:43:06 +0200150static int
Radek Iša56ca9e42020-09-08 18:42:00 +0200151teardown_ctx(void **UNUSED(state))
David Sedlák8985a142019-07-31 16:43:06 +0200152{
Michal Vaskoc636ea42022-09-16 10:20:31 +0200153 lys_module_free(&fctx, PARSER_CUR_PMOD(YCTX)->mod, 0);
Michal Vaskod0625d72022-10-06 15:02:50 +0200154 lysp_yin_ctx_free(YCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200155 YCTX = NULL;
David Sedlák8985a142019-07-31 16:43:06 +0200156
Radek Iša56ca9e42020-09-08 18:42:00 +0200157 return 0;
David Sedlák8985a142019-07-31 16:43:06 +0200158}
159
Radek Iša56ca9e42020-09-08 18:42:00 +0200160static int
161teardown(void **state)
162{
163 teardown_ctx(state);
164
Michal Vaskoc636ea42022-09-16 10:20:31 +0200165 lysf_ctx_erase(&fctx);
166
Radek Iša56ca9e42020-09-08 18:42:00 +0200167 UTEST_TEARDOWN;
168
169 return 0;
170}
171
172#define RESET_STATE \
173 ly_in_free(UTEST_IN, 0); \
174 UTEST_IN = NULL; \
175 teardown_ctx(state); \
176 setup_ctx(state)
177
David Sedlák68a1af12019-03-08 13:46:54 +0100178static void
David Sedlák1bccdfa2019-06-17 15:55:27 +0200179test_yin_match_keyword(void **state)
David Sedlák3b4db242018-10-19 16:11:01 +0200180{
Michal Vaskob36053d2020-03-26 15:49:30 +0100181 const char *prefix;
182 size_t prefix_len;
Radek Iša56ca9e42020-09-08 18:42:00 +0200183
David Sedlák8f7a1172019-06-20 14:42:18 +0200184 /* create mock yin namespace in xml context */
Radek Iša56ca9e42020-09-08 18:42:00 +0200185 ly_in_new_memory("<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" />", &UTEST_IN);
186 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
187 prefix = YCTX->xmlctx->prefix;
188 prefix_len = YCTX->xmlctx->prefix_len;
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100189
Radek Iša56ca9e42020-09-08 18:42:00 +0200190 assert_int_equal(yin_match_keyword(YCTX, "anydatax", strlen("anydatax"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
191 assert_int_equal(yin_match_keyword(YCTX, "asdasd", strlen("asdasd"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
192 assert_int_equal(yin_match_keyword(YCTX, "", 0, prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
193 assert_int_equal(yin_match_keyword(YCTX, "anydata", strlen("anydata"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ANYDATA);
194 assert_int_equal(yin_match_keyword(YCTX, "anyxml", strlen("anyxml"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ANYXML);
195 assert_int_equal(yin_match_keyword(YCTX, "argument", strlen("argument"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ARGUMENT);
196 assert_int_equal(yin_match_keyword(YCTX, "augment", strlen("augment"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_AUGMENT);
197 assert_int_equal(yin_match_keyword(YCTX, "base", strlen("base"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BASE);
198 assert_int_equal(yin_match_keyword(YCTX, "belongs-to", strlen("belongs-to"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BELONGS_TO);
199 assert_int_equal(yin_match_keyword(YCTX, "bit", strlen("bit"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BIT);
200 assert_int_equal(yin_match_keyword(YCTX, "case", strlen("case"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CASE);
201 assert_int_equal(yin_match_keyword(YCTX, "choice", strlen("choice"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CHOICE);
202 assert_int_equal(yin_match_keyword(YCTX, "config", strlen("config"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONFIG);
203 assert_int_equal(yin_match_keyword(YCTX, "contact", strlen("contact"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONTACT);
204 assert_int_equal(yin_match_keyword(YCTX, "container", strlen("container"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONTAINER);
205 assert_int_equal(yin_match_keyword(YCTX, "default", strlen("default"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEFAULT);
206 assert_int_equal(yin_match_keyword(YCTX, "description", strlen("description"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DESCRIPTION);
207 assert_int_equal(yin_match_keyword(YCTX, "deviate", strlen("deviate"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEVIATE);
208 assert_int_equal(yin_match_keyword(YCTX, "deviation", strlen("deviation"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEVIATION);
209 assert_int_equal(yin_match_keyword(YCTX, "enum", strlen("enum"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ENUM);
210 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);
211 assert_int_equal(yin_match_keyword(YCTX, "error-message", strlen("error-message"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ERROR_MESSAGE);
212 assert_int_equal(yin_match_keyword(YCTX, "extension", strlen("extension"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_EXTENSION);
213 assert_int_equal(yin_match_keyword(YCTX, "feature", strlen("feature"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_FEATURE);
214 assert_int_equal(yin_match_keyword(YCTX, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_FRACTION_DIGITS);
215 assert_int_equal(yin_match_keyword(YCTX, "grouping", strlen("grouping"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_GROUPING);
216 assert_int_equal(yin_match_keyword(YCTX, "identity", strlen("identity"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IDENTITY);
217 assert_int_equal(yin_match_keyword(YCTX, "if-feature", strlen("if-feature"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IF_FEATURE);
218 assert_int_equal(yin_match_keyword(YCTX, "import", strlen("import"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IMPORT);
219 assert_int_equal(yin_match_keyword(YCTX, "include", strlen("include"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_INCLUDE);
220 assert_int_equal(yin_match_keyword(YCTX, "input", strlen("input"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_INPUT);
221 assert_int_equal(yin_match_keyword(YCTX, "key", strlen("key"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_KEY);
222 assert_int_equal(yin_match_keyword(YCTX, "leaf", strlen("leaf"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LEAF);
223 assert_int_equal(yin_match_keyword(YCTX, "leaf-list", strlen("leaf-list"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LEAF_LIST);
224 assert_int_equal(yin_match_keyword(YCTX, "length", strlen("length"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LENGTH);
225 assert_int_equal(yin_match_keyword(YCTX, "list", strlen("list"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LIST);
226 assert_int_equal(yin_match_keyword(YCTX, "mandatory", strlen("mandatory"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MANDATORY);
227 assert_int_equal(yin_match_keyword(YCTX, "max-elements", strlen("max-elements"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MAX_ELEMENTS);
228 assert_int_equal(yin_match_keyword(YCTX, "min-elements", strlen("min-elements"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MIN_ELEMENTS);
229 assert_int_equal(yin_match_keyword(YCTX, "modifier", strlen("modifier"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MODIFIER);
230 assert_int_equal(yin_match_keyword(YCTX, "module", strlen("module"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MODULE);
231 assert_int_equal(yin_match_keyword(YCTX, "must", strlen("must"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MUST);
232 assert_int_equal(yin_match_keyword(YCTX, "namespace", strlen("namespace"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NAMESPACE);
233 assert_int_equal(yin_match_keyword(YCTX, "notification", strlen("notification"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NOTIFICATION);
234 assert_int_equal(yin_match_keyword(YCTX, "ordered-by", strlen("ordered-by"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ORDERED_BY);
235 assert_int_equal(yin_match_keyword(YCTX, "organization", strlen("organization"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ORGANIZATION);
236 assert_int_equal(yin_match_keyword(YCTX, "output", strlen("output"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_OUTPUT);
237 assert_int_equal(yin_match_keyword(YCTX, "path", strlen("path"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PATH);
238 assert_int_equal(yin_match_keyword(YCTX, "pattern", strlen("pattern"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PATTERN);
239 assert_int_equal(yin_match_keyword(YCTX, "position", strlen("position"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_POSITION);
240 assert_int_equal(yin_match_keyword(YCTX, "prefix", strlen("prefix"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PREFIX);
241 assert_int_equal(yin_match_keyword(YCTX, "presence", strlen("presence"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PRESENCE);
242 assert_int_equal(yin_match_keyword(YCTX, "range", strlen("range"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_RANGE);
243 assert_int_equal(yin_match_keyword(YCTX, "reference", strlen("reference"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REFERENCE);
244 assert_int_equal(yin_match_keyword(YCTX, "refine", strlen("refine"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REFINE);
245 assert_int_equal(yin_match_keyword(YCTX, "require-instance", strlen("require-instance"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REQUIRE_INSTANCE);
246 assert_int_equal(yin_match_keyword(YCTX, "revision", strlen("revision"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REVISION);
247 assert_int_equal(yin_match_keyword(YCTX, "revision-date", strlen("revision-date"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REVISION_DATE);
248 assert_int_equal(yin_match_keyword(YCTX, "rpc", strlen("rpc"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_RPC);
249 assert_int_equal(yin_match_keyword(YCTX, "status", strlen("status"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_STATUS);
250 assert_int_equal(yin_match_keyword(YCTX, "submodule", strlen("submodule"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_SUBMODULE);
251 assert_int_equal(yin_match_keyword(YCTX, "type", strlen("type"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_TYPE);
252 assert_int_equal(yin_match_keyword(YCTX, "typedef", strlen("typedef"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_TYPEDEF);
253 assert_int_equal(yin_match_keyword(YCTX, "unique", strlen("unique"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_UNIQUE);
254 assert_int_equal(yin_match_keyword(YCTX, "units", strlen("units"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_UNITS);
255 assert_int_equal(yin_match_keyword(YCTX, "uses", strlen("uses"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_USES);
256 assert_int_equal(yin_match_keyword(YCTX, "value", strlen("value"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_VALUE);
257 assert_int_equal(yin_match_keyword(YCTX, "when", strlen("when"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_WHEN);
258 assert_int_equal(yin_match_keyword(YCTX, "yang-version", strlen("yang-version"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_YANG_VERSION);
259 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 +0200260}
David Sedlák3b4db242018-10-19 16:11:01 +0200261
David Sedlák872c7b42018-10-26 13:15:20 +0200262static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200263test_yin_match_argument_name(void **UNUSED(state))
David Sedlák872c7b42018-10-26 13:15:20 +0200264{
David Sedlák060b00e2019-06-19 11:12:06 +0200265 assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN);
266 assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN);
267 assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN);
268 assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION);
269 assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE);
270 assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE);
271 assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME);
272 assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG);
273 assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
274 assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT);
275 assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI);
276 assert_int_equal(yin_match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200277}
278
David Sedlák68a1af12019-03-08 13:46:54 +0100279static void
David Sedlák555c7202019-07-04 12:14:12 +0200280test_yin_parse_content(void **state)
281{
David Sedlák555c7202019-07-04 12:14:12 +0200282 LY_ERR ret = LY_SUCCESS;
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100283 const char *data =
284 "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
285 " <myext:custom xmlns:myext=\"urn:example:extensions\">totally amazing extension</myext:custom>\n"
286 " <extension name=\"ext\">\n"
287 " <argument name=\"argname\"></argument>\n"
288 " <description><text>desc</text></description>\n"
289 " <reference><text>ref</text></reference>\n"
290 " <status value=\"deprecated\"></status>\n"
291 " </extension>\n"
292 " <text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>\n"
293 " <if-feature name=\"foo\"></if-feature>\n"
294 " <when condition=\"condition...\">\n"
295 " <reference><text>when_ref</text></reference>\n"
296 " <description><text>when_desc</text></description>\n"
297 " </when>\n"
298 " <config value=\"true\"/>\n"
299 " <error-message>\n"
300 " <value>error-msg</value>\n"
301 " </error-message>\n"
302 " <error-app-tag value=\"err-app-tag\"/>\n"
303 " <units name=\"radians\"></units>\n"
304 " <default value=\"default-value\"/>\n"
305 " <position value=\"25\"></position>\n"
306 " <value value=\"-5\"/>\n"
307 " <require-instance value=\"true\"></require-instance>\n"
308 " <range value=\"5..10\" />\n"
309 " <length value=\"baf\"/>\n"
310 " <pattern value='pattern'>\n"
311 " <modifier value='invert-match'/>\n"
312 " </pattern>\n"
313 " <enum name=\"yay\">\n"
314 " </enum>\n"
315 "</prefix>";
David Sedlák555c7202019-07-04 12:14:12 +0200316 struct lysp_ext_instance *exts = NULL;
Michal Vasko12ef5362022-09-16 15:13:58 +0200317 const char *value;
David Sedlák555c7202019-07-04 12:14:12 +0200318
319 /* test unique subelem */
320 const char *prefix_value;
Radek Krejcid6b76452019-09-03 17:03:03 +0200321 struct yin_subelement subelems2[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE},
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100322 {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_UNIQUE}};
323
David Sedláke6cd89e2019-08-07 12:46:02 +0200324 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100325 "<prefix value=\"inv_mod\" />"
326 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
327 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
328 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200329 ly_in_new_memory(data, &UTEST_IN);
330 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
331 lyxml_ctx_next(YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100332
Radek Iša56ca9e42020-09-08 18:42:00 +0200333 ret = yin_parse_content(YCTX, subelems2, 2, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200334 assert_int_equal(ret, LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +0200335 CHECK_LOG_CTX("Redefinition of \"text\" sub-element in \"status\" element.", "Line number 1.");
336 lydict_remove(UTEST_LYCTX, prefix_value);
337 lydict_remove(UTEST_LYCTX, value);
338 RESET_STATE;
David Sedlák555c7202019-07-04 12:14:12 +0200339
340 /* test first subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200341 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100342 "<prefix value=\"inv_mod\" />"
343 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
344 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
345 ELEMENT_WRAPPER_END;
Radek Krejcid6b76452019-09-03 17:03:03 +0200346 struct yin_subelement subelems3[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE},
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100347 {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_FIRST}};
348
Radek Iša56ca9e42020-09-08 18:42:00 +0200349 ly_in_new_memory(data, &UTEST_IN);
350 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
351 lyxml_ctx_next(YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100352
Radek Iša56ca9e42020-09-08 18:42:00 +0200353 ret = yin_parse_content(YCTX, subelems3, 2, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200354 assert_int_equal(ret, LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +0200355 CHECK_LOG_CTX("Sub-element \"text\" of \"status\" element must be defined as it's first sub-element.", "Line number 1.");
356 lydict_remove(UTEST_LYCTX, prefix_value);
357 RESET_STATE;
David Sedlák555c7202019-07-04 12:14:12 +0200358
359 /* test mandatory subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200360 data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END;
Radek Krejcid6b76452019-09-03 17:03:03 +0200361 struct yin_subelement subelems4[1] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}};
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100362
Radek Iša56ca9e42020-09-08 18:42:00 +0200363 ly_in_new_memory(data, &UTEST_IN);
364 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
365 lyxml_ctx_next(YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100366
Radek Iša56ca9e42020-09-08 18:42:00 +0200367 ret = yin_parse_content(YCTX, subelems4, 1, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200368 assert_int_equal(ret, LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +0200369 CHECK_LOG_CTX("Missing mandatory sub-element \"prefix\" of \"status\" element.", "Line number 1.");
David Sedlák555c7202019-07-04 12:14:12 +0200370}
371
David Sedlák92147b02019-07-09 14:01:01 +0200372static void
David Sedlák4a650532019-07-10 11:55:18 +0200373test_validate_value(void **state)
374{
Michal Vaskob36053d2020-03-26 15:49:30 +0100375 const char *data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END;
376
377 /* create some XML context */
Radek Iša56ca9e42020-09-08 18:42:00 +0200378 ly_in_new_memory(data, &UTEST_IN);
379 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
380 YCTX->xmlctx->status = LYXML_ELEM_CONTENT;
381 YCTX->xmlctx->dynamic = 0;
Michal Vaskob36053d2020-03-26 15:49:30 +0100382
Radek Iša56ca9e42020-09-08 18:42:00 +0200383 YCTX->xmlctx->value = "#invalid";
384 YCTX->xmlctx->value_len = 8;
385 assert_int_equal(yin_validate_value(YCTX, Y_IDENTIF_ARG), LY_EVALID);
Radek Iša2eac74c2021-03-15 15:53:08 +0100386 CHECK_LOG_CTX("Invalid identifier first character '#' (0x0023).", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100387
Radek Iša56ca9e42020-09-08 18:42:00 +0200388 YCTX->xmlctx->value = "";
389 YCTX->xmlctx->value_len = 0;
390 assert_int_equal(yin_validate_value(YCTX, Y_STR_ARG), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +0100391
Radek Iša56ca9e42020-09-08 18:42:00 +0200392 YCTX->xmlctx->value = "pre:b";
393 YCTX->xmlctx->value_len = 5;
394 assert_int_equal(yin_validate_value(YCTX, Y_IDENTIF_ARG), LY_EVALID);
395 assert_int_equal(yin_validate_value(YCTX, Y_PREF_IDENTIF_ARG), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +0100396
Radek Iša56ca9e42020-09-08 18:42:00 +0200397 YCTX->xmlctx->value = "pre:pre:b";
398 YCTX->xmlctx->value_len = 9;
399 assert_int_equal(yin_validate_value(YCTX, Y_PREF_IDENTIF_ARG), LY_EVALID);
David Sedlák4a650532019-07-10 11:55:18 +0200400}
401
Michal Vasko12ef5362022-09-16 15:13:58 +0200402static void
403test_valid_module(void **state)
404{
405 struct lys_module *mod;
406 char *printed;
407 const char *links_yin =
408 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
409 "<module name=\"links\"\n"
410 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
411 " xmlns:mod2=\"urn:module2\">\n"
412 " <yang-version value=\"1.1\"/>\n"
413 " <namespace uri=\"urn:module2\"/>\n"
414 " <prefix value=\"mod2\"/>\n"
415 " <identity name=\"just-another-identity\"/>\n"
416 " <grouping name=\"rgroup\">\n"
417 " <leaf name=\"rg1\">\n"
418 " <type name=\"string\"/>\n"
419 " </leaf>\n"
420 " <leaf name=\"rg2\">\n"
421 " <type name=\"string\"/>\n"
422 " </leaf>\n"
423 " </grouping>\n"
424 " <leaf name=\"one-leaf\">\n"
425 " <type name=\"string\"/>\n"
426 " </leaf>\n"
427 " <list name=\"list-for-augment\">\n"
428 " <key value=\"keyleaf\"/>\n"
429 " <leaf name=\"keyleaf\">\n"
430 " <type name=\"string\"/>\n"
431 " </leaf>\n"
432 " <leaf name=\"just-leaf\">\n"
433 " <type name=\"int32\"/>\n"
434 " </leaf>\n"
435 " </list>\n"
436 " <leaf name=\"rleaf\">\n"
437 " <type name=\"string\"/>\n"
438 " </leaf>\n"
439 " <leaf-list name=\"llist\">\n"
440 " <type name=\"string\"/>\n"
441 " <min-elements value=\"0\"/>\n"
442 " <max-elements value=\"100\"/>\n"
443 " <ordered-by value=\"user\"/>\n"
444 " </leaf-list>\n"
445 "</module>\n";
446 const char *statements_yin =
447 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
448 "<module name=\"statements\"\n"
449 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
450 " xmlns:mod=\"urn:module\"\n"
451 " xmlns:mod2=\"urn:module2\">\n"
452 " <yang-version value=\"1.1\"/>\n"
453 " <namespace uri=\"urn:module\"/>\n"
454 " <prefix value=\"mod\"/>\n"
455 " <import module=\"links\">\n"
456 " <prefix value=\"mod2\"/>\n"
457 " </import>\n"
458 " <extension name=\"ext\"/>\n"
459 " <identity name=\"random-identity\">\n"
460 " <base name=\"mod2:just-another-identity\"/>\n"
461 " <base name=\"another-identity\"/>\n"
462 " </identity>\n"
463 " <identity name=\"another-identity\">\n"
464 " <base name=\"mod2:just-another-identity\"/>\n"
465 " </identity>\n"
466 " <typedef name=\"percent\">\n"
467 " <type name=\"uint8\">\n"
468 " <range value=\"0 .. 100\"/>\n"
469 " </type>\n"
470 " <units name=\"percent\"/>\n"
471 " </typedef>\n"
472 " <list name=\"list1\">\n"
473 " <key value=\"a\"/>\n"
474 " <leaf name=\"a\">\n"
475 " <type name=\"string\"/>\n"
476 " </leaf>\n"
477 " <leaf name=\"x\">\n"
478 " <type name=\"string\"/>\n"
479 " </leaf>\n"
480 " <leaf name=\"y\">\n"
481 " <type name=\"string\"/>\n"
482 " </leaf>\n"
483 " </list>\n"
484 " <container name=\"ice-cream-shop\">\n"
485 " <container name=\"employees\">\n"
486 " <when condition=\"/list1/x\"/>\n"
487 " <list name=\"employee\">\n"
488 " <key value=\"id\"/>\n"
489 " <unique tag=\"name\"/>\n"
490 " <config value=\"true\"/>\n"
491 " <min-elements value=\"0\">\n"
492 " <mod:ext/>\n"
493 " </min-elements>\n"
494 " <max-elements value=\"unbounded\"/>\n"
495 " <leaf name=\"id\">\n"
496 " <type name=\"uint64\"/>\n"
497 " <mandatory value=\"true\"/>\n"
498 " </leaf>\n"
499 " <leaf name=\"name\">\n"
500 " <type name=\"string\"/>\n"
501 " </leaf>\n"
502 " <leaf name=\"age\">\n"
503 " <type name=\"uint32\"/>\n"
504 " </leaf>\n"
505 " </list>\n"
506 " </container>\n"
507 " </container>\n"
508 " <container name=\"random\">\n"
509 " <grouping name=\"group\">\n"
510 " <leaf name=\"g1\">\n"
511 " <type name=\"percent\"/>\n"
512 " <mandatory value=\"false\"/>\n"
513 " </leaf>\n"
514 " <leaf name=\"g2\">\n"
515 " <type name=\"string\"/>\n"
516 " </leaf>\n"
517 " </grouping>\n"
518 " <choice name=\"switch\">\n"
519 " <case name=\"a\">\n"
520 " <leaf name=\"aleaf\">\n"
521 " <type name=\"string\"/>\n"
522 " <default value=\"aaa\"/>\n"
523 " </leaf>\n"
524 " </case>\n"
525 " <case name=\"c\">\n"
526 " <leaf name=\"cleaf\">\n"
527 " <type name=\"string\"/>\n"
528 " </leaf>\n"
529 " </case>\n"
530 " </choice>\n"
531 " <anyxml name=\"xml-data\"/>\n"
532 " <anydata name=\"any-data\"/>\n"
533 " <leaf-list name=\"leaflist\">\n"
534 " <type name=\"string\"/>\n"
535 " <min-elements value=\"0\"/>\n"
536 " <max-elements value=\"20\"/>\n"
537 " </leaf-list>\n"
538 " <uses name=\"group\"/>\n"
539 " <uses name=\"mod2:rgroup\"/>\n"
540 " <leaf name=\"lref\">\n"
541 " <type name=\"leafref\">\n"
542 " <path value=\"/mod2:one-leaf\"/>\n"
543 " </type>\n"
544 " </leaf>\n"
545 " <leaf name=\"iref\">\n"
546 " <type name=\"identityref\">\n"
547 " <base name=\"mod2:just-another-identity\"/>\n"
548 " </type>\n"
549 " </leaf>\n"
550 " </container>\n"
551 " <augment target-node=\"/random\">\n"
552 " <leaf name=\"aug-leaf\">\n"
553 " <type name=\"string\"/>\n"
554 " </leaf>\n"
555 " </augment>\n"
556 " <notification name=\"notif\"/>\n"
557 " <deviation target-node=\"/mod:ice-cream-shop/mod:employees/mod:employee/mod:age\">\n"
558 " <deviate value=\"not-supported\">\n"
559 " <mod:ext/>\n"
560 " </deviate>\n"
561 " </deviation>\n"
562 " <deviation target-node=\"/mod:list1\">\n"
563 " <deviate value=\"add\">\n"
564 " <mod:ext/>\n"
565 " <must condition=\"1\"/>\n"
566 " <must condition=\"2\"/>\n"
567 " <unique tag=\"x\"/>\n"
568 " <unique tag=\"y\"/>\n"
569 " <config value=\"true\"/>\n"
570 " <min-elements value=\"1\"/>\n"
571 " <max-elements value=\"2\"/>\n"
572 " </deviate>\n"
573 " </deviation>\n"
574 " <deviation target-node=\"/mod:ice-cream-shop/mod:employees/mod:employee\">\n"
575 " <deviate value=\"delete\">\n"
576 " <unique tag=\"name\"/>\n"
577 " </deviate>\n"
578 " </deviation>\n"
579 " <deviation target-node=\"/mod:random/mod:leaflist\">\n"
580 " <deviate value=\"replace\">\n"
581 " <type name=\"uint32\"/>\n"
582 " <min-elements value=\"10\"/>\n"
583 " <max-elements value=\"15\"/>\n"
584 " </deviate>\n"
585 " </deviation>\n"
586 "</module>\n";
587
588 UTEST_ADD_MODULE(links_yin, LYS_IN_YIN, NULL, NULL);
589 UTEST_ADD_MODULE(statements_yin, LYS_IN_YIN, NULL, &mod);
590 lys_print_mem(&printed, mod, LYS_OUT_YIN, 0);
591 assert_string_equal(printed, statements_yin);
592 free(printed);
593}
594
595static void
596test_print_module(void **state)
597{
598 struct lys_module *mod;
599
600 char *orig = malloc(8096);
601
602 strcpy(orig,
603 "module all {\n"
604 " yang-version 1.1;\n"
605 " namespace \"urn:all\";\n"
606 " prefix all_mod;\n\n"
607 " import ietf-yang-types {\n"
608 " prefix yt;\n"
609 " revision-date 2013-07-15;\n"
610 " description\n"
611 " \"YANG types\";\n"
612 " reference\n"
613 " \"RFC reference\";\n"
614 " }\n\n"
615 " feature feat1 {\n"
616 " if-feature \"feat2\";\n"
617 " status obsolete;\n"
618 " }\n\n"
619 " feature feat2;\n"
620 " feature feat3;\n\n"
621 " identity ident2 {\n"
622 " base ident1;\n"
623 " }\n\n"
624 " identity ident1;\n\n"
625 " typedef tdef1 {\n"
626 " type tdef2 {\n"
627 " length \"3..9 | 30..40\";\n"
628 " pattern \"[ac]*\";\n"
629 " }\n"
630 " units \"none\";\n"
631 " default \"aaa\";\n"
632 " }\n\n"
633 " typedef tdef2 {\n"
634 " type string {\n"
635 " length \"2..10 | 20..50\";\n"
636 " pattern \"[ab]*\";\n"
637 " }\n"
638 " }\n\n"
639 " grouping group1 {\n"
640 " leaf leaf1 {\n"
641 " type int8;\n"
642 " }\n"
643 " }\n\n"
644 " container cont1 {\n"
645 " leaf leaf2 {\n"
646 " if-feature \"feat1\";\n"
647 " type int16;\n"
648 " status obsolete;\n"
649 " }\n\n"
650 " uses group1 {\n"
651 " if-feature \"feat2\";\n"
652 " refine \"leaf1\" {\n"
653 " if-feature \"feat3\";\n"
654 " must \"24 - 4 = number('20')\";\n"
655 " default \"25\";\n"
656 " config true;\n"
657 " mandatory false;\n"
658 " description\n"
659 " \"dsc\";\n"
660 " reference\n"
661 " \"none\";\n"
662 " }\n"
663 " }\n\n"
664 " leaf leaf3 {\n"
665 " type int32;\n"
666 " }\n\n"
667 " leaf leaf4 {\n"
668 " type int64 {\n"
669 " range \"1000 .. 50000\" {\n"
670 " error-message\n"
671 " \"Special error message.\";\n"
672 " error-app-tag \"special-tag\";\n"
673 " }\n"
674 " }\n"
675 " }\n\n"
676 " leaf leaf5 {\n"
677 " type uint8;\n"
678 " }\n\n"
679 " leaf leaf6 {\n"
680 " type uint16;\n"
681 " }\n\n"
682 " leaf leaf7 {\n"
683 " type uint32;\n"
684 " }\n\n"
685 " leaf leaf8 {\n"
686 " type uint64;\n"
687 " }\n\n"
688 " choice choic1 {\n"
689 " default \"leaf9b\";\n"
690 " leaf leaf9a {\n"
691 " type decimal64 {\n"
692 " fraction-digits 9;\n"
693 " }\n"
694 " }\n\n"
695 " leaf leaf9b {\n"
696 " type boolean;\n"
697 " default \"false\";\n"
698 " }\n"
699 " }\n\n"
700 " leaf leaf10 {\n"
701 " type boolean;\n"
702 " }\n\n");
703 strcpy(orig + strlen(orig),
704 " leaf leaf11 {\n"
705 " type enumeration {\n"
706 " enum \"one\";\n"
707 " enum \"two\";\n"
708 " enum \"five\" {\n"
709 " value 5;\n"
710 " }\n"
711 " }\n"
712 " }\n\n"
713 " leaf leaf12 {\n"
714 " type bits {\n"
715 " bit flag0 {\n"
716 " position 0;\n"
717 " }\n"
718 " bit flag1;\n"
719 " bit flag2 {\n"
720 " position 2;\n"
721 " }\n"
722 " bit flag3 {\n"
723 " position 3;\n"
724 " }\n"
725 " }\n"
726 " default \"flag0 flag3\";\n"
727 " }\n\n"
728 " leaf leaf13 {\n"
729 " type binary;\n"
730 " }\n\n"
731 " leaf leaf14 {\n"
732 " type leafref {\n"
733 " path \"/cont1/leaf17\";\n"
734 " }\n"
735 " }\n\n"
736 " leaf leaf15 {\n"
737 " type empty;\n"
738 " }\n\n"
739 " leaf leaf16 {\n"
740 " type union {\n"
741 " type instance-identifier {\n"
742 " require-instance true;\n"
743 " }\n"
744 " type int8;\n"
745 " }\n"
746 " }\n\n"
747 " list list1 {\n"
748 " key \"leaf18\";\n"
749 " unique \"leaf19\";\n"
750 " min-elements 1;\n"
751 " max-elements 20;\n"
752 " leaf leaf18 {\n"
753 " type string;\n"
754 " }\n\n"
755 " leaf leaf19 {\n"
756 " type uint32;\n"
757 " }\n\n"
758 " anyxml axml1;\n"
759 " anydata adata1;\n\n"
760 " action act1 {\n"
761 " input {\n"
762 " leaf leaf24 {\n"
763 " type string;\n"
764 " }\n"
765 " }\n\n"
766 " output {\n"
767 " leaf leaf25 {\n"
768 " type string;\n"
769 " }\n"
770 " }\n"
771 " }\n\n"
772 " notification notif1 {\n"
773 " leaf leaf26 {\n"
774 " type string;\n"
775 " }\n"
776 " }\n"
777 " }\n\n"
778 " leaf-list llist1 {\n"
779 " type tdef1;\n"
780 " ordered-by user;\n"
781 " }\n\n"
782 " list list2 {\n"
783 " key \"leaf27 leaf28\";\n"
784 " leaf leaf27 {\n"
785 " type uint8;\n"
786 " }\n\n"
787 " leaf leaf28 {\n"
788 " type uint8;\n"
789 " }\n"
790 " }\n\n"
791 " leaf leaf29 {\n"
792 " type instance-identifier;\n"
793 " }\n\n"
794 " container must-deviations-container {\n"
795 " presence \"Allows deviations on the leaf\";\n"
796 " leaf leaf30 {\n"
797 " type string;\n"
798 " }\n"
799 " }\n\n"
800 " leaf leaf23 {\n"
801 " type empty;\n"
802 " }\n"
803 " }\n\n"
804 " augment \"/cont1\" {\n"
805 " leaf leaf17 {\n"
806 " type string;\n"
807 " }\n"
808 " }\n\n"
809 " rpc rpc1 {\n"
810 " input {\n"
811 " leaf leaf20 {\n"
812 " type tdef1;\n"
813 " }\n"
814 " }\n\n"
815 " output {\n"
816 " container cont2 {\n"
817 " leaf leaf21 {\n"
818 " type empty;\n"
819 " }\n"
820 " }\n"
821 " }\n"
822 " }\n\n"
823 " container test-when {\n"
824 " leaf when-check {\n"
825 " type boolean;\n"
826 " }\n\n"
827 " leaf gated-data {\n"
828 " when \"../when-check = 'true'\";\n"
829 " type uint16;\n"
830 " }\n"
831 " }\n\n"
832 " extension c-define {\n"
833 " description\n"
834 " \"Takes as an argument a name string.\n"
835 " Makes the code generator use the given name\n"
836 " in the #define.\";\n"
837 " argument \"name\";\n"
838 " }\n"
839 "}\n");
840
841 char *ori_res = malloc(8096);
842
843 strcpy(ori_res,
844 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
845 "<module name=\"all\"\n"
846 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
847 " xmlns:all_mod=\"urn:all\"\n"
848 " xmlns:yt=\"urn:ietf:params:xml:ns:yang:ietf-yang-types\">\n"
849 " <yang-version value=\"1.1\"/>\n"
850 " <namespace uri=\"urn:all\"/>\n"
851 " <prefix value=\"all_mod\"/>\n"
852 " <import module=\"ietf-yang-types\">\n"
853 " <prefix value=\"yt\"/>\n"
854 " <revision-date date=\"2013-07-15\"/>\n"
855 " <description>\n"
856 " <text>YANG types</text>\n"
857 " </description>\n"
858 " <reference>\n"
859 " <text>RFC reference</text>\n"
860 " </reference>\n"
861 " </import>\n"
862 " <extension name=\"c-define\">\n"
863 " <argument name=\"name\"/>\n"
864 " <description>\n"
865 " <text>Takes as an argument a name string.\n"
866 "Makes the code generator use the given name\n"
867 "in the #define.</text>\n"
868 " </description>\n"
869 " </extension>\n"
870 " <feature name=\"feat1\">\n"
871 " <if-feature name=\"feat2\"/>\n"
872 " <status value=\"obsolete\"/>\n"
873 " </feature>\n"
874 " <feature name=\"feat2\"/>\n"
875 " <feature name=\"feat3\"/>\n"
876 " <identity name=\"ident2\">\n"
877 " <base name=\"ident1\"/>\n"
878 " </identity>\n"
879 " <identity name=\"ident1\"/>\n"
880 " <typedef name=\"tdef1\">\n"
881 " <type name=\"tdef2\">\n"
882 " <length value=\"3..9 | 30..40\"/>\n"
883 " <pattern value=\"[ac]*\"/>\n"
884 " </type>\n"
885 " <units name=\"none\"/>\n"
886 " <default value=\"aaa\"/>\n"
887 " </typedef>\n"
888 " <typedef name=\"tdef2\">\n"
889 " <type name=\"string\">\n"
890 " <length value=\"2..10 | 20..50\"/>\n"
891 " <pattern value=\"[ab]*\"/>\n"
892 " </type>\n"
893 " </typedef>\n"
894 " <grouping name=\"group1\">\n"
895 " <leaf name=\"leaf1\">\n"
896 " <type name=\"int8\"/>\n"
897 " </leaf>\n"
898 " </grouping>\n"
899 " <container name=\"cont1\">\n"
900 " <leaf name=\"leaf2\">\n"
901 " <if-feature name=\"feat1\"/>\n"
902 " <type name=\"int16\"/>\n"
903 " <status value=\"obsolete\"/>\n"
904 " </leaf>\n"
905 " <uses name=\"group1\">\n"
906 " <if-feature name=\"feat2\"/>\n"
907 " <refine target-node=\"leaf1\">\n"
908 " <if-feature name=\"feat3\"/>\n"
909 " <must condition=\"24 - 4 = number('20')\"/>\n"
910 " <default value=\"25\"/>\n"
911 " <config value=\"true\"/>\n"
912 " <mandatory value=\"false\"/>\n"
913 " <description>\n"
914 " <text>dsc</text>\n"
915 " </description>\n"
916 " <reference>\n"
917 " <text>none</text>\n"
918 " </reference>\n"
919 " </refine>\n"
920 " </uses>\n"
921 " <leaf name=\"leaf3\">\n"
922 " <type name=\"int32\"/>\n"
923 " </leaf>\n"
924 " <leaf name=\"leaf4\">\n"
925 " <type name=\"int64\">\n"
926 " <range value=\"1000 .. 50000\">\n"
927 " <error-message>\n"
928 " <value>Special error message.</value>\n"
929 " </error-message>\n"
930 " <error-app-tag value=\"special-tag\"/>\n"
931 " </range>\n"
932 " </type>\n"
933 " </leaf>\n"
934 " <leaf name=\"leaf5\">\n"
935 " <type name=\"uint8\"/>\n"
936 " </leaf>\n"
937 " <leaf name=\"leaf6\">\n"
938 " <type name=\"uint16\"/>\n"
939 " </leaf>\n"
940 " <leaf name=\"leaf7\">\n"
941 " <type name=\"uint32\"/>\n"
942 " </leaf>\n"
943 " <leaf name=\"leaf8\">\n"
944 " <type name=\"uint64\"/>\n"
945 " </leaf>\n"
946 " <choice name=\"choic1\">\n"
947 " <default value=\"leaf9b\"/>\n"
948 " <leaf name=\"leaf9a\">\n"
949 " <type name=\"decimal64\">\n"
950 " <fraction-digits value=\"9\"/>\n"
951 " </type>\n"
952 " </leaf>\n"
953 " <leaf name=\"leaf9b\">\n"
954 " <type name=\"boolean\"/>\n"
955 " <default value=\"false\"/>\n"
956 " </leaf>\n"
957 " </choice>\n"
958 " <leaf name=\"leaf10\">\n"
959 " <type name=\"boolean\"/>\n"
960 " </leaf>\n");
961 strcpy(ori_res + strlen(ori_res),
962 " <leaf name=\"leaf11\">\n"
963 " <type name=\"enumeration\">\n"
964 " <enum name=\"one\"/>\n"
965 " <enum name=\"two\"/>\n"
966 " <enum name=\"five\">\n"
967 " <value value=\"5\"/>\n"
968 " </enum>\n"
969 " </type>\n"
970 " </leaf>\n"
971 " <leaf name=\"leaf12\">\n"
972 " <type name=\"bits\">\n"
973 " <bit name=\"flag0\">\n"
974 " <position value=\"0\"/>\n"
975 " </bit>\n"
976 " <bit name=\"flag1\"/>\n"
977 " <bit name=\"flag2\">\n"
978 " <position value=\"2\"/>\n"
979 " </bit>\n"
980 " <bit name=\"flag3\">\n"
981 " <position value=\"3\"/>\n"
982 " </bit>\n"
983 " </type>\n"
984 " <default value=\"flag0 flag3\"/>\n"
985 " </leaf>\n"
986 " <leaf name=\"leaf13\">\n"
987 " <type name=\"binary\"/>\n"
988 " </leaf>\n"
989 " <leaf name=\"leaf14\">\n"
990 " <type name=\"leafref\">\n"
991 " <path value=\"/cont1/leaf17\"/>\n"
992 " </type>\n"
993 " </leaf>\n"
994 " <leaf name=\"leaf15\">\n"
995 " <type name=\"empty\"/>\n"
996 " </leaf>\n"
997 " <leaf name=\"leaf16\">\n"
998 " <type name=\"union\">\n"
999 " <type name=\"instance-identifier\">\n"
1000 " <require-instance value=\"true\"/>\n"
1001 " </type>\n"
1002 " <type name=\"int8\"/>\n"
1003 " </type>\n"
1004 " </leaf>\n"
1005 " <list name=\"list1\">\n"
1006 " <key value=\"leaf18\"/>\n"
1007 " <unique tag=\"leaf19\"/>\n"
1008 " <min-elements value=\"1\"/>\n"
1009 " <max-elements value=\"20\"/>\n"
1010 " <leaf name=\"leaf18\">\n"
1011 " <type name=\"string\"/>\n"
1012 " </leaf>\n"
1013 " <leaf name=\"leaf19\">\n"
1014 " <type name=\"uint32\"/>\n"
1015 " </leaf>\n"
1016 " <anyxml name=\"axml1\"/>\n"
1017 " <anydata name=\"adata1\"/>\n"
1018 " <action name=\"act1\">\n"
1019 " <input>\n"
1020 " <leaf name=\"leaf24\">\n"
1021 " <type name=\"string\"/>\n"
1022 " </leaf>\n"
1023 " </input>\n"
1024 " <output>\n"
1025 " <leaf name=\"leaf25\">\n"
1026 " <type name=\"string\"/>\n"
1027 " </leaf>\n"
1028 " </output>\n"
1029 " </action>\n"
1030 " <notification name=\"notif1\">\n"
1031 " <leaf name=\"leaf26\">\n"
1032 " <type name=\"string\"/>\n"
1033 " </leaf>\n"
1034 " </notification>\n"
1035 " </list>\n"
1036 " <leaf-list name=\"llist1\">\n"
1037 " <type name=\"tdef1\"/>\n"
1038 " <ordered-by value=\"user\"/>\n"
1039 " </leaf-list>\n"
1040 " <list name=\"list2\">\n"
1041 " <key value=\"leaf27 leaf28\"/>\n"
1042 " <leaf name=\"leaf27\">\n"
1043 " <type name=\"uint8\"/>\n"
1044 " </leaf>\n"
1045 " <leaf name=\"leaf28\">\n"
1046 " <type name=\"uint8\"/>\n"
1047 " </leaf>\n"
1048 " </list>\n"
1049 " <leaf name=\"leaf29\">\n"
1050 " <type name=\"instance-identifier\"/>\n"
1051 " </leaf>\n"
1052 " <container name=\"must-deviations-container\">\n"
1053 " <presence value=\"Allows deviations on the leaf\"/>\n"
1054 " <leaf name=\"leaf30\">\n"
1055 " <type name=\"string\"/>\n"
1056 " </leaf>\n"
1057 " </container>\n"
1058 " <leaf name=\"leaf23\">\n"
1059 " <type name=\"empty\"/>\n"
1060 " </leaf>\n"
1061 " </container>\n"
1062 " <container name=\"test-when\">\n"
1063 " <leaf name=\"when-check\">\n"
1064 " <type name=\"boolean\"/>\n"
1065 " </leaf>\n"
1066 " <leaf name=\"gated-data\">\n"
1067 " <when condition=\"../when-check = 'true'\"/>\n"
1068 " <type name=\"uint16\"/>\n"
1069 " </leaf>\n"
1070 " </container>\n"
1071 " <augment target-node=\"/cont1\">\n"
1072 " <leaf name=\"leaf17\">\n"
1073 " <type name=\"string\"/>\n"
1074 " </leaf>\n"
1075 " </augment>\n"
1076 " <rpc name=\"rpc1\">\n"
1077 " <input>\n"
1078 " <leaf name=\"leaf20\">\n"
1079 " <type name=\"tdef1\"/>\n"
1080 " </leaf>\n"
1081 " </input>\n"
1082 " <output>\n"
1083 " <container name=\"cont2\">\n"
1084 " <leaf name=\"leaf21\">\n"
1085 " <type name=\"empty\"/>\n"
1086 " </leaf>\n"
1087 " </container>\n"
1088 " </output>\n"
1089 " </rpc>\n"
1090 "</module>\n");
1091
1092 char *printed;
1093 struct ly_out *out;
1094
1095 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out));
1096
1097 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1098 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YIN, 0, 0));
1099 assert_int_equal(strlen(ori_res), ly_out_printed(out));
1100 assert_string_equal(printed, ori_res);
1101
1102 ly_out_free(out, NULL, 1);
1103 free(orig);
1104 free(ori_res);
1105}
1106
1107static LY_ERR
1108test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
1109 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
1110 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
1111{
1112 *module_data = user_data;
1113 *format = LYS_IN_YIN;
1114 *free_module_data = NULL;
1115 return LY_SUCCESS;
1116}
1117
1118static void
1119test_print_submodule(void **state)
1120{
1121 struct lys_module *mod;
1122
1123 const char *mod_yin =
1124 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1125 "<module name=\"a\"\n"
1126 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1127 " xmlns:a_mod=\"urn:a\">\n"
1128 " <yang-version value=\"1.1\"/>\n"
1129 " <namespace uri=\"urn:a\"/>\n"
1130 " <prefix value=\"a_mod\"/>\n"
1131 " <include module=\"a-sub\"/>\n"
1132 "</module>\n";
1133
1134 char *submod_yin =
1135 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1136 "<submodule name=\"a-sub\"\n"
1137 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1138 " xmlns:a_mod=\"urn:a\"\n"
1139 " xmlns:yt=\"urn:ietf:params:xml:ns:yang:ietf-yang-types\">\n"
1140 " <yang-version value=\"1.1\"/>\n"
1141 " <belongs-to module=\"a\">\n"
1142 " <prefix value=\"a_mod\"/>\n"
1143 " </belongs-to>\n"
1144 " <import module=\"ietf-yang-types\">\n"
1145 " <prefix value=\"yt\"/>\n"
1146 " <revision-date date=\"2013-07-15\"/>\n"
1147 " </import>\n\n"
1148 " <description>\n"
1149 " <text>YANG types</text>\n"
1150 " </description>\n"
1151 " <reference>\n"
1152 " <text>RFC reference</text>\n"
1153 " </reference>\n"
1154 "</submodule>\n";
1155
1156 char *printed;
1157 struct ly_out *out;
1158
1159 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out));
1160
1161 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod_yin);
1162
1163 UTEST_ADD_MODULE(mod_yin, LYS_IN_YIN, NULL, &mod);
1164 assert_int_equal(LY_SUCCESS, lys_print_submodule(out, mod->parsed->includes[0].submodule, LYS_OUT_YIN, 0, 0));
1165 assert_int_equal(strlen(submod_yin), ly_out_printed(out));
1166 assert_string_equal(printed, submod_yin);
1167
1168 ly_out_free(out, NULL, 1);
1169}
1170
David Sedlák32488102019-07-15 17:44:10 +02001171/* helper function to simplify unit test of each element using parse_content function */
1172LY_ERR
Radek Iša56ca9e42020-09-08 18:42:00 +02001173test_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 +02001174{
David Sedlákc5b20842019-08-13 10:18:31 +02001175 const char *name, *prefix;
1176 size_t name_len, prefix_len;
David Sedlák32488102019-07-15 17:44:10 +02001177 LY_ERR ret = LY_SUCCESS;
1178 struct yin_subelement subelems[71] = {
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001179 {LY_STMT_ACTION, dest, 0},
1180 {LY_STMT_ANYDATA, dest, 0},
1181 {LY_STMT_ANYXML, dest, 0},
1182 {LY_STMT_ARGUMENT, dest, 0},
1183 {LY_STMT_AUGMENT, dest, 0},
1184 {LY_STMT_BASE, dest, 0},
1185 {LY_STMT_BELONGS_TO, dest, 0},
1186 {LY_STMT_BIT, dest, 0},
1187 {LY_STMT_CASE, dest, 0},
1188 {LY_STMT_CHOICE, dest, 0},
1189 {LY_STMT_CONFIG, dest, 0},
1190 {LY_STMT_CONTACT, dest, 0},
1191 {LY_STMT_CONTAINER, dest, 0},
1192 {LY_STMT_DEFAULT, dest, YIN_SUBELEM_UNIQUE},
1193 {LY_STMT_DESCRIPTION, dest, 0},
1194 {LY_STMT_DEVIATE, dest, 0},
1195 {LY_STMT_DEVIATION, dest, 0},
1196 {LY_STMT_ENUM, dest, 0},
1197 {LY_STMT_ERROR_APP_TAG, dest, YIN_SUBELEM_UNIQUE},
1198 {LY_STMT_ERROR_MESSAGE, dest, 0},
1199 {LY_STMT_EXTENSION, dest, 0},
1200 {LY_STMT_FEATURE, dest, 0},
1201 {LY_STMT_FRACTION_DIGITS, dest, 0},
1202 {LY_STMT_GROUPING, dest, 0},
1203 {LY_STMT_IDENTITY, dest, 0},
1204 {LY_STMT_IF_FEATURE, dest, 0},
1205 {LY_STMT_IMPORT, dest, 0},
1206 {LY_STMT_INCLUDE, dest, 0},
1207 {LY_STMT_INPUT, dest, 0},
1208 {LY_STMT_KEY, dest, YIN_SUBELEM_UNIQUE},
1209 {LY_STMT_LEAF, dest, 0},
1210 {LY_STMT_LEAF_LIST, dest, 0},
1211 {LY_STMT_LENGTH, dest, 0},
1212 {LY_STMT_LIST, dest, 0},
1213 {LY_STMT_MANDATORY, dest, 0},
1214 {LY_STMT_MAX_ELEMENTS, dest, 0},
1215 {LY_STMT_MIN_ELEMENTS, dest, 0},
1216 {LY_STMT_MODIFIER, dest, 0},
1217 {LY_STMT_MODULE, dest, 0},
1218 {LY_STMT_MUST, dest, 0},
1219 {LY_STMT_NAMESPACE, dest, YIN_SUBELEM_UNIQUE},
1220 {LY_STMT_NOTIFICATION, dest, 0},
1221 {LY_STMT_ORDERED_BY, dest, 0},
1222 {LY_STMT_ORGANIZATION, dest, 0},
1223 {LY_STMT_OUTPUT, dest, 0},
1224 {LY_STMT_PATH, dest, 0},
1225 {LY_STMT_PATTERN, dest, 0},
1226 {LY_STMT_POSITION, dest, 0},
1227 {LY_STMT_PREFIX, dest, YIN_SUBELEM_UNIQUE},
1228 {LY_STMT_PRESENCE, dest, YIN_SUBELEM_UNIQUE},
1229 {LY_STMT_RANGE, dest, 0},
1230 {LY_STMT_REFERENCE, dest, 0},
1231 {LY_STMT_REFINE, dest, 0},
1232 {LY_STMT_REQUIRE_INSTANCE, dest, 0},
1233 {LY_STMT_REVISION, dest, 0},
1234 {LY_STMT_REVISION_DATE, dest, 0},
1235 {LY_STMT_RPC, dest, 0},
1236 {LY_STMT_STATUS, dest, 0},
1237 {LY_STMT_SUBMODULE, dest, 0},
1238 {LY_STMT_TYPE, dest, 0},
1239 {LY_STMT_TYPEDEF, dest, 0},
1240 {LY_STMT_UNIQUE, dest, 0},
1241 {LY_STMT_UNITS, dest, YIN_SUBELEM_UNIQUE},
1242 {LY_STMT_USES, dest, 0},
1243 {LY_STMT_VALUE, dest, 0},
1244 {LY_STMT_WHEN, dest, 0},
1245 {LY_STMT_YANG_VERSION, dest, 0},
1246 {LY_STMT_YIN_ELEMENT, dest, 0},
1247 {LY_STMT_EXTENSION_INSTANCE, dest, 0},
1248 {LY_STMT_ARG_TEXT, dest, 0},
1249 {LY_STMT_ARG_VALUE, dest, 0}
1250 };
1251
Michal Vaskod0625d72022-10-06 15:02:50 +02001252 YCTX->main_ctx = (struct lysp_ctx *)YCTX;
Radek Iša56ca9e42020-09-08 18:42:00 +02001253 ly_in_new_memory(data, &UTEST_IN);
1254 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
1255 prefix = YCTX->xmlctx->prefix;
1256 prefix_len = YCTX->xmlctx->prefix_len;
1257 name = YCTX->xmlctx->name;
1258 name_len = YCTX->xmlctx->name_len;
1259 lyxml_ctx_next(YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +01001260
Radek Iša56ca9e42020-09-08 18:42:00 +02001261 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 +01001262
Michal Vasko63f3d842020-07-08 10:10:14 +02001263 /* free parser and input */
Radek Iša56ca9e42020-09-08 18:42:00 +02001264 lyxml_ctx_free(YCTX->xmlctx);
1265 YCTX->xmlctx = NULL;
1266 ly_in_free(UTEST_IN, 0);
1267 UTEST_IN = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001268 return ret;
1269}
1270
David Sedlákd1144562019-08-06 12:36:14 +02001271#define EXT_SUBELEM "<myext:c-define name=\"MY_MTU\" xmlns:myext=\"urn:example:extensions\"/>"
1272
David Sedlák32488102019-07-15 17:44:10 +02001273static void
David Sedlák43801c92019-08-05 15:58:54 +02001274test_enum_elem(void **state)
David Sedlák32488102019-07-15 17:44:10 +02001275{
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001276 struct lysp_type type = {0};
David Sedlák32488102019-07-15 17:44:10 +02001277 const char *data;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001278
David Sedlák32488102019-07-15 17:44:10 +02001279 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001280 "<enum name=\"enum-name\">\n"
1281 " <if-feature name=\"feature\" />\n"
1282 " <value value=\"55\" />\n"
1283 " <status value=\"deprecated\" />\n"
1284 " <description><text>desc...</text></description>\n"
1285 " <reference><text>ref...</text></reference>\n"
1286 " " EXT_SUBELEM "\n"
1287 "</enum>"
1288 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001289 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1290 uint16_t flags = LYS_STATUS_DEPRC | LYS_SET_VALUE;
1291
1292 CHECK_LYSP_TYPE_ENUM(type.enums, "desc...", 1, flags, 1, "enum-name", "ref...", 55);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001293 assert_string_equal(type.enums->iffeatures[0].str, "feature");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001294 TEST_1_CHECK_LYSP_EXT_INSTANCE(type.enums->exts, LY_STMT_ENUM);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001295 lysp_type_free(&fctx, &type);
David Sedlákd1144562019-08-06 12:36:14 +02001296 memset(&type, 0, sizeof type);
1297
1298 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001299 "<enum name=\"enum-name\"></enum>"
1300 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001301 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001302 assert_string_equal(type.enums->name, "enum-name");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001303 lysp_type_free(&fctx, &type);
David Sedlák32488102019-07-15 17:44:10 +02001304 memset(&type, 0, sizeof type);
David Sedlák43801c92019-08-05 15:58:54 +02001305}
1306
1307static void
1308test_bit_elem(void **state)
1309{
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001310 struct lysp_type type = {0};
David Sedlák43801c92019-08-05 15:58:54 +02001311 const char *data;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001312
David Sedlák43801c92019-08-05 15:58:54 +02001313 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001314 "<bit name=\"bit-name\">\n"
1315 " <if-feature name=\"feature\" />\n"
1316 " <position value=\"55\" />\n"
1317 " <status value=\"deprecated\" />\n"
1318 " <description><text>desc...</text></description>\n"
1319 " <reference><text>ref...</text></reference>\n"
1320 EXT_SUBELEM
1321 "</bit>"
1322 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001323 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1324 uint16_t flags = LYS_STATUS_DEPRC | LYS_SET_VALUE;
1325
1326 CHECK_LYSP_TYPE_ENUM(type.bits, "desc...", 1, flags, 1, "bit-name", "ref...", 55);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001327 assert_string_equal(type.bits->iffeatures[0].str, "feature");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001328 TEST_1_CHECK_LYSP_EXT_INSTANCE(type.bits->exts, LY_STMT_BIT);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001329 lysp_type_free(&fctx, &type);
David Sedlákd1144562019-08-06 12:36:14 +02001330 memset(&type, 0, sizeof type);
1331
1332 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001333 "<bit name=\"bit-name\"> </bit>"
1334 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001335 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1336 CHECK_LYSP_TYPE_ENUM(type.bits, NULL, 0, 0, 0, "bit-name", NULL, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001337 lysp_type_free(&fctx, &type);
David Sedlák43801c92019-08-05 15:58:54 +02001338 memset(&type, 0, sizeof type);
David Sedlák32488102019-07-15 17:44:10 +02001339}
1340
1341static void
David Sedlák32488102019-07-15 17:44:10 +02001342test_status_elem(void **state)
1343{
David Sedlák32488102019-07-15 17:44:10 +02001344 const char *data;
1345 uint16_t flags = 0;
David Sedlák32488102019-07-15 17:44:10 +02001346
1347 /* test invalid value */
1348 data = ELEMENT_WRAPPER_START "<status value=\"invalid\"></status>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001349 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID);
1350 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"status\" element. "
1351 "Valid values are \"current\", \"deprecated\" and \"obsolete\".", "Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001352}
1353
1354static void
David Sedlák32488102019-07-15 17:44:10 +02001355test_yin_element_elem(void **state)
1356{
David Sedlák32488102019-07-15 17:44:10 +02001357 const char *data;
1358 uint16_t flags = 0;
David Sedlák32488102019-07-15 17:44:10 +02001359
1360 data = ELEMENT_WRAPPER_START "<yin-element value=\"invalid\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001361 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +02001362 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"yin-element\" element. "
1363 "Valid values are \"true\" and \"false\".", "Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001364}
1365
1366static void
1367test_yangversion_elem(void **state)
1368{
David Sedlák32488102019-07-15 17:44:10 +02001369 const char *data;
1370 uint8_t version = 0;
David Sedlák32488102019-07-15 17:44:10 +02001371
1372 /* invalid value */
1373 data = ELEMENT_WRAPPER_START "<yang-version value=\"version\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001374 assert_int_equal(test_element_helper(state, data, &version, NULL, NULL), LY_EVALID);
1375 CHECK_LOG_CTX("Invalid value \"version\" of \"value\" attribute in \"yang-version\" element. "
1376 "Valid values are \"1\" and \"1.1\".", "Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001377}
1378
1379static void
David Sedlák8e7bda82019-07-16 17:57:50 +02001380test_argument_elem(void **state)
1381{
David Sedlák8e7bda82019-07-16 17:57:50 +02001382 const char *data;
1383 uint16_t flags = 0;
1384 const char *arg;
1385 struct yin_argument_meta arg_meta = {&flags, &arg};
David Sedlák8e7bda82019-07-16 17:57:50 +02001386
1387 /* min subelems */
1388 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001389 "<argument name=\"arg\">"
1390 "</argument>"
1391 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001392 assert_int_equal(test_element_helper(state, data, &arg_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001393 assert_string_equal(arg, "arg");
1394 assert_true(flags == 0);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001395 lydict_remove(UTEST_LYCTX, arg);
David Sedlák8e7bda82019-07-16 17:57:50 +02001396}
1397
1398static void
David Sedlák8e7bda82019-07-16 17:57:50 +02001399test_belongsto_elem(void **state)
1400{
David Sedlák8e7bda82019-07-16 17:57:50 +02001401 const char *data;
1402 struct lysp_submodule submod;
1403
Michal Vasko8a67eff2021-12-07 14:04:47 +01001404 lydict_insert(UTEST_LYCTX, "module-name", 0, &PARSER_CUR_PMOD(YCTX)->mod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +02001405
David Sedlák8e7bda82019-07-16 17:57:50 +02001406 data = ELEMENT_WRAPPER_START "<belongs-to module=\"module-name\"></belongs-to>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001407 assert_int_equal(test_element_helper(state, data, &submod, NULL, NULL), LY_EVALID);
1408 CHECK_LOG_CTX("Missing mandatory sub-element \"prefix\" of \"belongs-to\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001409}
1410
1411static void
1412test_config_elem(void **state)
1413{
David Sedlák8e7bda82019-07-16 17:57:50 +02001414 const char *data;
1415 uint16_t flags = 0;
David Sedlák8e7bda82019-07-16 17:57:50 +02001416
1417 data = ELEMENT_WRAPPER_START "<config value=\"false\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001418 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001419 assert_true(flags & LYS_CONFIG_R);
David Sedlák8e7bda82019-07-16 17:57:50 +02001420 flags = 0;
1421
1422 data = ELEMENT_WRAPPER_START "<config value=\"invalid\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001423 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID);
1424 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"config\" element. "
1425 "Valid values are \"true\" and \"false\".", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001426}
1427
1428static void
1429test_default_elem(void **state)
1430{
David Sedlák8e7bda82019-07-16 17:57:50 +02001431 const char *data;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001432 struct lysp_qname val = {0};
David Sedlák8e7bda82019-07-16 17:57:50 +02001433
1434 data = ELEMENT_WRAPPER_START "<default/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001435 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
1436 CHECK_LOG_CTX("Missing mandatory attribute value of default element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001437}
1438
1439static void
1440test_err_app_tag_elem(void **state)
1441{
David Sedlák8e7bda82019-07-16 17:57:50 +02001442 const char *data;
1443 const char *val = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001444
1445 data = ELEMENT_WRAPPER_START "<error-app-tag/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001446 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
1447 CHECK_LOG_CTX("Missing mandatory attribute value of error-app-tag element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001448}
1449
1450static void
1451test_err_msg_elem(void **state)
1452{
David Sedlák8e7bda82019-07-16 17:57:50 +02001453 const char *data;
1454 const char *val = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001455
1456 data = ELEMENT_WRAPPER_START "<error-message></error-message>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001457 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
1458 CHECK_LOG_CTX("Missing mandatory sub-element \"value\" of \"error-message\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001459
David Sedlákdf2a9732019-08-07 13:23:16 +02001460 data = ELEMENT_WRAPPER_START "<error-message invalid=\"text\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001461 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
1462 CHECK_LOG_CTX("Unexpected attribute \"invalid\" of \"error-message\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001463}
1464
1465static void
1466test_fracdigits_elem(void **state)
1467{
David Sedlák8e7bda82019-07-16 17:57:50 +02001468 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001469 struct lysp_type type = {0};
David Sedlák8e7bda82019-07-16 17:57:50 +02001470
David Sedlák8e7bda82019-07-16 17:57:50 +02001471 /* invalid values */
1472 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"-1\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001473 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1474 CHECK_LOG_CTX("Invalid value \"-1\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001475
1476 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"02\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001477 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1478 CHECK_LOG_CTX("Invalid value \"02\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001479
1480 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"1p\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001481 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1482 CHECK_LOG_CTX("Invalid value \"1p\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001483
1484 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"19\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001485 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1486 CHECK_LOG_CTX("Invalid value \"19\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001487
1488 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"999999999999999999\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001489 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1490 CHECK_LOG_CTX("Invalid value \"999999999999999999\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001491}
1492
1493static void
1494test_iffeature_elem(void **state)
1495{
David Sedlák8e7bda82019-07-16 17:57:50 +02001496 const char *data;
1497 const char **iffeatures = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001498
1499 data = ELEMENT_WRAPPER_START "<if-feature/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001500 assert_int_equal(test_element_helper(state, data, &iffeatures, NULL, NULL), LY_EVALID);
1501 CHECK_LOG_CTX("Missing mandatory attribute name of if-feature element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001502 LY_ARRAY_FREE(iffeatures);
1503 iffeatures = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001504}
1505
1506static void
1507test_length_elem(void **state)
1508{
David Sedlák8e7bda82019-07-16 17:57:50 +02001509 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001510 struct lysp_type type = {0};
David Sedlák8e7bda82019-07-16 17:57:50 +02001511
1512 /* max subelems */
1513 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001514 "<length value=\"length-str\">\n"
1515 " <error-message><value>err-msg</value></error-message>\n"
1516 " <error-app-tag value=\"err-app-tag\"/>\n"
1517 " <description><text>desc</text></description>\n"
1518 " <reference><text>ref</text></reference>\n"
1519 EXT_SUBELEM
1520 "</length>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001521 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001522 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1523 CHECK_LYSP_RESTR(type.length, "length-str", "desc",
1524 "err-app-tag", "err-msg", 1, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001525 assert_true(type.flags & LYS_SET_LENGTH);
Radek Krejci39b7fc22021-02-26 23:29:18 +01001526 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.length->exts[0]), LY_STMT_LENGTH);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001527 lysp_type_free(&fctx, &type);
David Sedlák8e7bda82019-07-16 17:57:50 +02001528 memset(&type, 0, sizeof(type));
1529
1530 /* min subelems */
1531 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001532 "<length value=\"length-str\">"
1533 "</length>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001534 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001535 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1536 CHECK_LYSP_RESTR(type.length, "length-str", NULL,
1537 NULL, NULL, 0, NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001538 lysp_type_free(&fctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001539 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001540 memset(&type, 0, sizeof(type));
1541
1542 data = ELEMENT_WRAPPER_START "<length></length>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001543 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1544 CHECK_LOG_CTX("Missing mandatory attribute value of length element.", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001545 lysp_type_free(&fctx, &type);
David Sedlák8e7bda82019-07-16 17:57:50 +02001546 memset(&type, 0, sizeof(type));
David Sedlák8e7bda82019-07-16 17:57:50 +02001547}
1548
1549static void
1550test_modifier_elem(void **state)
1551{
David Sedlák8e7bda82019-07-16 17:57:50 +02001552 const char *data;
Radek Krejci011e4aa2020-09-04 15:22:31 +02001553 const char *pat;
David Sedlák8e7bda82019-07-16 17:57:50 +02001554
Radek Iša56ca9e42020-09-08 18:42:00 +02001555 assert_int_equal(LY_SUCCESS, lydict_insert(UTEST_LYCTX, "\006pattern", 8, &pat));
David Sedlák8e7bda82019-07-16 17:57:50 +02001556 data = ELEMENT_WRAPPER_START "<modifier value=\"invert\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001557 assert_int_equal(test_element_helper(state, data, &pat, NULL, NULL), LY_EVALID);
1558 CHECK_LOG_CTX("Invalid value \"invert\" of \"value\" attribute in \"modifier\" element. "
1559 "Only valid value is \"invert-match\".", "Line number 1.");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001560 lydict_remove(UTEST_LYCTX, pat);
David Sedlák8e7bda82019-07-16 17:57:50 +02001561}
1562
1563static void
1564test_namespace_elem(void **state)
1565{
David Sedlák8e7bda82019-07-16 17:57:50 +02001566 const char *data;
1567 const char *ns;
David Sedlák8e7bda82019-07-16 17:57:50 +02001568
1569 data = ELEMENT_WRAPPER_START "<namespace/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001570 assert_int_equal(test_element_helper(state, data, &ns, NULL, NULL), LY_EVALID);
1571 CHECK_LOG_CTX("Missing mandatory attribute uri of namespace element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001572}
1573
1574static void
David Sedlák8e7bda82019-07-16 17:57:50 +02001575test_pattern_elem(void **state)
1576{
David Sedlák8e7bda82019-07-16 17:57:50 +02001577 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001578 struct lysp_type type = {0};
David Sedlák8e7bda82019-07-16 17:57:50 +02001579
1580 /* max subelems */
1581 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001582 "<pattern value=\"super_pattern\">\n"
1583 " <modifier value=\"invert-match\"/>\n"
1584 " <error-message><value>err-msg-value</value></error-message>\n"
1585 " <error-app-tag value=\"err-app-tag-value\"/>\n"
1586 " <description><text>&quot;pattern-desc&quot;</text></description>\n"
1587 " <reference><text>pattern-ref</text></reference>\n"
1588 EXT_SUBELEM
1589 "</pattern>"
1590 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001591 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001592 assert_true(type.flags & LYS_SET_PATTERN);
Radek Iša56ca9e42020-09-08 18:42:00 +02001593 CHECK_LYSP_RESTR(type.patterns, "\x015super_pattern", "\"pattern-desc\"",
1594 "err-app-tag-value", "err-msg-value", 1, "pattern-ref");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001595 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.patterns->exts[0]), LY_STMT_PATTERN);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001596 lysp_type_free(&fctx, &type);
David Sedlák8e7bda82019-07-16 17:57:50 +02001597 memset(&type, 0, sizeof(type));
1598
1599 /* min subelems */
1600 data = ELEMENT_WRAPPER_START "<pattern value=\"pattern\"> </pattern>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001601 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1602 CHECK_LYSP_RESTR(type.patterns, "\x006pattern", NULL, NULL, NULL, 0, NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001603 lysp_type_free(&fctx, &type);
David Sedlák8e7bda82019-07-16 17:57:50 +02001604 memset(&type, 0, sizeof(type));
David Sedlák8e7bda82019-07-16 17:57:50 +02001605}
1606
1607static void
1608test_value_position_elem(void **state)
1609{
David Sedlák8e7bda82019-07-16 17:57:50 +02001610 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001611 struct lysp_type_enum en = {0};
David Sedlák8e7bda82019-07-16 17:57:50 +02001612
1613 /* valid values */
David Sedlák8e7bda82019-07-16 17:57:50 +02001614 data = ELEMENT_WRAPPER_START "<value value=\"-55\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001615 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1616 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, -55);
David Sedlák8e7bda82019-07-16 17:57:50 +02001617 memset(&en, 0, sizeof(en));
1618
1619 data = ELEMENT_WRAPPER_START "<value value=\"0\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001620 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1621 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, 0);
David Sedlák8e7bda82019-07-16 17:57:50 +02001622 memset(&en, 0, sizeof(en));
1623
1624 data = ELEMENT_WRAPPER_START "<value value=\"-0\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001625 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1626 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, 0);
David Sedlák8e7bda82019-07-16 17:57:50 +02001627 memset(&en, 0, sizeof(en));
1628
1629 /* valid positions */
David Sedlák8e7bda82019-07-16 17:57:50 +02001630 data = ELEMENT_WRAPPER_START "<position value=\"0\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001631 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1632 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, 0);
David Sedlák8e7bda82019-07-16 17:57:50 +02001633 memset(&en, 0, sizeof(en));
1634
1635 /* invalid values */
1636 data = ELEMENT_WRAPPER_START "<value value=\"99999999999999999999999\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001637 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1638 CHECK_LOG_CTX("Invalid value \"99999999999999999999999\" of \"value\" attribute in \"value\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001639
1640 data = ELEMENT_WRAPPER_START "<value value=\"1k\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001641 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1642 CHECK_LOG_CTX("Invalid value \"1k\" of \"value\" attribute in \"value\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001643
David Sedlák69f01612019-07-17 11:41:08 +02001644 data = ELEMENT_WRAPPER_START "<value value=\"\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001645 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1646 CHECK_LOG_CTX("Invalid value \"\" of \"value\" attribute in \"value\" element.", "Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001647
David Sedlák8e7bda82019-07-16 17:57:50 +02001648 /*invalid positions */
1649 data = ELEMENT_WRAPPER_START "<position value=\"-5\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001650 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1651 CHECK_LOG_CTX("Invalid value \"-5\" of \"value\" attribute in \"position\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001652
1653 data = ELEMENT_WRAPPER_START "<position value=\"-0\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001654 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1655 CHECK_LOG_CTX("Invalid value \"-0\" of \"value\" attribute in \"position\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001656
1657 data = ELEMENT_WRAPPER_START "<position value=\"99999999999999999999\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001658 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1659 CHECK_LOG_CTX("Invalid value \"99999999999999999999\" of \"value\" attribute in \"position\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001660
David Sedlák69f01612019-07-17 11:41:08 +02001661 data = ELEMENT_WRAPPER_START "<position value=\"\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001662 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1663 CHECK_LOG_CTX("Invalid value \"\" of \"value\" attribute in \"position\" element.", "Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001664}
1665
1666static void
1667test_prefix_elem(void **state)
1668{
David Sedlák69f01612019-07-17 11:41:08 +02001669 const char *data;
1670 const char *value = NULL;
1671
1672 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001673 assert_int_equal(test_element_helper(state, data, &value, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001674 assert_string_equal(value, "pref");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001675 lydict_remove(UTEST_LYCTX, value);
David Sedlák69f01612019-07-17 11:41:08 +02001676}
1677
1678static void
1679test_range_elem(void **state)
1680{
David Sedlák69f01612019-07-17 11:41:08 +02001681 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001682 struct lysp_type type = {0};
David Sedlák69f01612019-07-17 11:41:08 +02001683
1684 /* max subelems */
1685 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001686 "<range value=\"range-str\">\n"
1687 " <error-message><value>err-msg</value></error-message>\n"
1688 " <error-app-tag value=\"err-app-tag\" />\n"
1689 " <description><text>desc</text></description>\n"
1690 " <reference><text>ref</text></reference>\n"
1691 EXT_SUBELEM
1692 "</range>"
1693 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001694 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1695 CHECK_LYSP_RESTR(type.range, "range-str", "desc",
1696 "err-app-tag", "err-msg", 1, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001697 assert_true(type.flags & LYS_SET_RANGE);
Radek Krejci39b7fc22021-02-26 23:29:18 +01001698 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.range->exts[0]), LY_STMT_RANGE);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001699 lysp_type_free(&fctx, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001700 memset(&type, 0, sizeof(type));
1701
1702 /* min subelems */
1703 data = ELEMENT_WRAPPER_START "<range value=\"range-str\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001704 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1705 CHECK_LYSP_RESTR(type.range, "range-str", NULL,
1706 NULL, NULL, 0, NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001707 lysp_type_free(&fctx, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001708 memset(&type, 0, sizeof(type));
David Sedlák69f01612019-07-17 11:41:08 +02001709}
1710
1711static void
1712test_reqinstance_elem(void **state)
1713{
David Sedlák69f01612019-07-17 11:41:08 +02001714 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001715 struct lysp_type type = {0};
David Sedlák69f01612019-07-17 11:41:08 +02001716
David Sedlákd1144562019-08-06 12:36:14 +02001717 data = ELEMENT_WRAPPER_START "<require-instance value=\"true\">" EXT_SUBELEM "</require-instance>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001718 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001719 assert_int_equal(type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001720 assert_true(type.flags & LYS_SET_REQINST);
Radek Krejci5984d222021-02-26 23:01:45 +01001721 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.exts[0]), LY_STMT_REQUIRE_INSTANCE);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001722 lysp_type_free(&fctx, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001723 memset(&type, 0, sizeof(type));
1724
1725 data = ELEMENT_WRAPPER_START "<require-instance value=\"false\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001726 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001727 assert_int_equal(type.require_instance, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001728 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001729 memset(&type, 0, sizeof(type));
1730
1731 data = ELEMENT_WRAPPER_START "<require-instance value=\"invalid\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001732 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
David Sedlák69f01612019-07-17 11:41:08 +02001733 memset(&type, 0, sizeof(type));
Radek Iša56ca9e42020-09-08 18:42:00 +02001734 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"require-instance\" element. "
1735 "Valid values are \"true\" and \"false\".", "Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001736}
1737
1738static void
1739test_revision_date_elem(void **state)
1740{
David Sedlák69f01612019-07-17 11:41:08 +02001741 const char *data;
1742 char rev[LY_REV_SIZE];
1743
1744 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001745 assert_int_equal(test_element_helper(state, data, rev, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001746 assert_string_equal(rev, "2000-01-01");
1747
1748 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-50-05\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001749 assert_int_equal(test_element_helper(state, data, rev, NULL, NULL), LY_EVALID);
1750 CHECK_LOG_CTX("Invalid value \"2000-50-05\" of \"revision-date\".", "Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001751}
1752
1753static void
1754test_unique_elem(void **state)
1755{
David Sedlák69f01612019-07-17 11:41:08 +02001756 const char *data;
1757 const char **values = NULL;
1758
1759 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001760 assert_int_equal(test_element_helper(state, data, &values, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001761 assert_string_equal(*values, "tag");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001762 lydict_remove(UTEST_LYCTX, *values);
David Sedlák69f01612019-07-17 11:41:08 +02001763 LY_ARRAY_FREE(values);
David Sedlákd1144562019-08-06 12:36:14 +02001764 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001765}
1766
1767static void
1768test_units_elem(void **state)
1769{
David Sedlák69f01612019-07-17 11:41:08 +02001770 const char *data;
1771 const char *values = NULL;
1772
1773 data = ELEMENT_WRAPPER_START "<units name=\"name\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001774 assert_int_equal(test_element_helper(state, data, &values, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001775 assert_string_equal(values, "name");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001776 lydict_remove(UTEST_LYCTX, values);
David Sedlákd1144562019-08-06 12:36:14 +02001777 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001778}
1779
1780static void
David Sedlák69f01612019-07-17 11:41:08 +02001781test_yin_text_value_elem(void **state)
1782{
David Sedlák69f01612019-07-17 11:41:08 +02001783 const char *data;
1784 const char *val;
1785
1786 data = ELEMENT_WRAPPER_START "<text>text</text>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001787 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001788 assert_string_equal(val, "text");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001789 lydict_remove(UTEST_LYCTX, val);
David Sedlák69f01612019-07-17 11:41:08 +02001790
1791 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 +02001792 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001793 assert_string_equal(val, "text");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001794 lydict_remove(UTEST_LYCTX, val);
David Sedlák69f01612019-07-17 11:41:08 +02001795
1796 data = ELEMENT_WRAPPER_START "<text></text>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001797 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001798 assert_string_equal("", val);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001799 lydict_remove(UTEST_LYCTX, val);
David Sedlák8e7bda82019-07-16 17:57:50 +02001800}
David Sedlák32488102019-07-15 17:44:10 +02001801
David Sedlák374d2b32019-07-17 15:06:55 +02001802static void
1803test_type_elem(void **state)
1804{
David Sedlák374d2b32019-07-17 15:06:55 +02001805 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001806 struct lysp_type type = {0};
David Sedlák374d2b32019-07-17 15:06:55 +02001807
1808 /* max subelems */
1809 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001810 "<type name=\"type-name\">\n"
1811 " <base name=\"base-name\"/>\n"
1812 " <bit name=\"bit\"/>\n"
1813 " <enum name=\"enum\"/>\n"
1814 " <fraction-digits value=\"2\"/>\n"
1815 " <length value=\"length\"/>\n"
1816 " <path value=\"/path\"/>\n"
1817 " <pattern value=\"pattern\"/>\n"
1818 " <range value=\"range\" />\n"
1819 " <require-instance value=\"true\"/>\n"
1820 " <type name=\"sub-type-name\"/>\n"
1821 EXT_SUBELEM
1822 "</type>"
1823 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001824 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák374d2b32019-07-17 15:06:55 +02001825 assert_string_equal(type.name, "type-name");
1826 assert_string_equal(*type.bases, "base-name");
1827 assert_string_equal(type.bits->name, "bit");
1828 assert_string_equal(type.enums->name, "enum");
1829 assert_int_equal(type.fraction_digits, 2);
Radek Iša56ca9e42020-09-08 18:42:00 +02001830 CHECK_LYSP_RESTR(type.length, "length", NULL,
1831 NULL, NULL, 0, NULL);
Michal Vaskocb8c6d42020-10-16 11:58:30 +02001832 assert_string_equal(type.path->expr, "/path");
Radek Iša56ca9e42020-09-08 18:42:00 +02001833 CHECK_LYSP_RESTR(type.patterns, "\006pattern", NULL,
1834 NULL, NULL, 0, NULL);
1835 CHECK_LYSP_RESTR(type.range, "range", NULL,
1836 NULL, NULL, 0, NULL);
David Sedlák374d2b32019-07-17 15:06:55 +02001837 assert_int_equal(type.require_instance, 1);
1838 assert_string_equal(type.types->name, "sub-type-name");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001839 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.exts[0]), LY_STMT_TYPE);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001840 assert_true(type.flags & LYS_SET_BASE);
1841 assert_true(type.flags & LYS_SET_BIT);
1842 assert_true(type.flags & LYS_SET_ENUM);
1843 assert_true(type.flags & LYS_SET_FRDIGITS);
1844 assert_true(type.flags & LYS_SET_LENGTH);
1845 assert_true(type.flags & LYS_SET_PATH);
1846 assert_true(type.flags & LYS_SET_PATTERN);
1847 assert_true(type.flags & LYS_SET_RANGE);
1848 assert_true(type.flags & LYS_SET_REQINST);
1849 assert_true(type.flags & LYS_SET_TYPE);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001850 lysp_type_free(&fctx, &type);
David Sedlák374d2b32019-07-17 15:06:55 +02001851 memset(&type, 0, sizeof(type));
1852
1853 /* min subelems */
1854 data = ELEMENT_WRAPPER_START "<type name=\"type-name\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001855 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001856 lysp_type_free(&fctx, &type);
David Sedlák374d2b32019-07-17 15:06:55 +02001857 memset(&type, 0, sizeof(type));
David Sedlák374d2b32019-07-17 15:06:55 +02001858}
1859
David Sedlák1af868e2019-07-17 17:03:14 +02001860static void
1861test_max_elems_elem(void **state)
1862{
David Sedlák1af868e2019-07-17 17:03:14 +02001863 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001864 struct lysp_node_list list = {0};
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001865 struct lysp_refine refine = {0};
David Sedlák1af868e2019-07-17 17:03:14 +02001866
David Sedlák1af868e2019-07-17 17:03:14 +02001867 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"10\"/> </refine>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001868 assert_int_equal(test_element_helper(state, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02001869 assert_int_equal(refine.max, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001870 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001871
1872 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"0\"/> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001873 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID);
1874 CHECK_LOG_CTX("Invalid value \"0\" of \"value\" attribute in \"max-elements\" element.", "Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02001875
1876 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"-10\"/> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001877 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID);
1878 CHECK_LOG_CTX("Invalid value \"-10\" of \"value\" attribute in \"max-elements\" element.", "Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02001879
1880 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"k\"/> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001881 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID);
1882 CHECK_LOG_CTX("Invalid value \"k\" of \"value\" attribute in \"max-elements\" element.", "Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02001883
1884 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"u12\"/> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001885 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID);
1886 CHECK_LOG_CTX("Invalid value \"u12\" of \"value\" attribute in \"max-elements\" element.", "Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02001887}
1888
David Sedlák09e18c92019-07-18 11:17:11 +02001889static void
1890test_min_elems_elem(void **state)
1891{
David Sedlák09e18c92019-07-18 11:17:11 +02001892 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001893 struct lysp_node_leaflist llist = {0};
David Sedlák09e18c92019-07-18 11:17:11 +02001894
1895 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 +02001896 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID);
1897 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 +02001898
1899 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 +02001900 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID);
1901 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 +02001902
1903 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 +02001904 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID);
1905 CHECK_LOG_CTX("Invalid value \"5k\" of \"value\" attribute in \"min-elements\" element.", "Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02001906
1907 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 +02001908 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID);
1909 CHECK_LOG_CTX("Invalid value \"05\" of \"value\" attribute in \"min-elements\" element.", "Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02001910}
1911
David Sedláka2dad212019-07-18 12:45:19 +02001912static void
1913test_ordby_elem(void **state)
1914{
David Sedláka2dad212019-07-18 12:45:19 +02001915 const char *data;
1916 uint16_t flags = 0;
David Sedláka2dad212019-07-18 12:45:19 +02001917
1918 data = ELEMENT_WRAPPER_START "<ordered-by value=\"user\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001919 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001920 assert_true(flags & LYS_ORDBY_USER);
David Sedláka2dad212019-07-18 12:45:19 +02001921
1922 data = ELEMENT_WRAPPER_START "<ordered-by value=\"inv\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001923 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID);
1924 CHECK_LOG_CTX("Invalid value \"inv\" of \"value\" attribute in \"ordered-by\" element. "
1925 "Valid values are \"system\" and \"user\".", "Line number 1.");
David Sedláka2dad212019-07-18 12:45:19 +02001926}
1927
David Sedlák8a83bbb2019-07-18 14:46:00 +02001928static void
1929test_any_elem(void **state)
1930{
David Sedlák8a83bbb2019-07-18 14:46:00 +02001931 const char *data;
1932 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02001933 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák8a83bbb2019-07-18 14:46:00 +02001934 struct lysp_node_anydata *parsed = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02001935 uint16_t flags;
David Sedlák8a83bbb2019-07-18 14:46:00 +02001936
1937 /* anyxml max subelems */
1938 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001939 "<anyxml name=\"any-name\">\n"
1940 " <config value=\"true\" />\n"
1941 " <description><text>desc</text></description>\n"
1942 " <if-feature name=\"feature\" />\n"
1943 " <mandatory value=\"true\" />\n"
1944 " <must condition=\"must-cond\" />\n"
1945 " <reference><text>ref</text></reference>\n"
1946 " <status value=\"deprecated\"/>\n"
1947 " <when condition=\"when-cond\"/>\n"
1948 EXT_SUBELEM
1949 "</anyxml>"
1950 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001951 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001952 parsed = (struct lysp_node_anydata *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02001953 flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_DEPRC;
1954 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
1955 "any-name", 0, LYS_ANYXML, 0, "ref", 1);
1956 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001957 assert_string_equal(parsed->iffeatures[0].str, "feature");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001958 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_ANYXML);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001959 lysp_node_free(&fctx, siblings);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001960 siblings = NULL;
1961
1962 /* anydata max subelems */
1963 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001964 "<anydata name=\"any-name\">\n"
1965 " <config value=\"true\" />\n"
1966 " <description><text>desc</text></description>\n"
1967 " <if-feature name=\"feature\" />\n"
1968 " <mandatory value=\"true\" />\n"
1969 " <must condition=\"must-cond\" />\n"
1970 " <reference><text>ref</text></reference>\n"
1971 " <status value=\"deprecated\"/>\n"
1972 " <when condition=\"when-cond\"/>\n"
1973 EXT_SUBELEM
1974 "</anydata>"
1975 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001976 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001977 parsed = (struct lysp_node_anydata *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02001978 flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_DEPRC;
1979 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
1980 "any-name", 0, LYS_ANYDATA, 0, "ref", 1);
1981 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001982 assert_string_equal(parsed->iffeatures[0].str, "feature");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001983 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_ANYDATA);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001984 lysp_node_free(&fctx, siblings);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001985 siblings = NULL;
1986
1987 /* min subelems */
1988 node_meta.parent = (void *)0x10;
1989 data = ELEMENT_WRAPPER_START "<anydata name=\"any-name\"> </anydata>" 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;
1992 assert_ptr_equal(parsed->parent, node_meta.parent);
Radek Iša56ca9e42020-09-08 18:42:00 +02001993 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
1994 "any-name", 0, LYS_ANYDATA, 1, NULL, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001995 lysp_node_free(&fctx, siblings);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001996}
1997
David Sedlák203ca3a2019-07-18 15:26:25 +02001998static void
1999test_leaf_elem(void **state)
2000{
David Sedlák203ca3a2019-07-18 15:26:25 +02002001 const char *data;
2002 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002003 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák203ca3a2019-07-18 15:26:25 +02002004 struct lysp_node_leaf *parsed = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02002005 uint16_t flags;
David Sedlák203ca3a2019-07-18 15:26:25 +02002006
2007 /* max elements */
2008 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002009 "<leaf name=\"leaf\">\n"
2010 " <config value=\"true\" />\n"
2011 " <default value=\"def-val\"/>\n"
2012 " <description><text>desc</text></description>\n"
2013 " <if-feature name=\"feature\" />\n"
2014 " <mandatory value=\"true\" />\n"
2015 " <must condition=\"must-cond\" />\n"
2016 " <reference><text>ref</text></reference>\n"
2017 " <status value=\"deprecated\"/>\n"
2018 " <type name=\"type\"/>\n"
2019 " <units name=\"uni\"/>\n"
2020 " <when condition=\"when-cond\"/>\n"
2021 EXT_SUBELEM
2022 "</leaf>"
David Sedlák203ca3a2019-07-18 15:26:25 +02002023 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002024 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák203ca3a2019-07-18 15:26:25 +02002025 parsed = (struct lysp_node_leaf *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002026 flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_DEPRC;
2027 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2028 "leaf", 0, LYS_LEAF, 0, "ref", 1);
2029 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002030 assert_string_equal(parsed->iffeatures[0].str, "feature");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002031 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_LEAF);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002032 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlák203ca3a2019-07-18 15:26:25 +02002033 assert_string_equal(parsed->type.name, "type");
2034 assert_string_equal(parsed->units, "uni");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002035 assert_string_equal(parsed->dflt.str, "def-val");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002036 lysp_node_free(&fctx, siblings);
David Sedlák203ca3a2019-07-18 15:26:25 +02002037 siblings = NULL;
2038
2039 /* min elements */
2040 data = ELEMENT_WRAPPER_START "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002041 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák203ca3a2019-07-18 15:26:25 +02002042 parsed = (struct lysp_node_leaf *)siblings;
2043 assert_string_equal(parsed->name, "leaf");
2044 assert_string_equal(parsed->type.name, "type");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002045 lysp_node_free(&fctx, siblings);
David Sedlák203ca3a2019-07-18 15:26:25 +02002046 siblings = NULL;
David Sedlák203ca3a2019-07-18 15:26:25 +02002047}
2048
David Sedlákc3da3ef2019-07-19 12:56:08 +02002049static void
2050test_leaf_list_elem(void **state)
2051{
David Sedlákc3da3ef2019-07-19 12:56:08 +02002052 const char *data;
2053 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002054 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlákc3da3ef2019-07-19 12:56:08 +02002055 struct lysp_node_leaflist *parsed = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02002056 uint16_t flags;
David Sedlákc3da3ef2019-07-19 12:56:08 +02002057
2058 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002059 "<leaf-list name=\"llist\">\n"
2060 " <config value=\"true\" />\n"
2061 " <default value=\"def-val0\"/>\n"
2062 " <default value=\"def-val1\"/>\n"
2063 " <description><text>desc</text></description>\n"
2064 " <if-feature name=\"feature\"/>\n"
2065 " <max-elements value=\"5\"/>\n"
2066 " <must condition=\"must-cond\"/>\n"
2067 " <ordered-by value=\"user\" />\n"
2068 " <reference><text>ref</text></reference>\n"
2069 " <status value=\"current\"/>\n"
2070 " <type name=\"type\"/>\n"
2071 " <units name=\"uni\"/>\n"
2072 " <when condition=\"when-cond\"/>\n"
2073 EXT_SUBELEM
2074 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002075 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002076 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002077 parsed = (struct lysp_node_leaflist *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002078 flags = LYS_CONFIG_W | LYS_ORDBY_USER | LYS_STATUS_CURR | LYS_SET_MAX;
2079 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2080 "llist", 0, LYS_LEAFLIST, 0, "ref", 1);
2081 CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002082 assert_string_equal(parsed->dflts[0].str, "def-val0");
2083 assert_string_equal(parsed->dflts[1].str, "def-val1");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002084 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002085 assert_int_equal(parsed->max, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002086 assert_string_equal(parsed->type.name, "type");
2087 assert_string_equal(parsed->units, "uni");
Radek Iša56ca9e42020-09-08 18:42:00 +02002088 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002089 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_LEAF_LIST);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002090 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002091 siblings = NULL;
2092
2093 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002094 "<leaf-list name=\"llist\">\n"
2095 " <config value=\"true\" />\n"
2096 " <description><text>desc</text></description>\n"
2097 " <if-feature name=\"feature\"/>\n"
2098 " <min-elements value=\"5\"/>\n"
2099 " <must condition=\"must-cond\"/>\n"
2100 " <ordered-by value=\"user\" />\n"
2101 " <reference><text>ref</text></reference>\n"
2102 " <status value=\"current\"/>\n"
2103 " <type name=\"type\"/>\n"
2104 " <units name=\"uni\"/>\n"
2105 " <when condition=\"when-cond\"/>\n"
2106 EXT_SUBELEM
2107 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002108 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002109 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002110 parsed = (struct lysp_node_leaflist *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002111 flags = LYS_CONFIG_W | LYS_ORDBY_USER | LYS_STATUS_CURR | LYS_SET_MIN;
2112 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2113 "llist", 0, LYS_LEAFLIST, 0, "ref", 1);
2114 CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL);
2115 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002116 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002117 assert_int_equal(parsed->min, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002118 assert_string_equal(parsed->type.name, "type");
2119 assert_string_equal(parsed->units, "uni");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002120 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_LEAF_LIST);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002121 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002122 siblings = NULL;
2123
2124 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002125 "<leaf-list name=\"llist\">\n"
2126 " <config value=\"true\" />\n"
2127 " <description><text>desc</text></description>\n"
2128 " <if-feature name=\"feature\"/>\n"
2129 " <max-elements value=\"15\"/>\n"
2130 " <min-elements value=\"5\"/>\n"
2131 " <must condition=\"must-cond\"/>\n"
2132 " <ordered-by value=\"user\" />\n"
2133 " <reference><text>ref</text></reference>\n"
2134 " <status value=\"current\"/>\n"
2135 " <type name=\"type\"/>\n"
2136 " <units name=\"uni\"/>\n"
2137 " <when condition=\"when-cond\"/>\n"
2138 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002139 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002140 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002141 parsed = (struct lysp_node_leaflist *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002142 flags = LYS_CONFIG_W | LYS_ORDBY_USER | LYS_STATUS_CURR | LYS_SET_MIN | LYS_SET_MAX;
2143 CHECK_LYSP_NODE(parsed, "desc", 0, flags, 1,
2144 "llist", 0, LYS_LEAFLIST, 0, "ref", 1);
2145 CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL);
2146 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002147 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002148 assert_int_equal(parsed->min, 5);
2149 assert_int_equal(parsed->max, 15);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002150 assert_string_equal(parsed->type.name, "type");
2151 assert_string_equal(parsed->units, "uni");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002152 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002153 siblings = NULL;
2154
2155 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002156 "<leaf-list name=\"llist\">\n"
2157 " <type name=\"type\"/>\n"
2158 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002159 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002160 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002161 parsed = (struct lysp_node_leaflist *)siblings;
2162 assert_string_equal(parsed->name, "llist");
2163 assert_string_equal(parsed->type.name, "type");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002164 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002165 siblings = NULL;
2166
2167 /* invalid combinations */
2168 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002169 "<leaf-list name=\"llist\">\n"
2170 " <max-elements value=\"5\"/>\n"
2171 " <min-elements value=\"15\"/>\n"
2172 " <type name=\"type\"/>"
2173 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002174 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002175 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_EVALID);
2176 CHECK_LOG_CTX("Invalid combination of min-elements and max-elements: min value 15 is bigger than the max value 5.", "Line number 4.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002177 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002178 siblings = NULL;
2179
2180 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002181 "<leaf-list name=\"llist\">\n"
2182 " <default value=\"def-val1\"/>\n"
2183 " <min-elements value=\"15\"/>\n"
2184 " <type name=\"type\"/>\n"
2185 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002186 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002187 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_EVALID);
2188 CHECK_LOG_CTX("Invalid combination of sub-elemnts \"min-elements\" and \"default\" in \"leaf-list\" element.", "Line number 5.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002189 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002190 siblings = NULL;
2191
2192 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002193 "<leaf-list name=\"llist\">"
2194 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002195 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002196 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_EVALID);
2197 CHECK_LOG_CTX("Missing mandatory sub-element \"type\" of \"leaf-list\" element.", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002198 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002199 siblings = NULL;
David Sedlákc3da3ef2019-07-19 12:56:08 +02002200}
2201
David Sedlákcb39f642019-07-19 13:19:55 +02002202static void
2203test_presence_elem(void **state)
2204{
David Sedlákcb39f642019-07-19 13:19:55 +02002205 const char *data;
2206 const char *val;
2207
2208 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002209 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlákcb39f642019-07-19 13:19:55 +02002210 assert_string_equal(val, "presence-val");
Michal Vaskoe180ed02021-02-05 16:31:20 +01002211 lydict_remove(UTEST_LYCTX, val);
David Sedlákcb39f642019-07-19 13:19:55 +02002212
2213 data = ELEMENT_WRAPPER_START "<presence/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002214 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
2215 CHECK_LOG_CTX("Missing mandatory attribute value of presence element.", "Line number 1.");
David Sedlákcb39f642019-07-19 13:19:55 +02002216}
2217
David Sedlák12470a82019-07-19 13:44:36 +02002218static void
2219test_key_elem(void **state)
2220{
David Sedlák12470a82019-07-19 13:44:36 +02002221 const char *data;
2222 const char *val;
2223
2224 data = ELEMENT_WRAPPER_START "<key value=\"key-value\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002225 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák12470a82019-07-19 13:44:36 +02002226 assert_string_equal(val, "key-value");
Michal Vaskoe180ed02021-02-05 16:31:20 +01002227 lydict_remove(UTEST_LYCTX, val);
David Sedlák12470a82019-07-19 13:44:36 +02002228
2229 data = ELEMENT_WRAPPER_START "<key/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002230 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
2231 CHECK_LOG_CTX("Missing mandatory attribute value of key element.", "Line number 1.");
David Sedlák12470a82019-07-19 13:44:36 +02002232}
2233
David Sedlák04e17b22019-07-19 15:29:48 +02002234static void
David Sedlák0d6de5a2019-07-22 13:25:44 +02002235test_uses_elem(void **state)
2236{
David Sedlák0d6de5a2019-07-22 13:25:44 +02002237 const char *data;
2238 struct lysp_node *siblings = NULL;
2239 struct tree_node_meta node_meta = {NULL, &siblings};
2240 struct lysp_node_uses *parsed = NULL;
2241
2242 /* max subelems */
2243 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002244 "<uses name=\"uses-name\">\n"
2245 " <when condition=\"cond\" />\n"
2246 " <if-feature name=\"feature\" />\n"
2247 " <status value=\"obsolete\" />\n"
2248 " <description><text>desc</text></description>\n"
2249 " <reference><text>ref</text></reference>\n"
2250 " <refine target-node=\"target\"/>\n"
2251 " <augment target-node=\"target\" />\n"
2252 EXT_SUBELEM
2253 "</uses>"
2254 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002255 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002256 parsed = (struct lysp_node_uses *)&siblings[0];
Radek Iša56ca9e42020-09-08 18:42:00 +02002257 CHECK_LYSP_NODE(parsed, "desc", 1, LYS_STATUS_OBSLT, 1,
2258 "uses-name", 0, LYS_USES, 0, "ref", 1);
2259 CHECK_LYSP_WHEN(parsed->when, "cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002260 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlák0d6de5a2019-07-22 13:25:44 +02002261 assert_string_equal(parsed->refines->nodeid, "target");
David Sedlák992fb7c2019-07-24 16:51:01 +02002262 assert_string_equal(parsed->augments->nodeid, "target");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002263 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_USES);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002264 lysp_node_free(&fctx, siblings);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002265 siblings = NULL;
2266
2267 /* min subelems */
2268 data = ELEMENT_WRAPPER_START "<uses name=\"uses-name\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002269 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002270 assert_string_equal(siblings[0].name, "uses-name");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002271 lysp_node_free(&fctx, siblings);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002272 siblings = NULL;
David Sedlák0d6de5a2019-07-22 13:25:44 +02002273}
2274
David Sedlákaa854b02019-07-22 14:17:10 +02002275static void
David Sedlákaf536aa2019-07-23 13:42:23 +02002276test_list_elem(void **state)
2277{
David Sedlákaf536aa2019-07-23 13:42:23 +02002278 const char *data;
2279 struct lysp_node *siblings = NULL;
2280 struct tree_node_meta node_meta = {NULL, &siblings};
2281 struct lysp_node_list *parsed = NULL;
2282
2283 /* max subelems */
2284 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002285 "<list name=\"list-name\">\n"
2286 " <when condition=\"when\"/>\n"
2287 " <if-feature name=\"iff\"/>\n"
2288 " <must condition=\"must-cond\"/>\n"
2289 " <key value=\"key\"/>\n"
2290 " <unique tag=\"utag\"/>\n"
2291 " <config value=\"true\"/>\n"
2292 " <min-elements value=\"10\"/>\n"
2293 " <ordered-by value=\"user\"/>\n"
2294 " <status value=\"deprecated\"/>\n"
2295 " <description><text>desc</text></description>\n"
2296 " <reference><text>ref</text></reference>\n"
2297 " <anydata name=\"anyd\"/>\n"
2298 " <anyxml name=\"anyx\"/>\n"
2299 " <container name=\"cont\"/>\n"
2300 " <choice name=\"choice\"/>\n"
2301 " <action name=\"action\"/>\n"
2302 " <grouping name=\"grp\"/>\n"
2303 " <notification name=\"notf\"/>\n"
2304 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2305 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2306 " <list name=\"sub-list\"/>\n"
2307 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2308 " <uses name=\"uses-name\"/>\n"
2309 EXT_SUBELEM
2310 "</list>"
2311 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002312 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákaf536aa2019-07-23 13:42:23 +02002313 parsed = (struct lysp_node_list *)&siblings[0];
David Sedlákaf536aa2019-07-23 13:42:23 +02002314 assert_string_equal(parsed->child->name, "anyd");
2315 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2316 assert_string_equal(parsed->child->next->name, "anyx");
2317 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
David Sedlákf111bcb2019-07-23 17:15:51 +02002318 assert_string_equal(parsed->child->next->next->name, "cont");
2319 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002320 assert_string_equal(parsed->child->next->next->next->name, "choice");
2321 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
David Sedlák85d0eca2019-07-24 15:15:21 +02002322 assert_string_equal(parsed->child->next->next->next->next->name, "leaf");
2323 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAF);
2324 assert_string_equal(parsed->child->next->next->next->next->next->name, "llist");
2325 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2326 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "sub-list");
2327 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LIST);
2328 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "uses-name");
2329 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_USES);
2330 assert_null(parsed->child->next->next->next->next->next->next->next->next);
Radek Iša56ca9e42020-09-08 18:42:00 +02002331 uint16_t flags = LYS_ORDBY_USER | LYS_STATUS_DEPRC | LYS_CONFIG_W | LYS_SET_MIN;
2332
2333 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2334 "list-name", 0, LYS_LIST, 0, "ref", 1);
2335 CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL);
2336 CHECK_LYSP_WHEN(parsed->when, "when", NULL, 0, NULL);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002337 assert_string_equal(parsed->groupings->name, "grp");
David Sedlák85d0eca2019-07-24 15:15:21 +02002338 assert_string_equal(parsed->actions->name, "action");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002339 assert_int_equal(parsed->groupings->nodetype, LYS_GROUPING);
David Sedlák031b9e72019-07-23 15:19:37 +02002340 assert_string_equal(parsed->notifs->name, "notf");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002341 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlákaf536aa2019-07-23 13:42:23 +02002342 assert_string_equal(parsed->key, "key");
2343 assert_int_equal(parsed->min, 10);
David Sedlákaf536aa2019-07-23 13:42:23 +02002344 assert_string_equal(parsed->typedefs->name, "tpdf");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002345 assert_string_equal(parsed->uniques->str, "utag");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002346 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_LIST);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002347 lysp_node_free(&fctx, siblings);
Radek Iša56ca9e42020-09-08 18:42:00 +02002348 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
David Sedlákaf536aa2019-07-23 13:42:23 +02002349 siblings = NULL;
2350
2351 /* min subelems */
2352 data = ELEMENT_WRAPPER_START "<list name=\"list-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002353 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákaf536aa2019-07-23 13:42:23 +02002354 parsed = (struct lysp_node_list *)&siblings[0];
Radek Iša56ca9e42020-09-08 18:42:00 +02002355 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2356 "list-name", 0, LYS_LIST, 0, NULL, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002357 lysp_node_free(&fctx, siblings);
David Sedlákaf536aa2019-07-23 13:42:23 +02002358 siblings = NULL;
David Sedlákaf536aa2019-07-23 13:42:23 +02002359}
2360
David Sedlák031b9e72019-07-23 15:19:37 +02002361static void
2362test_notification_elem(void **state)
2363{
David Sedlák031b9e72019-07-23 15:19:37 +02002364 const char *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002365 struct lysp_node_notif *notifs = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002366 struct tree_node_meta notif_meta = {NULL, (struct lysp_node **)&notifs};
David Sedlák031b9e72019-07-23 15:19:37 +02002367
2368 /* max subelems */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002369 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlák031b9e72019-07-23 15:19:37 +02002370 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002371 "<notification name=\"notif-name\">\n"
2372 " <anydata name=\"anyd\"/>\n"
2373 " <anyxml name=\"anyx\"/>\n"
2374 " <description><text>desc</text></description>\n"
2375 " <if-feature name=\"iff\"/>\n"
2376 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2377 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2378 " <list name=\"sub-list\"/>\n"
2379 " <must condition=\"cond\"/>\n"
2380 " <reference><text>ref</text></reference>\n"
2381 " <status value=\"deprecated\"/>\n"
2382 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2383 " <uses name=\"uses-name\"/>\n"
2384 " <container name=\"cont\"/>\n"
2385 " <choice name=\"choice\"/>\n"
2386 " <grouping name=\"grp\"/>\n"
2387 EXT_SUBELEM
2388 "</notification>"
2389 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002390 assert_int_equal(test_element_helper(state, data, &notif_meta, NULL, NULL), LY_SUCCESS);
David Sedlák031b9e72019-07-23 15:19:37 +02002391 assert_string_equal(notifs->name, "notif-name");
Radek Krejci01180ac2021-01-27 08:48:22 +01002392 assert_string_equal(notifs->child->name, "anyd");
2393 assert_int_equal(notifs->child->nodetype, LYS_ANYDATA);
2394 assert_string_equal(notifs->child->next->name, "anyx");
2395 assert_int_equal(notifs->child->next->nodetype, LYS_ANYXML);
2396 assert_string_equal(notifs->child->next->next->name, "leaf");
2397 assert_int_equal(notifs->child->next->next->nodetype, LYS_LEAF);
2398 assert_string_equal(notifs->child->next->next->next->name, "llist");
2399 assert_int_equal(notifs->child->next->next->next->nodetype, LYS_LEAFLIST);
2400 assert_string_equal(notifs->child->next->next->next->next->name, "sub-list");
2401 assert_int_equal(notifs->child->next->next->next->next->nodetype, LYS_LIST);
David Sedlák031b9e72019-07-23 15:19:37 +02002402 assert_true(notifs->flags & LYS_STATUS_DEPRC);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002403 assert_string_equal(notifs->groupings->name, "grp");
2404 assert_int_equal(notifs->groupings->nodetype, LYS_GROUPING);
Radek Krejci01180ac2021-01-27 08:48:22 +01002405 assert_string_equal(notifs->child->next->next->next->next->next->name, "uses-name");
2406 assert_int_equal(notifs->child->next->next->next->next->next->nodetype, LYS_USES);
2407 assert_string_equal(notifs->child->next->next->next->next->next->next->name, "cont");
2408 assert_int_equal(notifs->child->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
2409 assert_int_equal(notifs->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2410 assert_string_equal(notifs->child->next->next->next->next->next->next->next->name, "choice");
2411 assert_null(notifs->child->next->next->next->next->next->next->next->next);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002412 assert_string_equal(notifs->iffeatures[0].str, "iff");
2413 assert_string_equal(notifs->musts->arg.str, "cond");
David Sedlák031b9e72019-07-23 15:19:37 +02002414 assert_int_equal(notifs->nodetype, LYS_NOTIF);
2415 assert_null(notifs->parent);
2416 assert_string_equal(notifs->ref, "ref");
2417 assert_string_equal(notifs->typedefs->name, "tpdf");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002418 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(notifs->exts[0]), LY_STMT_NOTIFICATION);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002419 lysp_node_free(&fctx, (struct lysp_node *)notifs);
David Sedlák031b9e72019-07-23 15:19:37 +02002420 notifs = NULL;
2421
2422 /* min subelems */
2423 data = ELEMENT_WRAPPER_START "<notification name=\"notif-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002424 assert_int_equal(test_element_helper(state, data, &notif_meta, NULL, NULL), LY_SUCCESS);
David Sedlák031b9e72019-07-23 15:19:37 +02002425 assert_string_equal(notifs->name, "notif-name");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002426 lysp_node_free(&fctx, (struct lysp_node *)notifs);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002427 notifs = NULL;
David Sedláke3ce9ef2019-07-23 16:34:30 +02002428}
2429
2430static void
2431test_grouping_elem(void **state)
2432{
David Sedláke3ce9ef2019-07-23 16:34:30 +02002433 const char *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002434 struct lysp_node_grp *grps = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002435 struct tree_node_meta grp_meta = {NULL, (struct lysp_node **)&grps};
David Sedláke3ce9ef2019-07-23 16:34:30 +02002436
2437 /* max subelems */
2438 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002439 "<grouping name=\"grp-name\">\n"
2440 " <anydata name=\"anyd\"/>\n"
2441 " <anyxml name=\"anyx\"/>\n"
2442 " <description><text>desc</text></description>\n"
2443 " <grouping name=\"sub-grp\"/>\n"
2444 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2445 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2446 " <list name=\"list\"/>\n"
2447 " <notification name=\"notf\"/>\n"
2448 " <reference><text>ref</text></reference>\n"
2449 " <status value=\"current\"/>\n"
2450 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2451 " <uses name=\"uses-name\"/>\n"
2452 " <action name=\"act\"/>\n"
2453 " <container name=\"cont\"/>\n"
2454 " <choice name=\"choice\"/>\n"
2455 EXT_SUBELEM
2456 "</grouping>"
2457 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002458 assert_int_equal(test_element_helper(state, data, &grp_meta, NULL, NULL), LY_SUCCESS);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002459 assert_string_equal(grps->name, "grp-name");
Radek Krejci01180ac2021-01-27 08:48:22 +01002460 assert_string_equal(grps->child->name, "anyd");
2461 assert_string_equal(grps->child->next->name, "anyx");
2462 assert_string_equal(grps->child->next->next->name, "leaf");
2463 assert_string_equal(grps->child->next->next->next->name, "llist");
2464 assert_string_equal(grps->child->next->next->next->next->name, "list");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002465 assert_string_equal(grps->dsc, "desc");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002466 assert_true(grps->flags & LYS_STATUS_CURR);
2467 assert_string_equal(grps->groupings->name, "sub-grp");
2468 assert_int_equal(grps->nodetype, LYS_GROUPING);
2469 assert_string_equal(grps->notifs->name, "notf");
2470 assert_null(grps->parent);
2471 assert_string_equal(grps->ref, "ref");
2472 assert_string_equal(grps->typedefs->name, "tpdf");
David Sedlák85d0eca2019-07-24 15:15:21 +02002473 assert_string_equal(grps->actions->name, "act");
Radek Krejci01180ac2021-01-27 08:48:22 +01002474 assert_string_equal(grps->child->next->next->next->next->next->name, "uses-name");
2475 assert_int_equal(grps->child->next->next->next->next->next->nodetype, LYS_USES);
2476 assert_string_equal(grps->child->next->next->next->next->next->next->name, "cont");
2477 assert_int_equal(grps->child->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
2478 assert_string_equal(grps->child->next->next->next->next->next->next->next->name, "choice");
2479 assert_int_equal(grps->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002480 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(grps->exts[0]), LY_STMT_GROUPING);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002481 lysp_node_free(&fctx, &grps->node);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002482 grps = NULL;
2483
2484 /* min subelems */
2485 data = ELEMENT_WRAPPER_START "<grouping name=\"grp-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002486 assert_int_equal(test_element_helper(state, data, &grp_meta, NULL, NULL), LY_SUCCESS);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002487 assert_string_equal(grps->name, "grp-name");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002488 lysp_node_free(&fctx, &grps->node);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002489 grps = NULL;
David Sedlák031b9e72019-07-23 15:19:37 +02002490}
2491
David Sedlákf111bcb2019-07-23 17:15:51 +02002492static void
2493test_container_elem(void **state)
2494{
David Sedlákf111bcb2019-07-23 17:15:51 +02002495 const char *data;
2496 struct lysp_node *siblings = NULL;
2497 struct tree_node_meta node_meta = {NULL, &siblings};
2498 struct lysp_node_container *parsed = NULL;
2499
2500 /* max subelems */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002501 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedláke2dc9e92019-07-24 09:59:21 +02002502 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002503 "<container name=\"cont-name\">\n"
2504 " <anydata name=\"anyd\"/>\n"
2505 " <anyxml name=\"anyx\"/>\n"
2506 " <config value=\"true\"/>\n"
2507 " <container name=\"subcont\"/>\n"
2508 " <description><text>desc</text></description>\n"
2509 " <grouping name=\"sub-grp\"/>\n"
2510 " <if-feature name=\"iff\"/>\n"
2511 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2512 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2513 " <list name=\"list\"/>\n"
2514 " <must condition=\"cond\"/>\n"
2515 " <notification name=\"notf\"/>\n"
2516 " <presence value=\"presence\"/>\n"
2517 " <reference><text>ref</text></reference>\n"
2518 " <status value=\"current\"/>\n"
2519 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2520 " <uses name=\"uses-name\"/>\n"
2521 " <when condition=\"when-cond\"/>\n"
2522 " <action name=\"act\"/>\n"
2523 " <choice name=\"choice\"/>\n"
2524 EXT_SUBELEM
2525 "</container>"
2526 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002527 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedláke2dc9e92019-07-24 09:59:21 +02002528 parsed = (struct lysp_node_container *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002529 uint16_t flags = LYS_CONFIG_W | LYS_STATUS_CURR;
2530
2531 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2532 "cont-name", 0, LYS_CONTAINER, 0, "ref", 1);
2533 CHECK_LYSP_RESTR(parsed->musts, "cond", NULL, NULL, NULL, 0, NULL);
2534 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
2535
Michal Vasko7f45cf22020-10-01 12:49:44 +02002536 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedláke2dc9e92019-07-24 09:59:21 +02002537 assert_string_equal(parsed->presence, "presence");
2538 assert_string_equal(parsed->typedefs->name, "tpdf");
2539 assert_string_equal(parsed->groupings->name, "sub-grp");
2540 assert_string_equal(parsed->child->name, "anyd");
2541 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2542 assert_string_equal(parsed->child->next->name, "anyx");
2543 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2544 assert_string_equal(parsed->child->next->next->name, "subcont");
2545 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
2546 assert_string_equal(parsed->child->next->next->next->name, "leaf");
2547 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
2548 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
2549 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
2550 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
2551 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
2552 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
2553 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002554 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
2555 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2556 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke2dc9e92019-07-24 09:59:21 +02002557 assert_string_equal(parsed->notifs->name, "notf");
David Sedlák85d0eca2019-07-24 15:15:21 +02002558 assert_string_equal(parsed->actions->name, "act");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002559 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_CONTAINER);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002560 lysp_node_free(&fctx, siblings);
Radek Iša56ca9e42020-09-08 18:42:00 +02002561 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
David Sedláke2dc9e92019-07-24 09:59:21 +02002562 siblings = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02002563
2564 /* min subelems */
2565 data = ELEMENT_WRAPPER_START "<container name=\"cont-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002566 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákf111bcb2019-07-23 17:15:51 +02002567 parsed = (struct lysp_node_container *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002568 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2569 "cont-name", 0, LYS_CONTAINER, 0, NULL, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002570 lysp_node_free(&fctx, siblings);
David Sedlákf111bcb2019-07-23 17:15:51 +02002571 siblings = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02002572}
2573
David Sedlák5379d392019-07-24 10:42:03 +02002574static void
2575test_case_elem(void **state)
2576{
David Sedlák5379d392019-07-24 10:42:03 +02002577 const char *data;
2578 struct lysp_node *siblings = NULL;
2579 struct tree_node_meta node_meta = {NULL, &siblings};
2580 struct lysp_node_case *parsed = NULL;
2581
2582 /* max subelems */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002583 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlák5379d392019-07-24 10:42:03 +02002584 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002585 "<case name=\"case-name\">\n"
2586 " <anydata name=\"anyd\"/>\n"
2587 " <anyxml name=\"anyx\"/>\n"
2588 " <container name=\"subcont\"/>\n"
2589 " <description><text>desc</text></description>\n"
2590 " <if-feature name=\"iff\"/>\n"
2591 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2592 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2593 " <list name=\"list\"/>\n"
2594 " <reference><text>ref</text></reference>\n"
2595 " <status value=\"current\"/>\n"
2596 " <uses name=\"uses-name\"/>\n"
2597 " <when condition=\"when-cond\"/>\n"
2598 " <choice name=\"choice\"/>\n"
2599 EXT_SUBELEM
2600 "</case>"
2601 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002602 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák5379d392019-07-24 10:42:03 +02002603 parsed = (struct lysp_node_case *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002604 uint16_t flags = LYS_STATUS_CURR;
2605
2606 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2607 "case-name", 0, LYS_CASE, 0, "ref", 1);
2608 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002609 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlák5379d392019-07-24 10:42:03 +02002610 assert_string_equal(parsed->child->name, "anyd");
2611 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2612 assert_string_equal(parsed->child->next->name, "anyx");
2613 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2614 assert_string_equal(parsed->child->next->next->name, "subcont");
2615 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
2616 assert_string_equal(parsed->child->next->next->next->name, "leaf");
2617 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
2618 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
2619 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
2620 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
2621 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
2622 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
2623 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002624 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
2625 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2626 assert_null(parsed->child->next->next->next->next->next->next->next->next);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002627 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_CASE);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002628 lysp_node_free(&fctx, siblings);
David Sedlák5379d392019-07-24 10:42:03 +02002629 siblings = NULL;
2630
2631 /* min subelems */
2632 data = ELEMENT_WRAPPER_START "<case name=\"case-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002633 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák5379d392019-07-24 10:42:03 +02002634 parsed = (struct lysp_node_case *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002635 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2636 "case-name", 0, LYS_CASE, 0, NULL, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002637 lysp_node_free(&fctx, siblings);
David Sedlák5379d392019-07-24 10:42:03 +02002638 siblings = NULL;
David Sedlák5379d392019-07-24 10:42:03 +02002639}
2640
David Sedlákb7abcfa2019-07-24 12:33:35 +02002641static void
2642test_choice_elem(void **state)
2643{
David Sedlákb7abcfa2019-07-24 12:33:35 +02002644 const char *data;
2645 struct lysp_node *siblings = NULL;
2646 struct tree_node_meta node_meta = {NULL, &siblings};
2647 struct lysp_node_choice *parsed = NULL;
2648
2649 /* max subelems */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002650 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002651 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002652 "<choice name=\"choice-name\">\n"
2653 " <anydata name=\"anyd\"/>\n"
2654 " <anyxml name=\"anyx\"/>\n"
2655 " <case name=\"sub-case\"/>\n"
2656 " <choice name=\"choice\"/>\n"
2657 " <config value=\"true\"/>\n"
2658 " <container name=\"subcont\"/>\n"
2659 " <default value=\"def\"/>\n"
2660 " <description><text>desc</text></description>\n"
2661 " <if-feature name=\"iff\"/>\n"
2662 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2663 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2664 " <list name=\"list\"/>\n"
2665 " <mandatory value=\"true\" />\n"
2666 " <reference><text>ref</text></reference>\n"
2667 " <status value=\"current\"/>\n"
2668 " <when condition=\"when-cond\"/>\n"
2669 EXT_SUBELEM
2670 "</choice>"
2671 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002672 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002673 parsed = (struct lysp_node_choice *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002674 uint16_t flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_CURR;
2675
2676 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2677 "choice-name", 0, LYS_CHOICE, 0, "ref", 1);
2678 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002679 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlákb7abcfa2019-07-24 12:33:35 +02002680 assert_string_equal(parsed->child->name, "anyd");
2681 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2682 assert_string_equal(parsed->child->next->name, "anyx");
2683 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2684 assert_string_equal(parsed->child->next->next->name, "sub-case");
2685 assert_int_equal(parsed->child->next->next->nodetype, LYS_CASE);
2686 assert_string_equal(parsed->child->next->next->next->name, "choice");
2687 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
2688 assert_string_equal(parsed->child->next->next->next->next->name, "subcont");
2689 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_CONTAINER);
2690 assert_string_equal(parsed->child->next->next->next->next->next->name, "leaf");
2691 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAF);
2692 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "llist");
2693 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2694 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "list");
2695 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
2696 assert_null(parsed->child->next->next->next->next->next->next->next->next);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002697 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_CHOICE);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002698 lysp_node_free(&fctx, siblings);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002699 siblings = NULL;
2700
2701 /* min subelems */
2702 data = ELEMENT_WRAPPER_START "<choice name=\"choice-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002703 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002704 parsed = (struct lysp_node_choice *)siblings;
2705 assert_string_equal(parsed->name, "choice-name");
Radek Iša56ca9e42020-09-08 18:42:00 +02002706 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2707 "choice-name", 0, LYS_CHOICE, 0, NULL, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002708 lysp_node_free(&fctx, siblings);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002709 siblings = NULL;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002710}
2711
David Sedlák05404f62019-07-24 14:11:53 +02002712static void
2713test_inout_elem(void **state)
2714{
David Sedlák05404f62019-07-24 14:11:53 +02002715 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02002716 struct lysp_node_action_inout inout = {0};
David Sedlák05404f62019-07-24 14:11:53 +02002717 struct inout_meta inout_meta = {NULL, &inout};
2718
2719 /* max subelements */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002720 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlák05404f62019-07-24 14:11:53 +02002721 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002722 "<input>\n"
2723 " <anydata name=\"anyd\"/>\n"
2724 " <anyxml name=\"anyx\"/>\n"
2725 " <choice name=\"choice\"/>\n"
2726 " <container name=\"subcont\"/>\n"
2727 " <grouping name=\"sub-grp\"/>\n"
2728 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2729 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2730 " <list name=\"list\"/>\n"
2731 " <must condition=\"cond\"/>\n"
2732 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2733 " <uses name=\"uses-name\"/>\n"
2734 EXT_SUBELEM
2735 "</input>"
2736 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002737 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
2738 CHECK_LYSP_ACTION_INOUT(&(inout), 1, 1, 1, 1, LYS_INPUT, 0, 1);
2739 CHECK_LYSP_RESTR(inout.musts, "cond", NULL, NULL, NULL, 0, NULL);
David Sedlák05404f62019-07-24 14:11:53 +02002740 assert_string_equal(inout.typedefs->name, "tpdf");
2741 assert_string_equal(inout.groupings->name, "sub-grp");
Radek Krejci01180ac2021-01-27 08:48:22 +01002742 assert_string_equal(inout.child->name, "anyd");
2743 assert_int_equal(inout.child->nodetype, LYS_ANYDATA);
2744 assert_string_equal(inout.child->next->name, "anyx");
2745 assert_int_equal(inout.child->next->nodetype, LYS_ANYXML);
2746 assert_string_equal(inout.child->next->next->name, "choice");
2747 assert_int_equal(inout.child->next->next->nodetype, LYS_CHOICE);
2748 assert_string_equal(inout.child->next->next->next->name, "subcont");
2749 assert_int_equal(inout.child->next->next->next->nodetype, LYS_CONTAINER);
2750 assert_string_equal(inout.child->next->next->next->next->name, "leaf");
2751 assert_int_equal(inout.child->next->next->next->next->nodetype, LYS_LEAF);
2752 assert_string_equal(inout.child->next->next->next->next->next->name, "llist");
2753 assert_int_equal(inout.child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2754 assert_string_equal(inout.child->next->next->next->next->next->next->name, "list");
2755 assert_int_equal(inout.child->next->next->next->next->next->next->nodetype, LYS_LIST);
2756 assert_string_equal(inout.child->next->next->next->next->next->next->next->name, "uses-name");
2757 assert_int_equal(inout.child->next->next->next->next->next->next->next->nodetype, LYS_USES);
2758 assert_null(inout.child->next->next->next->next->next->next->next->next);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002759 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(inout.exts[0]), LY_STMT_INPUT);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002760 lysp_node_free(&fctx, (struct lysp_node *)&inout);
David Sedlák05404f62019-07-24 14:11:53 +02002761 memset(&inout, 0, sizeof inout);
2762
2763 /* max subelements */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002764 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlák05404f62019-07-24 14:11:53 +02002765 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002766 "<output>\n"
2767 " <anydata name=\"anyd\"/>\n"
2768 " <anyxml name=\"anyx\"/>\n"
2769 " <choice name=\"choice\"/>\n"
2770 " <container name=\"subcont\"/>\n"
2771 " <grouping name=\"sub-grp\"/>\n"
2772 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2773 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2774 " <list name=\"list\"/>\n"
2775 " <must condition=\"cond\"/>\n"
2776 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2777 " <uses name=\"uses-name\"/>\n"
2778 EXT_SUBELEM
2779 "</output>"
2780 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002781 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
2782 CHECK_LYSP_ACTION_INOUT(&(inout), 1, 1, 1, 1, LYS_OUTPUT, 0, 1);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002783 assert_string_equal(inout.musts->arg.str, "cond");
David Sedlák05404f62019-07-24 14:11:53 +02002784 assert_string_equal(inout.typedefs->name, "tpdf");
2785 assert_string_equal(inout.groupings->name, "sub-grp");
Radek Krejci01180ac2021-01-27 08:48:22 +01002786 assert_string_equal(inout.child->name, "anyd");
2787 assert_int_equal(inout.child->nodetype, LYS_ANYDATA);
2788 assert_string_equal(inout.child->next->name, "anyx");
2789 assert_int_equal(inout.child->next->nodetype, LYS_ANYXML);
2790 assert_string_equal(inout.child->next->next->name, "choice");
2791 assert_int_equal(inout.child->next->next->nodetype, LYS_CHOICE);
2792 assert_string_equal(inout.child->next->next->next->name, "subcont");
2793 assert_int_equal(inout.child->next->next->next->nodetype, LYS_CONTAINER);
2794 assert_string_equal(inout.child->next->next->next->next->name, "leaf");
2795 assert_int_equal(inout.child->next->next->next->next->nodetype, LYS_LEAF);
2796 assert_string_equal(inout.child->next->next->next->next->next->name, "llist");
2797 assert_int_equal(inout.child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2798 assert_string_equal(inout.child->next->next->next->next->next->next->name, "list");
2799 assert_int_equal(inout.child->next->next->next->next->next->next->nodetype, LYS_LIST);
2800 assert_string_equal(inout.child->next->next->next->next->next->next->next->name, "uses-name");
2801 assert_int_equal(inout.child->next->next->next->next->next->next->next->nodetype, LYS_USES);
2802 assert_null(inout.child->next->next->next->next->next->next->next->next);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002803 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(inout.exts[0]), LY_STMT_OUTPUT);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002804 lysp_node_free(&fctx, (struct lysp_node *)&inout);
David Sedlák05404f62019-07-24 14:11:53 +02002805 memset(&inout, 0, sizeof inout);
2806
2807 /* min subelems */
Michal Vaskob83af8a2020-01-06 09:49:22 +01002808 data = ELEMENT_WRAPPER_START "<input><leaf name=\"l\"><type name=\"empty\"/></leaf></input>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002809 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002810 lysp_node_free(&fctx, (struct lysp_node *)&inout);
David Sedlák05404f62019-07-24 14:11:53 +02002811 memset(&inout, 0, sizeof inout);
2812
Michal Vaskob83af8a2020-01-06 09:49:22 +01002813 data = ELEMENT_WRAPPER_START "<output><leaf name=\"l\"><type name=\"empty\"/></leaf></output>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002814 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002815 lysp_node_free(&fctx, (struct lysp_node *)&inout);
David Sedlák05404f62019-07-24 14:11:53 +02002816 memset(&inout, 0, sizeof inout);
2817
2818 /* invalid combinations */
2819 data = ELEMENT_WRAPPER_START "<input name=\"test\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002820 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_EVALID);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002821 lysp_node_free(&fctx, (struct lysp_node *)&inout);
Radek Iša56ca9e42020-09-08 18:42:00 +02002822 CHECK_LOG_CTX("Unexpected attribute \"name\" of \"input\" element.", "Line number 1.");
David Sedlák05404f62019-07-24 14:11:53 +02002823 memset(&inout, 0, sizeof inout);
David Sedlák05404f62019-07-24 14:11:53 +02002824}
2825
David Sedlák85d0eca2019-07-24 15:15:21 +02002826static void
2827test_action_elem(void **state)
2828{
David Sedlák85d0eca2019-07-24 15:15:21 +02002829 const char *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002830 struct lysp_node_action *actions = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002831 struct tree_node_meta act_meta = {NULL, (struct lysp_node **)&actions};
Radek Iša56ca9e42020-09-08 18:42:00 +02002832 uint16_t flags;
David Sedlák85d0eca2019-07-24 15:15:21 +02002833
2834 /* max subelems */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002835 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlák85d0eca2019-07-24 15:15:21 +02002836 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002837 "<action name=\"act\">\n"
2838 " <description><text>desc</text></description>\n"
2839 " <grouping name=\"grouping\"/>\n"
2840 " <if-feature name=\"iff\"/>\n"
2841 " <input><uses name=\"uses-name\"/></input>\n"
2842 " <output><must condition=\"cond\"/><leaf name=\"l\"><type name=\"type\"/></leaf></output>\n"
2843 " <reference><text>ref</text></reference>\n"
2844 " <status value=\"deprecated\"/>\n"
2845 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2846 EXT_SUBELEM
2847 "</action>"
2848 ELEMENT_WRAPPER_END;
Michal Vasko1bf09392020-03-27 12:38:10 +01002849 /* there must be parent for action */
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002850 act_meta.parent = (void *)1;
Radek Iša56ca9e42020-09-08 18:42:00 +02002851 assert_int_equal(test_element_helper(state, data, &act_meta, NULL, NULL), LY_SUCCESS);
Michal Vasko1bf09392020-03-27 12:38:10 +01002852 act_meta.parent = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02002853 flags = LYS_STATUS_DEPRC;
2854 CHECK_LYSP_ACTION(actions, "desc", 1, flags, 1, 1,\
2855 1, 0, 0, 0,\
2856 1, 0,\
2857 "act", LYS_ACTION, \
2858 1, 0, 0, 1,\
2859 1, 0,\
2860 1, "ref", 1);
2861
Michal Vasko7f45cf22020-10-01 12:49:44 +02002862 assert_string_equal(actions->iffeatures[0].str, "iff");
David Sedlák85d0eca2019-07-24 15:15:21 +02002863 assert_string_equal(actions->typedefs->name, "tpdf");
2864 assert_string_equal(actions->groupings->name, "grouping");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002865 assert_string_equal(actions->output.musts->arg.str, "cond");
Radek Krejci01180ac2021-01-27 08:48:22 +01002866 assert_string_equal(actions->input.child->name, "uses-name");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002867 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(actions->exts[0]), LY_STMT_ACTION);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002868 lysp_node_free(&fctx, (struct lysp_node *)actions);
David Sedlák85d0eca2019-07-24 15:15:21 +02002869 actions = NULL;
2870
Michal Vasko8a67eff2021-12-07 14:04:47 +01002871 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlákeaa45792019-07-24 15:25:01 +02002872 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002873 "<rpc name=\"act\">\n"
2874 " <description><text>desc</text></description>\n"
2875 " <grouping name=\"grouping\"/>\n"
2876 " <if-feature name=\"iff\"/>\n"
2877 " <input><uses name=\"uses-name\"/></input>\n"
2878 " <output><must condition=\"cond\"/><leaf name=\"l\"><type name=\"type\"/></leaf></output>\n"
2879 " <reference><text>ref</text></reference>\n"
2880 " <status value=\"deprecated\"/>\n"
2881 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2882 EXT_SUBELEM
2883 "</rpc>"
2884 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002885 assert_int_equal(test_element_helper(state, data, &act_meta, NULL, NULL), LY_SUCCESS);
2886 flags = LYS_STATUS_DEPRC;
2887 CHECK_LYSP_ACTION(actions, "desc", 1, flags, 1, 1,\
2888 1, 0, 0, 0,\
2889 1, 0,\
2890 "act", LYS_RPC, \
2891 1, 0, 0, 1,\
2892 1, 0,\
2893 0, "ref", 1);
2894
Michal Vasko7f45cf22020-10-01 12:49:44 +02002895 assert_string_equal(actions->iffeatures[0].str, "iff");
David Sedlákeaa45792019-07-24 15:25:01 +02002896 assert_string_equal(actions->typedefs->name, "tpdf");
2897 assert_string_equal(actions->groupings->name, "grouping");
Radek Krejci01180ac2021-01-27 08:48:22 +01002898 assert_string_equal(actions->input.child->name, "uses-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002899 assert_string_equal(actions->output.musts->arg.str, "cond");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002900 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(actions->exts[0]), LY_STMT_RPC);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002901 lysp_node_free(&fctx, (struct lysp_node *)actions);
David Sedlákeaa45792019-07-24 15:25:01 +02002902 actions = NULL;
2903
David Sedlák85d0eca2019-07-24 15:15:21 +02002904 /* min subelems */
2905 data = ELEMENT_WRAPPER_START "<action name=\"act\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002906 assert_int_equal(test_element_helper(state, data, &act_meta, NULL, NULL), LY_SUCCESS);
David Sedlák85d0eca2019-07-24 15:15:21 +02002907 assert_string_equal(actions->name, "act");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002908 lysp_node_free(&fctx, (struct lysp_node *)actions);
David Sedlák85d0eca2019-07-24 15:15:21 +02002909 actions = NULL;
David Sedlák85d0eca2019-07-24 15:15:21 +02002910}
2911
David Sedlák992fb7c2019-07-24 16:51:01 +02002912static void
2913test_augment_elem(void **state)
2914{
David Sedlák992fb7c2019-07-24 16:51:01 +02002915 const char *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002916 struct lysp_node_augment *augments = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002917 struct tree_node_meta aug_meta = {NULL, (struct lysp_node **)&augments};
David Sedlák992fb7c2019-07-24 16:51:01 +02002918
Michal Vasko8a67eff2021-12-07 14:04:47 +01002919 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlák992fb7c2019-07-24 16:51:01 +02002920 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002921 "<augment target-node=\"target\">\n"
2922 " <action name=\"action\"/>\n"
2923 " <anydata name=\"anyd\"/>\n"
2924 " <anyxml name=\"anyx\"/>\n"
2925 " <case name=\"case\"/>\n"
2926 " <choice name=\"choice\"/>\n"
2927 " <container name=\"subcont\"/>\n"
2928 " <description><text>desc</text></description>\n"
2929 " <if-feature name=\"iff\"/>\n"
2930 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2931 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2932 " <list name=\"list\"/>\n"
2933 " <notification name=\"notif\"/>\n"
2934 " <reference><text>ref</text></reference>\n"
2935 " <status value=\"current\"/>\n"
2936 " <uses name=\"uses\"/>\n"
2937 " <when condition=\"when-cond\"/>\n"
2938 EXT_SUBELEM
2939 "</augment>"
2940 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002941 assert_int_equal(test_element_helper(state, data, &aug_meta, NULL, NULL), LY_SUCCESS);
David Sedlák992fb7c2019-07-24 16:51:01 +02002942 assert_string_equal(augments->nodeid, "target");
2943 assert_null(augments->parent);
2944 assert_int_equal(augments->nodetype, LYS_AUGMENT);
2945 assert_true(augments->flags & LYS_STATUS_CURR);
2946 assert_string_equal(augments->dsc, "desc");
2947 assert_string_equal(augments->ref, "ref");
2948 assert_string_equal(augments->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002949 assert_string_equal(augments->iffeatures[0].str, "iff");
David Sedlák992fb7c2019-07-24 16:51:01 +02002950 assert_string_equal(augments->child->name, "anyd");
2951 assert_int_equal(augments->child->nodetype, LYS_ANYDATA);
2952 assert_string_equal(augments->child->next->name, "anyx");
2953 assert_int_equal(augments->child->next->nodetype, LYS_ANYXML);
2954 assert_string_equal(augments->child->next->next->name, "case");
2955 assert_int_equal(augments->child->next->next->nodetype, LYS_CASE);
2956 assert_string_equal(augments->child->next->next->next->name, "choice");
2957 assert_int_equal(augments->child->next->next->next->nodetype, LYS_CHOICE);
2958 assert_string_equal(augments->child->next->next->next->next->name, "subcont");
2959 assert_int_equal(augments->child->next->next->next->next->nodetype, LYS_CONTAINER);
2960 assert_string_equal(augments->child->next->next->next->next->next->name, "leaf");
2961 assert_int_equal(augments->child->next->next->next->next->next->nodetype, LYS_LEAF);
2962 assert_string_equal(augments->child->next->next->next->next->next->next->name, "llist");
2963 assert_int_equal(augments->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2964 assert_string_equal(augments->child->next->next->next->next->next->next->next->name, "list");
2965 assert_int_equal(augments->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
2966 assert_string_equal(augments->child->next->next->next->next->next->next->next->next->name, "uses");
2967 assert_int_equal(augments->child->next->next->next->next->next->next->next->next->nodetype, LYS_USES);
2968 assert_null(augments->child->next->next->next->next->next->next->next->next->next);
2969 assert_string_equal(augments->actions->name, "action");
2970 assert_string_equal(augments->notifs->name, "notif");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002971 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(augments->exts[0]), LY_STMT_AUGMENT);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002972 lysp_node_free(&fctx, (struct lysp_node *)augments);
David Sedlák992fb7c2019-07-24 16:51:01 +02002973 augments = NULL;
2974
2975 data = ELEMENT_WRAPPER_START "<augment target-node=\"target\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002976 assert_int_equal(test_element_helper(state, data, &aug_meta, NULL, NULL), LY_SUCCESS);
David Sedlák992fb7c2019-07-24 16:51:01 +02002977 assert_string_equal(augments->nodeid, "target");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002978 lysp_node_free(&fctx, (struct lysp_node *)augments);
David Sedlák992fb7c2019-07-24 16:51:01 +02002979 augments = NULL;
David Sedlák992fb7c2019-07-24 16:51:01 +02002980}
2981
David Sedlák4ffcec82019-07-25 15:10:21 +02002982static void
2983test_deviate_elem(void **state)
2984{
David Sedlák4ffcec82019-07-25 15:10:21 +02002985 const char *data;
2986 struct lysp_deviate *deviates = NULL;
David Sedlák4ffcec82019-07-25 15:10:21 +02002987
2988 /* invalid arguments */
2989 data = ELEMENT_WRAPPER_START "<deviate value=\"\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002990 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
2991 CHECK_LOG_CTX("Invalid value \"\" of \"value\" attribute in \"deviate\" element. "
2992 "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", "Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02002993 deviates = NULL;
2994
2995 data = ELEMENT_WRAPPER_START "<deviate value=\"invalid\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002996 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
2997 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"deviate\" element. "
2998 "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", "Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02002999 deviates = NULL;
3000
3001 data = ELEMENT_WRAPPER_START "<deviate value=\"ad\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003002 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
3003 CHECK_LOG_CTX("Invalid value \"ad\" of \"value\" attribute in \"deviate\" element. "
3004 "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", "Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003005 deviates = NULL;
3006
3007 data = ELEMENT_WRAPPER_START "<deviate value=\"adds\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003008 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
3009 CHECK_LOG_CTX("Invalid value \"adds\" of \"value\" attribute in \"deviate\" element. "
3010 "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", "Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003011 deviates = NULL;
3012
3013 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003014 "<deviate value=\"not-supported\">\n"
3015 " <must condition=\"c\"/>\n"
3016 "</deviate>"
3017 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003018 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
3019 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 +02003020}
3021
David Sedlák8b754462019-07-25 16:22:13 +02003022static void
3023test_deviation_elem(void **state)
3024{
David Sedlák8b754462019-07-25 16:22:13 +02003025 const char *data;
3026 struct lysp_deviation *deviations = NULL;
3027
David Sedlák8b754462019-07-25 16:22:13 +02003028 /* invalid */
3029 data = ELEMENT_WRAPPER_START "<deviation target-node=\"target\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003030 assert_int_equal(test_element_helper(state, data, &deviations, NULL, NULL), LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +02003031 CHECK_LOG_CTX("Missing mandatory sub-element \"deviate\" of \"deviation\" element.", "Line number 1.");
Michal Vasko12ef5362022-09-16 15:13:58 +02003032}
David Sedlák8b754462019-07-25 16:22:13 +02003033
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003034static struct lysp_module *
Michal Vaskod0625d72022-10-06 15:02:50 +02003035mod_renew(struct lysp_yin_ctx *ctx)
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003036{
Michal Vasko8a67eff2021-12-07 14:04:47 +01003037 struct ly_ctx *ly_ctx = PARSER_CUR_PMOD(ctx)->mod->ctx;
3038 struct lysp_module *pmod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003039
Michal Vaskoc636ea42022-09-16 10:20:31 +02003040 lys_module_free(&fctx, PARSER_CUR_PMOD(ctx)->mod, 0);
Michal Vasko8a67eff2021-12-07 14:04:47 +01003041 pmod = calloc(1, sizeof *pmod);
3042 ctx->parsed_mods->objs[0] = pmod;
3043 pmod->mod = calloc(1, sizeof *pmod->mod);
3044 pmod->mod->parsed = pmod;
3045 pmod->mod->ctx = ly_ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003046
Michal Vaskoc636ea42022-09-16 10:20:31 +02003047 fctx.mod = pmod->mod;
3048
Michal Vasko8a67eff2021-12-07 14:04:47 +01003049 return pmod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003050}
3051
David Sedlák4f03b932019-07-26 13:01:47 +02003052static void
3053test_module_elem(void **state)
3054{
Michal Vaskob36053d2020-03-26 15:49:30 +01003055 const char *data;
Radek Iša56ca9e42020-09-08 18:42:00 +02003056 struct lysp_module *lysp_mod = mod_renew(YCTX);
David Sedlák4f03b932019-07-26 13:01:47 +02003057
3058 /* max subelems */
David Sedlák4f03b932019-07-26 13:01:47 +02003059 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003060 " <yang-version value=\"1.1\"/>\n"
3061 " <namespace uri=\"ns\"/>\n"
3062 " <prefix value=\"pref\"/>\n"
3063 " <include module=\"b-mod\"/>\n"
3064 " <import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
3065 " <organization><text>org</text></organization>\n"
3066 " <contact><text>contact</text></contact>\n"
3067 " <description><text>desc</text></description>\n"
3068 " <reference><text>ref</text></reference>\n"
3069 " <revision date=\"2019-02-02\"/>\n"
3070 " <anydata name=\"anyd\"/>\n"
3071 " <anyxml name=\"anyx\"/>\n"
3072 " <choice name=\"choice\"/>\n"
3073 " <container name=\"cont\"/>\n"
3074 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3075 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3076 " <list name=\"sub-list\"/>\n"
3077 " <uses name=\"uses-name\"/>\n"
3078 " <augment target-node=\"target\"/>\n"
3079 " <deviation target-node=\"target\">\n"
3080 " <deviate value=\"not-supported\"/>\n"
3081 " </deviation>\n"
3082 " <extension name=\"ext\"/>\n"
3083 " <feature name=\"feature\"/>\n"
3084 " <grouping name=\"grp\"/>\n"
3085 " <identity name=\"ident-name\"/>\n"
3086 " <notification name=\"notf\"/>\n"
3087 " <rpc name=\"rpc-name\"/>\n"
3088 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3089 EXT_SUBELEM "\n"
3090 "</module>\n";
Radek Iša56ca9e42020-09-08 18:42:00 +02003091 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3092 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01003093
Radek Iša56ca9e42020-09-08 18:42:00 +02003094 assert_int_equal(yin_parse_mod(YCTX, lysp_mod), LY_SUCCESS);
David Sedlák4f03b932019-07-26 13:01:47 +02003095 assert_string_equal(lysp_mod->mod->name, "mod");
3096 assert_string_equal(lysp_mod->revs, "2019-02-02");
3097 assert_string_equal(lysp_mod->mod->ns, "ns");
3098 assert_string_equal(lysp_mod->mod->prefix, "pref");
3099 assert_null(lysp_mod->mod->filepath);
3100 assert_string_equal(lysp_mod->mod->org, "org");
3101 assert_string_equal(lysp_mod->mod->contact, "contact");
3102 assert_string_equal(lysp_mod->mod->dsc, "desc");
3103 assert_string_equal(lysp_mod->mod->ref, "ref");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003104 assert_int_equal(lysp_mod->version, LYS_VERSION_1_1);
Radek Iša56ca9e42020-09-08 18:42:00 +02003105 CHECK_LYSP_IMPORT(lysp_mod->imports, NULL, 0, "a-mod",
3106 "imp-pref", NULL, "");
David Sedlák4f03b932019-07-26 13:01:47 +02003107 assert_string_equal(lysp_mod->includes->name, "b-mod");
3108 assert_string_equal(lysp_mod->extensions->name, "ext");
3109 assert_string_equal(lysp_mod->features->name, "feature");
3110 assert_string_equal(lysp_mod->identities->name, "ident-name");
3111 assert_string_equal(lysp_mod->typedefs->name, "tpdf");
3112 assert_string_equal(lysp_mod->groupings->name, "grp");
3113 assert_string_equal(lysp_mod->data->name, "anyd");
3114 assert_int_equal(lysp_mod->data->nodetype, LYS_ANYDATA);
3115 assert_string_equal(lysp_mod->data->next->name, "anyx");
3116 assert_int_equal(lysp_mod->data->next->nodetype, LYS_ANYXML);
3117 assert_string_equal(lysp_mod->data->next->next->name, "choice");
3118 assert_int_equal(lysp_mod->data->next->next->nodetype, LYS_CHOICE);
3119 assert_string_equal(lysp_mod->data->next->next->next->name, "cont");
3120 assert_int_equal(lysp_mod->data->next->next->next->nodetype, LYS_CONTAINER);
3121 assert_string_equal(lysp_mod->data->next->next->next->next->name, "leaf");
3122 assert_int_equal(lysp_mod->data->next->next->next->next->nodetype, LYS_LEAF);
3123 assert_string_equal(lysp_mod->data->next->next->next->next->next->name, "llist");
3124 assert_int_equal(lysp_mod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3125 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->name, "sub-list");
3126 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
3127 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->next->name, "uses-name");
3128 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3129 assert_null(lysp_mod->data->next->next->next->next->next->next->next->next);
3130 assert_string_equal(lysp_mod->augments->nodeid, "target");
3131 assert_string_equal(lysp_mod->rpcs->name, "rpc-name");
3132 assert_string_equal(lysp_mod->notifs->name, "notf");
3133 assert_string_equal(lysp_mod->deviations->nodeid, "target");
Radek Krejci39b7fc22021-02-26 23:29:18 +01003134 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(lysp_mod->exts[0]), LY_STMT_MODULE);
David Sedlák4f03b932019-07-26 13:01:47 +02003135
3136 /* min subelems */
Radek Iša56ca9e42020-09-08 18:42:00 +02003137 ly_in_free(UTEST_IN, 0);
3138 lyxml_ctx_free(YCTX->xmlctx);
3139 lysp_mod = mod_renew(YCTX);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003140 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
3141 " <namespace uri=\"ns\"/>\n"
3142 " <prefix value=\"pref\"/>\n"
3143 " <yang-version value=\"1.1\"/>\n"
3144 "</module>";
Radek Iša56ca9e42020-09-08 18:42:00 +02003145 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3146 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
3147 assert_int_equal(yin_parse_mod(YCTX, lysp_mod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003148 assert_string_equal(lysp_mod->mod->name, "mod");
David Sedlák298ff6d2019-07-26 14:29:03 +02003149
David Sedláke6cd89e2019-08-07 12:46:02 +02003150 /* incorrect subelem order */
Radek Iša56ca9e42020-09-08 18:42:00 +02003151 ly_in_free(UTEST_IN, 0);
3152 lyxml_ctx_free(YCTX->xmlctx);
3153 lysp_mod = mod_renew(YCTX);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003154 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
3155 " <feature name=\"feature\"/>\n"
3156 " <namespace uri=\"ns\"/>\n"
3157 " <prefix value=\"pref\"/>\n"
3158 " <yang-version value=\"1.1\"/>\n"
3159 "</module>";
Radek Iša56ca9e42020-09-08 18:42:00 +02003160 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3161 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
3162 assert_int_equal(yin_parse_mod(YCTX, lysp_mod), LY_EVALID);
3163 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 +02003164}
3165
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003166static struct lysp_submodule *
Michal Vaskod0625d72022-10-06 15:02:50 +02003167submod_renew(struct lysp_yin_ctx *ctx, const char *belongs_to)
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003168{
Michal Vasko8a67eff2021-12-07 14:04:47 +01003169 struct ly_ctx *ly_ctx = PARSER_CUR_PMOD(ctx)->mod->ctx;
3170 struct lysp_submodule *submod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003171
Michal Vaskoc636ea42022-09-16 10:20:31 +02003172 lys_module_free(&fctx, PARSER_CUR_PMOD(ctx)->mod, 0);
Michal Vasko8a67eff2021-12-07 14:04:47 +01003173 submod = calloc(1, sizeof *submod);
3174 ctx->parsed_mods->objs[0] = submod;
3175 submod->mod = calloc(1, sizeof *submod->mod);
3176 lydict_insert(ly_ctx, belongs_to, 0, &submod->mod->name);
3177 submod->mod->parsed = (struct lysp_module *)submod;
3178 submod->mod->ctx = ly_ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003179
Michal Vaskoc636ea42022-09-16 10:20:31 +02003180 fctx.mod = submod->mod;
3181
Michal Vasko8a67eff2021-12-07 14:04:47 +01003182 return submod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003183}
3184
David Sedlák298ff6d2019-07-26 14:29:03 +02003185static void
3186test_submodule_elem(void **state)
3187{
Michal Vaskob36053d2020-03-26 15:49:30 +01003188 const char *data;
Radek Iša56ca9e42020-09-08 18:42:00 +02003189 struct lysp_submodule *lysp_submod = submod_renew(YCTX, "module-name");
David Sedlák298ff6d2019-07-26 14:29:03 +02003190
3191 /* max subelements */
David Sedlák298ff6d2019-07-26 14:29:03 +02003192 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003193 " <yang-version value=\"1.1\"/>\n"
3194 " <belongs-to module=\"module-name\">\n"
3195 " <prefix value=\"pref\"/>\n"
3196 " </belongs-to>\n"
3197 " <include module=\"b-mod\"/>\n"
3198 " <import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
3199 " <organization><text>org</text></organization>\n"
3200 " <contact><text>contact</text></contact>\n"
3201 " <description><text>desc</text></description>\n"
3202 " <reference><text>ref</text></reference>\n"
3203 " <revision date=\"2019-02-02\"/>\n"
3204 " <anydata name=\"anyd\"/>\n"
3205 " <anyxml name=\"anyx\"/>\n"
3206 " <choice name=\"choice\"/>\n"
3207 " <container name=\"cont\"/>\n"
3208 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3209 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3210 " <list name=\"sub-list\"/>\n"
3211 " <uses name=\"uses-name\"/>\n"
3212 " <augment target-node=\"target\"/>\n"
3213 " <deviation target-node=\"target\">\n"
3214 " <deviate value=\"not-supported\"/>\n"
3215 " </deviation>\n"
3216 " <extension name=\"ext\"/>\n"
3217 " <feature name=\"feature\"/>\n"
3218 " <grouping name=\"grp\"/>\n"
3219 " <identity name=\"ident-name\"/>\n"
3220 " <notification name=\"notf\"/>\n"
3221 " <rpc name=\"rpc-name\"/>\n"
3222 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3223 EXT_SUBELEM "\n"
3224 "</submodule>\n";
Radek Iša56ca9e42020-09-08 18:42:00 +02003225 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3226 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003227
Radek Iša56ca9e42020-09-08 18:42:00 +02003228 assert_int_equal(yin_parse_submod(YCTX, lysp_submod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003229 assert_string_equal(lysp_submod->name, "mod");
3230 assert_string_equal(lysp_submod->revs, "2019-02-02");
David Sedlák298ff6d2019-07-26 14:29:03 +02003231 assert_string_equal(lysp_submod->prefix, "pref");
3232 assert_null(lysp_submod->filepath);
3233 assert_string_equal(lysp_submod->org, "org");
3234 assert_string_equal(lysp_submod->contact, "contact");
3235 assert_string_equal(lysp_submod->dsc, "desc");
3236 assert_string_equal(lysp_submod->ref, "ref");
3237 assert_int_equal(lysp_submod->version, LYS_VERSION_1_1);
Radek Iša56ca9e42020-09-08 18:42:00 +02003238 CHECK_LYSP_IMPORT(lysp_submod->imports, NULL, 0, "a-mod",
3239 "imp-pref", NULL, "");
David Sedlák298ff6d2019-07-26 14:29:03 +02003240 assert_string_equal(lysp_submod->includes->name, "b-mod");
3241 assert_string_equal(lysp_submod->extensions->name, "ext");
3242 assert_string_equal(lysp_submod->features->name, "feature");
3243 assert_string_equal(lysp_submod->identities->name, "ident-name");
3244 assert_string_equal(lysp_submod->typedefs->name, "tpdf");
3245 assert_string_equal(lysp_submod->groupings->name, "grp");
3246 assert_string_equal(lysp_submod->data->name, "anyd");
3247 assert_int_equal(lysp_submod->data->nodetype, LYS_ANYDATA);
3248 assert_string_equal(lysp_submod->data->next->name, "anyx");
3249 assert_int_equal(lysp_submod->data->next->nodetype, LYS_ANYXML);
3250 assert_string_equal(lysp_submod->data->next->next->name, "choice");
3251 assert_int_equal(lysp_submod->data->next->next->nodetype, LYS_CHOICE);
3252 assert_string_equal(lysp_submod->data->next->next->next->name, "cont");
3253 assert_int_equal(lysp_submod->data->next->next->next->nodetype, LYS_CONTAINER);
3254 assert_string_equal(lysp_submod->data->next->next->next->next->name, "leaf");
3255 assert_int_equal(lysp_submod->data->next->next->next->next->nodetype, LYS_LEAF);
3256 assert_string_equal(lysp_submod->data->next->next->next->next->next->name, "llist");
3257 assert_int_equal(lysp_submod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3258 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->name, "sub-list");
3259 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
3260 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->next->name, "uses-name");
3261 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3262 assert_null(lysp_submod->data->next->next->next->next->next->next->next->next);
3263 assert_string_equal(lysp_submod->augments->nodeid, "target");
3264 assert_string_equal(lysp_submod->rpcs->name, "rpc-name");
3265 assert_string_equal(lysp_submod->notifs->name, "notf");
3266 assert_string_equal(lysp_submod->deviations->nodeid, "target");
Radek Krejci39b7fc22021-02-26 23:29:18 +01003267 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(lysp_submod->exts[0]), LY_STMT_SUBMODULE);
David Sedlák298ff6d2019-07-26 14:29:03 +02003268
David Sedlák298ff6d2019-07-26 14:29:03 +02003269 /* min subelemnts */
Radek Iša56ca9e42020-09-08 18:42:00 +02003270 ly_in_free(UTEST_IN, 0);
3271 lyxml_ctx_free(YCTX->xmlctx);
3272 lysp_submod = submod_renew(YCTX, "module-name");
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003273 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">\n"
3274 " <yang-version value=\"1\"/>\n"
3275 " <belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>\n"
3276 "</submodule>";
Radek Iša56ca9e42020-09-08 18:42:00 +02003277 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3278 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
3279 assert_int_equal(yin_parse_submod(YCTX, lysp_submod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003280 assert_string_equal(lysp_submod->prefix, "pref");
David Sedlák298ff6d2019-07-26 14:29:03 +02003281 assert_int_equal(lysp_submod->version, LYS_VERSION_1_0);
David Sedlák298ff6d2019-07-26 14:29:03 +02003282
David Sedláke6cd89e2019-08-07 12:46:02 +02003283 /* incorrect subelem order */
Radek Iša56ca9e42020-09-08 18:42:00 +02003284 ly_in_free(UTEST_IN, 0);
3285 lyxml_ctx_free(YCTX->xmlctx);
3286 lysp_submod = submod_renew(YCTX, "module-name");
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003287 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">\n"
3288 " <yang-version value=\"1\"/>\n"
3289 " <reference><text>ref</text></reference>\n"
3290 " <belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>\n"
3291 "</submodule>";
Radek Iša56ca9e42020-09-08 18:42:00 +02003292 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3293 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
3294 assert_int_equal(yin_parse_submod(YCTX, lysp_submod), LY_EVALID);
3295 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 +02003296}
3297
David Sedlák8985a142019-07-31 16:43:06 +02003298static void
3299test_yin_parse_module(void **state)
3300{
David Sedlák8985a142019-07-31 16:43:06 +02003301 const char *data;
3302 struct lys_module *mod;
Michal Vaskod0625d72022-10-06 15:02:50 +02003303 struct lysp_yin_ctx *yin_ctx = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02003304 struct ly_in *in = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003305
3306 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003307 mod->ctx = UTEST_LYCTX;
David Sedlákd2844882019-09-13 16:01:22 +02003308 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 +01003309 " <yang-version value=\"1.1\"/>\n"
3310 " <namespace uri=\"urn:tests:extensions:metadata:a\"/>\n"
3311 " <prefix value=\"a\"/>\n"
3312 " <import module=\"ietf-yang-metadata\">\n"
3313 " <prefix value=\"md\"/>\n"
3314 " </import>\n"
3315 " <feature name=\"f\"/>\n"
3316 " <md:annotation name=\"x\">\n"
3317 " <description>\n"
3318 " <text>test</text>\n"
3319 " </description>\n"
3320 " <reference>\n"
3321 " <text>test</text>\n"
3322 " </reference>\n"
3323 " <if-feature name=\"f\"/>\n"
3324 " <status value=\"current\"/>\n"
3325 " <type name=\"uint8\"/>\n"
3326 " <units name=\"meters\"/>\n"
3327 " </md:annotation>\n"
3328 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003329 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
aPiecekc3e26142021-06-22 14:25:49 +02003330 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS);
David Sedlákd2844882019-09-13 16:01:22 +02003331 assert_null(mod->parsed->exts->child->next->child);
3332 assert_string_equal(mod->parsed->exts->child->next->arg, "test");
Michal Vaskoc636ea42022-09-16 10:20:31 +02003333 lys_module_free(&fctx, mod, 0);
Michal Vaskod0625d72022-10-06 15:02:50 +02003334 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003335 ly_in_free(in, 0);
David Sedlákd2844882019-09-13 16:01:22 +02003336 mod = NULL;
3337 yin_ctx = NULL;
3338
3339 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003340 mod->ctx = UTEST_LYCTX;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003341 data = "<module name=\"example-foo\""
3342 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
3343 " xmlns:foo=\"urn:example:foo\""
3344 " xmlns:myext=\"urn:example:extensions\">\n"
David Sedlák8985a142019-07-31 16:43:06 +02003345
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003346 " <yang-version value=\"1\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003347
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003348 " <namespace uri=\"urn:example:foo\"/>\n"
3349 " <prefix value=\"foo\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003350
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003351 " <import module=\"example-extensions\">\n"
3352 " <prefix value=\"myext\"/>\n"
3353 " </import>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003354
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003355 " <list name=\"interface\">\n"
3356 " <key value=\"name\"/>\n"
3357 " <leaf name=\"name\">\n"
3358 " <type name=\"string\"/>\n"
3359 " </leaf>\n"
3360 " <leaf name=\"mtu\">\n"
3361 " <type name=\"uint32\"/>\n"
3362 " <description>\n"
3363 " <text>The MTU of the interface.</text>\n"
3364 " </description>\n"
3365 " <myext:c-define name=\"MY_MTU\"/>\n"
3366 " </leaf>\n"
3367 " </list>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003368 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003369 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
aPiecekc3e26142021-06-22 14:25:49 +02003370 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS);
Michal Vaskoc636ea42022-09-16 10:20:31 +02003371 lys_module_free(&fctx, mod, 0);
Michal Vaskod0625d72022-10-06 15:02:50 +02003372 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003373 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02003374 mod = NULL;
3375 yin_ctx = NULL;
3376
3377 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003378 mod->ctx = UTEST_LYCTX;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003379 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3380 " <yang-version value=\"1\"/>\n"
3381 " <namespace uri=\"urn:example:foo\"/>\n"
3382 " <prefix value=\"foo\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003383 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003384 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
aPiecekc3e26142021-06-22 14:25:49 +02003385 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS);
Michal Vaskoc636ea42022-09-16 10:20:31 +02003386 lys_module_free(&fctx, mod, 0);
Michal Vaskod0625d72022-10-06 15:02:50 +02003387 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003388 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02003389 mod = NULL;
3390 yin_ctx = NULL;
3391
David Sedlák6d781b62019-08-02 15:22:52 +02003392 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003393 mod->ctx = UTEST_LYCTX;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003394 data = "<submodule name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlák8985a142019-07-31 16:43:06 +02003395 "</submodule>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003396 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
aPiecekc3e26142021-06-22 14:25:49 +02003397 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_EINVAL);
Radek Iša56ca9e42020-09-08 18:42:00 +02003398 CHECK_LOG_CTX("Input data contains submodule which cannot be parsed directly without its main module.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02003399 lys_module_free(&fctx, mod, 0);
Michal Vaskod0625d72022-10-06 15:02:50 +02003400 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003401 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02003402
David Sedlák6d781b62019-08-02 15:22:52 +02003403 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003404 mod->ctx = UTEST_LYCTX;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003405 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3406 " <yang-version value=\"1\"/>\n"
3407 " <namespace uri=\"urn:example:foo\"/>\n"
3408 " <prefix value=\"foo\"/>\n"
3409 "</module>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003410 "<module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003411 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
aPiecekc3e26142021-06-22 14:25:49 +02003412 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +02003413 CHECK_LOG_CTX("Trailing garbage \"<module>\" after module, expected end-of-input.", "Line number 6.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02003414 lys_module_free(&fctx, mod, 0);
Michal Vaskod0625d72022-10-06 15:02:50 +02003415 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003416 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02003417 mod = NULL;
3418 yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003419}
3420
3421static void
3422test_yin_parse_submodule(void **state)
3423{
David Sedlák8985a142019-07-31 16:43:06 +02003424 const char *data;
Michal Vaskod0625d72022-10-06 15:02:50 +02003425 struct lysp_yin_ctx *yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003426 struct lysp_submodule *submod = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02003427 struct ly_in *in;
David Sedlák8985a142019-07-31 16:43:06 +02003428
Michal Vasko8a67eff2021-12-07 14:04:47 +01003429 lydict_insert(UTEST_LYCTX, "a", 0, &PARSER_CUR_PMOD(YCTX)->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02003430
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003431 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003432 "<submodule name=\"asub\""
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003433 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
3434 " xmlns:a=\"urn:a\">\n"
3435 " <yang-version value=\"1\"/>\n"
3436 " <belongs-to module=\"a\">\n"
3437 " <prefix value=\"a_pref\"/>\n"
3438 " </belongs-to>\n"
3439 " <include module=\"atop\"/>\n"
3440 " <feature name=\"fox\"/>\n"
3441 " <notification name=\"bar-notif\">\n"
3442 " <if-feature name=\"bar\"/>\n"
3443 " </notification>\n"
3444 " <notification name=\"fox-notif\">\n"
3445 " <if-feature name=\"fox\"/>\n"
3446 " </notification>\n"
3447 " <augment target-node=\"/a_pref:top\">\n"
3448 " <if-feature name=\"bar\"/>\n"
3449 " <container name=\"bar-sub\"/>\n"
3450 " </augment>\n"
3451 " <augment target-node=\"/top\">\n"
3452 " <container name=\"bar-sub2\"/>\n"
3453 " </augment>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003454 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003455 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vaskod0625d72022-10-06 15:02:50 +02003456 assert_int_equal(yin_parse_submodule(&yin_ctx, UTEST_LYCTX, (struct lysp_ctx *)YCTX, in, &submod), LY_SUCCESS);
Michal Vaskoc636ea42022-09-16 10:20:31 +02003457 lysp_module_free(&fctx, (struct lysp_module *)submod);
Michal Vaskod0625d72022-10-06 15:02:50 +02003458 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003459 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02003460 yin_ctx = NULL;
3461 submod = NULL;
3462
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003463 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3464 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3465 " <yang-version value=\"1\"/>\n"
3466 " <belongs-to module=\"a\">\n"
3467 " <prefix value=\"a_pref\"/>\n"
3468 " </belongs-to>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003469 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003470 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vaskod0625d72022-10-06 15:02:50 +02003471 assert_int_equal(yin_parse_submodule(&yin_ctx, UTEST_LYCTX, (struct lysp_ctx *)YCTX, in, &submod), LY_SUCCESS);
Michal Vaskoc636ea42022-09-16 10:20:31 +02003472 lysp_module_free(&fctx, (struct lysp_module *)submod);
Michal Vaskod0625d72022-10-06 15:02:50 +02003473 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003474 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02003475 yin_ctx = NULL;
3476 submod = NULL;
3477
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003478 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003479 "<module name=\"inval\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
3480 "</module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003481 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vaskod0625d72022-10-06 15:02:50 +02003482 assert_int_equal(yin_parse_submodule(&yin_ctx, UTEST_LYCTX, (struct lysp_ctx *)YCTX, in, &submod), LY_EINVAL);
Radek Iša56ca9e42020-09-08 18:42:00 +02003483 CHECK_LOG_CTX("Input data contains module in situation when a submodule is expected.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02003484 lysp_module_free(&fctx, (struct lysp_module *)submod);
Michal Vaskod0625d72022-10-06 15:02:50 +02003485 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003486 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02003487 yin_ctx = NULL;
3488 submod = NULL;
3489
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003490 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3491 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3492 " <yang-version value=\"1\"/>\n"
3493 " <belongs-to module=\"a\">\n"
3494 " <prefix value=\"a_pref\"/>\n"
3495 " </belongs-to>\n"
3496 "</submodule>\n"
3497 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3498 " <yang-version value=\"1\"/>\n"
3499 " <belongs-to module=\"a\">\n"
3500 " <prefix value=\"a_pref\"/>\n"
3501 " </belongs-to>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003502 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003503 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vaskod0625d72022-10-06 15:02:50 +02003504 assert_int_equal(yin_parse_submodule(&yin_ctx, UTEST_LYCTX, (struct lysp_ctx *)YCTX, in, &submod), LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +02003505 CHECK_LOG_CTX("Trailing garbage \"<submodule name...\" after submodule, expected end-of-input.", "Line number 8.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02003506 lysp_module_free(&fctx, (struct lysp_module *)submod);
Michal Vaskod0625d72022-10-06 15:02:50 +02003507 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003508 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02003509 yin_ctx = NULL;
3510 submod = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003511}
3512
David Sedlák3b4db242018-10-19 16:11:01 +02003513int
3514main(void)
3515{
3516
3517 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +02003518 UTEST(test_yin_match_keyword, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003519 UTEST(test_yin_parse_content, setup, teardown),
3520 UTEST(test_validate_value, setup, teardown),
Michal Vasko12ef5362022-09-16 15:13:58 +02003521 UTEST(test_valid_module),
3522 UTEST(test_print_module),
3523 UTEST(test_print_submodule),
David Sedlák32488102019-07-15 17:44:10 +02003524
Radek Iša56ca9e42020-09-08 18:42:00 +02003525 UTEST(test_yin_match_argument_name),
3526 cmocka_unit_test_setup_teardown(test_enum_elem, setup, teardown),
3527 cmocka_unit_test_setup_teardown(test_bit_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003528 cmocka_unit_test_setup_teardown(test_status_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003529 cmocka_unit_test_setup_teardown(test_yin_element_elem, setup, teardown),
3530 cmocka_unit_test_setup_teardown(test_yangversion_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003531 cmocka_unit_test_setup_teardown(test_argument_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003532 cmocka_unit_test_setup_teardown(test_belongsto_elem, setup, teardown),
3533 cmocka_unit_test_setup_teardown(test_config_elem, setup, teardown),
3534 cmocka_unit_test_setup_teardown(test_default_elem, setup, teardown),
3535 cmocka_unit_test_setup_teardown(test_err_app_tag_elem, setup, teardown),
3536 cmocka_unit_test_setup_teardown(test_err_msg_elem, setup, teardown),
3537 cmocka_unit_test_setup_teardown(test_fracdigits_elem, setup, teardown),
3538 cmocka_unit_test_setup_teardown(test_iffeature_elem, setup, teardown),
3539 cmocka_unit_test_setup_teardown(test_length_elem, setup, teardown),
3540 cmocka_unit_test_setup_teardown(test_modifier_elem, setup, teardown),
3541 cmocka_unit_test_setup_teardown(test_namespace_elem, setup, teardown),
3542 cmocka_unit_test_setup_teardown(test_pattern_elem, setup, teardown),
3543 cmocka_unit_test_setup_teardown(test_value_position_elem, setup, teardown),
3544 cmocka_unit_test_setup_teardown(test_prefix_elem, setup, teardown),
3545 cmocka_unit_test_setup_teardown(test_range_elem, setup, teardown),
3546 cmocka_unit_test_setup_teardown(test_reqinstance_elem, setup, teardown),
3547 cmocka_unit_test_setup_teardown(test_revision_date_elem, setup, teardown),
3548 cmocka_unit_test_setup_teardown(test_unique_elem, setup, teardown),
3549 cmocka_unit_test_setup_teardown(test_units_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003550 cmocka_unit_test_setup_teardown(test_yin_text_value_elem, setup, teardown),
3551 cmocka_unit_test_setup_teardown(test_type_elem, setup, teardown),
3552 cmocka_unit_test_setup_teardown(test_max_elems_elem, setup, teardown),
3553 cmocka_unit_test_setup_teardown(test_min_elems_elem, setup, teardown),
3554 cmocka_unit_test_setup_teardown(test_ordby_elem, setup, teardown),
3555 cmocka_unit_test_setup_teardown(test_any_elem, setup, teardown),
3556 cmocka_unit_test_setup_teardown(test_leaf_elem, setup, teardown),
3557 cmocka_unit_test_setup_teardown(test_leaf_list_elem, setup, teardown),
3558 cmocka_unit_test_setup_teardown(test_presence_elem, setup, teardown),
3559 cmocka_unit_test_setup_teardown(test_key_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003560 cmocka_unit_test_setup_teardown(test_uses_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003561 cmocka_unit_test_setup_teardown(test_list_elem, setup, teardown),
3562 cmocka_unit_test_setup_teardown(test_notification_elem, setup, teardown),
3563 cmocka_unit_test_setup_teardown(test_grouping_elem, setup, teardown),
3564 cmocka_unit_test_setup_teardown(test_container_elem, setup, teardown),
3565 cmocka_unit_test_setup_teardown(test_case_elem, setup, teardown),
3566 cmocka_unit_test_setup_teardown(test_choice_elem, setup, teardown),
3567 cmocka_unit_test_setup_teardown(test_inout_elem, setup, teardown),
3568 cmocka_unit_test_setup_teardown(test_action_elem, setup, teardown),
3569 cmocka_unit_test_setup_teardown(test_augment_elem, setup, teardown),
3570 cmocka_unit_test_setup_teardown(test_deviate_elem, setup, teardown),
3571 cmocka_unit_test_setup_teardown(test_deviation_elem, setup, teardown),
3572 cmocka_unit_test_setup_teardown(test_module_elem, setup, teardown),
3573 cmocka_unit_test_setup_teardown(test_submodule_elem, setup, teardown),
David Sedlák8985a142019-07-31 16:43:06 +02003574
Radek Iša56ca9e42020-09-08 18:42:00 +02003575 cmocka_unit_test_setup_teardown(test_yin_parse_module, setup, teardown),
3576 cmocka_unit_test_setup_teardown(test_yin_parse_submodule, setup, teardown),
David Sedlák3b4db242018-10-19 16:11:01 +02003577 };
3578
Radek Iša56ca9e42020-09-08 18:42:00 +02003579 return cmocka_run_group_tests(tests, NULL, NULL);
David Sedlák3b4db242018-10-19 16:11:01 +02003580}