blob: 461f3fac5427cbb1eb72057eeb3fc866f5f50106 [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ák555c7202019-07-04 12:14:12 +020042
David Sedlák68a1af12019-03-08 13:46:54 +010043struct state {
David Sedlák3b4db242018-10-19 16:11:01 +020044 struct ly_ctx *ctx;
David Sedlák3017da42019-02-15 09:48:04 +010045 struct lys_module *mod;
David Sedlák619db942019-07-03 14:47:30 +020046 struct lysp_module *lysp_mod;
David Sedlákda8ffa32019-07-08 14:17:10 +020047 struct yin_parser_ctx *yin_ctx;
David Sedlák79e50cb2019-06-05 16:33:09 +020048 bool finished_correctly;
David Sedlák68a1af12019-03-08 13:46:54 +010049};
David Sedlák872c7b42018-10-26 13:15:20 +020050
David Sedlák79e50cb2019-06-05 16:33:09 +020051#define BUFSIZE 1024
52char logbuf[BUFSIZE] = {0};
53int store = -1; /* negative for infinite logging, positive for limited logging */
54
55/* set to 0 to printing error messages to stderr instead of checking them in code */
David Sedlákaf536aa2019-07-23 13:42:23 +020056#define ENABLE_LOGGER_CHECKING 0
David Sedlák79e50cb2019-06-05 16:33:09 +020057
58#if ENABLE_LOGGER_CHECKING
59static void
60logger(LY_LOG_LEVEL level, const char *msg, const char *path)
61{
62 (void) level; /* unused */
63 if (store) {
64 if (path && path[0]) {
65 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
66 } else {
67 strncpy(logbuf, msg, BUFSIZE - 1);
68 }
69 if (store > 0) {
70 --store;
71 }
72 }
73}
74#endif
75
76#if ENABLE_LOGGER_CHECKING
77# define logbuf_assert(str) assert_string_equal(logbuf, str)
78#else
79# define logbuf_assert(str)
80#endif
81
82#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
83 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
84 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
85 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
86 CLEANUP
87
David Sedlák8e7bda82019-07-16 17:57:50 +020088int
89setup_ly_ctx(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +010090{
91 struct state *st = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +020092
David Sedlák68a1af12019-03-08 13:46:54 +010093 /* allocate state variable */
94 (*state) = st = calloc(1, sizeof(*st));
95 if (!st) {
96 fprintf(stderr, "Memmory allocation failed");
97 return EXIT_FAILURE;
98 }
David Sedlák872c7b42018-10-26 13:15:20 +020099
David Sedlák68a1af12019-03-08 13:46:54 +0100100 /* create new libyang context */
101 ly_ctx_new(NULL, 0, &st->ctx);
David Sedlák872c7b42018-10-26 13:15:20 +0200102
David Sedlák8e7bda82019-07-16 17:57:50 +0200103 return EXIT_SUCCESS;
104}
105
106int
107destroy_ly_ctx(void **state)
108{
109 struct state *st = *state;
110 ly_ctx_destroy(st->ctx, NULL);
111 free(st);
112
113 return EXIT_SUCCESS;
114}
115
116static int
117setup_f(void **state)
118{
119 struct state *st = *state;
120
121#if ENABLE_LOGGER_CHECKING
122 /* setup logger */
123 ly_set_log_clb(logger, 1);
124#endif
125
David Sedlák68a1af12019-03-08 13:46:54 +0100126 /* allocate new module */
127 st->mod = calloc(1, sizeof(*st->mod));
128 st->mod->ctx = st->ctx;
129
David Sedlák619db942019-07-03 14:47:30 +0200130 /* allocate new parsed module */
131 st->lysp_mod = calloc(1, sizeof(*st->lysp_mod));
132 st->lysp_mod->mod = calloc(1, sizeof(*st->lysp_mod->mod));
133 st->lysp_mod->mod->ctx = st->ctx;
134
135 /* allocate parser context */
David Sedlákda8ffa32019-07-08 14:17:10 +0200136 st->yin_ctx = calloc(1, sizeof(*st->yin_ctx));
137 st->yin_ctx->xml_ctx.ctx = st->ctx;
138 st->yin_ctx->xml_ctx.line = 1;
David Sedlák8f5bce02019-06-03 16:41:08 +0200139
David Sedlák68a1af12019-03-08 13:46:54 +0100140 return EXIT_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +0200141}
142
143static int
David Sedlák68a1af12019-03-08 13:46:54 +0100144teardown_f(void **state)
145{
146 struct state *st = *(struct state **)state;
David Sedlák619db942019-07-03 14:47:30 +0200147 struct lys_module *temp;
David Sedlák68a1af12019-03-08 13:46:54 +0100148
David Sedlák79e50cb2019-06-05 16:33:09 +0200149#if ENABLE_LOGGER_CHECKING
150 /* teardown logger */
151 if (!st->finished_correctly && logbuf[0] != '\0') {
152 fprintf(stderr, "%s\n", logbuf);
153 }
154#endif
155
David Sedlák619db942019-07-03 14:47:30 +0200156 temp = st->lysp_mod->mod;
157
David Sedlákda8ffa32019-07-08 14:17:10 +0200158 lyxml_context_clear(&st->yin_ctx->xml_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100159 lys_module_free(st->mod, NULL);
David Sedlák619db942019-07-03 14:47:30 +0200160 lysp_module_free(st->lysp_mod);
161 lys_module_free(temp, NULL);
David Sedlákda8ffa32019-07-08 14:17:10 +0200162 free(st->yin_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100163
164 return EXIT_SUCCESS;
165}
166
David Sedlák392af4f2019-06-04 16:02:42 +0200167static struct state*
168reset_state(void **state)
169{
David Sedlák79e50cb2019-06-05 16:33:09 +0200170 ((struct state *)*state)->finished_correctly = true;
David Sedlák555c7202019-07-04 12:14:12 +0200171 logbuf[0] = '\0';
David Sedlák392af4f2019-06-04 16:02:42 +0200172 teardown_f(state);
173 setup_f(state);
174
175 return *state;
176}
177
David Sedlák79e50cb2019-06-05 16:33:09 +0200178void
179logbuf_clean(void)
180{
181 logbuf[0] = '\0';
182}
183
David Sedlák68a1af12019-03-08 13:46:54 +0100184static void
David Sedlák392af4f2019-06-04 16:02:42 +0200185test_yin_parse_module(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +0100186{
187 LY_ERR ret = LY_SUCCESS;
188 struct state *st = *state;
189
190 ret = yin_parse_module(st->ctx,
David Sedlák2b214ac2019-06-06 16:11:03 +0200191 "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\
192 name=\"example-foo\"\
David Sedlák18730132019-03-15 15:51:34 +0100193 xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\
194 xmlns:foo=\"urn:example:foo\"\
195 xmlns:myext=\"urn:example:extensions\">\
David Sedlákcd0c9512019-03-29 13:23:06 +0100196 <namespace uri=\"urn:example:foo\" xmlns:myext=\"urn:example:extensions\"/>\
David Sedláka7406952019-04-05 10:33:07 +0200197 <prefix xmlns:myxt=\"urn:emple:extensions\" value=\"foo\" xmlns:myext=\"urn:example:extensions\"/>\
David Sedlákd9d3a312019-06-04 09:47:10 +0200198 </module>",
David Sedlák68a1af12019-03-08 13:46:54 +0100199 st->mod);
200
201 assert_int_equal(ret, LY_SUCCESS);
202 assert_string_equal(st->mod->parsed->mod->name, "example-foo");
203 assert_string_equal(st->mod->parsed->mod->prefix, "foo");
David Sedlákcd0c9512019-03-29 13:23:06 +0100204 assert_string_equal(st->mod->parsed->mod->ns, "urn:example:foo");
David Sedlák392af4f2019-06-04 16:02:42 +0200205
206 st = reset_state(state);
207 ret = yin_parse_module(st->ctx,
David Sedlák2b214ac2019-06-06 16:11:03 +0200208 "<module name=\"example-foo\">\
209 <invalid-tag uri=\"urn:example:foo\"\"/>\
210 </module>",
211 st->mod);
David Sedlák392af4f2019-06-04 16:02:42 +0200212 assert_int_equal(ret, LY_EVALID);
213
214 st = reset_state(state);
215 ret = yin_parse_module(st->ctx,
David Sedlák8f7a1172019-06-20 14:42:18 +0200216 "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\
David Sedlák57715b12019-06-17 13:05:22 +0200217 </module>",
David Sedlák2b214ac2019-06-06 16:11:03 +0200218 st->mod);
David Sedlák392af4f2019-06-04 16:02:42 +0200219 assert_int_equal(ret, LY_EVALID);
David Sedlák882a8072019-07-08 17:51:20 +0200220 logbuf_assert("Missing mandatory attribute name of module element. Line number 1.");
David Sedlák392af4f2019-06-04 16:02:42 +0200221
222 st = reset_state(state);
223 ret = yin_parse_module(st->ctx,
224 "",
225 st->mod);
226 assert_int_equal(ret, LY_EVALID);
David Sedlák79e50cb2019-06-05 16:33:09 +0200227 logbuf_assert("Invalid keyword \"(null)\", expected \"module\" or \"submodule\". Line number 1.");
228 st->finished_correctly = true;
David Sedlák3b4db242018-10-19 16:11:01 +0200229}
230
231static void
David Sedlák1bccdfa2019-06-17 15:55:27 +0200232test_yin_match_keyword(void **state)
David Sedlák3b4db242018-10-19 16:11:01 +0200233{
David Sedlák8f7a1172019-06-20 14:42:18 +0200234 struct state *st = *state;
David Sedlák3b4db242018-10-19 16:11:01 +0200235
David Sedlák8f7a1172019-06-20 14:42:18 +0200236 const char *prefix, *name;
237 struct yin_arg_record *args = NULL;
238 size_t prefix_len, name_len;
239 /* create mock yin namespace in xml context */
240 const char *data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" />";
David Sedlákda8ffa32019-07-08 14:17:10 +0200241 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
242 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák8f7a1172019-06-20 14:42:18 +0200243 LY_ARRAY_FREE(args);
244
David Sedlákc1771b12019-07-10 15:55:46 +0200245 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydatax", strlen("anydatax"), prefix, prefix_len, YANG_NONE), YANG_NONE);
246 assert_int_equal(yin_match_keyword(st->yin_ctx, "asdasd", strlen("asdasd"), prefix, prefix_len, YANG_NONE), YANG_NONE);
247 assert_int_equal(yin_match_keyword(st->yin_ctx, "", 0, prefix, prefix_len, YANG_NONE), YANG_NONE);
248 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydata", strlen("anydata"), prefix, prefix_len, YANG_NONE), YANG_ANYDATA);
249 assert_int_equal(yin_match_keyword(st->yin_ctx, "anyxml", strlen("anyxml"), prefix, prefix_len, YANG_NONE), YANG_ANYXML);
250 assert_int_equal(yin_match_keyword(st->yin_ctx, "argument", strlen("argument"), prefix, prefix_len, YANG_NONE), YANG_ARGUMENT);
251 assert_int_equal(yin_match_keyword(st->yin_ctx, "augment", strlen("augment"), prefix, prefix_len, YANG_NONE), YANG_AUGMENT);
252 assert_int_equal(yin_match_keyword(st->yin_ctx, "base", strlen("base"), prefix, prefix_len, YANG_NONE), YANG_BASE);
253 assert_int_equal(yin_match_keyword(st->yin_ctx, "belongs-to", strlen("belongs-to"), prefix, prefix_len, YANG_NONE), YANG_BELONGS_TO);
254 assert_int_equal(yin_match_keyword(st->yin_ctx, "bit", strlen("bit"), prefix, prefix_len, YANG_NONE), YANG_BIT);
255 assert_int_equal(yin_match_keyword(st->yin_ctx, "case", strlen("case"), prefix, prefix_len, YANG_NONE), YANG_CASE);
256 assert_int_equal(yin_match_keyword(st->yin_ctx, "choice", strlen("choice"), prefix, prefix_len, YANG_NONE), YANG_CHOICE);
257 assert_int_equal(yin_match_keyword(st->yin_ctx, "config", strlen("config"), prefix, prefix_len, YANG_NONE), YANG_CONFIG);
258 assert_int_equal(yin_match_keyword(st->yin_ctx, "contact", strlen("contact"), prefix, prefix_len, YANG_NONE), YANG_CONTACT);
259 assert_int_equal(yin_match_keyword(st->yin_ctx, "container", strlen("container"), prefix, prefix_len, YANG_NONE), YANG_CONTAINER);
260 assert_int_equal(yin_match_keyword(st->yin_ctx, "default", strlen("default"), prefix, prefix_len, YANG_NONE), YANG_DEFAULT);
261 assert_int_equal(yin_match_keyword(st->yin_ctx, "description", strlen("description"), prefix, prefix_len, YANG_NONE), YANG_DESCRIPTION);
262 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviate", strlen("deviate"), prefix, prefix_len, YANG_NONE), YANG_DEVIATE);
263 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviation", strlen("deviation"), prefix, prefix_len, YANG_NONE), YANG_DEVIATION);
264 assert_int_equal(yin_match_keyword(st->yin_ctx, "enum", strlen("enum"), prefix, prefix_len, YANG_NONE), YANG_ENUM);
265 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);
266 assert_int_equal(yin_match_keyword(st->yin_ctx, "error-message", strlen("error-message"), prefix, prefix_len, YANG_NONE), YANG_ERROR_MESSAGE);
267 assert_int_equal(yin_match_keyword(st->yin_ctx, "extension", strlen("extension"), prefix, prefix_len, YANG_NONE), YANG_EXTENSION);
268 assert_int_equal(yin_match_keyword(st->yin_ctx, "feature", strlen("feature"), prefix, prefix_len, YANG_NONE), YANG_FEATURE);
269 assert_int_equal(yin_match_keyword(st->yin_ctx, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len, YANG_NONE), YANG_FRACTION_DIGITS);
270 assert_int_equal(yin_match_keyword(st->yin_ctx, "grouping", strlen("grouping"), prefix, prefix_len, YANG_NONE), YANG_GROUPING);
271 assert_int_equal(yin_match_keyword(st->yin_ctx, "identity", strlen("identity"), prefix, prefix_len, YANG_NONE), YANG_IDENTITY);
272 assert_int_equal(yin_match_keyword(st->yin_ctx, "if-feature", strlen("if-feature"), prefix, prefix_len, YANG_NONE), YANG_IF_FEATURE);
273 assert_int_equal(yin_match_keyword(st->yin_ctx, "import", strlen("import"), prefix, prefix_len, YANG_NONE), YANG_IMPORT);
274 assert_int_equal(yin_match_keyword(st->yin_ctx, "include", strlen("include"), prefix, prefix_len, YANG_NONE), YANG_INCLUDE);
275 assert_int_equal(yin_match_keyword(st->yin_ctx, "input", strlen("input"), prefix, prefix_len, YANG_NONE), YANG_INPUT);
276 assert_int_equal(yin_match_keyword(st->yin_ctx, "key", strlen("key"), prefix, prefix_len, YANG_NONE), YANG_KEY);
277 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf", strlen("leaf"), prefix, prefix_len, YANG_NONE), YANG_LEAF);
278 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf-list", strlen("leaf-list"), prefix, prefix_len, YANG_NONE), YANG_LEAF_LIST);
279 assert_int_equal(yin_match_keyword(st->yin_ctx, "length", strlen("length"), prefix, prefix_len, YANG_NONE), YANG_LENGTH);
280 assert_int_equal(yin_match_keyword(st->yin_ctx, "list", strlen("list"), prefix, prefix_len, YANG_NONE), YANG_LIST);
281 assert_int_equal(yin_match_keyword(st->yin_ctx, "mandatory", strlen("mandatory"), prefix, prefix_len, YANG_NONE), YANG_MANDATORY);
282 assert_int_equal(yin_match_keyword(st->yin_ctx, "max-elements", strlen("max-elements"), prefix, prefix_len, YANG_NONE), YANG_MAX_ELEMENTS);
283 assert_int_equal(yin_match_keyword(st->yin_ctx, "min-elements", strlen("min-elements"), prefix, prefix_len, YANG_NONE), YANG_MIN_ELEMENTS);
284 assert_int_equal(yin_match_keyword(st->yin_ctx, "modifier", strlen("modifier"), prefix, prefix_len, YANG_NONE), YANG_MODIFIER);
285 assert_int_equal(yin_match_keyword(st->yin_ctx, "module", strlen("module"), prefix, prefix_len, YANG_NONE), YANG_MODULE);
286 assert_int_equal(yin_match_keyword(st->yin_ctx, "must", strlen("must"), prefix, prefix_len, YANG_NONE), YANG_MUST);
287 assert_int_equal(yin_match_keyword(st->yin_ctx, "namespace", strlen("namespace"), prefix, prefix_len, YANG_NONE), YANG_NAMESPACE);
288 assert_int_equal(yin_match_keyword(st->yin_ctx, "notification", strlen("notification"), prefix, prefix_len, YANG_NONE), YANG_NOTIFICATION);
289 assert_int_equal(yin_match_keyword(st->yin_ctx, "ordered-by", strlen("ordered-by"), prefix, prefix_len, YANG_NONE), YANG_ORDERED_BY);
290 assert_int_equal(yin_match_keyword(st->yin_ctx, "organization", strlen("organization"), prefix, prefix_len, YANG_NONE), YANG_ORGANIZATION);
291 assert_int_equal(yin_match_keyword(st->yin_ctx, "output", strlen("output"), prefix, prefix_len, YANG_NONE), YANG_OUTPUT);
292 assert_int_equal(yin_match_keyword(st->yin_ctx, "path", strlen("path"), prefix, prefix_len, YANG_NONE), YANG_PATH);
293 assert_int_equal(yin_match_keyword(st->yin_ctx, "pattern", strlen("pattern"), prefix, prefix_len, YANG_NONE), YANG_PATTERN);
294 assert_int_equal(yin_match_keyword(st->yin_ctx, "position", strlen("position"), prefix, prefix_len, YANG_NONE), YANG_POSITION);
295 assert_int_equal(yin_match_keyword(st->yin_ctx, "prefix", strlen("prefix"), prefix, prefix_len, YANG_NONE), YANG_PREFIX);
296 assert_int_equal(yin_match_keyword(st->yin_ctx, "presence", strlen("presence"), prefix, prefix_len, YANG_NONE), YANG_PRESENCE);
297 assert_int_equal(yin_match_keyword(st->yin_ctx, "range", strlen("range"), prefix, prefix_len, YANG_NONE), YANG_RANGE);
298 assert_int_equal(yin_match_keyword(st->yin_ctx, "reference", strlen("reference"), prefix, prefix_len, YANG_NONE), YANG_REFERENCE);
299 assert_int_equal(yin_match_keyword(st->yin_ctx, "refine", strlen("refine"), prefix, prefix_len, YANG_NONE), YANG_REFINE);
300 assert_int_equal(yin_match_keyword(st->yin_ctx, "require-instance", strlen("require-instance"), prefix, prefix_len, YANG_NONE), YANG_REQUIRE_INSTANCE);
301 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision", strlen("revision"), prefix, prefix_len, YANG_NONE), YANG_REVISION);
302 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision-date", strlen("revision-date"), prefix, prefix_len, YANG_NONE), YANG_REVISION_DATE);
303 assert_int_equal(yin_match_keyword(st->yin_ctx, "rpc", strlen("rpc"), prefix, prefix_len, YANG_NONE), YANG_RPC);
304 assert_int_equal(yin_match_keyword(st->yin_ctx, "status", strlen("status"), prefix, prefix_len, YANG_NONE), YANG_STATUS);
305 assert_int_equal(yin_match_keyword(st->yin_ctx, "submodule", strlen("submodule"), prefix, prefix_len, YANG_NONE), YANG_SUBMODULE);
306 assert_int_equal(yin_match_keyword(st->yin_ctx, "type", strlen("type"), prefix, prefix_len, YANG_NONE), YANG_TYPE);
307 assert_int_equal(yin_match_keyword(st->yin_ctx, "typedef", strlen("typedef"), prefix, prefix_len, YANG_NONE), YANG_TYPEDEF);
308 assert_int_equal(yin_match_keyword(st->yin_ctx, "unique", strlen("unique"), prefix, prefix_len, YANG_NONE), YANG_UNIQUE);
309 assert_int_equal(yin_match_keyword(st->yin_ctx, "units", strlen("units"), prefix, prefix_len, YANG_NONE), YANG_UNITS);
310 assert_int_equal(yin_match_keyword(st->yin_ctx, "uses", strlen("uses"), prefix, prefix_len, YANG_NONE), YANG_USES);
311 assert_int_equal(yin_match_keyword(st->yin_ctx, "value", strlen("value"), prefix, prefix_len, YANG_NONE), YANG_VALUE);
312 assert_int_equal(yin_match_keyword(st->yin_ctx, "when", strlen("when"), prefix, prefix_len, YANG_NONE), YANG_WHEN);
313 assert_int_equal(yin_match_keyword(st->yin_ctx, "yang-version", strlen("yang-version"), prefix, prefix_len, YANG_NONE), YANG_YANG_VERSION);
314 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 +0200315
316 st->finished_correctly = true;
David Sedlák872c7b42018-10-26 13:15:20 +0200317}
David Sedlák3b4db242018-10-19 16:11:01 +0200318
David Sedlák872c7b42018-10-26 13:15:20 +0200319static void
David Sedlák060b00e2019-06-19 11:12:06 +0200320test_yin_match_argument_name(void **state)
David Sedlák872c7b42018-10-26 13:15:20 +0200321{
David Sedlák68a1af12019-03-08 13:46:54 +0100322 (void)state; /* unused */
David Sedlák872c7b42018-10-26 13:15:20 +0200323
David Sedlák060b00e2019-06-19 11:12:06 +0200324 assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN);
325 assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN);
326 assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN);
327 assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION);
328 assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE);
329 assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE);
330 assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME);
331 assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG);
332 assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
333 assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT);
334 assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI);
335 assert_int_equal(yin_match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200336}
337
David Sedlák68a1af12019-03-08 13:46:54 +0100338static void
David Sedlákb1a78352019-06-28 16:16:29 +0200339test_yin_parse_element_generic(void **state)
340{
341 const char *prefix, *name;
342 struct state *st = *state;
343 struct lysp_ext_instance exts;
344 size_t prefix_len, name_len;
345 LY_ERR ret;
346
347 memset(&exts, 0, sizeof(exts));
348
349 const char *data = "<elem attr=\"value\">text_value</elem>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200350 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
351 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 +0200352 assert_int_equal(ret, LY_SUCCESS);
David Sedlák1fdb2522019-07-09 16:22:57 +0200353 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákb1a78352019-06-28 16:16:29 +0200354 assert_string_equal(exts.child->stmt, "elem");
355 assert_string_equal(exts.child->arg, "text_value");
David Sedlákb1a78352019-06-28 16:16:29 +0200356 assert_string_equal(exts.child->child->stmt, "attr");
357 assert_string_equal(exts.child->child->arg, "value");
358 assert_true(exts.child->child->flags & LYS_YIN_ATTR);
David Sedlákb1a78352019-06-28 16:16:29 +0200359 lysp_ext_instance_free(st->ctx, &exts);
David Sedlák5392a212019-07-01 09:19:10 +0200360 st = reset_state(state);
361
362 data = "<elem></elem>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200363 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
364 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 +0200365 assert_int_equal(ret, LY_SUCCESS);
366 assert_string_equal(exts.child->stmt, "elem");
367 assert_null(exts.child->child);
368 assert_null(exts.child->arg);
David Sedlákda8ffa32019-07-08 14:17:10 +0200369 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlák5392a212019-07-01 09:19:10 +0200370 lysp_ext_instance_free(st->ctx, &exts);
371
David Sedlákb1a78352019-06-28 16:16:29 +0200372 st->finished_correctly = true;
373}
374
375static void
376test_yin_parse_extension_instance(void **state)
377{
378 LY_ERR ret;
379 struct state *st = *state;
380 const char *prefix, *name;
381 size_t prefix_len, name_len;
382 struct yin_arg_record *args = NULL;
383 struct lysp_ext_instance *exts = NULL;
David Sedlákb1a78352019-06-28 16:16:29 +0200384 const char *data = "<ext value1=\"test\" value=\"test2\"><subelem>text</subelem></ext>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200385 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
386 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák1f90d252019-07-10 17:09:32 +0200387 ret = yin_parse_extension_instance(st->yin_ctx, args, &data, name2fullname(name, prefix_len),
David Sedlák619db942019-07-03 14:47:30 +0200388 namelen2fulllen(name_len, prefix_len), LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200389 assert_int_equal(ret, LY_SUCCESS);
390 assert_string_equal(exts->name, "ext");
391 assert_int_equal(exts->insubstmt_index, 0);
392 assert_true(exts->insubstmt == LYEXT_SUBSTMT_CONTACT);
393 assert_true(exts->yin & LYS_YIN);
394 assert_string_equal(exts->child->stmt, "value1");
395 assert_string_equal(exts->child->arg, "test");
396 assert_null(exts->child->child);
397 assert_true(exts->child->flags & LYS_YIN_ATTR);
398 assert_string_equal(exts->child->next->stmt, "value");
399 assert_string_equal(exts->child->next->arg, "test2");
400 assert_null(exts->child->next->child);
401 assert_true(exts->child->next->flags & LYS_YIN_ATTR);
402
403 assert_string_equal(exts->child->next->next->stmt, "subelem");
404 assert_string_equal(exts->child->next->next->arg, "text");
405 assert_null(exts->child->next->next->child);
406 assert_null(exts->child->next->next->next);
407 assert_false(exts->child->next->next->flags & LYS_YIN_ATTR);
David Sedlákda8ffa32019-07-08 14:17:10 +0200408 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákb1a78352019-06-28 16:16:29 +0200409 LY_ARRAY_FREE(args);
410 lysp_ext_instance_free(st->ctx, exts);
411 LY_ARRAY_FREE(exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200412 exts = NULL;
413 args = NULL;
414 st = reset_state(state);
415
416 data = "<extension-elem />";
David Sedlákda8ffa32019-07-08 14:17:10 +0200417 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
418 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák1f90d252019-07-10 17:09:32 +0200419 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 +0200420 assert_int_equal(ret, LY_SUCCESS);
421 assert_string_equal(exts->name, "extension-elem");
422 assert_null(exts->argument);
423 assert_null(exts->child);
424 assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT);
425 assert_int_equal(exts->insubstmt_index, 0);
426 assert_true(exts->yin & LYS_YIN);
David Sedlákda8ffa32019-07-08 14:17:10 +0200427 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákf250ecf2019-07-01 11:02:05 +0200428 LY_ARRAY_FREE(args);
429 lysp_ext_instance_free(st->ctx, exts);
430 LY_ARRAY_FREE(exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200431 st->finished_correctly = true;
432}
433
David Sedlák555c7202019-07-04 12:14:12 +0200434static void
435test_yin_parse_content(void **state)
436{
437 struct state *st = *state;
438 LY_ERR ret = LY_SUCCESS;
439 struct sized_string name, prefix;
440 const char *data = "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
441 "<custom xmlns=\"my-ext\">"
442 "totally amazing extension"
443 "</custom>"
David Sedlák986cb412019-07-04 13:10:11 +0200444 "<extension name=\"ext\">"
445 "<argument name=\"argname\"></argument>"
446 "<description><text>desc</text></description>"
447 "<reference><text>ref</text></reference>"
448 "<status value=\"deprecated\"></status>"
449 "</extension>"
David Sedlák555c7202019-07-04 12:14:12 +0200450 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedlák32488102019-07-15 17:44:10 +0200451 "<if-feature name=\"foo\"></if-feature>"
David Sedlák32eee7b2019-07-09 12:38:44 +0200452 "<when condition=\"condition...\">"
453 "<reference><text>when_ref</text></reference>"
454 "<description><text>when_desc</text></description>"
455 "</when>"
David Sedláke1a30302019-07-10 13:49:38 +0200456 "<config value=\"true\"/>"
David Sedlákc1771b12019-07-10 15:55:46 +0200457 "<error-message>"
458 "<value>error-msg</value>"
459 "</error-message>"
David Sedlák2ce1be62019-07-10 16:15:09 +0200460 "<error-app-tag value=\"err-app-tag\"/>"
David Sedláka5b1d382019-07-10 16:31:09 +0200461 "<units name=\"radians\"></units>"
David Sedláke7084ce2019-07-10 16:44:15 +0200462 "<default value=\"default-value\"/>"
David Sedlák5545f5d2019-07-11 11:55:16 +0200463 "<position value=\"25\"></position>"
464 "<value value=\"-5\"/>"
David Sedlákcf5569a2019-07-11 13:31:34 +0200465 "<require-instance value=\"true\"></require-instance>"
David Sedlákb7296dd2019-07-11 14:58:38 +0200466 "<range value=\"5..10\" />"
David Sedlák438ae432019-07-11 15:36:54 +0200467 "<length value=\"baf\"/>"
David Sedlákd3983112019-07-12 11:20:56 +0200468 "<pattern value='pattern'>"
469 "<modifier value='invert-match'/>"
470 "</pattern>"
David Sedlákfd5b9c32019-07-12 15:33:13 +0200471 "<enum name=\"yay\">"
472 "</enum>"
David Sedlák555c7202019-07-04 12:14:12 +0200473 "</prefix>";
474 struct lysp_ext_instance *exts = NULL;
David Sedlák5f8191e2019-07-08 16:35:52 +0200475 const char **if_features = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200476 struct yin_arg_record *attrs = NULL;
David Sedláke7084ce2019-07-10 16:44:15 +0200477 const char *value, *err_msg, *app_tag, *units, *def;
David Sedlák986cb412019-07-04 13:10:11 +0200478 struct lysp_ext *ext_def = NULL;
David Sedlák32eee7b2019-07-09 12:38:44 +0200479 struct lysp_when *when_p = NULL;
David Sedlákcf5569a2019-07-11 13:31:34 +0200480 struct lysp_type_enum pos_enum = {}, val_enum = {};
David Sedlákfd5b9c32019-07-12 15:33:13 +0200481 struct lysp_type req_type = {}, range_type = {}, len_type = {}, patter_type = {}, enum_type = {};
David Sedláke1a30302019-07-10 13:49:38 +0200482 uint8_t config = 0;
David Sedlák555c7202019-07-04 12:14:12 +0200483
David Sedlákda8ffa32019-07-08 14:17:10 +0200484 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
485 yin_load_attributes(st->yin_ctx, &data, &attrs);
David Sedlák555c7202019-07-04 12:14:12 +0200486
David Sedlákfd5b9c32019-07-12 15:33:13 +0200487 struct yin_subelement subelems[17] = {
David Sedlák438ae432019-07-11 15:36:54 +0200488 {YANG_CONFIG, &config, 0},
David Sedlákc3da3ef2019-07-19 12:56:08 +0200489 {YANG_DEFAULT, &def, YIN_SUBELEM_UNIQUE},
David Sedlákfd5b9c32019-07-12 15:33:13 +0200490 {YANG_ENUM, &enum_type, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200491 {YANG_ERROR_APP_TAG, &app_tag, 0},
492 {YANG_ERROR_MESSAGE, &err_msg, 0},
493 {YANG_EXTENSION, &ext_def, 0},
494 {YANG_IF_FEATURE, &if_features, 0},
495 {YANG_LENGTH, &len_type, 0},
David Sedlákd3983112019-07-12 11:20:56 +0200496 {YANG_PATTERN, &patter_type, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200497 {YANG_RANGE, &range_type, 0},
498 {YANG_REQUIRE_INSTANCE, &req_type, 0},
499 {YANG_UNITS, &units, 0},
David Sedlák374d2b32019-07-17 15:06:55 +0200500 {YANG_POSITION, &pos_enum, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200501 {YANG_VALUE, &val_enum, 0},
502 {YANG_WHEN, &when_p, 0},
503 {YANG_CUSTOM, NULL, 0},
504 {YIN_TEXT, &value, 0}
David Sedlákd3983112019-07-12 11:20:56 +0200505 };
David Sedlákfd5b9c32019-07-12 15:33:13 +0200506 ret = yin_parse_content(st->yin_ctx, subelems, 17, &data, YANG_PREFIX, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200507 assert_int_equal(ret, LY_SUCCESS);
David Sedlák1fdb2522019-07-09 16:22:57 +0200508 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlák2ce1be62019-07-10 16:15:09 +0200509 /* check parsed values */
David Sedláke7084ce2019-07-10 16:44:15 +0200510 assert_string_equal(def, "default-value");
David Sedlák555c7202019-07-04 12:14:12 +0200511 assert_string_equal(exts->name, "custom");
512 assert_string_equal(exts->argument, "totally amazing extension");
513 assert_string_equal(value, "wsefsdf");
David Sedláka5b1d382019-07-10 16:31:09 +0200514 assert_string_equal(units, "radians");
David Sedlák32eee7b2019-07-09 12:38:44 +0200515 assert_string_equal(when_p->cond, "condition...");
516 assert_string_equal(when_p->dsc, "when_desc");
517 assert_string_equal(when_p->ref, "when_ref");
David Sedláke1a30302019-07-10 13:49:38 +0200518 assert_int_equal(config, LYS_CONFIG_W);
David Sedlák5545f5d2019-07-11 11:55:16 +0200519 assert_int_equal(pos_enum.value, 25);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200520 assert_true(pos_enum.flags & LYS_SET_VALUE);
David Sedlák5545f5d2019-07-11 11:55:16 +0200521 assert_int_equal(val_enum.value, -5);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200522 assert_true(val_enum.flags & LYS_SET_VALUE);
David Sedlákcf5569a2019-07-11 13:31:34 +0200523 assert_int_equal(req_type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200524 assert_true(req_type.flags &= LYS_SET_REQINST);
David Sedlákb7296dd2019-07-11 14:58:38 +0200525 assert_string_equal(range_type.range->arg, "5..10");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200526 assert_true(range_type.flags & LYS_SET_RANGE);
David Sedlákc1771b12019-07-10 15:55:46 +0200527 assert_string_equal(err_msg, "error-msg");
David Sedlák2ce1be62019-07-10 16:15:09 +0200528 assert_string_equal(app_tag, "err-app-tag");
David Sedlákfd5b9c32019-07-12 15:33:13 +0200529 assert_string_equal(enum_type.enums->name, "yay");
David Sedlák438ae432019-07-11 15:36:54 +0200530 assert_string_equal(len_type.length->arg, "baf");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200531 assert_true(len_type.flags & LYS_SET_LENGTH);
David Sedlákd3983112019-07-12 11:20:56 +0200532 assert_string_equal(patter_type.patterns->arg, "\x015pattern");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200533 assert_true(patter_type.flags & LYS_SET_PATTERN);
David Sedlák2ce1be62019-07-10 16:15:09 +0200534 /* cleanup */
David Sedlák555c7202019-07-04 12:14:12 +0200535 lysp_ext_instance_free(st->ctx, exts);
David Sedlák32eee7b2019-07-09 12:38:44 +0200536 lysp_when_free(st->ctx, when_p);
David Sedlák986cb412019-07-04 13:10:11 +0200537 lysp_ext_free(st->ctx, ext_def);
David Sedlák5f8191e2019-07-08 16:35:52 +0200538 FREE_STRING(st->ctx, *if_features);
David Sedlákc1771b12019-07-10 15:55:46 +0200539 FREE_STRING(st->ctx, err_msg);
David Sedlák2ce1be62019-07-10 16:15:09 +0200540 FREE_STRING(st->ctx, app_tag);
David Sedláka5b1d382019-07-10 16:31:09 +0200541 FREE_STRING(st->ctx, units);
David Sedlákd3983112019-07-12 11:20:56 +0200542 FREE_STRING(st->ctx, patter_type.patterns->arg);
David Sedláke7084ce2019-07-10 16:44:15 +0200543 FREE_STRING(st->ctx, def);
David Sedlákb7296dd2019-07-11 14:58:38 +0200544 FREE_STRING(st->ctx, range_type.range->arg);
David Sedlák438ae432019-07-11 15:36:54 +0200545 FREE_STRING(st->ctx, len_type.length->arg);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200546 FREE_STRING(st->ctx, enum_type.enums->name);
David Sedlákb7296dd2019-07-11 14:58:38 +0200547 FREE_STRING(st->ctx, value);
David Sedlák5f8191e2019-07-08 16:35:52 +0200548 LY_ARRAY_FREE(if_features);
David Sedlák555c7202019-07-04 12:14:12 +0200549 LY_ARRAY_FREE(exts);
David Sedlák986cb412019-07-04 13:10:11 +0200550 LY_ARRAY_FREE(ext_def);
David Sedlák555c7202019-07-04 12:14:12 +0200551 LY_ARRAY_FREE(attrs);
David Sedlákd3983112019-07-12 11:20:56 +0200552 LY_ARRAY_FREE(patter_type.patterns);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200553 LY_ARRAY_FREE(enum_type.enums);
David Sedlák32eee7b2019-07-09 12:38:44 +0200554 free(when_p);
David Sedlákb7296dd2019-07-11 14:58:38 +0200555 free(range_type.range);
David Sedlák438ae432019-07-11 15:36:54 +0200556 free(len_type.length);
David Sedlák555c7202019-07-04 12:14:12 +0200557 attrs = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200558 st = reset_state(state);
559
560 /* test unique subelem */
561 const char *prefix_value;
562 struct yin_subelement subelems2[2] = {{YANG_PREFIX, &prefix_value, 0},
563 {YIN_TEXT, &value, YIN_SUBELEM_UNIQUE}};
564 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
565 "<prefix value=\"inv_mod\" />"
566 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
567 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
568 "</module>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200569 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
570 yin_load_attributes(st->yin_ctx, &data, &attrs);
571 ret = yin_parse_content(st->yin_ctx, subelems2, 2, &data, YANG_MODULE, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200572 assert_int_equal(ret, LY_EVALID);
573 logbuf_assert("Redefinition of text element in module element. Line number 1.");
574 lydict_remove(st->ctx, prefix_value);
575 lydict_remove(st->ctx, value);
576 st = reset_state(state);
577 LY_ARRAY_FREE(attrs);
578 attrs = NULL;
579
580 /* test first subelem */
581 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
582 "<prefix value=\"inv_mod\" />"
583 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
584 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
585 "</module>";
586 struct yin_subelement subelems3[2] = {{YANG_PREFIX, &prefix_value, 0},
587 {YIN_TEXT, &value, YIN_SUBELEM_FIRST}};
David Sedlákda8ffa32019-07-08 14:17:10 +0200588 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
589 yin_load_attributes(st->yin_ctx, &data, &attrs);
590 ret = yin_parse_content(st->yin_ctx, subelems3, 2, &data, YANG_MODULE, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200591 assert_int_equal(ret, LY_EVALID);
592 logbuf_assert("Subelement text of module element must be defined as first subelement. Line number 1.");
593 lydict_remove(st->ctx, prefix_value);
594 st = reset_state(state);
595 LY_ARRAY_FREE(attrs);
596 attrs = NULL;
597
598 /* test mandatory subelem */
599 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
600 "</module>";
601 struct yin_subelement subelems4[1] = {{YANG_PREFIX, &prefix_value, YIN_SUBELEM_MANDATORY}};
David Sedlákda8ffa32019-07-08 14:17:10 +0200602 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
603 yin_load_attributes(st->yin_ctx, &data, &attrs);
604 ret = yin_parse_content(st->yin_ctx, subelems4, 1, &data, YANG_MODULE, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200605 assert_int_equal(ret, LY_EVALID);
606 logbuf_assert("Missing mandatory subelement prefix of module element. Line number 1.");
607 LY_ARRAY_FREE(attrs);
608
609 st->finished_correctly = true;
610}
611
David Sedlák92147b02019-07-09 14:01:01 +0200612static void
David Sedlák4a650532019-07-10 11:55:18 +0200613test_validate_value(void **state)
614{
615 struct state *st = *state;
616 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "#invalid", 8), LY_EVALID);
617 logbuf_assert("Invalid identifier character '#'. Line number 1.");
618 assert_int_equal(yin_validate_value(st->yin_ctx, Y_STR_ARG, "", 0), LY_SUCCESS);
619 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "pre:b", 5), LY_EVALID);
620 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:b", 5), LY_SUCCESS);
621 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:pre:b", 9), LY_EVALID);
622
623 st->finished_correctly = true;
624}
625
David Sedlák32488102019-07-15 17:44:10 +0200626static int
627setup_element_test(void **state)
628{
David Sedlák8e7bda82019-07-16 17:57:50 +0200629 struct state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +0200630
631#if ENABLE_LOGGER_CHECKING
632 /* setup logger */
633 ly_set_log_clb(logger, 1);
634#endif
635
636 /* reset logbuf */
637 logbuf[0] = '\0';
David Sedlák32488102019-07-15 17:44:10 +0200638
639 /* allocate parser context */
640 st->yin_ctx = calloc(1, sizeof(*st->yin_ctx));
641 st->yin_ctx->xml_ctx.ctx = st->ctx;
642 st->yin_ctx->xml_ctx.line = 1;
643
644 return EXIT_SUCCESS;
645}
646
647static int
648teardown_element_test(void **state)
649{
650 struct state *st = *(struct state **)state;
651
652#if ENABLE_LOGGER_CHECKING
653 /* teardown logger */
654 if (!st->finished_correctly && logbuf[0] != '\0') {
655 fprintf(stderr, "%s\n", logbuf);
656 }
657#endif
658
659 lyxml_context_clear(&st->yin_ctx->xml_ctx);
David Sedlák32488102019-07-15 17:44:10 +0200660 free(st->yin_ctx);
David Sedlák32488102019-07-15 17:44:10 +0200661
662 return EXIT_SUCCESS;
663}
664
665#define ELEMENT_WRAPPER_START "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
666#define ELEMENT_WRAPPER_END "</module>"
667
668/* helper function to simplify unit test of each element using parse_content function */
669LY_ERR
670test_element_helper(struct state *st, const char **data, void *dest, const char **text,
671 struct lysp_ext_instance **exts, bool valid)
672{
673 struct yin_arg_record *attrs = NULL;
674 struct sized_string name, prefix;
675 LY_ERR ret = LY_SUCCESS;
676 struct yin_subelement subelems[71] = {
677 {YANG_ACTION, dest, 0},
678 {YANG_ANYDATA, dest, 0},
679 {YANG_ANYXML, dest, 0},
680 {YANG_ARGUMENT,dest, 0},
681 {YANG_AUGMENT, dest, 0},
682 {YANG_BASE, dest, 0},
683 {YANG_BELONGS_TO, dest, 0},
684 {YANG_BIT, dest, 0},
685 {YANG_CASE, dest, 0},
686 {YANG_CHOICE, dest, 0},
687 {YANG_CONFIG, dest, 0},
688 {YANG_CONTACT, dest, 0},
689 {YANG_CONTAINER, dest, 0},
David Sedlákc3da3ef2019-07-19 12:56:08 +0200690 {YANG_DEFAULT, dest, YIN_SUBELEM_UNIQUE},
David Sedlák32488102019-07-15 17:44:10 +0200691 {YANG_DESCRIPTION, dest, 0},
692 {YANG_DEVIATE, dest, 0},
693 {YANG_DEVIATION, dest, 0},
694 {YANG_ENUM, dest, 0},
695 {YANG_ERROR_APP_TAG, dest, 0},
696 {YANG_ERROR_MESSAGE, dest, 0},
697 {YANG_EXTENSION, dest, 0},
698 {YANG_FEATURE, dest, 0},
699 {YANG_FRACTION_DIGITS, dest, 0},
700 {YANG_GROUPING, dest, 0},
701 {YANG_IDENTITY, dest, 0},
702 {YANG_IF_FEATURE, dest, 0},
703 {YANG_IMPORT, dest, 0},
704 {YANG_INCLUDE, dest, 0},
705 {YANG_INPUT, dest, 0},
706 {YANG_KEY, dest, 0},
707 {YANG_LEAF, dest, 0},
708 {YANG_LEAF_LIST, dest, 0},
709 {YANG_LENGTH, dest, 0},
710 {YANG_LIST, dest, 0},
711 {YANG_MANDATORY, dest, 0},
712 {YANG_MAX_ELEMENTS, dest, 0},
713 {YANG_MIN_ELEMENTS, dest, 0},
714 {YANG_MODIFIER, dest, 0},
715 {YANG_MODULE, dest, 0},
716 {YANG_MUST, dest, 0},
717 {YANG_NAMESPACE, dest, 0},
718 {YANG_NOTIFICATION, dest, 0},
719 {YANG_ORDERED_BY, dest, 0},
720 {YANG_ORGANIZATION, dest, 0},
721 {YANG_OUTPUT, dest, 0},
722 {YANG_PATH, dest, 0},
723 {YANG_PATTERN, dest, 0},
David Sedlák32488102019-07-15 17:44:10 +0200724 {YANG_PREFIX, dest, 0},
725 {YANG_PRESENCE, dest, 0},
726 {YANG_RANGE, dest, 0},
727 {YANG_REFERENCE, dest, 0},
728 {YANG_REFINE, dest, 0},
729 {YANG_REQUIRE_INSTANCE, dest, 0},
730 {YANG_REVISION, dest, 0},
731 {YANG_REVISION_DATE, dest, 0},
732 {YANG_RPC, dest, 0},
733 {YANG_STATUS, dest, 0},
734 {YANG_SUBMODULE, dest, 0},
735 {YANG_TYPE, dest, 0},
736 {YANG_TYPEDEF, dest, 0},
737 {YANG_UNIQUE, dest, 0},
738 {YANG_UNITS, dest, 0},
739 {YANG_USES, dest, 0},
David Sedlák374d2b32019-07-17 15:06:55 +0200740 {YANG_POSITION, dest, 0},
David Sedlák32488102019-07-15 17:44:10 +0200741 {YANG_VALUE, dest, 0},
742 {YANG_WHEN, dest, 0},
743 {YANG_YANG_VERSION, dest, 0},
744 {YANG_YIN_ELEMENT, dest, 0},
745 {YANG_CUSTOM, dest, 0},
746 {YIN_TEXT, dest, 0},
747 {YIN_VALUE, dest, 0}
748 };
749 LY_CHECK_RET(lyxml_get_element(&st->yin_ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len));\
750 LY_CHECK_RET(yin_load_attributes(st->yin_ctx, data, &attrs));\
David Sedlák8e7bda82019-07-16 17:57:50 +0200751 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 +0200752 LY_ARRAY_FREE(attrs);
753 if (valid) {
754 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
755 }
756 /* reset status */
757 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
758 return ret;
759}
760
761static void
762test_enum_bit_elem(void **state)
763{
764 /* yin_parse_enum_bit is function that is being mainly tested by this test */
765 struct state *st = *state;
766 struct lysp_type type = {};
767 const char *data;
768 data = ELEMENT_WRAPPER_START
769 "<enum name=\"enum-name\">"
770 "<if-feature name=\"feature\" />"
771 "<value value=\"55\" />"
772 "<status value=\"deprecated\" />"
773 "<description><text>desc...</text></description>"
774 "<reference><text>ref...</text></reference>"
775 "</enum>"
776 ELEMENT_WRAPPER_END;
777 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
778 assert_string_equal(*type.enums->iffeatures, "feature");
779 assert_int_equal(type.enums->value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200780 assert_true((type.enums->flags & LYS_STATUS_DEPRC) && (type.enums->flags & LYS_SET_VALUE));
David Sedlák32488102019-07-15 17:44:10 +0200781 assert_string_equal(type.enums->dsc, "desc...");
782 assert_string_equal(type.enums->ref, "ref...");
783 lysp_type_free(st->ctx, &type);
784 memset(&type, 0, sizeof type);
785
786 /* todo bit element test */
787 st->finished_correctly = true;
788}
789
790static void
791test_meta_elem(void **state)
792{
793 struct state *st = *state;
794 char *value = NULL;
795 const char *data;
796
797 /* organization element */
798 data = ELEMENT_WRAPPER_START
799 "<organization><text>organization...</text></organization>"
800 ELEMENT_WRAPPER_END;
801 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
802 assert_string_equal(value, "organization...");
803 FREE_STRING(st->ctx, value);
804 value = NULL;
805 /* contact element */
806 data = ELEMENT_WRAPPER_START
807 "<contact><text>contact...</text></contact>"
808 ELEMENT_WRAPPER_END;
809 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
810 assert_string_equal(value, "contact...");
811 FREE_STRING(st->ctx, value);
812 value = NULL;
813 /* description element */
814 data = ELEMENT_WRAPPER_START
815 "<description><text>description...</text></description>"
816 ELEMENT_WRAPPER_END;
817 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
818 assert_string_equal(value, "description...");
819 FREE_STRING(st->ctx, value);
820 value = NULL;
821 /* reference element */
822 data = ELEMENT_WRAPPER_START
823 "<reference><text>reference...</text></reference>"
824 ELEMENT_WRAPPER_END;
825 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
826 assert_string_equal(value, "reference...");
827 FREE_STRING(st->ctx, value);
828 value = NULL;
829
830 /* missing text subelement */
831 data = ELEMENT_WRAPPER_START
832 "<reference>reference...</reference>"
833 ELEMENT_WRAPPER_END;
834 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, false), LY_EVALID);
835 logbuf_assert("Missing mandatory subelement text of reference element. Line number 1.");
836
837 st->finished_correctly = true;
838}
839
840static void
841test_import_elem(void **state)
842{
843 struct state *st = *state;
844 const char *data;
845 struct lys_module *lys_mod = calloc(1, sizeof *lys_mod);
846 struct lysp_module *lysp_mod = calloc(1, sizeof *lysp_mod);
847 lys_mod->ctx = st->ctx;
848 lysp_mod->mod = lys_mod;
849
850 /* max subelems */
851 data = ELEMENT_WRAPPER_START
852 "<import module=\"a\">"
853 "<prefix value=\"a_mod\"/>"
854 "<revision-date date=\"2015-01-01\"></revision-date>"
855 "<description><text>import description</text></description>"
856 "<reference><text>import reference</text></reference>"
857 "</import>"
858 ELEMENT_WRAPPER_END;
859 assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, true), LY_SUCCESS);
860 assert_string_equal(lysp_mod->imports->name, "a");
861 assert_string_equal(lysp_mod->imports->prefix, "a_mod");
862 assert_string_equal(lysp_mod->imports->rev, "2015-01-01");
863 assert_string_equal(lysp_mod->imports->dsc, "import description");
864 assert_string_equal(lysp_mod->imports->ref, "import reference");
865 lysp_module_free(lysp_mod);
866 lys_module_free(lys_mod, NULL);
867
868 /* min subelems */
869 lys_mod = calloc(1, sizeof *lys_mod);
870 lysp_mod = calloc(1, sizeof *lysp_mod);
871 lys_mod->ctx = st->ctx;
872 lysp_mod->mod = lys_mod;
873 data = ELEMENT_WRAPPER_START
874 "<import module=\"a\">"
875 "<prefix value=\"a_mod\"/>"
876 "</import>"
877 ELEMENT_WRAPPER_END;
878 assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, true), LY_SUCCESS);
879 assert_string_equal(lysp_mod->imports->prefix, "a_mod");
880 lysp_module_free(lysp_mod);
881 lys_module_free(lys_mod, NULL);
882
883 /* invalid (missing prefix) */
884 lys_mod = calloc(1, sizeof *lys_mod);
885 lysp_mod = calloc(1, sizeof *lysp_mod);
886 lys_mod->ctx = st->ctx;
887 lysp_mod->mod = lys_mod;
888 data = ELEMENT_WRAPPER_START "<import module=\"a\">""</import>" ELEMENT_WRAPPER_END;
889 assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, false), LY_EVALID);
890 logbuf_assert("Missing mandatory subelement prefix of import element. Line number 1.");
891 lysp_module_free(lysp_mod);
892 lys_module_free(lys_mod, NULL);
893
894 /* invalid reused prefix */
895 lys_mod = calloc(1, sizeof *lys_mod);
896 lysp_mod = calloc(1, sizeof *lysp_mod);
897 lys_mod->ctx = st->ctx;
898 lysp_mod->mod = lys_mod;
899 data = ELEMENT_WRAPPER_START
900 "<import module=\"a\">"
901 "<prefix value=\"a_mod\"/>"
902 "</import>"
903 "<import module=\"a\">"
904 "<prefix value=\"a_mod\"/>"
905 "</import>"
906 ELEMENT_WRAPPER_END;
907 assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, false), LY_EVALID);
908 logbuf_assert("Prefix \"a_mod\" already used to import \"a\" module. Line number 1.");
909 lysp_module_free(lysp_mod);
910 lys_module_free(lys_mod, NULL);
911
912 st->finished_correctly = true;
913}
914
915static void
916test_status_elem(void **state)
917{
918 struct state *st = *state;
919 const char *data;
920 uint16_t flags = 0;
921
922 /* test valid values */
923 data = ELEMENT_WRAPPER_START "<status value=\"current\" />" ELEMENT_WRAPPER_END;
924 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200925 assert_true(flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +0200926
927 data = ELEMENT_WRAPPER_START "<status value=\"deprecated\" />" ELEMENT_WRAPPER_END;
928 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200929 assert_true(flags & LYS_STATUS_DEPRC);
David Sedlák32488102019-07-15 17:44:10 +0200930
931 data = ELEMENT_WRAPPER_START "<status value=\"obsolete\"></status>" ELEMENT_WRAPPER_END;
932 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200933 assert_true(flags & LYS_STATUS_OBSLT);
David Sedlák32488102019-07-15 17:44:10 +0200934
935 /* test invalid value */
936 data = ELEMENT_WRAPPER_START "<status value=\"invalid\"></status>" ELEMENT_WRAPPER_END;
937 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
938 logbuf_assert("Invalid value \"invalid\" of \"status\". Line number 1.");
939 st->finished_correctly = true;
940}
941
942static void
943test_ext_elem(void **state)
944{
945 struct state *st = *state;
946 const char *data;
947 struct lysp_ext *ext = NULL;
948
949 /* max subelems */
950 data = ELEMENT_WRAPPER_START
951 "<extension name=\"ext_name\">"
952 "<argument name=\"arg\"></argument>"
953 "<status value=\"current\"/>"
954 "<description><text>ext_desc</text></description>"
955 "<reference><text>ext_ref</text></reference>"
956 "</extension>"
957 ELEMENT_WRAPPER_END;
958 assert_int_equal(test_element_helper(st, &data, &ext, NULL, NULL, true), LY_SUCCESS);
959 assert_string_equal(ext->name, "ext_name");
960 assert_string_equal(ext->argument, "arg");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200961 assert_true(ext->flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +0200962 assert_string_equal(ext->dsc, "ext_desc");
963 assert_string_equal(ext->ref, "ext_ref");
964 lysp_ext_free(st->ctx, ext);
965 LY_ARRAY_FREE(ext);
966 ext = NULL;
967
968 /* min subelems */
969 data = ELEMENT_WRAPPER_START "<extension name=\"ext_name\"></extension>" ELEMENT_WRAPPER_END;
970 assert_int_equal(test_element_helper(st, &data, &ext, NULL, NULL, true), LY_SUCCESS);
971 assert_string_equal(ext->name, "ext_name");
972 lysp_ext_free(st->ctx, ext);
973 LY_ARRAY_FREE(ext);
974 ext = NULL;
975
976 st->finished_correctly = true;
977}
978
979static void
980test_yin_element_elem(void **state)
981{
982 struct state *st = *state;
983 const char *data;
984 uint16_t flags = 0;
985
986 data = ELEMENT_WRAPPER_START "<yin-element value=\"true\" />" ELEMENT_WRAPPER_END;
987 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200988 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +0200989
990 data = ELEMENT_WRAPPER_START "<yin-element value=\"false\" />" ELEMENT_WRAPPER_END;
991 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200992 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +0200993
994 data = ELEMENT_WRAPPER_START "<yin-element value=\"invalid\" />" ELEMENT_WRAPPER_END;
995 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200996 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +0200997 logbuf_assert("Invalid value \"invalid\" of \"yin-element\". Line number 1.");
998 st->finished_correctly = true;
999}
1000
1001static void
1002test_yangversion_elem(void **state)
1003{
1004 struct state *st = *state;
1005 const char *data;
1006 uint8_t version = 0;
1007
1008 /* valid values */
1009 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.0\" />" ELEMENT_WRAPPER_END;
1010 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001011 assert_true(version & LYS_VERSION_1_0);
David Sedlák32488102019-07-15 17:44:10 +02001012 assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_0);
1013
1014 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.1\" />" ELEMENT_WRAPPER_END;
1015 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001016 assert_true(version & LYS_VERSION_1_1);
David Sedlák32488102019-07-15 17:44:10 +02001017 assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_1);
1018
1019 /* invalid value */
1020 data = ELEMENT_WRAPPER_START "<yang-version value=\"version\" />" ELEMENT_WRAPPER_END;
1021 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, false), LY_EVALID);
1022 logbuf_assert("Invalid value \"version\" of \"yang-version\". Line number 1.");
1023
1024 st->finished_correctly = true;
1025}
1026
1027static void
1028test_mandatory_elem(void **state)
1029{
1030 struct state *st = *state;
1031 const char *data;
1032 uint16_t man = 0;
1033
1034 /* valid values */
1035 data = ELEMENT_WRAPPER_START "<mandatory value=\"true\" />" ELEMENT_WRAPPER_END;
1036 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, true), LY_SUCCESS);
1037 assert_int_equal(man, LYS_MAND_TRUE);
1038 man = 0;
1039
1040 data = ELEMENT_WRAPPER_START "<mandatory value=\"false\" />" ELEMENT_WRAPPER_END;
1041 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, true), LY_SUCCESS);
1042 assert_int_equal(man, LYS_MAND_FALSE);
1043
1044 data = ELEMENT_WRAPPER_START "<mandatory value=\"invalid\" />" ELEMENT_WRAPPER_END;
1045 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, false), LY_EVALID);
1046 logbuf_assert("Invalid value \"invalid\" of \"mandatory\". Line number 1.");
1047
1048 st->finished_correctly = true;
1049}
1050
David Sedlák8e7bda82019-07-16 17:57:50 +02001051static void
1052test_argument_elem(void **state)
1053{
1054 struct state *st = *state;
1055 const char *data;
1056 uint16_t flags = 0;
1057 const char *arg;
1058 struct yin_argument_meta arg_meta = {&flags, &arg};
1059 /* max subelems */
1060 data = ELEMENT_WRAPPER_START
1061 "<argument name=\"arg-name\">"
1062 "<yin-element value=\"true\" />"
1063 "</argument>"
1064 ELEMENT_WRAPPER_END;
1065 assert_int_equal(test_element_helper(st, &data, &arg_meta, NULL, NULL, true), LY_SUCCESS);
1066 assert_string_equal(arg, "arg-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001067 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001068 flags = 0;
1069 FREE_STRING(st->ctx, arg);
1070 arg = NULL;
1071
1072 /* min subelems */
1073 data = ELEMENT_WRAPPER_START
1074 "<argument name=\"arg\">"
1075 "</argument>"
1076 ELEMENT_WRAPPER_END;
1077 assert_int_equal(test_element_helper(st, &data, &arg_meta, NULL, NULL, true), LY_SUCCESS);
1078 assert_string_equal(arg, "arg");
1079 assert_true(flags == 0);
1080 FREE_STRING(st->ctx, arg);
1081
1082 st->finished_correctly = true;
1083}
1084
1085static void
1086test_base_elem(void **state)
1087{
1088 struct state *st = *state;
1089 const char *data;
1090 const char **bases = NULL;
1091 struct lysp_type type = {};
1092
1093 /* as identity subelement */
1094 data = "<identity xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
1095 "<base name=\"base-name\"/>"
1096 "</identity>";
1097 assert_int_equal(test_element_helper(st, &data, &bases, NULL, NULL, true), LY_SUCCESS);
1098 assert_string_equal(*bases, "base-name");
1099 FREE_STRING(st->ctx, *bases);
1100 LY_ARRAY_FREE(bases);
1101
1102 /* as type subelement */
1103 data = "<type xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
1104 "<base name=\"base-name\"/>"
1105 "</type>";
1106 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1107 assert_string_equal(*type.bases, "base-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001108 assert_true(type.flags & LYS_SET_BASE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001109 FREE_STRING(st->ctx, *type.bases);
1110 LY_ARRAY_FREE(type.bases);
1111
1112 st->finished_correctly = true;
1113}
1114
1115static void
1116test_belongsto_elem(void **state)
1117{
1118 struct state *st = *state;
1119 const char *data;
1120 struct lysp_submodule submod;
1121
1122 data = ELEMENT_WRAPPER_START
1123 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>"
1124 ELEMENT_WRAPPER_END;
1125 assert_int_equal(test_element_helper(st, &data, &submod, NULL, NULL, true), LY_SUCCESS);
1126 assert_string_equal(submod.belongsto, "module-name");
1127 assert_string_equal(submod.prefix, "pref");
1128 FREE_STRING(st->ctx, submod.belongsto);
1129 FREE_STRING(st->ctx, submod.prefix);
1130
1131 data = ELEMENT_WRAPPER_START "<belongs-to module=\"module-name\"></belongs-to>" ELEMENT_WRAPPER_END;
1132 assert_int_equal(test_element_helper(st, &data, &submod, NULL, NULL, false), LY_EVALID);
1133 logbuf_assert("Missing mandatory subelement prefix of belongs-to element. Line number 1.");
1134 FREE_STRING(st->ctx, submod.belongsto);
1135
1136 st->finished_correctly = true;
1137}
1138
1139static void
1140test_config_elem(void **state)
1141{
1142 struct state *st = *state;
1143 const char *data;
1144 uint16_t flags = 0;
1145
1146 data = ELEMENT_WRAPPER_START "<config value=\"true\"/>" ELEMENT_WRAPPER_END;
1147 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001148 assert_true(flags & LYS_CONFIG_W);
David Sedlák8e7bda82019-07-16 17:57:50 +02001149 flags = 0;
1150
1151 data = ELEMENT_WRAPPER_START "<config value=\"false\"/>" ELEMENT_WRAPPER_END;
1152 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001153 assert_true(flags & LYS_CONFIG_R);
David Sedlák8e7bda82019-07-16 17:57:50 +02001154 flags = 0;
1155
1156 data = ELEMENT_WRAPPER_START "<config value=\"invalid\"/>" ELEMENT_WRAPPER_END;
1157 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
1158 logbuf_assert("Invalid value \"invalid\" of \"config\". Line number 1.");
1159
1160 st->finished_correctly = true;
1161}
1162
1163static void
1164test_default_elem(void **state)
1165{
1166 struct state *st = *state;
1167 const char *data;
1168 const char *val = NULL;
1169
1170 data = ELEMENT_WRAPPER_START "<default value=\"defaul-value\"/>" ELEMENT_WRAPPER_END;
1171 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1172 assert_string_equal(val, "defaul-value");
1173 FREE_STRING(st->ctx, val);
1174 val = NULL;
1175
1176 data = ELEMENT_WRAPPER_START "<default/>" ELEMENT_WRAPPER_END;
1177 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1178 logbuf_assert("Missing mandatory attribute value of default element. Line number 1.");
1179
1180 st->finished_correctly = true;
1181}
1182
1183static void
1184test_err_app_tag_elem(void **state)
1185{
1186 struct state *st = *state;
1187 const char *data;
1188 const char *val = NULL;
1189
1190 data = ELEMENT_WRAPPER_START "<error-app-tag value=\"val\"/>" ELEMENT_WRAPPER_END;
1191 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1192 assert_string_equal(val, "val");
1193 FREE_STRING(st->ctx, val);
1194 val = NULL;
1195
1196 data = ELEMENT_WRAPPER_START "<error-app-tag/>" ELEMENT_WRAPPER_END;
1197 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1198 logbuf_assert("Missing mandatory attribute value of error-app-tag element. Line number 1.");
1199
1200 st->finished_correctly = true;
1201}
1202
1203static void
1204test_err_msg_elem(void **state)
1205{
1206 struct state *st = *state;
1207 const char *data;
1208 const char *val = NULL;
1209
1210 data = ELEMENT_WRAPPER_START "<error-message><value>val</value></error-message>" ELEMENT_WRAPPER_END;
1211 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1212 assert_string_equal(val, "val");
1213 FREE_STRING(st->ctx, val);
1214
1215 data = ELEMENT_WRAPPER_START "<error-message></error-message>" ELEMENT_WRAPPER_END;
1216 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1217 logbuf_assert("Missing mandatory subelement value of error-message element. Line number 1.");
1218
1219 st->finished_correctly = true;
1220}
1221
1222static void
1223test_fracdigits_elem(void **state)
1224{
1225 struct state *st = *state;
1226 const char *data;
1227 struct lysp_type type = {};
1228
1229 /* valid value */
1230 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"10\"></fraction-digits>" ELEMENT_WRAPPER_END;
1231 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1232 assert_int_equal(type.fraction_digits, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001233 assert_true(type.flags & LYS_SET_FRDIGITS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001234
1235 /* invalid values */
1236 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"-1\"></fraction-digits>" ELEMENT_WRAPPER_END;
1237 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1238 logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1.");
1239
1240 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"02\"></fraction-digits>" ELEMENT_WRAPPER_END;
1241 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1242 logbuf_assert("Invalid value \"02\" of \"fraction-digits\". Line number 1.");
1243
1244 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"1p\"></fraction-digits>" ELEMENT_WRAPPER_END;
1245 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1246 logbuf_assert("Invalid value \"1p\" of \"fraction-digits\". Line number 1.");
1247
1248 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"19\"></fraction-digits>" ELEMENT_WRAPPER_END;
1249 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1250 logbuf_assert("Invalid value \"19\" of \"fraction-digits\". Line number 1.");
1251
1252 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"999999999999999999\"></fraction-digits>" ELEMENT_WRAPPER_END;
1253 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1254 logbuf_assert("Invalid value \"999999999999999999\" of \"fraction-digits\". Line number 1.");
1255
1256 st->finished_correctly = true;
1257}
1258
1259static void
1260test_iffeature_elem(void **state)
1261{
1262 struct state *st = *state;
1263 const char *data;
1264 const char **iffeatures = NULL;
1265
1266 data = ELEMENT_WRAPPER_START "<if-feature name=\"local-storage\"></if-feature>" ELEMENT_WRAPPER_END;
1267 assert_int_equal(test_element_helper(st, &data, &iffeatures, NULL, NULL, true), LY_SUCCESS);
1268 assert_string_equal(*iffeatures, "local-storage");
1269 FREE_STRING(st->ctx, *iffeatures);
1270 LY_ARRAY_FREE(iffeatures);
1271 iffeatures = NULL;
1272
1273 data = ELEMENT_WRAPPER_START "<if-feature/>" ELEMENT_WRAPPER_END;
1274 assert_int_equal(test_element_helper(st, &data, &iffeatures, NULL, NULL, false), LY_EVALID);
1275 logbuf_assert("Missing mandatory attribute name of if-feature element. Line number 1.");
1276 LY_ARRAY_FREE(iffeatures);
1277 iffeatures = NULL;
1278
1279 st->finished_correctly = true;
1280}
1281
1282static void
1283test_length_elem(void **state)
1284{
1285 struct state *st = *state;
1286 const char *data;
1287 struct lysp_type type = {};
1288
1289 /* max subelems */
1290 data = ELEMENT_WRAPPER_START
1291 "<length value=\"length-str\">"
1292 "<error-message><value>err-msg</value></error-message>"
1293 "<error-app-tag value=\"err-app-tag\"/>"
1294 "<description><text>desc</text></description>"
1295 "<reference><text>ref</text></reference>"
1296 "</length>"
1297 ELEMENT_WRAPPER_END;
1298 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1299 assert_string_equal(type.length->arg, "length-str");
1300 assert_string_equal(type.length->emsg, "err-msg");
1301 assert_string_equal(type.length->eapptag, "err-app-tag");
1302 assert_string_equal(type.length->dsc, "desc");
1303 assert_string_equal(type.length->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001304 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001305 lysp_type_free(st->ctx, &type);
1306 memset(&type, 0, sizeof(type));
1307
1308 /* min subelems */
1309 data = ELEMENT_WRAPPER_START
1310 "<length value=\"length-str\">"
1311 "</length>"
1312 ELEMENT_WRAPPER_END;
1313 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1314 assert_string_equal(type.length->arg, "length-str");
1315 lysp_type_free(st->ctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001316 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001317 memset(&type, 0, sizeof(type));
1318
1319 data = ELEMENT_WRAPPER_START "<length></length>" ELEMENT_WRAPPER_END;
1320 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1321 logbuf_assert("Missing mandatory attribute value of length element. Line number 1.");
1322 lysp_type_free(st->ctx, &type);
1323 memset(&type, 0, sizeof(type));
1324
1325 st->finished_correctly = true;
1326}
1327
1328static void
1329test_modifier_elem(void **state)
1330{
1331 struct state *st = *state;
1332 const char *data;
1333 const char *pat = lydict_insert(st->ctx, "\006pattern", 8);
1334
1335 data = ELEMENT_WRAPPER_START "<modifier value=\"invert-match\" />" ELEMENT_WRAPPER_END;
1336 assert_int_equal(test_element_helper(st, &data, &pat, NULL, NULL, true), LY_SUCCESS);
1337 assert_string_equal(pat, "\x015pattern");
1338 FREE_STRING(st->ctx, pat);
1339
1340 pat = lydict_insert(st->ctx, "\006pattern", 8);
1341 data = ELEMENT_WRAPPER_START "<modifier value=\"invert\" />" ELEMENT_WRAPPER_END;
1342 assert_int_equal(test_element_helper(st, &data, &pat, NULL, NULL, false), LY_EVALID);
1343 logbuf_assert("Invalid value \"invert\" of \"modifier\". Line number 1.");
1344 FREE_STRING(st->ctx, pat);
1345
1346 st->finished_correctly = true;
1347}
1348
1349static void
1350test_namespace_elem(void **state)
1351{
1352 struct state *st = *state;
1353 const char *data;
1354 const char *ns;
1355
1356 data = ELEMENT_WRAPPER_START "<namespace uri=\"ns\"/>" ELEMENT_WRAPPER_END;
1357 assert_int_equal(test_element_helper(st, &data, &ns, NULL, NULL, true), LY_SUCCESS);
1358 assert_string_equal(ns, "ns");
1359 FREE_STRING(st->ctx, ns);
1360
1361 data = ELEMENT_WRAPPER_START "<namespace/>" ELEMENT_WRAPPER_END;
1362 assert_int_equal(test_element_helper(st, &data, &ns, NULL, NULL, false), LY_EVALID);
1363 logbuf_assert("Missing mandatory attribute uri of namespace element. Line number 1.");
1364
1365 st->finished_correctly = true;
1366}
1367
1368static void
1369test_path_elem(void **state)
1370{
1371 struct state *st = *state;
1372 const char *data;
1373 struct lysp_type type = {};
1374
1375 data = ELEMENT_WRAPPER_START "<path value=\"path-val\"/>" ELEMENT_WRAPPER_END;
1376 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1377 assert_string_equal("path-val", type.path);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001378 assert_true(type.flags & LYS_SET_PATH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001379 lysp_type_free(st->ctx, &type);
1380
1381 st->finished_correctly = true;
1382}
1383
1384static void
1385test_pattern_elem(void **state)
1386{
1387 struct state *st = *state;
1388 const char *data;
1389 struct lysp_type type = {};
1390
1391 /* max subelems */
1392 data = ELEMENT_WRAPPER_START
1393 "<pattern value=\"super_pattern\">"
1394 "<modifier value=\"invert-match\"/>"
1395 "<error-message><value>err-msg-value</value></error-message>"
1396 "<error-app-tag value=\"err-app-tag-value\"/>"
1397 "<description><text>pattern-desc</text></description>"
1398 "<reference><text>pattern-ref</text></reference>"
1399 "</pattern>"
1400 ELEMENT_WRAPPER_END;
1401 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001402 assert_true(type.flags & LYS_SET_PATTERN);
David Sedlák8e7bda82019-07-16 17:57:50 +02001403 assert_string_equal(type.patterns->arg, "\x015super_pattern");
1404 assert_string_equal(type.patterns->dsc, "pattern-desc");
1405 assert_string_equal(type.patterns->eapptag, "err-app-tag-value");
1406 assert_string_equal(type.patterns->emsg, "err-msg-value");
1407 assert_string_equal(type.patterns->dsc, "pattern-desc");
1408 assert_string_equal(type.patterns->ref, "pattern-ref");
1409 lysp_type_free(st->ctx, &type);
1410 memset(&type, 0, sizeof(type));
1411
1412 /* min subelems */
1413 data = ELEMENT_WRAPPER_START "<pattern value=\"pattern\"> </pattern>" ELEMENT_WRAPPER_END;
1414 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1415 assert_string_equal(type.patterns->arg, "\x006pattern");
1416 lysp_type_free(st->ctx, &type);
1417 memset(&type, 0, sizeof(type));
1418
1419 st->finished_correctly = true;
1420}
1421
1422static void
1423test_value_position_elem(void **state)
1424{
1425 struct state *st = *state;
1426 const char *data;
1427 struct lysp_type_enum en = {};
1428
1429 /* valid values */
1430 data = ELEMENT_WRAPPER_START "<value value=\"55\" />" ELEMENT_WRAPPER_END;
1431 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1432 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001433 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001434 memset(&en, 0, sizeof(en));
1435
1436 data = ELEMENT_WRAPPER_START "<value value=\"-55\"/>" ELEMENT_WRAPPER_END;
1437 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1438 assert_int_equal(en.value, -55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001439 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001440 memset(&en, 0, sizeof(en));
1441
1442 data = ELEMENT_WRAPPER_START "<value value=\"0\"/>" ELEMENT_WRAPPER_END;
1443 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1444 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001445 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001446 memset(&en, 0, sizeof(en));
1447
1448 data = ELEMENT_WRAPPER_START "<value value=\"-0\"/>" ELEMENT_WRAPPER_END;
1449 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1450 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001451 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001452 memset(&en, 0, sizeof(en));
1453
1454 /* valid positions */
1455 data = ELEMENT_WRAPPER_START "<position value=\"55\" />" ELEMENT_WRAPPER_END;
1456 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1457 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001458 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001459 memset(&en, 0, sizeof(en));
1460
1461 data = ELEMENT_WRAPPER_START "<position value=\"0\" />" ELEMENT_WRAPPER_END;
1462 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1463 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001464 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001465 memset(&en, 0, sizeof(en));
1466
1467 /* invalid values */
1468 data = ELEMENT_WRAPPER_START "<value value=\"99999999999999999999999\"/>" ELEMENT_WRAPPER_END;
1469 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1470 logbuf_assert("Invalid value \"99999999999999999999999\" of \"value\". Line number 1.");
1471
1472 data = ELEMENT_WRAPPER_START "<value value=\"1k\"/>" ELEMENT_WRAPPER_END;
1473 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1474 logbuf_assert("Invalid value \"1k\" of \"value\". Line number 1.");
1475
David Sedlák69f01612019-07-17 11:41:08 +02001476 data = ELEMENT_WRAPPER_START "<value value=\"\"/>" ELEMENT_WRAPPER_END;
1477 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1478 logbuf_assert("Invalid value \"\" of \"value\". Line number 1.");
1479
David Sedlák8e7bda82019-07-16 17:57:50 +02001480 /*invalid positions */
1481 data = ELEMENT_WRAPPER_START "<position value=\"-5\"/>" ELEMENT_WRAPPER_END;
1482 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1483 logbuf_assert("Invalid value \"-5\" of \"position\". Line number 1.");
1484
1485 data = ELEMENT_WRAPPER_START "<position value=\"-0\"/>" ELEMENT_WRAPPER_END;
1486 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1487 logbuf_assert("Invalid value \"-0\" of \"position\". Line number 1.");
1488
1489 data = ELEMENT_WRAPPER_START "<position value=\"99999999999999999999\"/>" ELEMENT_WRAPPER_END;
1490 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1491 logbuf_assert("Invalid value \"99999999999999999999\" of \"position\". Line number 1.");
1492
David Sedlák69f01612019-07-17 11:41:08 +02001493 data = ELEMENT_WRAPPER_START "<position value=\"\"/>" ELEMENT_WRAPPER_END;
1494 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1495 logbuf_assert("Invalid value \"\" of \"position\". Line number 1.");
1496
1497 st->finished_correctly = true;
1498}
1499
1500static void
1501test_prefix_elem(void **state)
1502{
1503 struct state *st = *state;
1504 const char *data;
1505 const char *value = NULL;
1506
1507 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\"/>" ELEMENT_WRAPPER_END;
1508 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
1509 assert_string_equal(value, "pref");
1510 FREE_STRING(st->ctx, value);
1511
1512 st->finished_correctly = true;
1513}
1514
1515static void
1516test_range_elem(void **state)
1517{
1518 struct state *st = *state;
1519 const char *data;
1520 struct lysp_type type = {};
1521
1522 /* max subelems */
1523 data = ELEMENT_WRAPPER_START
1524 "<range value=\"range-str\">"
1525 "<error-message><value>err-msg</value></error-message>"
1526 "<error-app-tag value=\"err-app-tag\" />"
1527 "<description><text>desc</text></description>"
1528 "<reference><text>ref</text></reference>"
1529 "</range>"
1530 ELEMENT_WRAPPER_END;
1531 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1532 assert_string_equal(type.range->arg, "range-str");
1533 assert_string_equal(type.range->dsc, "desc");
1534 assert_string_equal(type.range->eapptag, "err-app-tag");
1535 assert_string_equal(type.range->emsg, "err-msg");
1536 assert_string_equal(type.range->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001537 assert_true(type.flags & LYS_SET_RANGE);
David Sedlák69f01612019-07-17 11:41:08 +02001538 lysp_type_free(st->ctx, &type);
1539 memset(&type, 0, sizeof(type));
1540
1541 /* min subelems */
1542 data = ELEMENT_WRAPPER_START "<range value=\"range-str\"/>" ELEMENT_WRAPPER_END;
1543 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1544 assert_string_equal(type.range->arg, "range-str");
1545 lysp_type_free(st->ctx, &type);
1546 memset(&type, 0, sizeof(type));
1547
1548 st->finished_correctly = true;
1549}
1550
1551static void
1552test_reqinstance_elem(void **state)
1553{
1554 struct state *st = *state;
1555 const char *data;
1556 struct lysp_type type = {};
1557
1558 data = ELEMENT_WRAPPER_START "<require-instance value=\"true\"/>" ELEMENT_WRAPPER_END;
1559 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1560 assert_int_equal(type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001561 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001562 memset(&type, 0, sizeof(type));
1563
1564 data = ELEMENT_WRAPPER_START "<require-instance value=\"false\"/>" ELEMENT_WRAPPER_END;
1565 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1566 assert_int_equal(type.require_instance, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001567 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001568 memset(&type, 0, sizeof(type));
1569
1570 data = ELEMENT_WRAPPER_START "<require-instance value=\"invalid\"/>" ELEMENT_WRAPPER_END;
1571 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1572 memset(&type, 0, sizeof(type));
1573 logbuf_assert("Invalid value \"invalid\" of \"require-instance\". Line number 1.");
1574
1575 st->finished_correctly = true;
1576}
1577
1578static void
1579test_revision_date_elem(void **state)
1580{
1581 struct state *st = *state;
1582 const char *data;
1583 char rev[LY_REV_SIZE];
1584
1585 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\"/>" ELEMENT_WRAPPER_END;
1586 assert_int_equal(test_element_helper(st, &data, rev, NULL, NULL, true), LY_SUCCESS);
1587 assert_string_equal(rev, "2000-01-01");
1588
1589 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-50-05\"/>" ELEMENT_WRAPPER_END;
1590 assert_int_equal(test_element_helper(st, &data, rev, NULL, NULL, false), LY_EVALID);
1591 logbuf_assert("Invalid value \"2000-50-05\" of \"revision-date\". Line number 1.");
1592
1593 st->finished_correctly = true;
1594}
1595
1596static void
1597test_unique_elem(void **state)
1598{
1599 struct state *st = *state;
1600 const char *data;
1601 const char **values = NULL;
1602
1603 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\"/>" ELEMENT_WRAPPER_END;
1604 assert_int_equal(test_element_helper(st, &data, &values, NULL, NULL, true), LY_SUCCESS);
1605 assert_string_equal(*values, "tag");
1606 FREE_STRING(st->ctx, *values);
1607 LY_ARRAY_FREE(values);
1608
1609 st->finished_correctly = true;
1610}
1611
1612static void
1613test_units_elem(void **state)
1614{
1615 struct state *st = *state;
1616 const char *data;
1617 const char *values = NULL;
1618
1619 data = ELEMENT_WRAPPER_START "<units name=\"name\"/>" ELEMENT_WRAPPER_END;
1620 assert_int_equal(test_element_helper(st, &data, &values, NULL, NULL, true), LY_SUCCESS);
1621 assert_string_equal(values, "name");
1622 FREE_STRING(st->ctx, values);
1623
1624 st->finished_correctly = true;
1625}
1626
1627static void
1628test_when_elem(void **state)
1629{
1630 struct state *st = *state;
1631 const char *data;
1632 struct lysp_when *when = NULL;
1633
1634 data = ELEMENT_WRAPPER_START
1635 "<when condition=\"cond\">"
1636 "<description><text>desc</text></description>"
1637 "<reference><text>ref</text></reference>"
1638 "</when>"
1639 ELEMENT_WRAPPER_END;
1640 assert_int_equal(test_element_helper(st, &data, &when, NULL, NULL, true), LY_SUCCESS);
1641 assert_string_equal(when->cond, "cond");
1642 assert_string_equal(when->dsc, "desc");
1643 assert_string_equal(when->ref, "ref");
1644 lysp_when_free(st->ctx, when);
1645 free(when);
1646 when = NULL;
1647
1648 data = ELEMENT_WRAPPER_START "<when condition=\"cond\" />" ELEMENT_WRAPPER_END;
1649 assert_int_equal(test_element_helper(st, &data, &when, NULL, NULL, true), LY_SUCCESS);
1650 assert_string_equal(when->cond, "cond");
1651 lysp_when_free(st->ctx, when);
1652 free(when);
1653 when = NULL;
1654
1655 st->finished_correctly = true;
1656}
1657
1658static void
1659test_yin_text_value_elem(void **state)
1660{
1661 struct state *st = *state;
1662 const char *data;
1663 const char *val;
1664
1665 data = ELEMENT_WRAPPER_START "<text>text</text>" ELEMENT_WRAPPER_END;
1666 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1667 assert_string_equal(val, "text");
1668 FREE_STRING(st->ctx, val);
1669
1670 data = "<error-message xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <value>text</value> </error-message>";
1671 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1672 assert_string_equal(val, "text");
1673 FREE_STRING(st->ctx, val);
1674
1675 data = ELEMENT_WRAPPER_START "<text></text>" ELEMENT_WRAPPER_END;
1676 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1677 assert_string_equal("", val);
1678 FREE_STRING(st->ctx, val);
1679
David Sedlák8e7bda82019-07-16 17:57:50 +02001680 st->finished_correctly = true;
1681}
David Sedlák32488102019-07-15 17:44:10 +02001682
David Sedlák374d2b32019-07-17 15:06:55 +02001683static void
1684test_type_elem(void **state)
1685{
1686 struct state *st = *state;
1687 const char *data;
1688 struct lysp_type type = {};
1689
1690 /* max subelems */
1691 data = ELEMENT_WRAPPER_START
1692 "<type name=\"type-name\">"
1693 "<base name=\"base-name\"/>"
1694 "<bit name=\"bit\"/>"
1695 "<enum name=\"enum\"/>"
1696 "<fraction-digits value=\"2\"/>"
1697 "<length value=\"length\"/>"
1698 "<path value=\"path\"/>"
1699 "<pattern value=\"pattern\"/>"
1700 "<range value=\"range\" />"
1701 "<require-instance value=\"true\"/>"
1702 "<type name=\"sub-type-name\"/>"
1703 "</type>"
1704 ELEMENT_WRAPPER_END;
1705 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1706 assert_string_equal(type.name, "type-name");
1707 assert_string_equal(*type.bases, "base-name");
1708 assert_string_equal(type.bits->name, "bit");
1709 assert_string_equal(type.enums->name, "enum");
1710 assert_int_equal(type.fraction_digits, 2);
1711 assert_string_equal(type.length->arg, "length");
1712 assert_string_equal(type.path, "path");
1713 assert_string_equal(type.patterns->arg, "\006pattern");
1714 assert_string_equal(type.range->arg, "range");
1715 assert_int_equal(type.require_instance, 1);
1716 assert_string_equal(type.types->name, "sub-type-name");
1717 lysp_type_free(st->ctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001718 assert_true(type.flags & LYS_SET_BASE);
1719 assert_true(type.flags & LYS_SET_BIT);
1720 assert_true(type.flags & LYS_SET_ENUM);
1721 assert_true(type.flags & LYS_SET_FRDIGITS);
1722 assert_true(type.flags & LYS_SET_LENGTH);
1723 assert_true(type.flags & LYS_SET_PATH);
1724 assert_true(type.flags & LYS_SET_PATTERN);
1725 assert_true(type.flags & LYS_SET_RANGE);
1726 assert_true(type.flags & LYS_SET_REQINST);
1727 assert_true(type.flags & LYS_SET_TYPE);
David Sedlák374d2b32019-07-17 15:06:55 +02001728 memset(&type, 0, sizeof(type));
1729
1730 /* min subelems */
1731 data = ELEMENT_WRAPPER_START "<type name=\"type-name\"/>" ELEMENT_WRAPPER_END;
1732 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1733 lysp_type_free(st->ctx, &type);
1734 memset(&type, 0, sizeof(type));
1735
1736 st->finished_correctly = true;
1737}
1738
David Sedlák1af868e2019-07-17 17:03:14 +02001739static void
1740test_max_elems_elem(void **state)
1741{
1742 struct state *st = *state;
1743 const char *data;
1744 struct lysp_node_list list = {};
1745 struct lysp_node_leaflist llist = {};
1746 struct lysp_refine refine = {};
1747
1748 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"unbounded\"/> </refine>";
1749 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
1750 assert_int_equal(refine.max, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001751 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001752
1753 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"5\"/> </list>";
1754 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, true), LY_SUCCESS);
1755 assert_int_equal(list.max, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001756 assert_true(list.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001757
1758 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"85\"/> </leaf-list>";
1759 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, true), LY_SUCCESS);
1760 assert_int_equal(llist.max, 85);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001761 assert_true(llist.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001762
1763 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"10\"/> </refine>";
1764 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
1765 assert_int_equal(refine.max, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001766 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001767
1768 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"0\"/> </list>";
1769 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1770 logbuf_assert("Invalid value \"0\" of \"max-elements\". Line number 1.");
1771
1772 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"-10\"/> </list>";
1773 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1774 logbuf_assert("Invalid value \"-10\" of \"max-elements\". Line number 1.");
1775
1776 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"k\"/> </list>";
1777 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1778 logbuf_assert("Invalid value \"k\" of \"max-elements\". Line number 1.");
1779
1780 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"u12\"/> </list>";
1781 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1782 logbuf_assert("Invalid value \"u12\" of \"max-elements\". Line number 1.");
1783
1784 st->finished_correctly = true;
1785}
1786
David Sedlák09e18c92019-07-18 11:17:11 +02001787static void
1788test_min_elems_elem(void **state)
1789{
1790 struct state *st = *state;
1791 const char *data;
1792 struct lysp_node_list list = {};
1793 struct lysp_node_leaflist llist = {};
1794 struct lysp_refine refine = {};
1795
1796 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"0\"/> </refine>";
1797 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
1798 assert_int_equal(refine.min, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001799 assert_true(refine.flags & LYS_SET_MIN);
David Sedlák09e18c92019-07-18 11:17:11 +02001800
1801 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"41\"/> </list>";
1802 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, true), LY_SUCCESS);
1803 assert_int_equal(list.min, 41);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001804 assert_true(list.flags & LYS_SET_MIN);
David Sedlák09e18c92019-07-18 11:17:11 +02001805
1806 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"50\"/> </leaf-list>";
1807 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, true), LY_SUCCESS);
1808 assert_int_equal(llist.min, 50);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001809 assert_true(llist.flags & LYS_SET_MIN);
David Sedlák09e18c92019-07-18 11:17:11 +02001810
1811 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"-5\"/> </leaf-list>";
1812 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1813 logbuf_assert("Value \"-5\" is out of \"min-elements\" bounds. Line number 1.");
1814
1815 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"99999999999999999\"/> </leaf-list>";
1816 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1817 logbuf_assert("Value \"99999999999999999\" is out of \"min-elements\" bounds. Line number 1.");
1818
1819 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"5k\"/> </leaf-list>";
1820 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1821 logbuf_assert("Invalid value \"5k\" of \"min-elements\". Line number 1.");
1822
1823 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"05\"/> </leaf-list>";
1824 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1825 logbuf_assert("Invalid value \"05\" of \"min-elements\". Line number 1.");
1826
1827 st->finished_correctly = true;
1828}
1829
David Sedláka2dad212019-07-18 12:45:19 +02001830static void
1831test_ordby_elem(void **state)
1832{
1833 struct state *st = *state;
1834 const char *data;
1835 uint16_t flags = 0;
1836
1837 data = ELEMENT_WRAPPER_START "<ordered-by value=\"system\"/>" ELEMENT_WRAPPER_END;
1838 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001839 assert_true(flags & LYS_ORDBY_SYSTEM);
David Sedláka2dad212019-07-18 12:45:19 +02001840
1841 data = ELEMENT_WRAPPER_START "<ordered-by value=\"user\"/>" ELEMENT_WRAPPER_END;
1842 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001843 assert_true(flags & LYS_ORDBY_USER);
David Sedláka2dad212019-07-18 12:45:19 +02001844
1845 data = ELEMENT_WRAPPER_START "<ordered-by value=\"inv\"/>" ELEMENT_WRAPPER_END;
1846 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
1847 logbuf_assert("Invalid value \"inv\" of \"ordered-by\". Line number 1.");
1848
1849 st->finished_correctly = true;
1850}
1851
David Sedlák8a83bbb2019-07-18 14:46:00 +02001852static void
1853test_any_elem(void **state)
1854{
1855 struct state *st = *state;
1856 const char *data;
1857 struct lysp_node *siblings = NULL;
1858 struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
1859 struct lysp_node_anydata *parsed = NULL;
1860
1861 /* anyxml max subelems */
1862 data = ELEMENT_WRAPPER_START
1863 "<anyxml name=\"any-name\">"
1864 "<config value=\"true\" />"
1865 "<description><text>desc</text></description>"
1866 "<if-feature name=\"feature\" />"
1867 "<mandatory value=\"true\" />"
1868 "<must condition=\"must-cond\" />"
1869 "<reference><text>ref</text></reference>"
1870 "<status value=\"deprecated\"/>"
1871 "<when condition=\"when-cond\"/>"
1872 "</anyxml>"
1873 ELEMENT_WRAPPER_END;
1874 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1875 parsed = (struct lysp_node_anydata *)siblings;
1876 assert_null(parsed->parent);
1877 assert_int_equal(parsed->nodetype, LYS_ANYXML);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001878 assert_true(parsed->flags & LYS_CONFIG_W);
1879 assert_true(parsed->flags & LYS_MAND_TRUE);
1880 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001881 assert_null(parsed->next);
1882 assert_string_equal(parsed->name, "any-name");
1883 assert_string_equal(parsed->dsc, "desc");
1884 assert_string_equal(parsed->ref, "ref");
1885 assert_string_equal(parsed->when->cond, "when-cond");
1886 assert_string_equal(*parsed->iffeatures, "feature");
1887 assert_null(parsed->exts);
1888 lysp_node_free(st->ctx, siblings);
1889 siblings = NULL;
1890
1891 /* anydata max subelems */
1892 data = ELEMENT_WRAPPER_START
1893 "<anydata name=\"any-name\">"
1894 "<config value=\"true\" />"
1895 "<description><text>desc</text></description>"
1896 "<if-feature name=\"feature\" />"
1897 "<mandatory value=\"true\" />"
1898 "<must condition=\"must-cond\" />"
1899 "<reference><text>ref</text></reference>"
1900 "<status value=\"deprecated\"/>"
1901 "<when condition=\"when-cond\"/>"
1902 "</anydata>"
1903 ELEMENT_WRAPPER_END;
1904 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1905 parsed = (struct lysp_node_anydata *)siblings;
1906 assert_null(parsed->parent);
1907 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001908 assert_true(parsed->flags & LYS_CONFIG_W);
1909 assert_true(parsed->flags & LYS_MAND_TRUE);
1910 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001911 assert_null(parsed->next);
1912 assert_string_equal(parsed->name, "any-name");
1913 assert_string_equal(parsed->dsc, "desc");
1914 assert_string_equal(parsed->ref, "ref");
1915 assert_string_equal(parsed->when->cond, "when-cond");
1916 assert_string_equal(*parsed->iffeatures, "feature");
1917 assert_null(parsed->exts);
1918 lysp_node_free(st->ctx, siblings);
1919 siblings = NULL;
1920
1921 /* min subelems */
1922 node_meta.parent = (void *)0x10;
1923 data = ELEMENT_WRAPPER_START "<anydata name=\"any-name\"> </anydata>" ELEMENT_WRAPPER_END;
1924 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1925 parsed = (struct lysp_node_anydata *)siblings;
1926 assert_ptr_equal(parsed->parent, node_meta.parent);
1927 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
1928 assert_null(parsed->next);
1929 assert_null(parsed->exts);
1930 lysp_node_free(st->ctx, siblings);
1931
1932 st->finished_correctly = true;
1933}
1934
David Sedlák203ca3a2019-07-18 15:26:25 +02001935static void
1936test_leaf_elem(void **state)
1937{
1938 struct state *st = *state;
1939 const char *data;
1940 struct lysp_node *siblings = NULL;
1941 struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
1942 struct lysp_node_leaf *parsed = NULL;
1943
1944 /* max elements */
1945 data = ELEMENT_WRAPPER_START
1946 "<leaf name=\"leaf\">"
1947 "<config value=\"true\" />"
1948 "<default value=\"def-val\"/>"
1949 "<description><text>desc</text></description>"
1950 "<if-feature name=\"feature\" />"
1951 "<mandatory value=\"true\" />"
1952 "<must condition=\"must-cond\" />"
1953 "<reference><text>ref</text></reference>"
1954 "<status value=\"deprecated\"/>"
1955 "<type name=\"type\"/>"
1956 "<units name=\"uni\"/>"
1957 "<when condition=\"when-cond\"/>"
1958 "</leaf>"
1959 ELEMENT_WRAPPER_END;
1960 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1961 parsed = (struct lysp_node_leaf *)siblings;
1962 assert_null(parsed->parent);
1963 assert_int_equal(parsed->nodetype, LYS_LEAF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001964 assert_true(parsed->flags & LYS_CONFIG_W);
1965 assert_true(parsed->flags & LYS_MAND_TRUE);
1966 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák203ca3a2019-07-18 15:26:25 +02001967 assert_null(parsed->next);
1968 assert_string_equal(parsed->name, "leaf");
1969 assert_string_equal(parsed->dsc, "desc");
1970 assert_string_equal(parsed->ref, "ref");
1971 assert_string_equal(parsed->when->cond, "when-cond");
1972 assert_string_equal(*parsed->iffeatures, "feature");
1973 assert_null(parsed->exts);
1974 assert_string_equal(parsed->musts->arg, "must-cond");
1975 assert_string_equal(parsed->type.name, "type");
1976 assert_string_equal(parsed->units, "uni");
1977 assert_string_equal(parsed->dflt, "def-val");
1978 lysp_node_free(st->ctx, siblings);
1979 siblings = NULL;
1980
1981 /* min elements */
1982 data = ELEMENT_WRAPPER_START "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>" ELEMENT_WRAPPER_END;
1983 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1984 parsed = (struct lysp_node_leaf *)siblings;
1985 assert_string_equal(parsed->name, "leaf");
1986 assert_string_equal(parsed->type.name, "type");
1987 lysp_node_free(st->ctx, siblings);
1988 siblings = NULL;
1989
1990 st->finished_correctly = true;
1991}
1992
David Sedlákc3da3ef2019-07-19 12:56:08 +02001993static void
1994test_leaf_list_elem(void **state)
1995{
1996 struct state *st = *state;
1997 const char *data;
1998 struct lysp_node *siblings = NULL;
1999 struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
2000 struct lysp_node_leaflist *parsed = NULL;
2001
2002 data = ELEMENT_WRAPPER_START
2003 "<leaf-list name=\"llist\">"
2004 "<config value=\"true\" />"
2005 "<default value=\"def-val0\"/>"
2006 "<default value=\"def-val1\"/>"
2007 "<description><text>desc</text></description>"
2008 "<if-feature name=\"feature\"/>"
2009 "<max-elements value=\"5\"/>"
2010 "<must condition=\"must-cond\"/>"
2011 "<ordered-by value=\"user\" />"
2012 "<reference><text>ref</text></reference>"
2013 "<status value=\"current\"/>"
2014 "<type name=\"type\"/>"
2015 "<units name=\"uni\"/>"
2016 "<when condition=\"when-cond\"/>"
2017 "</leaf-list>"
2018 ELEMENT_WRAPPER_END;
2019 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2020 parsed = (struct lysp_node_leaflist *)siblings;
2021 assert_string_equal(parsed->dflts[0], "def-val0");
2022 assert_string_equal(parsed->dflts[1], "def-val1");
2023 assert_string_equal(parsed->dsc, "desc");
2024 assert_string_equal(*parsed->iffeatures, "feature");
2025 assert_int_equal(parsed->max, 5);
2026 assert_string_equal(parsed->musts->arg, "must-cond");
2027 assert_string_equal(parsed->name, "llist");
2028 assert_null(parsed->next);
2029 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2030 assert_null(parsed->parent);
2031 assert_string_equal(parsed->ref, "ref");
2032 assert_string_equal(parsed->type.name, "type");
2033 assert_string_equal(parsed->units, "uni");
2034 assert_string_equal(parsed->when->cond, "when-cond");
2035 assert_true(parsed->flags & LYS_CONFIG_W);
2036 assert_true(parsed->flags & LYS_ORDBY_USER);
2037 assert_true(parsed->flags & LYS_STATUS_CURR);
2038 lysp_node_free(st->ctx, siblings);
2039 siblings = NULL;
2040
2041 data = ELEMENT_WRAPPER_START
2042 "<leaf-list name=\"llist\">"
2043 "<config value=\"true\" />"
2044 "<description><text>desc</text></description>"
2045 "<if-feature name=\"feature\"/>"
2046 "<min-elements value=\"5\"/>"
2047 "<must condition=\"must-cond\"/>"
2048 "<ordered-by value=\"user\" />"
2049 "<reference><text>ref</text></reference>"
2050 "<status value=\"current\"/>"
2051 "<type name=\"type\"/>"
2052 "<units name=\"uni\"/>"
2053 "<when condition=\"when-cond\"/>"
2054 "</leaf-list>"
2055 ELEMENT_WRAPPER_END;
2056 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2057 parsed = (struct lysp_node_leaflist *)siblings;
2058 assert_string_equal(parsed->dsc, "desc");
2059 assert_string_equal(*parsed->iffeatures, "feature");
2060 assert_int_equal(parsed->min, 5);
2061 assert_string_equal(parsed->musts->arg, "must-cond");
2062 assert_string_equal(parsed->name, "llist");
2063 assert_null(parsed->next);
2064 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2065 assert_null(parsed->parent);
2066 assert_string_equal(parsed->ref, "ref");
2067 assert_string_equal(parsed->type.name, "type");
2068 assert_string_equal(parsed->units, "uni");
2069 assert_string_equal(parsed->when->cond, "when-cond");
2070 assert_true(parsed->flags & LYS_CONFIG_W);
2071 assert_true(parsed->flags & LYS_ORDBY_USER);
2072 assert_true(parsed->flags & LYS_STATUS_CURR);
2073 lysp_node_free(st->ctx, siblings);
2074 siblings = NULL;
2075
2076 data = ELEMENT_WRAPPER_START
2077 "<leaf-list name=\"llist\">"
2078 "<config value=\"true\" />"
2079 "<description><text>desc</text></description>"
2080 "<if-feature name=\"feature\"/>"
2081 "<max-elements value=\"15\"/>"
2082 "<min-elements value=\"5\"/>"
2083 "<must condition=\"must-cond\"/>"
2084 "<ordered-by value=\"user\" />"
2085 "<reference><text>ref</text></reference>"
2086 "<status value=\"current\"/>"
2087 "<type name=\"type\"/>"
2088 "<units name=\"uni\"/>"
2089 "<when condition=\"when-cond\"/>"
2090 "</leaf-list>"
2091 ELEMENT_WRAPPER_END;
2092 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2093 parsed = (struct lysp_node_leaflist *)siblings;
2094 assert_string_equal(parsed->dsc, "desc");
2095 assert_string_equal(*parsed->iffeatures, "feature");
2096 assert_int_equal(parsed->min, 5);
2097 assert_int_equal(parsed->max, 15);
2098 assert_string_equal(parsed->musts->arg, "must-cond");
2099 assert_string_equal(parsed->name, "llist");
2100 assert_null(parsed->next);
2101 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2102 assert_null(parsed->parent);
2103 assert_string_equal(parsed->ref, "ref");
2104 assert_string_equal(parsed->type.name, "type");
2105 assert_string_equal(parsed->units, "uni");
2106 assert_string_equal(parsed->when->cond, "when-cond");
2107 assert_true(parsed->flags & LYS_CONFIG_W);
2108 assert_true(parsed->flags & LYS_ORDBY_USER);
2109 assert_true(parsed->flags & LYS_STATUS_CURR);
2110 lysp_node_free(st->ctx, siblings);
2111 siblings = NULL;
2112
2113 data = ELEMENT_WRAPPER_START
2114 "<leaf-list name=\"llist\">"
2115 "<type name=\"type\"/>"
2116 "</leaf-list>"
2117 ELEMENT_WRAPPER_END;
2118 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2119 parsed = (struct lysp_node_leaflist *)siblings;
2120 assert_string_equal(parsed->name, "llist");
2121 assert_string_equal(parsed->type.name, "type");
2122 lysp_node_free(st->ctx, siblings);
2123 siblings = NULL;
2124
2125 /* invalid combinations */
2126 data = ELEMENT_WRAPPER_START
2127 "<leaf-list name=\"llist\">"
2128 "<max-elements value=\"5\"/>"
2129 "<min-elements value=\"15\"/>"
2130 "<type name=\"type\"/>"
2131 "</leaf-list>"
2132 ELEMENT_WRAPPER_END;
2133 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
2134 logbuf_assert("Invalid combination of min-elements and max-elements: min value 15 is bigger than the max value 5. Line number 1.");
2135 lysp_node_free(st->ctx, siblings);
2136 siblings = NULL;
2137
2138 data = ELEMENT_WRAPPER_START
2139 "<leaf-list name=\"llist\">"
2140 "<default value=\"def-val1\"/>"
2141 "<min-elements value=\"15\"/>"
2142 "<type name=\"type\"/>"
2143 "</leaf-list>"
2144 ELEMENT_WRAPPER_END;
2145 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
2146 logbuf_assert("Invalid combination of keywords \"min-elements\" and \"default\" as substatements of \"leaf-list\". Line number 1.");
2147 lysp_node_free(st->ctx, siblings);
2148 siblings = NULL;
2149
2150 data = ELEMENT_WRAPPER_START
2151 "<leaf-list name=\"llist\">"
2152 "</leaf-list>"
2153 ELEMENT_WRAPPER_END;
2154 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
2155 logbuf_assert("Missing mandatory subelement type of leaf-list element. Line number 1.");
2156 lysp_node_free(st->ctx, siblings);
2157 siblings = NULL;
2158
2159 st->finished_correctly = true;
2160}
2161
David Sedlákcb39f642019-07-19 13:19:55 +02002162static void
2163test_presence_elem(void **state)
2164{
2165 struct state *st = *state;
2166 const char *data;
2167 const char *val;
2168
2169 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\"/>" ELEMENT_WRAPPER_END;
2170 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2171 assert_string_equal(val, "presence-val");
2172 FREE_STRING(st->ctx, val);
2173
2174 data = ELEMENT_WRAPPER_START "<presence/>" ELEMENT_WRAPPER_END;
2175 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
2176 logbuf_assert("Missing mandatory attribute value of presence element. Line number 1.");
2177
2178 st->finished_correctly = true;
2179}
2180
David Sedlák12470a82019-07-19 13:44:36 +02002181static void
2182test_key_elem(void **state)
2183{
2184 struct state *st = *state;
2185 const char *data;
2186 const char *val;
2187
2188 data = ELEMENT_WRAPPER_START "<key value=\"key-value\"/>" ELEMENT_WRAPPER_END;
2189 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2190 assert_string_equal(val, "key-value");
2191 FREE_STRING(st->ctx, val);
2192
2193 data = ELEMENT_WRAPPER_START "<key/>" ELEMENT_WRAPPER_END;
2194 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
2195 logbuf_assert("Missing mandatory attribute value of key element. Line number 1.");
2196
2197 st->finished_correctly = true;
2198}
2199
David Sedlák04e17b22019-07-19 15:29:48 +02002200static void
2201test_typedef_elem(void **state)
2202{
2203 struct state *st = *state;
2204 const char *data;
2205 struct lysp_tpdf *tpdfs = NULL;
2206 struct typedef_meta typdef_meta = {NULL, &tpdfs};
2207
2208 data = ELEMENT_WRAPPER_START
2209 "<typedef name=\"tpdf-name\">"
2210 "<default value=\"def-val\"/>"
2211 "<description><text>desc-text</text></description>"
2212 "<reference><text>ref-text</text></reference>"
2213 "<status value=\"current\"/>"
2214 "<type name=\"type\"/>"
2215 "<units name=\"uni\"/>"
2216 "</typedef>"
2217 ELEMENT_WRAPPER_END;
2218 assert_int_equal(test_element_helper(st, &data, &typdef_meta, NULL, NULL, true), LY_SUCCESS);
2219 assert_string_equal(tpdfs[0].dflt, "def-val");
2220 assert_string_equal(tpdfs[0].dsc, "desc-text");
2221 assert_null(tpdfs[0].exts);
2222 assert_string_equal(tpdfs[0].name, "tpdf-name");
2223 assert_string_equal(tpdfs[0].ref, "ref-text");
2224 assert_string_equal(tpdfs[0].type.name, "type");
2225 assert_string_equal(tpdfs[0].units, "uni");
2226 assert_true(tpdfs[0].flags & LYS_STATUS_CURR);
2227 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2228 tpdfs = NULL;
2229
2230 data = ELEMENT_WRAPPER_START
2231 "<typedef name=\"tpdf-name\">"
2232 "<type name=\"type\"/>"
2233 "</typedef>"
2234 ELEMENT_WRAPPER_END;
2235 assert_int_equal(test_element_helper(st, &data, &typdef_meta, NULL, NULL, true), LY_SUCCESS);
2236 assert_string_equal(tpdfs[0].name, "tpdf-name");
2237 assert_string_equal(tpdfs[0].type.name, "type");
2238 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2239 tpdfs = NULL;
2240
2241 st->finished_correctly = true;
2242}
2243
David Sedlákd2d676a2019-07-22 11:28:19 +02002244static void
2245test_refine_elem(void **state)
2246{
2247 struct state *st = *state;
2248 const char *data;
2249 struct lysp_refine *refines = NULL;
2250
2251 /* max subelems */
2252 data = ELEMENT_WRAPPER_START
2253 "<refine target-node=\"target\">"
2254 "<if-feature name=\"feature\" />"
2255 "<must condition=\"cond\" />"
2256 "<presence value=\"presence\" />"
2257 "<default value=\"def\" />"
2258 "<config value=\"true\" />"
2259 "<mandatory value=\"true\" />"
2260 "<min-elements value=\"10\" />"
2261 "<max-elements value=\"20\" />"
2262 "<description><text>desc</text></description>"
2263 "<reference><text>ref</text></reference>"
2264 "</refine>"
2265 ELEMENT_WRAPPER_END;
2266 assert_int_equal(test_element_helper(st, &data, &refines, NULL, NULL, true), LY_SUCCESS);
2267 assert_string_equal(refines->nodeid, "target");
2268 assert_string_equal(*refines->dflts, "def");
2269 assert_string_equal(refines->dsc, "desc");
2270 assert_null(refines->exts);
2271 assert_true(refines->flags & LYS_CONFIG_W);
2272 assert_true(refines->flags & LYS_MAND_TRUE);
2273 assert_string_equal(*refines->iffeatures, "feature");
2274 assert_int_equal(refines->max, 20);
2275 assert_int_equal(refines->min, 10);
2276 assert_string_equal(refines->musts->arg, "cond");
2277 assert_string_equal(refines->presence, "presence");
2278 assert_string_equal(refines->ref, "ref");
2279 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2280 refines = NULL;
2281
2282 /* min subelems */
2283 data = ELEMENT_WRAPPER_START "<refine target-node=\"target\" />" ELEMENT_WRAPPER_END;
2284 assert_int_equal(test_element_helper(st, &data, &refines, NULL, NULL, true), LY_SUCCESS);
2285 assert_string_equal(refines->nodeid, "target");
2286 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2287 refines = NULL;
2288
2289 st->finished_correctly = true;
2290}
2291
David Sedlák0d6de5a2019-07-22 13:25:44 +02002292static void
2293test_uses_elem(void **state)
2294{
2295 struct state *st = *state;
2296 const char *data;
2297 struct lysp_node *siblings = NULL;
2298 struct tree_node_meta node_meta = {NULL, &siblings};
2299 struct lysp_node_uses *parsed = NULL;
2300
2301 /* max subelems */
2302 data = ELEMENT_WRAPPER_START
2303 "<uses name=\"uses-name\">"
2304 "<when condition=\"cond\" />"
2305 "<if-feature name=\"feature\" />"
2306 "<status value=\"obsolete\" />"
2307 "<description><text>desc</text></description>"
2308 "<reference><text>ref</text></reference>"
2309 "<refine target-node=\"target\"/>"
2310 /* TODO add uses-augment-stmt instance */
2311 "</uses>"
2312 ELEMENT_WRAPPER_END;
2313 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2314 parsed = (struct lysp_node_uses *)&siblings[0];
2315 assert_string_equal(parsed->name, "uses-name");
2316 assert_string_equal(parsed->dsc, "desc");
2317 assert_null(parsed->exts);
2318 assert_true(parsed->flags & LYS_STATUS_OBSLT);
2319 assert_string_equal(*parsed->iffeatures, "feature");
2320 assert_null(parsed->next);
2321 assert_int_equal(parsed->nodetype, LYS_USES);
2322 assert_null(parsed->parent);
2323 assert_string_equal(parsed->ref, "ref");
2324 assert_string_equal(parsed->refines->nodeid, "target");
2325 assert_string_equal(parsed->when->cond, "cond");
2326 lysp_node_free(st->ctx, siblings);
2327 siblings = NULL;
2328
2329 /* min subelems */
2330 data = ELEMENT_WRAPPER_START "<uses name=\"uses-name\"/>" ELEMENT_WRAPPER_END;
2331 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2332 assert_string_equal(siblings[0].name, "uses-name");
2333 lysp_node_free(st->ctx, siblings);
2334 siblings = NULL;
2335
2336 st->finished_correctly = true;
2337}
2338
David Sedlákaa854b02019-07-22 14:17:10 +02002339static void
2340test_revision_elem(void **state)
2341{
2342 struct state *st = *state;
2343 const char *data;
2344 struct lysp_revision *revs = NULL;
2345
2346 /* max subelems */
2347 data = ELEMENT_WRAPPER_START
2348 "<revision date=\"2018-12-25\">"
2349 "<description><text>desc</text></description>"
2350 "<reference><text>ref</text></reference>"
2351 "</revision>"
2352 ELEMENT_WRAPPER_END;
2353 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, true), LY_SUCCESS);
2354 assert_string_equal(revs->date, "2018-12-25");
2355 assert_string_equal(revs->dsc, "desc");
2356 assert_string_equal(revs->ref, "ref");
2357 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2358 revs = NULL;
2359
2360 /* min subelems */
2361 data = ELEMENT_WRAPPER_START "<revision date=\"2005-05-05\" />" ELEMENT_WRAPPER_END;
2362 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, true), LY_SUCCESS);
2363 assert_string_equal(revs->date, "2005-05-05");
2364 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2365 revs = NULL;
2366
2367 /* invalid value */
2368 data = ELEMENT_WRAPPER_START "<revision date=\"05-05-2005\" />" ELEMENT_WRAPPER_END;
2369 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, false), LY_EVALID);
2370 logbuf_assert("Invalid value \"05-05-2005\" of \"revision\". Line number 1.");
2371 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2372 revs = NULL;
2373
2374 st->finished_correctly = true;
2375}
2376
David Sedlák0c2bab92019-07-22 15:33:19 +02002377static void
2378test_include_elem(void **state)
2379{
2380 struct state *st = *state;
2381 const char *data;
2382 struct lysp_include *includes = NULL;
2383 struct include_meta inc_meta = {"module-name", &includes};
2384
2385 /* max subelems */
2386 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2387 data = ELEMENT_WRAPPER_START
2388 "<include module=\"mod\">"
2389 "<description><text>desc</text></description>"
2390 "<reference><text>ref</text></reference>"
2391 "<revision-date date=\"1999-09-09\"/>"
2392 "</include>"
2393 ELEMENT_WRAPPER_END;
2394 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, true), LY_SUCCESS);
2395 assert_string_equal(includes->name, "mod");
2396 assert_string_equal(includes->dsc, "desc");
2397 assert_string_equal(includes->ref, "ref");
2398 assert_null(includes->exts);
2399 assert_string_equal(includes->rev, "1999-09-09");
2400 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2401 includes = NULL;
2402
2403 /* min subelems */
2404 data = ELEMENT_WRAPPER_START "<include module=\"mod\"/>" ELEMENT_WRAPPER_END;
2405 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, true), LY_SUCCESS);
2406 assert_string_equal(includes->name, "mod");
2407 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2408 includes = NULL;
2409
2410 /* invalid combinations */
2411 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2412 data = ELEMENT_WRAPPER_START
2413 "<include module=\"mod\">"
2414 "<description><text>desc</text></description>"
2415 "<revision-date date=\"1999-09-09\"/>"
2416 "</include>"
2417 ELEMENT_WRAPPER_END;
2418 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, false), LY_EVALID);
2419 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.");
2420 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2421 includes = NULL;
2422
2423 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2424 data = ELEMENT_WRAPPER_START
2425 "<include module=\"mod\">"
2426 "<reference><text>ref</text></reference>"
2427 "<revision-date date=\"1999-09-09\"/>"
2428 "</include>"
2429 ELEMENT_WRAPPER_END;
2430 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, false), LY_EVALID);
2431 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.");
2432 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2433 includes = NULL;
2434
2435 st->finished_correctly = true;
2436}
2437
David Sedlák5e13dea2019-07-22 16:06:45 +02002438static void
2439test_feature_elem(void **state)
2440{
2441 struct state *st = *state;
2442 const char *data;
2443 struct lysp_feature *features = NULL;
2444
2445 /* max subelems */
2446 data = ELEMENT_WRAPPER_START
2447 "<feature name=\"feature-name\">"
2448 "<if-feature name=\"iff\"/>"
2449 "<status value=\"deprecated\"/>"
2450 "<description><text>desc</text></description>"
2451 "<reference><text>ref</text></reference>"
2452 "</feature>"
2453 ELEMENT_WRAPPER_END;
2454 assert_int_equal(test_element_helper(st, &data, &features, NULL, NULL, true), LY_SUCCESS);
2455 assert_string_equal(features->name, "feature-name");
2456 assert_string_equal(features->dsc, "desc");
2457 assert_null(features->exts);
2458 assert_true(features->flags & LYS_STATUS_DEPRC);
2459 assert_string_equal(*features->iffeatures, "iff");
2460 assert_string_equal(features->ref, "ref");
2461 FREE_ARRAY(st->ctx, features, lysp_feature_free);
2462 features = NULL;
2463
2464 /* min subelems */
2465 data = ELEMENT_WRAPPER_START "<feature name=\"feature-name\"/>" ELEMENT_WRAPPER_END;
2466 assert_int_equal(test_element_helper(st, &data, &features, NULL, NULL, true), LY_SUCCESS);
2467 assert_string_equal(features->name, "feature-name");
2468 FREE_ARRAY(st->ctx, features, lysp_feature_free);
2469 features = NULL;
2470
2471 st->finished_correctly = true;
2472}
2473
David Sedlák28794f22019-07-22 16:45:00 +02002474static void
2475test_identity_elem(void **state)
2476{
2477 struct state *st = *state;
2478 const char *data;
2479 struct lysp_ident *identities = NULL;
2480
2481 /* max subelems */
2482 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2483 data = ELEMENT_WRAPPER_START
2484 "<identity name=\"ident-name\">"
2485 "<if-feature name=\"iff\"/>"
2486 "<base name=\"base-name\"/>"
2487 "<status value=\"deprecated\"/>"
2488 "<description><text>desc</text></description>"
2489 "<reference><text>ref</text></reference>"
2490 "</identity>"
2491 ELEMENT_WRAPPER_END;
2492 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, true), LY_SUCCESS);
2493 assert_string_equal(identities->name, "ident-name");
2494 assert_string_equal(*identities->bases, "base-name");
2495 assert_string_equal(*identities->iffeatures, "iff");
2496 assert_string_equal(identities->dsc, "desc");
2497 assert_string_equal(identities->ref, "ref");
2498 assert_true(identities->flags & LYS_STATUS_DEPRC);
2499 assert_null(identities->exts);
2500 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2501 identities = NULL;
2502
2503 /* min subelems */
2504 data = ELEMENT_WRAPPER_START "<identity name=\"ident-name\" />" ELEMENT_WRAPPER_END;
2505 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, true), LY_SUCCESS);
2506 assert_string_equal(identities->name, "ident-name");
2507 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2508 identities = NULL;
2509
2510 /* invalid */
2511 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2512 data = ELEMENT_WRAPPER_START
2513 "<identity name=\"ident-name\">"
2514 "<if-feature name=\"iff\"/>"
2515 "</identity>"
2516 ELEMENT_WRAPPER_END;
2517 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, false), LY_EVALID);
2518 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.");
2519 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2520 identities = NULL;
2521
2522 st->finished_correctly = true;
2523}
2524
David Sedlákaf536aa2019-07-23 13:42:23 +02002525static void
2526test_list_elem(void **state)
2527{
2528 struct state *st = *state;
2529 const char *data;
2530 struct lysp_node *siblings = NULL;
2531 struct tree_node_meta node_meta = {NULL, &siblings};
2532 struct lysp_node_list *parsed = NULL;
2533
2534 /* max subelems */
2535 data = ELEMENT_WRAPPER_START
2536 "<list name=\"list-name\">"
2537 "<when condition=\"when\"/>"
2538 "<if-feature name=\"iff\"/>"
2539 "<must condition=\"must-cond\"/>"
2540 "<key value=\"key\"/>"
2541 "<unique tag=\"utag\"/>"
2542 "<config value=\"true\"/>"
2543 "<min-elements value=\"10\"/>"
2544 "<ordered-by value=\"user\"/>"
2545 "<status value=\"deprecated\"/>"
2546 "<description><text>desc</text></description>"
2547 "<reference><text>ref</text></reference>"
2548 "<anydata name=\"anyd\"/>"
2549 "<anyxml name=\"anyx\"/>"
2550 // "<choice name=\"choice\"/>"
2551 // "<action name=\"action\"/>"
2552 // "<container name=\"cont\"/>"
2553 // "<grouping name=\"grp\"/>"
2554 // "<notification name=\"notf\"/>"
2555 "<leaf name=\"leaf\"/>"
2556 "<leaf-list name=\"llist\"/>"
2557 "<list name=\"sub-list\"/>"
2558 "<typedef name=\"tpdf\"/>"
2559 "<uses name=\"uses-name\"/>"
2560 "</list>"
2561 ELEMENT_WRAPPER_END;
2562 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2563 parsed = (struct lysp_node_list *)&siblings[0];
2564 assert_string_equal(parsed->dsc, "desc");
2565 assert_string_equal(parsed->child->name, "anyd");
2566 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2567 assert_string_equal(parsed->child->next->name, "anyx");
2568 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2569 // assert_string_equal(parsed->child->next->next->name, "choice");
2570 // assert_int_equal(parsed->child->next->next, LYS_CHOICE);
2571 // assert_string_equal(parsed->actions->name, "action");
2572 // assert_string_equal(parsed->child->next->next->name, "cont");
2573 // assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
2574 // assert_string_equal(parsed->groupings->name, "grp");
2575 // assert_string_equal(parsed->notifs->name, "notf");
2576 assert_null(parsed->exts);
2577 assert_true(parsed->flags & LYS_ORDBY_USER);
2578 assert_true(parsed->flags & LYS_STATUS_DEPRC);
2579 assert_true(parsed->flags & LYS_CONFIG_W);
2580 assert_string_equal(*parsed->iffeatures, "iff");
2581 assert_string_equal(parsed->key, "key");
2582 assert_int_equal(parsed->min, 10);
2583 assert_string_equal(parsed->musts->arg, "must-cond");
2584 assert_string_equal(parsed->name, "list-name");
2585 assert_null(parsed->next);
2586 assert_int_equal(parsed->nodetype, LYS_LIST);
2587 assert_null(parsed->parent);
2588 assert_string_equal(parsed->ref, "ref");
2589 assert_string_equal(parsed->typedefs->name, "tpdf");
2590 assert_string_equal(*parsed->uniques, "utag");
2591 assert_string_equal(parsed->when->cond, "when");
2592 lysp_node_free(st->ctx, siblings);
2593 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
2594 siblings = NULL;
2595
2596 /* min subelems */
2597 data = ELEMENT_WRAPPER_START "<list name=\"list-name\" />" ELEMENT_WRAPPER_END;
2598 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2599 parsed = (struct lysp_node_list *)&siblings[0];
2600 assert_string_equal(parsed->name, "list-name");
2601 lysp_node_free(st->ctx, siblings);
2602 siblings = NULL;
2603
2604 st->finished_correctly = true;
2605}
2606
David Sedlák3b4db242018-10-19 16:11:01 +02002607int
2608main(void)
2609{
2610
2611 const struct CMUnitTest tests[] = {
David Sedlák392af4f2019-06-04 16:02:42 +02002612 cmocka_unit_test_setup_teardown(test_yin_parse_module, setup_f, teardown_f),
David Sedlák8f7a1172019-06-20 14:42:18 +02002613 cmocka_unit_test_setup_teardown(test_yin_match_keyword, setup_f, teardown_f),
David Sedlákb1a78352019-06-28 16:16:29 +02002614 cmocka_unit_test_setup_teardown(test_yin_parse_element_generic, setup_f, teardown_f),
2615 cmocka_unit_test_setup_teardown(test_yin_parse_extension_instance, setup_f, teardown_f),
David Sedlák555c7202019-07-04 12:14:12 +02002616 cmocka_unit_test_setup_teardown(test_yin_parse_content, setup_f, teardown_f),
David Sedlák4a650532019-07-10 11:55:18 +02002617 cmocka_unit_test_setup_teardown(test_validate_value, setup_f, teardown_f),
David Sedlák32488102019-07-15 17:44:10 +02002618
David Sedlák8e7bda82019-07-16 17:57:50 +02002619 cmocka_unit_test(test_yin_match_argument_name),
David Sedlák32488102019-07-15 17:44:10 +02002620 cmocka_unit_test_setup_teardown(test_enum_bit_elem, setup_element_test, teardown_element_test),
2621 cmocka_unit_test_setup_teardown(test_meta_elem, setup_element_test, teardown_element_test),
2622 cmocka_unit_test_setup_teardown(test_import_elem, setup_element_test, teardown_element_test),
2623 cmocka_unit_test_setup_teardown(test_status_elem, setup_element_test, teardown_element_test),
2624 cmocka_unit_test_setup_teardown(test_ext_elem, setup_element_test, teardown_element_test),
2625 cmocka_unit_test_setup_teardown(test_yin_element_elem, setup_element_test, teardown_element_test),
2626 cmocka_unit_test_setup_teardown(test_yangversion_elem, setup_element_test, teardown_element_test),
2627 cmocka_unit_test_setup_teardown(test_mandatory_elem, setup_element_test, teardown_element_test),
David Sedlák8e7bda82019-07-16 17:57:50 +02002628 cmocka_unit_test_setup_teardown(test_argument_elem, setup_element_test, teardown_element_test),
2629 cmocka_unit_test_setup_teardown(test_base_elem, setup_element_test, teardown_element_test),
2630 cmocka_unit_test_setup_teardown(test_belongsto_elem, setup_element_test, teardown_element_test),
2631 cmocka_unit_test_setup_teardown(test_config_elem, setup_element_test, teardown_element_test),
2632 cmocka_unit_test_setup_teardown(test_default_elem, setup_element_test, teardown_element_test),
2633 cmocka_unit_test_setup_teardown(test_err_app_tag_elem, setup_element_test, teardown_element_test),
2634 cmocka_unit_test_setup_teardown(test_err_msg_elem, setup_element_test, teardown_element_test),
2635 cmocka_unit_test_setup_teardown(test_fracdigits_elem, setup_element_test, teardown_element_test),
2636 cmocka_unit_test_setup_teardown(test_iffeature_elem, setup_element_test, teardown_element_test),
2637 cmocka_unit_test_setup_teardown(test_length_elem, setup_element_test, teardown_element_test),
2638 cmocka_unit_test_setup_teardown(test_modifier_elem, setup_element_test, teardown_element_test),
2639 cmocka_unit_test_setup_teardown(test_namespace_elem, setup_element_test, teardown_element_test),
2640 cmocka_unit_test_setup_teardown(test_path_elem, setup_element_test, teardown_element_test),
2641 cmocka_unit_test_setup_teardown(test_pattern_elem, setup_element_test, teardown_element_test),
2642 cmocka_unit_test_setup_teardown(test_value_position_elem, setup_element_test, teardown_element_test),
David Sedlák69f01612019-07-17 11:41:08 +02002643 cmocka_unit_test_setup_teardown(test_prefix_elem, setup_element_test, teardown_element_test),
2644 cmocka_unit_test_setup_teardown(test_range_elem, setup_element_test, teardown_element_test),
2645 cmocka_unit_test_setup_teardown(test_reqinstance_elem, setup_element_test, teardown_element_test),
2646 cmocka_unit_test_setup_teardown(test_revision_date_elem, setup_element_test, teardown_element_test),
2647 cmocka_unit_test_setup_teardown(test_unique_elem, setup_element_test, teardown_element_test),
2648 cmocka_unit_test_setup_teardown(test_units_elem, setup_element_test, teardown_element_test),
2649 cmocka_unit_test_setup_teardown(test_when_elem, setup_element_test, teardown_element_test),
2650 cmocka_unit_test_setup_teardown(test_yin_text_value_elem, setup_element_test, teardown_element_test),
David Sedlák374d2b32019-07-17 15:06:55 +02002651 cmocka_unit_test_setup_teardown(test_type_elem, setup_element_test, teardown_element_test),
David Sedlák1af868e2019-07-17 17:03:14 +02002652 cmocka_unit_test_setup_teardown(test_max_elems_elem, setup_element_test, teardown_element_test),
David Sedlák09e18c92019-07-18 11:17:11 +02002653 cmocka_unit_test_setup_teardown(test_min_elems_elem, setup_element_test, teardown_element_test),
David Sedláka2dad212019-07-18 12:45:19 +02002654 cmocka_unit_test_setup_teardown(test_ordby_elem, setup_element_test, teardown_element_test),
David Sedlák8a83bbb2019-07-18 14:46:00 +02002655 cmocka_unit_test_setup_teardown(test_any_elem, setup_element_test, teardown_element_test),
David Sedlák203ca3a2019-07-18 15:26:25 +02002656 cmocka_unit_test_setup_teardown(test_leaf_elem, setup_element_test, teardown_element_test),
David Sedlákc3da3ef2019-07-19 12:56:08 +02002657 cmocka_unit_test_setup_teardown(test_leaf_list_elem, setup_element_test, teardown_element_test),
David Sedlákcb39f642019-07-19 13:19:55 +02002658 cmocka_unit_test_setup_teardown(test_presence_elem, setup_element_test, teardown_element_test),
David Sedlák12470a82019-07-19 13:44:36 +02002659 cmocka_unit_test_setup_teardown(test_key_elem, setup_element_test, teardown_element_test),
David Sedlák04e17b22019-07-19 15:29:48 +02002660 cmocka_unit_test_setup_teardown(test_typedef_elem, setup_element_test, teardown_element_test),
David Sedlákd2d676a2019-07-22 11:28:19 +02002661 cmocka_unit_test_setup_teardown(test_refine_elem, setup_element_test, teardown_element_test),
David Sedlák0d6de5a2019-07-22 13:25:44 +02002662 cmocka_unit_test_setup_teardown(test_uses_elem, setup_element_test, teardown_element_test),
David Sedlákaa854b02019-07-22 14:17:10 +02002663 cmocka_unit_test_setup_teardown(test_revision_elem, setup_element_test, teardown_element_test),
David Sedlák0c2bab92019-07-22 15:33:19 +02002664 cmocka_unit_test_setup_teardown(test_include_elem, setup_element_test, teardown_element_test),
David Sedlák5e13dea2019-07-22 16:06:45 +02002665 cmocka_unit_test_setup_teardown(test_feature_elem, setup_element_test, teardown_element_test),
David Sedlák28794f22019-07-22 16:45:00 +02002666 cmocka_unit_test_setup_teardown(test_identity_elem, setup_element_test, teardown_element_test),
David Sedlákaf536aa2019-07-23 13:42:23 +02002667 cmocka_unit_test_setup_teardown(test_list_elem, setup_element_test, teardown_element_test),
David Sedlák3b4db242018-10-19 16:11:01 +02002668 };
2669
David Sedlák8e7bda82019-07-16 17:57:50 +02002670 return cmocka_run_group_tests(tests, setup_ly_ctx, destroy_ly_ctx);
David Sedlák3b4db242018-10-19 16:11:01 +02002671}