blob: 3099cacffec8ed2baada3fdc13c9484fb9681db4 [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));
Michal Vaskoc8806c82022-12-06 10:31:24 +0100122 YCTX->main_ctx = (struct lysp_ctx *)YCTX;
Radek Iša56ca9e42020-09-08 18:42:00 +0200123 YCTX->format = LYS_IN_YIN;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100124 ly_set_new(&YCTX->parsed_mods);
David Sedlák8f5bce02019-06-03 16:41:08 +0200125
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200126 /* allocate new parsed module */
Michal Vasko8a67eff2021-12-07 14:04:47 +0100127 pmod = calloc(1, sizeof *pmod);
128 ly_set_add(YCTX->parsed_mods, pmod, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200129
130 /* allocate new module */
Michal Vasko8a67eff2021-12-07 14:04:47 +0100131 pmod->mod = calloc(1, sizeof *pmod->mod);
132 pmod->mod->ctx = UTEST_LYCTX;
133 pmod->mod->parsed = pmod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200134
Radek Iša56ca9e42020-09-08 18:42:00 +0200135 return 0;
David Sedlák3b4db242018-10-19 16:11:01 +0200136}
137
138static int
Radek Iša56ca9e42020-09-08 18:42:00 +0200139setup(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +0100140{
Radek Iša56ca9e42020-09-08 18:42:00 +0200141 UTEST_SETUP;
David Sedlák68a1af12019-03-08 13:46:54 +0100142
Radek Iša56ca9e42020-09-08 18:42:00 +0200143 setup_ctx(state);
David Sedlák79e50cb2019-06-05 16:33:09 +0200144
Michal Vaskoc636ea42022-09-16 10:20:31 +0200145 fctx.ctx = UTEST_LYCTX;
146 fctx.mod = PARSER_CUR_PMOD(YCTX)->mod;
147
Radek Iša56ca9e42020-09-08 18:42:00 +0200148 return 0;
David Sedlák79e50cb2019-06-05 16:33:09 +0200149}
150
David Sedlák8985a142019-07-31 16:43:06 +0200151static int
Radek Iša56ca9e42020-09-08 18:42:00 +0200152teardown_ctx(void **UNUSED(state))
David Sedlák8985a142019-07-31 16:43:06 +0200153{
Michal Vaskoc636ea42022-09-16 10:20:31 +0200154 lys_module_free(&fctx, PARSER_CUR_PMOD(YCTX)->mod, 0);
Michal Vaskod0625d72022-10-06 15:02:50 +0200155 lysp_yin_ctx_free(YCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200156 YCTX = NULL;
David Sedlák8985a142019-07-31 16:43:06 +0200157
Radek Iša56ca9e42020-09-08 18:42:00 +0200158 return 0;
David Sedlák8985a142019-07-31 16:43:06 +0200159}
160
Radek Iša56ca9e42020-09-08 18:42:00 +0200161static int
162teardown(void **state)
163{
164 teardown_ctx(state);
165
Michal Vaskoc636ea42022-09-16 10:20:31 +0200166 lysf_ctx_erase(&fctx);
167
Radek Iša56ca9e42020-09-08 18:42:00 +0200168 UTEST_TEARDOWN;
169
170 return 0;
171}
172
173#define RESET_STATE \
174 ly_in_free(UTEST_IN, 0); \
175 UTEST_IN = NULL; \
176 teardown_ctx(state); \
177 setup_ctx(state)
178
David Sedlák68a1af12019-03-08 13:46:54 +0100179static void
David Sedlák1bccdfa2019-06-17 15:55:27 +0200180test_yin_match_keyword(void **state)
David Sedlák3b4db242018-10-19 16:11:01 +0200181{
Michal Vaskob36053d2020-03-26 15:49:30 +0100182 const char *prefix;
183 size_t prefix_len;
Radek Iša56ca9e42020-09-08 18:42:00 +0200184
David Sedlák8f7a1172019-06-20 14:42:18 +0200185 /* create mock yin namespace in xml context */
Radek Iša56ca9e42020-09-08 18:42:00 +0200186 ly_in_new_memory("<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" />", &UTEST_IN);
187 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
188 prefix = YCTX->xmlctx->prefix;
189 prefix_len = YCTX->xmlctx->prefix_len;
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100190
Radek Iša56ca9e42020-09-08 18:42:00 +0200191 assert_int_equal(yin_match_keyword(YCTX, "anydatax", strlen("anydatax"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
192 assert_int_equal(yin_match_keyword(YCTX, "asdasd", strlen("asdasd"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
193 assert_int_equal(yin_match_keyword(YCTX, "", 0, prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
194 assert_int_equal(yin_match_keyword(YCTX, "anydata", strlen("anydata"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ANYDATA);
195 assert_int_equal(yin_match_keyword(YCTX, "anyxml", strlen("anyxml"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ANYXML);
196 assert_int_equal(yin_match_keyword(YCTX, "argument", strlen("argument"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ARGUMENT);
197 assert_int_equal(yin_match_keyword(YCTX, "augment", strlen("augment"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_AUGMENT);
198 assert_int_equal(yin_match_keyword(YCTX, "base", strlen("base"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BASE);
199 assert_int_equal(yin_match_keyword(YCTX, "belongs-to", strlen("belongs-to"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BELONGS_TO);
200 assert_int_equal(yin_match_keyword(YCTX, "bit", strlen("bit"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BIT);
201 assert_int_equal(yin_match_keyword(YCTX, "case", strlen("case"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CASE);
202 assert_int_equal(yin_match_keyword(YCTX, "choice", strlen("choice"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CHOICE);
203 assert_int_equal(yin_match_keyword(YCTX, "config", strlen("config"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONFIG);
204 assert_int_equal(yin_match_keyword(YCTX, "contact", strlen("contact"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONTACT);
205 assert_int_equal(yin_match_keyword(YCTX, "container", strlen("container"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONTAINER);
206 assert_int_equal(yin_match_keyword(YCTX, "default", strlen("default"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEFAULT);
207 assert_int_equal(yin_match_keyword(YCTX, "description", strlen("description"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DESCRIPTION);
208 assert_int_equal(yin_match_keyword(YCTX, "deviate", strlen("deviate"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEVIATE);
209 assert_int_equal(yin_match_keyword(YCTX, "deviation", strlen("deviation"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEVIATION);
210 assert_int_equal(yin_match_keyword(YCTX, "enum", strlen("enum"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ENUM);
211 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);
212 assert_int_equal(yin_match_keyword(YCTX, "error-message", strlen("error-message"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ERROR_MESSAGE);
213 assert_int_equal(yin_match_keyword(YCTX, "extension", strlen("extension"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_EXTENSION);
214 assert_int_equal(yin_match_keyword(YCTX, "feature", strlen("feature"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_FEATURE);
215 assert_int_equal(yin_match_keyword(YCTX, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_FRACTION_DIGITS);
216 assert_int_equal(yin_match_keyword(YCTX, "grouping", strlen("grouping"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_GROUPING);
217 assert_int_equal(yin_match_keyword(YCTX, "identity", strlen("identity"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IDENTITY);
218 assert_int_equal(yin_match_keyword(YCTX, "if-feature", strlen("if-feature"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IF_FEATURE);
219 assert_int_equal(yin_match_keyword(YCTX, "import", strlen("import"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IMPORT);
220 assert_int_equal(yin_match_keyword(YCTX, "include", strlen("include"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_INCLUDE);
221 assert_int_equal(yin_match_keyword(YCTX, "input", strlen("input"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_INPUT);
222 assert_int_equal(yin_match_keyword(YCTX, "key", strlen("key"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_KEY);
223 assert_int_equal(yin_match_keyword(YCTX, "leaf", strlen("leaf"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LEAF);
224 assert_int_equal(yin_match_keyword(YCTX, "leaf-list", strlen("leaf-list"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LEAF_LIST);
225 assert_int_equal(yin_match_keyword(YCTX, "length", strlen("length"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LENGTH);
226 assert_int_equal(yin_match_keyword(YCTX, "list", strlen("list"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LIST);
227 assert_int_equal(yin_match_keyword(YCTX, "mandatory", strlen("mandatory"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MANDATORY);
228 assert_int_equal(yin_match_keyword(YCTX, "max-elements", strlen("max-elements"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MAX_ELEMENTS);
229 assert_int_equal(yin_match_keyword(YCTX, "min-elements", strlen("min-elements"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MIN_ELEMENTS);
230 assert_int_equal(yin_match_keyword(YCTX, "modifier", strlen("modifier"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MODIFIER);
231 assert_int_equal(yin_match_keyword(YCTX, "module", strlen("module"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MODULE);
232 assert_int_equal(yin_match_keyword(YCTX, "must", strlen("must"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MUST);
233 assert_int_equal(yin_match_keyword(YCTX, "namespace", strlen("namespace"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NAMESPACE);
234 assert_int_equal(yin_match_keyword(YCTX, "notification", strlen("notification"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NOTIFICATION);
235 assert_int_equal(yin_match_keyword(YCTX, "ordered-by", strlen("ordered-by"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ORDERED_BY);
236 assert_int_equal(yin_match_keyword(YCTX, "organization", strlen("organization"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ORGANIZATION);
237 assert_int_equal(yin_match_keyword(YCTX, "output", strlen("output"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_OUTPUT);
238 assert_int_equal(yin_match_keyword(YCTX, "path", strlen("path"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PATH);
239 assert_int_equal(yin_match_keyword(YCTX, "pattern", strlen("pattern"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PATTERN);
240 assert_int_equal(yin_match_keyword(YCTX, "position", strlen("position"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_POSITION);
241 assert_int_equal(yin_match_keyword(YCTX, "prefix", strlen("prefix"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PREFIX);
242 assert_int_equal(yin_match_keyword(YCTX, "presence", strlen("presence"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PRESENCE);
243 assert_int_equal(yin_match_keyword(YCTX, "range", strlen("range"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_RANGE);
244 assert_int_equal(yin_match_keyword(YCTX, "reference", strlen("reference"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REFERENCE);
245 assert_int_equal(yin_match_keyword(YCTX, "refine", strlen("refine"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REFINE);
246 assert_int_equal(yin_match_keyword(YCTX, "require-instance", strlen("require-instance"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REQUIRE_INSTANCE);
247 assert_int_equal(yin_match_keyword(YCTX, "revision", strlen("revision"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REVISION);
248 assert_int_equal(yin_match_keyword(YCTX, "revision-date", strlen("revision-date"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REVISION_DATE);
249 assert_int_equal(yin_match_keyword(YCTX, "rpc", strlen("rpc"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_RPC);
250 assert_int_equal(yin_match_keyword(YCTX, "status", strlen("status"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_STATUS);
251 assert_int_equal(yin_match_keyword(YCTX, "submodule", strlen("submodule"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_SUBMODULE);
252 assert_int_equal(yin_match_keyword(YCTX, "type", strlen("type"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_TYPE);
253 assert_int_equal(yin_match_keyword(YCTX, "typedef", strlen("typedef"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_TYPEDEF);
254 assert_int_equal(yin_match_keyword(YCTX, "unique", strlen("unique"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_UNIQUE);
255 assert_int_equal(yin_match_keyword(YCTX, "units", strlen("units"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_UNITS);
256 assert_int_equal(yin_match_keyword(YCTX, "uses", strlen("uses"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_USES);
257 assert_int_equal(yin_match_keyword(YCTX, "value", strlen("value"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_VALUE);
258 assert_int_equal(yin_match_keyword(YCTX, "when", strlen("when"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_WHEN);
259 assert_int_equal(yin_match_keyword(YCTX, "yang-version", strlen("yang-version"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_YANG_VERSION);
260 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 +0200261}
David Sedlák3b4db242018-10-19 16:11:01 +0200262
David Sedlák872c7b42018-10-26 13:15:20 +0200263static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200264test_yin_match_argument_name(void **UNUSED(state))
David Sedlák872c7b42018-10-26 13:15:20 +0200265{
David Sedlák060b00e2019-06-19 11:12:06 +0200266 assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN);
267 assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN);
268 assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN);
269 assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION);
270 assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE);
271 assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE);
272 assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME);
273 assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG);
274 assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
275 assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT);
276 assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI);
277 assert_int_equal(yin_match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200278}
279
David Sedlák68a1af12019-03-08 13:46:54 +0100280static void
David Sedlák555c7202019-07-04 12:14:12 +0200281test_yin_parse_content(void **state)
282{
David Sedlák555c7202019-07-04 12:14:12 +0200283 LY_ERR ret = LY_SUCCESS;
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100284 const char *data =
285 "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
286 " <myext:custom xmlns:myext=\"urn:example:extensions\">totally amazing extension</myext:custom>\n"
287 " <extension name=\"ext\">\n"
288 " <argument name=\"argname\"></argument>\n"
289 " <description><text>desc</text></description>\n"
290 " <reference><text>ref</text></reference>\n"
291 " <status value=\"deprecated\"></status>\n"
292 " </extension>\n"
293 " <text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>\n"
294 " <if-feature name=\"foo\"></if-feature>\n"
295 " <when condition=\"condition...\">\n"
296 " <reference><text>when_ref</text></reference>\n"
297 " <description><text>when_desc</text></description>\n"
298 " </when>\n"
299 " <config value=\"true\"/>\n"
300 " <error-message>\n"
301 " <value>error-msg</value>\n"
302 " </error-message>\n"
303 " <error-app-tag value=\"err-app-tag\"/>\n"
304 " <units name=\"radians\"></units>\n"
305 " <default value=\"default-value\"/>\n"
306 " <position value=\"25\"></position>\n"
307 " <value value=\"-5\"/>\n"
308 " <require-instance value=\"true\"></require-instance>\n"
309 " <range value=\"5..10\" />\n"
310 " <length value=\"baf\"/>\n"
311 " <pattern value='pattern'>\n"
312 " <modifier value='invert-match'/>\n"
313 " </pattern>\n"
314 " <enum name=\"yay\">\n"
315 " </enum>\n"
316 "</prefix>";
David Sedlák555c7202019-07-04 12:14:12 +0200317 struct lysp_ext_instance *exts = NULL;
Michal Vasko12ef5362022-09-16 15:13:58 +0200318 const char *value;
David Sedlák555c7202019-07-04 12:14:12 +0200319
320 /* test unique subelem */
321 const char *prefix_value;
Radek Krejcid6b76452019-09-03 17:03:03 +0200322 struct yin_subelement subelems2[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE},
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100323 {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_UNIQUE}};
324
David Sedláke6cd89e2019-08-07 12:46:02 +0200325 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100326 "<prefix value=\"inv_mod\" />"
327 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
328 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
329 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200330 ly_in_new_memory(data, &UTEST_IN);
331 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
332 lyxml_ctx_next(YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100333
Michal Vasko193dacd2022-10-13 08:43:05 +0200334 ret = yin_parse_content(YCTX, subelems2, 2, NULL, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200335 assert_int_equal(ret, LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +0200336 CHECK_LOG_CTX("Redefinition of \"text\" sub-element in \"status\" element.", "Line number 1.");
337 lydict_remove(UTEST_LYCTX, prefix_value);
338 lydict_remove(UTEST_LYCTX, value);
339 RESET_STATE;
David Sedlák555c7202019-07-04 12:14:12 +0200340
341 /* test first subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200342 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100343 "<prefix value=\"inv_mod\" />"
344 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
345 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
346 ELEMENT_WRAPPER_END;
Radek Krejcid6b76452019-09-03 17:03:03 +0200347 struct yin_subelement subelems3[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE},
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100348 {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_FIRST}};
349
Radek Iša56ca9e42020-09-08 18:42:00 +0200350 ly_in_new_memory(data, &UTEST_IN);
351 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
352 lyxml_ctx_next(YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100353
Michal Vasko193dacd2022-10-13 08:43:05 +0200354 ret = yin_parse_content(YCTX, subelems3, 2, NULL, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200355 assert_int_equal(ret, LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +0200356 CHECK_LOG_CTX("Sub-element \"text\" of \"status\" element must be defined as it's first sub-element.", "Line number 1.");
357 lydict_remove(UTEST_LYCTX, prefix_value);
358 RESET_STATE;
David Sedlák555c7202019-07-04 12:14:12 +0200359
360 /* test mandatory subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200361 data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END;
Radek Krejcid6b76452019-09-03 17:03:03 +0200362 struct yin_subelement subelems4[1] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}};
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100363
Radek Iša56ca9e42020-09-08 18:42:00 +0200364 ly_in_new_memory(data, &UTEST_IN);
365 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
366 lyxml_ctx_next(YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100367
Michal Vasko193dacd2022-10-13 08:43:05 +0200368 ret = yin_parse_content(YCTX, subelems4, 1, NULL, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200369 assert_int_equal(ret, LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +0200370 CHECK_LOG_CTX("Missing mandatory sub-element \"prefix\" of \"status\" element.", "Line number 1.");
David Sedlák555c7202019-07-04 12:14:12 +0200371}
372
David Sedlák92147b02019-07-09 14:01:01 +0200373static void
David Sedlák4a650532019-07-10 11:55:18 +0200374test_validate_value(void **state)
375{
Michal Vaskob36053d2020-03-26 15:49:30 +0100376 const char *data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END;
377
378 /* create some XML context */
Radek Iša56ca9e42020-09-08 18:42:00 +0200379 ly_in_new_memory(data, &UTEST_IN);
380 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
381 YCTX->xmlctx->status = LYXML_ELEM_CONTENT;
382 YCTX->xmlctx->dynamic = 0;
Michal Vaskob36053d2020-03-26 15:49:30 +0100383
Radek Iša56ca9e42020-09-08 18:42:00 +0200384 YCTX->xmlctx->value = "#invalid";
385 YCTX->xmlctx->value_len = 8;
386 assert_int_equal(yin_validate_value(YCTX, Y_IDENTIF_ARG), LY_EVALID);
Radek Iša2eac74c2021-03-15 15:53:08 +0100387 CHECK_LOG_CTX("Invalid identifier first character '#' (0x0023).", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100388
Radek Iša56ca9e42020-09-08 18:42:00 +0200389 YCTX->xmlctx->value = "";
390 YCTX->xmlctx->value_len = 0;
391 assert_int_equal(yin_validate_value(YCTX, Y_STR_ARG), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +0100392
Radek Iša56ca9e42020-09-08 18:42:00 +0200393 YCTX->xmlctx->value = "pre:b";
394 YCTX->xmlctx->value_len = 5;
395 assert_int_equal(yin_validate_value(YCTX, Y_IDENTIF_ARG), LY_EVALID);
396 assert_int_equal(yin_validate_value(YCTX, Y_PREF_IDENTIF_ARG), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +0100397
Radek Iša56ca9e42020-09-08 18:42:00 +0200398 YCTX->xmlctx->value = "pre:pre:b";
399 YCTX->xmlctx->value_len = 9;
400 assert_int_equal(yin_validate_value(YCTX, Y_PREF_IDENTIF_ARG), LY_EVALID);
David Sedlák4a650532019-07-10 11:55:18 +0200401}
402
Michal Vasko12ef5362022-09-16 15:13:58 +0200403static void
404test_valid_module(void **state)
405{
406 struct lys_module *mod;
407 char *printed;
408 const char *links_yin =
409 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
410 "<module name=\"links\"\n"
411 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
412 " xmlns:mod2=\"urn:module2\">\n"
413 " <yang-version value=\"1.1\"/>\n"
414 " <namespace uri=\"urn:module2\"/>\n"
415 " <prefix value=\"mod2\"/>\n"
416 " <identity name=\"just-another-identity\"/>\n"
417 " <grouping name=\"rgroup\">\n"
418 " <leaf name=\"rg1\">\n"
419 " <type name=\"string\"/>\n"
420 " </leaf>\n"
421 " <leaf name=\"rg2\">\n"
422 " <type name=\"string\"/>\n"
423 " </leaf>\n"
424 " </grouping>\n"
425 " <leaf name=\"one-leaf\">\n"
426 " <type name=\"string\"/>\n"
427 " </leaf>\n"
428 " <list name=\"list-for-augment\">\n"
429 " <key value=\"keyleaf\"/>\n"
430 " <leaf name=\"keyleaf\">\n"
431 " <type name=\"string\"/>\n"
432 " </leaf>\n"
433 " <leaf name=\"just-leaf\">\n"
434 " <type name=\"int32\"/>\n"
435 " </leaf>\n"
436 " </list>\n"
437 " <leaf name=\"rleaf\">\n"
438 " <type name=\"string\"/>\n"
439 " </leaf>\n"
440 " <leaf-list name=\"llist\">\n"
441 " <type name=\"string\"/>\n"
442 " <min-elements value=\"0\"/>\n"
443 " <max-elements value=\"100\"/>\n"
444 " <ordered-by value=\"user\"/>\n"
445 " </leaf-list>\n"
446 "</module>\n";
447 const char *statements_yin =
448 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
449 "<module name=\"statements\"\n"
450 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
451 " xmlns:mod=\"urn:module\"\n"
452 " xmlns:mod2=\"urn:module2\">\n"
453 " <yang-version value=\"1.1\"/>\n"
454 " <namespace uri=\"urn:module\"/>\n"
455 " <prefix value=\"mod\"/>\n"
456 " <import module=\"links\">\n"
457 " <prefix value=\"mod2\"/>\n"
458 " </import>\n"
459 " <extension name=\"ext\"/>\n"
460 " <identity name=\"random-identity\">\n"
461 " <base name=\"mod2:just-another-identity\"/>\n"
462 " <base name=\"another-identity\"/>\n"
463 " </identity>\n"
464 " <identity name=\"another-identity\">\n"
465 " <base name=\"mod2:just-another-identity\"/>\n"
466 " </identity>\n"
467 " <typedef name=\"percent\">\n"
468 " <type name=\"uint8\">\n"
469 " <range value=\"0 .. 100\"/>\n"
470 " </type>\n"
471 " <units name=\"percent\"/>\n"
472 " </typedef>\n"
473 " <list name=\"list1\">\n"
474 " <key value=\"a\"/>\n"
475 " <leaf name=\"a\">\n"
476 " <type name=\"string\"/>\n"
477 " </leaf>\n"
478 " <leaf name=\"x\">\n"
479 " <type name=\"string\"/>\n"
480 " </leaf>\n"
481 " <leaf name=\"y\">\n"
482 " <type name=\"string\"/>\n"
483 " </leaf>\n"
484 " </list>\n"
485 " <container name=\"ice-cream-shop\">\n"
486 " <container name=\"employees\">\n"
487 " <when condition=\"/list1/x\"/>\n"
488 " <list name=\"employee\">\n"
489 " <key value=\"id\"/>\n"
490 " <unique tag=\"name\"/>\n"
491 " <config value=\"true\"/>\n"
492 " <min-elements value=\"0\">\n"
493 " <mod:ext/>\n"
494 " </min-elements>\n"
495 " <max-elements value=\"unbounded\"/>\n"
496 " <leaf name=\"id\">\n"
497 " <type name=\"uint64\"/>\n"
498 " <mandatory value=\"true\"/>\n"
499 " </leaf>\n"
500 " <leaf name=\"name\">\n"
501 " <type name=\"string\"/>\n"
502 " </leaf>\n"
503 " <leaf name=\"age\">\n"
504 " <type name=\"uint32\"/>\n"
505 " </leaf>\n"
506 " </list>\n"
507 " </container>\n"
508 " </container>\n"
509 " <container name=\"random\">\n"
510 " <grouping name=\"group\">\n"
511 " <leaf name=\"g1\">\n"
512 " <type name=\"percent\"/>\n"
513 " <mandatory value=\"false\"/>\n"
514 " </leaf>\n"
515 " <leaf name=\"g2\">\n"
516 " <type name=\"string\"/>\n"
517 " </leaf>\n"
518 " </grouping>\n"
519 " <choice name=\"switch\">\n"
520 " <case name=\"a\">\n"
521 " <leaf name=\"aleaf\">\n"
522 " <type name=\"string\"/>\n"
523 " <default value=\"aaa\"/>\n"
524 " </leaf>\n"
525 " </case>\n"
526 " <case name=\"c\">\n"
527 " <leaf name=\"cleaf\">\n"
528 " <type name=\"string\"/>\n"
529 " </leaf>\n"
530 " </case>\n"
531 " </choice>\n"
532 " <anyxml name=\"xml-data\"/>\n"
533 " <anydata name=\"any-data\"/>\n"
534 " <leaf-list name=\"leaflist\">\n"
535 " <type name=\"string\"/>\n"
536 " <min-elements value=\"0\"/>\n"
537 " <max-elements value=\"20\"/>\n"
538 " </leaf-list>\n"
539 " <uses name=\"group\"/>\n"
540 " <uses name=\"mod2:rgroup\"/>\n"
541 " <leaf name=\"lref\">\n"
542 " <type name=\"leafref\">\n"
543 " <path value=\"/mod2:one-leaf\"/>\n"
544 " </type>\n"
545 " </leaf>\n"
546 " <leaf name=\"iref\">\n"
547 " <type name=\"identityref\">\n"
548 " <base name=\"mod2:just-another-identity\"/>\n"
549 " </type>\n"
550 " </leaf>\n"
551 " </container>\n"
552 " <augment target-node=\"/random\">\n"
553 " <leaf name=\"aug-leaf\">\n"
554 " <type name=\"string\"/>\n"
555 " </leaf>\n"
556 " </augment>\n"
557 " <notification name=\"notif\"/>\n"
558 " <deviation target-node=\"/mod:ice-cream-shop/mod:employees/mod:employee/mod:age\">\n"
559 " <deviate value=\"not-supported\">\n"
560 " <mod:ext/>\n"
561 " </deviate>\n"
562 " </deviation>\n"
563 " <deviation target-node=\"/mod:list1\">\n"
564 " <deviate value=\"add\">\n"
565 " <mod:ext/>\n"
566 " <must condition=\"1\"/>\n"
567 " <must condition=\"2\"/>\n"
568 " <unique tag=\"x\"/>\n"
569 " <unique tag=\"y\"/>\n"
570 " <config value=\"true\"/>\n"
571 " <min-elements value=\"1\"/>\n"
572 " <max-elements value=\"2\"/>\n"
573 " </deviate>\n"
574 " </deviation>\n"
575 " <deviation target-node=\"/mod:ice-cream-shop/mod:employees/mod:employee\">\n"
576 " <deviate value=\"delete\">\n"
577 " <unique tag=\"name\"/>\n"
578 " </deviate>\n"
579 " </deviation>\n"
580 " <deviation target-node=\"/mod:random/mod:leaflist\">\n"
581 " <deviate value=\"replace\">\n"
582 " <type name=\"uint32\"/>\n"
583 " <min-elements value=\"10\"/>\n"
584 " <max-elements value=\"15\"/>\n"
585 " </deviate>\n"
586 " </deviation>\n"
587 "</module>\n";
588
589 UTEST_ADD_MODULE(links_yin, LYS_IN_YIN, NULL, NULL);
590 UTEST_ADD_MODULE(statements_yin, LYS_IN_YIN, NULL, &mod);
591 lys_print_mem(&printed, mod, LYS_OUT_YIN, 0);
592 assert_string_equal(printed, statements_yin);
593 free(printed);
594}
595
596static void
597test_print_module(void **state)
598{
599 struct lys_module *mod;
600
601 char *orig = malloc(8096);
602
603 strcpy(orig,
604 "module all {\n"
605 " yang-version 1.1;\n"
606 " namespace \"urn:all\";\n"
607 " prefix all_mod;\n\n"
608 " import ietf-yang-types {\n"
609 " prefix yt;\n"
610 " revision-date 2013-07-15;\n"
611 " description\n"
612 " \"YANG types\";\n"
613 " reference\n"
614 " \"RFC reference\";\n"
615 " }\n\n"
616 " feature feat1 {\n"
617 " if-feature \"feat2\";\n"
618 " status obsolete;\n"
619 " }\n\n"
620 " feature feat2;\n"
621 " feature feat3;\n\n"
622 " identity ident2 {\n"
623 " base ident1;\n"
624 " }\n\n"
625 " identity ident1;\n\n"
626 " typedef tdef1 {\n"
627 " type tdef2 {\n"
628 " length \"3..9 | 30..40\";\n"
629 " pattern \"[ac]*\";\n"
630 " }\n"
631 " units \"none\";\n"
632 " default \"aaa\";\n"
633 " }\n\n"
634 " typedef tdef2 {\n"
635 " type string {\n"
636 " length \"2..10 | 20..50\";\n"
637 " pattern \"[ab]*\";\n"
638 " }\n"
639 " }\n\n"
640 " grouping group1 {\n"
641 " leaf leaf1 {\n"
642 " type int8;\n"
643 " }\n"
644 " }\n\n"
645 " container cont1 {\n"
646 " leaf leaf2 {\n"
647 " if-feature \"feat1\";\n"
648 " type int16;\n"
649 " status obsolete;\n"
650 " }\n\n"
651 " uses group1 {\n"
652 " if-feature \"feat2\";\n"
653 " refine \"leaf1\" {\n"
654 " if-feature \"feat3\";\n"
655 " must \"24 - 4 = number('20')\";\n"
656 " default \"25\";\n"
657 " config true;\n"
658 " mandatory false;\n"
659 " description\n"
660 " \"dsc\";\n"
661 " reference\n"
662 " \"none\";\n"
663 " }\n"
664 " }\n\n"
665 " leaf leaf3 {\n"
666 " type int32;\n"
667 " }\n\n"
668 " leaf leaf4 {\n"
669 " type int64 {\n"
670 " range \"1000 .. 50000\" {\n"
671 " error-message\n"
672 " \"Special error message.\";\n"
673 " error-app-tag \"special-tag\";\n"
674 " }\n"
675 " }\n"
676 " }\n\n"
677 " leaf leaf5 {\n"
678 " type uint8;\n"
679 " }\n\n"
680 " leaf leaf6 {\n"
681 " type uint16;\n"
682 " }\n\n"
683 " leaf leaf7 {\n"
684 " type uint32;\n"
685 " }\n\n"
686 " leaf leaf8 {\n"
687 " type uint64;\n"
688 " }\n\n"
689 " choice choic1 {\n"
690 " default \"leaf9b\";\n"
691 " leaf leaf9a {\n"
692 " type decimal64 {\n"
693 " fraction-digits 9;\n"
694 " }\n"
695 " }\n\n"
696 " leaf leaf9b {\n"
697 " type boolean;\n"
698 " default \"false\";\n"
699 " }\n"
700 " }\n\n"
701 " leaf leaf10 {\n"
702 " type boolean;\n"
703 " }\n\n");
704 strcpy(orig + strlen(orig),
705 " leaf leaf11 {\n"
706 " type enumeration {\n"
707 " enum \"one\";\n"
708 " enum \"two\";\n"
709 " enum \"five\" {\n"
710 " value 5;\n"
711 " }\n"
712 " }\n"
713 " }\n\n"
714 " leaf leaf12 {\n"
715 " type bits {\n"
716 " bit flag0 {\n"
717 " position 0;\n"
718 " }\n"
719 " bit flag1;\n"
720 " bit flag2 {\n"
721 " position 2;\n"
722 " }\n"
723 " bit flag3 {\n"
724 " position 3;\n"
725 " }\n"
726 " }\n"
727 " default \"flag0 flag3\";\n"
728 " }\n\n"
729 " leaf leaf13 {\n"
730 " type binary;\n"
731 " }\n\n"
732 " leaf leaf14 {\n"
733 " type leafref {\n"
734 " path \"/cont1/leaf17\";\n"
735 " }\n"
736 " }\n\n"
737 " leaf leaf15 {\n"
738 " type empty;\n"
739 " }\n\n"
740 " leaf leaf16 {\n"
741 " type union {\n"
742 " type instance-identifier {\n"
743 " require-instance true;\n"
744 " }\n"
745 " type int8;\n"
746 " }\n"
747 " }\n\n"
748 " list list1 {\n"
749 " key \"leaf18\";\n"
750 " unique \"leaf19\";\n"
751 " min-elements 1;\n"
752 " max-elements 20;\n"
753 " leaf leaf18 {\n"
754 " type string;\n"
755 " }\n\n"
756 " leaf leaf19 {\n"
757 " type uint32;\n"
758 " }\n\n"
759 " anyxml axml1;\n"
760 " anydata adata1;\n\n"
761 " action act1 {\n"
762 " input {\n"
763 " leaf leaf24 {\n"
764 " type string;\n"
765 " }\n"
766 " }\n\n"
767 " output {\n"
768 " leaf leaf25 {\n"
769 " type string;\n"
770 " }\n"
771 " }\n"
772 " }\n\n"
773 " notification notif1 {\n"
774 " leaf leaf26 {\n"
775 " type string;\n"
776 " }\n"
777 " }\n"
778 " }\n\n"
779 " leaf-list llist1 {\n"
780 " type tdef1;\n"
781 " ordered-by user;\n"
782 " }\n\n"
783 " list list2 {\n"
784 " key \"leaf27 leaf28\";\n"
785 " leaf leaf27 {\n"
786 " type uint8;\n"
787 " }\n\n"
788 " leaf leaf28 {\n"
789 " type uint8;\n"
790 " }\n"
791 " }\n\n"
792 " leaf leaf29 {\n"
793 " type instance-identifier;\n"
794 " }\n\n"
795 " container must-deviations-container {\n"
796 " presence \"Allows deviations on the leaf\";\n"
797 " leaf leaf30 {\n"
798 " type string;\n"
799 " }\n"
800 " }\n\n"
801 " leaf leaf23 {\n"
802 " type empty;\n"
803 " }\n"
804 " }\n\n"
805 " augment \"/cont1\" {\n"
806 " leaf leaf17 {\n"
807 " type string;\n"
808 " }\n"
809 " }\n\n"
810 " rpc rpc1 {\n"
811 " input {\n"
812 " leaf leaf20 {\n"
813 " type tdef1;\n"
814 " }\n"
815 " }\n\n"
816 " output {\n"
817 " container cont2 {\n"
818 " leaf leaf21 {\n"
819 " type empty;\n"
820 " }\n"
821 " }\n"
822 " }\n"
823 " }\n\n"
824 " container test-when {\n"
825 " leaf when-check {\n"
826 " type boolean;\n"
827 " }\n\n"
828 " leaf gated-data {\n"
829 " when \"../when-check = 'true'\";\n"
830 " type uint16;\n"
831 " }\n"
832 " }\n\n"
833 " extension c-define {\n"
834 " description\n"
835 " \"Takes as an argument a name string.\n"
836 " Makes the code generator use the given name\n"
837 " in the #define.\";\n"
838 " argument \"name\";\n"
839 " }\n"
840 "}\n");
841
842 char *ori_res = malloc(8096);
843
844 strcpy(ori_res,
845 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
846 "<module name=\"all\"\n"
847 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
848 " xmlns:all_mod=\"urn:all\"\n"
849 " xmlns:yt=\"urn:ietf:params:xml:ns:yang:ietf-yang-types\">\n"
850 " <yang-version value=\"1.1\"/>\n"
851 " <namespace uri=\"urn:all\"/>\n"
852 " <prefix value=\"all_mod\"/>\n"
853 " <import module=\"ietf-yang-types\">\n"
854 " <prefix value=\"yt\"/>\n"
855 " <revision-date date=\"2013-07-15\"/>\n"
856 " <description>\n"
857 " <text>YANG types</text>\n"
858 " </description>\n"
859 " <reference>\n"
860 " <text>RFC reference</text>\n"
861 " </reference>\n"
862 " </import>\n"
863 " <extension name=\"c-define\">\n"
864 " <argument name=\"name\"/>\n"
865 " <description>\n"
866 " <text>Takes as an argument a name string.\n"
867 "Makes the code generator use the given name\n"
868 "in the #define.</text>\n"
869 " </description>\n"
870 " </extension>\n"
871 " <feature name=\"feat1\">\n"
872 " <if-feature name=\"feat2\"/>\n"
873 " <status value=\"obsolete\"/>\n"
874 " </feature>\n"
875 " <feature name=\"feat2\"/>\n"
876 " <feature name=\"feat3\"/>\n"
877 " <identity name=\"ident2\">\n"
878 " <base name=\"ident1\"/>\n"
879 " </identity>\n"
880 " <identity name=\"ident1\"/>\n"
881 " <typedef name=\"tdef1\">\n"
882 " <type name=\"tdef2\">\n"
883 " <length value=\"3..9 | 30..40\"/>\n"
884 " <pattern value=\"[ac]*\"/>\n"
885 " </type>\n"
886 " <units name=\"none\"/>\n"
887 " <default value=\"aaa\"/>\n"
888 " </typedef>\n"
889 " <typedef name=\"tdef2\">\n"
890 " <type name=\"string\">\n"
891 " <length value=\"2..10 | 20..50\"/>\n"
892 " <pattern value=\"[ab]*\"/>\n"
893 " </type>\n"
894 " </typedef>\n"
895 " <grouping name=\"group1\">\n"
896 " <leaf name=\"leaf1\">\n"
897 " <type name=\"int8\"/>\n"
898 " </leaf>\n"
899 " </grouping>\n"
900 " <container name=\"cont1\">\n"
901 " <leaf name=\"leaf2\">\n"
902 " <if-feature name=\"feat1\"/>\n"
903 " <type name=\"int16\"/>\n"
904 " <status value=\"obsolete\"/>\n"
905 " </leaf>\n"
906 " <uses name=\"group1\">\n"
907 " <if-feature name=\"feat2\"/>\n"
908 " <refine target-node=\"leaf1\">\n"
909 " <if-feature name=\"feat3\"/>\n"
910 " <must condition=\"24 - 4 = number('20')\"/>\n"
911 " <default value=\"25\"/>\n"
912 " <config value=\"true\"/>\n"
913 " <mandatory value=\"false\"/>\n"
914 " <description>\n"
915 " <text>dsc</text>\n"
916 " </description>\n"
917 " <reference>\n"
918 " <text>none</text>\n"
919 " </reference>\n"
920 " </refine>\n"
921 " </uses>\n"
922 " <leaf name=\"leaf3\">\n"
923 " <type name=\"int32\"/>\n"
924 " </leaf>\n"
925 " <leaf name=\"leaf4\">\n"
926 " <type name=\"int64\">\n"
927 " <range value=\"1000 .. 50000\">\n"
928 " <error-message>\n"
929 " <value>Special error message.</value>\n"
930 " </error-message>\n"
931 " <error-app-tag value=\"special-tag\"/>\n"
932 " </range>\n"
933 " </type>\n"
934 " </leaf>\n"
935 " <leaf name=\"leaf5\">\n"
936 " <type name=\"uint8\"/>\n"
937 " </leaf>\n"
938 " <leaf name=\"leaf6\">\n"
939 " <type name=\"uint16\"/>\n"
940 " </leaf>\n"
941 " <leaf name=\"leaf7\">\n"
942 " <type name=\"uint32\"/>\n"
943 " </leaf>\n"
944 " <leaf name=\"leaf8\">\n"
945 " <type name=\"uint64\"/>\n"
946 " </leaf>\n"
947 " <choice name=\"choic1\">\n"
948 " <default value=\"leaf9b\"/>\n"
949 " <leaf name=\"leaf9a\">\n"
950 " <type name=\"decimal64\">\n"
951 " <fraction-digits value=\"9\"/>\n"
952 " </type>\n"
953 " </leaf>\n"
954 " <leaf name=\"leaf9b\">\n"
955 " <type name=\"boolean\"/>\n"
956 " <default value=\"false\"/>\n"
957 " </leaf>\n"
958 " </choice>\n"
959 " <leaf name=\"leaf10\">\n"
960 " <type name=\"boolean\"/>\n"
961 " </leaf>\n");
962 strcpy(ori_res + strlen(ori_res),
963 " <leaf name=\"leaf11\">\n"
964 " <type name=\"enumeration\">\n"
965 " <enum name=\"one\"/>\n"
966 " <enum name=\"two\"/>\n"
967 " <enum name=\"five\">\n"
968 " <value value=\"5\"/>\n"
969 " </enum>\n"
970 " </type>\n"
971 " </leaf>\n"
972 " <leaf name=\"leaf12\">\n"
973 " <type name=\"bits\">\n"
974 " <bit name=\"flag0\">\n"
975 " <position value=\"0\"/>\n"
976 " </bit>\n"
977 " <bit name=\"flag1\"/>\n"
978 " <bit name=\"flag2\">\n"
979 " <position value=\"2\"/>\n"
980 " </bit>\n"
981 " <bit name=\"flag3\">\n"
982 " <position value=\"3\"/>\n"
983 " </bit>\n"
984 " </type>\n"
985 " <default value=\"flag0 flag3\"/>\n"
986 " </leaf>\n"
987 " <leaf name=\"leaf13\">\n"
988 " <type name=\"binary\"/>\n"
989 " </leaf>\n"
990 " <leaf name=\"leaf14\">\n"
991 " <type name=\"leafref\">\n"
992 " <path value=\"/cont1/leaf17\"/>\n"
993 " </type>\n"
994 " </leaf>\n"
995 " <leaf name=\"leaf15\">\n"
996 " <type name=\"empty\"/>\n"
997 " </leaf>\n"
998 " <leaf name=\"leaf16\">\n"
999 " <type name=\"union\">\n"
1000 " <type name=\"instance-identifier\">\n"
1001 " <require-instance value=\"true\"/>\n"
1002 " </type>\n"
1003 " <type name=\"int8\"/>\n"
1004 " </type>\n"
1005 " </leaf>\n"
1006 " <list name=\"list1\">\n"
1007 " <key value=\"leaf18\"/>\n"
1008 " <unique tag=\"leaf19\"/>\n"
1009 " <min-elements value=\"1\"/>\n"
1010 " <max-elements value=\"20\"/>\n"
1011 " <leaf name=\"leaf18\">\n"
1012 " <type name=\"string\"/>\n"
1013 " </leaf>\n"
1014 " <leaf name=\"leaf19\">\n"
1015 " <type name=\"uint32\"/>\n"
1016 " </leaf>\n"
1017 " <anyxml name=\"axml1\"/>\n"
1018 " <anydata name=\"adata1\"/>\n"
1019 " <action name=\"act1\">\n"
1020 " <input>\n"
1021 " <leaf name=\"leaf24\">\n"
1022 " <type name=\"string\"/>\n"
1023 " </leaf>\n"
1024 " </input>\n"
1025 " <output>\n"
1026 " <leaf name=\"leaf25\">\n"
1027 " <type name=\"string\"/>\n"
1028 " </leaf>\n"
1029 " </output>\n"
1030 " </action>\n"
1031 " <notification name=\"notif1\">\n"
1032 " <leaf name=\"leaf26\">\n"
1033 " <type name=\"string\"/>\n"
1034 " </leaf>\n"
1035 " </notification>\n"
1036 " </list>\n"
1037 " <leaf-list name=\"llist1\">\n"
1038 " <type name=\"tdef1\"/>\n"
1039 " <ordered-by value=\"user\"/>\n"
1040 " </leaf-list>\n"
1041 " <list name=\"list2\">\n"
1042 " <key value=\"leaf27 leaf28\"/>\n"
1043 " <leaf name=\"leaf27\">\n"
1044 " <type name=\"uint8\"/>\n"
1045 " </leaf>\n"
1046 " <leaf name=\"leaf28\">\n"
1047 " <type name=\"uint8\"/>\n"
1048 " </leaf>\n"
1049 " </list>\n"
1050 " <leaf name=\"leaf29\">\n"
1051 " <type name=\"instance-identifier\"/>\n"
1052 " </leaf>\n"
1053 " <container name=\"must-deviations-container\">\n"
1054 " <presence value=\"Allows deviations on the leaf\"/>\n"
1055 " <leaf name=\"leaf30\">\n"
1056 " <type name=\"string\"/>\n"
1057 " </leaf>\n"
1058 " </container>\n"
1059 " <leaf name=\"leaf23\">\n"
1060 " <type name=\"empty\"/>\n"
1061 " </leaf>\n"
1062 " </container>\n"
1063 " <container name=\"test-when\">\n"
1064 " <leaf name=\"when-check\">\n"
1065 " <type name=\"boolean\"/>\n"
1066 " </leaf>\n"
1067 " <leaf name=\"gated-data\">\n"
1068 " <when condition=\"../when-check = 'true'\"/>\n"
1069 " <type name=\"uint16\"/>\n"
1070 " </leaf>\n"
1071 " </container>\n"
1072 " <augment target-node=\"/cont1\">\n"
1073 " <leaf name=\"leaf17\">\n"
1074 " <type name=\"string\"/>\n"
1075 " </leaf>\n"
1076 " </augment>\n"
1077 " <rpc name=\"rpc1\">\n"
1078 " <input>\n"
1079 " <leaf name=\"leaf20\">\n"
1080 " <type name=\"tdef1\"/>\n"
1081 " </leaf>\n"
1082 " </input>\n"
1083 " <output>\n"
1084 " <container name=\"cont2\">\n"
1085 " <leaf name=\"leaf21\">\n"
1086 " <type name=\"empty\"/>\n"
1087 " </leaf>\n"
1088 " </container>\n"
1089 " </output>\n"
1090 " </rpc>\n"
1091 "</module>\n");
1092
1093 char *printed;
1094 struct ly_out *out;
1095
1096 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out));
1097
1098 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1099 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YIN, 0, 0));
1100 assert_int_equal(strlen(ori_res), ly_out_printed(out));
1101 assert_string_equal(printed, ori_res);
1102
1103 ly_out_free(out, NULL, 1);
1104 free(orig);
1105 free(ori_res);
1106}
1107
1108static LY_ERR
1109test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
1110 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
1111 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
1112{
1113 *module_data = user_data;
1114 *format = LYS_IN_YIN;
1115 *free_module_data = NULL;
1116 return LY_SUCCESS;
1117}
1118
1119static void
1120test_print_submodule(void **state)
1121{
1122 struct lys_module *mod;
1123
1124 const char *mod_yin =
1125 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1126 "<module name=\"a\"\n"
1127 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1128 " xmlns:a_mod=\"urn:a\">\n"
1129 " <yang-version value=\"1.1\"/>\n"
1130 " <namespace uri=\"urn:a\"/>\n"
1131 " <prefix value=\"a_mod\"/>\n"
1132 " <include module=\"a-sub\"/>\n"
1133 "</module>\n";
1134
1135 char *submod_yin =
1136 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1137 "<submodule name=\"a-sub\"\n"
1138 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1139 " xmlns:a_mod=\"urn:a\"\n"
1140 " xmlns:yt=\"urn:ietf:params:xml:ns:yang:ietf-yang-types\">\n"
1141 " <yang-version value=\"1.1\"/>\n"
1142 " <belongs-to module=\"a\">\n"
1143 " <prefix value=\"a_mod\"/>\n"
1144 " </belongs-to>\n"
1145 " <import module=\"ietf-yang-types\">\n"
1146 " <prefix value=\"yt\"/>\n"
1147 " <revision-date date=\"2013-07-15\"/>\n"
1148 " </import>\n\n"
1149 " <description>\n"
1150 " <text>YANG types</text>\n"
1151 " </description>\n"
1152 " <reference>\n"
1153 " <text>RFC reference</text>\n"
1154 " </reference>\n"
1155 "</submodule>\n";
1156
1157 char *printed;
1158 struct ly_out *out;
1159
1160 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out));
1161
1162 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod_yin);
1163
1164 UTEST_ADD_MODULE(mod_yin, LYS_IN_YIN, NULL, &mod);
1165 assert_int_equal(LY_SUCCESS, lys_print_submodule(out, mod->parsed->includes[0].submodule, LYS_OUT_YIN, 0, 0));
1166 assert_int_equal(strlen(submod_yin), ly_out_printed(out));
1167 assert_string_equal(printed, submod_yin);
1168
1169 ly_out_free(out, NULL, 1);
1170}
1171
David Sedlák32488102019-07-15 17:44:10 +02001172/* helper function to simplify unit test of each element using parse_content function */
1173LY_ERR
Radek Iša56ca9e42020-09-08 18:42:00 +02001174test_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 +02001175{
David Sedlákc5b20842019-08-13 10:18:31 +02001176 const char *name, *prefix;
1177 size_t name_len, prefix_len;
David Sedlák32488102019-07-15 17:44:10 +02001178 LY_ERR ret = LY_SUCCESS;
1179 struct yin_subelement subelems[71] = {
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001180 {LY_STMT_ACTION, dest, 0},
1181 {LY_STMT_ANYDATA, dest, 0},
1182 {LY_STMT_ANYXML, dest, 0},
1183 {LY_STMT_ARGUMENT, dest, 0},
1184 {LY_STMT_AUGMENT, dest, 0},
1185 {LY_STMT_BASE, dest, 0},
1186 {LY_STMT_BELONGS_TO, dest, 0},
1187 {LY_STMT_BIT, dest, 0},
1188 {LY_STMT_CASE, dest, 0},
1189 {LY_STMT_CHOICE, dest, 0},
1190 {LY_STMT_CONFIG, dest, 0},
1191 {LY_STMT_CONTACT, dest, 0},
1192 {LY_STMT_CONTAINER, dest, 0},
1193 {LY_STMT_DEFAULT, dest, YIN_SUBELEM_UNIQUE},
1194 {LY_STMT_DESCRIPTION, dest, 0},
1195 {LY_STMT_DEVIATE, dest, 0},
1196 {LY_STMT_DEVIATION, dest, 0},
1197 {LY_STMT_ENUM, dest, 0},
1198 {LY_STMT_ERROR_APP_TAG, dest, YIN_SUBELEM_UNIQUE},
1199 {LY_STMT_ERROR_MESSAGE, dest, 0},
1200 {LY_STMT_EXTENSION, dest, 0},
1201 {LY_STMT_FEATURE, dest, 0},
1202 {LY_STMT_FRACTION_DIGITS, dest, 0},
1203 {LY_STMT_GROUPING, dest, 0},
1204 {LY_STMT_IDENTITY, dest, 0},
1205 {LY_STMT_IF_FEATURE, dest, 0},
1206 {LY_STMT_IMPORT, dest, 0},
1207 {LY_STMT_INCLUDE, dest, 0},
1208 {LY_STMT_INPUT, dest, 0},
1209 {LY_STMT_KEY, dest, YIN_SUBELEM_UNIQUE},
1210 {LY_STMT_LEAF, dest, 0},
1211 {LY_STMT_LEAF_LIST, dest, 0},
1212 {LY_STMT_LENGTH, dest, 0},
1213 {LY_STMT_LIST, dest, 0},
1214 {LY_STMT_MANDATORY, dest, 0},
1215 {LY_STMT_MAX_ELEMENTS, dest, 0},
1216 {LY_STMT_MIN_ELEMENTS, dest, 0},
1217 {LY_STMT_MODIFIER, dest, 0},
1218 {LY_STMT_MODULE, dest, 0},
1219 {LY_STMT_MUST, dest, 0},
1220 {LY_STMT_NAMESPACE, dest, YIN_SUBELEM_UNIQUE},
1221 {LY_STMT_NOTIFICATION, dest, 0},
1222 {LY_STMT_ORDERED_BY, dest, 0},
1223 {LY_STMT_ORGANIZATION, dest, 0},
1224 {LY_STMT_OUTPUT, dest, 0},
1225 {LY_STMT_PATH, dest, 0},
1226 {LY_STMT_PATTERN, dest, 0},
1227 {LY_STMT_POSITION, dest, 0},
1228 {LY_STMT_PREFIX, dest, YIN_SUBELEM_UNIQUE},
1229 {LY_STMT_PRESENCE, dest, YIN_SUBELEM_UNIQUE},
1230 {LY_STMT_RANGE, dest, 0},
1231 {LY_STMT_REFERENCE, dest, 0},
1232 {LY_STMT_REFINE, dest, 0},
1233 {LY_STMT_REQUIRE_INSTANCE, dest, 0},
1234 {LY_STMT_REVISION, dest, 0},
1235 {LY_STMT_REVISION_DATE, dest, 0},
1236 {LY_STMT_RPC, dest, 0},
1237 {LY_STMT_STATUS, dest, 0},
1238 {LY_STMT_SUBMODULE, dest, 0},
1239 {LY_STMT_TYPE, dest, 0},
1240 {LY_STMT_TYPEDEF, dest, 0},
1241 {LY_STMT_UNIQUE, dest, 0},
1242 {LY_STMT_UNITS, dest, YIN_SUBELEM_UNIQUE},
1243 {LY_STMT_USES, dest, 0},
1244 {LY_STMT_VALUE, dest, 0},
1245 {LY_STMT_WHEN, dest, 0},
1246 {LY_STMT_YANG_VERSION, dest, 0},
1247 {LY_STMT_YIN_ELEMENT, dest, 0},
1248 {LY_STMT_EXTENSION_INSTANCE, dest, 0},
1249 {LY_STMT_ARG_TEXT, dest, 0},
1250 {LY_STMT_ARG_VALUE, dest, 0}
1251 };
1252
Michal Vaskod0625d72022-10-06 15:02:50 +02001253 YCTX->main_ctx = (struct lysp_ctx *)YCTX;
Radek Iša56ca9e42020-09-08 18:42:00 +02001254 ly_in_new_memory(data, &UTEST_IN);
1255 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
1256 prefix = YCTX->xmlctx->prefix;
1257 prefix_len = YCTX->xmlctx->prefix_len;
1258 name = YCTX->xmlctx->name;
1259 name_len = YCTX->xmlctx->name_len;
1260 lyxml_ctx_next(YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +01001261
Michal Vasko193dacd2022-10-13 08:43:05 +02001262 ret = yin_parse_content(YCTX, subelems, 71, NULL,
1263 yin_match_keyword(YCTX, name, name_len, prefix, prefix_len, LY_STMT_NONE), text, exts);
Michal Vaskob36053d2020-03-26 15:49:30 +01001264
Michal Vasko63f3d842020-07-08 10:10:14 +02001265 /* free parser and input */
Radek Iša56ca9e42020-09-08 18:42:00 +02001266 lyxml_ctx_free(YCTX->xmlctx);
1267 YCTX->xmlctx = NULL;
1268 ly_in_free(UTEST_IN, 0);
1269 UTEST_IN = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001270 return ret;
1271}
1272
David Sedlákd1144562019-08-06 12:36:14 +02001273#define EXT_SUBELEM "<myext:c-define name=\"MY_MTU\" xmlns:myext=\"urn:example:extensions\"/>"
1274
David Sedlák32488102019-07-15 17:44:10 +02001275static void
David Sedlák43801c92019-08-05 15:58:54 +02001276test_enum_elem(void **state)
David Sedlák32488102019-07-15 17:44:10 +02001277{
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001278 struct lysp_type type = {0};
David Sedlák32488102019-07-15 17:44:10 +02001279 const char *data;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001280
David Sedlák32488102019-07-15 17:44:10 +02001281 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001282 "<enum name=\"enum-name\">\n"
1283 " <if-feature name=\"feature\" />\n"
1284 " <value value=\"55\" />\n"
1285 " <status value=\"deprecated\" />\n"
1286 " <description><text>desc...</text></description>\n"
1287 " <reference><text>ref...</text></reference>\n"
1288 " " EXT_SUBELEM "\n"
1289 "</enum>"
1290 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001291 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1292 uint16_t flags = LYS_STATUS_DEPRC | LYS_SET_VALUE;
1293
1294 CHECK_LYSP_TYPE_ENUM(type.enums, "desc...", 1, flags, 1, "enum-name", "ref...", 55);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001295 assert_string_equal(type.enums->iffeatures[0].str, "feature");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001296 TEST_1_CHECK_LYSP_EXT_INSTANCE(type.enums->exts, LY_STMT_ENUM);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001297 lysp_type_free(&fctx, &type);
David Sedlákd1144562019-08-06 12:36:14 +02001298 memset(&type, 0, sizeof type);
1299
1300 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001301 "<enum name=\"enum-name\"></enum>"
1302 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001303 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001304 assert_string_equal(type.enums->name, "enum-name");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001305 lysp_type_free(&fctx, &type);
David Sedlák32488102019-07-15 17:44:10 +02001306 memset(&type, 0, sizeof type);
David Sedlák43801c92019-08-05 15:58:54 +02001307}
1308
1309static void
1310test_bit_elem(void **state)
1311{
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001312 struct lysp_type type = {0};
David Sedlák43801c92019-08-05 15:58:54 +02001313 const char *data;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001314
David Sedlák43801c92019-08-05 15:58:54 +02001315 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001316 "<bit name=\"bit-name\">\n"
1317 " <if-feature name=\"feature\" />\n"
1318 " <position value=\"55\" />\n"
1319 " <status value=\"deprecated\" />\n"
1320 " <description><text>desc...</text></description>\n"
1321 " <reference><text>ref...</text></reference>\n"
1322 EXT_SUBELEM
1323 "</bit>"
1324 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001325 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1326 uint16_t flags = LYS_STATUS_DEPRC | LYS_SET_VALUE;
1327
1328 CHECK_LYSP_TYPE_ENUM(type.bits, "desc...", 1, flags, 1, "bit-name", "ref...", 55);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001329 assert_string_equal(type.bits->iffeatures[0].str, "feature");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001330 TEST_1_CHECK_LYSP_EXT_INSTANCE(type.bits->exts, LY_STMT_BIT);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001331 lysp_type_free(&fctx, &type);
David Sedlákd1144562019-08-06 12:36:14 +02001332 memset(&type, 0, sizeof type);
1333
1334 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001335 "<bit name=\"bit-name\"> </bit>"
1336 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001337 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1338 CHECK_LYSP_TYPE_ENUM(type.bits, NULL, 0, 0, 0, "bit-name", NULL, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001339 lysp_type_free(&fctx, &type);
David Sedlák43801c92019-08-05 15:58:54 +02001340 memset(&type, 0, sizeof type);
David Sedlák32488102019-07-15 17:44:10 +02001341}
1342
1343static void
David Sedlák32488102019-07-15 17:44:10 +02001344test_status_elem(void **state)
1345{
David Sedlák32488102019-07-15 17:44:10 +02001346 const char *data;
1347 uint16_t flags = 0;
David Sedlák32488102019-07-15 17:44:10 +02001348
1349 /* test invalid value */
1350 data = ELEMENT_WRAPPER_START "<status value=\"invalid\"></status>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001351 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID);
1352 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"status\" element. "
1353 "Valid values are \"current\", \"deprecated\" and \"obsolete\".", "Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001354}
1355
1356static void
David Sedlák32488102019-07-15 17:44:10 +02001357test_yin_element_elem(void **state)
1358{
David Sedlák32488102019-07-15 17:44:10 +02001359 const char *data;
1360 uint16_t flags = 0;
David Sedlák32488102019-07-15 17:44:10 +02001361
1362 data = ELEMENT_WRAPPER_START "<yin-element value=\"invalid\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001363 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +02001364 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"yin-element\" element. "
1365 "Valid values are \"true\" and \"false\".", "Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001366}
1367
1368static void
1369test_yangversion_elem(void **state)
1370{
David Sedlák32488102019-07-15 17:44:10 +02001371 const char *data;
1372 uint8_t version = 0;
David Sedlák32488102019-07-15 17:44:10 +02001373
1374 /* invalid value */
1375 data = ELEMENT_WRAPPER_START "<yang-version value=\"version\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001376 assert_int_equal(test_element_helper(state, data, &version, NULL, NULL), LY_EVALID);
1377 CHECK_LOG_CTX("Invalid value \"version\" of \"value\" attribute in \"yang-version\" element. "
1378 "Valid values are \"1\" and \"1.1\".", "Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001379}
1380
1381static void
David Sedlák8e7bda82019-07-16 17:57:50 +02001382test_argument_elem(void **state)
1383{
David Sedlák8e7bda82019-07-16 17:57:50 +02001384 const char *data;
1385 uint16_t flags = 0;
1386 const char *arg;
1387 struct yin_argument_meta arg_meta = {&flags, &arg};
David Sedlák8e7bda82019-07-16 17:57:50 +02001388
1389 /* min subelems */
1390 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001391 "<argument name=\"arg\">"
1392 "</argument>"
1393 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001394 assert_int_equal(test_element_helper(state, data, &arg_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001395 assert_string_equal(arg, "arg");
1396 assert_true(flags == 0);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001397 lydict_remove(UTEST_LYCTX, arg);
David Sedlák8e7bda82019-07-16 17:57:50 +02001398}
1399
1400static void
David Sedlák8e7bda82019-07-16 17:57:50 +02001401test_belongsto_elem(void **state)
1402{
David Sedlák8e7bda82019-07-16 17:57:50 +02001403 const char *data;
1404 struct lysp_submodule submod;
1405
Michal Vasko8a67eff2021-12-07 14:04:47 +01001406 lydict_insert(UTEST_LYCTX, "module-name", 0, &PARSER_CUR_PMOD(YCTX)->mod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +02001407
David Sedlák8e7bda82019-07-16 17:57:50 +02001408 data = ELEMENT_WRAPPER_START "<belongs-to module=\"module-name\"></belongs-to>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001409 assert_int_equal(test_element_helper(state, data, &submod, NULL, NULL), LY_EVALID);
1410 CHECK_LOG_CTX("Missing mandatory sub-element \"prefix\" of \"belongs-to\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001411}
1412
1413static void
1414test_config_elem(void **state)
1415{
David Sedlák8e7bda82019-07-16 17:57:50 +02001416 const char *data;
1417 uint16_t flags = 0;
David Sedlák8e7bda82019-07-16 17:57:50 +02001418
1419 data = ELEMENT_WRAPPER_START "<config value=\"false\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001420 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001421 assert_true(flags & LYS_CONFIG_R);
David Sedlák8e7bda82019-07-16 17:57:50 +02001422 flags = 0;
1423
1424 data = ELEMENT_WRAPPER_START "<config value=\"invalid\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001425 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID);
1426 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"config\" element. "
1427 "Valid values are \"true\" and \"false\".", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001428}
1429
1430static void
1431test_default_elem(void **state)
1432{
David Sedlák8e7bda82019-07-16 17:57:50 +02001433 const char *data;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001434 struct lysp_qname val = {0};
David Sedlák8e7bda82019-07-16 17:57:50 +02001435
1436 data = ELEMENT_WRAPPER_START "<default/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001437 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
1438 CHECK_LOG_CTX("Missing mandatory attribute value of default element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001439}
1440
1441static void
1442test_err_app_tag_elem(void **state)
1443{
David Sedlák8e7bda82019-07-16 17:57:50 +02001444 const char *data;
1445 const char *val = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001446
1447 data = ELEMENT_WRAPPER_START "<error-app-tag/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001448 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
1449 CHECK_LOG_CTX("Missing mandatory attribute value of error-app-tag element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001450}
1451
1452static void
1453test_err_msg_elem(void **state)
1454{
David Sedlák8e7bda82019-07-16 17:57:50 +02001455 const char *data;
1456 const char *val = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001457
1458 data = ELEMENT_WRAPPER_START "<error-message></error-message>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001459 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
1460 CHECK_LOG_CTX("Missing mandatory sub-element \"value\" of \"error-message\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001461
David Sedlákdf2a9732019-08-07 13:23:16 +02001462 data = ELEMENT_WRAPPER_START "<error-message invalid=\"text\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001463 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
1464 CHECK_LOG_CTX("Unexpected attribute \"invalid\" of \"error-message\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001465}
1466
1467static void
1468test_fracdigits_elem(void **state)
1469{
David Sedlák8e7bda82019-07-16 17:57:50 +02001470 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001471 struct lysp_type type = {0};
David Sedlák8e7bda82019-07-16 17:57:50 +02001472
David Sedlák8e7bda82019-07-16 17:57:50 +02001473 /* invalid values */
1474 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"-1\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001475 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1476 CHECK_LOG_CTX("Invalid value \"-1\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001477
1478 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"02\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001479 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1480 CHECK_LOG_CTX("Invalid value \"02\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001481
1482 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"1p\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001483 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1484 CHECK_LOG_CTX("Invalid value \"1p\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001485
1486 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"19\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001487 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1488 CHECK_LOG_CTX("Invalid value \"19\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001489
1490 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"999999999999999999\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001491 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1492 CHECK_LOG_CTX("Invalid value \"999999999999999999\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001493}
1494
1495static void
1496test_iffeature_elem(void **state)
1497{
David Sedlák8e7bda82019-07-16 17:57:50 +02001498 const char *data;
1499 const char **iffeatures = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001500
1501 data = ELEMENT_WRAPPER_START "<if-feature/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001502 assert_int_equal(test_element_helper(state, data, &iffeatures, NULL, NULL), LY_EVALID);
1503 CHECK_LOG_CTX("Missing mandatory attribute name of if-feature element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001504 LY_ARRAY_FREE(iffeatures);
1505 iffeatures = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001506}
1507
1508static void
1509test_length_elem(void **state)
1510{
David Sedlák8e7bda82019-07-16 17:57:50 +02001511 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001512 struct lysp_type type = {0};
David Sedlák8e7bda82019-07-16 17:57:50 +02001513
1514 /* max subelems */
1515 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001516 "<length value=\"length-str\">\n"
1517 " <error-message><value>err-msg</value></error-message>\n"
1518 " <error-app-tag value=\"err-app-tag\"/>\n"
1519 " <description><text>desc</text></description>\n"
1520 " <reference><text>ref</text></reference>\n"
1521 EXT_SUBELEM
1522 "</length>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001523 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001524 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1525 CHECK_LYSP_RESTR(type.length, "length-str", "desc",
1526 "err-app-tag", "err-msg", 1, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001527 assert_true(type.flags & LYS_SET_LENGTH);
Radek Krejci39b7fc22021-02-26 23:29:18 +01001528 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.length->exts[0]), LY_STMT_LENGTH);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001529 lysp_type_free(&fctx, &type);
David Sedlák8e7bda82019-07-16 17:57:50 +02001530 memset(&type, 0, sizeof(type));
1531
1532 /* min subelems */
1533 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001534 "<length value=\"length-str\">"
1535 "</length>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001536 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001537 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1538 CHECK_LYSP_RESTR(type.length, "length-str", NULL,
1539 NULL, NULL, 0, NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001540 lysp_type_free(&fctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001541 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001542 memset(&type, 0, sizeof(type));
1543
1544 data = ELEMENT_WRAPPER_START "<length></length>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001545 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1546 CHECK_LOG_CTX("Missing mandatory attribute value of length element.", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001547 lysp_type_free(&fctx, &type);
David Sedlák8e7bda82019-07-16 17:57:50 +02001548 memset(&type, 0, sizeof(type));
David Sedlák8e7bda82019-07-16 17:57:50 +02001549}
1550
1551static void
1552test_modifier_elem(void **state)
1553{
David Sedlák8e7bda82019-07-16 17:57:50 +02001554 const char *data;
Radek Krejci011e4aa2020-09-04 15:22:31 +02001555 const char *pat;
David Sedlák8e7bda82019-07-16 17:57:50 +02001556
Radek Iša56ca9e42020-09-08 18:42:00 +02001557 assert_int_equal(LY_SUCCESS, lydict_insert(UTEST_LYCTX, "\006pattern", 8, &pat));
David Sedlák8e7bda82019-07-16 17:57:50 +02001558 data = ELEMENT_WRAPPER_START "<modifier value=\"invert\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001559 assert_int_equal(test_element_helper(state, data, &pat, NULL, NULL), LY_EVALID);
1560 CHECK_LOG_CTX("Invalid value \"invert\" of \"value\" attribute in \"modifier\" element. "
1561 "Only valid value is \"invert-match\".", "Line number 1.");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001562 lydict_remove(UTEST_LYCTX, pat);
David Sedlák8e7bda82019-07-16 17:57:50 +02001563}
1564
1565static void
1566test_namespace_elem(void **state)
1567{
David Sedlák8e7bda82019-07-16 17:57:50 +02001568 const char *data;
1569 const char *ns;
David Sedlák8e7bda82019-07-16 17:57:50 +02001570
1571 data = ELEMENT_WRAPPER_START "<namespace/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001572 assert_int_equal(test_element_helper(state, data, &ns, NULL, NULL), LY_EVALID);
1573 CHECK_LOG_CTX("Missing mandatory attribute uri of namespace element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001574}
1575
1576static void
David Sedlák8e7bda82019-07-16 17:57:50 +02001577test_pattern_elem(void **state)
1578{
David Sedlák8e7bda82019-07-16 17:57:50 +02001579 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001580 struct lysp_type type = {0};
David Sedlák8e7bda82019-07-16 17:57:50 +02001581
1582 /* max subelems */
1583 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001584 "<pattern value=\"super_pattern\">\n"
1585 " <modifier value=\"invert-match\"/>\n"
1586 " <error-message><value>err-msg-value</value></error-message>\n"
1587 " <error-app-tag value=\"err-app-tag-value\"/>\n"
1588 " <description><text>&quot;pattern-desc&quot;</text></description>\n"
1589 " <reference><text>pattern-ref</text></reference>\n"
1590 EXT_SUBELEM
1591 "</pattern>"
1592 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001593 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001594 assert_true(type.flags & LYS_SET_PATTERN);
Radek Iša56ca9e42020-09-08 18:42:00 +02001595 CHECK_LYSP_RESTR(type.patterns, "\x015super_pattern", "\"pattern-desc\"",
1596 "err-app-tag-value", "err-msg-value", 1, "pattern-ref");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001597 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.patterns->exts[0]), LY_STMT_PATTERN);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001598 lysp_type_free(&fctx, &type);
David Sedlák8e7bda82019-07-16 17:57:50 +02001599 memset(&type, 0, sizeof(type));
1600
1601 /* min subelems */
1602 data = ELEMENT_WRAPPER_START "<pattern value=\"pattern\"> </pattern>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001603 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1604 CHECK_LYSP_RESTR(type.patterns, "\x006pattern", NULL, NULL, NULL, 0, NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001605 lysp_type_free(&fctx, &type);
David Sedlák8e7bda82019-07-16 17:57:50 +02001606 memset(&type, 0, sizeof(type));
David Sedlák8e7bda82019-07-16 17:57:50 +02001607}
1608
1609static void
1610test_value_position_elem(void **state)
1611{
David Sedlák8e7bda82019-07-16 17:57:50 +02001612 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001613 struct lysp_type_enum en = {0};
David Sedlák8e7bda82019-07-16 17:57:50 +02001614
1615 /* valid values */
David Sedlák8e7bda82019-07-16 17:57:50 +02001616 data = ELEMENT_WRAPPER_START "<value value=\"-55\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001617 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1618 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, -55);
David Sedlák8e7bda82019-07-16 17:57:50 +02001619 memset(&en, 0, sizeof(en));
1620
1621 data = ELEMENT_WRAPPER_START "<value value=\"0\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001622 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1623 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, 0);
David Sedlák8e7bda82019-07-16 17:57:50 +02001624 memset(&en, 0, sizeof(en));
1625
1626 data = ELEMENT_WRAPPER_START "<value value=\"-0\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001627 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1628 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, 0);
David Sedlák8e7bda82019-07-16 17:57:50 +02001629 memset(&en, 0, sizeof(en));
1630
1631 /* valid positions */
David Sedlák8e7bda82019-07-16 17:57:50 +02001632 data = ELEMENT_WRAPPER_START "<position value=\"0\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001633 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1634 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, 0);
David Sedlák8e7bda82019-07-16 17:57:50 +02001635 memset(&en, 0, sizeof(en));
1636
1637 /* invalid values */
1638 data = ELEMENT_WRAPPER_START "<value value=\"99999999999999999999999\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001639 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1640 CHECK_LOG_CTX("Invalid value \"99999999999999999999999\" of \"value\" attribute in \"value\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001641
1642 data = ELEMENT_WRAPPER_START "<value value=\"1k\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001643 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1644 CHECK_LOG_CTX("Invalid value \"1k\" of \"value\" attribute in \"value\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001645
David Sedlák69f01612019-07-17 11:41:08 +02001646 data = ELEMENT_WRAPPER_START "<value value=\"\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001647 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1648 CHECK_LOG_CTX("Invalid value \"\" of \"value\" attribute in \"value\" element.", "Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001649
David Sedlák8e7bda82019-07-16 17:57:50 +02001650 /*invalid positions */
1651 data = ELEMENT_WRAPPER_START "<position value=\"-5\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001652 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1653 CHECK_LOG_CTX("Invalid value \"-5\" of \"value\" attribute in \"position\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001654
1655 data = ELEMENT_WRAPPER_START "<position value=\"-0\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001656 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1657 CHECK_LOG_CTX("Invalid value \"-0\" of \"value\" attribute in \"position\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001658
1659 data = ELEMENT_WRAPPER_START "<position value=\"99999999999999999999\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001660 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1661 CHECK_LOG_CTX("Invalid value \"99999999999999999999\" of \"value\" attribute in \"position\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001662
David Sedlák69f01612019-07-17 11:41:08 +02001663 data = ELEMENT_WRAPPER_START "<position value=\"\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001664 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1665 CHECK_LOG_CTX("Invalid value \"\" of \"value\" attribute in \"position\" element.", "Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001666}
1667
1668static void
1669test_prefix_elem(void **state)
1670{
David Sedlák69f01612019-07-17 11:41:08 +02001671 const char *data;
1672 const char *value = NULL;
1673
1674 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001675 assert_int_equal(test_element_helper(state, data, &value, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001676 assert_string_equal(value, "pref");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001677 lydict_remove(UTEST_LYCTX, value);
David Sedlák69f01612019-07-17 11:41:08 +02001678}
1679
1680static void
1681test_range_elem(void **state)
1682{
David Sedlák69f01612019-07-17 11:41:08 +02001683 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001684 struct lysp_type type = {0};
David Sedlák69f01612019-07-17 11:41:08 +02001685
1686 /* max subelems */
1687 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001688 "<range value=\"range-str\">\n"
1689 " <error-message><value>err-msg</value></error-message>\n"
1690 " <error-app-tag value=\"err-app-tag\" />\n"
1691 " <description><text>desc</text></description>\n"
1692 " <reference><text>ref</text></reference>\n"
1693 EXT_SUBELEM
1694 "</range>"
1695 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001696 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1697 CHECK_LYSP_RESTR(type.range, "range-str", "desc",
1698 "err-app-tag", "err-msg", 1, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001699 assert_true(type.flags & LYS_SET_RANGE);
Radek Krejci39b7fc22021-02-26 23:29:18 +01001700 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.range->exts[0]), LY_STMT_RANGE);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001701 lysp_type_free(&fctx, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001702 memset(&type, 0, sizeof(type));
1703
1704 /* min subelems */
1705 data = ELEMENT_WRAPPER_START "<range value=\"range-str\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001706 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1707 CHECK_LYSP_RESTR(type.range, "range-str", NULL,
1708 NULL, NULL, 0, NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001709 lysp_type_free(&fctx, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001710 memset(&type, 0, sizeof(type));
David Sedlák69f01612019-07-17 11:41:08 +02001711}
1712
1713static void
1714test_reqinstance_elem(void **state)
1715{
David Sedlák69f01612019-07-17 11:41:08 +02001716 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001717 struct lysp_type type = {0};
David Sedlák69f01612019-07-17 11:41:08 +02001718
David Sedlákd1144562019-08-06 12:36:14 +02001719 data = ELEMENT_WRAPPER_START "<require-instance value=\"true\">" EXT_SUBELEM "</require-instance>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001720 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001721 assert_int_equal(type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001722 assert_true(type.flags & LYS_SET_REQINST);
Radek Krejci5984d222021-02-26 23:01:45 +01001723 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.exts[0]), LY_STMT_REQUIRE_INSTANCE);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001724 lysp_type_free(&fctx, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001725 memset(&type, 0, sizeof(type));
1726
1727 data = ELEMENT_WRAPPER_START "<require-instance value=\"false\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001728 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001729 assert_int_equal(type.require_instance, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001730 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001731 memset(&type, 0, sizeof(type));
1732
1733 data = ELEMENT_WRAPPER_START "<require-instance value=\"invalid\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001734 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
David Sedlák69f01612019-07-17 11:41:08 +02001735 memset(&type, 0, sizeof(type));
Radek Iša56ca9e42020-09-08 18:42:00 +02001736 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"require-instance\" element. "
1737 "Valid values are \"true\" and \"false\".", "Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001738}
1739
1740static void
1741test_revision_date_elem(void **state)
1742{
David Sedlák69f01612019-07-17 11:41:08 +02001743 const char *data;
1744 char rev[LY_REV_SIZE];
1745
1746 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001747 assert_int_equal(test_element_helper(state, data, rev, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001748 assert_string_equal(rev, "2000-01-01");
1749
1750 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-50-05\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001751 assert_int_equal(test_element_helper(state, data, rev, NULL, NULL), LY_EVALID);
1752 CHECK_LOG_CTX("Invalid value \"2000-50-05\" of \"revision-date\".", "Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001753}
1754
1755static void
1756test_unique_elem(void **state)
1757{
David Sedlák69f01612019-07-17 11:41:08 +02001758 const char *data;
1759 const char **values = NULL;
1760
1761 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001762 assert_int_equal(test_element_helper(state, data, &values, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001763 assert_string_equal(*values, "tag");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001764 lydict_remove(UTEST_LYCTX, *values);
David Sedlák69f01612019-07-17 11:41:08 +02001765 LY_ARRAY_FREE(values);
David Sedlákd1144562019-08-06 12:36:14 +02001766 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001767}
1768
1769static void
1770test_units_elem(void **state)
1771{
David Sedlák69f01612019-07-17 11:41:08 +02001772 const char *data;
1773 const char *values = NULL;
1774
1775 data = ELEMENT_WRAPPER_START "<units name=\"name\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001776 assert_int_equal(test_element_helper(state, data, &values, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001777 assert_string_equal(values, "name");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001778 lydict_remove(UTEST_LYCTX, values);
David Sedlákd1144562019-08-06 12:36:14 +02001779 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001780}
1781
1782static void
David Sedlák69f01612019-07-17 11:41:08 +02001783test_yin_text_value_elem(void **state)
1784{
David Sedlák69f01612019-07-17 11:41:08 +02001785 const char *data;
1786 const char *val;
1787
1788 data = ELEMENT_WRAPPER_START "<text>text</text>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001789 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001790 assert_string_equal(val, "text");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001791 lydict_remove(UTEST_LYCTX, val);
David Sedlák69f01612019-07-17 11:41:08 +02001792
1793 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 +02001794 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001795 assert_string_equal(val, "text");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001796 lydict_remove(UTEST_LYCTX, val);
David Sedlák69f01612019-07-17 11:41:08 +02001797
1798 data = ELEMENT_WRAPPER_START "<text></text>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001799 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001800 assert_string_equal("", val);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001801 lydict_remove(UTEST_LYCTX, val);
David Sedlák8e7bda82019-07-16 17:57:50 +02001802}
David Sedlák32488102019-07-15 17:44:10 +02001803
David Sedlák374d2b32019-07-17 15:06:55 +02001804static void
1805test_type_elem(void **state)
1806{
David Sedlák374d2b32019-07-17 15:06:55 +02001807 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001808 struct lysp_type type = {0};
David Sedlák374d2b32019-07-17 15:06:55 +02001809
1810 /* max subelems */
1811 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001812 "<type name=\"type-name\">\n"
1813 " <base name=\"base-name\"/>\n"
1814 " <bit name=\"bit\"/>\n"
1815 " <enum name=\"enum\"/>\n"
1816 " <fraction-digits value=\"2\"/>\n"
1817 " <length value=\"length\"/>\n"
1818 " <path value=\"/path\"/>\n"
1819 " <pattern value=\"pattern\"/>\n"
1820 " <range value=\"range\" />\n"
1821 " <require-instance value=\"true\"/>\n"
1822 " <type name=\"sub-type-name\"/>\n"
1823 EXT_SUBELEM
1824 "</type>"
1825 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001826 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák374d2b32019-07-17 15:06:55 +02001827 assert_string_equal(type.name, "type-name");
1828 assert_string_equal(*type.bases, "base-name");
1829 assert_string_equal(type.bits->name, "bit");
1830 assert_string_equal(type.enums->name, "enum");
1831 assert_int_equal(type.fraction_digits, 2);
Radek Iša56ca9e42020-09-08 18:42:00 +02001832 CHECK_LYSP_RESTR(type.length, "length", NULL,
1833 NULL, NULL, 0, NULL);
Michal Vaskocb8c6d42020-10-16 11:58:30 +02001834 assert_string_equal(type.path->expr, "/path");
Radek Iša56ca9e42020-09-08 18:42:00 +02001835 CHECK_LYSP_RESTR(type.patterns, "\006pattern", NULL,
1836 NULL, NULL, 0, NULL);
1837 CHECK_LYSP_RESTR(type.range, "range", NULL,
1838 NULL, NULL, 0, NULL);
David Sedlák374d2b32019-07-17 15:06:55 +02001839 assert_int_equal(type.require_instance, 1);
1840 assert_string_equal(type.types->name, "sub-type-name");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001841 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.exts[0]), LY_STMT_TYPE);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001842 assert_true(type.flags & LYS_SET_BASE);
1843 assert_true(type.flags & LYS_SET_BIT);
1844 assert_true(type.flags & LYS_SET_ENUM);
1845 assert_true(type.flags & LYS_SET_FRDIGITS);
1846 assert_true(type.flags & LYS_SET_LENGTH);
1847 assert_true(type.flags & LYS_SET_PATH);
1848 assert_true(type.flags & LYS_SET_PATTERN);
1849 assert_true(type.flags & LYS_SET_RANGE);
1850 assert_true(type.flags & LYS_SET_REQINST);
1851 assert_true(type.flags & LYS_SET_TYPE);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001852 lysp_type_free(&fctx, &type);
David Sedlák374d2b32019-07-17 15:06:55 +02001853 memset(&type, 0, sizeof(type));
1854
1855 /* min subelems */
1856 data = ELEMENT_WRAPPER_START "<type name=\"type-name\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001857 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001858 lysp_type_free(&fctx, &type);
David Sedlák374d2b32019-07-17 15:06:55 +02001859 memset(&type, 0, sizeof(type));
David Sedlák374d2b32019-07-17 15:06:55 +02001860}
1861
David Sedlák1af868e2019-07-17 17:03:14 +02001862static void
1863test_max_elems_elem(void **state)
1864{
David Sedlák1af868e2019-07-17 17:03:14 +02001865 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001866 struct lysp_node_list list = {0};
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001867 struct lysp_refine refine = {0};
David Sedlák1af868e2019-07-17 17:03:14 +02001868
David Sedlák1af868e2019-07-17 17:03:14 +02001869 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"10\"/> </refine>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001870 assert_int_equal(test_element_helper(state, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02001871 assert_int_equal(refine.max, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001872 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001873
1874 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"0\"/> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001875 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID);
1876 CHECK_LOG_CTX("Invalid value \"0\" of \"value\" attribute in \"max-elements\" element.", "Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02001877
1878 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"-10\"/> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001879 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID);
1880 CHECK_LOG_CTX("Invalid value \"-10\" of \"value\" attribute in \"max-elements\" element.", "Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02001881
1882 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"k\"/> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001883 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID);
1884 CHECK_LOG_CTX("Invalid value \"k\" of \"value\" attribute in \"max-elements\" element.", "Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02001885
1886 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"u12\"/> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001887 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID);
1888 CHECK_LOG_CTX("Invalid value \"u12\" of \"value\" attribute in \"max-elements\" element.", "Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02001889}
1890
David Sedlák09e18c92019-07-18 11:17:11 +02001891static void
1892test_min_elems_elem(void **state)
1893{
David Sedlák09e18c92019-07-18 11:17:11 +02001894 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001895 struct lysp_node_leaflist llist = {0};
David Sedlák09e18c92019-07-18 11:17:11 +02001896
1897 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 +02001898 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID);
1899 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 +02001900
1901 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 +02001902 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID);
1903 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 +02001904
1905 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 +02001906 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID);
1907 CHECK_LOG_CTX("Invalid value \"5k\" of \"value\" attribute in \"min-elements\" element.", "Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02001908
1909 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 +02001910 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID);
1911 CHECK_LOG_CTX("Invalid value \"05\" of \"value\" attribute in \"min-elements\" element.", "Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02001912}
1913
David Sedláka2dad212019-07-18 12:45:19 +02001914static void
1915test_ordby_elem(void **state)
1916{
David Sedláka2dad212019-07-18 12:45:19 +02001917 const char *data;
1918 uint16_t flags = 0;
David Sedláka2dad212019-07-18 12:45:19 +02001919
1920 data = ELEMENT_WRAPPER_START "<ordered-by value=\"user\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001921 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001922 assert_true(flags & LYS_ORDBY_USER);
David Sedláka2dad212019-07-18 12:45:19 +02001923
1924 data = ELEMENT_WRAPPER_START "<ordered-by value=\"inv\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001925 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID);
1926 CHECK_LOG_CTX("Invalid value \"inv\" of \"value\" attribute in \"ordered-by\" element. "
1927 "Valid values are \"system\" and \"user\".", "Line number 1.");
David Sedláka2dad212019-07-18 12:45:19 +02001928}
1929
David Sedlák8a83bbb2019-07-18 14:46:00 +02001930static void
1931test_any_elem(void **state)
1932{
David Sedlák8a83bbb2019-07-18 14:46:00 +02001933 const char *data;
1934 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02001935 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák8a83bbb2019-07-18 14:46:00 +02001936 struct lysp_node_anydata *parsed = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02001937 uint16_t flags;
David Sedlák8a83bbb2019-07-18 14:46:00 +02001938
1939 /* anyxml max subelems */
1940 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001941 "<anyxml name=\"any-name\">\n"
1942 " <config value=\"true\" />\n"
1943 " <description><text>desc</text></description>\n"
1944 " <if-feature name=\"feature\" />\n"
1945 " <mandatory value=\"true\" />\n"
1946 " <must condition=\"must-cond\" />\n"
1947 " <reference><text>ref</text></reference>\n"
1948 " <status value=\"deprecated\"/>\n"
1949 " <when condition=\"when-cond\"/>\n"
1950 EXT_SUBELEM
1951 "</anyxml>"
1952 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001953 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001954 parsed = (struct lysp_node_anydata *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02001955 flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_DEPRC;
1956 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
1957 "any-name", 0, LYS_ANYXML, 0, "ref", 1);
1958 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001959 assert_string_equal(parsed->iffeatures[0].str, "feature");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001960 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_ANYXML);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001961 lysp_node_free(&fctx, siblings);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001962 siblings = NULL;
1963
1964 /* anydata max subelems */
1965 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001966 "<anydata name=\"any-name\">\n"
1967 " <config value=\"true\" />\n"
1968 " <description><text>desc</text></description>\n"
1969 " <if-feature name=\"feature\" />\n"
1970 " <mandatory value=\"true\" />\n"
1971 " <must condition=\"must-cond\" />\n"
1972 " <reference><text>ref</text></reference>\n"
1973 " <status value=\"deprecated\"/>\n"
1974 " <when condition=\"when-cond\"/>\n"
1975 EXT_SUBELEM
1976 "</anydata>"
1977 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001978 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001979 parsed = (struct lysp_node_anydata *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02001980 flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_DEPRC;
1981 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
1982 "any-name", 0, LYS_ANYDATA, 0, "ref", 1);
1983 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001984 assert_string_equal(parsed->iffeatures[0].str, "feature");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001985 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_ANYDATA);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001986 lysp_node_free(&fctx, siblings);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001987 siblings = NULL;
1988
1989 /* min subelems */
1990 node_meta.parent = (void *)0x10;
1991 data = ELEMENT_WRAPPER_START "<anydata name=\"any-name\"> </anydata>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001992 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001993 parsed = (struct lysp_node_anydata *)siblings;
1994 assert_ptr_equal(parsed->parent, node_meta.parent);
Radek Iša56ca9e42020-09-08 18:42:00 +02001995 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
1996 "any-name", 0, LYS_ANYDATA, 1, NULL, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001997 lysp_node_free(&fctx, siblings);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001998}
1999
David Sedlák203ca3a2019-07-18 15:26:25 +02002000static void
2001test_leaf_elem(void **state)
2002{
David Sedlák203ca3a2019-07-18 15:26:25 +02002003 const char *data;
2004 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002005 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák203ca3a2019-07-18 15:26:25 +02002006 struct lysp_node_leaf *parsed = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02002007 uint16_t flags;
David Sedlák203ca3a2019-07-18 15:26:25 +02002008
2009 /* max elements */
2010 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002011 "<leaf name=\"leaf\">\n"
2012 " <config value=\"true\" />\n"
2013 " <default value=\"def-val\"/>\n"
2014 " <description><text>desc</text></description>\n"
2015 " <if-feature name=\"feature\" />\n"
2016 " <mandatory value=\"true\" />\n"
2017 " <must condition=\"must-cond\" />\n"
2018 " <reference><text>ref</text></reference>\n"
2019 " <status value=\"deprecated\"/>\n"
2020 " <type name=\"type\"/>\n"
2021 " <units name=\"uni\"/>\n"
2022 " <when condition=\"when-cond\"/>\n"
2023 EXT_SUBELEM
2024 "</leaf>"
David Sedlák203ca3a2019-07-18 15:26:25 +02002025 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002026 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák203ca3a2019-07-18 15:26:25 +02002027 parsed = (struct lysp_node_leaf *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002028 flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_DEPRC;
2029 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2030 "leaf", 0, LYS_LEAF, 0, "ref", 1);
2031 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002032 assert_string_equal(parsed->iffeatures[0].str, "feature");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002033 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_LEAF);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002034 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlák203ca3a2019-07-18 15:26:25 +02002035 assert_string_equal(parsed->type.name, "type");
2036 assert_string_equal(parsed->units, "uni");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002037 assert_string_equal(parsed->dflt.str, "def-val");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002038 lysp_node_free(&fctx, siblings);
David Sedlák203ca3a2019-07-18 15:26:25 +02002039 siblings = NULL;
2040
2041 /* min elements */
2042 data = ELEMENT_WRAPPER_START "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002043 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák203ca3a2019-07-18 15:26:25 +02002044 parsed = (struct lysp_node_leaf *)siblings;
2045 assert_string_equal(parsed->name, "leaf");
2046 assert_string_equal(parsed->type.name, "type");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002047 lysp_node_free(&fctx, siblings);
David Sedlák203ca3a2019-07-18 15:26:25 +02002048 siblings = NULL;
David Sedlák203ca3a2019-07-18 15:26:25 +02002049}
2050
David Sedlákc3da3ef2019-07-19 12:56:08 +02002051static void
2052test_leaf_list_elem(void **state)
2053{
David Sedlákc3da3ef2019-07-19 12:56:08 +02002054 const char *data;
2055 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002056 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlákc3da3ef2019-07-19 12:56:08 +02002057 struct lysp_node_leaflist *parsed = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02002058 uint16_t flags;
David Sedlákc3da3ef2019-07-19 12:56:08 +02002059
2060 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002061 "<leaf-list name=\"llist\">\n"
2062 " <config value=\"true\" />\n"
2063 " <default value=\"def-val0\"/>\n"
2064 " <default value=\"def-val1\"/>\n"
2065 " <description><text>desc</text></description>\n"
2066 " <if-feature name=\"feature\"/>\n"
2067 " <max-elements value=\"5\"/>\n"
2068 " <must condition=\"must-cond\"/>\n"
2069 " <ordered-by value=\"user\" />\n"
2070 " <reference><text>ref</text></reference>\n"
2071 " <status value=\"current\"/>\n"
2072 " <type name=\"type\"/>\n"
2073 " <units name=\"uni\"/>\n"
2074 " <when condition=\"when-cond\"/>\n"
2075 EXT_SUBELEM
2076 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002077 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002078 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002079 parsed = (struct lysp_node_leaflist *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002080 flags = LYS_CONFIG_W | LYS_ORDBY_USER | LYS_STATUS_CURR | LYS_SET_MAX;
2081 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2082 "llist", 0, LYS_LEAFLIST, 0, "ref", 1);
2083 CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002084 assert_string_equal(parsed->dflts[0].str, "def-val0");
2085 assert_string_equal(parsed->dflts[1].str, "def-val1");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002086 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002087 assert_int_equal(parsed->max, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002088 assert_string_equal(parsed->type.name, "type");
2089 assert_string_equal(parsed->units, "uni");
Radek Iša56ca9e42020-09-08 18:42:00 +02002090 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002091 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_LEAF_LIST);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002092 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002093 siblings = NULL;
2094
2095 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002096 "<leaf-list name=\"llist\">\n"
2097 " <config value=\"true\" />\n"
2098 " <description><text>desc</text></description>\n"
2099 " <if-feature name=\"feature\"/>\n"
2100 " <min-elements value=\"5\"/>\n"
2101 " <must condition=\"must-cond\"/>\n"
2102 " <ordered-by value=\"user\" />\n"
2103 " <reference><text>ref</text></reference>\n"
2104 " <status value=\"current\"/>\n"
2105 " <type name=\"type\"/>\n"
2106 " <units name=\"uni\"/>\n"
2107 " <when condition=\"when-cond\"/>\n"
2108 EXT_SUBELEM
2109 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002110 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002111 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002112 parsed = (struct lysp_node_leaflist *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002113 flags = LYS_CONFIG_W | LYS_ORDBY_USER | LYS_STATUS_CURR | LYS_SET_MIN;
2114 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2115 "llist", 0, LYS_LEAFLIST, 0, "ref", 1);
2116 CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL);
2117 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002118 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002119 assert_int_equal(parsed->min, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002120 assert_string_equal(parsed->type.name, "type");
2121 assert_string_equal(parsed->units, "uni");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002122 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_LEAF_LIST);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002123 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002124 siblings = NULL;
2125
2126 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002127 "<leaf-list name=\"llist\">\n"
2128 " <config value=\"true\" />\n"
2129 " <description><text>desc</text></description>\n"
2130 " <if-feature name=\"feature\"/>\n"
2131 " <max-elements value=\"15\"/>\n"
2132 " <min-elements value=\"5\"/>\n"
2133 " <must condition=\"must-cond\"/>\n"
2134 " <ordered-by value=\"user\" />\n"
2135 " <reference><text>ref</text></reference>\n"
2136 " <status value=\"current\"/>\n"
2137 " <type name=\"type\"/>\n"
2138 " <units name=\"uni\"/>\n"
2139 " <when condition=\"when-cond\"/>\n"
2140 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002141 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002142 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002143 parsed = (struct lysp_node_leaflist *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002144 flags = LYS_CONFIG_W | LYS_ORDBY_USER | LYS_STATUS_CURR | LYS_SET_MIN | LYS_SET_MAX;
2145 CHECK_LYSP_NODE(parsed, "desc", 0, flags, 1,
2146 "llist", 0, LYS_LEAFLIST, 0, "ref", 1);
2147 CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL);
2148 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002149 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002150 assert_int_equal(parsed->min, 5);
2151 assert_int_equal(parsed->max, 15);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002152 assert_string_equal(parsed->type.name, "type");
2153 assert_string_equal(parsed->units, "uni");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002154 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002155 siblings = NULL;
2156
2157 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002158 "<leaf-list name=\"llist\">\n"
2159 " <type name=\"type\"/>\n"
2160 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002161 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002162 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002163 parsed = (struct lysp_node_leaflist *)siblings;
2164 assert_string_equal(parsed->name, "llist");
2165 assert_string_equal(parsed->type.name, "type");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002166 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002167 siblings = NULL;
2168
2169 /* invalid combinations */
2170 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002171 "<leaf-list name=\"llist\">\n"
2172 " <max-elements value=\"5\"/>\n"
2173 " <min-elements value=\"15\"/>\n"
2174 " <type name=\"type\"/>"
2175 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002176 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002177 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_EVALID);
2178 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 +02002179 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002180 siblings = NULL;
2181
2182 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002183 "<leaf-list name=\"llist\">\n"
2184 " <default value=\"def-val1\"/>\n"
2185 " <min-elements value=\"15\"/>\n"
2186 " <type name=\"type\"/>\n"
2187 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002188 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002189 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_EVALID);
2190 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 +02002191 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002192 siblings = NULL;
2193
2194 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002195 "<leaf-list name=\"llist\">"
2196 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002197 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002198 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_EVALID);
2199 CHECK_LOG_CTX("Missing mandatory sub-element \"type\" of \"leaf-list\" element.", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002200 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002201 siblings = NULL;
David Sedlákc3da3ef2019-07-19 12:56:08 +02002202}
2203
David Sedlákcb39f642019-07-19 13:19:55 +02002204static void
2205test_presence_elem(void **state)
2206{
David Sedlákcb39f642019-07-19 13:19:55 +02002207 const char *data;
2208 const char *val;
2209
2210 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002211 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlákcb39f642019-07-19 13:19:55 +02002212 assert_string_equal(val, "presence-val");
Michal Vaskoe180ed02021-02-05 16:31:20 +01002213 lydict_remove(UTEST_LYCTX, val);
David Sedlákcb39f642019-07-19 13:19:55 +02002214
2215 data = ELEMENT_WRAPPER_START "<presence/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002216 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
2217 CHECK_LOG_CTX("Missing mandatory attribute value of presence element.", "Line number 1.");
David Sedlákcb39f642019-07-19 13:19:55 +02002218}
2219
David Sedlák12470a82019-07-19 13:44:36 +02002220static void
2221test_key_elem(void **state)
2222{
David Sedlák12470a82019-07-19 13:44:36 +02002223 const char *data;
2224 const char *val;
2225
2226 data = ELEMENT_WRAPPER_START "<key value=\"key-value\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002227 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák12470a82019-07-19 13:44:36 +02002228 assert_string_equal(val, "key-value");
Michal Vaskoe180ed02021-02-05 16:31:20 +01002229 lydict_remove(UTEST_LYCTX, val);
David Sedlák12470a82019-07-19 13:44:36 +02002230
2231 data = ELEMENT_WRAPPER_START "<key/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002232 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
2233 CHECK_LOG_CTX("Missing mandatory attribute value of key element.", "Line number 1.");
David Sedlák12470a82019-07-19 13:44:36 +02002234}
2235
David Sedlák04e17b22019-07-19 15:29:48 +02002236static void
David Sedlák0d6de5a2019-07-22 13:25:44 +02002237test_uses_elem(void **state)
2238{
David Sedlák0d6de5a2019-07-22 13:25:44 +02002239 const char *data;
2240 struct lysp_node *siblings = NULL;
2241 struct tree_node_meta node_meta = {NULL, &siblings};
2242 struct lysp_node_uses *parsed = NULL;
2243
2244 /* max subelems */
2245 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002246 "<uses name=\"uses-name\">\n"
2247 " <when condition=\"cond\" />\n"
2248 " <if-feature name=\"feature\" />\n"
2249 " <status value=\"obsolete\" />\n"
2250 " <description><text>desc</text></description>\n"
2251 " <reference><text>ref</text></reference>\n"
2252 " <refine target-node=\"target\"/>\n"
2253 " <augment target-node=\"target\" />\n"
2254 EXT_SUBELEM
2255 "</uses>"
2256 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002257 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002258 parsed = (struct lysp_node_uses *)&siblings[0];
Radek Iša56ca9e42020-09-08 18:42:00 +02002259 CHECK_LYSP_NODE(parsed, "desc", 1, LYS_STATUS_OBSLT, 1,
2260 "uses-name", 0, LYS_USES, 0, "ref", 1);
2261 CHECK_LYSP_WHEN(parsed->when, "cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002262 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlák0d6de5a2019-07-22 13:25:44 +02002263 assert_string_equal(parsed->refines->nodeid, "target");
David Sedlák992fb7c2019-07-24 16:51:01 +02002264 assert_string_equal(parsed->augments->nodeid, "target");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002265 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_USES);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002266 lysp_node_free(&fctx, siblings);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002267 siblings = NULL;
2268
2269 /* min subelems */
2270 data = ELEMENT_WRAPPER_START "<uses name=\"uses-name\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002271 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002272 assert_string_equal(siblings[0].name, "uses-name");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002273 lysp_node_free(&fctx, siblings);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002274 siblings = NULL;
David Sedlák0d6de5a2019-07-22 13:25:44 +02002275}
2276
David Sedlákaa854b02019-07-22 14:17:10 +02002277static void
David Sedlákaf536aa2019-07-23 13:42:23 +02002278test_list_elem(void **state)
2279{
David Sedlákaf536aa2019-07-23 13:42:23 +02002280 const char *data;
2281 struct lysp_node *siblings = NULL;
2282 struct tree_node_meta node_meta = {NULL, &siblings};
2283 struct lysp_node_list *parsed = NULL;
2284
2285 /* max subelems */
2286 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002287 "<list name=\"list-name\">\n"
2288 " <when condition=\"when\"/>\n"
2289 " <if-feature name=\"iff\"/>\n"
2290 " <must condition=\"must-cond\"/>\n"
2291 " <key value=\"key\"/>\n"
2292 " <unique tag=\"utag\"/>\n"
2293 " <config value=\"true\"/>\n"
2294 " <min-elements value=\"10\"/>\n"
2295 " <ordered-by value=\"user\"/>\n"
2296 " <status value=\"deprecated\"/>\n"
2297 " <description><text>desc</text></description>\n"
2298 " <reference><text>ref</text></reference>\n"
2299 " <anydata name=\"anyd\"/>\n"
2300 " <anyxml name=\"anyx\"/>\n"
2301 " <container name=\"cont\"/>\n"
2302 " <choice name=\"choice\"/>\n"
2303 " <action name=\"action\"/>\n"
2304 " <grouping name=\"grp\"/>\n"
2305 " <notification name=\"notf\"/>\n"
2306 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2307 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2308 " <list name=\"sub-list\"/>\n"
2309 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2310 " <uses name=\"uses-name\"/>\n"
2311 EXT_SUBELEM
2312 "</list>"
2313 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002314 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákaf536aa2019-07-23 13:42:23 +02002315 parsed = (struct lysp_node_list *)&siblings[0];
David Sedlákaf536aa2019-07-23 13:42:23 +02002316 assert_string_equal(parsed->child->name, "anyd");
2317 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2318 assert_string_equal(parsed->child->next->name, "anyx");
2319 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
David Sedlákf111bcb2019-07-23 17:15:51 +02002320 assert_string_equal(parsed->child->next->next->name, "cont");
2321 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002322 assert_string_equal(parsed->child->next->next->next->name, "choice");
2323 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
David Sedlák85d0eca2019-07-24 15:15:21 +02002324 assert_string_equal(parsed->child->next->next->next->next->name, "leaf");
2325 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAF);
2326 assert_string_equal(parsed->child->next->next->next->next->next->name, "llist");
2327 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2328 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "sub-list");
2329 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LIST);
2330 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "uses-name");
2331 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_USES);
2332 assert_null(parsed->child->next->next->next->next->next->next->next->next);
Radek Iša56ca9e42020-09-08 18:42:00 +02002333 uint16_t flags = LYS_ORDBY_USER | LYS_STATUS_DEPRC | LYS_CONFIG_W | LYS_SET_MIN;
2334
2335 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2336 "list-name", 0, LYS_LIST, 0, "ref", 1);
2337 CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL);
2338 CHECK_LYSP_WHEN(parsed->when, "when", NULL, 0, NULL);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002339 assert_string_equal(parsed->groupings->name, "grp");
David Sedlák85d0eca2019-07-24 15:15:21 +02002340 assert_string_equal(parsed->actions->name, "action");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002341 assert_int_equal(parsed->groupings->nodetype, LYS_GROUPING);
David Sedlák031b9e72019-07-23 15:19:37 +02002342 assert_string_equal(parsed->notifs->name, "notf");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002343 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlákaf536aa2019-07-23 13:42:23 +02002344 assert_string_equal(parsed->key, "key");
2345 assert_int_equal(parsed->min, 10);
David Sedlákaf536aa2019-07-23 13:42:23 +02002346 assert_string_equal(parsed->typedefs->name, "tpdf");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002347 assert_string_equal(parsed->uniques->str, "utag");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002348 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_LIST);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002349 lysp_node_free(&fctx, siblings);
Radek Iša56ca9e42020-09-08 18:42:00 +02002350 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
David Sedlákaf536aa2019-07-23 13:42:23 +02002351 siblings = NULL;
2352
2353 /* min subelems */
2354 data = ELEMENT_WRAPPER_START "<list name=\"list-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002355 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákaf536aa2019-07-23 13:42:23 +02002356 parsed = (struct lysp_node_list *)&siblings[0];
Radek Iša56ca9e42020-09-08 18:42:00 +02002357 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2358 "list-name", 0, LYS_LIST, 0, NULL, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002359 lysp_node_free(&fctx, siblings);
David Sedlákaf536aa2019-07-23 13:42:23 +02002360 siblings = NULL;
David Sedlákaf536aa2019-07-23 13:42:23 +02002361}
2362
David Sedlák031b9e72019-07-23 15:19:37 +02002363static void
2364test_notification_elem(void **state)
2365{
David Sedlák031b9e72019-07-23 15:19:37 +02002366 const char *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002367 struct lysp_node_notif *notifs = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002368 struct tree_node_meta notif_meta = {NULL, (struct lysp_node **)&notifs};
David Sedlák031b9e72019-07-23 15:19:37 +02002369
2370 /* max subelems */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002371 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlák031b9e72019-07-23 15:19:37 +02002372 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002373 "<notification name=\"notif-name\">\n"
2374 " <anydata name=\"anyd\"/>\n"
2375 " <anyxml name=\"anyx\"/>\n"
2376 " <description><text>desc</text></description>\n"
2377 " <if-feature name=\"iff\"/>\n"
2378 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2379 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2380 " <list name=\"sub-list\"/>\n"
2381 " <must condition=\"cond\"/>\n"
2382 " <reference><text>ref</text></reference>\n"
2383 " <status value=\"deprecated\"/>\n"
2384 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2385 " <uses name=\"uses-name\"/>\n"
2386 " <container name=\"cont\"/>\n"
2387 " <choice name=\"choice\"/>\n"
2388 " <grouping name=\"grp\"/>\n"
2389 EXT_SUBELEM
2390 "</notification>"
2391 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002392 assert_int_equal(test_element_helper(state, data, &notif_meta, NULL, NULL), LY_SUCCESS);
David Sedlák031b9e72019-07-23 15:19:37 +02002393 assert_string_equal(notifs->name, "notif-name");
Radek Krejci01180ac2021-01-27 08:48:22 +01002394 assert_string_equal(notifs->child->name, "anyd");
2395 assert_int_equal(notifs->child->nodetype, LYS_ANYDATA);
2396 assert_string_equal(notifs->child->next->name, "anyx");
2397 assert_int_equal(notifs->child->next->nodetype, LYS_ANYXML);
2398 assert_string_equal(notifs->child->next->next->name, "leaf");
2399 assert_int_equal(notifs->child->next->next->nodetype, LYS_LEAF);
2400 assert_string_equal(notifs->child->next->next->next->name, "llist");
2401 assert_int_equal(notifs->child->next->next->next->nodetype, LYS_LEAFLIST);
2402 assert_string_equal(notifs->child->next->next->next->next->name, "sub-list");
2403 assert_int_equal(notifs->child->next->next->next->next->nodetype, LYS_LIST);
David Sedlák031b9e72019-07-23 15:19:37 +02002404 assert_true(notifs->flags & LYS_STATUS_DEPRC);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002405 assert_string_equal(notifs->groupings->name, "grp");
2406 assert_int_equal(notifs->groupings->nodetype, LYS_GROUPING);
Radek Krejci01180ac2021-01-27 08:48:22 +01002407 assert_string_equal(notifs->child->next->next->next->next->next->name, "uses-name");
2408 assert_int_equal(notifs->child->next->next->next->next->next->nodetype, LYS_USES);
2409 assert_string_equal(notifs->child->next->next->next->next->next->next->name, "cont");
2410 assert_int_equal(notifs->child->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
2411 assert_int_equal(notifs->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2412 assert_string_equal(notifs->child->next->next->next->next->next->next->next->name, "choice");
2413 assert_null(notifs->child->next->next->next->next->next->next->next->next);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002414 assert_string_equal(notifs->iffeatures[0].str, "iff");
2415 assert_string_equal(notifs->musts->arg.str, "cond");
David Sedlák031b9e72019-07-23 15:19:37 +02002416 assert_int_equal(notifs->nodetype, LYS_NOTIF);
2417 assert_null(notifs->parent);
2418 assert_string_equal(notifs->ref, "ref");
2419 assert_string_equal(notifs->typedefs->name, "tpdf");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002420 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(notifs->exts[0]), LY_STMT_NOTIFICATION);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002421 lysp_node_free(&fctx, (struct lysp_node *)notifs);
David Sedlák031b9e72019-07-23 15:19:37 +02002422 notifs = NULL;
2423
2424 /* min subelems */
2425 data = ELEMENT_WRAPPER_START "<notification name=\"notif-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002426 assert_int_equal(test_element_helper(state, data, &notif_meta, NULL, NULL), LY_SUCCESS);
David Sedlák031b9e72019-07-23 15:19:37 +02002427 assert_string_equal(notifs->name, "notif-name");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002428 lysp_node_free(&fctx, (struct lysp_node *)notifs);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002429 notifs = NULL;
David Sedláke3ce9ef2019-07-23 16:34:30 +02002430}
2431
2432static void
2433test_grouping_elem(void **state)
2434{
David Sedláke3ce9ef2019-07-23 16:34:30 +02002435 const char *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002436 struct lysp_node_grp *grps = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002437 struct tree_node_meta grp_meta = {NULL, (struct lysp_node **)&grps};
David Sedláke3ce9ef2019-07-23 16:34:30 +02002438
2439 /* max subelems */
2440 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002441 "<grouping name=\"grp-name\">\n"
2442 " <anydata name=\"anyd\"/>\n"
2443 " <anyxml name=\"anyx\"/>\n"
2444 " <description><text>desc</text></description>\n"
2445 " <grouping name=\"sub-grp\"/>\n"
2446 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2447 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2448 " <list name=\"list\"/>\n"
2449 " <notification name=\"notf\"/>\n"
2450 " <reference><text>ref</text></reference>\n"
2451 " <status value=\"current\"/>\n"
2452 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2453 " <uses name=\"uses-name\"/>\n"
2454 " <action name=\"act\"/>\n"
2455 " <container name=\"cont\"/>\n"
2456 " <choice name=\"choice\"/>\n"
2457 EXT_SUBELEM
2458 "</grouping>"
2459 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002460 assert_int_equal(test_element_helper(state, data, &grp_meta, NULL, NULL), LY_SUCCESS);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002461 assert_string_equal(grps->name, "grp-name");
Radek Krejci01180ac2021-01-27 08:48:22 +01002462 assert_string_equal(grps->child->name, "anyd");
2463 assert_string_equal(grps->child->next->name, "anyx");
2464 assert_string_equal(grps->child->next->next->name, "leaf");
2465 assert_string_equal(grps->child->next->next->next->name, "llist");
2466 assert_string_equal(grps->child->next->next->next->next->name, "list");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002467 assert_string_equal(grps->dsc, "desc");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002468 assert_true(grps->flags & LYS_STATUS_CURR);
2469 assert_string_equal(grps->groupings->name, "sub-grp");
2470 assert_int_equal(grps->nodetype, LYS_GROUPING);
2471 assert_string_equal(grps->notifs->name, "notf");
2472 assert_null(grps->parent);
2473 assert_string_equal(grps->ref, "ref");
2474 assert_string_equal(grps->typedefs->name, "tpdf");
David Sedlák85d0eca2019-07-24 15:15:21 +02002475 assert_string_equal(grps->actions->name, "act");
Radek Krejci01180ac2021-01-27 08:48:22 +01002476 assert_string_equal(grps->child->next->next->next->next->next->name, "uses-name");
2477 assert_int_equal(grps->child->next->next->next->next->next->nodetype, LYS_USES);
2478 assert_string_equal(grps->child->next->next->next->next->next->next->name, "cont");
2479 assert_int_equal(grps->child->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
2480 assert_string_equal(grps->child->next->next->next->next->next->next->next->name, "choice");
2481 assert_int_equal(grps->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002482 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(grps->exts[0]), LY_STMT_GROUPING);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002483 lysp_node_free(&fctx, &grps->node);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002484 grps = NULL;
2485
2486 /* min subelems */
2487 data = ELEMENT_WRAPPER_START "<grouping name=\"grp-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002488 assert_int_equal(test_element_helper(state, data, &grp_meta, NULL, NULL), LY_SUCCESS);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002489 assert_string_equal(grps->name, "grp-name");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002490 lysp_node_free(&fctx, &grps->node);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002491 grps = NULL;
David Sedlák031b9e72019-07-23 15:19:37 +02002492}
2493
David Sedlákf111bcb2019-07-23 17:15:51 +02002494static void
2495test_container_elem(void **state)
2496{
David Sedlákf111bcb2019-07-23 17:15:51 +02002497 const char *data;
2498 struct lysp_node *siblings = NULL;
2499 struct tree_node_meta node_meta = {NULL, &siblings};
2500 struct lysp_node_container *parsed = NULL;
2501
2502 /* max subelems */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002503 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedláke2dc9e92019-07-24 09:59:21 +02002504 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002505 "<container name=\"cont-name\">\n"
2506 " <anydata name=\"anyd\"/>\n"
2507 " <anyxml name=\"anyx\"/>\n"
2508 " <config value=\"true\"/>\n"
2509 " <container name=\"subcont\"/>\n"
2510 " <description><text>desc</text></description>\n"
2511 " <grouping name=\"sub-grp\"/>\n"
2512 " <if-feature name=\"iff\"/>\n"
2513 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2514 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2515 " <list name=\"list\"/>\n"
2516 " <must condition=\"cond\"/>\n"
2517 " <notification name=\"notf\"/>\n"
2518 " <presence value=\"presence\"/>\n"
2519 " <reference><text>ref</text></reference>\n"
2520 " <status value=\"current\"/>\n"
2521 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2522 " <uses name=\"uses-name\"/>\n"
2523 " <when condition=\"when-cond\"/>\n"
2524 " <action name=\"act\"/>\n"
2525 " <choice name=\"choice\"/>\n"
2526 EXT_SUBELEM
2527 "</container>"
2528 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002529 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedláke2dc9e92019-07-24 09:59:21 +02002530 parsed = (struct lysp_node_container *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002531 uint16_t flags = LYS_CONFIG_W | LYS_STATUS_CURR;
2532
2533 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2534 "cont-name", 0, LYS_CONTAINER, 0, "ref", 1);
2535 CHECK_LYSP_RESTR(parsed->musts, "cond", NULL, NULL, NULL, 0, NULL);
2536 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
2537
Michal Vasko7f45cf22020-10-01 12:49:44 +02002538 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedláke2dc9e92019-07-24 09:59:21 +02002539 assert_string_equal(parsed->presence, "presence");
2540 assert_string_equal(parsed->typedefs->name, "tpdf");
2541 assert_string_equal(parsed->groupings->name, "sub-grp");
2542 assert_string_equal(parsed->child->name, "anyd");
2543 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2544 assert_string_equal(parsed->child->next->name, "anyx");
2545 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2546 assert_string_equal(parsed->child->next->next->name, "subcont");
2547 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
2548 assert_string_equal(parsed->child->next->next->next->name, "leaf");
2549 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
2550 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
2551 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
2552 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
2553 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
2554 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
2555 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002556 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
2557 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2558 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke2dc9e92019-07-24 09:59:21 +02002559 assert_string_equal(parsed->notifs->name, "notf");
David Sedlák85d0eca2019-07-24 15:15:21 +02002560 assert_string_equal(parsed->actions->name, "act");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002561 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_CONTAINER);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002562 lysp_node_free(&fctx, siblings);
Radek Iša56ca9e42020-09-08 18:42:00 +02002563 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
David Sedláke2dc9e92019-07-24 09:59:21 +02002564 siblings = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02002565
2566 /* min subelems */
2567 data = ELEMENT_WRAPPER_START "<container name=\"cont-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002568 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákf111bcb2019-07-23 17:15:51 +02002569 parsed = (struct lysp_node_container *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002570 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2571 "cont-name", 0, LYS_CONTAINER, 0, NULL, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002572 lysp_node_free(&fctx, siblings);
David Sedlákf111bcb2019-07-23 17:15:51 +02002573 siblings = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02002574}
2575
David Sedlák5379d392019-07-24 10:42:03 +02002576static void
2577test_case_elem(void **state)
2578{
David Sedlák5379d392019-07-24 10:42:03 +02002579 const char *data;
2580 struct lysp_node *siblings = NULL;
2581 struct tree_node_meta node_meta = {NULL, &siblings};
2582 struct lysp_node_case *parsed = NULL;
2583
2584 /* max subelems */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002585 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlák5379d392019-07-24 10:42:03 +02002586 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002587 "<case name=\"case-name\">\n"
2588 " <anydata name=\"anyd\"/>\n"
2589 " <anyxml name=\"anyx\"/>\n"
2590 " <container name=\"subcont\"/>\n"
2591 " <description><text>desc</text></description>\n"
2592 " <if-feature name=\"iff\"/>\n"
2593 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2594 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2595 " <list name=\"list\"/>\n"
2596 " <reference><text>ref</text></reference>\n"
2597 " <status value=\"current\"/>\n"
2598 " <uses name=\"uses-name\"/>\n"
2599 " <when condition=\"when-cond\"/>\n"
2600 " <choice name=\"choice\"/>\n"
2601 EXT_SUBELEM
2602 "</case>"
2603 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002604 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák5379d392019-07-24 10:42:03 +02002605 parsed = (struct lysp_node_case *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002606 uint16_t flags = LYS_STATUS_CURR;
2607
2608 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2609 "case-name", 0, LYS_CASE, 0, "ref", 1);
2610 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002611 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlák5379d392019-07-24 10:42:03 +02002612 assert_string_equal(parsed->child->name, "anyd");
2613 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2614 assert_string_equal(parsed->child->next->name, "anyx");
2615 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2616 assert_string_equal(parsed->child->next->next->name, "subcont");
2617 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
2618 assert_string_equal(parsed->child->next->next->next->name, "leaf");
2619 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
2620 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
2621 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
2622 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
2623 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
2624 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
2625 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002626 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
2627 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2628 assert_null(parsed->child->next->next->next->next->next->next->next->next);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002629 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_CASE);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002630 lysp_node_free(&fctx, siblings);
David Sedlák5379d392019-07-24 10:42:03 +02002631 siblings = NULL;
2632
2633 /* min subelems */
2634 data = ELEMENT_WRAPPER_START "<case name=\"case-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002635 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák5379d392019-07-24 10:42:03 +02002636 parsed = (struct lysp_node_case *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002637 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2638 "case-name", 0, LYS_CASE, 0, NULL, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002639 lysp_node_free(&fctx, siblings);
David Sedlák5379d392019-07-24 10:42:03 +02002640 siblings = NULL;
David Sedlák5379d392019-07-24 10:42:03 +02002641}
2642
David Sedlákb7abcfa2019-07-24 12:33:35 +02002643static void
2644test_choice_elem(void **state)
2645{
David Sedlákb7abcfa2019-07-24 12:33:35 +02002646 const char *data;
2647 struct lysp_node *siblings = NULL;
2648 struct tree_node_meta node_meta = {NULL, &siblings};
2649 struct lysp_node_choice *parsed = NULL;
2650
2651 /* max subelems */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002652 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002653 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002654 "<choice name=\"choice-name\">\n"
2655 " <anydata name=\"anyd\"/>\n"
2656 " <anyxml name=\"anyx\"/>\n"
2657 " <case name=\"sub-case\"/>\n"
2658 " <choice name=\"choice\"/>\n"
2659 " <config value=\"true\"/>\n"
2660 " <container name=\"subcont\"/>\n"
2661 " <default value=\"def\"/>\n"
2662 " <description><text>desc</text></description>\n"
2663 " <if-feature name=\"iff\"/>\n"
2664 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2665 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2666 " <list name=\"list\"/>\n"
2667 " <mandatory value=\"true\" />\n"
2668 " <reference><text>ref</text></reference>\n"
2669 " <status value=\"current\"/>\n"
2670 " <when condition=\"when-cond\"/>\n"
2671 EXT_SUBELEM
2672 "</choice>"
2673 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002674 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002675 parsed = (struct lysp_node_choice *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002676 uint16_t flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_CURR;
2677
2678 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2679 "choice-name", 0, LYS_CHOICE, 0, "ref", 1);
2680 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002681 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlákb7abcfa2019-07-24 12:33:35 +02002682 assert_string_equal(parsed->child->name, "anyd");
2683 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2684 assert_string_equal(parsed->child->next->name, "anyx");
2685 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2686 assert_string_equal(parsed->child->next->next->name, "sub-case");
2687 assert_int_equal(parsed->child->next->next->nodetype, LYS_CASE);
2688 assert_string_equal(parsed->child->next->next->next->name, "choice");
2689 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
2690 assert_string_equal(parsed->child->next->next->next->next->name, "subcont");
2691 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_CONTAINER);
2692 assert_string_equal(parsed->child->next->next->next->next->next->name, "leaf");
2693 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAF);
2694 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "llist");
2695 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2696 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "list");
2697 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
2698 assert_null(parsed->child->next->next->next->next->next->next->next->next);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002699 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_CHOICE);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002700 lysp_node_free(&fctx, siblings);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002701 siblings = NULL;
2702
2703 /* min subelems */
2704 data = ELEMENT_WRAPPER_START "<choice name=\"choice-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002705 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002706 parsed = (struct lysp_node_choice *)siblings;
2707 assert_string_equal(parsed->name, "choice-name");
Radek Iša56ca9e42020-09-08 18:42:00 +02002708 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2709 "choice-name", 0, LYS_CHOICE, 0, NULL, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002710 lysp_node_free(&fctx, siblings);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002711 siblings = NULL;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002712}
2713
David Sedlák05404f62019-07-24 14:11:53 +02002714static void
2715test_inout_elem(void **state)
2716{
David Sedlák05404f62019-07-24 14:11:53 +02002717 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02002718 struct lysp_node_action_inout inout = {0};
David Sedlák05404f62019-07-24 14:11:53 +02002719 struct inout_meta inout_meta = {NULL, &inout};
2720
2721 /* max subelements */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002722 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlák05404f62019-07-24 14:11:53 +02002723 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002724 "<input>\n"
2725 " <anydata name=\"anyd\"/>\n"
2726 " <anyxml name=\"anyx\"/>\n"
2727 " <choice name=\"choice\"/>\n"
2728 " <container name=\"subcont\"/>\n"
2729 " <grouping name=\"sub-grp\"/>\n"
2730 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2731 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2732 " <list name=\"list\"/>\n"
2733 " <must condition=\"cond\"/>\n"
2734 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2735 " <uses name=\"uses-name\"/>\n"
2736 EXT_SUBELEM
2737 "</input>"
2738 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002739 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
2740 CHECK_LYSP_ACTION_INOUT(&(inout), 1, 1, 1, 1, LYS_INPUT, 0, 1);
2741 CHECK_LYSP_RESTR(inout.musts, "cond", NULL, NULL, NULL, 0, NULL);
David Sedlák05404f62019-07-24 14:11:53 +02002742 assert_string_equal(inout.typedefs->name, "tpdf");
2743 assert_string_equal(inout.groupings->name, "sub-grp");
Radek Krejci01180ac2021-01-27 08:48:22 +01002744 assert_string_equal(inout.child->name, "anyd");
2745 assert_int_equal(inout.child->nodetype, LYS_ANYDATA);
2746 assert_string_equal(inout.child->next->name, "anyx");
2747 assert_int_equal(inout.child->next->nodetype, LYS_ANYXML);
2748 assert_string_equal(inout.child->next->next->name, "choice");
2749 assert_int_equal(inout.child->next->next->nodetype, LYS_CHOICE);
2750 assert_string_equal(inout.child->next->next->next->name, "subcont");
2751 assert_int_equal(inout.child->next->next->next->nodetype, LYS_CONTAINER);
2752 assert_string_equal(inout.child->next->next->next->next->name, "leaf");
2753 assert_int_equal(inout.child->next->next->next->next->nodetype, LYS_LEAF);
2754 assert_string_equal(inout.child->next->next->next->next->next->name, "llist");
2755 assert_int_equal(inout.child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2756 assert_string_equal(inout.child->next->next->next->next->next->next->name, "list");
2757 assert_int_equal(inout.child->next->next->next->next->next->next->nodetype, LYS_LIST);
2758 assert_string_equal(inout.child->next->next->next->next->next->next->next->name, "uses-name");
2759 assert_int_equal(inout.child->next->next->next->next->next->next->next->nodetype, LYS_USES);
2760 assert_null(inout.child->next->next->next->next->next->next->next->next);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002761 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(inout.exts[0]), LY_STMT_INPUT);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002762 lysp_node_free(&fctx, (struct lysp_node *)&inout);
David Sedlák05404f62019-07-24 14:11:53 +02002763 memset(&inout, 0, sizeof inout);
2764
2765 /* max subelements */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002766 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlák05404f62019-07-24 14:11:53 +02002767 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002768 "<output>\n"
2769 " <anydata name=\"anyd\"/>\n"
2770 " <anyxml name=\"anyx\"/>\n"
2771 " <choice name=\"choice\"/>\n"
2772 " <container name=\"subcont\"/>\n"
2773 " <grouping name=\"sub-grp\"/>\n"
2774 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2775 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2776 " <list name=\"list\"/>\n"
2777 " <must condition=\"cond\"/>\n"
2778 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2779 " <uses name=\"uses-name\"/>\n"
2780 EXT_SUBELEM
2781 "</output>"
2782 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002783 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
2784 CHECK_LYSP_ACTION_INOUT(&(inout), 1, 1, 1, 1, LYS_OUTPUT, 0, 1);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002785 assert_string_equal(inout.musts->arg.str, "cond");
David Sedlák05404f62019-07-24 14:11:53 +02002786 assert_string_equal(inout.typedefs->name, "tpdf");
2787 assert_string_equal(inout.groupings->name, "sub-grp");
Radek Krejci01180ac2021-01-27 08:48:22 +01002788 assert_string_equal(inout.child->name, "anyd");
2789 assert_int_equal(inout.child->nodetype, LYS_ANYDATA);
2790 assert_string_equal(inout.child->next->name, "anyx");
2791 assert_int_equal(inout.child->next->nodetype, LYS_ANYXML);
2792 assert_string_equal(inout.child->next->next->name, "choice");
2793 assert_int_equal(inout.child->next->next->nodetype, LYS_CHOICE);
2794 assert_string_equal(inout.child->next->next->next->name, "subcont");
2795 assert_int_equal(inout.child->next->next->next->nodetype, LYS_CONTAINER);
2796 assert_string_equal(inout.child->next->next->next->next->name, "leaf");
2797 assert_int_equal(inout.child->next->next->next->next->nodetype, LYS_LEAF);
2798 assert_string_equal(inout.child->next->next->next->next->next->name, "llist");
2799 assert_int_equal(inout.child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2800 assert_string_equal(inout.child->next->next->next->next->next->next->name, "list");
2801 assert_int_equal(inout.child->next->next->next->next->next->next->nodetype, LYS_LIST);
2802 assert_string_equal(inout.child->next->next->next->next->next->next->next->name, "uses-name");
2803 assert_int_equal(inout.child->next->next->next->next->next->next->next->nodetype, LYS_USES);
2804 assert_null(inout.child->next->next->next->next->next->next->next->next);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002805 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(inout.exts[0]), LY_STMT_OUTPUT);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002806 lysp_node_free(&fctx, (struct lysp_node *)&inout);
David Sedlák05404f62019-07-24 14:11:53 +02002807 memset(&inout, 0, sizeof inout);
2808
2809 /* min subelems */
Michal Vaskob83af8a2020-01-06 09:49:22 +01002810 data = ELEMENT_WRAPPER_START "<input><leaf name=\"l\"><type name=\"empty\"/></leaf></input>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002811 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002812 lysp_node_free(&fctx, (struct lysp_node *)&inout);
David Sedlák05404f62019-07-24 14:11:53 +02002813 memset(&inout, 0, sizeof inout);
2814
Michal Vaskob83af8a2020-01-06 09:49:22 +01002815 data = ELEMENT_WRAPPER_START "<output><leaf name=\"l\"><type name=\"empty\"/></leaf></output>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002816 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002817 lysp_node_free(&fctx, (struct lysp_node *)&inout);
David Sedlák05404f62019-07-24 14:11:53 +02002818 memset(&inout, 0, sizeof inout);
2819
2820 /* invalid combinations */
2821 data = ELEMENT_WRAPPER_START "<input name=\"test\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002822 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_EVALID);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002823 lysp_node_free(&fctx, (struct lysp_node *)&inout);
Radek Iša56ca9e42020-09-08 18:42:00 +02002824 CHECK_LOG_CTX("Unexpected attribute \"name\" of \"input\" element.", "Line number 1.");
David Sedlák05404f62019-07-24 14:11:53 +02002825 memset(&inout, 0, sizeof inout);
David Sedlák05404f62019-07-24 14:11:53 +02002826}
2827
David Sedlák85d0eca2019-07-24 15:15:21 +02002828static void
2829test_action_elem(void **state)
2830{
David Sedlák85d0eca2019-07-24 15:15:21 +02002831 const char *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002832 struct lysp_node_action *actions = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002833 struct tree_node_meta act_meta = {NULL, (struct lysp_node **)&actions};
Radek Iša56ca9e42020-09-08 18:42:00 +02002834 uint16_t flags;
David Sedlák85d0eca2019-07-24 15:15:21 +02002835
2836 /* max subelems */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002837 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlák85d0eca2019-07-24 15:15:21 +02002838 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002839 "<action name=\"act\">\n"
2840 " <description><text>desc</text></description>\n"
2841 " <grouping name=\"grouping\"/>\n"
2842 " <if-feature name=\"iff\"/>\n"
2843 " <input><uses name=\"uses-name\"/></input>\n"
2844 " <output><must condition=\"cond\"/><leaf name=\"l\"><type name=\"type\"/></leaf></output>\n"
2845 " <reference><text>ref</text></reference>\n"
2846 " <status value=\"deprecated\"/>\n"
2847 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2848 EXT_SUBELEM
2849 "</action>"
2850 ELEMENT_WRAPPER_END;
Michal Vasko1bf09392020-03-27 12:38:10 +01002851 /* there must be parent for action */
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002852 act_meta.parent = (void *)1;
Radek Iša56ca9e42020-09-08 18:42:00 +02002853 assert_int_equal(test_element_helper(state, data, &act_meta, NULL, NULL), LY_SUCCESS);
Michal Vasko1bf09392020-03-27 12:38:10 +01002854 act_meta.parent = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02002855 flags = LYS_STATUS_DEPRC;
2856 CHECK_LYSP_ACTION(actions, "desc", 1, flags, 1, 1,\
2857 1, 0, 0, 0,\
2858 1, 0,\
2859 "act", LYS_ACTION, \
2860 1, 0, 0, 1,\
2861 1, 0,\
2862 1, "ref", 1);
2863
Michal Vasko7f45cf22020-10-01 12:49:44 +02002864 assert_string_equal(actions->iffeatures[0].str, "iff");
David Sedlák85d0eca2019-07-24 15:15:21 +02002865 assert_string_equal(actions->typedefs->name, "tpdf");
2866 assert_string_equal(actions->groupings->name, "grouping");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002867 assert_string_equal(actions->output.musts->arg.str, "cond");
Radek Krejci01180ac2021-01-27 08:48:22 +01002868 assert_string_equal(actions->input.child->name, "uses-name");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002869 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(actions->exts[0]), LY_STMT_ACTION);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002870 lysp_node_free(&fctx, (struct lysp_node *)actions);
David Sedlák85d0eca2019-07-24 15:15:21 +02002871 actions = NULL;
2872
Michal Vasko8a67eff2021-12-07 14:04:47 +01002873 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlákeaa45792019-07-24 15:25:01 +02002874 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002875 "<rpc name=\"act\">\n"
2876 " <description><text>desc</text></description>\n"
2877 " <grouping name=\"grouping\"/>\n"
2878 " <if-feature name=\"iff\"/>\n"
2879 " <input><uses name=\"uses-name\"/></input>\n"
2880 " <output><must condition=\"cond\"/><leaf name=\"l\"><type name=\"type\"/></leaf></output>\n"
2881 " <reference><text>ref</text></reference>\n"
2882 " <status value=\"deprecated\"/>\n"
2883 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2884 EXT_SUBELEM
2885 "</rpc>"
2886 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002887 assert_int_equal(test_element_helper(state, data, &act_meta, NULL, NULL), LY_SUCCESS);
2888 flags = LYS_STATUS_DEPRC;
2889 CHECK_LYSP_ACTION(actions, "desc", 1, flags, 1, 1,\
2890 1, 0, 0, 0,\
2891 1, 0,\
2892 "act", LYS_RPC, \
2893 1, 0, 0, 1,\
2894 1, 0,\
2895 0, "ref", 1);
2896
Michal Vasko7f45cf22020-10-01 12:49:44 +02002897 assert_string_equal(actions->iffeatures[0].str, "iff");
David Sedlákeaa45792019-07-24 15:25:01 +02002898 assert_string_equal(actions->typedefs->name, "tpdf");
2899 assert_string_equal(actions->groupings->name, "grouping");
Radek Krejci01180ac2021-01-27 08:48:22 +01002900 assert_string_equal(actions->input.child->name, "uses-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002901 assert_string_equal(actions->output.musts->arg.str, "cond");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002902 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(actions->exts[0]), LY_STMT_RPC);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002903 lysp_node_free(&fctx, (struct lysp_node *)actions);
David Sedlákeaa45792019-07-24 15:25:01 +02002904 actions = NULL;
2905
David Sedlák85d0eca2019-07-24 15:15:21 +02002906 /* min subelems */
2907 data = ELEMENT_WRAPPER_START "<action name=\"act\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002908 assert_int_equal(test_element_helper(state, data, &act_meta, NULL, NULL), LY_SUCCESS);
David Sedlák85d0eca2019-07-24 15:15:21 +02002909 assert_string_equal(actions->name, "act");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002910 lysp_node_free(&fctx, (struct lysp_node *)actions);
David Sedlák85d0eca2019-07-24 15:15:21 +02002911 actions = NULL;
David Sedlák85d0eca2019-07-24 15:15:21 +02002912}
2913
David Sedlák992fb7c2019-07-24 16:51:01 +02002914static void
2915test_augment_elem(void **state)
2916{
David Sedlák992fb7c2019-07-24 16:51:01 +02002917 const char *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002918 struct lysp_node_augment *augments = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002919 struct tree_node_meta aug_meta = {NULL, (struct lysp_node **)&augments};
David Sedlák992fb7c2019-07-24 16:51:01 +02002920
Michal Vasko8a67eff2021-12-07 14:04:47 +01002921 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlák992fb7c2019-07-24 16:51:01 +02002922 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002923 "<augment target-node=\"target\">\n"
2924 " <action name=\"action\"/>\n"
2925 " <anydata name=\"anyd\"/>\n"
2926 " <anyxml name=\"anyx\"/>\n"
2927 " <case name=\"case\"/>\n"
2928 " <choice name=\"choice\"/>\n"
2929 " <container name=\"subcont\"/>\n"
2930 " <description><text>desc</text></description>\n"
2931 " <if-feature name=\"iff\"/>\n"
2932 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2933 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2934 " <list name=\"list\"/>\n"
2935 " <notification name=\"notif\"/>\n"
2936 " <reference><text>ref</text></reference>\n"
2937 " <status value=\"current\"/>\n"
2938 " <uses name=\"uses\"/>\n"
2939 " <when condition=\"when-cond\"/>\n"
2940 EXT_SUBELEM
2941 "</augment>"
2942 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002943 assert_int_equal(test_element_helper(state, data, &aug_meta, NULL, NULL), LY_SUCCESS);
David Sedlák992fb7c2019-07-24 16:51:01 +02002944 assert_string_equal(augments->nodeid, "target");
2945 assert_null(augments->parent);
2946 assert_int_equal(augments->nodetype, LYS_AUGMENT);
2947 assert_true(augments->flags & LYS_STATUS_CURR);
2948 assert_string_equal(augments->dsc, "desc");
2949 assert_string_equal(augments->ref, "ref");
2950 assert_string_equal(augments->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002951 assert_string_equal(augments->iffeatures[0].str, "iff");
David Sedlák992fb7c2019-07-24 16:51:01 +02002952 assert_string_equal(augments->child->name, "anyd");
2953 assert_int_equal(augments->child->nodetype, LYS_ANYDATA);
2954 assert_string_equal(augments->child->next->name, "anyx");
2955 assert_int_equal(augments->child->next->nodetype, LYS_ANYXML);
2956 assert_string_equal(augments->child->next->next->name, "case");
2957 assert_int_equal(augments->child->next->next->nodetype, LYS_CASE);
2958 assert_string_equal(augments->child->next->next->next->name, "choice");
2959 assert_int_equal(augments->child->next->next->next->nodetype, LYS_CHOICE);
2960 assert_string_equal(augments->child->next->next->next->next->name, "subcont");
2961 assert_int_equal(augments->child->next->next->next->next->nodetype, LYS_CONTAINER);
2962 assert_string_equal(augments->child->next->next->next->next->next->name, "leaf");
2963 assert_int_equal(augments->child->next->next->next->next->next->nodetype, LYS_LEAF);
2964 assert_string_equal(augments->child->next->next->next->next->next->next->name, "llist");
2965 assert_int_equal(augments->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2966 assert_string_equal(augments->child->next->next->next->next->next->next->next->name, "list");
2967 assert_int_equal(augments->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
2968 assert_string_equal(augments->child->next->next->next->next->next->next->next->next->name, "uses");
2969 assert_int_equal(augments->child->next->next->next->next->next->next->next->next->nodetype, LYS_USES);
2970 assert_null(augments->child->next->next->next->next->next->next->next->next->next);
2971 assert_string_equal(augments->actions->name, "action");
2972 assert_string_equal(augments->notifs->name, "notif");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002973 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(augments->exts[0]), LY_STMT_AUGMENT);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002974 lysp_node_free(&fctx, (struct lysp_node *)augments);
David Sedlák992fb7c2019-07-24 16:51:01 +02002975 augments = NULL;
2976
2977 data = ELEMENT_WRAPPER_START "<augment target-node=\"target\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002978 assert_int_equal(test_element_helper(state, data, &aug_meta, NULL, NULL), LY_SUCCESS);
David Sedlák992fb7c2019-07-24 16:51:01 +02002979 assert_string_equal(augments->nodeid, "target");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002980 lysp_node_free(&fctx, (struct lysp_node *)augments);
David Sedlák992fb7c2019-07-24 16:51:01 +02002981 augments = NULL;
David Sedlák992fb7c2019-07-24 16:51:01 +02002982}
2983
David Sedlák4ffcec82019-07-25 15:10:21 +02002984static void
2985test_deviate_elem(void **state)
2986{
David Sedlák4ffcec82019-07-25 15:10:21 +02002987 const char *data;
2988 struct lysp_deviate *deviates = NULL;
David Sedlák4ffcec82019-07-25 15:10:21 +02002989
2990 /* invalid arguments */
2991 data = ELEMENT_WRAPPER_START "<deviate value=\"\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002992 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
2993 CHECK_LOG_CTX("Invalid value \"\" of \"value\" attribute in \"deviate\" element. "
2994 "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", "Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02002995 deviates = NULL;
2996
2997 data = ELEMENT_WRAPPER_START "<deviate value=\"invalid\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002998 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
2999 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"deviate\" element. "
3000 "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", "Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003001 deviates = NULL;
3002
3003 data = ELEMENT_WRAPPER_START "<deviate value=\"ad\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003004 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
3005 CHECK_LOG_CTX("Invalid value \"ad\" of \"value\" attribute in \"deviate\" element. "
3006 "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", "Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003007 deviates = NULL;
3008
3009 data = ELEMENT_WRAPPER_START "<deviate value=\"adds\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003010 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
3011 CHECK_LOG_CTX("Invalid value \"adds\" of \"value\" attribute in \"deviate\" element. "
3012 "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", "Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003013 deviates = NULL;
3014
3015 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003016 "<deviate value=\"not-supported\">\n"
3017 " <must condition=\"c\"/>\n"
3018 "</deviate>"
3019 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003020 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
3021 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 +02003022}
3023
David Sedlák8b754462019-07-25 16:22:13 +02003024static void
3025test_deviation_elem(void **state)
3026{
David Sedlák8b754462019-07-25 16:22:13 +02003027 const char *data;
3028 struct lysp_deviation *deviations = NULL;
3029
David Sedlák8b754462019-07-25 16:22:13 +02003030 /* invalid */
3031 data = ELEMENT_WRAPPER_START "<deviation target-node=\"target\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003032 assert_int_equal(test_element_helper(state, data, &deviations, NULL, NULL), LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +02003033 CHECK_LOG_CTX("Missing mandatory sub-element \"deviate\" of \"deviation\" element.", "Line number 1.");
Michal Vasko12ef5362022-09-16 15:13:58 +02003034}
David Sedlák8b754462019-07-25 16:22:13 +02003035
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003036static struct lysp_module *
Michal Vaskod0625d72022-10-06 15:02:50 +02003037mod_renew(struct lysp_yin_ctx *ctx)
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003038{
Michal Vasko8a67eff2021-12-07 14:04:47 +01003039 struct ly_ctx *ly_ctx = PARSER_CUR_PMOD(ctx)->mod->ctx;
3040 struct lysp_module *pmod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003041
Michal Vaskoc636ea42022-09-16 10:20:31 +02003042 lys_module_free(&fctx, PARSER_CUR_PMOD(ctx)->mod, 0);
Michal Vasko8a67eff2021-12-07 14:04:47 +01003043 pmod = calloc(1, sizeof *pmod);
3044 ctx->parsed_mods->objs[0] = pmod;
3045 pmod->mod = calloc(1, sizeof *pmod->mod);
3046 pmod->mod->parsed = pmod;
3047 pmod->mod->ctx = ly_ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003048
Michal Vaskoc636ea42022-09-16 10:20:31 +02003049 fctx.mod = pmod->mod;
3050
Michal Vasko8a67eff2021-12-07 14:04:47 +01003051 return pmod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003052}
3053
David Sedlák4f03b932019-07-26 13:01:47 +02003054static void
3055test_module_elem(void **state)
3056{
Michal Vaskob36053d2020-03-26 15:49:30 +01003057 const char *data;
Radek Iša56ca9e42020-09-08 18:42:00 +02003058 struct lysp_module *lysp_mod = mod_renew(YCTX);
David Sedlák4f03b932019-07-26 13:01:47 +02003059
3060 /* max subelems */
David Sedlák4f03b932019-07-26 13:01:47 +02003061 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003062 " <yang-version value=\"1.1\"/>\n"
3063 " <namespace uri=\"ns\"/>\n"
3064 " <prefix value=\"pref\"/>\n"
3065 " <include module=\"b-mod\"/>\n"
3066 " <import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
3067 " <organization><text>org</text></organization>\n"
3068 " <contact><text>contact</text></contact>\n"
3069 " <description><text>desc</text></description>\n"
3070 " <reference><text>ref</text></reference>\n"
3071 " <revision date=\"2019-02-02\"/>\n"
3072 " <anydata name=\"anyd\"/>\n"
3073 " <anyxml name=\"anyx\"/>\n"
3074 " <choice name=\"choice\"/>\n"
3075 " <container name=\"cont\"/>\n"
3076 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3077 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3078 " <list name=\"sub-list\"/>\n"
3079 " <uses name=\"uses-name\"/>\n"
3080 " <augment target-node=\"target\"/>\n"
3081 " <deviation target-node=\"target\">\n"
3082 " <deviate value=\"not-supported\"/>\n"
3083 " </deviation>\n"
3084 " <extension name=\"ext\"/>\n"
3085 " <feature name=\"feature\"/>\n"
3086 " <grouping name=\"grp\"/>\n"
3087 " <identity name=\"ident-name\"/>\n"
3088 " <notification name=\"notf\"/>\n"
3089 " <rpc name=\"rpc-name\"/>\n"
3090 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3091 EXT_SUBELEM "\n"
3092 "</module>\n";
Radek Iša56ca9e42020-09-08 18:42:00 +02003093 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3094 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01003095
Radek Iša56ca9e42020-09-08 18:42:00 +02003096 assert_int_equal(yin_parse_mod(YCTX, lysp_mod), LY_SUCCESS);
David Sedlák4f03b932019-07-26 13:01:47 +02003097 assert_string_equal(lysp_mod->mod->name, "mod");
3098 assert_string_equal(lysp_mod->revs, "2019-02-02");
3099 assert_string_equal(lysp_mod->mod->ns, "ns");
3100 assert_string_equal(lysp_mod->mod->prefix, "pref");
3101 assert_null(lysp_mod->mod->filepath);
3102 assert_string_equal(lysp_mod->mod->org, "org");
3103 assert_string_equal(lysp_mod->mod->contact, "contact");
3104 assert_string_equal(lysp_mod->mod->dsc, "desc");
3105 assert_string_equal(lysp_mod->mod->ref, "ref");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003106 assert_int_equal(lysp_mod->version, LYS_VERSION_1_1);
Radek Iša56ca9e42020-09-08 18:42:00 +02003107 CHECK_LYSP_IMPORT(lysp_mod->imports, NULL, 0, "a-mod",
3108 "imp-pref", NULL, "");
David Sedlák4f03b932019-07-26 13:01:47 +02003109 assert_string_equal(lysp_mod->includes->name, "b-mod");
3110 assert_string_equal(lysp_mod->extensions->name, "ext");
3111 assert_string_equal(lysp_mod->features->name, "feature");
3112 assert_string_equal(lysp_mod->identities->name, "ident-name");
3113 assert_string_equal(lysp_mod->typedefs->name, "tpdf");
3114 assert_string_equal(lysp_mod->groupings->name, "grp");
3115 assert_string_equal(lysp_mod->data->name, "anyd");
3116 assert_int_equal(lysp_mod->data->nodetype, LYS_ANYDATA);
3117 assert_string_equal(lysp_mod->data->next->name, "anyx");
3118 assert_int_equal(lysp_mod->data->next->nodetype, LYS_ANYXML);
3119 assert_string_equal(lysp_mod->data->next->next->name, "choice");
3120 assert_int_equal(lysp_mod->data->next->next->nodetype, LYS_CHOICE);
3121 assert_string_equal(lysp_mod->data->next->next->next->name, "cont");
3122 assert_int_equal(lysp_mod->data->next->next->next->nodetype, LYS_CONTAINER);
3123 assert_string_equal(lysp_mod->data->next->next->next->next->name, "leaf");
3124 assert_int_equal(lysp_mod->data->next->next->next->next->nodetype, LYS_LEAF);
3125 assert_string_equal(lysp_mod->data->next->next->next->next->next->name, "llist");
3126 assert_int_equal(lysp_mod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3127 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->name, "sub-list");
3128 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
3129 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->next->name, "uses-name");
3130 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3131 assert_null(lysp_mod->data->next->next->next->next->next->next->next->next);
3132 assert_string_equal(lysp_mod->augments->nodeid, "target");
3133 assert_string_equal(lysp_mod->rpcs->name, "rpc-name");
3134 assert_string_equal(lysp_mod->notifs->name, "notf");
3135 assert_string_equal(lysp_mod->deviations->nodeid, "target");
Radek Krejci39b7fc22021-02-26 23:29:18 +01003136 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(lysp_mod->exts[0]), LY_STMT_MODULE);
David Sedlák4f03b932019-07-26 13:01:47 +02003137
3138 /* min subelems */
Radek Iša56ca9e42020-09-08 18:42:00 +02003139 ly_in_free(UTEST_IN, 0);
3140 lyxml_ctx_free(YCTX->xmlctx);
3141 lysp_mod = mod_renew(YCTX);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003142 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
3143 " <namespace uri=\"ns\"/>\n"
3144 " <prefix value=\"pref\"/>\n"
3145 " <yang-version value=\"1.1\"/>\n"
3146 "</module>";
Radek Iša56ca9e42020-09-08 18:42:00 +02003147 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3148 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
3149 assert_int_equal(yin_parse_mod(YCTX, lysp_mod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003150 assert_string_equal(lysp_mod->mod->name, "mod");
David Sedlák298ff6d2019-07-26 14:29:03 +02003151
David Sedláke6cd89e2019-08-07 12:46:02 +02003152 /* incorrect subelem order */
Radek Iša56ca9e42020-09-08 18:42:00 +02003153 ly_in_free(UTEST_IN, 0);
3154 lyxml_ctx_free(YCTX->xmlctx);
3155 lysp_mod = mod_renew(YCTX);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003156 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
3157 " <feature name=\"feature\"/>\n"
3158 " <namespace uri=\"ns\"/>\n"
3159 " <prefix value=\"pref\"/>\n"
3160 " <yang-version value=\"1.1\"/>\n"
3161 "</module>";
Radek Iša56ca9e42020-09-08 18:42:00 +02003162 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3163 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
3164 assert_int_equal(yin_parse_mod(YCTX, lysp_mod), LY_EVALID);
3165 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 +02003166}
3167
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003168static struct lysp_submodule *
Michal Vaskod0625d72022-10-06 15:02:50 +02003169submod_renew(struct lysp_yin_ctx *ctx, const char *belongs_to)
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003170{
Michal Vasko8a67eff2021-12-07 14:04:47 +01003171 struct ly_ctx *ly_ctx = PARSER_CUR_PMOD(ctx)->mod->ctx;
3172 struct lysp_submodule *submod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003173
Michal Vaskoc636ea42022-09-16 10:20:31 +02003174 lys_module_free(&fctx, PARSER_CUR_PMOD(ctx)->mod, 0);
Michal Vasko8a67eff2021-12-07 14:04:47 +01003175 submod = calloc(1, sizeof *submod);
3176 ctx->parsed_mods->objs[0] = submod;
3177 submod->mod = calloc(1, sizeof *submod->mod);
3178 lydict_insert(ly_ctx, belongs_to, 0, &submod->mod->name);
3179 submod->mod->parsed = (struct lysp_module *)submod;
3180 submod->mod->ctx = ly_ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003181
Michal Vaskoc636ea42022-09-16 10:20:31 +02003182 fctx.mod = submod->mod;
3183
Michal Vasko8a67eff2021-12-07 14:04:47 +01003184 return submod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003185}
3186
David Sedlák298ff6d2019-07-26 14:29:03 +02003187static void
3188test_submodule_elem(void **state)
3189{
Michal Vaskob36053d2020-03-26 15:49:30 +01003190 const char *data;
Radek Iša56ca9e42020-09-08 18:42:00 +02003191 struct lysp_submodule *lysp_submod = submod_renew(YCTX, "module-name");
David Sedlák298ff6d2019-07-26 14:29:03 +02003192
3193 /* max subelements */
David Sedlák298ff6d2019-07-26 14:29:03 +02003194 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003195 " <yang-version value=\"1.1\"/>\n"
3196 " <belongs-to module=\"module-name\">\n"
3197 " <prefix value=\"pref\"/>\n"
3198 " </belongs-to>\n"
3199 " <include module=\"b-mod\"/>\n"
3200 " <import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
3201 " <organization><text>org</text></organization>\n"
3202 " <contact><text>contact</text></contact>\n"
3203 " <description><text>desc</text></description>\n"
3204 " <reference><text>ref</text></reference>\n"
3205 " <revision date=\"2019-02-02\"/>\n"
3206 " <anydata name=\"anyd\"/>\n"
3207 " <anyxml name=\"anyx\"/>\n"
3208 " <choice name=\"choice\"/>\n"
3209 " <container name=\"cont\"/>\n"
3210 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3211 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3212 " <list name=\"sub-list\"/>\n"
3213 " <uses name=\"uses-name\"/>\n"
3214 " <augment target-node=\"target\"/>\n"
3215 " <deviation target-node=\"target\">\n"
3216 " <deviate value=\"not-supported\"/>\n"
3217 " </deviation>\n"
3218 " <extension name=\"ext\"/>\n"
3219 " <feature name=\"feature\"/>\n"
3220 " <grouping name=\"grp\"/>\n"
3221 " <identity name=\"ident-name\"/>\n"
3222 " <notification name=\"notf\"/>\n"
3223 " <rpc name=\"rpc-name\"/>\n"
3224 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3225 EXT_SUBELEM "\n"
3226 "</submodule>\n";
Radek Iša56ca9e42020-09-08 18:42:00 +02003227 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3228 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003229
Radek Iša56ca9e42020-09-08 18:42:00 +02003230 assert_int_equal(yin_parse_submod(YCTX, lysp_submod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003231 assert_string_equal(lysp_submod->name, "mod");
3232 assert_string_equal(lysp_submod->revs, "2019-02-02");
David Sedlák298ff6d2019-07-26 14:29:03 +02003233 assert_string_equal(lysp_submod->prefix, "pref");
3234 assert_null(lysp_submod->filepath);
3235 assert_string_equal(lysp_submod->org, "org");
3236 assert_string_equal(lysp_submod->contact, "contact");
3237 assert_string_equal(lysp_submod->dsc, "desc");
3238 assert_string_equal(lysp_submod->ref, "ref");
3239 assert_int_equal(lysp_submod->version, LYS_VERSION_1_1);
Radek Iša56ca9e42020-09-08 18:42:00 +02003240 CHECK_LYSP_IMPORT(lysp_submod->imports, NULL, 0, "a-mod",
3241 "imp-pref", NULL, "");
David Sedlák298ff6d2019-07-26 14:29:03 +02003242 assert_string_equal(lysp_submod->includes->name, "b-mod");
3243 assert_string_equal(lysp_submod->extensions->name, "ext");
3244 assert_string_equal(lysp_submod->features->name, "feature");
3245 assert_string_equal(lysp_submod->identities->name, "ident-name");
3246 assert_string_equal(lysp_submod->typedefs->name, "tpdf");
3247 assert_string_equal(lysp_submod->groupings->name, "grp");
3248 assert_string_equal(lysp_submod->data->name, "anyd");
3249 assert_int_equal(lysp_submod->data->nodetype, LYS_ANYDATA);
3250 assert_string_equal(lysp_submod->data->next->name, "anyx");
3251 assert_int_equal(lysp_submod->data->next->nodetype, LYS_ANYXML);
3252 assert_string_equal(lysp_submod->data->next->next->name, "choice");
3253 assert_int_equal(lysp_submod->data->next->next->nodetype, LYS_CHOICE);
3254 assert_string_equal(lysp_submod->data->next->next->next->name, "cont");
3255 assert_int_equal(lysp_submod->data->next->next->next->nodetype, LYS_CONTAINER);
3256 assert_string_equal(lysp_submod->data->next->next->next->next->name, "leaf");
3257 assert_int_equal(lysp_submod->data->next->next->next->next->nodetype, LYS_LEAF);
3258 assert_string_equal(lysp_submod->data->next->next->next->next->next->name, "llist");
3259 assert_int_equal(lysp_submod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3260 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->name, "sub-list");
3261 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
3262 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->next->name, "uses-name");
3263 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3264 assert_null(lysp_submod->data->next->next->next->next->next->next->next->next);
3265 assert_string_equal(lysp_submod->augments->nodeid, "target");
3266 assert_string_equal(lysp_submod->rpcs->name, "rpc-name");
3267 assert_string_equal(lysp_submod->notifs->name, "notf");
3268 assert_string_equal(lysp_submod->deviations->nodeid, "target");
Radek Krejci39b7fc22021-02-26 23:29:18 +01003269 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(lysp_submod->exts[0]), LY_STMT_SUBMODULE);
David Sedlák298ff6d2019-07-26 14:29:03 +02003270
David Sedlák298ff6d2019-07-26 14:29:03 +02003271 /* min subelemnts */
Radek Iša56ca9e42020-09-08 18:42:00 +02003272 ly_in_free(UTEST_IN, 0);
3273 lyxml_ctx_free(YCTX->xmlctx);
3274 lysp_submod = submod_renew(YCTX, "module-name");
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003275 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">\n"
3276 " <yang-version value=\"1\"/>\n"
3277 " <belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>\n"
3278 "</submodule>";
Radek Iša56ca9e42020-09-08 18:42:00 +02003279 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3280 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
3281 assert_int_equal(yin_parse_submod(YCTX, lysp_submod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003282 assert_string_equal(lysp_submod->prefix, "pref");
David Sedlák298ff6d2019-07-26 14:29:03 +02003283 assert_int_equal(lysp_submod->version, LYS_VERSION_1_0);
David Sedlák298ff6d2019-07-26 14:29:03 +02003284
David Sedláke6cd89e2019-08-07 12:46:02 +02003285 /* incorrect subelem order */
Radek Iša56ca9e42020-09-08 18:42:00 +02003286 ly_in_free(UTEST_IN, 0);
3287 lyxml_ctx_free(YCTX->xmlctx);
3288 lysp_submod = submod_renew(YCTX, "module-name");
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003289 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">\n"
3290 " <yang-version value=\"1\"/>\n"
3291 " <reference><text>ref</text></reference>\n"
3292 " <belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>\n"
3293 "</submodule>";
Radek Iša56ca9e42020-09-08 18:42:00 +02003294 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3295 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
3296 assert_int_equal(yin_parse_submod(YCTX, lysp_submod), LY_EVALID);
3297 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 +02003298}
3299
David Sedlák8985a142019-07-31 16:43:06 +02003300static void
3301test_yin_parse_module(void **state)
3302{
David Sedlák8985a142019-07-31 16:43:06 +02003303 const char *data;
3304 struct lys_module *mod;
Michal Vaskod0625d72022-10-06 15:02:50 +02003305 struct lysp_yin_ctx *yin_ctx = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02003306 struct ly_in *in = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003307
3308 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003309 mod->ctx = UTEST_LYCTX;
David Sedlákd2844882019-09-13 16:01:22 +02003310 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 +01003311 " <yang-version value=\"1.1\"/>\n"
3312 " <namespace uri=\"urn:tests:extensions:metadata:a\"/>\n"
3313 " <prefix value=\"a\"/>\n"
3314 " <import module=\"ietf-yang-metadata\">\n"
3315 " <prefix value=\"md\"/>\n"
3316 " </import>\n"
3317 " <feature name=\"f\"/>\n"
3318 " <md:annotation name=\"x\">\n"
3319 " <description>\n"
3320 " <text>test</text>\n"
3321 " </description>\n"
3322 " <reference>\n"
3323 " <text>test</text>\n"
3324 " </reference>\n"
3325 " <if-feature name=\"f\"/>\n"
3326 " <status value=\"current\"/>\n"
3327 " <type name=\"uint8\"/>\n"
3328 " <units name=\"meters\"/>\n"
3329 " </md:annotation>\n"
3330 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003331 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
aPiecekc3e26142021-06-22 14:25:49 +02003332 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS);
David Sedlákd2844882019-09-13 16:01:22 +02003333 assert_null(mod->parsed->exts->child->next->child);
3334 assert_string_equal(mod->parsed->exts->child->next->arg, "test");
Michal Vaskoc636ea42022-09-16 10:20:31 +02003335 lys_module_free(&fctx, mod, 0);
Michal Vaskod0625d72022-10-06 15:02:50 +02003336 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003337 ly_in_free(in, 0);
David Sedlákd2844882019-09-13 16:01:22 +02003338 mod = NULL;
3339 yin_ctx = NULL;
3340
3341 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003342 mod->ctx = UTEST_LYCTX;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003343 data = "<module name=\"example-foo\""
3344 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
3345 " xmlns:foo=\"urn:example:foo\""
3346 " xmlns:myext=\"urn:example:extensions\">\n"
David Sedlák8985a142019-07-31 16:43:06 +02003347
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003348 " <yang-version value=\"1\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003349
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003350 " <namespace uri=\"urn:example:foo\"/>\n"
3351 " <prefix value=\"foo\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003352
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003353 " <import module=\"example-extensions\">\n"
3354 " <prefix value=\"myext\"/>\n"
3355 " </import>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003356
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003357 " <list name=\"interface\">\n"
3358 " <key value=\"name\"/>\n"
3359 " <leaf name=\"name\">\n"
3360 " <type name=\"string\"/>\n"
3361 " </leaf>\n"
3362 " <leaf name=\"mtu\">\n"
3363 " <type name=\"uint32\"/>\n"
3364 " <description>\n"
3365 " <text>The MTU of the interface.</text>\n"
3366 " </description>\n"
3367 " <myext:c-define name=\"MY_MTU\"/>\n"
3368 " </leaf>\n"
3369 " </list>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003370 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003371 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
aPiecekc3e26142021-06-22 14:25:49 +02003372 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS);
Michal Vaskoc636ea42022-09-16 10:20:31 +02003373 lys_module_free(&fctx, mod, 0);
Michal Vaskod0625d72022-10-06 15:02:50 +02003374 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003375 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02003376 mod = NULL;
3377 yin_ctx = NULL;
3378
3379 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003380 mod->ctx = UTEST_LYCTX;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003381 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3382 " <yang-version value=\"1\"/>\n"
3383 " <namespace uri=\"urn:example:foo\"/>\n"
3384 " <prefix value=\"foo\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003385 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003386 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
aPiecekc3e26142021-06-22 14:25:49 +02003387 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS);
Michal Vaskoc636ea42022-09-16 10:20:31 +02003388 lys_module_free(&fctx, mod, 0);
Michal Vaskod0625d72022-10-06 15:02:50 +02003389 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003390 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02003391 mod = NULL;
3392 yin_ctx = NULL;
3393
David Sedlák6d781b62019-08-02 15:22:52 +02003394 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003395 mod->ctx = UTEST_LYCTX;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003396 data = "<submodule name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlák8985a142019-07-31 16:43:06 +02003397 "</submodule>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003398 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
aPiecekc3e26142021-06-22 14:25:49 +02003399 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_EINVAL);
Radek Iša56ca9e42020-09-08 18:42:00 +02003400 CHECK_LOG_CTX("Input data contains submodule which cannot be parsed directly without its main module.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02003401 lys_module_free(&fctx, mod, 0);
Michal Vaskod0625d72022-10-06 15:02:50 +02003402 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003403 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02003404
David Sedlák6d781b62019-08-02 15:22:52 +02003405 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003406 mod->ctx = UTEST_LYCTX;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003407 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3408 " <yang-version value=\"1\"/>\n"
3409 " <namespace uri=\"urn:example:foo\"/>\n"
3410 " <prefix value=\"foo\"/>\n"
3411 "</module>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003412 "<module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003413 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
aPiecekc3e26142021-06-22 14:25:49 +02003414 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +02003415 CHECK_LOG_CTX("Trailing garbage \"<module>\" after module, expected end-of-input.", "Line number 6.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02003416 lys_module_free(&fctx, mod, 0);
Michal Vaskod0625d72022-10-06 15:02:50 +02003417 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003418 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02003419 mod = NULL;
3420 yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003421}
3422
3423static void
3424test_yin_parse_submodule(void **state)
3425{
David Sedlák8985a142019-07-31 16:43:06 +02003426 const char *data;
Michal Vaskod0625d72022-10-06 15:02:50 +02003427 struct lysp_yin_ctx *yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003428 struct lysp_submodule *submod = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02003429 struct ly_in *in;
David Sedlák8985a142019-07-31 16:43:06 +02003430
Michal Vasko8a67eff2021-12-07 14:04:47 +01003431 lydict_insert(UTEST_LYCTX, "a", 0, &PARSER_CUR_PMOD(YCTX)->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02003432
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003433 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003434 "<submodule name=\"asub\""
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003435 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
3436 " xmlns:a=\"urn:a\">\n"
3437 " <yang-version value=\"1\"/>\n"
3438 " <belongs-to module=\"a\">\n"
3439 " <prefix value=\"a_pref\"/>\n"
3440 " </belongs-to>\n"
3441 " <include module=\"atop\"/>\n"
3442 " <feature name=\"fox\"/>\n"
3443 " <notification name=\"bar-notif\">\n"
3444 " <if-feature name=\"bar\"/>\n"
3445 " </notification>\n"
3446 " <notification name=\"fox-notif\">\n"
3447 " <if-feature name=\"fox\"/>\n"
3448 " </notification>\n"
3449 " <augment target-node=\"/a_pref:top\">\n"
3450 " <if-feature name=\"bar\"/>\n"
3451 " <container name=\"bar-sub\"/>\n"
3452 " </augment>\n"
3453 " <augment target-node=\"/top\">\n"
3454 " <container name=\"bar-sub2\"/>\n"
3455 " </augment>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003456 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003457 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vaskod0625d72022-10-06 15:02:50 +02003458 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 +02003459 lysp_module_free(&fctx, (struct lysp_module *)submod);
Michal Vaskod0625d72022-10-06 15:02:50 +02003460 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003461 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02003462 yin_ctx = NULL;
3463 submod = NULL;
3464
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003465 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3466 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3467 " <yang-version value=\"1\"/>\n"
3468 " <belongs-to module=\"a\">\n"
3469 " <prefix value=\"a_pref\"/>\n"
3470 " </belongs-to>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003471 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003472 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vaskod0625d72022-10-06 15:02:50 +02003473 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 +02003474 lysp_module_free(&fctx, (struct lysp_module *)submod);
Michal Vaskod0625d72022-10-06 15:02:50 +02003475 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003476 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02003477 yin_ctx = NULL;
3478 submod = NULL;
3479
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003480 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003481 "<module name=\"inval\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
3482 "</module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003483 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vaskod0625d72022-10-06 15:02:50 +02003484 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 +02003485 CHECK_LOG_CTX("Input data contains module when a submodule is expected.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02003486 lysp_module_free(&fctx, (struct lysp_module *)submod);
Michal Vaskod0625d72022-10-06 15:02:50 +02003487 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003488 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02003489 yin_ctx = NULL;
3490 submod = NULL;
3491
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003492 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3493 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3494 " <yang-version value=\"1\"/>\n"
3495 " <belongs-to module=\"a\">\n"
3496 " <prefix value=\"a_pref\"/>\n"
3497 " </belongs-to>\n"
3498 "</submodule>\n"
3499 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3500 " <yang-version value=\"1\"/>\n"
3501 " <belongs-to module=\"a\">\n"
3502 " <prefix value=\"a_pref\"/>\n"
3503 " </belongs-to>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003504 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003505 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vaskod0625d72022-10-06 15:02:50 +02003506 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 +02003507 CHECK_LOG_CTX("Trailing garbage \"<submodule name...\" after submodule, expected end-of-input.", "Line number 8.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02003508 lysp_module_free(&fctx, (struct lysp_module *)submod);
Michal Vaskod0625d72022-10-06 15:02:50 +02003509 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003510 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02003511 yin_ctx = NULL;
3512 submod = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003513}
3514
David Sedlák3b4db242018-10-19 16:11:01 +02003515int
3516main(void)
3517{
3518
3519 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +02003520 UTEST(test_yin_match_keyword, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003521 UTEST(test_yin_parse_content, setup, teardown),
3522 UTEST(test_validate_value, setup, teardown),
Michal Vasko12ef5362022-09-16 15:13:58 +02003523 UTEST(test_valid_module),
3524 UTEST(test_print_module),
3525 UTEST(test_print_submodule),
David Sedlák32488102019-07-15 17:44:10 +02003526
Radek Iša56ca9e42020-09-08 18:42:00 +02003527 UTEST(test_yin_match_argument_name),
3528 cmocka_unit_test_setup_teardown(test_enum_elem, setup, teardown),
3529 cmocka_unit_test_setup_teardown(test_bit_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003530 cmocka_unit_test_setup_teardown(test_status_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003531 cmocka_unit_test_setup_teardown(test_yin_element_elem, setup, teardown),
3532 cmocka_unit_test_setup_teardown(test_yangversion_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003533 cmocka_unit_test_setup_teardown(test_argument_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003534 cmocka_unit_test_setup_teardown(test_belongsto_elem, setup, teardown),
3535 cmocka_unit_test_setup_teardown(test_config_elem, setup, teardown),
3536 cmocka_unit_test_setup_teardown(test_default_elem, setup, teardown),
3537 cmocka_unit_test_setup_teardown(test_err_app_tag_elem, setup, teardown),
3538 cmocka_unit_test_setup_teardown(test_err_msg_elem, setup, teardown),
3539 cmocka_unit_test_setup_teardown(test_fracdigits_elem, setup, teardown),
3540 cmocka_unit_test_setup_teardown(test_iffeature_elem, setup, teardown),
3541 cmocka_unit_test_setup_teardown(test_length_elem, setup, teardown),
3542 cmocka_unit_test_setup_teardown(test_modifier_elem, setup, teardown),
3543 cmocka_unit_test_setup_teardown(test_namespace_elem, setup, teardown),
3544 cmocka_unit_test_setup_teardown(test_pattern_elem, setup, teardown),
3545 cmocka_unit_test_setup_teardown(test_value_position_elem, setup, teardown),
3546 cmocka_unit_test_setup_teardown(test_prefix_elem, setup, teardown),
3547 cmocka_unit_test_setup_teardown(test_range_elem, setup, teardown),
3548 cmocka_unit_test_setup_teardown(test_reqinstance_elem, setup, teardown),
3549 cmocka_unit_test_setup_teardown(test_revision_date_elem, setup, teardown),
3550 cmocka_unit_test_setup_teardown(test_unique_elem, setup, teardown),
3551 cmocka_unit_test_setup_teardown(test_units_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003552 cmocka_unit_test_setup_teardown(test_yin_text_value_elem, setup, teardown),
3553 cmocka_unit_test_setup_teardown(test_type_elem, setup, teardown),
3554 cmocka_unit_test_setup_teardown(test_max_elems_elem, setup, teardown),
3555 cmocka_unit_test_setup_teardown(test_min_elems_elem, setup, teardown),
3556 cmocka_unit_test_setup_teardown(test_ordby_elem, setup, teardown),
3557 cmocka_unit_test_setup_teardown(test_any_elem, setup, teardown),
3558 cmocka_unit_test_setup_teardown(test_leaf_elem, setup, teardown),
3559 cmocka_unit_test_setup_teardown(test_leaf_list_elem, setup, teardown),
3560 cmocka_unit_test_setup_teardown(test_presence_elem, setup, teardown),
3561 cmocka_unit_test_setup_teardown(test_key_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003562 cmocka_unit_test_setup_teardown(test_uses_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003563 cmocka_unit_test_setup_teardown(test_list_elem, setup, teardown),
3564 cmocka_unit_test_setup_teardown(test_notification_elem, setup, teardown),
3565 cmocka_unit_test_setup_teardown(test_grouping_elem, setup, teardown),
3566 cmocka_unit_test_setup_teardown(test_container_elem, setup, teardown),
3567 cmocka_unit_test_setup_teardown(test_case_elem, setup, teardown),
3568 cmocka_unit_test_setup_teardown(test_choice_elem, setup, teardown),
3569 cmocka_unit_test_setup_teardown(test_inout_elem, setup, teardown),
3570 cmocka_unit_test_setup_teardown(test_action_elem, setup, teardown),
3571 cmocka_unit_test_setup_teardown(test_augment_elem, setup, teardown),
3572 cmocka_unit_test_setup_teardown(test_deviate_elem, setup, teardown),
3573 cmocka_unit_test_setup_teardown(test_deviation_elem, setup, teardown),
3574 cmocka_unit_test_setup_teardown(test_module_elem, setup, teardown),
3575 cmocka_unit_test_setup_teardown(test_submodule_elem, setup, teardown),
David Sedlák8985a142019-07-31 16:43:06 +02003576
Radek Iša56ca9e42020-09-08 18:42:00 +02003577 cmocka_unit_test_setup_teardown(test_yin_parse_module, setup, teardown),
3578 cmocka_unit_test_setup_teardown(test_yin_parse_submodule, setup, teardown),
David Sedlák3b4db242018-10-19 16:11:01 +02003579 };
3580
Radek Iša56ca9e42020-09-08 18:42:00 +02003581 return cmocka_run_group_tests(tests, NULL, NULL);
David Sedlák3b4db242018-10-19 16:11:01 +02003582}