blob: af8de4de1de2b7826d852be12e5e23fa35970456 [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 Vasko193dacd2022-10-13 08:43:05 +020095 const void *parent, enum ly_stmt parent_stmt, 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 Vasko193dacd2022-10-13 08:43:05 +020099LY_ERR yin_parse_extension_instance(struct lysp_yin_ctx *ctx, const void *parent, enum ly_stmt parent_stmt,
100 LY_ARRAY_COUNT_TYPE parent_stmt_index, 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
Michal Vasko193dacd2022-10-13 08:43:05 +0200333 ret = yin_parse_content(YCTX, subelems2, 2, NULL, 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
Michal Vasko193dacd2022-10-13 08:43:05 +0200353 ret = yin_parse_content(YCTX, subelems3, 2, NULL, 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
Michal Vasko193dacd2022-10-13 08:43:05 +0200367 ret = yin_parse_content(YCTX, subelems4, 1, NULL, 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
Michal Vasko193dacd2022-10-13 08:43:05 +02001261 ret = yin_parse_content(YCTX, subelems, 71, NULL,
1262 yin_match_keyword(YCTX, name, name_len, prefix, prefix_len, LY_STMT_NONE), text, exts);
Michal Vaskob36053d2020-03-26 15:49:30 +01001263
Michal Vasko63f3d842020-07-08 10:10:14 +02001264 /* free parser and input */
Radek Iša56ca9e42020-09-08 18:42:00 +02001265 lyxml_ctx_free(YCTX->xmlctx);
1266 YCTX->xmlctx = NULL;
1267 ly_in_free(UTEST_IN, 0);
1268 UTEST_IN = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001269 return ret;
1270}
1271
David Sedlákd1144562019-08-06 12:36:14 +02001272#define EXT_SUBELEM "<myext:c-define name=\"MY_MTU\" xmlns:myext=\"urn:example:extensions\"/>"
1273
David Sedlák32488102019-07-15 17:44:10 +02001274static void
David Sedlák43801c92019-08-05 15:58:54 +02001275test_enum_elem(void **state)
David Sedlák32488102019-07-15 17:44:10 +02001276{
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001277 struct lysp_type type = {0};
David Sedlák32488102019-07-15 17:44:10 +02001278 const char *data;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001279
David Sedlák32488102019-07-15 17:44:10 +02001280 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001281 "<enum name=\"enum-name\">\n"
1282 " <if-feature name=\"feature\" />\n"
1283 " <value value=\"55\" />\n"
1284 " <status value=\"deprecated\" />\n"
1285 " <description><text>desc...</text></description>\n"
1286 " <reference><text>ref...</text></reference>\n"
1287 " " EXT_SUBELEM "\n"
1288 "</enum>"
1289 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001290 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1291 uint16_t flags = LYS_STATUS_DEPRC | LYS_SET_VALUE;
1292
1293 CHECK_LYSP_TYPE_ENUM(type.enums, "desc...", 1, flags, 1, "enum-name", "ref...", 55);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001294 assert_string_equal(type.enums->iffeatures[0].str, "feature");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001295 TEST_1_CHECK_LYSP_EXT_INSTANCE(type.enums->exts, LY_STMT_ENUM);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001296 lysp_type_free(&fctx, &type);
David Sedlákd1144562019-08-06 12:36:14 +02001297 memset(&type, 0, sizeof type);
1298
1299 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001300 "<enum name=\"enum-name\"></enum>"
1301 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001302 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001303 assert_string_equal(type.enums->name, "enum-name");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001304 lysp_type_free(&fctx, &type);
David Sedlák32488102019-07-15 17:44:10 +02001305 memset(&type, 0, sizeof type);
David Sedlák43801c92019-08-05 15:58:54 +02001306}
1307
1308static void
1309test_bit_elem(void **state)
1310{
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001311 struct lysp_type type = {0};
David Sedlák43801c92019-08-05 15:58:54 +02001312 const char *data;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001313
David Sedlák43801c92019-08-05 15:58:54 +02001314 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001315 "<bit name=\"bit-name\">\n"
1316 " <if-feature name=\"feature\" />\n"
1317 " <position value=\"55\" />\n"
1318 " <status value=\"deprecated\" />\n"
1319 " <description><text>desc...</text></description>\n"
1320 " <reference><text>ref...</text></reference>\n"
1321 EXT_SUBELEM
1322 "</bit>"
1323 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001324 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1325 uint16_t flags = LYS_STATUS_DEPRC | LYS_SET_VALUE;
1326
1327 CHECK_LYSP_TYPE_ENUM(type.bits, "desc...", 1, flags, 1, "bit-name", "ref...", 55);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001328 assert_string_equal(type.bits->iffeatures[0].str, "feature");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001329 TEST_1_CHECK_LYSP_EXT_INSTANCE(type.bits->exts, LY_STMT_BIT);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001330 lysp_type_free(&fctx, &type);
David Sedlákd1144562019-08-06 12:36:14 +02001331 memset(&type, 0, sizeof type);
1332
1333 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001334 "<bit name=\"bit-name\"> </bit>"
1335 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001336 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1337 CHECK_LYSP_TYPE_ENUM(type.bits, NULL, 0, 0, 0, "bit-name", NULL, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001338 lysp_type_free(&fctx, &type);
David Sedlák43801c92019-08-05 15:58:54 +02001339 memset(&type, 0, sizeof type);
David Sedlák32488102019-07-15 17:44:10 +02001340}
1341
1342static void
David Sedlák32488102019-07-15 17:44:10 +02001343test_status_elem(void **state)
1344{
David Sedlák32488102019-07-15 17:44:10 +02001345 const char *data;
1346 uint16_t flags = 0;
David Sedlák32488102019-07-15 17:44:10 +02001347
1348 /* test invalid value */
1349 data = ELEMENT_WRAPPER_START "<status value=\"invalid\"></status>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001350 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID);
1351 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"status\" element. "
1352 "Valid values are \"current\", \"deprecated\" and \"obsolete\".", "Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001353}
1354
1355static void
David Sedlák32488102019-07-15 17:44:10 +02001356test_yin_element_elem(void **state)
1357{
David Sedlák32488102019-07-15 17:44:10 +02001358 const char *data;
1359 uint16_t flags = 0;
David Sedlák32488102019-07-15 17:44:10 +02001360
1361 data = ELEMENT_WRAPPER_START "<yin-element value=\"invalid\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001362 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +02001363 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"yin-element\" element. "
1364 "Valid values are \"true\" and \"false\".", "Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001365}
1366
1367static void
1368test_yangversion_elem(void **state)
1369{
David Sedlák32488102019-07-15 17:44:10 +02001370 const char *data;
1371 uint8_t version = 0;
David Sedlák32488102019-07-15 17:44:10 +02001372
1373 /* invalid value */
1374 data = ELEMENT_WRAPPER_START "<yang-version value=\"version\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001375 assert_int_equal(test_element_helper(state, data, &version, NULL, NULL), LY_EVALID);
1376 CHECK_LOG_CTX("Invalid value \"version\" of \"value\" attribute in \"yang-version\" element. "
1377 "Valid values are \"1\" and \"1.1\".", "Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001378}
1379
1380static void
David Sedlák8e7bda82019-07-16 17:57:50 +02001381test_argument_elem(void **state)
1382{
David Sedlák8e7bda82019-07-16 17:57:50 +02001383 const char *data;
1384 uint16_t flags = 0;
1385 const char *arg;
1386 struct yin_argument_meta arg_meta = {&flags, &arg};
David Sedlák8e7bda82019-07-16 17:57:50 +02001387
1388 /* min subelems */
1389 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001390 "<argument name=\"arg\">"
1391 "</argument>"
1392 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001393 assert_int_equal(test_element_helper(state, data, &arg_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001394 assert_string_equal(arg, "arg");
1395 assert_true(flags == 0);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001396 lydict_remove(UTEST_LYCTX, arg);
David Sedlák8e7bda82019-07-16 17:57:50 +02001397}
1398
1399static void
David Sedlák8e7bda82019-07-16 17:57:50 +02001400test_belongsto_elem(void **state)
1401{
David Sedlák8e7bda82019-07-16 17:57:50 +02001402 const char *data;
1403 struct lysp_submodule submod;
1404
Michal Vasko8a67eff2021-12-07 14:04:47 +01001405 lydict_insert(UTEST_LYCTX, "module-name", 0, &PARSER_CUR_PMOD(YCTX)->mod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +02001406
David Sedlák8e7bda82019-07-16 17:57:50 +02001407 data = ELEMENT_WRAPPER_START "<belongs-to module=\"module-name\"></belongs-to>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001408 assert_int_equal(test_element_helper(state, data, &submod, NULL, NULL), LY_EVALID);
1409 CHECK_LOG_CTX("Missing mandatory sub-element \"prefix\" of \"belongs-to\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001410}
1411
1412static void
1413test_config_elem(void **state)
1414{
David Sedlák8e7bda82019-07-16 17:57:50 +02001415 const char *data;
1416 uint16_t flags = 0;
David Sedlák8e7bda82019-07-16 17:57:50 +02001417
1418 data = ELEMENT_WRAPPER_START "<config value=\"false\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001419 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001420 assert_true(flags & LYS_CONFIG_R);
David Sedlák8e7bda82019-07-16 17:57:50 +02001421 flags = 0;
1422
1423 data = ELEMENT_WRAPPER_START "<config value=\"invalid\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001424 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID);
1425 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"config\" element. "
1426 "Valid values are \"true\" and \"false\".", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001427}
1428
1429static void
1430test_default_elem(void **state)
1431{
David Sedlák8e7bda82019-07-16 17:57:50 +02001432 const char *data;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001433 struct lysp_qname val = {0};
David Sedlák8e7bda82019-07-16 17:57:50 +02001434
1435 data = ELEMENT_WRAPPER_START "<default/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001436 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
1437 CHECK_LOG_CTX("Missing mandatory attribute value of default element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001438}
1439
1440static void
1441test_err_app_tag_elem(void **state)
1442{
David Sedlák8e7bda82019-07-16 17:57:50 +02001443 const char *data;
1444 const char *val = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001445
1446 data = ELEMENT_WRAPPER_START "<error-app-tag/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001447 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
1448 CHECK_LOG_CTX("Missing mandatory attribute value of error-app-tag element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001449}
1450
1451static void
1452test_err_msg_elem(void **state)
1453{
David Sedlák8e7bda82019-07-16 17:57:50 +02001454 const char *data;
1455 const char *val = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001456
1457 data = ELEMENT_WRAPPER_START "<error-message></error-message>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001458 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
1459 CHECK_LOG_CTX("Missing mandatory sub-element \"value\" of \"error-message\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001460
David Sedlákdf2a9732019-08-07 13:23:16 +02001461 data = ELEMENT_WRAPPER_START "<error-message invalid=\"text\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001462 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
1463 CHECK_LOG_CTX("Unexpected attribute \"invalid\" of \"error-message\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001464}
1465
1466static void
1467test_fracdigits_elem(void **state)
1468{
David Sedlák8e7bda82019-07-16 17:57:50 +02001469 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001470 struct lysp_type type = {0};
David Sedlák8e7bda82019-07-16 17:57:50 +02001471
David Sedlák8e7bda82019-07-16 17:57:50 +02001472 /* invalid values */
1473 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"-1\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001474 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1475 CHECK_LOG_CTX("Invalid value \"-1\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001476
1477 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"02\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001478 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1479 CHECK_LOG_CTX("Invalid value \"02\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001480
1481 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"1p\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001482 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1483 CHECK_LOG_CTX("Invalid value \"1p\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001484
1485 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"19\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001486 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1487 CHECK_LOG_CTX("Invalid value \"19\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001488
1489 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"999999999999999999\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001490 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1491 CHECK_LOG_CTX("Invalid value \"999999999999999999\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001492}
1493
1494static void
1495test_iffeature_elem(void **state)
1496{
David Sedlák8e7bda82019-07-16 17:57:50 +02001497 const char *data;
1498 const char **iffeatures = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001499
1500 data = ELEMENT_WRAPPER_START "<if-feature/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001501 assert_int_equal(test_element_helper(state, data, &iffeatures, NULL, NULL), LY_EVALID);
1502 CHECK_LOG_CTX("Missing mandatory attribute name of if-feature element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001503 LY_ARRAY_FREE(iffeatures);
1504 iffeatures = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001505}
1506
1507static void
1508test_length_elem(void **state)
1509{
David Sedlák8e7bda82019-07-16 17:57:50 +02001510 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001511 struct lysp_type type = {0};
David Sedlák8e7bda82019-07-16 17:57:50 +02001512
1513 /* max subelems */
1514 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001515 "<length value=\"length-str\">\n"
1516 " <error-message><value>err-msg</value></error-message>\n"
1517 " <error-app-tag value=\"err-app-tag\"/>\n"
1518 " <description><text>desc</text></description>\n"
1519 " <reference><text>ref</text></reference>\n"
1520 EXT_SUBELEM
1521 "</length>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001522 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001523 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1524 CHECK_LYSP_RESTR(type.length, "length-str", "desc",
1525 "err-app-tag", "err-msg", 1, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001526 assert_true(type.flags & LYS_SET_LENGTH);
Radek Krejci39b7fc22021-02-26 23:29:18 +01001527 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.length->exts[0]), LY_STMT_LENGTH);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001528 lysp_type_free(&fctx, &type);
David Sedlák8e7bda82019-07-16 17:57:50 +02001529 memset(&type, 0, sizeof(type));
1530
1531 /* min subelems */
1532 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001533 "<length value=\"length-str\">"
1534 "</length>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001535 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001536 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1537 CHECK_LYSP_RESTR(type.length, "length-str", NULL,
1538 NULL, NULL, 0, NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001539 lysp_type_free(&fctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001540 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001541 memset(&type, 0, sizeof(type));
1542
1543 data = ELEMENT_WRAPPER_START "<length></length>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001544 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1545 CHECK_LOG_CTX("Missing mandatory attribute value of length element.", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001546 lysp_type_free(&fctx, &type);
David Sedlák8e7bda82019-07-16 17:57:50 +02001547 memset(&type, 0, sizeof(type));
David Sedlák8e7bda82019-07-16 17:57:50 +02001548}
1549
1550static void
1551test_modifier_elem(void **state)
1552{
David Sedlák8e7bda82019-07-16 17:57:50 +02001553 const char *data;
Radek Krejci011e4aa2020-09-04 15:22:31 +02001554 const char *pat;
David Sedlák8e7bda82019-07-16 17:57:50 +02001555
Radek Iša56ca9e42020-09-08 18:42:00 +02001556 assert_int_equal(LY_SUCCESS, lydict_insert(UTEST_LYCTX, "\006pattern", 8, &pat));
David Sedlák8e7bda82019-07-16 17:57:50 +02001557 data = ELEMENT_WRAPPER_START "<modifier value=\"invert\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001558 assert_int_equal(test_element_helper(state, data, &pat, NULL, NULL), LY_EVALID);
1559 CHECK_LOG_CTX("Invalid value \"invert\" of \"value\" attribute in \"modifier\" element. "
1560 "Only valid value is \"invert-match\".", "Line number 1.");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001561 lydict_remove(UTEST_LYCTX, pat);
David Sedlák8e7bda82019-07-16 17:57:50 +02001562}
1563
1564static void
1565test_namespace_elem(void **state)
1566{
David Sedlák8e7bda82019-07-16 17:57:50 +02001567 const char *data;
1568 const char *ns;
David Sedlák8e7bda82019-07-16 17:57:50 +02001569
1570 data = ELEMENT_WRAPPER_START "<namespace/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001571 assert_int_equal(test_element_helper(state, data, &ns, NULL, NULL), LY_EVALID);
1572 CHECK_LOG_CTX("Missing mandatory attribute uri of namespace element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001573}
1574
1575static void
David Sedlák8e7bda82019-07-16 17:57:50 +02001576test_pattern_elem(void **state)
1577{
David Sedlák8e7bda82019-07-16 17:57:50 +02001578 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001579 struct lysp_type type = {0};
David Sedlák8e7bda82019-07-16 17:57:50 +02001580
1581 /* max subelems */
1582 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001583 "<pattern value=\"super_pattern\">\n"
1584 " <modifier value=\"invert-match\"/>\n"
1585 " <error-message><value>err-msg-value</value></error-message>\n"
1586 " <error-app-tag value=\"err-app-tag-value\"/>\n"
1587 " <description><text>&quot;pattern-desc&quot;</text></description>\n"
1588 " <reference><text>pattern-ref</text></reference>\n"
1589 EXT_SUBELEM
1590 "</pattern>"
1591 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001592 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001593 assert_true(type.flags & LYS_SET_PATTERN);
Radek Iša56ca9e42020-09-08 18:42:00 +02001594 CHECK_LYSP_RESTR(type.patterns, "\x015super_pattern", "\"pattern-desc\"",
1595 "err-app-tag-value", "err-msg-value", 1, "pattern-ref");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001596 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.patterns->exts[0]), LY_STMT_PATTERN);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001597 lysp_type_free(&fctx, &type);
David Sedlák8e7bda82019-07-16 17:57:50 +02001598 memset(&type, 0, sizeof(type));
1599
1600 /* min subelems */
1601 data = ELEMENT_WRAPPER_START "<pattern value=\"pattern\"> </pattern>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001602 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1603 CHECK_LYSP_RESTR(type.patterns, "\x006pattern", NULL, NULL, NULL, 0, NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001604 lysp_type_free(&fctx, &type);
David Sedlák8e7bda82019-07-16 17:57:50 +02001605 memset(&type, 0, sizeof(type));
David Sedlák8e7bda82019-07-16 17:57:50 +02001606}
1607
1608static void
1609test_value_position_elem(void **state)
1610{
David Sedlák8e7bda82019-07-16 17:57:50 +02001611 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001612 struct lysp_type_enum en = {0};
David Sedlák8e7bda82019-07-16 17:57:50 +02001613
1614 /* valid values */
David Sedlák8e7bda82019-07-16 17:57:50 +02001615 data = ELEMENT_WRAPPER_START "<value value=\"-55\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001616 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1617 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, -55);
David Sedlák8e7bda82019-07-16 17:57:50 +02001618 memset(&en, 0, sizeof(en));
1619
1620 data = ELEMENT_WRAPPER_START "<value value=\"0\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001621 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1622 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, 0);
David Sedlák8e7bda82019-07-16 17:57:50 +02001623 memset(&en, 0, sizeof(en));
1624
1625 data = ELEMENT_WRAPPER_START "<value value=\"-0\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001626 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1627 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, 0);
David Sedlák8e7bda82019-07-16 17:57:50 +02001628 memset(&en, 0, sizeof(en));
1629
1630 /* valid positions */
David Sedlák8e7bda82019-07-16 17:57:50 +02001631 data = ELEMENT_WRAPPER_START "<position value=\"0\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001632 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1633 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, 0);
David Sedlák8e7bda82019-07-16 17:57:50 +02001634 memset(&en, 0, sizeof(en));
1635
1636 /* invalid values */
1637 data = ELEMENT_WRAPPER_START "<value value=\"99999999999999999999999\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001638 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1639 CHECK_LOG_CTX("Invalid value \"99999999999999999999999\" of \"value\" attribute in \"value\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001640
1641 data = ELEMENT_WRAPPER_START "<value value=\"1k\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001642 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1643 CHECK_LOG_CTX("Invalid value \"1k\" of \"value\" attribute in \"value\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001644
David Sedlák69f01612019-07-17 11:41:08 +02001645 data = ELEMENT_WRAPPER_START "<value value=\"\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001646 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1647 CHECK_LOG_CTX("Invalid value \"\" of \"value\" attribute in \"value\" element.", "Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001648
David Sedlák8e7bda82019-07-16 17:57:50 +02001649 /*invalid positions */
1650 data = ELEMENT_WRAPPER_START "<position value=\"-5\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001651 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1652 CHECK_LOG_CTX("Invalid value \"-5\" of \"value\" attribute in \"position\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001653
1654 data = ELEMENT_WRAPPER_START "<position value=\"-0\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001655 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1656 CHECK_LOG_CTX("Invalid value \"-0\" of \"value\" attribute in \"position\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001657
1658 data = ELEMENT_WRAPPER_START "<position value=\"99999999999999999999\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001659 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1660 CHECK_LOG_CTX("Invalid value \"99999999999999999999\" of \"value\" attribute in \"position\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001661
David Sedlák69f01612019-07-17 11:41:08 +02001662 data = ELEMENT_WRAPPER_START "<position value=\"\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001663 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1664 CHECK_LOG_CTX("Invalid value \"\" of \"value\" attribute in \"position\" element.", "Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001665}
1666
1667static void
1668test_prefix_elem(void **state)
1669{
David Sedlák69f01612019-07-17 11:41:08 +02001670 const char *data;
1671 const char *value = NULL;
1672
1673 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001674 assert_int_equal(test_element_helper(state, data, &value, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001675 assert_string_equal(value, "pref");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001676 lydict_remove(UTEST_LYCTX, value);
David Sedlák69f01612019-07-17 11:41:08 +02001677}
1678
1679static void
1680test_range_elem(void **state)
1681{
David Sedlák69f01612019-07-17 11:41:08 +02001682 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001683 struct lysp_type type = {0};
David Sedlák69f01612019-07-17 11:41:08 +02001684
1685 /* max subelems */
1686 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001687 "<range value=\"range-str\">\n"
1688 " <error-message><value>err-msg</value></error-message>\n"
1689 " <error-app-tag value=\"err-app-tag\" />\n"
1690 " <description><text>desc</text></description>\n"
1691 " <reference><text>ref</text></reference>\n"
1692 EXT_SUBELEM
1693 "</range>"
1694 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001695 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1696 CHECK_LYSP_RESTR(type.range, "range-str", "desc",
1697 "err-app-tag", "err-msg", 1, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001698 assert_true(type.flags & LYS_SET_RANGE);
Radek Krejci39b7fc22021-02-26 23:29:18 +01001699 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.range->exts[0]), LY_STMT_RANGE);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001700 lysp_type_free(&fctx, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001701 memset(&type, 0, sizeof(type));
1702
1703 /* min subelems */
1704 data = ELEMENT_WRAPPER_START "<range value=\"range-str\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001705 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1706 CHECK_LYSP_RESTR(type.range, "range-str", NULL,
1707 NULL, NULL, 0, NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001708 lysp_type_free(&fctx, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001709 memset(&type, 0, sizeof(type));
David Sedlák69f01612019-07-17 11:41:08 +02001710}
1711
1712static void
1713test_reqinstance_elem(void **state)
1714{
David Sedlák69f01612019-07-17 11:41:08 +02001715 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001716 struct lysp_type type = {0};
David Sedlák69f01612019-07-17 11:41:08 +02001717
David Sedlákd1144562019-08-06 12:36:14 +02001718 data = ELEMENT_WRAPPER_START "<require-instance value=\"true\">" EXT_SUBELEM "</require-instance>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001719 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001720 assert_int_equal(type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001721 assert_true(type.flags & LYS_SET_REQINST);
Radek Krejci5984d222021-02-26 23:01:45 +01001722 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.exts[0]), LY_STMT_REQUIRE_INSTANCE);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001723 lysp_type_free(&fctx, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001724 memset(&type, 0, sizeof(type));
1725
1726 data = ELEMENT_WRAPPER_START "<require-instance value=\"false\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001727 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001728 assert_int_equal(type.require_instance, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001729 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001730 memset(&type, 0, sizeof(type));
1731
1732 data = ELEMENT_WRAPPER_START "<require-instance value=\"invalid\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001733 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
David Sedlák69f01612019-07-17 11:41:08 +02001734 memset(&type, 0, sizeof(type));
Radek Iša56ca9e42020-09-08 18:42:00 +02001735 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"require-instance\" element. "
1736 "Valid values are \"true\" and \"false\".", "Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001737}
1738
1739static void
1740test_revision_date_elem(void **state)
1741{
David Sedlák69f01612019-07-17 11:41:08 +02001742 const char *data;
1743 char rev[LY_REV_SIZE];
1744
1745 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001746 assert_int_equal(test_element_helper(state, data, rev, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001747 assert_string_equal(rev, "2000-01-01");
1748
1749 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-50-05\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001750 assert_int_equal(test_element_helper(state, data, rev, NULL, NULL), LY_EVALID);
1751 CHECK_LOG_CTX("Invalid value \"2000-50-05\" of \"revision-date\".", "Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001752}
1753
1754static void
1755test_unique_elem(void **state)
1756{
David Sedlák69f01612019-07-17 11:41:08 +02001757 const char *data;
1758 const char **values = NULL;
1759
1760 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001761 assert_int_equal(test_element_helper(state, data, &values, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001762 assert_string_equal(*values, "tag");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001763 lydict_remove(UTEST_LYCTX, *values);
David Sedlák69f01612019-07-17 11:41:08 +02001764 LY_ARRAY_FREE(values);
David Sedlákd1144562019-08-06 12:36:14 +02001765 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001766}
1767
1768static void
1769test_units_elem(void **state)
1770{
David Sedlák69f01612019-07-17 11:41:08 +02001771 const char *data;
1772 const char *values = NULL;
1773
1774 data = ELEMENT_WRAPPER_START "<units name=\"name\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001775 assert_int_equal(test_element_helper(state, data, &values, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001776 assert_string_equal(values, "name");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001777 lydict_remove(UTEST_LYCTX, values);
David Sedlákd1144562019-08-06 12:36:14 +02001778 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001779}
1780
1781static void
David Sedlák69f01612019-07-17 11:41:08 +02001782test_yin_text_value_elem(void **state)
1783{
David Sedlák69f01612019-07-17 11:41:08 +02001784 const char *data;
1785 const char *val;
1786
1787 data = ELEMENT_WRAPPER_START "<text>text</text>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001788 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001789 assert_string_equal(val, "text");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001790 lydict_remove(UTEST_LYCTX, val);
David Sedlák69f01612019-07-17 11:41:08 +02001791
1792 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 +02001793 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001794 assert_string_equal(val, "text");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001795 lydict_remove(UTEST_LYCTX, val);
David Sedlák69f01612019-07-17 11:41:08 +02001796
1797 data = ELEMENT_WRAPPER_START "<text></text>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001798 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001799 assert_string_equal("", val);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001800 lydict_remove(UTEST_LYCTX, val);
David Sedlák8e7bda82019-07-16 17:57:50 +02001801}
David Sedlák32488102019-07-15 17:44:10 +02001802
David Sedlák374d2b32019-07-17 15:06:55 +02001803static void
1804test_type_elem(void **state)
1805{
David Sedlák374d2b32019-07-17 15:06:55 +02001806 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001807 struct lysp_type type = {0};
David Sedlák374d2b32019-07-17 15:06:55 +02001808
1809 /* max subelems */
1810 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001811 "<type name=\"type-name\">\n"
1812 " <base name=\"base-name\"/>\n"
1813 " <bit name=\"bit\"/>\n"
1814 " <enum name=\"enum\"/>\n"
1815 " <fraction-digits value=\"2\"/>\n"
1816 " <length value=\"length\"/>\n"
1817 " <path value=\"/path\"/>\n"
1818 " <pattern value=\"pattern\"/>\n"
1819 " <range value=\"range\" />\n"
1820 " <require-instance value=\"true\"/>\n"
1821 " <type name=\"sub-type-name\"/>\n"
1822 EXT_SUBELEM
1823 "</type>"
1824 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001825 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák374d2b32019-07-17 15:06:55 +02001826 assert_string_equal(type.name, "type-name");
1827 assert_string_equal(*type.bases, "base-name");
1828 assert_string_equal(type.bits->name, "bit");
1829 assert_string_equal(type.enums->name, "enum");
1830 assert_int_equal(type.fraction_digits, 2);
Radek Iša56ca9e42020-09-08 18:42:00 +02001831 CHECK_LYSP_RESTR(type.length, "length", NULL,
1832 NULL, NULL, 0, NULL);
Michal Vaskocb8c6d42020-10-16 11:58:30 +02001833 assert_string_equal(type.path->expr, "/path");
Radek Iša56ca9e42020-09-08 18:42:00 +02001834 CHECK_LYSP_RESTR(type.patterns, "\006pattern", NULL,
1835 NULL, NULL, 0, NULL);
1836 CHECK_LYSP_RESTR(type.range, "range", NULL,
1837 NULL, NULL, 0, NULL);
David Sedlák374d2b32019-07-17 15:06:55 +02001838 assert_int_equal(type.require_instance, 1);
1839 assert_string_equal(type.types->name, "sub-type-name");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001840 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.exts[0]), LY_STMT_TYPE);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001841 assert_true(type.flags & LYS_SET_BASE);
1842 assert_true(type.flags & LYS_SET_BIT);
1843 assert_true(type.flags & LYS_SET_ENUM);
1844 assert_true(type.flags & LYS_SET_FRDIGITS);
1845 assert_true(type.flags & LYS_SET_LENGTH);
1846 assert_true(type.flags & LYS_SET_PATH);
1847 assert_true(type.flags & LYS_SET_PATTERN);
1848 assert_true(type.flags & LYS_SET_RANGE);
1849 assert_true(type.flags & LYS_SET_REQINST);
1850 assert_true(type.flags & LYS_SET_TYPE);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001851 lysp_type_free(&fctx, &type);
David Sedlák374d2b32019-07-17 15:06:55 +02001852 memset(&type, 0, sizeof(type));
1853
1854 /* min subelems */
1855 data = ELEMENT_WRAPPER_START "<type name=\"type-name\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001856 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001857 lysp_type_free(&fctx, &type);
David Sedlák374d2b32019-07-17 15:06:55 +02001858 memset(&type, 0, sizeof(type));
David Sedlák374d2b32019-07-17 15:06:55 +02001859}
1860
David Sedlák1af868e2019-07-17 17:03:14 +02001861static void
1862test_max_elems_elem(void **state)
1863{
David Sedlák1af868e2019-07-17 17:03:14 +02001864 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001865 struct lysp_node_list list = {0};
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001866 struct lysp_refine refine = {0};
David Sedlák1af868e2019-07-17 17:03:14 +02001867
David Sedlák1af868e2019-07-17 17:03:14 +02001868 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"10\"/> </refine>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001869 assert_int_equal(test_element_helper(state, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02001870 assert_int_equal(refine.max, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001871 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001872
1873 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"0\"/> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001874 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID);
1875 CHECK_LOG_CTX("Invalid value \"0\" of \"value\" attribute in \"max-elements\" element.", "Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02001876
1877 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"-10\"/> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001878 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID);
1879 CHECK_LOG_CTX("Invalid value \"-10\" of \"value\" attribute in \"max-elements\" element.", "Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02001880
1881 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"k\"/> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001882 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID);
1883 CHECK_LOG_CTX("Invalid value \"k\" of \"value\" attribute in \"max-elements\" element.", "Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02001884
1885 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"u12\"/> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001886 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID);
1887 CHECK_LOG_CTX("Invalid value \"u12\" of \"value\" attribute in \"max-elements\" element.", "Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02001888}
1889
David Sedlák09e18c92019-07-18 11:17:11 +02001890static void
1891test_min_elems_elem(void **state)
1892{
David Sedlák09e18c92019-07-18 11:17:11 +02001893 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001894 struct lysp_node_leaflist llist = {0};
David Sedlák09e18c92019-07-18 11:17:11 +02001895
1896 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 +02001897 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID);
1898 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 +02001899
1900 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 +02001901 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID);
1902 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 +02001903
1904 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 +02001905 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID);
1906 CHECK_LOG_CTX("Invalid value \"5k\" of \"value\" attribute in \"min-elements\" element.", "Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02001907
1908 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 +02001909 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID);
1910 CHECK_LOG_CTX("Invalid value \"05\" of \"value\" attribute in \"min-elements\" element.", "Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02001911}
1912
David Sedláka2dad212019-07-18 12:45:19 +02001913static void
1914test_ordby_elem(void **state)
1915{
David Sedláka2dad212019-07-18 12:45:19 +02001916 const char *data;
1917 uint16_t flags = 0;
David Sedláka2dad212019-07-18 12:45:19 +02001918
1919 data = ELEMENT_WRAPPER_START "<ordered-by value=\"user\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001920 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001921 assert_true(flags & LYS_ORDBY_USER);
David Sedláka2dad212019-07-18 12:45:19 +02001922
1923 data = ELEMENT_WRAPPER_START "<ordered-by value=\"inv\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001924 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID);
1925 CHECK_LOG_CTX("Invalid value \"inv\" of \"value\" attribute in \"ordered-by\" element. "
1926 "Valid values are \"system\" and \"user\".", "Line number 1.");
David Sedláka2dad212019-07-18 12:45:19 +02001927}
1928
David Sedlák8a83bbb2019-07-18 14:46:00 +02001929static void
1930test_any_elem(void **state)
1931{
David Sedlák8a83bbb2019-07-18 14:46:00 +02001932 const char *data;
1933 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02001934 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák8a83bbb2019-07-18 14:46:00 +02001935 struct lysp_node_anydata *parsed = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02001936 uint16_t flags;
David Sedlák8a83bbb2019-07-18 14:46:00 +02001937
1938 /* anyxml max subelems */
1939 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001940 "<anyxml name=\"any-name\">\n"
1941 " <config value=\"true\" />\n"
1942 " <description><text>desc</text></description>\n"
1943 " <if-feature name=\"feature\" />\n"
1944 " <mandatory value=\"true\" />\n"
1945 " <must condition=\"must-cond\" />\n"
1946 " <reference><text>ref</text></reference>\n"
1947 " <status value=\"deprecated\"/>\n"
1948 " <when condition=\"when-cond\"/>\n"
1949 EXT_SUBELEM
1950 "</anyxml>"
1951 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001952 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001953 parsed = (struct lysp_node_anydata *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02001954 flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_DEPRC;
1955 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
1956 "any-name", 0, LYS_ANYXML, 0, "ref", 1);
1957 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001958 assert_string_equal(parsed->iffeatures[0].str, "feature");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001959 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_ANYXML);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001960 lysp_node_free(&fctx, siblings);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001961 siblings = NULL;
1962
1963 /* anydata max subelems */
1964 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001965 "<anydata name=\"any-name\">\n"
1966 " <config value=\"true\" />\n"
1967 " <description><text>desc</text></description>\n"
1968 " <if-feature name=\"feature\" />\n"
1969 " <mandatory value=\"true\" />\n"
1970 " <must condition=\"must-cond\" />\n"
1971 " <reference><text>ref</text></reference>\n"
1972 " <status value=\"deprecated\"/>\n"
1973 " <when condition=\"when-cond\"/>\n"
1974 EXT_SUBELEM
1975 "</anydata>"
1976 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001977 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001978 parsed = (struct lysp_node_anydata *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02001979 flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_DEPRC;
1980 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
1981 "any-name", 0, LYS_ANYDATA, 0, "ref", 1);
1982 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001983 assert_string_equal(parsed->iffeatures[0].str, "feature");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001984 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_ANYDATA);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001985 lysp_node_free(&fctx, siblings);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001986 siblings = NULL;
1987
1988 /* min subelems */
1989 node_meta.parent = (void *)0x10;
1990 data = ELEMENT_WRAPPER_START "<anydata name=\"any-name\"> </anydata>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001991 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001992 parsed = (struct lysp_node_anydata *)siblings;
1993 assert_ptr_equal(parsed->parent, node_meta.parent);
Radek Iša56ca9e42020-09-08 18:42:00 +02001994 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
1995 "any-name", 0, LYS_ANYDATA, 1, NULL, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001996 lysp_node_free(&fctx, siblings);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001997}
1998
David Sedlák203ca3a2019-07-18 15:26:25 +02001999static void
2000test_leaf_elem(void **state)
2001{
David Sedlák203ca3a2019-07-18 15:26:25 +02002002 const char *data;
2003 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002004 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák203ca3a2019-07-18 15:26:25 +02002005 struct lysp_node_leaf *parsed = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02002006 uint16_t flags;
David Sedlák203ca3a2019-07-18 15:26:25 +02002007
2008 /* max elements */
2009 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002010 "<leaf name=\"leaf\">\n"
2011 " <config value=\"true\" />\n"
2012 " <default value=\"def-val\"/>\n"
2013 " <description><text>desc</text></description>\n"
2014 " <if-feature name=\"feature\" />\n"
2015 " <mandatory value=\"true\" />\n"
2016 " <must condition=\"must-cond\" />\n"
2017 " <reference><text>ref</text></reference>\n"
2018 " <status value=\"deprecated\"/>\n"
2019 " <type name=\"type\"/>\n"
2020 " <units name=\"uni\"/>\n"
2021 " <when condition=\"when-cond\"/>\n"
2022 EXT_SUBELEM
2023 "</leaf>"
David Sedlák203ca3a2019-07-18 15:26:25 +02002024 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002025 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák203ca3a2019-07-18 15:26:25 +02002026 parsed = (struct lysp_node_leaf *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002027 flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_DEPRC;
2028 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2029 "leaf", 0, LYS_LEAF, 0, "ref", 1);
2030 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002031 assert_string_equal(parsed->iffeatures[0].str, "feature");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002032 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_LEAF);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002033 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlák203ca3a2019-07-18 15:26:25 +02002034 assert_string_equal(parsed->type.name, "type");
2035 assert_string_equal(parsed->units, "uni");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002036 assert_string_equal(parsed->dflt.str, "def-val");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002037 lysp_node_free(&fctx, siblings);
David Sedlák203ca3a2019-07-18 15:26:25 +02002038 siblings = NULL;
2039
2040 /* min elements */
2041 data = ELEMENT_WRAPPER_START "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002042 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák203ca3a2019-07-18 15:26:25 +02002043 parsed = (struct lysp_node_leaf *)siblings;
2044 assert_string_equal(parsed->name, "leaf");
2045 assert_string_equal(parsed->type.name, "type");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002046 lysp_node_free(&fctx, siblings);
David Sedlák203ca3a2019-07-18 15:26:25 +02002047 siblings = NULL;
David Sedlák203ca3a2019-07-18 15:26:25 +02002048}
2049
David Sedlákc3da3ef2019-07-19 12:56:08 +02002050static void
2051test_leaf_list_elem(void **state)
2052{
David Sedlákc3da3ef2019-07-19 12:56:08 +02002053 const char *data;
2054 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002055 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlákc3da3ef2019-07-19 12:56:08 +02002056 struct lysp_node_leaflist *parsed = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02002057 uint16_t flags;
David Sedlákc3da3ef2019-07-19 12:56:08 +02002058
2059 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002060 "<leaf-list name=\"llist\">\n"
2061 " <config value=\"true\" />\n"
2062 " <default value=\"def-val0\"/>\n"
2063 " <default value=\"def-val1\"/>\n"
2064 " <description><text>desc</text></description>\n"
2065 " <if-feature name=\"feature\"/>\n"
2066 " <max-elements value=\"5\"/>\n"
2067 " <must condition=\"must-cond\"/>\n"
2068 " <ordered-by value=\"user\" />\n"
2069 " <reference><text>ref</text></reference>\n"
2070 " <status value=\"current\"/>\n"
2071 " <type name=\"type\"/>\n"
2072 " <units name=\"uni\"/>\n"
2073 " <when condition=\"when-cond\"/>\n"
2074 EXT_SUBELEM
2075 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002076 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002077 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002078 parsed = (struct lysp_node_leaflist *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002079 flags = LYS_CONFIG_W | LYS_ORDBY_USER | LYS_STATUS_CURR | LYS_SET_MAX;
2080 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2081 "llist", 0, LYS_LEAFLIST, 0, "ref", 1);
2082 CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002083 assert_string_equal(parsed->dflts[0].str, "def-val0");
2084 assert_string_equal(parsed->dflts[1].str, "def-val1");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002085 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002086 assert_int_equal(parsed->max, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002087 assert_string_equal(parsed->type.name, "type");
2088 assert_string_equal(parsed->units, "uni");
Radek Iša56ca9e42020-09-08 18:42:00 +02002089 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002090 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_LEAF_LIST);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002091 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002092 siblings = NULL;
2093
2094 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002095 "<leaf-list name=\"llist\">\n"
2096 " <config value=\"true\" />\n"
2097 " <description><text>desc</text></description>\n"
2098 " <if-feature name=\"feature\"/>\n"
2099 " <min-elements value=\"5\"/>\n"
2100 " <must condition=\"must-cond\"/>\n"
2101 " <ordered-by value=\"user\" />\n"
2102 " <reference><text>ref</text></reference>\n"
2103 " <status value=\"current\"/>\n"
2104 " <type name=\"type\"/>\n"
2105 " <units name=\"uni\"/>\n"
2106 " <when condition=\"when-cond\"/>\n"
2107 EXT_SUBELEM
2108 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002109 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002110 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002111 parsed = (struct lysp_node_leaflist *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002112 flags = LYS_CONFIG_W | LYS_ORDBY_USER | LYS_STATUS_CURR | LYS_SET_MIN;
2113 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2114 "llist", 0, LYS_LEAFLIST, 0, "ref", 1);
2115 CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL);
2116 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002117 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002118 assert_int_equal(parsed->min, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002119 assert_string_equal(parsed->type.name, "type");
2120 assert_string_equal(parsed->units, "uni");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002121 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_LEAF_LIST);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002122 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002123 siblings = NULL;
2124
2125 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002126 "<leaf-list name=\"llist\">\n"
2127 " <config value=\"true\" />\n"
2128 " <description><text>desc</text></description>\n"
2129 " <if-feature name=\"feature\"/>\n"
2130 " <max-elements value=\"15\"/>\n"
2131 " <min-elements value=\"5\"/>\n"
2132 " <must condition=\"must-cond\"/>\n"
2133 " <ordered-by value=\"user\" />\n"
2134 " <reference><text>ref</text></reference>\n"
2135 " <status value=\"current\"/>\n"
2136 " <type name=\"type\"/>\n"
2137 " <units name=\"uni\"/>\n"
2138 " <when condition=\"when-cond\"/>\n"
2139 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002140 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002141 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002142 parsed = (struct lysp_node_leaflist *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002143 flags = LYS_CONFIG_W | LYS_ORDBY_USER | LYS_STATUS_CURR | LYS_SET_MIN | LYS_SET_MAX;
2144 CHECK_LYSP_NODE(parsed, "desc", 0, flags, 1,
2145 "llist", 0, LYS_LEAFLIST, 0, "ref", 1);
2146 CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL);
2147 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002148 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002149 assert_int_equal(parsed->min, 5);
2150 assert_int_equal(parsed->max, 15);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002151 assert_string_equal(parsed->type.name, "type");
2152 assert_string_equal(parsed->units, "uni");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002153 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002154 siblings = NULL;
2155
2156 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002157 "<leaf-list name=\"llist\">\n"
2158 " <type name=\"type\"/>\n"
2159 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002160 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002161 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002162 parsed = (struct lysp_node_leaflist *)siblings;
2163 assert_string_equal(parsed->name, "llist");
2164 assert_string_equal(parsed->type.name, "type");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002165 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002166 siblings = NULL;
2167
2168 /* invalid combinations */
2169 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002170 "<leaf-list name=\"llist\">\n"
2171 " <max-elements value=\"5\"/>\n"
2172 " <min-elements value=\"15\"/>\n"
2173 " <type name=\"type\"/>"
2174 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002175 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002176 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_EVALID);
2177 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 +02002178 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002179 siblings = NULL;
2180
2181 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002182 "<leaf-list name=\"llist\">\n"
2183 " <default value=\"def-val1\"/>\n"
2184 " <min-elements value=\"15\"/>\n"
2185 " <type name=\"type\"/>\n"
2186 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002187 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002188 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_EVALID);
2189 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 +02002190 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002191 siblings = NULL;
2192
2193 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002194 "<leaf-list name=\"llist\">"
2195 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002196 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002197 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_EVALID);
2198 CHECK_LOG_CTX("Missing mandatory sub-element \"type\" of \"leaf-list\" element.", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002199 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002200 siblings = NULL;
David Sedlákc3da3ef2019-07-19 12:56:08 +02002201}
2202
David Sedlákcb39f642019-07-19 13:19:55 +02002203static void
2204test_presence_elem(void **state)
2205{
David Sedlákcb39f642019-07-19 13:19:55 +02002206 const char *data;
2207 const char *val;
2208
2209 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002210 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlákcb39f642019-07-19 13:19:55 +02002211 assert_string_equal(val, "presence-val");
Michal Vaskoe180ed02021-02-05 16:31:20 +01002212 lydict_remove(UTEST_LYCTX, val);
David Sedlákcb39f642019-07-19 13:19:55 +02002213
2214 data = ELEMENT_WRAPPER_START "<presence/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002215 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
2216 CHECK_LOG_CTX("Missing mandatory attribute value of presence element.", "Line number 1.");
David Sedlákcb39f642019-07-19 13:19:55 +02002217}
2218
David Sedlák12470a82019-07-19 13:44:36 +02002219static void
2220test_key_elem(void **state)
2221{
David Sedlák12470a82019-07-19 13:44:36 +02002222 const char *data;
2223 const char *val;
2224
2225 data = ELEMENT_WRAPPER_START "<key value=\"key-value\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002226 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák12470a82019-07-19 13:44:36 +02002227 assert_string_equal(val, "key-value");
Michal Vaskoe180ed02021-02-05 16:31:20 +01002228 lydict_remove(UTEST_LYCTX, val);
David Sedlák12470a82019-07-19 13:44:36 +02002229
2230 data = ELEMENT_WRAPPER_START "<key/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002231 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
2232 CHECK_LOG_CTX("Missing mandatory attribute value of key element.", "Line number 1.");
David Sedlák12470a82019-07-19 13:44:36 +02002233}
2234
David Sedlák04e17b22019-07-19 15:29:48 +02002235static void
David Sedlák0d6de5a2019-07-22 13:25:44 +02002236test_uses_elem(void **state)
2237{
David Sedlák0d6de5a2019-07-22 13:25:44 +02002238 const char *data;
2239 struct lysp_node *siblings = NULL;
2240 struct tree_node_meta node_meta = {NULL, &siblings};
2241 struct lysp_node_uses *parsed = NULL;
2242
2243 /* max subelems */
2244 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002245 "<uses name=\"uses-name\">\n"
2246 " <when condition=\"cond\" />\n"
2247 " <if-feature name=\"feature\" />\n"
2248 " <status value=\"obsolete\" />\n"
2249 " <description><text>desc</text></description>\n"
2250 " <reference><text>ref</text></reference>\n"
2251 " <refine target-node=\"target\"/>\n"
2252 " <augment target-node=\"target\" />\n"
2253 EXT_SUBELEM
2254 "</uses>"
2255 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002256 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002257 parsed = (struct lysp_node_uses *)&siblings[0];
Radek Iša56ca9e42020-09-08 18:42:00 +02002258 CHECK_LYSP_NODE(parsed, "desc", 1, LYS_STATUS_OBSLT, 1,
2259 "uses-name", 0, LYS_USES, 0, "ref", 1);
2260 CHECK_LYSP_WHEN(parsed->when, "cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002261 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlák0d6de5a2019-07-22 13:25:44 +02002262 assert_string_equal(parsed->refines->nodeid, "target");
David Sedlák992fb7c2019-07-24 16:51:01 +02002263 assert_string_equal(parsed->augments->nodeid, "target");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002264 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_USES);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002265 lysp_node_free(&fctx, siblings);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002266 siblings = NULL;
2267
2268 /* min subelems */
2269 data = ELEMENT_WRAPPER_START "<uses name=\"uses-name\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002270 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002271 assert_string_equal(siblings[0].name, "uses-name");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002272 lysp_node_free(&fctx, siblings);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002273 siblings = NULL;
David Sedlák0d6de5a2019-07-22 13:25:44 +02002274}
2275
David Sedlákaa854b02019-07-22 14:17:10 +02002276static void
David Sedlákaf536aa2019-07-23 13:42:23 +02002277test_list_elem(void **state)
2278{
David Sedlákaf536aa2019-07-23 13:42:23 +02002279 const char *data;
2280 struct lysp_node *siblings = NULL;
2281 struct tree_node_meta node_meta = {NULL, &siblings};
2282 struct lysp_node_list *parsed = NULL;
2283
2284 /* max subelems */
2285 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002286 "<list name=\"list-name\">\n"
2287 " <when condition=\"when\"/>\n"
2288 " <if-feature name=\"iff\"/>\n"
2289 " <must condition=\"must-cond\"/>\n"
2290 " <key value=\"key\"/>\n"
2291 " <unique tag=\"utag\"/>\n"
2292 " <config value=\"true\"/>\n"
2293 " <min-elements value=\"10\"/>\n"
2294 " <ordered-by value=\"user\"/>\n"
2295 " <status value=\"deprecated\"/>\n"
2296 " <description><text>desc</text></description>\n"
2297 " <reference><text>ref</text></reference>\n"
2298 " <anydata name=\"anyd\"/>\n"
2299 " <anyxml name=\"anyx\"/>\n"
2300 " <container name=\"cont\"/>\n"
2301 " <choice name=\"choice\"/>\n"
2302 " <action name=\"action\"/>\n"
2303 " <grouping name=\"grp\"/>\n"
2304 " <notification name=\"notf\"/>\n"
2305 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2306 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2307 " <list name=\"sub-list\"/>\n"
2308 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2309 " <uses name=\"uses-name\"/>\n"
2310 EXT_SUBELEM
2311 "</list>"
2312 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002313 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákaf536aa2019-07-23 13:42:23 +02002314 parsed = (struct lysp_node_list *)&siblings[0];
David Sedlákaf536aa2019-07-23 13:42:23 +02002315 assert_string_equal(parsed->child->name, "anyd");
2316 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2317 assert_string_equal(parsed->child->next->name, "anyx");
2318 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
David Sedlákf111bcb2019-07-23 17:15:51 +02002319 assert_string_equal(parsed->child->next->next->name, "cont");
2320 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002321 assert_string_equal(parsed->child->next->next->next->name, "choice");
2322 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
David Sedlák85d0eca2019-07-24 15:15:21 +02002323 assert_string_equal(parsed->child->next->next->next->next->name, "leaf");
2324 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAF);
2325 assert_string_equal(parsed->child->next->next->next->next->next->name, "llist");
2326 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2327 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "sub-list");
2328 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LIST);
2329 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "uses-name");
2330 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_USES);
2331 assert_null(parsed->child->next->next->next->next->next->next->next->next);
Radek Iša56ca9e42020-09-08 18:42:00 +02002332 uint16_t flags = LYS_ORDBY_USER | LYS_STATUS_DEPRC | LYS_CONFIG_W | LYS_SET_MIN;
2333
2334 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2335 "list-name", 0, LYS_LIST, 0, "ref", 1);
2336 CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL);
2337 CHECK_LYSP_WHEN(parsed->when, "when", NULL, 0, NULL);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002338 assert_string_equal(parsed->groupings->name, "grp");
David Sedlák85d0eca2019-07-24 15:15:21 +02002339 assert_string_equal(parsed->actions->name, "action");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002340 assert_int_equal(parsed->groupings->nodetype, LYS_GROUPING);
David Sedlák031b9e72019-07-23 15:19:37 +02002341 assert_string_equal(parsed->notifs->name, "notf");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002342 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlákaf536aa2019-07-23 13:42:23 +02002343 assert_string_equal(parsed->key, "key");
2344 assert_int_equal(parsed->min, 10);
David Sedlákaf536aa2019-07-23 13:42:23 +02002345 assert_string_equal(parsed->typedefs->name, "tpdf");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002346 assert_string_equal(parsed->uniques->str, "utag");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002347 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_LIST);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002348 lysp_node_free(&fctx, siblings);
Radek Iša56ca9e42020-09-08 18:42:00 +02002349 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
David Sedlákaf536aa2019-07-23 13:42:23 +02002350 siblings = NULL;
2351
2352 /* min subelems */
2353 data = ELEMENT_WRAPPER_START "<list name=\"list-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002354 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákaf536aa2019-07-23 13:42:23 +02002355 parsed = (struct lysp_node_list *)&siblings[0];
Radek Iša56ca9e42020-09-08 18:42:00 +02002356 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2357 "list-name", 0, LYS_LIST, 0, NULL, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002358 lysp_node_free(&fctx, siblings);
David Sedlákaf536aa2019-07-23 13:42:23 +02002359 siblings = NULL;
David Sedlákaf536aa2019-07-23 13:42:23 +02002360}
2361
David Sedlák031b9e72019-07-23 15:19:37 +02002362static void
2363test_notification_elem(void **state)
2364{
David Sedlák031b9e72019-07-23 15:19:37 +02002365 const char *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002366 struct lysp_node_notif *notifs = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002367 struct tree_node_meta notif_meta = {NULL, (struct lysp_node **)&notifs};
David Sedlák031b9e72019-07-23 15:19:37 +02002368
2369 /* max subelems */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002370 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlák031b9e72019-07-23 15:19:37 +02002371 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002372 "<notification name=\"notif-name\">\n"
2373 " <anydata name=\"anyd\"/>\n"
2374 " <anyxml name=\"anyx\"/>\n"
2375 " <description><text>desc</text></description>\n"
2376 " <if-feature name=\"iff\"/>\n"
2377 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2378 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2379 " <list name=\"sub-list\"/>\n"
2380 " <must condition=\"cond\"/>\n"
2381 " <reference><text>ref</text></reference>\n"
2382 " <status value=\"deprecated\"/>\n"
2383 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2384 " <uses name=\"uses-name\"/>\n"
2385 " <container name=\"cont\"/>\n"
2386 " <choice name=\"choice\"/>\n"
2387 " <grouping name=\"grp\"/>\n"
2388 EXT_SUBELEM
2389 "</notification>"
2390 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002391 assert_int_equal(test_element_helper(state, data, &notif_meta, NULL, NULL), LY_SUCCESS);
David Sedlák031b9e72019-07-23 15:19:37 +02002392 assert_string_equal(notifs->name, "notif-name");
Radek Krejci01180ac2021-01-27 08:48:22 +01002393 assert_string_equal(notifs->child->name, "anyd");
2394 assert_int_equal(notifs->child->nodetype, LYS_ANYDATA);
2395 assert_string_equal(notifs->child->next->name, "anyx");
2396 assert_int_equal(notifs->child->next->nodetype, LYS_ANYXML);
2397 assert_string_equal(notifs->child->next->next->name, "leaf");
2398 assert_int_equal(notifs->child->next->next->nodetype, LYS_LEAF);
2399 assert_string_equal(notifs->child->next->next->next->name, "llist");
2400 assert_int_equal(notifs->child->next->next->next->nodetype, LYS_LEAFLIST);
2401 assert_string_equal(notifs->child->next->next->next->next->name, "sub-list");
2402 assert_int_equal(notifs->child->next->next->next->next->nodetype, LYS_LIST);
David Sedlák031b9e72019-07-23 15:19:37 +02002403 assert_true(notifs->flags & LYS_STATUS_DEPRC);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002404 assert_string_equal(notifs->groupings->name, "grp");
2405 assert_int_equal(notifs->groupings->nodetype, LYS_GROUPING);
Radek Krejci01180ac2021-01-27 08:48:22 +01002406 assert_string_equal(notifs->child->next->next->next->next->next->name, "uses-name");
2407 assert_int_equal(notifs->child->next->next->next->next->next->nodetype, LYS_USES);
2408 assert_string_equal(notifs->child->next->next->next->next->next->next->name, "cont");
2409 assert_int_equal(notifs->child->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
2410 assert_int_equal(notifs->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2411 assert_string_equal(notifs->child->next->next->next->next->next->next->next->name, "choice");
2412 assert_null(notifs->child->next->next->next->next->next->next->next->next);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002413 assert_string_equal(notifs->iffeatures[0].str, "iff");
2414 assert_string_equal(notifs->musts->arg.str, "cond");
David Sedlák031b9e72019-07-23 15:19:37 +02002415 assert_int_equal(notifs->nodetype, LYS_NOTIF);
2416 assert_null(notifs->parent);
2417 assert_string_equal(notifs->ref, "ref");
2418 assert_string_equal(notifs->typedefs->name, "tpdf");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002419 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(notifs->exts[0]), LY_STMT_NOTIFICATION);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002420 lysp_node_free(&fctx, (struct lysp_node *)notifs);
David Sedlák031b9e72019-07-23 15:19:37 +02002421 notifs = NULL;
2422
2423 /* min subelems */
2424 data = ELEMENT_WRAPPER_START "<notification name=\"notif-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002425 assert_int_equal(test_element_helper(state, data, &notif_meta, NULL, NULL), LY_SUCCESS);
David Sedlák031b9e72019-07-23 15:19:37 +02002426 assert_string_equal(notifs->name, "notif-name");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002427 lysp_node_free(&fctx, (struct lysp_node *)notifs);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002428 notifs = NULL;
David Sedláke3ce9ef2019-07-23 16:34:30 +02002429}
2430
2431static void
2432test_grouping_elem(void **state)
2433{
David Sedláke3ce9ef2019-07-23 16:34:30 +02002434 const char *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002435 struct lysp_node_grp *grps = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002436 struct tree_node_meta grp_meta = {NULL, (struct lysp_node **)&grps};
David Sedláke3ce9ef2019-07-23 16:34:30 +02002437
2438 /* max subelems */
2439 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002440 "<grouping name=\"grp-name\">\n"
2441 " <anydata name=\"anyd\"/>\n"
2442 " <anyxml name=\"anyx\"/>\n"
2443 " <description><text>desc</text></description>\n"
2444 " <grouping name=\"sub-grp\"/>\n"
2445 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2446 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2447 " <list name=\"list\"/>\n"
2448 " <notification name=\"notf\"/>\n"
2449 " <reference><text>ref</text></reference>\n"
2450 " <status value=\"current\"/>\n"
2451 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2452 " <uses name=\"uses-name\"/>\n"
2453 " <action name=\"act\"/>\n"
2454 " <container name=\"cont\"/>\n"
2455 " <choice name=\"choice\"/>\n"
2456 EXT_SUBELEM
2457 "</grouping>"
2458 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002459 assert_int_equal(test_element_helper(state, data, &grp_meta, NULL, NULL), LY_SUCCESS);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002460 assert_string_equal(grps->name, "grp-name");
Radek Krejci01180ac2021-01-27 08:48:22 +01002461 assert_string_equal(grps->child->name, "anyd");
2462 assert_string_equal(grps->child->next->name, "anyx");
2463 assert_string_equal(grps->child->next->next->name, "leaf");
2464 assert_string_equal(grps->child->next->next->next->name, "llist");
2465 assert_string_equal(grps->child->next->next->next->next->name, "list");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002466 assert_string_equal(grps->dsc, "desc");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002467 assert_true(grps->flags & LYS_STATUS_CURR);
2468 assert_string_equal(grps->groupings->name, "sub-grp");
2469 assert_int_equal(grps->nodetype, LYS_GROUPING);
2470 assert_string_equal(grps->notifs->name, "notf");
2471 assert_null(grps->parent);
2472 assert_string_equal(grps->ref, "ref");
2473 assert_string_equal(grps->typedefs->name, "tpdf");
David Sedlák85d0eca2019-07-24 15:15:21 +02002474 assert_string_equal(grps->actions->name, "act");
Radek Krejci01180ac2021-01-27 08:48:22 +01002475 assert_string_equal(grps->child->next->next->next->next->next->name, "uses-name");
2476 assert_int_equal(grps->child->next->next->next->next->next->nodetype, LYS_USES);
2477 assert_string_equal(grps->child->next->next->next->next->next->next->name, "cont");
2478 assert_int_equal(grps->child->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
2479 assert_string_equal(grps->child->next->next->next->next->next->next->next->name, "choice");
2480 assert_int_equal(grps->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002481 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(grps->exts[0]), LY_STMT_GROUPING);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002482 lysp_node_free(&fctx, &grps->node);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002483 grps = NULL;
2484
2485 /* min subelems */
2486 data = ELEMENT_WRAPPER_START "<grouping name=\"grp-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002487 assert_int_equal(test_element_helper(state, data, &grp_meta, NULL, NULL), LY_SUCCESS);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002488 assert_string_equal(grps->name, "grp-name");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002489 lysp_node_free(&fctx, &grps->node);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002490 grps = NULL;
David Sedlák031b9e72019-07-23 15:19:37 +02002491}
2492
David Sedlákf111bcb2019-07-23 17:15:51 +02002493static void
2494test_container_elem(void **state)
2495{
David Sedlákf111bcb2019-07-23 17:15:51 +02002496 const char *data;
2497 struct lysp_node *siblings = NULL;
2498 struct tree_node_meta node_meta = {NULL, &siblings};
2499 struct lysp_node_container *parsed = NULL;
2500
2501 /* max subelems */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002502 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedláke2dc9e92019-07-24 09:59:21 +02002503 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002504 "<container name=\"cont-name\">\n"
2505 " <anydata name=\"anyd\"/>\n"
2506 " <anyxml name=\"anyx\"/>\n"
2507 " <config value=\"true\"/>\n"
2508 " <container name=\"subcont\"/>\n"
2509 " <description><text>desc</text></description>\n"
2510 " <grouping name=\"sub-grp\"/>\n"
2511 " <if-feature name=\"iff\"/>\n"
2512 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2513 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2514 " <list name=\"list\"/>\n"
2515 " <must condition=\"cond\"/>\n"
2516 " <notification name=\"notf\"/>\n"
2517 " <presence value=\"presence\"/>\n"
2518 " <reference><text>ref</text></reference>\n"
2519 " <status value=\"current\"/>\n"
2520 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2521 " <uses name=\"uses-name\"/>\n"
2522 " <when condition=\"when-cond\"/>\n"
2523 " <action name=\"act\"/>\n"
2524 " <choice name=\"choice\"/>\n"
2525 EXT_SUBELEM
2526 "</container>"
2527 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002528 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedláke2dc9e92019-07-24 09:59:21 +02002529 parsed = (struct lysp_node_container *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002530 uint16_t flags = LYS_CONFIG_W | LYS_STATUS_CURR;
2531
2532 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2533 "cont-name", 0, LYS_CONTAINER, 0, "ref", 1);
2534 CHECK_LYSP_RESTR(parsed->musts, "cond", NULL, NULL, NULL, 0, NULL);
2535 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
2536
Michal Vasko7f45cf22020-10-01 12:49:44 +02002537 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedláke2dc9e92019-07-24 09:59:21 +02002538 assert_string_equal(parsed->presence, "presence");
2539 assert_string_equal(parsed->typedefs->name, "tpdf");
2540 assert_string_equal(parsed->groupings->name, "sub-grp");
2541 assert_string_equal(parsed->child->name, "anyd");
2542 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2543 assert_string_equal(parsed->child->next->name, "anyx");
2544 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2545 assert_string_equal(parsed->child->next->next->name, "subcont");
2546 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
2547 assert_string_equal(parsed->child->next->next->next->name, "leaf");
2548 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
2549 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
2550 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
2551 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
2552 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
2553 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
2554 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002555 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
2556 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2557 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke2dc9e92019-07-24 09:59:21 +02002558 assert_string_equal(parsed->notifs->name, "notf");
David Sedlák85d0eca2019-07-24 15:15:21 +02002559 assert_string_equal(parsed->actions->name, "act");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002560 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_CONTAINER);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002561 lysp_node_free(&fctx, siblings);
Radek Iša56ca9e42020-09-08 18:42:00 +02002562 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
David Sedláke2dc9e92019-07-24 09:59:21 +02002563 siblings = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02002564
2565 /* min subelems */
2566 data = ELEMENT_WRAPPER_START "<container name=\"cont-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002567 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákf111bcb2019-07-23 17:15:51 +02002568 parsed = (struct lysp_node_container *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002569 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2570 "cont-name", 0, LYS_CONTAINER, 0, NULL, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002571 lysp_node_free(&fctx, siblings);
David Sedlákf111bcb2019-07-23 17:15:51 +02002572 siblings = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02002573}
2574
David Sedlák5379d392019-07-24 10:42:03 +02002575static void
2576test_case_elem(void **state)
2577{
David Sedlák5379d392019-07-24 10:42:03 +02002578 const char *data;
2579 struct lysp_node *siblings = NULL;
2580 struct tree_node_meta node_meta = {NULL, &siblings};
2581 struct lysp_node_case *parsed = NULL;
2582
2583 /* max subelems */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002584 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlák5379d392019-07-24 10:42:03 +02002585 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002586 "<case name=\"case-name\">\n"
2587 " <anydata name=\"anyd\"/>\n"
2588 " <anyxml name=\"anyx\"/>\n"
2589 " <container name=\"subcont\"/>\n"
2590 " <description><text>desc</text></description>\n"
2591 " <if-feature name=\"iff\"/>\n"
2592 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2593 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2594 " <list name=\"list\"/>\n"
2595 " <reference><text>ref</text></reference>\n"
2596 " <status value=\"current\"/>\n"
2597 " <uses name=\"uses-name\"/>\n"
2598 " <when condition=\"when-cond\"/>\n"
2599 " <choice name=\"choice\"/>\n"
2600 EXT_SUBELEM
2601 "</case>"
2602 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002603 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák5379d392019-07-24 10:42:03 +02002604 parsed = (struct lysp_node_case *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002605 uint16_t flags = LYS_STATUS_CURR;
2606
2607 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2608 "case-name", 0, LYS_CASE, 0, "ref", 1);
2609 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002610 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlák5379d392019-07-24 10:42:03 +02002611 assert_string_equal(parsed->child->name, "anyd");
2612 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2613 assert_string_equal(parsed->child->next->name, "anyx");
2614 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2615 assert_string_equal(parsed->child->next->next->name, "subcont");
2616 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
2617 assert_string_equal(parsed->child->next->next->next->name, "leaf");
2618 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
2619 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
2620 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
2621 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
2622 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
2623 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
2624 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002625 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
2626 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2627 assert_null(parsed->child->next->next->next->next->next->next->next->next);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002628 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_CASE);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002629 lysp_node_free(&fctx, siblings);
David Sedlák5379d392019-07-24 10:42:03 +02002630 siblings = NULL;
2631
2632 /* min subelems */
2633 data = ELEMENT_WRAPPER_START "<case name=\"case-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002634 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák5379d392019-07-24 10:42:03 +02002635 parsed = (struct lysp_node_case *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002636 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2637 "case-name", 0, LYS_CASE, 0, NULL, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002638 lysp_node_free(&fctx, siblings);
David Sedlák5379d392019-07-24 10:42:03 +02002639 siblings = NULL;
David Sedlák5379d392019-07-24 10:42:03 +02002640}
2641
David Sedlákb7abcfa2019-07-24 12:33:35 +02002642static void
2643test_choice_elem(void **state)
2644{
David Sedlákb7abcfa2019-07-24 12:33:35 +02002645 const char *data;
2646 struct lysp_node *siblings = NULL;
2647 struct tree_node_meta node_meta = {NULL, &siblings};
2648 struct lysp_node_choice *parsed = NULL;
2649
2650 /* max subelems */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002651 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002652 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002653 "<choice name=\"choice-name\">\n"
2654 " <anydata name=\"anyd\"/>\n"
2655 " <anyxml name=\"anyx\"/>\n"
2656 " <case name=\"sub-case\"/>\n"
2657 " <choice name=\"choice\"/>\n"
2658 " <config value=\"true\"/>\n"
2659 " <container name=\"subcont\"/>\n"
2660 " <default value=\"def\"/>\n"
2661 " <description><text>desc</text></description>\n"
2662 " <if-feature name=\"iff\"/>\n"
2663 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2664 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2665 " <list name=\"list\"/>\n"
2666 " <mandatory value=\"true\" />\n"
2667 " <reference><text>ref</text></reference>\n"
2668 " <status value=\"current\"/>\n"
2669 " <when condition=\"when-cond\"/>\n"
2670 EXT_SUBELEM
2671 "</choice>"
2672 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002673 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002674 parsed = (struct lysp_node_choice *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002675 uint16_t flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_CURR;
2676
2677 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2678 "choice-name", 0, LYS_CHOICE, 0, "ref", 1);
2679 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002680 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlákb7abcfa2019-07-24 12:33:35 +02002681 assert_string_equal(parsed->child->name, "anyd");
2682 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2683 assert_string_equal(parsed->child->next->name, "anyx");
2684 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2685 assert_string_equal(parsed->child->next->next->name, "sub-case");
2686 assert_int_equal(parsed->child->next->next->nodetype, LYS_CASE);
2687 assert_string_equal(parsed->child->next->next->next->name, "choice");
2688 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
2689 assert_string_equal(parsed->child->next->next->next->next->name, "subcont");
2690 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_CONTAINER);
2691 assert_string_equal(parsed->child->next->next->next->next->next->name, "leaf");
2692 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAF);
2693 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "llist");
2694 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2695 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "list");
2696 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
2697 assert_null(parsed->child->next->next->next->next->next->next->next->next);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002698 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_CHOICE);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002699 lysp_node_free(&fctx, siblings);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002700 siblings = NULL;
2701
2702 /* min subelems */
2703 data = ELEMENT_WRAPPER_START "<choice name=\"choice-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002704 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002705 parsed = (struct lysp_node_choice *)siblings;
2706 assert_string_equal(parsed->name, "choice-name");
Radek Iša56ca9e42020-09-08 18:42:00 +02002707 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2708 "choice-name", 0, LYS_CHOICE, 0, NULL, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002709 lysp_node_free(&fctx, siblings);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002710 siblings = NULL;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002711}
2712
David Sedlák05404f62019-07-24 14:11:53 +02002713static void
2714test_inout_elem(void **state)
2715{
David Sedlák05404f62019-07-24 14:11:53 +02002716 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02002717 struct lysp_node_action_inout inout = {0};
David Sedlák05404f62019-07-24 14:11:53 +02002718 struct inout_meta inout_meta = {NULL, &inout};
2719
2720 /* max subelements */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002721 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlák05404f62019-07-24 14:11:53 +02002722 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002723 "<input>\n"
2724 " <anydata name=\"anyd\"/>\n"
2725 " <anyxml name=\"anyx\"/>\n"
2726 " <choice name=\"choice\"/>\n"
2727 " <container name=\"subcont\"/>\n"
2728 " <grouping name=\"sub-grp\"/>\n"
2729 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2730 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2731 " <list name=\"list\"/>\n"
2732 " <must condition=\"cond\"/>\n"
2733 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2734 " <uses name=\"uses-name\"/>\n"
2735 EXT_SUBELEM
2736 "</input>"
2737 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002738 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
2739 CHECK_LYSP_ACTION_INOUT(&(inout), 1, 1, 1, 1, LYS_INPUT, 0, 1);
2740 CHECK_LYSP_RESTR(inout.musts, "cond", NULL, NULL, NULL, 0, NULL);
David Sedlák05404f62019-07-24 14:11:53 +02002741 assert_string_equal(inout.typedefs->name, "tpdf");
2742 assert_string_equal(inout.groupings->name, "sub-grp");
Radek Krejci01180ac2021-01-27 08:48:22 +01002743 assert_string_equal(inout.child->name, "anyd");
2744 assert_int_equal(inout.child->nodetype, LYS_ANYDATA);
2745 assert_string_equal(inout.child->next->name, "anyx");
2746 assert_int_equal(inout.child->next->nodetype, LYS_ANYXML);
2747 assert_string_equal(inout.child->next->next->name, "choice");
2748 assert_int_equal(inout.child->next->next->nodetype, LYS_CHOICE);
2749 assert_string_equal(inout.child->next->next->next->name, "subcont");
2750 assert_int_equal(inout.child->next->next->next->nodetype, LYS_CONTAINER);
2751 assert_string_equal(inout.child->next->next->next->next->name, "leaf");
2752 assert_int_equal(inout.child->next->next->next->next->nodetype, LYS_LEAF);
2753 assert_string_equal(inout.child->next->next->next->next->next->name, "llist");
2754 assert_int_equal(inout.child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2755 assert_string_equal(inout.child->next->next->next->next->next->next->name, "list");
2756 assert_int_equal(inout.child->next->next->next->next->next->next->nodetype, LYS_LIST);
2757 assert_string_equal(inout.child->next->next->next->next->next->next->next->name, "uses-name");
2758 assert_int_equal(inout.child->next->next->next->next->next->next->next->nodetype, LYS_USES);
2759 assert_null(inout.child->next->next->next->next->next->next->next->next);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002760 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(inout.exts[0]), LY_STMT_INPUT);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002761 lysp_node_free(&fctx, (struct lysp_node *)&inout);
David Sedlák05404f62019-07-24 14:11:53 +02002762 memset(&inout, 0, sizeof inout);
2763
2764 /* max subelements */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002765 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlák05404f62019-07-24 14:11:53 +02002766 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002767 "<output>\n"
2768 " <anydata name=\"anyd\"/>\n"
2769 " <anyxml name=\"anyx\"/>\n"
2770 " <choice name=\"choice\"/>\n"
2771 " <container name=\"subcont\"/>\n"
2772 " <grouping name=\"sub-grp\"/>\n"
2773 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2774 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2775 " <list name=\"list\"/>\n"
2776 " <must condition=\"cond\"/>\n"
2777 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2778 " <uses name=\"uses-name\"/>\n"
2779 EXT_SUBELEM
2780 "</output>"
2781 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002782 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
2783 CHECK_LYSP_ACTION_INOUT(&(inout), 1, 1, 1, 1, LYS_OUTPUT, 0, 1);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002784 assert_string_equal(inout.musts->arg.str, "cond");
David Sedlák05404f62019-07-24 14:11:53 +02002785 assert_string_equal(inout.typedefs->name, "tpdf");
2786 assert_string_equal(inout.groupings->name, "sub-grp");
Radek Krejci01180ac2021-01-27 08:48:22 +01002787 assert_string_equal(inout.child->name, "anyd");
2788 assert_int_equal(inout.child->nodetype, LYS_ANYDATA);
2789 assert_string_equal(inout.child->next->name, "anyx");
2790 assert_int_equal(inout.child->next->nodetype, LYS_ANYXML);
2791 assert_string_equal(inout.child->next->next->name, "choice");
2792 assert_int_equal(inout.child->next->next->nodetype, LYS_CHOICE);
2793 assert_string_equal(inout.child->next->next->next->name, "subcont");
2794 assert_int_equal(inout.child->next->next->next->nodetype, LYS_CONTAINER);
2795 assert_string_equal(inout.child->next->next->next->next->name, "leaf");
2796 assert_int_equal(inout.child->next->next->next->next->nodetype, LYS_LEAF);
2797 assert_string_equal(inout.child->next->next->next->next->next->name, "llist");
2798 assert_int_equal(inout.child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2799 assert_string_equal(inout.child->next->next->next->next->next->next->name, "list");
2800 assert_int_equal(inout.child->next->next->next->next->next->next->nodetype, LYS_LIST);
2801 assert_string_equal(inout.child->next->next->next->next->next->next->next->name, "uses-name");
2802 assert_int_equal(inout.child->next->next->next->next->next->next->next->nodetype, LYS_USES);
2803 assert_null(inout.child->next->next->next->next->next->next->next->next);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002804 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(inout.exts[0]), LY_STMT_OUTPUT);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002805 lysp_node_free(&fctx, (struct lysp_node *)&inout);
David Sedlák05404f62019-07-24 14:11:53 +02002806 memset(&inout, 0, sizeof inout);
2807
2808 /* min subelems */
Michal Vaskob83af8a2020-01-06 09:49:22 +01002809 data = ELEMENT_WRAPPER_START "<input><leaf name=\"l\"><type name=\"empty\"/></leaf></input>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002810 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002811 lysp_node_free(&fctx, (struct lysp_node *)&inout);
David Sedlák05404f62019-07-24 14:11:53 +02002812 memset(&inout, 0, sizeof inout);
2813
Michal Vaskob83af8a2020-01-06 09:49:22 +01002814 data = ELEMENT_WRAPPER_START "<output><leaf name=\"l\"><type name=\"empty\"/></leaf></output>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002815 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002816 lysp_node_free(&fctx, (struct lysp_node *)&inout);
David Sedlák05404f62019-07-24 14:11:53 +02002817 memset(&inout, 0, sizeof inout);
2818
2819 /* invalid combinations */
2820 data = ELEMENT_WRAPPER_START "<input name=\"test\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002821 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_EVALID);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002822 lysp_node_free(&fctx, (struct lysp_node *)&inout);
Radek Iša56ca9e42020-09-08 18:42:00 +02002823 CHECK_LOG_CTX("Unexpected attribute \"name\" of \"input\" element.", "Line number 1.");
David Sedlák05404f62019-07-24 14:11:53 +02002824 memset(&inout, 0, sizeof inout);
David Sedlák05404f62019-07-24 14:11:53 +02002825}
2826
David Sedlák85d0eca2019-07-24 15:15:21 +02002827static void
2828test_action_elem(void **state)
2829{
David Sedlák85d0eca2019-07-24 15:15:21 +02002830 const char *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002831 struct lysp_node_action *actions = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002832 struct tree_node_meta act_meta = {NULL, (struct lysp_node **)&actions};
Radek Iša56ca9e42020-09-08 18:42:00 +02002833 uint16_t flags;
David Sedlák85d0eca2019-07-24 15:15:21 +02002834
2835 /* max subelems */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002836 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlák85d0eca2019-07-24 15:15:21 +02002837 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002838 "<action name=\"act\">\n"
2839 " <description><text>desc</text></description>\n"
2840 " <grouping name=\"grouping\"/>\n"
2841 " <if-feature name=\"iff\"/>\n"
2842 " <input><uses name=\"uses-name\"/></input>\n"
2843 " <output><must condition=\"cond\"/><leaf name=\"l\"><type name=\"type\"/></leaf></output>\n"
2844 " <reference><text>ref</text></reference>\n"
2845 " <status value=\"deprecated\"/>\n"
2846 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2847 EXT_SUBELEM
2848 "</action>"
2849 ELEMENT_WRAPPER_END;
Michal Vasko1bf09392020-03-27 12:38:10 +01002850 /* there must be parent for action */
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002851 act_meta.parent = (void *)1;
Radek Iša56ca9e42020-09-08 18:42:00 +02002852 assert_int_equal(test_element_helper(state, data, &act_meta, NULL, NULL), LY_SUCCESS);
Michal Vasko1bf09392020-03-27 12:38:10 +01002853 act_meta.parent = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02002854 flags = LYS_STATUS_DEPRC;
2855 CHECK_LYSP_ACTION(actions, "desc", 1, flags, 1, 1,\
2856 1, 0, 0, 0,\
2857 1, 0,\
2858 "act", LYS_ACTION, \
2859 1, 0, 0, 1,\
2860 1, 0,\
2861 1, "ref", 1);
2862
Michal Vasko7f45cf22020-10-01 12:49:44 +02002863 assert_string_equal(actions->iffeatures[0].str, "iff");
David Sedlák85d0eca2019-07-24 15:15:21 +02002864 assert_string_equal(actions->typedefs->name, "tpdf");
2865 assert_string_equal(actions->groupings->name, "grouping");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002866 assert_string_equal(actions->output.musts->arg.str, "cond");
Radek Krejci01180ac2021-01-27 08:48:22 +01002867 assert_string_equal(actions->input.child->name, "uses-name");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002868 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(actions->exts[0]), LY_STMT_ACTION);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002869 lysp_node_free(&fctx, (struct lysp_node *)actions);
David Sedlák85d0eca2019-07-24 15:15:21 +02002870 actions = NULL;
2871
Michal Vasko8a67eff2021-12-07 14:04:47 +01002872 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlákeaa45792019-07-24 15:25:01 +02002873 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002874 "<rpc name=\"act\">\n"
2875 " <description><text>desc</text></description>\n"
2876 " <grouping name=\"grouping\"/>\n"
2877 " <if-feature name=\"iff\"/>\n"
2878 " <input><uses name=\"uses-name\"/></input>\n"
2879 " <output><must condition=\"cond\"/><leaf name=\"l\"><type name=\"type\"/></leaf></output>\n"
2880 " <reference><text>ref</text></reference>\n"
2881 " <status value=\"deprecated\"/>\n"
2882 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2883 EXT_SUBELEM
2884 "</rpc>"
2885 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002886 assert_int_equal(test_element_helper(state, data, &act_meta, NULL, NULL), LY_SUCCESS);
2887 flags = LYS_STATUS_DEPRC;
2888 CHECK_LYSP_ACTION(actions, "desc", 1, flags, 1, 1,\
2889 1, 0, 0, 0,\
2890 1, 0,\
2891 "act", LYS_RPC, \
2892 1, 0, 0, 1,\
2893 1, 0,\
2894 0, "ref", 1);
2895
Michal Vasko7f45cf22020-10-01 12:49:44 +02002896 assert_string_equal(actions->iffeatures[0].str, "iff");
David Sedlákeaa45792019-07-24 15:25:01 +02002897 assert_string_equal(actions->typedefs->name, "tpdf");
2898 assert_string_equal(actions->groupings->name, "grouping");
Radek Krejci01180ac2021-01-27 08:48:22 +01002899 assert_string_equal(actions->input.child->name, "uses-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002900 assert_string_equal(actions->output.musts->arg.str, "cond");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002901 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(actions->exts[0]), LY_STMT_RPC);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002902 lysp_node_free(&fctx, (struct lysp_node *)actions);
David Sedlákeaa45792019-07-24 15:25:01 +02002903 actions = NULL;
2904
David Sedlák85d0eca2019-07-24 15:15:21 +02002905 /* min subelems */
2906 data = ELEMENT_WRAPPER_START "<action name=\"act\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002907 assert_int_equal(test_element_helper(state, data, &act_meta, NULL, NULL), LY_SUCCESS);
David Sedlák85d0eca2019-07-24 15:15:21 +02002908 assert_string_equal(actions->name, "act");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002909 lysp_node_free(&fctx, (struct lysp_node *)actions);
David Sedlák85d0eca2019-07-24 15:15:21 +02002910 actions = NULL;
David Sedlák85d0eca2019-07-24 15:15:21 +02002911}
2912
David Sedlák992fb7c2019-07-24 16:51:01 +02002913static void
2914test_augment_elem(void **state)
2915{
David Sedlák992fb7c2019-07-24 16:51:01 +02002916 const char *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002917 struct lysp_node_augment *augments = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002918 struct tree_node_meta aug_meta = {NULL, (struct lysp_node **)&augments};
David Sedlák992fb7c2019-07-24 16:51:01 +02002919
Michal Vasko8a67eff2021-12-07 14:04:47 +01002920 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlák992fb7c2019-07-24 16:51:01 +02002921 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002922 "<augment target-node=\"target\">\n"
2923 " <action name=\"action\"/>\n"
2924 " <anydata name=\"anyd\"/>\n"
2925 " <anyxml name=\"anyx\"/>\n"
2926 " <case name=\"case\"/>\n"
2927 " <choice name=\"choice\"/>\n"
2928 " <container name=\"subcont\"/>\n"
2929 " <description><text>desc</text></description>\n"
2930 " <if-feature name=\"iff\"/>\n"
2931 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2932 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2933 " <list name=\"list\"/>\n"
2934 " <notification name=\"notif\"/>\n"
2935 " <reference><text>ref</text></reference>\n"
2936 " <status value=\"current\"/>\n"
2937 " <uses name=\"uses\"/>\n"
2938 " <when condition=\"when-cond\"/>\n"
2939 EXT_SUBELEM
2940 "</augment>"
2941 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002942 assert_int_equal(test_element_helper(state, data, &aug_meta, NULL, NULL), LY_SUCCESS);
David Sedlák992fb7c2019-07-24 16:51:01 +02002943 assert_string_equal(augments->nodeid, "target");
2944 assert_null(augments->parent);
2945 assert_int_equal(augments->nodetype, LYS_AUGMENT);
2946 assert_true(augments->flags & LYS_STATUS_CURR);
2947 assert_string_equal(augments->dsc, "desc");
2948 assert_string_equal(augments->ref, "ref");
2949 assert_string_equal(augments->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002950 assert_string_equal(augments->iffeatures[0].str, "iff");
David Sedlák992fb7c2019-07-24 16:51:01 +02002951 assert_string_equal(augments->child->name, "anyd");
2952 assert_int_equal(augments->child->nodetype, LYS_ANYDATA);
2953 assert_string_equal(augments->child->next->name, "anyx");
2954 assert_int_equal(augments->child->next->nodetype, LYS_ANYXML);
2955 assert_string_equal(augments->child->next->next->name, "case");
2956 assert_int_equal(augments->child->next->next->nodetype, LYS_CASE);
2957 assert_string_equal(augments->child->next->next->next->name, "choice");
2958 assert_int_equal(augments->child->next->next->next->nodetype, LYS_CHOICE);
2959 assert_string_equal(augments->child->next->next->next->next->name, "subcont");
2960 assert_int_equal(augments->child->next->next->next->next->nodetype, LYS_CONTAINER);
2961 assert_string_equal(augments->child->next->next->next->next->next->name, "leaf");
2962 assert_int_equal(augments->child->next->next->next->next->next->nodetype, LYS_LEAF);
2963 assert_string_equal(augments->child->next->next->next->next->next->next->name, "llist");
2964 assert_int_equal(augments->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2965 assert_string_equal(augments->child->next->next->next->next->next->next->next->name, "list");
2966 assert_int_equal(augments->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
2967 assert_string_equal(augments->child->next->next->next->next->next->next->next->next->name, "uses");
2968 assert_int_equal(augments->child->next->next->next->next->next->next->next->next->nodetype, LYS_USES);
2969 assert_null(augments->child->next->next->next->next->next->next->next->next->next);
2970 assert_string_equal(augments->actions->name, "action");
2971 assert_string_equal(augments->notifs->name, "notif");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002972 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(augments->exts[0]), LY_STMT_AUGMENT);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002973 lysp_node_free(&fctx, (struct lysp_node *)augments);
David Sedlák992fb7c2019-07-24 16:51:01 +02002974 augments = NULL;
2975
2976 data = ELEMENT_WRAPPER_START "<augment target-node=\"target\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002977 assert_int_equal(test_element_helper(state, data, &aug_meta, NULL, NULL), LY_SUCCESS);
David Sedlák992fb7c2019-07-24 16:51:01 +02002978 assert_string_equal(augments->nodeid, "target");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002979 lysp_node_free(&fctx, (struct lysp_node *)augments);
David Sedlák992fb7c2019-07-24 16:51:01 +02002980 augments = NULL;
David Sedlák992fb7c2019-07-24 16:51:01 +02002981}
2982
David Sedlák4ffcec82019-07-25 15:10:21 +02002983static void
2984test_deviate_elem(void **state)
2985{
David Sedlák4ffcec82019-07-25 15:10:21 +02002986 const char *data;
2987 struct lysp_deviate *deviates = NULL;
David Sedlák4ffcec82019-07-25 15:10:21 +02002988
2989 /* invalid arguments */
2990 data = ELEMENT_WRAPPER_START "<deviate value=\"\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002991 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
2992 CHECK_LOG_CTX("Invalid value \"\" of \"value\" attribute in \"deviate\" element. "
2993 "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", "Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02002994 deviates = NULL;
2995
2996 data = ELEMENT_WRAPPER_START "<deviate value=\"invalid\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002997 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
2998 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"deviate\" element. "
2999 "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", "Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003000 deviates = NULL;
3001
3002 data = ELEMENT_WRAPPER_START "<deviate value=\"ad\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003003 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
3004 CHECK_LOG_CTX("Invalid value \"ad\" of \"value\" attribute in \"deviate\" element. "
3005 "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", "Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003006 deviates = NULL;
3007
3008 data = ELEMENT_WRAPPER_START "<deviate value=\"adds\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003009 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
3010 CHECK_LOG_CTX("Invalid value \"adds\" of \"value\" attribute in \"deviate\" element. "
3011 "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", "Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003012 deviates = NULL;
3013
3014 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003015 "<deviate value=\"not-supported\">\n"
3016 " <must condition=\"c\"/>\n"
3017 "</deviate>"
3018 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003019 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
3020 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 +02003021}
3022
David Sedlák8b754462019-07-25 16:22:13 +02003023static void
3024test_deviation_elem(void **state)
3025{
David Sedlák8b754462019-07-25 16:22:13 +02003026 const char *data;
3027 struct lysp_deviation *deviations = NULL;
3028
David Sedlák8b754462019-07-25 16:22:13 +02003029 /* invalid */
3030 data = ELEMENT_WRAPPER_START "<deviation target-node=\"target\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003031 assert_int_equal(test_element_helper(state, data, &deviations, NULL, NULL), LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +02003032 CHECK_LOG_CTX("Missing mandatory sub-element \"deviate\" of \"deviation\" element.", "Line number 1.");
Michal Vasko12ef5362022-09-16 15:13:58 +02003033}
David Sedlák8b754462019-07-25 16:22:13 +02003034
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003035static struct lysp_module *
Michal Vaskod0625d72022-10-06 15:02:50 +02003036mod_renew(struct lysp_yin_ctx *ctx)
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003037{
Michal Vasko8a67eff2021-12-07 14:04:47 +01003038 struct ly_ctx *ly_ctx = PARSER_CUR_PMOD(ctx)->mod->ctx;
3039 struct lysp_module *pmod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003040
Michal Vaskoc636ea42022-09-16 10:20:31 +02003041 lys_module_free(&fctx, PARSER_CUR_PMOD(ctx)->mod, 0);
Michal Vasko8a67eff2021-12-07 14:04:47 +01003042 pmod = calloc(1, sizeof *pmod);
3043 ctx->parsed_mods->objs[0] = pmod;
3044 pmod->mod = calloc(1, sizeof *pmod->mod);
3045 pmod->mod->parsed = pmod;
3046 pmod->mod->ctx = ly_ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003047
Michal Vaskoc636ea42022-09-16 10:20:31 +02003048 fctx.mod = pmod->mod;
3049
Michal Vasko8a67eff2021-12-07 14:04:47 +01003050 return pmod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003051}
3052
David Sedlák4f03b932019-07-26 13:01:47 +02003053static void
3054test_module_elem(void **state)
3055{
Michal Vaskob36053d2020-03-26 15:49:30 +01003056 const char *data;
Radek Iša56ca9e42020-09-08 18:42:00 +02003057 struct lysp_module *lysp_mod = mod_renew(YCTX);
David Sedlák4f03b932019-07-26 13:01:47 +02003058
3059 /* max subelems */
David Sedlák4f03b932019-07-26 13:01:47 +02003060 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003061 " <yang-version value=\"1.1\"/>\n"
3062 " <namespace uri=\"ns\"/>\n"
3063 " <prefix value=\"pref\"/>\n"
3064 " <include module=\"b-mod\"/>\n"
3065 " <import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
3066 " <organization><text>org</text></organization>\n"
3067 " <contact><text>contact</text></contact>\n"
3068 " <description><text>desc</text></description>\n"
3069 " <reference><text>ref</text></reference>\n"
3070 " <revision date=\"2019-02-02\"/>\n"
3071 " <anydata name=\"anyd\"/>\n"
3072 " <anyxml name=\"anyx\"/>\n"
3073 " <choice name=\"choice\"/>\n"
3074 " <container name=\"cont\"/>\n"
3075 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3076 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3077 " <list name=\"sub-list\"/>\n"
3078 " <uses name=\"uses-name\"/>\n"
3079 " <augment target-node=\"target\"/>\n"
3080 " <deviation target-node=\"target\">\n"
3081 " <deviate value=\"not-supported\"/>\n"
3082 " </deviation>\n"
3083 " <extension name=\"ext\"/>\n"
3084 " <feature name=\"feature\"/>\n"
3085 " <grouping name=\"grp\"/>\n"
3086 " <identity name=\"ident-name\"/>\n"
3087 " <notification name=\"notf\"/>\n"
3088 " <rpc name=\"rpc-name\"/>\n"
3089 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3090 EXT_SUBELEM "\n"
3091 "</module>\n";
Radek Iša56ca9e42020-09-08 18:42:00 +02003092 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3093 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01003094
Radek Iša56ca9e42020-09-08 18:42:00 +02003095 assert_int_equal(yin_parse_mod(YCTX, lysp_mod), LY_SUCCESS);
David Sedlák4f03b932019-07-26 13:01:47 +02003096 assert_string_equal(lysp_mod->mod->name, "mod");
3097 assert_string_equal(lysp_mod->revs, "2019-02-02");
3098 assert_string_equal(lysp_mod->mod->ns, "ns");
3099 assert_string_equal(lysp_mod->mod->prefix, "pref");
3100 assert_null(lysp_mod->mod->filepath);
3101 assert_string_equal(lysp_mod->mod->org, "org");
3102 assert_string_equal(lysp_mod->mod->contact, "contact");
3103 assert_string_equal(lysp_mod->mod->dsc, "desc");
3104 assert_string_equal(lysp_mod->mod->ref, "ref");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003105 assert_int_equal(lysp_mod->version, LYS_VERSION_1_1);
Radek Iša56ca9e42020-09-08 18:42:00 +02003106 CHECK_LYSP_IMPORT(lysp_mod->imports, NULL, 0, "a-mod",
3107 "imp-pref", NULL, "");
David Sedlák4f03b932019-07-26 13:01:47 +02003108 assert_string_equal(lysp_mod->includes->name, "b-mod");
3109 assert_string_equal(lysp_mod->extensions->name, "ext");
3110 assert_string_equal(lysp_mod->features->name, "feature");
3111 assert_string_equal(lysp_mod->identities->name, "ident-name");
3112 assert_string_equal(lysp_mod->typedefs->name, "tpdf");
3113 assert_string_equal(lysp_mod->groupings->name, "grp");
3114 assert_string_equal(lysp_mod->data->name, "anyd");
3115 assert_int_equal(lysp_mod->data->nodetype, LYS_ANYDATA);
3116 assert_string_equal(lysp_mod->data->next->name, "anyx");
3117 assert_int_equal(lysp_mod->data->next->nodetype, LYS_ANYXML);
3118 assert_string_equal(lysp_mod->data->next->next->name, "choice");
3119 assert_int_equal(lysp_mod->data->next->next->nodetype, LYS_CHOICE);
3120 assert_string_equal(lysp_mod->data->next->next->next->name, "cont");
3121 assert_int_equal(lysp_mod->data->next->next->next->nodetype, LYS_CONTAINER);
3122 assert_string_equal(lysp_mod->data->next->next->next->next->name, "leaf");
3123 assert_int_equal(lysp_mod->data->next->next->next->next->nodetype, LYS_LEAF);
3124 assert_string_equal(lysp_mod->data->next->next->next->next->next->name, "llist");
3125 assert_int_equal(lysp_mod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3126 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->name, "sub-list");
3127 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
3128 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->next->name, "uses-name");
3129 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3130 assert_null(lysp_mod->data->next->next->next->next->next->next->next->next);
3131 assert_string_equal(lysp_mod->augments->nodeid, "target");
3132 assert_string_equal(lysp_mod->rpcs->name, "rpc-name");
3133 assert_string_equal(lysp_mod->notifs->name, "notf");
3134 assert_string_equal(lysp_mod->deviations->nodeid, "target");
Radek Krejci39b7fc22021-02-26 23:29:18 +01003135 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(lysp_mod->exts[0]), LY_STMT_MODULE);
David Sedlák4f03b932019-07-26 13:01:47 +02003136
3137 /* min subelems */
Radek Iša56ca9e42020-09-08 18:42:00 +02003138 ly_in_free(UTEST_IN, 0);
3139 lyxml_ctx_free(YCTX->xmlctx);
3140 lysp_mod = mod_renew(YCTX);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003141 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
3142 " <namespace uri=\"ns\"/>\n"
3143 " <prefix value=\"pref\"/>\n"
3144 " <yang-version value=\"1.1\"/>\n"
3145 "</module>";
Radek Iša56ca9e42020-09-08 18:42:00 +02003146 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3147 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
3148 assert_int_equal(yin_parse_mod(YCTX, lysp_mod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003149 assert_string_equal(lysp_mod->mod->name, "mod");
David Sedlák298ff6d2019-07-26 14:29:03 +02003150
David Sedláke6cd89e2019-08-07 12:46:02 +02003151 /* incorrect subelem order */
Radek Iša56ca9e42020-09-08 18:42:00 +02003152 ly_in_free(UTEST_IN, 0);
3153 lyxml_ctx_free(YCTX->xmlctx);
3154 lysp_mod = mod_renew(YCTX);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003155 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
3156 " <feature name=\"feature\"/>\n"
3157 " <namespace uri=\"ns\"/>\n"
3158 " <prefix value=\"pref\"/>\n"
3159 " <yang-version value=\"1.1\"/>\n"
3160 "</module>";
Radek Iša56ca9e42020-09-08 18:42:00 +02003161 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3162 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
3163 assert_int_equal(yin_parse_mod(YCTX, lysp_mod), LY_EVALID);
3164 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 +02003165}
3166
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003167static struct lysp_submodule *
Michal Vaskod0625d72022-10-06 15:02:50 +02003168submod_renew(struct lysp_yin_ctx *ctx, const char *belongs_to)
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003169{
Michal Vasko8a67eff2021-12-07 14:04:47 +01003170 struct ly_ctx *ly_ctx = PARSER_CUR_PMOD(ctx)->mod->ctx;
3171 struct lysp_submodule *submod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003172
Michal Vaskoc636ea42022-09-16 10:20:31 +02003173 lys_module_free(&fctx, PARSER_CUR_PMOD(ctx)->mod, 0);
Michal Vasko8a67eff2021-12-07 14:04:47 +01003174 submod = calloc(1, sizeof *submod);
3175 ctx->parsed_mods->objs[0] = submod;
3176 submod->mod = calloc(1, sizeof *submod->mod);
3177 lydict_insert(ly_ctx, belongs_to, 0, &submod->mod->name);
3178 submod->mod->parsed = (struct lysp_module *)submod;
3179 submod->mod->ctx = ly_ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003180
Michal Vaskoc636ea42022-09-16 10:20:31 +02003181 fctx.mod = submod->mod;
3182
Michal Vasko8a67eff2021-12-07 14:04:47 +01003183 return submod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003184}
3185
David Sedlák298ff6d2019-07-26 14:29:03 +02003186static void
3187test_submodule_elem(void **state)
3188{
Michal Vaskob36053d2020-03-26 15:49:30 +01003189 const char *data;
Radek Iša56ca9e42020-09-08 18:42:00 +02003190 struct lysp_submodule *lysp_submod = submod_renew(YCTX, "module-name");
David Sedlák298ff6d2019-07-26 14:29:03 +02003191
3192 /* max subelements */
David Sedlák298ff6d2019-07-26 14:29:03 +02003193 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003194 " <yang-version value=\"1.1\"/>\n"
3195 " <belongs-to module=\"module-name\">\n"
3196 " <prefix value=\"pref\"/>\n"
3197 " </belongs-to>\n"
3198 " <include module=\"b-mod\"/>\n"
3199 " <import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
3200 " <organization><text>org</text></organization>\n"
3201 " <contact><text>contact</text></contact>\n"
3202 " <description><text>desc</text></description>\n"
3203 " <reference><text>ref</text></reference>\n"
3204 " <revision date=\"2019-02-02\"/>\n"
3205 " <anydata name=\"anyd\"/>\n"
3206 " <anyxml name=\"anyx\"/>\n"
3207 " <choice name=\"choice\"/>\n"
3208 " <container name=\"cont\"/>\n"
3209 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3210 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3211 " <list name=\"sub-list\"/>\n"
3212 " <uses name=\"uses-name\"/>\n"
3213 " <augment target-node=\"target\"/>\n"
3214 " <deviation target-node=\"target\">\n"
3215 " <deviate value=\"not-supported\"/>\n"
3216 " </deviation>\n"
3217 " <extension name=\"ext\"/>\n"
3218 " <feature name=\"feature\"/>\n"
3219 " <grouping name=\"grp\"/>\n"
3220 " <identity name=\"ident-name\"/>\n"
3221 " <notification name=\"notf\"/>\n"
3222 " <rpc name=\"rpc-name\"/>\n"
3223 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3224 EXT_SUBELEM "\n"
3225 "</submodule>\n";
Radek Iša56ca9e42020-09-08 18:42:00 +02003226 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3227 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003228
Radek Iša56ca9e42020-09-08 18:42:00 +02003229 assert_int_equal(yin_parse_submod(YCTX, lysp_submod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003230 assert_string_equal(lysp_submod->name, "mod");
3231 assert_string_equal(lysp_submod->revs, "2019-02-02");
David Sedlák298ff6d2019-07-26 14:29:03 +02003232 assert_string_equal(lysp_submod->prefix, "pref");
3233 assert_null(lysp_submod->filepath);
3234 assert_string_equal(lysp_submod->org, "org");
3235 assert_string_equal(lysp_submod->contact, "contact");
3236 assert_string_equal(lysp_submod->dsc, "desc");
3237 assert_string_equal(lysp_submod->ref, "ref");
3238 assert_int_equal(lysp_submod->version, LYS_VERSION_1_1);
Radek Iša56ca9e42020-09-08 18:42:00 +02003239 CHECK_LYSP_IMPORT(lysp_submod->imports, NULL, 0, "a-mod",
3240 "imp-pref", NULL, "");
David Sedlák298ff6d2019-07-26 14:29:03 +02003241 assert_string_equal(lysp_submod->includes->name, "b-mod");
3242 assert_string_equal(lysp_submod->extensions->name, "ext");
3243 assert_string_equal(lysp_submod->features->name, "feature");
3244 assert_string_equal(lysp_submod->identities->name, "ident-name");
3245 assert_string_equal(lysp_submod->typedefs->name, "tpdf");
3246 assert_string_equal(lysp_submod->groupings->name, "grp");
3247 assert_string_equal(lysp_submod->data->name, "anyd");
3248 assert_int_equal(lysp_submod->data->nodetype, LYS_ANYDATA);
3249 assert_string_equal(lysp_submod->data->next->name, "anyx");
3250 assert_int_equal(lysp_submod->data->next->nodetype, LYS_ANYXML);
3251 assert_string_equal(lysp_submod->data->next->next->name, "choice");
3252 assert_int_equal(lysp_submod->data->next->next->nodetype, LYS_CHOICE);
3253 assert_string_equal(lysp_submod->data->next->next->next->name, "cont");
3254 assert_int_equal(lysp_submod->data->next->next->next->nodetype, LYS_CONTAINER);
3255 assert_string_equal(lysp_submod->data->next->next->next->next->name, "leaf");
3256 assert_int_equal(lysp_submod->data->next->next->next->next->nodetype, LYS_LEAF);
3257 assert_string_equal(lysp_submod->data->next->next->next->next->next->name, "llist");
3258 assert_int_equal(lysp_submod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3259 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->name, "sub-list");
3260 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
3261 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->next->name, "uses-name");
3262 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3263 assert_null(lysp_submod->data->next->next->next->next->next->next->next->next);
3264 assert_string_equal(lysp_submod->augments->nodeid, "target");
3265 assert_string_equal(lysp_submod->rpcs->name, "rpc-name");
3266 assert_string_equal(lysp_submod->notifs->name, "notf");
3267 assert_string_equal(lysp_submod->deviations->nodeid, "target");
Radek Krejci39b7fc22021-02-26 23:29:18 +01003268 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(lysp_submod->exts[0]), LY_STMT_SUBMODULE);
David Sedlák298ff6d2019-07-26 14:29:03 +02003269
David Sedlák298ff6d2019-07-26 14:29:03 +02003270 /* min subelemnts */
Radek Iša56ca9e42020-09-08 18:42:00 +02003271 ly_in_free(UTEST_IN, 0);
3272 lyxml_ctx_free(YCTX->xmlctx);
3273 lysp_submod = submod_renew(YCTX, "module-name");
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003274 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">\n"
3275 " <yang-version value=\"1\"/>\n"
3276 " <belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>\n"
3277 "</submodule>";
Radek Iša56ca9e42020-09-08 18:42:00 +02003278 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3279 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
3280 assert_int_equal(yin_parse_submod(YCTX, lysp_submod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003281 assert_string_equal(lysp_submod->prefix, "pref");
David Sedlák298ff6d2019-07-26 14:29:03 +02003282 assert_int_equal(lysp_submod->version, LYS_VERSION_1_0);
David Sedlák298ff6d2019-07-26 14:29:03 +02003283
David Sedláke6cd89e2019-08-07 12:46:02 +02003284 /* incorrect subelem order */
Radek Iša56ca9e42020-09-08 18:42:00 +02003285 ly_in_free(UTEST_IN, 0);
3286 lyxml_ctx_free(YCTX->xmlctx);
3287 lysp_submod = submod_renew(YCTX, "module-name");
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003288 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">\n"
3289 " <yang-version value=\"1\"/>\n"
3290 " <reference><text>ref</text></reference>\n"
3291 " <belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>\n"
3292 "</submodule>";
Radek Iša56ca9e42020-09-08 18:42:00 +02003293 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3294 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
3295 assert_int_equal(yin_parse_submod(YCTX, lysp_submod), LY_EVALID);
3296 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 +02003297}
3298
David Sedlák8985a142019-07-31 16:43:06 +02003299static void
3300test_yin_parse_module(void **state)
3301{
David Sedlák8985a142019-07-31 16:43:06 +02003302 const char *data;
3303 struct lys_module *mod;
Michal Vaskod0625d72022-10-06 15:02:50 +02003304 struct lysp_yin_ctx *yin_ctx = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02003305 struct ly_in *in = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003306
3307 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003308 mod->ctx = UTEST_LYCTX;
David Sedlákd2844882019-09-13 16:01:22 +02003309 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 +01003310 " <yang-version value=\"1.1\"/>\n"
3311 " <namespace uri=\"urn:tests:extensions:metadata:a\"/>\n"
3312 " <prefix value=\"a\"/>\n"
3313 " <import module=\"ietf-yang-metadata\">\n"
3314 " <prefix value=\"md\"/>\n"
3315 " </import>\n"
3316 " <feature name=\"f\"/>\n"
3317 " <md:annotation name=\"x\">\n"
3318 " <description>\n"
3319 " <text>test</text>\n"
3320 " </description>\n"
3321 " <reference>\n"
3322 " <text>test</text>\n"
3323 " </reference>\n"
3324 " <if-feature name=\"f\"/>\n"
3325 " <status value=\"current\"/>\n"
3326 " <type name=\"uint8\"/>\n"
3327 " <units name=\"meters\"/>\n"
3328 " </md:annotation>\n"
3329 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003330 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
aPiecekc3e26142021-06-22 14:25:49 +02003331 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS);
David Sedlákd2844882019-09-13 16:01:22 +02003332 assert_null(mod->parsed->exts->child->next->child);
3333 assert_string_equal(mod->parsed->exts->child->next->arg, "test");
Michal Vaskoc636ea42022-09-16 10:20:31 +02003334 lys_module_free(&fctx, mod, 0);
Michal Vaskod0625d72022-10-06 15:02:50 +02003335 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003336 ly_in_free(in, 0);
David Sedlákd2844882019-09-13 16:01:22 +02003337 mod = NULL;
3338 yin_ctx = NULL;
3339
3340 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003341 mod->ctx = UTEST_LYCTX;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003342 data = "<module name=\"example-foo\""
3343 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
3344 " xmlns:foo=\"urn:example:foo\""
3345 " xmlns:myext=\"urn:example:extensions\">\n"
David Sedlák8985a142019-07-31 16:43:06 +02003346
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003347 " <yang-version value=\"1\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003348
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003349 " <namespace uri=\"urn:example:foo\"/>\n"
3350 " <prefix value=\"foo\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003351
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003352 " <import module=\"example-extensions\">\n"
3353 " <prefix value=\"myext\"/>\n"
3354 " </import>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003355
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003356 " <list name=\"interface\">\n"
3357 " <key value=\"name\"/>\n"
3358 " <leaf name=\"name\">\n"
3359 " <type name=\"string\"/>\n"
3360 " </leaf>\n"
3361 " <leaf name=\"mtu\">\n"
3362 " <type name=\"uint32\"/>\n"
3363 " <description>\n"
3364 " <text>The MTU of the interface.</text>\n"
3365 " </description>\n"
3366 " <myext:c-define name=\"MY_MTU\"/>\n"
3367 " </leaf>\n"
3368 " </list>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003369 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003370 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
aPiecekc3e26142021-06-22 14:25:49 +02003371 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS);
Michal Vaskoc636ea42022-09-16 10:20:31 +02003372 lys_module_free(&fctx, mod, 0);
Michal Vaskod0625d72022-10-06 15:02:50 +02003373 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003374 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02003375 mod = NULL;
3376 yin_ctx = NULL;
3377
3378 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003379 mod->ctx = UTEST_LYCTX;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003380 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3381 " <yang-version value=\"1\"/>\n"
3382 " <namespace uri=\"urn:example:foo\"/>\n"
3383 " <prefix value=\"foo\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003384 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003385 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
aPiecekc3e26142021-06-22 14:25:49 +02003386 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS);
Michal Vaskoc636ea42022-09-16 10:20:31 +02003387 lys_module_free(&fctx, mod, 0);
Michal Vaskod0625d72022-10-06 15:02:50 +02003388 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003389 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02003390 mod = NULL;
3391 yin_ctx = NULL;
3392
David Sedlák6d781b62019-08-02 15:22:52 +02003393 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003394 mod->ctx = UTEST_LYCTX;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003395 data = "<submodule name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlák8985a142019-07-31 16:43:06 +02003396 "</submodule>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003397 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
aPiecekc3e26142021-06-22 14:25:49 +02003398 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_EINVAL);
Radek Iša56ca9e42020-09-08 18:42:00 +02003399 CHECK_LOG_CTX("Input data contains submodule which cannot be parsed directly without its main module.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02003400 lys_module_free(&fctx, mod, 0);
Michal Vaskod0625d72022-10-06 15:02:50 +02003401 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003402 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02003403
David Sedlák6d781b62019-08-02 15:22:52 +02003404 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003405 mod->ctx = UTEST_LYCTX;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003406 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3407 " <yang-version value=\"1\"/>\n"
3408 " <namespace uri=\"urn:example:foo\"/>\n"
3409 " <prefix value=\"foo\"/>\n"
3410 "</module>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003411 "<module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003412 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
aPiecekc3e26142021-06-22 14:25:49 +02003413 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +02003414 CHECK_LOG_CTX("Trailing garbage \"<module>\" after module, expected end-of-input.", "Line number 6.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02003415 lys_module_free(&fctx, mod, 0);
Michal Vaskod0625d72022-10-06 15:02:50 +02003416 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003417 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02003418 mod = NULL;
3419 yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003420}
3421
3422static void
3423test_yin_parse_submodule(void **state)
3424{
David Sedlák8985a142019-07-31 16:43:06 +02003425 const char *data;
Michal Vaskod0625d72022-10-06 15:02:50 +02003426 struct lysp_yin_ctx *yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003427 struct lysp_submodule *submod = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02003428 struct ly_in *in;
David Sedlák8985a142019-07-31 16:43:06 +02003429
Michal Vasko8a67eff2021-12-07 14:04:47 +01003430 lydict_insert(UTEST_LYCTX, "a", 0, &PARSER_CUR_PMOD(YCTX)->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02003431
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003432 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003433 "<submodule name=\"asub\""
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003434 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
3435 " xmlns:a=\"urn:a\">\n"
3436 " <yang-version value=\"1\"/>\n"
3437 " <belongs-to module=\"a\">\n"
3438 " <prefix value=\"a_pref\"/>\n"
3439 " </belongs-to>\n"
3440 " <include module=\"atop\"/>\n"
3441 " <feature name=\"fox\"/>\n"
3442 " <notification name=\"bar-notif\">\n"
3443 " <if-feature name=\"bar\"/>\n"
3444 " </notification>\n"
3445 " <notification name=\"fox-notif\">\n"
3446 " <if-feature name=\"fox\"/>\n"
3447 " </notification>\n"
3448 " <augment target-node=\"/a_pref:top\">\n"
3449 " <if-feature name=\"bar\"/>\n"
3450 " <container name=\"bar-sub\"/>\n"
3451 " </augment>\n"
3452 " <augment target-node=\"/top\">\n"
3453 " <container name=\"bar-sub2\"/>\n"
3454 " </augment>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003455 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003456 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vaskod0625d72022-10-06 15:02:50 +02003457 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 +02003458 lysp_module_free(&fctx, (struct lysp_module *)submod);
Michal Vaskod0625d72022-10-06 15:02:50 +02003459 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003460 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02003461 yin_ctx = NULL;
3462 submod = NULL;
3463
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003464 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3465 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3466 " <yang-version value=\"1\"/>\n"
3467 " <belongs-to module=\"a\">\n"
3468 " <prefix value=\"a_pref\"/>\n"
3469 " </belongs-to>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003470 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003471 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vaskod0625d72022-10-06 15:02:50 +02003472 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 +02003473 lysp_module_free(&fctx, (struct lysp_module *)submod);
Michal Vaskod0625d72022-10-06 15:02:50 +02003474 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003475 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02003476 yin_ctx = NULL;
3477 submod = NULL;
3478
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003479 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003480 "<module name=\"inval\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
3481 "</module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003482 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vaskod0625d72022-10-06 15:02:50 +02003483 assert_int_equal(yin_parse_submodule(&yin_ctx, UTEST_LYCTX, (struct lysp_ctx *)YCTX, in, &submod), LY_EINVAL);
Michal Vasko193dacd2022-10-13 08:43:05 +02003484 CHECK_LOG_CTX("Input data contains module when a submodule is expected.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02003485 lysp_module_free(&fctx, (struct lysp_module *)submod);
Michal Vaskod0625d72022-10-06 15:02:50 +02003486 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003487 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02003488 yin_ctx = NULL;
3489 submod = NULL;
3490
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003491 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3492 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3493 " <yang-version value=\"1\"/>\n"
3494 " <belongs-to module=\"a\">\n"
3495 " <prefix value=\"a_pref\"/>\n"
3496 " </belongs-to>\n"
3497 "</submodule>\n"
3498 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3499 " <yang-version value=\"1\"/>\n"
3500 " <belongs-to module=\"a\">\n"
3501 " <prefix value=\"a_pref\"/>\n"
3502 " </belongs-to>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003503 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003504 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vaskod0625d72022-10-06 15:02:50 +02003505 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 +02003506 CHECK_LOG_CTX("Trailing garbage \"<submodule name...\" after submodule, expected end-of-input.", "Line number 8.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02003507 lysp_module_free(&fctx, (struct lysp_module *)submod);
Michal Vaskod0625d72022-10-06 15:02:50 +02003508 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003509 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02003510 yin_ctx = NULL;
3511 submod = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003512}
3513
David Sedlák3b4db242018-10-19 16:11:01 +02003514int
3515main(void)
3516{
3517
3518 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +02003519 UTEST(test_yin_match_keyword, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003520 UTEST(test_yin_parse_content, setup, teardown),
3521 UTEST(test_validate_value, setup, teardown),
Michal Vasko12ef5362022-09-16 15:13:58 +02003522 UTEST(test_valid_module),
3523 UTEST(test_print_module),
3524 UTEST(test_print_submodule),
David Sedlák32488102019-07-15 17:44:10 +02003525
Radek Iša56ca9e42020-09-08 18:42:00 +02003526 UTEST(test_yin_match_argument_name),
3527 cmocka_unit_test_setup_teardown(test_enum_elem, setup, teardown),
3528 cmocka_unit_test_setup_teardown(test_bit_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003529 cmocka_unit_test_setup_teardown(test_status_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003530 cmocka_unit_test_setup_teardown(test_yin_element_elem, setup, teardown),
3531 cmocka_unit_test_setup_teardown(test_yangversion_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003532 cmocka_unit_test_setup_teardown(test_argument_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003533 cmocka_unit_test_setup_teardown(test_belongsto_elem, setup, teardown),
3534 cmocka_unit_test_setup_teardown(test_config_elem, setup, teardown),
3535 cmocka_unit_test_setup_teardown(test_default_elem, setup, teardown),
3536 cmocka_unit_test_setup_teardown(test_err_app_tag_elem, setup, teardown),
3537 cmocka_unit_test_setup_teardown(test_err_msg_elem, setup, teardown),
3538 cmocka_unit_test_setup_teardown(test_fracdigits_elem, setup, teardown),
3539 cmocka_unit_test_setup_teardown(test_iffeature_elem, setup, teardown),
3540 cmocka_unit_test_setup_teardown(test_length_elem, setup, teardown),
3541 cmocka_unit_test_setup_teardown(test_modifier_elem, setup, teardown),
3542 cmocka_unit_test_setup_teardown(test_namespace_elem, setup, teardown),
3543 cmocka_unit_test_setup_teardown(test_pattern_elem, setup, teardown),
3544 cmocka_unit_test_setup_teardown(test_value_position_elem, setup, teardown),
3545 cmocka_unit_test_setup_teardown(test_prefix_elem, setup, teardown),
3546 cmocka_unit_test_setup_teardown(test_range_elem, setup, teardown),
3547 cmocka_unit_test_setup_teardown(test_reqinstance_elem, setup, teardown),
3548 cmocka_unit_test_setup_teardown(test_revision_date_elem, setup, teardown),
3549 cmocka_unit_test_setup_teardown(test_unique_elem, setup, teardown),
3550 cmocka_unit_test_setup_teardown(test_units_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003551 cmocka_unit_test_setup_teardown(test_yin_text_value_elem, setup, teardown),
3552 cmocka_unit_test_setup_teardown(test_type_elem, setup, teardown),
3553 cmocka_unit_test_setup_teardown(test_max_elems_elem, setup, teardown),
3554 cmocka_unit_test_setup_teardown(test_min_elems_elem, setup, teardown),
3555 cmocka_unit_test_setup_teardown(test_ordby_elem, setup, teardown),
3556 cmocka_unit_test_setup_teardown(test_any_elem, setup, teardown),
3557 cmocka_unit_test_setup_teardown(test_leaf_elem, setup, teardown),
3558 cmocka_unit_test_setup_teardown(test_leaf_list_elem, setup, teardown),
3559 cmocka_unit_test_setup_teardown(test_presence_elem, setup, teardown),
3560 cmocka_unit_test_setup_teardown(test_key_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003561 cmocka_unit_test_setup_teardown(test_uses_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003562 cmocka_unit_test_setup_teardown(test_list_elem, setup, teardown),
3563 cmocka_unit_test_setup_teardown(test_notification_elem, setup, teardown),
3564 cmocka_unit_test_setup_teardown(test_grouping_elem, setup, teardown),
3565 cmocka_unit_test_setup_teardown(test_container_elem, setup, teardown),
3566 cmocka_unit_test_setup_teardown(test_case_elem, setup, teardown),
3567 cmocka_unit_test_setup_teardown(test_choice_elem, setup, teardown),
3568 cmocka_unit_test_setup_teardown(test_inout_elem, setup, teardown),
3569 cmocka_unit_test_setup_teardown(test_action_elem, setup, teardown),
3570 cmocka_unit_test_setup_teardown(test_augment_elem, setup, teardown),
3571 cmocka_unit_test_setup_teardown(test_deviate_elem, setup, teardown),
3572 cmocka_unit_test_setup_teardown(test_deviation_elem, setup, teardown),
3573 cmocka_unit_test_setup_teardown(test_module_elem, setup, teardown),
3574 cmocka_unit_test_setup_teardown(test_submodule_elem, setup, teardown),
David Sedlák8985a142019-07-31 16:43:06 +02003575
Radek Iša56ca9e42020-09-08 18:42:00 +02003576 cmocka_unit_test_setup_teardown(test_yin_parse_module, setup, teardown),
3577 cmocka_unit_test_setup_teardown(test_yin_parse_submodule, setup, teardown),
David Sedlák3b4db242018-10-19 16:11:01 +02003578 };
3579
Radek Iša56ca9e42020-09-08 18:42:00 +02003580 return cmocka_run_group_tests(tests, NULL, NULL);
David Sedlák3b4db242018-10-19 16:11:01 +02003581}