blob: 1a069a1e78b4d4c1f042d4275976ed4691688474 [file] [log] [blame]
David Sedlákb1ce3f82019-06-05 14:37:26 +02001/**
2 * @file test_parser_yin.c
3 * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz>
4 * @brief unit tests for functions from parser_yin.c
5 *
6 * Copyright (c) 2015 - 2019 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
David Sedlák3b4db242018-10-19 16:11:01 +020015#include <stdarg.h>
16#include <stddef.h>
17#include <setjmp.h>
18#include <cmocka.h>
19
20#include <stdio.h>
21#include <string.h>
David Sedlák79e50cb2019-06-05 16:33:09 +020022#include <stdbool.h>
David Sedlák3b4db242018-10-19 16:11:01 +020023
Radek Krejci70593c12020-06-13 20:48:09 +020024#include "common.h"
25#include "parser_internal.h"
26#include "parser_yin.h"
27#include "tree_schema.h"
28#include "tree_schema_internal.h"
29#include "xml.h"
30#include "xpath.h"
David Sedlák3b4db242018-10-19 16:11:01 +020031
David Sedlák555c7202019-07-04 12:14:12 +020032/* prototypes of static functions */
33void lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext);
David Sedlák986cb412019-07-04 13:10:11 +020034void lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext);
David Sedlák32eee7b2019-07-09 12:38:44 +020035void lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when);
David Sedlák32488102019-07-15 17:44:10 +020036void lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type);
David Sedlák8a83bbb2019-07-18 14:46:00 +020037void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
David Sedlák04e17b22019-07-19 15:29:48 +020038void lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf);
David Sedlákd2d676a2019-07-22 11:28:19 +020039void lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref);
David Sedlákaa854b02019-07-22 14:17:10 +020040void lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev);
David Sedlák0c2bab92019-07-22 15:33:19 +020041void lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include);
David Sedlák5e13dea2019-07-22 16:06:45 +020042void lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat);
David Sedlák28794f22019-07-22 16:45:00 +020043void lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident);
David Sedlák031b9e72019-07-23 15:19:37 +020044void lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif);
David Sedláke3ce9ef2019-07-23 16:34:30 +020045void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp);
David Sedlák05404f62019-07-24 14:11:53 +020046void lysp_action_inout_free(struct ly_ctx *ctx, struct lysp_action_inout *inout);
David Sedlák85d0eca2019-07-24 15:15:21 +020047void lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action);
David Sedlák992fb7c2019-07-24 16:51:01 +020048void lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment);
David Sedlák4ffcec82019-07-25 15:10:21 +020049void lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d);
David Sedlák8b754462019-07-25 16:22:13 +020050void lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev);
David Sedlák298ff6d2019-07-26 14:29:03 +020051void lysp_submodule_free(struct ly_ctx *ctx, struct lysp_submodule *submod);
52void lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import);
David Sedlák555c7202019-07-04 12:14:12 +020053
David Sedláke6cd89e2019-08-07 12:46:02 +020054/* wrapping element used for mocking has nothing to do with real module structure */
55#define ELEMENT_WRAPPER_START "<status xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
56#define ELEMENT_WRAPPER_END "</status>"
57
Radek Krejci3a4889a2020-05-19 17:01:58 +020058struct test_parser_yin_state {
David Sedlák3b4db242018-10-19 16:11:01 +020059 struct ly_ctx *ctx;
David Sedlák3017da42019-02-15 09:48:04 +010060 struct lys_module *mod;
David Sedlák619db942019-07-03 14:47:30 +020061 struct lysp_module *lysp_mod;
Michal Vaskob36053d2020-03-26 15:49:30 +010062 struct lys_yin_parser_ctx *yin_ctx;
Michal Vasko63f3d842020-07-08 10:10:14 +020063 struct ly_in *in;
David Sedlák79e50cb2019-06-05 16:33:09 +020064 bool finished_correctly;
David Sedlák68a1af12019-03-08 13:46:54 +010065};
David Sedlák872c7b42018-10-26 13:15:20 +020066
David Sedlák79e50cb2019-06-05 16:33:09 +020067#define BUFSIZE 1024
68char logbuf[BUFSIZE] = {0};
69int store = -1; /* negative for infinite logging, positive for limited logging */
70
71/* set to 0 to printing error messages to stderr instead of checking them in code */
David Sedláke0ef1c62019-09-13 10:05:55 +020072#define ENABLE_LOGGER_CHECKING 1
David Sedlák79e50cb2019-06-05 16:33:09 +020073
74#if ENABLE_LOGGER_CHECKING
75static void
76logger(LY_LOG_LEVEL level, const char *msg, const char *path)
77{
78 (void) level; /* unused */
79 if (store) {
80 if (path && path[0]) {
81 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
82 } else {
83 strncpy(logbuf, msg, BUFSIZE - 1);
84 }
85 if (store > 0) {
86 --store;
87 }
88 }
89}
90#endif
91
92#if ENABLE_LOGGER_CHECKING
93# define logbuf_assert(str) assert_string_equal(logbuf, str)
94#else
95# define logbuf_assert(str)
96#endif
97
98#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
99 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
100 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
101 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
102 CLEANUP
103
David Sedlák8e7bda82019-07-16 17:57:50 +0200104int
105setup_ly_ctx(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +0100106{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200107 struct test_parser_yin_state *st = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +0200108
David Sedlák68a1af12019-03-08 13:46:54 +0100109 /* allocate state variable */
110 (*state) = st = calloc(1, sizeof(*st));
111 if (!st) {
112 fprintf(stderr, "Memmory allocation failed");
113 return EXIT_FAILURE;
114 }
David Sedlák872c7b42018-10-26 13:15:20 +0200115
David Sedlák68a1af12019-03-08 13:46:54 +0100116 /* create new libyang context */
117 ly_ctx_new(NULL, 0, &st->ctx);
David Sedlák872c7b42018-10-26 13:15:20 +0200118
David Sedlák8e7bda82019-07-16 17:57:50 +0200119 return EXIT_SUCCESS;
120}
121
122int
123destroy_ly_ctx(void **state)
124{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200125 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +0200126 ly_ctx_destroy(st->ctx, NULL);
127 free(st);
128
129 return EXIT_SUCCESS;
130}
131
132static int
133setup_f(void **state)
134{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200135 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +0200136
137#if ENABLE_LOGGER_CHECKING
138 /* setup logger */
139 ly_set_log_clb(logger, 1);
140#endif
141
David Sedlák68a1af12019-03-08 13:46:54 +0100142 /* allocate new module */
143 st->mod = calloc(1, sizeof(*st->mod));
144 st->mod->ctx = st->ctx;
145
David Sedlák619db942019-07-03 14:47:30 +0200146 /* allocate new parsed module */
147 st->lysp_mod = calloc(1, sizeof(*st->lysp_mod));
148 st->lysp_mod->mod = calloc(1, sizeof(*st->lysp_mod->mod));
149 st->lysp_mod->mod->ctx = st->ctx;
150
151 /* allocate parser context */
David Sedlákda8ffa32019-07-08 14:17:10 +0200152 st->yin_ctx = calloc(1, sizeof(*st->yin_ctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100153 st->yin_ctx->format = LYS_IN_YIN;
David Sedlák8f5bce02019-06-03 16:41:08 +0200154
Michal Vasko63f3d842020-07-08 10:10:14 +0200155 st->in = NULL;
156
David Sedlák68a1af12019-03-08 13:46:54 +0100157 return EXIT_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +0200158}
159
160static int
David Sedlák68a1af12019-03-08 13:46:54 +0100161teardown_f(void **state)
162{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200163 struct test_parser_yin_state *st = *(struct test_parser_yin_state **)state;
David Sedlák619db942019-07-03 14:47:30 +0200164 struct lys_module *temp;
David Sedlák68a1af12019-03-08 13:46:54 +0100165
David Sedlák79e50cb2019-06-05 16:33:09 +0200166#if ENABLE_LOGGER_CHECKING
167 /* teardown logger */
168 if (!st->finished_correctly && logbuf[0] != '\0') {
169 fprintf(stderr, "%s\n", logbuf);
170 }
171#endif
172
David Sedlák619db942019-07-03 14:47:30 +0200173 temp = st->lysp_mod->mod;
174
Michal Vaskob36053d2020-03-26 15:49:30 +0100175 lyxml_ctx_free(st->yin_ctx->xmlctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100176 lys_module_free(st->mod, NULL);
David Sedlák619db942019-07-03 14:47:30 +0200177 lysp_module_free(st->lysp_mod);
178 lys_module_free(temp, NULL);
David Sedlákda8ffa32019-07-08 14:17:10 +0200179 free(st->yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200180 ly_in_free(st->in, 0);
David Sedlák68a1af12019-03-08 13:46:54 +0100181
182 return EXIT_SUCCESS;
183}
184
Radek Krejci3a4889a2020-05-19 17:01:58 +0200185static struct test_parser_yin_state*
David Sedlák392af4f2019-06-04 16:02:42 +0200186reset_state(void **state)
187{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200188 ((struct test_parser_yin_state *)*state)->finished_correctly = true;
David Sedlák555c7202019-07-04 12:14:12 +0200189 logbuf[0] = '\0';
David Sedlák392af4f2019-06-04 16:02:42 +0200190 teardown_f(state);
191 setup_f(state);
192
193 return *state;
194}
195
David Sedlák79e50cb2019-06-05 16:33:09 +0200196void
197logbuf_clean(void)
198{
199 logbuf[0] = '\0';
200}
201
David Sedlák8985a142019-07-31 16:43:06 +0200202static int
203setup_logger(void **state)
204{
205 (void)state; /* unused */
206#if ENABLE_LOGGER_CHECKING
207 /* setup logger */
208 ly_set_log_clb(logger, 1);
209#endif
210
211 logbuf[0] = '\0';
212
213 return EXIT_SUCCESS;
214}
215
216static int
217teardown_logger(void **state)
218{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200219 struct test_parser_yin_state *st = *state;
David Sedlák8985a142019-07-31 16:43:06 +0200220
221#if ENABLE_LOGGER_CHECKING
222 /* teardown logger */
223 if (!st->finished_correctly && logbuf[0] != '\0') {
224 fprintf(stderr, "%s\n", logbuf);
225 }
226#endif
227
228 return EXIT_SUCCESS;
229}
230
231static int
232setup_element_test(void **state)
233{
234 setup_logger(state);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200235 struct test_parser_yin_state *st = *state;
David Sedlák8985a142019-07-31 16:43:06 +0200236
237 st->yin_ctx = calloc(1, sizeof(*st->yin_ctx));
Michal Vaskob36053d2020-03-26 15:49:30 +0100238 st->yin_ctx->format = LYS_IN_YIN;
David Sedlák8985a142019-07-31 16:43:06 +0200239
240 return EXIT_SUCCESS;
241}
242
243static int
244teardown_element_test(void **state)
245{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200246 struct test_parser_yin_state *st = *(struct test_parser_yin_state **)state;
David Sedlák8985a142019-07-31 16:43:06 +0200247
Michal Vaskob36053d2020-03-26 15:49:30 +0100248 lyxml_ctx_free(st->yin_ctx->xmlctx);
David Sedlák8985a142019-07-31 16:43:06 +0200249 free(st->yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200250 ly_in_free(st->in, 0);
David Sedlák8985a142019-07-31 16:43:06 +0200251
252 teardown_logger(state);
253
254 return EXIT_SUCCESS;
255}
256
David Sedlák68a1af12019-03-08 13:46:54 +0100257static void
David Sedlák1bccdfa2019-06-17 15:55:27 +0200258test_yin_match_keyword(void **state)
David Sedlák3b4db242018-10-19 16:11:01 +0200259{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200260 struct test_parser_yin_state *st = *state;
David Sedlák3b4db242018-10-19 16:11:01 +0200261
Michal Vaskob36053d2020-03-26 15:49:30 +0100262 const char *prefix;
263 size_t prefix_len;
David Sedlák8f7a1172019-06-20 14:42:18 +0200264 /* create mock yin namespace in xml context */
265 const char *data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" />";
Michal Vasko63f3d842020-07-08 10:10:14 +0200266 ly_in_new_memory(data, &st->in);
267 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100268 prefix = st->yin_ctx->xmlctx->prefix;
269 prefix_len = st->yin_ctx->xmlctx->prefix_len;
David Sedlák8f7a1172019-06-20 14:42:18 +0200270
Radek Krejcid6b76452019-09-03 17:03:03 +0200271 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydatax", strlen("anydatax"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
272 assert_int_equal(yin_match_keyword(st->yin_ctx, "asdasd", strlen("asdasd"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
273 assert_int_equal(yin_match_keyword(st->yin_ctx, "", 0, prefix, prefix_len, LY_STMT_NONE), LY_STMT_NONE);
274 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydata", strlen("anydata"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ANYDATA);
275 assert_int_equal(yin_match_keyword(st->yin_ctx, "anyxml", strlen("anyxml"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ANYXML);
276 assert_int_equal(yin_match_keyword(st->yin_ctx, "argument", strlen("argument"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ARGUMENT);
277 assert_int_equal(yin_match_keyword(st->yin_ctx, "augment", strlen("augment"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_AUGMENT);
278 assert_int_equal(yin_match_keyword(st->yin_ctx, "base", strlen("base"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BASE);
279 assert_int_equal(yin_match_keyword(st->yin_ctx, "belongs-to", strlen("belongs-to"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BELONGS_TO);
280 assert_int_equal(yin_match_keyword(st->yin_ctx, "bit", strlen("bit"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_BIT);
281 assert_int_equal(yin_match_keyword(st->yin_ctx, "case", strlen("case"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CASE);
282 assert_int_equal(yin_match_keyword(st->yin_ctx, "choice", strlen("choice"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CHOICE);
283 assert_int_equal(yin_match_keyword(st->yin_ctx, "config", strlen("config"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONFIG);
284 assert_int_equal(yin_match_keyword(st->yin_ctx, "contact", strlen("contact"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONTACT);
285 assert_int_equal(yin_match_keyword(st->yin_ctx, "container", strlen("container"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_CONTAINER);
286 assert_int_equal(yin_match_keyword(st->yin_ctx, "default", strlen("default"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEFAULT);
287 assert_int_equal(yin_match_keyword(st->yin_ctx, "description", strlen("description"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DESCRIPTION);
288 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviate", strlen("deviate"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEVIATE);
289 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviation", strlen("deviation"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_DEVIATION);
290 assert_int_equal(yin_match_keyword(st->yin_ctx, "enum", strlen("enum"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ENUM);
291 assert_int_equal(yin_match_keyword(st->yin_ctx, "error-app-tag", strlen("error-app-tag"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ERROR_APP_TAG);
292 assert_int_equal(yin_match_keyword(st->yin_ctx, "error-message", strlen("error-message"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ERROR_MESSAGE);
293 assert_int_equal(yin_match_keyword(st->yin_ctx, "extension", strlen("extension"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_EXTENSION);
294 assert_int_equal(yin_match_keyword(st->yin_ctx, "feature", strlen("feature"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_FEATURE);
295 assert_int_equal(yin_match_keyword(st->yin_ctx, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_FRACTION_DIGITS);
296 assert_int_equal(yin_match_keyword(st->yin_ctx, "grouping", strlen("grouping"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_GROUPING);
297 assert_int_equal(yin_match_keyword(st->yin_ctx, "identity", strlen("identity"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IDENTITY);
298 assert_int_equal(yin_match_keyword(st->yin_ctx, "if-feature", strlen("if-feature"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IF_FEATURE);
299 assert_int_equal(yin_match_keyword(st->yin_ctx, "import", strlen("import"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_IMPORT);
300 assert_int_equal(yin_match_keyword(st->yin_ctx, "include", strlen("include"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_INCLUDE);
301 assert_int_equal(yin_match_keyword(st->yin_ctx, "input", strlen("input"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_INPUT);
302 assert_int_equal(yin_match_keyword(st->yin_ctx, "key", strlen("key"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_KEY);
303 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf", strlen("leaf"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LEAF);
304 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf-list", strlen("leaf-list"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LEAF_LIST);
305 assert_int_equal(yin_match_keyword(st->yin_ctx, "length", strlen("length"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LENGTH);
306 assert_int_equal(yin_match_keyword(st->yin_ctx, "list", strlen("list"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_LIST);
307 assert_int_equal(yin_match_keyword(st->yin_ctx, "mandatory", strlen("mandatory"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MANDATORY);
308 assert_int_equal(yin_match_keyword(st->yin_ctx, "max-elements", strlen("max-elements"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MAX_ELEMENTS);
309 assert_int_equal(yin_match_keyword(st->yin_ctx, "min-elements", strlen("min-elements"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MIN_ELEMENTS);
310 assert_int_equal(yin_match_keyword(st->yin_ctx, "modifier", strlen("modifier"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MODIFIER);
311 assert_int_equal(yin_match_keyword(st->yin_ctx, "module", strlen("module"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MODULE);
312 assert_int_equal(yin_match_keyword(st->yin_ctx, "must", strlen("must"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_MUST);
313 assert_int_equal(yin_match_keyword(st->yin_ctx, "namespace", strlen("namespace"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NAMESPACE);
314 assert_int_equal(yin_match_keyword(st->yin_ctx, "notification", strlen("notification"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_NOTIFICATION);
315 assert_int_equal(yin_match_keyword(st->yin_ctx, "ordered-by", strlen("ordered-by"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ORDERED_BY);
316 assert_int_equal(yin_match_keyword(st->yin_ctx, "organization", strlen("organization"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_ORGANIZATION);
317 assert_int_equal(yin_match_keyword(st->yin_ctx, "output", strlen("output"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_OUTPUT);
318 assert_int_equal(yin_match_keyword(st->yin_ctx, "path", strlen("path"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PATH);
319 assert_int_equal(yin_match_keyword(st->yin_ctx, "pattern", strlen("pattern"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PATTERN);
320 assert_int_equal(yin_match_keyword(st->yin_ctx, "position", strlen("position"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_POSITION);
321 assert_int_equal(yin_match_keyword(st->yin_ctx, "prefix", strlen("prefix"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PREFIX);
322 assert_int_equal(yin_match_keyword(st->yin_ctx, "presence", strlen("presence"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_PRESENCE);
323 assert_int_equal(yin_match_keyword(st->yin_ctx, "range", strlen("range"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_RANGE);
324 assert_int_equal(yin_match_keyword(st->yin_ctx, "reference", strlen("reference"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REFERENCE);
325 assert_int_equal(yin_match_keyword(st->yin_ctx, "refine", strlen("refine"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REFINE);
326 assert_int_equal(yin_match_keyword(st->yin_ctx, "require-instance", strlen("require-instance"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REQUIRE_INSTANCE);
327 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision", strlen("revision"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REVISION);
328 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision-date", strlen("revision-date"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_REVISION_DATE);
329 assert_int_equal(yin_match_keyword(st->yin_ctx, "rpc", strlen("rpc"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_RPC);
330 assert_int_equal(yin_match_keyword(st->yin_ctx, "status", strlen("status"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_STATUS);
331 assert_int_equal(yin_match_keyword(st->yin_ctx, "submodule", strlen("submodule"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_SUBMODULE);
332 assert_int_equal(yin_match_keyword(st->yin_ctx, "type", strlen("type"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_TYPE);
333 assert_int_equal(yin_match_keyword(st->yin_ctx, "typedef", strlen("typedef"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_TYPEDEF);
334 assert_int_equal(yin_match_keyword(st->yin_ctx, "unique", strlen("unique"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_UNIQUE);
335 assert_int_equal(yin_match_keyword(st->yin_ctx, "units", strlen("units"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_UNITS);
336 assert_int_equal(yin_match_keyword(st->yin_ctx, "uses", strlen("uses"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_USES);
337 assert_int_equal(yin_match_keyword(st->yin_ctx, "value", strlen("value"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_VALUE);
338 assert_int_equal(yin_match_keyword(st->yin_ctx, "when", strlen("when"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_WHEN);
339 assert_int_equal(yin_match_keyword(st->yin_ctx, "yang-version", strlen("yang-version"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_YANG_VERSION);
340 assert_int_equal(yin_match_keyword(st->yin_ctx, "yin-element", strlen("yin-element"), prefix, prefix_len, LY_STMT_NONE), LY_STMT_YIN_ELEMENT);
David Sedlák8f7a1172019-06-20 14:42:18 +0200341
342 st->finished_correctly = true;
David Sedlák872c7b42018-10-26 13:15:20 +0200343}
David Sedlák3b4db242018-10-19 16:11:01 +0200344
David Sedlák872c7b42018-10-26 13:15:20 +0200345static void
David Sedlák060b00e2019-06-19 11:12:06 +0200346test_yin_match_argument_name(void **state)
David Sedlák872c7b42018-10-26 13:15:20 +0200347{
David Sedlák68a1af12019-03-08 13:46:54 +0100348 (void)state; /* unused */
David Sedlák872c7b42018-10-26 13:15:20 +0200349
David Sedlák060b00e2019-06-19 11:12:06 +0200350 assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN);
351 assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN);
352 assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN);
353 assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION);
354 assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE);
355 assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE);
356 assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME);
357 assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG);
358 assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
359 assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT);
360 assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI);
361 assert_int_equal(yin_match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200362}
363
David Sedlák68a1af12019-03-08 13:46:54 +0100364static void
David Sedlákb1a78352019-06-28 16:16:29 +0200365test_yin_parse_element_generic(void **state)
366{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200367 struct test_parser_yin_state *st = *state;
David Sedlákb1a78352019-06-28 16:16:29 +0200368 struct lysp_ext_instance exts;
David Sedlákb1a78352019-06-28 16:16:29 +0200369 LY_ERR ret;
370
371 memset(&exts, 0, sizeof(exts));
372
David Sedlákb0ca07d2019-09-11 11:54:05 +0200373 const char *data = "<myext:elem attr=\"value\" xmlns:myext=\"urn:example:extensions\">text_value</myext:elem>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200374 ly_in_new_memory(data, &st->in);
375 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100376
377 ret = yin_parse_element_generic(st->yin_ctx, LY_STMT_EXTENSION_INSTANCE, &exts.child);
David Sedlákb1a78352019-06-28 16:16:29 +0200378 assert_int_equal(ret, LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +0100379 assert_int_equal(st->yin_ctx->xmlctx->status, LYXML_ELEM_CLOSE);
David Sedláke0ef1c62019-09-13 10:05:55 +0200380 assert_string_equal(exts.child->stmt, "urn:example:extensions:elem");
David Sedlákb1a78352019-06-28 16:16:29 +0200381 assert_string_equal(exts.child->arg, "text_value");
David Sedlákb1a78352019-06-28 16:16:29 +0200382 assert_string_equal(exts.child->child->stmt, "attr");
383 assert_string_equal(exts.child->child->arg, "value");
384 assert_true(exts.child->child->flags & LYS_YIN_ATTR);
David Sedlákb1a78352019-06-28 16:16:29 +0200385 lysp_ext_instance_free(st->ctx, &exts);
David Sedlák5392a212019-07-01 09:19:10 +0200386 st = reset_state(state);
387
David Sedlákb0ca07d2019-09-11 11:54:05 +0200388 data = "<myext:elem xmlns:myext=\"urn:example:extensions\"></myext:elem>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200389 ly_in_new_memory(data, &st->in);
390 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100391
392 ret = yin_parse_element_generic(st->yin_ctx, LY_STMT_EXTENSION_INSTANCE, &exts.child);
David Sedlák5392a212019-07-01 09:19:10 +0200393 assert_int_equal(ret, LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +0100394 assert_int_equal(st->yin_ctx->xmlctx->status, LYXML_ELEM_CLOSE);
David Sedláke0ef1c62019-09-13 10:05:55 +0200395 assert_string_equal(exts.child->stmt, "urn:example:extensions:elem");
David Sedlák5392a212019-07-01 09:19:10 +0200396 assert_null(exts.child->child);
397 assert_null(exts.child->arg);
398 lysp_ext_instance_free(st->ctx, &exts);
399
David Sedlákb1a78352019-06-28 16:16:29 +0200400 st->finished_correctly = true;
401}
402
403static void
404test_yin_parse_extension_instance(void **state)
405{
406 LY_ERR ret;
Radek Krejci3a4889a2020-05-19 17:01:58 +0200407 struct test_parser_yin_state *st = *state;
David Sedlákb1a78352019-06-28 16:16:29 +0200408 struct lysp_ext_instance *exts = NULL;
David Sedlákb0ca07d2019-09-11 11:54:05 +0200409 const char *data = "<myext:ext value1=\"test\" value=\"test2\" xmlns:myext=\"urn:example:extensions\"><myext:subelem>text</myext:subelem></myext:ext>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200410 ly_in_new_memory(data, &st->in);
411 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100412
413 ret = yin_parse_extension_instance(st->yin_ctx, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200414 assert_int_equal(ret, LY_SUCCESS);
David Sedláke0ef1c62019-09-13 10:05:55 +0200415 assert_string_equal(exts->name, "urn:example:extensions:ext");
David Sedlákb1a78352019-06-28 16:16:29 +0200416 assert_int_equal(exts->insubstmt_index, 0);
417 assert_true(exts->insubstmt == LYEXT_SUBSTMT_CONTACT);
418 assert_true(exts->yin & LYS_YIN);
419 assert_string_equal(exts->child->stmt, "value1");
420 assert_string_equal(exts->child->arg, "test");
421 assert_null(exts->child->child);
422 assert_true(exts->child->flags & LYS_YIN_ATTR);
423 assert_string_equal(exts->child->next->stmt, "value");
424 assert_string_equal(exts->child->next->arg, "test2");
425 assert_null(exts->child->next->child);
426 assert_true(exts->child->next->flags & LYS_YIN_ATTR);
427
David Sedláke0ef1c62019-09-13 10:05:55 +0200428 assert_string_equal(exts->child->next->next->stmt, "urn:example:extensions:subelem");
David Sedlákb1a78352019-06-28 16:16:29 +0200429 assert_string_equal(exts->child->next->next->arg, "text");
430 assert_null(exts->child->next->next->child);
431 assert_null(exts->child->next->next->next);
432 assert_false(exts->child->next->next->flags & LYS_YIN_ATTR);
David Sedlákb1a78352019-06-28 16:16:29 +0200433 lysp_ext_instance_free(st->ctx, exts);
434 LY_ARRAY_FREE(exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200435 exts = NULL;
David Sedlákf250ecf2019-07-01 11:02:05 +0200436 st = reset_state(state);
437
David Sedlákb0ca07d2019-09-11 11:54:05 +0200438 data = "<myext:extension-elem xmlns:myext=\"urn:example:extensions\" />";
Michal Vasko63f3d842020-07-08 10:10:14 +0200439 ly_in_new_memory(data, &st->in);
440 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100441
442 ret = yin_parse_extension_instance(st->yin_ctx, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200443 assert_int_equal(ret, LY_SUCCESS);
David Sedláke0ef1c62019-09-13 10:05:55 +0200444 assert_string_equal(exts->name, "urn:example:extensions:extension-elem");
David Sedlákf250ecf2019-07-01 11:02:05 +0200445 assert_null(exts->argument);
446 assert_null(exts->child);
447 assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT);
448 assert_int_equal(exts->insubstmt_index, 0);
449 assert_true(exts->yin & LYS_YIN);
David Sedlákf250ecf2019-07-01 11:02:05 +0200450 lysp_ext_instance_free(st->ctx, exts);
451 LY_ARRAY_FREE(exts);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200452 exts = NULL;
David Sedlákadd0c2e2019-08-16 10:49:12 +0200453 st = reset_state(state);
454
David Sedlákb0ca07d2019-09-11 11:54:05 +0200455 data = "<myext:ext attr1=\"text1\" attr2=\"text2\" xmlns:myext=\"urn:example:extensions\">"
456 "<myext:ext-sub1/>"
457 "<myext:ext-sub2 sattr1=\"stext2\">"
458 "<myext:ext-sub21>"
459 "<myext:ext-sub211 sattr21=\"text21\"/>"
460 "</myext:ext-sub21>"
461 "</myext:ext-sub2>"
462 "<myext:ext-sub3 attr3=\"text3\"></myext:ext-sub3>"
463 "</myext:ext>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200464 ly_in_new_memory(data, &st->in);
465 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100466
467 ret = yin_parse_extension_instance(st->yin_ctx, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200468 assert_int_equal(ret, LY_SUCCESS);
469
David Sedláke0ef1c62019-09-13 10:05:55 +0200470 assert_string_equal(exts->name, "urn:example:extensions:ext");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200471 assert_null(exts->argument);
472 assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT);
473 assert_int_equal(exts->insubstmt_index, 0);
474 assert_true(exts->yin & LYS_YIN);
475 assert_string_equal(exts->child->stmt, "attr1");
476 assert_string_equal(exts->child->arg, "text1");
477 assert_null(exts->child->child);
478 assert_true(exts->child->flags & LYS_YIN_ATTR);
479
480 assert_string_equal(exts->child->next->stmt, "attr2");
481 assert_string_equal(exts->child->next->arg, "text2");
482 assert_null(exts->child->next->child);
483 assert_true(exts->child->next->flags & LYS_YIN_ATTR);
484
David Sedláke0ef1c62019-09-13 10:05:55 +0200485 assert_string_equal(exts->child->next->next->stmt, "urn:example:extensions:ext-sub1");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200486 assert_null(exts->child->next->next->arg);
487 assert_null(exts->child->next->next->child);
488 assert_int_equal(exts->child->next->next->flags, 0);
489
David Sedláke0ef1c62019-09-13 10:05:55 +0200490 assert_string_equal(exts->child->next->next->next->stmt, "urn:example:extensions:ext-sub2");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200491 assert_null(exts->child->next->next->next->arg);
492 assert_int_equal(exts->child->next->next->next->flags, 0);
493 assert_string_equal(exts->child->next->next->next->child->stmt, "sattr1");
494 assert_string_equal(exts->child->next->next->next->child->arg, "stext2");
495 assert_null(exts->child->next->next->next->child->child);
496 assert_true(exts->child->next->next->next->child->flags & LYS_YIN_ATTR);
497
David Sedláke0ef1c62019-09-13 10:05:55 +0200498 assert_string_equal(exts->child->next->next->next->child->next->stmt, "urn:example:extensions:ext-sub21");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200499 assert_null(exts->child->next->next->next->child->next->arg);
500 assert_null(exts->child->next->next->next->child->next->next);
501 assert_int_equal(exts->child->next->next->next->child->next->flags, 0);
502
David Sedláke0ef1c62019-09-13 10:05:55 +0200503 assert_string_equal(exts->child->next->next->next->child->next->child->stmt, "urn:example:extensions:ext-sub211");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200504 assert_null(exts->child->next->next->next->child->next->child->arg);
505 assert_int_equal(exts->child->next->next->next->child->next->child->flags, 0);
506 assert_null(exts->child->next->next->next->child->next->child->next);
507
508 assert_string_equal(exts->child->next->next->next->child->next->child->child->stmt, "sattr21");
509 assert_string_equal(exts->child->next->next->next->child->next->child->child->arg, "text21");
510 assert_null(exts->child->next->next->next->child->next->child->child->next);
511 assert_null(exts->child->next->next->next->child->next->child->child->child);
512 assert_true(exts->child->next->next->next->child->next->child->child->flags & LYS_YIN_ATTR);
513
David Sedláke0ef1c62019-09-13 10:05:55 +0200514 assert_string_equal(exts->child->next->next->next->next->stmt, "urn:example:extensions:ext-sub3");
David Sedlákadd0c2e2019-08-16 10:49:12 +0200515 assert_null(exts->child->next->next->next->next->arg);
516 assert_null(exts->child->next->next->next->next->next);
517 assert_int_equal(exts->child->next->next->next->next->flags, 0);
518
519 assert_string_equal(exts->child->next->next->next->next->child->stmt, "attr3");
520 assert_string_equal(exts->child->next->next->next->next->child->arg, "text3");
521 assert_null(exts->child->next->next->next->next->child->next);
522 assert_null(exts->child->next->next->next->next->child->child);
523 assert_true(exts->child->next->next->next->next->child->flags & LYS_YIN_ATTR);
524
David Sedlákadd0c2e2019-08-16 10:49:12 +0200525 lysp_ext_instance_free(st->ctx, exts);
526 LY_ARRAY_FREE(exts);
527 exts = NULL;
David Sedlákaa98bba2019-09-12 11:52:14 +0200528 st = reset_state(state);
529
530 data = "<myext:extension-elem xmlns:myext=\"urn:example:extensions\" xmlns:yin=\"urn:ietf:params:xml:ns:yang:yin:1\">"
531 "<yin:action name=\"act-name\" pre:prefixed=\"ignored\"/>"
532 "<yin:augment target-node=\"target\"/>"
533 "<yin:status value=\"value\"/>"
534 "<yin:include module=\"mod\"/>"
535 "<yin:input />"
536 "<yin:must condition=\"cond\"/>"
537 "<yin:namespace uri=\"uri\"/>"
538 "<yin:revision date=\"data\"/>"
539 "<yin:unique tag=\"tag\"/>"
David Sedlákd2844882019-09-13 16:01:22 +0200540 "<yin:description><yin:text>contact-val</yin:text></yin:description>"
541 "<yin:error-message><yin:value>err-msg</yin:value></yin:error-message>"
David Sedlákaa98bba2019-09-12 11:52:14 +0200542 "</myext:extension-elem>";
Michal Vasko63f3d842020-07-08 10:10:14 +0200543 ly_in_new_memory(data, &st->in);
544 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100545
546 ret = yin_parse_extension_instance(st->yin_ctx, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákaa98bba2019-09-12 11:52:14 +0200547 assert_int_equal(ret, LY_SUCCESS);
548 assert_string_equal(exts->child->arg, "act-name");
549 assert_string_equal(exts->child->next->arg, "target");
550 assert_string_equal(exts->child->next->next->arg, "value");
551 assert_string_equal(exts->child->next->next->next->arg, "mod");
552 assert_null(exts->child->next->next->next->next->arg);
553 assert_string_equal(exts->child->next->next->next->next->next->arg, "cond");
554 assert_string_equal(exts->child->next->next->next->next->next->next->arg, "uri");
555 assert_string_equal(exts->child->next->next->next->next->next->next->next->arg, "data");
556 assert_string_equal(exts->child->next->next->next->next->next->next->next->next->arg, "tag");
557 assert_string_equal(exts->child->next->next->next->next->next->next->next->next->next->arg, "contact-val");
David Sedlákaa98bba2019-09-12 11:52:14 +0200558 lysp_ext_instance_free(st->ctx, exts);
559 LY_ARRAY_FREE(exts);
560 exts = NULL;
David Sedlákaa98bba2019-09-12 11:52:14 +0200561 st = reset_state(state);
David Sedlákadd0c2e2019-08-16 10:49:12 +0200562
David Sedlákb1a78352019-06-28 16:16:29 +0200563 st->finished_correctly = true;
564}
565
David Sedlák555c7202019-07-04 12:14:12 +0200566static void
567test_yin_parse_content(void **state)
568{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200569 struct test_parser_yin_state *st = *state;
David Sedlák555c7202019-07-04 12:14:12 +0200570 LY_ERR ret = LY_SUCCESS;
David Sedlák555c7202019-07-04 12:14:12 +0200571 const char *data = "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlákb0ca07d2019-09-11 11:54:05 +0200572 "<myext:custom xmlns:myext=\"urn:example:extensions\">"
David Sedlák555c7202019-07-04 12:14:12 +0200573 "totally amazing extension"
David Sedlákb0ca07d2019-09-11 11:54:05 +0200574 "</myext:custom>"
David Sedlákaa98bba2019-09-12 11:52:14 +0200575 "<extension name=\"ext\">"
David Sedlák986cb412019-07-04 13:10:11 +0200576 "<argument name=\"argname\"></argument>"
577 "<description><text>desc</text></description>"
578 "<reference><text>ref</text></reference>"
579 "<status value=\"deprecated\"></status>"
David Sedlákaa98bba2019-09-12 11:52:14 +0200580 "</extension>"
David Sedlák555c7202019-07-04 12:14:12 +0200581 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedlák32488102019-07-15 17:44:10 +0200582 "<if-feature name=\"foo\"></if-feature>"
David Sedlák32eee7b2019-07-09 12:38:44 +0200583 "<when condition=\"condition...\">"
584 "<reference><text>when_ref</text></reference>"
585 "<description><text>when_desc</text></description>"
586 "</when>"
David Sedláke1a30302019-07-10 13:49:38 +0200587 "<config value=\"true\"/>"
David Sedlákc1771b12019-07-10 15:55:46 +0200588 "<error-message>"
589 "<value>error-msg</value>"
590 "</error-message>"
David Sedlák2ce1be62019-07-10 16:15:09 +0200591 "<error-app-tag value=\"err-app-tag\"/>"
David Sedláka5b1d382019-07-10 16:31:09 +0200592 "<units name=\"radians\"></units>"
David Sedláke7084ce2019-07-10 16:44:15 +0200593 "<default value=\"default-value\"/>"
David Sedlák5545f5d2019-07-11 11:55:16 +0200594 "<position value=\"25\"></position>"
595 "<value value=\"-5\"/>"
David Sedlákcf5569a2019-07-11 13:31:34 +0200596 "<require-instance value=\"true\"></require-instance>"
David Sedlákb7296dd2019-07-11 14:58:38 +0200597 "<range value=\"5..10\" />"
David Sedlák438ae432019-07-11 15:36:54 +0200598 "<length value=\"baf\"/>"
David Sedlákd3983112019-07-12 11:20:56 +0200599 "<pattern value='pattern'>"
600 "<modifier value='invert-match'/>"
601 "</pattern>"
David Sedlákfd5b9c32019-07-12 15:33:13 +0200602 "<enum name=\"yay\">"
603 "</enum>"
David Sedlák555c7202019-07-04 12:14:12 +0200604 "</prefix>";
605 struct lysp_ext_instance *exts = NULL;
David Sedlák5f8191e2019-07-08 16:35:52 +0200606 const char **if_features = NULL;
Michal Vasko7f45cf22020-10-01 12:49:44 +0200607 const char *value, *err_msg, *app_tag, *units;
608 struct lysp_qname def = {0};
David Sedlák986cb412019-07-04 13:10:11 +0200609 struct lysp_ext *ext_def = NULL;
David Sedlák32eee7b2019-07-09 12:38:44 +0200610 struct lysp_when *when_p = NULL;
David Sedlákcf5569a2019-07-11 13:31:34 +0200611 struct lysp_type_enum pos_enum = {}, val_enum = {};
David Sedlákfd5b9c32019-07-12 15:33:13 +0200612 struct lysp_type req_type = {}, range_type = {}, len_type = {}, patter_type = {}, enum_type = {};
David Sedláke1a30302019-07-10 13:49:38 +0200613 uint8_t config = 0;
David Sedlák555c7202019-07-04 12:14:12 +0200614
Michal Vasko63f3d842020-07-08 10:10:14 +0200615 ly_in_new_memory(data, &st->in);
616 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100617 lyxml_ctx_next(st->yin_ctx->xmlctx);
618 lyxml_ctx_next(st->yin_ctx->xmlctx);
619 lyxml_ctx_next(st->yin_ctx->xmlctx);
David Sedlák555c7202019-07-04 12:14:12 +0200620
David Sedlákfd5b9c32019-07-12 15:33:13 +0200621 struct yin_subelement subelems[17] = {
Radek Krejcid6b76452019-09-03 17:03:03 +0200622 {LY_STMT_CONFIG, &config, 0},
623 {LY_STMT_DEFAULT, &def, YIN_SUBELEM_UNIQUE},
624 {LY_STMT_ENUM, &enum_type, 0},
625 {LY_STMT_ERROR_APP_TAG, &app_tag, YIN_SUBELEM_UNIQUE},
626 {LY_STMT_ERROR_MESSAGE, &err_msg, 0},
627 {LY_STMT_EXTENSION, &ext_def, 0},
628 {LY_STMT_IF_FEATURE, &if_features, 0},
629 {LY_STMT_LENGTH, &len_type, 0},
630 {LY_STMT_PATTERN, &patter_type, 0},
631 {LY_STMT_POSITION, &pos_enum, 0},
632 {LY_STMT_RANGE, &range_type, 0},
633 {LY_STMT_REQUIRE_INSTANCE, &req_type, 0},
634 {LY_STMT_UNITS, &units, YIN_SUBELEM_UNIQUE},
635 {LY_STMT_VALUE, &val_enum, 0},
636 {LY_STMT_WHEN, &when_p, 0},
637 {LY_STMT_EXTENSION_INSTANCE, NULL, 0},
638 {LY_STMT_ARG_TEXT, &value, 0}
David Sedlákd3983112019-07-12 11:20:56 +0200639 };
Michal Vaskob36053d2020-03-26 15:49:30 +0100640 ret = yin_parse_content(st->yin_ctx, subelems, 17, LY_STMT_PREFIX, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200641 assert_int_equal(ret, LY_SUCCESS);
David Sedlák2ce1be62019-07-10 16:15:09 +0200642 /* check parsed values */
Michal Vasko7f45cf22020-10-01 12:49:44 +0200643 assert_string_equal(def.str, "default-value");
David Sedláke0ef1c62019-09-13 10:05:55 +0200644 assert_string_equal(exts->name, "urn:example:extensions:custom");
David Sedlák555c7202019-07-04 12:14:12 +0200645 assert_string_equal(exts->argument, "totally amazing extension");
646 assert_string_equal(value, "wsefsdf");
David Sedláka5b1d382019-07-10 16:31:09 +0200647 assert_string_equal(units, "radians");
David Sedlák32eee7b2019-07-09 12:38:44 +0200648 assert_string_equal(when_p->cond, "condition...");
649 assert_string_equal(when_p->dsc, "when_desc");
650 assert_string_equal(when_p->ref, "when_ref");
David Sedláke1a30302019-07-10 13:49:38 +0200651 assert_int_equal(config, LYS_CONFIG_W);
David Sedlák5545f5d2019-07-11 11:55:16 +0200652 assert_int_equal(pos_enum.value, 25);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200653 assert_true(pos_enum.flags & LYS_SET_VALUE);
David Sedlák5545f5d2019-07-11 11:55:16 +0200654 assert_int_equal(val_enum.value, -5);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200655 assert_true(val_enum.flags & LYS_SET_VALUE);
David Sedlákcf5569a2019-07-11 13:31:34 +0200656 assert_int_equal(req_type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200657 assert_true(req_type.flags &= LYS_SET_REQINST);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200658 assert_string_equal(range_type.range->arg.str, "5..10");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200659 assert_true(range_type.flags & LYS_SET_RANGE);
David Sedlákc1771b12019-07-10 15:55:46 +0200660 assert_string_equal(err_msg, "error-msg");
David Sedlák2ce1be62019-07-10 16:15:09 +0200661 assert_string_equal(app_tag, "err-app-tag");
David Sedlákfd5b9c32019-07-12 15:33:13 +0200662 assert_string_equal(enum_type.enums->name, "yay");
Michal Vasko7f45cf22020-10-01 12:49:44 +0200663 assert_string_equal(len_type.length->arg.str, "baf");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200664 assert_true(len_type.flags & LYS_SET_LENGTH);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200665 assert_string_equal(patter_type.patterns->arg.str, "\x015pattern");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200666 assert_true(patter_type.flags & LYS_SET_PATTERN);
David Sedlák2ce1be62019-07-10 16:15:09 +0200667 /* cleanup */
David Sedlák555c7202019-07-04 12:14:12 +0200668 lysp_ext_instance_free(st->ctx, exts);
David Sedlák32eee7b2019-07-09 12:38:44 +0200669 lysp_when_free(st->ctx, when_p);
David Sedlák986cb412019-07-04 13:10:11 +0200670 lysp_ext_free(st->ctx, ext_def);
David Sedlák5f8191e2019-07-08 16:35:52 +0200671 FREE_STRING(st->ctx, *if_features);
David Sedlákc1771b12019-07-10 15:55:46 +0200672 FREE_STRING(st->ctx, err_msg);
David Sedlák2ce1be62019-07-10 16:15:09 +0200673 FREE_STRING(st->ctx, app_tag);
David Sedláka5b1d382019-07-10 16:31:09 +0200674 FREE_STRING(st->ctx, units);
Michal Vasko7f45cf22020-10-01 12:49:44 +0200675 FREE_STRING(st->ctx, patter_type.patterns->arg.str);
676 FREE_STRING(st->ctx, def.str);
677 FREE_STRING(st->ctx, range_type.range->arg.str);
678 FREE_STRING(st->ctx, len_type.length->arg.str);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200679 FREE_STRING(st->ctx, enum_type.enums->name);
David Sedlákb7296dd2019-07-11 14:58:38 +0200680 FREE_STRING(st->ctx, value);
David Sedlák5f8191e2019-07-08 16:35:52 +0200681 LY_ARRAY_FREE(if_features);
David Sedlák555c7202019-07-04 12:14:12 +0200682 LY_ARRAY_FREE(exts);
David Sedlák986cb412019-07-04 13:10:11 +0200683 LY_ARRAY_FREE(ext_def);
David Sedlákd3983112019-07-12 11:20:56 +0200684 LY_ARRAY_FREE(patter_type.patterns);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200685 LY_ARRAY_FREE(enum_type.enums);
David Sedlák32eee7b2019-07-09 12:38:44 +0200686 free(when_p);
David Sedlákb7296dd2019-07-11 14:58:38 +0200687 free(range_type.range);
David Sedlák438ae432019-07-11 15:36:54 +0200688 free(len_type.length);
David Sedlák555c7202019-07-04 12:14:12 +0200689 st = reset_state(state);
690
691 /* test unique subelem */
692 const char *prefix_value;
Radek Krejcid6b76452019-09-03 17:03:03 +0200693 struct yin_subelement subelems2[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE},
694 {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_UNIQUE}};
David Sedláke6cd89e2019-08-07 12:46:02 +0200695 data = ELEMENT_WRAPPER_START
David Sedlák555c7202019-07-04 12:14:12 +0200696 "<prefix value=\"inv_mod\" />"
697 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
698 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedláke6cd89e2019-08-07 12:46:02 +0200699 ELEMENT_WRAPPER_END;
Michal Vasko63f3d842020-07-08 10:10:14 +0200700 ly_in_new_memory(data, &st->in);
701 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100702 lyxml_ctx_next(st->yin_ctx->xmlctx);
703
704 ret = yin_parse_content(st->yin_ctx, subelems2, 2, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200705 assert_int_equal(ret, LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200706 logbuf_assert("Redefinition of \"text\" sub-element in \"status\" element. Line number 1.");
David Sedlák555c7202019-07-04 12:14:12 +0200707 lydict_remove(st->ctx, prefix_value);
708 lydict_remove(st->ctx, value);
709 st = reset_state(state);
David Sedlák555c7202019-07-04 12:14:12 +0200710
711 /* test first subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200712 data = ELEMENT_WRAPPER_START
David Sedlák555c7202019-07-04 12:14:12 +0200713 "<prefix value=\"inv_mod\" />"
714 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
715 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedláke6cd89e2019-08-07 12:46:02 +0200716 ELEMENT_WRAPPER_END;
Radek Krejcid6b76452019-09-03 17:03:03 +0200717 struct yin_subelement subelems3[2] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_UNIQUE},
718 {LY_STMT_ARG_TEXT, &value, YIN_SUBELEM_FIRST}};
Michal Vasko63f3d842020-07-08 10:10:14 +0200719 ly_in_new_memory(data, &st->in);
720 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100721 lyxml_ctx_next(st->yin_ctx->xmlctx);
722
723 ret = yin_parse_content(st->yin_ctx, subelems3, 2, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200724 assert_int_equal(ret, LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200725 logbuf_assert("Sub-element \"text\" of \"status\" element must be defined as it's first sub-element. Line number 1.");
David Sedlák555c7202019-07-04 12:14:12 +0200726 lydict_remove(st->ctx, prefix_value);
727 st = reset_state(state);
David Sedlák555c7202019-07-04 12:14:12 +0200728
729 /* test mandatory subelem */
David Sedláke6cd89e2019-08-07 12:46:02 +0200730 data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END;
Radek Krejcid6b76452019-09-03 17:03:03 +0200731 struct yin_subelement subelems4[1] = {{LY_STMT_PREFIX, &prefix_value, YIN_SUBELEM_MANDATORY | YIN_SUBELEM_UNIQUE}};
Michal Vasko63f3d842020-07-08 10:10:14 +0200732 ly_in_new_memory(data, &st->in);
733 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100734 lyxml_ctx_next(st->yin_ctx->xmlctx);
735
736 ret = yin_parse_content(st->yin_ctx, subelems4, 1, LY_STMT_STATUS, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200737 assert_int_equal(ret, LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +0200738 logbuf_assert("Missing mandatory sub-element \"prefix\" of \"status\" element. Line number 1.");
David Sedlák555c7202019-07-04 12:14:12 +0200739
740 st->finished_correctly = true;
741}
742
David Sedlák92147b02019-07-09 14:01:01 +0200743static void
David Sedlák4a650532019-07-10 11:55:18 +0200744test_validate_value(void **state)
745{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200746 struct test_parser_yin_state *st = *state;
Michal Vaskob36053d2020-03-26 15:49:30 +0100747 const char *data = ELEMENT_WRAPPER_START ELEMENT_WRAPPER_END;
748
749 /* create some XML context */
Michal Vasko63f3d842020-07-08 10:10:14 +0200750 ly_in_new_memory(data, &st->in);
751 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100752 st->yin_ctx->xmlctx->status = LYXML_ELEM_CONTENT;
753 st->yin_ctx->xmlctx->dynamic = 0;
754
755 st->yin_ctx->xmlctx->value = "#invalid";
756 st->yin_ctx->xmlctx->value_len = 8;
757 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG), LY_EVALID);
Radek Krejci1deb5be2020-08-26 16:43:36 +0200758 logbuf_assert("Invalid identifier character '#' (0x0023). Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100759
760 st->yin_ctx->xmlctx->value = "";
761 st->yin_ctx->xmlctx->value_len = 0;
762 assert_int_equal(yin_validate_value(st->yin_ctx, Y_STR_ARG), LY_SUCCESS);
763
764 st->yin_ctx->xmlctx->value = "pre:b";
765 st->yin_ctx->xmlctx->value_len = 5;
766 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG), LY_EVALID);
767 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG), LY_SUCCESS);
768
769 st->yin_ctx->xmlctx->value = "pre:pre:b";
770 st->yin_ctx->xmlctx->value_len = 9;
771 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG), LY_EVALID);
David Sedlák4a650532019-07-10 11:55:18 +0200772
773 st->finished_correctly = true;
774}
775
David Sedlák32488102019-07-15 17:44:10 +0200776/* helper function to simplify unit test of each element using parse_content function */
777LY_ERR
Radek Krejci3a4889a2020-05-19 17:01:58 +0200778test_element_helper(struct test_parser_yin_state *st, const char *data, void *dest, const char **text, struct lysp_ext_instance **exts)
David Sedlák32488102019-07-15 17:44:10 +0200779{
David Sedlákc5b20842019-08-13 10:18:31 +0200780 const char *name, *prefix;
781 size_t name_len, prefix_len;
David Sedlák32488102019-07-15 17:44:10 +0200782 LY_ERR ret = LY_SUCCESS;
783 struct yin_subelement subelems[71] = {
Radek Krejcid6b76452019-09-03 17:03:03 +0200784 {LY_STMT_ACTION, dest, 0},
785 {LY_STMT_ANYDATA, dest, 0},
786 {LY_STMT_ANYXML, dest, 0},
787 {LY_STMT_ARGUMENT,dest, 0},
788 {LY_STMT_AUGMENT, dest, 0},
789 {LY_STMT_BASE, dest, 0},
790 {LY_STMT_BELONGS_TO, dest, 0},
791 {LY_STMT_BIT, dest, 0},
792 {LY_STMT_CASE, dest, 0},
793 {LY_STMT_CHOICE, dest, 0},
794 {LY_STMT_CONFIG, dest, 0},
795 {LY_STMT_CONTACT, dest, 0},
796 {LY_STMT_CONTAINER, dest, 0},
797 {LY_STMT_DEFAULT, dest, YIN_SUBELEM_UNIQUE},
798 {LY_STMT_DESCRIPTION, dest, 0},
799 {LY_STMT_DEVIATE, dest, 0},
800 {LY_STMT_DEVIATION, dest, 0},
801 {LY_STMT_ENUM, dest, 0},
802 {LY_STMT_ERROR_APP_TAG, dest, YIN_SUBELEM_UNIQUE},
803 {LY_STMT_ERROR_MESSAGE, dest, 0},
804 {LY_STMT_EXTENSION, dest, 0},
805 {LY_STMT_FEATURE, dest, 0},
806 {LY_STMT_FRACTION_DIGITS, dest, 0},
807 {LY_STMT_GROUPING, dest, 0},
808 {LY_STMT_IDENTITY, dest, 0},
809 {LY_STMT_IF_FEATURE, dest, 0},
810 {LY_STMT_IMPORT, dest, 0},
811 {LY_STMT_INCLUDE, dest, 0},
812 {LY_STMT_INPUT, dest, 0},
813 {LY_STMT_KEY, dest, YIN_SUBELEM_UNIQUE},
814 {LY_STMT_LEAF, dest, 0},
815 {LY_STMT_LEAF_LIST, dest, 0},
816 {LY_STMT_LENGTH, dest, 0},
817 {LY_STMT_LIST, dest, 0},
818 {LY_STMT_MANDATORY, dest, 0},
819 {LY_STMT_MAX_ELEMENTS, dest, 0},
820 {LY_STMT_MIN_ELEMENTS, dest, 0},
821 {LY_STMT_MODIFIER, dest, 0},
822 {LY_STMT_MODULE, dest, 0},
823 {LY_STMT_MUST, dest, 0},
824 {LY_STMT_NAMESPACE, dest, YIN_SUBELEM_UNIQUE},
825 {LY_STMT_NOTIFICATION, dest, 0},
826 {LY_STMT_ORDERED_BY, dest, 0},
827 {LY_STMT_ORGANIZATION, dest, 0},
828 {LY_STMT_OUTPUT, dest, 0},
829 {LY_STMT_PATH, dest, 0},
830 {LY_STMT_PATTERN, dest, 0},
831 {LY_STMT_POSITION, dest, 0},
832 {LY_STMT_PREFIX, dest, YIN_SUBELEM_UNIQUE},
833 {LY_STMT_PRESENCE, dest, YIN_SUBELEM_UNIQUE},
834 {LY_STMT_RANGE, dest, 0},
835 {LY_STMT_REFERENCE, dest, 0},
836 {LY_STMT_REFINE, dest, 0},
837 {LY_STMT_REQUIRE_INSTANCE, dest, 0},
838 {LY_STMT_REVISION, dest, 0},
839 {LY_STMT_REVISION_DATE, dest, 0},
840 {LY_STMT_RPC, dest, 0},
841 {LY_STMT_STATUS, dest, 0},
842 {LY_STMT_SUBMODULE, dest, 0},
843 {LY_STMT_TYPE, dest, 0},
844 {LY_STMT_TYPEDEF, dest, 0},
845 {LY_STMT_UNIQUE, dest, 0},
846 {LY_STMT_UNITS, dest, YIN_SUBELEM_UNIQUE},
847 {LY_STMT_USES, dest, 0},
848 {LY_STMT_VALUE, dest, 0},
849 {LY_STMT_WHEN, dest, 0},
850 {LY_STMT_YANG_VERSION, dest, 0},
851 {LY_STMT_YIN_ELEMENT, dest, 0},
852 {LY_STMT_EXTENSION_INSTANCE, dest, 0},
853 {LY_STMT_ARG_TEXT, dest, 0},
854 {LY_STMT_ARG_VALUE, dest, 0}
David Sedlák32488102019-07-15 17:44:10 +0200855 };
Michal Vasko63f3d842020-07-08 10:10:14 +0200856 ly_in_new_memory(data, &st->in);
857 lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx);
Michal Vaskob36053d2020-03-26 15:49:30 +0100858 prefix = st->yin_ctx->xmlctx->prefix;
859 prefix_len = st->yin_ctx->xmlctx->prefix_len;
860 name = st->yin_ctx->xmlctx->name;
861 name_len = st->yin_ctx->xmlctx->name_len;
862 lyxml_ctx_next(st->yin_ctx->xmlctx);
863
864 ret = yin_parse_content(st->yin_ctx, subelems, 71, yin_match_keyword(st->yin_ctx, name, name_len, prefix, prefix_len, LY_STMT_NONE), text, exts);
865
Michal Vasko63f3d842020-07-08 10:10:14 +0200866 /* free parser and input */
Michal Vaskob36053d2020-03-26 15:49:30 +0100867 lyxml_ctx_free(st->yin_ctx->xmlctx);
868 st->yin_ctx->xmlctx = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200869 ly_in_free(st->in, 0);
870 st->in = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200871 return ret;
872}
873
David Sedlákd1144562019-08-06 12:36:14 +0200874#define EXT_SUBELEM "<myext:c-define name=\"MY_MTU\" xmlns:myext=\"urn:example:extensions\"/>"
875
David Sedlák32488102019-07-15 17:44:10 +0200876static void
David Sedlák43801c92019-08-05 15:58:54 +0200877test_enum_elem(void **state)
David Sedlák32488102019-07-15 17:44:10 +0200878{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200879 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +0200880 struct lysp_type type = {};
881 const char *data;
882 data = ELEMENT_WRAPPER_START
883 "<enum name=\"enum-name\">"
884 "<if-feature name=\"feature\" />"
885 "<value value=\"55\" />"
886 "<status value=\"deprecated\" />"
887 "<description><text>desc...</text></description>"
888 "<reference><text>ref...</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +0200889 EXT_SUBELEM
David Sedlák32488102019-07-15 17:44:10 +0200890 "</enum>"
891 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100892 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200893 assert_string_equal(type.enums->name, "enum-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +0200894 assert_string_equal(type.enums->iffeatures[0].str, "feature");
David Sedlák32488102019-07-15 17:44:10 +0200895 assert_int_equal(type.enums->value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200896 assert_true((type.enums->flags & LYS_STATUS_DEPRC) && (type.enums->flags & LYS_SET_VALUE));
David Sedlák32488102019-07-15 17:44:10 +0200897 assert_string_equal(type.enums->dsc, "desc...");
898 assert_string_equal(type.enums->ref, "ref...");
David Sedláke0ef1c62019-09-13 10:05:55 +0200899 assert_string_equal(type.enums->exts->name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +0200900 assert_int_equal(type.enums->exts->insubstmt_index, 0);
901 assert_int_equal(type.enums->exts->insubstmt, LYEXT_SUBSTMT_SELF);
902 lysp_type_free(st->ctx, &type);
903 memset(&type, 0, sizeof type);
904
905 data = ELEMENT_WRAPPER_START
906 "<enum name=\"enum-name\"></enum>"
907 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100908 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200909 assert_string_equal(type.enums->name, "enum-name");
David Sedlák32488102019-07-15 17:44:10 +0200910 lysp_type_free(st->ctx, &type);
911 memset(&type, 0, sizeof type);
912
David Sedlák43801c92019-08-05 15:58:54 +0200913 st->finished_correctly = true;
914}
915
916static void
917test_bit_elem(void **state)
918{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200919 struct test_parser_yin_state *st = *state;
David Sedlák43801c92019-08-05 15:58:54 +0200920 struct lysp_type type = {};
921 const char *data;
922 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200923 "<bit name=\"bit-name\">"
David Sedlák43801c92019-08-05 15:58:54 +0200924 "<if-feature name=\"feature\" />"
925 "<position value=\"55\" />"
926 "<status value=\"deprecated\" />"
927 "<description><text>desc...</text></description>"
928 "<reference><text>ref...</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +0200929 EXT_SUBELEM
David Sedlák43801c92019-08-05 15:58:54 +0200930 "</bit>"
931 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100932 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200933 assert_string_equal(type.bits->name, "bit-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +0200934 assert_string_equal(type.bits->iffeatures[0].str, "feature");
David Sedlák43801c92019-08-05 15:58:54 +0200935 assert_int_equal(type.bits->value, 55);
936 assert_true((type.bits->flags & LYS_STATUS_DEPRC) && (type.bits->flags & LYS_SET_VALUE));
937 assert_string_equal(type.bits->dsc, "desc...");
938 assert_string_equal(type.bits->ref, "ref...");
David Sedláke0ef1c62019-09-13 10:05:55 +0200939 assert_string_equal(type.bits->exts->name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +0200940 assert_int_equal(type.bits->exts->insubstmt_index, 0);
941 assert_int_equal(type.bits->exts->insubstmt, LYEXT_SUBSTMT_SELF);
942 lysp_type_free(st->ctx, &type);
943 memset(&type, 0, sizeof type);
944
945 data = ELEMENT_WRAPPER_START
946 "<bit name=\"bit-name\"> </bit>"
947 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100948 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +0200949 assert_string_equal(type.bits->name, "bit-name");
David Sedlák43801c92019-08-05 15:58:54 +0200950 lysp_type_free(st->ctx, &type);
951 memset(&type, 0, sizeof type);
952
David Sedlák32488102019-07-15 17:44:10 +0200953 st->finished_correctly = true;
954}
955
956static void
957test_meta_elem(void **state)
958{
Radek Krejci3a4889a2020-05-19 17:01:58 +0200959 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +0200960 char *value = NULL;
961 const char *data;
David Sedlákd1144562019-08-06 12:36:14 +0200962 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200963
964 /* organization element */
965 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200966 "<organization><text>organization...</text>" EXT_SUBELEM EXT_SUBELEM "</organization>"
David Sedlák32488102019-07-15 17:44:10 +0200967 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100968 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_SUCCESS);
David Sedláke0ef1c62019-09-13 10:05:55 +0200969 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +0200970 assert_int_equal(exts[0].insubstmt_index, 0);
971 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ORGANIZATION);
David Sedláke0ef1c62019-09-13 10:05:55 +0200972 assert_string_equal(exts[1].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +0200973 assert_int_equal(exts[1].insubstmt_index, 0);
974 assert_int_equal(exts[1].insubstmt, LYEXT_SUBSTMT_ORGANIZATION);
David Sedlák32488102019-07-15 17:44:10 +0200975 assert_string_equal(value, "organization...");
976 FREE_STRING(st->ctx, value);
David Sedlákd1144562019-08-06 12:36:14 +0200977 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +0200978 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +0200979 exts = NULL;
980
David Sedlák32488102019-07-15 17:44:10 +0200981 /* contact element */
982 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200983 "<contact><text>contact...</text>" EXT_SUBELEM "</contact>"
David Sedlák32488102019-07-15 17:44:10 +0200984 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100985 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_SUCCESS);
David Sedláke0ef1c62019-09-13 10:05:55 +0200986 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +0200987 assert_int_equal(exts[0].insubstmt_index, 0);
988 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_CONTACT);
David Sedlák32488102019-07-15 17:44:10 +0200989 assert_string_equal(value, "contact...");
990 FREE_STRING(st->ctx, value);
David Sedlákd1144562019-08-06 12:36:14 +0200991 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
992 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200993 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +0200994
David Sedlák32488102019-07-15 17:44:10 +0200995 /* description element */
996 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +0200997 "<description><text>description...</text>" EXT_SUBELEM "</description>"
David Sedlák32488102019-07-15 17:44:10 +0200998 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +0100999 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001000 assert_string_equal(value, "description...");
David Sedláke0ef1c62019-09-13 10:05:55 +02001001 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001002 assert_int_equal(exts[0].insubstmt_index, 0);
1003 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_DESCRIPTION);
David Sedlák32488102019-07-15 17:44:10 +02001004 FREE_STRING(st->ctx, value);
1005 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001006 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1007 exts = NULL;
1008
David Sedlák32488102019-07-15 17:44:10 +02001009 /* reference element */
1010 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +02001011 "<reference><text>reference...</text>" EXT_SUBELEM "</reference>"
David Sedlák32488102019-07-15 17:44:10 +02001012 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001013 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001014 assert_string_equal(value, "reference...");
David Sedláke0ef1c62019-09-13 10:05:55 +02001015 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001016 assert_int_equal(exts[0].insubstmt_index, 0);
1017 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_REFERENCE);
David Sedlák32488102019-07-15 17:44:10 +02001018 FREE_STRING(st->ctx, value);
1019 value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001020 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1021 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001022
David Sedlákdf2a9732019-08-07 13:23:16 +02001023 /* reference element */
1024 data = ELEMENT_WRAPPER_START
1025 "<reference invalid=\"text\"><text>reference...</text>""</reference>"
1026 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001027 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001028 logbuf_assert("Unexpected attribute \"invalid\" of \"reference\" element. Line number 1.");
David Sedlákdf2a9732019-08-07 13:23:16 +02001029 FREE_STRING(st->ctx, value);
1030 value = NULL;
1031 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1032 exts = NULL;
1033
David Sedlák32488102019-07-15 17:44:10 +02001034 /* missing text subelement */
1035 data = ELEMENT_WRAPPER_START
1036 "<reference>reference...</reference>"
1037 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001038 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001039 logbuf_assert("Missing mandatory sub-element \"text\" of \"reference\" element. Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001040
David Sedlákd1144562019-08-06 12:36:14 +02001041 /* reference element */
1042 data = ELEMENT_WRAPPER_START
1043 "<reference>" EXT_SUBELEM "<text>reference...</text></reference>"
1044 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001045 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001046 logbuf_assert("Sub-element \"text\" of \"reference\" element must be defined as it's first sub-element. Line number 1.");
David Sedlákd1144562019-08-06 12:36:14 +02001047 FREE_STRING(st->ctx, value);
1048 value = NULL;
1049 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1050 exts = NULL;
1051
David Sedlák32488102019-07-15 17:44:10 +02001052 st->finished_correctly = true;
1053}
1054
1055static void
1056test_import_elem(void **state)
1057{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001058 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001059 const char *data;
David Sedlák298ff6d2019-07-26 14:29:03 +02001060 struct lysp_import *imports = NULL;
1061 struct import_meta imp_meta = {"prefix", &imports};
David Sedlák32488102019-07-15 17:44:10 +02001062
1063 /* max subelems */
1064 data = ELEMENT_WRAPPER_START
1065 "<import module=\"a\">"
David Sedlákd1144562019-08-06 12:36:14 +02001066 EXT_SUBELEM
David Sedlák32488102019-07-15 17:44:10 +02001067 "<prefix value=\"a_mod\"/>"
1068 "<revision-date date=\"2015-01-01\"></revision-date>"
1069 "<description><text>import description</text></description>"
1070 "<reference><text>import reference</text></reference>"
1071 "</import>"
1072 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001073 assert_int_equal(test_element_helper(st, data, &imp_meta, NULL, NULL), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02001074 assert_string_equal(imports->name, "a");
1075 assert_string_equal(imports->prefix, "a_mod");
1076 assert_string_equal(imports->rev, "2015-01-01");
1077 assert_string_equal(imports->dsc, "import description");
1078 assert_string_equal(imports->ref, "import reference");
David Sedláke0ef1c62019-09-13 10:05:55 +02001079 assert_string_equal(imports->exts->name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001080 assert_int_equal(imports->exts->insubstmt, LYEXT_SUBSTMT_SELF);
1081 assert_int_equal(imports->exts->insubstmt_index, 0);
David Sedlák298ff6d2019-07-26 14:29:03 +02001082 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1083 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001084
1085 /* min subelems */
David Sedlák32488102019-07-15 17:44:10 +02001086 data = ELEMENT_WRAPPER_START
1087 "<import module=\"a\">"
1088 "<prefix value=\"a_mod\"/>"
1089 "</import>"
1090 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001091 assert_int_equal(test_element_helper(st, data, &imp_meta, NULL, NULL), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02001092 assert_string_equal(imports->prefix, "a_mod");
1093 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1094 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001095
1096 /* invalid (missing prefix) */
David Sedlák298ff6d2019-07-26 14:29:03 +02001097 data = ELEMENT_WRAPPER_START "<import module=\"a\"></import>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001098 assert_int_equal(test_element_helper(st, data, &imp_meta, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001099 logbuf_assert("Missing mandatory sub-element \"prefix\" of \"import\" element. Line number 1.");
David Sedlák298ff6d2019-07-26 14:29:03 +02001100 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1101 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001102
1103 /* invalid reused prefix */
David Sedlák32488102019-07-15 17:44:10 +02001104 data = ELEMENT_WRAPPER_START
1105 "<import module=\"a\">"
David Sedlák298ff6d2019-07-26 14:29:03 +02001106 "<prefix value=\"prefix\"/>"
David Sedlák32488102019-07-15 17:44:10 +02001107 "</import>"
1108 "<import module=\"a\">"
David Sedlák298ff6d2019-07-26 14:29:03 +02001109 "<prefix value=\"prefix\"/>"
David Sedlák32488102019-07-15 17:44:10 +02001110 "</import>"
1111 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001112 assert_int_equal(test_element_helper(st, data, &imp_meta, NULL, NULL), LY_EVALID);
David Sedlák298ff6d2019-07-26 14:29:03 +02001113 logbuf_assert("Prefix \"prefix\" already used as module prefix. Line number 1.");
1114 FREE_ARRAY(st->ctx, imports, lysp_import_free);
1115 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001116
1117 st->finished_correctly = true;
1118}
1119
1120static void
1121test_status_elem(void **state)
1122{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001123 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001124 const char *data;
1125 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001126 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001127
1128 /* test valid values */
1129 data = ELEMENT_WRAPPER_START "<status value=\"current\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001130 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001131 assert_true(flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +02001132
1133 data = ELEMENT_WRAPPER_START "<status value=\"deprecated\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001134 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001135 assert_true(flags & LYS_STATUS_DEPRC);
David Sedlák32488102019-07-15 17:44:10 +02001136
David Sedlákd1144562019-08-06 12:36:14 +02001137 data = ELEMENT_WRAPPER_START "<status value=\"obsolete\">"EXT_SUBELEM"</status>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001138 assert_int_equal(test_element_helper(st, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001139 assert_true(flags & LYS_STATUS_OBSLT);
David Sedláke0ef1c62019-09-13 10:05:55 +02001140 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001141 assert_int_equal(exts[0].insubstmt_index, 0);
1142 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_STATUS);
1143 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1144 exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001145
1146 /* test invalid value */
1147 data = ELEMENT_WRAPPER_START "<status value=\"invalid\"></status>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001148 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001149 logbuf_assert("Invalid value \"invalid\" of \"value\" attribute in \"status\" element. Valid values are \"current\", \"deprecated\" and \"obsolete\". Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001150 st->finished_correctly = true;
1151}
1152
1153static void
1154test_ext_elem(void **state)
1155{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001156 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001157 const char *data;
1158 struct lysp_ext *ext = NULL;
1159
1160 /* max subelems */
1161 data = ELEMENT_WRAPPER_START
1162 "<extension name=\"ext_name\">"
1163 "<argument name=\"arg\"></argument>"
1164 "<status value=\"current\"/>"
1165 "<description><text>ext_desc</text></description>"
1166 "<reference><text>ext_ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001167 EXT_SUBELEM
David Sedlák32488102019-07-15 17:44:10 +02001168 "</extension>"
1169 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001170 assert_int_equal(test_element_helper(st, data, &ext, NULL, NULL), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001171 assert_string_equal(ext->name, "ext_name");
1172 assert_string_equal(ext->argument, "arg");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001173 assert_true(ext->flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +02001174 assert_string_equal(ext->dsc, "ext_desc");
1175 assert_string_equal(ext->ref, "ext_ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02001176 assert_string_equal(ext->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001177 assert_int_equal(ext->exts[0].insubstmt_index, 0);
1178 assert_int_equal(ext->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák32488102019-07-15 17:44:10 +02001179 lysp_ext_free(st->ctx, ext);
1180 LY_ARRAY_FREE(ext);
1181 ext = NULL;
1182
1183 /* min subelems */
1184 data = ELEMENT_WRAPPER_START "<extension name=\"ext_name\"></extension>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001185 assert_int_equal(test_element_helper(st, data, &ext, NULL, NULL), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001186 assert_string_equal(ext->name, "ext_name");
1187 lysp_ext_free(st->ctx, ext);
1188 LY_ARRAY_FREE(ext);
1189 ext = NULL;
1190
1191 st->finished_correctly = true;
1192}
1193
1194static void
1195test_yin_element_elem(void **state)
1196{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001197 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001198 const char *data;
1199 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001200 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001201
1202 data = ELEMENT_WRAPPER_START "<yin-element value=\"true\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001203 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001204 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +02001205
David Sedlákd1144562019-08-06 12:36:14 +02001206 data = ELEMENT_WRAPPER_START "<yin-element value=\"false\">" EXT_SUBELEM "</yin-element>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001207 assert_int_equal(test_element_helper(st, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001208 assert_true(flags & LYS_YINELEM_TRUE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001209 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001210 assert_int_equal(exts[0].insubstmt_index, 0);
1211 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_YINELEM);
1212 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001213
1214 data = ELEMENT_WRAPPER_START "<yin-element value=\"invalid\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001215 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_EVALID);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001216 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák26ea1432019-08-14 13:42:23 +02001217 logbuf_assert("Invalid value \"invalid\" of \"value\" attribute in \"yin-element\" element. Valid values are \"true\" and \"false\". Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001218 st->finished_correctly = true;
1219}
1220
1221static void
1222test_yangversion_elem(void **state)
1223{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001224 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001225 const char *data;
1226 uint8_t version = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001227 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001228
1229 /* valid values */
Radek Krejci96e48da2020-09-04 13:18:06 +02001230 data = ELEMENT_WRAPPER_START "<yang-version value=\"1\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001231 assert_int_equal(test_element_helper(st, data, &version, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001232 assert_true(version & LYS_VERSION_1_0);
David Sedlák32488102019-07-15 17:44:10 +02001233 assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_0);
1234
David Sedlákd1144562019-08-06 12:36:14 +02001235 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.1\">" EXT_SUBELEM "</yang-version>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001236 assert_int_equal(test_element_helper(st, data, &version, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001237 assert_true(version & LYS_VERSION_1_1);
David Sedlák32488102019-07-15 17:44:10 +02001238 assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_1);
David Sedláke0ef1c62019-09-13 10:05:55 +02001239 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001240 assert_int_equal(exts[0].insubstmt_index, 0);
1241 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_VERSION);
1242 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001243
1244 /* invalid value */
1245 data = ELEMENT_WRAPPER_START "<yang-version value=\"version\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001246 assert_int_equal(test_element_helper(st, data, &version, NULL, NULL), LY_EVALID);
Radek Krejci96e48da2020-09-04 13:18:06 +02001247 logbuf_assert("Invalid value \"version\" of \"value\" attribute in \"yang-version\" element. Valid values are \"1\" and \"1.1\". Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001248
1249 st->finished_correctly = true;
1250}
1251
1252static void
1253test_mandatory_elem(void **state)
1254{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001255 struct test_parser_yin_state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +02001256 const char *data;
1257 uint16_t man = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001258 struct lysp_ext_instance *exts = NULL;
David Sedlák32488102019-07-15 17:44:10 +02001259
1260 /* valid values */
1261 data = ELEMENT_WRAPPER_START "<mandatory value=\"true\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001262 assert_int_equal(test_element_helper(st, data, &man, NULL, NULL), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001263 assert_int_equal(man, LYS_MAND_TRUE);
1264 man = 0;
1265
David Sedlákd1144562019-08-06 12:36:14 +02001266 data = ELEMENT_WRAPPER_START "<mandatory value=\"false\">" EXT_SUBELEM "</mandatory>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001267 assert_int_equal(test_element_helper(st, data, &man, NULL, &exts), LY_SUCCESS);
David Sedlák32488102019-07-15 17:44:10 +02001268 assert_int_equal(man, LYS_MAND_FALSE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001269 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001270 assert_int_equal(exts[0].insubstmt_index, 0);
1271 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_MANDATORY);
1272 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák32488102019-07-15 17:44:10 +02001273
1274 data = ELEMENT_WRAPPER_START "<mandatory value=\"invalid\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001275 assert_int_equal(test_element_helper(st, data, &man, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001276 logbuf_assert("Invalid value \"invalid\" of \"value\" attribute in \"mandatory\" element. Valid values are \"true\" and \"false\". Line number 1.");
David Sedlák32488102019-07-15 17:44:10 +02001277
1278 st->finished_correctly = true;
1279}
1280
David Sedlák8e7bda82019-07-16 17:57:50 +02001281static void
1282test_argument_elem(void **state)
1283{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001284 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001285 const char *data;
1286 uint16_t flags = 0;
1287 const char *arg;
1288 struct yin_argument_meta arg_meta = {&flags, &arg};
David Sedlákd1144562019-08-06 12:36:14 +02001289 struct lysp_ext_instance *exts = NULL;
1290
David Sedlák8e7bda82019-07-16 17:57:50 +02001291 /* max subelems */
1292 data = ELEMENT_WRAPPER_START
1293 "<argument name=\"arg-name\">"
1294 "<yin-element value=\"true\" />"
David Sedlákd1144562019-08-06 12:36:14 +02001295 EXT_SUBELEM
David Sedlák8e7bda82019-07-16 17:57:50 +02001296 "</argument>"
1297 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001298 assert_int_equal(test_element_helper(st, data, &arg_meta, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001299 assert_string_equal(arg, "arg-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001300 assert_true(flags & LYS_YINELEM_TRUE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001301 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001302 assert_int_equal(exts[0].insubstmt_index, 0);
1303 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ARGUMENT);
1304 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1305 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001306 flags = 0;
1307 FREE_STRING(st->ctx, arg);
1308 arg = NULL;
1309
1310 /* min subelems */
1311 data = ELEMENT_WRAPPER_START
1312 "<argument name=\"arg\">"
1313 "</argument>"
1314 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001315 assert_int_equal(test_element_helper(st, data, &arg_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001316 assert_string_equal(arg, "arg");
1317 assert_true(flags == 0);
1318 FREE_STRING(st->ctx, arg);
1319
1320 st->finished_correctly = true;
1321}
1322
1323static void
1324test_base_elem(void **state)
1325{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001326 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001327 const char *data;
1328 const char **bases = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001329 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001330 struct lysp_type type = {};
1331
1332 /* as identity subelement */
1333 data = "<identity xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlákd1144562019-08-06 12:36:14 +02001334 "<base name=\"base-name\">"
1335 EXT_SUBELEM
1336 "</base>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001337 "</identity>";
Michal Vaskob36053d2020-03-26 15:49:30 +01001338 assert_int_equal(test_element_helper(st, data, &bases, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001339 assert_string_equal(*bases, "base-name");
David Sedláke0ef1c62019-09-13 10:05:55 +02001340 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001341 assert_int_equal(exts[0].insubstmt_index, 0);
1342 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_BASE);
1343 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1344 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001345 FREE_STRING(st->ctx, *bases);
1346 LY_ARRAY_FREE(bases);
1347
1348 /* as type subelement */
1349 data = "<type xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
David Sedlákd1144562019-08-06 12:36:14 +02001350 "<base name=\"base-name\">"
1351 EXT_SUBELEM
1352 "</base>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001353 "</type>";
Michal Vaskob36053d2020-03-26 15:49:30 +01001354 assert_int_equal(test_element_helper(st, data, &type, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001355 assert_string_equal(*type.bases, "base-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001356 assert_true(type.flags & LYS_SET_BASE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001357 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001358 assert_int_equal(exts[0].insubstmt_index, 0);
1359 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_BASE);
1360 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1361 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001362 FREE_STRING(st->ctx, *type.bases);
1363 LY_ARRAY_FREE(type.bases);
1364
1365 st->finished_correctly = true;
1366}
1367
1368static void
1369test_belongsto_elem(void **state)
1370{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001371 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001372 const char *data;
1373 struct lysp_submodule submod;
David Sedlákd1144562019-08-06 12:36:14 +02001374 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001375
1376 data = ELEMENT_WRAPPER_START
David Sedlákd1144562019-08-06 12:36:14 +02001377 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/>"EXT_SUBELEM"</belongs-to>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001378 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001379 assert_int_equal(test_element_helper(st, data, &submod, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001380 assert_string_equal(submod.belongsto, "module-name");
1381 assert_string_equal(submod.prefix, "pref");
David Sedláke0ef1c62019-09-13 10:05:55 +02001382 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001383 assert_int_equal(exts[0].insubstmt_index, 0);
1384 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_BELONGSTO);
1385 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1386 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001387 FREE_STRING(st->ctx, submod.belongsto);
1388 FREE_STRING(st->ctx, submod.prefix);
1389
1390 data = ELEMENT_WRAPPER_START "<belongs-to module=\"module-name\"></belongs-to>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001391 assert_int_equal(test_element_helper(st, data, &submod, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001392 logbuf_assert("Missing mandatory sub-element \"prefix\" of \"belongs-to\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001393 FREE_STRING(st->ctx, submod.belongsto);
1394
1395 st->finished_correctly = true;
1396}
1397
1398static void
1399test_config_elem(void **state)
1400{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001401 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001402 const char *data;
1403 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02001404 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001405
David Sedlákd1144562019-08-06 12:36:14 +02001406 data = ELEMENT_WRAPPER_START "<config value=\"true\">" EXT_SUBELEM "</config>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001407 assert_int_equal(test_element_helper(st, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001408 assert_true(flags & LYS_CONFIG_W);
David Sedláke0ef1c62019-09-13 10:05:55 +02001409 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001410 assert_int_equal(exts[0].insubstmt_index, 0);
1411 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_CONFIG);
1412 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1413 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001414 flags = 0;
1415
1416 data = ELEMENT_WRAPPER_START "<config value=\"false\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001417 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001418 assert_true(flags & LYS_CONFIG_R);
David Sedlák8e7bda82019-07-16 17:57:50 +02001419 flags = 0;
1420
1421 data = ELEMENT_WRAPPER_START "<config value=\"invalid\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001422 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001423 logbuf_assert("Invalid value \"invalid\" of \"value\" attribute in \"config\" element. Valid values are \"true\" and \"false\". Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001424
1425 st->finished_correctly = true;
1426}
1427
1428static void
1429test_default_elem(void **state)
1430{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001431 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001432 const char *data;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001433 struct lysp_qname val = {0};
David Sedlákd1144562019-08-06 12:36:14 +02001434 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001435
David Sedlákd1144562019-08-06 12:36:14 +02001436 data = ELEMENT_WRAPPER_START "<default value=\"defaul-value\">"EXT_SUBELEM"</default>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001437 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001438 assert_string_equal(val.str, "defaul-value");
David Sedláke0ef1c62019-09-13 10:05:55 +02001439 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001440 assert_int_equal(exts[0].insubstmt_index, 0);
1441 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_DEFAULT);
1442 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1443 exts = NULL;
Michal Vasko7f45cf22020-10-01 12:49:44 +02001444 FREE_STRING(st->ctx, val.str);
1445 val.str = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001446
1447 data = ELEMENT_WRAPPER_START "<default/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001448 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001449 logbuf_assert("Missing mandatory attribute value of default element. Line number 1.");
1450
1451 st->finished_correctly = true;
1452}
1453
1454static void
1455test_err_app_tag_elem(void **state)
1456{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001457 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001458 const char *data;
1459 const char *val = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001460 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001461
David Sedlákd1144562019-08-06 12:36:14 +02001462 data = ELEMENT_WRAPPER_START "<error-app-tag value=\"val\">"EXT_SUBELEM"</error-app-tag>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001463 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001464 assert_string_equal(val, "val");
David Sedláke0ef1c62019-09-13 10:05:55 +02001465 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001466 assert_int_equal(exts[0].insubstmt_index, 0);
1467 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ERRTAG);
1468 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1469 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001470 FREE_STRING(st->ctx, val);
1471 val = NULL;
1472
1473 data = ELEMENT_WRAPPER_START "<error-app-tag/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001474 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001475 logbuf_assert("Missing mandatory attribute value of error-app-tag element. Line number 1.");
1476
1477 st->finished_correctly = true;
1478}
1479
1480static void
1481test_err_msg_elem(void **state)
1482{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001483 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001484 const char *data;
1485 const char *val = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001486 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001487
David Sedlákd1144562019-08-06 12:36:14 +02001488 data = ELEMENT_WRAPPER_START "<error-message><value>val</value>"EXT_SUBELEM"</error-message>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001489 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001490 assert_string_equal(val, "val");
David Sedláke0ef1c62019-09-13 10:05:55 +02001491 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001492 assert_int_equal(exts[0].insubstmt_index, 0);
1493 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ERRMSG);
1494 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1495 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001496 FREE_STRING(st->ctx, val);
1497
1498 data = ELEMENT_WRAPPER_START "<error-message></error-message>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001499 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001500 logbuf_assert("Missing mandatory sub-element \"value\" of \"error-message\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001501
David Sedlákdf2a9732019-08-07 13:23:16 +02001502 data = ELEMENT_WRAPPER_START "<error-message invalid=\"text\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001503 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001504 logbuf_assert("Unexpected attribute \"invalid\" of \"error-message\" element. Line number 1.");
David Sedlákdf2a9732019-08-07 13:23:16 +02001505
David Sedlák8e7bda82019-07-16 17:57:50 +02001506 st->finished_correctly = true;
1507}
1508
1509static void
1510test_fracdigits_elem(void **state)
1511{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001512 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001513 const char *data;
1514 struct lysp_type type = {};
1515
1516 /* valid value */
David Sedlákd1144562019-08-06 12:36:14 +02001517 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"10\">"EXT_SUBELEM"</fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001518 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedláke0ef1c62019-09-13 10:05:55 +02001519 assert_string_equal(type.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001520 assert_int_equal(type.exts[0].insubstmt_index, 0);
1521 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_FRACDIGITS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001522 assert_int_equal(type.fraction_digits, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001523 assert_true(type.flags & LYS_SET_FRDIGITS);
David Sedlákd1144562019-08-06 12:36:14 +02001524 FREE_ARRAY(st->ctx, type.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001525
1526 /* invalid values */
1527 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"-1\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001528 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001529 logbuf_assert("Invalid value \"-1\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001530
1531 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"02\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001532 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001533 logbuf_assert("Invalid value \"02\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001534
1535 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"1p\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001536 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001537 logbuf_assert("Invalid value \"1p\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001538
1539 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"19\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001540 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001541 logbuf_assert("Invalid value \"19\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001542
1543 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"999999999999999999\"></fraction-digits>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001544 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001545 logbuf_assert("Invalid value \"999999999999999999\" of \"value\" attribute in \"fraction-digits\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001546
1547 st->finished_correctly = true;
1548}
1549
1550static void
1551test_iffeature_elem(void **state)
1552{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001553 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001554 const char *data;
1555 const char **iffeatures = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001556 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001557
David Sedlákd1144562019-08-06 12:36:14 +02001558 data = ELEMENT_WRAPPER_START "<if-feature name=\"local-storage\">"EXT_SUBELEM"</if-feature>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001559 assert_int_equal(test_element_helper(st, data, &iffeatures, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001560 assert_string_equal(*iffeatures, "local-storage");
David Sedláke0ef1c62019-09-13 10:05:55 +02001561 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001562 assert_int_equal(exts[0].insubstmt_index, 0);
1563 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_IFFEATURE);
1564 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1565 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001566 FREE_STRING(st->ctx, *iffeatures);
1567 LY_ARRAY_FREE(iffeatures);
1568 iffeatures = NULL;
1569
1570 data = ELEMENT_WRAPPER_START "<if-feature/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001571 assert_int_equal(test_element_helper(st, data, &iffeatures, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001572 logbuf_assert("Missing mandatory attribute name of if-feature element. Line number 1.");
1573 LY_ARRAY_FREE(iffeatures);
1574 iffeatures = NULL;
1575
1576 st->finished_correctly = true;
1577}
1578
1579static void
1580test_length_elem(void **state)
1581{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001582 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001583 const char *data;
1584 struct lysp_type type = {};
1585
1586 /* max subelems */
1587 data = ELEMENT_WRAPPER_START
1588 "<length value=\"length-str\">"
1589 "<error-message><value>err-msg</value></error-message>"
1590 "<error-app-tag value=\"err-app-tag\"/>"
1591 "<description><text>desc</text></description>"
1592 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001593 EXT_SUBELEM
David Sedlák8e7bda82019-07-16 17:57:50 +02001594 "</length>"
1595 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001596 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001597 assert_string_equal(type.length->arg.str, "length-str");
David Sedlák8e7bda82019-07-16 17:57:50 +02001598 assert_string_equal(type.length->emsg, "err-msg");
1599 assert_string_equal(type.length->eapptag, "err-app-tag");
1600 assert_string_equal(type.length->dsc, "desc");
1601 assert_string_equal(type.length->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001602 assert_true(type.flags & LYS_SET_LENGTH);
David Sedláke0ef1c62019-09-13 10:05:55 +02001603 assert_string_equal(type.length->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001604 assert_int_equal(type.length->exts[0].insubstmt_index, 0);
1605 assert_int_equal(type.length->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8e7bda82019-07-16 17:57:50 +02001606 lysp_type_free(st->ctx, &type);
1607 memset(&type, 0, sizeof(type));
1608
1609 /* min subelems */
1610 data = ELEMENT_WRAPPER_START
1611 "<length value=\"length-str\">"
1612 "</length>"
1613 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001614 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001615 assert_string_equal(type.length->arg.str, "length-str");
David Sedlák8e7bda82019-07-16 17:57:50 +02001616 lysp_type_free(st->ctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001617 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001618 memset(&type, 0, sizeof(type));
1619
1620 data = ELEMENT_WRAPPER_START "<length></length>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001621 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001622 logbuf_assert("Missing mandatory attribute value of length element. Line number 1.");
1623 lysp_type_free(st->ctx, &type);
1624 memset(&type, 0, sizeof(type));
1625
1626 st->finished_correctly = true;
1627}
1628
1629static void
1630test_modifier_elem(void **state)
1631{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001632 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001633 const char *data;
Radek Krejci011e4aa2020-09-04 15:22:31 +02001634 const char *pat;
David Sedlákd1144562019-08-06 12:36:14 +02001635 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001636
Radek Krejci011e4aa2020-09-04 15:22:31 +02001637 assert_int_equal(LY_SUCCESS, lydict_insert(st->ctx, "\006pattern", 8, &pat));
David Sedlákd1144562019-08-06 12:36:14 +02001638 data = ELEMENT_WRAPPER_START "<modifier value=\"invert-match\">" EXT_SUBELEM "</modifier>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001639 assert_int_equal(test_element_helper(st, data, &pat, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001640 assert_string_equal(pat, "\x015pattern");
David Sedláke0ef1c62019-09-13 10:05:55 +02001641 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001642 assert_int_equal(exts[0].insubstmt_index, 0);
1643 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_MODIFIER);
1644 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1645 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001646 FREE_STRING(st->ctx, pat);
1647
Radek Krejci011e4aa2020-09-04 15:22:31 +02001648 assert_int_equal(LY_SUCCESS, lydict_insert(st->ctx, "\006pattern", 8, &pat));
David Sedlák8e7bda82019-07-16 17:57:50 +02001649 data = ELEMENT_WRAPPER_START "<modifier value=\"invert\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001650 assert_int_equal(test_element_helper(st, data, &pat, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02001651 logbuf_assert("Invalid value \"invert\" of \"value\" attribute in \"modifier\" element. Only valid value is \"invert-match\". Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001652 FREE_STRING(st->ctx, pat);
1653
1654 st->finished_correctly = true;
1655}
1656
1657static void
1658test_namespace_elem(void **state)
1659{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001660 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001661 const char *data;
1662 const char *ns;
David Sedlákd1144562019-08-06 12:36:14 +02001663 struct lysp_ext_instance *exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001664
David Sedlákd1144562019-08-06 12:36:14 +02001665 data = ELEMENT_WRAPPER_START "<namespace uri=\"ns\">" EXT_SUBELEM "</namespace>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001666 assert_int_equal(test_element_helper(st, data, &ns, NULL, &exts), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001667 assert_string_equal(ns, "ns");
David Sedláke0ef1c62019-09-13 10:05:55 +02001668 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001669 assert_int_equal(exts[0].insubstmt_index, 0);
1670 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_NAMESPACE);
1671 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1672 exts = NULL;
David Sedlák8e7bda82019-07-16 17:57:50 +02001673 FREE_STRING(st->ctx, ns);
1674
1675 data = ELEMENT_WRAPPER_START "<namespace/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001676 assert_int_equal(test_element_helper(st, data, &ns, NULL, NULL), LY_EVALID);
David Sedlák8e7bda82019-07-16 17:57:50 +02001677 logbuf_assert("Missing mandatory attribute uri of namespace element. Line number 1.");
1678
1679 st->finished_correctly = true;
1680}
1681
1682static void
David Sedlák8e7bda82019-07-16 17:57:50 +02001683test_pattern_elem(void **state)
1684{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001685 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001686 const char *data;
1687 struct lysp_type type = {};
1688
1689 /* max subelems */
1690 data = ELEMENT_WRAPPER_START
1691 "<pattern value=\"super_pattern\">"
1692 "<modifier value=\"invert-match\"/>"
1693 "<error-message><value>err-msg-value</value></error-message>"
1694 "<error-app-tag value=\"err-app-tag-value\"/>"
David Sedlák169cc522019-08-15 13:23:45 +02001695 "<description><text>&quot;pattern-desc&quot;</text></description>"
David Sedlák8e7bda82019-07-16 17:57:50 +02001696 "<reference><text>pattern-ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001697 EXT_SUBELEM
David Sedlák8e7bda82019-07-16 17:57:50 +02001698 "</pattern>"
1699 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001700 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001701 assert_true(type.flags & LYS_SET_PATTERN);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001702 assert_string_equal(type.patterns->arg.str, "\x015super_pattern");
David Sedlák169cc522019-08-15 13:23:45 +02001703 assert_string_equal(type.patterns->dsc, "\"pattern-desc\"");
David Sedlák8e7bda82019-07-16 17:57:50 +02001704 assert_string_equal(type.patterns->eapptag, "err-app-tag-value");
1705 assert_string_equal(type.patterns->emsg, "err-msg-value");
David Sedlák8e7bda82019-07-16 17:57:50 +02001706 assert_string_equal(type.patterns->ref, "pattern-ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02001707 assert_string_equal(type.patterns->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001708 assert_int_equal(type.patterns->exts[0].insubstmt_index, 0);
1709 assert_int_equal(type.patterns->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8e7bda82019-07-16 17:57:50 +02001710 lysp_type_free(st->ctx, &type);
1711 memset(&type, 0, sizeof(type));
1712
1713 /* min subelems */
1714 data = ELEMENT_WRAPPER_START "<pattern value=\"pattern\"> </pattern>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001715 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001716 assert_string_equal(type.patterns->arg.str, "\x006pattern");
David Sedlák8e7bda82019-07-16 17:57:50 +02001717 lysp_type_free(st->ctx, &type);
1718 memset(&type, 0, sizeof(type));
1719
1720 st->finished_correctly = true;
1721}
1722
1723static void
1724test_value_position_elem(void **state)
1725{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001726 struct test_parser_yin_state *st = *state;
David Sedlák8e7bda82019-07-16 17:57:50 +02001727 const char *data;
1728 struct lysp_type_enum en = {};
1729
1730 /* valid values */
David Sedlákd1144562019-08-06 12:36:14 +02001731 data = ELEMENT_WRAPPER_START "<value value=\"55\">" EXT_SUBELEM "</value>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001732 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001733 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001734 assert_true(en.flags & LYS_SET_VALUE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001735 assert_string_equal(en.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001736 assert_int_equal(en.exts[0].insubstmt_index, 0);
1737 assert_int_equal(en.exts[0].insubstmt, LYEXT_SUBSTMT_VALUE);
1738 FREE_ARRAY(st->ctx, en.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001739 memset(&en, 0, sizeof(en));
1740
1741 data = ELEMENT_WRAPPER_START "<value value=\"-55\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001742 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001743 assert_int_equal(en.value, -55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001744 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001745 memset(&en, 0, sizeof(en));
1746
1747 data = ELEMENT_WRAPPER_START "<value value=\"0\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001748 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001749 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001750 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001751 memset(&en, 0, sizeof(en));
1752
1753 data = ELEMENT_WRAPPER_START "<value value=\"-0\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001754 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001755 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001756 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001757 memset(&en, 0, sizeof(en));
1758
1759 /* valid positions */
David Sedlák8d552d62019-08-06 15:29:05 +02001760 data = ELEMENT_WRAPPER_START "<position value=\"55\">" EXT_SUBELEM "</position>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001761 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001762 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001763 assert_true(en.flags & LYS_SET_VALUE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001764 assert_string_equal(en.exts[0].name, "urn:example:extensions:c-define");
David Sedlák8d552d62019-08-06 15:29:05 +02001765 assert_int_equal(en.exts[0].insubstmt_index, 0);
1766 assert_int_equal(en.exts[0].insubstmt, LYEXT_SUBSTMT_POSITION);
1767 FREE_ARRAY(st->ctx, en.exts, lysp_ext_instance_free);
David Sedlák8e7bda82019-07-16 17:57:50 +02001768 memset(&en, 0, sizeof(en));
1769
1770 data = ELEMENT_WRAPPER_START "<position value=\"0\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001771 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_SUCCESS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001772 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001773 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001774 memset(&en, 0, sizeof(en));
1775
1776 /* invalid values */
1777 data = ELEMENT_WRAPPER_START "<value value=\"99999999999999999999999\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001778 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001779 logbuf_assert("Invalid value \"99999999999999999999999\" of \"value\" attribute in \"value\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001780
1781 data = ELEMENT_WRAPPER_START "<value value=\"1k\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001782 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001783 logbuf_assert("Invalid value \"1k\" of \"value\" attribute in \"value\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001784
David Sedlák69f01612019-07-17 11:41:08 +02001785 data = ELEMENT_WRAPPER_START "<value value=\"\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001786 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001787 logbuf_assert("Invalid value \"\" of \"value\" attribute in \"value\" element. Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001788
David Sedlák8e7bda82019-07-16 17:57:50 +02001789 /*invalid positions */
1790 data = ELEMENT_WRAPPER_START "<position value=\"-5\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001791 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001792 logbuf_assert("Invalid value \"-5\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001793
1794 data = ELEMENT_WRAPPER_START "<position value=\"-0\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001795 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001796 logbuf_assert("Invalid value \"-0\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001797
1798 data = ELEMENT_WRAPPER_START "<position value=\"99999999999999999999\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001799 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001800 logbuf_assert("Invalid value \"99999999999999999999\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák8e7bda82019-07-16 17:57:50 +02001801
David Sedlák69f01612019-07-17 11:41:08 +02001802 data = ELEMENT_WRAPPER_START "<position value=\"\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001803 assert_int_equal(test_element_helper(st, data, &en, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02001804 logbuf_assert("Invalid value \"\" of \"value\" attribute in \"position\" element. Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001805
1806 st->finished_correctly = true;
1807}
1808
1809static void
1810test_prefix_elem(void **state)
1811{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001812 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001813 const char *data;
1814 const char *value = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001815 struct lysp_ext_instance *exts = NULL;
1816
1817 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\">" EXT_SUBELEM "</prefix>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001818 assert_int_equal(test_element_helper(st, data, &value, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001819 assert_string_equal(value, "pref");
David Sedláke0ef1c62019-09-13 10:05:55 +02001820 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001821 assert_int_equal(exts[0].insubstmt_index, 0);
1822 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_PREFIX);
1823 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1824 exts = NULL;
1825 FREE_STRING(st->ctx, value);
David Sedlák69f01612019-07-17 11:41:08 +02001826
1827 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001828 assert_int_equal(test_element_helper(st, data, &value, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001829 assert_string_equal(value, "pref");
1830 FREE_STRING(st->ctx, value);
1831
1832 st->finished_correctly = true;
1833}
1834
1835static void
1836test_range_elem(void **state)
1837{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001838 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001839 const char *data;
1840 struct lysp_type type = {};
1841
1842 /* max subelems */
1843 data = ELEMENT_WRAPPER_START
1844 "<range value=\"range-str\">"
1845 "<error-message><value>err-msg</value></error-message>"
1846 "<error-app-tag value=\"err-app-tag\" />"
1847 "<description><text>desc</text></description>"
1848 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02001849 EXT_SUBELEM
David Sedlák69f01612019-07-17 11:41:08 +02001850 "</range>"
1851 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001852 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001853 assert_string_equal(type.range->arg.str, "range-str");
David Sedlák69f01612019-07-17 11:41:08 +02001854 assert_string_equal(type.range->dsc, "desc");
1855 assert_string_equal(type.range->eapptag, "err-app-tag");
1856 assert_string_equal(type.range->emsg, "err-msg");
1857 assert_string_equal(type.range->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001858 assert_true(type.flags & LYS_SET_RANGE);
David Sedláke0ef1c62019-09-13 10:05:55 +02001859 assert_string_equal(type.range->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001860 assert_int_equal(type.range->exts[0].insubstmt_index, 0);
1861 assert_int_equal(type.range->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák69f01612019-07-17 11:41:08 +02001862 lysp_type_free(st->ctx, &type);
1863 memset(&type, 0, sizeof(type));
1864
1865 /* min subelems */
1866 data = ELEMENT_WRAPPER_START "<range value=\"range-str\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001867 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001868 assert_string_equal(type.range->arg.str, "range-str");
David Sedlák69f01612019-07-17 11:41:08 +02001869 lysp_type_free(st->ctx, &type);
1870 memset(&type, 0, sizeof(type));
1871
1872 st->finished_correctly = true;
1873}
1874
1875static void
1876test_reqinstance_elem(void **state)
1877{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001878 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001879 const char *data;
1880 struct lysp_type type = {};
1881
David Sedlákd1144562019-08-06 12:36:14 +02001882 data = ELEMENT_WRAPPER_START "<require-instance value=\"true\">" EXT_SUBELEM "</require-instance>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001883 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001884 assert_int_equal(type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001885 assert_true(type.flags & LYS_SET_REQINST);
David Sedláke0ef1c62019-09-13 10:05:55 +02001886 assert_string_equal(type.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001887 assert_int_equal(type.exts[0].insubstmt_index, 0);
1888 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_REQINSTANCE);
1889 lysp_type_free(st->ctx, &type);
David Sedlák69f01612019-07-17 11:41:08 +02001890 memset(&type, 0, sizeof(type));
1891
1892 data = ELEMENT_WRAPPER_START "<require-instance value=\"false\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001893 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001894 assert_int_equal(type.require_instance, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001895 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001896 memset(&type, 0, sizeof(type));
1897
1898 data = ELEMENT_WRAPPER_START "<require-instance value=\"invalid\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001899 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_EVALID);
David Sedlák69f01612019-07-17 11:41:08 +02001900 memset(&type, 0, sizeof(type));
David Sedlák26ea1432019-08-14 13:42:23 +02001901 logbuf_assert("Invalid value \"invalid\" of \"value\" attribute in \"require-instance\" element. Valid values are \"true\" and \"false\". Line number 1.");
David Sedlák69f01612019-07-17 11:41:08 +02001902
1903 st->finished_correctly = true;
1904}
1905
1906static void
1907test_revision_date_elem(void **state)
1908{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001909 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001910 const char *data;
1911 char rev[LY_REV_SIZE];
David Sedlákd1144562019-08-06 12:36:14 +02001912 struct lysp_ext_instance *exts = NULL;
1913
1914 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\">"EXT_SUBELEM"</revision-date>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001915 assert_int_equal(test_element_helper(st, data, rev, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001916 assert_string_equal(rev, "2000-01-01");
David Sedláke0ef1c62019-09-13 10:05:55 +02001917 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001918 assert_int_equal(exts[0].insubstmt_index, 0);
1919 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_REVISIONDATE);
1920 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedlák69f01612019-07-17 11:41:08 +02001921
1922 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001923 assert_int_equal(test_element_helper(st, data, rev, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001924 assert_string_equal(rev, "2000-01-01");
1925
1926 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-50-05\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001927 assert_int_equal(test_element_helper(st, data, rev, NULL, NULL), LY_EVALID);
David Sedlák69f01612019-07-17 11:41:08 +02001928 logbuf_assert("Invalid value \"2000-50-05\" of \"revision-date\". Line number 1.");
1929
1930 st->finished_correctly = true;
1931}
1932
1933static void
1934test_unique_elem(void **state)
1935{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001936 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001937 const char *data;
1938 const char **values = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001939 struct lysp_ext_instance *exts = NULL;
1940
1941 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\">"EXT_SUBELEM"</unique>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001942 assert_int_equal(test_element_helper(st, data, &values, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001943 assert_string_equal(*values, "tag");
David Sedláke0ef1c62019-09-13 10:05:55 +02001944 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001945 assert_int_equal(exts[0].insubstmt_index, 0);
1946 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_UNIQUE);
1947 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1948 FREE_STRING(st->ctx, *values);
1949 LY_ARRAY_FREE(values);
1950 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001951
1952 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001953 assert_int_equal(test_element_helper(st, data, &values, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001954 assert_string_equal(*values, "tag");
1955 FREE_STRING(st->ctx, *values);
1956 LY_ARRAY_FREE(values);
David Sedlákd1144562019-08-06 12:36:14 +02001957 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001958
1959 st->finished_correctly = true;
1960}
1961
1962static void
1963test_units_elem(void **state)
1964{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001965 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001966 const char *data;
1967 const char *values = NULL;
David Sedlákd1144562019-08-06 12:36:14 +02001968 struct lysp_ext_instance *exts = NULL;
1969
1970 data = ELEMENT_WRAPPER_START "<units name=\"name\">"EXT_SUBELEM"</units>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001971 assert_int_equal(test_element_helper(st, data, &values, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02001972 assert_string_equal(values, "name");
David Sedláke0ef1c62019-09-13 10:05:55 +02001973 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02001974 assert_int_equal(exts[0].insubstmt_index, 0);
1975 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_UNITS);
1976 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
1977 FREE_STRING(st->ctx, values);
1978 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001979
1980 data = ELEMENT_WRAPPER_START "<units name=\"name\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01001981 assert_int_equal(test_element_helper(st, data, &values, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02001982 assert_string_equal(values, "name");
1983 FREE_STRING(st->ctx, values);
David Sedlákd1144562019-08-06 12:36:14 +02001984 values = NULL;
David Sedlák69f01612019-07-17 11:41:08 +02001985
1986 st->finished_correctly = true;
1987}
1988
1989static void
1990test_when_elem(void **state)
1991{
Radek Krejci3a4889a2020-05-19 17:01:58 +02001992 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02001993 const char *data;
1994 struct lysp_when *when = NULL;
1995
1996 data = ELEMENT_WRAPPER_START
1997 "<when condition=\"cond\">"
1998 "<description><text>desc</text></description>"
1999 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02002000 EXT_SUBELEM
David Sedlák69f01612019-07-17 11:41:08 +02002001 "</when>"
2002 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002003 assert_int_equal(test_element_helper(st, data, &when, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002004 assert_string_equal(when->cond, "cond");
2005 assert_string_equal(when->dsc, "desc");
2006 assert_string_equal(when->ref, "ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02002007 assert_string_equal(when->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002008 assert_int_equal(when->exts[0].insubstmt_index, 0);
2009 assert_int_equal(when->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák69f01612019-07-17 11:41:08 +02002010 lysp_when_free(st->ctx, when);
2011 free(when);
2012 when = NULL;
2013
2014 data = ELEMENT_WRAPPER_START "<when condition=\"cond\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002015 assert_int_equal(test_element_helper(st, data, &when, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002016 assert_string_equal(when->cond, "cond");
2017 lysp_when_free(st->ctx, when);
2018 free(when);
2019 when = NULL;
2020
2021 st->finished_correctly = true;
2022}
2023
2024static void
2025test_yin_text_value_elem(void **state)
2026{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002027 struct test_parser_yin_state *st = *state;
David Sedlák69f01612019-07-17 11:41:08 +02002028 const char *data;
2029 const char *val;
2030
2031 data = ELEMENT_WRAPPER_START "<text>text</text>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002032 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002033 assert_string_equal(val, "text");
2034 FREE_STRING(st->ctx, val);
2035
2036 data = "<error-message xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <value>text</value> </error-message>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002037 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002038 assert_string_equal(val, "text");
2039 FREE_STRING(st->ctx, val);
2040
2041 data = ELEMENT_WRAPPER_START "<text></text>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002042 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák69f01612019-07-17 11:41:08 +02002043 assert_string_equal("", val);
2044 FREE_STRING(st->ctx, val);
2045
David Sedlák8e7bda82019-07-16 17:57:50 +02002046 st->finished_correctly = true;
2047}
David Sedlák32488102019-07-15 17:44:10 +02002048
David Sedlák374d2b32019-07-17 15:06:55 +02002049static void
2050test_type_elem(void **state)
2051{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002052 struct test_parser_yin_state *st = *state;
David Sedlák374d2b32019-07-17 15:06:55 +02002053 const char *data;
2054 struct lysp_type type = {};
2055
2056 /* max subelems */
2057 data = ELEMENT_WRAPPER_START
2058 "<type name=\"type-name\">"
2059 "<base name=\"base-name\"/>"
2060 "<bit name=\"bit\"/>"
2061 "<enum name=\"enum\"/>"
2062 "<fraction-digits value=\"2\"/>"
2063 "<length value=\"length\"/>"
2064 "<path value=\"path\"/>"
2065 "<pattern value=\"pattern\"/>"
2066 "<range value=\"range\" />"
2067 "<require-instance value=\"true\"/>"
2068 "<type name=\"sub-type-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002069 EXT_SUBELEM
David Sedlák374d2b32019-07-17 15:06:55 +02002070 "</type>"
2071 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002072 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák374d2b32019-07-17 15:06:55 +02002073 assert_string_equal(type.name, "type-name");
2074 assert_string_equal(*type.bases, "base-name");
2075 assert_string_equal(type.bits->name, "bit");
2076 assert_string_equal(type.enums->name, "enum");
2077 assert_int_equal(type.fraction_digits, 2);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002078 assert_string_equal(type.length->arg.str, "length");
Michal Vasko004d3152020-06-11 19:59:22 +02002079 assert_string_equal(type.path->expr, "path");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002080 assert_string_equal(type.patterns->arg.str, "\006pattern");
2081 assert_string_equal(type.range->arg.str, "range");
David Sedlák374d2b32019-07-17 15:06:55 +02002082 assert_int_equal(type.require_instance, 1);
2083 assert_string_equal(type.types->name, "sub-type-name");
David Sedláke0ef1c62019-09-13 10:05:55 +02002084 assert_string_equal(type.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002085 assert_int_equal(type.exts[0].insubstmt_index, 0);
2086 assert_int_equal(type.exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002087 assert_true(type.flags & LYS_SET_BASE);
2088 assert_true(type.flags & LYS_SET_BIT);
2089 assert_true(type.flags & LYS_SET_ENUM);
2090 assert_true(type.flags & LYS_SET_FRDIGITS);
2091 assert_true(type.flags & LYS_SET_LENGTH);
2092 assert_true(type.flags & LYS_SET_PATH);
2093 assert_true(type.flags & LYS_SET_PATTERN);
2094 assert_true(type.flags & LYS_SET_RANGE);
2095 assert_true(type.flags & LYS_SET_REQINST);
2096 assert_true(type.flags & LYS_SET_TYPE);
David Sedlákd1144562019-08-06 12:36:14 +02002097 lysp_type_free(st->ctx, &type);
David Sedlák374d2b32019-07-17 15:06:55 +02002098 memset(&type, 0, sizeof(type));
2099
2100 /* min subelems */
2101 data = ELEMENT_WRAPPER_START "<type name=\"type-name\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002102 assert_int_equal(test_element_helper(st, data, &type, NULL, NULL), LY_SUCCESS);
David Sedlák374d2b32019-07-17 15:06:55 +02002103 lysp_type_free(st->ctx, &type);
2104 memset(&type, 0, sizeof(type));
2105
2106 st->finished_correctly = true;
2107}
2108
David Sedlák1af868e2019-07-17 17:03:14 +02002109static void
2110test_max_elems_elem(void **state)
2111{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002112 struct test_parser_yin_state *st = *state;
David Sedlák1af868e2019-07-17 17:03:14 +02002113 const char *data;
2114 struct lysp_node_list list = {};
2115 struct lysp_node_leaflist llist = {};
2116 struct lysp_refine refine = {};
2117
David Sedlákd1144562019-08-06 12:36:14 +02002118 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"unbounded\">"EXT_SUBELEM"</max-elements> </refine>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002119 assert_int_equal(test_element_helper(st, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02002120 assert_int_equal(refine.max, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002121 assert_true(refine.flags & LYS_SET_MAX);
David Sedláke0ef1c62019-09-13 10:05:55 +02002122 assert_string_equal(refine.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002123 assert_int_equal(refine.exts[0].insubstmt_index, 0);
2124 assert_int_equal(refine.exts[0].insubstmt, LYEXT_SUBSTMT_MAX);
2125 FREE_ARRAY(st->ctx, refine.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02002126
David Sedlákd1144562019-08-06 12:36:14 +02002127 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"5\">"EXT_SUBELEM"</max-elements> </list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002128 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02002129 assert_int_equal(list.max, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002130 assert_true(list.flags & LYS_SET_MAX);
David Sedláke0ef1c62019-09-13 10:05:55 +02002131 assert_string_equal(list.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002132 assert_int_equal(list.exts[0].insubstmt_index, 0);
2133 assert_int_equal(list.exts[0].insubstmt, LYEXT_SUBSTMT_MAX);
2134 FREE_ARRAY(st->ctx, list.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02002135
David Sedlákd1144562019-08-06 12:36:14 +02002136 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"85\">"EXT_SUBELEM"</max-elements> </leaf-list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002137 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02002138 assert_int_equal(llist.max, 85);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002139 assert_true(llist.flags & LYS_SET_MAX);
David Sedláke0ef1c62019-09-13 10:05:55 +02002140 assert_string_equal(llist.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002141 assert_int_equal(llist.exts[0].insubstmt_index, 0);
2142 assert_int_equal(llist.exts[0].insubstmt, LYEXT_SUBSTMT_MAX);
2143 FREE_ARRAY(st->ctx, llist.exts, lysp_ext_instance_free);
David Sedlák1af868e2019-07-17 17:03:14 +02002144
2145 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"10\"/> </refine>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002146 assert_int_equal(test_element_helper(st, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák1af868e2019-07-17 17:03:14 +02002147 assert_int_equal(refine.max, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002148 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02002149
2150 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"0\"/> </list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002151 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002152 logbuf_assert("Invalid value \"0\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002153
2154 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"-10\"/> </list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002155 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002156 logbuf_assert("Invalid value \"-10\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002157
2158 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"k\"/> </list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002159 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002160 logbuf_assert("Invalid value \"k\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002161
2162 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"u12\"/> </list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002163 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002164 logbuf_assert("Invalid value \"u12\" of \"value\" attribute in \"max-elements\" element. Line number 1.");
David Sedlák1af868e2019-07-17 17:03:14 +02002165
2166 st->finished_correctly = true;
2167}
2168
David Sedlák09e18c92019-07-18 11:17:11 +02002169static void
2170test_min_elems_elem(void **state)
2171{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002172 struct test_parser_yin_state *st = *state;
David Sedlák09e18c92019-07-18 11:17:11 +02002173 const char *data;
2174 struct lysp_node_list list = {};
2175 struct lysp_node_leaflist llist = {};
2176 struct lysp_refine refine = {};
2177
David Sedlákd1144562019-08-06 12:36:14 +02002178 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"0\">"EXT_SUBELEM"</min-elements> </refine>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002179 assert_int_equal(test_element_helper(st, data, &refine, NULL, NULL), LY_SUCCESS);
David Sedlák09e18c92019-07-18 11:17:11 +02002180 assert_int_equal(refine.min, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002181 assert_true(refine.flags & LYS_SET_MIN);
David Sedláke0ef1c62019-09-13 10:05:55 +02002182 assert_string_equal(refine.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002183 assert_int_equal(refine.exts[0].insubstmt_index, 0);
2184 assert_int_equal(refine.exts[0].insubstmt, LYEXT_SUBSTMT_MIN);
2185 FREE_ARRAY(st->ctx, refine.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02002186
David Sedlákd1144562019-08-06 12:36:14 +02002187 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"41\">"EXT_SUBELEM"</min-elements> </list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002188 assert_int_equal(test_element_helper(st, data, &list, NULL, NULL), LY_SUCCESS);
David Sedlák09e18c92019-07-18 11:17:11 +02002189 assert_int_equal(list.min, 41);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002190 assert_true(list.flags & LYS_SET_MIN);
David Sedláke0ef1c62019-09-13 10:05:55 +02002191 assert_string_equal(list.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002192 assert_int_equal(list.exts[0].insubstmt_index, 0);
2193 assert_int_equal(list.exts[0].insubstmt, LYEXT_SUBSTMT_MIN);
2194 FREE_ARRAY(st->ctx, list.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02002195
David Sedlákd1144562019-08-06 12:36:14 +02002196 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"50\">"EXT_SUBELEM"</min-elements> </leaf-list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002197 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_SUCCESS);
David Sedlák09e18c92019-07-18 11:17:11 +02002198 assert_int_equal(llist.min, 50);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002199 assert_true(llist.flags & LYS_SET_MIN);
David Sedláke0ef1c62019-09-13 10:05:55 +02002200 assert_string_equal(llist.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002201 assert_int_equal(llist.exts[0].insubstmt_index, 0);
2202 assert_int_equal(llist.exts[0].insubstmt, LYEXT_SUBSTMT_MIN);
2203 FREE_ARRAY(st->ctx, llist.exts, lysp_ext_instance_free);
David Sedlák09e18c92019-07-18 11:17:11 +02002204
2205 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"-5\"/> </leaf-list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002206 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002207 logbuf_assert("Value \"-5\" of \"value\" attribute in \"min-elements\" element is out of bounds. Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02002208
2209 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"99999999999999999\"/> </leaf-list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002210 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002211 logbuf_assert("Value \"99999999999999999\" of \"value\" attribute in \"min-elements\" element is out of bounds. Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02002212
2213 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"5k\"/> </leaf-list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002214 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002215 logbuf_assert("Invalid value \"5k\" of \"value\" attribute in \"min-elements\" element. Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02002216
2217 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"05\"/> </leaf-list>";
Michal Vaskob36053d2020-03-26 15:49:30 +01002218 assert_int_equal(test_element_helper(st, data, &llist, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002219 logbuf_assert("Invalid value \"05\" of \"value\" attribute in \"min-elements\" element. Line number 1.");
David Sedlák09e18c92019-07-18 11:17:11 +02002220
2221 st->finished_correctly = true;
2222}
2223
David Sedláka2dad212019-07-18 12:45:19 +02002224static void
2225test_ordby_elem(void **state)
2226{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002227 struct test_parser_yin_state *st = *state;
David Sedláka2dad212019-07-18 12:45:19 +02002228 const char *data;
2229 uint16_t flags = 0;
David Sedlákd1144562019-08-06 12:36:14 +02002230 struct lysp_ext_instance *exts = NULL;
David Sedláka2dad212019-07-18 12:45:19 +02002231
David Sedlákd1144562019-08-06 12:36:14 +02002232 data = ELEMENT_WRAPPER_START "<ordered-by value=\"system\">"EXT_SUBELEM"</ordered-by>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002233 assert_int_equal(test_element_helper(st, data, &flags, NULL, &exts), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002234 assert_true(flags & LYS_ORDBY_SYSTEM);
David Sedláke0ef1c62019-09-13 10:05:55 +02002235 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002236 assert_int_equal(exts[0].insubstmt_index, 0);
2237 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_ORDEREDBY);
2238 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
David Sedláka2dad212019-07-18 12:45:19 +02002239
2240 data = ELEMENT_WRAPPER_START "<ordered-by value=\"user\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002241 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002242 assert_true(flags & LYS_ORDBY_USER);
David Sedláka2dad212019-07-18 12:45:19 +02002243
2244 data = ELEMENT_WRAPPER_START "<ordered-by value=\"inv\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002245 assert_int_equal(test_element_helper(st, data, &flags, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02002246 logbuf_assert("Invalid value \"inv\" of \"value\" attribute in \"ordered-by\" element. Valid values are \"system\" and \"user\". Line number 1.");
David Sedláka2dad212019-07-18 12:45:19 +02002247
2248 st->finished_correctly = true;
2249}
2250
David Sedlák8a83bbb2019-07-18 14:46:00 +02002251static void
2252test_any_elem(void **state)
2253{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002254 struct test_parser_yin_state *st = *state;
David Sedlák8a83bbb2019-07-18 14:46:00 +02002255 const char *data;
2256 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002257 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák8a83bbb2019-07-18 14:46:00 +02002258 struct lysp_node_anydata *parsed = NULL;
2259
2260 /* anyxml max subelems */
2261 data = ELEMENT_WRAPPER_START
2262 "<anyxml name=\"any-name\">"
2263 "<config value=\"true\" />"
2264 "<description><text>desc</text></description>"
2265 "<if-feature name=\"feature\" />"
2266 "<mandatory value=\"true\" />"
2267 "<must condition=\"must-cond\" />"
2268 "<reference><text>ref</text></reference>"
2269 "<status value=\"deprecated\"/>"
2270 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002271 EXT_SUBELEM
David Sedlák8a83bbb2019-07-18 14:46:00 +02002272 "</anyxml>"
2273 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002274 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002275 parsed = (struct lysp_node_anydata *)siblings;
2276 assert_null(parsed->parent);
2277 assert_int_equal(parsed->nodetype, LYS_ANYXML);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002278 assert_true(parsed->flags & LYS_CONFIG_W);
2279 assert_true(parsed->flags & LYS_MAND_TRUE);
2280 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002281 assert_null(parsed->next);
2282 assert_string_equal(parsed->name, "any-name");
2283 assert_string_equal(parsed->dsc, "desc");
2284 assert_string_equal(parsed->ref, "ref");
2285 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002286 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedláke0ef1c62019-09-13 10:05:55 +02002287 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002288 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2289 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002290 lysp_node_free(st->ctx, siblings);
2291 siblings = NULL;
2292
2293 /* anydata max subelems */
2294 data = ELEMENT_WRAPPER_START
2295 "<anydata name=\"any-name\">"
2296 "<config value=\"true\" />"
2297 "<description><text>desc</text></description>"
2298 "<if-feature name=\"feature\" />"
2299 "<mandatory value=\"true\" />"
2300 "<must condition=\"must-cond\" />"
2301 "<reference><text>ref</text></reference>"
2302 "<status value=\"deprecated\"/>"
2303 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002304 EXT_SUBELEM
David Sedlák8a83bbb2019-07-18 14:46:00 +02002305 "</anydata>"
2306 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002307 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002308 parsed = (struct lysp_node_anydata *)siblings;
2309 assert_null(parsed->parent);
2310 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002311 assert_true(parsed->flags & LYS_CONFIG_W);
2312 assert_true(parsed->flags & LYS_MAND_TRUE);
2313 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002314 assert_null(parsed->next);
2315 assert_string_equal(parsed->name, "any-name");
2316 assert_string_equal(parsed->dsc, "desc");
2317 assert_string_equal(parsed->ref, "ref");
2318 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002319 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedláke0ef1c62019-09-13 10:05:55 +02002320 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002321 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2322 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002323 lysp_node_free(st->ctx, siblings);
2324 siblings = NULL;
2325
2326 /* min subelems */
2327 node_meta.parent = (void *)0x10;
2328 data = ELEMENT_WRAPPER_START "<anydata name=\"any-name\"> </anydata>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002329 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák8a83bbb2019-07-18 14:46:00 +02002330 parsed = (struct lysp_node_anydata *)siblings;
2331 assert_ptr_equal(parsed->parent, node_meta.parent);
2332 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
2333 assert_null(parsed->next);
2334 assert_null(parsed->exts);
2335 lysp_node_free(st->ctx, siblings);
2336
2337 st->finished_correctly = true;
2338}
2339
David Sedlák203ca3a2019-07-18 15:26:25 +02002340static void
2341test_leaf_elem(void **state)
2342{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002343 struct test_parser_yin_state *st = *state;
David Sedlák203ca3a2019-07-18 15:26:25 +02002344 const char *data;
2345 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002346 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlák203ca3a2019-07-18 15:26:25 +02002347 struct lysp_node_leaf *parsed = NULL;
2348
2349 /* max elements */
2350 data = ELEMENT_WRAPPER_START
2351 "<leaf name=\"leaf\">"
2352 "<config value=\"true\" />"
2353 "<default value=\"def-val\"/>"
2354 "<description><text>desc</text></description>"
2355 "<if-feature name=\"feature\" />"
2356 "<mandatory value=\"true\" />"
2357 "<must condition=\"must-cond\" />"
2358 "<reference><text>ref</text></reference>"
2359 "<status value=\"deprecated\"/>"
2360 "<type name=\"type\"/>"
2361 "<units name=\"uni\"/>"
2362 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002363 EXT_SUBELEM
David Sedlák203ca3a2019-07-18 15:26:25 +02002364 "</leaf>"
2365 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002366 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák203ca3a2019-07-18 15:26:25 +02002367 parsed = (struct lysp_node_leaf *)siblings;
2368 assert_null(parsed->parent);
2369 assert_int_equal(parsed->nodetype, LYS_LEAF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002370 assert_true(parsed->flags & LYS_CONFIG_W);
2371 assert_true(parsed->flags & LYS_MAND_TRUE);
2372 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák203ca3a2019-07-18 15:26:25 +02002373 assert_null(parsed->next);
2374 assert_string_equal(parsed->name, "leaf");
2375 assert_string_equal(parsed->dsc, "desc");
2376 assert_string_equal(parsed->ref, "ref");
2377 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002378 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedláke0ef1c62019-09-13 10:05:55 +02002379 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002380 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2381 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002382 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlák203ca3a2019-07-18 15:26:25 +02002383 assert_string_equal(parsed->type.name, "type");
2384 assert_string_equal(parsed->units, "uni");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002385 assert_string_equal(parsed->dflt.str, "def-val");
David Sedlák203ca3a2019-07-18 15:26:25 +02002386 lysp_node_free(st->ctx, siblings);
2387 siblings = NULL;
2388
2389 /* min elements */
2390 data = ELEMENT_WRAPPER_START "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002391 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák203ca3a2019-07-18 15:26:25 +02002392 parsed = (struct lysp_node_leaf *)siblings;
2393 assert_string_equal(parsed->name, "leaf");
2394 assert_string_equal(parsed->type.name, "type");
2395 lysp_node_free(st->ctx, siblings);
2396 siblings = NULL;
2397
2398 st->finished_correctly = true;
2399}
2400
David Sedlákc3da3ef2019-07-19 12:56:08 +02002401static void
2402test_leaf_list_elem(void **state)
2403{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002404 struct test_parser_yin_state *st = *state;
David Sedlákc3da3ef2019-07-19 12:56:08 +02002405 const char *data;
2406 struct lysp_node *siblings = NULL;
David Sedlákbf8a2b72019-08-14 16:48:10 +02002407 struct tree_node_meta node_meta = {.parent = NULL, .nodes = &siblings};
David Sedlákc3da3ef2019-07-19 12:56:08 +02002408 struct lysp_node_leaflist *parsed = NULL;
2409
2410 data = ELEMENT_WRAPPER_START
2411 "<leaf-list name=\"llist\">"
2412 "<config value=\"true\" />"
2413 "<default value=\"def-val0\"/>"
2414 "<default value=\"def-val1\"/>"
2415 "<description><text>desc</text></description>"
2416 "<if-feature name=\"feature\"/>"
2417 "<max-elements value=\"5\"/>"
2418 "<must condition=\"must-cond\"/>"
2419 "<ordered-by value=\"user\" />"
2420 "<reference><text>ref</text></reference>"
2421 "<status value=\"current\"/>"
2422 "<type name=\"type\"/>"
2423 "<units name=\"uni\"/>"
2424 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002425 EXT_SUBELEM
David Sedlákc3da3ef2019-07-19 12:56:08 +02002426 "</leaf-list>"
2427 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002428 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002429 parsed = (struct lysp_node_leaflist *)siblings;
Michal Vasko7f45cf22020-10-01 12:49:44 +02002430 assert_string_equal(parsed->dflts[0].str, "def-val0");
2431 assert_string_equal(parsed->dflts[1].str, "def-val1");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002432 assert_string_equal(parsed->dsc, "desc");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002433 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002434 assert_int_equal(parsed->max, 5);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002435 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002436 assert_string_equal(parsed->name, "llist");
2437 assert_null(parsed->next);
2438 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2439 assert_null(parsed->parent);
2440 assert_string_equal(parsed->ref, "ref");
2441 assert_string_equal(parsed->type.name, "type");
2442 assert_string_equal(parsed->units, "uni");
2443 assert_string_equal(parsed->when->cond, "when-cond");
2444 assert_true(parsed->flags & LYS_CONFIG_W);
2445 assert_true(parsed->flags & LYS_ORDBY_USER);
2446 assert_true(parsed->flags & LYS_STATUS_CURR);
David Sedláke0ef1c62019-09-13 10:05:55 +02002447 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002448 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2449 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002450 lysp_node_free(st->ctx, siblings);
2451 siblings = NULL;
2452
2453 data = ELEMENT_WRAPPER_START
2454 "<leaf-list name=\"llist\">"
2455 "<config value=\"true\" />"
2456 "<description><text>desc</text></description>"
2457 "<if-feature name=\"feature\"/>"
2458 "<min-elements value=\"5\"/>"
2459 "<must condition=\"must-cond\"/>"
2460 "<ordered-by value=\"user\" />"
2461 "<reference><text>ref</text></reference>"
2462 "<status value=\"current\"/>"
2463 "<type name=\"type\"/>"
2464 "<units name=\"uni\"/>"
2465 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002466 EXT_SUBELEM
David Sedlákc3da3ef2019-07-19 12:56:08 +02002467 "</leaf-list>"
2468 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002469 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002470 parsed = (struct lysp_node_leaflist *)siblings;
2471 assert_string_equal(parsed->dsc, "desc");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002472 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002473 assert_int_equal(parsed->min, 5);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002474 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002475 assert_string_equal(parsed->name, "llist");
2476 assert_null(parsed->next);
2477 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2478 assert_null(parsed->parent);
2479 assert_string_equal(parsed->ref, "ref");
2480 assert_string_equal(parsed->type.name, "type");
2481 assert_string_equal(parsed->units, "uni");
2482 assert_string_equal(parsed->when->cond, "when-cond");
2483 assert_true(parsed->flags & LYS_CONFIG_W);
2484 assert_true(parsed->flags & LYS_ORDBY_USER);
2485 assert_true(parsed->flags & LYS_STATUS_CURR);
David Sedláke0ef1c62019-09-13 10:05:55 +02002486 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002487 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2488 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002489 lysp_node_free(st->ctx, siblings);
2490 siblings = NULL;
2491
2492 data = ELEMENT_WRAPPER_START
2493 "<leaf-list name=\"llist\">"
2494 "<config value=\"true\" />"
2495 "<description><text>desc</text></description>"
2496 "<if-feature name=\"feature\"/>"
2497 "<max-elements value=\"15\"/>"
2498 "<min-elements value=\"5\"/>"
2499 "<must condition=\"must-cond\"/>"
2500 "<ordered-by value=\"user\" />"
2501 "<reference><text>ref</text></reference>"
2502 "<status value=\"current\"/>"
2503 "<type name=\"type\"/>"
2504 "<units name=\"uni\"/>"
2505 "<when condition=\"when-cond\"/>"
2506 "</leaf-list>"
2507 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002508 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002509 parsed = (struct lysp_node_leaflist *)siblings;
2510 assert_string_equal(parsed->dsc, "desc");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002511 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002512 assert_int_equal(parsed->min, 5);
2513 assert_int_equal(parsed->max, 15);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002514 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002515 assert_string_equal(parsed->name, "llist");
2516 assert_null(parsed->next);
2517 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2518 assert_null(parsed->parent);
2519 assert_string_equal(parsed->ref, "ref");
2520 assert_string_equal(parsed->type.name, "type");
2521 assert_string_equal(parsed->units, "uni");
2522 assert_string_equal(parsed->when->cond, "when-cond");
2523 assert_true(parsed->flags & LYS_CONFIG_W);
2524 assert_true(parsed->flags & LYS_ORDBY_USER);
2525 assert_true(parsed->flags & LYS_STATUS_CURR);
2526 lysp_node_free(st->ctx, siblings);
2527 siblings = NULL;
2528
2529 data = ELEMENT_WRAPPER_START
2530 "<leaf-list name=\"llist\">"
2531 "<type name=\"type\"/>"
2532 "</leaf-list>"
2533 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002534 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002535 parsed = (struct lysp_node_leaflist *)siblings;
2536 assert_string_equal(parsed->name, "llist");
2537 assert_string_equal(parsed->type.name, "type");
2538 lysp_node_free(st->ctx, siblings);
2539 siblings = NULL;
2540
2541 /* invalid combinations */
2542 data = ELEMENT_WRAPPER_START
2543 "<leaf-list name=\"llist\">"
2544 "<max-elements value=\"5\"/>"
2545 "<min-elements value=\"15\"/>"
2546 "<type name=\"type\"/>"
2547 "</leaf-list>"
2548 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002549 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_EVALID);
David Sedlákc3da3ef2019-07-19 12:56:08 +02002550 logbuf_assert("Invalid combination of min-elements and max-elements: min value 15 is bigger than the max value 5. Line number 1.");
2551 lysp_node_free(st->ctx, siblings);
2552 siblings = NULL;
2553
2554 data = ELEMENT_WRAPPER_START
2555 "<leaf-list name=\"llist\">"
2556 "<default value=\"def-val1\"/>"
2557 "<min-elements value=\"15\"/>"
2558 "<type name=\"type\"/>"
2559 "</leaf-list>"
2560 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002561 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002562 logbuf_assert("Invalid combination of sub-elemnts \"min-elements\" and \"default\" in \"leaf-list\" element. Line number 1.");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002563 lysp_node_free(st->ctx, siblings);
2564 siblings = NULL;
2565
2566 data = ELEMENT_WRAPPER_START
2567 "<leaf-list name=\"llist\">"
2568 "</leaf-list>"
2569 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002570 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02002571 logbuf_assert("Missing mandatory sub-element \"type\" of \"leaf-list\" element. Line number 1.");
David Sedlákc3da3ef2019-07-19 12:56:08 +02002572 lysp_node_free(st->ctx, siblings);
2573 siblings = NULL;
2574
2575 st->finished_correctly = true;
2576}
2577
David Sedlákcb39f642019-07-19 13:19:55 +02002578static void
2579test_presence_elem(void **state)
2580{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002581 struct test_parser_yin_state *st = *state;
David Sedlákcb39f642019-07-19 13:19:55 +02002582 const char *data;
2583 const char *val;
David Sedlákd1144562019-08-06 12:36:14 +02002584 struct lysp_ext_instance *exts = NULL;
2585
2586 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\">"EXT_SUBELEM"</presence>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002587 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02002588 assert_string_equal(val, "presence-val");
David Sedláke0ef1c62019-09-13 10:05:55 +02002589 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002590 assert_int_equal(exts[0].insubstmt_index, 0);
2591 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_PRESENCE);
2592 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
2593 FREE_STRING(st->ctx, val);
David Sedlákcb39f642019-07-19 13:19:55 +02002594
2595 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002596 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlákcb39f642019-07-19 13:19:55 +02002597 assert_string_equal(val, "presence-val");
2598 FREE_STRING(st->ctx, val);
2599
2600 data = ELEMENT_WRAPPER_START "<presence/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002601 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlákcb39f642019-07-19 13:19:55 +02002602 logbuf_assert("Missing mandatory attribute value of presence element. Line number 1.");
2603
2604 st->finished_correctly = true;
2605}
2606
David Sedlák12470a82019-07-19 13:44:36 +02002607static void
2608test_key_elem(void **state)
2609{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002610 struct test_parser_yin_state *st = *state;
David Sedlák12470a82019-07-19 13:44:36 +02002611 const char *data;
2612 const char *val;
David Sedlákd1144562019-08-06 12:36:14 +02002613 struct lysp_ext_instance *exts = NULL;
2614
2615 data = ELEMENT_WRAPPER_START "<key value=\"key-value\">"EXT_SUBELEM"</key>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002616 assert_int_equal(test_element_helper(st, data, &val, NULL, &exts), LY_SUCCESS);
David Sedlákd1144562019-08-06 12:36:14 +02002617 assert_string_equal(val, "key-value");
David Sedláke0ef1c62019-09-13 10:05:55 +02002618 assert_string_equal(exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002619 assert_int_equal(exts[0].insubstmt_index, 0);
2620 assert_int_equal(exts[0].insubstmt, LYEXT_SUBSTMT_KEY);
2621 FREE_ARRAY(st->ctx, exts, lysp_ext_instance_free);
2622 FREE_STRING(st->ctx, val);
David Sedlák12470a82019-07-19 13:44:36 +02002623
2624 data = ELEMENT_WRAPPER_START "<key value=\"key-value\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002625 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_SUCCESS);
David Sedlák12470a82019-07-19 13:44:36 +02002626 assert_string_equal(val, "key-value");
2627 FREE_STRING(st->ctx, val);
2628
2629 data = ELEMENT_WRAPPER_START "<key/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002630 assert_int_equal(test_element_helper(st, data, &val, NULL, NULL), LY_EVALID);
David Sedlák12470a82019-07-19 13:44:36 +02002631 logbuf_assert("Missing mandatory attribute value of key element. Line number 1.");
2632
2633 st->finished_correctly = true;
2634}
2635
David Sedlák04e17b22019-07-19 15:29:48 +02002636static void
2637test_typedef_elem(void **state)
2638{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002639 struct test_parser_yin_state *st = *state;
David Sedlák04e17b22019-07-19 15:29:48 +02002640 const char *data;
2641 struct lysp_tpdf *tpdfs = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002642 struct tree_node_meta typdef_meta = {NULL, (struct lysp_node **)&tpdfs};
David Sedlák04e17b22019-07-19 15:29:48 +02002643
2644 data = ELEMENT_WRAPPER_START
2645 "<typedef name=\"tpdf-name\">"
2646 "<default value=\"def-val\"/>"
2647 "<description><text>desc-text</text></description>"
2648 "<reference><text>ref-text</text></reference>"
2649 "<status value=\"current\"/>"
2650 "<type name=\"type\"/>"
2651 "<units name=\"uni\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002652 EXT_SUBELEM
David Sedlák04e17b22019-07-19 15:29:48 +02002653 "</typedef>"
2654 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002655 assert_int_equal(test_element_helper(st, data, &typdef_meta, NULL, NULL), LY_SUCCESS);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002656 assert_string_equal(tpdfs[0].dflt.str, "def-val");
David Sedlák04e17b22019-07-19 15:29:48 +02002657 assert_string_equal(tpdfs[0].dsc, "desc-text");
David Sedlák04e17b22019-07-19 15:29:48 +02002658 assert_string_equal(tpdfs[0].name, "tpdf-name");
2659 assert_string_equal(tpdfs[0].ref, "ref-text");
2660 assert_string_equal(tpdfs[0].type.name, "type");
2661 assert_string_equal(tpdfs[0].units, "uni");
2662 assert_true(tpdfs[0].flags & LYS_STATUS_CURR);
David Sedláke0ef1c62019-09-13 10:05:55 +02002663 assert_string_equal(tpdfs[0].exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002664 assert_int_equal(tpdfs[0].exts[0].insubstmt_index, 0);
2665 assert_int_equal(tpdfs[0].exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák04e17b22019-07-19 15:29:48 +02002666 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2667 tpdfs = NULL;
2668
2669 data = ELEMENT_WRAPPER_START
2670 "<typedef name=\"tpdf-name\">"
2671 "<type name=\"type\"/>"
2672 "</typedef>"
2673 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002674 assert_int_equal(test_element_helper(st, data, &typdef_meta, NULL, NULL), LY_SUCCESS);
David Sedlák04e17b22019-07-19 15:29:48 +02002675 assert_string_equal(tpdfs[0].name, "tpdf-name");
2676 assert_string_equal(tpdfs[0].type.name, "type");
2677 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2678 tpdfs = NULL;
2679
2680 st->finished_correctly = true;
2681}
2682
David Sedlákd2d676a2019-07-22 11:28:19 +02002683static void
2684test_refine_elem(void **state)
2685{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002686 struct test_parser_yin_state *st = *state;
David Sedlákd2d676a2019-07-22 11:28:19 +02002687 const char *data;
2688 struct lysp_refine *refines = NULL;
2689
2690 /* max subelems */
2691 data = ELEMENT_WRAPPER_START
2692 "<refine target-node=\"target\">"
2693 "<if-feature name=\"feature\" />"
2694 "<must condition=\"cond\" />"
2695 "<presence value=\"presence\" />"
2696 "<default value=\"def\" />"
2697 "<config value=\"true\" />"
2698 "<mandatory value=\"true\" />"
2699 "<min-elements value=\"10\" />"
2700 "<max-elements value=\"20\" />"
2701 "<description><text>desc</text></description>"
2702 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02002703 EXT_SUBELEM
David Sedlákd2d676a2019-07-22 11:28:19 +02002704 "</refine>"
2705 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002706 assert_int_equal(test_element_helper(st, data, &refines, NULL, NULL), LY_SUCCESS);
David Sedlákd2d676a2019-07-22 11:28:19 +02002707 assert_string_equal(refines->nodeid, "target");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002708 assert_string_equal(refines->dflts[0], "def");
David Sedlákd2d676a2019-07-22 11:28:19 +02002709 assert_string_equal(refines->dsc, "desc");
David Sedlákd2d676a2019-07-22 11:28:19 +02002710 assert_true(refines->flags & LYS_CONFIG_W);
2711 assert_true(refines->flags & LYS_MAND_TRUE);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002712 assert_string_equal(refines->iffeatures[0].str, "feature");
David Sedlákd2d676a2019-07-22 11:28:19 +02002713 assert_int_equal(refines->max, 20);
2714 assert_int_equal(refines->min, 10);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002715 assert_string_equal(refines->musts->arg.str, "cond");
David Sedlákd2d676a2019-07-22 11:28:19 +02002716 assert_string_equal(refines->presence, "presence");
2717 assert_string_equal(refines->ref, "ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02002718 assert_string_equal(refines->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002719 assert_int_equal(refines->exts[0].insubstmt_index, 0);
2720 assert_int_equal(refines->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákd2d676a2019-07-22 11:28:19 +02002721 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2722 refines = NULL;
2723
2724 /* min subelems */
2725 data = ELEMENT_WRAPPER_START "<refine target-node=\"target\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002726 assert_int_equal(test_element_helper(st, data, &refines, NULL, NULL), LY_SUCCESS);
David Sedlákd2d676a2019-07-22 11:28:19 +02002727 assert_string_equal(refines->nodeid, "target");
2728 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2729 refines = NULL;
2730
2731 st->finished_correctly = true;
2732}
2733
David Sedlák0d6de5a2019-07-22 13:25:44 +02002734static void
2735test_uses_elem(void **state)
2736{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002737 struct test_parser_yin_state *st = *state;
David Sedlák0d6de5a2019-07-22 13:25:44 +02002738 const char *data;
2739 struct lysp_node *siblings = NULL;
2740 struct tree_node_meta node_meta = {NULL, &siblings};
2741 struct lysp_node_uses *parsed = NULL;
2742
2743 /* max subelems */
2744 data = ELEMENT_WRAPPER_START
2745 "<uses name=\"uses-name\">"
2746 "<when condition=\"cond\" />"
2747 "<if-feature name=\"feature\" />"
2748 "<status value=\"obsolete\" />"
2749 "<description><text>desc</text></description>"
2750 "<reference><text>ref</text></reference>"
2751 "<refine target-node=\"target\"/>"
David Sedlák992fb7c2019-07-24 16:51:01 +02002752 "<augment target-node=\"target\" />"
David Sedlákd1144562019-08-06 12:36:14 +02002753 EXT_SUBELEM
David Sedlák0d6de5a2019-07-22 13:25:44 +02002754 "</uses>"
2755 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002756 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002757 parsed = (struct lysp_node_uses *)&siblings[0];
2758 assert_string_equal(parsed->name, "uses-name");
2759 assert_string_equal(parsed->dsc, "desc");
David Sedlák0d6de5a2019-07-22 13:25:44 +02002760 assert_true(parsed->flags & LYS_STATUS_OBSLT);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002761 assert_string_equal(parsed->iffeatures[0].str, "feature");
David Sedlák0d6de5a2019-07-22 13:25:44 +02002762 assert_null(parsed->next);
2763 assert_int_equal(parsed->nodetype, LYS_USES);
2764 assert_null(parsed->parent);
2765 assert_string_equal(parsed->ref, "ref");
2766 assert_string_equal(parsed->refines->nodeid, "target");
2767 assert_string_equal(parsed->when->cond, "cond");
David Sedlák992fb7c2019-07-24 16:51:01 +02002768 assert_string_equal(parsed->augments->nodeid, "target");
David Sedláke0ef1c62019-09-13 10:05:55 +02002769 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002770 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2771 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002772 lysp_node_free(st->ctx, siblings);
2773 siblings = NULL;
2774
2775 /* min subelems */
2776 data = ELEMENT_WRAPPER_START "<uses name=\"uses-name\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002777 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0d6de5a2019-07-22 13:25:44 +02002778 assert_string_equal(siblings[0].name, "uses-name");
2779 lysp_node_free(st->ctx, siblings);
2780 siblings = NULL;
2781
2782 st->finished_correctly = true;
2783}
2784
David Sedlákaa854b02019-07-22 14:17:10 +02002785static void
2786test_revision_elem(void **state)
2787{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002788 struct test_parser_yin_state *st = *state;
David Sedlákaa854b02019-07-22 14:17:10 +02002789 const char *data;
2790 struct lysp_revision *revs = NULL;
2791
2792 /* max subelems */
2793 data = ELEMENT_WRAPPER_START
2794 "<revision date=\"2018-12-25\">"
2795 "<description><text>desc</text></description>"
2796 "<reference><text>ref</text></reference>"
David Sedlákd1144562019-08-06 12:36:14 +02002797 EXT_SUBELEM
David Sedlákaa854b02019-07-22 14:17:10 +02002798 "</revision>"
2799 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002800 assert_int_equal(test_element_helper(st, data, &revs, NULL, NULL), LY_SUCCESS);
David Sedlákaa854b02019-07-22 14:17:10 +02002801 assert_string_equal(revs->date, "2018-12-25");
2802 assert_string_equal(revs->dsc, "desc");
2803 assert_string_equal(revs->ref, "ref");
David Sedláke0ef1c62019-09-13 10:05:55 +02002804 assert_string_equal(revs->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002805 assert_int_equal(revs->exts[0].insubstmt_index, 0);
2806 assert_int_equal(revs->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákaa854b02019-07-22 14:17:10 +02002807 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2808 revs = NULL;
2809
2810 /* min subelems */
2811 data = ELEMENT_WRAPPER_START "<revision date=\"2005-05-05\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002812 assert_int_equal(test_element_helper(st, data, &revs, NULL, NULL), LY_SUCCESS);
David Sedlákaa854b02019-07-22 14:17:10 +02002813 assert_string_equal(revs->date, "2005-05-05");
2814 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2815 revs = NULL;
2816
2817 /* invalid value */
2818 data = ELEMENT_WRAPPER_START "<revision date=\"05-05-2005\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002819 assert_int_equal(test_element_helper(st, data, &revs, NULL, NULL), LY_EVALID);
David Sedlákaa854b02019-07-22 14:17:10 +02002820 logbuf_assert("Invalid value \"05-05-2005\" of \"revision\". Line number 1.");
2821 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2822 revs = NULL;
2823
2824 st->finished_correctly = true;
2825}
2826
David Sedlák0c2bab92019-07-22 15:33:19 +02002827static void
2828test_include_elem(void **state)
2829{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002830 struct test_parser_yin_state *st = *state;
David Sedlák0c2bab92019-07-22 15:33:19 +02002831 const char *data;
2832 struct lysp_include *includes = NULL;
2833 struct include_meta inc_meta = {"module-name", &includes};
2834
2835 /* max subelems */
2836 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2837 data = ELEMENT_WRAPPER_START
2838 "<include module=\"mod\">"
2839 "<description><text>desc</text></description>"
2840 "<reference><text>ref</text></reference>"
2841 "<revision-date date=\"1999-09-09\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002842 EXT_SUBELEM
David Sedlák0c2bab92019-07-22 15:33:19 +02002843 "</include>"
2844 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002845 assert_int_equal(test_element_helper(st, data, &inc_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0c2bab92019-07-22 15:33:19 +02002846 assert_string_equal(includes->name, "mod");
2847 assert_string_equal(includes->dsc, "desc");
2848 assert_string_equal(includes->ref, "ref");
David Sedlák0c2bab92019-07-22 15:33:19 +02002849 assert_string_equal(includes->rev, "1999-09-09");
David Sedláke0ef1c62019-09-13 10:05:55 +02002850 assert_string_equal(includes->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002851 assert_int_equal(includes->exts[0].insubstmt_index, 0);
2852 assert_int_equal(includes->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák0c2bab92019-07-22 15:33:19 +02002853 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2854 includes = NULL;
2855
2856 /* min subelems */
2857 data = ELEMENT_WRAPPER_START "<include module=\"mod\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002858 assert_int_equal(test_element_helper(st, data, &inc_meta, NULL, NULL), LY_SUCCESS);
David Sedlák0c2bab92019-07-22 15:33:19 +02002859 assert_string_equal(includes->name, "mod");
2860 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2861 includes = NULL;
2862
2863 /* invalid combinations */
2864 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2865 data = ELEMENT_WRAPPER_START
2866 "<include module=\"mod\">"
2867 "<description><text>desc</text></description>"
2868 "<revision-date date=\"1999-09-09\"/>"
2869 "</include>"
2870 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002871 assert_int_equal(test_element_helper(st, data, &inc_meta, NULL, NULL), LY_EVALID);
David Sedlák0c2bab92019-07-22 15:33:19 +02002872 logbuf_assert("Invalid sub-elemnt \"description\" of \"include\" element - this sub-element is allowed only in modules with version 1.1 or newer. Line number 1.");
2873 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2874 includes = NULL;
2875
2876 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2877 data = ELEMENT_WRAPPER_START
2878 "<include module=\"mod\">"
2879 "<reference><text>ref</text></reference>"
2880 "<revision-date date=\"1999-09-09\"/>"
2881 "</include>"
2882 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002883 assert_int_equal(test_element_helper(st, data, &inc_meta, NULL, NULL), LY_EVALID);
David Sedlák0c2bab92019-07-22 15:33:19 +02002884 logbuf_assert("Invalid sub-elemnt \"reference\" of \"include\" element - this sub-element is allowed only in modules with version 1.1 or newer. Line number 1.");
2885 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2886 includes = NULL;
2887
2888 st->finished_correctly = true;
2889}
2890
David Sedlák5e13dea2019-07-22 16:06:45 +02002891static void
David Sedlákaf536aa2019-07-23 13:42:23 +02002892test_list_elem(void **state)
2893{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002894 struct test_parser_yin_state *st = *state;
David Sedlákaf536aa2019-07-23 13:42:23 +02002895 const char *data;
2896 struct lysp_node *siblings = NULL;
2897 struct tree_node_meta node_meta = {NULL, &siblings};
2898 struct lysp_node_list *parsed = NULL;
2899
2900 /* max subelems */
2901 data = ELEMENT_WRAPPER_START
2902 "<list name=\"list-name\">"
2903 "<when condition=\"when\"/>"
2904 "<if-feature name=\"iff\"/>"
2905 "<must condition=\"must-cond\"/>"
2906 "<key value=\"key\"/>"
2907 "<unique tag=\"utag\"/>"
2908 "<config value=\"true\"/>"
2909 "<min-elements value=\"10\"/>"
2910 "<ordered-by value=\"user\"/>"
2911 "<status value=\"deprecated\"/>"
2912 "<description><text>desc</text></description>"
2913 "<reference><text>ref</text></reference>"
2914 "<anydata name=\"anyd\"/>"
2915 "<anyxml name=\"anyx\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02002916 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002917 "<choice name=\"choice\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02002918 "<action name=\"action\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02002919 "<grouping name=\"grp\"/>"
David Sedlák031b9e72019-07-23 15:19:37 +02002920 "<notification name=\"notf\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002921 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
2922 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlákaf536aa2019-07-23 13:42:23 +02002923 "<list name=\"sub-list\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002924 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákaf536aa2019-07-23 13:42:23 +02002925 "<uses name=\"uses-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02002926 EXT_SUBELEM
David Sedlákaf536aa2019-07-23 13:42:23 +02002927 "</list>"
2928 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002929 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákaf536aa2019-07-23 13:42:23 +02002930 parsed = (struct lysp_node_list *)&siblings[0];
2931 assert_string_equal(parsed->dsc, "desc");
2932 assert_string_equal(parsed->child->name, "anyd");
2933 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2934 assert_string_equal(parsed->child->next->name, "anyx");
2935 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
David Sedlákf111bcb2019-07-23 17:15:51 +02002936 assert_string_equal(parsed->child->next->next->name, "cont");
2937 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002938 assert_string_equal(parsed->child->next->next->next->name, "choice");
2939 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
David Sedlák85d0eca2019-07-24 15:15:21 +02002940 assert_string_equal(parsed->child->next->next->next->next->name, "leaf");
2941 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAF);
2942 assert_string_equal(parsed->child->next->next->next->next->next->name, "llist");
2943 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2944 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "sub-list");
2945 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LIST);
2946 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "uses-name");
2947 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_USES);
2948 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002949 assert_string_equal(parsed->groupings->name, "grp");
David Sedlák85d0eca2019-07-24 15:15:21 +02002950 assert_string_equal(parsed->actions->name, "action");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002951 assert_int_equal(parsed->groupings->nodetype, LYS_GROUPING);
David Sedlák031b9e72019-07-23 15:19:37 +02002952 assert_string_equal(parsed->notifs->name, "notf");
David Sedlákaf536aa2019-07-23 13:42:23 +02002953 assert_true(parsed->flags & LYS_ORDBY_USER);
2954 assert_true(parsed->flags & LYS_STATUS_DEPRC);
2955 assert_true(parsed->flags & LYS_CONFIG_W);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002956 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlákaf536aa2019-07-23 13:42:23 +02002957 assert_string_equal(parsed->key, "key");
2958 assert_int_equal(parsed->min, 10);
Michal Vasko7f45cf22020-10-01 12:49:44 +02002959 assert_string_equal(parsed->musts->arg.str, "must-cond");
David Sedlákaf536aa2019-07-23 13:42:23 +02002960 assert_string_equal(parsed->name, "list-name");
2961 assert_null(parsed->next);
2962 assert_int_equal(parsed->nodetype, LYS_LIST);
2963 assert_null(parsed->parent);
2964 assert_string_equal(parsed->ref, "ref");
2965 assert_string_equal(parsed->typedefs->name, "tpdf");
Michal Vasko7f45cf22020-10-01 12:49:44 +02002966 assert_string_equal(parsed->uniques->str, "utag");
David Sedlákaf536aa2019-07-23 13:42:23 +02002967 assert_string_equal(parsed->when->cond, "when");
David Sedláke0ef1c62019-09-13 10:05:55 +02002968 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02002969 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
2970 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákaf536aa2019-07-23 13:42:23 +02002971 lysp_node_free(st->ctx, siblings);
2972 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
2973 siblings = NULL;
2974
2975 /* min subelems */
2976 data = ELEMENT_WRAPPER_START "<list name=\"list-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01002977 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákaf536aa2019-07-23 13:42:23 +02002978 parsed = (struct lysp_node_list *)&siblings[0];
2979 assert_string_equal(parsed->name, "list-name");
2980 lysp_node_free(st->ctx, siblings);
2981 siblings = NULL;
2982
2983 st->finished_correctly = true;
2984}
2985
David Sedlák031b9e72019-07-23 15:19:37 +02002986static void
2987test_notification_elem(void **state)
2988{
Radek Krejci3a4889a2020-05-19 17:01:58 +02002989 struct test_parser_yin_state *st = *state;
David Sedlák031b9e72019-07-23 15:19:37 +02002990 const char *data;
2991 struct lysp_notif *notifs = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02002992 struct tree_node_meta notif_meta = {NULL, (struct lysp_node **)&notifs};
David Sedlák031b9e72019-07-23 15:19:37 +02002993
2994 /* max subelems */
2995 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2996 data = ELEMENT_WRAPPER_START
2997 "<notification name=\"notif-name\">"
2998 "<anydata name=\"anyd\"/>"
2999 "<anyxml name=\"anyx\"/>"
3000 "<description><text>desc</text></description>"
3001 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003002 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3003 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák031b9e72019-07-23 15:19:37 +02003004 "<list name=\"sub-list\"/>"
3005 "<must condition=\"cond\"/>"
3006 "<reference><text>ref</text></reference>"
3007 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003008 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák031b9e72019-07-23 15:19:37 +02003009 "<uses name=\"uses-name\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02003010 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003011 "<choice name=\"choice\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02003012 "<grouping name=\"grp\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003013 EXT_SUBELEM
David Sedlák031b9e72019-07-23 15:19:37 +02003014 "</notification>"
3015 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003016 assert_int_equal(test_element_helper(st, data, &notif_meta, NULL, NULL), LY_SUCCESS);
David Sedlák031b9e72019-07-23 15:19:37 +02003017 assert_string_equal(notifs->name, "notif-name");
3018 assert_string_equal(notifs->data->name, "anyd");
3019 assert_int_equal(notifs->data->nodetype, LYS_ANYDATA);
3020 assert_string_equal(notifs->data->next->name, "anyx");
3021 assert_int_equal(notifs->data->next->nodetype, LYS_ANYXML);
3022 assert_string_equal(notifs->data->next->next->name, "leaf");
3023 assert_int_equal(notifs->data->next->next->nodetype, LYS_LEAF);
3024 assert_string_equal(notifs->data->next->next->next->name, "llist");
3025 assert_int_equal(notifs->data->next->next->next->nodetype, LYS_LEAFLIST);
3026 assert_string_equal(notifs->data->next->next->next->next->name, "sub-list");
3027 assert_int_equal(notifs->data->next->next->next->next->nodetype, LYS_LIST);
David Sedlák031b9e72019-07-23 15:19:37 +02003028 assert_true(notifs->flags & LYS_STATUS_DEPRC);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003029 assert_string_equal(notifs->groupings->name, "grp");
3030 assert_int_equal(notifs->groupings->nodetype, LYS_GROUPING);
David Sedlákf111bcb2019-07-23 17:15:51 +02003031 assert_string_equal(notifs->data->next->next->next->next->next->name, "uses-name");
3032 assert_int_equal(notifs->data->next->next->next->next->next->nodetype, LYS_USES);
3033 assert_string_equal(notifs->data->next->next->next->next->next->next->name, "cont");
3034 assert_int_equal(notifs->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003035 assert_int_equal(notifs->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
3036 assert_string_equal(notifs->data->next->next->next->next->next->next->next->name, "choice");
3037 assert_null(notifs->data->next->next->next->next->next->next->next->next);
Michal Vasko7f45cf22020-10-01 12:49:44 +02003038 assert_string_equal(notifs->iffeatures[0].str, "iff");
3039 assert_string_equal(notifs->musts->arg.str, "cond");
David Sedlák031b9e72019-07-23 15:19:37 +02003040 assert_int_equal(notifs->nodetype, LYS_NOTIF);
3041 assert_null(notifs->parent);
3042 assert_string_equal(notifs->ref, "ref");
3043 assert_string_equal(notifs->typedefs->name, "tpdf");
David Sedláke0ef1c62019-09-13 10:05:55 +02003044 assert_string_equal(notifs->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003045 assert_int_equal(notifs->exts[0].insubstmt_index, 0);
3046 assert_int_equal(notifs->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák031b9e72019-07-23 15:19:37 +02003047 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
3048 notifs = NULL;
3049
3050 /* min subelems */
3051 data = ELEMENT_WRAPPER_START "<notification name=\"notif-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003052 assert_int_equal(test_element_helper(st, data, &notif_meta, NULL, NULL), LY_SUCCESS);
David Sedlák031b9e72019-07-23 15:19:37 +02003053 assert_string_equal(notifs->name, "notif-name");
3054 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003055 notifs = NULL;
3056
3057 st->finished_correctly = true;
3058}
3059
3060static void
3061test_grouping_elem(void **state)
3062{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003063 struct test_parser_yin_state *st = *state;
David Sedláke3ce9ef2019-07-23 16:34:30 +02003064 const char *data;
3065 struct lysp_grp *grps = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003066 struct tree_node_meta grp_meta = {NULL, (struct lysp_node **)&grps};
David Sedláke3ce9ef2019-07-23 16:34:30 +02003067
3068 /* max subelems */
3069 data = ELEMENT_WRAPPER_START
3070 "<grouping name=\"grp-name\">"
3071 "<anydata name=\"anyd\"/>"
3072 "<anyxml name=\"anyx\"/>"
3073 "<description><text>desc</text></description>"
3074 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003075 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3076 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02003077 "<list name=\"list\"/>"
3078 "<notification name=\"notf\"/>"
3079 "<reference><text>ref</text></reference>"
3080 "<status value=\"current\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003081 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02003082 "<uses name=\"uses-name\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02003083 "<action name=\"act\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02003084 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003085 "<choice name=\"choice\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003086 EXT_SUBELEM
David Sedláke3ce9ef2019-07-23 16:34:30 +02003087 "</grouping>"
3088 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003089 assert_int_equal(test_element_helper(st, data, &grp_meta, NULL, NULL), LY_SUCCESS);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003090 assert_string_equal(grps->name, "grp-name");
David Sedláke3ce9ef2019-07-23 16:34:30 +02003091 assert_string_equal(grps->data->name, "anyd");
3092 assert_string_equal(grps->data->next->name, "anyx");
3093 assert_string_equal(grps->data->next->next->name, "leaf");
3094 assert_string_equal(grps->data->next->next->next->name, "llist");
3095 assert_string_equal(grps->data->next->next->next->next->name, "list");
3096 assert_string_equal(grps->dsc, "desc");
David Sedláke3ce9ef2019-07-23 16:34:30 +02003097 assert_true(grps->flags & LYS_STATUS_CURR);
3098 assert_string_equal(grps->groupings->name, "sub-grp");
3099 assert_int_equal(grps->nodetype, LYS_GROUPING);
3100 assert_string_equal(grps->notifs->name, "notf");
3101 assert_null(grps->parent);
3102 assert_string_equal(grps->ref, "ref");
3103 assert_string_equal(grps->typedefs->name, "tpdf");
David Sedlák85d0eca2019-07-24 15:15:21 +02003104 assert_string_equal(grps->actions->name, "act");
David Sedlákf111bcb2019-07-23 17:15:51 +02003105 assert_string_equal(grps->data->next->next->next->next->next->name, "uses-name");
David Sedlákb7abcfa2019-07-24 12:33:35 +02003106 assert_int_equal(grps->data->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákf111bcb2019-07-23 17:15:51 +02003107 assert_string_equal(grps->data->next->next->next->next->next->next->name, "cont");
David Sedlákb7abcfa2019-07-24 12:33:35 +02003108 assert_int_equal(grps->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
3109 assert_string_equal(grps->data->next->next->next->next->next->next->next->name, "choice");
3110 assert_int_equal(grps->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
David Sedláke0ef1c62019-09-13 10:05:55 +02003111 assert_string_equal(grps->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003112 assert_int_equal(grps->exts[0].insubstmt_index, 0);
3113 assert_int_equal(grps->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003114 FREE_ARRAY(st->ctx, grps, lysp_grp_free);
3115 grps = NULL;
3116
3117 /* min subelems */
3118 data = ELEMENT_WRAPPER_START "<grouping name=\"grp-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003119 assert_int_equal(test_element_helper(st, data, &grp_meta, NULL, NULL), LY_SUCCESS);
David Sedláke3ce9ef2019-07-23 16:34:30 +02003120 assert_string_equal(grps->name, "grp-name");
3121 FREE_ARRAY(st->ctx, grps, lysp_grp_free);
3122 grps = NULL;
David Sedlák031b9e72019-07-23 15:19:37 +02003123
3124 st->finished_correctly = true;
3125}
3126
David Sedlákf111bcb2019-07-23 17:15:51 +02003127static void
3128test_container_elem(void **state)
3129{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003130 struct test_parser_yin_state *st = *state;
David Sedlákf111bcb2019-07-23 17:15:51 +02003131 const char *data;
3132 struct lysp_node *siblings = NULL;
3133 struct tree_node_meta node_meta = {NULL, &siblings};
3134 struct lysp_node_container *parsed = NULL;
3135
3136 /* max subelems */
David Sedláke2dc9e92019-07-24 09:59:21 +02003137 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3138 data = ELEMENT_WRAPPER_START
3139 "<container name=\"cont-name\">"
3140 "<anydata name=\"anyd\"/>"
3141 "<anyxml name=\"anyx\"/>"
3142 "<config value=\"true\"/>"
3143 "<container name=\"subcont\"/>"
3144 "<description><text>desc</text></description>"
3145 "<grouping name=\"sub-grp\"/>"
3146 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003147 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3148 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedláke2dc9e92019-07-24 09:59:21 +02003149 "<list name=\"list\"/>"
3150 "<must condition=\"cond\"/>"
3151 "<notification name=\"notf\"/>"
3152 "<presence value=\"presence\"/>"
3153 "<reference><text>ref</text></reference>"
3154 "<status value=\"current\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003155 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedláke2dc9e92019-07-24 09:59:21 +02003156 "<uses name=\"uses-name\"/>"
3157 "<when condition=\"when-cond\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02003158 "<action name=\"act\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003159 "<choice name=\"choice\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003160 EXT_SUBELEM
David Sedláke2dc9e92019-07-24 09:59:21 +02003161 "</container>"
3162 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003163 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedláke2dc9e92019-07-24 09:59:21 +02003164 parsed = (struct lysp_node_container *)siblings;
3165 assert_string_equal(parsed->name, "cont-name");
3166 assert_null(parsed->parent);
3167 assert_int_equal(parsed->nodetype, LYS_CONTAINER);
3168 assert_true(parsed->flags & LYS_CONFIG_W);
3169 assert_true(parsed->flags & LYS_STATUS_CURR);
3170 assert_null(parsed->next);
3171 assert_string_equal(parsed->dsc, "desc");
3172 assert_string_equal(parsed->ref, "ref");
3173 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003174 assert_string_equal(parsed->iffeatures[0].str, "iff");
3175 assert_string_equal(parsed->musts->arg.str, "cond");
David Sedláke2dc9e92019-07-24 09:59:21 +02003176 assert_string_equal(parsed->presence, "presence");
3177 assert_string_equal(parsed->typedefs->name, "tpdf");
3178 assert_string_equal(parsed->groupings->name, "sub-grp");
3179 assert_string_equal(parsed->child->name, "anyd");
3180 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
3181 assert_string_equal(parsed->child->next->name, "anyx");
3182 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
3183 assert_string_equal(parsed->child->next->next->name, "subcont");
3184 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
3185 assert_string_equal(parsed->child->next->next->next->name, "leaf");
3186 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
3187 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
3188 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
3189 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
3190 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
3191 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
3192 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003193 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
3194 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
3195 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke2dc9e92019-07-24 09:59:21 +02003196 assert_string_equal(parsed->notifs->name, "notf");
David Sedlák85d0eca2019-07-24 15:15:21 +02003197 assert_string_equal(parsed->actions->name, "act");
David Sedláke0ef1c62019-09-13 10:05:55 +02003198 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003199 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3200 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedláke2dc9e92019-07-24 09:59:21 +02003201 lysp_node_free(st->ctx, siblings);
3202 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
3203 siblings = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02003204
3205 /* min subelems */
3206 data = ELEMENT_WRAPPER_START "<container name=\"cont-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003207 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákf111bcb2019-07-23 17:15:51 +02003208 parsed = (struct lysp_node_container *)siblings;
3209 assert_string_equal(parsed->name, "cont-name");
3210 lysp_node_free(st->ctx, siblings);
3211 siblings = NULL;
3212
3213 st->finished_correctly = true;
3214}
3215
David Sedlák5379d392019-07-24 10:42:03 +02003216static void
3217test_case_elem(void **state)
3218{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003219 struct test_parser_yin_state *st = *state;
David Sedlák5379d392019-07-24 10:42:03 +02003220 const char *data;
3221 struct lysp_node *siblings = NULL;
3222 struct tree_node_meta node_meta = {NULL, &siblings};
3223 struct lysp_node_case *parsed = NULL;
3224
3225 /* max subelems */
3226 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3227 data = ELEMENT_WRAPPER_START
3228 "<case name=\"case-name\">"
3229 "<anydata name=\"anyd\"/>"
3230 "<anyxml name=\"anyx\"/>"
3231 "<container name=\"subcont\"/>"
3232 "<description><text>desc</text></description>"
3233 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003234 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3235 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák5379d392019-07-24 10:42:03 +02003236 "<list name=\"list\"/>"
3237 "<reference><text>ref</text></reference>"
3238 "<status value=\"current\"/>"
3239 "<uses name=\"uses-name\"/>"
3240 "<when condition=\"when-cond\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003241 "<choice name=\"choice\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003242 EXT_SUBELEM
David Sedlák5379d392019-07-24 10:42:03 +02003243 "</case>"
3244 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003245 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák5379d392019-07-24 10:42:03 +02003246 parsed = (struct lysp_node_case *)siblings;
3247 assert_string_equal(parsed->name, "case-name");
3248 assert_null(parsed->parent);
3249 assert_int_equal(parsed->nodetype, LYS_CASE);
3250 assert_true(parsed->flags & LYS_STATUS_CURR);
3251 assert_null(parsed->next);
3252 assert_string_equal(parsed->dsc, "desc");
3253 assert_string_equal(parsed->ref, "ref");
3254 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003255 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlák5379d392019-07-24 10:42:03 +02003256 assert_string_equal(parsed->child->name, "anyd");
3257 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
3258 assert_string_equal(parsed->child->next->name, "anyx");
3259 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
3260 assert_string_equal(parsed->child->next->next->name, "subcont");
3261 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
3262 assert_string_equal(parsed->child->next->next->next->name, "leaf");
3263 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
3264 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
3265 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
3266 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
3267 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
3268 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
3269 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003270 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
3271 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
3272 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke0ef1c62019-09-13 10:05:55 +02003273 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003274 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3275 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák5379d392019-07-24 10:42:03 +02003276 lysp_node_free(st->ctx, siblings);
3277 siblings = NULL;
3278
3279 /* min subelems */
3280 data = ELEMENT_WRAPPER_START "<case name=\"case-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003281 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlák5379d392019-07-24 10:42:03 +02003282 parsed = (struct lysp_node_case *)siblings;
3283 assert_string_equal(parsed->name, "case-name");
3284 lysp_node_free(st->ctx, siblings);
3285 siblings = NULL;
3286
3287 st->finished_correctly = true;
3288}
3289
David Sedlákb7abcfa2019-07-24 12:33:35 +02003290static void
3291test_choice_elem(void **state)
3292{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003293 struct test_parser_yin_state *st = *state;
David Sedlákb7abcfa2019-07-24 12:33:35 +02003294 const char *data;
3295 struct lysp_node *siblings = NULL;
3296 struct tree_node_meta node_meta = {NULL, &siblings};
3297 struct lysp_node_choice *parsed = NULL;
3298
3299 /* max subelems */
3300 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3301 data = ELEMENT_WRAPPER_START
3302 "<choice name=\"choice-name\">"
3303 "<anydata name=\"anyd\"/>"
3304 "<anyxml name=\"anyx\"/>"
3305 "<case name=\"sub-case\"/>"
3306 "<choice name=\"choice\"/>"
3307 "<config value=\"true\"/>"
3308 "<container name=\"subcont\"/>"
3309 "<default value=\"def\"/>"
3310 "<description><text>desc</text></description>"
3311 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003312 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3313 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02003314 "<list name=\"list\"/>"
3315 "<mandatory value=\"true\" />"
3316 "<reference><text>ref</text></reference>"
3317 "<status value=\"current\"/>"
3318 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003319 EXT_SUBELEM
David Sedlákb7abcfa2019-07-24 12:33:35 +02003320 "</choice>"
3321 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003322 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003323 parsed = (struct lysp_node_choice *)siblings;
3324 assert_string_equal(parsed->name, "choice-name");
3325 assert_null(parsed->parent);
3326 assert_int_equal(parsed->nodetype, LYS_CHOICE);
3327 assert_true(parsed->flags & LYS_CONFIG_W && parsed->flags & LYS_MAND_TRUE && parsed->flags & LYS_STATUS_CURR);
3328 assert_null(parsed->next);
3329 assert_string_equal(parsed->dsc, "desc");
3330 assert_string_equal(parsed->ref, "ref");
3331 assert_string_equal(parsed->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003332 assert_string_equal(parsed->iffeatures[0].str, "iff");
David Sedlákb7abcfa2019-07-24 12:33:35 +02003333 assert_string_equal(parsed->child->name, "anyd");
3334 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
3335 assert_string_equal(parsed->child->next->name, "anyx");
3336 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
3337 assert_string_equal(parsed->child->next->next->name, "sub-case");
3338 assert_int_equal(parsed->child->next->next->nodetype, LYS_CASE);
3339 assert_string_equal(parsed->child->next->next->next->name, "choice");
3340 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
3341 assert_string_equal(parsed->child->next->next->next->next->name, "subcont");
3342 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_CONTAINER);
3343 assert_string_equal(parsed->child->next->next->next->next->next->name, "leaf");
3344 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAF);
3345 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "llist");
3346 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3347 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "list");
3348 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
3349 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke0ef1c62019-09-13 10:05:55 +02003350 assert_string_equal(parsed->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003351 assert_int_equal(parsed->exts[0].insubstmt_index, 0);
3352 assert_int_equal(parsed->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003353 lysp_node_free(st->ctx, siblings);
3354 siblings = NULL;
3355
3356 /* min subelems */
3357 data = ELEMENT_WRAPPER_START "<choice name=\"choice-name\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003358 assert_int_equal(test_element_helper(st, data, &node_meta, NULL, NULL), LY_SUCCESS);
David Sedlákb7abcfa2019-07-24 12:33:35 +02003359 parsed = (struct lysp_node_choice *)siblings;
3360 assert_string_equal(parsed->name, "choice-name");
3361 lysp_node_free(st->ctx, siblings);
3362 siblings = NULL;
3363
3364 st->finished_correctly = true;
3365}
3366
David Sedlák05404f62019-07-24 14:11:53 +02003367static void
3368test_inout_elem(void **state)
3369{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003370 struct test_parser_yin_state *st = *state;
David Sedlák05404f62019-07-24 14:11:53 +02003371 const char *data;
3372 struct lysp_action_inout inout = {};
3373 struct inout_meta inout_meta = {NULL, &inout};
3374
3375 /* max subelements */
3376 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3377 data = ELEMENT_WRAPPER_START
3378 "<input>"
3379 "<anydata name=\"anyd\"/>"
3380 "<anyxml name=\"anyx\"/>"
3381 "<choice name=\"choice\"/>"
3382 "<container name=\"subcont\"/>"
3383 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003384 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3385 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák05404f62019-07-24 14:11:53 +02003386 "<list name=\"list\"/>"
3387 "<must condition=\"cond\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003388 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák05404f62019-07-24 14:11:53 +02003389 "<uses name=\"uses-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003390 EXT_SUBELEM
David Sedlák05404f62019-07-24 14:11:53 +02003391 "</input>"
3392 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003393 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_SUCCESS);
David Sedlák05404f62019-07-24 14:11:53 +02003394 assert_null(inout.parent);
3395 assert_int_equal(inout.nodetype, LYS_INPUT);
Michal Vasko7f45cf22020-10-01 12:49:44 +02003396 assert_string_equal(inout.musts->arg.str, "cond");
David Sedlák05404f62019-07-24 14:11:53 +02003397 assert_string_equal(inout.typedefs->name, "tpdf");
3398 assert_string_equal(inout.groupings->name, "sub-grp");
3399 assert_string_equal(inout.data->name, "anyd");
3400 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
3401 assert_string_equal(inout.data->next->name, "anyx");
3402 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
3403 assert_string_equal(inout.data->next->next->name, "choice");
3404 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
3405 assert_string_equal(inout.data->next->next->next->name, "subcont");
3406 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
3407 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
3408 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
3409 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
3410 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3411 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
3412 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
3413 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
3414 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3415 assert_null(inout.data->next->next->next->next->next->next->next->next);
David Sedláke0ef1c62019-09-13 10:05:55 +02003416 assert_string_equal(inout.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003417 assert_int_equal(inout.exts[0].insubstmt_index, 0);
3418 assert_int_equal(inout.exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák05404f62019-07-24 14:11:53 +02003419 lysp_action_inout_free(st->ctx, &inout);
3420 memset(&inout, 0, sizeof inout);
3421
3422 /* max subelements */
3423 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3424 data = ELEMENT_WRAPPER_START
3425 "<output>"
3426 "<anydata name=\"anyd\"/>"
3427 "<anyxml name=\"anyx\"/>"
3428 "<choice name=\"choice\"/>"
3429 "<container name=\"subcont\"/>"
3430 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003431 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3432 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák05404f62019-07-24 14:11:53 +02003433 "<list name=\"list\"/>"
3434 "<must condition=\"cond\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003435 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák05404f62019-07-24 14:11:53 +02003436 "<uses name=\"uses-name\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003437 EXT_SUBELEM
David Sedlák05404f62019-07-24 14:11:53 +02003438 "</output>"
3439 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003440 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_SUCCESS);
David Sedlák05404f62019-07-24 14:11:53 +02003441 assert_null(inout.parent);
3442 assert_int_equal(inout.nodetype, LYS_OUTPUT);
Michal Vasko7f45cf22020-10-01 12:49:44 +02003443 assert_string_equal(inout.musts->arg.str, "cond");
David Sedlák05404f62019-07-24 14:11:53 +02003444 assert_string_equal(inout.typedefs->name, "tpdf");
3445 assert_string_equal(inout.groupings->name, "sub-grp");
3446 assert_string_equal(inout.data->name, "anyd");
3447 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
3448 assert_string_equal(inout.data->next->name, "anyx");
3449 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
3450 assert_string_equal(inout.data->next->next->name, "choice");
3451 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
3452 assert_string_equal(inout.data->next->next->next->name, "subcont");
3453 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
3454 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
3455 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
3456 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
3457 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3458 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
3459 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
3460 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
3461 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3462 assert_null(inout.data->next->next->next->next->next->next->next->next);
David Sedláke0ef1c62019-09-13 10:05:55 +02003463 assert_string_equal(inout.exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003464 assert_int_equal(inout.exts[0].insubstmt_index, 0);
3465 assert_int_equal(inout.exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák05404f62019-07-24 14:11:53 +02003466 lysp_action_inout_free(st->ctx, &inout);
3467 memset(&inout, 0, sizeof inout);
3468
3469 /* min subelems */
Michal Vaskob83af8a2020-01-06 09:49:22 +01003470 data = ELEMENT_WRAPPER_START "<input><leaf name=\"l\"><type name=\"empty\"/></leaf></input>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003471 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_SUCCESS);
Michal Vaskob83af8a2020-01-06 09:49:22 +01003472 lysp_action_inout_free(st->ctx, &inout);
David Sedlák05404f62019-07-24 14:11:53 +02003473 memset(&inout, 0, sizeof inout);
3474
Michal Vaskob83af8a2020-01-06 09:49:22 +01003475 data = ELEMENT_WRAPPER_START "<output><leaf name=\"l\"><type name=\"empty\"/></leaf></output>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003476 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_SUCCESS);
Michal Vaskob83af8a2020-01-06 09:49:22 +01003477 lysp_action_inout_free(st->ctx, &inout);
David Sedlák05404f62019-07-24 14:11:53 +02003478 memset(&inout, 0, sizeof inout);
3479
3480 /* invalid combinations */
3481 data = ELEMENT_WRAPPER_START "<input name=\"test\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003482 assert_int_equal(test_element_helper(st, data, &inout_meta, NULL, NULL), LY_EVALID);
David Sedlák1538a842019-08-08 15:38:51 +02003483 logbuf_assert("Unexpected attribute \"name\" of \"input\" element. Line number 1.");
David Sedlák05404f62019-07-24 14:11:53 +02003484 memset(&inout, 0, sizeof inout);
3485
3486 st->finished_correctly = true;
3487}
3488
David Sedlák85d0eca2019-07-24 15:15:21 +02003489static void
3490test_action_elem(void **state)
3491{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003492 struct test_parser_yin_state *st = *state;
David Sedlák85d0eca2019-07-24 15:15:21 +02003493 const char *data;
3494 struct lysp_action *actions = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003495 struct tree_node_meta act_meta = {NULL, (struct lysp_node **)&actions};
David Sedlák85d0eca2019-07-24 15:15:21 +02003496
3497 /* max subelems */
3498 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3499 data = ELEMENT_WRAPPER_START
3500 "<action name=\"act\">"
3501 "<description><text>desc</text></description>"
3502 "<grouping name=\"grouping\"/>"
3503 "<if-feature name=\"iff\"/>"
3504 "<input><uses name=\"uses-name\"/></input>"
Michal Vaskob83af8a2020-01-06 09:49:22 +01003505 "<output><must condition=\"cond\"/><leaf name=\"l\"><type name=\"type\"/></leaf></output>"
David Sedlák85d0eca2019-07-24 15:15:21 +02003506 "<reference><text>ref</text></reference>"
3507 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003508 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákd1144562019-08-06 12:36:14 +02003509 EXT_SUBELEM
David Sedlák85d0eca2019-07-24 15:15:21 +02003510 "</action>"
3511 ELEMENT_WRAPPER_END;
Michal Vasko1bf09392020-03-27 12:38:10 +01003512 /* there must be parent for action */
Radek Krejcif482abc2020-08-27 15:29:05 +02003513 act_meta.parent = (void*)1;
Michal Vaskob36053d2020-03-26 15:49:30 +01003514 assert_int_equal(test_element_helper(st, data, &act_meta, NULL, NULL), LY_SUCCESS);
Michal Vasko1bf09392020-03-27 12:38:10 +01003515 act_meta.parent = NULL;
3516 assert_non_null(actions->parent);
David Sedlák85d0eca2019-07-24 15:15:21 +02003517 assert_int_equal(actions->nodetype, LYS_ACTION);
3518 assert_true(actions->flags & LYS_STATUS_DEPRC);
3519 assert_string_equal(actions->name, "act");
3520 assert_string_equal(actions->dsc, "desc");
3521 assert_string_equal(actions->ref, "ref");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003522 assert_string_equal(actions->iffeatures[0].str, "iff");
David Sedlák85d0eca2019-07-24 15:15:21 +02003523 assert_string_equal(actions->typedefs->name, "tpdf");
3524 assert_string_equal(actions->groupings->name, "grouping");
3525 assert_string_equal(actions->input.data->name, "uses-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003526 assert_string_equal(actions->output.musts->arg.str, "cond");
David Sedláke0ef1c62019-09-13 10:05:55 +02003527 assert_string_equal(actions->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003528 assert_int_equal(actions->exts[0].insubstmt_index, 0);
3529 assert_int_equal(actions->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák85d0eca2019-07-24 15:15:21 +02003530 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3531 actions = NULL;
3532
David Sedlákeaa45792019-07-24 15:25:01 +02003533 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3534 data = ELEMENT_WRAPPER_START
3535 "<rpc name=\"act\">"
3536 "<description><text>desc</text></description>"
3537 "<grouping name=\"grouping\"/>"
3538 "<if-feature name=\"iff\"/>"
3539 "<input><uses name=\"uses-name\"/></input>"
Michal Vaskob83af8a2020-01-06 09:49:22 +01003540 "<output><must condition=\"cond\"/><leaf name=\"l\"><type name=\"type\"/></leaf></output>"
David Sedlákeaa45792019-07-24 15:25:01 +02003541 "<reference><text>ref</text></reference>"
3542 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003543 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákd1144562019-08-06 12:36:14 +02003544 EXT_SUBELEM
David Sedlákeaa45792019-07-24 15:25:01 +02003545 "</rpc>"
3546 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003547 assert_int_equal(test_element_helper(st, data, &act_meta, NULL, NULL), LY_SUCCESS);
David Sedlákeaa45792019-07-24 15:25:01 +02003548 assert_null(actions->parent);
Michal Vasko1bf09392020-03-27 12:38:10 +01003549 assert_int_equal(actions->nodetype, LYS_RPC);
David Sedlákeaa45792019-07-24 15:25:01 +02003550 assert_true(actions->flags & LYS_STATUS_DEPRC);
3551 assert_string_equal(actions->name, "act");
3552 assert_string_equal(actions->dsc, "desc");
3553 assert_string_equal(actions->ref, "ref");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003554 assert_string_equal(actions->iffeatures[0].str, "iff");
David Sedlákeaa45792019-07-24 15:25:01 +02003555 assert_string_equal(actions->typedefs->name, "tpdf");
3556 assert_string_equal(actions->groupings->name, "grouping");
3557 assert_string_equal(actions->input.data->name, "uses-name");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003558 assert_string_equal(actions->output.musts->arg.str, "cond");
David Sedláke0ef1c62019-09-13 10:05:55 +02003559 assert_string_equal(actions->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003560 assert_int_equal(actions->exts[0].insubstmt_index, 0);
3561 assert_int_equal(actions->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlákeaa45792019-07-24 15:25:01 +02003562 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3563 actions = NULL;
3564
David Sedlák85d0eca2019-07-24 15:15:21 +02003565 /* min subelems */
3566 data = ELEMENT_WRAPPER_START "<action name=\"act\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003567 assert_int_equal(test_element_helper(st, data, &act_meta, NULL, NULL), LY_SUCCESS);
David Sedlák85d0eca2019-07-24 15:15:21 +02003568 assert_string_equal(actions->name, "act");
3569 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3570 actions = NULL;
3571
3572 st->finished_correctly = true;
3573}
3574
David Sedlák992fb7c2019-07-24 16:51:01 +02003575static void
3576test_augment_elem(void **state)
3577{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003578 struct test_parser_yin_state *st = *state;
David Sedlák992fb7c2019-07-24 16:51:01 +02003579 const char *data;
3580 struct lysp_augment *augments = NULL;
David Sedlák6881b512019-08-13 12:52:00 +02003581 struct tree_node_meta aug_meta = {NULL, (struct lysp_node **)&augments};
David Sedlák992fb7c2019-07-24 16:51:01 +02003582
3583 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3584 data = ELEMENT_WRAPPER_START
3585 "<augment target-node=\"target\">"
3586 "<action name=\"action\"/>"
3587 "<anydata name=\"anyd\"/>"
3588 "<anyxml name=\"anyx\"/>"
3589 "<case name=\"case\"/>"
3590 "<choice name=\"choice\"/>"
3591 "<container name=\"subcont\"/>"
3592 "<description><text>desc</text></description>"
3593 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003594 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3595 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák992fb7c2019-07-24 16:51:01 +02003596 "<list name=\"list\"/>"
3597 "<notification name=\"notif\"/>"
3598 "<reference><text>ref</text></reference>"
3599 "<status value=\"current\"/>"
3600 "<uses name=\"uses\"/>"
3601 "<when condition=\"when-cond\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003602 EXT_SUBELEM
David Sedlák992fb7c2019-07-24 16:51:01 +02003603 "</augment>"
3604 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003605 assert_int_equal(test_element_helper(st, data, &aug_meta, NULL, NULL), LY_SUCCESS);
David Sedlák992fb7c2019-07-24 16:51:01 +02003606 assert_string_equal(augments->nodeid, "target");
3607 assert_null(augments->parent);
3608 assert_int_equal(augments->nodetype, LYS_AUGMENT);
3609 assert_true(augments->flags & LYS_STATUS_CURR);
3610 assert_string_equal(augments->dsc, "desc");
3611 assert_string_equal(augments->ref, "ref");
3612 assert_string_equal(augments->when->cond, "when-cond");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003613 assert_string_equal(augments->iffeatures[0].str, "iff");
David Sedlák992fb7c2019-07-24 16:51:01 +02003614 assert_string_equal(augments->child->name, "anyd");
3615 assert_int_equal(augments->child->nodetype, LYS_ANYDATA);
3616 assert_string_equal(augments->child->next->name, "anyx");
3617 assert_int_equal(augments->child->next->nodetype, LYS_ANYXML);
3618 assert_string_equal(augments->child->next->next->name, "case");
3619 assert_int_equal(augments->child->next->next->nodetype, LYS_CASE);
3620 assert_string_equal(augments->child->next->next->next->name, "choice");
3621 assert_int_equal(augments->child->next->next->next->nodetype, LYS_CHOICE);
3622 assert_string_equal(augments->child->next->next->next->next->name, "subcont");
3623 assert_int_equal(augments->child->next->next->next->next->nodetype, LYS_CONTAINER);
3624 assert_string_equal(augments->child->next->next->next->next->next->name, "leaf");
3625 assert_int_equal(augments->child->next->next->next->next->next->nodetype, LYS_LEAF);
3626 assert_string_equal(augments->child->next->next->next->next->next->next->name, "llist");
3627 assert_int_equal(augments->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3628 assert_string_equal(augments->child->next->next->next->next->next->next->next->name, "list");
3629 assert_int_equal(augments->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
3630 assert_string_equal(augments->child->next->next->next->next->next->next->next->next->name, "uses");
3631 assert_int_equal(augments->child->next->next->next->next->next->next->next->next->nodetype, LYS_USES);
3632 assert_null(augments->child->next->next->next->next->next->next->next->next->next);
3633 assert_string_equal(augments->actions->name, "action");
3634 assert_string_equal(augments->notifs->name, "notif");
David Sedláke0ef1c62019-09-13 10:05:55 +02003635 assert_string_equal(augments->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003636 assert_int_equal(augments->exts[0].insubstmt_index, 0);
3637 assert_int_equal(augments->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák992fb7c2019-07-24 16:51:01 +02003638 FREE_ARRAY(st->ctx, augments, lysp_augment_free)
3639 augments = NULL;
3640
3641 data = ELEMENT_WRAPPER_START "<augment target-node=\"target\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003642 assert_int_equal(test_element_helper(st, data, &aug_meta, NULL, NULL), LY_SUCCESS);
David Sedlák992fb7c2019-07-24 16:51:01 +02003643 assert_string_equal(augments->nodeid, "target");
3644 FREE_ARRAY(st->ctx, augments, lysp_augment_free)
3645 augments = NULL;
3646
3647 st->finished_correctly = true;
3648}
3649
David Sedlák4ffcec82019-07-25 15:10:21 +02003650static void
3651test_deviate_elem(void **state)
3652{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003653 struct test_parser_yin_state *st = *state;
David Sedlák4ffcec82019-07-25 15:10:21 +02003654 const char *data;
3655 struct lysp_deviate *deviates = NULL;
3656 struct lysp_deviate_add *d_add;
3657 struct lysp_deviate_rpl *d_rpl;
3658 struct lysp_deviate_del *d_del;
3659
3660 /* all valid arguments with min subelems */
3661 data = ELEMENT_WRAPPER_START "<deviate value=\"not-supported\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003662 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003663 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
3664 lysp_deviate_free(st->ctx, deviates);
3665 free(deviates);
3666 deviates = NULL;
3667
3668 data = ELEMENT_WRAPPER_START "<deviate value=\"add\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003669 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003670 assert_int_equal(deviates->mod, LYS_DEV_ADD);
3671 lysp_deviate_free(st->ctx, deviates);
3672 free(deviates);
3673 deviates = NULL;
3674
3675 data = ELEMENT_WRAPPER_START "<deviate value=\"replace\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003676 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003677 assert_int_equal(deviates->mod, LYS_DEV_REPLACE);
3678 lysp_deviate_free(st->ctx, deviates);
3679 free(deviates);
3680 deviates = NULL;
3681
3682 data = ELEMENT_WRAPPER_START "<deviate value=\"delete\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003683 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003684 assert_int_equal(deviates->mod, LYS_DEV_DELETE);
3685 lysp_deviate_free(st->ctx, deviates);
3686 free(deviates);
3687 deviates = NULL;
3688
3689 /* max subelems and valid arguments */
3690 data = ELEMENT_WRAPPER_START
3691 "<deviate value=\"not-supported\">"
David Sedlákd1144562019-08-06 12:36:14 +02003692 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003693 "</deviate>"
3694 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003695 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003696 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
David Sedláke0ef1c62019-09-13 10:05:55 +02003697 assert_string_equal(deviates->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003698 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3699 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003700 lysp_deviate_free(st->ctx, deviates);
3701 free(deviates);
3702 deviates = NULL;
3703
3704 data = ELEMENT_WRAPPER_START
3705 "<deviate value=\"add\">"
3706 "<units name=\"units\"/>"
3707 "<must condition=\"cond\"/>"
3708 "<unique tag=\"utag\"/>"
3709 "<default value=\"def\"/>"
3710 "<config value=\"true\"/>"
3711 "<mandatory value=\"true\"/>"
3712 "<min-elements value=\"5\"/>"
3713 "<max-elements value=\"15\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003714 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003715 "</deviate>"
3716 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003717 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003718 d_add = (struct lysp_deviate_add *)deviates;
3719 assert_int_equal(d_add->mod, LYS_DEV_ADD);
3720 assert_null(d_add->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003721 assert_string_equal(d_add->units, "units");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003722 assert_string_equal(d_add->musts->arg.str, "cond");
David Sedlák4ffcec82019-07-25 15:10:21 +02003723 assert_string_equal(*d_add->uniques, "utag");
3724 assert_string_equal(*d_add->dflts, "def");
3725 assert_true(d_add->flags & LYS_MAND_TRUE && d_add->flags & LYS_CONFIG_W);
3726 assert_int_equal(d_add->min, 5);
3727 assert_int_equal(d_add->max, 15);
David Sedláke0ef1c62019-09-13 10:05:55 +02003728 assert_string_equal(deviates->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003729 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3730 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003731 lysp_deviate_free(st->ctx, deviates);
3732 free(deviates);
3733 deviates = NULL;
3734
3735 data = ELEMENT_WRAPPER_START
3736 "<deviate value=\"replace\">"
3737 "<type name=\"newtype\"/>"
3738 "<units name=\"uni\"/>"
3739 "<default value=\"def\"/>"
3740 "<config value=\"true\"/>"
3741 "<mandatory value=\"true\"/>"
3742 "<min-elements value=\"5\"/>"
3743 "<max-elements value=\"15\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003744 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003745 "</deviate>"
3746 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003747 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003748 d_rpl = (struct lysp_deviate_rpl *)deviates;
3749 assert_int_equal(d_rpl->mod, LYS_DEV_REPLACE);
3750 assert_null(d_rpl->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003751 assert_string_equal(d_rpl->type->name, "newtype");
3752 assert_string_equal(d_rpl->units, "uni");
3753 assert_string_equal(d_rpl->dflt, "def");
3754 assert_true(d_rpl->flags & LYS_MAND_TRUE && d_rpl->flags & LYS_CONFIG_W);
3755 assert_int_equal(d_rpl->min, 5);
3756 assert_int_equal(d_rpl->max, 15);
David Sedláke0ef1c62019-09-13 10:05:55 +02003757 assert_string_equal(deviates->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003758 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3759 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003760 lysp_deviate_free(st->ctx, deviates);
3761 free(deviates);
3762 deviates = NULL;
3763
3764 data = ELEMENT_WRAPPER_START
3765 "<deviate value=\"delete\">"
3766 "<units name=\"u\"/>"
3767 "<must condition=\"c\"/>"
3768 "<unique tag=\"tag\"/>"
3769 "<default value=\"default\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003770 EXT_SUBELEM
David Sedlák4ffcec82019-07-25 15:10:21 +02003771 "</deviate>"
3772 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003773 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_SUCCESS);
David Sedlák4ffcec82019-07-25 15:10:21 +02003774 d_del = (struct lysp_deviate_del *)deviates;
3775 assert_int_equal(d_del->mod, LYS_DEV_DELETE);
3776 assert_null(d_del->next);
David Sedlák4ffcec82019-07-25 15:10:21 +02003777 assert_string_equal(d_del->units, "u");
Michal Vasko7f45cf22020-10-01 12:49:44 +02003778 assert_string_equal(d_del->musts->arg.str, "c");
David Sedlák4ffcec82019-07-25 15:10:21 +02003779 assert_string_equal(*d_del->uniques, "tag");
3780 assert_string_equal(*d_del->dflts, "default");
David Sedláke0ef1c62019-09-13 10:05:55 +02003781 assert_string_equal(deviates->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003782 assert_int_equal(deviates->exts[0].insubstmt_index, 0);
3783 assert_int_equal(deviates->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4ffcec82019-07-25 15:10:21 +02003784 lysp_deviate_free(st->ctx, deviates);
3785 free(deviates);
3786 deviates = NULL;
3787
3788 /* invalid arguments */
3789 data = ELEMENT_WRAPPER_START "<deviate value=\"\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003790 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003791 logbuf_assert("Invalid value \"\" of \"value\" attribute in \"deviate\" element. Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\". Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003792 deviates = NULL;
3793
3794 data = ELEMENT_WRAPPER_START "<deviate value=\"invalid\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003795 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003796 logbuf_assert("Invalid value \"invalid\" of \"value\" attribute in \"deviate\" element. Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\". Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003797 deviates = NULL;
3798
3799 data = ELEMENT_WRAPPER_START "<deviate value=\"ad\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003800 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003801 logbuf_assert("Invalid value \"ad\" of \"value\" attribute in \"deviate\" element. Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\". Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003802 deviates = NULL;
3803
3804 data = ELEMENT_WRAPPER_START "<deviate value=\"adds\" />" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003805 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_EVALID);
David Sedlák26ea1432019-08-14 13:42:23 +02003806 logbuf_assert("Invalid value \"adds\" of \"value\" attribute in \"deviate\" element. Valid values are \"not-supported\", \"add\", \"replace\" and \"delete\". Line number 1.");
David Sedlák4ffcec82019-07-25 15:10:21 +02003807 deviates = NULL;
3808
3809 data = ELEMENT_WRAPPER_START
3810 "<deviate value=\"not-supported\">"
3811 "<must condition=\"c\"/>"
3812 "</deviate>"
3813 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003814 assert_int_equal(test_element_helper(st, data, &deviates, NULL, NULL), LY_EVALID);
David Sedlák4ffcec82019-07-25 15:10:21 +02003815 logbuf_assert("Deviate of this type doesn't allow \"must\" as it's sub-element. Line number 1.");
3816
3817 st->finished_correctly = true;
3818}
3819
David Sedlák8b754462019-07-25 16:22:13 +02003820static void
3821test_deviation_elem(void **state)
3822{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003823 struct test_parser_yin_state *st = *state;
David Sedlák8b754462019-07-25 16:22:13 +02003824 const char *data;
3825 struct lysp_deviation *deviations = NULL;
3826
3827 /* min subelems */
3828 data = ELEMENT_WRAPPER_START
3829 "<deviation target-node=\"target\">"
3830 "<deviate value=\"not-supported\"/>"
3831 "</deviation>"
3832 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003833 assert_int_equal(test_element_helper(st, data, &deviations, NULL, NULL), LY_SUCCESS);
David Sedlák8b754462019-07-25 16:22:13 +02003834 assert_string_equal(deviations->nodeid, "target");
3835 assert_int_equal(deviations->deviates->mod, LYS_DEV_NOT_SUPPORTED);
3836 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3837 deviations = NULL;
3838
3839 /* max subelems */
3840 data = ELEMENT_WRAPPER_START
3841 "<deviation target-node=\"target\">"
3842 "<reference><text>ref</text></reference>"
3843 "<description><text>desc</text></description>"
3844 "<deviate value=\"add\"/>"
David Sedlákd1144562019-08-06 12:36:14 +02003845 EXT_SUBELEM
David Sedlák8b754462019-07-25 16:22:13 +02003846 "</deviation>"
3847 ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003848 assert_int_equal(test_element_helper(st, data, &deviations, NULL, NULL), LY_SUCCESS);
David Sedlák8b754462019-07-25 16:22:13 +02003849 assert_string_equal(deviations->nodeid, "target");
3850 assert_int_equal(deviations->deviates->mod, LYS_DEV_ADD);
3851 assert_string_equal(deviations->ref, "ref");
3852 assert_string_equal(deviations->dsc, "desc");
David Sedláke0ef1c62019-09-13 10:05:55 +02003853 assert_string_equal(deviations->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003854 assert_int_equal(deviations->exts[0].insubstmt_index, 0);
3855 assert_int_equal(deviations->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák8b754462019-07-25 16:22:13 +02003856 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3857 deviations = NULL;
3858
3859 /* invalid */
3860 data = ELEMENT_WRAPPER_START "<deviation target-node=\"target\"/>" ELEMENT_WRAPPER_END;
Michal Vaskob36053d2020-03-26 15:49:30 +01003861 assert_int_equal(test_element_helper(st, data, &deviations, NULL, NULL), LY_EVALID);
David Sedlák8b754462019-07-25 16:22:13 +02003862 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3863 deviations = NULL;
David Sedlák1538a842019-08-08 15:38:51 +02003864 logbuf_assert("Missing mandatory sub-element \"deviate\" of \"deviation\" element. Line number 1.");
3865 /* TODO */
David Sedlák8b754462019-07-25 16:22:13 +02003866 st->finished_correctly = true;
3867}
3868
David Sedlák4f03b932019-07-26 13:01:47 +02003869static void
3870test_module_elem(void **state)
3871{
Radek Krejci3a4889a2020-05-19 17:01:58 +02003872 struct test_parser_yin_state *st = *state;
Michal Vaskob36053d2020-03-26 15:49:30 +01003873 const char *data;
David Sedlák4f03b932019-07-26 13:01:47 +02003874 struct lys_module *lys_mod = NULL;
3875 struct lysp_module *lysp_mod = NULL;
3876
3877 /* max subelems */
David Sedlák4f03b932019-07-26 13:01:47 +02003878 lys_mod = calloc(1, sizeof *lys_mod);
3879 lysp_mod = calloc(1, sizeof *lysp_mod);
3880 lys_mod->ctx = st->ctx;
3881 lysp_mod->mod = lys_mod;
3882 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
3883 "<yang-version value=\"1.1\"/>\n"
3884 "<namespace uri=\"ns\"/>\n"
3885 "<prefix value=\"pref\"/>\n"
3886 "<include module=\"b-mod\"/>\n"
3887 "<import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
3888 "<organization><text>org</text></organization>\n"
3889 "<contact><text>contact</text></contact>\n"
3890 "<description><text>desc</text></description>"
3891 "<reference><text>ref</text></reference>\n"
3892 "<revision date=\"2019-02-02\"/>\n"
3893 "<anydata name=\"anyd\"/>\n"
3894 "<anyxml name=\"anyx\"/>\n"
3895 "<choice name=\"choice\"/>\n"
3896 "<container name=\"cont\"/>\n"
3897 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3898 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3899 "<list name=\"sub-list\"/>\n"
3900 "<uses name=\"uses-name\"/>\n"
3901 "<augment target-node=\"target\"/>\n"
3902 "<deviation target-node=\"target\">""<deviate value=\"not-supported\"/>""</deviation>\n"
3903 "<extension name=\"ext\"/>\n"
3904 "<feature name=\"feature\"/>\n"
3905 "<grouping name=\"grp\"/>\n"
3906 "<identity name=\"ident-name\"/>\n"
3907 "<notification name=\"notf\"/>\n"
3908 "<rpc name=\"rpc-name\"/>\n"
3909 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
David Sedlákd1144562019-08-06 12:36:14 +02003910 EXT_SUBELEM"\n"
David Sedlák4f03b932019-07-26 13:01:47 +02003911 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02003912 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
3913 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01003914
3915 assert_int_equal(yin_parse_mod(st->yin_ctx, lysp_mod), LY_SUCCESS);
David Sedlák4f03b932019-07-26 13:01:47 +02003916 assert_string_equal(lysp_mod->mod->name, "mod");
3917 assert_string_equal(lysp_mod->revs, "2019-02-02");
3918 assert_string_equal(lysp_mod->mod->ns, "ns");
3919 assert_string_equal(lysp_mod->mod->prefix, "pref");
3920 assert_null(lysp_mod->mod->filepath);
3921 assert_string_equal(lysp_mod->mod->org, "org");
3922 assert_string_equal(lysp_mod->mod->contact, "contact");
3923 assert_string_equal(lysp_mod->mod->dsc, "desc");
3924 assert_string_equal(lysp_mod->mod->ref, "ref");
3925 assert_int_equal(lysp_mod->mod->version, LYS_VERSION_1_1);
3926 assert_string_equal(lysp_mod->imports->name, "a-mod");
3927 assert_string_equal(lysp_mod->includes->name, "b-mod");
3928 assert_string_equal(lysp_mod->extensions->name, "ext");
3929 assert_string_equal(lysp_mod->features->name, "feature");
3930 assert_string_equal(lysp_mod->identities->name, "ident-name");
3931 assert_string_equal(lysp_mod->typedefs->name, "tpdf");
3932 assert_string_equal(lysp_mod->groupings->name, "grp");
3933 assert_string_equal(lysp_mod->data->name, "anyd");
3934 assert_int_equal(lysp_mod->data->nodetype, LYS_ANYDATA);
3935 assert_string_equal(lysp_mod->data->next->name, "anyx");
3936 assert_int_equal(lysp_mod->data->next->nodetype, LYS_ANYXML);
3937 assert_string_equal(lysp_mod->data->next->next->name, "choice");
3938 assert_int_equal(lysp_mod->data->next->next->nodetype, LYS_CHOICE);
3939 assert_string_equal(lysp_mod->data->next->next->next->name, "cont");
3940 assert_int_equal(lysp_mod->data->next->next->next->nodetype, LYS_CONTAINER);
3941 assert_string_equal(lysp_mod->data->next->next->next->next->name, "leaf");
3942 assert_int_equal(lysp_mod->data->next->next->next->next->nodetype, LYS_LEAF);
3943 assert_string_equal(lysp_mod->data->next->next->next->next->next->name, "llist");
3944 assert_int_equal(lysp_mod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3945 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->name, "sub-list");
3946 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
3947 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->next->name, "uses-name");
3948 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3949 assert_null(lysp_mod->data->next->next->next->next->next->next->next->next);
3950 assert_string_equal(lysp_mod->augments->nodeid, "target");
3951 assert_string_equal(lysp_mod->rpcs->name, "rpc-name");
3952 assert_string_equal(lysp_mod->notifs->name, "notf");
3953 assert_string_equal(lysp_mod->deviations->nodeid, "target");
David Sedláke0ef1c62019-09-13 10:05:55 +02003954 assert_string_equal(lysp_mod->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02003955 assert_int_equal(lysp_mod->exts[0].insubstmt_index, 0);
3956 assert_int_equal(lysp_mod->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák4f03b932019-07-26 13:01:47 +02003957 lysp_module_free(lysp_mod);
3958 lys_module_free(lys_mod, NULL);
David Sedlák4f03b932019-07-26 13:01:47 +02003959
3960 /* min subelems */
Michal Vasko63f3d842020-07-08 10:10:14 +02003961 ly_in_free(st->in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +01003962 lyxml_ctx_free(st->yin_ctx->xmlctx);
David Sedlák4f03b932019-07-26 13:01:47 +02003963 lys_mod = calloc(1, sizeof *lys_mod);
3964 lysp_mod = calloc(1, sizeof *lysp_mod);
3965 lys_mod->ctx = st->ctx;
3966 lysp_mod->mod = lys_mod;
3967 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">"
3968 "<namespace uri=\"ns\"/>"
3969 "<prefix value=\"pref\"/>"
3970 "<yang-version value=\"1.1\"/>"
3971 "</module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003972 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
3973 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01003974 assert_int_equal(yin_parse_mod(st->yin_ctx, lysp_mod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003975 assert_string_equal(lysp_mod->mod->name, "mod");
David Sedlák4f03b932019-07-26 13:01:47 +02003976 lysp_module_free(lysp_mod);
3977 lys_module_free(lys_mod, NULL);
David Sedlák298ff6d2019-07-26 14:29:03 +02003978
David Sedláke6cd89e2019-08-07 12:46:02 +02003979 /* incorrect subelem order */
Michal Vasko63f3d842020-07-08 10:10:14 +02003980 ly_in_free(st->in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +01003981 lyxml_ctx_free(st->yin_ctx->xmlctx);
David Sedláke6cd89e2019-08-07 12:46:02 +02003982 lys_mod = calloc(1, sizeof *lys_mod);
3983 lysp_mod = calloc(1, sizeof *lysp_mod);
3984 lys_mod->ctx = st->ctx;
3985 lysp_mod->mod = lys_mod;
3986 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">"
3987 "<feature name=\"feature\"/>\n"
3988 "<namespace uri=\"ns\"/>"
3989 "<prefix value=\"pref\"/>"
3990 "<yang-version value=\"1.1\"/>"
3991 "</module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02003992 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
3993 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01003994 assert_int_equal(yin_parse_mod(st->yin_ctx, lysp_mod), LY_EVALID);
3995 logbuf_assert("Invalid order of module\'s sub-elements \"namespace\" can\'t appear after \"feature\". Line number 2.");
David Sedláke6cd89e2019-08-07 12:46:02 +02003996 lysp_module_free(lysp_mod);
3997 lys_module_free(lys_mod, NULL);
David Sedláke6cd89e2019-08-07 12:46:02 +02003998
David Sedlák298ff6d2019-07-26 14:29:03 +02003999 st->finished_correctly = true;
4000}
4001
4002static void
4003test_submodule_elem(void **state)
4004{
Radek Krejci3a4889a2020-05-19 17:01:58 +02004005 struct test_parser_yin_state *st = *state;
Michal Vaskob36053d2020-03-26 15:49:30 +01004006 const char *data;
David Sedlák298ff6d2019-07-26 14:29:03 +02004007 struct lysp_submodule *lysp_submod = NULL;
4008
4009 /* max subelements */
David Sedlák298ff6d2019-07-26 14:29:03 +02004010 lysp_submod = calloc(1, sizeof *lysp_submod);
4011 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
4012 "<yang-version value=\"1.1\"/>\n"
4013 "<belongs-to module=\"mod-name\"><prefix value=\"pref\"/></belongs-to>"
4014 "<include module=\"b-mod\"/>\n"
4015 "<import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
4016 "<organization><text>org</text></organization>\n"
4017 "<contact><text>contact</text></contact>\n"
4018 "<description><text>desc</text></description>"
4019 "<reference><text>ref</text></reference>\n"
4020 "<revision date=\"2019-02-02\"/>\n"
4021 "<anydata name=\"anyd\"/>\n"
4022 "<anyxml name=\"anyx\"/>\n"
4023 "<choice name=\"choice\"/>\n"
4024 "<container name=\"cont\"/>\n"
4025 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
4026 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
4027 "<list name=\"sub-list\"/>\n"
4028 "<uses name=\"uses-name\"/>\n"
4029 "<augment target-node=\"target\"/>\n"
4030 "<deviation target-node=\"target\">""<deviate value=\"not-supported\"/>""</deviation>\n"
4031 "<extension name=\"ext\"/>\n"
4032 "<feature name=\"feature\"/>\n"
4033 "<grouping name=\"grp\"/>\n"
4034 "<identity name=\"ident-name\"/>\n"
4035 "<notification name=\"notf\"/>\n"
4036 "<rpc name=\"rpc-name\"/>\n"
4037 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
David Sedlákd1144562019-08-06 12:36:14 +02004038 EXT_SUBELEM"\n"
David Sedlák298ff6d2019-07-26 14:29:03 +02004039 "</submodule>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004040 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
4041 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01004042 assert_int_equal(yin_parse_submod(st->yin_ctx, lysp_submod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02004043
4044 assert_string_equal(lysp_submod->name, "mod");
4045 assert_string_equal(lysp_submod->revs, "2019-02-02");
David Sedlák298ff6d2019-07-26 14:29:03 +02004046 assert_string_equal(lysp_submod->prefix, "pref");
4047 assert_null(lysp_submod->filepath);
4048 assert_string_equal(lysp_submod->org, "org");
4049 assert_string_equal(lysp_submod->contact, "contact");
4050 assert_string_equal(lysp_submod->dsc, "desc");
4051 assert_string_equal(lysp_submod->ref, "ref");
4052 assert_int_equal(lysp_submod->version, LYS_VERSION_1_1);
4053 assert_string_equal(lysp_submod->imports->name, "a-mod");
4054 assert_string_equal(lysp_submod->includes->name, "b-mod");
4055 assert_string_equal(lysp_submod->extensions->name, "ext");
4056 assert_string_equal(lysp_submod->features->name, "feature");
4057 assert_string_equal(lysp_submod->identities->name, "ident-name");
4058 assert_string_equal(lysp_submod->typedefs->name, "tpdf");
4059 assert_string_equal(lysp_submod->groupings->name, "grp");
4060 assert_string_equal(lysp_submod->data->name, "anyd");
4061 assert_int_equal(lysp_submod->data->nodetype, LYS_ANYDATA);
4062 assert_string_equal(lysp_submod->data->next->name, "anyx");
4063 assert_int_equal(lysp_submod->data->next->nodetype, LYS_ANYXML);
4064 assert_string_equal(lysp_submod->data->next->next->name, "choice");
4065 assert_int_equal(lysp_submod->data->next->next->nodetype, LYS_CHOICE);
4066 assert_string_equal(lysp_submod->data->next->next->next->name, "cont");
4067 assert_int_equal(lysp_submod->data->next->next->next->nodetype, LYS_CONTAINER);
4068 assert_string_equal(lysp_submod->data->next->next->next->next->name, "leaf");
4069 assert_int_equal(lysp_submod->data->next->next->next->next->nodetype, LYS_LEAF);
4070 assert_string_equal(lysp_submod->data->next->next->next->next->next->name, "llist");
4071 assert_int_equal(lysp_submod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
4072 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->name, "sub-list");
4073 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
4074 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->next->name, "uses-name");
4075 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
4076 assert_null(lysp_submod->data->next->next->next->next->next->next->next->next);
4077 assert_string_equal(lysp_submod->augments->nodeid, "target");
4078 assert_string_equal(lysp_submod->rpcs->name, "rpc-name");
4079 assert_string_equal(lysp_submod->notifs->name, "notf");
4080 assert_string_equal(lysp_submod->deviations->nodeid, "target");
David Sedláke0ef1c62019-09-13 10:05:55 +02004081 assert_string_equal(lysp_submod->exts[0].name, "urn:example:extensions:c-define");
David Sedlákd1144562019-08-06 12:36:14 +02004082 assert_int_equal(lysp_submod->exts[0].insubstmt_index, 0);
4083 assert_int_equal(lysp_submod->exts[0].insubstmt, LYEXT_SUBSTMT_SELF);
David Sedlák298ff6d2019-07-26 14:29:03 +02004084
4085 lysp_submodule_free(st->ctx, lysp_submod);
David Sedlák298ff6d2019-07-26 14:29:03 +02004086
4087 /* min subelemnts */
Michal Vasko63f3d842020-07-08 10:10:14 +02004088 ly_in_free(st->in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +01004089 lyxml_ctx_free(st->yin_ctx->xmlctx);
David Sedlák298ff6d2019-07-26 14:29:03 +02004090 lysp_submod = calloc(1, sizeof *lysp_submod);
4091 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">"
Radek Krejci96e48da2020-09-04 13:18:06 +02004092 "<yang-version value=\"1\"/>"
David Sedlák298ff6d2019-07-26 14:29:03 +02004093 "<belongs-to module=\"mod-name\"><prefix value=\"pref\"/></belongs-to>"
4094 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004095 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
4096 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01004097 assert_int_equal(yin_parse_submod(st->yin_ctx, lysp_submod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02004098 assert_string_equal(lysp_submod->prefix, "pref");
4099 assert_string_equal(lysp_submod->belongsto, "mod-name");
4100 assert_int_equal(lysp_submod->version, LYS_VERSION_1_0);
4101 lysp_submodule_free(st->ctx, lysp_submod);
David Sedlák298ff6d2019-07-26 14:29:03 +02004102
David Sedláke6cd89e2019-08-07 12:46:02 +02004103 /* incorrect subelem order */
Michal Vasko63f3d842020-07-08 10:10:14 +02004104 ly_in_free(st->in, 0);
Michal Vaskob36053d2020-03-26 15:49:30 +01004105 lyxml_ctx_free(st->yin_ctx->xmlctx);
David Sedláke6cd89e2019-08-07 12:46:02 +02004106 lysp_submod = calloc(1, sizeof *lysp_submod);
4107 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">"
Radek Krejci96e48da2020-09-04 13:18:06 +02004108 "<yang-version value=\"1\"/>"
David Sedláke6cd89e2019-08-07 12:46:02 +02004109 "<reference><text>ref</text></reference>\n"
4110 "<belongs-to module=\"mod-name\"><prefix value=\"pref\"/></belongs-to>"
4111 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004112 assert_int_equal(ly_in_new_memory(data, &st->in), LY_SUCCESS);
4113 assert_int_equal(lyxml_ctx_new(st->ctx, st->in, &st->yin_ctx->xmlctx), LY_SUCCESS);
Michal Vaskob36053d2020-03-26 15:49:30 +01004114 assert_int_equal(yin_parse_submod(st->yin_ctx, lysp_submod), LY_EVALID);
4115 logbuf_assert("Invalid order of submodule's sub-elements \"belongs-to\" can't appear after \"reference\". Line number 2.");
David Sedláke6cd89e2019-08-07 12:46:02 +02004116 lysp_submodule_free(st->ctx, lysp_submod);
David Sedláke6cd89e2019-08-07 12:46:02 +02004117
David Sedlák298ff6d2019-07-26 14:29:03 +02004118 st->finished_correctly = true;
David Sedlák4f03b932019-07-26 13:01:47 +02004119}
4120
David Sedlák8985a142019-07-31 16:43:06 +02004121static void
4122test_yin_parse_module(void **state)
4123{
Radek Krejci3a4889a2020-05-19 17:01:58 +02004124 struct test_parser_yin_state *st = *state;
David Sedlák8985a142019-07-31 16:43:06 +02004125 const char *data;
4126 struct lys_module *mod;
Michal Vaskob36053d2020-03-26 15:49:30 +01004127 struct lys_yin_parser_ctx *yin_ctx = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02004128 struct ly_in *in = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02004129
4130 mod = calloc(1, sizeof *mod);
4131 mod->ctx = st->ctx;
David Sedlákd2844882019-09-13 16:01:22 +02004132 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"
4133 "<yang-version value=\"1.1\"/>\n"
4134 "<namespace uri=\"urn:tests:extensions:metadata:a\"/>\n"
4135 "<prefix value=\"a\"/>\n"
4136 "<import module=\"ietf-yang-metadata\">\n"
4137 "<prefix value=\"md\"/>\n"
4138 "</import>\n"
4139 "<feature name=\"f\"/>\n"
4140 "<md:annotation name=\"x\">\n"
4141 "<description>\n"
4142 "<text>test</text>\n"
4143 "</description>\n"
4144 "<reference>\n"
4145 "<text>test</text>\n"
4146 "</reference>\n"
4147 "<if-feature name=\"f\"/>\n"
4148 "<status value=\"current\"/>\n"
4149 "<type name=\"uint8\"/>\n"
4150 "<units name=\"meters\"/>\n"
4151 "</md:annotation>\n"
4152 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004153 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
4154 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS);
David Sedlákd2844882019-09-13 16:01:22 +02004155 assert_null(mod->parsed->exts->child->next->child);
4156 assert_string_equal(mod->parsed->exts->child->next->arg, "test");
4157 lys_module_free(mod, NULL);
4158 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004159 ly_in_free(in, 0);
David Sedlákd2844882019-09-13 16:01:22 +02004160 mod = NULL;
4161 yin_ctx = NULL;
4162
4163 mod = calloc(1, sizeof *mod);
4164 mod->ctx = st->ctx;
David Sedlák8985a142019-07-31 16:43:06 +02004165 data = "<module name=\"example-foo\""
4166 "xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
4167 "xmlns:foo=\"urn:example:foo\""
4168 "xmlns:myext=\"urn:example:extensions\">\n"
4169
Radek Krejci96e48da2020-09-04 13:18:06 +02004170 "<yang-version value=\"1\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02004171
4172 "<namespace uri=\"urn:example:foo\"/>\n"
4173 "<prefix value=\"foo\"/>\n"
4174
4175 "<import module=\"example-extensions\">\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004176 "<prefix value=\"myext\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02004177 "</import>\n"
4178
4179 "<list name=\"interface\">\n"
4180 "<key value=\"name\"/>\n"
4181 "<leaf name=\"name\">\n"
4182 "<type name=\"string\"/>\n"
4183 "</leaf>\n"
4184 "<leaf name=\"mtu\">\n"
4185 "<type name=\"uint32\"/>\n"
4186 "<description>\n"
4187 "<text>The MTU of the interface.</text>\n"
4188 "</description>\n"
4189 "<myext:c-define name=\"MY_MTU\"/>\n"
4190 "</leaf>\n"
4191 "</list>\n"
4192 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004193 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
4194 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS);
David Sedlák8985a142019-07-31 16:43:06 +02004195 lys_module_free(mod, NULL);
4196 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004197 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02004198 mod = NULL;
4199 yin_ctx = NULL;
4200
4201 mod = calloc(1, sizeof *mod);
4202 mod->ctx = st->ctx;
David Sedlák6d781b62019-08-02 15:22:52 +02004203 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
Radek Krejci96e48da2020-09-04 13:18:06 +02004204 "<yang-version value=\"1\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004205 "<namespace uri=\"urn:example:foo\"/>\n"
4206 "<prefix value=\"foo\"/>\n"
4207 "</module>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004208 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
4209 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_SUCCESS);
David Sedlák6d781b62019-08-02 15:22:52 +02004210 lys_module_free(mod, NULL);
4211 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004212 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02004213 mod = NULL;
4214 yin_ctx = NULL;
4215
4216
4217 mod = calloc(1, sizeof *mod);
4218 mod->ctx = st->ctx;
David Sedlák8985a142019-07-31 16:43:06 +02004219 data = "<submodule name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
4220 "</submodule>\n";
Michal Vasko63f3d842020-07-08 10:10:14 +02004221 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
4222 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_EINVAL);
David Sedlák8985a142019-07-31 16:43:06 +02004223 logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
4224 lys_module_free(mod, NULL);
4225 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004226 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02004227
David Sedlák6d781b62019-08-02 15:22:52 +02004228 mod = calloc(1, sizeof *mod);
4229 mod->ctx = st->ctx;
4230 data = "<module name=\"example-foo\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\n"
Radek Krejci96e48da2020-09-04 13:18:06 +02004231 "<yang-version value=\"1\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004232 "<namespace uri=\"urn:example:foo\"/>\n"
4233 "<prefix value=\"foo\"/>\n"
4234 "</module>"
4235 "<module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004236 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
4237 assert_int_equal(yin_parse_module(&yin_ctx, in, mod), LY_EVALID);
David Sedlák6d781b62019-08-02 15:22:52 +02004238 logbuf_assert("Trailing garbage \"<module>\" after module, expected end-of-input. Line number 5.");
4239 lys_module_free(mod, NULL);
4240 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004241 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02004242 mod = NULL;
4243 yin_ctx = NULL;
4244
David Sedlák8985a142019-07-31 16:43:06 +02004245 st->finished_correctly = true;
4246}
4247
4248static void
4249test_yin_parse_submodule(void **state)
4250{
Radek Krejci3a4889a2020-05-19 17:01:58 +02004251 struct test_parser_yin_state *st = *state;
David Sedlák8985a142019-07-31 16:43:06 +02004252 const char *data;
Michal Vaskob36053d2020-03-26 15:49:30 +01004253 struct lys_yin_parser_ctx *yin_ctx = NULL;
David Sedlák8985a142019-07-31 16:43:06 +02004254 struct lysp_submodule *submod = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02004255 struct lys_parser_ctx main_ctx = {};
Michal Vasko63f3d842020-07-08 10:10:14 +02004256 struct ly_in *in;
David Sedlák8985a142019-07-31 16:43:06 +02004257
4258 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
4259 "<submodule name=\"asub\""
4260 "xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\""
4261 "xmlns:a=\"urn:a\">"
Radek Krejci96e48da2020-09-04 13:18:06 +02004262 "<yang-version value=\"1\"/>\n"
David Sedlák8985a142019-07-31 16:43:06 +02004263 "<belongs-to module=\"a\">"
4264 "<prefix value=\"a_pref\"/>"
4265 "</belongs-to>"
4266 "<include module=\"atop\"/>"
4267 "<feature name=\"fox\"/>"
4268 "<notification name=\"bar-notif\">"
4269 "<if-feature name=\"bar\"/>"
4270 "</notification>"
4271 "<notification name=\"fox-notif\">"
4272 "<if-feature name=\"fox\"/>"
4273 "</notification>"
4274 "<augment target-node=\"/a_pref:top\">"
4275 "<if-feature name=\"bar\"/>"
4276 "<container name=\"bar-sub\"/>"
4277 "</augment>"
4278 "<augment target-node=\"/top\">"
4279 "<container name=\"bar-sub2\"/>"
4280 "</augment>"
4281 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004282 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
4283 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, &main_ctx, in, &submod), LY_SUCCESS);
David Sedlák8985a142019-07-31 16:43:06 +02004284 lysp_submodule_free(st->ctx, submod);
4285 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004286 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02004287 yin_ctx = NULL;
4288 submod = NULL;
4289
4290 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
David Sedlák6d781b62019-08-02 15:22:52 +02004291 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
Radek Krejci96e48da2020-09-04 13:18:06 +02004292 "<yang-version value=\"1\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004293 "<belongs-to module=\"a\">"
4294 "<prefix value=\"a_pref\"/>"
4295 "</belongs-to>"
4296 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004297 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
4298 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, &main_ctx, in, &submod), LY_SUCCESS);
David Sedlák6d781b62019-08-02 15:22:52 +02004299 lysp_submodule_free(st->ctx, submod);
4300 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004301 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02004302 yin_ctx = NULL;
4303 submod = NULL;
4304
4305 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
David Sedlák8985a142019-07-31 16:43:06 +02004306 "<module name=\"inval\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
4307 "</module>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004308 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
4309 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, &main_ctx, in, &submod), LY_EINVAL);
David Sedlák8985a142019-07-31 16:43:06 +02004310 logbuf_assert("Input data contains module in situation when a submodule is expected.");
4311 lysp_submodule_free(st->ctx, submod);
4312 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004313 ly_in_free(in, 0);
David Sedlák8985a142019-07-31 16:43:06 +02004314 yin_ctx = NULL;
4315 submod = NULL;
4316
David Sedlák6d781b62019-08-02 15:22:52 +02004317 data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
4318 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
Radek Krejci96e48da2020-09-04 13:18:06 +02004319 "<yang-version value=\"1\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004320 "<belongs-to module=\"a\">"
4321 "<prefix value=\"a_pref\"/>"
4322 "</belongs-to>"
4323 "</submodule>"
4324 "<submodule name=\"asub\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
Radek Krejci96e48da2020-09-04 13:18:06 +02004325 "<yang-version value=\"1\"/>\n"
David Sedlák6d781b62019-08-02 15:22:52 +02004326 "<belongs-to module=\"a\">"
4327 "<prefix value=\"a_pref\"/>"
4328 "</belongs-to>"
4329 "</submodule>";
Michal Vasko63f3d842020-07-08 10:10:14 +02004330 assert_int_equal(ly_in_new_memory(data, &in), LY_SUCCESS);
4331 assert_int_equal(yin_parse_submodule(&yin_ctx, st->ctx, &main_ctx, in, &submod), LY_EVALID);
David Sedlák6d781b62019-08-02 15:22:52 +02004332 logbuf_assert("Trailing garbage \"<submodule name...\" after submodule, expected end-of-input. Line number 2.");
4333 lysp_submodule_free(st->ctx, submod);
4334 yin_parser_ctx_free(yin_ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02004335 ly_in_free(in, 0);
David Sedlák6d781b62019-08-02 15:22:52 +02004336 yin_ctx = NULL;
4337 submod = NULL;
4338
David Sedlák8985a142019-07-31 16:43:06 +02004339 st->finished_correctly = true;
4340}
4341
David Sedlák3b4db242018-10-19 16:11:01 +02004342int
4343main(void)
4344{
4345
4346 const struct CMUnitTest tests[] = {
David Sedlák8f7a1172019-06-20 14:42:18 +02004347 cmocka_unit_test_setup_teardown(test_yin_match_keyword, setup_f, teardown_f),
David Sedlákb1a78352019-06-28 16:16:29 +02004348 cmocka_unit_test_setup_teardown(test_yin_parse_element_generic, setup_f, teardown_f),
4349 cmocka_unit_test_setup_teardown(test_yin_parse_extension_instance, setup_f, teardown_f),
David Sedlák071f7662019-09-12 02:02:51 +02004350 cmocka_unit_test_setup_teardown(test_yin_parse_content, setup_f, teardown_f),
David Sedlák4a650532019-07-10 11:55:18 +02004351 cmocka_unit_test_setup_teardown(test_validate_value, setup_f, teardown_f),
David Sedlák32488102019-07-15 17:44:10 +02004352
David Sedlák8e7bda82019-07-16 17:57:50 +02004353 cmocka_unit_test(test_yin_match_argument_name),
David Sedlák43801c92019-08-05 15:58:54 +02004354 cmocka_unit_test_setup_teardown(test_enum_elem, setup_element_test, teardown_element_test),
4355 cmocka_unit_test_setup_teardown(test_bit_elem, setup_element_test, teardown_element_test),
David Sedlák32488102019-07-15 17:44:10 +02004356 cmocka_unit_test_setup_teardown(test_meta_elem, setup_element_test, teardown_element_test),
4357 cmocka_unit_test_setup_teardown(test_import_elem, setup_element_test, teardown_element_test),
4358 cmocka_unit_test_setup_teardown(test_status_elem, setup_element_test, teardown_element_test),
4359 cmocka_unit_test_setup_teardown(test_ext_elem, setup_element_test, teardown_element_test),
4360 cmocka_unit_test_setup_teardown(test_yin_element_elem, setup_element_test, teardown_element_test),
4361 cmocka_unit_test_setup_teardown(test_yangversion_elem, setup_element_test, teardown_element_test),
4362 cmocka_unit_test_setup_teardown(test_mandatory_elem, setup_element_test, teardown_element_test),
David Sedlák8e7bda82019-07-16 17:57:50 +02004363 cmocka_unit_test_setup_teardown(test_argument_elem, setup_element_test, teardown_element_test),
4364 cmocka_unit_test_setup_teardown(test_base_elem, setup_element_test, teardown_element_test),
4365 cmocka_unit_test_setup_teardown(test_belongsto_elem, setup_element_test, teardown_element_test),
4366 cmocka_unit_test_setup_teardown(test_config_elem, setup_element_test, teardown_element_test),
4367 cmocka_unit_test_setup_teardown(test_default_elem, setup_element_test, teardown_element_test),
4368 cmocka_unit_test_setup_teardown(test_err_app_tag_elem, setup_element_test, teardown_element_test),
4369 cmocka_unit_test_setup_teardown(test_err_msg_elem, setup_element_test, teardown_element_test),
4370 cmocka_unit_test_setup_teardown(test_fracdigits_elem, setup_element_test, teardown_element_test),
4371 cmocka_unit_test_setup_teardown(test_iffeature_elem, setup_element_test, teardown_element_test),
4372 cmocka_unit_test_setup_teardown(test_length_elem, setup_element_test, teardown_element_test),
4373 cmocka_unit_test_setup_teardown(test_modifier_elem, setup_element_test, teardown_element_test),
4374 cmocka_unit_test_setup_teardown(test_namespace_elem, setup_element_test, teardown_element_test),
David Sedlák8e7bda82019-07-16 17:57:50 +02004375 cmocka_unit_test_setup_teardown(test_pattern_elem, setup_element_test, teardown_element_test),
4376 cmocka_unit_test_setup_teardown(test_value_position_elem, setup_element_test, teardown_element_test),
David Sedlák69f01612019-07-17 11:41:08 +02004377 cmocka_unit_test_setup_teardown(test_prefix_elem, setup_element_test, teardown_element_test),
4378 cmocka_unit_test_setup_teardown(test_range_elem, setup_element_test, teardown_element_test),
4379 cmocka_unit_test_setup_teardown(test_reqinstance_elem, setup_element_test, teardown_element_test),
4380 cmocka_unit_test_setup_teardown(test_revision_date_elem, setup_element_test, teardown_element_test),
4381 cmocka_unit_test_setup_teardown(test_unique_elem, setup_element_test, teardown_element_test),
4382 cmocka_unit_test_setup_teardown(test_units_elem, setup_element_test, teardown_element_test),
4383 cmocka_unit_test_setup_teardown(test_when_elem, setup_element_test, teardown_element_test),
4384 cmocka_unit_test_setup_teardown(test_yin_text_value_elem, setup_element_test, teardown_element_test),
David Sedlák374d2b32019-07-17 15:06:55 +02004385 cmocka_unit_test_setup_teardown(test_type_elem, setup_element_test, teardown_element_test),
David Sedlák1af868e2019-07-17 17:03:14 +02004386 cmocka_unit_test_setup_teardown(test_max_elems_elem, setup_element_test, teardown_element_test),
David Sedlák09e18c92019-07-18 11:17:11 +02004387 cmocka_unit_test_setup_teardown(test_min_elems_elem, setup_element_test, teardown_element_test),
David Sedláka2dad212019-07-18 12:45:19 +02004388 cmocka_unit_test_setup_teardown(test_ordby_elem, setup_element_test, teardown_element_test),
David Sedlák8a83bbb2019-07-18 14:46:00 +02004389 cmocka_unit_test_setup_teardown(test_any_elem, setup_element_test, teardown_element_test),
David Sedlák203ca3a2019-07-18 15:26:25 +02004390 cmocka_unit_test_setup_teardown(test_leaf_elem, setup_element_test, teardown_element_test),
David Sedlákc3da3ef2019-07-19 12:56:08 +02004391 cmocka_unit_test_setup_teardown(test_leaf_list_elem, setup_element_test, teardown_element_test),
David Sedlákcb39f642019-07-19 13:19:55 +02004392 cmocka_unit_test_setup_teardown(test_presence_elem, setup_element_test, teardown_element_test),
David Sedlák12470a82019-07-19 13:44:36 +02004393 cmocka_unit_test_setup_teardown(test_key_elem, setup_element_test, teardown_element_test),
David Sedlák04e17b22019-07-19 15:29:48 +02004394 cmocka_unit_test_setup_teardown(test_typedef_elem, setup_element_test, teardown_element_test),
David Sedlákd2d676a2019-07-22 11:28:19 +02004395 cmocka_unit_test_setup_teardown(test_refine_elem, setup_element_test, teardown_element_test),
David Sedlák0d6de5a2019-07-22 13:25:44 +02004396 cmocka_unit_test_setup_teardown(test_uses_elem, setup_element_test, teardown_element_test),
David Sedlákaa854b02019-07-22 14:17:10 +02004397 cmocka_unit_test_setup_teardown(test_revision_elem, setup_element_test, teardown_element_test),
David Sedlák0c2bab92019-07-22 15:33:19 +02004398 cmocka_unit_test_setup_teardown(test_include_elem, setup_element_test, teardown_element_test),
David Sedlákaf536aa2019-07-23 13:42:23 +02004399 cmocka_unit_test_setup_teardown(test_list_elem, setup_element_test, teardown_element_test),
David Sedlák031b9e72019-07-23 15:19:37 +02004400 cmocka_unit_test_setup_teardown(test_notification_elem, setup_element_test, teardown_element_test),
David Sedláke3ce9ef2019-07-23 16:34:30 +02004401 cmocka_unit_test_setup_teardown(test_grouping_elem, setup_element_test, teardown_element_test),
David Sedlákf111bcb2019-07-23 17:15:51 +02004402 cmocka_unit_test_setup_teardown(test_container_elem, setup_element_test, teardown_element_test),
David Sedlák5379d392019-07-24 10:42:03 +02004403 cmocka_unit_test_setup_teardown(test_case_elem, setup_element_test, teardown_element_test),
David Sedlákb7abcfa2019-07-24 12:33:35 +02004404 cmocka_unit_test_setup_teardown(test_choice_elem, setup_element_test, teardown_element_test),
David Sedlák05404f62019-07-24 14:11:53 +02004405 cmocka_unit_test_setup_teardown(test_inout_elem, setup_element_test, teardown_element_test),
David Sedlák85d0eca2019-07-24 15:15:21 +02004406 cmocka_unit_test_setup_teardown(test_action_elem, setup_element_test, teardown_element_test),
David Sedlák992fb7c2019-07-24 16:51:01 +02004407 cmocka_unit_test_setup_teardown(test_augment_elem, setup_element_test, teardown_element_test),
David Sedlák4ffcec82019-07-25 15:10:21 +02004408 cmocka_unit_test_setup_teardown(test_deviate_elem, setup_element_test, teardown_element_test),
David Sedlák8b754462019-07-25 16:22:13 +02004409 cmocka_unit_test_setup_teardown(test_deviation_elem, setup_element_test, teardown_element_test),
David Sedlák4f03b932019-07-26 13:01:47 +02004410 cmocka_unit_test_setup_teardown(test_module_elem, setup_element_test, teardown_element_test),
David Sedlák298ff6d2019-07-26 14:29:03 +02004411 cmocka_unit_test_setup_teardown(test_submodule_elem, setup_element_test, teardown_element_test),
David Sedlák8985a142019-07-31 16:43:06 +02004412
4413 cmocka_unit_test_setup_teardown(test_yin_parse_module, setup_logger, teardown_logger),
4414 cmocka_unit_test_setup_teardown(test_yin_parse_submodule, setup_logger, teardown_logger),
David Sedlák3b4db242018-10-19 16:11:01 +02004415 };
4416
David Sedlák8e7bda82019-07-16 17:57:50 +02004417 return cmocka_run_group_tests(tests, setup_ly_ctx, destroy_ly_ctx);
David Sedlák3b4db242018-10-19 16:11:01 +02004418}