blob: 9c56aa4be9c69007e34a4f64da7546cd3b2b049d [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ák68a1af12019-03-08 13:46:54 +010030struct state {
David Sedlák3b4db242018-10-19 16:11:01 +020031 struct ly_ctx *ctx;
David Sedlák3017da42019-02-15 09:48:04 +010032 struct lys_module *mod;
David Sedlák8f5bce02019-06-03 16:41:08 +020033 struct lyxml_context *xml_ctx;
David Sedlák79e50cb2019-06-05 16:33:09 +020034 bool finished_correctly;
David Sedlák68a1af12019-03-08 13:46:54 +010035};
David Sedlák872c7b42018-10-26 13:15:20 +020036
David Sedlák79e50cb2019-06-05 16:33:09 +020037#define BUFSIZE 1024
38char logbuf[BUFSIZE] = {0};
39int store = -1; /* negative for infinite logging, positive for limited logging */
40
41/* set to 0 to printing error messages to stderr instead of checking them in code */
42#define ENABLE_LOGGER_CHECKING 1
43
44#if ENABLE_LOGGER_CHECKING
45static void
46logger(LY_LOG_LEVEL level, const char *msg, const char *path)
47{
48 (void) level; /* unused */
49 if (store) {
50 if (path && path[0]) {
51 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
52 } else {
53 strncpy(logbuf, msg, BUFSIZE - 1);
54 }
55 if (store > 0) {
56 --store;
57 }
58 }
59}
60#endif
61
62#if ENABLE_LOGGER_CHECKING
63# define logbuf_assert(str) assert_string_equal(logbuf, str)
64#else
65# define logbuf_assert(str)
66#endif
67
68#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
69 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
70 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
71 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
72 CLEANUP
73
74
David Sedlák68a1af12019-03-08 13:46:54 +010075static int
76setup_f(void **state)
77{
78 struct state *st = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +020079
David Sedlák79e50cb2019-06-05 16:33:09 +020080#if ENABLE_LOGGER_CHECKING
81 /* setup logger */
82 ly_set_log_clb(logger, 1);
83#endif
84
David Sedlák68a1af12019-03-08 13:46:54 +010085 /* allocate state variable */
86 (*state) = st = calloc(1, sizeof(*st));
87 if (!st) {
88 fprintf(stderr, "Memmory allocation failed");
89 return EXIT_FAILURE;
90 }
David Sedlák872c7b42018-10-26 13:15:20 +020091
David Sedlák68a1af12019-03-08 13:46:54 +010092 /* create new libyang context */
93 ly_ctx_new(NULL, 0, &st->ctx);
David Sedlák872c7b42018-10-26 13:15:20 +020094
David Sedlák68a1af12019-03-08 13:46:54 +010095 /* allocate new module */
96 st->mod = calloc(1, sizeof(*st->mod));
97 st->mod->ctx = st->ctx;
98
David Sedlák8f5bce02019-06-03 16:41:08 +020099 st->xml_ctx = calloc(1, sizeof(struct lys_parser_ctx));
100 st->xml_ctx->ctx = st->ctx;
101 st->xml_ctx->line = 1;
102
David Sedlák68a1af12019-03-08 13:46:54 +0100103 return EXIT_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +0200104}
105
106static int
David Sedlák68a1af12019-03-08 13:46:54 +0100107teardown_f(void **state)
108{
109 struct state *st = *(struct state **)state;
110
David Sedlák79e50cb2019-06-05 16:33:09 +0200111#if ENABLE_LOGGER_CHECKING
112 /* teardown logger */
113 if (!st->finished_correctly && logbuf[0] != '\0') {
114 fprintf(stderr, "%s\n", logbuf);
115 }
116#endif
117
David Sedlák8f5bce02019-06-03 16:41:08 +0200118 lyxml_context_clear(st->xml_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100119 lys_module_free(st->mod, NULL);
120 ly_ctx_destroy(st->ctx, NULL);
David Sedlák8f5bce02019-06-03 16:41:08 +0200121 free(st->xml_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +0100122 free(st);
123
124 return EXIT_SUCCESS;
125}
126
David Sedlák392af4f2019-06-04 16:02:42 +0200127static struct state*
128reset_state(void **state)
129{
David Sedlák79e50cb2019-06-05 16:33:09 +0200130 ((struct state *)*state)->finished_correctly = true;
David Sedlák392af4f2019-06-04 16:02:42 +0200131 teardown_f(state);
132 setup_f(state);
133
134 return *state;
135}
136
David Sedlák79e50cb2019-06-05 16:33:09 +0200137void
138logbuf_clean(void)
139{
140 logbuf[0] = '\0';
141}
142
David Sedlák68a1af12019-03-08 13:46:54 +0100143static void
David Sedlák392af4f2019-06-04 16:02:42 +0200144test_yin_parse_module(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +0100145{
146 LY_ERR ret = LY_SUCCESS;
147 struct state *st = *state;
148
149 ret = yin_parse_module(st->ctx,
David Sedlák2b214ac2019-06-06 16:11:03 +0200150 "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\
151 name=\"example-foo\"\
David Sedlák18730132019-03-15 15:51:34 +0100152 xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\
153 xmlns:foo=\"urn:example:foo\"\
154 xmlns:myext=\"urn:example:extensions\">\
David Sedlákcd0c9512019-03-29 13:23:06 +0100155 <namespace uri=\"urn:example:foo\" xmlns:myext=\"urn:example:extensions\"/>\
David Sedláka7406952019-04-05 10:33:07 +0200156 <prefix xmlns:myxt=\"urn:emple:extensions\" value=\"foo\" xmlns:myext=\"urn:example:extensions\"/>\
David Sedlákd9d3a312019-06-04 09:47:10 +0200157 </module>",
David Sedlák68a1af12019-03-08 13:46:54 +0100158 st->mod);
159
160 assert_int_equal(ret, LY_SUCCESS);
161 assert_string_equal(st->mod->parsed->mod->name, "example-foo");
162 assert_string_equal(st->mod->parsed->mod->prefix, "foo");
David Sedlákcd0c9512019-03-29 13:23:06 +0100163 assert_string_equal(st->mod->parsed->mod->ns, "urn:example:foo");
David Sedlák392af4f2019-06-04 16:02:42 +0200164
165 st = reset_state(state);
166 ret = yin_parse_module(st->ctx,
David Sedlák2b214ac2019-06-06 16:11:03 +0200167 "<module name=\"example-foo\">\
168 <invalid-tag uri=\"urn:example:foo\"\"/>\
169 </module>",
170 st->mod);
David Sedlák392af4f2019-06-04 16:02:42 +0200171 assert_int_equal(ret, LY_EVALID);
172
173 st = reset_state(state);
174 ret = yin_parse_module(st->ctx,
David Sedlák2b214ac2019-06-06 16:11:03 +0200175 "<module>\
David Sedlák57715b12019-06-17 13:05:22 +0200176 </module>",
David Sedlák2b214ac2019-06-06 16:11:03 +0200177 st->mod);
David Sedlák392af4f2019-06-04 16:02:42 +0200178 assert_int_equal(ret, LY_EVALID);
David Sedlák79e50cb2019-06-05 16:33:09 +0200179 logbuf_assert("Missing argument name of a module Line number 1.");
David Sedlák392af4f2019-06-04 16:02:42 +0200180
181 st = reset_state(state);
182 ret = yin_parse_module(st->ctx,
183 "",
184 st->mod);
185 assert_int_equal(ret, LY_EVALID);
David Sedlák79e50cb2019-06-05 16:33:09 +0200186 logbuf_assert("Invalid keyword \"(null)\", expected \"module\" or \"submodule\". Line number 1.");
187 st->finished_correctly = true;
David Sedlák3b4db242018-10-19 16:11:01 +0200188}
189
190static void
David Sedlák1bccdfa2019-06-17 15:55:27 +0200191test_yin_match_keyword(void **state)
David Sedlák3b4db242018-10-19 16:11:01 +0200192{
193 (void)state; /* unused */
194
David Sedlákc8dd80e2019-06-19 16:08:48 +0200195 assert_int_equal(yin_match_keyword("anydatax", strlen("anydatax")), YANG_NONE);
196 assert_int_equal(yin_match_keyword("asdasd", strlen("asdasd")), YANG_NONE);
197 assert_int_equal(yin_match_keyword("", 0), YANG_NONE);
198 assert_int_equal(yin_match_keyword("anydata", strlen("anydata")), YANG_ANYDATA);
199 assert_int_equal(yin_match_keyword("anyxml", strlen("anyxml")), YANG_ANYXML);
200 assert_int_equal(yin_match_keyword("argument", strlen("argument")), YANG_ARGUMENT);
201 assert_int_equal(yin_match_keyword("augment", strlen("augment")), YANG_AUGMENT);
202 assert_int_equal(yin_match_keyword("base", strlen("base")), YANG_BASE);
203 assert_int_equal(yin_match_keyword("belongs-to", strlen("belongs-to")), YANG_BELONGS_TO);
204 assert_int_equal(yin_match_keyword("bit", strlen("bit")), YANG_BIT);
205 assert_int_equal(yin_match_keyword("case", strlen("case")), YANG_CASE);
206 assert_int_equal(yin_match_keyword("choice", strlen("choice")), YANG_CHOICE);
207 assert_int_equal(yin_match_keyword("config", strlen("config")), YANG_CONFIG);
208 assert_int_equal(yin_match_keyword("contact", strlen("contact")), YANG_CONTACT);
209 assert_int_equal(yin_match_keyword("container", strlen("container")), YANG_CONTAINER);
210 assert_int_equal(yin_match_keyword("default", strlen("default")), YANG_DEFAULT);
211 assert_int_equal(yin_match_keyword("description", strlen("description")), YANG_DESCRIPTION);
212 assert_int_equal(yin_match_keyword("deviate", strlen("deviate")), YANG_DEVIATE);
213 assert_int_equal(yin_match_keyword("deviation", strlen("deviation")), YANG_DEVIATION);
214 assert_int_equal(yin_match_keyword("enum", strlen("enum")), YANG_ENUM);
215 assert_int_equal(yin_match_keyword("error-app-tag", strlen("error-app-tag")), YANG_ERROR_APP_TAG);
216 assert_int_equal(yin_match_keyword("error-message", strlen("error-message")), YANG_ERROR_MESSAGE);
217 assert_int_equal(yin_match_keyword("extension", strlen("extension")), YANG_EXTENSION);
218 assert_int_equal(yin_match_keyword("feature", strlen("feature")), YANG_FEATURE);
219 assert_int_equal(yin_match_keyword("fraction-digits", strlen("fraction-digits")), YANG_FRACTION_DIGITS);
220 assert_int_equal(yin_match_keyword("grouping", strlen("grouping")), YANG_GROUPING);
221 assert_int_equal(yin_match_keyword("identity", strlen("identity")), YANG_IDENTITY);
222 assert_int_equal(yin_match_keyword("if-feature", strlen("if-feature")), YANG_IF_FEATURE);
223 assert_int_equal(yin_match_keyword("import", strlen("import")), YANG_IMPORT);
224 assert_int_equal(yin_match_keyword("include", strlen("include")), YANG_INCLUDE);
225 assert_int_equal(yin_match_keyword("input", strlen("input")), YANG_INPUT);
226 assert_int_equal(yin_match_keyword("key", strlen("key")), YANG_KEY);
227 assert_int_equal(yin_match_keyword("leaf", strlen("leaf")), YANG_LEAF);
228 assert_int_equal(yin_match_keyword("leaf-list", strlen("leaf-list")), YANG_LEAF_LIST);
229 assert_int_equal(yin_match_keyword("length", strlen("length")), YANG_LENGTH);
230 assert_int_equal(yin_match_keyword("list", strlen("list")), YANG_LIST);
231 assert_int_equal(yin_match_keyword("mandatory", strlen("mandatory")), YANG_MANDATORY);
232 assert_int_equal(yin_match_keyword("max-elements", strlen("max-elements")), YANG_MAX_ELEMENTS);
233 assert_int_equal(yin_match_keyword("min-elements", strlen("min-elements")), YANG_MIN_ELEMENTS);
234 assert_int_equal(yin_match_keyword("modifier", strlen("modifier")), YANG_MODIFIER);
235 assert_int_equal(yin_match_keyword("module", strlen("module")), YANG_MODULE);
236 assert_int_equal(yin_match_keyword("must", strlen("must")), YANG_MUST);
237 assert_int_equal(yin_match_keyword("namespace", strlen("namespace")), YANG_NAMESPACE);
238 assert_int_equal(yin_match_keyword("notification", strlen("notification")), YANG_NOTIFICATION);
239 assert_int_equal(yin_match_keyword("ordered-by", strlen("ordered-by")), YANG_ORDERED_BY);
240 assert_int_equal(yin_match_keyword("organization", strlen("organization")), YANG_ORGANIZATION);
241 assert_int_equal(yin_match_keyword("output", strlen("output")), YANG_OUTPUT);
242 assert_int_equal(yin_match_keyword("path", strlen("path")), YANG_PATH);
243 assert_int_equal(yin_match_keyword("pattern", strlen("pattern")), YANG_PATTERN);
244 assert_int_equal(yin_match_keyword("position", strlen("position")), YANG_POSITION);
245 assert_int_equal(yin_match_keyword("prefix", strlen("prefix")), YANG_PREFIX);
246 assert_int_equal(yin_match_keyword("presence", strlen("presence")), YANG_PRESENCE);
247 assert_int_equal(yin_match_keyword("range", strlen("range")), YANG_RANGE);
248 assert_int_equal(yin_match_keyword("reference", strlen("reference")), YANG_REFERENCE);
249 assert_int_equal(yin_match_keyword("refine", strlen("refine")), YANG_REFINE);
250 assert_int_equal(yin_match_keyword("require-instance", strlen("require-instance")), YANG_REQUIRE_INSTANCE);
251 assert_int_equal(yin_match_keyword("revision", strlen("revision")), YANG_REVISION);
252 assert_int_equal(yin_match_keyword("revision-date", strlen("revision-date")), YANG_REVISION_DATE);
253 assert_int_equal(yin_match_keyword("rpc", strlen("rpc")), YANG_RPC);
254 assert_int_equal(yin_match_keyword("status", strlen("status")), YANG_STATUS);
255 assert_int_equal(yin_match_keyword("submodule", strlen("submodule")), YANG_SUBMODULE);
256 assert_int_equal(yin_match_keyword("type", strlen("type")), YANG_TYPE);
257 assert_int_equal(yin_match_keyword("typedef", strlen("typedef")), YANG_TYPEDEF);
258 assert_int_equal(yin_match_keyword("unique", strlen("unique")), YANG_UNIQUE);
259 assert_int_equal(yin_match_keyword("units", strlen("units")), YANG_UNITS);
260 assert_int_equal(yin_match_keyword("uses", strlen("uses")), YANG_USES);
261 assert_int_equal(yin_match_keyword("value", strlen("value")), YANG_VALUE);
262 assert_int_equal(yin_match_keyword("when", strlen("when")), YANG_WHEN);
263 assert_int_equal(yin_match_keyword("yang-version", strlen("yang-version")), YANG_YANG_VERSION);
264 assert_int_equal(yin_match_keyword("yin-element", strlen("yin-element")), YANG_YIN_ELEMENT);
David Sedlák872c7b42018-10-26 13:15:20 +0200265}
David Sedlák3b4db242018-10-19 16:11:01 +0200266
David Sedlák872c7b42018-10-26 13:15:20 +0200267static void
David Sedlák060b00e2019-06-19 11:12:06 +0200268test_yin_match_argument_name(void **state)
David Sedlák872c7b42018-10-26 13:15:20 +0200269{
David Sedlák68a1af12019-03-08 13:46:54 +0100270 (void)state; /* unused */
David Sedlák872c7b42018-10-26 13:15:20 +0200271
David Sedlák060b00e2019-06-19 11:12:06 +0200272 assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN);
273 assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN);
274 assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN);
275 assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION);
276 assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE);
277 assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE);
278 assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME);
279 assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG);
280 assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
281 assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT);
282 assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI);
283 assert_int_equal(yin_match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200284}
285
David Sedlák68a1af12019-03-08 13:46:54 +0100286static void
287test_meta(void **state)
288{
289 LY_ERR ret = LY_SUCCESS;
290 struct state *st = *state;
291
David Sedlák2b214ac2019-06-06 16:11:03 +0200292 ret = yin_parse_module(st->ctx,"<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\
293 name=\"example-foo\">\
David Sedlák15a92662019-06-18 11:55:15 +0200294 <organization xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"><text>organization...</text></organization>\
295 <contact><text>contact...</text></contact>\
296 <description><text>description...</text></description>\
297 <reference><text>reference...</text></reference>\
David Sedlák68a1af12019-03-08 13:46:54 +0100298 </module>", st->mod);
299
300 assert_int_equal(ret, LY_SUCCESS);
David Sedláka7406952019-04-05 10:33:07 +0200301 assert_string_equal(st->mod->parsed->mod->org, "organization...");
David Sedlák68a1af12019-03-08 13:46:54 +0100302 assert_string_equal(st->mod->parsed->mod->contact, "contact...");
303 assert_string_equal(st->mod->parsed->mod->dsc, "description...");
304 assert_string_equal(st->mod->parsed->mod->ref, "reference...");
David Sedlák68826732019-06-05 10:50:58 +0200305
306 st = reset_state(state);
307 ret = yin_parse_module(st->ctx,"<module name=\"example-foo\">\
308 <organization test=\"invalid-argument\">organization...</organization>\
309 </module>", st->mod);
310 assert_int_equal(ret, LY_EVALID);
David Sedlák79e50cb2019-06-05 16:33:09 +0200311
312 st->finished_correctly = true;
David Sedlák68a1af12019-03-08 13:46:54 +0100313}
314
David Sedlák8f5bce02019-06-03 16:41:08 +0200315static void
David Sedlák060b00e2019-06-19 11:12:06 +0200316test_yin_parse_text_element(void **state)
David Sedlák8f5bce02019-06-03 16:41:08 +0200317{
318 struct state *st = *state;
319 const char *res = NULL, *prefix = NULL, *name = NULL;
320 size_t prefix_len = 0, name_len = 0;
321 LY_ERR ret = LY_SUCCESS;
322
323 const char *data = "<elem>content</elem>";
324 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák060b00e2019-06-19 11:12:06 +0200325 yin_parse_text_element(st->xml_ctx, &data, &res);
David Sedlák8f5bce02019-06-03 16:41:08 +0200326 assert_string_equal(res, "content");
327 lydict_remove(st->ctx, "content");
David Sedlák15a92662019-06-18 11:55:15 +0200328 st = reset_state(state);
David Sedlák8f5bce02019-06-03 16:41:08 +0200329
David Sedlák8f5bce02019-06-03 16:41:08 +0200330 data = "<elem xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">another-content</elem>";
331 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák060b00e2019-06-19 11:12:06 +0200332 yin_parse_text_element(st->xml_ctx, &data, &res);
David Sedlák8f5bce02019-06-03 16:41:08 +0200333 assert_string_equal(res, "another-content");
334 lydict_remove(st->ctx, "another-content");
David Sedlák15a92662019-06-18 11:55:15 +0200335 st = reset_state(state);
David Sedlák8f5bce02019-06-03 16:41:08 +0200336
David Sedlák7ff55a92019-06-17 11:11:41 +0200337 data = "<elem invalid=\"invalid\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">text</elem>";
David Sedlák8f5bce02019-06-03 16:41:08 +0200338 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
David Sedlák060b00e2019-06-19 11:12:06 +0200339 ret = yin_parse_text_element(st->xml_ctx, &data, &res);
David Sedlák8f5bce02019-06-03 16:41:08 +0200340 assert_int_equal(ret, LY_EVALID);
David Sedlák79e50cb2019-06-05 16:33:09 +0200341
342 st->finished_correctly = true;
David Sedlák8f5bce02019-06-03 16:41:08 +0200343}
344
David Sedlákd9d3a312019-06-04 09:47:10 +0200345static void
David Sedlákda63c082019-06-04 13:52:23 +0200346test_yin_parse_import(void **state)
347{
348 struct state *st = *state;
349 const char *prefix = NULL, *name = NULL;
350 size_t prefix_len = 0, name_len = 0;
351 LY_ERR ret = LY_SUCCESS;
352 struct lysp_import *imports = NULL;
353
David Sedlák2b214ac2019-06-06 16:11:03 +0200354 const char *data = "<import xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" module=\"a\">\
David Sedlákda63c082019-06-04 13:52:23 +0200355 <prefix value=\"a_mod\"/>\
356 <revision-date date=\"2015-01-01\"/>\
David Sedlákb6e65972019-06-19 10:44:13 +0200357 <description><text>import description</text></description>\
358 <reference><text>import reference</text></reference>\
David Sedlákc67dcaa2019-06-04 14:49:05 +0200359 </import>\
360 \
David Sedlák2b214ac2019-06-06 16:11:03 +0200361 <import xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" module=\"a\">\
David Sedlákc67dcaa2019-06-04 14:49:05 +0200362 <prefix value=\"a_mod\"/>\
363 <revision-date date=\"2015-01-01\"/>\
David Sedlákda63c082019-06-04 13:52:23 +0200364 </import>";
David Sedlákb6e65972019-06-19 10:44:13 +0200365 /* first import */
David Sedlákda63c082019-06-04 13:52:23 +0200366 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
367 ret = yin_parse_import(st->xml_ctx, "b-mod", &data, &imports);
368 assert_int_equal(ret, LY_SUCCESS);
369 assert_string_equal(imports->name, "a");
370 assert_string_equal(imports->prefix, "a_mod");
371 assert_string_equal(imports->rev, "2015-01-01");
David Sedlákb6e65972019-06-19 10:44:13 +0200372 assert_string_equal(imports->dsc, "import description");
373 assert_string_equal(imports->ref, "import reference");
David Sedlákda63c082019-06-04 13:52:23 +0200374 lydict_remove(st->ctx, imports->name);
375 lydict_remove(st->ctx, imports->prefix);
David Sedlákb6e65972019-06-19 10:44:13 +0200376 lydict_remove(st->ctx, imports->dsc);
377 lydict_remove(st->ctx, imports->ref);
David Sedlákda63c082019-06-04 13:52:23 +0200378 LY_ARRAY_FREE(imports);
David Sedlákda63c082019-06-04 13:52:23 +0200379 imports = NULL;
David Sedlákc67dcaa2019-06-04 14:49:05 +0200380
David Sedlákb6e65972019-06-19 10:44:13 +0200381 /* second invalid import */
David Sedlákda63c082019-06-04 13:52:23 +0200382 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
383 ret = yin_parse_import(st->xml_ctx, "a_mod", &data, &imports);
384 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ákb6e65972019-06-19 10:44:13 +0200386 /* cleanup is supposed to be done by caller function */
David Sedlákda63c082019-06-04 13:52:23 +0200387 lydict_remove(st->ctx, imports->name);
388 lydict_remove(st->ctx, imports->prefix);
389 LY_ARRAY_FREE(imports);
David Sedlák79e50cb2019-06-05 16:33:09 +0200390
David Sedlákb6e65972019-06-19 10:44:13 +0200391 imports = NULL;
392 st = reset_state(state);
393 /* import with unknown child element */
394 data = "<import xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" module=\"a\">\
395 <what value=\"a_mod\"/>\
396 </import>";
397 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
398 ret = yin_parse_import(st->xml_ctx, "invalid_mod", &data, &imports);
399 assert_int_equal(ret, LY_EVALID);
400 logbuf_assert("Unexpected child element \"what\" of import element.");
401 /* cleanup is supposed to be done by caller function */
402 lydict_remove(st->ctx, imports->name);
403 LY_ARRAY_FREE(imports);
404
405 st->finished_correctly = true;
406}
407
408static void
409test_yin_parse_status(void **state)
410{
411 struct state *st = *state;
412 const char *prefix = NULL, *name = NULL;
413 size_t prefix_len = 0, name_len = 0;
414 LY_ERR ret = LY_SUCCESS;
415 uint16_t flags = 0;
416 struct lysp_ext_instance *exts;
417
418 /* try all valid values */
419 const char *data = "<status value=\"current\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>";
420 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
421 ret = yin_parse_status(st->xml_ctx, &data, &flags, &exts);
422 assert_int_equal(ret, LY_SUCCESS);
423 assert_true(flags & LYS_STATUS_CURR);
424
425 st = reset_state(state);
426 flags = 0;
427 data = "<status value=\"deprecated\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>";
428 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
429 ret = yin_parse_status(st->xml_ctx, &data, &flags, &exts);
430 assert_int_equal(ret, LY_SUCCESS);
431 assert_true(flags & LYS_STATUS_DEPRC);
432 st->finished_correctly = true;
433
434 st = reset_state(state);
435 flags = 0;
436 data = "<status value=\"obsolete\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>";
437 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
438 ret = yin_parse_status(st->xml_ctx, &data, &flags, &exts);
439 assert_int_equal(ret, LY_SUCCESS);
440 assert_true(flags & LYS_STATUS_OBSLT);
441 st->finished_correctly = true;
442
443 /* duplicit definition (no reset_state() call) */
444 data = "<status value=\"deprecated\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>";
445 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
446 ret = yin_parse_status(st->xml_ctx, &data, &flags, &exts);
447 assert_int_equal(ret, LY_EVALID);
448 logbuf_assert("Duplicate element \"status\". Line number 1.");
449
450 /* invalid status value */
451 st = reset_state(state);
452 flags = 0;
453 data = "<status value=\"dunno\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"/>";
454 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
455 ret = yin_parse_status(st->xml_ctx, &data, &flags, &exts);
456 assert_int_equal(ret, LY_EVALID);
457 logbuf_assert("Invalid value \"dunno\" of \"status\". Line number 1.");
David Sedlák79e50cb2019-06-05 16:33:09 +0200458 st->finished_correctly = true;
David Sedlákda63c082019-06-04 13:52:23 +0200459}
460
David Sedlák3b4db242018-10-19 16:11:01 +0200461int
462main(void)
463{
464
465 const struct CMUnitTest tests[] = {
David Sedlák392af4f2019-06-04 16:02:42 +0200466 cmocka_unit_test_setup_teardown(test_yin_parse_module, setup_f, teardown_f),
David Sedlák68a1af12019-03-08 13:46:54 +0100467 cmocka_unit_test_setup_teardown(test_meta, setup_f, teardown_f),
David Sedlák060b00e2019-06-19 11:12:06 +0200468 cmocka_unit_test_setup_teardown(test_yin_parse_text_element, setup_f, teardown_f),
David Sedlákda63c082019-06-04 13:52:23 +0200469 cmocka_unit_test_setup_teardown(test_yin_parse_import, setup_f, teardown_f),
David Sedlákb6e65972019-06-19 10:44:13 +0200470 cmocka_unit_test_setup_teardown(test_yin_parse_status, setup_f, teardown_f),
David Sedlák1bccdfa2019-06-17 15:55:27 +0200471 cmocka_unit_test(test_yin_match_keyword),
David Sedlák060b00e2019-06-19 11:12:06 +0200472 cmocka_unit_test(test_yin_match_argument_name),
David Sedlák3b4db242018-10-19 16:11:01 +0200473 };
474
475 return cmocka_run_group_tests(tests, NULL, NULL);
476}