blob: 4c458f10fc9b2a9c6ff6a2a1f3a325c4cb27b250 [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);
32
David Sedlák68a1af12019-03-08 13:46:54 +010033struct state {
David Sedlák3b4db242018-10-19 16:11:01 +020034 struct ly_ctx *ctx;
David Sedlák3017da42019-02-15 09:48:04 +010035 struct lys_module *mod;
David Sedlák619db942019-07-03 14:47:30 +020036 struct lysp_module *lysp_mod;
David Sedlák8f5bce02019-06-03 16:41:08 +020037 struct lyxml_context *xml_ctx;
David Sedlák79e50cb2019-06-05 16:33:09 +020038 bool finished_correctly;
David Sedlák68a1af12019-03-08 13:46:54 +010039};
David Sedlák872c7b42018-10-26 13:15:20 +020040
David Sedlák79e50cb2019-06-05 16:33:09 +020041#define BUFSIZE 1024
42char logbuf[BUFSIZE] = {0};
43int store = -1; /* negative for infinite logging, positive for limited logging */
44
45/* set to 0 to printing error messages to stderr instead of checking them in code */
46#define ENABLE_LOGGER_CHECKING 1
47
48#if ENABLE_LOGGER_CHECKING
49static void
50logger(LY_LOG_LEVEL level, const char *msg, const char *path)
51{
52 (void) level; /* unused */
53 if (store) {
54 if (path && path[0]) {
55 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
56 } else {
57 strncpy(logbuf, msg, BUFSIZE - 1);
58 }
59 if (store > 0) {
60 --store;
61 }
62 }
63}
64#endif
65
66#if ENABLE_LOGGER_CHECKING
67# define logbuf_assert(str) assert_string_equal(logbuf, str)
68#else
69# define logbuf_assert(str)
70#endif
71
72#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
73 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
74 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
75 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
76 CLEANUP
77
78
David Sedlák68a1af12019-03-08 13:46:54 +010079static int
80setup_f(void **state)
81{
82 struct state *st = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +020083
David Sedlák79e50cb2019-06-05 16:33:09 +020084#if ENABLE_LOGGER_CHECKING
85 /* setup logger */
86 ly_set_log_clb(logger, 1);
87#endif
88
David Sedlák68a1af12019-03-08 13:46:54 +010089 /* allocate state variable */
90 (*state) = st = calloc(1, sizeof(*st));
91 if (!st) {
92 fprintf(stderr, "Memmory allocation failed");
93 return EXIT_FAILURE;
94 }
David Sedlák872c7b42018-10-26 13:15:20 +020095
David Sedlák68a1af12019-03-08 13:46:54 +010096 /* create new libyang context */
97 ly_ctx_new(NULL, 0, &st->ctx);
David Sedlák872c7b42018-10-26 13:15:20 +020098
David Sedlák68a1af12019-03-08 13:46:54 +010099 /* allocate new module */
100 st->mod = calloc(1, sizeof(*st->mod));
101 st->mod->ctx = st->ctx;
102
David Sedlák619db942019-07-03 14:47:30 +0200103 /* allocate new parsed module */
104 st->lysp_mod = calloc(1, sizeof(*st->lysp_mod));
105 st->lysp_mod->mod = calloc(1, sizeof(*st->lysp_mod->mod));
106 st->lysp_mod->mod->ctx = st->ctx;
107
108 /* allocate parser context */
David Sedlák8f5bce02019-06-03 16:41:08 +0200109 st->xml_ctx = calloc(1, sizeof(struct lys_parser_ctx));
110 st->xml_ctx->ctx = st->ctx;
111 st->xml_ctx->line = 1;
112
David Sedlák68a1af12019-03-08 13:46:54 +0100113 return EXIT_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +0200114}
115
116static int
David Sedlák68a1af12019-03-08 13:46:54 +0100117teardown_f(void **state)
118{
119 struct state *st = *(struct state **)state;
David Sedlák619db942019-07-03 14:47:30 +0200120 struct lys_module *temp;
David Sedlák68a1af12019-03-08 13:46:54 +0100121
David Sedlák79e50cb2019-06-05 16:33:09 +0200122#if ENABLE_LOGGER_CHECKING
123 /* teardown logger */
124 if (!st->finished_correctly && logbuf[0] != '\0') {
125 fprintf(stderr, "%s\n", logbuf);
126 }
127#endif
128
David Sedlák619db942019-07-03 14:47:30 +0200129 temp = st->lysp_mod->mod;
130
David Sedlák8f5bce02019-06-03 16:41:08 +0200131 lyxml_context_clear(st->xml_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100132 lys_module_free(st->mod, NULL);
David Sedlák619db942019-07-03 14:47:30 +0200133 lysp_module_free(st->lysp_mod);
134 lys_module_free(temp, NULL);
David Sedlák68a1af12019-03-08 13:46:54 +0100135 ly_ctx_destroy(st->ctx, NULL);
David Sedlák8f5bce02019-06-03 16:41:08 +0200136 free(st->xml_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100137 free(st);
138
139 return EXIT_SUCCESS;
140}
141
David Sedlák392af4f2019-06-04 16:02:42 +0200142static struct state*
143reset_state(void **state)
144{
David Sedlák79e50cb2019-06-05 16:33:09 +0200145 ((struct state *)*state)->finished_correctly = true;
David Sedlák555c7202019-07-04 12:14:12 +0200146 logbuf[0] = '\0';
David Sedlák392af4f2019-06-04 16:02:42 +0200147 teardown_f(state);
148 setup_f(state);
149
150 return *state;
151}
152
David Sedlák79e50cb2019-06-05 16:33:09 +0200153void
154logbuf_clean(void)
155{
156 logbuf[0] = '\0';
157}
158
David Sedlák68a1af12019-03-08 13:46:54 +0100159static void
David Sedlák392af4f2019-06-04 16:02:42 +0200160test_yin_parse_module(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +0100161{
162 LY_ERR ret = LY_SUCCESS;
163 struct state *st = *state;
164
165 ret = yin_parse_module(st->ctx,
David Sedlák2b214ac2019-06-06 16:11:03 +0200166 "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\
167 name=\"example-foo\"\
David Sedlák18730132019-03-15 15:51:34 +0100168 xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\
169 xmlns:foo=\"urn:example:foo\"\
170 xmlns:myext=\"urn:example:extensions\">\
David Sedlákcd0c9512019-03-29 13:23:06 +0100171 <namespace uri=\"urn:example:foo\" xmlns:myext=\"urn:example:extensions\"/>\
David Sedláka7406952019-04-05 10:33:07 +0200172 <prefix xmlns:myxt=\"urn:emple:extensions\" value=\"foo\" xmlns:myext=\"urn:example:extensions\"/>\
David Sedlákd9d3a312019-06-04 09:47:10 +0200173 </module>",
David Sedlák68a1af12019-03-08 13:46:54 +0100174 st->mod);
175
176 assert_int_equal(ret, LY_SUCCESS);
177 assert_string_equal(st->mod->parsed->mod->name, "example-foo");
178 assert_string_equal(st->mod->parsed->mod->prefix, "foo");
David Sedlákcd0c9512019-03-29 13:23:06 +0100179 assert_string_equal(st->mod->parsed->mod->ns, "urn:example:foo");
David Sedlák392af4f2019-06-04 16:02:42 +0200180
181 st = reset_state(state);
182 ret = yin_parse_module(st->ctx,
David Sedlák2b214ac2019-06-06 16:11:03 +0200183 "<module name=\"example-foo\">\
184 <invalid-tag uri=\"urn:example:foo\"\"/>\
185 </module>",
186 st->mod);
David Sedlák392af4f2019-06-04 16:02:42 +0200187 assert_int_equal(ret, LY_EVALID);
188
189 st = reset_state(state);
190 ret = yin_parse_module(st->ctx,
David Sedlák8f7a1172019-06-20 14:42:18 +0200191 "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\
David Sedlák57715b12019-06-17 13:05:22 +0200192 </module>",
David Sedlák2b214ac2019-06-06 16:11:03 +0200193 st->mod);
David Sedlák392af4f2019-06-04 16:02:42 +0200194 assert_int_equal(ret, LY_EVALID);
David Sedlák21f87cd2019-07-03 16:53:23 +0200195 logbuf_assert("Missing mandatory attribute \"name\" of module element. Line number 1.");
David Sedlák392af4f2019-06-04 16:02:42 +0200196
197 st = reset_state(state);
198 ret = yin_parse_module(st->ctx,
199 "",
200 st->mod);
201 assert_int_equal(ret, LY_EVALID);
David Sedlák79e50cb2019-06-05 16:33:09 +0200202 logbuf_assert("Invalid keyword \"(null)\", expected \"module\" or \"submodule\". Line number 1.");
203 st->finished_correctly = true;
David Sedlák3b4db242018-10-19 16:11:01 +0200204}
205
206static void
David Sedlák1bccdfa2019-06-17 15:55:27 +0200207test_yin_match_keyword(void **state)
David Sedlák3b4db242018-10-19 16:11:01 +0200208{
David Sedlák8f7a1172019-06-20 14:42:18 +0200209 struct state *st = *state;
David Sedlák3b4db242018-10-19 16:11:01 +0200210
David Sedlák8f7a1172019-06-20 14:42:18 +0200211 const char *prefix, *name;
212 struct yin_arg_record *args = NULL;
213 size_t prefix_len, name_len;
214 /* create mock yin namespace in xml context */
215 const char *data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" />";
216 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
217 yin_load_attributes(st->xml_ctx, &data, &args);
218 LY_ARRAY_FREE(args);
219
220 assert_int_equal(yin_match_keyword(st->xml_ctx, "anydatax", strlen("anydatax"), prefix, prefix_len), YANG_NONE);
221 assert_int_equal(yin_match_keyword(st->xml_ctx, "asdasd", strlen("asdasd"), prefix, prefix_len), YANG_NONE);
222 assert_int_equal(yin_match_keyword(st->xml_ctx, "", 0, prefix, prefix_len), YANG_NONE);
223 assert_int_equal(yin_match_keyword(st->xml_ctx, "anydata", strlen("anydata"), prefix, prefix_len), YANG_ANYDATA);
224 assert_int_equal(yin_match_keyword(st->xml_ctx, "anyxml", strlen("anyxml"), prefix, prefix_len), YANG_ANYXML);
225 assert_int_equal(yin_match_keyword(st->xml_ctx, "argument", strlen("argument"), prefix, prefix_len), YANG_ARGUMENT);
226 assert_int_equal(yin_match_keyword(st->xml_ctx, "augment", strlen("augment"), prefix, prefix_len), YANG_AUGMENT);
227 assert_int_equal(yin_match_keyword(st->xml_ctx, "base", strlen("base"), prefix, prefix_len), YANG_BASE);
228 assert_int_equal(yin_match_keyword(st->xml_ctx, "belongs-to", strlen("belongs-to"), prefix, prefix_len), YANG_BELONGS_TO);
229 assert_int_equal(yin_match_keyword(st->xml_ctx, "bit", strlen("bit"), prefix, prefix_len), YANG_BIT);
230 assert_int_equal(yin_match_keyword(st->xml_ctx, "case", strlen("case"), prefix, prefix_len), YANG_CASE);
231 assert_int_equal(yin_match_keyword(st->xml_ctx, "choice", strlen("choice"), prefix, prefix_len), YANG_CHOICE);
232 assert_int_equal(yin_match_keyword(st->xml_ctx, "config", strlen("config"), prefix, prefix_len), YANG_CONFIG);
233 assert_int_equal(yin_match_keyword(st->xml_ctx, "contact", strlen("contact"), prefix, prefix_len), YANG_CONTACT);
234 assert_int_equal(yin_match_keyword(st->xml_ctx, "container", strlen("container"), prefix, prefix_len), YANG_CONTAINER);
235 assert_int_equal(yin_match_keyword(st->xml_ctx, "default", strlen("default"), prefix, prefix_len), YANG_DEFAULT);
236 assert_int_equal(yin_match_keyword(st->xml_ctx, "description", strlen("description"), prefix, prefix_len), YANG_DESCRIPTION);
237 assert_int_equal(yin_match_keyword(st->xml_ctx, "deviate", strlen("deviate"), prefix, prefix_len), YANG_DEVIATE);
238 assert_int_equal(yin_match_keyword(st->xml_ctx, "deviation", strlen("deviation"), prefix, prefix_len), YANG_DEVIATION);
239 assert_int_equal(yin_match_keyword(st->xml_ctx, "enum", strlen("enum"), prefix, prefix_len), YANG_ENUM);
240 assert_int_equal(yin_match_keyword(st->xml_ctx, "error-app-tag", strlen("error-app-tag"), prefix, prefix_len), YANG_ERROR_APP_TAG);
241 assert_int_equal(yin_match_keyword(st->xml_ctx, "error-message", strlen("error-message"), prefix, prefix_len), YANG_ERROR_MESSAGE);
242 assert_int_equal(yin_match_keyword(st->xml_ctx, "extension", strlen("extension"), prefix, prefix_len), YANG_EXTENSION);
243 assert_int_equal(yin_match_keyword(st->xml_ctx, "feature", strlen("feature"), prefix, prefix_len), YANG_FEATURE);
244 assert_int_equal(yin_match_keyword(st->xml_ctx, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len), YANG_FRACTION_DIGITS);
245 assert_int_equal(yin_match_keyword(st->xml_ctx, "grouping", strlen("grouping"), prefix, prefix_len), YANG_GROUPING);
246 assert_int_equal(yin_match_keyword(st->xml_ctx, "identity", strlen("identity"), prefix, prefix_len), YANG_IDENTITY);
247 assert_int_equal(yin_match_keyword(st->xml_ctx, "if-feature", strlen("if-feature"), prefix, prefix_len), YANG_IF_FEATURE);
248 assert_int_equal(yin_match_keyword(st->xml_ctx, "import", strlen("import"), prefix, prefix_len), YANG_IMPORT);
249 assert_int_equal(yin_match_keyword(st->xml_ctx, "include", strlen("include"), prefix, prefix_len), YANG_INCLUDE);
250 assert_int_equal(yin_match_keyword(st->xml_ctx, "input", strlen("input"), prefix, prefix_len), YANG_INPUT);
251 assert_int_equal(yin_match_keyword(st->xml_ctx, "key", strlen("key"), prefix, prefix_len), YANG_KEY);
252 assert_int_equal(yin_match_keyword(st->xml_ctx, "leaf", strlen("leaf"), prefix, prefix_len), YANG_LEAF);
253 assert_int_equal(yin_match_keyword(st->xml_ctx, "leaf-list", strlen("leaf-list"), prefix, prefix_len), YANG_LEAF_LIST);
254 assert_int_equal(yin_match_keyword(st->xml_ctx, "length", strlen("length"), prefix, prefix_len), YANG_LENGTH);
255 assert_int_equal(yin_match_keyword(st->xml_ctx, "list", strlen("list"), prefix, prefix_len), YANG_LIST);
256 assert_int_equal(yin_match_keyword(st->xml_ctx, "mandatory", strlen("mandatory"), prefix, prefix_len), YANG_MANDATORY);
257 assert_int_equal(yin_match_keyword(st->xml_ctx, "max-elements", strlen("max-elements"), prefix, prefix_len), YANG_MAX_ELEMENTS);
258 assert_int_equal(yin_match_keyword(st->xml_ctx, "min-elements", strlen("min-elements"), prefix, prefix_len), YANG_MIN_ELEMENTS);
259 assert_int_equal(yin_match_keyword(st->xml_ctx, "modifier", strlen("modifier"), prefix, prefix_len), YANG_MODIFIER);
260 assert_int_equal(yin_match_keyword(st->xml_ctx, "module", strlen("module"), prefix, prefix_len), YANG_MODULE);
261 assert_int_equal(yin_match_keyword(st->xml_ctx, "must", strlen("must"), prefix, prefix_len), YANG_MUST);
262 assert_int_equal(yin_match_keyword(st->xml_ctx, "namespace", strlen("namespace"), prefix, prefix_len), YANG_NAMESPACE);
263 assert_int_equal(yin_match_keyword(st->xml_ctx, "notification", strlen("notification"), prefix, prefix_len), YANG_NOTIFICATION);
264 assert_int_equal(yin_match_keyword(st->xml_ctx, "ordered-by", strlen("ordered-by"), prefix, prefix_len), YANG_ORDERED_BY);
265 assert_int_equal(yin_match_keyword(st->xml_ctx, "organization", strlen("organization"), prefix, prefix_len), YANG_ORGANIZATION);
266 assert_int_equal(yin_match_keyword(st->xml_ctx, "output", strlen("output"), prefix, prefix_len), YANG_OUTPUT);
267 assert_int_equal(yin_match_keyword(st->xml_ctx, "path", strlen("path"), prefix, prefix_len), YANG_PATH);
268 assert_int_equal(yin_match_keyword(st->xml_ctx, "pattern", strlen("pattern"), prefix, prefix_len), YANG_PATTERN);
269 assert_int_equal(yin_match_keyword(st->xml_ctx, "position", strlen("position"), prefix, prefix_len), YANG_POSITION);
270 assert_int_equal(yin_match_keyword(st->xml_ctx, "prefix", strlen("prefix"), prefix, prefix_len), YANG_PREFIX);
271 assert_int_equal(yin_match_keyword(st->xml_ctx, "presence", strlen("presence"), prefix, prefix_len), YANG_PRESENCE);
272 assert_int_equal(yin_match_keyword(st->xml_ctx, "range", strlen("range"), prefix, prefix_len), YANG_RANGE);
273 assert_int_equal(yin_match_keyword(st->xml_ctx, "reference", strlen("reference"), prefix, prefix_len), YANG_REFERENCE);
274 assert_int_equal(yin_match_keyword(st->xml_ctx, "refine", strlen("refine"), prefix, prefix_len), YANG_REFINE);
275 assert_int_equal(yin_match_keyword(st->xml_ctx, "require-instance", strlen("require-instance"), prefix, prefix_len), YANG_REQUIRE_INSTANCE);
276 assert_int_equal(yin_match_keyword(st->xml_ctx, "revision", strlen("revision"), prefix, prefix_len), YANG_REVISION);
277 assert_int_equal(yin_match_keyword(st->xml_ctx, "revision-date", strlen("revision-date"), prefix, prefix_len), YANG_REVISION_DATE);
278 assert_int_equal(yin_match_keyword(st->xml_ctx, "rpc", strlen("rpc"), prefix, prefix_len), YANG_RPC);
279 assert_int_equal(yin_match_keyword(st->xml_ctx, "status", strlen("status"), prefix, prefix_len), YANG_STATUS);
280 assert_int_equal(yin_match_keyword(st->xml_ctx, "submodule", strlen("submodule"), prefix, prefix_len), YANG_SUBMODULE);
281 assert_int_equal(yin_match_keyword(st->xml_ctx, "type", strlen("type"), prefix, prefix_len), YANG_TYPE);
282 assert_int_equal(yin_match_keyword(st->xml_ctx, "typedef", strlen("typedef"), prefix, prefix_len), YANG_TYPEDEF);
283 assert_int_equal(yin_match_keyword(st->xml_ctx, "unique", strlen("unique"), prefix, prefix_len), YANG_UNIQUE);
284 assert_int_equal(yin_match_keyword(st->xml_ctx, "units", strlen("units"), prefix, prefix_len), YANG_UNITS);
285 assert_int_equal(yin_match_keyword(st->xml_ctx, "uses", strlen("uses"), prefix, prefix_len), YANG_USES);
286 assert_int_equal(yin_match_keyword(st->xml_ctx, "value", strlen("value"), prefix, prefix_len), YANG_VALUE);
287 assert_int_equal(yin_match_keyword(st->xml_ctx, "when", strlen("when"), prefix, prefix_len), YANG_WHEN);
288 assert_int_equal(yin_match_keyword(st->xml_ctx, "yang-version", strlen("yang-version"), prefix, prefix_len), YANG_YANG_VERSION);
289 assert_int_equal(yin_match_keyword(st->xml_ctx, "yin-element", strlen("yin-element"), prefix, prefix_len), YANG_YIN_ELEMENT);
290
291 st->finished_correctly = true;
David Sedlák872c7b42018-10-26 13:15:20 +0200292}
David Sedlák3b4db242018-10-19 16:11:01 +0200293
David Sedlák872c7b42018-10-26 13:15:20 +0200294static void
David Sedlák060b00e2019-06-19 11:12:06 +0200295test_yin_match_argument_name(void **state)
David Sedlák872c7b42018-10-26 13:15:20 +0200296{
David Sedlák68a1af12019-03-08 13:46:54 +0100297 (void)state; /* unused */
David Sedlák872c7b42018-10-26 13:15:20 +0200298
David Sedlák060b00e2019-06-19 11:12:06 +0200299 assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN);
300 assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN);
301 assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN);
302 assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION);
303 assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE);
304 assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE);
305 assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME);
306 assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG);
307 assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
308 assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT);
309 assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI);
310 assert_int_equal(yin_match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200311}
312
David Sedlák68a1af12019-03-08 13:46:54 +0100313static void
314test_meta(void **state)
315{
316 LY_ERR ret = LY_SUCCESS;
317 struct state *st = *state;
318
David Sedlák2b214ac2019-06-06 16:11:03 +0200319 ret = yin_parse_module(st->ctx,"<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\
320 name=\"example-foo\">\
David Sedlák21f87cd2019-07-03 16:53:23 +0200321 <prefix value=\"foo\">ignored</prefix>\
322 <namespace uri=\"urn:example:foo\" xmlns:myext=\"urn:example:extensions\"/>\
David Sedlák15a92662019-06-18 11:55:15 +0200323 <organization xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"><text>organization...</text></organization>\
324 <contact><text>contact...</text></contact>\
325 <description><text>description...</text></description>\
326 <reference><text>reference...</text></reference>\
David Sedlák68a1af12019-03-08 13:46:54 +0100327 </module>", st->mod);
328
329 assert_int_equal(ret, LY_SUCCESS);
David Sedláka7406952019-04-05 10:33:07 +0200330 assert_string_equal(st->mod->parsed->mod->org, "organization...");
David Sedlák68a1af12019-03-08 13:46:54 +0100331 assert_string_equal(st->mod->parsed->mod->contact, "contact...");
332 assert_string_equal(st->mod->parsed->mod->dsc, "description...");
333 assert_string_equal(st->mod->parsed->mod->ref, "reference...");
David Sedlák68826732019-06-05 10:50:58 +0200334
335 st = reset_state(state);
336 ret = yin_parse_module(st->ctx,"<module name=\"example-foo\">\
337 <organization test=\"invalid-argument\">organization...</organization>\
338 </module>", st->mod);
339 assert_int_equal(ret, LY_EVALID);
David Sedlák79e50cb2019-06-05 16:33:09 +0200340
341 st->finished_correctly = true;
David Sedlák68a1af12019-03-08 13:46:54 +0100342}
343
David Sedlák8f5bce02019-06-03 16:41:08 +0200344static void
David Sedlákda63c082019-06-04 13:52:23 +0200345test_yin_parse_import(void **state)
346{
347 struct state *st = *state;
348 const char *prefix = NULL, *name = NULL;
349 size_t prefix_len = 0, name_len = 0;
350 LY_ERR ret = LY_SUCCESS;
David Sedlák8f7a1172019-06-20 14:42:18 +0200351 struct yin_arg_record *args = NULL;
David Sedlákda63c082019-06-04 13:52:23 +0200352
David Sedlák2b214ac2019-06-06 16:11:03 +0200353 const char *data = "<import xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" module=\"a\">\
David Sedlákda63c082019-06-04 13:52:23 +0200354 <prefix value=\"a_mod\"/>\
David Sedlák00250342019-06-21 14:19:39 +0200355 <revision-date date=\"2015-01-01\"></revision-date>\
David Sedlákb6e65972019-06-19 10:44:13 +0200356 <description><text>import description</text></description>\
357 <reference><text>import reference</text></reference>\
David Sedlákc67dcaa2019-06-04 14:49:05 +0200358 </import>\
359 \
David Sedlák2b214ac2019-06-06 16:11:03 +0200360 <import xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" module=\"a\">\
David Sedlákc67dcaa2019-06-04 14:49:05 +0200361 <prefix value=\"a_mod\"/>\
David Sedlák00250342019-06-21 14:19:39 +0200362 <revision-date date=\"2015-01-01\" />\
David Sedlákda63c082019-06-04 13:52:23 +0200363 </import>";
David Sedlákb6e65972019-06-19 10:44:13 +0200364 /* first import */
David Sedlákda63c082019-06-04 13:52:23 +0200365 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák8f7a1172019-06-20 14:42:18 +0200366 yin_load_attributes(st->xml_ctx, &data, &args);
David Sedlák619db942019-07-03 14:47:30 +0200367 st->lysp_mod->mod->prefix = "b-mod";
368 ret = yin_parse_import(st->xml_ctx, &args, &data, st->lysp_mod);
David Sedlákda63c082019-06-04 13:52:23 +0200369 assert_int_equal(ret, LY_SUCCESS);
David Sedlák619db942019-07-03 14:47:30 +0200370 assert_string_equal(st->lysp_mod->imports->name, "a");
371 assert_string_equal(st->lysp_mod->imports->prefix, "a_mod");
372 assert_string_equal(st->lysp_mod->imports->rev, "2015-01-01");
373 assert_string_equal(st->lysp_mod->imports->dsc, "import description");
374 assert_string_equal(st->lysp_mod->imports->ref, "import reference");
David Sedlák8f7a1172019-06-20 14:42:18 +0200375 LY_ARRAY_FREE(args);
David Sedlák8f7a1172019-06-20 14:42:18 +0200376 args = NULL;
David Sedlák619db942019-07-03 14:47:30 +0200377 st = reset_state(state);
David Sedlákc67dcaa2019-06-04 14:49:05 +0200378
David Sedlákb6e65972019-06-19 10:44:13 +0200379 /* second invalid import */
David Sedlákda63c082019-06-04 13:52:23 +0200380 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák8f7a1172019-06-20 14:42:18 +0200381 yin_load_attributes(st->xml_ctx, &data, &args);
David Sedlák619db942019-07-03 14:47:30 +0200382 st->lysp_mod->mod->prefix = "a_mod";
383 ret = yin_parse_import(st->xml_ctx, &args, &data, st->lysp_mod);
David Sedlákda63c082019-06-04 13:52:23 +0200384 assert_int_equal(ret, LY_EVALID);
David Sedlák79e50cb2019-06-05 16:33:09 +0200385 logbuf_assert("Prefix \"a_mod\" already used as module prefix. Line number 1.");
David Sedlák8f7a1172019-06-20 14:42:18 +0200386 LY_ARRAY_FREE(args);
David Sedlák8f7a1172019-06-20 14:42:18 +0200387 args = NULL;
388
David Sedlákb6e65972019-06-19 10:44:13 +0200389 st = reset_state(state);
390 /* import with unknown child element */
391 data = "<import xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" module=\"a\">\
392 <what value=\"a_mod\"/>\
393 </import>";
394 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák8f7a1172019-06-20 14:42:18 +0200395 yin_load_attributes(st->xml_ctx, &data, &args);
David Sedlák619db942019-07-03 14:47:30 +0200396 st->lysp_mod->mod->prefix = "invalid_mod";
397 ret = yin_parse_import(st->xml_ctx, &args, &data, st->lysp_mod);
David Sedlákb6e65972019-06-19 10:44:13 +0200398 assert_int_equal(ret, LY_EVALID);
David Sedlák3ffbc522019-07-02 17:49:28 +0200399 logbuf_assert("Unexpected child element \"what\" of import element. Line number 1.");
David Sedlák8f7a1172019-06-20 14:42:18 +0200400 LY_ARRAY_FREE(args);
David Sedlákb6e65972019-06-19 10:44:13 +0200401
402 st->finished_correctly = true;
403}
404
405static void
406test_yin_parse_status(void **state)
407{
408 struct state *st = *state;
409 const char *prefix = NULL, *name = NULL;
410 size_t prefix_len = 0, name_len = 0;
411 LY_ERR ret = LY_SUCCESS;
412 uint16_t flags = 0;
413 struct lysp_ext_instance *exts;
David Sedlák8f7a1172019-06-20 14:42:18 +0200414 struct yin_arg_record *args = NULL;
David Sedlákb6e65972019-06-19 10:44:13 +0200415
416 /* try all valid values */
417 const char *data = "<status value=\"current\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>";
418 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák8f7a1172019-06-20 14:42:18 +0200419 yin_load_attributes(st->xml_ctx, &data, &args);
420 ret = yin_parse_status(st->xml_ctx, &args, &data, &flags, &exts);
David Sedlák4af339b2019-06-24 15:37:29 +0200421 assert_int_equal(st->xml_ctx->status, LYXML_END);
David Sedlákb6e65972019-06-19 10:44:13 +0200422 assert_int_equal(ret, LY_SUCCESS);
423 assert_true(flags & LYS_STATUS_CURR);
David Sedlák8f7a1172019-06-20 14:42:18 +0200424 LY_ARRAY_FREE(args);
425 args = NULL;
David Sedlákb6e65972019-06-19 10:44:13 +0200426
427 st = reset_state(state);
428 flags = 0;
429 data = "<status value=\"deprecated\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>";
430 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák8f7a1172019-06-20 14:42:18 +0200431 yin_load_attributes(st->xml_ctx, &data, &args);
432 ret = yin_parse_status(st->xml_ctx, &args, &data, &flags, &exts);
David Sedlák4af339b2019-06-24 15:37:29 +0200433 assert_int_equal(st->xml_ctx->status, LYXML_END);
David Sedlákb6e65972019-06-19 10:44:13 +0200434 assert_int_equal(ret, LY_SUCCESS);
435 assert_true(flags & LYS_STATUS_DEPRC);
David Sedlák8f7a1172019-06-20 14:42:18 +0200436 LY_ARRAY_FREE(args);
437 args = NULL;
David Sedlákb6e65972019-06-19 10:44:13 +0200438
439 st = reset_state(state);
440 flags = 0;
441 data = "<status value=\"obsolete\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>";
442 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák8f7a1172019-06-20 14:42:18 +0200443 yin_load_attributes(st->xml_ctx, &data, &args);
444 ret = yin_parse_status(st->xml_ctx, &args, &data, &flags, &exts);
David Sedlák4af339b2019-06-24 15:37:29 +0200445 assert_int_equal(st->xml_ctx->status, LYXML_END);
David Sedlákb6e65972019-06-19 10:44:13 +0200446 assert_int_equal(ret, LY_SUCCESS);
447 assert_true(flags & LYS_STATUS_OBSLT);
David Sedlák8f7a1172019-06-20 14:42:18 +0200448 LY_ARRAY_FREE(args);
449 args = NULL;
David Sedlákb6e65972019-06-19 10:44:13 +0200450
451 /* duplicit definition (no reset_state() call) */
452 data = "<status value=\"deprecated\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>";
453 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák8f7a1172019-06-20 14:42:18 +0200454 yin_load_attributes(st->xml_ctx, &data, &args);
455 ret = yin_parse_status(st->xml_ctx, &args, &data, &flags, &exts);
David Sedlákb6e65972019-06-19 10:44:13 +0200456 assert_int_equal(ret, LY_EVALID);
457 logbuf_assert("Duplicate element \"status\". Line number 1.");
David Sedlák8f7a1172019-06-20 14:42:18 +0200458 LY_ARRAY_FREE(args);
459 args = NULL;
David Sedlákb6e65972019-06-19 10:44:13 +0200460
461 /* invalid status value */
462 st = reset_state(state);
463 flags = 0;
464 data = "<status value=\"dunno\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>";
465 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák8f7a1172019-06-20 14:42:18 +0200466 yin_load_attributes(st->xml_ctx, &data, &args);
467 ret = yin_parse_status(st->xml_ctx, &args, &data, &flags, &exts);
David Sedlákb6e65972019-06-19 10:44:13 +0200468 assert_int_equal(ret, LY_EVALID);
469 logbuf_assert("Invalid value \"dunno\" of \"status\". Line number 1.");
David Sedlák8f7a1172019-06-20 14:42:18 +0200470 LY_ARRAY_FREE(args);
471
David Sedlák79e50cb2019-06-05 16:33:09 +0200472 st->finished_correctly = true;
David Sedlákda63c082019-06-04 13:52:23 +0200473}
474
David Sedlák554e36d2019-06-20 16:00:04 +0200475static void
476test_yin_parse_extension(void **state)
477{
478 struct state *st = *state;
479 const char *prefix = NULL, *name = NULL;
480 size_t prefix_len = 0, name_len = 0;
481 LY_ERR ret = LY_SUCCESS;
482 struct yin_arg_record *args = NULL;
483 struct lysp_ext *exts = NULL, *iter = NULL;
484
485 const char *data = "<extension name=\"b\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\
David Sedlák9494eb22019-06-21 16:06:53 +0200486 <argument name=\"argname\"></argument>\
David Sedlák554e36d2019-06-20 16:00:04 +0200487 <description><text>desc</text></description>\
488 <reference><text>ref</text></reference>\
489 <status value=\"deprecated\"></status>\
490 </extension>";
491 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
492 yin_load_attributes(st->xml_ctx, &data, &args);
493 ret = yin_parse_extension(st->xml_ctx, &args, &data, &exts);
David Sedlák4af339b2019-06-24 15:37:29 +0200494 assert_int_equal(st->xml_ctx->status, LYXML_END);
David Sedlák554e36d2019-06-20 16:00:04 +0200495 assert_int_equal(ret, LY_SUCCESS);
496 LY_ARRAY_FOR_ITER(exts, struct lysp_ext, iter) {
497 assert_string_equal(iter->name, "b");
498 assert_string_equal(iter->dsc, "desc");
499 assert_string_equal(iter->ref, "ref");
David Sedlák9494eb22019-06-21 16:06:53 +0200500 assert_string_equal(iter->argument, "argname");
David Sedlák554e36d2019-06-20 16:00:04 +0200501 assert_true(iter->flags & LYS_STATUS_DEPRC);
502 }
David Sedlák288c1472019-06-20 16:09:48 +0200503 lydict_remove(st->ctx, "b");
504 lydict_remove(st->ctx, "desc");
505 lydict_remove(st->ctx, "ref");
David Sedlák9494eb22019-06-21 16:06:53 +0200506 lydict_remove(st->ctx, "argname");
David Sedlák554e36d2019-06-20 16:00:04 +0200507 LY_ARRAY_FREE(args);
David Sedlák288c1472019-06-20 16:09:48 +0200508 LY_ARRAY_FREE(exts);
David Sedlák554e36d2019-06-20 16:00:04 +0200509 st->finished_correctly = true;
510}
511
David Sedlák2721d3d2019-06-21 15:37:41 +0200512static void
513test_yin_parse_yin_element_element(void **state)
514{
515 struct state *st = *state;
516 const char *prefix = NULL, *name = NULL;
517 size_t prefix_len = 0, name_len = 0;
518 LY_ERR ret = LY_SUCCESS;
519 uint16_t flags = 0;
David Sedlák3ffbc522019-07-02 17:49:28 +0200520 struct lysp_ext_instance *exts;
David Sedlák2721d3d2019-06-21 15:37:41 +0200521 struct yin_arg_record *args = NULL;
522
523 /* try all valid values */
524 const char *data = "<yin-element value=\"true\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>";
525 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
526 yin_load_attributes(st->xml_ctx, &data, &args);
David Sedlák3ffbc522019-07-02 17:49:28 +0200527 ret = yin_parse_yin_element_element(st->xml_ctx, args, &data, &flags, &exts);
David Sedlák4af339b2019-06-24 15:37:29 +0200528 assert_int_equal(st->xml_ctx->status, LYXML_END);
David Sedlák2721d3d2019-06-21 15:37:41 +0200529 assert_int_equal(ret, LY_SUCCESS);
530 assert_true(flags & LYS_YINELEM_TRUE);
531 LY_ARRAY_FREE(args);
532 args = NULL;
533
534 st = reset_state(state);
535 flags = 0;
536 data = "<yin-element value=\"false\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>";
537 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
538 yin_load_attributes(st->xml_ctx, &data, &args);
David Sedlák3ffbc522019-07-02 17:49:28 +0200539 ret = yin_parse_yin_element_element(st->xml_ctx, args, &data, &flags, &exts);
David Sedlák4af339b2019-06-24 15:37:29 +0200540 assert_int_equal(st->xml_ctx->status, LYXML_END);
David Sedlák2721d3d2019-06-21 15:37:41 +0200541 assert_int_equal(ret, LY_SUCCESS);
542 assert_true(flags & LYS_YINELEM_FALSE);
543 LY_ARRAY_FREE(args);
544 args = NULL;
545
546 /* invalid value */
547 st = reset_state(state);
548 flags = 0;
549 data = "<yin-element value=\"invalid\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>";
550 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
551 yin_load_attributes(st->xml_ctx, &data, &args);
David Sedlák3ffbc522019-07-02 17:49:28 +0200552 ret = yin_parse_yin_element_element(st->xml_ctx, args, &data, &flags, &exts);
David Sedlák2721d3d2019-06-21 15:37:41 +0200553 assert_int_equal(ret, LY_EVALID);
554 LY_ARRAY_FREE(args);
555 args = NULL;
556
557 st->finished_correctly = true;
558}
559
David Sedlákb1a78352019-06-28 16:16:29 +0200560static void
561test_yin_parse_element_generic(void **state)
562{
563 const char *prefix, *name;
564 struct state *st = *state;
565 struct lysp_ext_instance exts;
566 size_t prefix_len, name_len;
567 LY_ERR ret;
568
569 memset(&exts, 0, sizeof(exts));
570
571 const char *data = "<elem attr=\"value\">text_value</elem>";
572 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlákf250ecf2019-07-01 11:02:05 +0200573 ret = yin_parse_element_generic(st->xml_ctx, name, name_len, prefix, prefix_len, &data, &exts.child);
David Sedlákb1a78352019-06-28 16:16:29 +0200574 assert_int_equal(ret, LY_SUCCESS);
575 assert_string_equal(exts.child->stmt, "elem");
576 assert_string_equal(exts.child->arg, "text_value");
David Sedlákb1a78352019-06-28 16:16:29 +0200577 assert_string_equal(exts.child->child->stmt, "attr");
578 assert_string_equal(exts.child->child->arg, "value");
579 assert_true(exts.child->child->flags & LYS_YIN_ATTR);
David Sedlákb1a78352019-06-28 16:16:29 +0200580 lysp_ext_instance_free(st->ctx, &exts);
David Sedlák5392a212019-07-01 09:19:10 +0200581 st = reset_state(state);
582
583 data = "<elem></elem>";
584 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlákf250ecf2019-07-01 11:02:05 +0200585 ret = yin_parse_element_generic(st->xml_ctx, name, name_len, prefix, prefix_len, &data, &exts.child);
David Sedlák5392a212019-07-01 09:19:10 +0200586 assert_int_equal(ret, LY_SUCCESS);
587 assert_string_equal(exts.child->stmt, "elem");
588 assert_null(exts.child->child);
589 assert_null(exts.child->arg);
David Sedlákf250ecf2019-07-01 11:02:05 +0200590 assert_int_equal(st->xml_ctx->status, LYXML_END);
David Sedlák5392a212019-07-01 09:19:10 +0200591 lysp_ext_instance_free(st->ctx, &exts);
592
David Sedlákb1a78352019-06-28 16:16:29 +0200593 st->finished_correctly = true;
594}
595
596static void
597test_yin_parse_extension_instance(void **state)
598{
599 LY_ERR ret;
600 struct state *st = *state;
601 const char *prefix, *name;
602 size_t prefix_len, name_len;
603 struct yin_arg_record *args = NULL;
604 struct lysp_ext_instance *exts = NULL;
David Sedlákb1a78352019-06-28 16:16:29 +0200605 const char *data = "<ext value1=\"test\" value=\"test2\"><subelem>text</subelem></ext>";
606 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
607 yin_load_attributes(st->xml_ctx, &data, &args);
David Sedlákd6e56892019-07-01 15:40:24 +0200608 ret = yin_parse_extension_instance(st->xml_ctx, &args, &data, name2fullname(name, prefix_len),
David Sedlák619db942019-07-03 14:47:30 +0200609 namelen2fulllen(name_len, prefix_len), LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200610 assert_int_equal(ret, LY_SUCCESS);
611 assert_string_equal(exts->name, "ext");
612 assert_int_equal(exts->insubstmt_index, 0);
613 assert_true(exts->insubstmt == LYEXT_SUBSTMT_CONTACT);
614 assert_true(exts->yin & LYS_YIN);
615 assert_string_equal(exts->child->stmt, "value1");
616 assert_string_equal(exts->child->arg, "test");
617 assert_null(exts->child->child);
618 assert_true(exts->child->flags & LYS_YIN_ATTR);
619 assert_string_equal(exts->child->next->stmt, "value");
620 assert_string_equal(exts->child->next->arg, "test2");
621 assert_null(exts->child->next->child);
622 assert_true(exts->child->next->flags & LYS_YIN_ATTR);
623
624 assert_string_equal(exts->child->next->next->stmt, "subelem");
625 assert_string_equal(exts->child->next->next->arg, "text");
626 assert_null(exts->child->next->next->child);
627 assert_null(exts->child->next->next->next);
628 assert_false(exts->child->next->next->flags & LYS_YIN_ATTR);
629 assert_int_equal(st->xml_ctx->status, LYXML_END);
630 LY_ARRAY_FREE(args);
631 lysp_ext_instance_free(st->ctx, exts);
632 LY_ARRAY_FREE(exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200633 exts = NULL;
634 args = NULL;
635 st = reset_state(state);
636
637 data = "<extension-elem />";
638 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
639 yin_load_attributes(st->xml_ctx, &data, &args);
David Sedlák619db942019-07-03 14:47:30 +0200640 ret = yin_parse_extension_instance(st->xml_ctx, &args, &data, name, name_len, LYEXT_SUBSTMT_CONTACT, 0, &exts);
David Sedlákf250ecf2019-07-01 11:02:05 +0200641 assert_int_equal(ret, LY_SUCCESS);
642 assert_string_equal(exts->name, "extension-elem");
643 assert_null(exts->argument);
644 assert_null(exts->child);
645 assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT);
646 assert_int_equal(exts->insubstmt_index, 0);
647 assert_true(exts->yin & LYS_YIN);
648 assert_int_equal(st->xml_ctx->status, LYXML_END);
649 LY_ARRAY_FREE(args);
650 lysp_ext_instance_free(st->ctx, exts);
651 LY_ARRAY_FREE(exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200652 st->finished_correctly = true;
653}
654
David Sedlák555c7202019-07-04 12:14:12 +0200655static void
656test_yin_parse_content(void **state)
657{
658 struct state *st = *state;
659 LY_ERR ret = LY_SUCCESS;
660 struct sized_string name, prefix;
661 const char *data = "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
662 "<custom xmlns=\"my-ext\">"
663 "totally amazing extension"
664 "</custom>"
665 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
666 "</prefix>";
667 struct lysp_ext_instance *exts = NULL;
668 struct yin_arg_record *attrs = NULL;
669 const char *value;
670
671 lyxml_get_element(st->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
672 yin_load_attributes(st->xml_ctx, &data, &attrs);
673
674 struct yin_subelement subelems[2] = {{YANG_CUSTOM, NULL, 0},
675 {YIN_TEXT, &value, 0}};
676 ret = yin_parse_content(st->xml_ctx, subelems, 2, &data, YANG_ACTION, NULL, &exts);
677 assert_int_equal(ret, LY_SUCCESS);
678 assert_string_equal(exts->name, "custom");
679 assert_string_equal(exts->argument, "totally amazing extension");
680 assert_string_equal(value, "wsefsdf");
681 lysp_ext_instance_free(st->ctx, exts);
682 LY_ARRAY_FREE(exts);
683 LY_ARRAY_FREE(attrs);
684 attrs = NULL;
685 lydict_remove(st->ctx, value);
686 st = reset_state(state);
687
688 /* test unique subelem */
689 const char *prefix_value;
690 struct yin_subelement subelems2[2] = {{YANG_PREFIX, &prefix_value, 0},
691 {YIN_TEXT, &value, YIN_SUBELEM_UNIQUE}};
692 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
693 "<prefix value=\"inv_mod\" />"
694 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
695 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
696 "</module>";
697 lyxml_get_element(st->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
698 yin_load_attributes(st->xml_ctx, &data, &attrs);
699 ret = yin_parse_content(st->xml_ctx, subelems2, 2, &data, YANG_MODULE, NULL, &exts);
700 assert_int_equal(ret, LY_EVALID);
701 logbuf_assert("Redefinition of text element in module element. Line number 1.");
702 lydict_remove(st->ctx, prefix_value);
703 lydict_remove(st->ctx, value);
704 st = reset_state(state);
705 LY_ARRAY_FREE(attrs);
706 attrs = NULL;
707
708 /* test first subelem */
709 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
710 "<prefix value=\"inv_mod\" />"
711 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
712 "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>"
713 "</module>";
714 struct yin_subelement subelems3[2] = {{YANG_PREFIX, &prefix_value, 0},
715 {YIN_TEXT, &value, YIN_SUBELEM_FIRST}};
716 lyxml_get_element(st->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
717 yin_load_attributes(st->xml_ctx, &data, &attrs);
718 ret = yin_parse_content(st->xml_ctx, subelems3, 2, &data, YANG_MODULE, NULL, &exts);
719 assert_int_equal(ret, LY_EVALID);
720 logbuf_assert("Subelement text of module element must be defined as first subelement. Line number 1.");
721 lydict_remove(st->ctx, prefix_value);
722 st = reset_state(state);
723 LY_ARRAY_FREE(attrs);
724 attrs = NULL;
725
726 /* test mandatory subelem */
727 data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">"
728 "</module>";
729 struct yin_subelement subelems4[1] = {{YANG_PREFIX, &prefix_value, YIN_SUBELEM_MANDATORY}};
730 lyxml_get_element(st->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
731 yin_load_attributes(st->xml_ctx, &data, &attrs);
732 ret = yin_parse_content(st->xml_ctx, subelems4, 1, &data, YANG_MODULE, NULL, &exts);
733 assert_int_equal(ret, LY_EVALID);
734 logbuf_assert("Missing mandatory subelement prefix of module element. Line number 1.");
735 LY_ARRAY_FREE(attrs);
736
737 st->finished_correctly = true;
738}
739
David Sedlák3b4db242018-10-19 16:11:01 +0200740int
741main(void)
742{
743
744 const struct CMUnitTest tests[] = {
David Sedlák392af4f2019-06-04 16:02:42 +0200745 cmocka_unit_test_setup_teardown(test_yin_parse_module, setup_f, teardown_f),
David Sedlák68a1af12019-03-08 13:46:54 +0100746 cmocka_unit_test_setup_teardown(test_meta, setup_f, teardown_f),
David Sedlákda63c082019-06-04 13:52:23 +0200747 cmocka_unit_test_setup_teardown(test_yin_parse_import, setup_f, teardown_f),
David Sedlákb6e65972019-06-19 10:44:13 +0200748 cmocka_unit_test_setup_teardown(test_yin_parse_status, setup_f, teardown_f),
David Sedlák8f7a1172019-06-20 14:42:18 +0200749 cmocka_unit_test_setup_teardown(test_yin_match_keyword, setup_f, teardown_f),
David Sedlák554e36d2019-06-20 16:00:04 +0200750 cmocka_unit_test_setup_teardown(test_yin_parse_extension, setup_f, teardown_f),
David Sedlák2721d3d2019-06-21 15:37:41 +0200751 cmocka_unit_test_setup_teardown(test_yin_parse_yin_element_element, setup_f, teardown_f),
David Sedlákb1a78352019-06-28 16:16:29 +0200752 cmocka_unit_test_setup_teardown(test_yin_parse_element_generic, setup_f, teardown_f),
753 cmocka_unit_test_setup_teardown(test_yin_parse_extension_instance, setup_f, teardown_f),
David Sedlák555c7202019-07-04 12:14:12 +0200754 cmocka_unit_test_setup_teardown(test_yin_parse_content, setup_f, teardown_f),
David Sedlák060b00e2019-06-19 11:12:06 +0200755 cmocka_unit_test(test_yin_match_argument_name),
David Sedlák3b4db242018-10-19 16:11:01 +0200756 };
757
758 return cmocka_run_group_tests(tests, NULL, NULL);
759}