blob: 37ccc56432a19386f9f1b4cc2ad2cf1b4aad3119 [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
David Sedlákecf5eb82019-06-03 14:12:44 +020024#include "../../src/common.h"
25#include "../../src/tree_schema.h"
26#include "../../src/tree_schema_internal.h"
27#include "../../src/parser_yin.h"
David Sedlák8f5bce02019-06-03 16:41:08 +020028#include "../../src/xml.h"
David Sedlák3b4db242018-10-19 16:11:01 +020029
David Sedlák555c7202019-07-04 12:14:12 +020030/* prototypes of static functions */
31void lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext);
David Sedlák986cb412019-07-04 13:10:11 +020032void lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext);
David Sedlák32eee7b2019-07-09 12:38:44 +020033void lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when);
David Sedlák32488102019-07-15 17:44:10 +020034void lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type);
David Sedlák8a83bbb2019-07-18 14:46:00 +020035void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
David Sedlák04e17b22019-07-19 15:29:48 +020036void lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf);
David Sedlákd2d676a2019-07-22 11:28:19 +020037void lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref);
David Sedlákaa854b02019-07-22 14:17:10 +020038void lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev);
David Sedlák0c2bab92019-07-22 15:33:19 +020039void lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include);
David Sedlák5e13dea2019-07-22 16:06:45 +020040void lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat);
David Sedlák28794f22019-07-22 16:45:00 +020041void lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident);
David Sedlák031b9e72019-07-23 15:19:37 +020042void lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif);
David Sedlák555c7202019-07-04 12:14:12 +020043
David Sedlák68a1af12019-03-08 13:46:54 +010044struct state {
David Sedlák3b4db242018-10-19 16:11:01 +020045 struct ly_ctx *ctx;
David Sedlák3017da42019-02-15 09:48:04 +010046 struct lys_module *mod;
David Sedlák619db942019-07-03 14:47:30 +020047 struct lysp_module *lysp_mod;
David Sedlákda8ffa32019-07-08 14:17:10 +020048 struct yin_parser_ctx *yin_ctx;
David Sedlák79e50cb2019-06-05 16:33:09 +020049 bool finished_correctly;
David Sedlák68a1af12019-03-08 13:46:54 +010050};
David Sedlák872c7b42018-10-26 13:15:20 +020051
David Sedlák79e50cb2019-06-05 16:33:09 +020052#define BUFSIZE 1024
53char logbuf[BUFSIZE] = {0};
54int store = -1; /* negative for infinite logging, positive for limited logging */
55
56/* set to 0 to printing error messages to stderr instead of checking them in code */
David Sedlákaf536aa2019-07-23 13:42:23 +020057#define ENABLE_LOGGER_CHECKING 0
David Sedlák79e50cb2019-06-05 16:33:09 +020058
59#if ENABLE_LOGGER_CHECKING
60static void
61logger(LY_LOG_LEVEL level, const char *msg, const char *path)
62{
63 (void) level; /* unused */
64 if (store) {
65 if (path && path[0]) {
66 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
67 } else {
68 strncpy(logbuf, msg, BUFSIZE - 1);
69 }
70 if (store > 0) {
71 --store;
72 }
73 }
74}
75#endif
76
77#if ENABLE_LOGGER_CHECKING
78# define logbuf_assert(str) assert_string_equal(logbuf, str)
79#else
80# define logbuf_assert(str)
81#endif
82
83#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
84 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
85 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
86 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
87 CLEANUP
88
David Sedlák8e7bda82019-07-16 17:57:50 +020089int
90setup_ly_ctx(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +010091{
92 struct state *st = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +020093
David Sedlák68a1af12019-03-08 13:46:54 +010094 /* allocate state variable */
95 (*state) = st = calloc(1, sizeof(*st));
96 if (!st) {
97 fprintf(stderr, "Memmory allocation failed");
98 return EXIT_FAILURE;
99 }
David Sedlák872c7b42018-10-26 13:15:20 +0200100
David Sedlák68a1af12019-03-08 13:46:54 +0100101 /* create new libyang context */
102 ly_ctx_new(NULL, 0, &st->ctx);
David Sedlák872c7b42018-10-26 13:15:20 +0200103
David Sedlák8e7bda82019-07-16 17:57:50 +0200104 return EXIT_SUCCESS;
105}
106
107int
108destroy_ly_ctx(void **state)
109{
110 struct state *st = *state;
111 ly_ctx_destroy(st->ctx, NULL);
112 free(st);
113
114 return EXIT_SUCCESS;
115}
116
117static int
118setup_f(void **state)
119{
120 struct state *st = *state;
121
122#if ENABLE_LOGGER_CHECKING
123 /* setup logger */
124 ly_set_log_clb(logger, 1);
125#endif
126
David Sedlák68a1af12019-03-08 13:46:54 +0100127 /* allocate new module */
128 st->mod = calloc(1, sizeof(*st->mod));
129 st->mod->ctx = st->ctx;
130
David Sedlák619db942019-07-03 14:47:30 +0200131 /* allocate new parsed module */
132 st->lysp_mod = calloc(1, sizeof(*st->lysp_mod));
133 st->lysp_mod->mod = calloc(1, sizeof(*st->lysp_mod->mod));
134 st->lysp_mod->mod->ctx = st->ctx;
135
136 /* allocate parser context */
David Sedlákda8ffa32019-07-08 14:17:10 +0200137 st->yin_ctx = calloc(1, sizeof(*st->yin_ctx));
138 st->yin_ctx->xml_ctx.ctx = st->ctx;
139 st->yin_ctx->xml_ctx.line = 1;
David Sedlák8f5bce02019-06-03 16:41:08 +0200140
David Sedlák68a1af12019-03-08 13:46:54 +0100141 return EXIT_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +0200142}
143
144static int
David Sedlák68a1af12019-03-08 13:46:54 +0100145teardown_f(void **state)
146{
147 struct state *st = *(struct state **)state;
David Sedlák619db942019-07-03 14:47:30 +0200148 struct lys_module *temp;
David Sedlák68a1af12019-03-08 13:46:54 +0100149
David Sedlák79e50cb2019-06-05 16:33:09 +0200150#if ENABLE_LOGGER_CHECKING
151 /* teardown logger */
152 if (!st->finished_correctly && logbuf[0] != '\0') {
153 fprintf(stderr, "%s\n", logbuf);
154 }
155#endif
156
David Sedlák619db942019-07-03 14:47:30 +0200157 temp = st->lysp_mod->mod;
158
David Sedlákda8ffa32019-07-08 14:17:10 +0200159 lyxml_context_clear(&st->yin_ctx->xml_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100160 lys_module_free(st->mod, NULL);
David Sedlák619db942019-07-03 14:47:30 +0200161 lysp_module_free(st->lysp_mod);
162 lys_module_free(temp, NULL);
David Sedlákda8ffa32019-07-08 14:17:10 +0200163 free(st->yin_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100164
165 return EXIT_SUCCESS;
166}
167
David Sedlák392af4f2019-06-04 16:02:42 +0200168static struct state*
169reset_state(void **state)
170{
David Sedlák79e50cb2019-06-05 16:33:09 +0200171 ((struct state *)*state)->finished_correctly = true;
David Sedlák555c7202019-07-04 12:14:12 +0200172 logbuf[0] = '\0';
David Sedlák392af4f2019-06-04 16:02:42 +0200173 teardown_f(state);
174 setup_f(state);
175
176 return *state;
177}
178
David Sedlák79e50cb2019-06-05 16:33:09 +0200179void
180logbuf_clean(void)
181{
182 logbuf[0] = '\0';
183}
184
David Sedlák68a1af12019-03-08 13:46:54 +0100185static void
David Sedlák392af4f2019-06-04 16:02:42 +0200186test_yin_parse_module(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +0100187{
188 LY_ERR ret = LY_SUCCESS;
189 struct state *st = *state;
190
191 ret = yin_parse_module(st->ctx,
David Sedlák2b214ac2019-06-06 16:11:03 +0200192 "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\
193 name=\"example-foo\"\
David Sedlák18730132019-03-15 15:51:34 +0100194 xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\
195 xmlns:foo=\"urn:example:foo\"\
196 xmlns:myext=\"urn:example:extensions\">\
David Sedlákcd0c9512019-03-29 13:23:06 +0100197 <namespace uri=\"urn:example:foo\" xmlns:myext=\"urn:example:extensions\"/>\
David Sedláka7406952019-04-05 10:33:07 +0200198 <prefix xmlns:myxt=\"urn:emple:extensions\" value=\"foo\" xmlns:myext=\"urn:example:extensions\"/>\
David Sedlákd9d3a312019-06-04 09:47:10 +0200199 </module>",
David Sedlák68a1af12019-03-08 13:46:54 +0100200 st->mod);
201
202 assert_int_equal(ret, LY_SUCCESS);
203 assert_string_equal(st->mod->parsed->mod->name, "example-foo");
204 assert_string_equal(st->mod->parsed->mod->prefix, "foo");
David Sedlákcd0c9512019-03-29 13:23:06 +0100205 assert_string_equal(st->mod->parsed->mod->ns, "urn:example:foo");
David Sedlák392af4f2019-06-04 16:02:42 +0200206
207 st = reset_state(state);
208 ret = yin_parse_module(st->ctx,
David Sedlák2b214ac2019-06-06 16:11:03 +0200209 "<module name=\"example-foo\">\
210 <invalid-tag uri=\"urn:example:foo\"\"/>\
211 </module>",
212 st->mod);
David Sedlák392af4f2019-06-04 16:02:42 +0200213 assert_int_equal(ret, LY_EVALID);
214
215 st = reset_state(state);
216 ret = yin_parse_module(st->ctx,
David Sedlák8f7a1172019-06-20 14:42:18 +0200217 "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\
David Sedlák57715b12019-06-17 13:05:22 +0200218 </module>",
David Sedlák2b214ac2019-06-06 16:11:03 +0200219 st->mod);
David Sedlák392af4f2019-06-04 16:02:42 +0200220 assert_int_equal(ret, LY_EVALID);
David Sedlák882a8072019-07-08 17:51:20 +0200221 logbuf_assert("Missing mandatory attribute name of module element. Line number 1.");
David Sedlák392af4f2019-06-04 16:02:42 +0200222
223 st = reset_state(state);
224 ret = yin_parse_module(st->ctx,
225 "",
226 st->mod);
227 assert_int_equal(ret, LY_EVALID);
David Sedlák79e50cb2019-06-05 16:33:09 +0200228 logbuf_assert("Invalid keyword \"(null)\", expected \"module\" or \"submodule\". Line number 1.");
229 st->finished_correctly = true;
David Sedlák3b4db242018-10-19 16:11:01 +0200230}
231
232static void
David Sedlák1bccdfa2019-06-17 15:55:27 +0200233test_yin_match_keyword(void **state)
David Sedlák3b4db242018-10-19 16:11:01 +0200234{
David Sedlák8f7a1172019-06-20 14:42:18 +0200235 struct state *st = *state;
David Sedlák3b4db242018-10-19 16:11:01 +0200236
David Sedlák8f7a1172019-06-20 14:42:18 +0200237 const char *prefix, *name;
238 struct yin_arg_record *args = NULL;
239 size_t prefix_len, name_len;
240 /* create mock yin namespace in xml context */
241 const char *data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" />";
David Sedlákda8ffa32019-07-08 14:17:10 +0200242 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
243 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák8f7a1172019-06-20 14:42:18 +0200244 LY_ARRAY_FREE(args);
245
David Sedlákc1771b12019-07-10 15:55:46 +0200246 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydatax", strlen("anydatax"), prefix, prefix_len, YANG_NONE), YANG_NONE);
247 assert_int_equal(yin_match_keyword(st->yin_ctx, "asdasd", strlen("asdasd"), prefix, prefix_len, YANG_NONE), YANG_NONE);
248 assert_int_equal(yin_match_keyword(st->yin_ctx, "", 0, prefix, prefix_len, YANG_NONE), YANG_NONE);
249 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydata", strlen("anydata"), prefix, prefix_len, YANG_NONE), YANG_ANYDATA);
250 assert_int_equal(yin_match_keyword(st->yin_ctx, "anyxml", strlen("anyxml"), prefix, prefix_len, YANG_NONE), YANG_ANYXML);
251 assert_int_equal(yin_match_keyword(st->yin_ctx, "argument", strlen("argument"), prefix, prefix_len, YANG_NONE), YANG_ARGUMENT);
252 assert_int_equal(yin_match_keyword(st->yin_ctx, "augment", strlen("augment"), prefix, prefix_len, YANG_NONE), YANG_AUGMENT);
253 assert_int_equal(yin_match_keyword(st->yin_ctx, "base", strlen("base"), prefix, prefix_len, YANG_NONE), YANG_BASE);
254 assert_int_equal(yin_match_keyword(st->yin_ctx, "belongs-to", strlen("belongs-to"), prefix, prefix_len, YANG_NONE), YANG_BELONGS_TO);
255 assert_int_equal(yin_match_keyword(st->yin_ctx, "bit", strlen("bit"), prefix, prefix_len, YANG_NONE), YANG_BIT);
256 assert_int_equal(yin_match_keyword(st->yin_ctx, "case", strlen("case"), prefix, prefix_len, YANG_NONE), YANG_CASE);
257 assert_int_equal(yin_match_keyword(st->yin_ctx, "choice", strlen("choice"), prefix, prefix_len, YANG_NONE), YANG_CHOICE);
258 assert_int_equal(yin_match_keyword(st->yin_ctx, "config", strlen("config"), prefix, prefix_len, YANG_NONE), YANG_CONFIG);
259 assert_int_equal(yin_match_keyword(st->yin_ctx, "contact", strlen("contact"), prefix, prefix_len, YANG_NONE), YANG_CONTACT);
260 assert_int_equal(yin_match_keyword(st->yin_ctx, "container", strlen("container"), prefix, prefix_len, YANG_NONE), YANG_CONTAINER);
261 assert_int_equal(yin_match_keyword(st->yin_ctx, "default", strlen("default"), prefix, prefix_len, YANG_NONE), YANG_DEFAULT);
262 assert_int_equal(yin_match_keyword(st->yin_ctx, "description", strlen("description"), prefix, prefix_len, YANG_NONE), YANG_DESCRIPTION);
263 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviate", strlen("deviate"), prefix, prefix_len, YANG_NONE), YANG_DEVIATE);
264 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviation", strlen("deviation"), prefix, prefix_len, YANG_NONE), YANG_DEVIATION);
265 assert_int_equal(yin_match_keyword(st->yin_ctx, "enum", strlen("enum"), prefix, prefix_len, YANG_NONE), YANG_ENUM);
266 assert_int_equal(yin_match_keyword(st->yin_ctx, "error-app-tag", strlen("error-app-tag"), prefix, prefix_len, YANG_NONE), YANG_ERROR_APP_TAG);
267 assert_int_equal(yin_match_keyword(st->yin_ctx, "error-message", strlen("error-message"), prefix, prefix_len, YANG_NONE), YANG_ERROR_MESSAGE);
268 assert_int_equal(yin_match_keyword(st->yin_ctx, "extension", strlen("extension"), prefix, prefix_len, YANG_NONE), YANG_EXTENSION);
269 assert_int_equal(yin_match_keyword(st->yin_ctx, "feature", strlen("feature"), prefix, prefix_len, YANG_NONE), YANG_FEATURE);
270 assert_int_equal(yin_match_keyword(st->yin_ctx, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len, YANG_NONE), YANG_FRACTION_DIGITS);
271 assert_int_equal(yin_match_keyword(st->yin_ctx, "grouping", strlen("grouping"), prefix, prefix_len, YANG_NONE), YANG_GROUPING);
272 assert_int_equal(yin_match_keyword(st->yin_ctx, "identity", strlen("identity"), prefix, prefix_len, YANG_NONE), YANG_IDENTITY);
273 assert_int_equal(yin_match_keyword(st->yin_ctx, "if-feature", strlen("if-feature"), prefix, prefix_len, YANG_NONE), YANG_IF_FEATURE);
274 assert_int_equal(yin_match_keyword(st->yin_ctx, "import", strlen("import"), prefix, prefix_len, YANG_NONE), YANG_IMPORT);
275 assert_int_equal(yin_match_keyword(st->yin_ctx, "include", strlen("include"), prefix, prefix_len, YANG_NONE), YANG_INCLUDE);
276 assert_int_equal(yin_match_keyword(st->yin_ctx, "input", strlen("input"), prefix, prefix_len, YANG_NONE), YANG_INPUT);
277 assert_int_equal(yin_match_keyword(st->yin_ctx, "key", strlen("key"), prefix, prefix_len, YANG_NONE), YANG_KEY);
278 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf", strlen("leaf"), prefix, prefix_len, YANG_NONE), YANG_LEAF);
279 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf-list", strlen("leaf-list"), prefix, prefix_len, YANG_NONE), YANG_LEAF_LIST);
280 assert_int_equal(yin_match_keyword(st->yin_ctx, "length", strlen("length"), prefix, prefix_len, YANG_NONE), YANG_LENGTH);
281 assert_int_equal(yin_match_keyword(st->yin_ctx, "list", strlen("list"), prefix, prefix_len, YANG_NONE), YANG_LIST);
282 assert_int_equal(yin_match_keyword(st->yin_ctx, "mandatory", strlen("mandatory"), prefix, prefix_len, YANG_NONE), YANG_MANDATORY);
283 assert_int_equal(yin_match_keyword(st->yin_ctx, "max-elements", strlen("max-elements"), prefix, prefix_len, YANG_NONE), YANG_MAX_ELEMENTS);
284 assert_int_equal(yin_match_keyword(st->yin_ctx, "min-elements", strlen("min-elements"), prefix, prefix_len, YANG_NONE), YANG_MIN_ELEMENTS);
285 assert_int_equal(yin_match_keyword(st->yin_ctx, "modifier", strlen("modifier"), prefix, prefix_len, YANG_NONE), YANG_MODIFIER);
286 assert_int_equal(yin_match_keyword(st->yin_ctx, "module", strlen("module"), prefix, prefix_len, YANG_NONE), YANG_MODULE);
287 assert_int_equal(yin_match_keyword(st->yin_ctx, "must", strlen("must"), prefix, prefix_len, YANG_NONE), YANG_MUST);
288 assert_int_equal(yin_match_keyword(st->yin_ctx, "namespace", strlen("namespace"), prefix, prefix_len, YANG_NONE), YANG_NAMESPACE);
289 assert_int_equal(yin_match_keyword(st->yin_ctx, "notification", strlen("notification"), prefix, prefix_len, YANG_NONE), YANG_NOTIFICATION);
290 assert_int_equal(yin_match_keyword(st->yin_ctx, "ordered-by", strlen("ordered-by"), prefix, prefix_len, YANG_NONE), YANG_ORDERED_BY);
291 assert_int_equal(yin_match_keyword(st->yin_ctx, "organization", strlen("organization"), prefix, prefix_len, YANG_NONE), YANG_ORGANIZATION);
292 assert_int_equal(yin_match_keyword(st->yin_ctx, "output", strlen("output"), prefix, prefix_len, YANG_NONE), YANG_OUTPUT);
293 assert_int_equal(yin_match_keyword(st->yin_ctx, "path", strlen("path"), prefix, prefix_len, YANG_NONE), YANG_PATH);
294 assert_int_equal(yin_match_keyword(st->yin_ctx, "pattern", strlen("pattern"), prefix, prefix_len, YANG_NONE), YANG_PATTERN);
295 assert_int_equal(yin_match_keyword(st->yin_ctx, "position", strlen("position"), prefix, prefix_len, YANG_NONE), YANG_POSITION);
296 assert_int_equal(yin_match_keyword(st->yin_ctx, "prefix", strlen("prefix"), prefix, prefix_len, YANG_NONE), YANG_PREFIX);
297 assert_int_equal(yin_match_keyword(st->yin_ctx, "presence", strlen("presence"), prefix, prefix_len, YANG_NONE), YANG_PRESENCE);
298 assert_int_equal(yin_match_keyword(st->yin_ctx, "range", strlen("range"), prefix, prefix_len, YANG_NONE), YANG_RANGE);
299 assert_int_equal(yin_match_keyword(st->yin_ctx, "reference", strlen("reference"), prefix, prefix_len, YANG_NONE), YANG_REFERENCE);
300 assert_int_equal(yin_match_keyword(st->yin_ctx, "refine", strlen("refine"), prefix, prefix_len, YANG_NONE), YANG_REFINE);
301 assert_int_equal(yin_match_keyword(st->yin_ctx, "require-instance", strlen("require-instance"), prefix, prefix_len, YANG_NONE), YANG_REQUIRE_INSTANCE);
302 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision", strlen("revision"), prefix, prefix_len, YANG_NONE), YANG_REVISION);
303 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision-date", strlen("revision-date"), prefix, prefix_len, YANG_NONE), YANG_REVISION_DATE);
304 assert_int_equal(yin_match_keyword(st->yin_ctx, "rpc", strlen("rpc"), prefix, prefix_len, YANG_NONE), YANG_RPC);
305 assert_int_equal(yin_match_keyword(st->yin_ctx, "status", strlen("status"), prefix, prefix_len, YANG_NONE), YANG_STATUS);
306 assert_int_equal(yin_match_keyword(st->yin_ctx, "submodule", strlen("submodule"), prefix, prefix_len, YANG_NONE), YANG_SUBMODULE);
307 assert_int_equal(yin_match_keyword(st->yin_ctx, "type", strlen("type"), prefix, prefix_len, YANG_NONE), YANG_TYPE);
308 assert_int_equal(yin_match_keyword(st->yin_ctx, "typedef", strlen("typedef"), prefix, prefix_len, YANG_NONE), YANG_TYPEDEF);
309 assert_int_equal(yin_match_keyword(st->yin_ctx, "unique", strlen("unique"), prefix, prefix_len, YANG_NONE), YANG_UNIQUE);
310 assert_int_equal(yin_match_keyword(st->yin_ctx, "units", strlen("units"), prefix, prefix_len, YANG_NONE), YANG_UNITS);
311 assert_int_equal(yin_match_keyword(st->yin_ctx, "uses", strlen("uses"), prefix, prefix_len, YANG_NONE), YANG_USES);
312 assert_int_equal(yin_match_keyword(st->yin_ctx, "value", strlen("value"), prefix, prefix_len, YANG_NONE), YANG_VALUE);
313 assert_int_equal(yin_match_keyword(st->yin_ctx, "when", strlen("when"), prefix, prefix_len, YANG_NONE), YANG_WHEN);
314 assert_int_equal(yin_match_keyword(st->yin_ctx, "yang-version", strlen("yang-version"), prefix, prefix_len, YANG_NONE), YANG_YANG_VERSION);
315 assert_int_equal(yin_match_keyword(st->yin_ctx, "yin-element", strlen("yin-element"), prefix, prefix_len, YANG_NONE), YANG_YIN_ELEMENT);
David Sedlák8f7a1172019-06-20 14:42:18 +0200316
317 st->finished_correctly = true;
David Sedlák872c7b42018-10-26 13:15:20 +0200318}
David Sedlák3b4db242018-10-19 16:11:01 +0200319
David Sedlák872c7b42018-10-26 13:15:20 +0200320static void
David Sedlák060b00e2019-06-19 11:12:06 +0200321test_yin_match_argument_name(void **state)
David Sedlák872c7b42018-10-26 13:15:20 +0200322{
David Sedlák68a1af12019-03-08 13:46:54 +0100323 (void)state; /* unused */
David Sedlák872c7b42018-10-26 13:15:20 +0200324
David Sedlák060b00e2019-06-19 11:12:06 +0200325 assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN);
326 assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN);
327 assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN);
328 assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION);
329 assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE);
330 assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE);
331 assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME);
332 assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG);
333 assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
334 assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT);
335 assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI);
336 assert_int_equal(yin_match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200337}
338
David Sedlák68a1af12019-03-08 13:46:54 +0100339static void
David Sedlákb1a78352019-06-28 16:16:29 +0200340test_yin_parse_element_generic(void **state)
341{
342 const char *prefix, *name;
343 struct state *st = *state;
344 struct lysp_ext_instance exts;
345 size_t prefix_len, name_len;
346 LY_ERR ret;
347
348 memset(&exts, 0, sizeof(exts));
349
350 const char *data = "<elem attr=\"value\">text_value</elem>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200351 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
352 ret = yin_parse_element_generic(st->yin_ctx, name, name_len, prefix, prefix_len, &data, &exts.child);
David Sedlákb1a78352019-06-28 16:16:29 +0200353 assert_int_equal(ret, LY_SUCCESS);
David Sedlák1fdb2522019-07-09 16:22:57 +0200354 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákb1a78352019-06-28 16:16:29 +0200355 assert_string_equal(exts.child->stmt, "elem");
356 assert_string_equal(exts.child->arg, "text_value");
David Sedlákb1a78352019-06-28 16:16:29 +0200357 assert_string_equal(exts.child->child->stmt, "attr");
358 assert_string_equal(exts.child->child->arg, "value");
359 assert_true(exts.child->child->flags & LYS_YIN_ATTR);
David Sedlákb1a78352019-06-28 16:16:29 +0200360 lysp_ext_instance_free(st->ctx, &exts);
David Sedlák5392a212019-07-01 09:19:10 +0200361 st = reset_state(state);
362
363 data = "<elem></elem>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200364 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
365 ret = yin_parse_element_generic(st->yin_ctx, name, name_len, prefix, prefix_len, &data, &exts.child);
David Sedlák5392a212019-07-01 09:19:10 +0200366 assert_int_equal(ret, LY_SUCCESS);
367 assert_string_equal(exts.child->stmt, "elem");
368 assert_null(exts.child->child);
369 assert_null(exts.child->arg);
David Sedlákda8ffa32019-07-08 14:17:10 +0200370 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlák5392a212019-07-01 09:19:10 +0200371 lysp_ext_instance_free(st->ctx, &exts);
372
David Sedlákb1a78352019-06-28 16:16:29 +0200373 st->finished_correctly = true;
374}
375
376static void
377test_yin_parse_extension_instance(void **state)
378{
379 LY_ERR ret;
380 struct state *st = *state;
381 const char *prefix, *name;
382 size_t prefix_len, name_len;
383 struct yin_arg_record *args = NULL;
384 struct lysp_ext_instance *exts = NULL;
David Sedlákb1a78352019-06-28 16:16:29 +0200385 const char *data = "<ext value1=\"test\" value=\"test2\"><subelem>text</subelem></ext>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200386 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
387 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák1f90d252019-07-10 17:09:32 +0200388 ret = yin_parse_extension_instance(st->yin_ctx, args, &data, name2fullname(name, prefix_len),
David Sedlák619db942019-07-03 14:47:30 +0200389 namelen2fulllen(name_len, prefix_len), LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200390 assert_int_equal(ret, LY_SUCCESS);
391 assert_string_equal(exts->name, "ext");
392 assert_int_equal(exts->insubstmt_index, 0);
393 assert_true(exts->insubstmt == LYEXT_SUBSTMT_CONTACT);
394 assert_true(exts->yin & LYS_YIN);
395 assert_string_equal(exts->child->stmt, "value1");
396 assert_string_equal(exts->child->arg, "test");
397 assert_null(exts->child->child);
398 assert_true(exts->child->flags & LYS_YIN_ATTR);
399 assert_string_equal(exts->child->next->stmt, "value");
400 assert_string_equal(exts->child->next->arg, "test2");
401 assert_null(exts->child->next->child);
402 assert_true(exts->child->next->flags & LYS_YIN_ATTR);
403
404 assert_string_equal(exts->child->next->next->stmt, "subelem");
405 assert_string_equal(exts->child->next->next->arg, "text");
406 assert_null(exts->child->next->next->child);
407 assert_null(exts->child->next->next->next);
408 assert_false(exts->child->next->next->flags & LYS_YIN_ATTR);
David Sedlákda8ffa32019-07-08 14:17:10 +0200409 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákb1a78352019-06-28 16:16:29 +0200410 LY_ARRAY_FREE(args);
411 lysp_ext_instance_free(st->ctx, exts);
412 LY_ARRAY_FREE(exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200413 exts = NULL;
414 args = NULL;
415 st = reset_state(state);
416
417 data = "<extension-elem />";
David Sedlákda8ffa32019-07-08 14:17:10 +0200418 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
419 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák1f90d252019-07-10 17:09:32 +0200420 ret = yin_parse_extension_instance(st->yin_ctx, args, &data, name, name_len, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200421 assert_int_equal(ret, LY_SUCCESS);
422 assert_string_equal(exts->name, "extension-elem");
423 assert_null(exts->argument);
424 assert_null(exts->child);
425 assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT);
426 assert_int_equal(exts->insubstmt_index, 0);
427 assert_true(exts->yin & LYS_YIN);
David Sedlákda8ffa32019-07-08 14:17:10 +0200428 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákf250ecf2019-07-01 11:02:05 +0200429 LY_ARRAY_FREE(args);
430 lysp_ext_instance_free(st->ctx, exts);
431 LY_ARRAY_FREE(exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200432 st->finished_correctly = true;
433}
434
David Sedlák555c7202019-07-04 12:14:12 +0200435static void
436test_yin_parse_content(void **state)
437{
438 struct state *st = *state;
439 LY_ERR ret = LY_SUCCESS;
440 struct sized_string name, prefix;
441 const char *data = "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
442 "<custom xmlns=\"my-ext\">"
443 "totally amazing extension"
444 "</custom>"
David Sedlák986cb412019-07-04 13:10:11 +0200445 "<extension name=\"ext\">"
446 "<argument name=\"argname\"></argument>"
447 "<description><text>desc</text></description>"
448 "<reference><text>ref</text></reference>"
449 "<status value=\"deprecated\"></status>"
450 "</extension>"
David Sedlák555c7202019-07-04 12:14:12 +0200451 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedlák32488102019-07-15 17:44:10 +0200452 "<if-feature name=\"foo\"></if-feature>"
David Sedlák32eee7b2019-07-09 12:38:44 +0200453 "<when condition=\"condition...\">"
454 "<reference><text>when_ref</text></reference>"
455 "<description><text>when_desc</text></description>"
456 "</when>"
David Sedláke1a30302019-07-10 13:49:38 +0200457 "<config value=\"true\"/>"
David Sedlákc1771b12019-07-10 15:55:46 +0200458 "<error-message>"
459 "<value>error-msg</value>"
460 "</error-message>"
David Sedlák2ce1be62019-07-10 16:15:09 +0200461 "<error-app-tag value=\"err-app-tag\"/>"
David Sedláka5b1d382019-07-10 16:31:09 +0200462 "<units name=\"radians\"></units>"
David Sedláke7084ce2019-07-10 16:44:15 +0200463 "<default value=\"default-value\"/>"
David Sedlák5545f5d2019-07-11 11:55:16 +0200464 "<position value=\"25\"></position>"
465 "<value value=\"-5\"/>"
David Sedlákcf5569a2019-07-11 13:31:34 +0200466 "<require-instance value=\"true\"></require-instance>"
David Sedlákb7296dd2019-07-11 14:58:38 +0200467 "<range value=\"5..10\" />"
David Sedlák438ae432019-07-11 15:36:54 +0200468 "<length value=\"baf\"/>"
David Sedlákd3983112019-07-12 11:20:56 +0200469 "<pattern value='pattern'>"
470 "<modifier value='invert-match'/>"
471 "</pattern>"
David Sedlákfd5b9c32019-07-12 15:33:13 +0200472 "<enum name=\"yay\">"
473 "</enum>"
David Sedlák555c7202019-07-04 12:14:12 +0200474 "</prefix>";
475 struct lysp_ext_instance *exts = NULL;
David Sedlák5f8191e2019-07-08 16:35:52 +0200476 const char **if_features = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200477 struct yin_arg_record *attrs = NULL;
David Sedláke7084ce2019-07-10 16:44:15 +0200478 const char *value, *err_msg, *app_tag, *units, *def;
David Sedlák986cb412019-07-04 13:10:11 +0200479 struct lysp_ext *ext_def = NULL;
David Sedlák32eee7b2019-07-09 12:38:44 +0200480 struct lysp_when *when_p = NULL;
David Sedlákcf5569a2019-07-11 13:31:34 +0200481 struct lysp_type_enum pos_enum = {}, val_enum = {};
David Sedlákfd5b9c32019-07-12 15:33:13 +0200482 struct lysp_type req_type = {}, range_type = {}, len_type = {}, patter_type = {}, enum_type = {};
David Sedláke1a30302019-07-10 13:49:38 +0200483 uint8_t config = 0;
David Sedlák555c7202019-07-04 12:14:12 +0200484
David Sedlákda8ffa32019-07-08 14:17:10 +0200485 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
486 yin_load_attributes(st->yin_ctx, &data, &attrs);
David Sedlák555c7202019-07-04 12:14:12 +0200487
David Sedlákfd5b9c32019-07-12 15:33:13 +0200488 struct yin_subelement subelems[17] = {
David Sedlák438ae432019-07-11 15:36:54 +0200489 {YANG_CONFIG, &config, 0},
David Sedlákc3da3ef2019-07-19 12:56:08 +0200490 {YANG_DEFAULT, &def, YIN_SUBELEM_UNIQUE},
David Sedlákfd5b9c32019-07-12 15:33:13 +0200491 {YANG_ENUM, &enum_type, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200492 {YANG_ERROR_APP_TAG, &app_tag, 0},
493 {YANG_ERROR_MESSAGE, &err_msg, 0},
494 {YANG_EXTENSION, &ext_def, 0},
495 {YANG_IF_FEATURE, &if_features, 0},
496 {YANG_LENGTH, &len_type, 0},
David Sedlákd3983112019-07-12 11:20:56 +0200497 {YANG_PATTERN, &patter_type, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200498 {YANG_RANGE, &range_type, 0},
499 {YANG_REQUIRE_INSTANCE, &req_type, 0},
500 {YANG_UNITS, &units, 0},
David Sedlák374d2b32019-07-17 15:06:55 +0200501 {YANG_POSITION, &pos_enum, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200502 {YANG_VALUE, &val_enum, 0},
503 {YANG_WHEN, &when_p, 0},
504 {YANG_CUSTOM, NULL, 0},
505 {YIN_TEXT, &value, 0}
David Sedlákd3983112019-07-12 11:20:56 +0200506 };
David Sedlákfd5b9c32019-07-12 15:33:13 +0200507 ret = yin_parse_content(st->yin_ctx, subelems, 17, &data, YANG_PREFIX, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200508 assert_int_equal(ret, LY_SUCCESS);
David Sedlák1fdb2522019-07-09 16:22:57 +0200509 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlák2ce1be62019-07-10 16:15:09 +0200510 /* check parsed values */
David Sedláke7084ce2019-07-10 16:44:15 +0200511 assert_string_equal(def, "default-value");
David Sedlák555c7202019-07-04 12:14:12 +0200512 assert_string_equal(exts->name, "custom");
513 assert_string_equal(exts->argument, "totally amazing extension");
514 assert_string_equal(value, "wsefsdf");
David Sedláka5b1d382019-07-10 16:31:09 +0200515 assert_string_equal(units, "radians");
David Sedlák32eee7b2019-07-09 12:38:44 +0200516 assert_string_equal(when_p->cond, "condition...");
517 assert_string_equal(when_p->dsc, "when_desc");
518 assert_string_equal(when_p->ref, "when_ref");
David Sedláke1a30302019-07-10 13:49:38 +0200519 assert_int_equal(config, LYS_CONFIG_W);
David Sedlák5545f5d2019-07-11 11:55:16 +0200520 assert_int_equal(pos_enum.value, 25);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200521 assert_true(pos_enum.flags & LYS_SET_VALUE);
David Sedlák5545f5d2019-07-11 11:55:16 +0200522 assert_int_equal(val_enum.value, -5);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200523 assert_true(val_enum.flags & LYS_SET_VALUE);
David Sedlákcf5569a2019-07-11 13:31:34 +0200524 assert_int_equal(req_type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200525 assert_true(req_type.flags &= LYS_SET_REQINST);
David Sedlákb7296dd2019-07-11 14:58:38 +0200526 assert_string_equal(range_type.range->arg, "5..10");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200527 assert_true(range_type.flags & LYS_SET_RANGE);
David Sedlákc1771b12019-07-10 15:55:46 +0200528 assert_string_equal(err_msg, "error-msg");
David Sedlák2ce1be62019-07-10 16:15:09 +0200529 assert_string_equal(app_tag, "err-app-tag");
David Sedlákfd5b9c32019-07-12 15:33:13 +0200530 assert_string_equal(enum_type.enums->name, "yay");
David Sedlák438ae432019-07-11 15:36:54 +0200531 assert_string_equal(len_type.length->arg, "baf");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200532 assert_true(len_type.flags & LYS_SET_LENGTH);
David Sedlákd3983112019-07-12 11:20:56 +0200533 assert_string_equal(patter_type.patterns->arg, "\x015pattern");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200534 assert_true(patter_type.flags & LYS_SET_PATTERN);
David Sedlák2ce1be62019-07-10 16:15:09 +0200535 /* cleanup */
David Sedlák555c7202019-07-04 12:14:12 +0200536 lysp_ext_instance_free(st->ctx, exts);
David Sedlák32eee7b2019-07-09 12:38:44 +0200537 lysp_when_free(st->ctx, when_p);
David Sedlák986cb412019-07-04 13:10:11 +0200538 lysp_ext_free(st->ctx, ext_def);
David Sedlák5f8191e2019-07-08 16:35:52 +0200539 FREE_STRING(st->ctx, *if_features);
David Sedlákc1771b12019-07-10 15:55:46 +0200540 FREE_STRING(st->ctx, err_msg);
David Sedlák2ce1be62019-07-10 16:15:09 +0200541 FREE_STRING(st->ctx, app_tag);
David Sedláka5b1d382019-07-10 16:31:09 +0200542 FREE_STRING(st->ctx, units);
David Sedlákd3983112019-07-12 11:20:56 +0200543 FREE_STRING(st->ctx, patter_type.patterns->arg);
David Sedláke7084ce2019-07-10 16:44:15 +0200544 FREE_STRING(st->ctx, def);
David Sedlákb7296dd2019-07-11 14:58:38 +0200545 FREE_STRING(st->ctx, range_type.range->arg);
David Sedlák438ae432019-07-11 15:36:54 +0200546 FREE_STRING(st->ctx, len_type.length->arg);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200547 FREE_STRING(st->ctx, enum_type.enums->name);
David Sedlákb7296dd2019-07-11 14:58:38 +0200548 FREE_STRING(st->ctx, value);
David Sedlák5f8191e2019-07-08 16:35:52 +0200549 LY_ARRAY_FREE(if_features);
David Sedlák555c7202019-07-04 12:14:12 +0200550 LY_ARRAY_FREE(exts);
David Sedlák986cb412019-07-04 13:10:11 +0200551 LY_ARRAY_FREE(ext_def);
David Sedlák555c7202019-07-04 12:14:12 +0200552 LY_ARRAY_FREE(attrs);
David Sedlákd3983112019-07-12 11:20:56 +0200553 LY_ARRAY_FREE(patter_type.patterns);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200554 LY_ARRAY_FREE(enum_type.enums);
David Sedlák32eee7b2019-07-09 12:38:44 +0200555 free(when_p);
David Sedlákb7296dd2019-07-11 14:58:38 +0200556 free(range_type.range);
David Sedlák438ae432019-07-11 15:36:54 +0200557 free(len_type.length);
David Sedlák555c7202019-07-04 12:14:12 +0200558 attrs = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200559 st = reset_state(state);
560
561 /* test unique subelem */
562 const char *prefix_value;
563 struct yin_subelement subelems2[2] = {{YANG_PREFIX, &prefix_value, 0},
564 {YIN_TEXT, &value, YIN_SUBELEM_UNIQUE}};
565 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
566 "<prefix value=\"inv_mod\" />"
567 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
568 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
569 "</module>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200570 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
571 yin_load_attributes(st->yin_ctx, &data, &attrs);
572 ret = yin_parse_content(st->yin_ctx, subelems2, 2, &data, YANG_MODULE, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200573 assert_int_equal(ret, LY_EVALID);
574 logbuf_assert("Redefinition of text element in module element. Line number 1.");
575 lydict_remove(st->ctx, prefix_value);
576 lydict_remove(st->ctx, value);
577 st = reset_state(state);
578 LY_ARRAY_FREE(attrs);
579 attrs = NULL;
580
581 /* test first subelem */
582 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
583 "<prefix value=\"inv_mod\" />"
584 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
585 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
586 "</module>";
587 struct yin_subelement subelems3[2] = {{YANG_PREFIX, &prefix_value, 0},
588 {YIN_TEXT, &value, YIN_SUBELEM_FIRST}};
David Sedlákda8ffa32019-07-08 14:17:10 +0200589 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
590 yin_load_attributes(st->yin_ctx, &data, &attrs);
591 ret = yin_parse_content(st->yin_ctx, subelems3, 2, &data, YANG_MODULE, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200592 assert_int_equal(ret, LY_EVALID);
593 logbuf_assert("Subelement text of module element must be defined as first subelement. Line number 1.");
594 lydict_remove(st->ctx, prefix_value);
595 st = reset_state(state);
596 LY_ARRAY_FREE(attrs);
597 attrs = NULL;
598
599 /* test mandatory subelem */
600 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
601 "</module>";
602 struct yin_subelement subelems4[1] = {{YANG_PREFIX, &prefix_value, YIN_SUBELEM_MANDATORY}};
David Sedlákda8ffa32019-07-08 14:17:10 +0200603 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
604 yin_load_attributes(st->yin_ctx, &data, &attrs);
605 ret = yin_parse_content(st->yin_ctx, subelems4, 1, &data, YANG_MODULE, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200606 assert_int_equal(ret, LY_EVALID);
607 logbuf_assert("Missing mandatory subelement prefix of module element. Line number 1.");
608 LY_ARRAY_FREE(attrs);
609
610 st->finished_correctly = true;
611}
612
David Sedlák92147b02019-07-09 14:01:01 +0200613static void
David Sedlák4a650532019-07-10 11:55:18 +0200614test_validate_value(void **state)
615{
616 struct state *st = *state;
617 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "#invalid", 8), LY_EVALID);
618 logbuf_assert("Invalid identifier character '#'. Line number 1.");
619 assert_int_equal(yin_validate_value(st->yin_ctx, Y_STR_ARG, "", 0), LY_SUCCESS);
620 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "pre:b", 5), LY_EVALID);
621 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:b", 5), LY_SUCCESS);
622 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:pre:b", 9), LY_EVALID);
623
624 st->finished_correctly = true;
625}
626
David Sedlák32488102019-07-15 17:44:10 +0200627static int
628setup_element_test(void **state)
629{
David Sedlák8e7bda82019-07-16 17:57:50 +0200630 struct state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +0200631
632#if ENABLE_LOGGER_CHECKING
633 /* setup logger */
634 ly_set_log_clb(logger, 1);
635#endif
636
637 /* reset logbuf */
638 logbuf[0] = '\0';
David Sedlák32488102019-07-15 17:44:10 +0200639
640 /* allocate parser context */
641 st->yin_ctx = calloc(1, sizeof(*st->yin_ctx));
642 st->yin_ctx->xml_ctx.ctx = st->ctx;
643 st->yin_ctx->xml_ctx.line = 1;
644
645 return EXIT_SUCCESS;
646}
647
648static int
649teardown_element_test(void **state)
650{
651 struct state *st = *(struct state **)state;
652
653#if ENABLE_LOGGER_CHECKING
654 /* teardown logger */
655 if (!st->finished_correctly && logbuf[0] != '\0') {
656 fprintf(stderr, "%s\n", logbuf);
657 }
658#endif
659
660 lyxml_context_clear(&st->yin_ctx->xml_ctx);
David Sedlák32488102019-07-15 17:44:10 +0200661 free(st->yin_ctx);
David Sedlák32488102019-07-15 17:44:10 +0200662
663 return EXIT_SUCCESS;
664}
665
666#define ELEMENT_WRAPPER_START "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
667#define ELEMENT_WRAPPER_END "</module>"
668
669/* helper function to simplify unit test of each element using parse_content function */
670LY_ERR
671test_element_helper(struct state *st, const char **data, void *dest, const char **text,
672 struct lysp_ext_instance **exts, bool valid)
673{
674 struct yin_arg_record *attrs = NULL;
675 struct sized_string name, prefix;
676 LY_ERR ret = LY_SUCCESS;
677 struct yin_subelement subelems[71] = {
678 {YANG_ACTION, dest, 0},
679 {YANG_ANYDATA, dest, 0},
680 {YANG_ANYXML, dest, 0},
681 {YANG_ARGUMENT,dest, 0},
682 {YANG_AUGMENT, dest, 0},
683 {YANG_BASE, dest, 0},
684 {YANG_BELONGS_TO, dest, 0},
685 {YANG_BIT, dest, 0},
686 {YANG_CASE, dest, 0},
687 {YANG_CHOICE, dest, 0},
688 {YANG_CONFIG, dest, 0},
689 {YANG_CONTACT, dest, 0},
690 {YANG_CONTAINER, dest, 0},
David Sedlákc3da3ef2019-07-19 12:56:08 +0200691 {YANG_DEFAULT, dest, YIN_SUBELEM_UNIQUE},
David Sedlák32488102019-07-15 17:44:10 +0200692 {YANG_DESCRIPTION, dest, 0},
693 {YANG_DEVIATE, dest, 0},
694 {YANG_DEVIATION, dest, 0},
695 {YANG_ENUM, dest, 0},
696 {YANG_ERROR_APP_TAG, dest, 0},
697 {YANG_ERROR_MESSAGE, dest, 0},
698 {YANG_EXTENSION, dest, 0},
699 {YANG_FEATURE, dest, 0},
700 {YANG_FRACTION_DIGITS, dest, 0},
701 {YANG_GROUPING, dest, 0},
702 {YANG_IDENTITY, dest, 0},
703 {YANG_IF_FEATURE, dest, 0},
704 {YANG_IMPORT, dest, 0},
705 {YANG_INCLUDE, dest, 0},
706 {YANG_INPUT, dest, 0},
707 {YANG_KEY, dest, 0},
708 {YANG_LEAF, dest, 0},
709 {YANG_LEAF_LIST, dest, 0},
710 {YANG_LENGTH, dest, 0},
711 {YANG_LIST, dest, 0},
712 {YANG_MANDATORY, dest, 0},
713 {YANG_MAX_ELEMENTS, dest, 0},
714 {YANG_MIN_ELEMENTS, dest, 0},
715 {YANG_MODIFIER, dest, 0},
716 {YANG_MODULE, dest, 0},
717 {YANG_MUST, dest, 0},
718 {YANG_NAMESPACE, dest, 0},
719 {YANG_NOTIFICATION, dest, 0},
720 {YANG_ORDERED_BY, dest, 0},
721 {YANG_ORGANIZATION, dest, 0},
722 {YANG_OUTPUT, dest, 0},
723 {YANG_PATH, dest, 0},
724 {YANG_PATTERN, dest, 0},
David Sedlák32488102019-07-15 17:44:10 +0200725 {YANG_PREFIX, dest, 0},
726 {YANG_PRESENCE, dest, 0},
727 {YANG_RANGE, dest, 0},
728 {YANG_REFERENCE, dest, 0},
729 {YANG_REFINE, dest, 0},
730 {YANG_REQUIRE_INSTANCE, dest, 0},
731 {YANG_REVISION, dest, 0},
732 {YANG_REVISION_DATE, dest, 0},
733 {YANG_RPC, dest, 0},
734 {YANG_STATUS, dest, 0},
735 {YANG_SUBMODULE, dest, 0},
736 {YANG_TYPE, dest, 0},
737 {YANG_TYPEDEF, dest, 0},
738 {YANG_UNIQUE, dest, 0},
739 {YANG_UNITS, dest, 0},
740 {YANG_USES, dest, 0},
David Sedlák374d2b32019-07-17 15:06:55 +0200741 {YANG_POSITION, dest, 0},
David Sedlák32488102019-07-15 17:44:10 +0200742 {YANG_VALUE, dest, 0},
743 {YANG_WHEN, dest, 0},
744 {YANG_YANG_VERSION, dest, 0},
745 {YANG_YIN_ELEMENT, dest, 0},
746 {YANG_CUSTOM, dest, 0},
747 {YIN_TEXT, dest, 0},
748 {YIN_VALUE, dest, 0}
749 };
750 LY_CHECK_RET(lyxml_get_element(&st->yin_ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len));\
751 LY_CHECK_RET(yin_load_attributes(st->yin_ctx, data, &attrs));\
David Sedlák8e7bda82019-07-16 17:57:50 +0200752 ret = yin_parse_content(st->yin_ctx, subelems, 71, data, yin_match_keyword(st->yin_ctx, name.value, name.len, prefix.value, prefix.len, YANG_NONE), text, exts);
David Sedlák32488102019-07-15 17:44:10 +0200753 LY_ARRAY_FREE(attrs);
754 if (valid) {
755 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
756 }
757 /* reset status */
758 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
759 return ret;
760}
761
762static void
763test_enum_bit_elem(void **state)
764{
765 /* yin_parse_enum_bit is function that is being mainly tested by this test */
766 struct state *st = *state;
767 struct lysp_type type = {};
768 const char *data;
769 data = ELEMENT_WRAPPER_START
770 "<enum name=\"enum-name\">"
771 "<if-feature name=\"feature\" />"
772 "<value value=\"55\" />"
773 "<status value=\"deprecated\" />"
774 "<description><text>desc...</text></description>"
775 "<reference><text>ref...</text></reference>"
776 "</enum>"
777 ELEMENT_WRAPPER_END;
778 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
779 assert_string_equal(*type.enums->iffeatures, "feature");
780 assert_int_equal(type.enums->value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200781 assert_true((type.enums->flags & LYS_STATUS_DEPRC) && (type.enums->flags & LYS_SET_VALUE));
David Sedlák32488102019-07-15 17:44:10 +0200782 assert_string_equal(type.enums->dsc, "desc...");
783 assert_string_equal(type.enums->ref, "ref...");
784 lysp_type_free(st->ctx, &type);
785 memset(&type, 0, sizeof type);
786
787 /* todo bit element test */
788 st->finished_correctly = true;
789}
790
791static void
792test_meta_elem(void **state)
793{
794 struct state *st = *state;
795 char *value = NULL;
796 const char *data;
797
798 /* organization element */
799 data = ELEMENT_WRAPPER_START
800 "<organization><text>organization...</text></organization>"
801 ELEMENT_WRAPPER_END;
802 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
803 assert_string_equal(value, "organization...");
804 FREE_STRING(st->ctx, value);
805 value = NULL;
806 /* contact element */
807 data = ELEMENT_WRAPPER_START
808 "<contact><text>contact...</text></contact>"
809 ELEMENT_WRAPPER_END;
810 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
811 assert_string_equal(value, "contact...");
812 FREE_STRING(st->ctx, value);
813 value = NULL;
814 /* description element */
815 data = ELEMENT_WRAPPER_START
816 "<description><text>description...</text></description>"
817 ELEMENT_WRAPPER_END;
818 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
819 assert_string_equal(value, "description...");
820 FREE_STRING(st->ctx, value);
821 value = NULL;
822 /* reference element */
823 data = ELEMENT_WRAPPER_START
824 "<reference><text>reference...</text></reference>"
825 ELEMENT_WRAPPER_END;
826 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
827 assert_string_equal(value, "reference...");
828 FREE_STRING(st->ctx, value);
829 value = NULL;
830
831 /* missing text subelement */
832 data = ELEMENT_WRAPPER_START
833 "<reference>reference...</reference>"
834 ELEMENT_WRAPPER_END;
835 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, false), LY_EVALID);
836 logbuf_assert("Missing mandatory subelement text of reference element. Line number 1.");
837
838 st->finished_correctly = true;
839}
840
841static void
842test_import_elem(void **state)
843{
844 struct state *st = *state;
845 const char *data;
846 struct lys_module *lys_mod = calloc(1, sizeof *lys_mod);
847 struct lysp_module *lysp_mod = calloc(1, sizeof *lysp_mod);
848 lys_mod->ctx = st->ctx;
849 lysp_mod->mod = lys_mod;
850
851 /* max subelems */
852 data = ELEMENT_WRAPPER_START
853 "<import module=\"a\">"
854 "<prefix value=\"a_mod\"/>"
855 "<revision-date date=\"2015-01-01\"></revision-date>"
856 "<description><text>import description</text></description>"
857 "<reference><text>import reference</text></reference>"
858 "</import>"
859 ELEMENT_WRAPPER_END;
860 assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, true), LY_SUCCESS);
861 assert_string_equal(lysp_mod->imports->name, "a");
862 assert_string_equal(lysp_mod->imports->prefix, "a_mod");
863 assert_string_equal(lysp_mod->imports->rev, "2015-01-01");
864 assert_string_equal(lysp_mod->imports->dsc, "import description");
865 assert_string_equal(lysp_mod->imports->ref, "import reference");
866 lysp_module_free(lysp_mod);
867 lys_module_free(lys_mod, NULL);
868
869 /* min subelems */
870 lys_mod = calloc(1, sizeof *lys_mod);
871 lysp_mod = calloc(1, sizeof *lysp_mod);
872 lys_mod->ctx = st->ctx;
873 lysp_mod->mod = lys_mod;
874 data = ELEMENT_WRAPPER_START
875 "<import module=\"a\">"
876 "<prefix value=\"a_mod\"/>"
877 "</import>"
878 ELEMENT_WRAPPER_END;
879 assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, true), LY_SUCCESS);
880 assert_string_equal(lysp_mod->imports->prefix, "a_mod");
881 lysp_module_free(lysp_mod);
882 lys_module_free(lys_mod, NULL);
883
884 /* invalid (missing prefix) */
885 lys_mod = calloc(1, sizeof *lys_mod);
886 lysp_mod = calloc(1, sizeof *lysp_mod);
887 lys_mod->ctx = st->ctx;
888 lysp_mod->mod = lys_mod;
889 data = ELEMENT_WRAPPER_START "<import module=\"a\">""</import>" ELEMENT_WRAPPER_END;
890 assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, false), LY_EVALID);
891 logbuf_assert("Missing mandatory subelement prefix of import element. Line number 1.");
892 lysp_module_free(lysp_mod);
893 lys_module_free(lys_mod, NULL);
894
895 /* invalid reused prefix */
896 lys_mod = calloc(1, sizeof *lys_mod);
897 lysp_mod = calloc(1, sizeof *lysp_mod);
898 lys_mod->ctx = st->ctx;
899 lysp_mod->mod = lys_mod;
900 data = ELEMENT_WRAPPER_START
901 "<import module=\"a\">"
902 "<prefix value=\"a_mod\"/>"
903 "</import>"
904 "<import module=\"a\">"
905 "<prefix value=\"a_mod\"/>"
906 "</import>"
907 ELEMENT_WRAPPER_END;
908 assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, false), LY_EVALID);
909 logbuf_assert("Prefix \"a_mod\" already used to import \"a\" module. Line number 1.");
910 lysp_module_free(lysp_mod);
911 lys_module_free(lys_mod, NULL);
912
913 st->finished_correctly = true;
914}
915
916static void
917test_status_elem(void **state)
918{
919 struct state *st = *state;
920 const char *data;
921 uint16_t flags = 0;
922
923 /* test valid values */
924 data = ELEMENT_WRAPPER_START "<status value=\"current\" />" ELEMENT_WRAPPER_END;
925 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200926 assert_true(flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +0200927
928 data = ELEMENT_WRAPPER_START "<status value=\"deprecated\" />" ELEMENT_WRAPPER_END;
929 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200930 assert_true(flags & LYS_STATUS_DEPRC);
David Sedlák32488102019-07-15 17:44:10 +0200931
932 data = ELEMENT_WRAPPER_START "<status value=\"obsolete\"></status>" ELEMENT_WRAPPER_END;
933 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200934 assert_true(flags & LYS_STATUS_OBSLT);
David Sedlák32488102019-07-15 17:44:10 +0200935
936 /* test invalid value */
937 data = ELEMENT_WRAPPER_START "<status value=\"invalid\"></status>" ELEMENT_WRAPPER_END;
938 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
939 logbuf_assert("Invalid value \"invalid\" of \"status\". Line number 1.");
940 st->finished_correctly = true;
941}
942
943static void
944test_ext_elem(void **state)
945{
946 struct state *st = *state;
947 const char *data;
948 struct lysp_ext *ext = NULL;
949
950 /* max subelems */
951 data = ELEMENT_WRAPPER_START
952 "<extension name=\"ext_name\">"
953 "<argument name=\"arg\"></argument>"
954 "<status value=\"current\"/>"
955 "<description><text>ext_desc</text></description>"
956 "<reference><text>ext_ref</text></reference>"
957 "</extension>"
958 ELEMENT_WRAPPER_END;
959 assert_int_equal(test_element_helper(st, &data, &ext, NULL, NULL, true), LY_SUCCESS);
960 assert_string_equal(ext->name, "ext_name");
961 assert_string_equal(ext->argument, "arg");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200962 assert_true(ext->flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +0200963 assert_string_equal(ext->dsc, "ext_desc");
964 assert_string_equal(ext->ref, "ext_ref");
965 lysp_ext_free(st->ctx, ext);
966 LY_ARRAY_FREE(ext);
967 ext = NULL;
968
969 /* min subelems */
970 data = ELEMENT_WRAPPER_START "<extension name=\"ext_name\"></extension>" ELEMENT_WRAPPER_END;
971 assert_int_equal(test_element_helper(st, &data, &ext, NULL, NULL, true), LY_SUCCESS);
972 assert_string_equal(ext->name, "ext_name");
973 lysp_ext_free(st->ctx, ext);
974 LY_ARRAY_FREE(ext);
975 ext = NULL;
976
977 st->finished_correctly = true;
978}
979
980static void
981test_yin_element_elem(void **state)
982{
983 struct state *st = *state;
984 const char *data;
985 uint16_t flags = 0;
986
987 data = ELEMENT_WRAPPER_START "<yin-element value=\"true\" />" ELEMENT_WRAPPER_END;
988 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200989 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +0200990
991 data = ELEMENT_WRAPPER_START "<yin-element value=\"false\" />" ELEMENT_WRAPPER_END;
992 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200993 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +0200994
995 data = ELEMENT_WRAPPER_START "<yin-element value=\"invalid\" />" ELEMENT_WRAPPER_END;
996 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200997 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +0200998 logbuf_assert("Invalid value \"invalid\" of \"yin-element\". Line number 1.");
999 st->finished_correctly = true;
1000}
1001
1002static void
1003test_yangversion_elem(void **state)
1004{
1005 struct state *st = *state;
1006 const char *data;
1007 uint8_t version = 0;
1008
1009 /* valid values */
1010 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.0\" />" ELEMENT_WRAPPER_END;
1011 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001012 assert_true(version & LYS_VERSION_1_0);
David Sedlák32488102019-07-15 17:44:10 +02001013 assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_0);
1014
1015 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.1\" />" ELEMENT_WRAPPER_END;
1016 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001017 assert_true(version & LYS_VERSION_1_1);
David Sedlák32488102019-07-15 17:44:10 +02001018 assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_1);
1019
1020 /* invalid value */
1021 data = ELEMENT_WRAPPER_START "<yang-version value=\"version\" />" ELEMENT_WRAPPER_END;
1022 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, false), LY_EVALID);
1023 logbuf_assert("Invalid value \"version\" of \"yang-version\". Line number 1.");
1024
1025 st->finished_correctly = true;
1026}
1027
1028static void
1029test_mandatory_elem(void **state)
1030{
1031 struct state *st = *state;
1032 const char *data;
1033 uint16_t man = 0;
1034
1035 /* valid values */
1036 data = ELEMENT_WRAPPER_START "<mandatory value=\"true\" />" ELEMENT_WRAPPER_END;
1037 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, true), LY_SUCCESS);
1038 assert_int_equal(man, LYS_MAND_TRUE);
1039 man = 0;
1040
1041 data = ELEMENT_WRAPPER_START "<mandatory value=\"false\" />" ELEMENT_WRAPPER_END;
1042 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, true), LY_SUCCESS);
1043 assert_int_equal(man, LYS_MAND_FALSE);
1044
1045 data = ELEMENT_WRAPPER_START "<mandatory value=\"invalid\" />" ELEMENT_WRAPPER_END;
1046 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, false), LY_EVALID);
1047 logbuf_assert("Invalid value \"invalid\" of \"mandatory\". Line number 1.");
1048
1049 st->finished_correctly = true;
1050}
1051
David Sedlák8e7bda82019-07-16 17:57:50 +02001052static void
1053test_argument_elem(void **state)
1054{
1055 struct state *st = *state;
1056 const char *data;
1057 uint16_t flags = 0;
1058 const char *arg;
1059 struct yin_argument_meta arg_meta = {&flags, &arg};
1060 /* max subelems */
1061 data = ELEMENT_WRAPPER_START
1062 "<argument name=\"arg-name\">"
1063 "<yin-element value=\"true\" />"
1064 "</argument>"
1065 ELEMENT_WRAPPER_END;
1066 assert_int_equal(test_element_helper(st, &data, &arg_meta, NULL, NULL, true), LY_SUCCESS);
1067 assert_string_equal(arg, "arg-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001068 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001069 flags = 0;
1070 FREE_STRING(st->ctx, arg);
1071 arg = NULL;
1072
1073 /* min subelems */
1074 data = ELEMENT_WRAPPER_START
1075 "<argument name=\"arg\">"
1076 "</argument>"
1077 ELEMENT_WRAPPER_END;
1078 assert_int_equal(test_element_helper(st, &data, &arg_meta, NULL, NULL, true), LY_SUCCESS);
1079 assert_string_equal(arg, "arg");
1080 assert_true(flags == 0);
1081 FREE_STRING(st->ctx, arg);
1082
1083 st->finished_correctly = true;
1084}
1085
1086static void
1087test_base_elem(void **state)
1088{
1089 struct state *st = *state;
1090 const char *data;
1091 const char **bases = NULL;
1092 struct lysp_type type = {};
1093
1094 /* as identity subelement */
1095 data = "<identity xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
1096 "<base name=\"base-name\"/>"
1097 "</identity>";
1098 assert_int_equal(test_element_helper(st, &data, &bases, NULL, NULL, true), LY_SUCCESS);
1099 assert_string_equal(*bases, "base-name");
1100 FREE_STRING(st->ctx, *bases);
1101 LY_ARRAY_FREE(bases);
1102
1103 /* as type subelement */
1104 data = "<type xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
1105 "<base name=\"base-name\"/>"
1106 "</type>";
1107 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1108 assert_string_equal(*type.bases, "base-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001109 assert_true(type.flags & LYS_SET_BASE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001110 FREE_STRING(st->ctx, *type.bases);
1111 LY_ARRAY_FREE(type.bases);
1112
1113 st->finished_correctly = true;
1114}
1115
1116static void
1117test_belongsto_elem(void **state)
1118{
1119 struct state *st = *state;
1120 const char *data;
1121 struct lysp_submodule submod;
1122
1123 data = ELEMENT_WRAPPER_START
1124 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>"
1125 ELEMENT_WRAPPER_END;
1126 assert_int_equal(test_element_helper(st, &data, &submod, NULL, NULL, true), LY_SUCCESS);
1127 assert_string_equal(submod.belongsto, "module-name");
1128 assert_string_equal(submod.prefix, "pref");
1129 FREE_STRING(st->ctx, submod.belongsto);
1130 FREE_STRING(st->ctx, submod.prefix);
1131
1132 data = ELEMENT_WRAPPER_START "<belongs-to module=\"module-name\"></belongs-to>" ELEMENT_WRAPPER_END;
1133 assert_int_equal(test_element_helper(st, &data, &submod, NULL, NULL, false), LY_EVALID);
1134 logbuf_assert("Missing mandatory subelement prefix of belongs-to element. Line number 1.");
1135 FREE_STRING(st->ctx, submod.belongsto);
1136
1137 st->finished_correctly = true;
1138}
1139
1140static void
1141test_config_elem(void **state)
1142{
1143 struct state *st = *state;
1144 const char *data;
1145 uint16_t flags = 0;
1146
1147 data = ELEMENT_WRAPPER_START "<config value=\"true\"/>" ELEMENT_WRAPPER_END;
1148 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001149 assert_true(flags & LYS_CONFIG_W);
David Sedlák8e7bda82019-07-16 17:57:50 +02001150 flags = 0;
1151
1152 data = ELEMENT_WRAPPER_START "<config value=\"false\"/>" ELEMENT_WRAPPER_END;
1153 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001154 assert_true(flags & LYS_CONFIG_R);
David Sedlák8e7bda82019-07-16 17:57:50 +02001155 flags = 0;
1156
1157 data = ELEMENT_WRAPPER_START "<config value=\"invalid\"/>" ELEMENT_WRAPPER_END;
1158 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
1159 logbuf_assert("Invalid value \"invalid\" of \"config\". Line number 1.");
1160
1161 st->finished_correctly = true;
1162}
1163
1164static void
1165test_default_elem(void **state)
1166{
1167 struct state *st = *state;
1168 const char *data;
1169 const char *val = NULL;
1170
1171 data = ELEMENT_WRAPPER_START "<default value=\"defaul-value\"/>" ELEMENT_WRAPPER_END;
1172 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1173 assert_string_equal(val, "defaul-value");
1174 FREE_STRING(st->ctx, val);
1175 val = NULL;
1176
1177 data = ELEMENT_WRAPPER_START "<default/>" ELEMENT_WRAPPER_END;
1178 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1179 logbuf_assert("Missing mandatory attribute value of default element. Line number 1.");
1180
1181 st->finished_correctly = true;
1182}
1183
1184static void
1185test_err_app_tag_elem(void **state)
1186{
1187 struct state *st = *state;
1188 const char *data;
1189 const char *val = NULL;
1190
1191 data = ELEMENT_WRAPPER_START "<error-app-tag value=\"val\"/>" ELEMENT_WRAPPER_END;
1192 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1193 assert_string_equal(val, "val");
1194 FREE_STRING(st->ctx, val);
1195 val = NULL;
1196
1197 data = ELEMENT_WRAPPER_START "<error-app-tag/>" ELEMENT_WRAPPER_END;
1198 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1199 logbuf_assert("Missing mandatory attribute value of error-app-tag element. Line number 1.");
1200
1201 st->finished_correctly = true;
1202}
1203
1204static void
1205test_err_msg_elem(void **state)
1206{
1207 struct state *st = *state;
1208 const char *data;
1209 const char *val = NULL;
1210
1211 data = ELEMENT_WRAPPER_START "<error-message><value>val</value></error-message>" ELEMENT_WRAPPER_END;
1212 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1213 assert_string_equal(val, "val");
1214 FREE_STRING(st->ctx, val);
1215
1216 data = ELEMENT_WRAPPER_START "<error-message></error-message>" ELEMENT_WRAPPER_END;
1217 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1218 logbuf_assert("Missing mandatory subelement value of error-message element. Line number 1.");
1219
1220 st->finished_correctly = true;
1221}
1222
1223static void
1224test_fracdigits_elem(void **state)
1225{
1226 struct state *st = *state;
1227 const char *data;
1228 struct lysp_type type = {};
1229
1230 /* valid value */
1231 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"10\"></fraction-digits>" ELEMENT_WRAPPER_END;
1232 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1233 assert_int_equal(type.fraction_digits, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001234 assert_true(type.flags & LYS_SET_FRDIGITS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001235
1236 /* invalid values */
1237 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"-1\"></fraction-digits>" ELEMENT_WRAPPER_END;
1238 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1239 logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1.");
1240
1241 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"02\"></fraction-digits>" ELEMENT_WRAPPER_END;
1242 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1243 logbuf_assert("Invalid value \"02\" of \"fraction-digits\". Line number 1.");
1244
1245 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"1p\"></fraction-digits>" ELEMENT_WRAPPER_END;
1246 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1247 logbuf_assert("Invalid value \"1p\" of \"fraction-digits\". Line number 1.");
1248
1249 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"19\"></fraction-digits>" ELEMENT_WRAPPER_END;
1250 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1251 logbuf_assert("Invalid value \"19\" of \"fraction-digits\". Line number 1.");
1252
1253 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"999999999999999999\"></fraction-digits>" ELEMENT_WRAPPER_END;
1254 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1255 logbuf_assert("Invalid value \"999999999999999999\" of \"fraction-digits\". Line number 1.");
1256
1257 st->finished_correctly = true;
1258}
1259
1260static void
1261test_iffeature_elem(void **state)
1262{
1263 struct state *st = *state;
1264 const char *data;
1265 const char **iffeatures = NULL;
1266
1267 data = ELEMENT_WRAPPER_START "<if-feature name=\"local-storage\"></if-feature>" ELEMENT_WRAPPER_END;
1268 assert_int_equal(test_element_helper(st, &data, &iffeatures, NULL, NULL, true), LY_SUCCESS);
1269 assert_string_equal(*iffeatures, "local-storage");
1270 FREE_STRING(st->ctx, *iffeatures);
1271 LY_ARRAY_FREE(iffeatures);
1272 iffeatures = NULL;
1273
1274 data = ELEMENT_WRAPPER_START "<if-feature/>" ELEMENT_WRAPPER_END;
1275 assert_int_equal(test_element_helper(st, &data, &iffeatures, NULL, NULL, false), LY_EVALID);
1276 logbuf_assert("Missing mandatory attribute name of if-feature element. Line number 1.");
1277 LY_ARRAY_FREE(iffeatures);
1278 iffeatures = NULL;
1279
1280 st->finished_correctly = true;
1281}
1282
1283static void
1284test_length_elem(void **state)
1285{
1286 struct state *st = *state;
1287 const char *data;
1288 struct lysp_type type = {};
1289
1290 /* max subelems */
1291 data = ELEMENT_WRAPPER_START
1292 "<length value=\"length-str\">"
1293 "<error-message><value>err-msg</value></error-message>"
1294 "<error-app-tag value=\"err-app-tag\"/>"
1295 "<description><text>desc</text></description>"
1296 "<reference><text>ref</text></reference>"
1297 "</length>"
1298 ELEMENT_WRAPPER_END;
1299 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1300 assert_string_equal(type.length->arg, "length-str");
1301 assert_string_equal(type.length->emsg, "err-msg");
1302 assert_string_equal(type.length->eapptag, "err-app-tag");
1303 assert_string_equal(type.length->dsc, "desc");
1304 assert_string_equal(type.length->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001305 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001306 lysp_type_free(st->ctx, &type);
1307 memset(&type, 0, sizeof(type));
1308
1309 /* min subelems */
1310 data = ELEMENT_WRAPPER_START
1311 "<length value=\"length-str\">"
1312 "</length>"
1313 ELEMENT_WRAPPER_END;
1314 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1315 assert_string_equal(type.length->arg, "length-str");
1316 lysp_type_free(st->ctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001317 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001318 memset(&type, 0, sizeof(type));
1319
1320 data = ELEMENT_WRAPPER_START "<length></length>" ELEMENT_WRAPPER_END;
1321 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1322 logbuf_assert("Missing mandatory attribute value of length element. Line number 1.");
1323 lysp_type_free(st->ctx, &type);
1324 memset(&type, 0, sizeof(type));
1325
1326 st->finished_correctly = true;
1327}
1328
1329static void
1330test_modifier_elem(void **state)
1331{
1332 struct state *st = *state;
1333 const char *data;
1334 const char *pat = lydict_insert(st->ctx, "\006pattern", 8);
1335
1336 data = ELEMENT_WRAPPER_START "<modifier value=\"invert-match\" />" ELEMENT_WRAPPER_END;
1337 assert_int_equal(test_element_helper(st, &data, &pat, NULL, NULL, true), LY_SUCCESS);
1338 assert_string_equal(pat, "\x015pattern");
1339 FREE_STRING(st->ctx, pat);
1340
1341 pat = lydict_insert(st->ctx, "\006pattern", 8);
1342 data = ELEMENT_WRAPPER_START "<modifier value=\"invert\" />" ELEMENT_WRAPPER_END;
1343 assert_int_equal(test_element_helper(st, &data, &pat, NULL, NULL, false), LY_EVALID);
1344 logbuf_assert("Invalid value \"invert\" of \"modifier\". Line number 1.");
1345 FREE_STRING(st->ctx, pat);
1346
1347 st->finished_correctly = true;
1348}
1349
1350static void
1351test_namespace_elem(void **state)
1352{
1353 struct state *st = *state;
1354 const char *data;
1355 const char *ns;
1356
1357 data = ELEMENT_WRAPPER_START "<namespace uri=\"ns\"/>" ELEMENT_WRAPPER_END;
1358 assert_int_equal(test_element_helper(st, &data, &ns, NULL, NULL, true), LY_SUCCESS);
1359 assert_string_equal(ns, "ns");
1360 FREE_STRING(st->ctx, ns);
1361
1362 data = ELEMENT_WRAPPER_START "<namespace/>" ELEMENT_WRAPPER_END;
1363 assert_int_equal(test_element_helper(st, &data, &ns, NULL, NULL, false), LY_EVALID);
1364 logbuf_assert("Missing mandatory attribute uri of namespace element. Line number 1.");
1365
1366 st->finished_correctly = true;
1367}
1368
1369static void
1370test_path_elem(void **state)
1371{
1372 struct state *st = *state;
1373 const char *data;
1374 struct lysp_type type = {};
1375
1376 data = ELEMENT_WRAPPER_START "<path value=\"path-val\"/>" ELEMENT_WRAPPER_END;
1377 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1378 assert_string_equal("path-val", type.path);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001379 assert_true(type.flags & LYS_SET_PATH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001380 lysp_type_free(st->ctx, &type);
1381
1382 st->finished_correctly = true;
1383}
1384
1385static void
1386test_pattern_elem(void **state)
1387{
1388 struct state *st = *state;
1389 const char *data;
1390 struct lysp_type type = {};
1391
1392 /* max subelems */
1393 data = ELEMENT_WRAPPER_START
1394 "<pattern value=\"super_pattern\">"
1395 "<modifier value=\"invert-match\"/>"
1396 "<error-message><value>err-msg-value</value></error-message>"
1397 "<error-app-tag value=\"err-app-tag-value\"/>"
1398 "<description><text>pattern-desc</text></description>"
1399 "<reference><text>pattern-ref</text></reference>"
1400 "</pattern>"
1401 ELEMENT_WRAPPER_END;
1402 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001403 assert_true(type.flags & LYS_SET_PATTERN);
David Sedlák8e7bda82019-07-16 17:57:50 +02001404 assert_string_equal(type.patterns->arg, "\x015super_pattern");
1405 assert_string_equal(type.patterns->dsc, "pattern-desc");
1406 assert_string_equal(type.patterns->eapptag, "err-app-tag-value");
1407 assert_string_equal(type.patterns->emsg, "err-msg-value");
1408 assert_string_equal(type.patterns->dsc, "pattern-desc");
1409 assert_string_equal(type.patterns->ref, "pattern-ref");
1410 lysp_type_free(st->ctx, &type);
1411 memset(&type, 0, sizeof(type));
1412
1413 /* min subelems */
1414 data = ELEMENT_WRAPPER_START "<pattern value=\"pattern\"> </pattern>" ELEMENT_WRAPPER_END;
1415 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1416 assert_string_equal(type.patterns->arg, "\x006pattern");
1417 lysp_type_free(st->ctx, &type);
1418 memset(&type, 0, sizeof(type));
1419
1420 st->finished_correctly = true;
1421}
1422
1423static void
1424test_value_position_elem(void **state)
1425{
1426 struct state *st = *state;
1427 const char *data;
1428 struct lysp_type_enum en = {};
1429
1430 /* valid values */
1431 data = ELEMENT_WRAPPER_START "<value value=\"55\" />" ELEMENT_WRAPPER_END;
1432 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1433 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001434 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001435 memset(&en, 0, sizeof(en));
1436
1437 data = ELEMENT_WRAPPER_START "<value value=\"-55\"/>" ELEMENT_WRAPPER_END;
1438 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1439 assert_int_equal(en.value, -55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001440 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001441 memset(&en, 0, sizeof(en));
1442
1443 data = ELEMENT_WRAPPER_START "<value value=\"0\"/>" ELEMENT_WRAPPER_END;
1444 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1445 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001446 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001447 memset(&en, 0, sizeof(en));
1448
1449 data = ELEMENT_WRAPPER_START "<value value=\"-0\"/>" ELEMENT_WRAPPER_END;
1450 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1451 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001452 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001453 memset(&en, 0, sizeof(en));
1454
1455 /* valid positions */
1456 data = ELEMENT_WRAPPER_START "<position value=\"55\" />" ELEMENT_WRAPPER_END;
1457 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1458 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001459 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001460 memset(&en, 0, sizeof(en));
1461
1462 data = ELEMENT_WRAPPER_START "<position value=\"0\" />" ELEMENT_WRAPPER_END;
1463 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1464 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001465 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001466 memset(&en, 0, sizeof(en));
1467
1468 /* invalid values */
1469 data = ELEMENT_WRAPPER_START "<value value=\"99999999999999999999999\"/>" ELEMENT_WRAPPER_END;
1470 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1471 logbuf_assert("Invalid value \"99999999999999999999999\" of \"value\". Line number 1.");
1472
1473 data = ELEMENT_WRAPPER_START "<value value=\"1k\"/>" ELEMENT_WRAPPER_END;
1474 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1475 logbuf_assert("Invalid value \"1k\" of \"value\". Line number 1.");
1476
David Sedlák69f01612019-07-17 11:41:08 +02001477 data = ELEMENT_WRAPPER_START "<value value=\"\"/>" ELEMENT_WRAPPER_END;
1478 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1479 logbuf_assert("Invalid value \"\" of \"value\". Line number 1.");
1480
David Sedlák8e7bda82019-07-16 17:57:50 +02001481 /*invalid positions */
1482 data = ELEMENT_WRAPPER_START "<position value=\"-5\"/>" ELEMENT_WRAPPER_END;
1483 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1484 logbuf_assert("Invalid value \"-5\" of \"position\". Line number 1.");
1485
1486 data = ELEMENT_WRAPPER_START "<position value=\"-0\"/>" ELEMENT_WRAPPER_END;
1487 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1488 logbuf_assert("Invalid value \"-0\" of \"position\". Line number 1.");
1489
1490 data = ELEMENT_WRAPPER_START "<position value=\"99999999999999999999\"/>" ELEMENT_WRAPPER_END;
1491 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1492 logbuf_assert("Invalid value \"99999999999999999999\" of \"position\". Line number 1.");
1493
David Sedlák69f01612019-07-17 11:41:08 +02001494 data = ELEMENT_WRAPPER_START "<position value=\"\"/>" ELEMENT_WRAPPER_END;
1495 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1496 logbuf_assert("Invalid value \"\" of \"position\". Line number 1.");
1497
1498 st->finished_correctly = true;
1499}
1500
1501static void
1502test_prefix_elem(void **state)
1503{
1504 struct state *st = *state;
1505 const char *data;
1506 const char *value = NULL;
1507
1508 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\"/>" ELEMENT_WRAPPER_END;
1509 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
1510 assert_string_equal(value, "pref");
1511 FREE_STRING(st->ctx, value);
1512
1513 st->finished_correctly = true;
1514}
1515
1516static void
1517test_range_elem(void **state)
1518{
1519 struct state *st = *state;
1520 const char *data;
1521 struct lysp_type type = {};
1522
1523 /* max subelems */
1524 data = ELEMENT_WRAPPER_START
1525 "<range value=\"range-str\">"
1526 "<error-message><value>err-msg</value></error-message>"
1527 "<error-app-tag value=\"err-app-tag\" />"
1528 "<description><text>desc</text></description>"
1529 "<reference><text>ref</text></reference>"
1530 "</range>"
1531 ELEMENT_WRAPPER_END;
1532 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1533 assert_string_equal(type.range->arg, "range-str");
1534 assert_string_equal(type.range->dsc, "desc");
1535 assert_string_equal(type.range->eapptag, "err-app-tag");
1536 assert_string_equal(type.range->emsg, "err-msg");
1537 assert_string_equal(type.range->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001538 assert_true(type.flags & LYS_SET_RANGE);
David Sedlák69f01612019-07-17 11:41:08 +02001539 lysp_type_free(st->ctx, &type);
1540 memset(&type, 0, sizeof(type));
1541
1542 /* min subelems */
1543 data = ELEMENT_WRAPPER_START "<range value=\"range-str\"/>" ELEMENT_WRAPPER_END;
1544 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1545 assert_string_equal(type.range->arg, "range-str");
1546 lysp_type_free(st->ctx, &type);
1547 memset(&type, 0, sizeof(type));
1548
1549 st->finished_correctly = true;
1550}
1551
1552static void
1553test_reqinstance_elem(void **state)
1554{
1555 struct state *st = *state;
1556 const char *data;
1557 struct lysp_type type = {};
1558
1559 data = ELEMENT_WRAPPER_START "<require-instance value=\"true\"/>" ELEMENT_WRAPPER_END;
1560 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1561 assert_int_equal(type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001562 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001563 memset(&type, 0, sizeof(type));
1564
1565 data = ELEMENT_WRAPPER_START "<require-instance value=\"false\"/>" ELEMENT_WRAPPER_END;
1566 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1567 assert_int_equal(type.require_instance, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001568 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001569 memset(&type, 0, sizeof(type));
1570
1571 data = ELEMENT_WRAPPER_START "<require-instance value=\"invalid\"/>" ELEMENT_WRAPPER_END;
1572 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1573 memset(&type, 0, sizeof(type));
1574 logbuf_assert("Invalid value \"invalid\" of \"require-instance\". Line number 1.");
1575
1576 st->finished_correctly = true;
1577}
1578
1579static void
1580test_revision_date_elem(void **state)
1581{
1582 struct state *st = *state;
1583 const char *data;
1584 char rev[LY_REV_SIZE];
1585
1586 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\"/>" ELEMENT_WRAPPER_END;
1587 assert_int_equal(test_element_helper(st, &data, rev, NULL, NULL, true), LY_SUCCESS);
1588 assert_string_equal(rev, "2000-01-01");
1589
1590 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-50-05\"/>" ELEMENT_WRAPPER_END;
1591 assert_int_equal(test_element_helper(st, &data, rev, NULL, NULL, false), LY_EVALID);
1592 logbuf_assert("Invalid value \"2000-50-05\" of \"revision-date\". Line number 1.");
1593
1594 st->finished_correctly = true;
1595}
1596
1597static void
1598test_unique_elem(void **state)
1599{
1600 struct state *st = *state;
1601 const char *data;
1602 const char **values = NULL;
1603
1604 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\"/>" ELEMENT_WRAPPER_END;
1605 assert_int_equal(test_element_helper(st, &data, &values, NULL, NULL, true), LY_SUCCESS);
1606 assert_string_equal(*values, "tag");
1607 FREE_STRING(st->ctx, *values);
1608 LY_ARRAY_FREE(values);
1609
1610 st->finished_correctly = true;
1611}
1612
1613static void
1614test_units_elem(void **state)
1615{
1616 struct state *st = *state;
1617 const char *data;
1618 const char *values = NULL;
1619
1620 data = ELEMENT_WRAPPER_START "<units name=\"name\"/>" ELEMENT_WRAPPER_END;
1621 assert_int_equal(test_element_helper(st, &data, &values, NULL, NULL, true), LY_SUCCESS);
1622 assert_string_equal(values, "name");
1623 FREE_STRING(st->ctx, values);
1624
1625 st->finished_correctly = true;
1626}
1627
1628static void
1629test_when_elem(void **state)
1630{
1631 struct state *st = *state;
1632 const char *data;
1633 struct lysp_when *when = NULL;
1634
1635 data = ELEMENT_WRAPPER_START
1636 "<when condition=\"cond\">"
1637 "<description><text>desc</text></description>"
1638 "<reference><text>ref</text></reference>"
1639 "</when>"
1640 ELEMENT_WRAPPER_END;
1641 assert_int_equal(test_element_helper(st, &data, &when, NULL, NULL, true), LY_SUCCESS);
1642 assert_string_equal(when->cond, "cond");
1643 assert_string_equal(when->dsc, "desc");
1644 assert_string_equal(when->ref, "ref");
1645 lysp_when_free(st->ctx, when);
1646 free(when);
1647 when = NULL;
1648
1649 data = ELEMENT_WRAPPER_START "<when condition=\"cond\" />" ELEMENT_WRAPPER_END;
1650 assert_int_equal(test_element_helper(st, &data, &when, NULL, NULL, true), LY_SUCCESS);
1651 assert_string_equal(when->cond, "cond");
1652 lysp_when_free(st->ctx, when);
1653 free(when);
1654 when = NULL;
1655
1656 st->finished_correctly = true;
1657}
1658
1659static void
1660test_yin_text_value_elem(void **state)
1661{
1662 struct state *st = *state;
1663 const char *data;
1664 const char *val;
1665
1666 data = ELEMENT_WRAPPER_START "<text>text</text>" ELEMENT_WRAPPER_END;
1667 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1668 assert_string_equal(val, "text");
1669 FREE_STRING(st->ctx, val);
1670
1671 data = "<error-message xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <value>text</value> </error-message>";
1672 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1673 assert_string_equal(val, "text");
1674 FREE_STRING(st->ctx, val);
1675
1676 data = ELEMENT_WRAPPER_START "<text></text>" ELEMENT_WRAPPER_END;
1677 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1678 assert_string_equal("", val);
1679 FREE_STRING(st->ctx, val);
1680
David Sedlák8e7bda82019-07-16 17:57:50 +02001681 st->finished_correctly = true;
1682}
David Sedlák32488102019-07-15 17:44:10 +02001683
David Sedlák374d2b32019-07-17 15:06:55 +02001684static void
1685test_type_elem(void **state)
1686{
1687 struct state *st = *state;
1688 const char *data;
1689 struct lysp_type type = {};
1690
1691 /* max subelems */
1692 data = ELEMENT_WRAPPER_START
1693 "<type name=\"type-name\">"
1694 "<base name=\"base-name\"/>"
1695 "<bit name=\"bit\"/>"
1696 "<enum name=\"enum\"/>"
1697 "<fraction-digits value=\"2\"/>"
1698 "<length value=\"length\"/>"
1699 "<path value=\"path\"/>"
1700 "<pattern value=\"pattern\"/>"
1701 "<range value=\"range\" />"
1702 "<require-instance value=\"true\"/>"
1703 "<type name=\"sub-type-name\"/>"
1704 "</type>"
1705 ELEMENT_WRAPPER_END;
1706 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1707 assert_string_equal(type.name, "type-name");
1708 assert_string_equal(*type.bases, "base-name");
1709 assert_string_equal(type.bits->name, "bit");
1710 assert_string_equal(type.enums->name, "enum");
1711 assert_int_equal(type.fraction_digits, 2);
1712 assert_string_equal(type.length->arg, "length");
1713 assert_string_equal(type.path, "path");
1714 assert_string_equal(type.patterns->arg, "\006pattern");
1715 assert_string_equal(type.range->arg, "range");
1716 assert_int_equal(type.require_instance, 1);
1717 assert_string_equal(type.types->name, "sub-type-name");
1718 lysp_type_free(st->ctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001719 assert_true(type.flags & LYS_SET_BASE);
1720 assert_true(type.flags & LYS_SET_BIT);
1721 assert_true(type.flags & LYS_SET_ENUM);
1722 assert_true(type.flags & LYS_SET_FRDIGITS);
1723 assert_true(type.flags & LYS_SET_LENGTH);
1724 assert_true(type.flags & LYS_SET_PATH);
1725 assert_true(type.flags & LYS_SET_PATTERN);
1726 assert_true(type.flags & LYS_SET_RANGE);
1727 assert_true(type.flags & LYS_SET_REQINST);
1728 assert_true(type.flags & LYS_SET_TYPE);
David Sedlák374d2b32019-07-17 15:06:55 +02001729 memset(&type, 0, sizeof(type));
1730
1731 /* min subelems */
1732 data = ELEMENT_WRAPPER_START "<type name=\"type-name\"/>" ELEMENT_WRAPPER_END;
1733 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1734 lysp_type_free(st->ctx, &type);
1735 memset(&type, 0, sizeof(type));
1736
1737 st->finished_correctly = true;
1738}
1739
David Sedlák1af868e2019-07-17 17:03:14 +02001740static void
1741test_max_elems_elem(void **state)
1742{
1743 struct state *st = *state;
1744 const char *data;
1745 struct lysp_node_list list = {};
1746 struct lysp_node_leaflist llist = {};
1747 struct lysp_refine refine = {};
1748
1749 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"unbounded\"/> </refine>";
1750 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
1751 assert_int_equal(refine.max, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001752 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001753
1754 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"5\"/> </list>";
1755 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, true), LY_SUCCESS);
1756 assert_int_equal(list.max, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001757 assert_true(list.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001758
1759 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"85\"/> </leaf-list>";
1760 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, true), LY_SUCCESS);
1761 assert_int_equal(llist.max, 85);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001762 assert_true(llist.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001763
1764 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"10\"/> </refine>";
1765 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
1766 assert_int_equal(refine.max, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001767 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001768
1769 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"0\"/> </list>";
1770 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1771 logbuf_assert("Invalid value \"0\" of \"max-elements\". Line number 1.");
1772
1773 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"-10\"/> </list>";
1774 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1775 logbuf_assert("Invalid value \"-10\" of \"max-elements\". Line number 1.");
1776
1777 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"k\"/> </list>";
1778 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1779 logbuf_assert("Invalid value \"k\" of \"max-elements\". Line number 1.");
1780
1781 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"u12\"/> </list>";
1782 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1783 logbuf_assert("Invalid value \"u12\" of \"max-elements\". Line number 1.");
1784
1785 st->finished_correctly = true;
1786}
1787
David Sedlák09e18c92019-07-18 11:17:11 +02001788static void
1789test_min_elems_elem(void **state)
1790{
1791 struct state *st = *state;
1792 const char *data;
1793 struct lysp_node_list list = {};
1794 struct lysp_node_leaflist llist = {};
1795 struct lysp_refine refine = {};
1796
1797 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"0\"/> </refine>";
1798 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
1799 assert_int_equal(refine.min, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001800 assert_true(refine.flags & LYS_SET_MIN);
David Sedlák09e18c92019-07-18 11:17:11 +02001801
1802 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"41\"/> </list>";
1803 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, true), LY_SUCCESS);
1804 assert_int_equal(list.min, 41);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001805 assert_true(list.flags & LYS_SET_MIN);
David Sedlák09e18c92019-07-18 11:17:11 +02001806
1807 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"50\"/> </leaf-list>";
1808 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, true), LY_SUCCESS);
1809 assert_int_equal(llist.min, 50);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001810 assert_true(llist.flags & LYS_SET_MIN);
David Sedlák09e18c92019-07-18 11:17:11 +02001811
1812 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"-5\"/> </leaf-list>";
1813 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1814 logbuf_assert("Value \"-5\" is out of \"min-elements\" bounds. Line number 1.");
1815
1816 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"99999999999999999\"/> </leaf-list>";
1817 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1818 logbuf_assert("Value \"99999999999999999\" is out of \"min-elements\" bounds. Line number 1.");
1819
1820 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"5k\"/> </leaf-list>";
1821 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1822 logbuf_assert("Invalid value \"5k\" of \"min-elements\". Line number 1.");
1823
1824 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"05\"/> </leaf-list>";
1825 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1826 logbuf_assert("Invalid value \"05\" of \"min-elements\". Line number 1.");
1827
1828 st->finished_correctly = true;
1829}
1830
David Sedláka2dad212019-07-18 12:45:19 +02001831static void
1832test_ordby_elem(void **state)
1833{
1834 struct state *st = *state;
1835 const char *data;
1836 uint16_t flags = 0;
1837
1838 data = ELEMENT_WRAPPER_START "<ordered-by value=\"system\"/>" ELEMENT_WRAPPER_END;
1839 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001840 assert_true(flags & LYS_ORDBY_SYSTEM);
David Sedláka2dad212019-07-18 12:45:19 +02001841
1842 data = ELEMENT_WRAPPER_START "<ordered-by value=\"user\"/>" ELEMENT_WRAPPER_END;
1843 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001844 assert_true(flags & LYS_ORDBY_USER);
David Sedláka2dad212019-07-18 12:45:19 +02001845
1846 data = ELEMENT_WRAPPER_START "<ordered-by value=\"inv\"/>" ELEMENT_WRAPPER_END;
1847 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
1848 logbuf_assert("Invalid value \"inv\" of \"ordered-by\". Line number 1.");
1849
1850 st->finished_correctly = true;
1851}
1852
David Sedlák8a83bbb2019-07-18 14:46:00 +02001853static void
1854test_any_elem(void **state)
1855{
1856 struct state *st = *state;
1857 const char *data;
1858 struct lysp_node *siblings = NULL;
1859 struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
1860 struct lysp_node_anydata *parsed = NULL;
1861
1862 /* anyxml max subelems */
1863 data = ELEMENT_WRAPPER_START
1864 "<anyxml name=\"any-name\">"
1865 "<config value=\"true\" />"
1866 "<description><text>desc</text></description>"
1867 "<if-feature name=\"feature\" />"
1868 "<mandatory value=\"true\" />"
1869 "<must condition=\"must-cond\" />"
1870 "<reference><text>ref</text></reference>"
1871 "<status value=\"deprecated\"/>"
1872 "<when condition=\"when-cond\"/>"
1873 "</anyxml>"
1874 ELEMENT_WRAPPER_END;
1875 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1876 parsed = (struct lysp_node_anydata *)siblings;
1877 assert_null(parsed->parent);
1878 assert_int_equal(parsed->nodetype, LYS_ANYXML);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001879 assert_true(parsed->flags & LYS_CONFIG_W);
1880 assert_true(parsed->flags & LYS_MAND_TRUE);
1881 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001882 assert_null(parsed->next);
1883 assert_string_equal(parsed->name, "any-name");
1884 assert_string_equal(parsed->dsc, "desc");
1885 assert_string_equal(parsed->ref, "ref");
1886 assert_string_equal(parsed->when->cond, "when-cond");
1887 assert_string_equal(*parsed->iffeatures, "feature");
1888 assert_null(parsed->exts);
1889 lysp_node_free(st->ctx, siblings);
1890 siblings = NULL;
1891
1892 /* anydata max subelems */
1893 data = ELEMENT_WRAPPER_START
1894 "<anydata name=\"any-name\">"
1895 "<config value=\"true\" />"
1896 "<description><text>desc</text></description>"
1897 "<if-feature name=\"feature\" />"
1898 "<mandatory value=\"true\" />"
1899 "<must condition=\"must-cond\" />"
1900 "<reference><text>ref</text></reference>"
1901 "<status value=\"deprecated\"/>"
1902 "<when condition=\"when-cond\"/>"
1903 "</anydata>"
1904 ELEMENT_WRAPPER_END;
1905 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1906 parsed = (struct lysp_node_anydata *)siblings;
1907 assert_null(parsed->parent);
1908 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001909 assert_true(parsed->flags & LYS_CONFIG_W);
1910 assert_true(parsed->flags & LYS_MAND_TRUE);
1911 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001912 assert_null(parsed->next);
1913 assert_string_equal(parsed->name, "any-name");
1914 assert_string_equal(parsed->dsc, "desc");
1915 assert_string_equal(parsed->ref, "ref");
1916 assert_string_equal(parsed->when->cond, "when-cond");
1917 assert_string_equal(*parsed->iffeatures, "feature");
1918 assert_null(parsed->exts);
1919 lysp_node_free(st->ctx, siblings);
1920 siblings = NULL;
1921
1922 /* min subelems */
1923 node_meta.parent = (void *)0x10;
1924 data = ELEMENT_WRAPPER_START "<anydata name=\"any-name\"> </anydata>" ELEMENT_WRAPPER_END;
1925 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1926 parsed = (struct lysp_node_anydata *)siblings;
1927 assert_ptr_equal(parsed->parent, node_meta.parent);
1928 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
1929 assert_null(parsed->next);
1930 assert_null(parsed->exts);
1931 lysp_node_free(st->ctx, siblings);
1932
1933 st->finished_correctly = true;
1934}
1935
David Sedlák203ca3a2019-07-18 15:26:25 +02001936static void
1937test_leaf_elem(void **state)
1938{
1939 struct state *st = *state;
1940 const char *data;
1941 struct lysp_node *siblings = NULL;
1942 struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
1943 struct lysp_node_leaf *parsed = NULL;
1944
1945 /* max elements */
1946 data = ELEMENT_WRAPPER_START
1947 "<leaf name=\"leaf\">"
1948 "<config value=\"true\" />"
1949 "<default value=\"def-val\"/>"
1950 "<description><text>desc</text></description>"
1951 "<if-feature name=\"feature\" />"
1952 "<mandatory value=\"true\" />"
1953 "<must condition=\"must-cond\" />"
1954 "<reference><text>ref</text></reference>"
1955 "<status value=\"deprecated\"/>"
1956 "<type name=\"type\"/>"
1957 "<units name=\"uni\"/>"
1958 "<when condition=\"when-cond\"/>"
1959 "</leaf>"
1960 ELEMENT_WRAPPER_END;
1961 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1962 parsed = (struct lysp_node_leaf *)siblings;
1963 assert_null(parsed->parent);
1964 assert_int_equal(parsed->nodetype, LYS_LEAF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001965 assert_true(parsed->flags & LYS_CONFIG_W);
1966 assert_true(parsed->flags & LYS_MAND_TRUE);
1967 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák203ca3a2019-07-18 15:26:25 +02001968 assert_null(parsed->next);
1969 assert_string_equal(parsed->name, "leaf");
1970 assert_string_equal(parsed->dsc, "desc");
1971 assert_string_equal(parsed->ref, "ref");
1972 assert_string_equal(parsed->when->cond, "when-cond");
1973 assert_string_equal(*parsed->iffeatures, "feature");
1974 assert_null(parsed->exts);
1975 assert_string_equal(parsed->musts->arg, "must-cond");
1976 assert_string_equal(parsed->type.name, "type");
1977 assert_string_equal(parsed->units, "uni");
1978 assert_string_equal(parsed->dflt, "def-val");
1979 lysp_node_free(st->ctx, siblings);
1980 siblings = NULL;
1981
1982 /* min elements */
1983 data = ELEMENT_WRAPPER_START "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>" ELEMENT_WRAPPER_END;
1984 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1985 parsed = (struct lysp_node_leaf *)siblings;
1986 assert_string_equal(parsed->name, "leaf");
1987 assert_string_equal(parsed->type.name, "type");
1988 lysp_node_free(st->ctx, siblings);
1989 siblings = NULL;
1990
1991 st->finished_correctly = true;
1992}
1993
David Sedlákc3da3ef2019-07-19 12:56:08 +02001994static void
1995test_leaf_list_elem(void **state)
1996{
1997 struct state *st = *state;
1998 const char *data;
1999 struct lysp_node *siblings = NULL;
2000 struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
2001 struct lysp_node_leaflist *parsed = NULL;
2002
2003 data = ELEMENT_WRAPPER_START
2004 "<leaf-list name=\"llist\">"
2005 "<config value=\"true\" />"
2006 "<default value=\"def-val0\"/>"
2007 "<default value=\"def-val1\"/>"
2008 "<description><text>desc</text></description>"
2009 "<if-feature name=\"feature\"/>"
2010 "<max-elements value=\"5\"/>"
2011 "<must condition=\"must-cond\"/>"
2012 "<ordered-by value=\"user\" />"
2013 "<reference><text>ref</text></reference>"
2014 "<status value=\"current\"/>"
2015 "<type name=\"type\"/>"
2016 "<units name=\"uni\"/>"
2017 "<when condition=\"when-cond\"/>"
2018 "</leaf-list>"
2019 ELEMENT_WRAPPER_END;
2020 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2021 parsed = (struct lysp_node_leaflist *)siblings;
2022 assert_string_equal(parsed->dflts[0], "def-val0");
2023 assert_string_equal(parsed->dflts[1], "def-val1");
2024 assert_string_equal(parsed->dsc, "desc");
2025 assert_string_equal(*parsed->iffeatures, "feature");
2026 assert_int_equal(parsed->max, 5);
2027 assert_string_equal(parsed->musts->arg, "must-cond");
2028 assert_string_equal(parsed->name, "llist");
2029 assert_null(parsed->next);
2030 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2031 assert_null(parsed->parent);
2032 assert_string_equal(parsed->ref, "ref");
2033 assert_string_equal(parsed->type.name, "type");
2034 assert_string_equal(parsed->units, "uni");
2035 assert_string_equal(parsed->when->cond, "when-cond");
2036 assert_true(parsed->flags & LYS_CONFIG_W);
2037 assert_true(parsed->flags & LYS_ORDBY_USER);
2038 assert_true(parsed->flags & LYS_STATUS_CURR);
2039 lysp_node_free(st->ctx, siblings);
2040 siblings = NULL;
2041
2042 data = ELEMENT_WRAPPER_START
2043 "<leaf-list name=\"llist\">"
2044 "<config value=\"true\" />"
2045 "<description><text>desc</text></description>"
2046 "<if-feature name=\"feature\"/>"
2047 "<min-elements value=\"5\"/>"
2048 "<must condition=\"must-cond\"/>"
2049 "<ordered-by value=\"user\" />"
2050 "<reference><text>ref</text></reference>"
2051 "<status value=\"current\"/>"
2052 "<type name=\"type\"/>"
2053 "<units name=\"uni\"/>"
2054 "<when condition=\"when-cond\"/>"
2055 "</leaf-list>"
2056 ELEMENT_WRAPPER_END;
2057 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2058 parsed = (struct lysp_node_leaflist *)siblings;
2059 assert_string_equal(parsed->dsc, "desc");
2060 assert_string_equal(*parsed->iffeatures, "feature");
2061 assert_int_equal(parsed->min, 5);
2062 assert_string_equal(parsed->musts->arg, "must-cond");
2063 assert_string_equal(parsed->name, "llist");
2064 assert_null(parsed->next);
2065 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2066 assert_null(parsed->parent);
2067 assert_string_equal(parsed->ref, "ref");
2068 assert_string_equal(parsed->type.name, "type");
2069 assert_string_equal(parsed->units, "uni");
2070 assert_string_equal(parsed->when->cond, "when-cond");
2071 assert_true(parsed->flags & LYS_CONFIG_W);
2072 assert_true(parsed->flags & LYS_ORDBY_USER);
2073 assert_true(parsed->flags & LYS_STATUS_CURR);
2074 lysp_node_free(st->ctx, siblings);
2075 siblings = NULL;
2076
2077 data = ELEMENT_WRAPPER_START
2078 "<leaf-list name=\"llist\">"
2079 "<config value=\"true\" />"
2080 "<description><text>desc</text></description>"
2081 "<if-feature name=\"feature\"/>"
2082 "<max-elements value=\"15\"/>"
2083 "<min-elements value=\"5\"/>"
2084 "<must condition=\"must-cond\"/>"
2085 "<ordered-by value=\"user\" />"
2086 "<reference><text>ref</text></reference>"
2087 "<status value=\"current\"/>"
2088 "<type name=\"type\"/>"
2089 "<units name=\"uni\"/>"
2090 "<when condition=\"when-cond\"/>"
2091 "</leaf-list>"
2092 ELEMENT_WRAPPER_END;
2093 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2094 parsed = (struct lysp_node_leaflist *)siblings;
2095 assert_string_equal(parsed->dsc, "desc");
2096 assert_string_equal(*parsed->iffeatures, "feature");
2097 assert_int_equal(parsed->min, 5);
2098 assert_int_equal(parsed->max, 15);
2099 assert_string_equal(parsed->musts->arg, "must-cond");
2100 assert_string_equal(parsed->name, "llist");
2101 assert_null(parsed->next);
2102 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2103 assert_null(parsed->parent);
2104 assert_string_equal(parsed->ref, "ref");
2105 assert_string_equal(parsed->type.name, "type");
2106 assert_string_equal(parsed->units, "uni");
2107 assert_string_equal(parsed->when->cond, "when-cond");
2108 assert_true(parsed->flags & LYS_CONFIG_W);
2109 assert_true(parsed->flags & LYS_ORDBY_USER);
2110 assert_true(parsed->flags & LYS_STATUS_CURR);
2111 lysp_node_free(st->ctx, siblings);
2112 siblings = NULL;
2113
2114 data = ELEMENT_WRAPPER_START
2115 "<leaf-list name=\"llist\">"
2116 "<type name=\"type\"/>"
2117 "</leaf-list>"
2118 ELEMENT_WRAPPER_END;
2119 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2120 parsed = (struct lysp_node_leaflist *)siblings;
2121 assert_string_equal(parsed->name, "llist");
2122 assert_string_equal(parsed->type.name, "type");
2123 lysp_node_free(st->ctx, siblings);
2124 siblings = NULL;
2125
2126 /* invalid combinations */
2127 data = ELEMENT_WRAPPER_START
2128 "<leaf-list name=\"llist\">"
2129 "<max-elements value=\"5\"/>"
2130 "<min-elements value=\"15\"/>"
2131 "<type name=\"type\"/>"
2132 "</leaf-list>"
2133 ELEMENT_WRAPPER_END;
2134 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
2135 logbuf_assert("Invalid combination of min-elements and max-elements: min value 15 is bigger than the max value 5. Line number 1.");
2136 lysp_node_free(st->ctx, siblings);
2137 siblings = NULL;
2138
2139 data = ELEMENT_WRAPPER_START
2140 "<leaf-list name=\"llist\">"
2141 "<default value=\"def-val1\"/>"
2142 "<min-elements value=\"15\"/>"
2143 "<type name=\"type\"/>"
2144 "</leaf-list>"
2145 ELEMENT_WRAPPER_END;
2146 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
2147 logbuf_assert("Invalid combination of keywords \"min-elements\" and \"default\" as substatements of \"leaf-list\". Line number 1.");
2148 lysp_node_free(st->ctx, siblings);
2149 siblings = NULL;
2150
2151 data = ELEMENT_WRAPPER_START
2152 "<leaf-list name=\"llist\">"
2153 "</leaf-list>"
2154 ELEMENT_WRAPPER_END;
2155 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
2156 logbuf_assert("Missing mandatory subelement type of leaf-list element. Line number 1.");
2157 lysp_node_free(st->ctx, siblings);
2158 siblings = NULL;
2159
2160 st->finished_correctly = true;
2161}
2162
David Sedlákcb39f642019-07-19 13:19:55 +02002163static void
2164test_presence_elem(void **state)
2165{
2166 struct state *st = *state;
2167 const char *data;
2168 const char *val;
2169
2170 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\"/>" ELEMENT_WRAPPER_END;
2171 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2172 assert_string_equal(val, "presence-val");
2173 FREE_STRING(st->ctx, val);
2174
2175 data = ELEMENT_WRAPPER_START "<presence/>" ELEMENT_WRAPPER_END;
2176 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
2177 logbuf_assert("Missing mandatory attribute value of presence element. Line number 1.");
2178
2179 st->finished_correctly = true;
2180}
2181
David Sedlák12470a82019-07-19 13:44:36 +02002182static void
2183test_key_elem(void **state)
2184{
2185 struct state *st = *state;
2186 const char *data;
2187 const char *val;
2188
2189 data = ELEMENT_WRAPPER_START "<key value=\"key-value\"/>" ELEMENT_WRAPPER_END;
2190 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2191 assert_string_equal(val, "key-value");
2192 FREE_STRING(st->ctx, val);
2193
2194 data = ELEMENT_WRAPPER_START "<key/>" ELEMENT_WRAPPER_END;
2195 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
2196 logbuf_assert("Missing mandatory attribute value of key element. Line number 1.");
2197
2198 st->finished_correctly = true;
2199}
2200
David Sedlák04e17b22019-07-19 15:29:48 +02002201static void
2202test_typedef_elem(void **state)
2203{
2204 struct state *st = *state;
2205 const char *data;
2206 struct lysp_tpdf *tpdfs = NULL;
2207 struct typedef_meta typdef_meta = {NULL, &tpdfs};
2208
2209 data = ELEMENT_WRAPPER_START
2210 "<typedef name=\"tpdf-name\">"
2211 "<default value=\"def-val\"/>"
2212 "<description><text>desc-text</text></description>"
2213 "<reference><text>ref-text</text></reference>"
2214 "<status value=\"current\"/>"
2215 "<type name=\"type\"/>"
2216 "<units name=\"uni\"/>"
2217 "</typedef>"
2218 ELEMENT_WRAPPER_END;
2219 assert_int_equal(test_element_helper(st, &data, &typdef_meta, NULL, NULL, true), LY_SUCCESS);
2220 assert_string_equal(tpdfs[0].dflt, "def-val");
2221 assert_string_equal(tpdfs[0].dsc, "desc-text");
2222 assert_null(tpdfs[0].exts);
2223 assert_string_equal(tpdfs[0].name, "tpdf-name");
2224 assert_string_equal(tpdfs[0].ref, "ref-text");
2225 assert_string_equal(tpdfs[0].type.name, "type");
2226 assert_string_equal(tpdfs[0].units, "uni");
2227 assert_true(tpdfs[0].flags & LYS_STATUS_CURR);
2228 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2229 tpdfs = NULL;
2230
2231 data = ELEMENT_WRAPPER_START
2232 "<typedef name=\"tpdf-name\">"
2233 "<type name=\"type\"/>"
2234 "</typedef>"
2235 ELEMENT_WRAPPER_END;
2236 assert_int_equal(test_element_helper(st, &data, &typdef_meta, NULL, NULL, true), LY_SUCCESS);
2237 assert_string_equal(tpdfs[0].name, "tpdf-name");
2238 assert_string_equal(tpdfs[0].type.name, "type");
2239 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2240 tpdfs = NULL;
2241
2242 st->finished_correctly = true;
2243}
2244
David Sedlákd2d676a2019-07-22 11:28:19 +02002245static void
2246test_refine_elem(void **state)
2247{
2248 struct state *st = *state;
2249 const char *data;
2250 struct lysp_refine *refines = NULL;
2251
2252 /* max subelems */
2253 data = ELEMENT_WRAPPER_START
2254 "<refine target-node=\"target\">"
2255 "<if-feature name=\"feature\" />"
2256 "<must condition=\"cond\" />"
2257 "<presence value=\"presence\" />"
2258 "<default value=\"def\" />"
2259 "<config value=\"true\" />"
2260 "<mandatory value=\"true\" />"
2261 "<min-elements value=\"10\" />"
2262 "<max-elements value=\"20\" />"
2263 "<description><text>desc</text></description>"
2264 "<reference><text>ref</text></reference>"
2265 "</refine>"
2266 ELEMENT_WRAPPER_END;
2267 assert_int_equal(test_element_helper(st, &data, &refines, NULL, NULL, true), LY_SUCCESS);
2268 assert_string_equal(refines->nodeid, "target");
2269 assert_string_equal(*refines->dflts, "def");
2270 assert_string_equal(refines->dsc, "desc");
2271 assert_null(refines->exts);
2272 assert_true(refines->flags & LYS_CONFIG_W);
2273 assert_true(refines->flags & LYS_MAND_TRUE);
2274 assert_string_equal(*refines->iffeatures, "feature");
2275 assert_int_equal(refines->max, 20);
2276 assert_int_equal(refines->min, 10);
2277 assert_string_equal(refines->musts->arg, "cond");
2278 assert_string_equal(refines->presence, "presence");
2279 assert_string_equal(refines->ref, "ref");
2280 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2281 refines = NULL;
2282
2283 /* min subelems */
2284 data = ELEMENT_WRAPPER_START "<refine target-node=\"target\" />" ELEMENT_WRAPPER_END;
2285 assert_int_equal(test_element_helper(st, &data, &refines, NULL, NULL, true), LY_SUCCESS);
2286 assert_string_equal(refines->nodeid, "target");
2287 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2288 refines = NULL;
2289
2290 st->finished_correctly = true;
2291}
2292
David Sedlák0d6de5a2019-07-22 13:25:44 +02002293static void
2294test_uses_elem(void **state)
2295{
2296 struct state *st = *state;
2297 const char *data;
2298 struct lysp_node *siblings = NULL;
2299 struct tree_node_meta node_meta = {NULL, &siblings};
2300 struct lysp_node_uses *parsed = NULL;
2301
2302 /* max subelems */
2303 data = ELEMENT_WRAPPER_START
2304 "<uses name=\"uses-name\">"
2305 "<when condition=\"cond\" />"
2306 "<if-feature name=\"feature\" />"
2307 "<status value=\"obsolete\" />"
2308 "<description><text>desc</text></description>"
2309 "<reference><text>ref</text></reference>"
2310 "<refine target-node=\"target\"/>"
2311 /* TODO add uses-augment-stmt instance */
2312 "</uses>"
2313 ELEMENT_WRAPPER_END;
2314 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2315 parsed = (struct lysp_node_uses *)&siblings[0];
2316 assert_string_equal(parsed->name, "uses-name");
2317 assert_string_equal(parsed->dsc, "desc");
2318 assert_null(parsed->exts);
2319 assert_true(parsed->flags & LYS_STATUS_OBSLT);
2320 assert_string_equal(*parsed->iffeatures, "feature");
2321 assert_null(parsed->next);
2322 assert_int_equal(parsed->nodetype, LYS_USES);
2323 assert_null(parsed->parent);
2324 assert_string_equal(parsed->ref, "ref");
2325 assert_string_equal(parsed->refines->nodeid, "target");
2326 assert_string_equal(parsed->when->cond, "cond");
2327 lysp_node_free(st->ctx, siblings);
2328 siblings = NULL;
2329
2330 /* min subelems */
2331 data = ELEMENT_WRAPPER_START "<uses name=\"uses-name\"/>" ELEMENT_WRAPPER_END;
2332 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2333 assert_string_equal(siblings[0].name, "uses-name");
2334 lysp_node_free(st->ctx, siblings);
2335 siblings = NULL;
2336
2337 st->finished_correctly = true;
2338}
2339
David Sedlákaa854b02019-07-22 14:17:10 +02002340static void
2341test_revision_elem(void **state)
2342{
2343 struct state *st = *state;
2344 const char *data;
2345 struct lysp_revision *revs = NULL;
2346
2347 /* max subelems */
2348 data = ELEMENT_WRAPPER_START
2349 "<revision date=\"2018-12-25\">"
2350 "<description><text>desc</text></description>"
2351 "<reference><text>ref</text></reference>"
2352 "</revision>"
2353 ELEMENT_WRAPPER_END;
2354 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, true), LY_SUCCESS);
2355 assert_string_equal(revs->date, "2018-12-25");
2356 assert_string_equal(revs->dsc, "desc");
2357 assert_string_equal(revs->ref, "ref");
2358 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2359 revs = NULL;
2360
2361 /* min subelems */
2362 data = ELEMENT_WRAPPER_START "<revision date=\"2005-05-05\" />" ELEMENT_WRAPPER_END;
2363 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, true), LY_SUCCESS);
2364 assert_string_equal(revs->date, "2005-05-05");
2365 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2366 revs = NULL;
2367
2368 /* invalid value */
2369 data = ELEMENT_WRAPPER_START "<revision date=\"05-05-2005\" />" ELEMENT_WRAPPER_END;
2370 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, false), LY_EVALID);
2371 logbuf_assert("Invalid value \"05-05-2005\" of \"revision\". Line number 1.");
2372 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2373 revs = NULL;
2374
2375 st->finished_correctly = true;
2376}
2377
David Sedlák0c2bab92019-07-22 15:33:19 +02002378static void
2379test_include_elem(void **state)
2380{
2381 struct state *st = *state;
2382 const char *data;
2383 struct lysp_include *includes = NULL;
2384 struct include_meta inc_meta = {"module-name", &includes};
2385
2386 /* max subelems */
2387 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2388 data = ELEMENT_WRAPPER_START
2389 "<include module=\"mod\">"
2390 "<description><text>desc</text></description>"
2391 "<reference><text>ref</text></reference>"
2392 "<revision-date date=\"1999-09-09\"/>"
2393 "</include>"
2394 ELEMENT_WRAPPER_END;
2395 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, true), LY_SUCCESS);
2396 assert_string_equal(includes->name, "mod");
2397 assert_string_equal(includes->dsc, "desc");
2398 assert_string_equal(includes->ref, "ref");
2399 assert_null(includes->exts);
2400 assert_string_equal(includes->rev, "1999-09-09");
2401 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2402 includes = NULL;
2403
2404 /* min subelems */
2405 data = ELEMENT_WRAPPER_START "<include module=\"mod\"/>" ELEMENT_WRAPPER_END;
2406 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, true), LY_SUCCESS);
2407 assert_string_equal(includes->name, "mod");
2408 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2409 includes = NULL;
2410
2411 /* invalid combinations */
2412 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2413 data = ELEMENT_WRAPPER_START
2414 "<include module=\"mod\">"
2415 "<description><text>desc</text></description>"
2416 "<revision-date date=\"1999-09-09\"/>"
2417 "</include>"
2418 ELEMENT_WRAPPER_END;
2419 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, false), LY_EVALID);
2420 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.");
2421 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2422 includes = NULL;
2423
2424 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2425 data = ELEMENT_WRAPPER_START
2426 "<include module=\"mod\">"
2427 "<reference><text>ref</text></reference>"
2428 "<revision-date date=\"1999-09-09\"/>"
2429 "</include>"
2430 ELEMENT_WRAPPER_END;
2431 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, false), LY_EVALID);
2432 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.");
2433 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2434 includes = NULL;
2435
2436 st->finished_correctly = true;
2437}
2438
David Sedlák5e13dea2019-07-22 16:06:45 +02002439static void
2440test_feature_elem(void **state)
2441{
2442 struct state *st = *state;
2443 const char *data;
2444 struct lysp_feature *features = NULL;
2445
2446 /* max subelems */
2447 data = ELEMENT_WRAPPER_START
2448 "<feature name=\"feature-name\">"
2449 "<if-feature name=\"iff\"/>"
2450 "<status value=\"deprecated\"/>"
2451 "<description><text>desc</text></description>"
2452 "<reference><text>ref</text></reference>"
2453 "</feature>"
2454 ELEMENT_WRAPPER_END;
2455 assert_int_equal(test_element_helper(st, &data, &features, NULL, NULL, true), LY_SUCCESS);
2456 assert_string_equal(features->name, "feature-name");
2457 assert_string_equal(features->dsc, "desc");
2458 assert_null(features->exts);
2459 assert_true(features->flags & LYS_STATUS_DEPRC);
2460 assert_string_equal(*features->iffeatures, "iff");
2461 assert_string_equal(features->ref, "ref");
2462 FREE_ARRAY(st->ctx, features, lysp_feature_free);
2463 features = NULL;
2464
2465 /* min subelems */
2466 data = ELEMENT_WRAPPER_START "<feature name=\"feature-name\"/>" ELEMENT_WRAPPER_END;
2467 assert_int_equal(test_element_helper(st, &data, &features, NULL, NULL, true), LY_SUCCESS);
2468 assert_string_equal(features->name, "feature-name");
2469 FREE_ARRAY(st->ctx, features, lysp_feature_free);
2470 features = NULL;
2471
2472 st->finished_correctly = true;
2473}
2474
David Sedlák28794f22019-07-22 16:45:00 +02002475static void
2476test_identity_elem(void **state)
2477{
2478 struct state *st = *state;
2479 const char *data;
2480 struct lysp_ident *identities = NULL;
2481
2482 /* max subelems */
2483 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2484 data = ELEMENT_WRAPPER_START
2485 "<identity name=\"ident-name\">"
2486 "<if-feature name=\"iff\"/>"
2487 "<base name=\"base-name\"/>"
2488 "<status value=\"deprecated\"/>"
2489 "<description><text>desc</text></description>"
2490 "<reference><text>ref</text></reference>"
2491 "</identity>"
2492 ELEMENT_WRAPPER_END;
2493 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, true), LY_SUCCESS);
2494 assert_string_equal(identities->name, "ident-name");
2495 assert_string_equal(*identities->bases, "base-name");
2496 assert_string_equal(*identities->iffeatures, "iff");
2497 assert_string_equal(identities->dsc, "desc");
2498 assert_string_equal(identities->ref, "ref");
2499 assert_true(identities->flags & LYS_STATUS_DEPRC);
2500 assert_null(identities->exts);
2501 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2502 identities = NULL;
2503
2504 /* min subelems */
2505 data = ELEMENT_WRAPPER_START "<identity name=\"ident-name\" />" ELEMENT_WRAPPER_END;
2506 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, true), LY_SUCCESS);
2507 assert_string_equal(identities->name, "ident-name");
2508 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2509 identities = NULL;
2510
2511 /* invalid */
2512 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2513 data = ELEMENT_WRAPPER_START
2514 "<identity name=\"ident-name\">"
2515 "<if-feature name=\"iff\"/>"
2516 "</identity>"
2517 ELEMENT_WRAPPER_END;
2518 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, false), LY_EVALID);
2519 logbuf_assert("Invalid sub-elemnt \"if-feature\" of \"identity\" element - this sub-element is allowed only in modules with version 1.1 or newer. Line number 1.");
2520 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2521 identities = NULL;
2522
2523 st->finished_correctly = true;
2524}
2525
David Sedlákaf536aa2019-07-23 13:42:23 +02002526static void
2527test_list_elem(void **state)
2528{
2529 struct state *st = *state;
2530 const char *data;
2531 struct lysp_node *siblings = NULL;
2532 struct tree_node_meta node_meta = {NULL, &siblings};
2533 struct lysp_node_list *parsed = NULL;
2534
2535 /* max subelems */
2536 data = ELEMENT_WRAPPER_START
2537 "<list name=\"list-name\">"
2538 "<when condition=\"when\"/>"
2539 "<if-feature name=\"iff\"/>"
2540 "<must condition=\"must-cond\"/>"
2541 "<key value=\"key\"/>"
2542 "<unique tag=\"utag\"/>"
2543 "<config value=\"true\"/>"
2544 "<min-elements value=\"10\"/>"
2545 "<ordered-by value=\"user\"/>"
2546 "<status value=\"deprecated\"/>"
2547 "<description><text>desc</text></description>"
2548 "<reference><text>ref</text></reference>"
2549 "<anydata name=\"anyd\"/>"
2550 "<anyxml name=\"anyx\"/>"
2551 // "<choice name=\"choice\"/>"
2552 // "<action name=\"action\"/>"
2553 // "<container name=\"cont\"/>"
2554 // "<grouping name=\"grp\"/>"
David Sedlák031b9e72019-07-23 15:19:37 +02002555 "<notification name=\"notf\"/>"
David Sedlákaf536aa2019-07-23 13:42:23 +02002556 "<leaf name=\"leaf\"/>"
2557 "<leaf-list name=\"llist\"/>"
2558 "<list name=\"sub-list\"/>"
2559 "<typedef name=\"tpdf\"/>"
2560 "<uses name=\"uses-name\"/>"
2561 "</list>"
2562 ELEMENT_WRAPPER_END;
2563 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2564 parsed = (struct lysp_node_list *)&siblings[0];
2565 assert_string_equal(parsed->dsc, "desc");
2566 assert_string_equal(parsed->child->name, "anyd");
2567 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2568 assert_string_equal(parsed->child->next->name, "anyx");
2569 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2570 // assert_string_equal(parsed->child->next->next->name, "choice");
2571 // assert_int_equal(parsed->child->next->next, LYS_CHOICE);
2572 // assert_string_equal(parsed->actions->name, "action");
2573 // assert_string_equal(parsed->child->next->next->name, "cont");
2574 // assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
2575 // assert_string_equal(parsed->groupings->name, "grp");
David Sedlák031b9e72019-07-23 15:19:37 +02002576 assert_string_equal(parsed->notifs->name, "notf");
David Sedlákaf536aa2019-07-23 13:42:23 +02002577 assert_null(parsed->exts);
2578 assert_true(parsed->flags & LYS_ORDBY_USER);
2579 assert_true(parsed->flags & LYS_STATUS_DEPRC);
2580 assert_true(parsed->flags & LYS_CONFIG_W);
2581 assert_string_equal(*parsed->iffeatures, "iff");
2582 assert_string_equal(parsed->key, "key");
2583 assert_int_equal(parsed->min, 10);
2584 assert_string_equal(parsed->musts->arg, "must-cond");
2585 assert_string_equal(parsed->name, "list-name");
2586 assert_null(parsed->next);
2587 assert_int_equal(parsed->nodetype, LYS_LIST);
2588 assert_null(parsed->parent);
2589 assert_string_equal(parsed->ref, "ref");
2590 assert_string_equal(parsed->typedefs->name, "tpdf");
2591 assert_string_equal(*parsed->uniques, "utag");
2592 assert_string_equal(parsed->when->cond, "when");
2593 lysp_node_free(st->ctx, siblings);
2594 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
2595 siblings = NULL;
2596
2597 /* min subelems */
2598 data = ELEMENT_WRAPPER_START "<list name=\"list-name\" />" ELEMENT_WRAPPER_END;
2599 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2600 parsed = (struct lysp_node_list *)&siblings[0];
2601 assert_string_equal(parsed->name, "list-name");
2602 lysp_node_free(st->ctx, siblings);
2603 siblings = NULL;
2604
2605 st->finished_correctly = true;
2606}
2607
David Sedlák031b9e72019-07-23 15:19:37 +02002608static void
2609test_notification_elem(void **state)
2610{
2611 struct state *st = *state;
2612 const char *data;
2613 struct lysp_notif *notifs = NULL;
2614 struct notif_meta notif_meta = {NULL, &notifs};
2615
2616 /* max subelems */
2617 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2618 data = ELEMENT_WRAPPER_START
2619 "<notification name=\"notif-name\">"
2620 "<anydata name=\"anyd\"/>"
2621 "<anyxml name=\"anyx\"/>"
2622 "<description><text>desc</text></description>"
2623 "<if-feature name=\"iff\"/>"
2624 "<leaf name=\"leaf\"/>"
2625 "<leaf-list name=\"llist\"/>"
2626 "<list name=\"sub-list\"/>"
2627 "<must condition=\"cond\"/>"
2628 "<reference><text>ref</text></reference>"
2629 "<status value=\"deprecated\"/>"
2630 "<typedef name=\"tpdf\"/>"
2631 "<uses name=\"uses-name\"/>"
2632 // "<choice name=\"choice\"/>"
2633 // "<container name=\"cont\"/>"
2634 // "<grouping name=\"grp\"/>"
2635 "</notification>"
2636 ELEMENT_WRAPPER_END;
2637 assert_int_equal(test_element_helper(st, &data, &notif_meta, NULL, NULL, true), LY_SUCCESS);
2638 assert_string_equal(notifs->name, "notif-name");
2639 assert_string_equal(notifs->data->name, "anyd");
2640 assert_int_equal(notifs->data->nodetype, LYS_ANYDATA);
2641 assert_string_equal(notifs->data->next->name, "anyx");
2642 assert_int_equal(notifs->data->next->nodetype, LYS_ANYXML);
2643 assert_string_equal(notifs->data->next->next->name, "leaf");
2644 assert_int_equal(notifs->data->next->next->nodetype, LYS_LEAF);
2645 assert_string_equal(notifs->data->next->next->next->name, "llist");
2646 assert_int_equal(notifs->data->next->next->next->nodetype, LYS_LEAFLIST);
2647 assert_string_equal(notifs->data->next->next->next->next->name, "sub-list");
2648 assert_int_equal(notifs->data->next->next->next->next->nodetype, LYS_LIST);
2649 assert_null(notifs->exts);
2650 assert_true(notifs->flags & LYS_STATUS_DEPRC);
2651 // assert_string_equal(notifs->groupings->name, "grp");
2652 // assert_int_equal(notifs->data->next->next->next->next->nodetype, LYS_CHOICE);
2653 // assert_string_equal(notifs->data->next->next->next->next->next->name, "choice");
2654 // assert_int_equal(notifs->data->next->next->next->next->next->nodetype, LYS_CONTAINER);
2655 // assert_string_equal(notifs->data->next->next->next->next->next->next->name, "cont");
2656 // assert_null(notifs->data->next->next->next->next->next->next->next);
2657 assert_string_equal(*notifs->iffeatures, "iff");
2658 assert_string_equal(notifs->musts->arg, "cond");
2659 assert_int_equal(notifs->nodetype, LYS_NOTIF);
2660 assert_null(notifs->parent);
2661 assert_string_equal(notifs->ref, "ref");
2662 assert_string_equal(notifs->typedefs->name, "tpdf");
2663 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
2664 notifs = NULL;
2665
2666 /* min subelems */
2667 data = ELEMENT_WRAPPER_START "<notification name=\"notif-name\" />" ELEMENT_WRAPPER_END;
2668 assert_int_equal(test_element_helper(st, &data, &notif_meta, NULL, NULL, true), LY_SUCCESS);
2669 assert_string_equal(notifs->name, "notif-name");
2670 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
2671
2672 st->finished_correctly = true;
2673}
2674
David Sedlák3b4db242018-10-19 16:11:01 +02002675int
2676main(void)
2677{
2678
2679 const struct CMUnitTest tests[] = {
David Sedlák392af4f2019-06-04 16:02:42 +02002680 cmocka_unit_test_setup_teardown(test_yin_parse_module, setup_f, teardown_f),
David Sedlák8f7a1172019-06-20 14:42:18 +02002681 cmocka_unit_test_setup_teardown(test_yin_match_keyword, setup_f, teardown_f),
David Sedlákb1a78352019-06-28 16:16:29 +02002682 cmocka_unit_test_setup_teardown(test_yin_parse_element_generic, setup_f, teardown_f),
2683 cmocka_unit_test_setup_teardown(test_yin_parse_extension_instance, setup_f, teardown_f),
David Sedlák555c7202019-07-04 12:14:12 +02002684 cmocka_unit_test_setup_teardown(test_yin_parse_content, setup_f, teardown_f),
David Sedlák4a650532019-07-10 11:55:18 +02002685 cmocka_unit_test_setup_teardown(test_validate_value, setup_f, teardown_f),
David Sedlák32488102019-07-15 17:44:10 +02002686
David Sedlák8e7bda82019-07-16 17:57:50 +02002687 cmocka_unit_test(test_yin_match_argument_name),
David Sedlák32488102019-07-15 17:44:10 +02002688 cmocka_unit_test_setup_teardown(test_enum_bit_elem, setup_element_test, teardown_element_test),
2689 cmocka_unit_test_setup_teardown(test_meta_elem, setup_element_test, teardown_element_test),
2690 cmocka_unit_test_setup_teardown(test_import_elem, setup_element_test, teardown_element_test),
2691 cmocka_unit_test_setup_teardown(test_status_elem, setup_element_test, teardown_element_test),
2692 cmocka_unit_test_setup_teardown(test_ext_elem, setup_element_test, teardown_element_test),
2693 cmocka_unit_test_setup_teardown(test_yin_element_elem, setup_element_test, teardown_element_test),
2694 cmocka_unit_test_setup_teardown(test_yangversion_elem, setup_element_test, teardown_element_test),
2695 cmocka_unit_test_setup_teardown(test_mandatory_elem, setup_element_test, teardown_element_test),
David Sedlák8e7bda82019-07-16 17:57:50 +02002696 cmocka_unit_test_setup_teardown(test_argument_elem, setup_element_test, teardown_element_test),
2697 cmocka_unit_test_setup_teardown(test_base_elem, setup_element_test, teardown_element_test),
2698 cmocka_unit_test_setup_teardown(test_belongsto_elem, setup_element_test, teardown_element_test),
2699 cmocka_unit_test_setup_teardown(test_config_elem, setup_element_test, teardown_element_test),
2700 cmocka_unit_test_setup_teardown(test_default_elem, setup_element_test, teardown_element_test),
2701 cmocka_unit_test_setup_teardown(test_err_app_tag_elem, setup_element_test, teardown_element_test),
2702 cmocka_unit_test_setup_teardown(test_err_msg_elem, setup_element_test, teardown_element_test),
2703 cmocka_unit_test_setup_teardown(test_fracdigits_elem, setup_element_test, teardown_element_test),
2704 cmocka_unit_test_setup_teardown(test_iffeature_elem, setup_element_test, teardown_element_test),
2705 cmocka_unit_test_setup_teardown(test_length_elem, setup_element_test, teardown_element_test),
2706 cmocka_unit_test_setup_teardown(test_modifier_elem, setup_element_test, teardown_element_test),
2707 cmocka_unit_test_setup_teardown(test_namespace_elem, setup_element_test, teardown_element_test),
2708 cmocka_unit_test_setup_teardown(test_path_elem, setup_element_test, teardown_element_test),
2709 cmocka_unit_test_setup_teardown(test_pattern_elem, setup_element_test, teardown_element_test),
2710 cmocka_unit_test_setup_teardown(test_value_position_elem, setup_element_test, teardown_element_test),
David Sedlák69f01612019-07-17 11:41:08 +02002711 cmocka_unit_test_setup_teardown(test_prefix_elem, setup_element_test, teardown_element_test),
2712 cmocka_unit_test_setup_teardown(test_range_elem, setup_element_test, teardown_element_test),
2713 cmocka_unit_test_setup_teardown(test_reqinstance_elem, setup_element_test, teardown_element_test),
2714 cmocka_unit_test_setup_teardown(test_revision_date_elem, setup_element_test, teardown_element_test),
2715 cmocka_unit_test_setup_teardown(test_unique_elem, setup_element_test, teardown_element_test),
2716 cmocka_unit_test_setup_teardown(test_units_elem, setup_element_test, teardown_element_test),
2717 cmocka_unit_test_setup_teardown(test_when_elem, setup_element_test, teardown_element_test),
2718 cmocka_unit_test_setup_teardown(test_yin_text_value_elem, setup_element_test, teardown_element_test),
David Sedlák374d2b32019-07-17 15:06:55 +02002719 cmocka_unit_test_setup_teardown(test_type_elem, setup_element_test, teardown_element_test),
David Sedlák1af868e2019-07-17 17:03:14 +02002720 cmocka_unit_test_setup_teardown(test_max_elems_elem, setup_element_test, teardown_element_test),
David Sedlák09e18c92019-07-18 11:17:11 +02002721 cmocka_unit_test_setup_teardown(test_min_elems_elem, setup_element_test, teardown_element_test),
David Sedláka2dad212019-07-18 12:45:19 +02002722 cmocka_unit_test_setup_teardown(test_ordby_elem, setup_element_test, teardown_element_test),
David Sedlák8a83bbb2019-07-18 14:46:00 +02002723 cmocka_unit_test_setup_teardown(test_any_elem, setup_element_test, teardown_element_test),
David Sedlák203ca3a2019-07-18 15:26:25 +02002724 cmocka_unit_test_setup_teardown(test_leaf_elem, setup_element_test, teardown_element_test),
David Sedlákc3da3ef2019-07-19 12:56:08 +02002725 cmocka_unit_test_setup_teardown(test_leaf_list_elem, setup_element_test, teardown_element_test),
David Sedlákcb39f642019-07-19 13:19:55 +02002726 cmocka_unit_test_setup_teardown(test_presence_elem, setup_element_test, teardown_element_test),
David Sedlák12470a82019-07-19 13:44:36 +02002727 cmocka_unit_test_setup_teardown(test_key_elem, setup_element_test, teardown_element_test),
David Sedlák04e17b22019-07-19 15:29:48 +02002728 cmocka_unit_test_setup_teardown(test_typedef_elem, setup_element_test, teardown_element_test),
David Sedlákd2d676a2019-07-22 11:28:19 +02002729 cmocka_unit_test_setup_teardown(test_refine_elem, setup_element_test, teardown_element_test),
David Sedlák0d6de5a2019-07-22 13:25:44 +02002730 cmocka_unit_test_setup_teardown(test_uses_elem, setup_element_test, teardown_element_test),
David Sedlákaa854b02019-07-22 14:17:10 +02002731 cmocka_unit_test_setup_teardown(test_revision_elem, setup_element_test, teardown_element_test),
David Sedlák0c2bab92019-07-22 15:33:19 +02002732 cmocka_unit_test_setup_teardown(test_include_elem, setup_element_test, teardown_element_test),
David Sedlák5e13dea2019-07-22 16:06:45 +02002733 cmocka_unit_test_setup_teardown(test_feature_elem, setup_element_test, teardown_element_test),
David Sedlák28794f22019-07-22 16:45:00 +02002734 cmocka_unit_test_setup_teardown(test_identity_elem, setup_element_test, teardown_element_test),
David Sedlákaf536aa2019-07-23 13:42:23 +02002735 cmocka_unit_test_setup_teardown(test_list_elem, setup_element_test, teardown_element_test),
David Sedlák031b9e72019-07-23 15:19:37 +02002736 cmocka_unit_test_setup_teardown(test_notification_elem, setup_element_test, teardown_element_test),
2737
David Sedlák3b4db242018-10-19 16:11:01 +02002738 };
2739
David Sedlák8e7bda82019-07-16 17:57:50 +02002740 return cmocka_run_group_tests(tests, setup_ly_ctx, destroy_ly_ctx);
David Sedlák3b4db242018-10-19 16:11:01 +02002741}