David Sedlák | b1ce3f8 | 2019-06-05 14:37:26 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file test_parser_yin.c |
| 3 | * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz> |
| 4 | * @brief unit tests for functions from parser_yin.c |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2019 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 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> |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 22 | #include <stdbool.h> |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 23 | |
David Sedlák | ecf5eb8 | 2019-06-03 14:12:44 +0200 | [diff] [blame] | 24 | #include "../../src/common.h" |
| 25 | #include "../../src/tree_schema.h" |
| 26 | #include "../../src/tree_schema_internal.h" |
| 27 | #include "../../src/parser_yin.h" |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 28 | #include "../../src/xml.h" |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 29 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 30 | struct state { |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 31 | struct ly_ctx *ctx; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 32 | struct lys_module *mod; |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 33 | struct lyxml_context *xml_ctx; |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 34 | bool finished_correctly; |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 35 | }; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 36 | |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 37 | #define BUFSIZE 1024 |
| 38 | char logbuf[BUFSIZE] = {0}; |
| 39 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
| 40 | |
| 41 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 42 | #define ENABLE_LOGGER_CHECKING 1 |
| 43 | |
| 44 | #if ENABLE_LOGGER_CHECKING |
| 45 | static void |
| 46 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 47 | { |
| 48 | (void) level; /* unused */ |
| 49 | if (store) { |
| 50 | if (path && path[0]) { |
| 51 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 52 | } else { |
| 53 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 54 | } |
| 55 | if (store > 0) { |
| 56 | --store; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | #endif |
| 61 | |
| 62 | #if ENABLE_LOGGER_CHECKING |
| 63 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 64 | #else |
| 65 | # define logbuf_assert(str) |
| 66 | #endif |
| 67 | |
| 68 | #define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \ |
| 69 | str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 70 | assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \ |
| 71 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \ |
| 72 | CLEANUP |
| 73 | |
| 74 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 75 | static int |
| 76 | setup_f(void **state) |
| 77 | { |
| 78 | struct state *st = NULL; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 79 | |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 80 | #if ENABLE_LOGGER_CHECKING |
| 81 | /* setup logger */ |
| 82 | ly_set_log_clb(logger, 1); |
| 83 | #endif |
| 84 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 85 | /* allocate state variable */ |
| 86 | (*state) = st = calloc(1, sizeof(*st)); |
| 87 | if (!st) { |
| 88 | fprintf(stderr, "Memmory allocation failed"); |
| 89 | return EXIT_FAILURE; |
| 90 | } |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 91 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 92 | /* create new libyang context */ |
| 93 | ly_ctx_new(NULL, 0, &st->ctx); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 94 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 95 | /* allocate new module */ |
| 96 | st->mod = calloc(1, sizeof(*st->mod)); |
| 97 | st->mod->ctx = st->ctx; |
| 98 | |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 99 | st->xml_ctx = calloc(1, sizeof(struct lys_parser_ctx)); |
| 100 | st->xml_ctx->ctx = st->ctx; |
| 101 | st->xml_ctx->line = 1; |
| 102 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 103 | return EXIT_SUCCESS; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static int |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 107 | teardown_f(void **state) |
| 108 | { |
| 109 | struct state *st = *(struct state **)state; |
| 110 | |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 111 | #if ENABLE_LOGGER_CHECKING |
| 112 | /* teardown logger */ |
| 113 | if (!st->finished_correctly && logbuf[0] != '\0') { |
| 114 | fprintf(stderr, "%s\n", logbuf); |
| 115 | } |
| 116 | #endif |
| 117 | |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 118 | lyxml_context_clear(st->xml_ctx); |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 119 | lys_module_free(st->mod, NULL); |
| 120 | ly_ctx_destroy(st->ctx, NULL); |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 121 | free(st->xml_ctx); |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 122 | free(st); |
| 123 | |
| 124 | return EXIT_SUCCESS; |
| 125 | } |
| 126 | |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 127 | static struct state* |
| 128 | reset_state(void **state) |
| 129 | { |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 130 | ((struct state *)*state)->finished_correctly = true; |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 131 | teardown_f(state); |
| 132 | setup_f(state); |
| 133 | |
| 134 | return *state; |
| 135 | } |
| 136 | |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 137 | void |
| 138 | logbuf_clean(void) |
| 139 | { |
| 140 | logbuf[0] = '\0'; |
| 141 | } |
| 142 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 143 | static void |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 144 | test_yin_parse_module(void **state) |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 145 | { |
| 146 | LY_ERR ret = LY_SUCCESS; |
| 147 | struct state *st = *state; |
| 148 | |
| 149 | ret = yin_parse_module(st->ctx, |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 150 | "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\ |
| 151 | name=\"example-foo\"\ |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 152 | xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\ |
| 153 | xmlns:foo=\"urn:example:foo\"\ |
| 154 | xmlns:myext=\"urn:example:extensions\">\ |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 155 | <namespace uri=\"urn:example:foo\" xmlns:myext=\"urn:example:extensions\"/>\ |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 156 | <prefix xmlns:myxt=\"urn:emple:extensions\" value=\"foo\" xmlns:myext=\"urn:example:extensions\"/>\ |
David Sedlák | d9d3a31 | 2019-06-04 09:47:10 +0200 | [diff] [blame] | 157 | </module>", |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 158 | st->mod); |
| 159 | |
| 160 | assert_int_equal(ret, LY_SUCCESS); |
| 161 | assert_string_equal(st->mod->parsed->mod->name, "example-foo"); |
| 162 | assert_string_equal(st->mod->parsed->mod->prefix, "foo"); |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 163 | assert_string_equal(st->mod->parsed->mod->ns, "urn:example:foo"); |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 164 | |
| 165 | st = reset_state(state); |
| 166 | ret = yin_parse_module(st->ctx, |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 167 | "<module name=\"example-foo\">\ |
| 168 | <invalid-tag uri=\"urn:example:foo\"\"/>\ |
| 169 | </module>", |
| 170 | st->mod); |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 171 | assert_int_equal(ret, LY_EVALID); |
| 172 | |
| 173 | st = reset_state(state); |
| 174 | ret = yin_parse_module(st->ctx, |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 175 | "<module>\ |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 176 | </module>", |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 177 | st->mod); |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 178 | assert_int_equal(ret, LY_EVALID); |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 179 | logbuf_assert("Missing argument name of a module Line number 1."); |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 180 | |
| 181 | st = reset_state(state); |
| 182 | ret = yin_parse_module(st->ctx, |
| 183 | "", |
| 184 | st->mod); |
| 185 | assert_int_equal(ret, LY_EVALID); |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 186 | logbuf_assert("Invalid keyword \"(null)\", expected \"module\" or \"submodule\". Line number 1."); |
| 187 | st->finished_correctly = true; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static void |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame^] | 191 | test_yin_match_keyword(void **state) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 192 | { |
| 193 | (void)state; /* unused */ |
| 194 | |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame^] | 195 | assert_int_equal(yin_match_keyword("anydatax", strlen("anydatax"), 0), YANG_NONE); |
| 196 | assert_int_equal(yin_match_keyword("asdasd", strlen("asdasd"), 0), YANG_NONE); |
| 197 | assert_int_equal(yin_match_keyword("", 0, 0), YANG_NONE); |
| 198 | assert_int_equal(yin_match_keyword("anydata", strlen("anydata"), 0), YANG_ANYDATA); |
| 199 | assert_int_equal(yin_match_keyword("anyxml", strlen("anyxml"), 0), YANG_ANYXML); |
| 200 | assert_int_equal(yin_match_keyword("argument", strlen("argument"), 0), YANG_ARGUMENT); |
| 201 | assert_int_equal(yin_match_keyword("augment", strlen("augment"), 0), YANG_AUGMENT); |
| 202 | assert_int_equal(yin_match_keyword("base", strlen("base"), 0), YANG_BASE); |
| 203 | assert_int_equal(yin_match_keyword("belongs-to", strlen("belongs-to"), 0), YANG_BELONGS_TO); |
| 204 | assert_int_equal(yin_match_keyword("bit", strlen("bit"), 0), YANG_BIT); |
| 205 | assert_int_equal(yin_match_keyword("case", strlen("case"), 0), YANG_CASE); |
| 206 | assert_int_equal(yin_match_keyword("choice", strlen("choice"), 0), YANG_CHOICE); |
| 207 | assert_int_equal(yin_match_keyword("config", strlen("config"), 0), YANG_CONFIG); |
| 208 | assert_int_equal(yin_match_keyword("contact", strlen("contact"), 0), YANG_CONTACT); |
| 209 | assert_int_equal(yin_match_keyword("container", strlen("container"), 0), YANG_CONTAINER); |
| 210 | assert_int_equal(yin_match_keyword("default", strlen("default"), 0), YANG_DEFAULT); |
| 211 | assert_int_equal(yin_match_keyword("description", strlen("description"), 0), YANG_DESCRIPTION); |
| 212 | assert_int_equal(yin_match_keyword("deviate", strlen("deviate"), 0), YANG_DEVIATE); |
| 213 | assert_int_equal(yin_match_keyword("deviation", strlen("deviation"), 0), YANG_DEVIATION); |
| 214 | assert_int_equal(yin_match_keyword("enum", strlen("enum"), 0), YANG_ENUM); |
| 215 | assert_int_equal(yin_match_keyword("error-app-tag", strlen("error-app-tag"), 0), YANG_ERROR_APP_TAG); |
| 216 | assert_int_equal(yin_match_keyword("error-message", strlen("error-message"), 0), YANG_ERROR_MESSAGE); |
| 217 | assert_int_equal(yin_match_keyword("extension", strlen("extension"), 0), YANG_EXTENSION); |
| 218 | assert_int_equal(yin_match_keyword("feature", strlen("feature"), 0), YANG_FEATURE); |
| 219 | assert_int_equal(yin_match_keyword("fraction-digits", strlen("fraction-digits"), 0), YANG_FRACTION_DIGITS); |
| 220 | assert_int_equal(yin_match_keyword("grouping", strlen("grouping"), 0), YANG_GROUPING); |
| 221 | assert_int_equal(yin_match_keyword("identity", strlen("identity"), 0), YANG_IDENTITY); |
| 222 | assert_int_equal(yin_match_keyword("if-feature", strlen("if-feature"), 0), YANG_IF_FEATURE); |
| 223 | assert_int_equal(yin_match_keyword("import", strlen("import"), 0), YANG_IMPORT); |
| 224 | assert_int_equal(yin_match_keyword("include", strlen("include"), 0), YANG_INCLUDE); |
| 225 | assert_int_equal(yin_match_keyword("input", strlen("input"), 0), YANG_INPUT); |
| 226 | assert_int_equal(yin_match_keyword("key", strlen("key"), 0), YANG_KEY); |
| 227 | assert_int_equal(yin_match_keyword("leaf", strlen("leaf"), 0), YANG_LEAF); |
| 228 | assert_int_equal(yin_match_keyword("leaf-list", strlen("leaf-list"), 0), YANG_LEAF_LIST); |
| 229 | assert_int_equal(yin_match_keyword("length", strlen("length"), 0), YANG_LENGTH); |
| 230 | assert_int_equal(yin_match_keyword("list", strlen("list"), 0), YANG_LIST); |
| 231 | assert_int_equal(yin_match_keyword("mandatory", strlen("mandatory"), 0), YANG_MANDATORY); |
| 232 | assert_int_equal(yin_match_keyword("max-elements", strlen("max-elements"), 0), YANG_MAX_ELEMENTS); |
| 233 | assert_int_equal(yin_match_keyword("min-elements", strlen("min-elements"), 0), YANG_MIN_ELEMENTS); |
| 234 | assert_int_equal(yin_match_keyword("modifier", strlen("modifier"), 0), YANG_MODIFIER); |
| 235 | assert_int_equal(yin_match_keyword("module", strlen("module"), 0), YANG_MODULE); |
| 236 | assert_int_equal(yin_match_keyword("must", strlen("must"), 0), YANG_MUST); |
| 237 | assert_int_equal(yin_match_keyword("namespace", strlen("namespace"), 0), YANG_NAMESPACE); |
| 238 | assert_int_equal(yin_match_keyword("notification", strlen("notification"), 0), YANG_NOTIFICATION); |
| 239 | assert_int_equal(yin_match_keyword("ordered-by", strlen("ordered-by"), 0), YANG_ORDERED_BY); |
| 240 | assert_int_equal(yin_match_keyword("organization", strlen("organization"), 0), YANG_ORGANIZATION); |
| 241 | assert_int_equal(yin_match_keyword("output", strlen("output"), 0), YANG_OUTPUT); |
| 242 | assert_int_equal(yin_match_keyword("path", strlen("path"), 0), YANG_PATH); |
| 243 | assert_int_equal(yin_match_keyword("pattern", strlen("pattern"), 0), YANG_PATTERN); |
| 244 | assert_int_equal(yin_match_keyword("position", strlen("position"), 0), YANG_POSITION); |
| 245 | assert_int_equal(yin_match_keyword("prefix", strlen("prefix"), 0), YANG_PREFIX); |
| 246 | assert_int_equal(yin_match_keyword("presence", strlen("presence"), 0), YANG_PRESENCE); |
| 247 | assert_int_equal(yin_match_keyword("range", strlen("range"), 0), YANG_RANGE); |
| 248 | assert_int_equal(yin_match_keyword("reference", strlen("reference"), 0), YANG_REFERENCE); |
| 249 | assert_int_equal(yin_match_keyword("refine", strlen("refine"), 0), YANG_REFINE); |
| 250 | assert_int_equal(yin_match_keyword("require-instance", strlen("require-instance"), 0), YANG_REQUIRE_INSTANCE); |
| 251 | assert_int_equal(yin_match_keyword("revision", strlen("revision"), 0), YANG_REVISION); |
| 252 | assert_int_equal(yin_match_keyword("revision-date", strlen("revision-date"), 0), YANG_REVISION_DATE); |
| 253 | assert_int_equal(yin_match_keyword("rpc", strlen("rpc"), 0), YANG_RPC); |
| 254 | assert_int_equal(yin_match_keyword("status", strlen("status"), 0), YANG_STATUS); |
| 255 | assert_int_equal(yin_match_keyword("submodule", strlen("submodule"), 0), YANG_SUBMODULE); |
| 256 | assert_int_equal(yin_match_keyword("type", strlen("type"), 0), YANG_TYPE); |
| 257 | assert_int_equal(yin_match_keyword("typedef", strlen("typedef"), 0), YANG_TYPEDEF); |
| 258 | assert_int_equal(yin_match_keyword("unique", strlen("unique"), 0), YANG_UNIQUE); |
| 259 | assert_int_equal(yin_match_keyword("units", strlen("units"), 0), YANG_UNITS); |
| 260 | assert_int_equal(yin_match_keyword("uses", strlen("uses"), 0), YANG_USES); |
| 261 | assert_int_equal(yin_match_keyword("value", strlen("value"), 0), YANG_VALUE); |
| 262 | assert_int_equal(yin_match_keyword("when", strlen("when"), 0), YANG_WHEN); |
| 263 | assert_int_equal(yin_match_keyword("yang-version", strlen("yang-version"), 0), YANG_YANG_VERSION); |
| 264 | assert_int_equal(yin_match_keyword("yin-element", strlen("yin-element"), 0), YANG_YIN_ELEMENT); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 265 | } |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 266 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 267 | static void |
| 268 | test_match_argument(void **state) |
| 269 | { |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 270 | (void)state; /* unused */ |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 271 | |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 272 | assert_int_equal(match_argument_name("", 5), YIN_ARG_UNKNOWN); |
| 273 | assert_int_equal(match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN); |
| 274 | assert_int_equal(match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 275 | assert_int_equal(match_argument_name("condition", 9), YIN_ARG_CONDITION); |
| 276 | assert_int_equal(match_argument_name("date", 4), YIN_ARG_DATE); |
| 277 | assert_int_equal(match_argument_name("module", 6), YIN_ARG_MODULE); |
| 278 | assert_int_equal(match_argument_name("name", 4), YIN_ARG_NAME); |
| 279 | assert_int_equal(match_argument_name("tag", 3), YIN_ARG_TAG); |
| 280 | assert_int_equal(match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE); |
| 281 | assert_int_equal(match_argument_name("text", 4), YIN_ARG_TEXT); |
| 282 | assert_int_equal(match_argument_name("uri", 3), YIN_ARG_URI); |
| 283 | assert_int_equal(match_argument_name("value", 5), YIN_ARG_VALUE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 284 | } |
| 285 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 286 | static void |
| 287 | test_meta(void **state) |
| 288 | { |
| 289 | LY_ERR ret = LY_SUCCESS; |
| 290 | struct state *st = *state; |
| 291 | |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 292 | ret = yin_parse_module(st->ctx,"<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\ |
| 293 | name=\"example-foo\">\ |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 294 | <organization xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">organization...</organization>\ |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 295 | <contact>contact...</contact>\ |
| 296 | <description>description...</description>\ |
| 297 | <reference>reference...</reference>\ |
| 298 | </module>", st->mod); |
| 299 | |
| 300 | assert_int_equal(ret, LY_SUCCESS); |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 301 | assert_string_equal(st->mod->parsed->mod->org, "organization..."); |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 302 | assert_string_equal(st->mod->parsed->mod->contact, "contact..."); |
| 303 | assert_string_equal(st->mod->parsed->mod->dsc, "description..."); |
| 304 | assert_string_equal(st->mod->parsed->mod->ref, "reference..."); |
David Sedlák | 6882673 | 2019-06-05 10:50:58 +0200 | [diff] [blame] | 305 | |
| 306 | st = reset_state(state); |
| 307 | ret = yin_parse_module(st->ctx,"<module name=\"example-foo\">\ |
| 308 | <organization test=\"invalid-argument\">organization...</organization>\ |
| 309 | </module>", st->mod); |
| 310 | assert_int_equal(ret, LY_EVALID); |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 311 | |
| 312 | st->finished_correctly = true; |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 313 | } |
| 314 | |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 315 | static void |
| 316 | test_parse_text_element(void **state) |
| 317 | { |
| 318 | struct state *st = *state; |
| 319 | const char *res = NULL, *prefix = NULL, *name = NULL; |
| 320 | size_t prefix_len = 0, name_len = 0; |
| 321 | LY_ERR ret = LY_SUCCESS; |
| 322 | |
| 323 | const char *data = "<elem>content</elem>"; |
| 324 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 325 | parse_text_element(st->xml_ctx, &data, &res); |
| 326 | assert_string_equal(res, "content"); |
| 327 | lydict_remove(st->ctx, "content"); |
| 328 | |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 329 | data = "<elem xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">another-content</elem>"; |
| 330 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 331 | parse_text_element(st->xml_ctx, &data, &res); |
| 332 | assert_string_equal(res, "another-content"); |
| 333 | lydict_remove(st->ctx, "another-content"); |
| 334 | |
David Sedlák | 7ff55a9 | 2019-06-17 11:11:41 +0200 | [diff] [blame] | 335 | data = "<elem invalid=\"invalid\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">text</elem>"; |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 336 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 337 | ret = parse_text_element(st->xml_ctx, &data, &res); |
| 338 | assert_int_equal(ret, LY_EVALID); |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 339 | |
| 340 | st->finished_correctly = true; |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 341 | } |
| 342 | |
David Sedlák | d9d3a31 | 2019-06-04 09:47:10 +0200 | [diff] [blame] | 343 | static void |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 344 | test_yin_parse_import(void **state) |
| 345 | { |
| 346 | struct state *st = *state; |
| 347 | const char *prefix = NULL, *name = NULL; |
| 348 | size_t prefix_len = 0, name_len = 0; |
| 349 | LY_ERR ret = LY_SUCCESS; |
| 350 | struct lysp_import *imports = NULL; |
| 351 | |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 352 | const char *data = "<import xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" module=\"a\">\ |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 353 | <prefix value=\"a_mod\"/>\ |
| 354 | <revision-date date=\"2015-01-01\"/>\ |
David Sedlák | c67dcaa | 2019-06-04 14:49:05 +0200 | [diff] [blame] | 355 | </import>\ |
| 356 | \ |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 357 | <import xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" module=\"a\">\ |
David Sedlák | c67dcaa | 2019-06-04 14:49:05 +0200 | [diff] [blame] | 358 | <prefix value=\"a_mod\"/>\ |
| 359 | <revision-date date=\"2015-01-01\"/>\ |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 360 | </import>"; |
| 361 | |
| 362 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 363 | ret = yin_parse_import(st->xml_ctx, "b-mod", &data, &imports); |
| 364 | assert_int_equal(ret, LY_SUCCESS); |
| 365 | assert_string_equal(imports->name, "a"); |
| 366 | assert_string_equal(imports->prefix, "a_mod"); |
| 367 | assert_string_equal(imports->rev, "2015-01-01"); |
| 368 | lydict_remove(st->ctx, imports->name); |
| 369 | lydict_remove(st->ctx, imports->prefix); |
| 370 | LY_ARRAY_FREE(imports); |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 371 | imports = NULL; |
David Sedlák | c67dcaa | 2019-06-04 14:49:05 +0200 | [diff] [blame] | 372 | |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 373 | lyxml_get_element(st->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 374 | ret = yin_parse_import(st->xml_ctx, "a_mod", &data, &imports); |
| 375 | assert_int_equal(ret, LY_EVALID); |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 376 | logbuf_assert("Prefix \"a_mod\" already used as module prefix. Line number 1."); |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 377 | lydict_remove(st->ctx, imports->name); |
| 378 | lydict_remove(st->ctx, imports->prefix); |
| 379 | LY_ARRAY_FREE(imports); |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 380 | |
| 381 | st->finished_correctly = true; |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 382 | } |
| 383 | |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 384 | int |
| 385 | main(void) |
| 386 | { |
| 387 | |
| 388 | const struct CMUnitTest tests[] = { |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 389 | cmocka_unit_test_setup_teardown(test_yin_parse_module, setup_f, teardown_f), |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 390 | cmocka_unit_test_setup_teardown(test_meta, setup_f, teardown_f), |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 391 | cmocka_unit_test_setup_teardown(test_parse_text_element, setup_f, teardown_f), |
David Sedlák | da63c08 | 2019-06-04 13:52:23 +0200 | [diff] [blame] | 392 | cmocka_unit_test_setup_teardown(test_yin_parse_import, setup_f, teardown_f), |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame^] | 393 | cmocka_unit_test(test_yin_match_keyword), |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 394 | cmocka_unit_test(test_match_argument), |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 395 | }; |
| 396 | |
| 397 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 398 | } |