blob: de3e1d4426d90352c06a20581c5852dffb068b8e [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ák3b4db242018-10-19 16:11:01 +020013
David Sedlák68a1af12019-03-08 13:46:54 +010014struct state {
David Sedlák3b4db242018-10-19 16:11:01 +020015 struct ly_ctx *ctx;
David Sedlák3017da42019-02-15 09:48:04 +010016 struct lys_module *mod;
David Sedlák68a1af12019-03-08 13:46:54 +010017};
David Sedlák872c7b42018-10-26 13:15:20 +020018
David Sedlák68a1af12019-03-08 13:46:54 +010019static int
20setup_f(void **state)
21{
22 struct state *st = NULL;
David Sedlák3b4db242018-10-19 16:11:01 +020023
David Sedlák68a1af12019-03-08 13:46:54 +010024 /* allocate state variable */
25 (*state) = st = calloc(1, sizeof(*st));
26 if (!st) {
27 fprintf(stderr, "Memmory allocation failed");
28 return EXIT_FAILURE;
29 }
David Sedlák872c7b42018-10-26 13:15:20 +020030
David Sedlák68a1af12019-03-08 13:46:54 +010031 /* create new libyang context */
32 ly_ctx_new(NULL, 0, &st->ctx);
David Sedlák872c7b42018-10-26 13:15:20 +020033
David Sedlák68a1af12019-03-08 13:46:54 +010034 /* allocate new module */
35 st->mod = calloc(1, sizeof(*st->mod));
36 st->mod->ctx = st->ctx;
37
38 return EXIT_SUCCESS;
David Sedlák3b4db242018-10-19 16:11:01 +020039}
40
41static int
David Sedlák68a1af12019-03-08 13:46:54 +010042teardown_f(void **state)
43{
44 struct state *st = *(struct state **)state;
45
46 lys_module_free(st->mod, NULL);
47 ly_ctx_destroy(st->ctx, NULL);
48 free(st);
49
50 return EXIT_SUCCESS;
51}
52
53static void
54test_parse(void **state)
55{
56 LY_ERR ret = LY_SUCCESS;
57 struct state *st = *state;
58
59 ret = yin_parse_module(st->ctx,
David Sedlák18730132019-03-15 15:51:34 +010060 "<module name=\"example-foo\"\
61 xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\
62 xmlns:foo=\"urn:example:foo\"\
63 xmlns:myext=\"urn:example:extensions\">\
David Sedlákcd0c9512019-03-29 13:23:06 +010064 <namespace uri=\"urn:example:foo\" xmlns:myext=\"urn:example:extensions\"/>\
David Sedláka7406952019-04-05 10:33:07 +020065 <prefix xmlns:myxt=\"urn:emple:extensions\" value=\"foo\" xmlns:myext=\"urn:example:extensions\"/>\
David Sedlák68a1af12019-03-08 13:46:54 +010066 </module>",
67 st->mod);
68
69 assert_int_equal(ret, LY_SUCCESS);
70 assert_string_equal(st->mod->parsed->mod->name, "example-foo");
71 assert_string_equal(st->mod->parsed->mod->prefix, "foo");
David Sedlákcd0c9512019-03-29 13:23:06 +010072 assert_string_equal(st->mod->parsed->mod->ns, "urn:example:foo");
David Sedlák3b4db242018-10-19 16:11:01 +020073}
74
75static void
76test_match_keyword(void **state)
77{
78 (void)state; /* unused */
79
David Sedlák18730132019-03-15 15:51:34 +010080 assert_int_equal(match_keyword("anydatax", strlen("anydatax"), 0), YANG_NONE);
81 assert_int_equal(match_keyword("asdasd", strlen("asdasd"), 0), YANG_NONE);
82 assert_int_equal(match_keyword("", 0, 0), YANG_NONE);
83 assert_int_equal(match_keyword("anydata", strlen("anydata"), 0), YANG_ANYDATA);
84 assert_int_equal(match_keyword("anyxml", strlen("anyxml"), 0), YANG_ANYXML);
85 assert_int_equal(match_keyword("argument", strlen("argument"), 0), YANG_ARGUMENT);
86 assert_int_equal(match_keyword("augment", strlen("augment"), 0), YANG_AUGMENT);
87 assert_int_equal(match_keyword("base", strlen("base"), 0), YANG_BASE);
88 assert_int_equal(match_keyword("belongs-to", strlen("belongs-to"), 0), YANG_BELONGS_TO);
89 assert_int_equal(match_keyword("bit", strlen("bit"), 0), YANG_BIT);
90 assert_int_equal(match_keyword("case", strlen("case"), 0), YANG_CASE);
91 assert_int_equal(match_keyword("choice", strlen("choice"), 0), YANG_CHOICE);
92 assert_int_equal(match_keyword("config", strlen("config"), 0), YANG_CONFIG);
93 assert_int_equal(match_keyword("contact", strlen("contact"), 0), YANG_CONTACT);
94 assert_int_equal(match_keyword("container", strlen("container"), 0), YANG_CONTAINER);
95 assert_int_equal(match_keyword("default", strlen("default"), 0), YANG_DEFAULT);
96 assert_int_equal(match_keyword("description", strlen("description"), 0), YANG_DESCRIPTION);
97 assert_int_equal(match_keyword("deviate", strlen("deviate"), 0), YANG_DEVIATE);
98 assert_int_equal(match_keyword("deviation", strlen("deviation"), 0), YANG_DEVIATION);
99 assert_int_equal(match_keyword("enum", strlen("enum"), 0), YANG_ENUM);
100 assert_int_equal(match_keyword("error-app-tag", strlen("error-app-tag"), 0), YANG_ERROR_APP_TAG);
101 assert_int_equal(match_keyword("error-message", strlen("error-message"), 0), YANG_ERROR_MESSAGE);
102 assert_int_equal(match_keyword("extension", strlen("extension"), 0), YANG_EXTENSION);
103 assert_int_equal(match_keyword("feature", strlen("feature"), 0), YANG_FEATURE);
104 assert_int_equal(match_keyword("fraction-digits", strlen("fraction-digits"), 0), YANG_FRACTION_DIGITS);
105 assert_int_equal(match_keyword("grouping", strlen("grouping"), 0), YANG_GROUPING);
106 assert_int_equal(match_keyword("identity", strlen("identity"), 0), YANG_IDENTITY);
107 assert_int_equal(match_keyword("if-feature", strlen("if-feature"), 0), YANG_IF_FEATURE);
108 assert_int_equal(match_keyword("import", strlen("import"), 0), YANG_IMPORT);
109 assert_int_equal(match_keyword("include", strlen("include"), 0), YANG_INCLUDE);
110 assert_int_equal(match_keyword("input", strlen("input"), 0), YANG_INPUT);
111 assert_int_equal(match_keyword("key", strlen("key"), 0), YANG_KEY);
112 assert_int_equal(match_keyword("leaf", strlen("leaf"), 0), YANG_LEAF);
113 assert_int_equal(match_keyword("leaf-list", strlen("leaf-list"), 0), YANG_LEAF_LIST);
114 assert_int_equal(match_keyword("length", strlen("length"), 0), YANG_LENGTH);
115 assert_int_equal(match_keyword("list", strlen("list"), 0), YANG_LIST);
116 assert_int_equal(match_keyword("mandatory", strlen("mandatory"), 0), YANG_MANDATORY);
117 assert_int_equal(match_keyword("max-elements", strlen("max-elements"), 0), YANG_MAX_ELEMENTS);
118 assert_int_equal(match_keyword("min-elements", strlen("min-elements"), 0), YANG_MIN_ELEMENTS);
119 assert_int_equal(match_keyword("modifier", strlen("modifier"), 0), YANG_MODIFIER);
120 assert_int_equal(match_keyword("module", strlen("module"), 0), YANG_MODULE);
121 assert_int_equal(match_keyword("must", strlen("must"), 0), YANG_MUST);
122 assert_int_equal(match_keyword("namespace", strlen("namespace"), 0), YANG_NAMESPACE);
123 assert_int_equal(match_keyword("notification", strlen("notification"), 0), YANG_NOTIFICATION);
124 assert_int_equal(match_keyword("ordered-by", strlen("ordered-by"), 0), YANG_ORDERED_BY);
125 assert_int_equal(match_keyword("organization", strlen("organization"), 0), YANG_ORGANIZATION);
126 assert_int_equal(match_keyword("output", strlen("output"), 0), YANG_OUTPUT);
127 assert_int_equal(match_keyword("path", strlen("path"), 0), YANG_PATH);
128 assert_int_equal(match_keyword("pattern", strlen("pattern"), 0), YANG_PATTERN);
129 assert_int_equal(match_keyword("position", strlen("position"), 0), YANG_POSITION);
130 assert_int_equal(match_keyword("prefix", strlen("prefix"), 0), YANG_PREFIX);
131 assert_int_equal(match_keyword("presence", strlen("presence"), 0), YANG_PRESENCE);
132 assert_int_equal(match_keyword("range", strlen("range"), 0), YANG_RANGE);
133 assert_int_equal(match_keyword("reference", strlen("reference"), 0), YANG_REFERENCE);
134 assert_int_equal(match_keyword("refine", strlen("refine"), 0), YANG_REFINE);
135 assert_int_equal(match_keyword("require-instance", strlen("require-instance"), 0), YANG_REQUIRE_INSTANCE);
136 assert_int_equal(match_keyword("revision", strlen("revision"), 0), YANG_REVISION);
137 assert_int_equal(match_keyword("revision-date", strlen("revision-date"), 0), YANG_REVISION_DATE);
138 assert_int_equal(match_keyword("rpc", strlen("rpc"), 0), YANG_RPC);
139 assert_int_equal(match_keyword("status", strlen("status"), 0), YANG_STATUS);
140 assert_int_equal(match_keyword("submodule", strlen("submodule"), 0), YANG_SUBMODULE);
141 assert_int_equal(match_keyword("type", strlen("type"), 0), YANG_TYPE);
142 assert_int_equal(match_keyword("typedef", strlen("typedef"), 0), YANG_TYPEDEF);
143 assert_int_equal(match_keyword("unique", strlen("unique"), 0), YANG_UNIQUE);
144 assert_int_equal(match_keyword("units", strlen("units"), 0), YANG_UNITS);
145 assert_int_equal(match_keyword("uses", strlen("uses"), 0), YANG_USES);
146 assert_int_equal(match_keyword("value", strlen("value"), 0), YANG_VALUE);
147 assert_int_equal(match_keyword("when", strlen("when"), 0), YANG_WHEN);
148 assert_int_equal(match_keyword("yang-version", strlen("yang-version"), 0), YANG_YANG_VERSION);
149 assert_int_equal(match_keyword("yin-element", strlen("yin-element"), 0), YANG_YIN_ELEMENT);
David Sedlák872c7b42018-10-26 13:15:20 +0200150}
David Sedlák3b4db242018-10-19 16:11:01 +0200151
David Sedlák872c7b42018-10-26 13:15:20 +0200152static void
153test_match_argument(void **state)
154{
David Sedlák68a1af12019-03-08 13:46:54 +0100155 (void)state; /* unused */
David Sedlák872c7b42018-10-26 13:15:20 +0200156
David Sedláka7406952019-04-05 10:33:07 +0200157 assert_int_equal(match_argument_name("", 5), YIN_ARG_UNKNOWN);
158 assert_int_equal(match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN);
159 assert_int_equal(match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN);
David Sedlák872c7b42018-10-26 13:15:20 +0200160 assert_int_equal(match_argument_name("condition", 9), YIN_ARG_CONDITION);
161 assert_int_equal(match_argument_name("date", 4), YIN_ARG_DATE);
162 assert_int_equal(match_argument_name("module", 6), YIN_ARG_MODULE);
163 assert_int_equal(match_argument_name("name", 4), YIN_ARG_NAME);
164 assert_int_equal(match_argument_name("tag", 3), YIN_ARG_TAG);
165 assert_int_equal(match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
166 assert_int_equal(match_argument_name("text", 4), YIN_ARG_TEXT);
167 assert_int_equal(match_argument_name("uri", 3), YIN_ARG_URI);
168 assert_int_equal(match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200169}
170
David Sedlák68a1af12019-03-08 13:46:54 +0100171static void
172test_meta(void **state)
173{
174 LY_ERR ret = LY_SUCCESS;
175 struct state *st = *state;
176
177 ret = yin_parse_module(st->ctx,"<module name=\"example-foo\">\
David Sedláka7406952019-04-05 10:33:07 +0200178 <organization xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">organization...</organization>\
David Sedlák68a1af12019-03-08 13:46:54 +0100179 <contact>contact...</contact>\
180 <description>description...</description>\
181 <reference>reference...</reference>\
182 </module>", st->mod);
183
184 assert_int_equal(ret, LY_SUCCESS);
David Sedláka7406952019-04-05 10:33:07 +0200185 assert_string_equal(st->mod->parsed->mod->org, "organization...");
David Sedlák68a1af12019-03-08 13:46:54 +0100186 assert_string_equal(st->mod->parsed->mod->contact, "contact...");
187 assert_string_equal(st->mod->parsed->mod->dsc, "description...");
188 assert_string_equal(st->mod->parsed->mod->ref, "reference...");
189}
190
David Sedlák3b4db242018-10-19 16:11:01 +0200191int
192main(void)
193{
194
195 const struct CMUnitTest tests[] = {
David Sedlák68a1af12019-03-08 13:46:54 +0100196 cmocka_unit_test_setup_teardown(test_parse, setup_f, teardown_f),
197 cmocka_unit_test_setup_teardown(test_meta, setup_f, teardown_f),
David Sedlák3b4db242018-10-19 16:11:01 +0200198 cmocka_unit_test(test_match_keyword),
David Sedlák872c7b42018-10-26 13:15:20 +0200199 cmocka_unit_test(test_match_argument),
David Sedlák3b4db242018-10-19 16:11:01 +0200200 };
201
202 return cmocka_run_group_tests(tests, NULL, NULL);
203}