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 | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 30 | /* prototypes of static functions */ |
| 31 | void lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext); |
David Sedlák | 986cb41 | 2019-07-04 13:10:11 +0200 | [diff] [blame] | 32 | void lysp_ext_free(struct ly_ctx *ctx, struct lysp_ext *ext); |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 33 | void lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when); |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame^] | 34 | void lysp_type_free(struct ly_ctx *ctx, struct lysp_type *type); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 35 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 36 | struct state { |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 37 | struct ly_ctx *ctx; |
David Sedlák | 3017da4 | 2019-02-15 09:48:04 +0100 | [diff] [blame] | 38 | struct lys_module *mod; |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 39 | struct lysp_module *lysp_mod; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 40 | struct yin_parser_ctx *yin_ctx; |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 41 | bool finished_correctly; |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 42 | }; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 43 | |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 44 | #define BUFSIZE 1024 |
| 45 | char logbuf[BUFSIZE] = {0}; |
| 46 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
| 47 | |
| 48 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 49 | #define ENABLE_LOGGER_CHECKING 1 |
| 50 | |
| 51 | #if ENABLE_LOGGER_CHECKING |
| 52 | static void |
| 53 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 54 | { |
| 55 | (void) level; /* unused */ |
| 56 | if (store) { |
| 57 | if (path && path[0]) { |
| 58 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 59 | } else { |
| 60 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 61 | } |
| 62 | if (store > 0) { |
| 63 | --store; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | #endif |
| 68 | |
| 69 | #if ENABLE_LOGGER_CHECKING |
| 70 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 71 | #else |
| 72 | # define logbuf_assert(str) |
| 73 | #endif |
| 74 | |
| 75 | #define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \ |
| 76 | str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 77 | assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \ |
| 78 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \ |
| 79 | CLEANUP |
| 80 | |
| 81 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 82 | static int |
| 83 | setup_f(void **state) |
| 84 | { |
| 85 | struct state *st = NULL; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 86 | |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 87 | #if ENABLE_LOGGER_CHECKING |
| 88 | /* setup logger */ |
| 89 | ly_set_log_clb(logger, 1); |
| 90 | #endif |
| 91 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 92 | /* allocate state variable */ |
| 93 | (*state) = st = calloc(1, sizeof(*st)); |
| 94 | if (!st) { |
| 95 | fprintf(stderr, "Memmory allocation failed"); |
| 96 | return EXIT_FAILURE; |
| 97 | } |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 98 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 99 | /* create new libyang context */ |
| 100 | ly_ctx_new(NULL, 0, &st->ctx); |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 101 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 102 | /* allocate new module */ |
| 103 | st->mod = calloc(1, sizeof(*st->mod)); |
| 104 | st->mod->ctx = st->ctx; |
| 105 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 106 | /* allocate new parsed module */ |
| 107 | st->lysp_mod = calloc(1, sizeof(*st->lysp_mod)); |
| 108 | st->lysp_mod->mod = calloc(1, sizeof(*st->lysp_mod->mod)); |
| 109 | st->lysp_mod->mod->ctx = st->ctx; |
| 110 | |
| 111 | /* allocate parser context */ |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 112 | st->yin_ctx = calloc(1, sizeof(*st->yin_ctx)); |
| 113 | st->yin_ctx->xml_ctx.ctx = st->ctx; |
| 114 | st->yin_ctx->xml_ctx.line = 1; |
David Sedlák | 8f5bce0 | 2019-06-03 16:41:08 +0200 | [diff] [blame] | 115 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 116 | return EXIT_SUCCESS; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static int |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 120 | teardown_f(void **state) |
| 121 | { |
| 122 | struct state *st = *(struct state **)state; |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 123 | struct lys_module *temp; |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 124 | |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 125 | #if ENABLE_LOGGER_CHECKING |
| 126 | /* teardown logger */ |
| 127 | if (!st->finished_correctly && logbuf[0] != '\0') { |
| 128 | fprintf(stderr, "%s\n", logbuf); |
| 129 | } |
| 130 | #endif |
| 131 | |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 132 | temp = st->lysp_mod->mod; |
| 133 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 134 | lyxml_context_clear(&st->yin_ctx->xml_ctx); |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 135 | lys_module_free(st->mod, NULL); |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 136 | lysp_module_free(st->lysp_mod); |
| 137 | lys_module_free(temp, NULL); |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 138 | ly_ctx_destroy(st->ctx, NULL); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 139 | free(st->yin_ctx); |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 140 | free(st); |
| 141 | |
| 142 | return EXIT_SUCCESS; |
| 143 | } |
| 144 | |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 145 | static struct state* |
| 146 | reset_state(void **state) |
| 147 | { |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 148 | ((struct state *)*state)->finished_correctly = true; |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 149 | logbuf[0] = '\0'; |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 150 | teardown_f(state); |
| 151 | setup_f(state); |
| 152 | |
| 153 | return *state; |
| 154 | } |
| 155 | |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 156 | void |
| 157 | logbuf_clean(void) |
| 158 | { |
| 159 | logbuf[0] = '\0'; |
| 160 | } |
| 161 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 162 | static void |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 163 | test_yin_parse_module(void **state) |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 164 | { |
| 165 | LY_ERR ret = LY_SUCCESS; |
| 166 | struct state *st = *state; |
| 167 | |
| 168 | ret = yin_parse_module(st->ctx, |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 169 | "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\ |
| 170 | name=\"example-foo\"\ |
David Sedlák | 1873013 | 2019-03-15 15:51:34 +0100 | [diff] [blame] | 171 | xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\ |
| 172 | xmlns:foo=\"urn:example:foo\"\ |
| 173 | xmlns:myext=\"urn:example:extensions\">\ |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 174 | <namespace uri=\"urn:example:foo\" xmlns:myext=\"urn:example:extensions\"/>\ |
David Sedlák | a740695 | 2019-04-05 10:33:07 +0200 | [diff] [blame] | 175 | <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] | 176 | </module>", |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 177 | st->mod); |
| 178 | |
| 179 | assert_int_equal(ret, LY_SUCCESS); |
| 180 | assert_string_equal(st->mod->parsed->mod->name, "example-foo"); |
| 181 | assert_string_equal(st->mod->parsed->mod->prefix, "foo"); |
David Sedlák | cd0c951 | 2019-03-29 13:23:06 +0100 | [diff] [blame] | 182 | assert_string_equal(st->mod->parsed->mod->ns, "urn:example:foo"); |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 183 | |
| 184 | st = reset_state(state); |
| 185 | ret = yin_parse_module(st->ctx, |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 186 | "<module name=\"example-foo\">\ |
| 187 | <invalid-tag uri=\"urn:example:foo\"\"/>\ |
| 188 | </module>", |
| 189 | st->mod); |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 190 | assert_int_equal(ret, LY_EVALID); |
| 191 | |
| 192 | st = reset_state(state); |
| 193 | ret = yin_parse_module(st->ctx, |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 194 | "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">\ |
David Sedlák | 57715b1 | 2019-06-17 13:05:22 +0200 | [diff] [blame] | 195 | </module>", |
David Sedlák | 2b214ac | 2019-06-06 16:11:03 +0200 | [diff] [blame] | 196 | st->mod); |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 197 | assert_int_equal(ret, LY_EVALID); |
David Sedlák | 882a807 | 2019-07-08 17:51:20 +0200 | [diff] [blame] | 198 | logbuf_assert("Missing mandatory attribute name of module element. Line number 1."); |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 199 | |
| 200 | st = reset_state(state); |
| 201 | ret = yin_parse_module(st->ctx, |
| 202 | "", |
| 203 | st->mod); |
| 204 | assert_int_equal(ret, LY_EVALID); |
David Sedlák | 79e50cb | 2019-06-05 16:33:09 +0200 | [diff] [blame] | 205 | logbuf_assert("Invalid keyword \"(null)\", expected \"module\" or \"submodule\". Line number 1."); |
| 206 | st->finished_correctly = true; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static void |
David Sedlák | 1bccdfa | 2019-06-17 15:55:27 +0200 | [diff] [blame] | 210 | test_yin_match_keyword(void **state) |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 211 | { |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 212 | struct state *st = *state; |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 213 | |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 214 | const char *prefix, *name; |
| 215 | struct yin_arg_record *args = NULL; |
| 216 | size_t prefix_len, name_len; |
| 217 | /* create mock yin namespace in xml context */ |
| 218 | const char *data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" />"; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 219 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 220 | yin_load_attributes(st->yin_ctx, &data, &args); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 221 | LY_ARRAY_FREE(args); |
| 222 | |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 223 | assert_int_equal(yin_match_keyword(st->yin_ctx, "anydatax", strlen("anydatax"), prefix, prefix_len, YANG_NONE), YANG_NONE); |
| 224 | assert_int_equal(yin_match_keyword(st->yin_ctx, "asdasd", strlen("asdasd"), prefix, prefix_len, YANG_NONE), YANG_NONE); |
| 225 | assert_int_equal(yin_match_keyword(st->yin_ctx, "", 0, prefix, prefix_len, YANG_NONE), YANG_NONE); |
| 226 | assert_int_equal(yin_match_keyword(st->yin_ctx, "anydata", strlen("anydata"), prefix, prefix_len, YANG_NONE), YANG_ANYDATA); |
| 227 | assert_int_equal(yin_match_keyword(st->yin_ctx, "anyxml", strlen("anyxml"), prefix, prefix_len, YANG_NONE), YANG_ANYXML); |
| 228 | assert_int_equal(yin_match_keyword(st->yin_ctx, "argument", strlen("argument"), prefix, prefix_len, YANG_NONE), YANG_ARGUMENT); |
| 229 | assert_int_equal(yin_match_keyword(st->yin_ctx, "augment", strlen("augment"), prefix, prefix_len, YANG_NONE), YANG_AUGMENT); |
| 230 | assert_int_equal(yin_match_keyword(st->yin_ctx, "base", strlen("base"), prefix, prefix_len, YANG_NONE), YANG_BASE); |
| 231 | assert_int_equal(yin_match_keyword(st->yin_ctx, "belongs-to", strlen("belongs-to"), prefix, prefix_len, YANG_NONE), YANG_BELONGS_TO); |
| 232 | assert_int_equal(yin_match_keyword(st->yin_ctx, "bit", strlen("bit"), prefix, prefix_len, YANG_NONE), YANG_BIT); |
| 233 | assert_int_equal(yin_match_keyword(st->yin_ctx, "case", strlen("case"), prefix, prefix_len, YANG_NONE), YANG_CASE); |
| 234 | assert_int_equal(yin_match_keyword(st->yin_ctx, "choice", strlen("choice"), prefix, prefix_len, YANG_NONE), YANG_CHOICE); |
| 235 | assert_int_equal(yin_match_keyword(st->yin_ctx, "config", strlen("config"), prefix, prefix_len, YANG_NONE), YANG_CONFIG); |
| 236 | assert_int_equal(yin_match_keyword(st->yin_ctx, "contact", strlen("contact"), prefix, prefix_len, YANG_NONE), YANG_CONTACT); |
| 237 | assert_int_equal(yin_match_keyword(st->yin_ctx, "container", strlen("container"), prefix, prefix_len, YANG_NONE), YANG_CONTAINER); |
| 238 | assert_int_equal(yin_match_keyword(st->yin_ctx, "default", strlen("default"), prefix, prefix_len, YANG_NONE), YANG_DEFAULT); |
| 239 | assert_int_equal(yin_match_keyword(st->yin_ctx, "description", strlen("description"), prefix, prefix_len, YANG_NONE), YANG_DESCRIPTION); |
| 240 | assert_int_equal(yin_match_keyword(st->yin_ctx, "deviate", strlen("deviate"), prefix, prefix_len, YANG_NONE), YANG_DEVIATE); |
| 241 | assert_int_equal(yin_match_keyword(st->yin_ctx, "deviation", strlen("deviation"), prefix, prefix_len, YANG_NONE), YANG_DEVIATION); |
| 242 | assert_int_equal(yin_match_keyword(st->yin_ctx, "enum", strlen("enum"), prefix, prefix_len, YANG_NONE), YANG_ENUM); |
| 243 | assert_int_equal(yin_match_keyword(st->yin_ctx, "error-app-tag", strlen("error-app-tag"), prefix, prefix_len, YANG_NONE), YANG_ERROR_APP_TAG); |
| 244 | assert_int_equal(yin_match_keyword(st->yin_ctx, "error-message", strlen("error-message"), prefix, prefix_len, YANG_NONE), YANG_ERROR_MESSAGE); |
| 245 | assert_int_equal(yin_match_keyword(st->yin_ctx, "extension", strlen("extension"), prefix, prefix_len, YANG_NONE), YANG_EXTENSION); |
| 246 | assert_int_equal(yin_match_keyword(st->yin_ctx, "feature", strlen("feature"), prefix, prefix_len, YANG_NONE), YANG_FEATURE); |
| 247 | assert_int_equal(yin_match_keyword(st->yin_ctx, "fraction-digits", strlen("fraction-digits"), prefix, prefix_len, YANG_NONE), YANG_FRACTION_DIGITS); |
| 248 | assert_int_equal(yin_match_keyword(st->yin_ctx, "grouping", strlen("grouping"), prefix, prefix_len, YANG_NONE), YANG_GROUPING); |
| 249 | assert_int_equal(yin_match_keyword(st->yin_ctx, "identity", strlen("identity"), prefix, prefix_len, YANG_NONE), YANG_IDENTITY); |
| 250 | assert_int_equal(yin_match_keyword(st->yin_ctx, "if-feature", strlen("if-feature"), prefix, prefix_len, YANG_NONE), YANG_IF_FEATURE); |
| 251 | assert_int_equal(yin_match_keyword(st->yin_ctx, "import", strlen("import"), prefix, prefix_len, YANG_NONE), YANG_IMPORT); |
| 252 | assert_int_equal(yin_match_keyword(st->yin_ctx, "include", strlen("include"), prefix, prefix_len, YANG_NONE), YANG_INCLUDE); |
| 253 | assert_int_equal(yin_match_keyword(st->yin_ctx, "input", strlen("input"), prefix, prefix_len, YANG_NONE), YANG_INPUT); |
| 254 | assert_int_equal(yin_match_keyword(st->yin_ctx, "key", strlen("key"), prefix, prefix_len, YANG_NONE), YANG_KEY); |
| 255 | assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf", strlen("leaf"), prefix, prefix_len, YANG_NONE), YANG_LEAF); |
| 256 | assert_int_equal(yin_match_keyword(st->yin_ctx, "leaf-list", strlen("leaf-list"), prefix, prefix_len, YANG_NONE), YANG_LEAF_LIST); |
| 257 | assert_int_equal(yin_match_keyword(st->yin_ctx, "length", strlen("length"), prefix, prefix_len, YANG_NONE), YANG_LENGTH); |
| 258 | assert_int_equal(yin_match_keyword(st->yin_ctx, "list", strlen("list"), prefix, prefix_len, YANG_NONE), YANG_LIST); |
| 259 | assert_int_equal(yin_match_keyword(st->yin_ctx, "mandatory", strlen("mandatory"), prefix, prefix_len, YANG_NONE), YANG_MANDATORY); |
| 260 | assert_int_equal(yin_match_keyword(st->yin_ctx, "max-elements", strlen("max-elements"), prefix, prefix_len, YANG_NONE), YANG_MAX_ELEMENTS); |
| 261 | assert_int_equal(yin_match_keyword(st->yin_ctx, "min-elements", strlen("min-elements"), prefix, prefix_len, YANG_NONE), YANG_MIN_ELEMENTS); |
| 262 | assert_int_equal(yin_match_keyword(st->yin_ctx, "modifier", strlen("modifier"), prefix, prefix_len, YANG_NONE), YANG_MODIFIER); |
| 263 | assert_int_equal(yin_match_keyword(st->yin_ctx, "module", strlen("module"), prefix, prefix_len, YANG_NONE), YANG_MODULE); |
| 264 | assert_int_equal(yin_match_keyword(st->yin_ctx, "must", strlen("must"), prefix, prefix_len, YANG_NONE), YANG_MUST); |
| 265 | assert_int_equal(yin_match_keyword(st->yin_ctx, "namespace", strlen("namespace"), prefix, prefix_len, YANG_NONE), YANG_NAMESPACE); |
| 266 | assert_int_equal(yin_match_keyword(st->yin_ctx, "notification", strlen("notification"), prefix, prefix_len, YANG_NONE), YANG_NOTIFICATION); |
| 267 | assert_int_equal(yin_match_keyword(st->yin_ctx, "ordered-by", strlen("ordered-by"), prefix, prefix_len, YANG_NONE), YANG_ORDERED_BY); |
| 268 | assert_int_equal(yin_match_keyword(st->yin_ctx, "organization", strlen("organization"), prefix, prefix_len, YANG_NONE), YANG_ORGANIZATION); |
| 269 | assert_int_equal(yin_match_keyword(st->yin_ctx, "output", strlen("output"), prefix, prefix_len, YANG_NONE), YANG_OUTPUT); |
| 270 | assert_int_equal(yin_match_keyword(st->yin_ctx, "path", strlen("path"), prefix, prefix_len, YANG_NONE), YANG_PATH); |
| 271 | assert_int_equal(yin_match_keyword(st->yin_ctx, "pattern", strlen("pattern"), prefix, prefix_len, YANG_NONE), YANG_PATTERN); |
| 272 | assert_int_equal(yin_match_keyword(st->yin_ctx, "position", strlen("position"), prefix, prefix_len, YANG_NONE), YANG_POSITION); |
| 273 | assert_int_equal(yin_match_keyword(st->yin_ctx, "prefix", strlen("prefix"), prefix, prefix_len, YANG_NONE), YANG_PREFIX); |
| 274 | assert_int_equal(yin_match_keyword(st->yin_ctx, "presence", strlen("presence"), prefix, prefix_len, YANG_NONE), YANG_PRESENCE); |
| 275 | assert_int_equal(yin_match_keyword(st->yin_ctx, "range", strlen("range"), prefix, prefix_len, YANG_NONE), YANG_RANGE); |
| 276 | assert_int_equal(yin_match_keyword(st->yin_ctx, "reference", strlen("reference"), prefix, prefix_len, YANG_NONE), YANG_REFERENCE); |
| 277 | assert_int_equal(yin_match_keyword(st->yin_ctx, "refine", strlen("refine"), prefix, prefix_len, YANG_NONE), YANG_REFINE); |
| 278 | assert_int_equal(yin_match_keyword(st->yin_ctx, "require-instance", strlen("require-instance"), prefix, prefix_len, YANG_NONE), YANG_REQUIRE_INSTANCE); |
| 279 | assert_int_equal(yin_match_keyword(st->yin_ctx, "revision", strlen("revision"), prefix, prefix_len, YANG_NONE), YANG_REVISION); |
| 280 | assert_int_equal(yin_match_keyword(st->yin_ctx, "revision-date", strlen("revision-date"), prefix, prefix_len, YANG_NONE), YANG_REVISION_DATE); |
| 281 | assert_int_equal(yin_match_keyword(st->yin_ctx, "rpc", strlen("rpc"), prefix, prefix_len, YANG_NONE), YANG_RPC); |
| 282 | assert_int_equal(yin_match_keyword(st->yin_ctx, "status", strlen("status"), prefix, prefix_len, YANG_NONE), YANG_STATUS); |
| 283 | assert_int_equal(yin_match_keyword(st->yin_ctx, "submodule", strlen("submodule"), prefix, prefix_len, YANG_NONE), YANG_SUBMODULE); |
| 284 | assert_int_equal(yin_match_keyword(st->yin_ctx, "type", strlen("type"), prefix, prefix_len, YANG_NONE), YANG_TYPE); |
| 285 | assert_int_equal(yin_match_keyword(st->yin_ctx, "typedef", strlen("typedef"), prefix, prefix_len, YANG_NONE), YANG_TYPEDEF); |
| 286 | assert_int_equal(yin_match_keyword(st->yin_ctx, "unique", strlen("unique"), prefix, prefix_len, YANG_NONE), YANG_UNIQUE); |
| 287 | assert_int_equal(yin_match_keyword(st->yin_ctx, "units", strlen("units"), prefix, prefix_len, YANG_NONE), YANG_UNITS); |
| 288 | assert_int_equal(yin_match_keyword(st->yin_ctx, "uses", strlen("uses"), prefix, prefix_len, YANG_NONE), YANG_USES); |
| 289 | assert_int_equal(yin_match_keyword(st->yin_ctx, "value", strlen("value"), prefix, prefix_len, YANG_NONE), YANG_VALUE); |
| 290 | assert_int_equal(yin_match_keyword(st->yin_ctx, "when", strlen("when"), prefix, prefix_len, YANG_NONE), YANG_WHEN); |
| 291 | assert_int_equal(yin_match_keyword(st->yin_ctx, "yang-version", strlen("yang-version"), prefix, prefix_len, YANG_NONE), YANG_YANG_VERSION); |
| 292 | assert_int_equal(yin_match_keyword(st->yin_ctx, "yin-element", strlen("yin-element"), prefix, prefix_len, YANG_NONE), YANG_YIN_ELEMENT); |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 293 | |
| 294 | st->finished_correctly = true; |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 295 | } |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 296 | |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 297 | static void |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 298 | test_yin_match_argument_name(void **state) |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 299 | { |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 300 | (void)state; /* unused */ |
David Sedlák | 872c7b4 | 2018-10-26 13:15:20 +0200 | [diff] [blame] | 301 | |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 302 | assert_int_equal(yin_match_argument_name("", 5), YIN_ARG_UNKNOWN); |
| 303 | assert_int_equal(yin_match_argument_name("qwertyasd", 5), YIN_ARG_UNKNOWN); |
| 304 | assert_int_equal(yin_match_argument_name("conditionasd", 8), YIN_ARG_UNKNOWN); |
| 305 | assert_int_equal(yin_match_argument_name("condition", 9), YIN_ARG_CONDITION); |
| 306 | assert_int_equal(yin_match_argument_name("date", 4), YIN_ARG_DATE); |
| 307 | assert_int_equal(yin_match_argument_name("module", 6), YIN_ARG_MODULE); |
| 308 | assert_int_equal(yin_match_argument_name("name", 4), YIN_ARG_NAME); |
| 309 | assert_int_equal(yin_match_argument_name("tag", 3), YIN_ARG_TAG); |
| 310 | assert_int_equal(yin_match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE); |
| 311 | assert_int_equal(yin_match_argument_name("text", 4), YIN_ARG_TEXT); |
| 312 | assert_int_equal(yin_match_argument_name("uri", 3), YIN_ARG_URI); |
| 313 | assert_int_equal(yin_match_argument_name("value", 5), YIN_ARG_VALUE); |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 314 | } |
| 315 | |
David Sedlák | 68a1af1 | 2019-03-08 13:46:54 +0100 | [diff] [blame] | 316 | static void |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 317 | test_yin_parse_element_generic(void **state) |
| 318 | { |
| 319 | const char *prefix, *name; |
| 320 | struct state *st = *state; |
| 321 | struct lysp_ext_instance exts; |
| 322 | size_t prefix_len, name_len; |
| 323 | LY_ERR ret; |
| 324 | |
| 325 | memset(&exts, 0, sizeof(exts)); |
| 326 | |
| 327 | const char *data = "<elem attr=\"value\">text_value</elem>"; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 328 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 329 | ret = yin_parse_element_generic(st->yin_ctx, name, name_len, prefix, prefix_len, &data, &exts.child); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 330 | assert_int_equal(ret, LY_SUCCESS); |
David Sedlák | 1fdb252 | 2019-07-09 16:22:57 +0200 | [diff] [blame] | 331 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 332 | assert_string_equal(exts.child->stmt, "elem"); |
| 333 | assert_string_equal(exts.child->arg, "text_value"); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 334 | assert_string_equal(exts.child->child->stmt, "attr"); |
| 335 | assert_string_equal(exts.child->child->arg, "value"); |
| 336 | assert_true(exts.child->child->flags & LYS_YIN_ATTR); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 337 | lysp_ext_instance_free(st->ctx, &exts); |
David Sedlák | 5392a21 | 2019-07-01 09:19:10 +0200 | [diff] [blame] | 338 | st = reset_state(state); |
| 339 | |
| 340 | data = "<elem></elem>"; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 341 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 342 | ret = yin_parse_element_generic(st->yin_ctx, name, name_len, prefix, prefix_len, &data, &exts.child); |
David Sedlák | 5392a21 | 2019-07-01 09:19:10 +0200 | [diff] [blame] | 343 | assert_int_equal(ret, LY_SUCCESS); |
| 344 | assert_string_equal(exts.child->stmt, "elem"); |
| 345 | assert_null(exts.child->child); |
| 346 | assert_null(exts.child->arg); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 347 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
David Sedlák | 5392a21 | 2019-07-01 09:19:10 +0200 | [diff] [blame] | 348 | lysp_ext_instance_free(st->ctx, &exts); |
| 349 | |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 350 | st->finished_correctly = true; |
| 351 | } |
| 352 | |
| 353 | static void |
| 354 | test_yin_parse_extension_instance(void **state) |
| 355 | { |
| 356 | LY_ERR ret; |
| 357 | struct state *st = *state; |
| 358 | const char *prefix, *name; |
| 359 | size_t prefix_len, name_len; |
| 360 | struct yin_arg_record *args = NULL; |
| 361 | struct lysp_ext_instance *exts = NULL; |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 362 | const char *data = "<ext value1=\"test\" value=\"test2\"><subelem>text</subelem></ext>"; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 363 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 364 | yin_load_attributes(st->yin_ctx, &data, &args); |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 365 | ret = yin_parse_extension_instance(st->yin_ctx, args, &data, name2fullname(name, prefix_len), |
David Sedlák | 619db94 | 2019-07-03 14:47:30 +0200 | [diff] [blame] | 366 | namelen2fulllen(name_len, prefix_len), LYEXT_SUBSTMT_CONTACT, 0, &exts); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 367 | assert_int_equal(ret, LY_SUCCESS); |
| 368 | assert_string_equal(exts->name, "ext"); |
| 369 | assert_int_equal(exts->insubstmt_index, 0); |
| 370 | assert_true(exts->insubstmt == LYEXT_SUBSTMT_CONTACT); |
| 371 | assert_true(exts->yin & LYS_YIN); |
| 372 | assert_string_equal(exts->child->stmt, "value1"); |
| 373 | assert_string_equal(exts->child->arg, "test"); |
| 374 | assert_null(exts->child->child); |
| 375 | assert_true(exts->child->flags & LYS_YIN_ATTR); |
| 376 | assert_string_equal(exts->child->next->stmt, "value"); |
| 377 | assert_string_equal(exts->child->next->arg, "test2"); |
| 378 | assert_null(exts->child->next->child); |
| 379 | assert_true(exts->child->next->flags & LYS_YIN_ATTR); |
| 380 | |
| 381 | assert_string_equal(exts->child->next->next->stmt, "subelem"); |
| 382 | assert_string_equal(exts->child->next->next->arg, "text"); |
| 383 | assert_null(exts->child->next->next->child); |
| 384 | assert_null(exts->child->next->next->next); |
| 385 | assert_false(exts->child->next->next->flags & LYS_YIN_ATTR); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 386 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 387 | LY_ARRAY_FREE(args); |
| 388 | lysp_ext_instance_free(st->ctx, exts); |
| 389 | LY_ARRAY_FREE(exts); |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 390 | exts = NULL; |
| 391 | args = NULL; |
| 392 | st = reset_state(state); |
| 393 | |
| 394 | data = "<extension-elem />"; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 395 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix, &prefix_len, &name, &name_len); |
| 396 | yin_load_attributes(st->yin_ctx, &data, &args); |
David Sedlák | 1f90d25 | 2019-07-10 17:09:32 +0200 | [diff] [blame] | 397 | ret = yin_parse_extension_instance(st->yin_ctx, args, &data, name, name_len, LYEXT_SUBSTMT_CONTACT, 0, &exts); |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 398 | assert_int_equal(ret, LY_SUCCESS); |
| 399 | assert_string_equal(exts->name, "extension-elem"); |
| 400 | assert_null(exts->argument); |
| 401 | assert_null(exts->child); |
| 402 | assert_int_equal(exts->insubstmt, LYEXT_SUBSTMT_CONTACT); |
| 403 | assert_int_equal(exts->insubstmt_index, 0); |
| 404 | assert_true(exts->yin & LYS_YIN); |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 405 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
David Sedlák | f250ecf | 2019-07-01 11:02:05 +0200 | [diff] [blame] | 406 | LY_ARRAY_FREE(args); |
| 407 | lysp_ext_instance_free(st->ctx, exts); |
| 408 | LY_ARRAY_FREE(exts); |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 409 | st->finished_correctly = true; |
| 410 | } |
| 411 | |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 412 | static void |
| 413 | test_yin_parse_content(void **state) |
| 414 | { |
| 415 | struct state *st = *state; |
| 416 | LY_ERR ret = LY_SUCCESS; |
| 417 | struct sized_string name, prefix; |
| 418 | const char *data = "<prefix value=\"a_mod\" xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">" |
| 419 | "<custom xmlns=\"my-ext\">" |
| 420 | "totally amazing extension" |
| 421 | "</custom>" |
David Sedlák | 986cb41 | 2019-07-04 13:10:11 +0200 | [diff] [blame] | 422 | "<extension name=\"ext\">" |
| 423 | "<argument name=\"argname\"></argument>" |
| 424 | "<description><text>desc</text></description>" |
| 425 | "<reference><text>ref</text></reference>" |
| 426 | "<status value=\"deprecated\"></status>" |
| 427 | "</extension>" |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 428 | "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>" |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame^] | 429 | "<if-feature name=\"foo\"></if-feature>" |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 430 | "<when condition=\"condition...\">" |
| 431 | "<reference><text>when_ref</text></reference>" |
| 432 | "<description><text>when_desc</text></description>" |
| 433 | "</when>" |
David Sedlák | e1a3030 | 2019-07-10 13:49:38 +0200 | [diff] [blame] | 434 | "<config value=\"true\"/>" |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 435 | "<error-message>" |
| 436 | "<value>error-msg</value>" |
| 437 | "</error-message>" |
David Sedlák | 2ce1be6 | 2019-07-10 16:15:09 +0200 | [diff] [blame] | 438 | "<error-app-tag value=\"err-app-tag\"/>" |
David Sedlák | a5b1d38 | 2019-07-10 16:31:09 +0200 | [diff] [blame] | 439 | "<units name=\"radians\"></units>" |
David Sedlák | e7084ce | 2019-07-10 16:44:15 +0200 | [diff] [blame] | 440 | "<default value=\"default-value\"/>" |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 441 | "<position value=\"25\"></position>" |
| 442 | "<value value=\"-5\"/>" |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 443 | "<require-instance value=\"true\"></require-instance>" |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 444 | "<range value=\"5..10\" />" |
David Sedlák | 438ae43 | 2019-07-11 15:36:54 +0200 | [diff] [blame] | 445 | "<length value=\"baf\"/>" |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 446 | "<pattern value='pattern'>" |
| 447 | "<modifier value='invert-match'/>" |
| 448 | "</pattern>" |
David Sedlák | fd5b9c3 | 2019-07-12 15:33:13 +0200 | [diff] [blame] | 449 | "<enum name=\"yay\">" |
| 450 | "</enum>" |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 451 | "</prefix>"; |
| 452 | struct lysp_ext_instance *exts = NULL; |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 453 | const char **if_features = NULL; |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 454 | struct yin_arg_record *attrs = NULL; |
David Sedlák | e7084ce | 2019-07-10 16:44:15 +0200 | [diff] [blame] | 455 | const char *value, *err_msg, *app_tag, *units, *def; |
David Sedlák | 986cb41 | 2019-07-04 13:10:11 +0200 | [diff] [blame] | 456 | struct lysp_ext *ext_def = NULL; |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 457 | struct lysp_when *when_p = NULL; |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 458 | struct lysp_type_enum pos_enum = {}, val_enum = {}; |
David Sedlák | fd5b9c3 | 2019-07-12 15:33:13 +0200 | [diff] [blame] | 459 | struct lysp_type req_type = {}, range_type = {}, len_type = {}, patter_type = {}, enum_type = {}; |
David Sedlák | e1a3030 | 2019-07-10 13:49:38 +0200 | [diff] [blame] | 460 | uint8_t config = 0; |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 461 | |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 462 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 463 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 464 | |
David Sedlák | fd5b9c3 | 2019-07-12 15:33:13 +0200 | [diff] [blame] | 465 | struct yin_subelement subelems[17] = { |
David Sedlák | 438ae43 | 2019-07-11 15:36:54 +0200 | [diff] [blame] | 466 | {YANG_CONFIG, &config, 0}, |
| 467 | {YANG_DEFAULT, &def, 0}, |
David Sedlák | fd5b9c3 | 2019-07-12 15:33:13 +0200 | [diff] [blame] | 468 | {YANG_ENUM, &enum_type, 0}, |
David Sedlák | 438ae43 | 2019-07-11 15:36:54 +0200 | [diff] [blame] | 469 | {YANG_ERROR_APP_TAG, &app_tag, 0}, |
| 470 | {YANG_ERROR_MESSAGE, &err_msg, 0}, |
| 471 | {YANG_EXTENSION, &ext_def, 0}, |
| 472 | {YANG_IF_FEATURE, &if_features, 0}, |
| 473 | {YANG_LENGTH, &len_type, 0}, |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 474 | {YANG_PATTERN, &patter_type, 0}, |
David Sedlák | 438ae43 | 2019-07-11 15:36:54 +0200 | [diff] [blame] | 475 | {YANG_POSITION, &pos_enum, 0}, |
| 476 | {YANG_RANGE, &range_type, 0}, |
| 477 | {YANG_REQUIRE_INSTANCE, &req_type, 0}, |
| 478 | {YANG_UNITS, &units, 0}, |
| 479 | {YANG_VALUE, &val_enum, 0}, |
| 480 | {YANG_WHEN, &when_p, 0}, |
| 481 | {YANG_CUSTOM, NULL, 0}, |
| 482 | {YIN_TEXT, &value, 0} |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 483 | }; |
David Sedlák | fd5b9c3 | 2019-07-12 15:33:13 +0200 | [diff] [blame] | 484 | ret = yin_parse_content(st->yin_ctx, subelems, 17, &data, YANG_PREFIX, NULL, &exts); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 485 | assert_int_equal(ret, LY_SUCCESS); |
David Sedlák | 1fdb252 | 2019-07-09 16:22:57 +0200 | [diff] [blame] | 486 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
David Sedlák | 2ce1be6 | 2019-07-10 16:15:09 +0200 | [diff] [blame] | 487 | /* check parsed values */ |
David Sedlák | e7084ce | 2019-07-10 16:44:15 +0200 | [diff] [blame] | 488 | assert_string_equal(def, "default-value"); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 489 | assert_string_equal(exts->name, "custom"); |
| 490 | assert_string_equal(exts->argument, "totally amazing extension"); |
| 491 | assert_string_equal(value, "wsefsdf"); |
David Sedlák | a5b1d38 | 2019-07-10 16:31:09 +0200 | [diff] [blame] | 492 | assert_string_equal(units, "radians"); |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 493 | assert_string_equal(when_p->cond, "condition..."); |
| 494 | assert_string_equal(when_p->dsc, "when_desc"); |
| 495 | assert_string_equal(when_p->ref, "when_ref"); |
David Sedlák | e1a3030 | 2019-07-10 13:49:38 +0200 | [diff] [blame] | 496 | assert_int_equal(config, LYS_CONFIG_W); |
David Sedlák | 5545f5d | 2019-07-11 11:55:16 +0200 | [diff] [blame] | 497 | assert_int_equal(pos_enum.value, 25); |
| 498 | assert_true(pos_enum.flags | LYS_SET_VALUE); |
| 499 | assert_int_equal(val_enum.value, -5); |
| 500 | assert_true(val_enum.flags | LYS_SET_VALUE); |
David Sedlák | cf5569a | 2019-07-11 13:31:34 +0200 | [diff] [blame] | 501 | assert_int_equal(req_type.require_instance, 1); |
| 502 | assert_true(req_type.flags |= LYS_SET_REQINST); |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 503 | assert_string_equal(range_type.range->arg, "5..10"); |
| 504 | assert_true(range_type.flags | LYS_SET_RANGE); |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 505 | assert_string_equal(err_msg, "error-msg"); |
David Sedlák | 2ce1be6 | 2019-07-10 16:15:09 +0200 | [diff] [blame] | 506 | assert_string_equal(app_tag, "err-app-tag"); |
David Sedlák | fd5b9c3 | 2019-07-12 15:33:13 +0200 | [diff] [blame] | 507 | assert_string_equal(enum_type.enums->name, "yay"); |
David Sedlák | 438ae43 | 2019-07-11 15:36:54 +0200 | [diff] [blame] | 508 | assert_string_equal(len_type.length->arg, "baf"); |
| 509 | assert_true(len_type.flags | LYS_SET_LENGTH); |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 510 | assert_string_equal(patter_type.patterns->arg, "\x015pattern"); |
| 511 | assert_true(patter_type.flags | LYS_SET_PATTERN); |
David Sedlák | 2ce1be6 | 2019-07-10 16:15:09 +0200 | [diff] [blame] | 512 | /* cleanup */ |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 513 | lysp_ext_instance_free(st->ctx, exts); |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 514 | lysp_when_free(st->ctx, when_p); |
David Sedlák | 986cb41 | 2019-07-04 13:10:11 +0200 | [diff] [blame] | 515 | lysp_ext_free(st->ctx, ext_def); |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 516 | FREE_STRING(st->ctx, *if_features); |
David Sedlák | c1771b1 | 2019-07-10 15:55:46 +0200 | [diff] [blame] | 517 | FREE_STRING(st->ctx, err_msg); |
David Sedlák | 2ce1be6 | 2019-07-10 16:15:09 +0200 | [diff] [blame] | 518 | FREE_STRING(st->ctx, app_tag); |
David Sedlák | a5b1d38 | 2019-07-10 16:31:09 +0200 | [diff] [blame] | 519 | FREE_STRING(st->ctx, units); |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 520 | FREE_STRING(st->ctx, patter_type.patterns->arg); |
David Sedlák | e7084ce | 2019-07-10 16:44:15 +0200 | [diff] [blame] | 521 | FREE_STRING(st->ctx, def); |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 522 | FREE_STRING(st->ctx, range_type.range->arg); |
David Sedlák | 438ae43 | 2019-07-11 15:36:54 +0200 | [diff] [blame] | 523 | FREE_STRING(st->ctx, len_type.length->arg); |
David Sedlák | fd5b9c3 | 2019-07-12 15:33:13 +0200 | [diff] [blame] | 524 | FREE_STRING(st->ctx, enum_type.enums->name); |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 525 | FREE_STRING(st->ctx, value); |
David Sedlák | 5f8191e | 2019-07-08 16:35:52 +0200 | [diff] [blame] | 526 | LY_ARRAY_FREE(if_features); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 527 | LY_ARRAY_FREE(exts); |
David Sedlák | 986cb41 | 2019-07-04 13:10:11 +0200 | [diff] [blame] | 528 | LY_ARRAY_FREE(ext_def); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 529 | LY_ARRAY_FREE(attrs); |
David Sedlák | d398311 | 2019-07-12 11:20:56 +0200 | [diff] [blame] | 530 | LY_ARRAY_FREE(patter_type.patterns); |
David Sedlák | fd5b9c3 | 2019-07-12 15:33:13 +0200 | [diff] [blame] | 531 | LY_ARRAY_FREE(enum_type.enums); |
David Sedlák | 32eee7b | 2019-07-09 12:38:44 +0200 | [diff] [blame] | 532 | free(when_p); |
David Sedlák | b7296dd | 2019-07-11 14:58:38 +0200 | [diff] [blame] | 533 | free(range_type.range); |
David Sedlák | 438ae43 | 2019-07-11 15:36:54 +0200 | [diff] [blame] | 534 | free(len_type.length); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 535 | attrs = NULL; |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 536 | st = reset_state(state); |
| 537 | |
| 538 | /* test unique subelem */ |
| 539 | const char *prefix_value; |
| 540 | struct yin_subelement subelems2[2] = {{YANG_PREFIX, &prefix_value, 0}, |
| 541 | {YIN_TEXT, &value, YIN_SUBELEM_UNIQUE}}; |
| 542 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">" |
| 543 | "<prefix value=\"inv_mod\" />" |
| 544 | "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>" |
| 545 | "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>" |
| 546 | "</module>"; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 547 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 548 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 549 | ret = yin_parse_content(st->yin_ctx, subelems2, 2, &data, YANG_MODULE, NULL, &exts); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 550 | assert_int_equal(ret, LY_EVALID); |
| 551 | logbuf_assert("Redefinition of text element in module element. Line number 1."); |
| 552 | lydict_remove(st->ctx, prefix_value); |
| 553 | lydict_remove(st->ctx, value); |
| 554 | st = reset_state(state); |
| 555 | LY_ARRAY_FREE(attrs); |
| 556 | attrs = NULL; |
| 557 | |
| 558 | /* test first subelem */ |
| 559 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">" |
| 560 | "<prefix value=\"inv_mod\" />" |
| 561 | "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>" |
| 562 | "<text xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">wsefsdf</text>" |
| 563 | "</module>"; |
| 564 | struct yin_subelement subelems3[2] = {{YANG_PREFIX, &prefix_value, 0}, |
| 565 | {YIN_TEXT, &value, YIN_SUBELEM_FIRST}}; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 566 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 567 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 568 | ret = yin_parse_content(st->yin_ctx, subelems3, 2, &data, YANG_MODULE, NULL, &exts); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 569 | assert_int_equal(ret, LY_EVALID); |
| 570 | logbuf_assert("Subelement text of module element must be defined as first subelement. Line number 1."); |
| 571 | lydict_remove(st->ctx, prefix_value); |
| 572 | st = reset_state(state); |
| 573 | LY_ARRAY_FREE(attrs); |
| 574 | attrs = NULL; |
| 575 | |
| 576 | /* test mandatory subelem */ |
| 577 | data = "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">" |
| 578 | "</module>"; |
| 579 | struct yin_subelement subelems4[1] = {{YANG_PREFIX, &prefix_value, YIN_SUBELEM_MANDATORY}}; |
David Sedlák | da8ffa3 | 2019-07-08 14:17:10 +0200 | [diff] [blame] | 580 | lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len); |
| 581 | yin_load_attributes(st->yin_ctx, &data, &attrs); |
| 582 | ret = yin_parse_content(st->yin_ctx, subelems4, 1, &data, YANG_MODULE, NULL, &exts); |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 583 | assert_int_equal(ret, LY_EVALID); |
| 584 | logbuf_assert("Missing mandatory subelement prefix of module element. Line number 1."); |
| 585 | LY_ARRAY_FREE(attrs); |
| 586 | |
| 587 | st->finished_correctly = true; |
| 588 | } |
| 589 | |
David Sedlák | 92147b0 | 2019-07-09 14:01:01 +0200 | [diff] [blame] | 590 | static void |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 591 | test_validate_value(void **state) |
| 592 | { |
| 593 | struct state *st = *state; |
| 594 | assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "#invalid", 8), LY_EVALID); |
| 595 | logbuf_assert("Invalid identifier character '#'. Line number 1."); |
| 596 | assert_int_equal(yin_validate_value(st->yin_ctx, Y_STR_ARG, "", 0), LY_SUCCESS); |
| 597 | assert_int_equal(yin_validate_value(st->yin_ctx, Y_IDENTIF_ARG, "pre:b", 5), LY_EVALID); |
| 598 | assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:b", 5), LY_SUCCESS); |
| 599 | assert_int_equal(yin_validate_value(st->yin_ctx, Y_PREF_IDENTIF_ARG, "pre:pre:b", 9), LY_EVALID); |
| 600 | |
| 601 | st->finished_correctly = true; |
| 602 | } |
| 603 | |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame^] | 604 | static int |
| 605 | setup_element_test(void **state) |
| 606 | { |
| 607 | struct state *st = NULL; |
| 608 | |
| 609 | #if ENABLE_LOGGER_CHECKING |
| 610 | /* setup logger */ |
| 611 | ly_set_log_clb(logger, 1); |
| 612 | #endif |
| 613 | |
| 614 | /* reset logbuf */ |
| 615 | logbuf[0] = '\0'; |
| 616 | /* allocate state variable */ |
| 617 | (*state) = st = calloc(1, sizeof(*st)); |
| 618 | if (!st) { |
| 619 | fprintf(stderr, "Memmory allocation failed"); |
| 620 | return EXIT_FAILURE; |
| 621 | } |
| 622 | |
| 623 | /* create new libyang context */ |
| 624 | ly_ctx_new(NULL, 0, &st->ctx); |
| 625 | |
| 626 | /* allocate parser context */ |
| 627 | st->yin_ctx = calloc(1, sizeof(*st->yin_ctx)); |
| 628 | st->yin_ctx->xml_ctx.ctx = st->ctx; |
| 629 | st->yin_ctx->xml_ctx.line = 1; |
| 630 | |
| 631 | return EXIT_SUCCESS; |
| 632 | } |
| 633 | |
| 634 | static int |
| 635 | teardown_element_test(void **state) |
| 636 | { |
| 637 | struct state *st = *(struct state **)state; |
| 638 | |
| 639 | #if ENABLE_LOGGER_CHECKING |
| 640 | /* teardown logger */ |
| 641 | if (!st->finished_correctly && logbuf[0] != '\0') { |
| 642 | fprintf(stderr, "%s\n", logbuf); |
| 643 | } |
| 644 | #endif |
| 645 | |
| 646 | lyxml_context_clear(&st->yin_ctx->xml_ctx); |
| 647 | ly_ctx_destroy(st->ctx, NULL); |
| 648 | free(st->yin_ctx); |
| 649 | free(st); |
| 650 | |
| 651 | return EXIT_SUCCESS; |
| 652 | } |
| 653 | |
| 654 | #define ELEMENT_WRAPPER_START "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\">" |
| 655 | #define ELEMENT_WRAPPER_END "</module>" |
| 656 | |
| 657 | /* helper function to simplify unit test of each element using parse_content function */ |
| 658 | LY_ERR |
| 659 | test_element_helper(struct state *st, const char **data, void *dest, const char **text, |
| 660 | struct lysp_ext_instance **exts, bool valid) |
| 661 | { |
| 662 | struct yin_arg_record *attrs = NULL; |
| 663 | struct sized_string name, prefix; |
| 664 | LY_ERR ret = LY_SUCCESS; |
| 665 | struct yin_subelement subelems[71] = { |
| 666 | {YANG_ACTION, dest, 0}, |
| 667 | {YANG_ANYDATA, dest, 0}, |
| 668 | {YANG_ANYXML, dest, 0}, |
| 669 | {YANG_ARGUMENT,dest, 0}, |
| 670 | {YANG_AUGMENT, dest, 0}, |
| 671 | {YANG_BASE, dest, 0}, |
| 672 | {YANG_BELONGS_TO, dest, 0}, |
| 673 | {YANG_BIT, dest, 0}, |
| 674 | {YANG_CASE, dest, 0}, |
| 675 | {YANG_CHOICE, dest, 0}, |
| 676 | {YANG_CONFIG, dest, 0}, |
| 677 | {YANG_CONTACT, dest, 0}, |
| 678 | {YANG_CONTAINER, dest, 0}, |
| 679 | {YANG_DEFAULT, dest, 0}, |
| 680 | {YANG_DESCRIPTION, dest, 0}, |
| 681 | {YANG_DEVIATE, dest, 0}, |
| 682 | {YANG_DEVIATION, dest, 0}, |
| 683 | {YANG_ENUM, dest, 0}, |
| 684 | {YANG_ERROR_APP_TAG, dest, 0}, |
| 685 | {YANG_ERROR_MESSAGE, dest, 0}, |
| 686 | {YANG_EXTENSION, dest, 0}, |
| 687 | {YANG_FEATURE, dest, 0}, |
| 688 | {YANG_FRACTION_DIGITS, dest, 0}, |
| 689 | {YANG_GROUPING, dest, 0}, |
| 690 | {YANG_IDENTITY, dest, 0}, |
| 691 | {YANG_IF_FEATURE, dest, 0}, |
| 692 | {YANG_IMPORT, dest, 0}, |
| 693 | {YANG_INCLUDE, dest, 0}, |
| 694 | {YANG_INPUT, dest, 0}, |
| 695 | {YANG_KEY, dest, 0}, |
| 696 | {YANG_LEAF, dest, 0}, |
| 697 | {YANG_LEAF_LIST, dest, 0}, |
| 698 | {YANG_LENGTH, dest, 0}, |
| 699 | {YANG_LIST, dest, 0}, |
| 700 | {YANG_MANDATORY, dest, 0}, |
| 701 | {YANG_MAX_ELEMENTS, dest, 0}, |
| 702 | {YANG_MIN_ELEMENTS, dest, 0}, |
| 703 | {YANG_MODIFIER, dest, 0}, |
| 704 | {YANG_MODULE, dest, 0}, |
| 705 | {YANG_MUST, dest, 0}, |
| 706 | {YANG_NAMESPACE, dest, 0}, |
| 707 | {YANG_NOTIFICATION, dest, 0}, |
| 708 | {YANG_ORDERED_BY, dest, 0}, |
| 709 | {YANG_ORGANIZATION, dest, 0}, |
| 710 | {YANG_OUTPUT, dest, 0}, |
| 711 | {YANG_PATH, dest, 0}, |
| 712 | {YANG_PATTERN, dest, 0}, |
| 713 | {YANG_POSITION, dest, 0}, |
| 714 | {YANG_PREFIX, dest, 0}, |
| 715 | {YANG_PRESENCE, dest, 0}, |
| 716 | {YANG_RANGE, dest, 0}, |
| 717 | {YANG_REFERENCE, dest, 0}, |
| 718 | {YANG_REFINE, dest, 0}, |
| 719 | {YANG_REQUIRE_INSTANCE, dest, 0}, |
| 720 | {YANG_REVISION, dest, 0}, |
| 721 | {YANG_REVISION_DATE, dest, 0}, |
| 722 | {YANG_RPC, dest, 0}, |
| 723 | {YANG_STATUS, dest, 0}, |
| 724 | {YANG_SUBMODULE, dest, 0}, |
| 725 | {YANG_TYPE, dest, 0}, |
| 726 | {YANG_TYPEDEF, dest, 0}, |
| 727 | {YANG_UNIQUE, dest, 0}, |
| 728 | {YANG_UNITS, dest, 0}, |
| 729 | {YANG_USES, dest, 0}, |
| 730 | {YANG_VALUE, dest, 0}, |
| 731 | {YANG_WHEN, dest, 0}, |
| 732 | {YANG_YANG_VERSION, dest, 0}, |
| 733 | {YANG_YIN_ELEMENT, dest, 0}, |
| 734 | {YANG_CUSTOM, dest, 0}, |
| 735 | {YIN_TEXT, dest, 0}, |
| 736 | {YIN_VALUE, dest, 0} |
| 737 | }; |
| 738 | LY_CHECK_RET(lyxml_get_element(&st->yin_ctx->xml_ctx, data, &prefix.value, &prefix.len, &name.value, &name.len));\ |
| 739 | LY_CHECK_RET(yin_load_attributes(st->yin_ctx, data, &attrs));\ |
| 740 | ret = yin_parse_content(st->yin_ctx, subelems, 71, data, YANG_MODULE, text, exts); |
| 741 | LY_ARRAY_FREE(attrs); |
| 742 | if (valid) { |
| 743 | assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END); |
| 744 | } |
| 745 | /* reset status */ |
| 746 | st->yin_ctx->xml_ctx.status = LYXML_ELEMENT; |
| 747 | return ret; |
| 748 | } |
| 749 | |
| 750 | static void |
| 751 | test_enum_bit_elem(void **state) |
| 752 | { |
| 753 | /* yin_parse_enum_bit is function that is being mainly tested by this test */ |
| 754 | struct state *st = *state; |
| 755 | struct lysp_type type = {}; |
| 756 | const char *data; |
| 757 | data = ELEMENT_WRAPPER_START |
| 758 | "<enum name=\"enum-name\">" |
| 759 | "<if-feature name=\"feature\" />" |
| 760 | "<value value=\"55\" />" |
| 761 | "<status value=\"deprecated\" />" |
| 762 | "<description><text>desc...</text></description>" |
| 763 | "<reference><text>ref...</text></reference>" |
| 764 | "</enum>" |
| 765 | ELEMENT_WRAPPER_END; |
| 766 | assert_int_equal(test_element_helper(st, &data, &type, NULL, NULL, true), LY_SUCCESS); |
| 767 | assert_string_equal(*type.enums->iffeatures, "feature"); |
| 768 | assert_int_equal(type.enums->value, 55); |
| 769 | assert_true(type.enums->flags | LYS_STATUS_DEPRC && type.enums->flags | LYS_SET_VALUE); |
| 770 | assert_string_equal(type.enums->dsc, "desc..."); |
| 771 | assert_string_equal(type.enums->ref, "ref..."); |
| 772 | lysp_type_free(st->ctx, &type); |
| 773 | memset(&type, 0, sizeof type); |
| 774 | |
| 775 | /* todo bit element test */ |
| 776 | st->finished_correctly = true; |
| 777 | } |
| 778 | |
| 779 | static void |
| 780 | test_meta_elem(void **state) |
| 781 | { |
| 782 | struct state *st = *state; |
| 783 | char *value = NULL; |
| 784 | const char *data; |
| 785 | |
| 786 | /* organization element */ |
| 787 | data = ELEMENT_WRAPPER_START |
| 788 | "<organization><text>organization...</text></organization>" |
| 789 | ELEMENT_WRAPPER_END; |
| 790 | assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS); |
| 791 | assert_string_equal(value, "organization..."); |
| 792 | FREE_STRING(st->ctx, value); |
| 793 | value = NULL; |
| 794 | /* contact element */ |
| 795 | data = ELEMENT_WRAPPER_START |
| 796 | "<contact><text>contact...</text></contact>" |
| 797 | ELEMENT_WRAPPER_END; |
| 798 | assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS); |
| 799 | assert_string_equal(value, "contact..."); |
| 800 | FREE_STRING(st->ctx, value); |
| 801 | value = NULL; |
| 802 | /* description element */ |
| 803 | data = ELEMENT_WRAPPER_START |
| 804 | "<description><text>description...</text></description>" |
| 805 | ELEMENT_WRAPPER_END; |
| 806 | assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS); |
| 807 | assert_string_equal(value, "description..."); |
| 808 | FREE_STRING(st->ctx, value); |
| 809 | value = NULL; |
| 810 | /* reference element */ |
| 811 | data = ELEMENT_WRAPPER_START |
| 812 | "<reference><text>reference...</text></reference>" |
| 813 | ELEMENT_WRAPPER_END; |
| 814 | assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, true), LY_SUCCESS); |
| 815 | assert_string_equal(value, "reference..."); |
| 816 | FREE_STRING(st->ctx, value); |
| 817 | value = NULL; |
| 818 | |
| 819 | /* missing text subelement */ |
| 820 | data = ELEMENT_WRAPPER_START |
| 821 | "<reference>reference...</reference>" |
| 822 | ELEMENT_WRAPPER_END; |
| 823 | assert_int_equal(test_element_helper(st, &data, &value, NULL, NULL, false), LY_EVALID); |
| 824 | logbuf_assert("Missing mandatory subelement text of reference element. Line number 1."); |
| 825 | |
| 826 | st->finished_correctly = true; |
| 827 | } |
| 828 | |
| 829 | static void |
| 830 | test_import_elem(void **state) |
| 831 | { |
| 832 | struct state *st = *state; |
| 833 | const char *data; |
| 834 | struct lys_module *lys_mod = calloc(1, sizeof *lys_mod); |
| 835 | struct lysp_module *lysp_mod = calloc(1, sizeof *lysp_mod); |
| 836 | lys_mod->ctx = st->ctx; |
| 837 | lysp_mod->mod = lys_mod; |
| 838 | |
| 839 | /* max subelems */ |
| 840 | data = ELEMENT_WRAPPER_START |
| 841 | "<import module=\"a\">" |
| 842 | "<prefix value=\"a_mod\"/>" |
| 843 | "<revision-date date=\"2015-01-01\"></revision-date>" |
| 844 | "<description><text>import description</text></description>" |
| 845 | "<reference><text>import reference</text></reference>" |
| 846 | "</import>" |
| 847 | ELEMENT_WRAPPER_END; |
| 848 | assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, true), LY_SUCCESS); |
| 849 | assert_string_equal(lysp_mod->imports->name, "a"); |
| 850 | assert_string_equal(lysp_mod->imports->prefix, "a_mod"); |
| 851 | assert_string_equal(lysp_mod->imports->rev, "2015-01-01"); |
| 852 | assert_string_equal(lysp_mod->imports->dsc, "import description"); |
| 853 | assert_string_equal(lysp_mod->imports->ref, "import reference"); |
| 854 | lysp_module_free(lysp_mod); |
| 855 | lys_module_free(lys_mod, NULL); |
| 856 | |
| 857 | /* min subelems */ |
| 858 | lys_mod = calloc(1, sizeof *lys_mod); |
| 859 | lysp_mod = calloc(1, sizeof *lysp_mod); |
| 860 | lys_mod->ctx = st->ctx; |
| 861 | lysp_mod->mod = lys_mod; |
| 862 | data = ELEMENT_WRAPPER_START |
| 863 | "<import module=\"a\">" |
| 864 | "<prefix value=\"a_mod\"/>" |
| 865 | "</import>" |
| 866 | ELEMENT_WRAPPER_END; |
| 867 | assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, true), LY_SUCCESS); |
| 868 | assert_string_equal(lysp_mod->imports->prefix, "a_mod"); |
| 869 | lysp_module_free(lysp_mod); |
| 870 | lys_module_free(lys_mod, NULL); |
| 871 | |
| 872 | /* invalid (missing prefix) */ |
| 873 | lys_mod = calloc(1, sizeof *lys_mod); |
| 874 | lysp_mod = calloc(1, sizeof *lysp_mod); |
| 875 | lys_mod->ctx = st->ctx; |
| 876 | lysp_mod->mod = lys_mod; |
| 877 | data = ELEMENT_WRAPPER_START "<import module=\"a\">""</import>" ELEMENT_WRAPPER_END; |
| 878 | assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, false), LY_EVALID); |
| 879 | logbuf_assert("Missing mandatory subelement prefix of import element. Line number 1."); |
| 880 | lysp_module_free(lysp_mod); |
| 881 | lys_module_free(lys_mod, NULL); |
| 882 | |
| 883 | /* invalid reused prefix */ |
| 884 | lys_mod = calloc(1, sizeof *lys_mod); |
| 885 | lysp_mod = calloc(1, sizeof *lysp_mod); |
| 886 | lys_mod->ctx = st->ctx; |
| 887 | lysp_mod->mod = lys_mod; |
| 888 | data = ELEMENT_WRAPPER_START |
| 889 | "<import module=\"a\">" |
| 890 | "<prefix value=\"a_mod\"/>" |
| 891 | "</import>" |
| 892 | "<import module=\"a\">" |
| 893 | "<prefix value=\"a_mod\"/>" |
| 894 | "</import>" |
| 895 | ELEMENT_WRAPPER_END; |
| 896 | assert_int_equal(test_element_helper(st, &data, lysp_mod, NULL, NULL, false), LY_EVALID); |
| 897 | logbuf_assert("Prefix \"a_mod\" already used to import \"a\" module. Line number 1."); |
| 898 | lysp_module_free(lysp_mod); |
| 899 | lys_module_free(lys_mod, NULL); |
| 900 | |
| 901 | st->finished_correctly = true; |
| 902 | } |
| 903 | |
| 904 | static void |
| 905 | test_status_elem(void **state) |
| 906 | { |
| 907 | struct state *st = *state; |
| 908 | const char *data; |
| 909 | uint16_t flags = 0; |
| 910 | |
| 911 | /* test valid values */ |
| 912 | data = ELEMENT_WRAPPER_START "<status value=\"current\" />" ELEMENT_WRAPPER_END; |
| 913 | assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS); |
| 914 | assert_true(flags | LYS_STATUS_CURR); |
| 915 | |
| 916 | data = ELEMENT_WRAPPER_START "<status value=\"deprecated\" />" ELEMENT_WRAPPER_END; |
| 917 | assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS); |
| 918 | assert_true(flags | LYS_STATUS_DEPRC); |
| 919 | |
| 920 | data = ELEMENT_WRAPPER_START "<status value=\"obsolete\"></status>" ELEMENT_WRAPPER_END; |
| 921 | assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS); |
| 922 | assert_true(flags | LYS_STATUS_OBSLT); |
| 923 | |
| 924 | /* test invalid value */ |
| 925 | data = ELEMENT_WRAPPER_START "<status value=\"invalid\"></status>" ELEMENT_WRAPPER_END; |
| 926 | assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID); |
| 927 | logbuf_assert("Invalid value \"invalid\" of \"status\". Line number 1."); |
| 928 | st->finished_correctly = true; |
| 929 | } |
| 930 | |
| 931 | static void |
| 932 | test_ext_elem(void **state) |
| 933 | { |
| 934 | struct state *st = *state; |
| 935 | const char *data; |
| 936 | struct lysp_ext *ext = NULL; |
| 937 | |
| 938 | /* max subelems */ |
| 939 | data = ELEMENT_WRAPPER_START |
| 940 | "<extension name=\"ext_name\">" |
| 941 | "<argument name=\"arg\"></argument>" |
| 942 | "<status value=\"current\"/>" |
| 943 | "<description><text>ext_desc</text></description>" |
| 944 | "<reference><text>ext_ref</text></reference>" |
| 945 | "</extension>" |
| 946 | ELEMENT_WRAPPER_END; |
| 947 | assert_int_equal(test_element_helper(st, &data, &ext, NULL, NULL, true), LY_SUCCESS); |
| 948 | assert_string_equal(ext->name, "ext_name"); |
| 949 | assert_string_equal(ext->argument, "arg"); |
| 950 | assert_true(ext->flags | LYS_STATUS_CURR); |
| 951 | assert_string_equal(ext->dsc, "ext_desc"); |
| 952 | assert_string_equal(ext->ref, "ext_ref"); |
| 953 | lysp_ext_free(st->ctx, ext); |
| 954 | LY_ARRAY_FREE(ext); |
| 955 | ext = NULL; |
| 956 | |
| 957 | /* min subelems */ |
| 958 | data = ELEMENT_WRAPPER_START "<extension name=\"ext_name\"></extension>" ELEMENT_WRAPPER_END; |
| 959 | assert_int_equal(test_element_helper(st, &data, &ext, NULL, NULL, true), LY_SUCCESS); |
| 960 | assert_string_equal(ext->name, "ext_name"); |
| 961 | lysp_ext_free(st->ctx, ext); |
| 962 | LY_ARRAY_FREE(ext); |
| 963 | ext = NULL; |
| 964 | |
| 965 | st->finished_correctly = true; |
| 966 | } |
| 967 | |
| 968 | static void |
| 969 | test_yin_element_elem(void **state) |
| 970 | { |
| 971 | struct state *st = *state; |
| 972 | const char *data; |
| 973 | uint16_t flags = 0; |
| 974 | |
| 975 | data = ELEMENT_WRAPPER_START "<yin-element value=\"true\" />" ELEMENT_WRAPPER_END; |
| 976 | assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS); |
| 977 | assert_true(flags | LYS_YINELEM_TRUE); |
| 978 | |
| 979 | data = ELEMENT_WRAPPER_START "<yin-element value=\"false\" />" ELEMENT_WRAPPER_END; |
| 980 | assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, true), LY_SUCCESS); |
| 981 | assert_true(flags | LYS_YINELEM_TRUE); |
| 982 | |
| 983 | data = ELEMENT_WRAPPER_START "<yin-element value=\"invalid\" />" ELEMENT_WRAPPER_END; |
| 984 | assert_int_equal(test_element_helper(st, &data, &flags, NULL, NULL, false), LY_EVALID); |
| 985 | assert_true(flags | LYS_YINELEM_TRUE); |
| 986 | logbuf_assert("Invalid value \"invalid\" of \"yin-element\". Line number 1."); |
| 987 | st->finished_correctly = true; |
| 988 | } |
| 989 | |
| 990 | static void |
| 991 | test_yangversion_elem(void **state) |
| 992 | { |
| 993 | struct state *st = *state; |
| 994 | const char *data; |
| 995 | uint8_t version = 0; |
| 996 | |
| 997 | /* valid values */ |
| 998 | data = ELEMENT_WRAPPER_START "<yang-version value=\"1.0\" />" ELEMENT_WRAPPER_END; |
| 999 | assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, true), LY_SUCCESS); |
| 1000 | assert_true(version | LYS_VERSION_1_0); |
| 1001 | assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_0); |
| 1002 | |
| 1003 | data = ELEMENT_WRAPPER_START "<yang-version value=\"1.1\" />" ELEMENT_WRAPPER_END; |
| 1004 | assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, true), LY_SUCCESS); |
| 1005 | assert_true(version | LYS_VERSION_1_1); |
| 1006 | assert_int_equal(st->yin_ctx->mod_version, LYS_VERSION_1_1); |
| 1007 | |
| 1008 | /* invalid value */ |
| 1009 | data = ELEMENT_WRAPPER_START "<yang-version value=\"version\" />" ELEMENT_WRAPPER_END; |
| 1010 | assert_int_equal(test_element_helper(st, &data, &version, NULL, NULL, false), LY_EVALID); |
| 1011 | logbuf_assert("Invalid value \"version\" of \"yang-version\". Line number 1."); |
| 1012 | |
| 1013 | st->finished_correctly = true; |
| 1014 | } |
| 1015 | |
| 1016 | static void |
| 1017 | test_mandatory_elem(void **state) |
| 1018 | { |
| 1019 | struct state *st = *state; |
| 1020 | const char *data; |
| 1021 | uint16_t man = 0; |
| 1022 | |
| 1023 | /* valid values */ |
| 1024 | data = ELEMENT_WRAPPER_START "<mandatory value=\"true\" />" ELEMENT_WRAPPER_END; |
| 1025 | assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, true), LY_SUCCESS); |
| 1026 | assert_int_equal(man, LYS_MAND_TRUE); |
| 1027 | man = 0; |
| 1028 | |
| 1029 | data = ELEMENT_WRAPPER_START "<mandatory value=\"false\" />" ELEMENT_WRAPPER_END; |
| 1030 | assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, true), LY_SUCCESS); |
| 1031 | assert_int_equal(man, LYS_MAND_FALSE); |
| 1032 | |
| 1033 | data = ELEMENT_WRAPPER_START "<mandatory value=\"invalid\" />" ELEMENT_WRAPPER_END; |
| 1034 | assert_int_equal(test_element_helper(st, &data, &man, NULL, NULL, false), LY_EVALID); |
| 1035 | logbuf_assert("Invalid value \"invalid\" of \"mandatory\". Line number 1."); |
| 1036 | |
| 1037 | st->finished_correctly = true; |
| 1038 | } |
| 1039 | |
| 1040 | |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 1041 | int |
| 1042 | main(void) |
| 1043 | { |
| 1044 | |
| 1045 | const struct CMUnitTest tests[] = { |
David Sedlák | 392af4f | 2019-06-04 16:02:42 +0200 | [diff] [blame] | 1046 | cmocka_unit_test_setup_teardown(test_yin_parse_module, setup_f, teardown_f), |
David Sedlák | 8f7a117 | 2019-06-20 14:42:18 +0200 | [diff] [blame] | 1047 | cmocka_unit_test_setup_teardown(test_yin_match_keyword, setup_f, teardown_f), |
David Sedlák | b1a7835 | 2019-06-28 16:16:29 +0200 | [diff] [blame] | 1048 | cmocka_unit_test_setup_teardown(test_yin_parse_element_generic, setup_f, teardown_f), |
| 1049 | cmocka_unit_test_setup_teardown(test_yin_parse_extension_instance, setup_f, teardown_f), |
David Sedlák | 555c720 | 2019-07-04 12:14:12 +0200 | [diff] [blame] | 1050 | cmocka_unit_test_setup_teardown(test_yin_parse_content, setup_f, teardown_f), |
David Sedlák | 4a65053 | 2019-07-10 11:55:18 +0200 | [diff] [blame] | 1051 | cmocka_unit_test_setup_teardown(test_validate_value, setup_f, teardown_f), |
David Sedlák | 060b00e | 2019-06-19 11:12:06 +0200 | [diff] [blame] | 1052 | cmocka_unit_test(test_yin_match_argument_name), |
David Sedlák | 3248810 | 2019-07-15 17:44:10 +0200 | [diff] [blame^] | 1053 | |
| 1054 | cmocka_unit_test_setup_teardown(test_enum_bit_elem, setup_element_test, teardown_element_test), |
| 1055 | cmocka_unit_test_setup_teardown(test_meta_elem, setup_element_test, teardown_element_test), |
| 1056 | cmocka_unit_test_setup_teardown(test_import_elem, setup_element_test, teardown_element_test), |
| 1057 | cmocka_unit_test_setup_teardown(test_status_elem, setup_element_test, teardown_element_test), |
| 1058 | cmocka_unit_test_setup_teardown(test_ext_elem, setup_element_test, teardown_element_test), |
| 1059 | cmocka_unit_test_setup_teardown(test_yin_element_elem, setup_element_test, teardown_element_test), |
| 1060 | cmocka_unit_test_setup_teardown(test_yangversion_elem, setup_element_test, teardown_element_test), |
| 1061 | cmocka_unit_test_setup_teardown(test_mandatory_elem, setup_element_test, teardown_element_test), |
David Sedlák | 3b4db24 | 2018-10-19 16:11:01 +0200 | [diff] [blame] | 1062 | }; |
| 1063 | |
| 1064 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 1065 | } |