blob: 04125c25bd2e57b916237d408b5296dbd06fe6a0 [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ák555c7202019-07-04 12:14:12 +020040
David Sedlák68a1af12019-03-08 13:46:54 +010041struct state {
David Sedlák3b4db242018-10-19 16:11:01 +020042 struct ly_ctx *ctx;
David Sedlák3017da42019-02-15 09:48:04 +010043 struct lys_module *mod;
David Sedlák619db942019-07-03 14:47:30 +020044 struct lysp_module *lysp_mod;
David Sedlákda8ffa32019-07-08 14:17:10 +020045 struct yin_parser_ctx *yin_ctx;
David Sedlák79e50cb2019-06-05 16:33:09 +020046 bool finished_correctly;
David Sedlák68a1af12019-03-08 13:46:54 +010047};
David Sedlák872c7b42018-10-26 13:15:20 +020048
David Sedlák79e50cb2019-06-05 16:33:09 +020049#define BUFSIZE 1024
50char logbuf[BUFSIZE] = {0};
51int store = -1; /* negative for infinite logging, positive for limited logging */
52
53/* set to 0 to printing error messages to stderr instead of checking them in code */
David Sedlák203ca3a2019-07-18 15:26:25 +020054#define ENABLE_LOGGER_CHECKING 1
David Sedlák79e50cb2019-06-05 16:33:09 +020055
56#if ENABLE_LOGGER_CHECKING
57static void
58logger(LY_LOG_LEVEL level, const char *msg, const char *path)
59{
60 (void) level; /* unused */
61 if (store) {
62 if (path && path[0]) {
63 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
64 } else {
65 strncpy(logbuf, msg, BUFSIZE - 1);
66 }
67 if (store > 0) {
68 --store;
69 }
70 }
71}
72#endif
73
74#if ENABLE_LOGGER_CHECKING
75# define logbuf_assert(str) assert_string_equal(logbuf, str)
76#else
77# define logbuf_assert(str)
78#endif
79
80#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
81 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
82 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
83 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
84 CLEANUP
85
David Sedlák8e7bda82019-07-16 17:57:50 +020086int
87setup_ly_ctx(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +010088{
89 struct state *st = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +020090
David Sedlák68a1af12019-03-08 13:46:54 +010091 /* allocate state variable */
92 (*state) = st = calloc(1, sizeof(*st));
93 if (!st) {
94 fprintf(stderr, "Memmory allocation failed");
95 return EXIT_FAILURE;
96 }
David Sedlák872c7b42018-10-26 13:15:20 +020097
David Sedlák68a1af12019-03-08 13:46:54 +010098 /* create new libyang context */
99 ly_ctx_new(NULL, 0, &st->ctx);
David Sedlák872c7b42018-10-26 13:15:20 +0200100
David Sedlák8e7bda82019-07-16 17:57:50 +0200101 return EXIT_SUCCESS;
102}
103
104int
105destroy_ly_ctx(void **state)
106{
107 struct state *st = *state;
108 ly_ctx_destroy(st->ctx, NULL);
109 free(st);
110
111 return EXIT_SUCCESS;
112}
113
114static int
115setup_f(void **state)
116{
117 struct state *st = *state;
118
119#if ENABLE_LOGGER_CHECKING
120 /* setup logger */
121 ly_set_log_clb(logger, 1);
122#endif
123
David Sedlák68a1af12019-03-08 13:46:54 +0100124 /* allocate new module */
125 st->mod = calloc(1, sizeof(*st->mod));
126 st->mod->ctx = st->ctx;
127
David Sedlák619db942019-07-03 14:47:30 +0200128 /* allocate new parsed module */
129 st->lysp_mod = calloc(1, sizeof(*st->lysp_mod));
130 st->lysp_mod->mod = calloc(1, sizeof(*st->lysp_mod->mod));
131 st->lysp_mod->mod->ctx = st->ctx;
132
133 /* allocate parser context */
David Sedlákda8ffa32019-07-08 14:17:10 +0200134 st->yin_ctx = calloc(1, sizeof(*st->yin_ctx));
135 st->yin_ctx->xml_ctx.ctx = st->ctx;
136 st->yin_ctx->xml_ctx.line = 1;
David Sedlák8f5bce02019-06-03 16:41:08 +0200137
David Sedlák68a1af12019-03-08 13:46:54 +0100138 return EXIT_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +0200139}
140
141static int
David Sedlák68a1af12019-03-08 13:46:54 +0100142teardown_f(void **state)
143{
144 struct state *st = *(struct state **)state;
David Sedlák619db942019-07-03 14:47:30 +0200145 struct lys_module *temp;
David Sedlák68a1af12019-03-08 13:46:54 +0100146
David Sedlák79e50cb2019-06-05 16:33:09 +0200147#if ENABLE_LOGGER_CHECKING
148 /* teardown logger */
149 if (!st->finished_correctly && logbuf[0] != '\0') {
150 fprintf(stderr, "%s\n", logbuf);
151 }
152#endif
153
David Sedlák619db942019-07-03 14:47:30 +0200154 temp = st->lysp_mod->mod;
155
David Sedlákda8ffa32019-07-08 14:17:10 +0200156 lyxml_context_clear(&st->yin_ctx->xml_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100157 lys_module_free(st->mod, NULL);
David Sedlák619db942019-07-03 14:47:30 +0200158 lysp_module_free(st->lysp_mod);
159 lys_module_free(temp, NULL);
David Sedlákda8ffa32019-07-08 14:17:10 +0200160 free(st->yin_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100161
162 return EXIT_SUCCESS;
163}
164
David Sedlák392af4f2019-06-04 16:02:42 +0200165static struct state*
166reset_state(void **state)
167{
David Sedlák79e50cb2019-06-05 16:33:09 +0200168 ((struct state *)*state)->finished_correctly = true;
David Sedlák555c7202019-07-04 12:14:12 +0200169 logbuf[0] = '\0';
David Sedlák392af4f2019-06-04 16:02:42 +0200170 teardown_f(state);
171 setup_f(state);
172
173 return *state;
174}
175
David Sedlák79e50cb2019-06-05 16:33:09 +0200176void
177logbuf_clean(void)
178{
179 logbuf[0] = '\0';
180}
181
David Sedlák68a1af12019-03-08 13:46:54 +0100182static void
David Sedlák392af4f2019-06-04 16:02:42 +0200183test_yin_parse_module(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +0100184{
185 LY_ERR ret = LY_SUCCESS;
186 struct state *st = *state;
187
188 ret = yin_parse_module(st->ctx,
David Sedlák2b214ac2019-06-06 16:11:03 +0200189 "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\
190 name=\"example-foo\"\
David Sedlák18730132019-03-15 15:51:34 +0100191 xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\
192 xmlns:foo=\"urn:example:foo\"\
193 xmlns:myext=\"urn:example:extensions\">\
David Sedlákcd0c9512019-03-29 13:23:06 +0100194 <namespace uri=\"urn:example:foo\" xmlns:myext=\"urn:example:extensions\"/>\
David Sedláka7406952019-04-05 10:33:07 +0200195 <prefix xmlns:myxt=\"urn:emple:extensions\" value=\"foo\" xmlns:myext=\"urn:example:extensions\"/>\
David Sedlákd9d3a312019-06-04 09:47:10 +0200196 </module>",
David Sedlák68a1af12019-03-08 13:46:54 +0100197 st->mod);
198
199 assert_int_equal(ret, LY_SUCCESS);
200 assert_string_equal(st->mod->parsed->mod->name, "example-foo");
201 assert_string_equal(st->mod->parsed->mod->prefix, "foo");
David Sedlákcd0c9512019-03-29 13:23:06 +0100202 assert_string_equal(st->mod->parsed->mod->ns, "urn:example:foo");
David Sedlák392af4f2019-06-04 16:02:42 +0200203
204 st = reset_state(state);
205 ret = yin_parse_module(st->ctx,
David Sedlák2b214ac2019-06-06 16:11:03 +0200206 "<module name=\"example-foo\">\
207 <invalid-tag uri=\"urn:example:foo\"\"/>\
208 </module>",
209 st->mod);
David Sedlák392af4f2019-06-04 16:02:42 +0200210 assert_int_equal(ret, LY_EVALID);
211
212 st = reset_state(state);
213 ret = yin_parse_module(st->ctx,
David Sedlák8f7a1172019-06-20 14:42:18 +0200214 "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\
David Sedlák57715b12019-06-17 13:05:22 +0200215 </module>",
David Sedlák2b214ac2019-06-06 16:11:03 +0200216 st->mod);
David Sedlák392af4f2019-06-04 16:02:42 +0200217 assert_int_equal(ret, LY_EVALID);
David Sedlák882a8072019-07-08 17:51:20 +0200218 logbuf_assert("Missing mandatory attribute name of module element. Line number 1.");
David Sedlák392af4f2019-06-04 16:02:42 +0200219
220 st = reset_state(state);
221 ret = yin_parse_module(st->ctx,
222 "",
223 st->mod);
224 assert_int_equal(ret, LY_EVALID);
David Sedlák79e50cb2019-06-05 16:33:09 +0200225 logbuf_assert("Invalid keyword \"(null)\", expected \"module\" or \"submodule\". Line number 1.");
226 st->finished_correctly = true;
David Sedlák3b4db242018-10-19 16:11:01 +0200227}
228
229static void
David Sedlák1bccdfa2019-06-17 15:55:27 +0200230test_yin_match_keyword(void **state)
David Sedlák3b4db242018-10-19 16:11:01 +0200231{
David Sedlák8f7a1172019-06-20 14:42:18 +0200232 struct state *st = *state;
David Sedlák3b4db242018-10-19 16:11:01 +0200233
David Sedlák8f7a1172019-06-20 14:42:18 +0200234 const char *prefix, *name;
235 struct yin_arg_record *args = NULL;
236 size_t prefix_len, name_len;
237 /* create mock yin namespace in xml context */
238 const char *data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" />";
David Sedlákda8ffa32019-07-08 14:17:10 +0200239 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
240 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák8f7a1172019-06-20 14:42:18 +0200241 LY_ARRAY_FREE(args);
242
David Sedlákc1771b12019-07-10 15:55:46 +0200243 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydatax", strlen("anydatax"), prefix, prefix_len, YANG_NONE), YANG_NONE);
244 assert_int_equal(yin_match_keyword(st->yin_ctx, "asdasd", strlen("asdasd"), prefix, prefix_len, YANG_NONE), YANG_NONE);
245 assert_int_equal(yin_match_keyword(st->yin_ctx, "", 0, prefix, prefix_len, YANG_NONE), YANG_NONE);
246 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydata", strlen("anydata"), prefix, prefix_len, YANG_NONE), YANG_ANYDATA);
247 assert_int_equal(yin_match_keyword(st->yin_ctx, "anyxml", strlen("anyxml"), prefix, prefix_len, YANG_NONE), YANG_ANYXML);
248 assert_int_equal(yin_match_keyword(st->yin_ctx, "argument", strlen("argument"), prefix, prefix_len, YANG_NONE), YANG_ARGUMENT);
249 assert_int_equal(yin_match_keyword(st->yin_ctx, "augment", strlen("augment"), prefix, prefix_len, YANG_NONE), YANG_AUGMENT);
250 assert_int_equal(yin_match_keyword(st->yin_ctx, "base", strlen("base"), prefix, prefix_len, YANG_NONE), YANG_BASE);
251 assert_int_equal(yin_match_keyword(st->yin_ctx, "belongs-to", strlen("belongs-to"), prefix, prefix_len, YANG_NONE), YANG_BELONGS_TO);
252 assert_int_equal(yin_match_keyword(st->yin_ctx, "bit", strlen("bit"), prefix, prefix_len, YANG_NONE), YANG_BIT);
253 assert_int_equal(yin_match_keyword(st->yin_ctx, "case", strlen("case"), prefix, prefix_len, YANG_NONE), YANG_CASE);
254 assert_int_equal(yin_match_keyword(st->yin_ctx, "choice", strlen("choice"), prefix, prefix_len, YANG_NONE), YANG_CHOICE);
255 assert_int_equal(yin_match_keyword(st->yin_ctx, "config", strlen("config"), prefix, prefix_len, YANG_NONE), YANG_CONFIG);
256 assert_int_equal(yin_match_keyword(st->yin_ctx, "contact", strlen("contact"), prefix, prefix_len, YANG_NONE), YANG_CONTACT);
257 assert_int_equal(yin_match_keyword(st->yin_ctx, "container", strlen("container"), prefix, prefix_len, YANG_NONE), YANG_CONTAINER);
258 assert_int_equal(yin_match_keyword(st->yin_ctx, "default", strlen("default"), prefix, prefix_len, YANG_NONE), YANG_DEFAULT);
259 assert_int_equal(yin_match_keyword(st->yin_ctx, "description", strlen("description"), prefix, prefix_len, YANG_NONE), YANG_DESCRIPTION);
260 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviate", strlen("deviate"), prefix, prefix_len, YANG_NONE), YANG_DEVIATE);
261 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviation", strlen("deviation"), prefix, prefix_len, YANG_NONE), YANG_DEVIATION);
262 assert_int_equal(yin_match_keyword(st->yin_ctx, "enum", strlen("enum"), prefix, prefix_len, YANG_NONE), YANG_ENUM);
263 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);
264 assert_int_equal(yin_match_keyword(st->yin_ctx, "error-message", strlen("error-message"), prefix, prefix_len, YANG_NONE), YANG_ERROR_MESSAGE);
265 assert_int_equal(yin_match_keyword(st->yin_ctx, "extension", strlen("extension"), prefix, prefix_len, YANG_NONE), YANG_EXTENSION);
266 assert_int_equal(yin_match_keyword(st->yin_ctx, "feature", strlen("feature"), prefix, prefix_len, YANG_NONE), YANG_FEATURE);
267 assert_int_equal(yin_match_keyword(st->yin_ctx, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len, YANG_NONE), YANG_FRACTION_DIGITS);
268 assert_int_equal(yin_match_keyword(st->yin_ctx, "grouping", strlen("grouping"), prefix, prefix_len, YANG_NONE), YANG_GROUPING);
269 assert_int_equal(yin_match_keyword(st->yin_ctx, "identity", strlen("identity"), prefix, prefix_len, YANG_NONE), YANG_IDENTITY);
270 assert_int_equal(yin_match_keyword(st->yin_ctx, "if-feature", strlen("if-feature"), prefix, prefix_len, YANG_NONE), YANG_IF_FEATURE);
271 assert_int_equal(yin_match_keyword(st->yin_ctx, "import", strlen("import"), prefix, prefix_len, YANG_NONE), YANG_IMPORT);
272 assert_int_equal(yin_match_keyword(st->yin_ctx, "include", strlen("include"), prefix, prefix_len, YANG_NONE), YANG_INCLUDE);
273 assert_int_equal(yin_match_keyword(st->yin_ctx, "input", strlen("input"), prefix, prefix_len, YANG_NONE), YANG_INPUT);
274 assert_int_equal(yin_match_keyword(st->yin_ctx, "key", strlen("key"), prefix, prefix_len, YANG_NONE), YANG_KEY);
275 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf", strlen("leaf"), prefix, prefix_len, YANG_NONE), YANG_LEAF);
276 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf-list", strlen("leaf-list"), prefix, prefix_len, YANG_NONE), YANG_LEAF_LIST);
277 assert_int_equal(yin_match_keyword(st->yin_ctx, "length", strlen("length"), prefix, prefix_len, YANG_NONE), YANG_LENGTH);
278 assert_int_equal(yin_match_keyword(st->yin_ctx, "list", strlen("list"), prefix, prefix_len, YANG_NONE), YANG_LIST);
279 assert_int_equal(yin_match_keyword(st->yin_ctx, "mandatory", strlen("mandatory"), prefix, prefix_len, YANG_NONE), YANG_MANDATORY);
280 assert_int_equal(yin_match_keyword(st->yin_ctx, "max-elements", strlen("max-elements"), prefix, prefix_len, YANG_NONE), YANG_MAX_ELEMENTS);
281 assert_int_equal(yin_match_keyword(st->yin_ctx, "min-elements", strlen("min-elements"), prefix, prefix_len, YANG_NONE), YANG_MIN_ELEMENTS);
282 assert_int_equal(yin_match_keyword(st->yin_ctx, "modifier", strlen("modifier"), prefix, prefix_len, YANG_NONE), YANG_MODIFIER);
283 assert_int_equal(yin_match_keyword(st->yin_ctx, "module", strlen("module"), prefix, prefix_len, YANG_NONE), YANG_MODULE);
284 assert_int_equal(yin_match_keyword(st->yin_ctx, "must", strlen("must"), prefix, prefix_len, YANG_NONE), YANG_MUST);
285 assert_int_equal(yin_match_keyword(st->yin_ctx, "namespace", strlen("namespace"), prefix, prefix_len, YANG_NONE), YANG_NAMESPACE);
286 assert_int_equal(yin_match_keyword(st->yin_ctx, "notification", strlen("notification"), prefix, prefix_len, YANG_NONE), YANG_NOTIFICATION);
287 assert_int_equal(yin_match_keyword(st->yin_ctx, "ordered-by", strlen("ordered-by"), prefix, prefix_len, YANG_NONE), YANG_ORDERED_BY);
288 assert_int_equal(yin_match_keyword(st->yin_ctx, "organization", strlen("organization"), prefix, prefix_len, YANG_NONE), YANG_ORGANIZATION);
289 assert_int_equal(yin_match_keyword(st->yin_ctx, "output", strlen("output"), prefix, prefix_len, YANG_NONE), YANG_OUTPUT);
290 assert_int_equal(yin_match_keyword(st->yin_ctx, "path", strlen("path"), prefix, prefix_len, YANG_NONE), YANG_PATH);
291 assert_int_equal(yin_match_keyword(st->yin_ctx, "pattern", strlen("pattern"), prefix, prefix_len, YANG_NONE), YANG_PATTERN);
292 assert_int_equal(yin_match_keyword(st->yin_ctx, "position", strlen("position"), prefix, prefix_len, YANG_NONE), YANG_POSITION);
293 assert_int_equal(yin_match_keyword(st->yin_ctx, "prefix", strlen("prefix"), prefix, prefix_len, YANG_NONE), YANG_PREFIX);
294 assert_int_equal(yin_match_keyword(st->yin_ctx, "presence", strlen("presence"), prefix, prefix_len, YANG_NONE), YANG_PRESENCE);
295 assert_int_equal(yin_match_keyword(st->yin_ctx, "range", strlen("range"), prefix, prefix_len, YANG_NONE), YANG_RANGE);
296 assert_int_equal(yin_match_keyword(st->yin_ctx, "reference", strlen("reference"), prefix, prefix_len, YANG_NONE), YANG_REFERENCE);
297 assert_int_equal(yin_match_keyword(st->yin_ctx, "refine", strlen("refine"), prefix, prefix_len, YANG_NONE), YANG_REFINE);
298 assert_int_equal(yin_match_keyword(st->yin_ctx, "require-instance", strlen("require-instance"), prefix, prefix_len, YANG_NONE), YANG_REQUIRE_INSTANCE);
299 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision", strlen("revision"), prefix, prefix_len, YANG_NONE), YANG_REVISION);
300 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision-date", strlen("revision-date"), prefix, prefix_len, YANG_NONE), YANG_REVISION_DATE);
301 assert_int_equal(yin_match_keyword(st->yin_ctx, "rpc", strlen("rpc"), prefix, prefix_len, YANG_NONE), YANG_RPC);
302 assert_int_equal(yin_match_keyword(st->yin_ctx, "status", strlen("status"), prefix, prefix_len, YANG_NONE), YANG_STATUS);
303 assert_int_equal(yin_match_keyword(st->yin_ctx, "submodule", strlen("submodule"), prefix, prefix_len, YANG_NONE), YANG_SUBMODULE);
304 assert_int_equal(yin_match_keyword(st->yin_ctx, "type", strlen("type"), prefix, prefix_len, YANG_NONE), YANG_TYPE);
305 assert_int_equal(yin_match_keyword(st->yin_ctx, "typedef", strlen("typedef"), prefix, prefix_len, YANG_NONE), YANG_TYPEDEF);
306 assert_int_equal(yin_match_keyword(st->yin_ctx, "unique", strlen("unique"), prefix, prefix_len, YANG_NONE), YANG_UNIQUE);
307 assert_int_equal(yin_match_keyword(st->yin_ctx, "units", strlen("units"), prefix, prefix_len, YANG_NONE), YANG_UNITS);
308 assert_int_equal(yin_match_keyword(st->yin_ctx, "uses", strlen("uses"), prefix, prefix_len, YANG_NONE), YANG_USES);
309 assert_int_equal(yin_match_keyword(st->yin_ctx, "value", strlen("value"), prefix, prefix_len, YANG_NONE), YANG_VALUE);
310 assert_int_equal(yin_match_keyword(st->yin_ctx, "when", strlen("when"), prefix, prefix_len, YANG_NONE), YANG_WHEN);
311 assert_int_equal(yin_match_keyword(st->yin_ctx, "yang-version", strlen("yang-version"), prefix, prefix_len, YANG_NONE), YANG_YANG_VERSION);
312 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 +0200313
314 st->finished_correctly = true;
David Sedlák872c7b42018-10-26 13:15:20 +0200315}
David Sedlák3b4db242018-10-19 16:11:01 +0200316
David Sedlák872c7b42018-10-26 13:15:20 +0200317static void
David Sedlák060b00e2019-06-19 11:12:06 +0200318test_yin_match_argument_name(void **state)
David Sedlák872c7b42018-10-26 13:15:20 +0200319{
David Sedlák68a1af12019-03-08 13:46:54 +0100320 (void)state; /* unused */
David Sedlák872c7b42018-10-26 13:15:20 +0200321
David Sedlák060b00e2019-06-19 11:12:06 +0200322 assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN);
323 assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN);
324 assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN);
325 assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION);
326 assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE);
327 assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE);
328 assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME);
329 assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG);
330 assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
331 assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT);
332 assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI);
333 assert_int_equal(yin_match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200334}
335
David Sedlák68a1af12019-03-08 13:46:54 +0100336static void
David Sedlákb1a78352019-06-28 16:16:29 +0200337test_yin_parse_element_generic(void **state)
338{
339 const char *prefix, *name;
340 struct state *st = *state;
341 struct lysp_ext_instance exts;
342 size_t prefix_len, name_len;
343 LY_ERR ret;
344
345 memset(&exts, 0, sizeof(exts));
346
347 const char *data = "<elem attr=\"value\">text_value</elem>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200348 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
349 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 +0200350 assert_int_equal(ret, LY_SUCCESS);
David Sedlák1fdb2522019-07-09 16:22:57 +0200351 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákb1a78352019-06-28 16:16:29 +0200352 assert_string_equal(exts.child->stmt, "elem");
353 assert_string_equal(exts.child->arg, "text_value");
David Sedlákb1a78352019-06-28 16:16:29 +0200354 assert_string_equal(exts.child->child->stmt, "attr");
355 assert_string_equal(exts.child->child->arg, "value");
356 assert_true(exts.child->child->flags & LYS_YIN_ATTR);
David Sedlákb1a78352019-06-28 16:16:29 +0200357 lysp_ext_instance_free(st->ctx, &exts);
David Sedlák5392a212019-07-01 09:19:10 +0200358 st = reset_state(state);
359
360 data = "<elem></elem>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200361 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
362 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 +0200363 assert_int_equal(ret, LY_SUCCESS);
364 assert_string_equal(exts.child->stmt, "elem");
365 assert_null(exts.child->child);
366 assert_null(exts.child->arg);
David Sedlákda8ffa32019-07-08 14:17:10 +0200367 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlák5392a212019-07-01 09:19:10 +0200368 lysp_ext_instance_free(st->ctx, &exts);
369
David Sedlákb1a78352019-06-28 16:16:29 +0200370 st->finished_correctly = true;
371}
372
373static void
374test_yin_parse_extension_instance(void **state)
375{
376 LY_ERR ret;
377 struct state *st = *state;
378 const char *prefix, *name;
379 size_t prefix_len, name_len;
380 struct yin_arg_record *args = NULL;
381 struct lysp_ext_instance *exts = NULL;
David Sedlákb1a78352019-06-28 16:16:29 +0200382 const char *data = "<ext value1=\"test\" value=\"test2\"><subelem>text</subelem></ext>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200383 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
384 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák1f90d252019-07-10 17:09:32 +0200385 ret = yin_parse_extension_instance(st->yin_ctx, args, &data, name2fullname(name, prefix_len),
David Sedlák619db942019-07-03 14:47:30 +0200386 namelen2fulllen(name_len, prefix_len), LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200387 assert_int_equal(ret, LY_SUCCESS);
388 assert_string_equal(exts->name, "ext");
389 assert_int_equal(exts->insubstmt_index, 0);
390 assert_true(exts->insubstmt == LYEXT_SUBSTMT_CONTACT);
391 assert_true(exts->yin & LYS_YIN);
392 assert_string_equal(exts->child->stmt, "value1");
393 assert_string_equal(exts->child->arg, "test");
394 assert_null(exts->child->child);
395 assert_true(exts->child->flags & LYS_YIN_ATTR);
396 assert_string_equal(exts->child->next->stmt, "value");
397 assert_string_equal(exts->child->next->arg, "test2");
398 assert_null(exts->child->next->child);
399 assert_true(exts->child->next->flags & LYS_YIN_ATTR);
400
401 assert_string_equal(exts->child->next->next->stmt, "subelem");
402 assert_string_equal(exts->child->next->next->arg, "text");
403 assert_null(exts->child->next->next->child);
404 assert_null(exts->child->next->next->next);
405 assert_false(exts->child->next->next->flags & LYS_YIN_ATTR);
David Sedlákda8ffa32019-07-08 14:17:10 +0200406 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákb1a78352019-06-28 16:16:29 +0200407 LY_ARRAY_FREE(args);
408 lysp_ext_instance_free(st->ctx, exts);
409 LY_ARRAY_FREE(exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200410 exts = NULL;
411 args = NULL;
412 st = reset_state(state);
413
414 data = "<extension-elem />";
David Sedlákda8ffa32019-07-08 14:17:10 +0200415 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
416 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák1f90d252019-07-10 17:09:32 +0200417 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 +0200418 assert_int_equal(ret, LY_SUCCESS);
419 assert_string_equal(exts->name, "extension-elem");
420 assert_null(exts->argument);
421 assert_null(exts->child);
422 assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT);
423 assert_int_equal(exts->insubstmt_index, 0);
424 assert_true(exts->yin & LYS_YIN);
David Sedlákda8ffa32019-07-08 14:17:10 +0200425 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákf250ecf2019-07-01 11:02:05 +0200426 LY_ARRAY_FREE(args);
427 lysp_ext_instance_free(st->ctx, exts);
428 LY_ARRAY_FREE(exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200429 st->finished_correctly = true;
430}
431
David Sedlák555c7202019-07-04 12:14:12 +0200432static void
433test_yin_parse_content(void **state)
434{
435 struct state *st = *state;
436 LY_ERR ret = LY_SUCCESS;
437 struct sized_string name, prefix;
438 const char *data = "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
439 "<custom xmlns=\"my-ext\">"
440 "totally amazing extension"
441 "</custom>"
David Sedlák986cb412019-07-04 13:10:11 +0200442 "<extension name=\"ext\">"
443 "<argument name=\"argname\"></argument>"
444 "<description><text>desc</text></description>"
445 "<reference><text>ref</text></reference>"
446 "<status value=\"deprecated\"></status>"
447 "</extension>"
David Sedlák555c7202019-07-04 12:14:12 +0200448 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedlák32488102019-07-15 17:44:10 +0200449 "<if-feature name=\"foo\"></if-feature>"
David Sedlák32eee7b2019-07-09 12:38:44 +0200450 "<when condition=\"condition...\">"
451 "<reference><text>when_ref</text></reference>"
452 "<description><text>when_desc</text></description>"
453 "</when>"
David Sedláke1a30302019-07-10 13:49:38 +0200454 "<config value=\"true\"/>"
David Sedlákc1771b12019-07-10 15:55:46 +0200455 "<error-message>"
456 "<value>error-msg</value>"
457 "</error-message>"
David Sedlák2ce1be62019-07-10 16:15:09 +0200458 "<error-app-tag value=\"err-app-tag\"/>"
David Sedláka5b1d382019-07-10 16:31:09 +0200459 "<units name=\"radians\"></units>"
David Sedláke7084ce2019-07-10 16:44:15 +0200460 "<default value=\"default-value\"/>"
David Sedlák5545f5d2019-07-11 11:55:16 +0200461 "<position value=\"25\"></position>"
462 "<value value=\"-5\"/>"
David Sedlákcf5569a2019-07-11 13:31:34 +0200463 "<require-instance value=\"true\"></require-instance>"
David Sedlákb7296dd2019-07-11 14:58:38 +0200464 "<range value=\"5..10\" />"
David Sedlák438ae432019-07-11 15:36:54 +0200465 "<length value=\"baf\"/>"
David Sedlákd3983112019-07-12 11:20:56 +0200466 "<pattern value='pattern'>"
467 "<modifier value='invert-match'/>"
468 "</pattern>"
David Sedlákfd5b9c32019-07-12 15:33:13 +0200469 "<enum name=\"yay\">"
470 "</enum>"
David Sedlák555c7202019-07-04 12:14:12 +0200471 "</prefix>";
472 struct lysp_ext_instance *exts = NULL;
David Sedlák5f8191e2019-07-08 16:35:52 +0200473 const char **if_features = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200474 struct yin_arg_record *attrs = NULL;
David Sedláke7084ce2019-07-10 16:44:15 +0200475 const char *value, *err_msg, *app_tag, *units, *def;
David Sedlák986cb412019-07-04 13:10:11 +0200476 struct lysp_ext *ext_def = NULL;
David Sedlák32eee7b2019-07-09 12:38:44 +0200477 struct lysp_when *when_p = NULL;
David Sedlákcf5569a2019-07-11 13:31:34 +0200478 struct lysp_type_enum pos_enum = {}, val_enum = {};
David Sedlákfd5b9c32019-07-12 15:33:13 +0200479 struct lysp_type req_type = {}, range_type = {}, len_type = {}, patter_type = {}, enum_type = {};
David Sedláke1a30302019-07-10 13:49:38 +0200480 uint8_t config = 0;
David Sedlák555c7202019-07-04 12:14:12 +0200481
David Sedlákda8ffa32019-07-08 14:17:10 +0200482 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
483 yin_load_attributes(st->yin_ctx, &data, &attrs);
David Sedlák555c7202019-07-04 12:14:12 +0200484
David Sedlákfd5b9c32019-07-12 15:33:13 +0200485 struct yin_subelement subelems[17] = {
David Sedlák438ae432019-07-11 15:36:54 +0200486 {YANG_CONFIG, &config, 0},
David Sedlákc3da3ef2019-07-19 12:56:08 +0200487 {YANG_DEFAULT, &def, YIN_SUBELEM_UNIQUE},
David Sedlákfd5b9c32019-07-12 15:33:13 +0200488 {YANG_ENUM, &enum_type, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200489 {YANG_ERROR_APP_TAG, &app_tag, 0},
490 {YANG_ERROR_MESSAGE, &err_msg, 0},
491 {YANG_EXTENSION, &ext_def, 0},
492 {YANG_IF_FEATURE, &if_features, 0},
493 {YANG_LENGTH, &len_type, 0},
David Sedlákd3983112019-07-12 11:20:56 +0200494 {YANG_PATTERN, &patter_type, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200495 {YANG_RANGE, &range_type, 0},
496 {YANG_REQUIRE_INSTANCE, &req_type, 0},
497 {YANG_UNITS, &units, 0},
David Sedlák374d2b32019-07-17 15:06:55 +0200498 {YANG_POSITION, &pos_enum, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200499 {YANG_VALUE, &val_enum, 0},
500 {YANG_WHEN, &when_p, 0},
501 {YANG_CUSTOM, NULL, 0},
502 {YIN_TEXT, &value, 0}
David Sedlákd3983112019-07-12 11:20:56 +0200503 };
David Sedlákfd5b9c32019-07-12 15:33:13 +0200504 ret = yin_parse_content(st->yin_ctx, subelems, 17, &data, YANG_PREFIX, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200505 assert_int_equal(ret, LY_SUCCESS);
David Sedlák1fdb2522019-07-09 16:22:57 +0200506 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlák2ce1be62019-07-10 16:15:09 +0200507 /* check parsed values */
David Sedláke7084ce2019-07-10 16:44:15 +0200508 assert_string_equal(def, "default-value");
David Sedlák555c7202019-07-04 12:14:12 +0200509 assert_string_equal(exts->name, "custom");
510 assert_string_equal(exts->argument, "totally amazing extension");
511 assert_string_equal(value, "wsefsdf");
David Sedláka5b1d382019-07-10 16:31:09 +0200512 assert_string_equal(units, "radians");
David Sedlák32eee7b2019-07-09 12:38:44 +0200513 assert_string_equal(when_p->cond, "condition...");
514 assert_string_equal(when_p->dsc, "when_desc");
515 assert_string_equal(when_p->ref, "when_ref");
David Sedláke1a30302019-07-10 13:49:38 +0200516 assert_int_equal(config, LYS_CONFIG_W);
David Sedlák5545f5d2019-07-11 11:55:16 +0200517 assert_int_equal(pos_enum.value, 25);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200518 assert_true(pos_enum.flags & LYS_SET_VALUE);
David Sedlák5545f5d2019-07-11 11:55:16 +0200519 assert_int_equal(val_enum.value, -5);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200520 assert_true(val_enum.flags & LYS_SET_VALUE);
David Sedlákcf5569a2019-07-11 13:31:34 +0200521 assert_int_equal(req_type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200522 assert_true(req_type.flags &= LYS_SET_REQINST);
David Sedlákb7296dd2019-07-11 14:58:38 +0200523 assert_string_equal(range_type.range->arg, "5..10");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200524 assert_true(range_type.flags & LYS_SET_RANGE);
David Sedlákc1771b12019-07-10 15:55:46 +0200525 assert_string_equal(err_msg, "error-msg");
David Sedlák2ce1be62019-07-10 16:15:09 +0200526 assert_string_equal(app_tag, "err-app-tag");
David Sedlákfd5b9c32019-07-12 15:33:13 +0200527 assert_string_equal(enum_type.enums->name, "yay");
David Sedlák438ae432019-07-11 15:36:54 +0200528 assert_string_equal(len_type.length->arg, "baf");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200529 assert_true(len_type.flags & LYS_SET_LENGTH);
David Sedlákd3983112019-07-12 11:20:56 +0200530 assert_string_equal(patter_type.patterns->arg, "\x015pattern");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200531 assert_true(patter_type.flags & LYS_SET_PATTERN);
David Sedlák2ce1be62019-07-10 16:15:09 +0200532 /* cleanup */
David Sedlák555c7202019-07-04 12:14:12 +0200533 lysp_ext_instance_free(st->ctx, exts);
David Sedlák32eee7b2019-07-09 12:38:44 +0200534 lysp_when_free(st->ctx, when_p);
David Sedlák986cb412019-07-04 13:10:11 +0200535 lysp_ext_free(st->ctx, ext_def);
David Sedlák5f8191e2019-07-08 16:35:52 +0200536 FREE_STRING(st->ctx, *if_features);
David Sedlákc1771b12019-07-10 15:55:46 +0200537 FREE_STRING(st->ctx, err_msg);
David Sedlák2ce1be62019-07-10 16:15:09 +0200538 FREE_STRING(st->ctx, app_tag);
David Sedláka5b1d382019-07-10 16:31:09 +0200539 FREE_STRING(st->ctx, units);
David Sedlákd3983112019-07-12 11:20:56 +0200540 FREE_STRING(st->ctx, patter_type.patterns->arg);
David Sedláke7084ce2019-07-10 16:44:15 +0200541 FREE_STRING(st->ctx, def);
David Sedlákb7296dd2019-07-11 14:58:38 +0200542 FREE_STRING(st->ctx, range_type.range->arg);
David Sedlák438ae432019-07-11 15:36:54 +0200543 FREE_STRING(st->ctx, len_type.length->arg);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200544 FREE_STRING(st->ctx, enum_type.enums->name);
David Sedlákb7296dd2019-07-11 14:58:38 +0200545 FREE_STRING(st->ctx, value);
David Sedlák5f8191e2019-07-08 16:35:52 +0200546 LY_ARRAY_FREE(if_features);
David Sedlák555c7202019-07-04 12:14:12 +0200547 LY_ARRAY_FREE(exts);
David Sedlák986cb412019-07-04 13:10:11 +0200548 LY_ARRAY_FREE(ext_def);
David Sedlák555c7202019-07-04 12:14:12 +0200549 LY_ARRAY_FREE(attrs);
David Sedlákd3983112019-07-12 11:20:56 +0200550 LY_ARRAY_FREE(patter_type.patterns);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200551 LY_ARRAY_FREE(enum_type.enums);
David Sedlák32eee7b2019-07-09 12:38:44 +0200552 free(when_p);
David Sedlákb7296dd2019-07-11 14:58:38 +0200553 free(range_type.range);
David Sedlák438ae432019-07-11 15:36:54 +0200554 free(len_type.length);
David Sedlák555c7202019-07-04 12:14:12 +0200555 attrs = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200556 st = reset_state(state);
557
558 /* test unique subelem */
559 const char *prefix_value;
560 struct yin_subelement subelems2[2] = {{YANG_PREFIX, &prefix_value, 0},
561 {YIN_TEXT, &value, YIN_SUBELEM_UNIQUE}};
562 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
563 "<prefix value=\"inv_mod\" />"
564 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
565 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
566 "</module>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200567 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
568 yin_load_attributes(st->yin_ctx, &data, &attrs);
569 ret = yin_parse_content(st->yin_ctx, subelems2, 2, &data, YANG_MODULE, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200570 assert_int_equal(ret, LY_EVALID);
571 logbuf_assert("Redefinition of text element in module element. Line number 1.");
572 lydict_remove(st->ctx, prefix_value);
573 lydict_remove(st->ctx, value);
574 st = reset_state(state);
575 LY_ARRAY_FREE(attrs);
576 attrs = NULL;
577
578 /* test first subelem */
579 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
580 "<prefix value=\"inv_mod\" />"
581 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
582 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
583 "</module>";
584 struct yin_subelement subelems3[2] = {{YANG_PREFIX, &prefix_value, 0},
585 {YIN_TEXT, &value, YIN_SUBELEM_FIRST}};
David Sedlákda8ffa32019-07-08 14:17:10 +0200586 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
587 yin_load_attributes(st->yin_ctx, &data, &attrs);
588 ret = yin_parse_content(st->yin_ctx, subelems3, 2, &data, YANG_MODULE, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200589 assert_int_equal(ret, LY_EVALID);
590 logbuf_assert("Subelement text of module element must be defined as first subelement. Line number 1.");
591 lydict_remove(st->ctx, prefix_value);
592 st = reset_state(state);
593 LY_ARRAY_FREE(attrs);
594 attrs = NULL;
595
596 /* test mandatory subelem */
597 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
598 "</module>";
599 struct yin_subelement subelems4[1] = {{YANG_PREFIX, &prefix_value, YIN_SUBELEM_MANDATORY}};
David Sedlákda8ffa32019-07-08 14:17:10 +0200600 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
601 yin_load_attributes(st->yin_ctx, &data, &attrs);
602 ret = yin_parse_content(st->yin_ctx, subelems4, 1, &data, YANG_MODULE, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200603 assert_int_equal(ret, LY_EVALID);
604 logbuf_assert("Missing mandatory subelement prefix of module element. Line number 1.");
605 LY_ARRAY_FREE(attrs);
606
607 st->finished_correctly = true;
608}
609
David Sedlák92147b02019-07-09 14:01:01 +0200610static void
David Sedlák4a650532019-07-10 11:55:18 +0200611test_validate_value(void **state)
612{
613 struct state *st = *state;
614 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "#invalid", 8), LY_EVALID);
615 logbuf_assert("Invalid identifier character '#'. Line number 1.");
616 assert_int_equal(yin_validate_value(st->yin_ctx, Y_STR_ARG, "", 0), LY_SUCCESS);
617 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "pre:b", 5), LY_EVALID);
618 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:b", 5), LY_SUCCESS);
619 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:pre:b", 9), LY_EVALID);
620
621 st->finished_correctly = true;
622}
623
David Sedlák32488102019-07-15 17:44:10 +0200624static int
625setup_element_test(void **state)
626{
David Sedlák8e7bda82019-07-16 17:57:50 +0200627 struct state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +0200628
629#if ENABLE_LOGGER_CHECKING
630 /* setup logger */
631 ly_set_log_clb(logger, 1);
632#endif
633
634 /* reset logbuf */
635 logbuf[0] = '\0';
David Sedlák32488102019-07-15 17:44:10 +0200636
637 /* allocate parser context */
638 st->yin_ctx = calloc(1, sizeof(*st->yin_ctx));
639 st->yin_ctx->xml_ctx.ctx = st->ctx;
640 st->yin_ctx->xml_ctx.line = 1;
641
642 return EXIT_SUCCESS;
643}
644
645static int
646teardown_element_test(void **state)
647{
648 struct state *st = *(struct state **)state;
649
650#if ENABLE_LOGGER_CHECKING
651 /* teardown logger */
652 if (!st->finished_correctly && logbuf[0] != '\0') {
653 fprintf(stderr, "%s\n", logbuf);
654 }
655#endif
656
657 lyxml_context_clear(&st->yin_ctx->xml_ctx);
David Sedlák32488102019-07-15 17:44:10 +0200658 free(st->yin_ctx);
David Sedlák32488102019-07-15 17:44:10 +0200659
660 return EXIT_SUCCESS;
661}
662
663#define ELEMENT_WRAPPER_START "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
664#define ELEMENT_WRAPPER_END "</module>"
665
666/* helper function to simplify unit test of each element using parse_content function */
667LY_ERR
668test_element_helper(struct state *st, const char **data, void *dest, const char **text,
669 struct lysp_ext_instance **exts, bool valid)
670{
671 struct yin_arg_record *attrs = NULL;
672 struct sized_string name, prefix;
673 LY_ERR ret = LY_SUCCESS;
674 struct yin_subelement subelems[71] = {
675 {YANG_ACTION, dest, 0},
676 {YANG_ANYDATA, dest, 0},
677 {YANG_ANYXML, dest, 0},
678 {YANG_ARGUMENT,dest, 0},
679 {YANG_AUGMENT, dest, 0},
680 {YANG_BASE, dest, 0},
681 {YANG_BELONGS_TO, dest, 0},
682 {YANG_BIT, dest, 0},
683 {YANG_CASE, dest, 0},
684 {YANG_CHOICE, dest, 0},
685 {YANG_CONFIG, dest, 0},
686 {YANG_CONTACT, dest, 0},
687 {YANG_CONTAINER, dest, 0},
David Sedlákc3da3ef2019-07-19 12:56:08 +0200688 {YANG_DEFAULT, dest, YIN_SUBELEM_UNIQUE},
David Sedlák32488102019-07-15 17:44:10 +0200689 {YANG_DESCRIPTION, dest, 0},
690 {YANG_DEVIATE, dest, 0},
691 {YANG_DEVIATION, dest, 0},
692 {YANG_ENUM, dest, 0},
693 {YANG_ERROR_APP_TAG, dest, 0},
694 {YANG_ERROR_MESSAGE, dest, 0},
695 {YANG_EXTENSION, dest, 0},
696 {YANG_FEATURE, dest, 0},
697 {YANG_FRACTION_DIGITS, dest, 0},
698 {YANG_GROUPING, dest, 0},
699 {YANG_IDENTITY, dest, 0},
700 {YANG_IF_FEATURE, dest, 0},
701 {YANG_IMPORT, dest, 0},
702 {YANG_INCLUDE, dest, 0},
703 {YANG_INPUT, dest, 0},
704 {YANG_KEY, dest, 0},
705 {YANG_LEAF, dest, 0},
706 {YANG_LEAF_LIST, dest, 0},
707 {YANG_LENGTH, dest, 0},
708 {YANG_LIST, dest, 0},
709 {YANG_MANDATORY, dest, 0},
710 {YANG_MAX_ELEMENTS, dest, 0},
711 {YANG_MIN_ELEMENTS, dest, 0},
712 {YANG_MODIFIER, dest, 0},
713 {YANG_MODULE, dest, 0},
714 {YANG_MUST, dest, 0},
715 {YANG_NAMESPACE, dest, 0},
716 {YANG_NOTIFICATION, dest, 0},
717 {YANG_ORDERED_BY, dest, 0},
718 {YANG_ORGANIZATION, dest, 0},
719 {YANG_OUTPUT, dest, 0},
720 {YANG_PATH, dest, 0},
721 {YANG_PATTERN, dest, 0},
David Sedlák32488102019-07-15 17:44:10 +0200722 {YANG_PREFIX, dest, 0},
723 {YANG_PRESENCE, dest, 0},
724 {YANG_RANGE, dest, 0},
725 {YANG_REFERENCE, dest, 0},
726 {YANG_REFINE, dest, 0},
727 {YANG_REQUIRE_INSTANCE, dest, 0},
728 {YANG_REVISION, dest, 0},
729 {YANG_REVISION_DATE, dest, 0},
730 {YANG_RPC, dest, 0},
731 {YANG_STATUS, dest, 0},
732 {YANG_SUBMODULE, dest, 0},
733 {YANG_TYPE, dest, 0},
734 {YANG_TYPEDEF, dest, 0},
735 {YANG_UNIQUE, dest, 0},
736 {YANG_UNITS, dest, 0},
737 {YANG_USES, dest, 0},
David Sedlák374d2b32019-07-17 15:06:55 +0200738 {YANG_POSITION, dest, 0},
David Sedlák32488102019-07-15 17:44:10 +0200739 {YANG_VALUE, dest, 0},
740 {YANG_WHEN, dest, 0},
741 {YANG_YANG_VERSION, dest, 0},
742 {YANG_YIN_ELEMENT, dest, 0},
743 {YANG_CUSTOM, dest, 0},
744 {YIN_TEXT, dest, 0},
745 {YIN_VALUE, dest, 0}
746 };
747 LY_CHECK_RET(lyxml_get_element(&st->yin_ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len));\
748 LY_CHECK_RET(yin_load_attributes(st->yin_ctx, data, &attrs));\
David Sedlák8e7bda82019-07-16 17:57:50 +0200749 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 +0200750 LY_ARRAY_FREE(attrs);
751 if (valid) {
752 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
753 }
754 /* reset status */
755 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
756 return ret;
757}
758
759static void
760test_enum_bit_elem(void **state)
761{
762 /* yin_parse_enum_bit is function that is being mainly tested by this test */
763 struct state *st = *state;
764 struct lysp_type type = {};
765 const char *data;
766 data = ELEMENT_WRAPPER_START
767 "<enum name=\"enum-name\">"
768 "<if-feature name=\"feature\" />"
769 "<value value=\"55\" />"
770 "<status value=\"deprecated\" />"
771 "<description><text>desc...</text></description>"
772 "<reference><text>ref...</text></reference>"
773 "</enum>"
774 ELEMENT_WRAPPER_END;
775 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
776 assert_string_equal(*type.enums->iffeatures, "feature");
777 assert_int_equal(type.enums->value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200778 assert_true((type.enums->flags & LYS_STATUS_DEPRC) && (type.enums->flags & LYS_SET_VALUE));
David Sedlák32488102019-07-15 17:44:10 +0200779 assert_string_equal(type.enums->dsc, "desc...");
780 assert_string_equal(type.enums->ref, "ref...");
781 lysp_type_free(st->ctx, &type);
782 memset(&type, 0, sizeof type);
783
784 /* todo bit element test */
785 st->finished_correctly = true;
786}
787
788static void
789test_meta_elem(void **state)
790{
791 struct state *st = *state;
792 char *value = NULL;
793 const char *data;
794
795 /* organization element */
796 data = ELEMENT_WRAPPER_START
797 "<organization><text>organization...</text></organization>"
798 ELEMENT_WRAPPER_END;
799 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
800 assert_string_equal(value, "organization...");
801 FREE_STRING(st->ctx, value);
802 value = NULL;
803 /* contact element */
804 data = ELEMENT_WRAPPER_START
805 "<contact><text>contact...</text></contact>"
806 ELEMENT_WRAPPER_END;
807 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
808 assert_string_equal(value, "contact...");
809 FREE_STRING(st->ctx, value);
810 value = NULL;
811 /* description element */
812 data = ELEMENT_WRAPPER_START
813 "<description><text>description...</text></description>"
814 ELEMENT_WRAPPER_END;
815 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
816 assert_string_equal(value, "description...");
817 FREE_STRING(st->ctx, value);
818 value = NULL;
819 /* reference element */
820 data = ELEMENT_WRAPPER_START
821 "<reference><text>reference...</text></reference>"
822 ELEMENT_WRAPPER_END;
823 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
824 assert_string_equal(value, "reference...");
825 FREE_STRING(st->ctx, value);
826 value = NULL;
827
828 /* missing text subelement */
829 data = ELEMENT_WRAPPER_START
830 "<reference>reference...</reference>"
831 ELEMENT_WRAPPER_END;
832 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, false), LY_EVALID);
833 logbuf_assert("Missing mandatory subelement text of reference element. Line number 1.");
834
835 st->finished_correctly = true;
836}
837
838static void
839test_import_elem(void **state)
840{
841 struct state *st = *state;
842 const char *data;
843 struct lys_module *lys_mod = calloc(1, sizeof *lys_mod);
844 struct lysp_module *lysp_mod = calloc(1, sizeof *lysp_mod);
845 lys_mod->ctx = st->ctx;
846 lysp_mod->mod = lys_mod;
847
848 /* max subelems */
849 data = ELEMENT_WRAPPER_START
850 "<import module=\"a\">"
851 "<prefix value=\"a_mod\"/>"
852 "<revision-date date=\"2015-01-01\"></revision-date>"
853 "<description><text>import description</text></description>"
854 "<reference><text>import reference</text></reference>"
855 "</import>"
856 ELEMENT_WRAPPER_END;
857 assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, true), LY_SUCCESS);
858 assert_string_equal(lysp_mod->imports->name, "a");
859 assert_string_equal(lysp_mod->imports->prefix, "a_mod");
860 assert_string_equal(lysp_mod->imports->rev, "2015-01-01");
861 assert_string_equal(lysp_mod->imports->dsc, "import description");
862 assert_string_equal(lysp_mod->imports->ref, "import reference");
863 lysp_module_free(lysp_mod);
864 lys_module_free(lys_mod, NULL);
865
866 /* min subelems */
867 lys_mod = calloc(1, sizeof *lys_mod);
868 lysp_mod = calloc(1, sizeof *lysp_mod);
869 lys_mod->ctx = st->ctx;
870 lysp_mod->mod = lys_mod;
871 data = ELEMENT_WRAPPER_START
872 "<import module=\"a\">"
873 "<prefix value=\"a_mod\"/>"
874 "</import>"
875 ELEMENT_WRAPPER_END;
876 assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, true), LY_SUCCESS);
877 assert_string_equal(lysp_mod->imports->prefix, "a_mod");
878 lysp_module_free(lysp_mod);
879 lys_module_free(lys_mod, NULL);
880
881 /* invalid (missing prefix) */
882 lys_mod = calloc(1, sizeof *lys_mod);
883 lysp_mod = calloc(1, sizeof *lysp_mod);
884 lys_mod->ctx = st->ctx;
885 lysp_mod->mod = lys_mod;
886 data = ELEMENT_WRAPPER_START "<import module=\"a\">""</import>" ELEMENT_WRAPPER_END;
887 assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, false), LY_EVALID);
888 logbuf_assert("Missing mandatory subelement prefix of import element. Line number 1.");
889 lysp_module_free(lysp_mod);
890 lys_module_free(lys_mod, NULL);
891
892 /* invalid reused prefix */
893 lys_mod = calloc(1, sizeof *lys_mod);
894 lysp_mod = calloc(1, sizeof *lysp_mod);
895 lys_mod->ctx = st->ctx;
896 lysp_mod->mod = lys_mod;
897 data = ELEMENT_WRAPPER_START
898 "<import module=\"a\">"
899 "<prefix value=\"a_mod\"/>"
900 "</import>"
901 "<import module=\"a\">"
902 "<prefix value=\"a_mod\"/>"
903 "</import>"
904 ELEMENT_WRAPPER_END;
905 assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, false), LY_EVALID);
906 logbuf_assert("Prefix \"a_mod\" already used to import \"a\" module. Line number 1.");
907 lysp_module_free(lysp_mod);
908 lys_module_free(lys_mod, NULL);
909
910 st->finished_correctly = true;
911}
912
913static void
914test_status_elem(void **state)
915{
916 struct state *st = *state;
917 const char *data;
918 uint16_t flags = 0;
919
920 /* test valid values */
921 data = ELEMENT_WRAPPER_START "<status value=\"current\" />" ELEMENT_WRAPPER_END;
922 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200923 assert_true(flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +0200924
925 data = ELEMENT_WRAPPER_START "<status value=\"deprecated\" />" ELEMENT_WRAPPER_END;
926 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200927 assert_true(flags & LYS_STATUS_DEPRC);
David Sedlák32488102019-07-15 17:44:10 +0200928
929 data = ELEMENT_WRAPPER_START "<status value=\"obsolete\"></status>" ELEMENT_WRAPPER_END;
930 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200931 assert_true(flags & LYS_STATUS_OBSLT);
David Sedlák32488102019-07-15 17:44:10 +0200932
933 /* test invalid value */
934 data = ELEMENT_WRAPPER_START "<status value=\"invalid\"></status>" ELEMENT_WRAPPER_END;
935 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
936 logbuf_assert("Invalid value \"invalid\" of \"status\". Line number 1.");
937 st->finished_correctly = true;
938}
939
940static void
941test_ext_elem(void **state)
942{
943 struct state *st = *state;
944 const char *data;
945 struct lysp_ext *ext = NULL;
946
947 /* max subelems */
948 data = ELEMENT_WRAPPER_START
949 "<extension name=\"ext_name\">"
950 "<argument name=\"arg\"></argument>"
951 "<status value=\"current\"/>"
952 "<description><text>ext_desc</text></description>"
953 "<reference><text>ext_ref</text></reference>"
954 "</extension>"
955 ELEMENT_WRAPPER_END;
956 assert_int_equal(test_element_helper(st, &data, &ext, NULL, NULL, true), LY_SUCCESS);
957 assert_string_equal(ext->name, "ext_name");
958 assert_string_equal(ext->argument, "arg");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200959 assert_true(ext->flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +0200960 assert_string_equal(ext->dsc, "ext_desc");
961 assert_string_equal(ext->ref, "ext_ref");
962 lysp_ext_free(st->ctx, ext);
963 LY_ARRAY_FREE(ext);
964 ext = NULL;
965
966 /* min subelems */
967 data = ELEMENT_WRAPPER_START "<extension name=\"ext_name\"></extension>" ELEMENT_WRAPPER_END;
968 assert_int_equal(test_element_helper(st, &data, &ext, NULL, NULL, true), LY_SUCCESS);
969 assert_string_equal(ext->name, "ext_name");
970 lysp_ext_free(st->ctx, ext);
971 LY_ARRAY_FREE(ext);
972 ext = NULL;
973
974 st->finished_correctly = true;
975}
976
977static void
978test_yin_element_elem(void **state)
979{
980 struct state *st = *state;
981 const char *data;
982 uint16_t flags = 0;
983
984 data = ELEMENT_WRAPPER_START "<yin-element value=\"true\" />" ELEMENT_WRAPPER_END;
985 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200986 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +0200987
988 data = ELEMENT_WRAPPER_START "<yin-element value=\"false\" />" ELEMENT_WRAPPER_END;
989 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200990 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +0200991
992 data = ELEMENT_WRAPPER_START "<yin-element value=\"invalid\" />" ELEMENT_WRAPPER_END;
993 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200994 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +0200995 logbuf_assert("Invalid value \"invalid\" of \"yin-element\". Line number 1.");
996 st->finished_correctly = true;
997}
998
999static void
1000test_yangversion_elem(void **state)
1001{
1002 struct state *st = *state;
1003 const char *data;
1004 uint8_t version = 0;
1005
1006 /* valid values */
1007 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.0\" />" ELEMENT_WRAPPER_END;
1008 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001009 assert_true(version & LYS_VERSION_1_0);
David Sedlák32488102019-07-15 17:44:10 +02001010 assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_0);
1011
1012 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.1\" />" ELEMENT_WRAPPER_END;
1013 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001014 assert_true(version & LYS_VERSION_1_1);
David Sedlák32488102019-07-15 17:44:10 +02001015 assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_1);
1016
1017 /* invalid value */
1018 data = ELEMENT_WRAPPER_START "<yang-version value=\"version\" />" ELEMENT_WRAPPER_END;
1019 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, false), LY_EVALID);
1020 logbuf_assert("Invalid value \"version\" of \"yang-version\". Line number 1.");
1021
1022 st->finished_correctly = true;
1023}
1024
1025static void
1026test_mandatory_elem(void **state)
1027{
1028 struct state *st = *state;
1029 const char *data;
1030 uint16_t man = 0;
1031
1032 /* valid values */
1033 data = ELEMENT_WRAPPER_START "<mandatory value=\"true\" />" ELEMENT_WRAPPER_END;
1034 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, true), LY_SUCCESS);
1035 assert_int_equal(man, LYS_MAND_TRUE);
1036 man = 0;
1037
1038 data = ELEMENT_WRAPPER_START "<mandatory value=\"false\" />" ELEMENT_WRAPPER_END;
1039 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, true), LY_SUCCESS);
1040 assert_int_equal(man, LYS_MAND_FALSE);
1041
1042 data = ELEMENT_WRAPPER_START "<mandatory value=\"invalid\" />" ELEMENT_WRAPPER_END;
1043 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, false), LY_EVALID);
1044 logbuf_assert("Invalid value \"invalid\" of \"mandatory\". Line number 1.");
1045
1046 st->finished_correctly = true;
1047}
1048
David Sedlák8e7bda82019-07-16 17:57:50 +02001049static void
1050test_argument_elem(void **state)
1051{
1052 struct state *st = *state;
1053 const char *data;
1054 uint16_t flags = 0;
1055 const char *arg;
1056 struct yin_argument_meta arg_meta = {&flags, &arg};
1057 /* max subelems */
1058 data = ELEMENT_WRAPPER_START
1059 "<argument name=\"arg-name\">"
1060 "<yin-element value=\"true\" />"
1061 "</argument>"
1062 ELEMENT_WRAPPER_END;
1063 assert_int_equal(test_element_helper(st, &data, &arg_meta, NULL, NULL, true), LY_SUCCESS);
1064 assert_string_equal(arg, "arg-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001065 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001066 flags = 0;
1067 FREE_STRING(st->ctx, arg);
1068 arg = NULL;
1069
1070 /* min subelems */
1071 data = ELEMENT_WRAPPER_START
1072 "<argument name=\"arg\">"
1073 "</argument>"
1074 ELEMENT_WRAPPER_END;
1075 assert_int_equal(test_element_helper(st, &data, &arg_meta, NULL, NULL, true), LY_SUCCESS);
1076 assert_string_equal(arg, "arg");
1077 assert_true(flags == 0);
1078 FREE_STRING(st->ctx, arg);
1079
1080 st->finished_correctly = true;
1081}
1082
1083static void
1084test_base_elem(void **state)
1085{
1086 struct state *st = *state;
1087 const char *data;
1088 const char **bases = NULL;
1089 struct lysp_type type = {};
1090
1091 /* as identity subelement */
1092 data = "<identity xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
1093 "<base name=\"base-name\"/>"
1094 "</identity>";
1095 assert_int_equal(test_element_helper(st, &data, &bases, NULL, NULL, true), LY_SUCCESS);
1096 assert_string_equal(*bases, "base-name");
1097 FREE_STRING(st->ctx, *bases);
1098 LY_ARRAY_FREE(bases);
1099
1100 /* as type subelement */
1101 data = "<type xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
1102 "<base name=\"base-name\"/>"
1103 "</type>";
1104 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1105 assert_string_equal(*type.bases, "base-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001106 assert_true(type.flags & LYS_SET_BASE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001107 FREE_STRING(st->ctx, *type.bases);
1108 LY_ARRAY_FREE(type.bases);
1109
1110 st->finished_correctly = true;
1111}
1112
1113static void
1114test_belongsto_elem(void **state)
1115{
1116 struct state *st = *state;
1117 const char *data;
1118 struct lysp_submodule submod;
1119
1120 data = ELEMENT_WRAPPER_START
1121 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>"
1122 ELEMENT_WRAPPER_END;
1123 assert_int_equal(test_element_helper(st, &data, &submod, NULL, NULL, true), LY_SUCCESS);
1124 assert_string_equal(submod.belongsto, "module-name");
1125 assert_string_equal(submod.prefix, "pref");
1126 FREE_STRING(st->ctx, submod.belongsto);
1127 FREE_STRING(st->ctx, submod.prefix);
1128
1129 data = ELEMENT_WRAPPER_START "<belongs-to module=\"module-name\"></belongs-to>" ELEMENT_WRAPPER_END;
1130 assert_int_equal(test_element_helper(st, &data, &submod, NULL, NULL, false), LY_EVALID);
1131 logbuf_assert("Missing mandatory subelement prefix of belongs-to element. Line number 1.");
1132 FREE_STRING(st->ctx, submod.belongsto);
1133
1134 st->finished_correctly = true;
1135}
1136
1137static void
1138test_config_elem(void **state)
1139{
1140 struct state *st = *state;
1141 const char *data;
1142 uint16_t flags = 0;
1143
1144 data = ELEMENT_WRAPPER_START "<config value=\"true\"/>" ELEMENT_WRAPPER_END;
1145 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001146 assert_true(flags & LYS_CONFIG_W);
David Sedlák8e7bda82019-07-16 17:57:50 +02001147 flags = 0;
1148
1149 data = ELEMENT_WRAPPER_START "<config value=\"false\"/>" ELEMENT_WRAPPER_END;
1150 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001151 assert_true(flags & LYS_CONFIG_R);
David Sedlák8e7bda82019-07-16 17:57:50 +02001152 flags = 0;
1153
1154 data = ELEMENT_WRAPPER_START "<config value=\"invalid\"/>" ELEMENT_WRAPPER_END;
1155 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
1156 logbuf_assert("Invalid value \"invalid\" of \"config\". Line number 1.");
1157
1158 st->finished_correctly = true;
1159}
1160
1161static void
1162test_default_elem(void **state)
1163{
1164 struct state *st = *state;
1165 const char *data;
1166 const char *val = NULL;
1167
1168 data = ELEMENT_WRAPPER_START "<default value=\"defaul-value\"/>" ELEMENT_WRAPPER_END;
1169 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1170 assert_string_equal(val, "defaul-value");
1171 FREE_STRING(st->ctx, val);
1172 val = NULL;
1173
1174 data = ELEMENT_WRAPPER_START "<default/>" ELEMENT_WRAPPER_END;
1175 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1176 logbuf_assert("Missing mandatory attribute value of default element. Line number 1.");
1177
1178 st->finished_correctly = true;
1179}
1180
1181static void
1182test_err_app_tag_elem(void **state)
1183{
1184 struct state *st = *state;
1185 const char *data;
1186 const char *val = NULL;
1187
1188 data = ELEMENT_WRAPPER_START "<error-app-tag value=\"val\"/>" ELEMENT_WRAPPER_END;
1189 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1190 assert_string_equal(val, "val");
1191 FREE_STRING(st->ctx, val);
1192 val = NULL;
1193
1194 data = ELEMENT_WRAPPER_START "<error-app-tag/>" ELEMENT_WRAPPER_END;
1195 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1196 logbuf_assert("Missing mandatory attribute value of error-app-tag element. Line number 1.");
1197
1198 st->finished_correctly = true;
1199}
1200
1201static void
1202test_err_msg_elem(void **state)
1203{
1204 struct state *st = *state;
1205 const char *data;
1206 const char *val = NULL;
1207
1208 data = ELEMENT_WRAPPER_START "<error-message><value>val</value></error-message>" ELEMENT_WRAPPER_END;
1209 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1210 assert_string_equal(val, "val");
1211 FREE_STRING(st->ctx, val);
1212
1213 data = ELEMENT_WRAPPER_START "<error-message></error-message>" ELEMENT_WRAPPER_END;
1214 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1215 logbuf_assert("Missing mandatory subelement value of error-message element. Line number 1.");
1216
1217 st->finished_correctly = true;
1218}
1219
1220static void
1221test_fracdigits_elem(void **state)
1222{
1223 struct state *st = *state;
1224 const char *data;
1225 struct lysp_type type = {};
1226
1227 /* valid value */
1228 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"10\"></fraction-digits>" ELEMENT_WRAPPER_END;
1229 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1230 assert_int_equal(type.fraction_digits, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001231 assert_true(type.flags & LYS_SET_FRDIGITS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001232
1233 /* invalid values */
1234 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"-1\"></fraction-digits>" ELEMENT_WRAPPER_END;
1235 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1236 logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1.");
1237
1238 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"02\"></fraction-digits>" ELEMENT_WRAPPER_END;
1239 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1240 logbuf_assert("Invalid value \"02\" of \"fraction-digits\". Line number 1.");
1241
1242 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"1p\"></fraction-digits>" ELEMENT_WRAPPER_END;
1243 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1244 logbuf_assert("Invalid value \"1p\" of \"fraction-digits\". Line number 1.");
1245
1246 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"19\"></fraction-digits>" ELEMENT_WRAPPER_END;
1247 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1248 logbuf_assert("Invalid value \"19\" of \"fraction-digits\". Line number 1.");
1249
1250 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"999999999999999999\"></fraction-digits>" ELEMENT_WRAPPER_END;
1251 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1252 logbuf_assert("Invalid value \"999999999999999999\" of \"fraction-digits\". Line number 1.");
1253
1254 st->finished_correctly = true;
1255}
1256
1257static void
1258test_iffeature_elem(void **state)
1259{
1260 struct state *st = *state;
1261 const char *data;
1262 const char **iffeatures = NULL;
1263
1264 data = ELEMENT_WRAPPER_START "<if-feature name=\"local-storage\"></if-feature>" ELEMENT_WRAPPER_END;
1265 assert_int_equal(test_element_helper(st, &data, &iffeatures, NULL, NULL, true), LY_SUCCESS);
1266 assert_string_equal(*iffeatures, "local-storage");
1267 FREE_STRING(st->ctx, *iffeatures);
1268 LY_ARRAY_FREE(iffeatures);
1269 iffeatures = NULL;
1270
1271 data = ELEMENT_WRAPPER_START "<if-feature/>" ELEMENT_WRAPPER_END;
1272 assert_int_equal(test_element_helper(st, &data, &iffeatures, NULL, NULL, false), LY_EVALID);
1273 logbuf_assert("Missing mandatory attribute name of if-feature element. Line number 1.");
1274 LY_ARRAY_FREE(iffeatures);
1275 iffeatures = NULL;
1276
1277 st->finished_correctly = true;
1278}
1279
1280static void
1281test_length_elem(void **state)
1282{
1283 struct state *st = *state;
1284 const char *data;
1285 struct lysp_type type = {};
1286
1287 /* max subelems */
1288 data = ELEMENT_WRAPPER_START
1289 "<length value=\"length-str\">"
1290 "<error-message><value>err-msg</value></error-message>"
1291 "<error-app-tag value=\"err-app-tag\"/>"
1292 "<description><text>desc</text></description>"
1293 "<reference><text>ref</text></reference>"
1294 "</length>"
1295 ELEMENT_WRAPPER_END;
1296 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1297 assert_string_equal(type.length->arg, "length-str");
1298 assert_string_equal(type.length->emsg, "err-msg");
1299 assert_string_equal(type.length->eapptag, "err-app-tag");
1300 assert_string_equal(type.length->dsc, "desc");
1301 assert_string_equal(type.length->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001302 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001303 lysp_type_free(st->ctx, &type);
1304 memset(&type, 0, sizeof(type));
1305
1306 /* min subelems */
1307 data = ELEMENT_WRAPPER_START
1308 "<length value=\"length-str\">"
1309 "</length>"
1310 ELEMENT_WRAPPER_END;
1311 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1312 assert_string_equal(type.length->arg, "length-str");
1313 lysp_type_free(st->ctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001314 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001315 memset(&type, 0, sizeof(type));
1316
1317 data = ELEMENT_WRAPPER_START "<length></length>" ELEMENT_WRAPPER_END;
1318 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1319 logbuf_assert("Missing mandatory attribute value of length element. Line number 1.");
1320 lysp_type_free(st->ctx, &type);
1321 memset(&type, 0, sizeof(type));
1322
1323 st->finished_correctly = true;
1324}
1325
1326static void
1327test_modifier_elem(void **state)
1328{
1329 struct state *st = *state;
1330 const char *data;
1331 const char *pat = lydict_insert(st->ctx, "\006pattern", 8);
1332
1333 data = ELEMENT_WRAPPER_START "<modifier value=\"invert-match\" />" ELEMENT_WRAPPER_END;
1334 assert_int_equal(test_element_helper(st, &data, &pat, NULL, NULL, true), LY_SUCCESS);
1335 assert_string_equal(pat, "\x015pattern");
1336 FREE_STRING(st->ctx, pat);
1337
1338 pat = lydict_insert(st->ctx, "\006pattern", 8);
1339 data = ELEMENT_WRAPPER_START "<modifier value=\"invert\" />" ELEMENT_WRAPPER_END;
1340 assert_int_equal(test_element_helper(st, &data, &pat, NULL, NULL, false), LY_EVALID);
1341 logbuf_assert("Invalid value \"invert\" of \"modifier\". Line number 1.");
1342 FREE_STRING(st->ctx, pat);
1343
1344 st->finished_correctly = true;
1345}
1346
1347static void
1348test_namespace_elem(void **state)
1349{
1350 struct state *st = *state;
1351 const char *data;
1352 const char *ns;
1353
1354 data = ELEMENT_WRAPPER_START "<namespace uri=\"ns\"/>" ELEMENT_WRAPPER_END;
1355 assert_int_equal(test_element_helper(st, &data, &ns, NULL, NULL, true), LY_SUCCESS);
1356 assert_string_equal(ns, "ns");
1357 FREE_STRING(st->ctx, ns);
1358
1359 data = ELEMENT_WRAPPER_START "<namespace/>" ELEMENT_WRAPPER_END;
1360 assert_int_equal(test_element_helper(st, &data, &ns, NULL, NULL, false), LY_EVALID);
1361 logbuf_assert("Missing mandatory attribute uri of namespace element. Line number 1.");
1362
1363 st->finished_correctly = true;
1364}
1365
1366static void
1367test_path_elem(void **state)
1368{
1369 struct state *st = *state;
1370 const char *data;
1371 struct lysp_type type = {};
1372
1373 data = ELEMENT_WRAPPER_START "<path value=\"path-val\"/>" ELEMENT_WRAPPER_END;
1374 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1375 assert_string_equal("path-val", type.path);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001376 assert_true(type.flags & LYS_SET_PATH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001377 lysp_type_free(st->ctx, &type);
1378
1379 st->finished_correctly = true;
1380}
1381
1382static void
1383test_pattern_elem(void **state)
1384{
1385 struct state *st = *state;
1386 const char *data;
1387 struct lysp_type type = {};
1388
1389 /* max subelems */
1390 data = ELEMENT_WRAPPER_START
1391 "<pattern value=\"super_pattern\">"
1392 "<modifier value=\"invert-match\"/>"
1393 "<error-message><value>err-msg-value</value></error-message>"
1394 "<error-app-tag value=\"err-app-tag-value\"/>"
1395 "<description><text>pattern-desc</text></description>"
1396 "<reference><text>pattern-ref</text></reference>"
1397 "</pattern>"
1398 ELEMENT_WRAPPER_END;
1399 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001400 assert_true(type.flags & LYS_SET_PATTERN);
David Sedlák8e7bda82019-07-16 17:57:50 +02001401 assert_string_equal(type.patterns->arg, "\x015super_pattern");
1402 assert_string_equal(type.patterns->dsc, "pattern-desc");
1403 assert_string_equal(type.patterns->eapptag, "err-app-tag-value");
1404 assert_string_equal(type.patterns->emsg, "err-msg-value");
1405 assert_string_equal(type.patterns->dsc, "pattern-desc");
1406 assert_string_equal(type.patterns->ref, "pattern-ref");
1407 lysp_type_free(st->ctx, &type);
1408 memset(&type, 0, sizeof(type));
1409
1410 /* min subelems */
1411 data = ELEMENT_WRAPPER_START "<pattern value=\"pattern\"> </pattern>" ELEMENT_WRAPPER_END;
1412 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1413 assert_string_equal(type.patterns->arg, "\x006pattern");
1414 lysp_type_free(st->ctx, &type);
1415 memset(&type, 0, sizeof(type));
1416
1417 st->finished_correctly = true;
1418}
1419
1420static void
1421test_value_position_elem(void **state)
1422{
1423 struct state *st = *state;
1424 const char *data;
1425 struct lysp_type_enum en = {};
1426
1427 /* valid values */
1428 data = ELEMENT_WRAPPER_START "<value value=\"55\" />" ELEMENT_WRAPPER_END;
1429 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1430 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001431 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001432 memset(&en, 0, sizeof(en));
1433
1434 data = ELEMENT_WRAPPER_START "<value value=\"-55\"/>" ELEMENT_WRAPPER_END;
1435 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1436 assert_int_equal(en.value, -55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001437 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001438 memset(&en, 0, sizeof(en));
1439
1440 data = ELEMENT_WRAPPER_START "<value value=\"0\"/>" ELEMENT_WRAPPER_END;
1441 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1442 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001443 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001444 memset(&en, 0, sizeof(en));
1445
1446 data = ELEMENT_WRAPPER_START "<value value=\"-0\"/>" ELEMENT_WRAPPER_END;
1447 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1448 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001449 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001450 memset(&en, 0, sizeof(en));
1451
1452 /* valid positions */
1453 data = ELEMENT_WRAPPER_START "<position value=\"55\" />" ELEMENT_WRAPPER_END;
1454 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1455 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001456 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001457 memset(&en, 0, sizeof(en));
1458
1459 data = ELEMENT_WRAPPER_START "<position value=\"0\" />" ELEMENT_WRAPPER_END;
1460 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1461 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001462 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001463 memset(&en, 0, sizeof(en));
1464
1465 /* invalid values */
1466 data = ELEMENT_WRAPPER_START "<value value=\"99999999999999999999999\"/>" ELEMENT_WRAPPER_END;
1467 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1468 logbuf_assert("Invalid value \"99999999999999999999999\" of \"value\". Line number 1.");
1469
1470 data = ELEMENT_WRAPPER_START "<value value=\"1k\"/>" ELEMENT_WRAPPER_END;
1471 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1472 logbuf_assert("Invalid value \"1k\" of \"value\". Line number 1.");
1473
David Sedlák69f01612019-07-17 11:41:08 +02001474 data = ELEMENT_WRAPPER_START "<value value=\"\"/>" ELEMENT_WRAPPER_END;
1475 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1476 logbuf_assert("Invalid value \"\" of \"value\". Line number 1.");
1477
David Sedlák8e7bda82019-07-16 17:57:50 +02001478 /*invalid positions */
1479 data = ELEMENT_WRAPPER_START "<position value=\"-5\"/>" ELEMENT_WRAPPER_END;
1480 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1481 logbuf_assert("Invalid value \"-5\" of \"position\". Line number 1.");
1482
1483 data = ELEMENT_WRAPPER_START "<position value=\"-0\"/>" ELEMENT_WRAPPER_END;
1484 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1485 logbuf_assert("Invalid value \"-0\" of \"position\". Line number 1.");
1486
1487 data = ELEMENT_WRAPPER_START "<position value=\"99999999999999999999\"/>" ELEMENT_WRAPPER_END;
1488 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1489 logbuf_assert("Invalid value \"99999999999999999999\" of \"position\". Line number 1.");
1490
David Sedlák69f01612019-07-17 11:41:08 +02001491 data = ELEMENT_WRAPPER_START "<position value=\"\"/>" ELEMENT_WRAPPER_END;
1492 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1493 logbuf_assert("Invalid value \"\" of \"position\". Line number 1.");
1494
1495 st->finished_correctly = true;
1496}
1497
1498static void
1499test_prefix_elem(void **state)
1500{
1501 struct state *st = *state;
1502 const char *data;
1503 const char *value = NULL;
1504
1505 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\"/>" ELEMENT_WRAPPER_END;
1506 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
1507 assert_string_equal(value, "pref");
1508 FREE_STRING(st->ctx, value);
1509
1510 st->finished_correctly = true;
1511}
1512
1513static void
1514test_range_elem(void **state)
1515{
1516 struct state *st = *state;
1517 const char *data;
1518 struct lysp_type type = {};
1519
1520 /* max subelems */
1521 data = ELEMENT_WRAPPER_START
1522 "<range value=\"range-str\">"
1523 "<error-message><value>err-msg</value></error-message>"
1524 "<error-app-tag value=\"err-app-tag\" />"
1525 "<description><text>desc</text></description>"
1526 "<reference><text>ref</text></reference>"
1527 "</range>"
1528 ELEMENT_WRAPPER_END;
1529 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1530 assert_string_equal(type.range->arg, "range-str");
1531 assert_string_equal(type.range->dsc, "desc");
1532 assert_string_equal(type.range->eapptag, "err-app-tag");
1533 assert_string_equal(type.range->emsg, "err-msg");
1534 assert_string_equal(type.range->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001535 assert_true(type.flags & LYS_SET_RANGE);
David Sedlák69f01612019-07-17 11:41:08 +02001536 lysp_type_free(st->ctx, &type);
1537 memset(&type, 0, sizeof(type));
1538
1539 /* min subelems */
1540 data = ELEMENT_WRAPPER_START "<range value=\"range-str\"/>" ELEMENT_WRAPPER_END;
1541 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1542 assert_string_equal(type.range->arg, "range-str");
1543 lysp_type_free(st->ctx, &type);
1544 memset(&type, 0, sizeof(type));
1545
1546 st->finished_correctly = true;
1547}
1548
1549static void
1550test_reqinstance_elem(void **state)
1551{
1552 struct state *st = *state;
1553 const char *data;
1554 struct lysp_type type = {};
1555
1556 data = ELEMENT_WRAPPER_START "<require-instance value=\"true\"/>" ELEMENT_WRAPPER_END;
1557 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1558 assert_int_equal(type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001559 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001560 memset(&type, 0, sizeof(type));
1561
1562 data = ELEMENT_WRAPPER_START "<require-instance value=\"false\"/>" ELEMENT_WRAPPER_END;
1563 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1564 assert_int_equal(type.require_instance, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001565 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001566 memset(&type, 0, sizeof(type));
1567
1568 data = ELEMENT_WRAPPER_START "<require-instance value=\"invalid\"/>" ELEMENT_WRAPPER_END;
1569 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1570 memset(&type, 0, sizeof(type));
1571 logbuf_assert("Invalid value \"invalid\" of \"require-instance\". Line number 1.");
1572
1573 st->finished_correctly = true;
1574}
1575
1576static void
1577test_revision_date_elem(void **state)
1578{
1579 struct state *st = *state;
1580 const char *data;
1581 char rev[LY_REV_SIZE];
1582
1583 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\"/>" ELEMENT_WRAPPER_END;
1584 assert_int_equal(test_element_helper(st, &data, rev, NULL, NULL, true), LY_SUCCESS);
1585 assert_string_equal(rev, "2000-01-01");
1586
1587 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-50-05\"/>" ELEMENT_WRAPPER_END;
1588 assert_int_equal(test_element_helper(st, &data, rev, NULL, NULL, false), LY_EVALID);
1589 logbuf_assert("Invalid value \"2000-50-05\" of \"revision-date\". Line number 1.");
1590
1591 st->finished_correctly = true;
1592}
1593
1594static void
1595test_unique_elem(void **state)
1596{
1597 struct state *st = *state;
1598 const char *data;
1599 const char **values = NULL;
1600
1601 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\"/>" ELEMENT_WRAPPER_END;
1602 assert_int_equal(test_element_helper(st, &data, &values, NULL, NULL, true), LY_SUCCESS);
1603 assert_string_equal(*values, "tag");
1604 FREE_STRING(st->ctx, *values);
1605 LY_ARRAY_FREE(values);
1606
1607 st->finished_correctly = true;
1608}
1609
1610static void
1611test_units_elem(void **state)
1612{
1613 struct state *st = *state;
1614 const char *data;
1615 const char *values = NULL;
1616
1617 data = ELEMENT_WRAPPER_START "<units name=\"name\"/>" ELEMENT_WRAPPER_END;
1618 assert_int_equal(test_element_helper(st, &data, &values, NULL, NULL, true), LY_SUCCESS);
1619 assert_string_equal(values, "name");
1620 FREE_STRING(st->ctx, values);
1621
1622 st->finished_correctly = true;
1623}
1624
1625static void
1626test_when_elem(void **state)
1627{
1628 struct state *st = *state;
1629 const char *data;
1630 struct lysp_when *when = NULL;
1631
1632 data = ELEMENT_WRAPPER_START
1633 "<when condition=\"cond\">"
1634 "<description><text>desc</text></description>"
1635 "<reference><text>ref</text></reference>"
1636 "</when>"
1637 ELEMENT_WRAPPER_END;
1638 assert_int_equal(test_element_helper(st, &data, &when, NULL, NULL, true), LY_SUCCESS);
1639 assert_string_equal(when->cond, "cond");
1640 assert_string_equal(when->dsc, "desc");
1641 assert_string_equal(when->ref, "ref");
1642 lysp_when_free(st->ctx, when);
1643 free(when);
1644 when = NULL;
1645
1646 data = ELEMENT_WRAPPER_START "<when condition=\"cond\" />" ELEMENT_WRAPPER_END;
1647 assert_int_equal(test_element_helper(st, &data, &when, NULL, NULL, true), LY_SUCCESS);
1648 assert_string_equal(when->cond, "cond");
1649 lysp_when_free(st->ctx, when);
1650 free(when);
1651 when = NULL;
1652
1653 st->finished_correctly = true;
1654}
1655
1656static void
1657test_yin_text_value_elem(void **state)
1658{
1659 struct state *st = *state;
1660 const char *data;
1661 const char *val;
1662
1663 data = ELEMENT_WRAPPER_START "<text>text</text>" ELEMENT_WRAPPER_END;
1664 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1665 assert_string_equal(val, "text");
1666 FREE_STRING(st->ctx, val);
1667
1668 data = "<error-message xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <value>text</value> </error-message>";
1669 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1670 assert_string_equal(val, "text");
1671 FREE_STRING(st->ctx, val);
1672
1673 data = ELEMENT_WRAPPER_START "<text></text>" ELEMENT_WRAPPER_END;
1674 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1675 assert_string_equal("", val);
1676 FREE_STRING(st->ctx, val);
1677
David Sedlák8e7bda82019-07-16 17:57:50 +02001678 st->finished_correctly = true;
1679}
David Sedlák32488102019-07-15 17:44:10 +02001680
David Sedlák374d2b32019-07-17 15:06:55 +02001681static void
1682test_type_elem(void **state)
1683{
1684 struct state *st = *state;
1685 const char *data;
1686 struct lysp_type type = {};
1687
1688 /* max subelems */
1689 data = ELEMENT_WRAPPER_START
1690 "<type name=\"type-name\">"
1691 "<base name=\"base-name\"/>"
1692 "<bit name=\"bit\"/>"
1693 "<enum name=\"enum\"/>"
1694 "<fraction-digits value=\"2\"/>"
1695 "<length value=\"length\"/>"
1696 "<path value=\"path\"/>"
1697 "<pattern value=\"pattern\"/>"
1698 "<range value=\"range\" />"
1699 "<require-instance value=\"true\"/>"
1700 "<type name=\"sub-type-name\"/>"
1701 "</type>"
1702 ELEMENT_WRAPPER_END;
1703 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1704 assert_string_equal(type.name, "type-name");
1705 assert_string_equal(*type.bases, "base-name");
1706 assert_string_equal(type.bits->name, "bit");
1707 assert_string_equal(type.enums->name, "enum");
1708 assert_int_equal(type.fraction_digits, 2);
1709 assert_string_equal(type.length->arg, "length");
1710 assert_string_equal(type.path, "path");
1711 assert_string_equal(type.patterns->arg, "\006pattern");
1712 assert_string_equal(type.range->arg, "range");
1713 assert_int_equal(type.require_instance, 1);
1714 assert_string_equal(type.types->name, "sub-type-name");
1715 lysp_type_free(st->ctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001716 assert_true(type.flags & LYS_SET_BASE);
1717 assert_true(type.flags & LYS_SET_BIT);
1718 assert_true(type.flags & LYS_SET_ENUM);
1719 assert_true(type.flags & LYS_SET_FRDIGITS);
1720 assert_true(type.flags & LYS_SET_LENGTH);
1721 assert_true(type.flags & LYS_SET_PATH);
1722 assert_true(type.flags & LYS_SET_PATTERN);
1723 assert_true(type.flags & LYS_SET_RANGE);
1724 assert_true(type.flags & LYS_SET_REQINST);
1725 assert_true(type.flags & LYS_SET_TYPE);
David Sedlák374d2b32019-07-17 15:06:55 +02001726 memset(&type, 0, sizeof(type));
1727
1728 /* min subelems */
1729 data = ELEMENT_WRAPPER_START "<type name=\"type-name\"/>" ELEMENT_WRAPPER_END;
1730 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1731 lysp_type_free(st->ctx, &type);
1732 memset(&type, 0, sizeof(type));
1733
1734 st->finished_correctly = true;
1735}
1736
David Sedlák1af868e2019-07-17 17:03:14 +02001737static void
1738test_max_elems_elem(void **state)
1739{
1740 struct state *st = *state;
1741 const char *data;
1742 struct lysp_node_list list = {};
1743 struct lysp_node_leaflist llist = {};
1744 struct lysp_refine refine = {};
1745
1746 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"unbounded\"/> </refine>";
1747 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
1748 assert_int_equal(refine.max, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001749 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001750
1751 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"5\"/> </list>";
1752 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, true), LY_SUCCESS);
1753 assert_int_equal(list.max, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001754 assert_true(list.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001755
1756 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"85\"/> </leaf-list>";
1757 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, true), LY_SUCCESS);
1758 assert_int_equal(llist.max, 85);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001759 assert_true(llist.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001760
1761 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"10\"/> </refine>";
1762 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
1763 assert_int_equal(refine.max, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001764 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001765
1766 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"0\"/> </list>";
1767 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1768 logbuf_assert("Invalid value \"0\" of \"max-elements\". Line number 1.");
1769
1770 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"-10\"/> </list>";
1771 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1772 logbuf_assert("Invalid value \"-10\" of \"max-elements\". Line number 1.");
1773
1774 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"k\"/> </list>";
1775 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1776 logbuf_assert("Invalid value \"k\" of \"max-elements\". Line number 1.");
1777
1778 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"u12\"/> </list>";
1779 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1780 logbuf_assert("Invalid value \"u12\" of \"max-elements\". Line number 1.");
1781
1782 st->finished_correctly = true;
1783}
1784
David Sedlák09e18c92019-07-18 11:17:11 +02001785static void
1786test_min_elems_elem(void **state)
1787{
1788 struct state *st = *state;
1789 const char *data;
1790 struct lysp_node_list list = {};
1791 struct lysp_node_leaflist llist = {};
1792 struct lysp_refine refine = {};
1793
1794 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"0\"/> </refine>";
1795 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
1796 assert_int_equal(refine.min, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001797 assert_true(refine.flags & LYS_SET_MIN);
David Sedlák09e18c92019-07-18 11:17:11 +02001798
1799 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"41\"/> </list>";
1800 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, true), LY_SUCCESS);
1801 assert_int_equal(list.min, 41);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001802 assert_true(list.flags & LYS_SET_MIN);
David Sedlák09e18c92019-07-18 11:17:11 +02001803
1804 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"50\"/> </leaf-list>";
1805 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, true), LY_SUCCESS);
1806 assert_int_equal(llist.min, 50);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001807 assert_true(llist.flags & LYS_SET_MIN);
David Sedlák09e18c92019-07-18 11:17:11 +02001808
1809 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"-5\"/> </leaf-list>";
1810 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1811 logbuf_assert("Value \"-5\" is out of \"min-elements\" bounds. Line number 1.");
1812
1813 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"99999999999999999\"/> </leaf-list>";
1814 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1815 logbuf_assert("Value \"99999999999999999\" is out of \"min-elements\" bounds. Line number 1.");
1816
1817 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"5k\"/> </leaf-list>";
1818 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1819 logbuf_assert("Invalid value \"5k\" of \"min-elements\". Line number 1.");
1820
1821 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"05\"/> </leaf-list>";
1822 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1823 logbuf_assert("Invalid value \"05\" of \"min-elements\". Line number 1.");
1824
1825 st->finished_correctly = true;
1826}
1827
David Sedláka2dad212019-07-18 12:45:19 +02001828static void
1829test_ordby_elem(void **state)
1830{
1831 struct state *st = *state;
1832 const char *data;
1833 uint16_t flags = 0;
1834
1835 data = ELEMENT_WRAPPER_START "<ordered-by value=\"system\"/>" ELEMENT_WRAPPER_END;
1836 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001837 assert_true(flags & LYS_ORDBY_SYSTEM);
David Sedláka2dad212019-07-18 12:45:19 +02001838
1839 data = ELEMENT_WRAPPER_START "<ordered-by value=\"user\"/>" ELEMENT_WRAPPER_END;
1840 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001841 assert_true(flags & LYS_ORDBY_USER);
David Sedláka2dad212019-07-18 12:45:19 +02001842
1843 data = ELEMENT_WRAPPER_START "<ordered-by value=\"inv\"/>" ELEMENT_WRAPPER_END;
1844 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
1845 logbuf_assert("Invalid value \"inv\" of \"ordered-by\". Line number 1.");
1846
1847 st->finished_correctly = true;
1848}
1849
David Sedlák8a83bbb2019-07-18 14:46:00 +02001850static void
1851test_any_elem(void **state)
1852{
1853 struct state *st = *state;
1854 const char *data;
1855 struct lysp_node *siblings = NULL;
1856 struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
1857 struct lysp_node_anydata *parsed = NULL;
1858
1859 /* anyxml max subelems */
1860 data = ELEMENT_WRAPPER_START
1861 "<anyxml name=\"any-name\">"
1862 "<config value=\"true\" />"
1863 "<description><text>desc</text></description>"
1864 "<if-feature name=\"feature\" />"
1865 "<mandatory value=\"true\" />"
1866 "<must condition=\"must-cond\" />"
1867 "<reference><text>ref</text></reference>"
1868 "<status value=\"deprecated\"/>"
1869 "<when condition=\"when-cond\"/>"
1870 "</anyxml>"
1871 ELEMENT_WRAPPER_END;
1872 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1873 parsed = (struct lysp_node_anydata *)siblings;
1874 assert_null(parsed->parent);
1875 assert_int_equal(parsed->nodetype, LYS_ANYXML);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001876 assert_true(parsed->flags & LYS_CONFIG_W);
1877 assert_true(parsed->flags & LYS_MAND_TRUE);
1878 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001879 assert_null(parsed->next);
1880 assert_string_equal(parsed->name, "any-name");
1881 assert_string_equal(parsed->dsc, "desc");
1882 assert_string_equal(parsed->ref, "ref");
1883 assert_string_equal(parsed->when->cond, "when-cond");
1884 assert_string_equal(*parsed->iffeatures, "feature");
1885 assert_null(parsed->exts);
1886 lysp_node_free(st->ctx, siblings);
1887 siblings = NULL;
1888
1889 /* anydata max subelems */
1890 data = ELEMENT_WRAPPER_START
1891 "<anydata name=\"any-name\">"
1892 "<config value=\"true\" />"
1893 "<description><text>desc</text></description>"
1894 "<if-feature name=\"feature\" />"
1895 "<mandatory value=\"true\" />"
1896 "<must condition=\"must-cond\" />"
1897 "<reference><text>ref</text></reference>"
1898 "<status value=\"deprecated\"/>"
1899 "<when condition=\"when-cond\"/>"
1900 "</anydata>"
1901 ELEMENT_WRAPPER_END;
1902 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1903 parsed = (struct lysp_node_anydata *)siblings;
1904 assert_null(parsed->parent);
1905 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001906 assert_true(parsed->flags & LYS_CONFIG_W);
1907 assert_true(parsed->flags & LYS_MAND_TRUE);
1908 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001909 assert_null(parsed->next);
1910 assert_string_equal(parsed->name, "any-name");
1911 assert_string_equal(parsed->dsc, "desc");
1912 assert_string_equal(parsed->ref, "ref");
1913 assert_string_equal(parsed->when->cond, "when-cond");
1914 assert_string_equal(*parsed->iffeatures, "feature");
1915 assert_null(parsed->exts);
1916 lysp_node_free(st->ctx, siblings);
1917 siblings = NULL;
1918
1919 /* min subelems */
1920 node_meta.parent = (void *)0x10;
1921 data = ELEMENT_WRAPPER_START "<anydata name=\"any-name\"> </anydata>" ELEMENT_WRAPPER_END;
1922 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1923 parsed = (struct lysp_node_anydata *)siblings;
1924 assert_ptr_equal(parsed->parent, node_meta.parent);
1925 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
1926 assert_null(parsed->next);
1927 assert_null(parsed->exts);
1928 lysp_node_free(st->ctx, siblings);
1929
1930 st->finished_correctly = true;
1931}
1932
David Sedlák203ca3a2019-07-18 15:26:25 +02001933static void
1934test_leaf_elem(void **state)
1935{
1936 struct state *st = *state;
1937 const char *data;
1938 struct lysp_node *siblings = NULL;
1939 struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
1940 struct lysp_node_leaf *parsed = NULL;
1941
1942 /* max elements */
1943 data = ELEMENT_WRAPPER_START
1944 "<leaf name=\"leaf\">"
1945 "<config value=\"true\" />"
1946 "<default value=\"def-val\"/>"
1947 "<description><text>desc</text></description>"
1948 "<if-feature name=\"feature\" />"
1949 "<mandatory value=\"true\" />"
1950 "<must condition=\"must-cond\" />"
1951 "<reference><text>ref</text></reference>"
1952 "<status value=\"deprecated\"/>"
1953 "<type name=\"type\"/>"
1954 "<units name=\"uni\"/>"
1955 "<when condition=\"when-cond\"/>"
1956 "</leaf>"
1957 ELEMENT_WRAPPER_END;
1958 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1959 parsed = (struct lysp_node_leaf *)siblings;
1960 assert_null(parsed->parent);
1961 assert_int_equal(parsed->nodetype, LYS_LEAF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001962 assert_true(parsed->flags & LYS_CONFIG_W);
1963 assert_true(parsed->flags & LYS_MAND_TRUE);
1964 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák203ca3a2019-07-18 15:26:25 +02001965 assert_null(parsed->next);
1966 assert_string_equal(parsed->name, "leaf");
1967 assert_string_equal(parsed->dsc, "desc");
1968 assert_string_equal(parsed->ref, "ref");
1969 assert_string_equal(parsed->when->cond, "when-cond");
1970 assert_string_equal(*parsed->iffeatures, "feature");
1971 assert_null(parsed->exts);
1972 assert_string_equal(parsed->musts->arg, "must-cond");
1973 assert_string_equal(parsed->type.name, "type");
1974 assert_string_equal(parsed->units, "uni");
1975 assert_string_equal(parsed->dflt, "def-val");
1976 lysp_node_free(st->ctx, siblings);
1977 siblings = NULL;
1978
1979 /* min elements */
1980 data = ELEMENT_WRAPPER_START "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>" ELEMENT_WRAPPER_END;
1981 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1982 parsed = (struct lysp_node_leaf *)siblings;
1983 assert_string_equal(parsed->name, "leaf");
1984 assert_string_equal(parsed->type.name, "type");
1985 lysp_node_free(st->ctx, siblings);
1986 siblings = NULL;
1987
1988 st->finished_correctly = true;
1989}
1990
David Sedlákc3da3ef2019-07-19 12:56:08 +02001991static void
1992test_leaf_list_elem(void **state)
1993{
1994 struct state *st = *state;
1995 const char *data;
1996 struct lysp_node *siblings = NULL;
1997 struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
1998 struct lysp_node_leaflist *parsed = NULL;
1999
2000 data = ELEMENT_WRAPPER_START
2001 "<leaf-list name=\"llist\">"
2002 "<config value=\"true\" />"
2003 "<default value=\"def-val0\"/>"
2004 "<default value=\"def-val1\"/>"
2005 "<description><text>desc</text></description>"
2006 "<if-feature name=\"feature\"/>"
2007 "<max-elements value=\"5\"/>"
2008 "<must condition=\"must-cond\"/>"
2009 "<ordered-by value=\"user\" />"
2010 "<reference><text>ref</text></reference>"
2011 "<status value=\"current\"/>"
2012 "<type name=\"type\"/>"
2013 "<units name=\"uni\"/>"
2014 "<when condition=\"when-cond\"/>"
2015 "</leaf-list>"
2016 ELEMENT_WRAPPER_END;
2017 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2018 parsed = (struct lysp_node_leaflist *)siblings;
2019 assert_string_equal(parsed->dflts[0], "def-val0");
2020 assert_string_equal(parsed->dflts[1], "def-val1");
2021 assert_string_equal(parsed->dsc, "desc");
2022 assert_string_equal(*parsed->iffeatures, "feature");
2023 assert_int_equal(parsed->max, 5);
2024 assert_string_equal(parsed->musts->arg, "must-cond");
2025 assert_string_equal(parsed->name, "llist");
2026 assert_null(parsed->next);
2027 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2028 assert_null(parsed->parent);
2029 assert_string_equal(parsed->ref, "ref");
2030 assert_string_equal(parsed->type.name, "type");
2031 assert_string_equal(parsed->units, "uni");
2032 assert_string_equal(parsed->when->cond, "when-cond");
2033 assert_true(parsed->flags & LYS_CONFIG_W);
2034 assert_true(parsed->flags & LYS_ORDBY_USER);
2035 assert_true(parsed->flags & LYS_STATUS_CURR);
2036 lysp_node_free(st->ctx, siblings);
2037 siblings = NULL;
2038
2039 data = ELEMENT_WRAPPER_START
2040 "<leaf-list name=\"llist\">"
2041 "<config value=\"true\" />"
2042 "<description><text>desc</text></description>"
2043 "<if-feature name=\"feature\"/>"
2044 "<min-elements value=\"5\"/>"
2045 "<must condition=\"must-cond\"/>"
2046 "<ordered-by value=\"user\" />"
2047 "<reference><text>ref</text></reference>"
2048 "<status value=\"current\"/>"
2049 "<type name=\"type\"/>"
2050 "<units name=\"uni\"/>"
2051 "<when condition=\"when-cond\"/>"
2052 "</leaf-list>"
2053 ELEMENT_WRAPPER_END;
2054 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2055 parsed = (struct lysp_node_leaflist *)siblings;
2056 assert_string_equal(parsed->dsc, "desc");
2057 assert_string_equal(*parsed->iffeatures, "feature");
2058 assert_int_equal(parsed->min, 5);
2059 assert_string_equal(parsed->musts->arg, "must-cond");
2060 assert_string_equal(parsed->name, "llist");
2061 assert_null(parsed->next);
2062 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2063 assert_null(parsed->parent);
2064 assert_string_equal(parsed->ref, "ref");
2065 assert_string_equal(parsed->type.name, "type");
2066 assert_string_equal(parsed->units, "uni");
2067 assert_string_equal(parsed->when->cond, "when-cond");
2068 assert_true(parsed->flags & LYS_CONFIG_W);
2069 assert_true(parsed->flags & LYS_ORDBY_USER);
2070 assert_true(parsed->flags & LYS_STATUS_CURR);
2071 lysp_node_free(st->ctx, siblings);
2072 siblings = NULL;
2073
2074 data = ELEMENT_WRAPPER_START
2075 "<leaf-list name=\"llist\">"
2076 "<config value=\"true\" />"
2077 "<description><text>desc</text></description>"
2078 "<if-feature name=\"feature\"/>"
2079 "<max-elements value=\"15\"/>"
2080 "<min-elements value=\"5\"/>"
2081 "<must condition=\"must-cond\"/>"
2082 "<ordered-by value=\"user\" />"
2083 "<reference><text>ref</text></reference>"
2084 "<status value=\"current\"/>"
2085 "<type name=\"type\"/>"
2086 "<units name=\"uni\"/>"
2087 "<when condition=\"when-cond\"/>"
2088 "</leaf-list>"
2089 ELEMENT_WRAPPER_END;
2090 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2091 parsed = (struct lysp_node_leaflist *)siblings;
2092 assert_string_equal(parsed->dsc, "desc");
2093 assert_string_equal(*parsed->iffeatures, "feature");
2094 assert_int_equal(parsed->min, 5);
2095 assert_int_equal(parsed->max, 15);
2096 assert_string_equal(parsed->musts->arg, "must-cond");
2097 assert_string_equal(parsed->name, "llist");
2098 assert_null(parsed->next);
2099 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2100 assert_null(parsed->parent);
2101 assert_string_equal(parsed->ref, "ref");
2102 assert_string_equal(parsed->type.name, "type");
2103 assert_string_equal(parsed->units, "uni");
2104 assert_string_equal(parsed->when->cond, "when-cond");
2105 assert_true(parsed->flags & LYS_CONFIG_W);
2106 assert_true(parsed->flags & LYS_ORDBY_USER);
2107 assert_true(parsed->flags & LYS_STATUS_CURR);
2108 lysp_node_free(st->ctx, siblings);
2109 siblings = NULL;
2110
2111 data = ELEMENT_WRAPPER_START
2112 "<leaf-list name=\"llist\">"
2113 "<type name=\"type\"/>"
2114 "</leaf-list>"
2115 ELEMENT_WRAPPER_END;
2116 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2117 parsed = (struct lysp_node_leaflist *)siblings;
2118 assert_string_equal(parsed->name, "llist");
2119 assert_string_equal(parsed->type.name, "type");
2120 lysp_node_free(st->ctx, siblings);
2121 siblings = NULL;
2122
2123 /* invalid combinations */
2124 data = ELEMENT_WRAPPER_START
2125 "<leaf-list name=\"llist\">"
2126 "<max-elements value=\"5\"/>"
2127 "<min-elements value=\"15\"/>"
2128 "<type name=\"type\"/>"
2129 "</leaf-list>"
2130 ELEMENT_WRAPPER_END;
2131 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
2132 logbuf_assert("Invalid combination of min-elements and max-elements: min value 15 is bigger than the max value 5. Line number 1.");
2133 lysp_node_free(st->ctx, siblings);
2134 siblings = NULL;
2135
2136 data = ELEMENT_WRAPPER_START
2137 "<leaf-list name=\"llist\">"
2138 "<default value=\"def-val1\"/>"
2139 "<min-elements value=\"15\"/>"
2140 "<type name=\"type\"/>"
2141 "</leaf-list>"
2142 ELEMENT_WRAPPER_END;
2143 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
2144 logbuf_assert("Invalid combination of keywords \"min-elements\" and \"default\" as substatements of \"leaf-list\". Line number 1.");
2145 lysp_node_free(st->ctx, siblings);
2146 siblings = NULL;
2147
2148 data = ELEMENT_WRAPPER_START
2149 "<leaf-list name=\"llist\">"
2150 "</leaf-list>"
2151 ELEMENT_WRAPPER_END;
2152 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
2153 logbuf_assert("Missing mandatory subelement type of leaf-list element. Line number 1.");
2154 lysp_node_free(st->ctx, siblings);
2155 siblings = NULL;
2156
2157 st->finished_correctly = true;
2158}
2159
David Sedlákcb39f642019-07-19 13:19:55 +02002160static void
2161test_presence_elem(void **state)
2162{
2163 struct state *st = *state;
2164 const char *data;
2165 const char *val;
2166
2167 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\"/>" ELEMENT_WRAPPER_END;
2168 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2169 assert_string_equal(val, "presence-val");
2170 FREE_STRING(st->ctx, val);
2171
2172 data = ELEMENT_WRAPPER_START "<presence/>" ELEMENT_WRAPPER_END;
2173 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
2174 logbuf_assert("Missing mandatory attribute value of presence element. Line number 1.");
2175
2176 st->finished_correctly = true;
2177}
2178
David Sedlák12470a82019-07-19 13:44:36 +02002179static void
2180test_key_elem(void **state)
2181{
2182 struct state *st = *state;
2183 const char *data;
2184 const char *val;
2185
2186 data = ELEMENT_WRAPPER_START "<key value=\"key-value\"/>" ELEMENT_WRAPPER_END;
2187 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2188 assert_string_equal(val, "key-value");
2189 FREE_STRING(st->ctx, val);
2190
2191 data = ELEMENT_WRAPPER_START "<key/>" ELEMENT_WRAPPER_END;
2192 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
2193 logbuf_assert("Missing mandatory attribute value of key element. Line number 1.");
2194
2195 st->finished_correctly = true;
2196}
2197
David Sedlák04e17b22019-07-19 15:29:48 +02002198static void
2199test_typedef_elem(void **state)
2200{
2201 struct state *st = *state;
2202 const char *data;
2203 struct lysp_tpdf *tpdfs = NULL;
2204 struct typedef_meta typdef_meta = {NULL, &tpdfs};
2205
2206 data = ELEMENT_WRAPPER_START
2207 "<typedef name=\"tpdf-name\">"
2208 "<default value=\"def-val\"/>"
2209 "<description><text>desc-text</text></description>"
2210 "<reference><text>ref-text</text></reference>"
2211 "<status value=\"current\"/>"
2212 "<type name=\"type\"/>"
2213 "<units name=\"uni\"/>"
2214 "</typedef>"
2215 ELEMENT_WRAPPER_END;
2216 assert_int_equal(test_element_helper(st, &data, &typdef_meta, NULL, NULL, true), LY_SUCCESS);
2217 assert_string_equal(tpdfs[0].dflt, "def-val");
2218 assert_string_equal(tpdfs[0].dsc, "desc-text");
2219 assert_null(tpdfs[0].exts);
2220 assert_string_equal(tpdfs[0].name, "tpdf-name");
2221 assert_string_equal(tpdfs[0].ref, "ref-text");
2222 assert_string_equal(tpdfs[0].type.name, "type");
2223 assert_string_equal(tpdfs[0].units, "uni");
2224 assert_true(tpdfs[0].flags & LYS_STATUS_CURR);
2225 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2226 tpdfs = NULL;
2227
2228 data = ELEMENT_WRAPPER_START
2229 "<typedef name=\"tpdf-name\">"
2230 "<type name=\"type\"/>"
2231 "</typedef>"
2232 ELEMENT_WRAPPER_END;
2233 assert_int_equal(test_element_helper(st, &data, &typdef_meta, NULL, NULL, true), LY_SUCCESS);
2234 assert_string_equal(tpdfs[0].name, "tpdf-name");
2235 assert_string_equal(tpdfs[0].type.name, "type");
2236 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2237 tpdfs = NULL;
2238
2239 st->finished_correctly = true;
2240}
2241
David Sedlákd2d676a2019-07-22 11:28:19 +02002242static void
2243test_refine_elem(void **state)
2244{
2245 struct state *st = *state;
2246 const char *data;
2247 struct lysp_refine *refines = NULL;
2248
2249 /* max subelems */
2250 data = ELEMENT_WRAPPER_START
2251 "<refine target-node=\"target\">"
2252 "<if-feature name=\"feature\" />"
2253 "<must condition=\"cond\" />"
2254 "<presence value=\"presence\" />"
2255 "<default value=\"def\" />"
2256 "<config value=\"true\" />"
2257 "<mandatory value=\"true\" />"
2258 "<min-elements value=\"10\" />"
2259 "<max-elements value=\"20\" />"
2260 "<description><text>desc</text></description>"
2261 "<reference><text>ref</text></reference>"
2262 "</refine>"
2263 ELEMENT_WRAPPER_END;
2264 assert_int_equal(test_element_helper(st, &data, &refines, NULL, NULL, true), LY_SUCCESS);
2265 assert_string_equal(refines->nodeid, "target");
2266 assert_string_equal(*refines->dflts, "def");
2267 assert_string_equal(refines->dsc, "desc");
2268 assert_null(refines->exts);
2269 assert_true(refines->flags & LYS_CONFIG_W);
2270 assert_true(refines->flags & LYS_MAND_TRUE);
2271 assert_string_equal(*refines->iffeatures, "feature");
2272 assert_int_equal(refines->max, 20);
2273 assert_int_equal(refines->min, 10);
2274 assert_string_equal(refines->musts->arg, "cond");
2275 assert_string_equal(refines->presence, "presence");
2276 assert_string_equal(refines->ref, "ref");
2277 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2278 refines = NULL;
2279
2280 /* min subelems */
2281 data = ELEMENT_WRAPPER_START "<refine target-node=\"target\" />" ELEMENT_WRAPPER_END;
2282 assert_int_equal(test_element_helper(st, &data, &refines, NULL, NULL, true), LY_SUCCESS);
2283 assert_string_equal(refines->nodeid, "target");
2284 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2285 refines = NULL;
2286
2287 st->finished_correctly = true;
2288}
2289
David Sedlák0d6de5a2019-07-22 13:25:44 +02002290static void
2291test_uses_elem(void **state)
2292{
2293 struct state *st = *state;
2294 const char *data;
2295 struct lysp_node *siblings = NULL;
2296 struct tree_node_meta node_meta = {NULL, &siblings};
2297 struct lysp_node_uses *parsed = NULL;
2298
2299 /* max subelems */
2300 data = ELEMENT_WRAPPER_START
2301 "<uses name=\"uses-name\">"
2302 "<when condition=\"cond\" />"
2303 "<if-feature name=\"feature\" />"
2304 "<status value=\"obsolete\" />"
2305 "<description><text>desc</text></description>"
2306 "<reference><text>ref</text></reference>"
2307 "<refine target-node=\"target\"/>"
2308 /* TODO add uses-augment-stmt instance */
2309 "</uses>"
2310 ELEMENT_WRAPPER_END;
2311 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2312 parsed = (struct lysp_node_uses *)&siblings[0];
2313 assert_string_equal(parsed->name, "uses-name");
2314 assert_string_equal(parsed->dsc, "desc");
2315 assert_null(parsed->exts);
2316 assert_true(parsed->flags & LYS_STATUS_OBSLT);
2317 assert_string_equal(*parsed->iffeatures, "feature");
2318 assert_null(parsed->next);
2319 assert_int_equal(parsed->nodetype, LYS_USES);
2320 assert_null(parsed->parent);
2321 assert_string_equal(parsed->ref, "ref");
2322 assert_string_equal(parsed->refines->nodeid, "target");
2323 assert_string_equal(parsed->when->cond, "cond");
2324 lysp_node_free(st->ctx, siblings);
2325 siblings = NULL;
2326
2327 /* min subelems */
2328 data = ELEMENT_WRAPPER_START "<uses name=\"uses-name\"/>" ELEMENT_WRAPPER_END;
2329 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2330 assert_string_equal(siblings[0].name, "uses-name");
2331 lysp_node_free(st->ctx, siblings);
2332 siblings = NULL;
2333
2334 st->finished_correctly = true;
2335}
2336
David Sedlákaa854b02019-07-22 14:17:10 +02002337static void
2338test_revision_elem(void **state)
2339{
2340 struct state *st = *state;
2341 const char *data;
2342 struct lysp_revision *revs = NULL;
2343
2344 /* max subelems */
2345 data = ELEMENT_WRAPPER_START
2346 "<revision date=\"2018-12-25\">"
2347 "<description><text>desc</text></description>"
2348 "<reference><text>ref</text></reference>"
2349 "</revision>"
2350 ELEMENT_WRAPPER_END;
2351 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, true), LY_SUCCESS);
2352 assert_string_equal(revs->date, "2018-12-25");
2353 assert_string_equal(revs->dsc, "desc");
2354 assert_string_equal(revs->ref, "ref");
2355 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2356 revs = NULL;
2357
2358 /* min subelems */
2359 data = ELEMENT_WRAPPER_START "<revision date=\"2005-05-05\" />" ELEMENT_WRAPPER_END;
2360 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, true), LY_SUCCESS);
2361 assert_string_equal(revs->date, "2005-05-05");
2362 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2363 revs = NULL;
2364
2365 /* invalid value */
2366 data = ELEMENT_WRAPPER_START "<revision date=\"05-05-2005\" />" ELEMENT_WRAPPER_END;
2367 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, false), LY_EVALID);
2368 logbuf_assert("Invalid value \"05-05-2005\" of \"revision\". Line number 1.");
2369 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2370 revs = NULL;
2371
2372 st->finished_correctly = true;
2373}
2374
David Sedlák0c2bab92019-07-22 15:33:19 +02002375static void
2376test_include_elem(void **state)
2377{
2378 struct state *st = *state;
2379 const char *data;
2380 struct lysp_include *includes = NULL;
2381 struct include_meta inc_meta = {"module-name", &includes};
2382
2383 /* max subelems */
2384 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2385 data = ELEMENT_WRAPPER_START
2386 "<include module=\"mod\">"
2387 "<description><text>desc</text></description>"
2388 "<reference><text>ref</text></reference>"
2389 "<revision-date date=\"1999-09-09\"/>"
2390 "</include>"
2391 ELEMENT_WRAPPER_END;
2392 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, true), LY_SUCCESS);
2393 assert_string_equal(includes->name, "mod");
2394 assert_string_equal(includes->dsc, "desc");
2395 assert_string_equal(includes->ref, "ref");
2396 assert_null(includes->exts);
2397 assert_string_equal(includes->rev, "1999-09-09");
2398 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2399 includes = NULL;
2400
2401 /* min subelems */
2402 data = ELEMENT_WRAPPER_START "<include module=\"mod\"/>" ELEMENT_WRAPPER_END;
2403 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, true), LY_SUCCESS);
2404 assert_string_equal(includes->name, "mod");
2405 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2406 includes = NULL;
2407
2408 /* invalid combinations */
2409 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2410 data = ELEMENT_WRAPPER_START
2411 "<include module=\"mod\">"
2412 "<description><text>desc</text></description>"
2413 "<revision-date date=\"1999-09-09\"/>"
2414 "</include>"
2415 ELEMENT_WRAPPER_END;
2416 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, false), LY_EVALID);
2417 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.");
2418 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2419 includes = NULL;
2420
2421 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2422 data = ELEMENT_WRAPPER_START
2423 "<include module=\"mod\">"
2424 "<reference><text>ref</text></reference>"
2425 "<revision-date date=\"1999-09-09\"/>"
2426 "</include>"
2427 ELEMENT_WRAPPER_END;
2428 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, false), LY_EVALID);
2429 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.");
2430 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2431 includes = NULL;
2432
2433 st->finished_correctly = true;
2434}
2435
David Sedlák3b4db242018-10-19 16:11:01 +02002436int
2437main(void)
2438{
2439
2440 const struct CMUnitTest tests[] = {
David Sedlák392af4f2019-06-04 16:02:42 +02002441 cmocka_unit_test_setup_teardown(test_yin_parse_module, setup_f, teardown_f),
David Sedlák8f7a1172019-06-20 14:42:18 +02002442 cmocka_unit_test_setup_teardown(test_yin_match_keyword, setup_f, teardown_f),
David Sedlákb1a78352019-06-28 16:16:29 +02002443 cmocka_unit_test_setup_teardown(test_yin_parse_element_generic, setup_f, teardown_f),
2444 cmocka_unit_test_setup_teardown(test_yin_parse_extension_instance, setup_f, teardown_f),
David Sedlák555c7202019-07-04 12:14:12 +02002445 cmocka_unit_test_setup_teardown(test_yin_parse_content, setup_f, teardown_f),
David Sedlák4a650532019-07-10 11:55:18 +02002446 cmocka_unit_test_setup_teardown(test_validate_value, setup_f, teardown_f),
David Sedlák32488102019-07-15 17:44:10 +02002447
David Sedlák8e7bda82019-07-16 17:57:50 +02002448 cmocka_unit_test(test_yin_match_argument_name),
David Sedlák32488102019-07-15 17:44:10 +02002449 cmocka_unit_test_setup_teardown(test_enum_bit_elem, setup_element_test, teardown_element_test),
2450 cmocka_unit_test_setup_teardown(test_meta_elem, setup_element_test, teardown_element_test),
2451 cmocka_unit_test_setup_teardown(test_import_elem, setup_element_test, teardown_element_test),
2452 cmocka_unit_test_setup_teardown(test_status_elem, setup_element_test, teardown_element_test),
2453 cmocka_unit_test_setup_teardown(test_ext_elem, setup_element_test, teardown_element_test),
2454 cmocka_unit_test_setup_teardown(test_yin_element_elem, setup_element_test, teardown_element_test),
2455 cmocka_unit_test_setup_teardown(test_yangversion_elem, setup_element_test, teardown_element_test),
2456 cmocka_unit_test_setup_teardown(test_mandatory_elem, setup_element_test, teardown_element_test),
David Sedlák8e7bda82019-07-16 17:57:50 +02002457 cmocka_unit_test_setup_teardown(test_argument_elem, setup_element_test, teardown_element_test),
2458 cmocka_unit_test_setup_teardown(test_base_elem, setup_element_test, teardown_element_test),
2459 cmocka_unit_test_setup_teardown(test_belongsto_elem, setup_element_test, teardown_element_test),
2460 cmocka_unit_test_setup_teardown(test_config_elem, setup_element_test, teardown_element_test),
2461 cmocka_unit_test_setup_teardown(test_default_elem, setup_element_test, teardown_element_test),
2462 cmocka_unit_test_setup_teardown(test_err_app_tag_elem, setup_element_test, teardown_element_test),
2463 cmocka_unit_test_setup_teardown(test_err_msg_elem, setup_element_test, teardown_element_test),
2464 cmocka_unit_test_setup_teardown(test_fracdigits_elem, setup_element_test, teardown_element_test),
2465 cmocka_unit_test_setup_teardown(test_iffeature_elem, setup_element_test, teardown_element_test),
2466 cmocka_unit_test_setup_teardown(test_length_elem, setup_element_test, teardown_element_test),
2467 cmocka_unit_test_setup_teardown(test_modifier_elem, setup_element_test, teardown_element_test),
2468 cmocka_unit_test_setup_teardown(test_namespace_elem, setup_element_test, teardown_element_test),
2469 cmocka_unit_test_setup_teardown(test_path_elem, setup_element_test, teardown_element_test),
2470 cmocka_unit_test_setup_teardown(test_pattern_elem, setup_element_test, teardown_element_test),
2471 cmocka_unit_test_setup_teardown(test_value_position_elem, setup_element_test, teardown_element_test),
David Sedlák69f01612019-07-17 11:41:08 +02002472 cmocka_unit_test_setup_teardown(test_prefix_elem, setup_element_test, teardown_element_test),
2473 cmocka_unit_test_setup_teardown(test_range_elem, setup_element_test, teardown_element_test),
2474 cmocka_unit_test_setup_teardown(test_reqinstance_elem, setup_element_test, teardown_element_test),
2475 cmocka_unit_test_setup_teardown(test_revision_date_elem, setup_element_test, teardown_element_test),
2476 cmocka_unit_test_setup_teardown(test_unique_elem, setup_element_test, teardown_element_test),
2477 cmocka_unit_test_setup_teardown(test_units_elem, setup_element_test, teardown_element_test),
2478 cmocka_unit_test_setup_teardown(test_when_elem, setup_element_test, teardown_element_test),
2479 cmocka_unit_test_setup_teardown(test_yin_text_value_elem, setup_element_test, teardown_element_test),
David Sedlák374d2b32019-07-17 15:06:55 +02002480 cmocka_unit_test_setup_teardown(test_type_elem, setup_element_test, teardown_element_test),
David Sedlák1af868e2019-07-17 17:03:14 +02002481 cmocka_unit_test_setup_teardown(test_max_elems_elem, setup_element_test, teardown_element_test),
David Sedlák09e18c92019-07-18 11:17:11 +02002482 cmocka_unit_test_setup_teardown(test_min_elems_elem, setup_element_test, teardown_element_test),
David Sedláka2dad212019-07-18 12:45:19 +02002483 cmocka_unit_test_setup_teardown(test_ordby_elem, setup_element_test, teardown_element_test),
David Sedlák8a83bbb2019-07-18 14:46:00 +02002484 cmocka_unit_test_setup_teardown(test_any_elem, setup_element_test, teardown_element_test),
David Sedlák203ca3a2019-07-18 15:26:25 +02002485 cmocka_unit_test_setup_teardown(test_leaf_elem, setup_element_test, teardown_element_test),
David Sedlákc3da3ef2019-07-19 12:56:08 +02002486 cmocka_unit_test_setup_teardown(test_leaf_list_elem, setup_element_test, teardown_element_test),
David Sedlákcb39f642019-07-19 13:19:55 +02002487 cmocka_unit_test_setup_teardown(test_presence_elem, setup_element_test, teardown_element_test),
David Sedlák12470a82019-07-19 13:44:36 +02002488 cmocka_unit_test_setup_teardown(test_key_elem, setup_element_test, teardown_element_test),
David Sedlák04e17b22019-07-19 15:29:48 +02002489 cmocka_unit_test_setup_teardown(test_typedef_elem, setup_element_test, teardown_element_test),
David Sedlákd2d676a2019-07-22 11:28:19 +02002490 cmocka_unit_test_setup_teardown(test_refine_elem, setup_element_test, teardown_element_test),
David Sedlák0d6de5a2019-07-22 13:25:44 +02002491 cmocka_unit_test_setup_teardown(test_uses_elem, setup_element_test, teardown_element_test),
David Sedlákaa854b02019-07-22 14:17:10 +02002492 cmocka_unit_test_setup_teardown(test_revision_elem, setup_element_test, teardown_element_test),
David Sedlák0c2bab92019-07-22 15:33:19 +02002493 cmocka_unit_test_setup_teardown(test_include_elem, setup_element_test, teardown_element_test),
2494
David Sedlák3b4db242018-10-19 16:11:01 +02002495 };
2496
David Sedlák8e7bda82019-07-16 17:57:50 +02002497 return cmocka_run_group_tests(tests, setup_ly_ctx, destroy_ly_ctx);
David Sedlák3b4db242018-10-19 16:11:01 +02002498}