blob: 0ce3abc445a2c454f9f4111f2e31c528e6d40443 [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 Vasko2bf4af42023-01-04 12:08:38 +010094
Michal Vaskod0625d72022-10-06 15:02:50 +020095LY_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 +020096 const void *parent, enum ly_stmt parent_stmt, const char **text_content, struct lysp_ext_instance **exts);
Michal Vaskod0625d72022-10-06 15:02:50 +020097LY_ERR yin_validate_value(struct lysp_yin_ctx *ctx, enum yang_arg val_type);
98enum ly_stmt yin_match_keyword(struct lysp_yin_ctx *ctx, const char *name, size_t name_len,
Michal Vaskoafac7822020-10-20 14:22:26 +020099 const char *prefix, size_t prefix_len, enum ly_stmt parrent);
Michal Vasko2bf4af42023-01-04 12:08:38 +0100100
Michal Vasko193dacd2022-10-13 08:43:05 +0200101LY_ERR yin_parse_extension_instance(struct lysp_yin_ctx *ctx, const void *parent, enum ly_stmt parent_stmt,
102 LY_ARRAY_COUNT_TYPE parent_stmt_index, struct lysp_ext_instance **exts);
Michal Vaskod0625d72022-10-06 15:02:50 +0200103LY_ERR yin_parse_element_generic(struct lysp_yin_ctx *ctx, enum ly_stmt parent, struct lysp_stmt **element);
104LY_ERR yin_parse_mod(struct lysp_yin_ctx *ctx, struct lysp_module *mod);
105LY_ERR yin_parse_submod(struct lysp_yin_ctx *ctx, struct lysp_submodule *submod);
Michal Vaskoafac7822020-10-20 14:22:26 +0200106
David Sedláke6cd89e2019-08-07 12:46:02 +0200107/* wrapping element used for mocking has nothing to do with real module structure */
108#define ELEMENT_WRAPPER_START "<status xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
109#define ELEMENT_WRAPPER_END "</status>"
110
Radek Iša56ca9e42020-09-08 18:42:00 +0200111#define TEST_1_CHECK_LYSP_EXT_INSTANCE(NODE, INSUBSTMT)\
Radek Krejci8df109d2021-04-23 12:19:08 +0200112 CHECK_LYSP_EXT_INSTANCE((NODE), NULL, 1, INSUBSTMT, 0, "myext:c-define", LY_VALUE_XML)
David Sedlák872c7b42018-10-26 13:15:20 +0200113
Michal Vaskod0625d72022-10-06 15:02:50 +0200114struct lysp_yin_ctx *YCTX;
Michal Vaskoc636ea42022-09-16 10:20:31 +0200115struct lysf_ctx fctx;
David Sedlák8e7bda82019-07-16 17:57:50 +0200116
117static int
Radek Iša56ca9e42020-09-08 18:42:00 +0200118setup_ctx(void **state)
David Sedlák8e7bda82019-07-16 17:57:50 +0200119{
Michal Vasko8a67eff2021-12-07 14:04:47 +0100120 struct lysp_module *pmod;
121
David Sedlák619db942019-07-03 14:47:30 +0200122 /* allocate parser context */
Radek Iša56ca9e42020-09-08 18:42:00 +0200123 YCTX = calloc(1, sizeof(*YCTX));
Michal Vaskoc8806c82022-12-06 10:31:24 +0100124 YCTX->main_ctx = (struct lysp_ctx *)YCTX;
Radek Iša56ca9e42020-09-08 18:42:00 +0200125 YCTX->format = LYS_IN_YIN;
Michal Vasko8a67eff2021-12-07 14:04:47 +0100126 ly_set_new(&YCTX->parsed_mods);
David Sedlák8f5bce02019-06-03 16:41:08 +0200127
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200128 /* allocate new parsed module */
Michal Vasko8a67eff2021-12-07 14:04:47 +0100129 pmod = calloc(1, sizeof *pmod);
130 ly_set_add(YCTX->parsed_mods, pmod, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200131
132 /* allocate new module */
Michal Vasko8a67eff2021-12-07 14:04:47 +0100133 pmod->mod = calloc(1, sizeof *pmod->mod);
134 pmod->mod->ctx = UTEST_LYCTX;
135 pmod->mod->parsed = pmod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200136
Radek Iša56ca9e42020-09-08 18:42:00 +0200137 return 0;
David Sedlák3b4db242018-10-19 16:11:01 +0200138}
139
140static int
Radek Iša56ca9e42020-09-08 18:42:00 +0200141setup(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +0100142{
Radek Iša56ca9e42020-09-08 18:42:00 +0200143 UTEST_SETUP;
David Sedlák68a1af12019-03-08 13:46:54 +0100144
Radek Iša56ca9e42020-09-08 18:42:00 +0200145 setup_ctx(state);
David Sedlák79e50cb2019-06-05 16:33:09 +0200146
Michal Vaskoc636ea42022-09-16 10:20:31 +0200147 fctx.ctx = UTEST_LYCTX;
148 fctx.mod = PARSER_CUR_PMOD(YCTX)->mod;
149
Radek Iša56ca9e42020-09-08 18:42:00 +0200150 return 0;
David Sedlák79e50cb2019-06-05 16:33:09 +0200151}
152
David Sedlák8985a142019-07-31 16:43:06 +0200153static int
Radek Iša56ca9e42020-09-08 18:42:00 +0200154teardown_ctx(void **UNUSED(state))
David Sedlák8985a142019-07-31 16:43:06 +0200155{
Michal Vaskoc636ea42022-09-16 10:20:31 +0200156 lys_module_free(&fctx, PARSER_CUR_PMOD(YCTX)->mod, 0);
Michal Vaskod0625d72022-10-06 15:02:50 +0200157 lysp_yin_ctx_free(YCTX);
Radek Iša56ca9e42020-09-08 18:42:00 +0200158 YCTX = NULL;
David Sedlák8985a142019-07-31 16:43:06 +0200159
Radek Iša56ca9e42020-09-08 18:42:00 +0200160 return 0;
David Sedlák8985a142019-07-31 16:43:06 +0200161}
162
Radek Iša56ca9e42020-09-08 18:42:00 +0200163static int
164teardown(void **state)
165{
166 teardown_ctx(state);
167
Michal Vaskoc636ea42022-09-16 10:20:31 +0200168 lysf_ctx_erase(&fctx);
169
Radek Iša56ca9e42020-09-08 18:42:00 +0200170 UTEST_TEARDOWN;
171
172 return 0;
173}
174
175#define RESET_STATE \
176 ly_in_free(UTEST_IN, 0); \
177 UTEST_IN = NULL; \
178 teardown_ctx(state); \
179 setup_ctx(state)
180
David Sedlák68a1af12019-03-08 13:46:54 +0100181static void
David Sedlák1bccdfa2019-06-17 15:55:27 +0200182test_yin_match_keyword(void **state)
David Sedlák3b4db242018-10-19 16:11:01 +0200183{
Michal Vaskob36053d2020-03-26 15:49:30 +0100184 const char *prefix;
185 size_t prefix_len;
Radek Iša56ca9e42020-09-08 18:42:00 +0200186
David Sedlák8f7a1172019-06-20 14:42:18 +0200187 /* create mock yin namespace in xml context */
Radek Iša56ca9e42020-09-08 18:42:00 +0200188 ly_in_new_memory("<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" />", &UTEST_IN);
189 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
190 prefix = YCTX->xmlctx->prefix;
191 prefix_len = YCTX->xmlctx->prefix_len;
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100192
Radek Iša56ca9e42020-09-08 18:42:00 +0200193 assert_int_equal(yin_match_keyword(YCTX, "anydatax", strlen("anydatax"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
194 assert_int_equal(yin_match_keyword(YCTX, "asdasd", strlen("asdasd"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
195 assert_int_equal(yin_match_keyword(YCTX, "", 0, prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
196 assert_int_equal(yin_match_keyword(YCTX, "anydata", strlen("anydata"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ANYDATA);
197 assert_int_equal(yin_match_keyword(YCTX, "anyxml", strlen("anyxml"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ANYXML);
198 assert_int_equal(yin_match_keyword(YCTX, "argument", strlen("argument"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ARGUMENT);
199 assert_int_equal(yin_match_keyword(YCTX, "augment", strlen("augment"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_AUGMENT);
200 assert_int_equal(yin_match_keyword(YCTX, "base", strlen("base"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BASE);
201 assert_int_equal(yin_match_keyword(YCTX, "belongs-to", strlen("belongs-to"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BELONGS_TO);
202 assert_int_equal(yin_match_keyword(YCTX, "bit", strlen("bit"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BIT);
203 assert_int_equal(yin_match_keyword(YCTX, "case", strlen("case"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CASE);
204 assert_int_equal(yin_match_keyword(YCTX, "choice", strlen("choice"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CHOICE);
205 assert_int_equal(yin_match_keyword(YCTX, "config", strlen("config"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONFIG);
206 assert_int_equal(yin_match_keyword(YCTX, "contact", strlen("contact"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONTACT);
207 assert_int_equal(yin_match_keyword(YCTX, "container", strlen("container"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONTAINER);
208 assert_int_equal(yin_match_keyword(YCTX, "default", strlen("default"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEFAULT);
209 assert_int_equal(yin_match_keyword(YCTX, "description", strlen("description"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DESCRIPTION);
210 assert_int_equal(yin_match_keyword(YCTX, "deviate", strlen("deviate"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEVIATE);
211 assert_int_equal(yin_match_keyword(YCTX, "deviation", strlen("deviation"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEVIATION);
212 assert_int_equal(yin_match_keyword(YCTX, "enum", strlen("enum"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ENUM);
213 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);
214 assert_int_equal(yin_match_keyword(YCTX, "error-message", strlen("error-message"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ERROR_MESSAGE);
215 assert_int_equal(yin_match_keyword(YCTX, "extension", strlen("extension"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_EXTENSION);
216 assert_int_equal(yin_match_keyword(YCTX, "feature", strlen("feature"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_FEATURE);
217 assert_int_equal(yin_match_keyword(YCTX, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_FRACTION_DIGITS);
218 assert_int_equal(yin_match_keyword(YCTX, "grouping", strlen("grouping"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_GROUPING);
219 assert_int_equal(yin_match_keyword(YCTX, "identity", strlen("identity"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IDENTITY);
220 assert_int_equal(yin_match_keyword(YCTX, "if-feature", strlen("if-feature"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IF_FEATURE);
221 assert_int_equal(yin_match_keyword(YCTX, "import", strlen("import"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IMPORT);
222 assert_int_equal(yin_match_keyword(YCTX, "include", strlen("include"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_INCLUDE);
223 assert_int_equal(yin_match_keyword(YCTX, "input", strlen("input"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_INPUT);
224 assert_int_equal(yin_match_keyword(YCTX, "key", strlen("key"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_KEY);
225 assert_int_equal(yin_match_keyword(YCTX, "leaf", strlen("leaf"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LEAF);
226 assert_int_equal(yin_match_keyword(YCTX, "leaf-list", strlen("leaf-list"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LEAF_LIST);
227 assert_int_equal(yin_match_keyword(YCTX, "length", strlen("length"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LENGTH);
228 assert_int_equal(yin_match_keyword(YCTX, "list", strlen("list"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LIST);
229 assert_int_equal(yin_match_keyword(YCTX, "mandatory", strlen("mandatory"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MANDATORY);
230 assert_int_equal(yin_match_keyword(YCTX, "max-elements", strlen("max-elements"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MAX_ELEMENTS);
231 assert_int_equal(yin_match_keyword(YCTX, "min-elements", strlen("min-elements"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MIN_ELEMENTS);
232 assert_int_equal(yin_match_keyword(YCTX, "modifier", strlen("modifier"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MODIFIER);
233 assert_int_equal(yin_match_keyword(YCTX, "module", strlen("module"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MODULE);
234 assert_int_equal(yin_match_keyword(YCTX, "must", strlen("must"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MUST);
235 assert_int_equal(yin_match_keyword(YCTX, "namespace", strlen("namespace"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NAMESPACE);
236 assert_int_equal(yin_match_keyword(YCTX, "notification", strlen("notification"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NOTIFICATION);
237 assert_int_equal(yin_match_keyword(YCTX, "ordered-by", strlen("ordered-by"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ORDERED_BY);
238 assert_int_equal(yin_match_keyword(YCTX, "organization", strlen("organization"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ORGANIZATION);
239 assert_int_equal(yin_match_keyword(YCTX, "output", strlen("output"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_OUTPUT);
240 assert_int_equal(yin_match_keyword(YCTX, "path", strlen("path"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PATH);
241 assert_int_equal(yin_match_keyword(YCTX, "pattern", strlen("pattern"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PATTERN);
242 assert_int_equal(yin_match_keyword(YCTX, "position", strlen("position"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_POSITION);
243 assert_int_equal(yin_match_keyword(YCTX, "prefix", strlen("prefix"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PREFIX);
244 assert_int_equal(yin_match_keyword(YCTX, "presence", strlen("presence"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PRESENCE);
245 assert_int_equal(yin_match_keyword(YCTX, "range", strlen("range"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_RANGE);
246 assert_int_equal(yin_match_keyword(YCTX, "reference", strlen("reference"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REFERENCE);
247 assert_int_equal(yin_match_keyword(YCTX, "refine", strlen("refine"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REFINE);
248 assert_int_equal(yin_match_keyword(YCTX, "require-instance", strlen("require-instance"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REQUIRE_INSTANCE);
249 assert_int_equal(yin_match_keyword(YCTX, "revision", strlen("revision"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REVISION);
250 assert_int_equal(yin_match_keyword(YCTX, "revision-date", strlen("revision-date"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REVISION_DATE);
251 assert_int_equal(yin_match_keyword(YCTX, "rpc", strlen("rpc"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_RPC);
252 assert_int_equal(yin_match_keyword(YCTX, "status", strlen("status"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_STATUS);
253 assert_int_equal(yin_match_keyword(YCTX, "submodule", strlen("submodule"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_SUBMODULE);
254 assert_int_equal(yin_match_keyword(YCTX, "type", strlen("type"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_TYPE);
255 assert_int_equal(yin_match_keyword(YCTX, "typedef", strlen("typedef"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_TYPEDEF);
256 assert_int_equal(yin_match_keyword(YCTX, "unique", strlen("unique"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_UNIQUE);
257 assert_int_equal(yin_match_keyword(YCTX, "units", strlen("units"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_UNITS);
258 assert_int_equal(yin_match_keyword(YCTX, "uses", strlen("uses"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_USES);
259 assert_int_equal(yin_match_keyword(YCTX, "value", strlen("value"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_VALUE);
260 assert_int_equal(yin_match_keyword(YCTX, "when", strlen("when"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_WHEN);
261 assert_int_equal(yin_match_keyword(YCTX, "yang-version", strlen("yang-version"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_YANG_VERSION);
262 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 +0200263}
David Sedlák3b4db242018-10-19 16:11:01 +0200264
David Sedlák872c7b42018-10-26 13:15:20 +0200265static void
Radek Iša56ca9e42020-09-08 18:42:00 +0200266test_yin_match_argument_name(void **UNUSED(state))
David Sedlák872c7b42018-10-26 13:15:20 +0200267{
David Sedlák060b00e2019-06-19 11:12:06 +0200268 assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN);
269 assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN);
270 assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN);
271 assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION);
272 assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE);
273 assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE);
274 assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME);
275 assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG);
276 assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
277 assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT);
278 assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI);
279 assert_int_equal(yin_match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200280}
281
David Sedlák68a1af12019-03-08 13:46:54 +0100282static void
David Sedlák555c7202019-07-04 12:14:12 +0200283test_yin_parse_content(void **state)
284{
David Sedlák555c7202019-07-04 12:14:12 +0200285 LY_ERR ret = LY_SUCCESS;
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100286 const char *data =
287 "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
288 " <myext:custom xmlns:myext=\"urn:example:extensions\">totally amazing extension</myext:custom>\n"
289 " <extension name=\"ext\">\n"
290 " <argument name=\"argname\"></argument>\n"
291 " <description><text>desc</text></description>\n"
292 " <reference><text>ref</text></reference>\n"
293 " <status value=\"deprecated\"></status>\n"
294 " </extension>\n"
295 " <text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>\n"
296 " <if-feature name=\"foo\"></if-feature>\n"
297 " <when condition=\"condition...\">\n"
298 " <reference><text>when_ref</text></reference>\n"
299 " <description><text>when_desc</text></description>\n"
300 " </when>\n"
301 " <config value=\"true\"/>\n"
302 " <error-message>\n"
303 " <value>error-msg</value>\n"
304 " </error-message>\n"
305 " <error-app-tag value=\"err-app-tag\"/>\n"
306 " <units name=\"radians\"></units>\n"
307 " <default value=\"default-value\"/>\n"
308 " <position value=\"25\"></position>\n"
309 " <value value=\"-5\"/>\n"
310 " <require-instance value=\"true\"></require-instance>\n"
311 " <range value=\"5..10\" />\n"
312 " <length value=\"baf\"/>\n"
313 " <pattern value='pattern'>\n"
314 " <modifier value='invert-match'/>\n"
315 " </pattern>\n"
316 " <enum name=\"yay\">\n"
317 " </enum>\n"
318 "</prefix>";
David Sedlák555c7202019-07-04 12:14:12 +0200319 struct lysp_ext_instance *exts = NULL;
Michal Vasko12ef5362022-09-16 15:13:58 +0200320 const char *value;
David Sedlák555c7202019-07-04 12:14:12 +0200321
322 /* test unique subelem */
323 const char *prefix_value;
Radek Krejcid6b76452019-09-03 17:03:03 +0200324 struct yin_subelement subelems2[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE},
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100325 {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_UNIQUE}};
326
David Sedláke6cd89e2019-08-07 12:46:02 +0200327 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100328 "<prefix value=\"inv_mod\" />"
329 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
330 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
331 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +0200332 ly_in_new_memory(data, &UTEST_IN);
333 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
334 lyxml_ctx_next(YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100335
Michal Vasko193dacd2022-10-13 08:43:05 +0200336 ret = yin_parse_content(YCTX, subelems2, 2, NULL, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200337 assert_int_equal(ret, LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +0200338 CHECK_LOG_CTX("Redefinition of \"text\" sub-element in \"status\" element.", "Line number 1.");
339 lydict_remove(UTEST_LYCTX, prefix_value);
340 lydict_remove(UTEST_LYCTX, value);
341 RESET_STATE;
David Sedlák555c7202019-07-04 12:14:12 +0200342
343 /* test first subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200344 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100345 "<prefix value=\"inv_mod\" />"
346 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
347 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
348 ELEMENT_WRAPPER_END;
Radek Krejcid6b76452019-09-03 17:03:03 +0200349 struct yin_subelement subelems3[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE},
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100350 {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_FIRST}};
351
Radek Iša56ca9e42020-09-08 18:42:00 +0200352 ly_in_new_memory(data, &UTEST_IN);
353 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
354 lyxml_ctx_next(YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100355
Michal Vasko193dacd2022-10-13 08:43:05 +0200356 ret = yin_parse_content(YCTX, subelems3, 2, NULL, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200357 assert_int_equal(ret, LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +0200358 CHECK_LOG_CTX("Sub-element \"text\" of \"status\" element must be defined as it's first sub-element.", "Line number 1.");
359 lydict_remove(UTEST_LYCTX, prefix_value);
360 RESET_STATE;
David Sedlák555c7202019-07-04 12:14:12 +0200361
362 /* test mandatory subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200363 data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END;
Radek Krejcid6b76452019-09-03 17:03:03 +0200364 struct yin_subelement subelems4[1] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}};
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100365
Radek Iša56ca9e42020-09-08 18:42:00 +0200366 ly_in_new_memory(data, &UTEST_IN);
367 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
368 lyxml_ctx_next(YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100369
Michal Vasko193dacd2022-10-13 08:43:05 +0200370 ret = yin_parse_content(YCTX, subelems4, 1, NULL, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200371 assert_int_equal(ret, LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +0200372 CHECK_LOG_CTX("Missing mandatory sub-element \"prefix\" of \"status\" element.", "Line number 1.");
David Sedlák555c7202019-07-04 12:14:12 +0200373}
374
David Sedlák92147b02019-07-09 14:01:01 +0200375static void
David Sedlák4a650532019-07-10 11:55:18 +0200376test_validate_value(void **state)
377{
Michal Vaskob36053d2020-03-26 15:49:30 +0100378 const char *data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END;
379
380 /* create some XML context */
Radek Iša56ca9e42020-09-08 18:42:00 +0200381 ly_in_new_memory(data, &UTEST_IN);
382 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
383 YCTX->xmlctx->status = LYXML_ELEM_CONTENT;
384 YCTX->xmlctx->dynamic = 0;
Michal Vaskob36053d2020-03-26 15:49:30 +0100385
Radek Iša56ca9e42020-09-08 18:42:00 +0200386 YCTX->xmlctx->value = "#invalid";
387 YCTX->xmlctx->value_len = 8;
388 assert_int_equal(yin_validate_value(YCTX, Y_IDENTIF_ARG), LY_EVALID);
Radek Iša2eac74c2021-03-15 15:53:08 +0100389 CHECK_LOG_CTX("Invalid identifier first character '#' (0x0023).", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100390
Radek Iša56ca9e42020-09-08 18:42:00 +0200391 YCTX->xmlctx->value = "";
392 YCTX->xmlctx->value_len = 0;
393 assert_int_equal(yin_validate_value(YCTX, Y_STR_ARG), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +0100394
Radek Iša56ca9e42020-09-08 18:42:00 +0200395 YCTX->xmlctx->value = "pre:b";
396 YCTX->xmlctx->value_len = 5;
397 assert_int_equal(yin_validate_value(YCTX, Y_IDENTIF_ARG), LY_EVALID);
398 assert_int_equal(yin_validate_value(YCTX, Y_PREF_IDENTIF_ARG), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +0100399
Radek Iša56ca9e42020-09-08 18:42:00 +0200400 YCTX->xmlctx->value = "pre:pre:b";
401 YCTX->xmlctx->value_len = 9;
402 assert_int_equal(yin_validate_value(YCTX, Y_PREF_IDENTIF_ARG), LY_EVALID);
David Sedlák4a650532019-07-10 11:55:18 +0200403}
404
Michal Vasko12ef5362022-09-16 15:13:58 +0200405static void
406test_valid_module(void **state)
407{
408 struct lys_module *mod;
409 char *printed;
410 const char *links_yin =
411 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
412 "<module name=\"links\"\n"
413 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
414 " xmlns:mod2=\"urn:module2\">\n"
415 " <yang-version value=\"1.1\"/>\n"
416 " <namespace uri=\"urn:module2\"/>\n"
417 " <prefix value=\"mod2\"/>\n"
418 " <identity name=\"just-another-identity\"/>\n"
419 " <grouping name=\"rgroup\">\n"
420 " <leaf name=\"rg1\">\n"
421 " <type name=\"string\"/>\n"
422 " </leaf>\n"
423 " <leaf name=\"rg2\">\n"
424 " <type name=\"string\"/>\n"
425 " </leaf>\n"
426 " </grouping>\n"
427 " <leaf name=\"one-leaf\">\n"
428 " <type name=\"string\"/>\n"
429 " </leaf>\n"
430 " <list name=\"list-for-augment\">\n"
431 " <key value=\"keyleaf\"/>\n"
432 " <leaf name=\"keyleaf\">\n"
433 " <type name=\"string\"/>\n"
434 " </leaf>\n"
435 " <leaf name=\"just-leaf\">\n"
436 " <type name=\"int32\"/>\n"
437 " </leaf>\n"
438 " </list>\n"
439 " <leaf name=\"rleaf\">\n"
440 " <type name=\"string\"/>\n"
441 " </leaf>\n"
442 " <leaf-list name=\"llist\">\n"
443 " <type name=\"string\"/>\n"
444 " <min-elements value=\"0\"/>\n"
445 " <max-elements value=\"100\"/>\n"
446 " <ordered-by value=\"user\"/>\n"
447 " </leaf-list>\n"
448 "</module>\n";
449 const char *statements_yin =
450 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
451 "<module name=\"statements\"\n"
452 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
453 " xmlns:mod=\"urn:module\"\n"
454 " xmlns:mod2=\"urn:module2\">\n"
455 " <yang-version value=\"1.1\"/>\n"
456 " <namespace uri=\"urn:module\"/>\n"
457 " <prefix value=\"mod\"/>\n"
458 " <import module=\"links\">\n"
459 " <prefix value=\"mod2\"/>\n"
460 " </import>\n"
461 " <extension name=\"ext\"/>\n"
462 " <identity name=\"random-identity\">\n"
463 " <base name=\"mod2:just-another-identity\"/>\n"
464 " <base name=\"another-identity\"/>\n"
465 " </identity>\n"
466 " <identity name=\"another-identity\">\n"
467 " <base name=\"mod2:just-another-identity\"/>\n"
468 " </identity>\n"
469 " <typedef name=\"percent\">\n"
470 " <type name=\"uint8\">\n"
471 " <range value=\"0 .. 100\"/>\n"
472 " </type>\n"
473 " <units name=\"percent\"/>\n"
474 " </typedef>\n"
475 " <list name=\"list1\">\n"
476 " <key value=\"a\"/>\n"
477 " <leaf name=\"a\">\n"
478 " <type name=\"string\"/>\n"
479 " </leaf>\n"
480 " <leaf name=\"x\">\n"
481 " <type name=\"string\"/>\n"
482 " </leaf>\n"
483 " <leaf name=\"y\">\n"
484 " <type name=\"string\"/>\n"
485 " </leaf>\n"
486 " </list>\n"
487 " <container name=\"ice-cream-shop\">\n"
488 " <container name=\"employees\">\n"
489 " <when condition=\"/list1/x\"/>\n"
490 " <list name=\"employee\">\n"
491 " <key value=\"id\"/>\n"
492 " <unique tag=\"name\"/>\n"
493 " <config value=\"true\"/>\n"
494 " <min-elements value=\"0\">\n"
495 " <mod:ext/>\n"
496 " </min-elements>\n"
497 " <max-elements value=\"unbounded\"/>\n"
498 " <leaf name=\"id\">\n"
499 " <type name=\"uint64\"/>\n"
500 " <mandatory value=\"true\"/>\n"
501 " </leaf>\n"
502 " <leaf name=\"name\">\n"
503 " <type name=\"string\"/>\n"
504 " </leaf>\n"
505 " <leaf name=\"age\">\n"
506 " <type name=\"uint32\"/>\n"
507 " </leaf>\n"
508 " </list>\n"
509 " </container>\n"
510 " </container>\n"
511 " <container name=\"random\">\n"
512 " <grouping name=\"group\">\n"
513 " <leaf name=\"g1\">\n"
514 " <type name=\"percent\"/>\n"
515 " <mandatory value=\"false\"/>\n"
516 " </leaf>\n"
517 " <leaf name=\"g2\">\n"
518 " <type name=\"string\"/>\n"
519 " </leaf>\n"
520 " </grouping>\n"
521 " <choice name=\"switch\">\n"
522 " <case name=\"a\">\n"
523 " <leaf name=\"aleaf\">\n"
524 " <type name=\"string\"/>\n"
525 " <default value=\"aaa\"/>\n"
526 " </leaf>\n"
527 " </case>\n"
528 " <case name=\"c\">\n"
529 " <leaf name=\"cleaf\">\n"
530 " <type name=\"string\"/>\n"
531 " </leaf>\n"
532 " </case>\n"
533 " </choice>\n"
534 " <anyxml name=\"xml-data\"/>\n"
535 " <anydata name=\"any-data\"/>\n"
536 " <leaf-list name=\"leaflist\">\n"
537 " <type name=\"string\"/>\n"
538 " <min-elements value=\"0\"/>\n"
539 " <max-elements value=\"20\"/>\n"
540 " </leaf-list>\n"
541 " <uses name=\"group\"/>\n"
542 " <uses name=\"mod2:rgroup\"/>\n"
543 " <leaf name=\"lref\">\n"
544 " <type name=\"leafref\">\n"
545 " <path value=\"/mod2:one-leaf\"/>\n"
546 " </type>\n"
547 " </leaf>\n"
548 " <leaf name=\"iref\">\n"
549 " <type name=\"identityref\">\n"
550 " <base name=\"mod2:just-another-identity\"/>\n"
551 " </type>\n"
552 " </leaf>\n"
553 " </container>\n"
554 " <augment target-node=\"/random\">\n"
555 " <leaf name=\"aug-leaf\">\n"
556 " <type name=\"string\"/>\n"
557 " </leaf>\n"
558 " </augment>\n"
559 " <notification name=\"notif\"/>\n"
560 " <deviation target-node=\"/mod:ice-cream-shop/mod:employees/mod:employee/mod:age\">\n"
561 " <deviate value=\"not-supported\">\n"
562 " <mod:ext/>\n"
563 " </deviate>\n"
564 " </deviation>\n"
565 " <deviation target-node=\"/mod:list1\">\n"
566 " <deviate value=\"add\">\n"
567 " <mod:ext/>\n"
568 " <must condition=\"1\"/>\n"
569 " <must condition=\"2\"/>\n"
570 " <unique tag=\"x\"/>\n"
571 " <unique tag=\"y\"/>\n"
572 " <config value=\"true\"/>\n"
573 " <min-elements value=\"1\"/>\n"
574 " <max-elements value=\"2\"/>\n"
575 " </deviate>\n"
576 " </deviation>\n"
577 " <deviation target-node=\"/mod:ice-cream-shop/mod:employees/mod:employee\">\n"
578 " <deviate value=\"delete\">\n"
579 " <unique tag=\"name\"/>\n"
580 " </deviate>\n"
581 " </deviation>\n"
582 " <deviation target-node=\"/mod:random/mod:leaflist\">\n"
583 " <deviate value=\"replace\">\n"
584 " <type name=\"uint32\"/>\n"
585 " <min-elements value=\"10\"/>\n"
586 " <max-elements value=\"15\"/>\n"
587 " </deviate>\n"
588 " </deviation>\n"
589 "</module>\n";
590
591 UTEST_ADD_MODULE(links_yin, LYS_IN_YIN, NULL, NULL);
592 UTEST_ADD_MODULE(statements_yin, LYS_IN_YIN, NULL, &mod);
593 lys_print_mem(&printed, mod, LYS_OUT_YIN, 0);
594 assert_string_equal(printed, statements_yin);
595 free(printed);
596}
597
598static void
599test_print_module(void **state)
600{
601 struct lys_module *mod;
602
603 char *orig = malloc(8096);
604
605 strcpy(orig,
606 "module all {\n"
607 " yang-version 1.1;\n"
608 " namespace \"urn:all\";\n"
609 " prefix all_mod;\n\n"
610 " import ietf-yang-types {\n"
611 " prefix yt;\n"
612 " revision-date 2013-07-15;\n"
613 " description\n"
614 " \"YANG types\";\n"
615 " reference\n"
616 " \"RFC reference\";\n"
617 " }\n\n"
618 " feature feat1 {\n"
619 " if-feature \"feat2\";\n"
620 " status obsolete;\n"
621 " }\n\n"
622 " feature feat2;\n"
623 " feature feat3;\n\n"
624 " identity ident2 {\n"
625 " base ident1;\n"
626 " }\n\n"
627 " identity ident1;\n\n"
628 " typedef tdef1 {\n"
629 " type tdef2 {\n"
630 " length \"3..9 | 30..40\";\n"
631 " pattern \"[ac]*\";\n"
632 " }\n"
633 " units \"none\";\n"
634 " default \"aaa\";\n"
635 " }\n\n"
636 " typedef tdef2 {\n"
637 " type string {\n"
638 " length \"2..10 | 20..50\";\n"
639 " pattern \"[ab]*\";\n"
640 " }\n"
641 " }\n\n"
642 " grouping group1 {\n"
643 " leaf leaf1 {\n"
644 " type int8;\n"
645 " }\n"
646 " }\n\n"
647 " container cont1 {\n"
648 " leaf leaf2 {\n"
649 " if-feature \"feat1\";\n"
650 " type int16;\n"
651 " status obsolete;\n"
652 " }\n\n"
653 " uses group1 {\n"
654 " if-feature \"feat2\";\n"
655 " refine \"leaf1\" {\n"
656 " if-feature \"feat3\";\n"
657 " must \"24 - 4 = number('20')\";\n"
658 " default \"25\";\n"
659 " config true;\n"
660 " mandatory false;\n"
661 " description\n"
662 " \"dsc\";\n"
663 " reference\n"
664 " \"none\";\n"
665 " }\n"
666 " }\n\n"
667 " leaf leaf3 {\n"
668 " type int32;\n"
669 " }\n\n"
670 " leaf leaf4 {\n"
671 " type int64 {\n"
672 " range \"1000 .. 50000\" {\n"
673 " error-message\n"
674 " \"Special error message.\";\n"
675 " error-app-tag \"special-tag\";\n"
676 " }\n"
677 " }\n"
678 " }\n\n"
679 " leaf leaf5 {\n"
680 " type uint8;\n"
681 " }\n\n"
682 " leaf leaf6 {\n"
683 " type uint16;\n"
684 " }\n\n"
685 " leaf leaf7 {\n"
686 " type uint32;\n"
687 " }\n\n"
688 " leaf leaf8 {\n"
689 " type uint64;\n"
690 " }\n\n"
691 " choice choic1 {\n"
692 " default \"leaf9b\";\n"
693 " leaf leaf9a {\n"
694 " type decimal64 {\n"
695 " fraction-digits 9;\n"
696 " }\n"
697 " }\n\n"
698 " leaf leaf9b {\n"
699 " type boolean;\n"
700 " default \"false\";\n"
701 " }\n"
702 " }\n\n"
703 " leaf leaf10 {\n"
704 " type boolean;\n"
705 " }\n\n");
706 strcpy(orig + strlen(orig),
707 " leaf leaf11 {\n"
708 " type enumeration {\n"
709 " enum \"one\";\n"
710 " enum \"two\";\n"
711 " enum \"five\" {\n"
712 " value 5;\n"
713 " }\n"
714 " }\n"
715 " }\n\n"
716 " leaf leaf12 {\n"
717 " type bits {\n"
718 " bit flag0 {\n"
719 " position 0;\n"
720 " }\n"
721 " bit flag1;\n"
722 " bit flag2 {\n"
723 " position 2;\n"
724 " }\n"
725 " bit flag3 {\n"
726 " position 3;\n"
727 " }\n"
728 " }\n"
729 " default \"flag0 flag3\";\n"
730 " }\n\n"
731 " leaf leaf13 {\n"
732 " type binary;\n"
733 " }\n\n"
734 " leaf leaf14 {\n"
735 " type leafref {\n"
736 " path \"/cont1/leaf17\";\n"
737 " }\n"
738 " }\n\n"
739 " leaf leaf15 {\n"
740 " type empty;\n"
741 " }\n\n"
742 " leaf leaf16 {\n"
743 " type union {\n"
744 " type instance-identifier {\n"
745 " require-instance true;\n"
746 " }\n"
747 " type int8;\n"
748 " }\n"
749 " }\n\n"
750 " list list1 {\n"
751 " key \"leaf18\";\n"
752 " unique \"leaf19\";\n"
753 " min-elements 1;\n"
754 " max-elements 20;\n"
755 " leaf leaf18 {\n"
756 " type string;\n"
757 " }\n\n"
758 " leaf leaf19 {\n"
759 " type uint32;\n"
760 " }\n\n"
761 " anyxml axml1;\n"
762 " anydata adata1;\n\n"
763 " action act1 {\n"
764 " input {\n"
765 " leaf leaf24 {\n"
766 " type string;\n"
767 " }\n"
768 " }\n\n"
769 " output {\n"
770 " leaf leaf25 {\n"
771 " type string;\n"
772 " }\n"
773 " }\n"
774 " }\n\n"
775 " notification notif1 {\n"
776 " leaf leaf26 {\n"
777 " type string;\n"
778 " }\n"
779 " }\n"
780 " }\n\n"
781 " leaf-list llist1 {\n"
782 " type tdef1;\n"
783 " ordered-by user;\n"
784 " }\n\n"
785 " list list2 {\n"
786 " key \"leaf27 leaf28\";\n"
787 " leaf leaf27 {\n"
788 " type uint8;\n"
789 " }\n\n"
790 " leaf leaf28 {\n"
791 " type uint8;\n"
792 " }\n"
793 " }\n\n"
794 " leaf leaf29 {\n"
795 " type instance-identifier;\n"
796 " }\n\n"
797 " container must-deviations-container {\n"
798 " presence \"Allows deviations on the leaf\";\n"
799 " leaf leaf30 {\n"
800 " type string;\n"
801 " }\n"
802 " }\n\n"
803 " leaf leaf23 {\n"
804 " type empty;\n"
805 " }\n"
806 " }\n\n"
807 " augment \"/cont1\" {\n"
808 " leaf leaf17 {\n"
809 " type string;\n"
810 " }\n"
811 " }\n\n"
812 " rpc rpc1 {\n"
813 " input {\n"
814 " leaf leaf20 {\n"
815 " type tdef1;\n"
816 " }\n"
817 " }\n\n"
818 " output {\n"
819 " container cont2 {\n"
820 " leaf leaf21 {\n"
821 " type empty;\n"
822 " }\n"
823 " }\n"
824 " }\n"
825 " }\n\n"
826 " container test-when {\n"
827 " leaf when-check {\n"
828 " type boolean;\n"
829 " }\n\n"
830 " leaf gated-data {\n"
831 " when \"../when-check = 'true'\";\n"
832 " type uint16;\n"
833 " }\n"
834 " }\n\n"
835 " extension c-define {\n"
836 " description\n"
837 " \"Takes as an argument a name string.\n"
838 " Makes the code generator use the given name\n"
839 " in the #define.\";\n"
840 " argument \"name\";\n"
841 " }\n"
842 "}\n");
843
844 char *ori_res = malloc(8096);
845
846 strcpy(ori_res,
847 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
848 "<module name=\"all\"\n"
849 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
850 " xmlns:all_mod=\"urn:all\"\n"
851 " xmlns:yt=\"urn:ietf:params:xml:ns:yang:ietf-yang-types\">\n"
852 " <yang-version value=\"1.1\"/>\n"
853 " <namespace uri=\"urn:all\"/>\n"
854 " <prefix value=\"all_mod\"/>\n"
855 " <import module=\"ietf-yang-types\">\n"
856 " <prefix value=\"yt\"/>\n"
857 " <revision-date date=\"2013-07-15\"/>\n"
858 " <description>\n"
859 " <text>YANG types</text>\n"
860 " </description>\n"
861 " <reference>\n"
862 " <text>RFC reference</text>\n"
863 " </reference>\n"
864 " </import>\n"
865 " <extension name=\"c-define\">\n"
866 " <argument name=\"name\"/>\n"
867 " <description>\n"
868 " <text>Takes as an argument a name string.\n"
869 "Makes the code generator use the given name\n"
870 "in the #define.</text>\n"
871 " </description>\n"
872 " </extension>\n"
873 " <feature name=\"feat1\">\n"
874 " <if-feature name=\"feat2\"/>\n"
875 " <status value=\"obsolete\"/>\n"
876 " </feature>\n"
877 " <feature name=\"feat2\"/>\n"
878 " <feature name=\"feat3\"/>\n"
879 " <identity name=\"ident2\">\n"
880 " <base name=\"ident1\"/>\n"
881 " </identity>\n"
882 " <identity name=\"ident1\"/>\n"
883 " <typedef name=\"tdef1\">\n"
884 " <type name=\"tdef2\">\n"
885 " <length value=\"3..9 | 30..40\"/>\n"
886 " <pattern value=\"[ac]*\"/>\n"
887 " </type>\n"
888 " <units name=\"none\"/>\n"
889 " <default value=\"aaa\"/>\n"
890 " </typedef>\n"
891 " <typedef name=\"tdef2\">\n"
892 " <type name=\"string\">\n"
893 " <length value=\"2..10 | 20..50\"/>\n"
894 " <pattern value=\"[ab]*\"/>\n"
895 " </type>\n"
896 " </typedef>\n"
897 " <grouping name=\"group1\">\n"
898 " <leaf name=\"leaf1\">\n"
899 " <type name=\"int8\"/>\n"
900 " </leaf>\n"
901 " </grouping>\n"
902 " <container name=\"cont1\">\n"
903 " <leaf name=\"leaf2\">\n"
904 " <if-feature name=\"feat1\"/>\n"
905 " <type name=\"int16\"/>\n"
906 " <status value=\"obsolete\"/>\n"
907 " </leaf>\n"
908 " <uses name=\"group1\">\n"
909 " <if-feature name=\"feat2\"/>\n"
910 " <refine target-node=\"leaf1\">\n"
911 " <if-feature name=\"feat3\"/>\n"
912 " <must condition=\"24 - 4 = number('20')\"/>\n"
913 " <default value=\"25\"/>\n"
914 " <config value=\"true\"/>\n"
915 " <mandatory value=\"false\"/>\n"
916 " <description>\n"
917 " <text>dsc</text>\n"
918 " </description>\n"
919 " <reference>\n"
920 " <text>none</text>\n"
921 " </reference>\n"
922 " </refine>\n"
923 " </uses>\n"
924 " <leaf name=\"leaf3\">\n"
925 " <type name=\"int32\"/>\n"
926 " </leaf>\n"
927 " <leaf name=\"leaf4\">\n"
928 " <type name=\"int64\">\n"
929 " <range value=\"1000 .. 50000\">\n"
930 " <error-message>\n"
931 " <value>Special error message.</value>\n"
932 " </error-message>\n"
933 " <error-app-tag value=\"special-tag\"/>\n"
934 " </range>\n"
935 " </type>\n"
936 " </leaf>\n"
937 " <leaf name=\"leaf5\">\n"
938 " <type name=\"uint8\"/>\n"
939 " </leaf>\n"
940 " <leaf name=\"leaf6\">\n"
941 " <type name=\"uint16\"/>\n"
942 " </leaf>\n"
943 " <leaf name=\"leaf7\">\n"
944 " <type name=\"uint32\"/>\n"
945 " </leaf>\n"
946 " <leaf name=\"leaf8\">\n"
947 " <type name=\"uint64\"/>\n"
948 " </leaf>\n"
949 " <choice name=\"choic1\">\n"
950 " <default value=\"leaf9b\"/>\n"
951 " <leaf name=\"leaf9a\">\n"
952 " <type name=\"decimal64\">\n"
953 " <fraction-digits value=\"9\"/>\n"
954 " </type>\n"
955 " </leaf>\n"
956 " <leaf name=\"leaf9b\">\n"
957 " <type name=\"boolean\"/>\n"
958 " <default value=\"false\"/>\n"
959 " </leaf>\n"
960 " </choice>\n"
961 " <leaf name=\"leaf10\">\n"
962 " <type name=\"boolean\"/>\n"
963 " </leaf>\n");
964 strcpy(ori_res + strlen(ori_res),
965 " <leaf name=\"leaf11\">\n"
966 " <type name=\"enumeration\">\n"
967 " <enum name=\"one\"/>\n"
968 " <enum name=\"two\"/>\n"
969 " <enum name=\"five\">\n"
970 " <value value=\"5\"/>\n"
971 " </enum>\n"
972 " </type>\n"
973 " </leaf>\n"
974 " <leaf name=\"leaf12\">\n"
975 " <type name=\"bits\">\n"
976 " <bit name=\"flag0\">\n"
977 " <position value=\"0\"/>\n"
978 " </bit>\n"
979 " <bit name=\"flag1\"/>\n"
980 " <bit name=\"flag2\">\n"
981 " <position value=\"2\"/>\n"
982 " </bit>\n"
983 " <bit name=\"flag3\">\n"
984 " <position value=\"3\"/>\n"
985 " </bit>\n"
986 " </type>\n"
987 " <default value=\"flag0 flag3\"/>\n"
988 " </leaf>\n"
989 " <leaf name=\"leaf13\">\n"
990 " <type name=\"binary\"/>\n"
991 " </leaf>\n"
992 " <leaf name=\"leaf14\">\n"
993 " <type name=\"leafref\">\n"
994 " <path value=\"/cont1/leaf17\"/>\n"
995 " </type>\n"
996 " </leaf>\n"
997 " <leaf name=\"leaf15\">\n"
998 " <type name=\"empty\"/>\n"
999 " </leaf>\n"
1000 " <leaf name=\"leaf16\">\n"
1001 " <type name=\"union\">\n"
1002 " <type name=\"instance-identifier\">\n"
1003 " <require-instance value=\"true\"/>\n"
1004 " </type>\n"
1005 " <type name=\"int8\"/>\n"
1006 " </type>\n"
1007 " </leaf>\n"
1008 " <list name=\"list1\">\n"
1009 " <key value=\"leaf18\"/>\n"
1010 " <unique tag=\"leaf19\"/>\n"
1011 " <min-elements value=\"1\"/>\n"
1012 " <max-elements value=\"20\"/>\n"
1013 " <leaf name=\"leaf18\">\n"
1014 " <type name=\"string\"/>\n"
1015 " </leaf>\n"
1016 " <leaf name=\"leaf19\">\n"
1017 " <type name=\"uint32\"/>\n"
1018 " </leaf>\n"
1019 " <anyxml name=\"axml1\"/>\n"
1020 " <anydata name=\"adata1\"/>\n"
1021 " <action name=\"act1\">\n"
1022 " <input>\n"
1023 " <leaf name=\"leaf24\">\n"
1024 " <type name=\"string\"/>\n"
1025 " </leaf>\n"
1026 " </input>\n"
1027 " <output>\n"
1028 " <leaf name=\"leaf25\">\n"
1029 " <type name=\"string\"/>\n"
1030 " </leaf>\n"
1031 " </output>\n"
1032 " </action>\n"
1033 " <notification name=\"notif1\">\n"
1034 " <leaf name=\"leaf26\">\n"
1035 " <type name=\"string\"/>\n"
1036 " </leaf>\n"
1037 " </notification>\n"
1038 " </list>\n"
1039 " <leaf-list name=\"llist1\">\n"
1040 " <type name=\"tdef1\"/>\n"
1041 " <ordered-by value=\"user\"/>\n"
1042 " </leaf-list>\n"
1043 " <list name=\"list2\">\n"
1044 " <key value=\"leaf27 leaf28\"/>\n"
1045 " <leaf name=\"leaf27\">\n"
1046 " <type name=\"uint8\"/>\n"
1047 " </leaf>\n"
1048 " <leaf name=\"leaf28\">\n"
1049 " <type name=\"uint8\"/>\n"
1050 " </leaf>\n"
1051 " </list>\n"
1052 " <leaf name=\"leaf29\">\n"
1053 " <type name=\"instance-identifier\"/>\n"
1054 " </leaf>\n"
1055 " <container name=\"must-deviations-container\">\n"
1056 " <presence value=\"Allows deviations on the leaf\"/>\n"
1057 " <leaf name=\"leaf30\">\n"
1058 " <type name=\"string\"/>\n"
1059 " </leaf>\n"
1060 " </container>\n"
1061 " <leaf name=\"leaf23\">\n"
1062 " <type name=\"empty\"/>\n"
1063 " </leaf>\n"
1064 " </container>\n"
1065 " <container name=\"test-when\">\n"
1066 " <leaf name=\"when-check\">\n"
1067 " <type name=\"boolean\"/>\n"
1068 " </leaf>\n"
1069 " <leaf name=\"gated-data\">\n"
1070 " <when condition=\"../when-check = 'true'\"/>\n"
1071 " <type name=\"uint16\"/>\n"
1072 " </leaf>\n"
1073 " </container>\n"
1074 " <augment target-node=\"/cont1\">\n"
1075 " <leaf name=\"leaf17\">\n"
1076 " <type name=\"string\"/>\n"
1077 " </leaf>\n"
1078 " </augment>\n"
1079 " <rpc name=\"rpc1\">\n"
1080 " <input>\n"
1081 " <leaf name=\"leaf20\">\n"
1082 " <type name=\"tdef1\"/>\n"
1083 " </leaf>\n"
1084 " </input>\n"
1085 " <output>\n"
1086 " <container name=\"cont2\">\n"
1087 " <leaf name=\"leaf21\">\n"
1088 " <type name=\"empty\"/>\n"
1089 " </leaf>\n"
1090 " </container>\n"
1091 " </output>\n"
1092 " </rpc>\n"
1093 "</module>\n");
1094
1095 char *printed;
1096 struct ly_out *out;
1097
1098 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out));
1099
1100 UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod);
1101 assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YIN, 0, 0));
1102 assert_int_equal(strlen(ori_res), ly_out_printed(out));
1103 assert_string_equal(printed, ori_res);
1104
1105 ly_out_free(out, NULL, 1);
1106 free(orig);
1107 free(ori_res);
1108}
1109
1110static LY_ERR
1111test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
1112 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
1113 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
1114{
1115 *module_data = user_data;
1116 *format = LYS_IN_YIN;
1117 *free_module_data = NULL;
1118 return LY_SUCCESS;
1119}
1120
1121static void
1122test_print_submodule(void **state)
1123{
1124 struct lys_module *mod;
1125
1126 const char *mod_yin =
1127 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1128 "<module name=\"a\"\n"
1129 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1130 " xmlns:a_mod=\"urn:a\">\n"
1131 " <yang-version value=\"1.1\"/>\n"
1132 " <namespace uri=\"urn:a\"/>\n"
1133 " <prefix value=\"a_mod\"/>\n"
1134 " <include module=\"a-sub\"/>\n"
1135 "</module>\n";
1136
1137 char *submod_yin =
1138 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1139 "<submodule name=\"a-sub\"\n"
1140 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\n"
1141 " xmlns:a_mod=\"urn:a\"\n"
1142 " xmlns:yt=\"urn:ietf:params:xml:ns:yang:ietf-yang-types\">\n"
1143 " <yang-version value=\"1.1\"/>\n"
1144 " <belongs-to module=\"a\">\n"
1145 " <prefix value=\"a_mod\"/>\n"
1146 " </belongs-to>\n"
1147 " <import module=\"ietf-yang-types\">\n"
1148 " <prefix value=\"yt\"/>\n"
1149 " <revision-date date=\"2013-07-15\"/>\n"
1150 " </import>\n\n"
1151 " <description>\n"
1152 " <text>YANG types</text>\n"
1153 " </description>\n"
1154 " <reference>\n"
1155 " <text>RFC reference</text>\n"
1156 " </reference>\n"
1157 "</submodule>\n";
1158
1159 char *printed;
1160 struct ly_out *out;
1161
1162 assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out));
1163
1164 ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod_yin);
1165
1166 UTEST_ADD_MODULE(mod_yin, LYS_IN_YIN, NULL, &mod);
1167 assert_int_equal(LY_SUCCESS, lys_print_submodule(out, mod->parsed->includes[0].submodule, LYS_OUT_YIN, 0, 0));
1168 assert_int_equal(strlen(submod_yin), ly_out_printed(out));
1169 assert_string_equal(printed, submod_yin);
1170
1171 ly_out_free(out, NULL, 1);
1172}
1173
David Sedlák32488102019-07-15 17:44:10 +02001174/* helper function to simplify unit test of each element using parse_content function */
1175LY_ERR
Radek Iša56ca9e42020-09-08 18:42:00 +02001176test_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 +02001177{
David Sedlákc5b20842019-08-13 10:18:31 +02001178 const char *name, *prefix;
1179 size_t name_len, prefix_len;
David Sedlák32488102019-07-15 17:44:10 +02001180 LY_ERR ret = LY_SUCCESS;
1181 struct yin_subelement subelems[71] = {
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001182 {LY_STMT_ACTION, dest, 0},
1183 {LY_STMT_ANYDATA, dest, 0},
1184 {LY_STMT_ANYXML, dest, 0},
1185 {LY_STMT_ARGUMENT, dest, 0},
1186 {LY_STMT_AUGMENT, dest, 0},
1187 {LY_STMT_BASE, dest, 0},
1188 {LY_STMT_BELONGS_TO, dest, 0},
1189 {LY_STMT_BIT, dest, 0},
1190 {LY_STMT_CASE, dest, 0},
1191 {LY_STMT_CHOICE, dest, 0},
1192 {LY_STMT_CONFIG, dest, 0},
1193 {LY_STMT_CONTACT, dest, 0},
1194 {LY_STMT_CONTAINER, dest, 0},
1195 {LY_STMT_DEFAULT, dest, YIN_SUBELEM_UNIQUE},
1196 {LY_STMT_DESCRIPTION, dest, 0},
1197 {LY_STMT_DEVIATE, dest, 0},
1198 {LY_STMT_DEVIATION, dest, 0},
1199 {LY_STMT_ENUM, dest, 0},
1200 {LY_STMT_ERROR_APP_TAG, dest, YIN_SUBELEM_UNIQUE},
1201 {LY_STMT_ERROR_MESSAGE, dest, 0},
1202 {LY_STMT_EXTENSION, dest, 0},
1203 {LY_STMT_FEATURE, dest, 0},
1204 {LY_STMT_FRACTION_DIGITS, dest, 0},
1205 {LY_STMT_GROUPING, dest, 0},
1206 {LY_STMT_IDENTITY, dest, 0},
1207 {LY_STMT_IF_FEATURE, dest, 0},
1208 {LY_STMT_IMPORT, dest, 0},
1209 {LY_STMT_INCLUDE, dest, 0},
1210 {LY_STMT_INPUT, dest, 0},
1211 {LY_STMT_KEY, dest, YIN_SUBELEM_UNIQUE},
1212 {LY_STMT_LEAF, dest, 0},
1213 {LY_STMT_LEAF_LIST, dest, 0},
1214 {LY_STMT_LENGTH, dest, 0},
1215 {LY_STMT_LIST, dest, 0},
1216 {LY_STMT_MANDATORY, dest, 0},
1217 {LY_STMT_MAX_ELEMENTS, dest, 0},
1218 {LY_STMT_MIN_ELEMENTS, dest, 0},
1219 {LY_STMT_MODIFIER, dest, 0},
1220 {LY_STMT_MODULE, dest, 0},
1221 {LY_STMT_MUST, dest, 0},
1222 {LY_STMT_NAMESPACE, dest, YIN_SUBELEM_UNIQUE},
1223 {LY_STMT_NOTIFICATION, dest, 0},
1224 {LY_STMT_ORDERED_BY, dest, 0},
1225 {LY_STMT_ORGANIZATION, dest, 0},
1226 {LY_STMT_OUTPUT, dest, 0},
1227 {LY_STMT_PATH, dest, 0},
1228 {LY_STMT_PATTERN, dest, 0},
1229 {LY_STMT_POSITION, dest, 0},
1230 {LY_STMT_PREFIX, dest, YIN_SUBELEM_UNIQUE},
1231 {LY_STMT_PRESENCE, dest, YIN_SUBELEM_UNIQUE},
1232 {LY_STMT_RANGE, dest, 0},
1233 {LY_STMT_REFERENCE, dest, 0},
1234 {LY_STMT_REFINE, dest, 0},
1235 {LY_STMT_REQUIRE_INSTANCE, dest, 0},
1236 {LY_STMT_REVISION, dest, 0},
1237 {LY_STMT_REVISION_DATE, dest, 0},
1238 {LY_STMT_RPC, dest, 0},
1239 {LY_STMT_STATUS, dest, 0},
1240 {LY_STMT_SUBMODULE, dest, 0},
1241 {LY_STMT_TYPE, dest, 0},
1242 {LY_STMT_TYPEDEF, dest, 0},
1243 {LY_STMT_UNIQUE, dest, 0},
1244 {LY_STMT_UNITS, dest, YIN_SUBELEM_UNIQUE},
1245 {LY_STMT_USES, dest, 0},
1246 {LY_STMT_VALUE, dest, 0},
1247 {LY_STMT_WHEN, dest, 0},
1248 {LY_STMT_YANG_VERSION, dest, 0},
1249 {LY_STMT_YIN_ELEMENT, dest, 0},
1250 {LY_STMT_EXTENSION_INSTANCE, dest, 0},
1251 {LY_STMT_ARG_TEXT, dest, 0},
1252 {LY_STMT_ARG_VALUE, dest, 0}
1253 };
1254
Michal Vaskod0625d72022-10-06 15:02:50 +02001255 YCTX->main_ctx = (struct lysp_ctx *)YCTX;
Radek Iša56ca9e42020-09-08 18:42:00 +02001256 ly_in_new_memory(data, &UTEST_IN);
1257 lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx);
1258 prefix = YCTX->xmlctx->prefix;
1259 prefix_len = YCTX->xmlctx->prefix_len;
1260 name = YCTX->xmlctx->name;
1261 name_len = YCTX->xmlctx->name_len;
1262 lyxml_ctx_next(YCTX->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +01001263
Michal Vasko193dacd2022-10-13 08:43:05 +02001264 ret = yin_parse_content(YCTX, subelems, 71, NULL,
1265 yin_match_keyword(YCTX, name, name_len, prefix, prefix_len, LY_STMT_NONE), text, exts);
Michal Vaskob36053d2020-03-26 15:49:30 +01001266
Michal Vasko63f3d842020-07-08 10:10:14 +02001267 /* free parser and input */
Radek Iša56ca9e42020-09-08 18:42:00 +02001268 lyxml_ctx_free(YCTX->xmlctx);
1269 YCTX->xmlctx = NULL;
1270 ly_in_free(UTEST_IN, 0);
1271 UTEST_IN = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001272 return ret;
1273}
1274
David Sedlákd1144562019-08-06 12:36:14 +02001275#define EXT_SUBELEM "<myext:c-define name=\"MY_MTU\" xmlns:myext=\"urn:example:extensions\"/>"
1276
David Sedlák32488102019-07-15 17:44:10 +02001277static void
David Sedlák43801c92019-08-05 15:58:54 +02001278test_enum_elem(void **state)
David Sedlák32488102019-07-15 17:44:10 +02001279{
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001280 struct lysp_type type = {0};
David Sedlák32488102019-07-15 17:44:10 +02001281 const char *data;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001282
David Sedlák32488102019-07-15 17:44:10 +02001283 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001284 "<enum name=\"enum-name\">\n"
1285 " <if-feature name=\"feature\" />\n"
1286 " <value value=\"55\" />\n"
1287 " <status value=\"deprecated\" />\n"
1288 " <description><text>desc...</text></description>\n"
1289 " <reference><text>ref...</text></reference>\n"
1290 " " EXT_SUBELEM "\n"
1291 "</enum>"
1292 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001293 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1294 uint16_t flags = LYS_STATUS_DEPRC | LYS_SET_VALUE;
1295
1296 CHECK_LYSP_TYPE_ENUM(type.enums, "desc...", 1, flags, 1, "enum-name", "ref...", 55);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001297 assert_string_equal(type.enums->iffeatures[0].str, "feature");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001298 TEST_1_CHECK_LYSP_EXT_INSTANCE(type.enums->exts, LY_STMT_ENUM);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001299 lysp_type_free(&fctx, &type);
David Sedlákd1144562019-08-06 12:36:14 +02001300 memset(&type, 0, sizeof type);
1301
1302 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001303 "<enum name=\"enum-name\"></enum>"
1304 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001305 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001306 assert_string_equal(type.enums->name, "enum-name");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001307 lysp_type_free(&fctx, &type);
David Sedlák32488102019-07-15 17:44:10 +02001308 memset(&type, 0, sizeof type);
David Sedlák43801c92019-08-05 15:58:54 +02001309}
1310
1311static void
1312test_bit_elem(void **state)
1313{
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001314 struct lysp_type type = {0};
David Sedlák43801c92019-08-05 15:58:54 +02001315 const char *data;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001316
David Sedlák43801c92019-08-05 15:58:54 +02001317 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001318 "<bit name=\"bit-name\">\n"
1319 " <if-feature name=\"feature\" />\n"
1320 " <position value=\"55\" />\n"
1321 " <status value=\"deprecated\" />\n"
1322 " <description><text>desc...</text></description>\n"
1323 " <reference><text>ref...</text></reference>\n"
1324 EXT_SUBELEM
1325 "</bit>"
1326 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001327 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1328 uint16_t flags = LYS_STATUS_DEPRC | LYS_SET_VALUE;
1329
1330 CHECK_LYSP_TYPE_ENUM(type.bits, "desc...", 1, flags, 1, "bit-name", "ref...", 55);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001331 assert_string_equal(type.bits->iffeatures[0].str, "feature");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001332 TEST_1_CHECK_LYSP_EXT_INSTANCE(type.bits->exts, LY_STMT_BIT);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001333 lysp_type_free(&fctx, &type);
David Sedlákd1144562019-08-06 12:36:14 +02001334 memset(&type, 0, sizeof type);
1335
1336 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001337 "<bit name=\"bit-name\"> </bit>"
1338 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001339 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1340 CHECK_LYSP_TYPE_ENUM(type.bits, NULL, 0, 0, 0, "bit-name", NULL, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001341 lysp_type_free(&fctx, &type);
David Sedlák43801c92019-08-05 15:58:54 +02001342 memset(&type, 0, sizeof type);
David Sedlák32488102019-07-15 17:44:10 +02001343}
1344
1345static void
David Sedlák32488102019-07-15 17:44:10 +02001346test_status_elem(void **state)
1347{
David Sedlák32488102019-07-15 17:44:10 +02001348 const char *data;
1349 uint16_t flags = 0;
David Sedlák32488102019-07-15 17:44:10 +02001350
1351 /* test invalid value */
1352 data = ELEMENT_WRAPPER_START "<status value=\"invalid\"></status>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001353 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID);
1354 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"status\" element. "
1355 "Valid values are \"current\", \"deprecated\" and \"obsolete\".", "Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001356}
1357
1358static void
David Sedlák32488102019-07-15 17:44:10 +02001359test_yin_element_elem(void **state)
1360{
David Sedlák32488102019-07-15 17:44:10 +02001361 const char *data;
1362 uint16_t flags = 0;
David Sedlák32488102019-07-15 17:44:10 +02001363
1364 data = ELEMENT_WRAPPER_START "<yin-element value=\"invalid\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001365 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +02001366 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"yin-element\" element. "
1367 "Valid values are \"true\" and \"false\".", "Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001368}
1369
1370static void
1371test_yangversion_elem(void **state)
1372{
David Sedlák32488102019-07-15 17:44:10 +02001373 const char *data;
1374 uint8_t version = 0;
David Sedlák32488102019-07-15 17:44:10 +02001375
1376 /* invalid value */
1377 data = ELEMENT_WRAPPER_START "<yang-version value=\"version\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001378 assert_int_equal(test_element_helper(state, data, &version, NULL, NULL), LY_EVALID);
1379 CHECK_LOG_CTX("Invalid value \"version\" of \"value\" attribute in \"yang-version\" element. "
1380 "Valid values are \"1\" and \"1.1\".", "Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001381}
1382
1383static void
David Sedlák8e7bda82019-07-16 17:57:50 +02001384test_argument_elem(void **state)
1385{
David Sedlák8e7bda82019-07-16 17:57:50 +02001386 const char *data;
1387 uint16_t flags = 0;
1388 const char *arg;
1389 struct yin_argument_meta arg_meta = {&flags, &arg};
David Sedlák8e7bda82019-07-16 17:57:50 +02001390
1391 /* min subelems */
1392 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001393 "<argument name=\"arg\">"
1394 "</argument>"
1395 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001396 assert_int_equal(test_element_helper(state, data, &arg_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001397 assert_string_equal(arg, "arg");
1398 assert_true(flags == 0);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001399 lydict_remove(UTEST_LYCTX, arg);
David Sedlák8e7bda82019-07-16 17:57:50 +02001400}
1401
1402static void
David Sedlák8e7bda82019-07-16 17:57:50 +02001403test_belongsto_elem(void **state)
1404{
David Sedlák8e7bda82019-07-16 17:57:50 +02001405 const char *data;
1406 struct lysp_submodule submod;
1407
Michal Vasko8a67eff2021-12-07 14:04:47 +01001408 lydict_insert(UTEST_LYCTX, "module-name", 0, &PARSER_CUR_PMOD(YCTX)->mod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +02001409
David Sedlák8e7bda82019-07-16 17:57:50 +02001410 data = ELEMENT_WRAPPER_START "<belongs-to module=\"module-name\"></belongs-to>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001411 assert_int_equal(test_element_helper(state, data, &submod, NULL, NULL), LY_EVALID);
1412 CHECK_LOG_CTX("Missing mandatory sub-element \"prefix\" of \"belongs-to\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001413}
1414
1415static void
1416test_config_elem(void **state)
1417{
David Sedlák8e7bda82019-07-16 17:57:50 +02001418 const char *data;
1419 uint16_t flags = 0;
David Sedlák8e7bda82019-07-16 17:57:50 +02001420
1421 data = ELEMENT_WRAPPER_START "<config value=\"false\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001422 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001423 assert_true(flags & LYS_CONFIG_R);
David Sedlák8e7bda82019-07-16 17:57:50 +02001424 flags = 0;
1425
1426 data = ELEMENT_WRAPPER_START "<config value=\"invalid\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001427 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID);
1428 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"config\" element. "
1429 "Valid values are \"true\" and \"false\".", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001430}
1431
1432static void
1433test_default_elem(void **state)
1434{
David Sedlák8e7bda82019-07-16 17:57:50 +02001435 const char *data;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001436 struct lysp_qname val = {0};
David Sedlák8e7bda82019-07-16 17:57:50 +02001437
1438 data = ELEMENT_WRAPPER_START "<default/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001439 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
1440 CHECK_LOG_CTX("Missing mandatory attribute value of default element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001441}
1442
1443static void
1444test_err_app_tag_elem(void **state)
1445{
David Sedlák8e7bda82019-07-16 17:57:50 +02001446 const char *data;
1447 const char *val = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001448
1449 data = ELEMENT_WRAPPER_START "<error-app-tag/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001450 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
1451 CHECK_LOG_CTX("Missing mandatory attribute value of error-app-tag element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001452}
1453
1454static void
1455test_err_msg_elem(void **state)
1456{
David Sedlák8e7bda82019-07-16 17:57:50 +02001457 const char *data;
1458 const char *val = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001459
1460 data = ELEMENT_WRAPPER_START "<error-message></error-message>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001461 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
1462 CHECK_LOG_CTX("Missing mandatory sub-element \"value\" of \"error-message\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001463
David Sedlákdf2a9732019-08-07 13:23:16 +02001464 data = ELEMENT_WRAPPER_START "<error-message invalid=\"text\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001465 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
1466 CHECK_LOG_CTX("Unexpected attribute \"invalid\" of \"error-message\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001467}
1468
1469static void
1470test_fracdigits_elem(void **state)
1471{
David Sedlák8e7bda82019-07-16 17:57:50 +02001472 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001473 struct lysp_type type = {0};
David Sedlák8e7bda82019-07-16 17:57:50 +02001474
David Sedlák8e7bda82019-07-16 17:57:50 +02001475 /* invalid values */
1476 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"-1\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001477 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1478 CHECK_LOG_CTX("Invalid value \"-1\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001479
1480 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"02\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001481 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1482 CHECK_LOG_CTX("Invalid value \"02\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001483
1484 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"1p\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001485 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1486 CHECK_LOG_CTX("Invalid value \"1p\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001487
1488 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"19\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001489 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1490 CHECK_LOG_CTX("Invalid value \"19\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001491
1492 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"999999999999999999\"></fraction-digits>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001493 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1494 CHECK_LOG_CTX("Invalid value \"999999999999999999\" of \"value\" attribute in \"fraction-digits\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001495}
1496
1497static void
1498test_iffeature_elem(void **state)
1499{
David Sedlák8e7bda82019-07-16 17:57:50 +02001500 const char *data;
1501 const char **iffeatures = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001502
1503 data = ELEMENT_WRAPPER_START "<if-feature/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001504 assert_int_equal(test_element_helper(state, data, &iffeatures, NULL, NULL), LY_EVALID);
1505 CHECK_LOG_CTX("Missing mandatory attribute name of if-feature element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001506 LY_ARRAY_FREE(iffeatures);
1507 iffeatures = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001508}
1509
1510static void
1511test_length_elem(void **state)
1512{
David Sedlák8e7bda82019-07-16 17:57:50 +02001513 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001514 struct lysp_type type = {0};
David Sedlák8e7bda82019-07-16 17:57:50 +02001515
1516 /* max subelems */
1517 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001518 "<length value=\"length-str\">\n"
1519 " <error-message><value>err-msg</value></error-message>\n"
1520 " <error-app-tag value=\"err-app-tag\"/>\n"
1521 " <description><text>desc</text></description>\n"
1522 " <reference><text>ref</text></reference>\n"
1523 EXT_SUBELEM
1524 "</length>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001525 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001526 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1527 CHECK_LYSP_RESTR(type.length, "length-str", "desc",
1528 "err-app-tag", "err-msg", 1, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001529 assert_true(type.flags & LYS_SET_LENGTH);
Radek Krejci39b7fc22021-02-26 23:29:18 +01001530 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.length->exts[0]), LY_STMT_LENGTH);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001531 lysp_type_free(&fctx, &type);
David Sedlák8e7bda82019-07-16 17:57:50 +02001532 memset(&type, 0, sizeof(type));
1533
1534 /* min subelems */
1535 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001536 "<length value=\"length-str\">"
1537 "</length>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001538 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001539 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1540 CHECK_LYSP_RESTR(type.length, "length-str", NULL,
1541 NULL, NULL, 0, NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001542 lysp_type_free(&fctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001543 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001544 memset(&type, 0, sizeof(type));
1545
1546 data = ELEMENT_WRAPPER_START "<length></length>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001547 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
1548 CHECK_LOG_CTX("Missing mandatory attribute value of length element.", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001549 lysp_type_free(&fctx, &type);
David Sedlák8e7bda82019-07-16 17:57:50 +02001550 memset(&type, 0, sizeof(type));
David Sedlák8e7bda82019-07-16 17:57:50 +02001551}
1552
1553static void
1554test_modifier_elem(void **state)
1555{
David Sedlák8e7bda82019-07-16 17:57:50 +02001556 const char *data;
Radek Krejci011e4aa2020-09-04 15:22:31 +02001557 const char *pat;
David Sedlák8e7bda82019-07-16 17:57:50 +02001558
Radek Iša56ca9e42020-09-08 18:42:00 +02001559 assert_int_equal(LY_SUCCESS, lydict_insert(UTEST_LYCTX, "\006pattern", 8, &pat));
David Sedlák8e7bda82019-07-16 17:57:50 +02001560 data = ELEMENT_WRAPPER_START "<modifier value=\"invert\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001561 assert_int_equal(test_element_helper(state, data, &pat, NULL, NULL), LY_EVALID);
1562 CHECK_LOG_CTX("Invalid value \"invert\" of \"value\" attribute in \"modifier\" element. "
1563 "Only valid value is \"invert-match\".", "Line number 1.");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001564 lydict_remove(UTEST_LYCTX, pat);
David Sedlák8e7bda82019-07-16 17:57:50 +02001565}
1566
1567static void
1568test_namespace_elem(void **state)
1569{
David Sedlák8e7bda82019-07-16 17:57:50 +02001570 const char *data;
1571 const char *ns;
David Sedlák8e7bda82019-07-16 17:57:50 +02001572
1573 data = ELEMENT_WRAPPER_START "<namespace/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001574 assert_int_equal(test_element_helper(state, data, &ns, NULL, NULL), LY_EVALID);
1575 CHECK_LOG_CTX("Missing mandatory attribute uri of namespace element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001576}
1577
1578static void
David Sedlák8e7bda82019-07-16 17:57:50 +02001579test_pattern_elem(void **state)
1580{
David Sedlák8e7bda82019-07-16 17:57:50 +02001581 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001582 struct lysp_type type = {0};
David Sedlák8e7bda82019-07-16 17:57:50 +02001583
1584 /* max subelems */
1585 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001586 "<pattern value=\"super_pattern\">\n"
1587 " <modifier value=\"invert-match\"/>\n"
1588 " <error-message><value>err-msg-value</value></error-message>\n"
1589 " <error-app-tag value=\"err-app-tag-value\"/>\n"
1590 " <description><text>&quot;pattern-desc&quot;</text></description>\n"
1591 " <reference><text>pattern-ref</text></reference>\n"
1592 EXT_SUBELEM
1593 "</pattern>"
1594 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001595 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001596 assert_true(type.flags & LYS_SET_PATTERN);
Radek Iša56ca9e42020-09-08 18:42:00 +02001597 CHECK_LYSP_RESTR(type.patterns, "\x015super_pattern", "\"pattern-desc\"",
1598 "err-app-tag-value", "err-msg-value", 1, "pattern-ref");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001599 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.patterns->exts[0]), LY_STMT_PATTERN);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001600 lysp_type_free(&fctx, &type);
David Sedlák8e7bda82019-07-16 17:57:50 +02001601 memset(&type, 0, sizeof(type));
1602
1603 /* min subelems */
1604 data = ELEMENT_WRAPPER_START "<pattern value=\"pattern\"> </pattern>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001605 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1606 CHECK_LYSP_RESTR(type.patterns, "\x006pattern", NULL, NULL, NULL, 0, NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001607 lysp_type_free(&fctx, &type);
David Sedlák8e7bda82019-07-16 17:57:50 +02001608 memset(&type, 0, sizeof(type));
David Sedlák8e7bda82019-07-16 17:57:50 +02001609}
1610
1611static void
1612test_value_position_elem(void **state)
1613{
David Sedlák8e7bda82019-07-16 17:57:50 +02001614 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001615 struct lysp_type_enum en = {0};
David Sedlák8e7bda82019-07-16 17:57:50 +02001616
1617 /* valid values */
David Sedlák8e7bda82019-07-16 17:57:50 +02001618 data = ELEMENT_WRAPPER_START "<value value=\"-55\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001619 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1620 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, -55);
David Sedlák8e7bda82019-07-16 17:57:50 +02001621 memset(&en, 0, sizeof(en));
1622
1623 data = ELEMENT_WRAPPER_START "<value value=\"0\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001624 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1625 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, 0);
David Sedlák8e7bda82019-07-16 17:57:50 +02001626 memset(&en, 0, sizeof(en));
1627
1628 data = ELEMENT_WRAPPER_START "<value value=\"-0\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001629 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1630 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, 0);
David Sedlák8e7bda82019-07-16 17:57:50 +02001631 memset(&en, 0, sizeof(en));
1632
1633 /* valid positions */
David Sedlák8e7bda82019-07-16 17:57:50 +02001634 data = ELEMENT_WRAPPER_START "<position value=\"0\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001635 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_SUCCESS);
1636 CHECK_LYSP_TYPE_ENUM(&(en), NULL, 0, LYS_SET_VALUE, 0, NULL, NULL, 0);
David Sedlák8e7bda82019-07-16 17:57:50 +02001637 memset(&en, 0, sizeof(en));
1638
1639 /* invalid values */
1640 data = ELEMENT_WRAPPER_START "<value value=\"99999999999999999999999\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001641 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1642 CHECK_LOG_CTX("Invalid value \"99999999999999999999999\" of \"value\" attribute in \"value\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001643
1644 data = ELEMENT_WRAPPER_START "<value value=\"1k\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001645 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1646 CHECK_LOG_CTX("Invalid value \"1k\" of \"value\" attribute in \"value\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001647
David Sedlák69f01612019-07-17 11:41:08 +02001648 data = ELEMENT_WRAPPER_START "<value value=\"\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001649 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1650 CHECK_LOG_CTX("Invalid value \"\" of \"value\" attribute in \"value\" element.", "Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001651
David Sedlák8e7bda82019-07-16 17:57:50 +02001652 /*invalid positions */
1653 data = ELEMENT_WRAPPER_START "<position value=\"-5\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001654 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1655 CHECK_LOG_CTX("Invalid value \"-5\" of \"value\" attribute in \"position\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001656
1657 data = ELEMENT_WRAPPER_START "<position value=\"-0\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001658 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1659 CHECK_LOG_CTX("Invalid value \"-0\" of \"value\" attribute in \"position\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001660
1661 data = ELEMENT_WRAPPER_START "<position value=\"99999999999999999999\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001662 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1663 CHECK_LOG_CTX("Invalid value \"99999999999999999999\" of \"value\" attribute in \"position\" element.", "Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001664
David Sedlák69f01612019-07-17 11:41:08 +02001665 data = ELEMENT_WRAPPER_START "<position value=\"\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001666 assert_int_equal(test_element_helper(state, data, &en, NULL, NULL), LY_EVALID);
1667 CHECK_LOG_CTX("Invalid value \"\" of \"value\" attribute in \"position\" element.", "Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001668}
1669
1670static void
1671test_prefix_elem(void **state)
1672{
David Sedlák69f01612019-07-17 11:41:08 +02001673 const char *data;
1674 const char *value = NULL;
1675
1676 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001677 assert_int_equal(test_element_helper(state, data, &value, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001678 assert_string_equal(value, "pref");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001679 lydict_remove(UTEST_LYCTX, value);
David Sedlák69f01612019-07-17 11:41:08 +02001680}
1681
1682static void
1683test_range_elem(void **state)
1684{
David Sedlák69f01612019-07-17 11:41:08 +02001685 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001686 struct lysp_type type = {0};
David Sedlák69f01612019-07-17 11:41:08 +02001687
1688 /* max subelems */
1689 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001690 "<range value=\"range-str\">\n"
1691 " <error-message><value>err-msg</value></error-message>\n"
1692 " <error-app-tag value=\"err-app-tag\" />\n"
1693 " <description><text>desc</text></description>\n"
1694 " <reference><text>ref</text></reference>\n"
1695 EXT_SUBELEM
1696 "</range>"
1697 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001698 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1699 CHECK_LYSP_RESTR(type.range, "range-str", "desc",
1700 "err-app-tag", "err-msg", 1, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001701 assert_true(type.flags & LYS_SET_RANGE);
Radek Krejci39b7fc22021-02-26 23:29:18 +01001702 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.range->exts[0]), LY_STMT_RANGE);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001703 lysp_type_free(&fctx, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001704 memset(&type, 0, sizeof(type));
1705
1706 /* min subelems */
1707 data = ELEMENT_WRAPPER_START "<range value=\"range-str\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001708 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
1709 CHECK_LYSP_RESTR(type.range, "range-str", NULL,
1710 NULL, NULL, 0, NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001711 lysp_type_free(&fctx, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001712 memset(&type, 0, sizeof(type));
David Sedlák69f01612019-07-17 11:41:08 +02001713}
1714
1715static void
1716test_reqinstance_elem(void **state)
1717{
David Sedlák69f01612019-07-17 11:41:08 +02001718 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001719 struct lysp_type type = {0};
David Sedlák69f01612019-07-17 11:41:08 +02001720
David Sedlákd1144562019-08-06 12:36:14 +02001721 data = ELEMENT_WRAPPER_START "<require-instance value=\"true\">" EXT_SUBELEM "</require-instance>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001722 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001723 assert_int_equal(type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001724 assert_true(type.flags & LYS_SET_REQINST);
Radek Krejci5984d222021-02-26 23:01:45 +01001725 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.exts[0]), LY_STMT_REQUIRE_INSTANCE);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001726 lysp_type_free(&fctx, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001727 memset(&type, 0, sizeof(type));
1728
1729 data = ELEMENT_WRAPPER_START "<require-instance value=\"false\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001730 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001731 assert_int_equal(type.require_instance, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001732 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001733 memset(&type, 0, sizeof(type));
1734
1735 data = ELEMENT_WRAPPER_START "<require-instance value=\"invalid\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001736 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_EVALID);
David Sedlák69f01612019-07-17 11:41:08 +02001737 memset(&type, 0, sizeof(type));
Radek Iša56ca9e42020-09-08 18:42:00 +02001738 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"require-instance\" element. "
1739 "Valid values are \"true\" and \"false\".", "Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001740}
1741
1742static void
1743test_revision_date_elem(void **state)
1744{
David Sedlák69f01612019-07-17 11:41:08 +02001745 const char *data;
1746 char rev[LY_REV_SIZE];
1747
1748 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001749 assert_int_equal(test_element_helper(state, data, rev, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001750 assert_string_equal(rev, "2000-01-01");
1751
1752 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-50-05\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001753 assert_int_equal(test_element_helper(state, data, rev, NULL, NULL), LY_EVALID);
1754 CHECK_LOG_CTX("Invalid value \"2000-50-05\" of \"revision-date\".", "Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001755}
1756
1757static void
1758test_unique_elem(void **state)
1759{
David Sedlák69f01612019-07-17 11:41:08 +02001760 const char *data;
1761 const char **values = NULL;
1762
1763 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001764 assert_int_equal(test_element_helper(state, data, &values, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001765 assert_string_equal(*values, "tag");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001766 lydict_remove(UTEST_LYCTX, *values);
David Sedlák69f01612019-07-17 11:41:08 +02001767 LY_ARRAY_FREE(values);
David Sedlákd1144562019-08-06 12:36:14 +02001768 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001769}
1770
1771static void
1772test_units_elem(void **state)
1773{
David Sedlák69f01612019-07-17 11:41:08 +02001774 const char *data;
1775 const char *values = NULL;
1776
1777 data = ELEMENT_WRAPPER_START "<units name=\"name\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001778 assert_int_equal(test_element_helper(state, data, &values, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001779 assert_string_equal(values, "name");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001780 lydict_remove(UTEST_LYCTX, values);
David Sedlákd1144562019-08-06 12:36:14 +02001781 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001782}
1783
1784static void
David Sedlák69f01612019-07-17 11:41:08 +02001785test_yin_text_value_elem(void **state)
1786{
David Sedlák69f01612019-07-17 11:41:08 +02001787 const char *data;
1788 const char *val;
1789
1790 data = ELEMENT_WRAPPER_START "<text>text</text>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001791 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001792 assert_string_equal(val, "text");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001793 lydict_remove(UTEST_LYCTX, val);
David Sedlák69f01612019-07-17 11:41:08 +02001794
1795 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 +02001796 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001797 assert_string_equal(val, "text");
Michal Vaskoe180ed02021-02-05 16:31:20 +01001798 lydict_remove(UTEST_LYCTX, val);
David Sedlák69f01612019-07-17 11:41:08 +02001799
1800 data = ELEMENT_WRAPPER_START "<text></text>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001801 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001802 assert_string_equal("", val);
Michal Vaskoe180ed02021-02-05 16:31:20 +01001803 lydict_remove(UTEST_LYCTX, val);
David Sedlák8e7bda82019-07-16 17:57:50 +02001804}
David Sedlák32488102019-07-15 17:44:10 +02001805
David Sedlák374d2b32019-07-17 15:06:55 +02001806static void
1807test_type_elem(void **state)
1808{
David Sedlák374d2b32019-07-17 15:06:55 +02001809 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001810 struct lysp_type type = {0};
David Sedlák374d2b32019-07-17 15:06:55 +02001811
1812 /* max subelems */
1813 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001814 "<type name=\"type-name\">\n"
1815 " <base name=\"base-name\"/>\n"
1816 " <bit name=\"bit\"/>\n"
1817 " <enum name=\"enum\"/>\n"
1818 " <fraction-digits value=\"2\"/>\n"
1819 " <length value=\"length\"/>\n"
1820 " <path value=\"/path\"/>\n"
1821 " <pattern value=\"pattern\"/>\n"
1822 " <range value=\"range\" />\n"
1823 " <require-instance value=\"true\"/>\n"
1824 " <type name=\"sub-type-name\"/>\n"
1825 EXT_SUBELEM
1826 "</type>"
1827 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001828 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák374d2b32019-07-17 15:06:55 +02001829 assert_string_equal(type.name, "type-name");
1830 assert_string_equal(*type.bases, "base-name");
1831 assert_string_equal(type.bits->name, "bit");
1832 assert_string_equal(type.enums->name, "enum");
1833 assert_int_equal(type.fraction_digits, 2);
Radek Iša56ca9e42020-09-08 18:42:00 +02001834 CHECK_LYSP_RESTR(type.length, "length", NULL,
1835 NULL, NULL, 0, NULL);
Michal Vaskocb8c6d42020-10-16 11:58:30 +02001836 assert_string_equal(type.path->expr, "/path");
Radek Iša56ca9e42020-09-08 18:42:00 +02001837 CHECK_LYSP_RESTR(type.patterns, "\006pattern", NULL,
1838 NULL, NULL, 0, NULL);
1839 CHECK_LYSP_RESTR(type.range, "range", NULL,
1840 NULL, NULL, 0, NULL);
David Sedlák374d2b32019-07-17 15:06:55 +02001841 assert_int_equal(type.require_instance, 1);
1842 assert_string_equal(type.types->name, "sub-type-name");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001843 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(type.exts[0]), LY_STMT_TYPE);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001844 assert_true(type.flags & LYS_SET_BASE);
1845 assert_true(type.flags & LYS_SET_BIT);
1846 assert_true(type.flags & LYS_SET_ENUM);
1847 assert_true(type.flags & LYS_SET_FRDIGITS);
1848 assert_true(type.flags & LYS_SET_LENGTH);
1849 assert_true(type.flags & LYS_SET_PATH);
1850 assert_true(type.flags & LYS_SET_PATTERN);
1851 assert_true(type.flags & LYS_SET_RANGE);
1852 assert_true(type.flags & LYS_SET_REQINST);
1853 assert_true(type.flags & LYS_SET_TYPE);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001854 lysp_type_free(&fctx, &type);
David Sedlák374d2b32019-07-17 15:06:55 +02001855 memset(&type, 0, sizeof(type));
1856
1857 /* min subelems */
1858 data = ELEMENT_WRAPPER_START "<type name=\"type-name\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001859 assert_int_equal(test_element_helper(state, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001860 lysp_type_free(&fctx, &type);
David Sedlák374d2b32019-07-17 15:06:55 +02001861 memset(&type, 0, sizeof(type));
David Sedlák374d2b32019-07-17 15:06:55 +02001862}
1863
David Sedlák1af868e2019-07-17 17:03:14 +02001864static void
1865test_max_elems_elem(void **state)
1866{
David Sedlák1af868e2019-07-17 17:03:14 +02001867 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001868 struct lysp_node_list list = {0};
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001869 struct lysp_refine refine = {0};
David Sedlák1af868e2019-07-17 17:03:14 +02001870
David Sedlák1af868e2019-07-17 17:03:14 +02001871 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"10\"/> </refine>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001872 assert_int_equal(test_element_helper(state, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02001873 assert_int_equal(refine.max, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001874 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001875
1876 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"0\"/> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001877 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID);
1878 CHECK_LOG_CTX("Invalid value \"0\" of \"value\" attribute in \"max-elements\" element.", "Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02001879
1880 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"-10\"/> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001881 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID);
1882 CHECK_LOG_CTX("Invalid value \"-10\" of \"value\" attribute in \"max-elements\" element.", "Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02001883
1884 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"k\"/> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001885 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID);
1886 CHECK_LOG_CTX("Invalid value \"k\" of \"value\" attribute in \"max-elements\" element.", "Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02001887
1888 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"u12\"/> </list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001889 assert_int_equal(test_element_helper(state, data, &list, NULL, NULL), LY_EVALID);
1890 CHECK_LOG_CTX("Invalid value \"u12\" of \"value\" attribute in \"max-elements\" element.", "Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02001891}
1892
David Sedlák09e18c92019-07-18 11:17:11 +02001893static void
1894test_min_elems_elem(void **state)
1895{
David Sedlák09e18c92019-07-18 11:17:11 +02001896 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02001897 struct lysp_node_leaflist llist = {0};
David Sedlák09e18c92019-07-18 11:17:11 +02001898
1899 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"-5\"/> </leaf-list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001900 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID);
1901 CHECK_LOG_CTX("Value \"-5\" of \"value\" attribute in \"min-elements\" element is out of bounds.", "Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02001902
1903 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"99999999999999999\"/> </leaf-list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001904 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID);
1905 CHECK_LOG_CTX("Value \"99999999999999999\" of \"value\" attribute in \"min-elements\" element is out of bounds.", "Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02001906
1907 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"5k\"/> </leaf-list>";
Radek Iša56ca9e42020-09-08 18:42:00 +02001908 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID);
1909 CHECK_LOG_CTX("Invalid value \"5k\" of \"value\" attribute in \"min-elements\" element.", "Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02001910
1911 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 +02001912 assert_int_equal(test_element_helper(state, data, &llist, NULL, NULL), LY_EVALID);
1913 CHECK_LOG_CTX("Invalid value \"05\" of \"value\" attribute in \"min-elements\" element.", "Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02001914}
1915
David Sedláka2dad212019-07-18 12:45:19 +02001916static void
1917test_ordby_elem(void **state)
1918{
David Sedláka2dad212019-07-18 12:45:19 +02001919 const char *data;
1920 uint16_t flags = 0;
David Sedláka2dad212019-07-18 12:45:19 +02001921
1922 data = ELEMENT_WRAPPER_START "<ordered-by value=\"user\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001923 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001924 assert_true(flags & LYS_ORDBY_USER);
David Sedláka2dad212019-07-18 12:45:19 +02001925
1926 data = ELEMENT_WRAPPER_START "<ordered-by value=\"inv\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001927 assert_int_equal(test_element_helper(state, data, &flags, NULL, NULL), LY_EVALID);
1928 CHECK_LOG_CTX("Invalid value \"inv\" of \"value\" attribute in \"ordered-by\" element. "
1929 "Valid values are \"system\" and \"user\".", "Line number 1.");
David Sedláka2dad212019-07-18 12:45:19 +02001930}
1931
David Sedlák8a83bbb2019-07-18 14:46:00 +02001932static void
1933test_any_elem(void **state)
1934{
David Sedlák8a83bbb2019-07-18 14:46:00 +02001935 const char *data;
1936 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02001937 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák8a83bbb2019-07-18 14:46:00 +02001938 struct lysp_node_anydata *parsed = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02001939 uint16_t flags;
David Sedlák8a83bbb2019-07-18 14:46:00 +02001940
1941 /* anyxml max subelems */
1942 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001943 "<anyxml name=\"any-name\">\n"
1944 " <config value=\"true\" />\n"
1945 " <description><text>desc</text></description>\n"
1946 " <if-feature name=\"feature\" />\n"
1947 " <mandatory value=\"true\" />\n"
1948 " <must condition=\"must-cond\" />\n"
1949 " <reference><text>ref</text></reference>\n"
1950 " <status value=\"deprecated\"/>\n"
1951 " <when condition=\"when-cond\"/>\n"
1952 EXT_SUBELEM
1953 "</anyxml>"
1954 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001955 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001956 parsed = (struct lysp_node_anydata *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02001957 flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_DEPRC;
1958 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
1959 "any-name", 0, LYS_ANYXML, 0, "ref", 1);
1960 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001961 assert_string_equal(parsed->iffeatures[0].str, "feature");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001962 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_ANYXML);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001963 lysp_node_free(&fctx, siblings);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001964 siblings = NULL;
1965
1966 /* anydata max subelems */
1967 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001968 "<anydata name=\"any-name\">\n"
1969 " <config value=\"true\" />\n"
1970 " <description><text>desc</text></description>\n"
1971 " <if-feature name=\"feature\" />\n"
1972 " <mandatory value=\"true\" />\n"
1973 " <must condition=\"must-cond\" />\n"
1974 " <reference><text>ref</text></reference>\n"
1975 " <status value=\"deprecated\"/>\n"
1976 " <when condition=\"when-cond\"/>\n"
1977 EXT_SUBELEM
1978 "</anydata>"
1979 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001980 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001981 parsed = (struct lysp_node_anydata *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02001982 flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_DEPRC;
1983 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
1984 "any-name", 0, LYS_ANYDATA, 0, "ref", 1);
1985 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001986 assert_string_equal(parsed->iffeatures[0].str, "feature");
Radek Krejci39b7fc22021-02-26 23:29:18 +01001987 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_ANYDATA);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001988 lysp_node_free(&fctx, siblings);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001989 siblings = NULL;
1990
1991 /* min subelems */
1992 node_meta.parent = (void *)0x10;
1993 data = ELEMENT_WRAPPER_START "<anydata name=\"any-name\"> </anydata>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02001994 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001995 parsed = (struct lysp_node_anydata *)siblings;
1996 assert_ptr_equal(parsed->parent, node_meta.parent);
Radek Iša56ca9e42020-09-08 18:42:00 +02001997 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
1998 "any-name", 0, LYS_ANYDATA, 1, NULL, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001999 lysp_node_free(&fctx, siblings);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002000}
2001
David Sedlák203ca3a2019-07-18 15:26:25 +02002002static void
2003test_leaf_elem(void **state)
2004{
David Sedlák203ca3a2019-07-18 15:26:25 +02002005 const char *data;
2006 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002007 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák203ca3a2019-07-18 15:26:25 +02002008 struct lysp_node_leaf *parsed = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02002009 uint16_t flags;
David Sedlák203ca3a2019-07-18 15:26:25 +02002010
2011 /* max elements */
2012 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002013 "<leaf name=\"leaf\">\n"
2014 " <config value=\"true\" />\n"
2015 " <default value=\"def-val\"/>\n"
2016 " <description><text>desc</text></description>\n"
2017 " <if-feature name=\"feature\" />\n"
2018 " <mandatory value=\"true\" />\n"
2019 " <must condition=\"must-cond\" />\n"
2020 " <reference><text>ref</text></reference>\n"
2021 " <status value=\"deprecated\"/>\n"
2022 " <type name=\"type\"/>\n"
2023 " <units name=\"uni\"/>\n"
2024 " <when condition=\"when-cond\"/>\n"
2025 EXT_SUBELEM
2026 "</leaf>"
David Sedlák203ca3a2019-07-18 15:26:25 +02002027 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002028 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák203ca3a2019-07-18 15:26:25 +02002029 parsed = (struct lysp_node_leaf *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002030 flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_DEPRC;
2031 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2032 "leaf", 0, LYS_LEAF, 0, "ref", 1);
2033 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002034 assert_string_equal(parsed->iffeatures[0].str, "feature");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002035 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_LEAF);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002036 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlák203ca3a2019-07-18 15:26:25 +02002037 assert_string_equal(parsed->type.name, "type");
2038 assert_string_equal(parsed->units, "uni");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002039 assert_string_equal(parsed->dflt.str, "def-val");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002040 lysp_node_free(&fctx, siblings);
David Sedlák203ca3a2019-07-18 15:26:25 +02002041 siblings = NULL;
2042
2043 /* min elements */
2044 data = ELEMENT_WRAPPER_START "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002045 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák203ca3a2019-07-18 15:26:25 +02002046 parsed = (struct lysp_node_leaf *)siblings;
2047 assert_string_equal(parsed->name, "leaf");
2048 assert_string_equal(parsed->type.name, "type");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002049 lysp_node_free(&fctx, siblings);
David Sedlák203ca3a2019-07-18 15:26:25 +02002050 siblings = NULL;
David Sedlák203ca3a2019-07-18 15:26:25 +02002051}
2052
David Sedlákc3da3ef2019-07-19 12:56:08 +02002053static void
2054test_leaf_list_elem(void **state)
2055{
David Sedlákc3da3ef2019-07-19 12:56:08 +02002056 const char *data;
2057 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002058 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlákc3da3ef2019-07-19 12:56:08 +02002059 struct lysp_node_leaflist *parsed = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02002060 uint16_t flags;
David Sedlákc3da3ef2019-07-19 12:56:08 +02002061
2062 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002063 "<leaf-list name=\"llist\">\n"
2064 " <config value=\"true\" />\n"
2065 " <default value=\"def-val0\"/>\n"
2066 " <default value=\"def-val1\"/>\n"
2067 " <description><text>desc</text></description>\n"
2068 " <if-feature name=\"feature\"/>\n"
2069 " <max-elements value=\"5\"/>\n"
2070 " <must condition=\"must-cond\"/>\n"
2071 " <ordered-by value=\"user\" />\n"
2072 " <reference><text>ref</text></reference>\n"
2073 " <status value=\"current\"/>\n"
2074 " <type name=\"type\"/>\n"
2075 " <units name=\"uni\"/>\n"
2076 " <when condition=\"when-cond\"/>\n"
2077 EXT_SUBELEM
2078 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002079 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002080 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002081 parsed = (struct lysp_node_leaflist *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002082 flags = LYS_CONFIG_W | LYS_ORDBY_USER | LYS_STATUS_CURR | LYS_SET_MAX;
2083 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2084 "llist", 0, LYS_LEAFLIST, 0, "ref", 1);
2085 CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002086 assert_string_equal(parsed->dflts[0].str, "def-val0");
2087 assert_string_equal(parsed->dflts[1].str, "def-val1");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002088 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002089 assert_int_equal(parsed->max, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002090 assert_string_equal(parsed->type.name, "type");
2091 assert_string_equal(parsed->units, "uni");
Radek Iša56ca9e42020-09-08 18:42:00 +02002092 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002093 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_LEAF_LIST);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002094 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002095 siblings = NULL;
2096
2097 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002098 "<leaf-list name=\"llist\">\n"
2099 " <config value=\"true\" />\n"
2100 " <description><text>desc</text></description>\n"
2101 " <if-feature name=\"feature\"/>\n"
2102 " <min-elements value=\"5\"/>\n"
2103 " <must condition=\"must-cond\"/>\n"
2104 " <ordered-by value=\"user\" />\n"
2105 " <reference><text>ref</text></reference>\n"
2106 " <status value=\"current\"/>\n"
2107 " <type name=\"type\"/>\n"
2108 " <units name=\"uni\"/>\n"
2109 " <when condition=\"when-cond\"/>\n"
2110 EXT_SUBELEM
2111 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002112 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002113 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002114 parsed = (struct lysp_node_leaflist *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002115 flags = LYS_CONFIG_W | LYS_ORDBY_USER | LYS_STATUS_CURR | LYS_SET_MIN;
2116 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2117 "llist", 0, LYS_LEAFLIST, 0, "ref", 1);
2118 CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL);
2119 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002120 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002121 assert_int_equal(parsed->min, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002122 assert_string_equal(parsed->type.name, "type");
2123 assert_string_equal(parsed->units, "uni");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002124 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_LEAF_LIST);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002125 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002126 siblings = NULL;
2127
2128 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002129 "<leaf-list name=\"llist\">\n"
2130 " <config value=\"true\" />\n"
2131 " <description><text>desc</text></description>\n"
2132 " <if-feature name=\"feature\"/>\n"
2133 " <max-elements value=\"15\"/>\n"
2134 " <min-elements value=\"5\"/>\n"
2135 " <must condition=\"must-cond\"/>\n"
2136 " <ordered-by value=\"user\" />\n"
2137 " <reference><text>ref</text></reference>\n"
2138 " <status value=\"current\"/>\n"
2139 " <type name=\"type\"/>\n"
2140 " <units name=\"uni\"/>\n"
2141 " <when condition=\"when-cond\"/>\n"
2142 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002143 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002144 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002145 parsed = (struct lysp_node_leaflist *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002146 flags = LYS_CONFIG_W | LYS_ORDBY_USER | LYS_STATUS_CURR | LYS_SET_MIN | LYS_SET_MAX;
2147 CHECK_LYSP_NODE(parsed, "desc", 0, flags, 1,
2148 "llist", 0, LYS_LEAFLIST, 0, "ref", 1);
2149 CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL);
2150 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002151 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002152 assert_int_equal(parsed->min, 5);
2153 assert_int_equal(parsed->max, 15);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002154 assert_string_equal(parsed->type.name, "type");
2155 assert_string_equal(parsed->units, "uni");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002156 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002157 siblings = NULL;
2158
2159 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002160 "<leaf-list name=\"llist\">\n"
2161 " <type name=\"type\"/>\n"
2162 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002163 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002164 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002165 parsed = (struct lysp_node_leaflist *)siblings;
2166 assert_string_equal(parsed->name, "llist");
2167 assert_string_equal(parsed->type.name, "type");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002168 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002169 siblings = NULL;
2170
2171 /* invalid combinations */
2172 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002173 "<leaf-list name=\"llist\">\n"
2174 " <max-elements value=\"5\"/>\n"
2175 " <min-elements value=\"15\"/>\n"
2176 " <type name=\"type\"/>"
2177 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002178 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002179 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_EVALID);
2180 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 +02002181 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002182 siblings = NULL;
2183
2184 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002185 "<leaf-list name=\"llist\">\n"
2186 " <default value=\"def-val1\"/>\n"
2187 " <min-elements value=\"15\"/>\n"
2188 " <type name=\"type\"/>\n"
2189 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002190 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002191 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_EVALID);
2192 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 +02002193 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002194 siblings = NULL;
2195
2196 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002197 "<leaf-list name=\"llist\">"
2198 "</leaf-list>"
David Sedlákc3da3ef2019-07-19 12:56:08 +02002199 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002200 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_EVALID);
2201 CHECK_LOG_CTX("Missing mandatory sub-element \"type\" of \"leaf-list\" element.", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002202 lysp_node_free(&fctx, siblings);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002203 siblings = NULL;
David Sedlákc3da3ef2019-07-19 12:56:08 +02002204}
2205
David Sedlákcb39f642019-07-19 13:19:55 +02002206static void
2207test_presence_elem(void **state)
2208{
David Sedlákcb39f642019-07-19 13:19:55 +02002209 const char *data;
2210 const char *val;
2211
2212 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002213 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlákcb39f642019-07-19 13:19:55 +02002214 assert_string_equal(val, "presence-val");
Michal Vaskoe180ed02021-02-05 16:31:20 +01002215 lydict_remove(UTEST_LYCTX, val);
David Sedlákcb39f642019-07-19 13:19:55 +02002216
2217 data = ELEMENT_WRAPPER_START "<presence/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002218 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
2219 CHECK_LOG_CTX("Missing mandatory attribute value of presence element.", "Line number 1.");
David Sedlákcb39f642019-07-19 13:19:55 +02002220}
2221
David Sedlák12470a82019-07-19 13:44:36 +02002222static void
2223test_key_elem(void **state)
2224{
David Sedlák12470a82019-07-19 13:44:36 +02002225 const char *data;
2226 const char *val;
2227
2228 data = ELEMENT_WRAPPER_START "<key value=\"key-value\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002229 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák12470a82019-07-19 13:44:36 +02002230 assert_string_equal(val, "key-value");
Michal Vaskoe180ed02021-02-05 16:31:20 +01002231 lydict_remove(UTEST_LYCTX, val);
David Sedlák12470a82019-07-19 13:44:36 +02002232
2233 data = ELEMENT_WRAPPER_START "<key/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002234 assert_int_equal(test_element_helper(state, data, &val, NULL, NULL), LY_EVALID);
2235 CHECK_LOG_CTX("Missing mandatory attribute value of key element.", "Line number 1.");
David Sedlák12470a82019-07-19 13:44:36 +02002236}
2237
David Sedlák04e17b22019-07-19 15:29:48 +02002238static void
David Sedlák0d6de5a2019-07-22 13:25:44 +02002239test_uses_elem(void **state)
2240{
David Sedlák0d6de5a2019-07-22 13:25:44 +02002241 const char *data;
2242 struct lysp_node *siblings = NULL;
2243 struct tree_node_meta node_meta = {NULL, &siblings};
2244 struct lysp_node_uses *parsed = NULL;
2245
2246 /* max subelems */
2247 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002248 "<uses name=\"uses-name\">\n"
2249 " <when condition=\"cond\" />\n"
2250 " <if-feature name=\"feature\" />\n"
2251 " <status value=\"obsolete\" />\n"
2252 " <description><text>desc</text></description>\n"
2253 " <reference><text>ref</text></reference>\n"
2254 " <refine target-node=\"target\"/>\n"
2255 " <augment target-node=\"target\" />\n"
2256 EXT_SUBELEM
2257 "</uses>"
2258 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002259 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002260 parsed = (struct lysp_node_uses *)&siblings[0];
Radek Iša56ca9e42020-09-08 18:42:00 +02002261 CHECK_LYSP_NODE(parsed, "desc", 1, LYS_STATUS_OBSLT, 1,
2262 "uses-name", 0, LYS_USES, 0, "ref", 1);
2263 CHECK_LYSP_WHEN(parsed->when, "cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002264 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlák0d6de5a2019-07-22 13:25:44 +02002265 assert_string_equal(parsed->refines->nodeid, "target");
David Sedlák992fb7c2019-07-24 16:51:01 +02002266 assert_string_equal(parsed->augments->nodeid, "target");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002267 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_USES);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002268 lysp_node_free(&fctx, siblings);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002269 siblings = NULL;
2270
2271 /* min subelems */
2272 data = ELEMENT_WRAPPER_START "<uses name=\"uses-name\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002273 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002274 assert_string_equal(siblings[0].name, "uses-name");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002275 lysp_node_free(&fctx, siblings);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002276 siblings = NULL;
David Sedlák0d6de5a2019-07-22 13:25:44 +02002277}
2278
David Sedlákaa854b02019-07-22 14:17:10 +02002279static void
David Sedlákaf536aa2019-07-23 13:42:23 +02002280test_list_elem(void **state)
2281{
David Sedlákaf536aa2019-07-23 13:42:23 +02002282 const char *data;
2283 struct lysp_node *siblings = NULL;
2284 struct tree_node_meta node_meta = {NULL, &siblings};
2285 struct lysp_node_list *parsed = NULL;
2286
2287 /* max subelems */
2288 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002289 "<list name=\"list-name\">\n"
2290 " <when condition=\"when\"/>\n"
2291 " <if-feature name=\"iff\"/>\n"
2292 " <must condition=\"must-cond\"/>\n"
2293 " <key value=\"key\"/>\n"
2294 " <unique tag=\"utag\"/>\n"
2295 " <config value=\"true\"/>\n"
2296 " <min-elements value=\"10\"/>\n"
2297 " <ordered-by value=\"user\"/>\n"
2298 " <status value=\"deprecated\"/>\n"
2299 " <description><text>desc</text></description>\n"
2300 " <reference><text>ref</text></reference>\n"
2301 " <anydata name=\"anyd\"/>\n"
2302 " <anyxml name=\"anyx\"/>\n"
2303 " <container name=\"cont\"/>\n"
2304 " <choice name=\"choice\"/>\n"
2305 " <action name=\"action\"/>\n"
2306 " <grouping name=\"grp\"/>\n"
2307 " <notification name=\"notf\"/>\n"
2308 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2309 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2310 " <list name=\"sub-list\"/>\n"
2311 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2312 " <uses name=\"uses-name\"/>\n"
2313 EXT_SUBELEM
2314 "</list>"
2315 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002316 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákaf536aa2019-07-23 13:42:23 +02002317 parsed = (struct lysp_node_list *)&siblings[0];
David Sedlákaf536aa2019-07-23 13:42:23 +02002318 assert_string_equal(parsed->child->name, "anyd");
2319 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2320 assert_string_equal(parsed->child->next->name, "anyx");
2321 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
David Sedlákf111bcb2019-07-23 17:15:51 +02002322 assert_string_equal(parsed->child->next->next->name, "cont");
2323 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002324 assert_string_equal(parsed->child->next->next->next->name, "choice");
2325 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
David Sedlák85d0eca2019-07-24 15:15:21 +02002326 assert_string_equal(parsed->child->next->next->next->next->name, "leaf");
2327 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAF);
2328 assert_string_equal(parsed->child->next->next->next->next->next->name, "llist");
2329 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2330 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "sub-list");
2331 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LIST);
2332 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "uses-name");
2333 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_USES);
2334 assert_null(parsed->child->next->next->next->next->next->next->next->next);
Radek Iša56ca9e42020-09-08 18:42:00 +02002335 uint16_t flags = LYS_ORDBY_USER | LYS_STATUS_DEPRC | LYS_CONFIG_W | LYS_SET_MIN;
2336
2337 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2338 "list-name", 0, LYS_LIST, 0, "ref", 1);
2339 CHECK_LYSP_RESTR(parsed->musts, "must-cond", NULL, NULL, NULL, 0, NULL);
2340 CHECK_LYSP_WHEN(parsed->when, "when", NULL, 0, NULL);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002341 assert_string_equal(parsed->groupings->name, "grp");
David Sedlák85d0eca2019-07-24 15:15:21 +02002342 assert_string_equal(parsed->actions->name, "action");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002343 assert_int_equal(parsed->groupings->nodetype, LYS_GROUPING);
David Sedlák031b9e72019-07-23 15:19:37 +02002344 assert_string_equal(parsed->notifs->name, "notf");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002345 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlákaf536aa2019-07-23 13:42:23 +02002346 assert_string_equal(parsed->key, "key");
2347 assert_int_equal(parsed->min, 10);
David Sedlákaf536aa2019-07-23 13:42:23 +02002348 assert_string_equal(parsed->typedefs->name, "tpdf");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002349 assert_string_equal(parsed->uniques->str, "utag");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002350 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_LIST);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002351 lysp_node_free(&fctx, siblings);
Radek Iša56ca9e42020-09-08 18:42:00 +02002352 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
David Sedlákaf536aa2019-07-23 13:42:23 +02002353 siblings = NULL;
2354
2355 /* min subelems */
2356 data = ELEMENT_WRAPPER_START "<list name=\"list-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002357 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákaf536aa2019-07-23 13:42:23 +02002358 parsed = (struct lysp_node_list *)&siblings[0];
Radek Iša56ca9e42020-09-08 18:42:00 +02002359 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2360 "list-name", 0, LYS_LIST, 0, NULL, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002361 lysp_node_free(&fctx, siblings);
David Sedlákaf536aa2019-07-23 13:42:23 +02002362 siblings = NULL;
David Sedlákaf536aa2019-07-23 13:42:23 +02002363}
2364
David Sedlák031b9e72019-07-23 15:19:37 +02002365static void
2366test_notification_elem(void **state)
2367{
David Sedlák031b9e72019-07-23 15:19:37 +02002368 const char *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002369 struct lysp_node_notif *notifs = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002370 struct tree_node_meta notif_meta = {NULL, (struct lysp_node **)&notifs};
David Sedlák031b9e72019-07-23 15:19:37 +02002371
2372 /* max subelems */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002373 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlák031b9e72019-07-23 15:19:37 +02002374 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002375 "<notification name=\"notif-name\">\n"
2376 " <anydata name=\"anyd\"/>\n"
2377 " <anyxml name=\"anyx\"/>\n"
2378 " <description><text>desc</text></description>\n"
2379 " <if-feature name=\"iff\"/>\n"
2380 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2381 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2382 " <list name=\"sub-list\"/>\n"
2383 " <must condition=\"cond\"/>\n"
2384 " <reference><text>ref</text></reference>\n"
2385 " <status value=\"deprecated\"/>\n"
2386 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2387 " <uses name=\"uses-name\"/>\n"
2388 " <container name=\"cont\"/>\n"
2389 " <choice name=\"choice\"/>\n"
2390 " <grouping name=\"grp\"/>\n"
2391 EXT_SUBELEM
2392 "</notification>"
2393 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002394 assert_int_equal(test_element_helper(state, data, &notif_meta, NULL, NULL), LY_SUCCESS);
David Sedlák031b9e72019-07-23 15:19:37 +02002395 assert_string_equal(notifs->name, "notif-name");
Radek Krejci01180ac2021-01-27 08:48:22 +01002396 assert_string_equal(notifs->child->name, "anyd");
2397 assert_int_equal(notifs->child->nodetype, LYS_ANYDATA);
2398 assert_string_equal(notifs->child->next->name, "anyx");
2399 assert_int_equal(notifs->child->next->nodetype, LYS_ANYXML);
2400 assert_string_equal(notifs->child->next->next->name, "leaf");
2401 assert_int_equal(notifs->child->next->next->nodetype, LYS_LEAF);
2402 assert_string_equal(notifs->child->next->next->next->name, "llist");
2403 assert_int_equal(notifs->child->next->next->next->nodetype, LYS_LEAFLIST);
2404 assert_string_equal(notifs->child->next->next->next->next->name, "sub-list");
2405 assert_int_equal(notifs->child->next->next->next->next->nodetype, LYS_LIST);
David Sedlák031b9e72019-07-23 15:19:37 +02002406 assert_true(notifs->flags & LYS_STATUS_DEPRC);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002407 assert_string_equal(notifs->groupings->name, "grp");
2408 assert_int_equal(notifs->groupings->nodetype, LYS_GROUPING);
Radek Krejci01180ac2021-01-27 08:48:22 +01002409 assert_string_equal(notifs->child->next->next->next->next->next->name, "uses-name");
2410 assert_int_equal(notifs->child->next->next->next->next->next->nodetype, LYS_USES);
2411 assert_string_equal(notifs->child->next->next->next->next->next->next->name, "cont");
2412 assert_int_equal(notifs->child->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
2413 assert_int_equal(notifs->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2414 assert_string_equal(notifs->child->next->next->next->next->next->next->next->name, "choice");
2415 assert_null(notifs->child->next->next->next->next->next->next->next->next);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002416 assert_string_equal(notifs->iffeatures[0].str, "iff");
2417 assert_string_equal(notifs->musts->arg.str, "cond");
David Sedlák031b9e72019-07-23 15:19:37 +02002418 assert_int_equal(notifs->nodetype, LYS_NOTIF);
2419 assert_null(notifs->parent);
2420 assert_string_equal(notifs->ref, "ref");
2421 assert_string_equal(notifs->typedefs->name, "tpdf");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002422 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(notifs->exts[0]), LY_STMT_NOTIFICATION);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002423 lysp_node_free(&fctx, (struct lysp_node *)notifs);
David Sedlák031b9e72019-07-23 15:19:37 +02002424 notifs = NULL;
2425
2426 /* min subelems */
2427 data = ELEMENT_WRAPPER_START "<notification name=\"notif-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002428 assert_int_equal(test_element_helper(state, data, &notif_meta, NULL, NULL), LY_SUCCESS);
David Sedlák031b9e72019-07-23 15:19:37 +02002429 assert_string_equal(notifs->name, "notif-name");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002430 lysp_node_free(&fctx, (struct lysp_node *)notifs);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002431 notifs = NULL;
David Sedláke3ce9ef2019-07-23 16:34:30 +02002432}
2433
2434static void
2435test_grouping_elem(void **state)
2436{
David Sedláke3ce9ef2019-07-23 16:34:30 +02002437 const char *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002438 struct lysp_node_grp *grps = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002439 struct tree_node_meta grp_meta = {NULL, (struct lysp_node **)&grps};
David Sedláke3ce9ef2019-07-23 16:34:30 +02002440
2441 /* max subelems */
2442 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002443 "<grouping name=\"grp-name\">\n"
2444 " <anydata name=\"anyd\"/>\n"
2445 " <anyxml name=\"anyx\"/>\n"
2446 " <description><text>desc</text></description>\n"
2447 " <grouping name=\"sub-grp\"/>\n"
2448 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2449 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2450 " <list name=\"list\"/>\n"
2451 " <notification name=\"notf\"/>\n"
2452 " <reference><text>ref</text></reference>\n"
2453 " <status value=\"current\"/>\n"
2454 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2455 " <uses name=\"uses-name\"/>\n"
2456 " <action name=\"act\"/>\n"
2457 " <container name=\"cont\"/>\n"
2458 " <choice name=\"choice\"/>\n"
2459 EXT_SUBELEM
2460 "</grouping>"
2461 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002462 assert_int_equal(test_element_helper(state, data, &grp_meta, NULL, NULL), LY_SUCCESS);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002463 assert_string_equal(grps->name, "grp-name");
Radek Krejci01180ac2021-01-27 08:48:22 +01002464 assert_string_equal(grps->child->name, "anyd");
2465 assert_string_equal(grps->child->next->name, "anyx");
2466 assert_string_equal(grps->child->next->next->name, "leaf");
2467 assert_string_equal(grps->child->next->next->next->name, "llist");
2468 assert_string_equal(grps->child->next->next->next->next->name, "list");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002469 assert_string_equal(grps->dsc, "desc");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002470 assert_true(grps->flags & LYS_STATUS_CURR);
2471 assert_string_equal(grps->groupings->name, "sub-grp");
2472 assert_int_equal(grps->nodetype, LYS_GROUPING);
2473 assert_string_equal(grps->notifs->name, "notf");
2474 assert_null(grps->parent);
2475 assert_string_equal(grps->ref, "ref");
2476 assert_string_equal(grps->typedefs->name, "tpdf");
David Sedlák85d0eca2019-07-24 15:15:21 +02002477 assert_string_equal(grps->actions->name, "act");
Radek Krejci01180ac2021-01-27 08:48:22 +01002478 assert_string_equal(grps->child->next->next->next->next->next->name, "uses-name");
2479 assert_int_equal(grps->child->next->next->next->next->next->nodetype, LYS_USES);
2480 assert_string_equal(grps->child->next->next->next->next->next->next->name, "cont");
2481 assert_int_equal(grps->child->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
2482 assert_string_equal(grps->child->next->next->next->next->next->next->next->name, "choice");
2483 assert_int_equal(grps->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002484 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(grps->exts[0]), LY_STMT_GROUPING);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002485 lysp_node_free(&fctx, &grps->node);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002486 grps = NULL;
2487
2488 /* min subelems */
2489 data = ELEMENT_WRAPPER_START "<grouping name=\"grp-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002490 assert_int_equal(test_element_helper(state, data, &grp_meta, NULL, NULL), LY_SUCCESS);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002491 assert_string_equal(grps->name, "grp-name");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002492 lysp_node_free(&fctx, &grps->node);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002493 grps = NULL;
David Sedlák031b9e72019-07-23 15:19:37 +02002494}
2495
David Sedlákf111bcb2019-07-23 17:15:51 +02002496static void
2497test_container_elem(void **state)
2498{
David Sedlákf111bcb2019-07-23 17:15:51 +02002499 const char *data;
2500 struct lysp_node *siblings = NULL;
2501 struct tree_node_meta node_meta = {NULL, &siblings};
2502 struct lysp_node_container *parsed = NULL;
2503
2504 /* max subelems */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002505 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedláke2dc9e92019-07-24 09:59:21 +02002506 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002507 "<container name=\"cont-name\">\n"
2508 " <anydata name=\"anyd\"/>\n"
2509 " <anyxml name=\"anyx\"/>\n"
2510 " <config value=\"true\"/>\n"
2511 " <container name=\"subcont\"/>\n"
2512 " <description><text>desc</text></description>\n"
2513 " <grouping name=\"sub-grp\"/>\n"
2514 " <if-feature name=\"iff\"/>\n"
2515 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2516 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2517 " <list name=\"list\"/>\n"
2518 " <must condition=\"cond\"/>\n"
2519 " <notification name=\"notf\"/>\n"
2520 " <presence value=\"presence\"/>\n"
2521 " <reference><text>ref</text></reference>\n"
2522 " <status value=\"current\"/>\n"
2523 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2524 " <uses name=\"uses-name\"/>\n"
2525 " <when condition=\"when-cond\"/>\n"
2526 " <action name=\"act\"/>\n"
2527 " <choice name=\"choice\"/>\n"
2528 EXT_SUBELEM
2529 "</container>"
2530 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002531 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedláke2dc9e92019-07-24 09:59:21 +02002532 parsed = (struct lysp_node_container *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002533 uint16_t flags = LYS_CONFIG_W | LYS_STATUS_CURR;
2534
2535 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2536 "cont-name", 0, LYS_CONTAINER, 0, "ref", 1);
2537 CHECK_LYSP_RESTR(parsed->musts, "cond", NULL, NULL, NULL, 0, NULL);
2538 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
2539
Michal Vasko7f45cf22020-10-01 12:49:44 +02002540 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedláke2dc9e92019-07-24 09:59:21 +02002541 assert_string_equal(parsed->presence, "presence");
2542 assert_string_equal(parsed->typedefs->name, "tpdf");
2543 assert_string_equal(parsed->groupings->name, "sub-grp");
2544 assert_string_equal(parsed->child->name, "anyd");
2545 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2546 assert_string_equal(parsed->child->next->name, "anyx");
2547 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2548 assert_string_equal(parsed->child->next->next->name, "subcont");
2549 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
2550 assert_string_equal(parsed->child->next->next->next->name, "leaf");
2551 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
2552 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
2553 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
2554 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
2555 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
2556 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
2557 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002558 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
2559 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2560 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke2dc9e92019-07-24 09:59:21 +02002561 assert_string_equal(parsed->notifs->name, "notf");
David Sedlák85d0eca2019-07-24 15:15:21 +02002562 assert_string_equal(parsed->actions->name, "act");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002563 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_CONTAINER);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002564 lysp_node_free(&fctx, siblings);
Radek Iša56ca9e42020-09-08 18:42:00 +02002565 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
David Sedláke2dc9e92019-07-24 09:59:21 +02002566 siblings = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02002567
2568 /* min subelems */
2569 data = ELEMENT_WRAPPER_START "<container name=\"cont-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002570 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákf111bcb2019-07-23 17:15:51 +02002571 parsed = (struct lysp_node_container *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002572 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2573 "cont-name", 0, LYS_CONTAINER, 0, NULL, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002574 lysp_node_free(&fctx, siblings);
David Sedlákf111bcb2019-07-23 17:15:51 +02002575 siblings = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02002576}
2577
David Sedlák5379d392019-07-24 10:42:03 +02002578static void
2579test_case_elem(void **state)
2580{
David Sedlák5379d392019-07-24 10:42:03 +02002581 const char *data;
2582 struct lysp_node *siblings = NULL;
2583 struct tree_node_meta node_meta = {NULL, &siblings};
2584 struct lysp_node_case *parsed = NULL;
2585
2586 /* max subelems */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002587 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlák5379d392019-07-24 10:42:03 +02002588 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002589 "<case name=\"case-name\">\n"
2590 " <anydata name=\"anyd\"/>\n"
2591 " <anyxml name=\"anyx\"/>\n"
2592 " <container name=\"subcont\"/>\n"
2593 " <description><text>desc</text></description>\n"
2594 " <if-feature name=\"iff\"/>\n"
2595 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2596 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2597 " <list name=\"list\"/>\n"
2598 " <reference><text>ref</text></reference>\n"
2599 " <status value=\"current\"/>\n"
2600 " <uses name=\"uses-name\"/>\n"
2601 " <when condition=\"when-cond\"/>\n"
2602 " <choice name=\"choice\"/>\n"
2603 EXT_SUBELEM
2604 "</case>"
2605 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002606 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák5379d392019-07-24 10:42:03 +02002607 parsed = (struct lysp_node_case *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002608 uint16_t flags = LYS_STATUS_CURR;
2609
2610 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2611 "case-name", 0, LYS_CASE, 0, "ref", 1);
2612 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002613 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlák5379d392019-07-24 10:42:03 +02002614 assert_string_equal(parsed->child->name, "anyd");
2615 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2616 assert_string_equal(parsed->child->next->name, "anyx");
2617 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2618 assert_string_equal(parsed->child->next->next->name, "subcont");
2619 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
2620 assert_string_equal(parsed->child->next->next->next->name, "leaf");
2621 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
2622 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
2623 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
2624 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
2625 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
2626 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
2627 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002628 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
2629 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2630 assert_null(parsed->child->next->next->next->next->next->next->next->next);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002631 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_CASE);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002632 lysp_node_free(&fctx, siblings);
David Sedlák5379d392019-07-24 10:42:03 +02002633 siblings = NULL;
2634
2635 /* min subelems */
2636 data = ELEMENT_WRAPPER_START "<case name=\"case-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002637 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák5379d392019-07-24 10:42:03 +02002638 parsed = (struct lysp_node_case *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002639 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2640 "case-name", 0, LYS_CASE, 0, NULL, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002641 lysp_node_free(&fctx, siblings);
David Sedlák5379d392019-07-24 10:42:03 +02002642 siblings = NULL;
David Sedlák5379d392019-07-24 10:42:03 +02002643}
2644
David Sedlákb7abcfa2019-07-24 12:33:35 +02002645static void
2646test_choice_elem(void **state)
2647{
David Sedlákb7abcfa2019-07-24 12:33:35 +02002648 const char *data;
2649 struct lysp_node *siblings = NULL;
2650 struct tree_node_meta node_meta = {NULL, &siblings};
2651 struct lysp_node_choice *parsed = NULL;
2652
2653 /* max subelems */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002654 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002655 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002656 "<choice name=\"choice-name\">\n"
2657 " <anydata name=\"anyd\"/>\n"
2658 " <anyxml name=\"anyx\"/>\n"
2659 " <case name=\"sub-case\"/>\n"
2660 " <choice name=\"choice\"/>\n"
2661 " <config value=\"true\"/>\n"
2662 " <container name=\"subcont\"/>\n"
2663 " <default value=\"def\"/>\n"
2664 " <description><text>desc</text></description>\n"
2665 " <if-feature name=\"iff\"/>\n"
2666 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2667 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2668 " <list name=\"list\"/>\n"
2669 " <mandatory value=\"true\" />\n"
2670 " <reference><text>ref</text></reference>\n"
2671 " <status value=\"current\"/>\n"
2672 " <when condition=\"when-cond\"/>\n"
2673 EXT_SUBELEM
2674 "</choice>"
2675 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002676 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002677 parsed = (struct lysp_node_choice *)siblings;
Radek Iša56ca9e42020-09-08 18:42:00 +02002678 uint16_t flags = LYS_CONFIG_W | LYS_MAND_TRUE | LYS_STATUS_CURR;
2679
2680 CHECK_LYSP_NODE(parsed, "desc", 1, flags, 1,
2681 "choice-name", 0, LYS_CHOICE, 0, "ref", 1);
2682 CHECK_LYSP_WHEN(parsed->when, "when-cond", NULL, 0, NULL);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002683 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlákb7abcfa2019-07-24 12:33:35 +02002684 assert_string_equal(parsed->child->name, "anyd");
2685 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2686 assert_string_equal(parsed->child->next->name, "anyx");
2687 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2688 assert_string_equal(parsed->child->next->next->name, "sub-case");
2689 assert_int_equal(parsed->child->next->next->nodetype, LYS_CASE);
2690 assert_string_equal(parsed->child->next->next->next->name, "choice");
2691 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
2692 assert_string_equal(parsed->child->next->next->next->next->name, "subcont");
2693 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_CONTAINER);
2694 assert_string_equal(parsed->child->next->next->next->next->next->name, "leaf");
2695 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAF);
2696 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "llist");
2697 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2698 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "list");
2699 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
2700 assert_null(parsed->child->next->next->next->next->next->next->next->next);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002701 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(parsed->exts[0]), LY_STMT_CHOICE);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002702 lysp_node_free(&fctx, siblings);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002703 siblings = NULL;
2704
2705 /* min subelems */
2706 data = ELEMENT_WRAPPER_START "<choice name=\"choice-name\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002707 assert_int_equal(test_element_helper(state, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002708 parsed = (struct lysp_node_choice *)siblings;
2709 assert_string_equal(parsed->name, "choice-name");
Radek Iša56ca9e42020-09-08 18:42:00 +02002710 CHECK_LYSP_NODE(parsed, NULL, 0, 0, 0,
2711 "choice-name", 0, LYS_CHOICE, 0, NULL, 0);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002712 lysp_node_free(&fctx, siblings);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002713 siblings = NULL;
David Sedlákb7abcfa2019-07-24 12:33:35 +02002714}
2715
David Sedlák05404f62019-07-24 14:11:53 +02002716static void
2717test_inout_elem(void **state)
2718{
David Sedlák05404f62019-07-24 14:11:53 +02002719 const char *data;
Michal Vasko2a6c53f2021-09-22 12:20:05 +02002720 struct lysp_node_action_inout inout = {0};
David Sedlák05404f62019-07-24 14:11:53 +02002721 struct inout_meta inout_meta = {NULL, &inout};
2722
2723 /* max subelements */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002724 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlák05404f62019-07-24 14:11:53 +02002725 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002726 "<input>\n"
2727 " <anydata name=\"anyd\"/>\n"
2728 " <anyxml name=\"anyx\"/>\n"
2729 " <choice name=\"choice\"/>\n"
2730 " <container name=\"subcont\"/>\n"
2731 " <grouping name=\"sub-grp\"/>\n"
2732 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2733 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2734 " <list name=\"list\"/>\n"
2735 " <must condition=\"cond\"/>\n"
2736 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2737 " <uses name=\"uses-name\"/>\n"
2738 EXT_SUBELEM
2739 "</input>"
2740 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002741 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
2742 CHECK_LYSP_ACTION_INOUT(&(inout), 1, 1, 1, 1, LYS_INPUT, 0, 1);
2743 CHECK_LYSP_RESTR(inout.musts, "cond", NULL, NULL, NULL, 0, NULL);
David Sedlák05404f62019-07-24 14:11:53 +02002744 assert_string_equal(inout.typedefs->name, "tpdf");
2745 assert_string_equal(inout.groupings->name, "sub-grp");
Radek Krejci01180ac2021-01-27 08:48:22 +01002746 assert_string_equal(inout.child->name, "anyd");
2747 assert_int_equal(inout.child->nodetype, LYS_ANYDATA);
2748 assert_string_equal(inout.child->next->name, "anyx");
2749 assert_int_equal(inout.child->next->nodetype, LYS_ANYXML);
2750 assert_string_equal(inout.child->next->next->name, "choice");
2751 assert_int_equal(inout.child->next->next->nodetype, LYS_CHOICE);
2752 assert_string_equal(inout.child->next->next->next->name, "subcont");
2753 assert_int_equal(inout.child->next->next->next->nodetype, LYS_CONTAINER);
2754 assert_string_equal(inout.child->next->next->next->next->name, "leaf");
2755 assert_int_equal(inout.child->next->next->next->next->nodetype, LYS_LEAF);
2756 assert_string_equal(inout.child->next->next->next->next->next->name, "llist");
2757 assert_int_equal(inout.child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2758 assert_string_equal(inout.child->next->next->next->next->next->next->name, "list");
2759 assert_int_equal(inout.child->next->next->next->next->next->next->nodetype, LYS_LIST);
2760 assert_string_equal(inout.child->next->next->next->next->next->next->next->name, "uses-name");
2761 assert_int_equal(inout.child->next->next->next->next->next->next->next->nodetype, LYS_USES);
2762 assert_null(inout.child->next->next->next->next->next->next->next->next);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002763 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(inout.exts[0]), LY_STMT_INPUT);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002764 lysp_node_free(&fctx, (struct lysp_node *)&inout);
David Sedlák05404f62019-07-24 14:11:53 +02002765 memset(&inout, 0, sizeof inout);
2766
2767 /* max subelements */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002768 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlák05404f62019-07-24 14:11:53 +02002769 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002770 "<output>\n"
2771 " <anydata name=\"anyd\"/>\n"
2772 " <anyxml name=\"anyx\"/>\n"
2773 " <choice name=\"choice\"/>\n"
2774 " <container name=\"subcont\"/>\n"
2775 " <grouping name=\"sub-grp\"/>\n"
2776 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2777 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2778 " <list name=\"list\"/>\n"
2779 " <must condition=\"cond\"/>\n"
2780 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2781 " <uses name=\"uses-name\"/>\n"
2782 EXT_SUBELEM
2783 "</output>"
2784 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002785 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
2786 CHECK_LYSP_ACTION_INOUT(&(inout), 1, 1, 1, 1, LYS_OUTPUT, 0, 1);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002787 assert_string_equal(inout.musts->arg.str, "cond");
David Sedlák05404f62019-07-24 14:11:53 +02002788 assert_string_equal(inout.typedefs->name, "tpdf");
2789 assert_string_equal(inout.groupings->name, "sub-grp");
Radek Krejci01180ac2021-01-27 08:48:22 +01002790 assert_string_equal(inout.child->name, "anyd");
2791 assert_int_equal(inout.child->nodetype, LYS_ANYDATA);
2792 assert_string_equal(inout.child->next->name, "anyx");
2793 assert_int_equal(inout.child->next->nodetype, LYS_ANYXML);
2794 assert_string_equal(inout.child->next->next->name, "choice");
2795 assert_int_equal(inout.child->next->next->nodetype, LYS_CHOICE);
2796 assert_string_equal(inout.child->next->next->next->name, "subcont");
2797 assert_int_equal(inout.child->next->next->next->nodetype, LYS_CONTAINER);
2798 assert_string_equal(inout.child->next->next->next->next->name, "leaf");
2799 assert_int_equal(inout.child->next->next->next->next->nodetype, LYS_LEAF);
2800 assert_string_equal(inout.child->next->next->next->next->next->name, "llist");
2801 assert_int_equal(inout.child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2802 assert_string_equal(inout.child->next->next->next->next->next->next->name, "list");
2803 assert_int_equal(inout.child->next->next->next->next->next->next->nodetype, LYS_LIST);
2804 assert_string_equal(inout.child->next->next->next->next->next->next->next->name, "uses-name");
2805 assert_int_equal(inout.child->next->next->next->next->next->next->next->nodetype, LYS_USES);
2806 assert_null(inout.child->next->next->next->next->next->next->next->next);
Radek Krejci39b7fc22021-02-26 23:29:18 +01002807 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(inout.exts[0]), LY_STMT_OUTPUT);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002808 lysp_node_free(&fctx, (struct lysp_node *)&inout);
David Sedlák05404f62019-07-24 14:11:53 +02002809 memset(&inout, 0, sizeof inout);
2810
2811 /* min subelems */
Michal Vaskob83af8a2020-01-06 09:49:22 +01002812 data = ELEMENT_WRAPPER_START "<input><leaf name=\"l\"><type name=\"empty\"/></leaf></input>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002813 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002814 lysp_node_free(&fctx, (struct lysp_node *)&inout);
David Sedlák05404f62019-07-24 14:11:53 +02002815 memset(&inout, 0, sizeof inout);
2816
Michal Vaskob83af8a2020-01-06 09:49:22 +01002817 data = ELEMENT_WRAPPER_START "<output><leaf name=\"l\"><type name=\"empty\"/></leaf></output>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002818 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_SUCCESS);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002819 lysp_node_free(&fctx, (struct lysp_node *)&inout);
David Sedlák05404f62019-07-24 14:11:53 +02002820 memset(&inout, 0, sizeof inout);
2821
2822 /* invalid combinations */
2823 data = ELEMENT_WRAPPER_START "<input name=\"test\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002824 assert_int_equal(test_element_helper(state, data, &inout_meta, NULL, NULL), LY_EVALID);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002825 lysp_node_free(&fctx, (struct lysp_node *)&inout);
Radek Iša56ca9e42020-09-08 18:42:00 +02002826 CHECK_LOG_CTX("Unexpected attribute \"name\" of \"input\" element.", "Line number 1.");
David Sedlák05404f62019-07-24 14:11:53 +02002827 memset(&inout, 0, sizeof inout);
David Sedlák05404f62019-07-24 14:11:53 +02002828}
2829
David Sedlák85d0eca2019-07-24 15:15:21 +02002830static void
2831test_action_elem(void **state)
2832{
David Sedlák85d0eca2019-07-24 15:15:21 +02002833 const char *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002834 struct lysp_node_action *actions = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002835 struct tree_node_meta act_meta = {NULL, (struct lysp_node **)&actions};
Radek Iša56ca9e42020-09-08 18:42:00 +02002836 uint16_t flags;
David Sedlák85d0eca2019-07-24 15:15:21 +02002837
2838 /* max subelems */
Michal Vasko8a67eff2021-12-07 14:04:47 +01002839 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlák85d0eca2019-07-24 15:15:21 +02002840 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002841 "<action name=\"act\">\n"
2842 " <description><text>desc</text></description>\n"
2843 " <grouping name=\"grouping\"/>\n"
2844 " <if-feature name=\"iff\"/>\n"
2845 " <input><uses name=\"uses-name\"/></input>\n"
2846 " <output><must condition=\"cond\"/><leaf name=\"l\"><type name=\"type\"/></leaf></output>\n"
2847 " <reference><text>ref</text></reference>\n"
2848 " <status value=\"deprecated\"/>\n"
2849 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2850 EXT_SUBELEM
2851 "</action>"
2852 ELEMENT_WRAPPER_END;
Michal Vasko1bf09392020-03-27 12:38:10 +01002853 /* there must be parent for action */
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002854 act_meta.parent = (void *)1;
Radek Iša56ca9e42020-09-08 18:42:00 +02002855 assert_int_equal(test_element_helper(state, data, &act_meta, NULL, NULL), LY_SUCCESS);
Michal Vasko1bf09392020-03-27 12:38:10 +01002856 act_meta.parent = NULL;
Radek Iša56ca9e42020-09-08 18:42:00 +02002857 flags = LYS_STATUS_DEPRC;
2858 CHECK_LYSP_ACTION(actions, "desc", 1, flags, 1, 1,\
2859 1, 0, 0, 0,\
2860 1, 0,\
2861 "act", LYS_ACTION, \
2862 1, 0, 0, 1,\
2863 1, 0,\
2864 1, "ref", 1);
2865
Michal Vasko7f45cf22020-10-01 12:49:44 +02002866 assert_string_equal(actions->iffeatures[0].str, "iff");
David Sedlák85d0eca2019-07-24 15:15:21 +02002867 assert_string_equal(actions->typedefs->name, "tpdf");
2868 assert_string_equal(actions->groupings->name, "grouping");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002869 assert_string_equal(actions->output.musts->arg.str, "cond");
Radek Krejci01180ac2021-01-27 08:48:22 +01002870 assert_string_equal(actions->input.child->name, "uses-name");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002871 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(actions->exts[0]), LY_STMT_ACTION);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002872 lysp_node_free(&fctx, (struct lysp_node *)actions);
David Sedlák85d0eca2019-07-24 15:15:21 +02002873 actions = NULL;
2874
Michal Vasko8a67eff2021-12-07 14:04:47 +01002875 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlákeaa45792019-07-24 15:25:01 +02002876 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002877 "<rpc name=\"act\">\n"
2878 " <description><text>desc</text></description>\n"
2879 " <grouping name=\"grouping\"/>\n"
2880 " <if-feature name=\"iff\"/>\n"
2881 " <input><uses name=\"uses-name\"/></input>\n"
2882 " <output><must condition=\"cond\"/><leaf name=\"l\"><type name=\"type\"/></leaf></output>\n"
2883 " <reference><text>ref</text></reference>\n"
2884 " <status value=\"deprecated\"/>\n"
2885 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
2886 EXT_SUBELEM
2887 "</rpc>"
2888 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002889 assert_int_equal(test_element_helper(state, data, &act_meta, NULL, NULL), LY_SUCCESS);
2890 flags = LYS_STATUS_DEPRC;
2891 CHECK_LYSP_ACTION(actions, "desc", 1, flags, 1, 1,\
2892 1, 0, 0, 0,\
2893 1, 0,\
2894 "act", LYS_RPC, \
2895 1, 0, 0, 1,\
2896 1, 0,\
2897 0, "ref", 1);
2898
Michal Vasko7f45cf22020-10-01 12:49:44 +02002899 assert_string_equal(actions->iffeatures[0].str, "iff");
David Sedlákeaa45792019-07-24 15:25:01 +02002900 assert_string_equal(actions->typedefs->name, "tpdf");
2901 assert_string_equal(actions->groupings->name, "grouping");
Radek Krejci01180ac2021-01-27 08:48:22 +01002902 assert_string_equal(actions->input.child->name, "uses-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002903 assert_string_equal(actions->output.musts->arg.str, "cond");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002904 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(actions->exts[0]), LY_STMT_RPC);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002905 lysp_node_free(&fctx, (struct lysp_node *)actions);
David Sedlákeaa45792019-07-24 15:25:01 +02002906 actions = NULL;
2907
David Sedlák85d0eca2019-07-24 15:15:21 +02002908 /* min subelems */
2909 data = ELEMENT_WRAPPER_START "<action name=\"act\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002910 assert_int_equal(test_element_helper(state, data, &act_meta, NULL, NULL), LY_SUCCESS);
David Sedlák85d0eca2019-07-24 15:15:21 +02002911 assert_string_equal(actions->name, "act");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002912 lysp_node_free(&fctx, (struct lysp_node *)actions);
David Sedlák85d0eca2019-07-24 15:15:21 +02002913 actions = NULL;
David Sedlák85d0eca2019-07-24 15:15:21 +02002914}
2915
David Sedlák992fb7c2019-07-24 16:51:01 +02002916static void
2917test_augment_elem(void **state)
2918{
David Sedlák992fb7c2019-07-24 16:51:01 +02002919 const char *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002920 struct lysp_node_augment *augments = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002921 struct tree_node_meta aug_meta = {NULL, (struct lysp_node **)&augments};
David Sedlák992fb7c2019-07-24 16:51:01 +02002922
Michal Vasko8a67eff2021-12-07 14:04:47 +01002923 PARSER_CUR_PMOD(YCTX)->version = LYS_VERSION_1_1;
David Sedlák992fb7c2019-07-24 16:51:01 +02002924 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01002925 "<augment target-node=\"target\">\n"
2926 " <action name=\"action\"/>\n"
2927 " <anydata name=\"anyd\"/>\n"
2928 " <anyxml name=\"anyx\"/>\n"
2929 " <case name=\"case\"/>\n"
2930 " <choice name=\"choice\"/>\n"
2931 " <container name=\"subcont\"/>\n"
2932 " <description><text>desc</text></description>\n"
2933 " <if-feature name=\"iff\"/>\n"
2934 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
2935 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
2936 " <list name=\"list\"/>\n"
2937 " <notification name=\"notif\"/>\n"
2938 " <reference><text>ref</text></reference>\n"
2939 " <status value=\"current\"/>\n"
2940 " <uses name=\"uses\"/>\n"
2941 " <when condition=\"when-cond\"/>\n"
2942 EXT_SUBELEM
2943 "</augment>"
2944 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002945 assert_int_equal(test_element_helper(state, data, &aug_meta, NULL, NULL), LY_SUCCESS);
David Sedlák992fb7c2019-07-24 16:51:01 +02002946 assert_string_equal(augments->nodeid, "target");
2947 assert_null(augments->parent);
2948 assert_int_equal(augments->nodetype, LYS_AUGMENT);
2949 assert_true(augments->flags & LYS_STATUS_CURR);
2950 assert_string_equal(augments->dsc, "desc");
2951 assert_string_equal(augments->ref, "ref");
2952 assert_string_equal(augments->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002953 assert_string_equal(augments->iffeatures[0].str, "iff");
David Sedlák992fb7c2019-07-24 16:51:01 +02002954 assert_string_equal(augments->child->name, "anyd");
2955 assert_int_equal(augments->child->nodetype, LYS_ANYDATA);
2956 assert_string_equal(augments->child->next->name, "anyx");
2957 assert_int_equal(augments->child->next->nodetype, LYS_ANYXML);
2958 assert_string_equal(augments->child->next->next->name, "case");
2959 assert_int_equal(augments->child->next->next->nodetype, LYS_CASE);
2960 assert_string_equal(augments->child->next->next->next->name, "choice");
2961 assert_int_equal(augments->child->next->next->next->nodetype, LYS_CHOICE);
2962 assert_string_equal(augments->child->next->next->next->next->name, "subcont");
2963 assert_int_equal(augments->child->next->next->next->next->nodetype, LYS_CONTAINER);
2964 assert_string_equal(augments->child->next->next->next->next->next->name, "leaf");
2965 assert_int_equal(augments->child->next->next->next->next->next->nodetype, LYS_LEAF);
2966 assert_string_equal(augments->child->next->next->next->next->next->next->name, "llist");
2967 assert_int_equal(augments->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2968 assert_string_equal(augments->child->next->next->next->next->next->next->next->name, "list");
2969 assert_int_equal(augments->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
2970 assert_string_equal(augments->child->next->next->next->next->next->next->next->next->name, "uses");
2971 assert_int_equal(augments->child->next->next->next->next->next->next->next->next->nodetype, LYS_USES);
2972 assert_null(augments->child->next->next->next->next->next->next->next->next->next);
2973 assert_string_equal(augments->actions->name, "action");
2974 assert_string_equal(augments->notifs->name, "notif");
Radek Krejci39b7fc22021-02-26 23:29:18 +01002975 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(augments->exts[0]), LY_STMT_AUGMENT);
Michal Vaskoc636ea42022-09-16 10:20:31 +02002976 lysp_node_free(&fctx, (struct lysp_node *)augments);
David Sedlák992fb7c2019-07-24 16:51:01 +02002977 augments = NULL;
2978
2979 data = ELEMENT_WRAPPER_START "<augment target-node=\"target\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002980 assert_int_equal(test_element_helper(state, data, &aug_meta, NULL, NULL), LY_SUCCESS);
David Sedlák992fb7c2019-07-24 16:51:01 +02002981 assert_string_equal(augments->nodeid, "target");
Michal Vaskoc636ea42022-09-16 10:20:31 +02002982 lysp_node_free(&fctx, (struct lysp_node *)augments);
David Sedlák992fb7c2019-07-24 16:51:01 +02002983 augments = NULL;
David Sedlák992fb7c2019-07-24 16:51:01 +02002984}
2985
David Sedlák4ffcec82019-07-25 15:10:21 +02002986static void
2987test_deviate_elem(void **state)
2988{
David Sedlák4ffcec82019-07-25 15:10:21 +02002989 const char *data;
2990 struct lysp_deviate *deviates = NULL;
David Sedlák4ffcec82019-07-25 15:10:21 +02002991
2992 /* invalid arguments */
2993 data = ELEMENT_WRAPPER_START "<deviate value=\"\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02002994 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
2995 CHECK_LOG_CTX("Invalid value \"\" of \"value\" attribute in \"deviate\" element. "
2996 "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", "Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02002997 deviates = NULL;
2998
2999 data = ELEMENT_WRAPPER_START "<deviate value=\"invalid\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003000 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
3001 CHECK_LOG_CTX("Invalid value \"invalid\" of \"value\" attribute in \"deviate\" element. "
3002 "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", "Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003003 deviates = NULL;
3004
3005 data = ELEMENT_WRAPPER_START "<deviate value=\"ad\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003006 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
3007 CHECK_LOG_CTX("Invalid value \"ad\" of \"value\" attribute in \"deviate\" element. "
3008 "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", "Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003009 deviates = NULL;
3010
3011 data = ELEMENT_WRAPPER_START "<deviate value=\"adds\" />" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003012 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
3013 CHECK_LOG_CTX("Invalid value \"adds\" of \"value\" attribute in \"deviate\" element. "
3014 "Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\".", "Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003015 deviates = NULL;
3016
3017 data = ELEMENT_WRAPPER_START
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003018 "<deviate value=\"not-supported\">\n"
3019 " <must condition=\"c\"/>\n"
3020 "</deviate>"
3021 ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003022 assert_int_equal(test_element_helper(state, data, &deviates, NULL, NULL), LY_EVALID);
3023 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 +02003024}
3025
David Sedlák8b754462019-07-25 16:22:13 +02003026static void
3027test_deviation_elem(void **state)
3028{
David Sedlák8b754462019-07-25 16:22:13 +02003029 const char *data;
3030 struct lysp_deviation *deviations = NULL;
3031
David Sedlák8b754462019-07-25 16:22:13 +02003032 /* invalid */
3033 data = ELEMENT_WRAPPER_START "<deviation target-node=\"target\"/>" ELEMENT_WRAPPER_END;
Radek Iša56ca9e42020-09-08 18:42:00 +02003034 assert_int_equal(test_element_helper(state, data, &deviations, NULL, NULL), LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +02003035 CHECK_LOG_CTX("Missing mandatory sub-element \"deviate\" of \"deviation\" element.", "Line number 1.");
Michal Vasko12ef5362022-09-16 15:13:58 +02003036}
David Sedlák8b754462019-07-25 16:22:13 +02003037
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003038static struct lysp_module *
Michal Vaskod0625d72022-10-06 15:02:50 +02003039mod_renew(struct lysp_yin_ctx *ctx)
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003040{
Michal Vasko8a67eff2021-12-07 14:04:47 +01003041 struct ly_ctx *ly_ctx = PARSER_CUR_PMOD(ctx)->mod->ctx;
3042 struct lysp_module *pmod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003043
Michal Vaskoc636ea42022-09-16 10:20:31 +02003044 lys_module_free(&fctx, PARSER_CUR_PMOD(ctx)->mod, 0);
Michal Vasko8a67eff2021-12-07 14:04:47 +01003045 pmod = calloc(1, sizeof *pmod);
3046 ctx->parsed_mods->objs[0] = pmod;
3047 pmod->mod = calloc(1, sizeof *pmod->mod);
3048 pmod->mod->parsed = pmod;
3049 pmod->mod->ctx = ly_ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003050
Michal Vaskoc636ea42022-09-16 10:20:31 +02003051 fctx.mod = pmod->mod;
3052
Michal Vasko8a67eff2021-12-07 14:04:47 +01003053 return pmod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003054}
3055
David Sedlák4f03b932019-07-26 13:01:47 +02003056static void
3057test_module_elem(void **state)
3058{
Michal Vaskob36053d2020-03-26 15:49:30 +01003059 const char *data;
Radek Iša56ca9e42020-09-08 18:42:00 +02003060 struct lysp_module *lysp_mod = mod_renew(YCTX);
David Sedlák4f03b932019-07-26 13:01:47 +02003061
3062 /* max subelems */
David Sedlák4f03b932019-07-26 13:01:47 +02003063 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003064 " <yang-version value=\"1.1\"/>\n"
3065 " <namespace uri=\"ns\"/>\n"
3066 " <prefix value=\"pref\"/>\n"
3067 " <include module=\"b-mod\"/>\n"
3068 " <import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
3069 " <organization><text>org</text></organization>\n"
3070 " <contact><text>contact</text></contact>\n"
3071 " <description><text>desc</text></description>\n"
3072 " <reference><text>ref</text></reference>\n"
3073 " <revision date=\"2019-02-02\"/>\n"
3074 " <anydata name=\"anyd\"/>\n"
3075 " <anyxml name=\"anyx\"/>\n"
3076 " <choice name=\"choice\"/>\n"
3077 " <container name=\"cont\"/>\n"
3078 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3079 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3080 " <list name=\"sub-list\"/>\n"
3081 " <uses name=\"uses-name\"/>\n"
3082 " <augment target-node=\"target\"/>\n"
3083 " <deviation target-node=\"target\">\n"
3084 " <deviate value=\"not-supported\"/>\n"
3085 " </deviation>\n"
3086 " <extension name=\"ext\"/>\n"
3087 " <feature name=\"feature\"/>\n"
3088 " <grouping name=\"grp\"/>\n"
3089 " <identity name=\"ident-name\"/>\n"
3090 " <notification name=\"notf\"/>\n"
3091 " <rpc name=\"rpc-name\"/>\n"
3092 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3093 EXT_SUBELEM "\n"
3094 "</module>\n";
Radek Iša56ca9e42020-09-08 18:42:00 +02003095 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3096 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01003097
Radek Iša56ca9e42020-09-08 18:42:00 +02003098 assert_int_equal(yin_parse_mod(YCTX, lysp_mod), LY_SUCCESS);
David Sedlák4f03b932019-07-26 13:01:47 +02003099 assert_string_equal(lysp_mod->mod->name, "mod");
3100 assert_string_equal(lysp_mod->revs, "2019-02-02");
3101 assert_string_equal(lysp_mod->mod->ns, "ns");
3102 assert_string_equal(lysp_mod->mod->prefix, "pref");
3103 assert_null(lysp_mod->mod->filepath);
3104 assert_string_equal(lysp_mod->mod->org, "org");
3105 assert_string_equal(lysp_mod->mod->contact, "contact");
3106 assert_string_equal(lysp_mod->mod->dsc, "desc");
3107 assert_string_equal(lysp_mod->mod->ref, "ref");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003108 assert_int_equal(lysp_mod->version, LYS_VERSION_1_1);
Radek Iša56ca9e42020-09-08 18:42:00 +02003109 CHECK_LYSP_IMPORT(lysp_mod->imports, NULL, 0, "a-mod",
3110 "imp-pref", NULL, "");
David Sedlák4f03b932019-07-26 13:01:47 +02003111 assert_string_equal(lysp_mod->includes->name, "b-mod");
3112 assert_string_equal(lysp_mod->extensions->name, "ext");
3113 assert_string_equal(lysp_mod->features->name, "feature");
3114 assert_string_equal(lysp_mod->identities->name, "ident-name");
3115 assert_string_equal(lysp_mod->typedefs->name, "tpdf");
3116 assert_string_equal(lysp_mod->groupings->name, "grp");
3117 assert_string_equal(lysp_mod->data->name, "anyd");
3118 assert_int_equal(lysp_mod->data->nodetype, LYS_ANYDATA);
3119 assert_string_equal(lysp_mod->data->next->name, "anyx");
3120 assert_int_equal(lysp_mod->data->next->nodetype, LYS_ANYXML);
3121 assert_string_equal(lysp_mod->data->next->next->name, "choice");
3122 assert_int_equal(lysp_mod->data->next->next->nodetype, LYS_CHOICE);
3123 assert_string_equal(lysp_mod->data->next->next->next->name, "cont");
3124 assert_int_equal(lysp_mod->data->next->next->next->nodetype, LYS_CONTAINER);
3125 assert_string_equal(lysp_mod->data->next->next->next->next->name, "leaf");
3126 assert_int_equal(lysp_mod->data->next->next->next->next->nodetype, LYS_LEAF);
3127 assert_string_equal(lysp_mod->data->next->next->next->next->next->name, "llist");
3128 assert_int_equal(lysp_mod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3129 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->name, "sub-list");
3130 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
3131 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->next->name, "uses-name");
3132 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3133 assert_null(lysp_mod->data->next->next->next->next->next->next->next->next);
3134 assert_string_equal(lysp_mod->augments->nodeid, "target");
3135 assert_string_equal(lysp_mod->rpcs->name, "rpc-name");
3136 assert_string_equal(lysp_mod->notifs->name, "notf");
3137 assert_string_equal(lysp_mod->deviations->nodeid, "target");
Radek Krejci39b7fc22021-02-26 23:29:18 +01003138 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(lysp_mod->exts[0]), LY_STMT_MODULE);
David Sedlák4f03b932019-07-26 13:01:47 +02003139
3140 /* min subelems */
Radek Iša56ca9e42020-09-08 18:42:00 +02003141 ly_in_free(UTEST_IN, 0);
3142 lyxml_ctx_free(YCTX->xmlctx);
3143 lysp_mod = mod_renew(YCTX);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003144 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
3145 " <namespace uri=\"ns\"/>\n"
3146 " <prefix value=\"pref\"/>\n"
3147 " <yang-version value=\"1.1\"/>\n"
3148 "</module>";
Radek Iša56ca9e42020-09-08 18:42:00 +02003149 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3150 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
3151 assert_int_equal(yin_parse_mod(YCTX, lysp_mod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003152 assert_string_equal(lysp_mod->mod->name, "mod");
David Sedlák298ff6d2019-07-26 14:29:03 +02003153
David Sedláke6cd89e2019-08-07 12:46:02 +02003154 /* incorrect subelem order */
Radek Iša56ca9e42020-09-08 18:42:00 +02003155 ly_in_free(UTEST_IN, 0);
3156 lyxml_ctx_free(YCTX->xmlctx);
3157 lysp_mod = mod_renew(YCTX);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003158 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
3159 " <feature name=\"feature\"/>\n"
3160 " <namespace uri=\"ns\"/>\n"
3161 " <prefix value=\"pref\"/>\n"
3162 " <yang-version value=\"1.1\"/>\n"
3163 "</module>";
Radek Iša56ca9e42020-09-08 18:42:00 +02003164 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3165 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
3166 assert_int_equal(yin_parse_mod(YCTX, lysp_mod), LY_EVALID);
3167 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 +02003168}
3169
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003170static struct lysp_submodule *
Michal Vaskod0625d72022-10-06 15:02:50 +02003171submod_renew(struct lysp_yin_ctx *ctx, const char *belongs_to)
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003172{
Michal Vasko8a67eff2021-12-07 14:04:47 +01003173 struct ly_ctx *ly_ctx = PARSER_CUR_PMOD(ctx)->mod->ctx;
3174 struct lysp_submodule *submod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003175
Michal Vaskoc636ea42022-09-16 10:20:31 +02003176 lys_module_free(&fctx, PARSER_CUR_PMOD(ctx)->mod, 0);
Michal Vasko8a67eff2021-12-07 14:04:47 +01003177 submod = calloc(1, sizeof *submod);
3178 ctx->parsed_mods->objs[0] = submod;
3179 submod->mod = calloc(1, sizeof *submod->mod);
3180 lydict_insert(ly_ctx, belongs_to, 0, &submod->mod->name);
3181 submod->mod->parsed = (struct lysp_module *)submod;
3182 submod->mod->ctx = ly_ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003183
Michal Vaskoc636ea42022-09-16 10:20:31 +02003184 fctx.mod = submod->mod;
3185
Michal Vasko8a67eff2021-12-07 14:04:47 +01003186 return submod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003187}
3188
David Sedlák298ff6d2019-07-26 14:29:03 +02003189static void
3190test_submodule_elem(void **state)
3191{
Michal Vaskob36053d2020-03-26 15:49:30 +01003192 const char *data;
Radek Iša56ca9e42020-09-08 18:42:00 +02003193 struct lysp_submodule *lysp_submod = submod_renew(YCTX, "module-name");
David Sedlák298ff6d2019-07-26 14:29:03 +02003194
3195 /* max subelements */
David Sedlák298ff6d2019-07-26 14:29:03 +02003196 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003197 " <yang-version value=\"1.1\"/>\n"
3198 " <belongs-to module=\"module-name\">\n"
3199 " <prefix value=\"pref\"/>\n"
3200 " </belongs-to>\n"
3201 " <include module=\"b-mod\"/>\n"
3202 " <import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
3203 " <organization><text>org</text></organization>\n"
3204 " <contact><text>contact</text></contact>\n"
3205 " <description><text>desc</text></description>\n"
3206 " <reference><text>ref</text></reference>\n"
3207 " <revision date=\"2019-02-02\"/>\n"
3208 " <anydata name=\"anyd\"/>\n"
3209 " <anyxml name=\"anyx\"/>\n"
3210 " <choice name=\"choice\"/>\n"
3211 " <container name=\"cont\"/>\n"
3212 " <leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3213 " <leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3214 " <list name=\"sub-list\"/>\n"
3215 " <uses name=\"uses-name\"/>\n"
3216 " <augment target-node=\"target\"/>\n"
3217 " <deviation target-node=\"target\">\n"
3218 " <deviate value=\"not-supported\"/>\n"
3219 " </deviation>\n"
3220 " <extension name=\"ext\"/>\n"
3221 " <feature name=\"feature\"/>\n"
3222 " <grouping name=\"grp\"/>\n"
3223 " <identity name=\"ident-name\"/>\n"
3224 " <notification name=\"notf\"/>\n"
3225 " <rpc name=\"rpc-name\"/>\n"
3226 " <typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3227 EXT_SUBELEM "\n"
3228 "</submodule>\n";
Radek Iša56ca9e42020-09-08 18:42:00 +02003229 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3230 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003231
Radek Iša56ca9e42020-09-08 18:42:00 +02003232 assert_int_equal(yin_parse_submod(YCTX, lysp_submod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003233 assert_string_equal(lysp_submod->name, "mod");
3234 assert_string_equal(lysp_submod->revs, "2019-02-02");
David Sedlák298ff6d2019-07-26 14:29:03 +02003235 assert_string_equal(lysp_submod->prefix, "pref");
3236 assert_null(lysp_submod->filepath);
3237 assert_string_equal(lysp_submod->org, "org");
3238 assert_string_equal(lysp_submod->contact, "contact");
3239 assert_string_equal(lysp_submod->dsc, "desc");
3240 assert_string_equal(lysp_submod->ref, "ref");
3241 assert_int_equal(lysp_submod->version, LYS_VERSION_1_1);
Radek Iša56ca9e42020-09-08 18:42:00 +02003242 CHECK_LYSP_IMPORT(lysp_submod->imports, NULL, 0, "a-mod",
3243 "imp-pref", NULL, "");
David Sedlák298ff6d2019-07-26 14:29:03 +02003244 assert_string_equal(lysp_submod->includes->name, "b-mod");
3245 assert_string_equal(lysp_submod->extensions->name, "ext");
3246 assert_string_equal(lysp_submod->features->name, "feature");
3247 assert_string_equal(lysp_submod->identities->name, "ident-name");
3248 assert_string_equal(lysp_submod->typedefs->name, "tpdf");
3249 assert_string_equal(lysp_submod->groupings->name, "grp");
3250 assert_string_equal(lysp_submod->data->name, "anyd");
3251 assert_int_equal(lysp_submod->data->nodetype, LYS_ANYDATA);
3252 assert_string_equal(lysp_submod->data->next->name, "anyx");
3253 assert_int_equal(lysp_submod->data->next->nodetype, LYS_ANYXML);
3254 assert_string_equal(lysp_submod->data->next->next->name, "choice");
3255 assert_int_equal(lysp_submod->data->next->next->nodetype, LYS_CHOICE);
3256 assert_string_equal(lysp_submod->data->next->next->next->name, "cont");
3257 assert_int_equal(lysp_submod->data->next->next->next->nodetype, LYS_CONTAINER);
3258 assert_string_equal(lysp_submod->data->next->next->next->next->name, "leaf");
3259 assert_int_equal(lysp_submod->data->next->next->next->next->nodetype, LYS_LEAF);
3260 assert_string_equal(lysp_submod->data->next->next->next->next->next->name, "llist");
3261 assert_int_equal(lysp_submod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3262 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->name, "sub-list");
3263 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
3264 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->next->name, "uses-name");
3265 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3266 assert_null(lysp_submod->data->next->next->next->next->next->next->next->next);
3267 assert_string_equal(lysp_submod->augments->nodeid, "target");
3268 assert_string_equal(lysp_submod->rpcs->name, "rpc-name");
3269 assert_string_equal(lysp_submod->notifs->name, "notf");
3270 assert_string_equal(lysp_submod->deviations->nodeid, "target");
Radek Krejci39b7fc22021-02-26 23:29:18 +01003271 TEST_1_CHECK_LYSP_EXT_INSTANCE(&(lysp_submod->exts[0]), LY_STMT_SUBMODULE);
David Sedlák298ff6d2019-07-26 14:29:03 +02003272
David Sedlák298ff6d2019-07-26 14:29:03 +02003273 /* min subelemnts */
Radek Iša56ca9e42020-09-08 18:42:00 +02003274 ly_in_free(UTEST_IN, 0);
3275 lyxml_ctx_free(YCTX->xmlctx);
3276 lysp_submod = submod_renew(YCTX, "module-name");
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003277 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">\n"
3278 " <yang-version value=\"1\"/>\n"
3279 " <belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>\n"
3280 "</submodule>";
Radek Iša56ca9e42020-09-08 18:42:00 +02003281 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3282 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
3283 assert_int_equal(yin_parse_submod(YCTX, lysp_submod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003284 assert_string_equal(lysp_submod->prefix, "pref");
David Sedlák298ff6d2019-07-26 14:29:03 +02003285 assert_int_equal(lysp_submod->version, LYS_VERSION_1_0);
David Sedlák298ff6d2019-07-26 14:29:03 +02003286
David Sedláke6cd89e2019-08-07 12:46:02 +02003287 /* incorrect subelem order */
Radek Iša56ca9e42020-09-08 18:42:00 +02003288 ly_in_free(UTEST_IN, 0);
3289 lyxml_ctx_free(YCTX->xmlctx);
3290 lysp_submod = submod_renew(YCTX, "module-name");
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003291 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">\n"
3292 " <yang-version value=\"1\"/>\n"
3293 " <reference><text>ref</text></reference>\n"
3294 " <belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>\n"
3295 "</submodule>";
Radek Iša56ca9e42020-09-08 18:42:00 +02003296 assert_int_equal(ly_in_new_memory(data, &UTEST_IN), LY_SUCCESS);
3297 assert_int_equal(lyxml_ctx_new(UTEST_LYCTX, UTEST_IN, &YCTX->xmlctx), LY_SUCCESS);
3298 assert_int_equal(yin_parse_submod(YCTX, lysp_submod), LY_EVALID);
3299 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 +02003300}
3301
David Sedlák8985a142019-07-31 16:43:06 +02003302static void
3303test_yin_parse_module(void **state)
3304{
David Sedlák8985a142019-07-31 16:43:06 +02003305 const char *data;
3306 struct lys_module *mod;
Michal Vaskod0625d72022-10-06 15:02:50 +02003307 struct lysp_yin_ctx *yin_ctx = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02003308 struct ly_in *in = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003309
3310 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003311 mod->ctx = UTEST_LYCTX;
David Sedlákd2844882019-09-13 16:01:22 +02003312 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 +01003313 " <yang-version value=\"1.1\"/>\n"
3314 " <namespace uri=\"urn:tests:extensions:metadata:a\"/>\n"
3315 " <prefix value=\"a\"/>\n"
3316 " <import module=\"ietf-yang-metadata\">\n"
3317 " <prefix value=\"md\"/>\n"
3318 " </import>\n"
3319 " <feature name=\"f\"/>\n"
3320 " <md:annotation name=\"x\">\n"
3321 " <description>\n"
3322 " <text>test</text>\n"
3323 " </description>\n"
3324 " <reference>\n"
3325 " <text>test</text>\n"
3326 " </reference>\n"
3327 " <if-feature name=\"f\"/>\n"
3328 " <status value=\"current\"/>\n"
3329 " <type name=\"uint8\"/>\n"
3330 " <units name=\"meters\"/>\n"
3331 " </md:annotation>\n"
3332 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003333 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
aPiecekc3e26142021-06-22 14:25:49 +02003334 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS);
David Sedlákd2844882019-09-13 16:01:22 +02003335 assert_null(mod->parsed->exts->child->next->child);
3336 assert_string_equal(mod->parsed->exts->child->next->arg, "test");
Michal Vaskoc636ea42022-09-16 10:20:31 +02003337 lys_module_free(&fctx, mod, 0);
Michal Vaskod0625d72022-10-06 15:02:50 +02003338 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003339 ly_in_free(in, 0);
David Sedlákd2844882019-09-13 16:01:22 +02003340 mod = NULL;
3341 yin_ctx = NULL;
3342
3343 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003344 mod->ctx = UTEST_LYCTX;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003345 data = "<module name=\"example-foo\""
3346 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
3347 " xmlns:foo=\"urn:example:foo\""
3348 " xmlns:myext=\"urn:example:extensions\">\n"
David Sedlák8985a142019-07-31 16:43:06 +02003349
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003350 " <yang-version value=\"1\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003351
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003352 " <namespace uri=\"urn:example:foo\"/>\n"
3353 " <prefix value=\"foo\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003354
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003355 " <import module=\"example-extensions\">\n"
3356 " <prefix value=\"myext\"/>\n"
3357 " </import>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003358
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003359 " <list name=\"interface\">\n"
3360 " <key value=\"name\"/>\n"
3361 " <leaf name=\"name\">\n"
3362 " <type name=\"string\"/>\n"
3363 " </leaf>\n"
3364 " <leaf name=\"mtu\">\n"
3365 " <type name=\"uint32\"/>\n"
3366 " <description>\n"
3367 " <text>The MTU of the interface.</text>\n"
3368 " </description>\n"
3369 " <myext:c-define name=\"MY_MTU\"/>\n"
3370 " </leaf>\n"
3371 " </list>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003372 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003373 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
aPiecekc3e26142021-06-22 14:25:49 +02003374 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS);
Michal Vaskoc636ea42022-09-16 10:20:31 +02003375 lys_module_free(&fctx, mod, 0);
Michal Vaskod0625d72022-10-06 15:02:50 +02003376 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003377 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02003378 mod = NULL;
3379 yin_ctx = NULL;
3380
3381 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003382 mod->ctx = UTEST_LYCTX;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003383 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3384 " <yang-version value=\"1\"/>\n"
3385 " <namespace uri=\"urn:example:foo\"/>\n"
3386 " <prefix value=\"foo\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003387 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003388 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
aPiecekc3e26142021-06-22 14:25:49 +02003389 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS);
Michal Vaskoc636ea42022-09-16 10:20:31 +02003390 lys_module_free(&fctx, mod, 0);
Michal Vaskod0625d72022-10-06 15:02:50 +02003391 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003392 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02003393 mod = NULL;
3394 yin_ctx = NULL;
3395
David Sedlák6d781b62019-08-02 15:22:52 +02003396 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003397 mod->ctx = UTEST_LYCTX;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003398 data = "<submodule name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlák8985a142019-07-31 16:43:06 +02003399 "</submodule>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003400 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
aPiecekc3e26142021-06-22 14:25:49 +02003401 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_EINVAL);
Radek Iša56ca9e42020-09-08 18:42:00 +02003402 CHECK_LOG_CTX("Input data contains submodule which cannot be parsed directly without its main module.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02003403 lys_module_free(&fctx, mod, 0);
Michal Vaskod0625d72022-10-06 15:02:50 +02003404 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003405 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02003406
David Sedlák6d781b62019-08-02 15:22:52 +02003407 mod = calloc(1, sizeof *mod);
Radek Iša56ca9e42020-09-08 18:42:00 +02003408 mod->ctx = UTEST_LYCTX;
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003409 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3410 " <yang-version value=\"1\"/>\n"
3411 " <namespace uri=\"urn:example:foo\"/>\n"
3412 " <prefix value=\"foo\"/>\n"
3413 "</module>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003414 "<module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003415 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
aPiecekc3e26142021-06-22 14:25:49 +02003416 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +02003417 CHECK_LOG_CTX("Trailing garbage \"<module>\" after module, expected end-of-input.", "Line number 6.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02003418 lys_module_free(&fctx, mod, 0);
Michal Vaskod0625d72022-10-06 15:02:50 +02003419 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003420 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02003421 mod = NULL;
3422 yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003423}
3424
3425static void
3426test_yin_parse_submodule(void **state)
3427{
David Sedlák8985a142019-07-31 16:43:06 +02003428 const char *data;
Michal Vaskod0625d72022-10-06 15:02:50 +02003429 struct lysp_yin_ctx *yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003430 struct lysp_submodule *submod = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02003431 struct ly_in *in;
David Sedlák8985a142019-07-31 16:43:06 +02003432
Michal Vasko8a67eff2021-12-07 14:04:47 +01003433 lydict_insert(UTEST_LYCTX, "a", 0, &PARSER_CUR_PMOD(YCTX)->mod->name);
Michal Vaskoc3781c32020-10-06 14:04:08 +02003434
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003435 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003436 "<submodule name=\"asub\""
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003437 " xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
3438 " xmlns:a=\"urn:a\">\n"
3439 " <yang-version value=\"1\"/>\n"
3440 " <belongs-to module=\"a\">\n"
3441 " <prefix value=\"a_pref\"/>\n"
3442 " </belongs-to>\n"
3443 " <include module=\"atop\"/>\n"
3444 " <feature name=\"fox\"/>\n"
3445 " <notification name=\"bar-notif\">\n"
3446 " <if-feature name=\"bar\"/>\n"
3447 " </notification>\n"
3448 " <notification name=\"fox-notif\">\n"
3449 " <if-feature name=\"fox\"/>\n"
3450 " </notification>\n"
3451 " <augment target-node=\"/a_pref:top\">\n"
3452 " <if-feature name=\"bar\"/>\n"
3453 " <container name=\"bar-sub\"/>\n"
3454 " </augment>\n"
3455 " <augment target-node=\"/top\">\n"
3456 " <container name=\"bar-sub2\"/>\n"
3457 " </augment>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003458 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003459 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vaskod0625d72022-10-06 15:02:50 +02003460 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 +02003461 lysp_module_free(&fctx, (struct lysp_module *)submod);
Michal Vaskod0625d72022-10-06 15:02:50 +02003462 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003463 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02003464 yin_ctx = NULL;
3465 submod = NULL;
3466
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003467 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3468 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3469 " <yang-version value=\"1\"/>\n"
3470 " <belongs-to module=\"a\">\n"
3471 " <prefix value=\"a_pref\"/>\n"
3472 " </belongs-to>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003473 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003474 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vaskod0625d72022-10-06 15:02:50 +02003475 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 +02003476 lysp_module_free(&fctx, (struct lysp_module *)submod);
Michal Vaskod0625d72022-10-06 15:02:50 +02003477 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003478 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02003479 yin_ctx = NULL;
3480 submod = NULL;
3481
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003482 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
David Sedlák8985a142019-07-31 16:43:06 +02003483 "<module name=\"inval\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
3484 "</module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003485 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vaskod0625d72022-10-06 15:02:50 +02003486 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 +02003487 CHECK_LOG_CTX("Input data contains module when a submodule is expected.", NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02003488 lysp_module_free(&fctx, (struct lysp_module *)submod);
Michal Vaskod0625d72022-10-06 15:02:50 +02003489 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003490 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02003491 yin_ctx = NULL;
3492 submod = NULL;
3493
Radek Krejcib4ac5a92020-11-23 17:54:33 +01003494 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3495 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3496 " <yang-version value=\"1\"/>\n"
3497 " <belongs-to module=\"a\">\n"
3498 " <prefix value=\"a_pref\"/>\n"
3499 " </belongs-to>\n"
3500 "</submodule>\n"
3501 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
3502 " <yang-version value=\"1\"/>\n"
3503 " <belongs-to module=\"a\">\n"
3504 " <prefix value=\"a_pref\"/>\n"
3505 " </belongs-to>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02003506 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003507 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
Michal Vaskod0625d72022-10-06 15:02:50 +02003508 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 +02003509 CHECK_LOG_CTX("Trailing garbage \"<submodule name...\" after submodule, expected end-of-input.", "Line number 8.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02003510 lysp_module_free(&fctx, (struct lysp_module *)submod);
Michal Vaskod0625d72022-10-06 15:02:50 +02003511 lysp_yin_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02003512 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02003513 yin_ctx = NULL;
3514 submod = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02003515}
3516
David Sedlák3b4db242018-10-19 16:11:01 +02003517int
3518main(void)
3519{
3520
3521 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +02003522 UTEST(test_yin_match_keyword, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003523 UTEST(test_yin_parse_content, setup, teardown),
3524 UTEST(test_validate_value, setup, teardown),
Michal Vasko12ef5362022-09-16 15:13:58 +02003525 UTEST(test_valid_module),
3526 UTEST(test_print_module),
3527 UTEST(test_print_submodule),
David Sedlák32488102019-07-15 17:44:10 +02003528
Radek Iša56ca9e42020-09-08 18:42:00 +02003529 UTEST(test_yin_match_argument_name),
3530 cmocka_unit_test_setup_teardown(test_enum_elem, setup, teardown),
3531 cmocka_unit_test_setup_teardown(test_bit_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003532 cmocka_unit_test_setup_teardown(test_status_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003533 cmocka_unit_test_setup_teardown(test_yin_element_elem, setup, teardown),
3534 cmocka_unit_test_setup_teardown(test_yangversion_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003535 cmocka_unit_test_setup_teardown(test_argument_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003536 cmocka_unit_test_setup_teardown(test_belongsto_elem, setup, teardown),
3537 cmocka_unit_test_setup_teardown(test_config_elem, setup, teardown),
3538 cmocka_unit_test_setup_teardown(test_default_elem, setup, teardown),
3539 cmocka_unit_test_setup_teardown(test_err_app_tag_elem, setup, teardown),
3540 cmocka_unit_test_setup_teardown(test_err_msg_elem, setup, teardown),
3541 cmocka_unit_test_setup_teardown(test_fracdigits_elem, setup, teardown),
3542 cmocka_unit_test_setup_teardown(test_iffeature_elem, setup, teardown),
3543 cmocka_unit_test_setup_teardown(test_length_elem, setup, teardown),
3544 cmocka_unit_test_setup_teardown(test_modifier_elem, setup, teardown),
3545 cmocka_unit_test_setup_teardown(test_namespace_elem, setup, teardown),
3546 cmocka_unit_test_setup_teardown(test_pattern_elem, setup, teardown),
3547 cmocka_unit_test_setup_teardown(test_value_position_elem, setup, teardown),
3548 cmocka_unit_test_setup_teardown(test_prefix_elem, setup, teardown),
3549 cmocka_unit_test_setup_teardown(test_range_elem, setup, teardown),
3550 cmocka_unit_test_setup_teardown(test_reqinstance_elem, setup, teardown),
3551 cmocka_unit_test_setup_teardown(test_revision_date_elem, setup, teardown),
3552 cmocka_unit_test_setup_teardown(test_unique_elem, setup, teardown),
3553 cmocka_unit_test_setup_teardown(test_units_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003554 cmocka_unit_test_setup_teardown(test_yin_text_value_elem, setup, teardown),
3555 cmocka_unit_test_setup_teardown(test_type_elem, setup, teardown),
3556 cmocka_unit_test_setup_teardown(test_max_elems_elem, setup, teardown),
3557 cmocka_unit_test_setup_teardown(test_min_elems_elem, setup, teardown),
3558 cmocka_unit_test_setup_teardown(test_ordby_elem, setup, teardown),
3559 cmocka_unit_test_setup_teardown(test_any_elem, setup, teardown),
3560 cmocka_unit_test_setup_teardown(test_leaf_elem, setup, teardown),
3561 cmocka_unit_test_setup_teardown(test_leaf_list_elem, setup, teardown),
3562 cmocka_unit_test_setup_teardown(test_presence_elem, setup, teardown),
3563 cmocka_unit_test_setup_teardown(test_key_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003564 cmocka_unit_test_setup_teardown(test_uses_elem, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02003565 cmocka_unit_test_setup_teardown(test_list_elem, setup, teardown),
3566 cmocka_unit_test_setup_teardown(test_notification_elem, setup, teardown),
3567 cmocka_unit_test_setup_teardown(test_grouping_elem, setup, teardown),
3568 cmocka_unit_test_setup_teardown(test_container_elem, setup, teardown),
3569 cmocka_unit_test_setup_teardown(test_case_elem, setup, teardown),
3570 cmocka_unit_test_setup_teardown(test_choice_elem, setup, teardown),
3571 cmocka_unit_test_setup_teardown(test_inout_elem, setup, teardown),
3572 cmocka_unit_test_setup_teardown(test_action_elem, setup, teardown),
3573 cmocka_unit_test_setup_teardown(test_augment_elem, setup, teardown),
3574 cmocka_unit_test_setup_teardown(test_deviate_elem, setup, teardown),
3575 cmocka_unit_test_setup_teardown(test_deviation_elem, setup, teardown),
3576 cmocka_unit_test_setup_teardown(test_module_elem, setup, teardown),
3577 cmocka_unit_test_setup_teardown(test_submodule_elem, setup, teardown),
David Sedlák8985a142019-07-31 16:43:06 +02003578
Radek Iša56ca9e42020-09-08 18:42:00 +02003579 cmocka_unit_test_setup_teardown(test_yin_parse_module, setup, teardown),
3580 cmocka_unit_test_setup_teardown(test_yin_parse_submodule, setup, teardown),
David Sedlák3b4db242018-10-19 16:11:01 +02003581 };
3582
Radek Iša56ca9e42020-09-08 18:42:00 +02003583 return cmocka_run_group_tests(tests, NULL, NULL);
David Sedlák3b4db242018-10-19 16:11:01 +02003584}