blob: bcd9c83958d4ec4362c9b9610260ecefbeb3923d [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;
David Sedlák3017da42019-02-15 09:48:04 +010031 struct lys_module *mod;
32 LY_ERR ret = LY_SUCCESS;
David Sedlák872c7b42018-10-26 13:15:20 +020033
David Sedlák3b4db242018-10-19 16:11:01 +020034 ly_ctx_new(NULL, 0, &ctx);
David Sedláke4889912018-11-02 09:52:40 +010035 mod = calloc(1, sizeof(*mod));
David Sedlák872c7b42018-10-26 13:15:20 +020036 mod->ctx = ctx;
David Sedlák3b4db242018-10-19 16:11:01 +020037
David Sedlák3017da42019-02-15 09:48:04 +010038 ret = yin_parse_module(ctx, "<module name=\"example-foo\"\
David Sedlák4a4c0722018-11-26 17:03:10 +010039 xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\
40 xmlns:foo=\"urn:example:foo\"\
41 xmlns:myext=\"urn:example:extensions\">\
42 <namespace uri=\"urn:example:foo\"/>\
43 <prefix value=\"foo\"/>\
44 \
45 <import module=\"example-extensions\">\
46 <prefix value=\"myext\"/>\
47 </import>\
48 \
49 <list name=\"interface\">\
50 <key value=\"name\"/>\
51 <leaf name=\"name\">\
52 <type name=\"string\"/>\
53 </leaf>\
54 <leaf name=\"mtu\">\
55 <type name=\"uint32\"/>\
56 <description>\
57 <text>The MTU of the interface.</text>\
58 </description>\
59 <myext:c-define name=\"MY_MTU\"/>\
60 </leaf>\
61 </list>\
62 </module>",
David Sedlák3017da42019-02-15 09:48:04 +010063 mod);
David Sedlák872c7b42018-10-26 13:15:20 +020064
David Sedlák3017da42019-02-15 09:48:04 +010065 assert_int_equal(ret, LY_SUCCESS);
66 assert_string_equal(mod->parsed->mod->name, "example-foo");
67 assert_string_equal(mod->parsed->mod->prefix, "foo");
David Sedlák872c7b42018-10-26 13:15:20 +020068
David Sedlák3017da42019-02-15 09:48:04 +010069 lys_module_free(mod, NULL);
David Sedlák3b4db242018-10-19 16:11:01 +020070 ly_ctx_destroy(ctx, NULL);
71}
72
73static int
74test_setup(void **state) {
75 (void)state; /* unused */
76 return 0;
77}
78
79static void
80test_match_keyword(void **state)
81{
82 (void)state; /* unused */
83
David Sedlák92206122019-02-15 12:39:17 +010084 assert_int_equal(match_keyword("anydatax", strlen("anydatax")), YANG_NONE);
85 assert_int_equal(match_keyword("asdasd", strlen("asdasd")), YANG_NONE);
86 assert_int_equal(match_keyword("", 0), YANG_NONE);
87 assert_int_equal(match_keyword("anydata", strlen("anydata")), YANG_ANYDATA);
88 assert_int_equal(match_keyword("anyxml", strlen("anyxml")), YANG_ANYXML);
89 assert_int_equal(match_keyword("argument", strlen("argument")), YANG_ARGUMENT);
90 assert_int_equal(match_keyword("augment", strlen("augment")), YANG_AUGMENT);
91 assert_int_equal(match_keyword("base", strlen("base")), YANG_BASE);
92 assert_int_equal(match_keyword("belongs-to", strlen("belongs-to")), YANG_BELONGS_TO);
93 assert_int_equal(match_keyword("bit", strlen("bit")), YANG_BIT);
94 assert_int_equal(match_keyword("case", strlen("case")), YANG_CASE);
95 assert_int_equal(match_keyword("choice", strlen("choice")), YANG_CHOICE);
96 assert_int_equal(match_keyword("config", strlen("config")), YANG_CONFIG);
97 assert_int_equal(match_keyword("contact", strlen("contact")), YANG_CONTACT);
98 assert_int_equal(match_keyword("container", strlen("container")), YANG_CONTAINER);
99 assert_int_equal(match_keyword("default", strlen("default")), YANG_DEFAULT);
100 assert_int_equal(match_keyword("description", strlen("description")), YANG_DESCRIPTION);
101 assert_int_equal(match_keyword("deviate", strlen("deviate")), YANG_DEVIATE);
102 assert_int_equal(match_keyword("deviation", strlen("deviation")), YANG_DEVIATION);
103 assert_int_equal(match_keyword("enum", strlen("enum")), YANG_ENUM);
104 assert_int_equal(match_keyword("error-app-tag", strlen("error-app-tag")), YANG_ERROR_APP_TAG);
105 assert_int_equal(match_keyword("error-message", strlen("error-message")), YANG_ERROR_MESSAGE);
106 assert_int_equal(match_keyword("extension", strlen("extension")), YANG_EXTENSION);
107 assert_int_equal(match_keyword("feature", strlen("feature")), YANG_FEATURE);
108 assert_int_equal(match_keyword("fraction-digits", strlen("fraction-digits")), YANG_FRACTION_DIGITS);
109 assert_int_equal(match_keyword("grouping", strlen("grouping")), YANG_GROUPING);
110 assert_int_equal(match_keyword("identity", strlen("identity")), YANG_IDENTITY);
111 assert_int_equal(match_keyword("if-feature", strlen("if-feature")), YANG_IF_FEATURE);
112 assert_int_equal(match_keyword("import", strlen("import")), YANG_IMPORT);
113 assert_int_equal(match_keyword("include", strlen("include")), YANG_INCLUDE);
114 assert_int_equal(match_keyword("input", strlen("input")), YANG_INPUT);
115 assert_int_equal(match_keyword("key", strlen("key")), YANG_KEY);
116 assert_int_equal(match_keyword("leaf", strlen("leaf")), YANG_LEAF);
117 assert_int_equal(match_keyword("leaf-list", strlen("leaf-list")), YANG_LEAF_LIST);
118 assert_int_equal(match_keyword("length", strlen("length")), YANG_LENGTH);
119 assert_int_equal(match_keyword("list", strlen("list")), YANG_LIST);
120 assert_int_equal(match_keyword("mandatory", strlen("mandatory")), YANG_MANDATORY);
121 assert_int_equal(match_keyword("max-elements", strlen("max-elements")), YANG_MAX_ELEMENTS);
122 assert_int_equal(match_keyword("min-elements", strlen("min-elements")), YANG_MIN_ELEMENTS);
123 assert_int_equal(match_keyword("modifier", strlen("modifier")), YANG_MODIFIER);
124 assert_int_equal(match_keyword("module", strlen("module")), YANG_MODULE);
125 assert_int_equal(match_keyword("must", strlen("must")), YANG_MUST);
126 assert_int_equal(match_keyword("namespace", strlen("namespace")), YANG_NAMESPACE);
127 assert_int_equal(match_keyword("notification", strlen("notification")), YANG_NOTIFICATION);
128 assert_int_equal(match_keyword("ordered-by", strlen("ordered-by")), YANG_ORDERED_BY);
129 assert_int_equal(match_keyword("organization", strlen("organization")), YANG_ORGANIZATION);
130 assert_int_equal(match_keyword("output", strlen("output")), YANG_OUTPUT);
131 assert_int_equal(match_keyword("path", strlen("path")), YANG_PATH);
132 assert_int_equal(match_keyword("pattern", strlen("pattern")), YANG_PATTERN);
133 assert_int_equal(match_keyword("position", strlen("position")), YANG_POSITION);
134 assert_int_equal(match_keyword("prefix", strlen("prefix")), YANG_PREFIX);
135 assert_int_equal(match_keyword("presence", strlen("presence")), YANG_PRESENCE);
136 assert_int_equal(match_keyword("range", strlen("range")), YANG_RANGE);
137 assert_int_equal(match_keyword("reference", strlen("reference")), YANG_REFERENCE);
138 assert_int_equal(match_keyword("refine", strlen("refine")), YANG_REFINE);
139 assert_int_equal(match_keyword("require-instance", strlen("require-instance")), YANG_REQUIRE_INSTANCE);
140 assert_int_equal(match_keyword("revision", strlen("revision")), YANG_REVISION);
141 assert_int_equal(match_keyword("revision-date", strlen("revision-date")), YANG_REVISION_DATE);
142 assert_int_equal(match_keyword("rpc", strlen("rpc")), YANG_RPC);
143 assert_int_equal(match_keyword("status", strlen("status")), YANG_STATUS);
144 assert_int_equal(match_keyword("submodule", strlen("submodule")), YANG_SUBMODULE);
145 assert_int_equal(match_keyword("type", strlen("type")), YANG_TYPE);
146 assert_int_equal(match_keyword("typedef", strlen("typedef")), YANG_TYPEDEF);
147 assert_int_equal(match_keyword("unique", strlen("unique")), YANG_UNIQUE);
148 assert_int_equal(match_keyword("units", strlen("units")), YANG_UNITS);
149 assert_int_equal(match_keyword("uses", strlen("uses")), YANG_USES);
150 assert_int_equal(match_keyword("value", strlen("value")), YANG_VALUE);
151 assert_int_equal(match_keyword("when", strlen("when")), YANG_WHEN);
152 assert_int_equal(match_keyword("yang-version", strlen("yang-version")), YANG_YANG_VERSION);
153 assert_int_equal(match_keyword("yin-element", strlen("yin-element")), YANG_YIN_ELEMENT);
David Sedlák872c7b42018-10-26 13:15:20 +0200154}
David Sedlák3b4db242018-10-19 16:11:01 +0200155
David Sedlák872c7b42018-10-26 13:15:20 +0200156static void
157test_match_argument(void **state)
158{
159 (void)state;
160
161 assert_int_equal(match_argument_name("", 5), YIN_ARG_NONE);
162 assert_int_equal(match_argument_name("qwertyasd", 5), YIN_ARG_NONE);
163 assert_int_equal(match_argument_name("conditionasd", 8), YIN_ARG_NONE);
164 assert_int_equal(match_argument_name("condition", 9), YIN_ARG_CONDITION);
165 assert_int_equal(match_argument_name("date", 4), YIN_ARG_DATE);
166 assert_int_equal(match_argument_name("module", 6), YIN_ARG_MODULE);
167 assert_int_equal(match_argument_name("name", 4), YIN_ARG_NAME);
168 assert_int_equal(match_argument_name("tag", 3), YIN_ARG_TAG);
169 assert_int_equal(match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
170 assert_int_equal(match_argument_name("text", 4), YIN_ARG_TEXT);
171 assert_int_equal(match_argument_name("uri", 3), YIN_ARG_URI);
172 assert_int_equal(match_argument_name("value", 5), YIN_ARG_VALUE);
David Sedlák3b4db242018-10-19 16:11:01 +0200173}
174
175int
176main(void)
177{
178
179 const struct CMUnitTest tests[] = {
David Sedlák872c7b42018-10-26 13:15:20 +0200180 cmocka_unit_test_setup(test_parse, test_setup),
David Sedlák3b4db242018-10-19 16:11:01 +0200181 cmocka_unit_test(test_match_keyword),
David Sedlák872c7b42018-10-26 13:15:20 +0200182 cmocka_unit_test(test_match_argument),
David Sedlák3b4db242018-10-19 16:11:01 +0200183 };
184
185 return cmocka_run_group_tests(tests, NULL, NULL);
186}