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; |
| 31 | struct lysp_module *mod; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 32 | |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 33 | ly_ctx_new(NULL, 0, &ctx); |
David Sedlák | e488991 | 2018-11-02 09:52:40 +0100 | [diff] [blame] | 34 | mod = calloc(1, sizeof(*mod)); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 35 | mod->ctx = ctx; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 36 | |
David Sedlák | 4a4c072 | 2018-11-26 17:03:10 +0100 | [diff] [blame] | 37 | 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ák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 62 | &mod); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 63 | |
| 64 | assert_string_equal(mod->name, "example-foo"); |
David Sedlák | a8eac08 | 2018-12-16 21:17:39 +0100 | [diff] [blame] | 65 | assert_string_equal(mod->prefix, "foo"); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 66 | |
David Sedlák | a8eac08 | 2018-12-16 21:17:39 +0100 | [diff] [blame] | 67 | lysp_module_free(mod); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 68 | ly_ctx_destroy(ctx, NULL); |
| 69 | } |
| 70 | |
| 71 | static int |
| 72 | test_setup(void **state) { |
| 73 | (void)state; /* unused */ |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static void |
| 78 | test_match_keyword(void **state) |
| 79 | { |
| 80 | (void)state; /* unused */ |
| 81 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 82 | 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ák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 153 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 154 | static void |
| 155 | test_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ák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | int |
| 174 | main(void) |
| 175 | { |
| 176 | |
| 177 | const struct CMUnitTest tests[] = { |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 178 | cmocka_unit_test_setup(test_parse, test_setup), |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 179 | cmocka_unit_test(test_match_keyword), |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 180 | cmocka_unit_test(test_match_argument), |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 184 | } |