blob: 07761d863c25918beff7659becae6338e8901cf9 [file] [log] [blame]
David Sedlák3b4db242018-10-19 16:11:01 +02001#include <stdarg.h>
2#include <stddef.h>
3#include <setjmp.h>
4#include <cmocka.h>
5
6#include <stdio.h>
7#include <string.h>
8
David Sedlákecf5eb82019-06-03 14:12:44 +02009#include "../../src/common.h"
10#include "../../src/tree_schema.h"
11#include "../../src/tree_schema_internal.h"
12#include "../../src/parser_yin.h"
David Sedlák8f5bce02019-06-03 16:41:08 +020013#include "../../src/xml.h"
David Sedlák3b4db242018-10-19 16:11:01 +020014
David Sedlák68a1af12019-03-08 13:46:54 +010015struct state {
David Sedlák3b4db242018-10-19 16:11:01 +020016 struct ly_ctx *ctx;
David Sedlák3017da42019-02-15 09:48:04 +010017 struct lys_module *mod;
David Sedlák8f5bce02019-06-03 16:41:08 +020018 struct lyxml_context *xml_ctx;
David Sedlák68a1af12019-03-08 13:46:54 +010019};
David Sedlák872c7b42018-10-26 13:15:20 +020020
David Sedlák68a1af12019-03-08 13:46:54 +010021static int
22setup_f(void **state)
23{
24 struct state *st = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +020025
David Sedlák68a1af12019-03-08 13:46:54 +010026 /* allocate state variable */
27 (*state) = st = calloc(1, sizeof(*st));
28 if (!st) {
29 fprintf(stderr, "Memmory allocation failed");
30 return EXIT_FAILURE;
31 }
David Sedlák872c7b42018-10-26 13:15:20 +020032
David Sedlák68a1af12019-03-08 13:46:54 +010033 /* create new libyang context */
34 ly_ctx_new(NULL, 0, &st->ctx);
David Sedlák872c7b42018-10-26 13:15:20 +020035
David Sedlák68a1af12019-03-08 13:46:54 +010036 /* allocate new module */
37 st->mod = calloc(1, sizeof(*st->mod));
38 st->mod->ctx = st->ctx;
39
David Sedlák8f5bce02019-06-03 16:41:08 +020040 st->xml_ctx = calloc(1, sizeof(struct lys_parser_ctx));
41 st->xml_ctx->ctx = st->ctx;
42 st->xml_ctx->line = 1;
43
David Sedlák68a1af12019-03-08 13:46:54 +010044 return EXIT_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +020045}
46
47static int
David Sedlák68a1af12019-03-08 13:46:54 +010048teardown_f(void **state)
49{
50 struct state *st = *(struct state **)state;
51
David Sedlák8f5bce02019-06-03 16:41:08 +020052 lyxml_context_clear(st->xml_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +010053 lys_module_free(st->mod, NULL);
54 ly_ctx_destroy(st->ctx, NULL);
David Sedlák8f5bce02019-06-03 16:41:08 +020055 free(st->xml_ctx);
David Sedlák68a1af12019-03-08 13:46:54 +010056 free(st);
57
58 return EXIT_SUCCESS;
59}
60
David Sedlák392af4f2019-06-04 16:02:42 +020061static struct state*
62reset_state(void **state)
63{
64 teardown_f(state);
65 setup_f(state);
66
67 return *state;
68}
69
David Sedlák68a1af12019-03-08 13:46:54 +010070static void
David Sedlák392af4f2019-06-04 16:02:42 +020071test_yin_parse_module(void **state)
David Sedlák68a1af12019-03-08 13:46:54 +010072{
73 LY_ERR ret = LY_SUCCESS;
74 struct state *st = *state;
75
76 ret = yin_parse_module(st->ctx,
David Sedlák18730132019-03-15 15:51:34 +010077 "<module name=\"example-foo\"\
78 xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\
79 xmlns:foo=\"urn:example:foo\"\
80 xmlns:myext=\"urn:example:extensions\">\
David Sedlákcd0c9512019-03-29 13:23:06 +010081 <namespace uri=\"urn:example:foo\" xmlns:myext=\"urn:example:extensions\"/>\
David Sedláka7406952019-04-05 10:33:07 +020082 <prefix xmlns:myxt=\"urn:emple:extensions\" value=\"foo\" xmlns:myext=\"urn:example:extensions\"/>\
David Sedlákd9d3a312019-06-04 09:47:10 +020083 </module>",
David Sedlák68a1af12019-03-08 13:46:54 +010084 st->mod);
85
86 assert_int_equal(ret, LY_SUCCESS);
87 assert_string_equal(st->mod->parsed->mod->name, "example-foo");
88 assert_string_equal(st->mod->parsed->mod->prefix, "foo");
David Sedlákcd0c9512019-03-29 13:23:06 +010089 assert_string_equal(st->mod->parsed->mod->ns, "urn:example:foo");
David Sedlák392af4f2019-06-04 16:02:42 +020090
91 st = reset_state(state);
92 ret = yin_parse_module(st->ctx,
93 "<module name=\"example-foo\">\
94 <invalid-tag uri=\"urn:example:foo\"\"/>\
95 </module>",
96 st->mod);
97 assert_int_equal(ret, LY_EVALID);
98
99 st = reset_state(state);
100 ret = yin_parse_module(st->ctx,
101 "<module>\
102 </module>",
103 st->mod);
104 assert_int_equal(ret, LY_EVALID);
105
106 st = reset_state(state);
107 ret = yin_parse_module(st->ctx,
108 "",
109 st->mod);
110 assert_int_equal(ret, LY_EVALID);
David Sedlák3b4db242018-10-19 16:11:01 +0200111}
112
113static void
114test_match_keyword(void **state)
115{
116 (void)state; /* unused */
117
David Sedlák18730132019-03-15 15:51:34 +0100118 assert_int_equal(match_keyword("anydatax", strlen("anydatax"), 0), YANG_NONE);
119 assert_int_equal(match_keyword("asdasd", strlen("asdasd"), 0), YANG_NONE);
120 assert_int_equal(match_keyword("", 0, 0), YANG_NONE);
121 assert_int_equal(match_keyword("anydata", strlen("anydata"), 0), YANG_ANYDATA);
122 assert_int_equal(match_keyword("anyxml", strlen("anyxml"), 0), YANG_ANYXML);
123 assert_int_equal(match_keyword("argument", strlen("argument"), 0), YANG_ARGUMENT);
124 assert_int_equal(match_keyword("augment", strlen("augment"), 0), YANG_AUGMENT);
125 assert_int_equal(match_keyword("base", strlen("base"), 0), YANG_BASE);
126 assert_int_equal(match_keyword("belongs-to", strlen("belongs-to"), 0), YANG_BELONGS_TO);
127 assert_int_equal(match_keyword("bit", strlen("bit"), 0), YANG_BIT);
128 assert_int_equal(match_keyword("case", strlen("case"), 0), YANG_CASE);
129 assert_int_equal(match_keyword("choice", strlen("choice"), 0), YANG_CHOICE);
130 assert_int_equal(match_keyword("config", strlen("config"), 0), YANG_CONFIG);
131 assert_int_equal(match_keyword("contact", strlen("contact"), 0), YANG_CONTACT);
132 assert_int_equal(match_keyword("container", strlen("container"), 0), YANG_CONTAINER);
133 assert_int_equal(match_keyword("default", strlen("default"), 0), YANG_DEFAULT);
134 assert_int_equal(match_keyword("description", strlen("description"), 0), YANG_DESCRIPTION);
135 assert_int_equal(match_keyword("deviate", strlen("deviate"), 0), YANG_DEVIATE);
136 assert_int_equal(match_keyword("deviation", strlen("deviation"), 0), YANG_DEVIATION);
137 assert_int_equal(match_keyword("enum", strlen("enum"), 0), YANG_ENUM);
138 assert_int_equal(match_keyword("error-app-tag", strlen("error-app-tag"), 0), YANG_ERROR_APP_TAG);
139 assert_int_equal(match_keyword("error-message", strlen("error-message"), 0), YANG_ERROR_MESSAGE);
140 assert_int_equal(match_keyword("extension", strlen("extension"), 0), YANG_EXTENSION);
141 assert_int_equal(match_keyword("feature", strlen("feature"), 0), YANG_FEATURE);
142 assert_int_equal(match_keyword("fraction-digits", strlen("fraction-digits"), 0), YANG_FRACTION_DIGITS);
143 assert_int_equal(match_keyword("grouping", strlen("grouping"), 0), YANG_GROUPING);
144 assert_int_equal(match_keyword("identity", strlen("identity"), 0), YANG_IDENTITY);
145 assert_int_equal(match_keyword("if-feature", strlen("if-feature"), 0), YANG_IF_FEATURE);
146 assert_int_equal(match_keyword("import", strlen("import"), 0), YANG_IMPORT);
147 assert_int_equal(match_keyword("include", strlen("include"), 0), YANG_INCLUDE);
148 assert_int_equal(match_keyword("input", strlen("input"), 0), YANG_INPUT);
149 assert_int_equal(match_keyword("key", strlen("key"), 0), YANG_KEY);
150 assert_int_equal(match_keyword("leaf", strlen("leaf"), 0), YANG_LEAF);
151 assert_int_equal(match_keyword("leaf-list", strlen("leaf-list"), 0), YANG_LEAF_LIST);
152 assert_int_equal(match_keyword("length", strlen("length"), 0), YANG_LENGTH);
153 assert_int_equal(match_keyword("list", strlen("list"), 0), YANG_LIST);
154 assert_int_equal(match_keyword("mandatory", strlen("mandatory"), 0), YANG_MANDATORY);
155 assert_int_equal(match_keyword("max-elements", strlen("max-elements"), 0), YANG_MAX_ELEMENTS);
156 assert_int_equal(match_keyword("min-elements", strlen("min-elements"), 0), YANG_MIN_ELEMENTS);
157 assert_int_equal(match_keyword("modifier", strlen("modifier"), 0), YANG_MODIFIER);
158 assert_int_equal(match_keyword("module", strlen("module"), 0), YANG_MODULE);
159 assert_int_equal(match_keyword("must", strlen("must"), 0), YANG_MUST);
160 assert_int_equal(match_keyword("namespace", strlen("namespace"), 0), YANG_NAMESPACE);
161 assert_int_equal(match_keyword("notification", strlen("notification"), 0), YANG_NOTIFICATION);
162 assert_int_equal(match_keyword("ordered-by", strlen("ordered-by"), 0), YANG_ORDERED_BY);
163 assert_int_equal(match_keyword("organization", strlen("organization"), 0), YANG_ORGANIZATION);
164 assert_int_equal(match_keyword("output", strlen("output"), 0), YANG_OUTPUT);
165 assert_int_equal(match_keyword("path", strlen("path"), 0), YANG_PATH);
166 assert_int_equal(match_keyword("pattern", strlen("pattern"), 0), YANG_PATTERN);
167 assert_int_equal(match_keyword("position", strlen("position"), 0), YANG_POSITION);
168 assert_int_equal(match_keyword("prefix", strlen("prefix"), 0), YANG_PREFIX);
169 assert_int_equal(match_keyword("presence", strlen("presence"), 0), YANG_PRESENCE);
170 assert_int_equal(match_keyword("range", strlen("range"), 0), YANG_RANGE);
171 assert_int_equal(match_keyword("reference", strlen("reference"), 0), YANG_REFERENCE);
172 assert_int_equal(match_keyword("refine", strlen("refine"), 0), YANG_REFINE);
173 assert_int_equal(match_keyword("require-instance", strlen("require-instance"), 0), YANG_REQUIRE_INSTANCE);
174 assert_int_equal(match_keyword("revision", strlen("revision"), 0), YANG_REVISION);
175 assert_int_equal(match_keyword("revision-date", strlen("revision-date"), 0), YANG_REVISION_DATE);
176 assert_int_equal(match_keyword("rpc", strlen("rpc"), 0), YANG_RPC);
177 assert_int_equal(match_keyword("status", strlen("status"), 0), YANG_STATUS);
178 assert_int_equal(match_keyword("submodule", strlen("submodule"), 0), YANG_SUBMODULE);
179 assert_int_equal(match_keyword("type", strlen("type"), 0), YANG_TYPE);
180 assert_int_equal(match_keyword("typedef", strlen("typedef"), 0), YANG_TYPEDEF);
181 assert_int_equal(match_keyword("unique", strlen("unique"), 0), YANG_UNIQUE);
182 assert_int_equal(match_keyword("units", strlen("units"), 0), YANG_UNITS);
183 assert_int_equal(match_keyword("uses", strlen("uses"), 0), YANG_USES);
184 assert_int_equal(match_keyword("value", strlen("value"), 0), YANG_VALUE);
185 assert_int_equal(match_keyword("when", strlen("when"), 0), YANG_WHEN);
186 assert_int_equal(match_keyword("yang-version", strlen("yang-version"), 0), YANG_YANG_VERSION);
187 assert_int_equal(match_keyword("yin-element", strlen("yin-element"), 0), YANG_YIN_ELEMENT);
David Sedlák872c7b42018-10-26 13:15:20 +0200188}
David Sedlák3b4db242018-10-19 16:11:01 +0200189
David Sedlák872c7b42018-10-26 13:15:20 +0200190static void
191test_match_argument(void **state)
192{
David Sedlák68a1af12019-03-08 13:46:54 +0100193 (void)state; /* unused */
David Sedlák872c7b42018-10-26 13:15:20 +0200194
David Sedláka7406952019-04-05 10:33:07 +0200195 assert_int_equal(match_argument_name("", 5), YIN_ARG_UNKNOWN);
196 assert_int_equal(match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN);
197 assert_int_equal(match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN);
David Sedlák872c7b42018-10-26 13:15:20 +0200198 assert_int_equal(match_argument_name("condition", 9), YIN_ARG_CONDITION);
199 assert_int_equal(match_argument_name("date", 4), YIN_ARG_DATE);
200 assert_int_equal(match_argument_name("module", 6), YIN_ARG_MODULE);
201 assert_int_equal(match_argument_name("name", 4), YIN_ARG_NAME);
202 assert_int_equal(match_argument_name("tag", 3), YIN_ARG_TAG);
203 assert_int_equal(match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
204 assert_int_equal(match_argument_name("text", 4), YIN_ARG_TEXT);
205 assert_int_equal(match_argument_name("uri", 3), YIN_ARG_URI);
206 assert_int_equal(match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200207}
208
David Sedlák68a1af12019-03-08 13:46:54 +0100209static void
210test_meta(void **state)
211{
212 LY_ERR ret = LY_SUCCESS;
213 struct state *st = *state;
214
215 ret = yin_parse_module(st->ctx,"<module name=\"example-foo\">\
David Sedláka7406952019-04-05 10:33:07 +0200216 <organization xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">organization...</organization>\
David Sedlák68a1af12019-03-08 13:46:54 +0100217 <contact>contact...</contact>\
218 <description>description...</description>\
219 <reference>reference...</reference>\
220 </module>", st->mod);
221
222 assert_int_equal(ret, LY_SUCCESS);
David Sedláka7406952019-04-05 10:33:07 +0200223 assert_string_equal(st->mod->parsed->mod->org, "organization...");
David Sedlák68a1af12019-03-08 13:46:54 +0100224 assert_string_equal(st->mod->parsed->mod->contact, "contact...");
225 assert_string_equal(st->mod->parsed->mod->dsc, "description...");
226 assert_string_equal(st->mod->parsed->mod->ref, "reference...");
David Sedlák68826732019-06-05 10:50:58 +0200227
228 st = reset_state(state);
229 ret = yin_parse_module(st->ctx,"<module name=\"example-foo\">\
230 <organization test=\"invalid-argument\">organization...</organization>\
231 </module>", st->mod);
232 assert_int_equal(ret, LY_EVALID);
David Sedlák68a1af12019-03-08 13:46:54 +0100233}
234
David Sedlák8f5bce02019-06-03 16:41:08 +0200235static void
236test_parse_text_element(void **state)
237{
238 struct state *st = *state;
239 const char *res = NULL, *prefix = NULL, *name = NULL;
240 size_t prefix_len = 0, name_len = 0;
241 LY_ERR ret = LY_SUCCESS;
242
243 const char *data = "<elem>content</elem>";
244 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
245 parse_text_element(st->xml_ctx, &data, &res);
246 assert_string_equal(res, "content");
247 lydict_remove(st->ctx, "content");
248
David Sedlák8f5bce02019-06-03 16:41:08 +0200249 data = "<elem xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">another-content</elem>";
250 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
251 parse_text_element(st->xml_ctx, &data, &res);
252 assert_string_equal(res, "another-content");
253 lydict_remove(st->ctx, "another-content");
254
255 data = "<elem invalid=\"invalid\">text</elem>";
256 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
257 ret = parse_text_element(st->xml_ctx, &data, &res);
258 assert_int_equal(ret, LY_EVALID);
259}
260
David Sedlákd9d3a312019-06-04 09:47:10 +0200261static void
David Sedlákda63c082019-06-04 13:52:23 +0200262test_yin_parse_import(void **state)
263{
264 struct state *st = *state;
265 const char *prefix = NULL, *name = NULL;
266 size_t prefix_len = 0, name_len = 0;
267 LY_ERR ret = LY_SUCCESS;
268 struct lysp_import *imports = NULL;
269
270 const char *data = "<import module=\"a\">\
271 <prefix value=\"a_mod\"/>\
272 <revision-date date=\"2015-01-01\"/>\
David Sedlákc67dcaa2019-06-04 14:49:05 +0200273 </import>\
274 \
275 <import module=\"a\">\
276 <prefix value=\"a_mod\"/>\
277 <revision-date date=\"2015-01-01\"/>\
David Sedlákda63c082019-06-04 13:52:23 +0200278 </import>";
279
280 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
281 ret = yin_parse_import(st->xml_ctx, "b-mod", &data, &imports);
282 assert_int_equal(ret, LY_SUCCESS);
283 assert_string_equal(imports->name, "a");
284 assert_string_equal(imports->prefix, "a_mod");
285 assert_string_equal(imports->rev, "2015-01-01");
286 lydict_remove(st->ctx, imports->name);
287 lydict_remove(st->ctx, imports->prefix);
288 LY_ARRAY_FREE(imports);
David Sedlákda63c082019-06-04 13:52:23 +0200289 imports = NULL;
David Sedlákc67dcaa2019-06-04 14:49:05 +0200290
David Sedlákda63c082019-06-04 13:52:23 +0200291 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
292 ret = yin_parse_import(st->xml_ctx, "a_mod", &data, &imports);
293 assert_int_equal(ret, LY_EVALID);
294 lydict_remove(st->ctx, imports->name);
295 lydict_remove(st->ctx, imports->prefix);
296 LY_ARRAY_FREE(imports);
297}
298
David Sedlák3b4db242018-10-19 16:11:01 +0200299int
300main(void)
301{
302
303 const struct CMUnitTest tests[] = {
David Sedlák392af4f2019-06-04 16:02:42 +0200304 cmocka_unit_test_setup_teardown(test_yin_parse_module, setup_f, teardown_f),
David Sedlák68a1af12019-03-08 13:46:54 +0100305 cmocka_unit_test_setup_teardown(test_meta, setup_f, teardown_f),
David Sedlák8f5bce02019-06-03 16:41:08 +0200306 cmocka_unit_test_setup_teardown(test_parse_text_element, setup_f, teardown_f),
David Sedlákda63c082019-06-04 13:52:23 +0200307 cmocka_unit_test_setup_teardown(test_yin_parse_import, setup_f, teardown_f),
David Sedlák3b4db242018-10-19 16:11:01 +0200308 cmocka_unit_test(test_match_keyword),
David Sedlák872c7b42018-10-26 13:15:20 +0200309 cmocka_unit_test(test_match_argument),
David Sedlák3b4db242018-10-19 16:11:01 +0200310 };
311
312 return cmocka_run_group_tests(tests, NULL, NULL);
313}