blob: 54ce3ca205ecaeb580583b82709359ce63e7944f [file] [log] [blame]
David Sedlákb1ce3f82019-06-05 14:37:26 +02001/**
2 * @file test_parser_yin.c
3 * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz>
4 * @brief unit tests for functions from parser_yin.c
5 *
6 * Copyright (c) 2015 - 2019 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
David Sedlák3b4db242018-10-19 16:11:01 +020015#include <stdarg.h>
16#include <stddef.h>
17#include <setjmp.h>
18#include <cmocka.h>
19
20#include <stdio.h>
21#include <string.h>
David Sedlák79e50cb2019-06-05 16:33:09 +020022#include <stdbool.h>
David Sedlák3b4db242018-10-19 16:11:01 +020023
David Sedlákecf5eb82019-06-03 14:12:44 +020024#include "../../src/common.h"
25#include "../../src/tree_schema.h"
26#include "../../src/tree_schema_internal.h"
27#include "../../src/parser_yin.h"
David Sedlák8f5bce02019-06-03 16:41:08 +020028#include "../../src/xml.h"
David Sedlák3b4db242018-10-19 16:11:01 +020029
David Sedlák555c7202019-07-04 12:14:12 +020030/* prototypes of static functions */
31void lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext);
David Sedlák986cb412019-07-04 13:10:11 +020032void lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext);
David Sedlák32eee7b2019-07-09 12:38:44 +020033void lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when);
David Sedlák32488102019-07-15 17:44:10 +020034void lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type);
David Sedlák8a83bbb2019-07-18 14:46:00 +020035void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
David Sedlák04e17b22019-07-19 15:29:48 +020036void lysp_tpdf_free(struct ly_ctx *ctx, struct lysp_tpdf *tpdf);
David Sedlákd2d676a2019-07-22 11:28:19 +020037void lysp_refine_free(struct ly_ctx *ctx, struct lysp_refine *ref);
David Sedlákaa854b02019-07-22 14:17:10 +020038void lysp_revision_free(struct ly_ctx *ctx, struct lysp_revision *rev);
David Sedlák0c2bab92019-07-22 15:33:19 +020039void lysp_include_free(struct ly_ctx *ctx, struct lysp_include *include);
David Sedlák5e13dea2019-07-22 16:06:45 +020040void lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat);
David Sedlák28794f22019-07-22 16:45:00 +020041void lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident);
David Sedlák031b9e72019-07-23 15:19:37 +020042void lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif);
David Sedláke3ce9ef2019-07-23 16:34:30 +020043void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp);
David Sedlák05404f62019-07-24 14:11:53 +020044void lysp_action_inout_free(struct ly_ctx *ctx, struct lysp_action_inout *inout);
David Sedlák85d0eca2019-07-24 15:15:21 +020045void lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action);
David Sedlák992fb7c2019-07-24 16:51:01 +020046void lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment);
David Sedlák4ffcec82019-07-25 15:10:21 +020047void lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d);
David Sedlák8b754462019-07-25 16:22:13 +020048void lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev);
David Sedlák298ff6d2019-07-26 14:29:03 +020049void lysp_submodule_free(struct ly_ctx *ctx, struct lysp_submodule *submod);
50void lysp_import_free(struct ly_ctx *ctx, struct lysp_import *import);
David Sedlák555c7202019-07-04 12:14:12 +020051
David Sedlák68a1af12019-03-08 13:46:54 +010052struct state {
David Sedlák3b4db242018-10-19 16:11:01 +020053 struct ly_ctx *ctx;
David Sedlák3017da42019-02-15 09:48:04 +010054 struct lys_module *mod;
David Sedlák619db942019-07-03 14:47:30 +020055 struct lysp_module *lysp_mod;
David Sedlákda8ffa32019-07-08 14:17:10 +020056 struct yin_parser_ctx *yin_ctx;
David Sedlák79e50cb2019-06-05 16:33:09 +020057 bool finished_correctly;
David Sedlák68a1af12019-03-08 13:46:54 +010058};
David Sedlák872c7b42018-10-26 13:15:20 +020059
David Sedlák79e50cb2019-06-05 16:33:09 +020060#define BUFSIZE 1024
61char logbuf[BUFSIZE] = {0};
62int store = -1; /* negative for infinite logging, positive for limited logging */
63
64/* set to 0 to printing error messages to stderr instead of checking them in code */
David Sedlák298ff6d2019-07-26 14:29:03 +020065#define ENABLE_LOGGER_CHECKING 1
David Sedlák79e50cb2019-06-05 16:33:09 +020066
67#if ENABLE_LOGGER_CHECKING
68static void
69logger(LY_LOG_LEVEL level, const char *msg, const char *path)
70{
71 (void) level; /* unused */
72 if (store) {
73 if (path && path[0]) {
74 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
75 } else {
76 strncpy(logbuf, msg, BUFSIZE - 1);
77 }
78 if (store > 0) {
79 --store;
80 }
81 }
82}
83#endif
84
85#if ENABLE_LOGGER_CHECKING
86# define logbuf_assert(str) assert_string_equal(logbuf, str)
87#else
88# define logbuf_assert(str)
89#endif
90
91#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
92 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
93 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
94 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
95 CLEANUP
96
David Sedlák8e7bda82019-07-16 17:57:50 +020097int
98setup_ly_ctx(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +010099{
100 struct state *st = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +0200101
David Sedlák68a1af12019-03-08 13:46:54 +0100102 /* allocate state variable */
103 (*state) = st = calloc(1, sizeof(*st));
104 if (!st) {
105 fprintf(stderr, "Memmory allocation failed");
106 return EXIT_FAILURE;
107 }
David Sedlák872c7b42018-10-26 13:15:20 +0200108
David Sedlák68a1af12019-03-08 13:46:54 +0100109 /* create new libyang context */
110 ly_ctx_new(NULL, 0, &st->ctx);
David Sedlák872c7b42018-10-26 13:15:20 +0200111
David Sedlák8e7bda82019-07-16 17:57:50 +0200112 return EXIT_SUCCESS;
113}
114
115int
116destroy_ly_ctx(void **state)
117{
118 struct state *st = *state;
119 ly_ctx_destroy(st->ctx, NULL);
120 free(st);
121
122 return EXIT_SUCCESS;
123}
124
125static int
126setup_f(void **state)
127{
128 struct state *st = *state;
129
130#if ENABLE_LOGGER_CHECKING
131 /* setup logger */
132 ly_set_log_clb(logger, 1);
133#endif
134
David Sedlák68a1af12019-03-08 13:46:54 +0100135 /* allocate new module */
136 st->mod = calloc(1, sizeof(*st->mod));
137 st->mod->ctx = st->ctx;
138
David Sedlák619db942019-07-03 14:47:30 +0200139 /* allocate new parsed module */
140 st->lysp_mod = calloc(1, sizeof(*st->lysp_mod));
141 st->lysp_mod->mod = calloc(1, sizeof(*st->lysp_mod->mod));
142 st->lysp_mod->mod->ctx = st->ctx;
143
144 /* allocate parser context */
David Sedlákda8ffa32019-07-08 14:17:10 +0200145 st->yin_ctx = calloc(1, sizeof(*st->yin_ctx));
146 st->yin_ctx->xml_ctx.ctx = st->ctx;
147 st->yin_ctx->xml_ctx.line = 1;
David Sedlák8f5bce02019-06-03 16:41:08 +0200148
David Sedlák68a1af12019-03-08 13:46:54 +0100149 return EXIT_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +0200150}
151
152static int
David Sedlák68a1af12019-03-08 13:46:54 +0100153teardown_f(void **state)
154{
155 struct state *st = *(struct state **)state;
David Sedlák619db942019-07-03 14:47:30 +0200156 struct lys_module *temp;
David Sedlák68a1af12019-03-08 13:46:54 +0100157
David Sedlák79e50cb2019-06-05 16:33:09 +0200158#if ENABLE_LOGGER_CHECKING
159 /* teardown logger */
160 if (!st->finished_correctly && logbuf[0] != '\0') {
161 fprintf(stderr, "%s\n", logbuf);
162 }
163#endif
164
David Sedlák619db942019-07-03 14:47:30 +0200165 temp = st->lysp_mod->mod;
166
David Sedlákda8ffa32019-07-08 14:17:10 +0200167 lyxml_context_clear(&st->yin_ctx->xml_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100168 lys_module_free(st->mod, NULL);
David Sedlák619db942019-07-03 14:47:30 +0200169 lysp_module_free(st->lysp_mod);
170 lys_module_free(temp, NULL);
David Sedlákda8ffa32019-07-08 14:17:10 +0200171 free(st->yin_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100172
173 return EXIT_SUCCESS;
174}
175
David Sedlák392af4f2019-06-04 16:02:42 +0200176static struct state*
177reset_state(void **state)
178{
David Sedlák79e50cb2019-06-05 16:33:09 +0200179 ((struct state *)*state)->finished_correctly = true;
David Sedlák555c7202019-07-04 12:14:12 +0200180 logbuf[0] = '\0';
David Sedlák392af4f2019-06-04 16:02:42 +0200181 teardown_f(state);
182 setup_f(state);
183
184 return *state;
185}
186
David Sedlák79e50cb2019-06-05 16:33:09 +0200187void
188logbuf_clean(void)
189{
190 logbuf[0] = '\0';
191}
192
David Sedlák68a1af12019-03-08 13:46:54 +0100193static void
David Sedlák1bccdfa2019-06-17 15:55:27 +0200194test_yin_match_keyword(void **state)
David Sedlák3b4db242018-10-19 16:11:01 +0200195{
David Sedlák8f7a1172019-06-20 14:42:18 +0200196 struct state *st = *state;
David Sedlák3b4db242018-10-19 16:11:01 +0200197
David Sedlák8f7a1172019-06-20 14:42:18 +0200198 const char *prefix, *name;
199 struct yin_arg_record *args = NULL;
200 size_t prefix_len, name_len;
201 /* create mock yin namespace in xml context */
202 const char *data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" />";
David Sedlákda8ffa32019-07-08 14:17:10 +0200203 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
204 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák8f7a1172019-06-20 14:42:18 +0200205 LY_ARRAY_FREE(args);
206
David Sedlákc1771b12019-07-10 15:55:46 +0200207 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydatax", strlen("anydatax"), prefix, prefix_len, YANG_NONE), YANG_NONE);
208 assert_int_equal(yin_match_keyword(st->yin_ctx, "asdasd", strlen("asdasd"), prefix, prefix_len, YANG_NONE), YANG_NONE);
209 assert_int_equal(yin_match_keyword(st->yin_ctx, "", 0, prefix, prefix_len, YANG_NONE), YANG_NONE);
210 assert_int_equal(yin_match_keyword(st->yin_ctx, "anydata", strlen("anydata"), prefix, prefix_len, YANG_NONE), YANG_ANYDATA);
211 assert_int_equal(yin_match_keyword(st->yin_ctx, "anyxml", strlen("anyxml"), prefix, prefix_len, YANG_NONE), YANG_ANYXML);
212 assert_int_equal(yin_match_keyword(st->yin_ctx, "argument", strlen("argument"), prefix, prefix_len, YANG_NONE), YANG_ARGUMENT);
213 assert_int_equal(yin_match_keyword(st->yin_ctx, "augment", strlen("augment"), prefix, prefix_len, YANG_NONE), YANG_AUGMENT);
214 assert_int_equal(yin_match_keyword(st->yin_ctx, "base", strlen("base"), prefix, prefix_len, YANG_NONE), YANG_BASE);
215 assert_int_equal(yin_match_keyword(st->yin_ctx, "belongs-to", strlen("belongs-to"), prefix, prefix_len, YANG_NONE), YANG_BELONGS_TO);
216 assert_int_equal(yin_match_keyword(st->yin_ctx, "bit", strlen("bit"), prefix, prefix_len, YANG_NONE), YANG_BIT);
217 assert_int_equal(yin_match_keyword(st->yin_ctx, "case", strlen("case"), prefix, prefix_len, YANG_NONE), YANG_CASE);
218 assert_int_equal(yin_match_keyword(st->yin_ctx, "choice", strlen("choice"), prefix, prefix_len, YANG_NONE), YANG_CHOICE);
219 assert_int_equal(yin_match_keyword(st->yin_ctx, "config", strlen("config"), prefix, prefix_len, YANG_NONE), YANG_CONFIG);
220 assert_int_equal(yin_match_keyword(st->yin_ctx, "contact", strlen("contact"), prefix, prefix_len, YANG_NONE), YANG_CONTACT);
221 assert_int_equal(yin_match_keyword(st->yin_ctx, "container", strlen("container"), prefix, prefix_len, YANG_NONE), YANG_CONTAINER);
222 assert_int_equal(yin_match_keyword(st->yin_ctx, "default", strlen("default"), prefix, prefix_len, YANG_NONE), YANG_DEFAULT);
223 assert_int_equal(yin_match_keyword(st->yin_ctx, "description", strlen("description"), prefix, prefix_len, YANG_NONE), YANG_DESCRIPTION);
224 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviate", strlen("deviate"), prefix, prefix_len, YANG_NONE), YANG_DEVIATE);
225 assert_int_equal(yin_match_keyword(st->yin_ctx, "deviation", strlen("deviation"), prefix, prefix_len, YANG_NONE), YANG_DEVIATION);
226 assert_int_equal(yin_match_keyword(st->yin_ctx, "enum", strlen("enum"), prefix, prefix_len, YANG_NONE), YANG_ENUM);
227 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);
228 assert_int_equal(yin_match_keyword(st->yin_ctx, "error-message", strlen("error-message"), prefix, prefix_len, YANG_NONE), YANG_ERROR_MESSAGE);
229 assert_int_equal(yin_match_keyword(st->yin_ctx, "extension", strlen("extension"), prefix, prefix_len, YANG_NONE), YANG_EXTENSION);
230 assert_int_equal(yin_match_keyword(st->yin_ctx, "feature", strlen("feature"), prefix, prefix_len, YANG_NONE), YANG_FEATURE);
231 assert_int_equal(yin_match_keyword(st->yin_ctx, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len, YANG_NONE), YANG_FRACTION_DIGITS);
232 assert_int_equal(yin_match_keyword(st->yin_ctx, "grouping", strlen("grouping"), prefix, prefix_len, YANG_NONE), YANG_GROUPING);
233 assert_int_equal(yin_match_keyword(st->yin_ctx, "identity", strlen("identity"), prefix, prefix_len, YANG_NONE), YANG_IDENTITY);
234 assert_int_equal(yin_match_keyword(st->yin_ctx, "if-feature", strlen("if-feature"), prefix, prefix_len, YANG_NONE), YANG_IF_FEATURE);
235 assert_int_equal(yin_match_keyword(st->yin_ctx, "import", strlen("import"), prefix, prefix_len, YANG_NONE), YANG_IMPORT);
236 assert_int_equal(yin_match_keyword(st->yin_ctx, "include", strlen("include"), prefix, prefix_len, YANG_NONE), YANG_INCLUDE);
237 assert_int_equal(yin_match_keyword(st->yin_ctx, "input", strlen("input"), prefix, prefix_len, YANG_NONE), YANG_INPUT);
238 assert_int_equal(yin_match_keyword(st->yin_ctx, "key", strlen("key"), prefix, prefix_len, YANG_NONE), YANG_KEY);
239 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf", strlen("leaf"), prefix, prefix_len, YANG_NONE), YANG_LEAF);
240 assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf-list", strlen("leaf-list"), prefix, prefix_len, YANG_NONE), YANG_LEAF_LIST);
241 assert_int_equal(yin_match_keyword(st->yin_ctx, "length", strlen("length"), prefix, prefix_len, YANG_NONE), YANG_LENGTH);
242 assert_int_equal(yin_match_keyword(st->yin_ctx, "list", strlen("list"), prefix, prefix_len, YANG_NONE), YANG_LIST);
243 assert_int_equal(yin_match_keyword(st->yin_ctx, "mandatory", strlen("mandatory"), prefix, prefix_len, YANG_NONE), YANG_MANDATORY);
244 assert_int_equal(yin_match_keyword(st->yin_ctx, "max-elements", strlen("max-elements"), prefix, prefix_len, YANG_NONE), YANG_MAX_ELEMENTS);
245 assert_int_equal(yin_match_keyword(st->yin_ctx, "min-elements", strlen("min-elements"), prefix, prefix_len, YANG_NONE), YANG_MIN_ELEMENTS);
246 assert_int_equal(yin_match_keyword(st->yin_ctx, "modifier", strlen("modifier"), prefix, prefix_len, YANG_NONE), YANG_MODIFIER);
247 assert_int_equal(yin_match_keyword(st->yin_ctx, "module", strlen("module"), prefix, prefix_len, YANG_NONE), YANG_MODULE);
248 assert_int_equal(yin_match_keyword(st->yin_ctx, "must", strlen("must"), prefix, prefix_len, YANG_NONE), YANG_MUST);
249 assert_int_equal(yin_match_keyword(st->yin_ctx, "namespace", strlen("namespace"), prefix, prefix_len, YANG_NONE), YANG_NAMESPACE);
250 assert_int_equal(yin_match_keyword(st->yin_ctx, "notification", strlen("notification"), prefix, prefix_len, YANG_NONE), YANG_NOTIFICATION);
251 assert_int_equal(yin_match_keyword(st->yin_ctx, "ordered-by", strlen("ordered-by"), prefix, prefix_len, YANG_NONE), YANG_ORDERED_BY);
252 assert_int_equal(yin_match_keyword(st->yin_ctx, "organization", strlen("organization"), prefix, prefix_len, YANG_NONE), YANG_ORGANIZATION);
253 assert_int_equal(yin_match_keyword(st->yin_ctx, "output", strlen("output"), prefix, prefix_len, YANG_NONE), YANG_OUTPUT);
254 assert_int_equal(yin_match_keyword(st->yin_ctx, "path", strlen("path"), prefix, prefix_len, YANG_NONE), YANG_PATH);
255 assert_int_equal(yin_match_keyword(st->yin_ctx, "pattern", strlen("pattern"), prefix, prefix_len, YANG_NONE), YANG_PATTERN);
256 assert_int_equal(yin_match_keyword(st->yin_ctx, "position", strlen("position"), prefix, prefix_len, YANG_NONE), YANG_POSITION);
257 assert_int_equal(yin_match_keyword(st->yin_ctx, "prefix", strlen("prefix"), prefix, prefix_len, YANG_NONE), YANG_PREFIX);
258 assert_int_equal(yin_match_keyword(st->yin_ctx, "presence", strlen("presence"), prefix, prefix_len, YANG_NONE), YANG_PRESENCE);
259 assert_int_equal(yin_match_keyword(st->yin_ctx, "range", strlen("range"), prefix, prefix_len, YANG_NONE), YANG_RANGE);
260 assert_int_equal(yin_match_keyword(st->yin_ctx, "reference", strlen("reference"), prefix, prefix_len, YANG_NONE), YANG_REFERENCE);
261 assert_int_equal(yin_match_keyword(st->yin_ctx, "refine", strlen("refine"), prefix, prefix_len, YANG_NONE), YANG_REFINE);
262 assert_int_equal(yin_match_keyword(st->yin_ctx, "require-instance", strlen("require-instance"), prefix, prefix_len, YANG_NONE), YANG_REQUIRE_INSTANCE);
263 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision", strlen("revision"), prefix, prefix_len, YANG_NONE), YANG_REVISION);
264 assert_int_equal(yin_match_keyword(st->yin_ctx, "revision-date", strlen("revision-date"), prefix, prefix_len, YANG_NONE), YANG_REVISION_DATE);
265 assert_int_equal(yin_match_keyword(st->yin_ctx, "rpc", strlen("rpc"), prefix, prefix_len, YANG_NONE), YANG_RPC);
266 assert_int_equal(yin_match_keyword(st->yin_ctx, "status", strlen("status"), prefix, prefix_len, YANG_NONE), YANG_STATUS);
267 assert_int_equal(yin_match_keyword(st->yin_ctx, "submodule", strlen("submodule"), prefix, prefix_len, YANG_NONE), YANG_SUBMODULE);
268 assert_int_equal(yin_match_keyword(st->yin_ctx, "type", strlen("type"), prefix, prefix_len, YANG_NONE), YANG_TYPE);
269 assert_int_equal(yin_match_keyword(st->yin_ctx, "typedef", strlen("typedef"), prefix, prefix_len, YANG_NONE), YANG_TYPEDEF);
270 assert_int_equal(yin_match_keyword(st->yin_ctx, "unique", strlen("unique"), prefix, prefix_len, YANG_NONE), YANG_UNIQUE);
271 assert_int_equal(yin_match_keyword(st->yin_ctx, "units", strlen("units"), prefix, prefix_len, YANG_NONE), YANG_UNITS);
272 assert_int_equal(yin_match_keyword(st->yin_ctx, "uses", strlen("uses"), prefix, prefix_len, YANG_NONE), YANG_USES);
273 assert_int_equal(yin_match_keyword(st->yin_ctx, "value", strlen("value"), prefix, prefix_len, YANG_NONE), YANG_VALUE);
274 assert_int_equal(yin_match_keyword(st->yin_ctx, "when", strlen("when"), prefix, prefix_len, YANG_NONE), YANG_WHEN);
275 assert_int_equal(yin_match_keyword(st->yin_ctx, "yang-version", strlen("yang-version"), prefix, prefix_len, YANG_NONE), YANG_YANG_VERSION);
276 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 +0200277
278 st->finished_correctly = true;
David Sedlák872c7b42018-10-26 13:15:20 +0200279}
David Sedlák3b4db242018-10-19 16:11:01 +0200280
David Sedlák872c7b42018-10-26 13:15:20 +0200281static void
David Sedlák060b00e2019-06-19 11:12:06 +0200282test_yin_match_argument_name(void **state)
David Sedlák872c7b42018-10-26 13:15:20 +0200283{
David Sedlák68a1af12019-03-08 13:46:54 +0100284 (void)state; /* unused */
David Sedlák872c7b42018-10-26 13:15:20 +0200285
David Sedlák060b00e2019-06-19 11:12:06 +0200286 assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN);
287 assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN);
288 assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN);
289 assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION);
290 assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE);
291 assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE);
292 assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME);
293 assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG);
294 assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
295 assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT);
296 assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI);
297 assert_int_equal(yin_match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200298}
299
David Sedlák68a1af12019-03-08 13:46:54 +0100300static void
David Sedlákb1a78352019-06-28 16:16:29 +0200301test_yin_parse_element_generic(void **state)
302{
303 const char *prefix, *name;
304 struct state *st = *state;
305 struct lysp_ext_instance exts;
306 size_t prefix_len, name_len;
307 LY_ERR ret;
308
309 memset(&exts, 0, sizeof(exts));
310
311 const char *data = "<elem attr=\"value\">text_value</elem>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200312 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák4ffcec82019-07-25 15:10:21 +0200313 ret = yin_parse_element_generic(st->yin_ctx, name, name_len, &data, &exts.child);
David Sedlákb1a78352019-06-28 16:16:29 +0200314 assert_int_equal(ret, LY_SUCCESS);
David Sedlák1fdb2522019-07-09 16:22:57 +0200315 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákb1a78352019-06-28 16:16:29 +0200316 assert_string_equal(exts.child->stmt, "elem");
317 assert_string_equal(exts.child->arg, "text_value");
David Sedlákb1a78352019-06-28 16:16:29 +0200318 assert_string_equal(exts.child->child->stmt, "attr");
319 assert_string_equal(exts.child->child->arg, "value");
320 assert_true(exts.child->child->flags & LYS_YIN_ATTR);
David Sedlákb1a78352019-06-28 16:16:29 +0200321 lysp_ext_instance_free(st->ctx, &exts);
David Sedlák5392a212019-07-01 09:19:10 +0200322 st = reset_state(state);
323
324 data = "<elem></elem>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200325 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák4ffcec82019-07-25 15:10:21 +0200326 ret = yin_parse_element_generic(st->yin_ctx, name, name_len, &data, &exts.child);
David Sedlák5392a212019-07-01 09:19:10 +0200327 assert_int_equal(ret, LY_SUCCESS);
328 assert_string_equal(exts.child->stmt, "elem");
329 assert_null(exts.child->child);
330 assert_null(exts.child->arg);
David Sedlákda8ffa32019-07-08 14:17:10 +0200331 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlák5392a212019-07-01 09:19:10 +0200332 lysp_ext_instance_free(st->ctx, &exts);
333
David Sedlákb1a78352019-06-28 16:16:29 +0200334 st->finished_correctly = true;
335}
336
337static void
338test_yin_parse_extension_instance(void **state)
339{
340 LY_ERR ret;
341 struct state *st = *state;
342 const char *prefix, *name;
343 size_t prefix_len, name_len;
344 struct yin_arg_record *args = NULL;
345 struct lysp_ext_instance *exts = NULL;
David Sedlákb1a78352019-06-28 16:16:29 +0200346 const char *data = "<ext value1=\"test\" value=\"test2\"><subelem>text</subelem></ext>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200347 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
348 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák1f90d252019-07-10 17:09:32 +0200349 ret = yin_parse_extension_instance(st->yin_ctx, args, &data, name2fullname(name, prefix_len),
David Sedlák619db942019-07-03 14:47:30 +0200350 namelen2fulllen(name_len, prefix_len), LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200351 assert_int_equal(ret, LY_SUCCESS);
352 assert_string_equal(exts->name, "ext");
353 assert_int_equal(exts->insubstmt_index, 0);
354 assert_true(exts->insubstmt == LYEXT_SUBSTMT_CONTACT);
355 assert_true(exts->yin & LYS_YIN);
356 assert_string_equal(exts->child->stmt, "value1");
357 assert_string_equal(exts->child->arg, "test");
358 assert_null(exts->child->child);
359 assert_true(exts->child->flags & LYS_YIN_ATTR);
360 assert_string_equal(exts->child->next->stmt, "value");
361 assert_string_equal(exts->child->next->arg, "test2");
362 assert_null(exts->child->next->child);
363 assert_true(exts->child->next->flags & LYS_YIN_ATTR);
364
365 assert_string_equal(exts->child->next->next->stmt, "subelem");
366 assert_string_equal(exts->child->next->next->arg, "text");
367 assert_null(exts->child->next->next->child);
368 assert_null(exts->child->next->next->next);
369 assert_false(exts->child->next->next->flags & LYS_YIN_ATTR);
David Sedlákda8ffa32019-07-08 14:17:10 +0200370 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákb1a78352019-06-28 16:16:29 +0200371 LY_ARRAY_FREE(args);
372 lysp_ext_instance_free(st->ctx, exts);
373 LY_ARRAY_FREE(exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200374 exts = NULL;
375 args = NULL;
376 st = reset_state(state);
377
378 data = "<extension-elem />";
David Sedlákda8ffa32019-07-08 14:17:10 +0200379 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
380 yin_load_attributes(st->yin_ctx, &data, &args);
David Sedlák1f90d252019-07-10 17:09:32 +0200381 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 +0200382 assert_int_equal(ret, LY_SUCCESS);
383 assert_string_equal(exts->name, "extension-elem");
384 assert_null(exts->argument);
385 assert_null(exts->child);
386 assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT);
387 assert_int_equal(exts->insubstmt_index, 0);
388 assert_true(exts->yin & LYS_YIN);
David Sedlákda8ffa32019-07-08 14:17:10 +0200389 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlákf250ecf2019-07-01 11:02:05 +0200390 LY_ARRAY_FREE(args);
391 lysp_ext_instance_free(st->ctx, exts);
392 LY_ARRAY_FREE(exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200393 st->finished_correctly = true;
394}
395
David Sedlák555c7202019-07-04 12:14:12 +0200396static void
397test_yin_parse_content(void **state)
398{
399 struct state *st = *state;
400 LY_ERR ret = LY_SUCCESS;
401 struct sized_string name, prefix;
402 const char *data = "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
403 "<custom xmlns=\"my-ext\">"
404 "totally amazing extension"
405 "</custom>"
David Sedlák986cb412019-07-04 13:10:11 +0200406 "<extension name=\"ext\">"
407 "<argument name=\"argname\"></argument>"
408 "<description><text>desc</text></description>"
409 "<reference><text>ref</text></reference>"
410 "<status value=\"deprecated\"></status>"
411 "</extension>"
David Sedlák555c7202019-07-04 12:14:12 +0200412 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
David Sedlák32488102019-07-15 17:44:10 +0200413 "<if-feature name=\"foo\"></if-feature>"
David Sedlák32eee7b2019-07-09 12:38:44 +0200414 "<when condition=\"condition...\">"
415 "<reference><text>when_ref</text></reference>"
416 "<description><text>when_desc</text></description>"
417 "</when>"
David Sedláke1a30302019-07-10 13:49:38 +0200418 "<config value=\"true\"/>"
David Sedlákc1771b12019-07-10 15:55:46 +0200419 "<error-message>"
420 "<value>error-msg</value>"
421 "</error-message>"
David Sedlák2ce1be62019-07-10 16:15:09 +0200422 "<error-app-tag value=\"err-app-tag\"/>"
David Sedláka5b1d382019-07-10 16:31:09 +0200423 "<units name=\"radians\"></units>"
David Sedláke7084ce2019-07-10 16:44:15 +0200424 "<default value=\"default-value\"/>"
David Sedlák5545f5d2019-07-11 11:55:16 +0200425 "<position value=\"25\"></position>"
426 "<value value=\"-5\"/>"
David Sedlákcf5569a2019-07-11 13:31:34 +0200427 "<require-instance value=\"true\"></require-instance>"
David Sedlákb7296dd2019-07-11 14:58:38 +0200428 "<range value=\"5..10\" />"
David Sedlák438ae432019-07-11 15:36:54 +0200429 "<length value=\"baf\"/>"
David Sedlákd3983112019-07-12 11:20:56 +0200430 "<pattern value='pattern'>"
431 "<modifier value='invert-match'/>"
432 "</pattern>"
David Sedlákfd5b9c32019-07-12 15:33:13 +0200433 "<enum name=\"yay\">"
434 "</enum>"
David Sedlák555c7202019-07-04 12:14:12 +0200435 "</prefix>";
436 struct lysp_ext_instance *exts = NULL;
David Sedlák5f8191e2019-07-08 16:35:52 +0200437 const char **if_features = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200438 struct yin_arg_record *attrs = NULL;
David Sedláke7084ce2019-07-10 16:44:15 +0200439 const char *value, *err_msg, *app_tag, *units, *def;
David Sedlák986cb412019-07-04 13:10:11 +0200440 struct lysp_ext *ext_def = NULL;
David Sedlák32eee7b2019-07-09 12:38:44 +0200441 struct lysp_when *when_p = NULL;
David Sedlákcf5569a2019-07-11 13:31:34 +0200442 struct lysp_type_enum pos_enum = {}, val_enum = {};
David Sedlákfd5b9c32019-07-12 15:33:13 +0200443 struct lysp_type req_type = {}, range_type = {}, len_type = {}, patter_type = {}, enum_type = {};
David Sedláke1a30302019-07-10 13:49:38 +0200444 uint8_t config = 0;
David Sedlák555c7202019-07-04 12:14:12 +0200445
David Sedlákda8ffa32019-07-08 14:17:10 +0200446 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
447 yin_load_attributes(st->yin_ctx, &data, &attrs);
David Sedlák555c7202019-07-04 12:14:12 +0200448
David Sedlákfd5b9c32019-07-12 15:33:13 +0200449 struct yin_subelement subelems[17] = {
David Sedlák438ae432019-07-11 15:36:54 +0200450 {YANG_CONFIG, &config, 0},
David Sedlákc3da3ef2019-07-19 12:56:08 +0200451 {YANG_DEFAULT, &def, YIN_SUBELEM_UNIQUE},
David Sedlákfd5b9c32019-07-12 15:33:13 +0200452 {YANG_ENUM, &enum_type, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200453 {YANG_ERROR_APP_TAG, &app_tag, 0},
454 {YANG_ERROR_MESSAGE, &err_msg, 0},
455 {YANG_EXTENSION, &ext_def, 0},
456 {YANG_IF_FEATURE, &if_features, 0},
457 {YANG_LENGTH, &len_type, 0},
David Sedlákd3983112019-07-12 11:20:56 +0200458 {YANG_PATTERN, &patter_type, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200459 {YANG_RANGE, &range_type, 0},
460 {YANG_REQUIRE_INSTANCE, &req_type, 0},
461 {YANG_UNITS, &units, 0},
David Sedlák374d2b32019-07-17 15:06:55 +0200462 {YANG_POSITION, &pos_enum, 0},
David Sedlák438ae432019-07-11 15:36:54 +0200463 {YANG_VALUE, &val_enum, 0},
464 {YANG_WHEN, &when_p, 0},
465 {YANG_CUSTOM, NULL, 0},
466 {YIN_TEXT, &value, 0}
David Sedlákd3983112019-07-12 11:20:56 +0200467 };
David Sedlákfd5b9c32019-07-12 15:33:13 +0200468 ret = yin_parse_content(st->yin_ctx, subelems, 17, &data, YANG_PREFIX, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200469 assert_int_equal(ret, LY_SUCCESS);
David Sedlák1fdb2522019-07-09 16:22:57 +0200470 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
David Sedlák2ce1be62019-07-10 16:15:09 +0200471 /* check parsed values */
David Sedláke7084ce2019-07-10 16:44:15 +0200472 assert_string_equal(def, "default-value");
David Sedlák555c7202019-07-04 12:14:12 +0200473 assert_string_equal(exts->name, "custom");
474 assert_string_equal(exts->argument, "totally amazing extension");
475 assert_string_equal(value, "wsefsdf");
David Sedláka5b1d382019-07-10 16:31:09 +0200476 assert_string_equal(units, "radians");
David Sedlák32eee7b2019-07-09 12:38:44 +0200477 assert_string_equal(when_p->cond, "condition...");
478 assert_string_equal(when_p->dsc, "when_desc");
479 assert_string_equal(when_p->ref, "when_ref");
David Sedláke1a30302019-07-10 13:49:38 +0200480 assert_int_equal(config, LYS_CONFIG_W);
David Sedlák5545f5d2019-07-11 11:55:16 +0200481 assert_int_equal(pos_enum.value, 25);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200482 assert_true(pos_enum.flags & LYS_SET_VALUE);
David Sedlák5545f5d2019-07-11 11:55:16 +0200483 assert_int_equal(val_enum.value, -5);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200484 assert_true(val_enum.flags & LYS_SET_VALUE);
David Sedlákcf5569a2019-07-11 13:31:34 +0200485 assert_int_equal(req_type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200486 assert_true(req_type.flags &= LYS_SET_REQINST);
David Sedlákb7296dd2019-07-11 14:58:38 +0200487 assert_string_equal(range_type.range->arg, "5..10");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200488 assert_true(range_type.flags & LYS_SET_RANGE);
David Sedlákc1771b12019-07-10 15:55:46 +0200489 assert_string_equal(err_msg, "error-msg");
David Sedlák2ce1be62019-07-10 16:15:09 +0200490 assert_string_equal(app_tag, "err-app-tag");
David Sedlákfd5b9c32019-07-12 15:33:13 +0200491 assert_string_equal(enum_type.enums->name, "yay");
David Sedlák438ae432019-07-11 15:36:54 +0200492 assert_string_equal(len_type.length->arg, "baf");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200493 assert_true(len_type.flags & LYS_SET_LENGTH);
David Sedlákd3983112019-07-12 11:20:56 +0200494 assert_string_equal(patter_type.patterns->arg, "\x015pattern");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200495 assert_true(patter_type.flags & LYS_SET_PATTERN);
David Sedlák2ce1be62019-07-10 16:15:09 +0200496 /* cleanup */
David Sedlák555c7202019-07-04 12:14:12 +0200497 lysp_ext_instance_free(st->ctx, exts);
David Sedlák32eee7b2019-07-09 12:38:44 +0200498 lysp_when_free(st->ctx, when_p);
David Sedlák986cb412019-07-04 13:10:11 +0200499 lysp_ext_free(st->ctx, ext_def);
David Sedlák5f8191e2019-07-08 16:35:52 +0200500 FREE_STRING(st->ctx, *if_features);
David Sedlákc1771b12019-07-10 15:55:46 +0200501 FREE_STRING(st->ctx, err_msg);
David Sedlák2ce1be62019-07-10 16:15:09 +0200502 FREE_STRING(st->ctx, app_tag);
David Sedláka5b1d382019-07-10 16:31:09 +0200503 FREE_STRING(st->ctx, units);
David Sedlákd3983112019-07-12 11:20:56 +0200504 FREE_STRING(st->ctx, patter_type.patterns->arg);
David Sedláke7084ce2019-07-10 16:44:15 +0200505 FREE_STRING(st->ctx, def);
David Sedlákb7296dd2019-07-11 14:58:38 +0200506 FREE_STRING(st->ctx, range_type.range->arg);
David Sedlák438ae432019-07-11 15:36:54 +0200507 FREE_STRING(st->ctx, len_type.length->arg);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200508 FREE_STRING(st->ctx, enum_type.enums->name);
David Sedlákb7296dd2019-07-11 14:58:38 +0200509 FREE_STRING(st->ctx, value);
David Sedlák5f8191e2019-07-08 16:35:52 +0200510 LY_ARRAY_FREE(if_features);
David Sedlák555c7202019-07-04 12:14:12 +0200511 LY_ARRAY_FREE(exts);
David Sedlák986cb412019-07-04 13:10:11 +0200512 LY_ARRAY_FREE(ext_def);
David Sedlák555c7202019-07-04 12:14:12 +0200513 LY_ARRAY_FREE(attrs);
David Sedlákd3983112019-07-12 11:20:56 +0200514 LY_ARRAY_FREE(patter_type.patterns);
David Sedlákfd5b9c32019-07-12 15:33:13 +0200515 LY_ARRAY_FREE(enum_type.enums);
David Sedlák32eee7b2019-07-09 12:38:44 +0200516 free(when_p);
David Sedlákb7296dd2019-07-11 14:58:38 +0200517 free(range_type.range);
David Sedlák438ae432019-07-11 15:36:54 +0200518 free(len_type.length);
David Sedlák555c7202019-07-04 12:14:12 +0200519 attrs = NULL;
David Sedlák555c7202019-07-04 12:14:12 +0200520 st = reset_state(state);
521
522 /* test unique subelem */
523 const char *prefix_value;
524 struct yin_subelement subelems2[2] = {{YANG_PREFIX, &prefix_value, 0},
525 {YIN_TEXT, &value, YIN_SUBELEM_UNIQUE}};
526 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
527 "<prefix value=\"inv_mod\" />"
528 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
529 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
530 "</module>";
David Sedlákda8ffa32019-07-08 14:17:10 +0200531 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
532 yin_load_attributes(st->yin_ctx, &data, &attrs);
533 ret = yin_parse_content(st->yin_ctx, subelems2, 2, &data, YANG_MODULE, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200534 assert_int_equal(ret, LY_EVALID);
535 logbuf_assert("Redefinition of text element in module element. Line number 1.");
536 lydict_remove(st->ctx, prefix_value);
537 lydict_remove(st->ctx, value);
538 st = reset_state(state);
539 LY_ARRAY_FREE(attrs);
540 attrs = NULL;
541
542 /* test first subelem */
543 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
544 "<prefix value=\"inv_mod\" />"
545 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
546 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
547 "</module>";
548 struct yin_subelement subelems3[2] = {{YANG_PREFIX, &prefix_value, 0},
549 {YIN_TEXT, &value, YIN_SUBELEM_FIRST}};
David Sedlákda8ffa32019-07-08 14:17:10 +0200550 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
551 yin_load_attributes(st->yin_ctx, &data, &attrs);
552 ret = yin_parse_content(st->yin_ctx, subelems3, 2, &data, YANG_MODULE, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200553 assert_int_equal(ret, LY_EVALID);
554 logbuf_assert("Subelement text of module element must be defined as first subelement. Line number 1.");
555 lydict_remove(st->ctx, prefix_value);
556 st = reset_state(state);
557 LY_ARRAY_FREE(attrs);
558 attrs = NULL;
559
560 /* test mandatory subelem */
561 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
562 "</module>";
563 struct yin_subelement subelems4[1] = {{YANG_PREFIX, &prefix_value, YIN_SUBELEM_MANDATORY}};
David Sedlákda8ffa32019-07-08 14:17:10 +0200564 lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
565 yin_load_attributes(st->yin_ctx, &data, &attrs);
566 ret = yin_parse_content(st->yin_ctx, subelems4, 1, &data, YANG_MODULE, NULL, &exts);
David Sedlák555c7202019-07-04 12:14:12 +0200567 assert_int_equal(ret, LY_EVALID);
568 logbuf_assert("Missing mandatory subelement prefix of module element. Line number 1.");
569 LY_ARRAY_FREE(attrs);
570
571 st->finished_correctly = true;
572}
573
David Sedlák92147b02019-07-09 14:01:01 +0200574static void
David Sedlák4a650532019-07-10 11:55:18 +0200575test_validate_value(void **state)
576{
577 struct state *st = *state;
578 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "#invalid", 8), LY_EVALID);
579 logbuf_assert("Invalid identifier character '#'. Line number 1.");
580 assert_int_equal(yin_validate_value(st->yin_ctx, Y_STR_ARG, "", 0), LY_SUCCESS);
581 assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "pre:b", 5), LY_EVALID);
582 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:b", 5), LY_SUCCESS);
583 assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:pre:b", 9), LY_EVALID);
584
585 st->finished_correctly = true;
586}
587
David Sedlák32488102019-07-15 17:44:10 +0200588static int
589setup_element_test(void **state)
590{
David Sedlák8e7bda82019-07-16 17:57:50 +0200591 struct state *st = *state;
David Sedlák32488102019-07-15 17:44:10 +0200592
593#if ENABLE_LOGGER_CHECKING
594 /* setup logger */
595 ly_set_log_clb(logger, 1);
596#endif
597
598 /* reset logbuf */
599 logbuf[0] = '\0';
David Sedlák32488102019-07-15 17:44:10 +0200600
601 /* allocate parser context */
602 st->yin_ctx = calloc(1, sizeof(*st->yin_ctx));
603 st->yin_ctx->xml_ctx.ctx = st->ctx;
604 st->yin_ctx->xml_ctx.line = 1;
605
606 return EXIT_SUCCESS;
607}
608
609static int
610teardown_element_test(void **state)
611{
612 struct state *st = *(struct state **)state;
613
614#if ENABLE_LOGGER_CHECKING
615 /* teardown logger */
616 if (!st->finished_correctly && logbuf[0] != '\0') {
617 fprintf(stderr, "%s\n", logbuf);
618 }
619#endif
620
621 lyxml_context_clear(&st->yin_ctx->xml_ctx);
David Sedlák32488102019-07-15 17:44:10 +0200622 free(st->yin_ctx);
David Sedlák32488102019-07-15 17:44:10 +0200623
624 return EXIT_SUCCESS;
625}
626
627#define ELEMENT_WRAPPER_START "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
628#define ELEMENT_WRAPPER_END "</module>"
629
630/* helper function to simplify unit test of each element using parse_content function */
631LY_ERR
632test_element_helper(struct state *st, const char **data, void *dest, const char **text,
633 struct lysp_ext_instance **exts, bool valid)
634{
635 struct yin_arg_record *attrs = NULL;
636 struct sized_string name, prefix;
637 LY_ERR ret = LY_SUCCESS;
638 struct yin_subelement subelems[71] = {
639 {YANG_ACTION, dest, 0},
640 {YANG_ANYDATA, dest, 0},
641 {YANG_ANYXML, dest, 0},
642 {YANG_ARGUMENT,dest, 0},
643 {YANG_AUGMENT, dest, 0},
644 {YANG_BASE, dest, 0},
645 {YANG_BELONGS_TO, dest, 0},
646 {YANG_BIT, dest, 0},
647 {YANG_CASE, dest, 0},
648 {YANG_CHOICE, dest, 0},
649 {YANG_CONFIG, dest, 0},
650 {YANG_CONTACT, dest, 0},
651 {YANG_CONTAINER, dest, 0},
David Sedlákc3da3ef2019-07-19 12:56:08 +0200652 {YANG_DEFAULT, dest, YIN_SUBELEM_UNIQUE},
David Sedlák32488102019-07-15 17:44:10 +0200653 {YANG_DESCRIPTION, dest, 0},
654 {YANG_DEVIATE, dest, 0},
655 {YANG_DEVIATION, dest, 0},
656 {YANG_ENUM, dest, 0},
657 {YANG_ERROR_APP_TAG, dest, 0},
658 {YANG_ERROR_MESSAGE, dest, 0},
659 {YANG_EXTENSION, dest, 0},
660 {YANG_FEATURE, dest, 0},
661 {YANG_FRACTION_DIGITS, dest, 0},
662 {YANG_GROUPING, dest, 0},
663 {YANG_IDENTITY, dest, 0},
664 {YANG_IF_FEATURE, dest, 0},
665 {YANG_IMPORT, dest, 0},
666 {YANG_INCLUDE, dest, 0},
667 {YANG_INPUT, dest, 0},
668 {YANG_KEY, dest, 0},
669 {YANG_LEAF, dest, 0},
670 {YANG_LEAF_LIST, dest, 0},
671 {YANG_LENGTH, dest, 0},
672 {YANG_LIST, dest, 0},
673 {YANG_MANDATORY, dest, 0},
674 {YANG_MAX_ELEMENTS, dest, 0},
675 {YANG_MIN_ELEMENTS, dest, 0},
676 {YANG_MODIFIER, dest, 0},
677 {YANG_MODULE, dest, 0},
678 {YANG_MUST, dest, 0},
679 {YANG_NAMESPACE, dest, 0},
680 {YANG_NOTIFICATION, dest, 0},
681 {YANG_ORDERED_BY, dest, 0},
682 {YANG_ORGANIZATION, dest, 0},
683 {YANG_OUTPUT, dest, 0},
684 {YANG_PATH, dest, 0},
685 {YANG_PATTERN, dest, 0},
David Sedlák32488102019-07-15 17:44:10 +0200686 {YANG_PREFIX, dest, 0},
687 {YANG_PRESENCE, dest, 0},
688 {YANG_RANGE, dest, 0},
689 {YANG_REFERENCE, dest, 0},
690 {YANG_REFINE, dest, 0},
691 {YANG_REQUIRE_INSTANCE, dest, 0},
692 {YANG_REVISION, dest, 0},
693 {YANG_REVISION_DATE, dest, 0},
694 {YANG_RPC, dest, 0},
695 {YANG_STATUS, dest, 0},
696 {YANG_SUBMODULE, dest, 0},
697 {YANG_TYPE, dest, 0},
698 {YANG_TYPEDEF, dest, 0},
699 {YANG_UNIQUE, dest, 0},
700 {YANG_UNITS, dest, 0},
701 {YANG_USES, dest, 0},
David Sedlák374d2b32019-07-17 15:06:55 +0200702 {YANG_POSITION, dest, 0},
David Sedlák32488102019-07-15 17:44:10 +0200703 {YANG_VALUE, dest, 0},
704 {YANG_WHEN, dest, 0},
705 {YANG_YANG_VERSION, dest, 0},
706 {YANG_YIN_ELEMENT, dest, 0},
707 {YANG_CUSTOM, dest, 0},
708 {YIN_TEXT, dest, 0},
709 {YIN_VALUE, dest, 0}
710 };
David Sedlák4f03b932019-07-26 13:01:47 +0200711 LY_CHECK_RET(lyxml_get_element(&st->yin_ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len));
712 LY_CHECK_RET(yin_load_attributes(st->yin_ctx, data, &attrs));
David Sedlák8e7bda82019-07-16 17:57:50 +0200713 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 +0200714 LY_ARRAY_FREE(attrs);
715 if (valid) {
716 assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
717 }
718 /* reset status */
719 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
720 return ret;
721}
722
723static void
724test_enum_bit_elem(void **state)
725{
726 /* yin_parse_enum_bit is function that is being mainly tested by this test */
727 struct state *st = *state;
728 struct lysp_type type = {};
729 const char *data;
730 data = ELEMENT_WRAPPER_START
731 "<enum name=\"enum-name\">"
732 "<if-feature name=\"feature\" />"
733 "<value value=\"55\" />"
734 "<status value=\"deprecated\" />"
735 "<description><text>desc...</text></description>"
736 "<reference><text>ref...</text></reference>"
737 "</enum>"
738 ELEMENT_WRAPPER_END;
739 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
740 assert_string_equal(*type.enums->iffeatures, "feature");
741 assert_int_equal(type.enums->value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200742 assert_true((type.enums->flags & LYS_STATUS_DEPRC) && (type.enums->flags & LYS_SET_VALUE));
David Sedlák32488102019-07-15 17:44:10 +0200743 assert_string_equal(type.enums->dsc, "desc...");
744 assert_string_equal(type.enums->ref, "ref...");
745 lysp_type_free(st->ctx, &type);
746 memset(&type, 0, sizeof type);
747
748 /* todo bit element test */
749 st->finished_correctly = true;
750}
751
752static void
753test_meta_elem(void **state)
754{
755 struct state *st = *state;
756 char *value = NULL;
757 const char *data;
758
759 /* organization element */
760 data = ELEMENT_WRAPPER_START
761 "<organization><text>organization...</text></organization>"
762 ELEMENT_WRAPPER_END;
763 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
764 assert_string_equal(value, "organization...");
765 FREE_STRING(st->ctx, value);
766 value = NULL;
767 /* contact element */
768 data = ELEMENT_WRAPPER_START
769 "<contact><text>contact...</text></contact>"
770 ELEMENT_WRAPPER_END;
771 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
772 assert_string_equal(value, "contact...");
773 FREE_STRING(st->ctx, value);
774 value = NULL;
775 /* description element */
776 data = ELEMENT_WRAPPER_START
777 "<description><text>description...</text></description>"
778 ELEMENT_WRAPPER_END;
779 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
780 assert_string_equal(value, "description...");
781 FREE_STRING(st->ctx, value);
782 value = NULL;
783 /* reference element */
784 data = ELEMENT_WRAPPER_START
785 "<reference><text>reference...</text></reference>"
786 ELEMENT_WRAPPER_END;
787 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
788 assert_string_equal(value, "reference...");
789 FREE_STRING(st->ctx, value);
790 value = NULL;
791
792 /* missing text subelement */
793 data = ELEMENT_WRAPPER_START
794 "<reference>reference...</reference>"
795 ELEMENT_WRAPPER_END;
796 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, false), LY_EVALID);
797 logbuf_assert("Missing mandatory subelement text of reference element. Line number 1.");
798
799 st->finished_correctly = true;
800}
801
802static void
803test_import_elem(void **state)
804{
805 struct state *st = *state;
806 const char *data;
David Sedlák298ff6d2019-07-26 14:29:03 +0200807 struct lysp_import *imports = NULL;
808 struct import_meta imp_meta = {"prefix", &imports};
David Sedlák32488102019-07-15 17:44:10 +0200809
810 /* max subelems */
811 data = ELEMENT_WRAPPER_START
812 "<import module=\"a\">"
813 "<prefix value=\"a_mod\"/>"
814 "<revision-date date=\"2015-01-01\"></revision-date>"
815 "<description><text>import description</text></description>"
816 "<reference><text>import reference</text></reference>"
817 "</import>"
818 ELEMENT_WRAPPER_END;
David Sedlák298ff6d2019-07-26 14:29:03 +0200819 assert_int_equal(test_element_helper(st, &data, &imp_meta, NULL, NULL, true), LY_SUCCESS);
820 assert_string_equal(imports->name, "a");
821 assert_string_equal(imports->prefix, "a_mod");
822 assert_string_equal(imports->rev, "2015-01-01");
823 assert_string_equal(imports->dsc, "import description");
824 assert_string_equal(imports->ref, "import reference");
825 FREE_ARRAY(st->ctx, imports, lysp_import_free);
826 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200827
828 /* min subelems */
David Sedlák32488102019-07-15 17:44:10 +0200829 data = ELEMENT_WRAPPER_START
830 "<import module=\"a\">"
831 "<prefix value=\"a_mod\"/>"
832 "</import>"
833 ELEMENT_WRAPPER_END;
David Sedlák298ff6d2019-07-26 14:29:03 +0200834 assert_int_equal(test_element_helper(st, &data, &imp_meta, NULL, NULL, true), LY_SUCCESS);
835 assert_string_equal(imports->prefix, "a_mod");
836 FREE_ARRAY(st->ctx, imports, lysp_import_free);
837 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200838
839 /* invalid (missing prefix) */
David Sedlák298ff6d2019-07-26 14:29:03 +0200840 data = ELEMENT_WRAPPER_START "<import module=\"a\"></import>" ELEMENT_WRAPPER_END;
841 assert_int_equal(test_element_helper(st, &data, &imp_meta, NULL, NULL, false), LY_EVALID);
David Sedlák32488102019-07-15 17:44:10 +0200842 logbuf_assert("Missing mandatory subelement prefix of import element. Line number 1.");
David Sedlák298ff6d2019-07-26 14:29:03 +0200843 FREE_ARRAY(st->ctx, imports, lysp_import_free);
844 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200845
846 /* invalid reused prefix */
David Sedlák32488102019-07-15 17:44:10 +0200847 data = ELEMENT_WRAPPER_START
848 "<import module=\"a\">"
David Sedlák298ff6d2019-07-26 14:29:03 +0200849 "<prefix value=\"prefix\"/>"
David Sedlák32488102019-07-15 17:44:10 +0200850 "</import>"
851 "<import module=\"a\">"
David Sedlák298ff6d2019-07-26 14:29:03 +0200852 "<prefix value=\"prefix\"/>"
David Sedlák32488102019-07-15 17:44:10 +0200853 "</import>"
854 ELEMENT_WRAPPER_END;
David Sedlák298ff6d2019-07-26 14:29:03 +0200855 assert_int_equal(test_element_helper(st, &data, &imp_meta, NULL, NULL, false), LY_EVALID);
856 logbuf_assert("Prefix \"prefix\" already used as module prefix. Line number 1.");
857 FREE_ARRAY(st->ctx, imports, lysp_import_free);
858 imports = NULL;
David Sedlák32488102019-07-15 17:44:10 +0200859
860 st->finished_correctly = true;
861}
862
863static void
864test_status_elem(void **state)
865{
866 struct state *st = *state;
867 const char *data;
868 uint16_t flags = 0;
869
870 /* test valid values */
871 data = ELEMENT_WRAPPER_START "<status value=\"current\" />" ELEMENT_WRAPPER_END;
872 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200873 assert_true(flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +0200874
875 data = ELEMENT_WRAPPER_START "<status value=\"deprecated\" />" ELEMENT_WRAPPER_END;
876 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200877 assert_true(flags & LYS_STATUS_DEPRC);
David Sedlák32488102019-07-15 17:44:10 +0200878
879 data = ELEMENT_WRAPPER_START "<status value=\"obsolete\"></status>" ELEMENT_WRAPPER_END;
880 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200881 assert_true(flags & LYS_STATUS_OBSLT);
David Sedlák32488102019-07-15 17:44:10 +0200882
883 /* test invalid value */
884 data = ELEMENT_WRAPPER_START "<status value=\"invalid\"></status>" ELEMENT_WRAPPER_END;
885 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
886 logbuf_assert("Invalid value \"invalid\" of \"status\". Line number 1.");
887 st->finished_correctly = true;
888}
889
890static void
891test_ext_elem(void **state)
892{
893 struct state *st = *state;
894 const char *data;
895 struct lysp_ext *ext = NULL;
896
897 /* max subelems */
898 data = ELEMENT_WRAPPER_START
899 "<extension name=\"ext_name\">"
900 "<argument name=\"arg\"></argument>"
901 "<status value=\"current\"/>"
902 "<description><text>ext_desc</text></description>"
903 "<reference><text>ext_ref</text></reference>"
904 "</extension>"
905 ELEMENT_WRAPPER_END;
906 assert_int_equal(test_element_helper(st, &data, &ext, NULL, NULL, true), LY_SUCCESS);
907 assert_string_equal(ext->name, "ext_name");
908 assert_string_equal(ext->argument, "arg");
David Sedlákc3da3ef2019-07-19 12:56:08 +0200909 assert_true(ext->flags & LYS_STATUS_CURR);
David Sedlák32488102019-07-15 17:44:10 +0200910 assert_string_equal(ext->dsc, "ext_desc");
911 assert_string_equal(ext->ref, "ext_ref");
912 lysp_ext_free(st->ctx, ext);
913 LY_ARRAY_FREE(ext);
914 ext = NULL;
915
916 /* min subelems */
917 data = ELEMENT_WRAPPER_START "<extension name=\"ext_name\"></extension>" ELEMENT_WRAPPER_END;
918 assert_int_equal(test_element_helper(st, &data, &ext, NULL, NULL, true), LY_SUCCESS);
919 assert_string_equal(ext->name, "ext_name");
920 lysp_ext_free(st->ctx, ext);
921 LY_ARRAY_FREE(ext);
922 ext = NULL;
923
924 st->finished_correctly = true;
925}
926
927static void
928test_yin_element_elem(void **state)
929{
930 struct state *st = *state;
931 const char *data;
932 uint16_t flags = 0;
933
934 data = ELEMENT_WRAPPER_START "<yin-element value=\"true\" />" ELEMENT_WRAPPER_END;
935 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200936 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +0200937
938 data = ELEMENT_WRAPPER_START "<yin-element value=\"false\" />" ELEMENT_WRAPPER_END;
939 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200940 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +0200941
942 data = ELEMENT_WRAPPER_START "<yin-element value=\"invalid\" />" ELEMENT_WRAPPER_END;
943 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200944 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák32488102019-07-15 17:44:10 +0200945 logbuf_assert("Invalid value \"invalid\" of \"yin-element\". Line number 1.");
946 st->finished_correctly = true;
947}
948
949static void
950test_yangversion_elem(void **state)
951{
952 struct state *st = *state;
953 const char *data;
954 uint8_t version = 0;
955
956 /* valid values */
957 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.0\" />" ELEMENT_WRAPPER_END;
958 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200959 assert_true(version & LYS_VERSION_1_0);
David Sedlák32488102019-07-15 17:44:10 +0200960 assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_0);
961
962 data = ELEMENT_WRAPPER_START "<yang-version value=\"1.1\" />" ELEMENT_WRAPPER_END;
963 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +0200964 assert_true(version & LYS_VERSION_1_1);
David Sedlák32488102019-07-15 17:44:10 +0200965 assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_1);
966
967 /* invalid value */
968 data = ELEMENT_WRAPPER_START "<yang-version value=\"version\" />" ELEMENT_WRAPPER_END;
969 assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, false), LY_EVALID);
970 logbuf_assert("Invalid value \"version\" of \"yang-version\". Line number 1.");
971
972 st->finished_correctly = true;
973}
974
975static void
976test_mandatory_elem(void **state)
977{
978 struct state *st = *state;
979 const char *data;
980 uint16_t man = 0;
981
982 /* valid values */
983 data = ELEMENT_WRAPPER_START "<mandatory value=\"true\" />" ELEMENT_WRAPPER_END;
984 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, true), LY_SUCCESS);
985 assert_int_equal(man, LYS_MAND_TRUE);
986 man = 0;
987
988 data = ELEMENT_WRAPPER_START "<mandatory value=\"false\" />" ELEMENT_WRAPPER_END;
989 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, true), LY_SUCCESS);
990 assert_int_equal(man, LYS_MAND_FALSE);
991
992 data = ELEMENT_WRAPPER_START "<mandatory value=\"invalid\" />" ELEMENT_WRAPPER_END;
993 assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, false), LY_EVALID);
994 logbuf_assert("Invalid value \"invalid\" of \"mandatory\". Line number 1.");
995
996 st->finished_correctly = true;
997}
998
David Sedlák8e7bda82019-07-16 17:57:50 +0200999static void
1000test_argument_elem(void **state)
1001{
1002 struct state *st = *state;
1003 const char *data;
1004 uint16_t flags = 0;
1005 const char *arg;
1006 struct yin_argument_meta arg_meta = {&flags, &arg};
1007 /* max subelems */
1008 data = ELEMENT_WRAPPER_START
1009 "<argument name=\"arg-name\">"
1010 "<yin-element value=\"true\" />"
1011 "</argument>"
1012 ELEMENT_WRAPPER_END;
1013 assert_int_equal(test_element_helper(st, &data, &arg_meta, NULL, NULL, true), LY_SUCCESS);
1014 assert_string_equal(arg, "arg-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001015 assert_true(flags & LYS_YINELEM_TRUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001016 flags = 0;
1017 FREE_STRING(st->ctx, arg);
1018 arg = NULL;
1019
1020 /* min subelems */
1021 data = ELEMENT_WRAPPER_START
1022 "<argument name=\"arg\">"
1023 "</argument>"
1024 ELEMENT_WRAPPER_END;
1025 assert_int_equal(test_element_helper(st, &data, &arg_meta, NULL, NULL, true), LY_SUCCESS);
1026 assert_string_equal(arg, "arg");
1027 assert_true(flags == 0);
1028 FREE_STRING(st->ctx, arg);
1029
1030 st->finished_correctly = true;
1031}
1032
1033static void
1034test_base_elem(void **state)
1035{
1036 struct state *st = *state;
1037 const char *data;
1038 const char **bases = NULL;
1039 struct lysp_type type = {};
1040
1041 /* as identity subelement */
1042 data = "<identity xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
1043 "<base name=\"base-name\"/>"
1044 "</identity>";
1045 assert_int_equal(test_element_helper(st, &data, &bases, NULL, NULL, true), LY_SUCCESS);
1046 assert_string_equal(*bases, "base-name");
1047 FREE_STRING(st->ctx, *bases);
1048 LY_ARRAY_FREE(bases);
1049
1050 /* as type subelement */
1051 data = "<type xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
1052 "<base name=\"base-name\"/>"
1053 "</type>";
1054 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1055 assert_string_equal(*type.bases, "base-name");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001056 assert_true(type.flags & LYS_SET_BASE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001057 FREE_STRING(st->ctx, *type.bases);
1058 LY_ARRAY_FREE(type.bases);
1059
1060 st->finished_correctly = true;
1061}
1062
1063static void
1064test_belongsto_elem(void **state)
1065{
1066 struct state *st = *state;
1067 const char *data;
1068 struct lysp_submodule submod;
1069
1070 data = ELEMENT_WRAPPER_START
1071 "<belongs-to module=\"module-name\"><prefix value=\"pref\"/></belongs-to>"
1072 ELEMENT_WRAPPER_END;
1073 assert_int_equal(test_element_helper(st, &data, &submod, NULL, NULL, true), LY_SUCCESS);
1074 assert_string_equal(submod.belongsto, "module-name");
1075 assert_string_equal(submod.prefix, "pref");
1076 FREE_STRING(st->ctx, submod.belongsto);
1077 FREE_STRING(st->ctx, submod.prefix);
1078
1079 data = ELEMENT_WRAPPER_START "<belongs-to module=\"module-name\"></belongs-to>" ELEMENT_WRAPPER_END;
1080 assert_int_equal(test_element_helper(st, &data, &submod, NULL, NULL, false), LY_EVALID);
1081 logbuf_assert("Missing mandatory subelement prefix of belongs-to element. Line number 1.");
1082 FREE_STRING(st->ctx, submod.belongsto);
1083
1084 st->finished_correctly = true;
1085}
1086
1087static void
1088test_config_elem(void **state)
1089{
1090 struct state *st = *state;
1091 const char *data;
1092 uint16_t flags = 0;
1093
1094 data = ELEMENT_WRAPPER_START "<config value=\"true\"/>" ELEMENT_WRAPPER_END;
1095 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001096 assert_true(flags & LYS_CONFIG_W);
David Sedlák8e7bda82019-07-16 17:57:50 +02001097 flags = 0;
1098
1099 data = ELEMENT_WRAPPER_START "<config value=\"false\"/>" ELEMENT_WRAPPER_END;
1100 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001101 assert_true(flags & LYS_CONFIG_R);
David Sedlák8e7bda82019-07-16 17:57:50 +02001102 flags = 0;
1103
1104 data = ELEMENT_WRAPPER_START "<config value=\"invalid\"/>" ELEMENT_WRAPPER_END;
1105 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
1106 logbuf_assert("Invalid value \"invalid\" of \"config\". Line number 1.");
1107
1108 st->finished_correctly = true;
1109}
1110
1111static void
1112test_default_elem(void **state)
1113{
1114 struct state *st = *state;
1115 const char *data;
1116 const char *val = NULL;
1117
1118 data = ELEMENT_WRAPPER_START "<default value=\"defaul-value\"/>" ELEMENT_WRAPPER_END;
1119 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1120 assert_string_equal(val, "defaul-value");
1121 FREE_STRING(st->ctx, val);
1122 val = NULL;
1123
1124 data = ELEMENT_WRAPPER_START "<default/>" ELEMENT_WRAPPER_END;
1125 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1126 logbuf_assert("Missing mandatory attribute value of default element. Line number 1.");
1127
1128 st->finished_correctly = true;
1129}
1130
1131static void
1132test_err_app_tag_elem(void **state)
1133{
1134 struct state *st = *state;
1135 const char *data;
1136 const char *val = NULL;
1137
1138 data = ELEMENT_WRAPPER_START "<error-app-tag value=\"val\"/>" ELEMENT_WRAPPER_END;
1139 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1140 assert_string_equal(val, "val");
1141 FREE_STRING(st->ctx, val);
1142 val = NULL;
1143
1144 data = ELEMENT_WRAPPER_START "<error-app-tag/>" ELEMENT_WRAPPER_END;
1145 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1146 logbuf_assert("Missing mandatory attribute value of error-app-tag element. Line number 1.");
1147
1148 st->finished_correctly = true;
1149}
1150
1151static void
1152test_err_msg_elem(void **state)
1153{
1154 struct state *st = *state;
1155 const char *data;
1156 const char *val = NULL;
1157
1158 data = ELEMENT_WRAPPER_START "<error-message><value>val</value></error-message>" ELEMENT_WRAPPER_END;
1159 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1160 assert_string_equal(val, "val");
1161 FREE_STRING(st->ctx, val);
1162
1163 data = ELEMENT_WRAPPER_START "<error-message></error-message>" ELEMENT_WRAPPER_END;
1164 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
1165 logbuf_assert("Missing mandatory subelement value of error-message element. Line number 1.");
1166
1167 st->finished_correctly = true;
1168}
1169
1170static void
1171test_fracdigits_elem(void **state)
1172{
1173 struct state *st = *state;
1174 const char *data;
1175 struct lysp_type type = {};
1176
1177 /* valid value */
1178 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"10\"></fraction-digits>" ELEMENT_WRAPPER_END;
1179 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1180 assert_int_equal(type.fraction_digits, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001181 assert_true(type.flags & LYS_SET_FRDIGITS);
David Sedlák8e7bda82019-07-16 17:57:50 +02001182
1183 /* invalid values */
1184 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"-1\"></fraction-digits>" ELEMENT_WRAPPER_END;
1185 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1186 logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1.");
1187
1188 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"02\"></fraction-digits>" ELEMENT_WRAPPER_END;
1189 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1190 logbuf_assert("Invalid value \"02\" of \"fraction-digits\". Line number 1.");
1191
1192 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"1p\"></fraction-digits>" ELEMENT_WRAPPER_END;
1193 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1194 logbuf_assert("Invalid value \"1p\" of \"fraction-digits\". Line number 1.");
1195
1196 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"19\"></fraction-digits>" ELEMENT_WRAPPER_END;
1197 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1198 logbuf_assert("Invalid value \"19\" of \"fraction-digits\". Line number 1.");
1199
1200 data = ELEMENT_WRAPPER_START "<fraction-digits value=\"999999999999999999\"></fraction-digits>" ELEMENT_WRAPPER_END;
1201 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1202 logbuf_assert("Invalid value \"999999999999999999\" of \"fraction-digits\". Line number 1.");
1203
1204 st->finished_correctly = true;
1205}
1206
1207static void
1208test_iffeature_elem(void **state)
1209{
1210 struct state *st = *state;
1211 const char *data;
1212 const char **iffeatures = NULL;
1213
1214 data = ELEMENT_WRAPPER_START "<if-feature name=\"local-storage\"></if-feature>" ELEMENT_WRAPPER_END;
1215 assert_int_equal(test_element_helper(st, &data, &iffeatures, NULL, NULL, true), LY_SUCCESS);
1216 assert_string_equal(*iffeatures, "local-storage");
1217 FREE_STRING(st->ctx, *iffeatures);
1218 LY_ARRAY_FREE(iffeatures);
1219 iffeatures = NULL;
1220
1221 data = ELEMENT_WRAPPER_START "<if-feature/>" ELEMENT_WRAPPER_END;
1222 assert_int_equal(test_element_helper(st, &data, &iffeatures, NULL, NULL, false), LY_EVALID);
1223 logbuf_assert("Missing mandatory attribute name of if-feature element. Line number 1.");
1224 LY_ARRAY_FREE(iffeatures);
1225 iffeatures = NULL;
1226
1227 st->finished_correctly = true;
1228}
1229
1230static void
1231test_length_elem(void **state)
1232{
1233 struct state *st = *state;
1234 const char *data;
1235 struct lysp_type type = {};
1236
1237 /* max subelems */
1238 data = ELEMENT_WRAPPER_START
1239 "<length value=\"length-str\">"
1240 "<error-message><value>err-msg</value></error-message>"
1241 "<error-app-tag value=\"err-app-tag\"/>"
1242 "<description><text>desc</text></description>"
1243 "<reference><text>ref</text></reference>"
1244 "</length>"
1245 ELEMENT_WRAPPER_END;
1246 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1247 assert_string_equal(type.length->arg, "length-str");
1248 assert_string_equal(type.length->emsg, "err-msg");
1249 assert_string_equal(type.length->eapptag, "err-app-tag");
1250 assert_string_equal(type.length->dsc, "desc");
1251 assert_string_equal(type.length->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001252 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001253 lysp_type_free(st->ctx, &type);
1254 memset(&type, 0, sizeof(type));
1255
1256 /* min subelems */
1257 data = ELEMENT_WRAPPER_START
1258 "<length value=\"length-str\">"
1259 "</length>"
1260 ELEMENT_WRAPPER_END;
1261 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1262 assert_string_equal(type.length->arg, "length-str");
1263 lysp_type_free(st->ctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001264 assert_true(type.flags & LYS_SET_LENGTH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001265 memset(&type, 0, sizeof(type));
1266
1267 data = ELEMENT_WRAPPER_START "<length></length>" ELEMENT_WRAPPER_END;
1268 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1269 logbuf_assert("Missing mandatory attribute value of length element. Line number 1.");
1270 lysp_type_free(st->ctx, &type);
1271 memset(&type, 0, sizeof(type));
1272
1273 st->finished_correctly = true;
1274}
1275
1276static void
1277test_modifier_elem(void **state)
1278{
1279 struct state *st = *state;
1280 const char *data;
1281 const char *pat = lydict_insert(st->ctx, "\006pattern", 8);
1282
1283 data = ELEMENT_WRAPPER_START "<modifier value=\"invert-match\" />" ELEMENT_WRAPPER_END;
1284 assert_int_equal(test_element_helper(st, &data, &pat, NULL, NULL, true), LY_SUCCESS);
1285 assert_string_equal(pat, "\x015pattern");
1286 FREE_STRING(st->ctx, pat);
1287
1288 pat = lydict_insert(st->ctx, "\006pattern", 8);
1289 data = ELEMENT_WRAPPER_START "<modifier value=\"invert\" />" ELEMENT_WRAPPER_END;
1290 assert_int_equal(test_element_helper(st, &data, &pat, NULL, NULL, false), LY_EVALID);
1291 logbuf_assert("Invalid value \"invert\" of \"modifier\". Line number 1.");
1292 FREE_STRING(st->ctx, pat);
1293
1294 st->finished_correctly = true;
1295}
1296
1297static void
1298test_namespace_elem(void **state)
1299{
1300 struct state *st = *state;
1301 const char *data;
1302 const char *ns;
1303
1304 data = ELEMENT_WRAPPER_START "<namespace uri=\"ns\"/>" ELEMENT_WRAPPER_END;
1305 assert_int_equal(test_element_helper(st, &data, &ns, NULL, NULL, true), LY_SUCCESS);
1306 assert_string_equal(ns, "ns");
1307 FREE_STRING(st->ctx, ns);
1308
1309 data = ELEMENT_WRAPPER_START "<namespace/>" ELEMENT_WRAPPER_END;
1310 assert_int_equal(test_element_helper(st, &data, &ns, NULL, NULL, false), LY_EVALID);
1311 logbuf_assert("Missing mandatory attribute uri of namespace element. Line number 1.");
1312
1313 st->finished_correctly = true;
1314}
1315
1316static void
1317test_path_elem(void **state)
1318{
1319 struct state *st = *state;
1320 const char *data;
1321 struct lysp_type type = {};
1322
1323 data = ELEMENT_WRAPPER_START "<path value=\"path-val\"/>" ELEMENT_WRAPPER_END;
1324 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1325 assert_string_equal("path-val", type.path);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001326 assert_true(type.flags & LYS_SET_PATH);
David Sedlák8e7bda82019-07-16 17:57:50 +02001327 lysp_type_free(st->ctx, &type);
1328
1329 st->finished_correctly = true;
1330}
1331
1332static void
1333test_pattern_elem(void **state)
1334{
1335 struct state *st = *state;
1336 const char *data;
1337 struct lysp_type type = {};
1338
1339 /* max subelems */
1340 data = ELEMENT_WRAPPER_START
1341 "<pattern value=\"super_pattern\">"
1342 "<modifier value=\"invert-match\"/>"
1343 "<error-message><value>err-msg-value</value></error-message>"
1344 "<error-app-tag value=\"err-app-tag-value\"/>"
1345 "<description><text>pattern-desc</text></description>"
1346 "<reference><text>pattern-ref</text></reference>"
1347 "</pattern>"
1348 ELEMENT_WRAPPER_END;
1349 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001350 assert_true(type.flags & LYS_SET_PATTERN);
David Sedlák8e7bda82019-07-16 17:57:50 +02001351 assert_string_equal(type.patterns->arg, "\x015super_pattern");
1352 assert_string_equal(type.patterns->dsc, "pattern-desc");
1353 assert_string_equal(type.patterns->eapptag, "err-app-tag-value");
1354 assert_string_equal(type.patterns->emsg, "err-msg-value");
1355 assert_string_equal(type.patterns->dsc, "pattern-desc");
1356 assert_string_equal(type.patterns->ref, "pattern-ref");
1357 lysp_type_free(st->ctx, &type);
1358 memset(&type, 0, sizeof(type));
1359
1360 /* min subelems */
1361 data = ELEMENT_WRAPPER_START "<pattern value=\"pattern\"> </pattern>" ELEMENT_WRAPPER_END;
1362 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1363 assert_string_equal(type.patterns->arg, "\x006pattern");
1364 lysp_type_free(st->ctx, &type);
1365 memset(&type, 0, sizeof(type));
1366
1367 st->finished_correctly = true;
1368}
1369
1370static void
1371test_value_position_elem(void **state)
1372{
1373 struct state *st = *state;
1374 const char *data;
1375 struct lysp_type_enum en = {};
1376
1377 /* valid values */
1378 data = ELEMENT_WRAPPER_START "<value value=\"55\" />" ELEMENT_WRAPPER_END;
1379 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1380 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001381 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001382 memset(&en, 0, sizeof(en));
1383
1384 data = ELEMENT_WRAPPER_START "<value value=\"-55\"/>" ELEMENT_WRAPPER_END;
1385 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1386 assert_int_equal(en.value, -55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001387 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001388 memset(&en, 0, sizeof(en));
1389
1390 data = ELEMENT_WRAPPER_START "<value value=\"0\"/>" ELEMENT_WRAPPER_END;
1391 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1392 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001393 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001394 memset(&en, 0, sizeof(en));
1395
1396 data = ELEMENT_WRAPPER_START "<value value=\"-0\"/>" ELEMENT_WRAPPER_END;
1397 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1398 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001399 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001400 memset(&en, 0, sizeof(en));
1401
1402 /* valid positions */
1403 data = ELEMENT_WRAPPER_START "<position value=\"55\" />" ELEMENT_WRAPPER_END;
1404 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1405 assert_int_equal(en.value, 55);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001406 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001407 memset(&en, 0, sizeof(en));
1408
1409 data = ELEMENT_WRAPPER_START "<position value=\"0\" />" ELEMENT_WRAPPER_END;
1410 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, true), LY_SUCCESS);
1411 assert_int_equal(en.value, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001412 assert_true(en.flags & LYS_SET_VALUE);
David Sedlák8e7bda82019-07-16 17:57:50 +02001413 memset(&en, 0, sizeof(en));
1414
1415 /* invalid values */
1416 data = ELEMENT_WRAPPER_START "<value value=\"99999999999999999999999\"/>" ELEMENT_WRAPPER_END;
1417 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1418 logbuf_assert("Invalid value \"99999999999999999999999\" of \"value\". Line number 1.");
1419
1420 data = ELEMENT_WRAPPER_START "<value value=\"1k\"/>" ELEMENT_WRAPPER_END;
1421 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1422 logbuf_assert("Invalid value \"1k\" of \"value\". Line number 1.");
1423
David Sedlák69f01612019-07-17 11:41:08 +02001424 data = ELEMENT_WRAPPER_START "<value value=\"\"/>" ELEMENT_WRAPPER_END;
1425 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1426 logbuf_assert("Invalid value \"\" of \"value\". Line number 1.");
1427
David Sedlák8e7bda82019-07-16 17:57:50 +02001428 /*invalid positions */
1429 data = ELEMENT_WRAPPER_START "<position value=\"-5\"/>" ELEMENT_WRAPPER_END;
1430 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1431 logbuf_assert("Invalid value \"-5\" of \"position\". Line number 1.");
1432
1433 data = ELEMENT_WRAPPER_START "<position value=\"-0\"/>" ELEMENT_WRAPPER_END;
1434 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1435 logbuf_assert("Invalid value \"-0\" of \"position\". Line number 1.");
1436
1437 data = ELEMENT_WRAPPER_START "<position value=\"99999999999999999999\"/>" ELEMENT_WRAPPER_END;
1438 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1439 logbuf_assert("Invalid value \"99999999999999999999\" of \"position\". Line number 1.");
1440
David Sedlák69f01612019-07-17 11:41:08 +02001441 data = ELEMENT_WRAPPER_START "<position value=\"\"/>" ELEMENT_WRAPPER_END;
1442 assert_int_equal(test_element_helper(st, &data, &en, NULL, NULL, false), LY_EVALID);
1443 logbuf_assert("Invalid value \"\" of \"position\". Line number 1.");
1444
1445 st->finished_correctly = true;
1446}
1447
1448static void
1449test_prefix_elem(void **state)
1450{
1451 struct state *st = *state;
1452 const char *data;
1453 const char *value = NULL;
1454
1455 data = ELEMENT_WRAPPER_START "<prefix value=\"pref\"/>" ELEMENT_WRAPPER_END;
1456 assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS);
1457 assert_string_equal(value, "pref");
1458 FREE_STRING(st->ctx, value);
1459
1460 st->finished_correctly = true;
1461}
1462
1463static void
1464test_range_elem(void **state)
1465{
1466 struct state *st = *state;
1467 const char *data;
1468 struct lysp_type type = {};
1469
1470 /* max subelems */
1471 data = ELEMENT_WRAPPER_START
1472 "<range value=\"range-str\">"
1473 "<error-message><value>err-msg</value></error-message>"
1474 "<error-app-tag value=\"err-app-tag\" />"
1475 "<description><text>desc</text></description>"
1476 "<reference><text>ref</text></reference>"
1477 "</range>"
1478 ELEMENT_WRAPPER_END;
1479 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1480 assert_string_equal(type.range->arg, "range-str");
1481 assert_string_equal(type.range->dsc, "desc");
1482 assert_string_equal(type.range->eapptag, "err-app-tag");
1483 assert_string_equal(type.range->emsg, "err-msg");
1484 assert_string_equal(type.range->ref, "ref");
David Sedlákc3da3ef2019-07-19 12:56:08 +02001485 assert_true(type.flags & LYS_SET_RANGE);
David Sedlák69f01612019-07-17 11:41:08 +02001486 lysp_type_free(st->ctx, &type);
1487 memset(&type, 0, sizeof(type));
1488
1489 /* min subelems */
1490 data = ELEMENT_WRAPPER_START "<range value=\"range-str\"/>" ELEMENT_WRAPPER_END;
1491 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1492 assert_string_equal(type.range->arg, "range-str");
1493 lysp_type_free(st->ctx, &type);
1494 memset(&type, 0, sizeof(type));
1495
1496 st->finished_correctly = true;
1497}
1498
1499static void
1500test_reqinstance_elem(void **state)
1501{
1502 struct state *st = *state;
1503 const char *data;
1504 struct lysp_type type = {};
1505
1506 data = ELEMENT_WRAPPER_START "<require-instance value=\"true\"/>" ELEMENT_WRAPPER_END;
1507 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1508 assert_int_equal(type.require_instance, 1);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001509 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001510 memset(&type, 0, sizeof(type));
1511
1512 data = ELEMENT_WRAPPER_START "<require-instance value=\"false\"/>" ELEMENT_WRAPPER_END;
1513 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1514 assert_int_equal(type.require_instance, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001515 assert_true(type.flags & LYS_SET_REQINST);
David Sedlák69f01612019-07-17 11:41:08 +02001516 memset(&type, 0, sizeof(type));
1517
1518 data = ELEMENT_WRAPPER_START "<require-instance value=\"invalid\"/>" ELEMENT_WRAPPER_END;
1519 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, false), LY_EVALID);
1520 memset(&type, 0, sizeof(type));
1521 logbuf_assert("Invalid value \"invalid\" of \"require-instance\". Line number 1.");
1522
1523 st->finished_correctly = true;
1524}
1525
1526static void
1527test_revision_date_elem(void **state)
1528{
1529 struct state *st = *state;
1530 const char *data;
1531 char rev[LY_REV_SIZE];
1532
1533 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-01-01\"/>" ELEMENT_WRAPPER_END;
1534 assert_int_equal(test_element_helper(st, &data, rev, NULL, NULL, true), LY_SUCCESS);
1535 assert_string_equal(rev, "2000-01-01");
1536
1537 data = ELEMENT_WRAPPER_START "<revision-date date=\"2000-50-05\"/>" ELEMENT_WRAPPER_END;
1538 assert_int_equal(test_element_helper(st, &data, rev, NULL, NULL, false), LY_EVALID);
1539 logbuf_assert("Invalid value \"2000-50-05\" of \"revision-date\". Line number 1.");
1540
1541 st->finished_correctly = true;
1542}
1543
1544static void
1545test_unique_elem(void **state)
1546{
1547 struct state *st = *state;
1548 const char *data;
1549 const char **values = NULL;
1550
1551 data = ELEMENT_WRAPPER_START "<unique tag=\"tag\"/>" ELEMENT_WRAPPER_END;
1552 assert_int_equal(test_element_helper(st, &data, &values, NULL, NULL, true), LY_SUCCESS);
1553 assert_string_equal(*values, "tag");
1554 FREE_STRING(st->ctx, *values);
1555 LY_ARRAY_FREE(values);
1556
1557 st->finished_correctly = true;
1558}
1559
1560static void
1561test_units_elem(void **state)
1562{
1563 struct state *st = *state;
1564 const char *data;
1565 const char *values = NULL;
1566
1567 data = ELEMENT_WRAPPER_START "<units name=\"name\"/>" ELEMENT_WRAPPER_END;
1568 assert_int_equal(test_element_helper(st, &data, &values, NULL, NULL, true), LY_SUCCESS);
1569 assert_string_equal(values, "name");
1570 FREE_STRING(st->ctx, values);
1571
1572 st->finished_correctly = true;
1573}
1574
1575static void
1576test_when_elem(void **state)
1577{
1578 struct state *st = *state;
1579 const char *data;
1580 struct lysp_when *when = NULL;
1581
1582 data = ELEMENT_WRAPPER_START
1583 "<when condition=\"cond\">"
1584 "<description><text>desc</text></description>"
1585 "<reference><text>ref</text></reference>"
1586 "</when>"
1587 ELEMENT_WRAPPER_END;
1588 assert_int_equal(test_element_helper(st, &data, &when, NULL, NULL, true), LY_SUCCESS);
1589 assert_string_equal(when->cond, "cond");
1590 assert_string_equal(when->dsc, "desc");
1591 assert_string_equal(when->ref, "ref");
1592 lysp_when_free(st->ctx, when);
1593 free(when);
1594 when = NULL;
1595
1596 data = ELEMENT_WRAPPER_START "<when condition=\"cond\" />" ELEMENT_WRAPPER_END;
1597 assert_int_equal(test_element_helper(st, &data, &when, NULL, NULL, true), LY_SUCCESS);
1598 assert_string_equal(when->cond, "cond");
1599 lysp_when_free(st->ctx, when);
1600 free(when);
1601 when = NULL;
1602
1603 st->finished_correctly = true;
1604}
1605
1606static void
1607test_yin_text_value_elem(void **state)
1608{
1609 struct state *st = *state;
1610 const char *data;
1611 const char *val;
1612
1613 data = ELEMENT_WRAPPER_START "<text>text</text>" ELEMENT_WRAPPER_END;
1614 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1615 assert_string_equal(val, "text");
1616 FREE_STRING(st->ctx, val);
1617
1618 data = "<error-message xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <value>text</value> </error-message>";
1619 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1620 assert_string_equal(val, "text");
1621 FREE_STRING(st->ctx, val);
1622
1623 data = ELEMENT_WRAPPER_START "<text></text>" ELEMENT_WRAPPER_END;
1624 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
1625 assert_string_equal("", val);
1626 FREE_STRING(st->ctx, val);
1627
David Sedlák8e7bda82019-07-16 17:57:50 +02001628 st->finished_correctly = true;
1629}
David Sedlák32488102019-07-15 17:44:10 +02001630
David Sedlák374d2b32019-07-17 15:06:55 +02001631static void
1632test_type_elem(void **state)
1633{
1634 struct state *st = *state;
1635 const char *data;
1636 struct lysp_type type = {};
1637
1638 /* max subelems */
1639 data = ELEMENT_WRAPPER_START
1640 "<type name=\"type-name\">"
1641 "<base name=\"base-name\"/>"
1642 "<bit name=\"bit\"/>"
1643 "<enum name=\"enum\"/>"
1644 "<fraction-digits value=\"2\"/>"
1645 "<length value=\"length\"/>"
1646 "<path value=\"path\"/>"
1647 "<pattern value=\"pattern\"/>"
1648 "<range value=\"range\" />"
1649 "<require-instance value=\"true\"/>"
1650 "<type name=\"sub-type-name\"/>"
1651 "</type>"
1652 ELEMENT_WRAPPER_END;
1653 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1654 assert_string_equal(type.name, "type-name");
1655 assert_string_equal(*type.bases, "base-name");
1656 assert_string_equal(type.bits->name, "bit");
1657 assert_string_equal(type.enums->name, "enum");
1658 assert_int_equal(type.fraction_digits, 2);
1659 assert_string_equal(type.length->arg, "length");
1660 assert_string_equal(type.path, "path");
1661 assert_string_equal(type.patterns->arg, "\006pattern");
1662 assert_string_equal(type.range->arg, "range");
1663 assert_int_equal(type.require_instance, 1);
1664 assert_string_equal(type.types->name, "sub-type-name");
1665 lysp_type_free(st->ctx, &type);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001666 assert_true(type.flags & LYS_SET_BASE);
1667 assert_true(type.flags & LYS_SET_BIT);
1668 assert_true(type.flags & LYS_SET_ENUM);
1669 assert_true(type.flags & LYS_SET_FRDIGITS);
1670 assert_true(type.flags & LYS_SET_LENGTH);
1671 assert_true(type.flags & LYS_SET_PATH);
1672 assert_true(type.flags & LYS_SET_PATTERN);
1673 assert_true(type.flags & LYS_SET_RANGE);
1674 assert_true(type.flags & LYS_SET_REQINST);
1675 assert_true(type.flags & LYS_SET_TYPE);
David Sedlák374d2b32019-07-17 15:06:55 +02001676 memset(&type, 0, sizeof(type));
1677
1678 /* min subelems */
1679 data = ELEMENT_WRAPPER_START "<type name=\"type-name\"/>" ELEMENT_WRAPPER_END;
1680 assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS);
1681 lysp_type_free(st->ctx, &type);
1682 memset(&type, 0, sizeof(type));
1683
1684 st->finished_correctly = true;
1685}
1686
David Sedlák1af868e2019-07-17 17:03:14 +02001687static void
1688test_max_elems_elem(void **state)
1689{
1690 struct state *st = *state;
1691 const char *data;
1692 struct lysp_node_list list = {};
1693 struct lysp_node_leaflist llist = {};
1694 struct lysp_refine refine = {};
1695
1696 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"unbounded\"/> </refine>";
1697 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
1698 assert_int_equal(refine.max, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001699 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001700
1701 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"5\"/> </list>";
1702 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, true), LY_SUCCESS);
1703 assert_int_equal(list.max, 5);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001704 assert_true(list.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001705
1706 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"85\"/> </leaf-list>";
1707 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, true), LY_SUCCESS);
1708 assert_int_equal(llist.max, 85);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001709 assert_true(llist.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001710
1711 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"10\"/> </refine>";
1712 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
1713 assert_int_equal(refine.max, 10);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001714 assert_true(refine.flags & LYS_SET_MAX);
David Sedlák1af868e2019-07-17 17:03:14 +02001715
1716 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"0\"/> </list>";
1717 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1718 logbuf_assert("Invalid value \"0\" of \"max-elements\". Line number 1.");
1719
1720 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"-10\"/> </list>";
1721 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1722 logbuf_assert("Invalid value \"-10\" of \"max-elements\". Line number 1.");
1723
1724 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"k\"/> </list>";
1725 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1726 logbuf_assert("Invalid value \"k\" of \"max-elements\". Line number 1.");
1727
1728 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <max-elements value=\"u12\"/> </list>";
1729 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, false), LY_EVALID);
1730 logbuf_assert("Invalid value \"u12\" of \"max-elements\". Line number 1.");
1731
1732 st->finished_correctly = true;
1733}
1734
David Sedlák09e18c92019-07-18 11:17:11 +02001735static void
1736test_min_elems_elem(void **state)
1737{
1738 struct state *st = *state;
1739 const char *data;
1740 struct lysp_node_list list = {};
1741 struct lysp_node_leaflist llist = {};
1742 struct lysp_refine refine = {};
1743
1744 data = "<refine xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"0\"/> </refine>";
1745 assert_int_equal(test_element_helper(st, &data, &refine, NULL, NULL, true), LY_SUCCESS);
1746 assert_int_equal(refine.min, 0);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001747 assert_true(refine.flags & LYS_SET_MIN);
David Sedlák09e18c92019-07-18 11:17:11 +02001748
1749 data = "<list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"41\"/> </list>";
1750 assert_int_equal(test_element_helper(st, &data, &list, NULL, NULL, true), LY_SUCCESS);
1751 assert_int_equal(list.min, 41);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001752 assert_true(list.flags & LYS_SET_MIN);
David Sedlák09e18c92019-07-18 11:17:11 +02001753
1754 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"50\"/> </leaf-list>";
1755 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, true), LY_SUCCESS);
1756 assert_int_equal(llist.min, 50);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001757 assert_true(llist.flags & LYS_SET_MIN);
David Sedlák09e18c92019-07-18 11:17:11 +02001758
1759 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"-5\"/> </leaf-list>";
1760 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1761 logbuf_assert("Value \"-5\" is out of \"min-elements\" bounds. Line number 1.");
1762
1763 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"99999999999999999\"/> </leaf-list>";
1764 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1765 logbuf_assert("Value \"99999999999999999\" is out of \"min-elements\" bounds. Line number 1.");
1766
1767 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"5k\"/> </leaf-list>";
1768 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1769 logbuf_assert("Invalid value \"5k\" of \"min-elements\". Line number 1.");
1770
1771 data = "<leaf-list xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"> <min-elements value=\"05\"/> </leaf-list>";
1772 assert_int_equal(test_element_helper(st, &data, &llist, NULL, NULL, false), LY_EVALID);
1773 logbuf_assert("Invalid value \"05\" of \"min-elements\". Line number 1.");
1774
1775 st->finished_correctly = true;
1776}
1777
David Sedláka2dad212019-07-18 12:45:19 +02001778static void
1779test_ordby_elem(void **state)
1780{
1781 struct state *st = *state;
1782 const char *data;
1783 uint16_t flags = 0;
1784
1785 data = ELEMENT_WRAPPER_START "<ordered-by value=\"system\"/>" ELEMENT_WRAPPER_END;
1786 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001787 assert_true(flags & LYS_ORDBY_SYSTEM);
David Sedláka2dad212019-07-18 12:45:19 +02001788
1789 data = ELEMENT_WRAPPER_START "<ordered-by value=\"user\"/>" ELEMENT_WRAPPER_END;
1790 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001791 assert_true(flags & LYS_ORDBY_USER);
David Sedláka2dad212019-07-18 12:45:19 +02001792
1793 data = ELEMENT_WRAPPER_START "<ordered-by value=\"inv\"/>" ELEMENT_WRAPPER_END;
1794 assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID);
1795 logbuf_assert("Invalid value \"inv\" of \"ordered-by\". Line number 1.");
1796
1797 st->finished_correctly = true;
1798}
1799
David Sedlák8a83bbb2019-07-18 14:46:00 +02001800static void
1801test_any_elem(void **state)
1802{
1803 struct state *st = *state;
1804 const char *data;
1805 struct lysp_node *siblings = NULL;
1806 struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
1807 struct lysp_node_anydata *parsed = NULL;
1808
1809 /* anyxml max subelems */
1810 data = ELEMENT_WRAPPER_START
1811 "<anyxml name=\"any-name\">"
1812 "<config value=\"true\" />"
1813 "<description><text>desc</text></description>"
1814 "<if-feature name=\"feature\" />"
1815 "<mandatory value=\"true\" />"
1816 "<must condition=\"must-cond\" />"
1817 "<reference><text>ref</text></reference>"
1818 "<status value=\"deprecated\"/>"
1819 "<when condition=\"when-cond\"/>"
1820 "</anyxml>"
1821 ELEMENT_WRAPPER_END;
1822 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1823 parsed = (struct lysp_node_anydata *)siblings;
1824 assert_null(parsed->parent);
1825 assert_int_equal(parsed->nodetype, LYS_ANYXML);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001826 assert_true(parsed->flags & LYS_CONFIG_W);
1827 assert_true(parsed->flags & LYS_MAND_TRUE);
1828 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001829 assert_null(parsed->next);
1830 assert_string_equal(parsed->name, "any-name");
1831 assert_string_equal(parsed->dsc, "desc");
1832 assert_string_equal(parsed->ref, "ref");
1833 assert_string_equal(parsed->when->cond, "when-cond");
1834 assert_string_equal(*parsed->iffeatures, "feature");
1835 assert_null(parsed->exts);
1836 lysp_node_free(st->ctx, siblings);
1837 siblings = NULL;
1838
1839 /* anydata max subelems */
1840 data = ELEMENT_WRAPPER_START
1841 "<anydata name=\"any-name\">"
1842 "<config value=\"true\" />"
1843 "<description><text>desc</text></description>"
1844 "<if-feature name=\"feature\" />"
1845 "<mandatory value=\"true\" />"
1846 "<must condition=\"must-cond\" />"
1847 "<reference><text>ref</text></reference>"
1848 "<status value=\"deprecated\"/>"
1849 "<when condition=\"when-cond\"/>"
1850 "</anydata>"
1851 ELEMENT_WRAPPER_END;
1852 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1853 parsed = (struct lysp_node_anydata *)siblings;
1854 assert_null(parsed->parent);
1855 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001856 assert_true(parsed->flags & LYS_CONFIG_W);
1857 assert_true(parsed->flags & LYS_MAND_TRUE);
1858 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák8a83bbb2019-07-18 14:46:00 +02001859 assert_null(parsed->next);
1860 assert_string_equal(parsed->name, "any-name");
1861 assert_string_equal(parsed->dsc, "desc");
1862 assert_string_equal(parsed->ref, "ref");
1863 assert_string_equal(parsed->when->cond, "when-cond");
1864 assert_string_equal(*parsed->iffeatures, "feature");
1865 assert_null(parsed->exts);
1866 lysp_node_free(st->ctx, siblings);
1867 siblings = NULL;
1868
1869 /* min subelems */
1870 node_meta.parent = (void *)0x10;
1871 data = ELEMENT_WRAPPER_START "<anydata name=\"any-name\"> </anydata>" 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_ptr_equal(parsed->parent, node_meta.parent);
1875 assert_int_equal(parsed->nodetype, LYS_ANYDATA);
1876 assert_null(parsed->next);
1877 assert_null(parsed->exts);
1878 lysp_node_free(st->ctx, siblings);
1879
1880 st->finished_correctly = true;
1881}
1882
David Sedlák203ca3a2019-07-18 15:26:25 +02001883static void
1884test_leaf_elem(void **state)
1885{
1886 struct state *st = *state;
1887 const char *data;
1888 struct lysp_node *siblings = NULL;
1889 struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
1890 struct lysp_node_leaf *parsed = NULL;
1891
1892 /* max elements */
1893 data = ELEMENT_WRAPPER_START
1894 "<leaf name=\"leaf\">"
1895 "<config value=\"true\" />"
1896 "<default value=\"def-val\"/>"
1897 "<description><text>desc</text></description>"
1898 "<if-feature name=\"feature\" />"
1899 "<mandatory value=\"true\" />"
1900 "<must condition=\"must-cond\" />"
1901 "<reference><text>ref</text></reference>"
1902 "<status value=\"deprecated\"/>"
1903 "<type name=\"type\"/>"
1904 "<units name=\"uni\"/>"
1905 "<when condition=\"when-cond\"/>"
1906 "</leaf>"
1907 ELEMENT_WRAPPER_END;
1908 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1909 parsed = (struct lysp_node_leaf *)siblings;
1910 assert_null(parsed->parent);
1911 assert_int_equal(parsed->nodetype, LYS_LEAF);
David Sedlákc3da3ef2019-07-19 12:56:08 +02001912 assert_true(parsed->flags & LYS_CONFIG_W);
1913 assert_true(parsed->flags & LYS_MAND_TRUE);
1914 assert_true(parsed->flags & LYS_STATUS_DEPRC);
David Sedlák203ca3a2019-07-18 15:26:25 +02001915 assert_null(parsed->next);
1916 assert_string_equal(parsed->name, "leaf");
1917 assert_string_equal(parsed->dsc, "desc");
1918 assert_string_equal(parsed->ref, "ref");
1919 assert_string_equal(parsed->when->cond, "when-cond");
1920 assert_string_equal(*parsed->iffeatures, "feature");
1921 assert_null(parsed->exts);
1922 assert_string_equal(parsed->musts->arg, "must-cond");
1923 assert_string_equal(parsed->type.name, "type");
1924 assert_string_equal(parsed->units, "uni");
1925 assert_string_equal(parsed->dflt, "def-val");
1926 lysp_node_free(st->ctx, siblings);
1927 siblings = NULL;
1928
1929 /* min elements */
1930 data = ELEMENT_WRAPPER_START "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>" ELEMENT_WRAPPER_END;
1931 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1932 parsed = (struct lysp_node_leaf *)siblings;
1933 assert_string_equal(parsed->name, "leaf");
1934 assert_string_equal(parsed->type.name, "type");
1935 lysp_node_free(st->ctx, siblings);
1936 siblings = NULL;
1937
1938 st->finished_correctly = true;
1939}
1940
David Sedlákc3da3ef2019-07-19 12:56:08 +02001941static void
1942test_leaf_list_elem(void **state)
1943{
1944 struct state *st = *state;
1945 const char *data;
1946 struct lysp_node *siblings = NULL;
1947 struct tree_node_meta node_meta = {.parent = NULL, .siblings = &siblings};
1948 struct lysp_node_leaflist *parsed = NULL;
1949
1950 data = ELEMENT_WRAPPER_START
1951 "<leaf-list name=\"llist\">"
1952 "<config value=\"true\" />"
1953 "<default value=\"def-val0\"/>"
1954 "<default value=\"def-val1\"/>"
1955 "<description><text>desc</text></description>"
1956 "<if-feature name=\"feature\"/>"
1957 "<max-elements value=\"5\"/>"
1958 "<must condition=\"must-cond\"/>"
1959 "<ordered-by value=\"user\" />"
1960 "<reference><text>ref</text></reference>"
1961 "<status value=\"current\"/>"
1962 "<type name=\"type\"/>"
1963 "<units name=\"uni\"/>"
1964 "<when condition=\"when-cond\"/>"
1965 "</leaf-list>"
1966 ELEMENT_WRAPPER_END;
1967 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
1968 parsed = (struct lysp_node_leaflist *)siblings;
1969 assert_string_equal(parsed->dflts[0], "def-val0");
1970 assert_string_equal(parsed->dflts[1], "def-val1");
1971 assert_string_equal(parsed->dsc, "desc");
1972 assert_string_equal(*parsed->iffeatures, "feature");
1973 assert_int_equal(parsed->max, 5);
1974 assert_string_equal(parsed->musts->arg, "must-cond");
1975 assert_string_equal(parsed->name, "llist");
1976 assert_null(parsed->next);
1977 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
1978 assert_null(parsed->parent);
1979 assert_string_equal(parsed->ref, "ref");
1980 assert_string_equal(parsed->type.name, "type");
1981 assert_string_equal(parsed->units, "uni");
1982 assert_string_equal(parsed->when->cond, "when-cond");
1983 assert_true(parsed->flags & LYS_CONFIG_W);
1984 assert_true(parsed->flags & LYS_ORDBY_USER);
1985 assert_true(parsed->flags & LYS_STATUS_CURR);
1986 lysp_node_free(st->ctx, siblings);
1987 siblings = NULL;
1988
1989 data = ELEMENT_WRAPPER_START
1990 "<leaf-list name=\"llist\">"
1991 "<config value=\"true\" />"
1992 "<description><text>desc</text></description>"
1993 "<if-feature name=\"feature\"/>"
1994 "<min-elements value=\"5\"/>"
1995 "<must condition=\"must-cond\"/>"
1996 "<ordered-by value=\"user\" />"
1997 "<reference><text>ref</text></reference>"
1998 "<status value=\"current\"/>"
1999 "<type name=\"type\"/>"
2000 "<units name=\"uni\"/>"
2001 "<when condition=\"when-cond\"/>"
2002 "</leaf-list>"
2003 ELEMENT_WRAPPER_END;
2004 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2005 parsed = (struct lysp_node_leaflist *)siblings;
2006 assert_string_equal(parsed->dsc, "desc");
2007 assert_string_equal(*parsed->iffeatures, "feature");
2008 assert_int_equal(parsed->min, 5);
2009 assert_string_equal(parsed->musts->arg, "must-cond");
2010 assert_string_equal(parsed->name, "llist");
2011 assert_null(parsed->next);
2012 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2013 assert_null(parsed->parent);
2014 assert_string_equal(parsed->ref, "ref");
2015 assert_string_equal(parsed->type.name, "type");
2016 assert_string_equal(parsed->units, "uni");
2017 assert_string_equal(parsed->when->cond, "when-cond");
2018 assert_true(parsed->flags & LYS_CONFIG_W);
2019 assert_true(parsed->flags & LYS_ORDBY_USER);
2020 assert_true(parsed->flags & LYS_STATUS_CURR);
2021 lysp_node_free(st->ctx, siblings);
2022 siblings = NULL;
2023
2024 data = ELEMENT_WRAPPER_START
2025 "<leaf-list name=\"llist\">"
2026 "<config value=\"true\" />"
2027 "<description><text>desc</text></description>"
2028 "<if-feature name=\"feature\"/>"
2029 "<max-elements value=\"15\"/>"
2030 "<min-elements value=\"5\"/>"
2031 "<must condition=\"must-cond\"/>"
2032 "<ordered-by value=\"user\" />"
2033 "<reference><text>ref</text></reference>"
2034 "<status value=\"current\"/>"
2035 "<type name=\"type\"/>"
2036 "<units name=\"uni\"/>"
2037 "<when condition=\"when-cond\"/>"
2038 "</leaf-list>"
2039 ELEMENT_WRAPPER_END;
2040 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2041 parsed = (struct lysp_node_leaflist *)siblings;
2042 assert_string_equal(parsed->dsc, "desc");
2043 assert_string_equal(*parsed->iffeatures, "feature");
2044 assert_int_equal(parsed->min, 5);
2045 assert_int_equal(parsed->max, 15);
2046 assert_string_equal(parsed->musts->arg, "must-cond");
2047 assert_string_equal(parsed->name, "llist");
2048 assert_null(parsed->next);
2049 assert_int_equal(parsed->nodetype, LYS_LEAFLIST);
2050 assert_null(parsed->parent);
2051 assert_string_equal(parsed->ref, "ref");
2052 assert_string_equal(parsed->type.name, "type");
2053 assert_string_equal(parsed->units, "uni");
2054 assert_string_equal(parsed->when->cond, "when-cond");
2055 assert_true(parsed->flags & LYS_CONFIG_W);
2056 assert_true(parsed->flags & LYS_ORDBY_USER);
2057 assert_true(parsed->flags & LYS_STATUS_CURR);
2058 lysp_node_free(st->ctx, siblings);
2059 siblings = NULL;
2060
2061 data = ELEMENT_WRAPPER_START
2062 "<leaf-list name=\"llist\">"
2063 "<type name=\"type\"/>"
2064 "</leaf-list>"
2065 ELEMENT_WRAPPER_END;
2066 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2067 parsed = (struct lysp_node_leaflist *)siblings;
2068 assert_string_equal(parsed->name, "llist");
2069 assert_string_equal(parsed->type.name, "type");
2070 lysp_node_free(st->ctx, siblings);
2071 siblings = NULL;
2072
2073 /* invalid combinations */
2074 data = ELEMENT_WRAPPER_START
2075 "<leaf-list name=\"llist\">"
2076 "<max-elements value=\"5\"/>"
2077 "<min-elements value=\"15\"/>"
2078 "<type name=\"type\"/>"
2079 "</leaf-list>"
2080 ELEMENT_WRAPPER_END;
2081 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
2082 logbuf_assert("Invalid combination of min-elements and max-elements: min value 15 is bigger than the max value 5. Line number 1.");
2083 lysp_node_free(st->ctx, siblings);
2084 siblings = NULL;
2085
2086 data = ELEMENT_WRAPPER_START
2087 "<leaf-list name=\"llist\">"
2088 "<default value=\"def-val1\"/>"
2089 "<min-elements value=\"15\"/>"
2090 "<type name=\"type\"/>"
2091 "</leaf-list>"
2092 ELEMENT_WRAPPER_END;
2093 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
2094 logbuf_assert("Invalid combination of keywords \"min-elements\" and \"default\" as substatements of \"leaf-list\". Line number 1.");
2095 lysp_node_free(st->ctx, siblings);
2096 siblings = NULL;
2097
2098 data = ELEMENT_WRAPPER_START
2099 "<leaf-list name=\"llist\">"
2100 "</leaf-list>"
2101 ELEMENT_WRAPPER_END;
2102 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, false), LY_EVALID);
2103 logbuf_assert("Missing mandatory subelement type of leaf-list element. Line number 1.");
2104 lysp_node_free(st->ctx, siblings);
2105 siblings = NULL;
2106
2107 st->finished_correctly = true;
2108}
2109
David Sedlákcb39f642019-07-19 13:19:55 +02002110static void
2111test_presence_elem(void **state)
2112{
2113 struct state *st = *state;
2114 const char *data;
2115 const char *val;
2116
2117 data = ELEMENT_WRAPPER_START "<presence value=\"presence-val\"/>" ELEMENT_WRAPPER_END;
2118 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2119 assert_string_equal(val, "presence-val");
2120 FREE_STRING(st->ctx, val);
2121
2122 data = ELEMENT_WRAPPER_START "<presence/>" ELEMENT_WRAPPER_END;
2123 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
2124 logbuf_assert("Missing mandatory attribute value of presence element. Line number 1.");
2125
2126 st->finished_correctly = true;
2127}
2128
David Sedlák12470a82019-07-19 13:44:36 +02002129static void
2130test_key_elem(void **state)
2131{
2132 struct state *st = *state;
2133 const char *data;
2134 const char *val;
2135
2136 data = ELEMENT_WRAPPER_START "<key value=\"key-value\"/>" ELEMENT_WRAPPER_END;
2137 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, true), LY_SUCCESS);
2138 assert_string_equal(val, "key-value");
2139 FREE_STRING(st->ctx, val);
2140
2141 data = ELEMENT_WRAPPER_START "<key/>" ELEMENT_WRAPPER_END;
2142 assert_int_equal(test_element_helper(st, &data, &val, NULL, NULL, false), LY_EVALID);
2143 logbuf_assert("Missing mandatory attribute value of key element. Line number 1.");
2144
2145 st->finished_correctly = true;
2146}
2147
David Sedlák04e17b22019-07-19 15:29:48 +02002148static void
2149test_typedef_elem(void **state)
2150{
2151 struct state *st = *state;
2152 const char *data;
2153 struct lysp_tpdf *tpdfs = NULL;
2154 struct typedef_meta typdef_meta = {NULL, &tpdfs};
2155
2156 data = ELEMENT_WRAPPER_START
2157 "<typedef name=\"tpdf-name\">"
2158 "<default value=\"def-val\"/>"
2159 "<description><text>desc-text</text></description>"
2160 "<reference><text>ref-text</text></reference>"
2161 "<status value=\"current\"/>"
2162 "<type name=\"type\"/>"
2163 "<units name=\"uni\"/>"
2164 "</typedef>"
2165 ELEMENT_WRAPPER_END;
2166 assert_int_equal(test_element_helper(st, &data, &typdef_meta, NULL, NULL, true), LY_SUCCESS);
2167 assert_string_equal(tpdfs[0].dflt, "def-val");
2168 assert_string_equal(tpdfs[0].dsc, "desc-text");
2169 assert_null(tpdfs[0].exts);
2170 assert_string_equal(tpdfs[0].name, "tpdf-name");
2171 assert_string_equal(tpdfs[0].ref, "ref-text");
2172 assert_string_equal(tpdfs[0].type.name, "type");
2173 assert_string_equal(tpdfs[0].units, "uni");
2174 assert_true(tpdfs[0].flags & LYS_STATUS_CURR);
2175 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2176 tpdfs = NULL;
2177
2178 data = ELEMENT_WRAPPER_START
2179 "<typedef name=\"tpdf-name\">"
2180 "<type name=\"type\"/>"
2181 "</typedef>"
2182 ELEMENT_WRAPPER_END;
2183 assert_int_equal(test_element_helper(st, &data, &typdef_meta, NULL, NULL, true), LY_SUCCESS);
2184 assert_string_equal(tpdfs[0].name, "tpdf-name");
2185 assert_string_equal(tpdfs[0].type.name, "type");
2186 FREE_ARRAY(st->ctx, tpdfs, lysp_tpdf_free);
2187 tpdfs = NULL;
2188
2189 st->finished_correctly = true;
2190}
2191
David Sedlákd2d676a2019-07-22 11:28:19 +02002192static void
2193test_refine_elem(void **state)
2194{
2195 struct state *st = *state;
2196 const char *data;
2197 struct lysp_refine *refines = NULL;
2198
2199 /* max subelems */
2200 data = ELEMENT_WRAPPER_START
2201 "<refine target-node=\"target\">"
2202 "<if-feature name=\"feature\" />"
2203 "<must condition=\"cond\" />"
2204 "<presence value=\"presence\" />"
2205 "<default value=\"def\" />"
2206 "<config value=\"true\" />"
2207 "<mandatory value=\"true\" />"
2208 "<min-elements value=\"10\" />"
2209 "<max-elements value=\"20\" />"
2210 "<description><text>desc</text></description>"
2211 "<reference><text>ref</text></reference>"
2212 "</refine>"
2213 ELEMENT_WRAPPER_END;
2214 assert_int_equal(test_element_helper(st, &data, &refines, NULL, NULL, true), LY_SUCCESS);
2215 assert_string_equal(refines->nodeid, "target");
2216 assert_string_equal(*refines->dflts, "def");
2217 assert_string_equal(refines->dsc, "desc");
2218 assert_null(refines->exts);
2219 assert_true(refines->flags & LYS_CONFIG_W);
2220 assert_true(refines->flags & LYS_MAND_TRUE);
2221 assert_string_equal(*refines->iffeatures, "feature");
2222 assert_int_equal(refines->max, 20);
2223 assert_int_equal(refines->min, 10);
2224 assert_string_equal(refines->musts->arg, "cond");
2225 assert_string_equal(refines->presence, "presence");
2226 assert_string_equal(refines->ref, "ref");
2227 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2228 refines = NULL;
2229
2230 /* min subelems */
2231 data = ELEMENT_WRAPPER_START "<refine target-node=\"target\" />" ELEMENT_WRAPPER_END;
2232 assert_int_equal(test_element_helper(st, &data, &refines, NULL, NULL, true), LY_SUCCESS);
2233 assert_string_equal(refines->nodeid, "target");
2234 FREE_ARRAY(st->ctx, refines, lysp_refine_free);
2235 refines = NULL;
2236
2237 st->finished_correctly = true;
2238}
2239
David Sedlák0d6de5a2019-07-22 13:25:44 +02002240static void
2241test_uses_elem(void **state)
2242{
2243 struct state *st = *state;
2244 const char *data;
2245 struct lysp_node *siblings = NULL;
2246 struct tree_node_meta node_meta = {NULL, &siblings};
2247 struct lysp_node_uses *parsed = NULL;
2248
2249 /* max subelems */
2250 data = ELEMENT_WRAPPER_START
2251 "<uses name=\"uses-name\">"
2252 "<when condition=\"cond\" />"
2253 "<if-feature name=\"feature\" />"
2254 "<status value=\"obsolete\" />"
2255 "<description><text>desc</text></description>"
2256 "<reference><text>ref</text></reference>"
2257 "<refine target-node=\"target\"/>"
David Sedlák992fb7c2019-07-24 16:51:01 +02002258 "<augment target-node=\"target\" />"
David Sedlák0d6de5a2019-07-22 13:25:44 +02002259 "</uses>"
2260 ELEMENT_WRAPPER_END;
2261 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2262 parsed = (struct lysp_node_uses *)&siblings[0];
2263 assert_string_equal(parsed->name, "uses-name");
2264 assert_string_equal(parsed->dsc, "desc");
2265 assert_null(parsed->exts);
2266 assert_true(parsed->flags & LYS_STATUS_OBSLT);
2267 assert_string_equal(*parsed->iffeatures, "feature");
2268 assert_null(parsed->next);
2269 assert_int_equal(parsed->nodetype, LYS_USES);
2270 assert_null(parsed->parent);
2271 assert_string_equal(parsed->ref, "ref");
2272 assert_string_equal(parsed->refines->nodeid, "target");
2273 assert_string_equal(parsed->when->cond, "cond");
David Sedlák992fb7c2019-07-24 16:51:01 +02002274 assert_string_equal(parsed->augments->nodeid, "target");
David Sedlák0d6de5a2019-07-22 13:25:44 +02002275 lysp_node_free(st->ctx, siblings);
2276 siblings = NULL;
2277
2278 /* min subelems */
2279 data = ELEMENT_WRAPPER_START "<uses name=\"uses-name\"/>" ELEMENT_WRAPPER_END;
2280 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2281 assert_string_equal(siblings[0].name, "uses-name");
2282 lysp_node_free(st->ctx, siblings);
2283 siblings = NULL;
2284
2285 st->finished_correctly = true;
2286}
2287
David Sedlákaa854b02019-07-22 14:17:10 +02002288static void
2289test_revision_elem(void **state)
2290{
2291 struct state *st = *state;
2292 const char *data;
2293 struct lysp_revision *revs = NULL;
2294
2295 /* max subelems */
2296 data = ELEMENT_WRAPPER_START
2297 "<revision date=\"2018-12-25\">"
2298 "<description><text>desc</text></description>"
2299 "<reference><text>ref</text></reference>"
2300 "</revision>"
2301 ELEMENT_WRAPPER_END;
2302 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, true), LY_SUCCESS);
2303 assert_string_equal(revs->date, "2018-12-25");
2304 assert_string_equal(revs->dsc, "desc");
2305 assert_string_equal(revs->ref, "ref");
2306 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2307 revs = NULL;
2308
2309 /* min subelems */
2310 data = ELEMENT_WRAPPER_START "<revision date=\"2005-05-05\" />" ELEMENT_WRAPPER_END;
2311 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, true), LY_SUCCESS);
2312 assert_string_equal(revs->date, "2005-05-05");
2313 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2314 revs = NULL;
2315
2316 /* invalid value */
2317 data = ELEMENT_WRAPPER_START "<revision date=\"05-05-2005\" />" ELEMENT_WRAPPER_END;
2318 assert_int_equal(test_element_helper(st, &data, &revs, NULL, NULL, false), LY_EVALID);
2319 logbuf_assert("Invalid value \"05-05-2005\" of \"revision\". Line number 1.");
2320 FREE_ARRAY(st->ctx, revs, lysp_revision_free);
2321 revs = NULL;
2322
2323 st->finished_correctly = true;
2324}
2325
David Sedlák0c2bab92019-07-22 15:33:19 +02002326static void
2327test_include_elem(void **state)
2328{
2329 struct state *st = *state;
2330 const char *data;
2331 struct lysp_include *includes = NULL;
2332 struct include_meta inc_meta = {"module-name", &includes};
2333
2334 /* max subelems */
2335 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2336 data = ELEMENT_WRAPPER_START
2337 "<include module=\"mod\">"
2338 "<description><text>desc</text></description>"
2339 "<reference><text>ref</text></reference>"
2340 "<revision-date date=\"1999-09-09\"/>"
2341 "</include>"
2342 ELEMENT_WRAPPER_END;
2343 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, true), LY_SUCCESS);
2344 assert_string_equal(includes->name, "mod");
2345 assert_string_equal(includes->dsc, "desc");
2346 assert_string_equal(includes->ref, "ref");
2347 assert_null(includes->exts);
2348 assert_string_equal(includes->rev, "1999-09-09");
2349 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2350 includes = NULL;
2351
2352 /* min subelems */
2353 data = ELEMENT_WRAPPER_START "<include module=\"mod\"/>" ELEMENT_WRAPPER_END;
2354 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, true), LY_SUCCESS);
2355 assert_string_equal(includes->name, "mod");
2356 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2357 includes = NULL;
2358
2359 /* invalid combinations */
2360 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2361 data = ELEMENT_WRAPPER_START
2362 "<include module=\"mod\">"
2363 "<description><text>desc</text></description>"
2364 "<revision-date date=\"1999-09-09\"/>"
2365 "</include>"
2366 ELEMENT_WRAPPER_END;
2367 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, false), LY_EVALID);
2368 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.");
2369 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2370 includes = NULL;
2371
2372 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2373 data = ELEMENT_WRAPPER_START
2374 "<include module=\"mod\">"
2375 "<reference><text>ref</text></reference>"
2376 "<revision-date date=\"1999-09-09\"/>"
2377 "</include>"
2378 ELEMENT_WRAPPER_END;
2379 assert_int_equal(test_element_helper(st, &data, &inc_meta, NULL, NULL, false), LY_EVALID);
2380 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.");
2381 FREE_ARRAY(st->ctx, includes, lysp_include_free);
2382 includes = NULL;
2383
2384 st->finished_correctly = true;
2385}
2386
David Sedlák5e13dea2019-07-22 16:06:45 +02002387static void
2388test_feature_elem(void **state)
2389{
2390 struct state *st = *state;
2391 const char *data;
2392 struct lysp_feature *features = NULL;
2393
2394 /* max subelems */
2395 data = ELEMENT_WRAPPER_START
2396 "<feature name=\"feature-name\">"
2397 "<if-feature name=\"iff\"/>"
2398 "<status value=\"deprecated\"/>"
2399 "<description><text>desc</text></description>"
2400 "<reference><text>ref</text></reference>"
2401 "</feature>"
2402 ELEMENT_WRAPPER_END;
2403 assert_int_equal(test_element_helper(st, &data, &features, NULL, NULL, true), LY_SUCCESS);
2404 assert_string_equal(features->name, "feature-name");
2405 assert_string_equal(features->dsc, "desc");
2406 assert_null(features->exts);
2407 assert_true(features->flags & LYS_STATUS_DEPRC);
2408 assert_string_equal(*features->iffeatures, "iff");
2409 assert_string_equal(features->ref, "ref");
2410 FREE_ARRAY(st->ctx, features, lysp_feature_free);
2411 features = NULL;
2412
2413 /* min subelems */
2414 data = ELEMENT_WRAPPER_START "<feature name=\"feature-name\"/>" ELEMENT_WRAPPER_END;
2415 assert_int_equal(test_element_helper(st, &data, &features, NULL, NULL, true), LY_SUCCESS);
2416 assert_string_equal(features->name, "feature-name");
2417 FREE_ARRAY(st->ctx, features, lysp_feature_free);
2418 features = NULL;
2419
2420 st->finished_correctly = true;
2421}
2422
David Sedlák28794f22019-07-22 16:45:00 +02002423static void
2424test_identity_elem(void **state)
2425{
2426 struct state *st = *state;
2427 const char *data;
2428 struct lysp_ident *identities = NULL;
2429
2430 /* max subelems */
2431 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2432 data = ELEMENT_WRAPPER_START
2433 "<identity name=\"ident-name\">"
2434 "<if-feature name=\"iff\"/>"
2435 "<base name=\"base-name\"/>"
2436 "<status value=\"deprecated\"/>"
2437 "<description><text>desc</text></description>"
2438 "<reference><text>ref</text></reference>"
2439 "</identity>"
2440 ELEMENT_WRAPPER_END;
2441 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, true), LY_SUCCESS);
2442 assert_string_equal(identities->name, "ident-name");
2443 assert_string_equal(*identities->bases, "base-name");
2444 assert_string_equal(*identities->iffeatures, "iff");
2445 assert_string_equal(identities->dsc, "desc");
2446 assert_string_equal(identities->ref, "ref");
2447 assert_true(identities->flags & LYS_STATUS_DEPRC);
2448 assert_null(identities->exts);
2449 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2450 identities = NULL;
2451
2452 /* min subelems */
2453 data = ELEMENT_WRAPPER_START "<identity name=\"ident-name\" />" ELEMENT_WRAPPER_END;
2454 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, true), LY_SUCCESS);
2455 assert_string_equal(identities->name, "ident-name");
2456 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2457 identities = NULL;
2458
2459 /* invalid */
2460 st->yin_ctx->mod_version = LYS_VERSION_1_0;
2461 data = ELEMENT_WRAPPER_START
2462 "<identity name=\"ident-name\">"
2463 "<if-feature name=\"iff\"/>"
2464 "</identity>"
2465 ELEMENT_WRAPPER_END;
2466 assert_int_equal(test_element_helper(st, &data, &identities, NULL, NULL, false), LY_EVALID);
2467 logbuf_assert("Invalid sub-elemnt \"if-feature\" of \"identity\" element - this sub-element is allowed only in modules with version 1.1 or newer. Line number 1.");
2468 FREE_ARRAY(st->ctx, identities, lysp_ident_free);
2469 identities = NULL;
2470
2471 st->finished_correctly = true;
2472}
2473
David Sedlákaf536aa2019-07-23 13:42:23 +02002474static void
2475test_list_elem(void **state)
2476{
2477 struct state *st = *state;
2478 const char *data;
2479 struct lysp_node *siblings = NULL;
2480 struct tree_node_meta node_meta = {NULL, &siblings};
2481 struct lysp_node_list *parsed = NULL;
2482
2483 /* max subelems */
2484 data = ELEMENT_WRAPPER_START
2485 "<list name=\"list-name\">"
2486 "<when condition=\"when\"/>"
2487 "<if-feature name=\"iff\"/>"
2488 "<must condition=\"must-cond\"/>"
2489 "<key value=\"key\"/>"
2490 "<unique tag=\"utag\"/>"
2491 "<config value=\"true\"/>"
2492 "<min-elements value=\"10\"/>"
2493 "<ordered-by value=\"user\"/>"
2494 "<status value=\"deprecated\"/>"
2495 "<description><text>desc</text></description>"
2496 "<reference><text>ref</text></reference>"
2497 "<anydata name=\"anyd\"/>"
2498 "<anyxml name=\"anyx\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02002499 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002500 "<choice name=\"choice\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02002501 "<action name=\"action\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02002502 "<grouping name=\"grp\"/>"
David Sedlák031b9e72019-07-23 15:19:37 +02002503 "<notification name=\"notf\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002504 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
2505 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlákaf536aa2019-07-23 13:42:23 +02002506 "<list name=\"sub-list\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002507 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákaf536aa2019-07-23 13:42:23 +02002508 "<uses name=\"uses-name\"/>"
2509 "</list>"
2510 ELEMENT_WRAPPER_END;
2511 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2512 parsed = (struct lysp_node_list *)&siblings[0];
2513 assert_string_equal(parsed->dsc, "desc");
2514 assert_string_equal(parsed->child->name, "anyd");
2515 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2516 assert_string_equal(parsed->child->next->name, "anyx");
2517 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
David Sedlákf111bcb2019-07-23 17:15:51 +02002518 assert_string_equal(parsed->child->next->next->name, "cont");
2519 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002520 assert_string_equal(parsed->child->next->next->next->name, "choice");
2521 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
David Sedlák85d0eca2019-07-24 15:15:21 +02002522 assert_string_equal(parsed->child->next->next->next->next->name, "leaf");
2523 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAF);
2524 assert_string_equal(parsed->child->next->next->next->next->next->name, "llist");
2525 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2526 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "sub-list");
2527 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LIST);
2528 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "uses-name");
2529 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_USES);
2530 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002531 assert_string_equal(parsed->groupings->name, "grp");
David Sedlák85d0eca2019-07-24 15:15:21 +02002532 assert_string_equal(parsed->actions->name, "action");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002533 assert_int_equal(parsed->groupings->nodetype, LYS_GROUPING);
David Sedlák031b9e72019-07-23 15:19:37 +02002534 assert_string_equal(parsed->notifs->name, "notf");
David Sedlákaf536aa2019-07-23 13:42:23 +02002535 assert_null(parsed->exts);
2536 assert_true(parsed->flags & LYS_ORDBY_USER);
2537 assert_true(parsed->flags & LYS_STATUS_DEPRC);
2538 assert_true(parsed->flags & LYS_CONFIG_W);
2539 assert_string_equal(*parsed->iffeatures, "iff");
2540 assert_string_equal(parsed->key, "key");
2541 assert_int_equal(parsed->min, 10);
2542 assert_string_equal(parsed->musts->arg, "must-cond");
2543 assert_string_equal(parsed->name, "list-name");
2544 assert_null(parsed->next);
2545 assert_int_equal(parsed->nodetype, LYS_LIST);
2546 assert_null(parsed->parent);
2547 assert_string_equal(parsed->ref, "ref");
2548 assert_string_equal(parsed->typedefs->name, "tpdf");
2549 assert_string_equal(*parsed->uniques, "utag");
2550 assert_string_equal(parsed->when->cond, "when");
2551 lysp_node_free(st->ctx, siblings);
2552 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
2553 siblings = NULL;
2554
2555 /* min subelems */
2556 data = ELEMENT_WRAPPER_START "<list name=\"list-name\" />" ELEMENT_WRAPPER_END;
2557 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2558 parsed = (struct lysp_node_list *)&siblings[0];
2559 assert_string_equal(parsed->name, "list-name");
2560 lysp_node_free(st->ctx, siblings);
2561 siblings = NULL;
2562
2563 st->finished_correctly = true;
2564}
2565
David Sedlák031b9e72019-07-23 15:19:37 +02002566static void
2567test_notification_elem(void **state)
2568{
2569 struct state *st = *state;
2570 const char *data;
2571 struct lysp_notif *notifs = NULL;
2572 struct notif_meta notif_meta = {NULL, &notifs};
2573
2574 /* max subelems */
2575 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2576 data = ELEMENT_WRAPPER_START
2577 "<notification name=\"notif-name\">"
2578 "<anydata name=\"anyd\"/>"
2579 "<anyxml name=\"anyx\"/>"
2580 "<description><text>desc</text></description>"
2581 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002582 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
2583 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák031b9e72019-07-23 15:19:37 +02002584 "<list name=\"sub-list\"/>"
2585 "<must condition=\"cond\"/>"
2586 "<reference><text>ref</text></reference>"
2587 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002588 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák031b9e72019-07-23 15:19:37 +02002589 "<uses name=\"uses-name\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02002590 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002591 "<choice name=\"choice\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02002592 "<grouping name=\"grp\"/>"
David Sedlák031b9e72019-07-23 15:19:37 +02002593 "</notification>"
2594 ELEMENT_WRAPPER_END;
2595 assert_int_equal(test_element_helper(st, &data, &notif_meta, NULL, NULL, true), LY_SUCCESS);
2596 assert_string_equal(notifs->name, "notif-name");
2597 assert_string_equal(notifs->data->name, "anyd");
2598 assert_int_equal(notifs->data->nodetype, LYS_ANYDATA);
2599 assert_string_equal(notifs->data->next->name, "anyx");
2600 assert_int_equal(notifs->data->next->nodetype, LYS_ANYXML);
2601 assert_string_equal(notifs->data->next->next->name, "leaf");
2602 assert_int_equal(notifs->data->next->next->nodetype, LYS_LEAF);
2603 assert_string_equal(notifs->data->next->next->next->name, "llist");
2604 assert_int_equal(notifs->data->next->next->next->nodetype, LYS_LEAFLIST);
2605 assert_string_equal(notifs->data->next->next->next->next->name, "sub-list");
2606 assert_int_equal(notifs->data->next->next->next->next->nodetype, LYS_LIST);
2607 assert_null(notifs->exts);
2608 assert_true(notifs->flags & LYS_STATUS_DEPRC);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002609 assert_string_equal(notifs->groupings->name, "grp");
2610 assert_int_equal(notifs->groupings->nodetype, LYS_GROUPING);
David Sedlákf111bcb2019-07-23 17:15:51 +02002611 assert_string_equal(notifs->data->next->next->next->next->next->name, "uses-name");
2612 assert_int_equal(notifs->data->next->next->next->next->next->nodetype, LYS_USES);
2613 assert_string_equal(notifs->data->next->next->next->next->next->next->name, "cont");
2614 assert_int_equal(notifs->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002615 assert_int_equal(notifs->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2616 assert_string_equal(notifs->data->next->next->next->next->next->next->next->name, "choice");
2617 assert_null(notifs->data->next->next->next->next->next->next->next->next);
David Sedlák031b9e72019-07-23 15:19:37 +02002618 assert_string_equal(*notifs->iffeatures, "iff");
2619 assert_string_equal(notifs->musts->arg, "cond");
2620 assert_int_equal(notifs->nodetype, LYS_NOTIF);
2621 assert_null(notifs->parent);
2622 assert_string_equal(notifs->ref, "ref");
2623 assert_string_equal(notifs->typedefs->name, "tpdf");
2624 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
2625 notifs = NULL;
2626
2627 /* min subelems */
2628 data = ELEMENT_WRAPPER_START "<notification name=\"notif-name\" />" ELEMENT_WRAPPER_END;
2629 assert_int_equal(test_element_helper(st, &data, &notif_meta, NULL, NULL, true), LY_SUCCESS);
2630 assert_string_equal(notifs->name, "notif-name");
2631 FREE_ARRAY(st->ctx, notifs, lysp_notif_free);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002632 notifs = NULL;
2633
2634 st->finished_correctly = true;
2635}
2636
2637static void
2638test_grouping_elem(void **state)
2639{
2640 struct state *st = *state;
2641 const char *data;
2642 struct lysp_grp *grps = NULL;
2643 struct grouping_meta grp_meta = {NULL, &grps};
2644
2645 /* max subelems */
2646 data = ELEMENT_WRAPPER_START
2647 "<grouping name=\"grp-name\">"
2648 "<anydata name=\"anyd\"/>"
2649 "<anyxml name=\"anyx\"/>"
2650 "<description><text>desc</text></description>"
2651 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002652 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
2653 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02002654 "<list name=\"list\"/>"
2655 "<notification name=\"notf\"/>"
2656 "<reference><text>ref</text></reference>"
2657 "<status value=\"current\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002658 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02002659 "<uses name=\"uses-name\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02002660 "<action name=\"act\"/>"
David Sedlákf111bcb2019-07-23 17:15:51 +02002661 "<container name=\"cont\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002662 "<choice name=\"choice\"/>"
David Sedláke3ce9ef2019-07-23 16:34:30 +02002663 "</grouping>"
2664 ELEMENT_WRAPPER_END;
2665 assert_int_equal(test_element_helper(st, &data, &grp_meta, NULL, NULL, true), LY_SUCCESS);
2666 assert_string_equal(grps->name, "grp-name");
David Sedláke3ce9ef2019-07-23 16:34:30 +02002667 assert_string_equal(grps->data->name, "anyd");
2668 assert_string_equal(grps->data->next->name, "anyx");
2669 assert_string_equal(grps->data->next->next->name, "leaf");
2670 assert_string_equal(grps->data->next->next->next->name, "llist");
2671 assert_string_equal(grps->data->next->next->next->next->name, "list");
2672 assert_string_equal(grps->dsc, "desc");
2673 assert_null(grps->exts);
2674 assert_true(grps->flags & LYS_STATUS_CURR);
2675 assert_string_equal(grps->groupings->name, "sub-grp");
2676 assert_int_equal(grps->nodetype, LYS_GROUPING);
2677 assert_string_equal(grps->notifs->name, "notf");
2678 assert_null(grps->parent);
2679 assert_string_equal(grps->ref, "ref");
2680 assert_string_equal(grps->typedefs->name, "tpdf");
David Sedlák85d0eca2019-07-24 15:15:21 +02002681 assert_string_equal(grps->actions->name, "act");
David Sedlákf111bcb2019-07-23 17:15:51 +02002682 assert_string_equal(grps->data->next->next->next->next->next->name, "uses-name");
David Sedlákb7abcfa2019-07-24 12:33:35 +02002683 assert_int_equal(grps->data->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákf111bcb2019-07-23 17:15:51 +02002684 assert_string_equal(grps->data->next->next->next->next->next->next->name, "cont");
David Sedlákb7abcfa2019-07-24 12:33:35 +02002685 assert_int_equal(grps->data->next->next->next->next->next->next->nodetype, LYS_CONTAINER);
2686 assert_string_equal(grps->data->next->next->next->next->next->next->next->name, "choice");
2687 assert_int_equal(grps->data->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
David Sedláke3ce9ef2019-07-23 16:34:30 +02002688 FREE_ARRAY(st->ctx, grps, lysp_grp_free);
2689 grps = NULL;
2690
2691 /* min subelems */
2692 data = ELEMENT_WRAPPER_START "<grouping name=\"grp-name\" />" ELEMENT_WRAPPER_END;
2693 assert_int_equal(test_element_helper(st, &data, &grp_meta, NULL, NULL, true), LY_SUCCESS);
2694 assert_string_equal(grps->name, "grp-name");
2695 FREE_ARRAY(st->ctx, grps, lysp_grp_free);
2696 grps = NULL;
David Sedlák031b9e72019-07-23 15:19:37 +02002697
2698 st->finished_correctly = true;
2699}
2700
David Sedlákf111bcb2019-07-23 17:15:51 +02002701static void
2702test_container_elem(void **state)
2703{
2704 struct state *st = *state;
2705 const char *data;
2706 struct lysp_node *siblings = NULL;
2707 struct tree_node_meta node_meta = {NULL, &siblings};
2708 struct lysp_node_container *parsed = NULL;
2709
2710 /* max subelems */
David Sedláke2dc9e92019-07-24 09:59:21 +02002711 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2712 data = ELEMENT_WRAPPER_START
2713 "<container name=\"cont-name\">"
2714 "<anydata name=\"anyd\"/>"
2715 "<anyxml name=\"anyx\"/>"
2716 "<config value=\"true\"/>"
2717 "<container name=\"subcont\"/>"
2718 "<description><text>desc</text></description>"
2719 "<grouping name=\"sub-grp\"/>"
2720 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002721 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
2722 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedláke2dc9e92019-07-24 09:59:21 +02002723 "<list name=\"list\"/>"
2724 "<must condition=\"cond\"/>"
2725 "<notification name=\"notf\"/>"
2726 "<presence value=\"presence\"/>"
2727 "<reference><text>ref</text></reference>"
2728 "<status value=\"current\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002729 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedláke2dc9e92019-07-24 09:59:21 +02002730 "<uses name=\"uses-name\"/>"
2731 "<when condition=\"when-cond\"/>"
David Sedlák85d0eca2019-07-24 15:15:21 +02002732 "<action name=\"act\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002733 "<choice name=\"choice\"/>"
David Sedláke2dc9e92019-07-24 09:59:21 +02002734 "</container>"
2735 ELEMENT_WRAPPER_END;
2736 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2737 parsed = (struct lysp_node_container *)siblings;
2738 assert_string_equal(parsed->name, "cont-name");
2739 assert_null(parsed->parent);
2740 assert_int_equal(parsed->nodetype, LYS_CONTAINER);
2741 assert_true(parsed->flags & LYS_CONFIG_W);
2742 assert_true(parsed->flags & LYS_STATUS_CURR);
2743 assert_null(parsed->next);
2744 assert_string_equal(parsed->dsc, "desc");
2745 assert_string_equal(parsed->ref, "ref");
2746 assert_string_equal(parsed->when->cond, "when-cond");
2747 assert_string_equal(*parsed->iffeatures, "iff");
2748 assert_null(parsed->exts);
2749 assert_string_equal(parsed->musts->arg, "cond");
2750 assert_string_equal(parsed->presence, "presence");
2751 assert_string_equal(parsed->typedefs->name, "tpdf");
2752 assert_string_equal(parsed->groupings->name, "sub-grp");
2753 assert_string_equal(parsed->child->name, "anyd");
2754 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2755 assert_string_equal(parsed->child->next->name, "anyx");
2756 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2757 assert_string_equal(parsed->child->next->next->name, "subcont");
2758 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
2759 assert_string_equal(parsed->child->next->next->next->name, "leaf");
2760 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
2761 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
2762 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
2763 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
2764 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
2765 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
2766 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002767 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
2768 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2769 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedláke2dc9e92019-07-24 09:59:21 +02002770 assert_string_equal(parsed->notifs->name, "notf");
David Sedlák85d0eca2019-07-24 15:15:21 +02002771 assert_string_equal(parsed->actions->name, "act");
David Sedláke2dc9e92019-07-24 09:59:21 +02002772 lysp_node_free(st->ctx, siblings);
2773 ly_set_erase(&st->yin_ctx->tpdfs_nodes, NULL);
2774 siblings = NULL;
David Sedlákf111bcb2019-07-23 17:15:51 +02002775
2776 /* min subelems */
2777 data = ELEMENT_WRAPPER_START "<container name=\"cont-name\" />" ELEMENT_WRAPPER_END;
2778 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2779 parsed = (struct lysp_node_container *)siblings;
2780 assert_string_equal(parsed->name, "cont-name");
2781 lysp_node_free(st->ctx, siblings);
2782 siblings = NULL;
2783
2784 st->finished_correctly = true;
2785}
2786
David Sedlák5379d392019-07-24 10:42:03 +02002787static void
2788test_case_elem(void **state)
2789{
2790 struct state *st = *state;
2791 const char *data;
2792 struct lysp_node *siblings = NULL;
2793 struct tree_node_meta node_meta = {NULL, &siblings};
2794 struct lysp_node_case *parsed = NULL;
2795
2796 /* max subelems */
2797 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2798 data = ELEMENT_WRAPPER_START
2799 "<case name=\"case-name\">"
2800 "<anydata name=\"anyd\"/>"
2801 "<anyxml name=\"anyx\"/>"
2802 "<container name=\"subcont\"/>"
2803 "<description><text>desc</text></description>"
2804 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002805 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
2806 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák5379d392019-07-24 10:42:03 +02002807 "<list name=\"list\"/>"
2808 "<reference><text>ref</text></reference>"
2809 "<status value=\"current\"/>"
2810 "<uses name=\"uses-name\"/>"
2811 "<when condition=\"when-cond\"/>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002812 "<choice name=\"choice\"/>"
David Sedlák5379d392019-07-24 10:42:03 +02002813 "</case>"
2814 ELEMENT_WRAPPER_END;
2815 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2816 parsed = (struct lysp_node_case *)siblings;
2817 assert_string_equal(parsed->name, "case-name");
2818 assert_null(parsed->parent);
2819 assert_int_equal(parsed->nodetype, LYS_CASE);
2820 assert_true(parsed->flags & LYS_STATUS_CURR);
2821 assert_null(parsed->next);
2822 assert_string_equal(parsed->dsc, "desc");
2823 assert_string_equal(parsed->ref, "ref");
2824 assert_string_equal(parsed->when->cond, "when-cond");
2825 assert_string_equal(*parsed->iffeatures, "iff");
2826 assert_null(parsed->exts);
2827 assert_string_equal(parsed->child->name, "anyd");
2828 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2829 assert_string_equal(parsed->child->next->name, "anyx");
2830 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2831 assert_string_equal(parsed->child->next->next->name, "subcont");
2832 assert_int_equal(parsed->child->next->next->nodetype, LYS_CONTAINER);
2833 assert_string_equal(parsed->child->next->next->next->name, "leaf");
2834 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_LEAF);
2835 assert_string_equal(parsed->child->next->next->next->next->name, "llist");
2836 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_LEAFLIST);
2837 assert_string_equal(parsed->child->next->next->next->next->next->name, "list");
2838 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LIST);
2839 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "uses-name");
2840 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_USES);
David Sedlákb7abcfa2019-07-24 12:33:35 +02002841 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "choice");
2842 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_CHOICE);
2843 assert_null(parsed->child->next->next->next->next->next->next->next->next);
David Sedlák5379d392019-07-24 10:42:03 +02002844 lysp_node_free(st->ctx, siblings);
2845 siblings = NULL;
2846
2847 /* min subelems */
2848 data = ELEMENT_WRAPPER_START "<case name=\"case-name\" />" ELEMENT_WRAPPER_END;
2849 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2850 parsed = (struct lysp_node_case *)siblings;
2851 assert_string_equal(parsed->name, "case-name");
2852 lysp_node_free(st->ctx, siblings);
2853 siblings = NULL;
2854
2855 st->finished_correctly = true;
2856}
2857
David Sedlákb7abcfa2019-07-24 12:33:35 +02002858static void
2859test_choice_elem(void **state)
2860{
2861 struct state *st = *state;
2862 const char *data;
2863 struct lysp_node *siblings = NULL;
2864 struct tree_node_meta node_meta = {NULL, &siblings};
2865 struct lysp_node_choice *parsed = NULL;
2866
2867 /* max subelems */
2868 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2869 data = ELEMENT_WRAPPER_START
2870 "<choice name=\"choice-name\">"
2871 "<anydata name=\"anyd\"/>"
2872 "<anyxml name=\"anyx\"/>"
2873 "<case name=\"sub-case\"/>"
2874 "<choice name=\"choice\"/>"
2875 "<config value=\"true\"/>"
2876 "<container name=\"subcont\"/>"
2877 "<default value=\"def\"/>"
2878 "<description><text>desc</text></description>"
2879 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002880 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
2881 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlákb7abcfa2019-07-24 12:33:35 +02002882 "<list name=\"list\"/>"
2883 "<mandatory value=\"true\" />"
2884 "<reference><text>ref</text></reference>"
2885 "<status value=\"current\"/>"
2886 "<when condition=\"when-cond\"/>"
2887 "</choice>"
2888 ELEMENT_WRAPPER_END;
2889 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2890 parsed = (struct lysp_node_choice *)siblings;
2891 assert_string_equal(parsed->name, "choice-name");
2892 assert_null(parsed->parent);
2893 assert_int_equal(parsed->nodetype, LYS_CHOICE);
2894 assert_true(parsed->flags & LYS_CONFIG_W && parsed->flags & LYS_MAND_TRUE && parsed->flags & LYS_STATUS_CURR);
2895 assert_null(parsed->next);
2896 assert_string_equal(parsed->dsc, "desc");
2897 assert_string_equal(parsed->ref, "ref");
2898 assert_string_equal(parsed->when->cond, "when-cond");
2899 assert_string_equal(*parsed->iffeatures, "iff");
2900 assert_null(parsed->exts);
2901 assert_string_equal(parsed->child->name, "anyd");
2902 assert_int_equal(parsed->child->nodetype, LYS_ANYDATA);
2903 assert_string_equal(parsed->child->next->name, "anyx");
2904 assert_int_equal(parsed->child->next->nodetype, LYS_ANYXML);
2905 assert_string_equal(parsed->child->next->next->name, "sub-case");
2906 assert_int_equal(parsed->child->next->next->nodetype, LYS_CASE);
2907 assert_string_equal(parsed->child->next->next->next->name, "choice");
2908 assert_int_equal(parsed->child->next->next->next->nodetype, LYS_CHOICE);
2909 assert_string_equal(parsed->child->next->next->next->next->name, "subcont");
2910 assert_int_equal(parsed->child->next->next->next->next->nodetype, LYS_CONTAINER);
2911 assert_string_equal(parsed->child->next->next->next->next->next->name, "leaf");
2912 assert_int_equal(parsed->child->next->next->next->next->next->nodetype, LYS_LEAF);
2913 assert_string_equal(parsed->child->next->next->next->next->next->next->name, "llist");
2914 assert_int_equal(parsed->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2915 assert_string_equal(parsed->child->next->next->next->next->next->next->next->name, "list");
2916 assert_int_equal(parsed->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
2917 assert_null(parsed->child->next->next->next->next->next->next->next->next);
2918 lysp_node_free(st->ctx, siblings);
2919 siblings = NULL;
2920
2921 /* min subelems */
2922 data = ELEMENT_WRAPPER_START "<choice name=\"choice-name\" />" ELEMENT_WRAPPER_END;
2923 assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
2924 parsed = (struct lysp_node_choice *)siblings;
2925 assert_string_equal(parsed->name, "choice-name");
2926 lysp_node_free(st->ctx, siblings);
2927 siblings = NULL;
2928
2929 st->finished_correctly = true;
2930}
2931
David Sedlák05404f62019-07-24 14:11:53 +02002932static void
2933test_inout_elem(void **state)
2934{
2935 struct state *st = *state;
2936 const char *data;
2937 struct lysp_action_inout inout = {};
2938 struct inout_meta inout_meta = {NULL, &inout};
2939
2940 /* max subelements */
2941 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2942 data = ELEMENT_WRAPPER_START
2943 "<input>"
2944 "<anydata name=\"anyd\"/>"
2945 "<anyxml name=\"anyx\"/>"
2946 "<choice name=\"choice\"/>"
2947 "<container name=\"subcont\"/>"
2948 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002949 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
2950 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák05404f62019-07-24 14:11:53 +02002951 "<list name=\"list\"/>"
2952 "<must condition=\"cond\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002953 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák05404f62019-07-24 14:11:53 +02002954 "<uses name=\"uses-name\"/>"
2955 "</input>"
2956 ELEMENT_WRAPPER_END;
2957 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
2958 assert_null(inout.parent);
2959 assert_int_equal(inout.nodetype, LYS_INPUT);
2960 assert_string_equal(inout.musts->arg, "cond");
2961 assert_string_equal(inout.typedefs->name, "tpdf");
2962 assert_string_equal(inout.groupings->name, "sub-grp");
2963 assert_string_equal(inout.data->name, "anyd");
2964 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
2965 assert_string_equal(inout.data->next->name, "anyx");
2966 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
2967 assert_string_equal(inout.data->next->next->name, "choice");
2968 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
2969 assert_string_equal(inout.data->next->next->next->name, "subcont");
2970 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
2971 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
2972 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
2973 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
2974 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
2975 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
2976 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
2977 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
2978 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
2979 assert_null(inout.data->next->next->next->next->next->next->next->next);
2980 lysp_action_inout_free(st->ctx, &inout);
2981 memset(&inout, 0, sizeof inout);
2982
2983 /* max subelements */
2984 st->yin_ctx->mod_version = LYS_VERSION_1_1;
2985 data = ELEMENT_WRAPPER_START
2986 "<output>"
2987 "<anydata name=\"anyd\"/>"
2988 "<anyxml name=\"anyx\"/>"
2989 "<choice name=\"choice\"/>"
2990 "<container name=\"subcont\"/>"
2991 "<grouping name=\"sub-grp\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002992 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
2993 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák05404f62019-07-24 14:11:53 +02002994 "<list name=\"list\"/>"
2995 "<must condition=\"cond\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02002996 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák05404f62019-07-24 14:11:53 +02002997 "<uses name=\"uses-name\"/>"
2998 "</output>"
2999 ELEMENT_WRAPPER_END;
3000 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3001 assert_null(inout.parent);
3002 assert_int_equal(inout.nodetype, LYS_OUTPUT);
3003 assert_string_equal(inout.musts->arg, "cond");
3004 assert_string_equal(inout.typedefs->name, "tpdf");
3005 assert_string_equal(inout.groupings->name, "sub-grp");
3006 assert_string_equal(inout.data->name, "anyd");
3007 assert_int_equal(inout.data->nodetype, LYS_ANYDATA);
3008 assert_string_equal(inout.data->next->name, "anyx");
3009 assert_int_equal(inout.data->next->nodetype, LYS_ANYXML);
3010 assert_string_equal(inout.data->next->next->name, "choice");
3011 assert_int_equal(inout.data->next->next->nodetype, LYS_CHOICE);
3012 assert_string_equal(inout.data->next->next->next->name, "subcont");
3013 assert_int_equal(inout.data->next->next->next->nodetype, LYS_CONTAINER);
3014 assert_string_equal(inout.data->next->next->next->next->name, "leaf");
3015 assert_int_equal(inout.data->next->next->next->next->nodetype, LYS_LEAF);
3016 assert_string_equal(inout.data->next->next->next->next->next->name, "llist");
3017 assert_int_equal(inout.data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3018 assert_string_equal(inout.data->next->next->next->next->next->next->name, "list");
3019 assert_int_equal(inout.data->next->next->next->next->next->next->nodetype, LYS_LIST);
3020 assert_string_equal(inout.data->next->next->next->next->next->next->next->name, "uses-name");
3021 assert_int_equal(inout.data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3022 assert_null(inout.data->next->next->next->next->next->next->next->next);
3023 lysp_action_inout_free(st->ctx, &inout);
3024 memset(&inout, 0, sizeof inout);
3025
3026 /* min subelems */
3027 data = ELEMENT_WRAPPER_START "<input />" ELEMENT_WRAPPER_END;
3028 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3029 memset(&inout, 0, sizeof inout);
3030
3031 data = ELEMENT_WRAPPER_START "<output />" ELEMENT_WRAPPER_END;
3032 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, true), LY_SUCCESS);
3033 memset(&inout, 0, sizeof inout);
3034
3035 /* invalid combinations */
3036 data = ELEMENT_WRAPPER_START "<input name=\"test\"/>" ELEMENT_WRAPPER_END;
3037 assert_int_equal(test_element_helper(st, &data, &inout_meta, NULL, NULL, false), LY_EVALID);
3038 logbuf_assert("Unexpected attribute \"name\" of input element. Line number 1.");
3039 memset(&inout, 0, sizeof inout);
3040
3041 st->finished_correctly = true;
3042}
3043
David Sedlák85d0eca2019-07-24 15:15:21 +02003044static void
3045test_action_elem(void **state)
3046{
3047 struct state *st = *state;
3048 const char *data;
3049 struct lysp_action *actions = NULL;
3050 struct action_meta act_meta = {NULL, &actions};
3051
3052 /* max subelems */
3053 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3054 data = ELEMENT_WRAPPER_START
3055 "<action name=\"act\">"
3056 "<description><text>desc</text></description>"
3057 "<grouping name=\"grouping\"/>"
3058 "<if-feature name=\"iff\"/>"
3059 "<input><uses name=\"uses-name\"/></input>"
3060 "<output><must condition=\"cond\"/></output>"
3061 "<reference><text>ref</text></reference>"
3062 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003063 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlák85d0eca2019-07-24 15:15:21 +02003064 "</action>"
3065 ELEMENT_WRAPPER_END;
3066 assert_int_equal(test_element_helper(st, &data, &act_meta, NULL, NULL, true), LY_SUCCESS);
3067 assert_null(actions->parent);
3068 assert_int_equal(actions->nodetype, LYS_ACTION);
3069 assert_true(actions->flags & LYS_STATUS_DEPRC);
3070 assert_string_equal(actions->name, "act");
3071 assert_string_equal(actions->dsc, "desc");
3072 assert_string_equal(actions->ref, "ref");
3073 assert_string_equal(*actions->iffeatures, "iff");
3074 assert_string_equal(actions->typedefs->name, "tpdf");
3075 assert_string_equal(actions->groupings->name, "grouping");
3076 assert_string_equal(actions->input.data->name, "uses-name");
3077 assert_string_equal(actions->output.musts->arg, "cond");
3078 assert_null(actions->exts);
3079 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3080 actions = NULL;
3081
David Sedlákeaa45792019-07-24 15:25:01 +02003082 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3083 data = ELEMENT_WRAPPER_START
3084 "<rpc name=\"act\">"
3085 "<description><text>desc</text></description>"
3086 "<grouping name=\"grouping\"/>"
3087 "<if-feature name=\"iff\"/>"
3088 "<input><uses name=\"uses-name\"/></input>"
3089 "<output><must condition=\"cond\"/></output>"
3090 "<reference><text>ref</text></reference>"
3091 "<status value=\"deprecated\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003092 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>"
David Sedlákeaa45792019-07-24 15:25:01 +02003093 "</rpc>"
3094 ELEMENT_WRAPPER_END;
3095 assert_int_equal(test_element_helper(st, &data, &act_meta, NULL, NULL, true), LY_SUCCESS);
3096 assert_null(actions->parent);
3097 assert_int_equal(actions->nodetype, LYS_ACTION);
3098 assert_true(actions->flags & LYS_STATUS_DEPRC);
3099 assert_string_equal(actions->name, "act");
3100 assert_string_equal(actions->dsc, "desc");
3101 assert_string_equal(actions->ref, "ref");
3102 assert_string_equal(*actions->iffeatures, "iff");
3103 assert_string_equal(actions->typedefs->name, "tpdf");
3104 assert_string_equal(actions->groupings->name, "grouping");
3105 assert_string_equal(actions->input.data->name, "uses-name");
3106 assert_string_equal(actions->output.musts->arg, "cond");
3107 assert_null(actions->exts);
3108 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3109 actions = NULL;
3110
David Sedlák85d0eca2019-07-24 15:15:21 +02003111 /* min subelems */
3112 data = ELEMENT_WRAPPER_START "<action name=\"act\" />" ELEMENT_WRAPPER_END;
3113 assert_int_equal(test_element_helper(st, &data, &act_meta, NULL, NULL, true), LY_SUCCESS);
3114 assert_string_equal(actions->name, "act");
3115 FREE_ARRAY(st->ctx, actions, lysp_action_free)
3116 actions = NULL;
3117
3118 st->finished_correctly = true;
3119}
3120
David Sedlák992fb7c2019-07-24 16:51:01 +02003121static void
3122test_augment_elem(void **state)
3123{
3124 struct state *st = *state;
3125 const char *data;
3126 struct lysp_augment *augments = NULL;
3127 struct augment_meta aug_meta = {NULL, &augments};
3128
3129 st->yin_ctx->mod_version = LYS_VERSION_1_1;
3130 data = ELEMENT_WRAPPER_START
3131 "<augment target-node=\"target\">"
3132 "<action name=\"action\"/>"
3133 "<anydata name=\"anyd\"/>"
3134 "<anyxml name=\"anyx\"/>"
3135 "<case name=\"case\"/>"
3136 "<choice name=\"choice\"/>"
3137 "<container name=\"subcont\"/>"
3138 "<description><text>desc</text></description>"
3139 "<if-feature name=\"iff\"/>"
David Sedlák8b754462019-07-25 16:22:13 +02003140 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>"
3141 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>"
David Sedlák992fb7c2019-07-24 16:51:01 +02003142 "<list name=\"list\"/>"
3143 "<notification name=\"notif\"/>"
3144 "<reference><text>ref</text></reference>"
3145 "<status value=\"current\"/>"
3146 "<uses name=\"uses\"/>"
3147 "<when condition=\"when-cond\"/>"
3148 "</augment>"
3149 ELEMENT_WRAPPER_END;
3150 assert_int_equal(test_element_helper(st, &data, &aug_meta, NULL, NULL, true), LY_SUCCESS);
3151 assert_string_equal(augments->nodeid, "target");
3152 assert_null(augments->parent);
3153 assert_int_equal(augments->nodetype, LYS_AUGMENT);
3154 assert_true(augments->flags & LYS_STATUS_CURR);
3155 assert_string_equal(augments->dsc, "desc");
3156 assert_string_equal(augments->ref, "ref");
3157 assert_string_equal(augments->when->cond, "when-cond");
3158 assert_string_equal(*augments->iffeatures, "iff");
3159 assert_string_equal(augments->child->name, "anyd");
3160 assert_int_equal(augments->child->nodetype, LYS_ANYDATA);
3161 assert_string_equal(augments->child->next->name, "anyx");
3162 assert_int_equal(augments->child->next->nodetype, LYS_ANYXML);
3163 assert_string_equal(augments->child->next->next->name, "case");
3164 assert_int_equal(augments->child->next->next->nodetype, LYS_CASE);
3165 assert_string_equal(augments->child->next->next->next->name, "choice");
3166 assert_int_equal(augments->child->next->next->next->nodetype, LYS_CHOICE);
3167 assert_string_equal(augments->child->next->next->next->next->name, "subcont");
3168 assert_int_equal(augments->child->next->next->next->next->nodetype, LYS_CONTAINER);
3169 assert_string_equal(augments->child->next->next->next->next->next->name, "leaf");
3170 assert_int_equal(augments->child->next->next->next->next->next->nodetype, LYS_LEAF);
3171 assert_string_equal(augments->child->next->next->next->next->next->next->name, "llist");
3172 assert_int_equal(augments->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3173 assert_string_equal(augments->child->next->next->next->next->next->next->next->name, "list");
3174 assert_int_equal(augments->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
3175 assert_string_equal(augments->child->next->next->next->next->next->next->next->next->name, "uses");
3176 assert_int_equal(augments->child->next->next->next->next->next->next->next->next->nodetype, LYS_USES);
3177 assert_null(augments->child->next->next->next->next->next->next->next->next->next);
3178 assert_string_equal(augments->actions->name, "action");
3179 assert_string_equal(augments->notifs->name, "notif");
3180 assert_null(augments->exts);
3181 FREE_ARRAY(st->ctx, augments, lysp_augment_free)
3182 augments = NULL;
3183
3184 data = ELEMENT_WRAPPER_START "<augment target-node=\"target\" />" ELEMENT_WRAPPER_END;
3185 assert_int_equal(test_element_helper(st, &data, &aug_meta, NULL, NULL, true), LY_SUCCESS);
3186 assert_string_equal(augments->nodeid, "target");
3187 FREE_ARRAY(st->ctx, augments, lysp_augment_free)
3188 augments = NULL;
3189
3190 st->finished_correctly = true;
3191}
3192
David Sedlák4ffcec82019-07-25 15:10:21 +02003193static void
3194test_deviate_elem(void **state)
3195{
3196 struct state *st = *state;
3197 const char *data;
3198 struct lysp_deviate *deviates = NULL;
3199 struct lysp_deviate_add *d_add;
3200 struct lysp_deviate_rpl *d_rpl;
3201 struct lysp_deviate_del *d_del;
3202
3203 /* all valid arguments with min subelems */
3204 data = ELEMENT_WRAPPER_START "<deviate value=\"not-supported\" />" ELEMENT_WRAPPER_END;
3205 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3206 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
3207 lysp_deviate_free(st->ctx, deviates);
3208 free(deviates);
3209 deviates = NULL;
3210
3211 data = ELEMENT_WRAPPER_START "<deviate value=\"add\" />" ELEMENT_WRAPPER_END;
3212 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3213 assert_int_equal(deviates->mod, LYS_DEV_ADD);
3214 lysp_deviate_free(st->ctx, deviates);
3215 free(deviates);
3216 deviates = NULL;
3217
3218 data = ELEMENT_WRAPPER_START "<deviate value=\"replace\" />" ELEMENT_WRAPPER_END;
3219 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3220 assert_int_equal(deviates->mod, LYS_DEV_REPLACE);
3221 lysp_deviate_free(st->ctx, deviates);
3222 free(deviates);
3223 deviates = NULL;
3224
3225 data = ELEMENT_WRAPPER_START "<deviate value=\"delete\" />" ELEMENT_WRAPPER_END;
3226 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3227 assert_int_equal(deviates->mod, LYS_DEV_DELETE);
3228 lysp_deviate_free(st->ctx, deviates);
3229 free(deviates);
3230 deviates = NULL;
3231
3232 /* max subelems and valid arguments */
3233 data = ELEMENT_WRAPPER_START
3234 "<deviate value=\"not-supported\">"
3235 "</deviate>"
3236 ELEMENT_WRAPPER_END;
3237 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3238 assert_int_equal(deviates->mod, LYS_DEV_NOT_SUPPORTED);
3239 lysp_deviate_free(st->ctx, deviates);
3240 free(deviates);
3241 deviates = NULL;
3242
3243 data = ELEMENT_WRAPPER_START
3244 "<deviate value=\"add\">"
3245 "<units name=\"units\"/>"
3246 "<must condition=\"cond\"/>"
3247 "<unique tag=\"utag\"/>"
3248 "<default value=\"def\"/>"
3249 "<config value=\"true\"/>"
3250 "<mandatory value=\"true\"/>"
3251 "<min-elements value=\"5\"/>"
3252 "<max-elements value=\"15\"/>"
3253 "</deviate>"
3254 ELEMENT_WRAPPER_END;
3255 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3256 d_add = (struct lysp_deviate_add *)deviates;
3257 assert_int_equal(d_add->mod, LYS_DEV_ADD);
3258 assert_null(d_add->next);
3259 assert_null(d_add->exts);
3260 assert_string_equal(d_add->units, "units");
3261 assert_string_equal(d_add->musts->arg, "cond");
3262 assert_string_equal(*d_add->uniques, "utag");
3263 assert_string_equal(*d_add->dflts, "def");
3264 assert_true(d_add->flags & LYS_MAND_TRUE && d_add->flags & LYS_CONFIG_W);
3265 assert_int_equal(d_add->min, 5);
3266 assert_int_equal(d_add->max, 15);
3267 lysp_deviate_free(st->ctx, deviates);
3268 free(deviates);
3269 deviates = NULL;
3270
3271 data = ELEMENT_WRAPPER_START
3272 "<deviate value=\"replace\">"
3273 "<type name=\"newtype\"/>"
3274 "<units name=\"uni\"/>"
3275 "<default value=\"def\"/>"
3276 "<config value=\"true\"/>"
3277 "<mandatory value=\"true\"/>"
3278 "<min-elements value=\"5\"/>"
3279 "<max-elements value=\"15\"/>"
3280 "</deviate>"
3281 ELEMENT_WRAPPER_END;
3282 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3283 d_rpl = (struct lysp_deviate_rpl *)deviates;
3284 assert_int_equal(d_rpl->mod, LYS_DEV_REPLACE);
3285 assert_null(d_rpl->next);
3286 assert_null(d_rpl->exts);
3287 assert_string_equal(d_rpl->type->name, "newtype");
3288 assert_string_equal(d_rpl->units, "uni");
3289 assert_string_equal(d_rpl->dflt, "def");
3290 assert_true(d_rpl->flags & LYS_MAND_TRUE && d_rpl->flags & LYS_CONFIG_W);
3291 assert_int_equal(d_rpl->min, 5);
3292 assert_int_equal(d_rpl->max, 15);
3293 lysp_deviate_free(st->ctx, deviates);
3294 free(deviates);
3295 deviates = NULL;
3296
3297 data = ELEMENT_WRAPPER_START
3298 "<deviate value=\"delete\">"
3299 "<units name=\"u\"/>"
3300 "<must condition=\"c\"/>"
3301 "<unique tag=\"tag\"/>"
3302 "<default value=\"default\"/>"
3303 "</deviate>"
3304 ELEMENT_WRAPPER_END;
3305 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, true), LY_SUCCESS);
3306 d_del = (struct lysp_deviate_del *)deviates;
3307 assert_int_equal(d_del->mod, LYS_DEV_DELETE);
3308 assert_null(d_del->next);
3309 assert_null(d_del->exts);
3310 assert_string_equal(d_del->units, "u");
3311 assert_string_equal(d_del->musts->arg, "c");
3312 assert_string_equal(*d_del->uniques, "tag");
3313 assert_string_equal(*d_del->dflts, "default");
3314 lysp_deviate_free(st->ctx, deviates);
3315 free(deviates);
3316 deviates = NULL;
3317
3318 /* invalid arguments */
3319 data = ELEMENT_WRAPPER_START "<deviate value=\"\" />" ELEMENT_WRAPPER_END;
3320 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
3321 logbuf_assert("Invalid value \"\" of \"deviate\". Line number 1.");
3322 deviates = NULL;
3323
3324 data = ELEMENT_WRAPPER_START "<deviate value=\"invalid\" />" ELEMENT_WRAPPER_END;
3325 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
3326 logbuf_assert("Invalid value \"invalid\" of \"deviate\". Line number 1.");
3327 deviates = NULL;
3328
3329 data = ELEMENT_WRAPPER_START "<deviate value=\"ad\" />" ELEMENT_WRAPPER_END;
3330 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
3331 logbuf_assert("Invalid value \"ad\" of \"deviate\". Line number 1.");
3332 deviates = NULL;
3333
3334 data = ELEMENT_WRAPPER_START "<deviate value=\"adds\" />" ELEMENT_WRAPPER_END;
3335 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
3336 logbuf_assert("Invalid value \"adds\" of \"deviate\". Line number 1.");
3337 deviates = NULL;
3338
3339 data = ELEMENT_WRAPPER_START
3340 "<deviate value=\"not-supported\">"
3341 "<must condition=\"c\"/>"
3342 "</deviate>"
3343 ELEMENT_WRAPPER_END;
3344 assert_int_equal(test_element_helper(st, &data, &deviates, NULL, NULL, false), LY_EVALID);
3345 logbuf_assert("Deviate of this type doesn't allow \"must\" as it's sub-element. Line number 1.");
3346
3347 st->finished_correctly = true;
3348}
3349
David Sedlák8b754462019-07-25 16:22:13 +02003350static void
3351test_deviation_elem(void **state)
3352{
3353 struct state *st = *state;
3354 const char *data;
3355 struct lysp_deviation *deviations = NULL;
3356
3357 /* min subelems */
3358 data = ELEMENT_WRAPPER_START
3359 "<deviation target-node=\"target\">"
3360 "<deviate value=\"not-supported\"/>"
3361 "</deviation>"
3362 ELEMENT_WRAPPER_END;
3363 assert_int_equal(test_element_helper(st, &data, &deviations, NULL, NULL, true), LY_SUCCESS);
3364 assert_string_equal(deviations->nodeid, "target");
3365 assert_int_equal(deviations->deviates->mod, LYS_DEV_NOT_SUPPORTED);
3366 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3367 deviations = NULL;
3368
3369 /* max subelems */
3370 data = ELEMENT_WRAPPER_START
3371 "<deviation target-node=\"target\">"
3372 "<reference><text>ref</text></reference>"
3373 "<description><text>desc</text></description>"
3374 "<deviate value=\"add\"/>"
3375 "</deviation>"
3376 ELEMENT_WRAPPER_END;
3377 assert_int_equal(test_element_helper(st, &data, &deviations, NULL, NULL, true), LY_SUCCESS);
3378 assert_string_equal(deviations->nodeid, "target");
3379 assert_int_equal(deviations->deviates->mod, LYS_DEV_ADD);
3380 assert_string_equal(deviations->ref, "ref");
3381 assert_string_equal(deviations->dsc, "desc");
3382 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3383 deviations = NULL;
3384
3385 /* invalid */
3386 data = ELEMENT_WRAPPER_START "<deviation target-node=\"target\"/>" ELEMENT_WRAPPER_END;
3387 assert_int_equal(test_element_helper(st, &data, &deviations, NULL, NULL, false), LY_EVALID);
3388 FREE_ARRAY(st->ctx, deviations, lysp_deviation_free);
3389 deviations = NULL;
3390 logbuf_assert("Missing mandatory subelement deviate of deviate element. Line number 1.");
3391
3392 st->finished_correctly = true;
3393}
3394
David Sedlák4f03b932019-07-26 13:01:47 +02003395static void
3396test_module_elem(void **state)
3397{
3398 struct state *st = *state;
3399 const char *data;
3400 struct yin_arg_record *attrs = NULL;
3401 struct sized_string name, prefix;
3402 struct lys_module *lys_mod = NULL;
3403 struct lysp_module *lysp_mod = NULL;
3404
3405 /* max subelems */
3406 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
3407 lys_mod = calloc(1, sizeof *lys_mod);
3408 lysp_mod = calloc(1, sizeof *lysp_mod);
3409 lys_mod->ctx = st->ctx;
3410 lysp_mod->mod = lys_mod;
3411 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
3412 "<yang-version value=\"1.1\"/>\n"
3413 "<namespace uri=\"ns\"/>\n"
3414 "<prefix value=\"pref\"/>\n"
3415 "<include module=\"b-mod\"/>\n"
3416 "<import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
3417 "<organization><text>org</text></organization>\n"
3418 "<contact><text>contact</text></contact>\n"
3419 "<description><text>desc</text></description>"
3420 "<reference><text>ref</text></reference>\n"
3421 "<revision date=\"2019-02-02\"/>\n"
3422 "<anydata name=\"anyd\"/>\n"
3423 "<anyxml name=\"anyx\"/>\n"
3424 "<choice name=\"choice\"/>\n"
3425 "<container name=\"cont\"/>\n"
3426 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3427 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3428 "<list name=\"sub-list\"/>\n"
3429 "<uses name=\"uses-name\"/>\n"
3430 "<augment target-node=\"target\"/>\n"
3431 "<deviation target-node=\"target\">""<deviate value=\"not-supported\"/>""</deviation>\n"
3432 "<extension name=\"ext\"/>\n"
3433 "<feature name=\"feature\"/>\n"
3434 "<grouping name=\"grp\"/>\n"
3435 "<identity name=\"ident-name\"/>\n"
3436 "<notification name=\"notf\"/>\n"
3437 "<rpc name=\"rpc-name\"/>\n"
3438 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3439 "</module>\n";
3440 assert_int_equal(lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len), LY_SUCCESS);
3441 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
3442 assert_int_equal(yin_parse_mod(st->yin_ctx, attrs, &data, lysp_mod), LY_SUCCESS);
3443 assert_string_equal(lysp_mod->mod->name, "mod");
3444 assert_string_equal(lysp_mod->revs, "2019-02-02");
3445 assert_string_equal(lysp_mod->mod->ns, "ns");
3446 assert_string_equal(lysp_mod->mod->prefix, "pref");
3447 assert_null(lysp_mod->mod->filepath);
3448 assert_string_equal(lysp_mod->mod->org, "org");
3449 assert_string_equal(lysp_mod->mod->contact, "contact");
3450 assert_string_equal(lysp_mod->mod->dsc, "desc");
3451 assert_string_equal(lysp_mod->mod->ref, "ref");
3452 assert_int_equal(lysp_mod->mod->version, LYS_VERSION_1_1);
3453 assert_string_equal(lysp_mod->imports->name, "a-mod");
3454 assert_string_equal(lysp_mod->includes->name, "b-mod");
3455 assert_string_equal(lysp_mod->extensions->name, "ext");
3456 assert_string_equal(lysp_mod->features->name, "feature");
3457 assert_string_equal(lysp_mod->identities->name, "ident-name");
3458 assert_string_equal(lysp_mod->typedefs->name, "tpdf");
3459 assert_string_equal(lysp_mod->groupings->name, "grp");
3460 assert_string_equal(lysp_mod->data->name, "anyd");
3461 assert_int_equal(lysp_mod->data->nodetype, LYS_ANYDATA);
3462 assert_string_equal(lysp_mod->data->next->name, "anyx");
3463 assert_int_equal(lysp_mod->data->next->nodetype, LYS_ANYXML);
3464 assert_string_equal(lysp_mod->data->next->next->name, "choice");
3465 assert_int_equal(lysp_mod->data->next->next->nodetype, LYS_CHOICE);
3466 assert_string_equal(lysp_mod->data->next->next->next->name, "cont");
3467 assert_int_equal(lysp_mod->data->next->next->next->nodetype, LYS_CONTAINER);
3468 assert_string_equal(lysp_mod->data->next->next->next->next->name, "leaf");
3469 assert_int_equal(lysp_mod->data->next->next->next->next->nodetype, LYS_LEAF);
3470 assert_string_equal(lysp_mod->data->next->next->next->next->next->name, "llist");
3471 assert_int_equal(lysp_mod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3472 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->name, "sub-list");
3473 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
3474 assert_string_equal(lysp_mod->data->next->next->next->next->next->next->next->name, "uses-name");
3475 assert_int_equal(lysp_mod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3476 assert_null(lysp_mod->data->next->next->next->next->next->next->next->next);
3477 assert_string_equal(lysp_mod->augments->nodeid, "target");
3478 assert_string_equal(lysp_mod->rpcs->name, "rpc-name");
3479 assert_string_equal(lysp_mod->notifs->name, "notf");
3480 assert_string_equal(lysp_mod->deviations->nodeid, "target");
3481 assert_null(lysp_mod->exts);
3482 lysp_module_free(lysp_mod);
3483 lys_module_free(lys_mod, NULL);
3484 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
3485 attrs = NULL;
3486
3487 /* min subelems */
3488 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
3489 lys_mod = calloc(1, sizeof *lys_mod);
3490 lysp_mod = calloc(1, sizeof *lysp_mod);
3491 lys_mod->ctx = st->ctx;
3492 lysp_mod->mod = lys_mod;
3493 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">"
3494 "<namespace uri=\"ns\"/>"
3495 "<prefix value=\"pref\"/>"
3496 "<yang-version value=\"1.1\"/>"
3497 "</module>";
3498 assert_int_equal(lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len), LY_SUCCESS);
3499 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
3500 assert_int_equal(yin_parse_mod(st->yin_ctx, attrs, &data, lysp_mod), LY_SUCCESS);
David Sedlák298ff6d2019-07-26 14:29:03 +02003501 assert_string_equal(lysp_mod->mod->name, "mod");
David Sedlák4f03b932019-07-26 13:01:47 +02003502 lysp_module_free(lysp_mod);
3503 lys_module_free(lys_mod, NULL);
3504 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
3505 attrs = NULL;
David Sedlák298ff6d2019-07-26 14:29:03 +02003506
3507 st->finished_correctly = true;
3508}
3509
3510static void
3511test_submodule_elem(void **state)
3512{
3513 struct state *st = *state;
3514 const char *data;
3515 struct yin_arg_record *attrs = NULL;
3516 struct sized_string name, prefix;
3517 struct lysp_submodule *lysp_submod = NULL;
3518
3519 /* max subelements */
3520 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
3521 lysp_submod = calloc(1, sizeof *lysp_submod);
3522 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"mod\">\n"
3523 "<yang-version value=\"1.1\"/>\n"
3524 "<belongs-to module=\"mod-name\"><prefix value=\"pref\"/></belongs-to>"
3525 "<include module=\"b-mod\"/>\n"
3526 "<import module=\"a-mod\"><prefix value=\"imp-pref\"/></import>\n"
3527 "<organization><text>org</text></organization>\n"
3528 "<contact><text>contact</text></contact>\n"
3529 "<description><text>desc</text></description>"
3530 "<reference><text>ref</text></reference>\n"
3531 "<revision date=\"2019-02-02\"/>\n"
3532 "<anydata name=\"anyd\"/>\n"
3533 "<anyxml name=\"anyx\"/>\n"
3534 "<choice name=\"choice\"/>\n"
3535 "<container name=\"cont\"/>\n"
3536 "<leaf name=\"leaf\"> <type name=\"type\"/> </leaf>\n"
3537 "<leaf-list name=\"llist\"> <type name=\"type\"/> </leaf-list>\n"
3538 "<list name=\"sub-list\"/>\n"
3539 "<uses name=\"uses-name\"/>\n"
3540 "<augment target-node=\"target\"/>\n"
3541 "<deviation target-node=\"target\">""<deviate value=\"not-supported\"/>""</deviation>\n"
3542 "<extension name=\"ext\"/>\n"
3543 "<feature name=\"feature\"/>\n"
3544 "<grouping name=\"grp\"/>\n"
3545 "<identity name=\"ident-name\"/>\n"
3546 "<notification name=\"notf\"/>\n"
3547 "<rpc name=\"rpc-name\"/>\n"
3548 "<typedef name=\"tpdf\"> <type name=\"type\"/> </typedef>\n"
3549 "</submodule>\n";
3550 assert_int_equal(lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len), LY_SUCCESS);
3551 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
3552 assert_int_equal(yin_parse_submod(st->yin_ctx, attrs, &data, lysp_submod), LY_SUCCESS);
3553
3554 assert_string_equal(lysp_submod->name, "mod");
3555 assert_string_equal(lysp_submod->revs, "2019-02-02");
3556 //assert_string_equal(lysp_submod->ns, "ns");
3557 assert_string_equal(lysp_submod->prefix, "pref");
3558 assert_null(lysp_submod->filepath);
3559 assert_string_equal(lysp_submod->org, "org");
3560 assert_string_equal(lysp_submod->contact, "contact");
3561 assert_string_equal(lysp_submod->dsc, "desc");
3562 assert_string_equal(lysp_submod->ref, "ref");
3563 assert_int_equal(lysp_submod->version, LYS_VERSION_1_1);
3564 assert_string_equal(lysp_submod->imports->name, "a-mod");
3565 assert_string_equal(lysp_submod->includes->name, "b-mod");
3566 assert_string_equal(lysp_submod->extensions->name, "ext");
3567 assert_string_equal(lysp_submod->features->name, "feature");
3568 assert_string_equal(lysp_submod->identities->name, "ident-name");
3569 assert_string_equal(lysp_submod->typedefs->name, "tpdf");
3570 assert_string_equal(lysp_submod->groupings->name, "grp");
3571 assert_string_equal(lysp_submod->data->name, "anyd");
3572 assert_int_equal(lysp_submod->data->nodetype, LYS_ANYDATA);
3573 assert_string_equal(lysp_submod->data->next->name, "anyx");
3574 assert_int_equal(lysp_submod->data->next->nodetype, LYS_ANYXML);
3575 assert_string_equal(lysp_submod->data->next->next->name, "choice");
3576 assert_int_equal(lysp_submod->data->next->next->nodetype, LYS_CHOICE);
3577 assert_string_equal(lysp_submod->data->next->next->next->name, "cont");
3578 assert_int_equal(lysp_submod->data->next->next->next->nodetype, LYS_CONTAINER);
3579 assert_string_equal(lysp_submod->data->next->next->next->next->name, "leaf");
3580 assert_int_equal(lysp_submod->data->next->next->next->next->nodetype, LYS_LEAF);
3581 assert_string_equal(lysp_submod->data->next->next->next->next->next->name, "llist");
3582 assert_int_equal(lysp_submod->data->next->next->next->next->next->nodetype, LYS_LEAFLIST);
3583 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->name, "sub-list");
3584 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->nodetype, LYS_LIST);
3585 assert_string_equal(lysp_submod->data->next->next->next->next->next->next->next->name, "uses-name");
3586 assert_int_equal(lysp_submod->data->next->next->next->next->next->next->next->nodetype, LYS_USES);
3587 assert_null(lysp_submod->data->next->next->next->next->next->next->next->next);
3588 assert_string_equal(lysp_submod->augments->nodeid, "target");
3589 assert_string_equal(lysp_submod->rpcs->name, "rpc-name");
3590 assert_string_equal(lysp_submod->notifs->name, "notf");
3591 assert_string_equal(lysp_submod->deviations->nodeid, "target");
3592 assert_null(lysp_submod->exts);
3593
3594 lysp_submodule_free(st->ctx, lysp_submod);
3595 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
3596 attrs = NULL;
3597
3598 /* min subelemnts */
3599 st->yin_ctx->xml_ctx.status = LYXML_ELEMENT;
3600 lysp_submod = calloc(1, sizeof *lysp_submod);
3601 data = "<submodule xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\"submod\">"
3602 "<yang-version value=\"1.0\"/>"
3603 "<belongs-to module=\"mod-name\"><prefix value=\"pref\"/></belongs-to>"
3604 "</submodule>";
3605 assert_int_equal(lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len), LY_SUCCESS);
3606 assert_int_equal(yin_load_attributes(st->yin_ctx, &data, &attrs), LY_SUCCESS);
3607 assert_int_equal(yin_parse_submod(st->yin_ctx, attrs, &data, lysp_submod), LY_SUCCESS);
3608 assert_string_equal(lysp_submod->prefix, "pref");
3609 assert_string_equal(lysp_submod->belongsto, "mod-name");
3610 assert_int_equal(lysp_submod->version, LYS_VERSION_1_0);
3611 lysp_submodule_free(st->ctx, lysp_submod);
3612 FREE_ARRAY(st->yin_ctx, attrs, free_arg_rec);
3613 attrs = NULL;
3614
3615 st->finished_correctly = true;
David Sedlák4f03b932019-07-26 13:01:47 +02003616}
3617
David Sedlák3b4db242018-10-19 16:11:01 +02003618int
3619main(void)
3620{
3621
3622 const struct CMUnitTest tests[] = {
David Sedlák8f7a1172019-06-20 14:42:18 +02003623 cmocka_unit_test_setup_teardown(test_yin_match_keyword, setup_f, teardown_f),
David Sedlákb1a78352019-06-28 16:16:29 +02003624 cmocka_unit_test_setup_teardown(test_yin_parse_element_generic, setup_f, teardown_f),
3625 cmocka_unit_test_setup_teardown(test_yin_parse_extension_instance, setup_f, teardown_f),
David Sedlák555c7202019-07-04 12:14:12 +02003626 cmocka_unit_test_setup_teardown(test_yin_parse_content, setup_f, teardown_f),
David Sedlák4a650532019-07-10 11:55:18 +02003627 cmocka_unit_test_setup_teardown(test_validate_value, setup_f, teardown_f),
David Sedlák32488102019-07-15 17:44:10 +02003628
David Sedlák8e7bda82019-07-16 17:57:50 +02003629 cmocka_unit_test(test_yin_match_argument_name),
David Sedlák32488102019-07-15 17:44:10 +02003630 cmocka_unit_test_setup_teardown(test_enum_bit_elem, setup_element_test, teardown_element_test),
3631 cmocka_unit_test_setup_teardown(test_meta_elem, setup_element_test, teardown_element_test),
3632 cmocka_unit_test_setup_teardown(test_import_elem, setup_element_test, teardown_element_test),
3633 cmocka_unit_test_setup_teardown(test_status_elem, setup_element_test, teardown_element_test),
3634 cmocka_unit_test_setup_teardown(test_ext_elem, setup_element_test, teardown_element_test),
3635 cmocka_unit_test_setup_teardown(test_yin_element_elem, setup_element_test, teardown_element_test),
3636 cmocka_unit_test_setup_teardown(test_yangversion_elem, setup_element_test, teardown_element_test),
3637 cmocka_unit_test_setup_teardown(test_mandatory_elem, setup_element_test, teardown_element_test),
David Sedlák8e7bda82019-07-16 17:57:50 +02003638 cmocka_unit_test_setup_teardown(test_argument_elem, setup_element_test, teardown_element_test),
3639 cmocka_unit_test_setup_teardown(test_base_elem, setup_element_test, teardown_element_test),
3640 cmocka_unit_test_setup_teardown(test_belongsto_elem, setup_element_test, teardown_element_test),
3641 cmocka_unit_test_setup_teardown(test_config_elem, setup_element_test, teardown_element_test),
3642 cmocka_unit_test_setup_teardown(test_default_elem, setup_element_test, teardown_element_test),
3643 cmocka_unit_test_setup_teardown(test_err_app_tag_elem, setup_element_test, teardown_element_test),
3644 cmocka_unit_test_setup_teardown(test_err_msg_elem, setup_element_test, teardown_element_test),
3645 cmocka_unit_test_setup_teardown(test_fracdigits_elem, setup_element_test, teardown_element_test),
3646 cmocka_unit_test_setup_teardown(test_iffeature_elem, setup_element_test, teardown_element_test),
3647 cmocka_unit_test_setup_teardown(test_length_elem, setup_element_test, teardown_element_test),
3648 cmocka_unit_test_setup_teardown(test_modifier_elem, setup_element_test, teardown_element_test),
3649 cmocka_unit_test_setup_teardown(test_namespace_elem, setup_element_test, teardown_element_test),
3650 cmocka_unit_test_setup_teardown(test_path_elem, setup_element_test, teardown_element_test),
3651 cmocka_unit_test_setup_teardown(test_pattern_elem, setup_element_test, teardown_element_test),
3652 cmocka_unit_test_setup_teardown(test_value_position_elem, setup_element_test, teardown_element_test),
David Sedlák69f01612019-07-17 11:41:08 +02003653 cmocka_unit_test_setup_teardown(test_prefix_elem, setup_element_test, teardown_element_test),
3654 cmocka_unit_test_setup_teardown(test_range_elem, setup_element_test, teardown_element_test),
3655 cmocka_unit_test_setup_teardown(test_reqinstance_elem, setup_element_test, teardown_element_test),
3656 cmocka_unit_test_setup_teardown(test_revision_date_elem, setup_element_test, teardown_element_test),
3657 cmocka_unit_test_setup_teardown(test_unique_elem, setup_element_test, teardown_element_test),
3658 cmocka_unit_test_setup_teardown(test_units_elem, setup_element_test, teardown_element_test),
3659 cmocka_unit_test_setup_teardown(test_when_elem, setup_element_test, teardown_element_test),
3660 cmocka_unit_test_setup_teardown(test_yin_text_value_elem, setup_element_test, teardown_element_test),
David Sedlák374d2b32019-07-17 15:06:55 +02003661 cmocka_unit_test_setup_teardown(test_type_elem, setup_element_test, teardown_element_test),
David Sedlák1af868e2019-07-17 17:03:14 +02003662 cmocka_unit_test_setup_teardown(test_max_elems_elem, setup_element_test, teardown_element_test),
David Sedlák09e18c92019-07-18 11:17:11 +02003663 cmocka_unit_test_setup_teardown(test_min_elems_elem, setup_element_test, teardown_element_test),
David Sedláka2dad212019-07-18 12:45:19 +02003664 cmocka_unit_test_setup_teardown(test_ordby_elem, setup_element_test, teardown_element_test),
David Sedlák8a83bbb2019-07-18 14:46:00 +02003665 cmocka_unit_test_setup_teardown(test_any_elem, setup_element_test, teardown_element_test),
David Sedlák203ca3a2019-07-18 15:26:25 +02003666 cmocka_unit_test_setup_teardown(test_leaf_elem, setup_element_test, teardown_element_test),
David Sedlákc3da3ef2019-07-19 12:56:08 +02003667 cmocka_unit_test_setup_teardown(test_leaf_list_elem, setup_element_test, teardown_element_test),
David Sedlákcb39f642019-07-19 13:19:55 +02003668 cmocka_unit_test_setup_teardown(test_presence_elem, setup_element_test, teardown_element_test),
David Sedlák12470a82019-07-19 13:44:36 +02003669 cmocka_unit_test_setup_teardown(test_key_elem, setup_element_test, teardown_element_test),
David Sedlák04e17b22019-07-19 15:29:48 +02003670 cmocka_unit_test_setup_teardown(test_typedef_elem, setup_element_test, teardown_element_test),
David Sedlákd2d676a2019-07-22 11:28:19 +02003671 cmocka_unit_test_setup_teardown(test_refine_elem, setup_element_test, teardown_element_test),
David Sedlák0d6de5a2019-07-22 13:25:44 +02003672 cmocka_unit_test_setup_teardown(test_uses_elem, setup_element_test, teardown_element_test),
David Sedlákaa854b02019-07-22 14:17:10 +02003673 cmocka_unit_test_setup_teardown(test_revision_elem, setup_element_test, teardown_element_test),
David Sedlák0c2bab92019-07-22 15:33:19 +02003674 cmocka_unit_test_setup_teardown(test_include_elem, setup_element_test, teardown_element_test),
David Sedlák5e13dea2019-07-22 16:06:45 +02003675 cmocka_unit_test_setup_teardown(test_feature_elem, setup_element_test, teardown_element_test),
David Sedlák28794f22019-07-22 16:45:00 +02003676 cmocka_unit_test_setup_teardown(test_identity_elem, setup_element_test, teardown_element_test),
David Sedlákaf536aa2019-07-23 13:42:23 +02003677 cmocka_unit_test_setup_teardown(test_list_elem, setup_element_test, teardown_element_test),
David Sedlák031b9e72019-07-23 15:19:37 +02003678 cmocka_unit_test_setup_teardown(test_notification_elem, setup_element_test, teardown_element_test),
David Sedláke3ce9ef2019-07-23 16:34:30 +02003679 cmocka_unit_test_setup_teardown(test_grouping_elem, setup_element_test, teardown_element_test),
David Sedlákf111bcb2019-07-23 17:15:51 +02003680 cmocka_unit_test_setup_teardown(test_container_elem, setup_element_test, teardown_element_test),
David Sedlák5379d392019-07-24 10:42:03 +02003681 cmocka_unit_test_setup_teardown(test_case_elem, setup_element_test, teardown_element_test),
David Sedlákb7abcfa2019-07-24 12:33:35 +02003682 cmocka_unit_test_setup_teardown(test_choice_elem, setup_element_test, teardown_element_test),
David Sedlák05404f62019-07-24 14:11:53 +02003683 cmocka_unit_test_setup_teardown(test_inout_elem, setup_element_test, teardown_element_test),
David Sedlák85d0eca2019-07-24 15:15:21 +02003684 cmocka_unit_test_setup_teardown(test_action_elem, setup_element_test, teardown_element_test),
David Sedlák992fb7c2019-07-24 16:51:01 +02003685 cmocka_unit_test_setup_teardown(test_augment_elem, setup_element_test, teardown_element_test),
David Sedlák4ffcec82019-07-25 15:10:21 +02003686 cmocka_unit_test_setup_teardown(test_deviate_elem, setup_element_test, teardown_element_test),
David Sedlák8b754462019-07-25 16:22:13 +02003687 cmocka_unit_test_setup_teardown(test_deviation_elem, setup_element_test, teardown_element_test),
David Sedlák4f03b932019-07-26 13:01:47 +02003688 cmocka_unit_test_setup_teardown(test_module_elem, setup_element_test, teardown_element_test),
David Sedlák298ff6d2019-07-26 14:29:03 +02003689 cmocka_unit_test_setup_teardown(test_submodule_elem, setup_element_test, teardown_element_test),
David Sedlák3b4db242018-10-19 16:11:01 +02003690 };
3691
David Sedlák8e7bda82019-07-16 17:57:50 +02003692 return cmocka_run_group_tests(tests, setup_ly_ctx, destroy_ly_ctx);
David Sedlák3b4db242018-10-19 16:11:01 +02003693}