David Sedlák | 18e494b | 2018-12-17 03:58:39 +0100 | [diff] [blame] | 1 | #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ák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 15 | #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ák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 24 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 25 | static void |
| 26 | test_parse(void **state) |
| 27 | { |
| 28 | (void)state; /* unused */ |
| 29 | |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 30 | struct ly_ctx *ctx; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 31 | struct lys_module *mod; |
| 32 | LY_ERR ret = LY_SUCCESS; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 33 | |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 34 | ly_ctx_new(NULL, 0, &ctx); |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 35 | mod = calloc(1, sizeof(*mod)); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 36 | mod->ctx = ctx; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 37 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 38 | ret = yin_parse_module(ctx, "<module name=\"example-foo\"\ |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 39 | 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ák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 63 | mod); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 64 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 65 | 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ák | 736fd0d | 2019-02-15 16:06:31 +0100 | [diff] [blame] | 68 | assert_string_equal(mod->parsed->imports->name, "example-extensions"); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 69 | |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 70 | lys_module_free(mod, NULL); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 71 | ly_ctx_destroy(ctx, NULL); |
| 72 | } |
| 73 | |
| 74 | static int |
| 75 | test_setup(void **state) { |
| 76 | (void)state; /* unused */ |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static void |
| 81 | test_match_keyword(void **state) |
| 82 | { |
| 83 | (void)state; /* unused */ |
| 84 | |
David Sedlák | 9220612 | 2019-02-15 12:39:17 +0100 | [diff] [blame] | 85 | assert_int_equal(match_keyword("anydatax", strlen("anydatax")), YANG_NONE); |
| 86 | assert_int_equal(match_keyword("asdasd", strlen("asdasd")), YANG_NONE); |
| 87 | assert_int_equal(match_keyword("", 0), YANG_NONE); |
| 88 | assert_int_equal(match_keyword("anydata", strlen("anydata")), YANG_ANYDATA); |
| 89 | assert_int_equal(match_keyword("anyxml", strlen("anyxml")), YANG_ANYXML); |
| 90 | assert_int_equal(match_keyword("argument", strlen("argument")), YANG_ARGUMENT); |
| 91 | assert_int_equal(match_keyword("augment", strlen("augment")), YANG_AUGMENT); |
| 92 | assert_int_equal(match_keyword("base", strlen("base")), YANG_BASE); |
| 93 | assert_int_equal(match_keyword("belongs-to", strlen("belongs-to")), YANG_BELONGS_TO); |
| 94 | assert_int_equal(match_keyword("bit", strlen("bit")), YANG_BIT); |
| 95 | assert_int_equal(match_keyword("case", strlen("case")), YANG_CASE); |
| 96 | assert_int_equal(match_keyword("choice", strlen("choice")), YANG_CHOICE); |
| 97 | assert_int_equal(match_keyword("config", strlen("config")), YANG_CONFIG); |
| 98 | assert_int_equal(match_keyword("contact", strlen("contact")), YANG_CONTACT); |
| 99 | assert_int_equal(match_keyword("container", strlen("container")), YANG_CONTAINER); |
| 100 | assert_int_equal(match_keyword("default", strlen("default")), YANG_DEFAULT); |
| 101 | assert_int_equal(match_keyword("description", strlen("description")), YANG_DESCRIPTION); |
| 102 | assert_int_equal(match_keyword("deviate", strlen("deviate")), YANG_DEVIATE); |
| 103 | assert_int_equal(match_keyword("deviation", strlen("deviation")), YANG_DEVIATION); |
| 104 | assert_int_equal(match_keyword("enum", strlen("enum")), YANG_ENUM); |
| 105 | assert_int_equal(match_keyword("error-app-tag", strlen("error-app-tag")), YANG_ERROR_APP_TAG); |
| 106 | assert_int_equal(match_keyword("error-message", strlen("error-message")), YANG_ERROR_MESSAGE); |
| 107 | assert_int_equal(match_keyword("extension", strlen("extension")), YANG_EXTENSION); |
| 108 | assert_int_equal(match_keyword("feature", strlen("feature")), YANG_FEATURE); |
| 109 | assert_int_equal(match_keyword("fraction-digits", strlen("fraction-digits")), YANG_FRACTION_DIGITS); |
| 110 | assert_int_equal(match_keyword("grouping", strlen("grouping")), YANG_GROUPING); |
| 111 | assert_int_equal(match_keyword("identity", strlen("identity")), YANG_IDENTITY); |
| 112 | assert_int_equal(match_keyword("if-feature", strlen("if-feature")), YANG_IF_FEATURE); |
| 113 | assert_int_equal(match_keyword("import", strlen("import")), YANG_IMPORT); |
| 114 | assert_int_equal(match_keyword("include", strlen("include")), YANG_INCLUDE); |
| 115 | assert_int_equal(match_keyword("input", strlen("input")), YANG_INPUT); |
| 116 | assert_int_equal(match_keyword("key", strlen("key")), YANG_KEY); |
| 117 | assert_int_equal(match_keyword("leaf", strlen("leaf")), YANG_LEAF); |
| 118 | assert_int_equal(match_keyword("leaf-list", strlen("leaf-list")), YANG_LEAF_LIST); |
| 119 | assert_int_equal(match_keyword("length", strlen("length")), YANG_LENGTH); |
| 120 | assert_int_equal(match_keyword("list", strlen("list")), YANG_LIST); |
| 121 | assert_int_equal(match_keyword("mandatory", strlen("mandatory")), YANG_MANDATORY); |
| 122 | assert_int_equal(match_keyword("max-elements", strlen("max-elements")), YANG_MAX_ELEMENTS); |
| 123 | assert_int_equal(match_keyword("min-elements", strlen("min-elements")), YANG_MIN_ELEMENTS); |
| 124 | assert_int_equal(match_keyword("modifier", strlen("modifier")), YANG_MODIFIER); |
| 125 | assert_int_equal(match_keyword("module", strlen("module")), YANG_MODULE); |
| 126 | assert_int_equal(match_keyword("must", strlen("must")), YANG_MUST); |
| 127 | assert_int_equal(match_keyword("namespace", strlen("namespace")), YANG_NAMESPACE); |
| 128 | assert_int_equal(match_keyword("notification", strlen("notification")), YANG_NOTIFICATION); |
| 129 | assert_int_equal(match_keyword("ordered-by", strlen("ordered-by")), YANG_ORDERED_BY); |
| 130 | assert_int_equal(match_keyword("organization", strlen("organization")), YANG_ORGANIZATION); |
| 131 | assert_int_equal(match_keyword("output", strlen("output")), YANG_OUTPUT); |
| 132 | assert_int_equal(match_keyword("path", strlen("path")), YANG_PATH); |
| 133 | assert_int_equal(match_keyword("pattern", strlen("pattern")), YANG_PATTERN); |
| 134 | assert_int_equal(match_keyword("position", strlen("position")), YANG_POSITION); |
| 135 | assert_int_equal(match_keyword("prefix", strlen("prefix")), YANG_PREFIX); |
| 136 | assert_int_equal(match_keyword("presence", strlen("presence")), YANG_PRESENCE); |
| 137 | assert_int_equal(match_keyword("range", strlen("range")), YANG_RANGE); |
| 138 | assert_int_equal(match_keyword("reference", strlen("reference")), YANG_REFERENCE); |
| 139 | assert_int_equal(match_keyword("refine", strlen("refine")), YANG_REFINE); |
| 140 | assert_int_equal(match_keyword("require-instance", strlen("require-instance")), YANG_REQUIRE_INSTANCE); |
| 141 | assert_int_equal(match_keyword("revision", strlen("revision")), YANG_REVISION); |
| 142 | assert_int_equal(match_keyword("revision-date", strlen("revision-date")), YANG_REVISION_DATE); |
| 143 | assert_int_equal(match_keyword("rpc", strlen("rpc")), YANG_RPC); |
| 144 | assert_int_equal(match_keyword("status", strlen("status")), YANG_STATUS); |
| 145 | assert_int_equal(match_keyword("submodule", strlen("submodule")), YANG_SUBMODULE); |
| 146 | assert_int_equal(match_keyword("type", strlen("type")), YANG_TYPE); |
| 147 | assert_int_equal(match_keyword("typedef", strlen("typedef")), YANG_TYPEDEF); |
| 148 | assert_int_equal(match_keyword("unique", strlen("unique")), YANG_UNIQUE); |
| 149 | assert_int_equal(match_keyword("units", strlen("units")), YANG_UNITS); |
| 150 | assert_int_equal(match_keyword("uses", strlen("uses")), YANG_USES); |
| 151 | assert_int_equal(match_keyword("value", strlen("value")), YANG_VALUE); |
| 152 | assert_int_equal(match_keyword("when", strlen("when")), YANG_WHEN); |
| 153 | assert_int_equal(match_keyword("yang-version", strlen("yang-version")), YANG_YANG_VERSION); |
| 154 | assert_int_equal(match_keyword("yin-element", strlen("yin-element")), YANG_YIN_ELEMENT); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 155 | } |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 156 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 157 | static void |
| 158 | test_match_argument(void **state) |
| 159 | { |
| 160 | (void)state; |
| 161 | |
| 162 | assert_int_equal(match_argument_name("", 5), YIN_ARG_NONE); |
| 163 | assert_int_equal(match_argument_name("qwertyasd", 5), YIN_ARG_NONE); |
| 164 | assert_int_equal(match_argument_name("conditionasd", 8), YIN_ARG_NONE); |
| 165 | assert_int_equal(match_argument_name("condition", 9), YIN_ARG_CONDITION); |
| 166 | assert_int_equal(match_argument_name("date", 4), YIN_ARG_DATE); |
| 167 | assert_int_equal(match_argument_name("module", 6), YIN_ARG_MODULE); |
| 168 | assert_int_equal(match_argument_name("name", 4), YIN_ARG_NAME); |
| 169 | assert_int_equal(match_argument_name("tag", 3), YIN_ARG_TAG); |
| 170 | assert_int_equal(match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE); |
| 171 | assert_int_equal(match_argument_name("text", 4), YIN_ARG_TEXT); |
| 172 | assert_int_equal(match_argument_name("uri", 3), YIN_ARG_URI); |
| 173 | assert_int_equal(match_argument_name("value", 5), YIN_ARG_VALUE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | int |
| 177 | main(void) |
| 178 | { |
| 179 | |
| 180 | const struct CMUnitTest tests[] = { |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 181 | cmocka_unit_test_setup(test_parse, test_setup), |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 182 | cmocka_unit_test(test_match_keyword), |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 183 | cmocka_unit_test(test_match_argument), |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 187 | } |