blob: 870e5b47d6942efa0e02e6581f3ae38f1e389382 [file] [log] [blame]
David Sedlák18e494b2018-12-17 03:58:39 +01001#include "../../src/common.c"
2#include "../../src/set.c"
3#include "../../src/log.c"
4#include "../../src/hash_table.c"
5#include "../../src/xpath.c"
6#include "../../src/parser_yang.c"
7#include "../../src/context.c"
8#include "../../src/tree_schema_helpers.c"
9#include "../../src/tree_schema_free.c"
10#include "../../src/tree_schema_compile.c"
11#include "../../src/tree_schema.c"
12#include "../../src/xml.c"
13#include "../../src/parser_yin.c"
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>
22
23#include "libyang.h"
David Sedlák3b4db242018-10-19 16:11:01 +020024
David Sedlák872c7b42018-10-26 13:15:20 +020025static void
26test_parse(void **state)
27{
28 (void)state; /* unused */
29
David Sedlák3b4db242018-10-19 16:11:01 +020030 struct ly_ctx *ctx;
31 struct lysp_module *mod;
David Sedlák872c7b42018-10-26 13:15:20 +020032
David Sedlák3b4db242018-10-19 16:11:01 +020033 ly_ctx_new(NULL, 0, &ctx);
David Sedláke4889912018-11-02 09:52:40 +010034 mod = calloc(1, sizeof(*mod));
David Sedlák872c7b42018-10-26 13:15:20 +020035 mod->ctx = ctx;
David Sedlák3b4db242018-10-19 16:11:01 +020036
David Sedlák4a4c0722018-11-26 17:03:10 +010037 yin_parse(ctx, "<module name=\"example-foo\"\
38 xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\
39 xmlns:foo=\"urn:example:foo\"\
40 xmlns:myext=\"urn:example:extensions\">\
41 <namespace uri=\"urn:example:foo\"/>\
42 <prefix value=\"foo\"/>\
43 \
44 <import module=\"example-extensions\">\
45 <prefix value=\"myext\"/>\
46 </import>\
47 \
48 <list name=\"interface\">\
49 <key value=\"name\"/>\
50 <leaf name=\"name\">\
51 <type name=\"string\"/>\
52 </leaf>\
53 <leaf name=\"mtu\">\
54 <type name=\"uint32\"/>\
55 <description>\
56 <text>The MTU of the interface.</text>\
57 </description>\
58 <myext:c-define name=\"MY_MTU\"/>\
59 </leaf>\
60 </list>\
61 </module>",
David Sedlák3b4db242018-10-19 16:11:01 +020062 &mod);
David Sedlák872c7b42018-10-26 13:15:20 +020063
64 assert_string_equal(mod->name, "example-foo");
David Sedláka8eac082018-12-16 21:17:39 +010065 assert_string_equal(mod->prefix, "foo");
David Sedlák872c7b42018-10-26 13:15:20 +020066
David Sedláka8eac082018-12-16 21:17:39 +010067 lysp_module_free(mod);
David Sedlák3b4db242018-10-19 16:11:01 +020068 ly_ctx_destroy(ctx, NULL);
69}
70
71static int
72test_setup(void **state) {
73 (void)state; /* unused */
74 return 0;
75}
76
77static void
78test_match_keyword(void **state)
79{
80 (void)state; /* unused */
81
David Sedlák872c7b42018-10-26 13:15:20 +020082 assert_int_equal(match_keyword("anydata"), YANG_ANYDATA);
83 assert_int_equal(match_keyword("asdasd"), YANG_NONE);
84 assert_int_equal(match_keyword(""), YANG_NONE);
85 assert_int_equal(match_keyword("anydata"), YANG_ANYDATA);
86 assert_int_equal(match_keyword("anyxml"), YANG_ANYXML);
87 assert_int_equal(match_keyword("argument"), YANG_ARGUMENT);
88 assert_int_equal(match_keyword("augment"), YANG_AUGMENT);
89 assert_int_equal(match_keyword("base"), YANG_BASE);
90 assert_int_equal(match_keyword("belongs-to"), YANG_BELONGS_TO);
91 assert_int_equal(match_keyword("bit"), YANG_BIT);
92 assert_int_equal(match_keyword("case"), YANG_CASE);
93 assert_int_equal(match_keyword("choice"), YANG_CHOICE);
94 assert_int_equal(match_keyword("config"), YANG_CONFIG);
95 assert_int_equal(match_keyword("contact"), YANG_CONTACT);
96 assert_int_equal(match_keyword("container"), YANG_CONTAINER);
97 assert_int_equal(match_keyword("default"), YANG_DEFAULT);
98 assert_int_equal(match_keyword("description"), YANG_DESCRIPTION);
99 assert_int_equal(match_keyword("deviate"), YANG_DEVIATE);
100 assert_int_equal(match_keyword("deviation"), YANG_DEVIATION);
101 assert_int_equal(match_keyword("enum"), YANG_ENUM);
102 assert_int_equal(match_keyword("error-app-tag"), YANG_ERROR_APP_TAG);
103 assert_int_equal(match_keyword("error-message"), YANG_ERROR_MESSAGE);
104 assert_int_equal(match_keyword("extension"), YANG_EXTENSION);
105 assert_int_equal(match_keyword("feature"), YANG_FEATURE);
106 assert_int_equal(match_keyword("fraction-digits"), YANG_FRACTION_DIGITS);
107 assert_int_equal(match_keyword("grouping"), YANG_GROUPING);
108 assert_int_equal(match_keyword("identity"), YANG_IDENTITY);
109 assert_int_equal(match_keyword("if-feature"), YANG_IF_FEATURE);
110 assert_int_equal(match_keyword("import"), YANG_IMPORT);
111 assert_int_equal(match_keyword("include"), YANG_INCLUDE);
112 assert_int_equal(match_keyword("input"), YANG_INPUT);
113 assert_int_equal(match_keyword("key"), YANG_KEY);
114 assert_int_equal(match_keyword("leaf"), YANG_LEAF);
115 assert_int_equal(match_keyword("leaf-list"), YANG_LEAF_LIST);
116 assert_int_equal(match_keyword("length"), YANG_LENGTH);
117 assert_int_equal(match_keyword("list"), YANG_LIST);
118 assert_int_equal(match_keyword("mandatory"), YANG_MANDATORY);
119 assert_int_equal(match_keyword("max-elements"), YANG_MAX_ELEMENTS);
120 assert_int_equal(match_keyword("min-elements"), YANG_MIN_ELEMENTS);
121 assert_int_equal(match_keyword("modifier"), YANG_MODIFIER);
122 assert_int_equal(match_keyword("module"), YANG_MODULE);
123 assert_int_equal(match_keyword("must"), YANG_MUST);
124 assert_int_equal(match_keyword("namespace"), YANG_NAMESPACE);
125 assert_int_equal(match_keyword("notification"), YANG_NOTIFICATION);
126 assert_int_equal(match_keyword("ordered-by"), YANG_ORDERED_BY);
127 assert_int_equal(match_keyword("organization"), YANG_ORGANIZATION);
128 assert_int_equal(match_keyword("output"), YANG_OUTPUT);
129 assert_int_equal(match_keyword("path"), YANG_PATH);
130 assert_int_equal(match_keyword("pattern"), YANG_PATTERN);
131 assert_int_equal(match_keyword("position"), YANG_POSITION);
132 assert_int_equal(match_keyword("prefix"), YANG_PREFIX);
133 assert_int_equal(match_keyword("presence"), YANG_PRESENCE);
134 assert_int_equal(match_keyword("range"), YANG_RANGE);
135 assert_int_equal(match_keyword("reference"), YANG_REFERENCE);
136 assert_int_equal(match_keyword("refine"), YANG_REFINE);
137 assert_int_equal(match_keyword("require-instance"), YANG_REQUIRE_INSTANCE);
138 assert_int_equal(match_keyword("revision"), YANG_REVISION);
139 assert_int_equal(match_keyword("revision-date"), YANG_REVISION_DATE);
140 assert_int_equal(match_keyword("rpc"), YANG_RPC);
141 assert_int_equal(match_keyword("status"), YANG_STATUS);
142 assert_int_equal(match_keyword("submodule"), YANG_SUBMODULE);
143 assert_int_equal(match_keyword("type"), YANG_TYPE);
144 assert_int_equal(match_keyword("typedef"), YANG_TYPEDEF);
145 assert_int_equal(match_keyword("unique"), YANG_UNIQUE);
146 assert_int_equal(match_keyword("units"), YANG_UNITS);
147 assert_int_equal(match_keyword("uses"), YANG_USES);
148 assert_int_equal(match_keyword("value"), YANG_VALUE);
149 assert_int_equal(match_keyword("when"), YANG_WHEN);
150 assert_int_equal(match_keyword("yang-version"), YANG_YANG_VERSION);
151 assert_int_equal(match_keyword("yin-element"), YANG_YIN_ELEMENT);
152}
David Sedlák3b4db242018-10-19 16:11:01 +0200153
David Sedlák872c7b42018-10-26 13:15:20 +0200154static void
155test_match_argument(void **state)
156{
157 (void)state;
158
159 assert_int_equal(match_argument_name("", 5), YIN_ARG_NONE);
160 assert_int_equal(match_argument_name("qwertyasd", 5), YIN_ARG_NONE);
161 assert_int_equal(match_argument_name("conditionasd", 8), YIN_ARG_NONE);
162 assert_int_equal(match_argument_name("condition", 9), YIN_ARG_CONDITION);
163 assert_int_equal(match_argument_name("date", 4), YIN_ARG_DATE);
164 assert_int_equal(match_argument_name("module", 6), YIN_ARG_MODULE);
165 assert_int_equal(match_argument_name("name", 4), YIN_ARG_NAME);
166 assert_int_equal(match_argument_name("tag", 3), YIN_ARG_TAG);
167 assert_int_equal(match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
168 assert_int_equal(match_argument_name("text", 4), YIN_ARG_TEXT);
169 assert_int_equal(match_argument_name("uri", 3), YIN_ARG_URI);
170 assert_int_equal(match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200171}
172
173int
174main(void)
175{
176
177 const struct CMUnitTest tests[] = {
David Sedlák872c7b42018-10-26 13:15:20 +0200178 cmocka_unit_test_setup(test_parse, test_setup),
David Sedlák3b4db242018-10-19 16:11:01 +0200179 cmocka_unit_test(test_match_keyword),
David Sedlák872c7b42018-10-26 13:15:20 +0200180 cmocka_unit_test(test_match_argument),
David Sedlák3b4db242018-10-19 16:11:01 +0200181 };
182
183 return cmocka_run_group_tests(tests, NULL, NULL);
184}