blob: 97d39d8869f15f3643b661d5919c35d45aa0aa72 [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
61static void
62test_parse(void **state)
63{
64 LY_ERR ret = LY_SUCCESS;
65 struct state *st = *state;
66
67 ret = yin_parse_module(st->ctx,
David Sedlák18730132019-03-15 15:51:34 +010068 "<module name=\"example-foo\"\
69 xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\
70 xmlns:foo=\"urn:example:foo\"\
71 xmlns:myext=\"urn:example:extensions\">\
David Sedlákcd0c9512019-03-29 13:23:06 +010072 <namespace uri=\"urn:example:foo\" xmlns:myext=\"urn:example:extensions\"/>\
David Sedláka7406952019-04-05 10:33:07 +020073 <prefix xmlns:myxt=\"urn:emple:extensions\" value=\"foo\" xmlns:myext=\"urn:example:extensions\"/>\
David Sedlákd9d3a312019-06-04 09:47:10 +020074 </module>",
David Sedlák68a1af12019-03-08 13:46:54 +010075 st->mod);
76
77 assert_int_equal(ret, LY_SUCCESS);
78 assert_string_equal(st->mod->parsed->mod->name, "example-foo");
79 assert_string_equal(st->mod->parsed->mod->prefix, "foo");
David Sedlákcd0c9512019-03-29 13:23:06 +010080 assert_string_equal(st->mod->parsed->mod->ns, "urn:example:foo");
David Sedlák3b4db242018-10-19 16:11:01 +020081}
82
83static void
84test_match_keyword(void **state)
85{
86 (void)state; /* unused */
87
David Sedlák18730132019-03-15 15:51:34 +010088 assert_int_equal(match_keyword("anydatax", strlen("anydatax"), 0), YANG_NONE);
89 assert_int_equal(match_keyword("asdasd", strlen("asdasd"), 0), YANG_NONE);
90 assert_int_equal(match_keyword("", 0, 0), YANG_NONE);
91 assert_int_equal(match_keyword("anydata", strlen("anydata"), 0), YANG_ANYDATA);
92 assert_int_equal(match_keyword("anyxml", strlen("anyxml"), 0), YANG_ANYXML);
93 assert_int_equal(match_keyword("argument", strlen("argument"), 0), YANG_ARGUMENT);
94 assert_int_equal(match_keyword("augment", strlen("augment"), 0), YANG_AUGMENT);
95 assert_int_equal(match_keyword("base", strlen("base"), 0), YANG_BASE);
96 assert_int_equal(match_keyword("belongs-to", strlen("belongs-to"), 0), YANG_BELONGS_TO);
97 assert_int_equal(match_keyword("bit", strlen("bit"), 0), YANG_BIT);
98 assert_int_equal(match_keyword("case", strlen("case"), 0), YANG_CASE);
99 assert_int_equal(match_keyword("choice", strlen("choice"), 0), YANG_CHOICE);
100 assert_int_equal(match_keyword("config", strlen("config"), 0), YANG_CONFIG);
101 assert_int_equal(match_keyword("contact", strlen("contact"), 0), YANG_CONTACT);
102 assert_int_equal(match_keyword("container", strlen("container"), 0), YANG_CONTAINER);
103 assert_int_equal(match_keyword("default", strlen("default"), 0), YANG_DEFAULT);
104 assert_int_equal(match_keyword("description", strlen("description"), 0), YANG_DESCRIPTION);
105 assert_int_equal(match_keyword("deviate", strlen("deviate"), 0), YANG_DEVIATE);
106 assert_int_equal(match_keyword("deviation", strlen("deviation"), 0), YANG_DEVIATION);
107 assert_int_equal(match_keyword("enum", strlen("enum"), 0), YANG_ENUM);
108 assert_int_equal(match_keyword("error-app-tag", strlen("error-app-tag"), 0), YANG_ERROR_APP_TAG);
109 assert_int_equal(match_keyword("error-message", strlen("error-message"), 0), YANG_ERROR_MESSAGE);
110 assert_int_equal(match_keyword("extension", strlen("extension"), 0), YANG_EXTENSION);
111 assert_int_equal(match_keyword("feature", strlen("feature"), 0), YANG_FEATURE);
112 assert_int_equal(match_keyword("fraction-digits", strlen("fraction-digits"), 0), YANG_FRACTION_DIGITS);
113 assert_int_equal(match_keyword("grouping", strlen("grouping"), 0), YANG_GROUPING);
114 assert_int_equal(match_keyword("identity", strlen("identity"), 0), YANG_IDENTITY);
115 assert_int_equal(match_keyword("if-feature", strlen("if-feature"), 0), YANG_IF_FEATURE);
116 assert_int_equal(match_keyword("import", strlen("import"), 0), YANG_IMPORT);
117 assert_int_equal(match_keyword("include", strlen("include"), 0), YANG_INCLUDE);
118 assert_int_equal(match_keyword("input", strlen("input"), 0), YANG_INPUT);
119 assert_int_equal(match_keyword("key", strlen("key"), 0), YANG_KEY);
120 assert_int_equal(match_keyword("leaf", strlen("leaf"), 0), YANG_LEAF);
121 assert_int_equal(match_keyword("leaf-list", strlen("leaf-list"), 0), YANG_LEAF_LIST);
122 assert_int_equal(match_keyword("length", strlen("length"), 0), YANG_LENGTH);
123 assert_int_equal(match_keyword("list", strlen("list"), 0), YANG_LIST);
124 assert_int_equal(match_keyword("mandatory", strlen("mandatory"), 0), YANG_MANDATORY);
125 assert_int_equal(match_keyword("max-elements", strlen("max-elements"), 0), YANG_MAX_ELEMENTS);
126 assert_int_equal(match_keyword("min-elements", strlen("min-elements"), 0), YANG_MIN_ELEMENTS);
127 assert_int_equal(match_keyword("modifier", strlen("modifier"), 0), YANG_MODIFIER);
128 assert_int_equal(match_keyword("module", strlen("module"), 0), YANG_MODULE);
129 assert_int_equal(match_keyword("must", strlen("must"), 0), YANG_MUST);
130 assert_int_equal(match_keyword("namespace", strlen("namespace"), 0), YANG_NAMESPACE);
131 assert_int_equal(match_keyword("notification", strlen("notification"), 0), YANG_NOTIFICATION);
132 assert_int_equal(match_keyword("ordered-by", strlen("ordered-by"), 0), YANG_ORDERED_BY);
133 assert_int_equal(match_keyword("organization", strlen("organization"), 0), YANG_ORGANIZATION);
134 assert_int_equal(match_keyword("output", strlen("output"), 0), YANG_OUTPUT);
135 assert_int_equal(match_keyword("path", strlen("path"), 0), YANG_PATH);
136 assert_int_equal(match_keyword("pattern", strlen("pattern"), 0), YANG_PATTERN);
137 assert_int_equal(match_keyword("position", strlen("position"), 0), YANG_POSITION);
138 assert_int_equal(match_keyword("prefix", strlen("prefix"), 0), YANG_PREFIX);
139 assert_int_equal(match_keyword("presence", strlen("presence"), 0), YANG_PRESENCE);
140 assert_int_equal(match_keyword("range", strlen("range"), 0), YANG_RANGE);
141 assert_int_equal(match_keyword("reference", strlen("reference"), 0), YANG_REFERENCE);
142 assert_int_equal(match_keyword("refine", strlen("refine"), 0), YANG_REFINE);
143 assert_int_equal(match_keyword("require-instance", strlen("require-instance"), 0), YANG_REQUIRE_INSTANCE);
144 assert_int_equal(match_keyword("revision", strlen("revision"), 0), YANG_REVISION);
145 assert_int_equal(match_keyword("revision-date", strlen("revision-date"), 0), YANG_REVISION_DATE);
146 assert_int_equal(match_keyword("rpc", strlen("rpc"), 0), YANG_RPC);
147 assert_int_equal(match_keyword("status", strlen("status"), 0), YANG_STATUS);
148 assert_int_equal(match_keyword("submodule", strlen("submodule"), 0), YANG_SUBMODULE);
149 assert_int_equal(match_keyword("type", strlen("type"), 0), YANG_TYPE);
150 assert_int_equal(match_keyword("typedef", strlen("typedef"), 0), YANG_TYPEDEF);
151 assert_int_equal(match_keyword("unique", strlen("unique"), 0), YANG_UNIQUE);
152 assert_int_equal(match_keyword("units", strlen("units"), 0), YANG_UNITS);
153 assert_int_equal(match_keyword("uses", strlen("uses"), 0), YANG_USES);
154 assert_int_equal(match_keyword("value", strlen("value"), 0), YANG_VALUE);
155 assert_int_equal(match_keyword("when", strlen("when"), 0), YANG_WHEN);
156 assert_int_equal(match_keyword("yang-version", strlen("yang-version"), 0), YANG_YANG_VERSION);
157 assert_int_equal(match_keyword("yin-element", strlen("yin-element"), 0), YANG_YIN_ELEMENT);
David Sedlák872c7b42018-10-26 13:15:20 +0200158}
David Sedlák3b4db242018-10-19 16:11:01 +0200159
David Sedlák872c7b42018-10-26 13:15:20 +0200160static void
161test_match_argument(void **state)
162{
David Sedlák68a1af12019-03-08 13:46:54 +0100163 (void)state; /* unused */
David Sedlák872c7b42018-10-26 13:15:20 +0200164
David Sedláka7406952019-04-05 10:33:07 +0200165 assert_int_equal(match_argument_name("", 5), YIN_ARG_UNKNOWN);
166 assert_int_equal(match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN);
167 assert_int_equal(match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN);
David Sedlák872c7b42018-10-26 13:15:20 +0200168 assert_int_equal(match_argument_name("condition", 9), YIN_ARG_CONDITION);
169 assert_int_equal(match_argument_name("date", 4), YIN_ARG_DATE);
170 assert_int_equal(match_argument_name("module", 6), YIN_ARG_MODULE);
171 assert_int_equal(match_argument_name("name", 4), YIN_ARG_NAME);
172 assert_int_equal(match_argument_name("tag", 3), YIN_ARG_TAG);
173 assert_int_equal(match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
174 assert_int_equal(match_argument_name("text", 4), YIN_ARG_TEXT);
175 assert_int_equal(match_argument_name("uri", 3), YIN_ARG_URI);
176 assert_int_equal(match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200177}
178
David Sedlák68a1af12019-03-08 13:46:54 +0100179static void
180test_meta(void **state)
181{
182 LY_ERR ret = LY_SUCCESS;
183 struct state *st = *state;
184
185 ret = yin_parse_module(st->ctx,"<module name=\"example-foo\">\
David Sedláka7406952019-04-05 10:33:07 +0200186 <organization xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">organization...</organization>\
David Sedlák68a1af12019-03-08 13:46:54 +0100187 <contact>contact...</contact>\
188 <description>description...</description>\
189 <reference>reference...</reference>\
190 </module>", st->mod);
191
192 assert_int_equal(ret, LY_SUCCESS);
David Sedláka7406952019-04-05 10:33:07 +0200193 assert_string_equal(st->mod->parsed->mod->org, "organization...");
David Sedlák68a1af12019-03-08 13:46:54 +0100194 assert_string_equal(st->mod->parsed->mod->contact, "contact...");
195 assert_string_equal(st->mod->parsed->mod->dsc, "description...");
196 assert_string_equal(st->mod->parsed->mod->ref, "reference...");
197}
198
David Sedlák8f5bce02019-06-03 16:41:08 +0200199static void
200test_parse_text_element(void **state)
201{
202 struct state *st = *state;
203 const char *res = NULL, *prefix = NULL, *name = NULL;
204 size_t prefix_len = 0, name_len = 0;
205 LY_ERR ret = LY_SUCCESS;
206
207 const char *data = "<elem>content</elem>";
208 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
209 parse_text_element(st->xml_ctx, &data, &res);
210 assert_string_equal(res, "content");
211 lydict_remove(st->ctx, "content");
212
213
214 data = "<elem xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">another-content</elem>";
215 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
216 parse_text_element(st->xml_ctx, &data, &res);
217 assert_string_equal(res, "another-content");
218 lydict_remove(st->ctx, "another-content");
219
220 data = "<elem invalid=\"invalid\">text</elem>";
221 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
222 ret = parse_text_element(st->xml_ctx, &data, &res);
223 assert_int_equal(ret, LY_EVALID);
224}
225
David Sedlákd9d3a312019-06-04 09:47:10 +0200226static void
227test_parse_namespace(void **state)
228{
229 struct state *st = *state;
230 const char *res = NULL, *prefix = NULL, *name = NULL;
231 size_t prefix_len = 0, name_len = 0;
232 LY_ERR ret = LY_SUCCESS;
233
234 const char *data = "<namespace uri=\"urn:example:foo\"/>";
235 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
236 ret = parse_namespace(st->xml_ctx, &data, &res);
237 assert_int_equal(ret, LY_SUCCESS);
238 assert_string_equal(res, "urn:example:foo");
239 lydict_remove(st->ctx, "urn:example:foo");
240
241 data = "<namespace urr=\"urn:example:foo\"/>";
242 lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
243 ret = parse_namespace(st->xml_ctx, &data, &res);
244 assert_int_equal(ret, LY_EVALID);
245}
246
David Sedlák3b4db242018-10-19 16:11:01 +0200247int
248main(void)
249{
250
251 const struct CMUnitTest tests[] = {
David Sedlák68a1af12019-03-08 13:46:54 +0100252 cmocka_unit_test_setup_teardown(test_parse, setup_f, teardown_f),
253 cmocka_unit_test_setup_teardown(test_meta, setup_f, teardown_f),
David Sedlák8f5bce02019-06-03 16:41:08 +0200254 cmocka_unit_test_setup_teardown(test_parse_text_element, setup_f, teardown_f),
David Sedlákd9d3a312019-06-04 09:47:10 +0200255 cmocka_unit_test_setup_teardown(test_parse_namespace, setup_f, teardown_f),
David Sedlák3b4db242018-10-19 16:11:01 +0200256 cmocka_unit_test(test_match_keyword),
David Sedlák872c7b42018-10-26 13:15:20 +0200257 cmocka_unit_test(test_match_argument),
David Sedlák3b4db242018-10-19 16:11:01 +0200258 };
259
260 return cmocka_run_group_tests(tests, NULL, NULL);
261}