Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 1 | /* |
| 2 | * @file test_parser_yang.c |
| 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief unit tests for functions from parser_yang.c |
| 5 | * |
| 6 | * Copyright (c) 2018 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 | |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 15 | #include <stdarg.h> |
| 16 | #include <stddef.h> |
| 17 | #include <setjmp.h> |
| 18 | #include <cmocka.h> |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 23 | #include "../../src/common.h" |
| 24 | #include "../../src/tree_schema.h" |
| 25 | #include "../../src/tree_schema_internal.h" |
| 26 | |
| 27 | /* originally static functions from tree_schema_free.c and parser_yang.c */ |
| 28 | void lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext); |
| 29 | void lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident); |
| 30 | void lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat); |
| 31 | void lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev); |
| 32 | void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp); |
| 33 | void lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action); |
| 34 | void lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif); |
| 35 | void lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment); |
| 36 | void lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d); |
| 37 | void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node); |
| 38 | |
| 39 | LY_ERR buf_add_char(struct ly_ctx *ctx, const char **input, size_t len, char **buf, size_t *buf_len, size_t *buf_used); |
| 40 | LY_ERR buf_store_char(struct lys_parser_ctx *ctx, const char **input, enum yang_arg arg, |
| 41 | char **word_p, size_t *word_len, char **word_b, size_t *buf_len, int need_buf); |
| 42 | LY_ERR get_keyword(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword *kw, char **word_p, size_t *word_len); |
| 43 | LY_ERR get_argument(struct lys_parser_ctx *ctx, const char **data, enum yang_arg arg, |
| 44 | uint16_t *flags, char **word_p, char **word_b, size_t *word_len); |
| 45 | LY_ERR skip_comment(struct lys_parser_ctx *ctx, const char **data, int comment); |
| 46 | LY_ERR check_identifierchar(struct lys_parser_ctx *ctx, unsigned int c, int first, int *prefix); |
| 47 | |
| 48 | LY_ERR parse_action(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions); |
| 49 | LY_ERR parse_any(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword kw, struct lysp_node *parent, struct lysp_node **siblings); |
| 50 | LY_ERR parse_augment(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_augment **augments); |
| 51 | LY_ERR parse_case(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings); |
| 52 | LY_ERR parse_container(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings); |
| 53 | LY_ERR parse_deviate(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates); |
| 54 | LY_ERR parse_deviation(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations); |
| 55 | LY_ERR parse_feature(struct lys_parser_ctx *ctx, const char **data, struct lysp_feature **features); |
| 56 | LY_ERR parse_grouping(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings); |
| 57 | LY_ERR parse_choice(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings); |
| 58 | LY_ERR parse_identity(struct lys_parser_ctx *ctx, const char **data, struct lysp_ident **identities); |
| 59 | LY_ERR parse_leaf(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings); |
| 60 | LY_ERR parse_leaflist(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings); |
| 61 | LY_ERR parse_list(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings); |
| 62 | LY_ERR parse_maxelements(struct lys_parser_ctx *ctx, const char **data, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts); |
| 63 | LY_ERR parse_minelements(struct lys_parser_ctx *ctx, const char **data, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts); |
| 64 | LY_ERR parse_module(struct lys_parser_ctx *ctx, const char **data, struct lysp_module *mod); |
| 65 | LY_ERR parse_notif(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_notif **notifs); |
| 66 | LY_ERR parse_submodule(struct lys_parser_ctx *ctx, const char **data, struct lysp_submodule *submod); |
| 67 | LY_ERR parse_uses(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 68 | |
| 69 | #define BUFSIZE 1024 |
| 70 | char logbuf[BUFSIZE] = {0}; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 71 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 72 | |
| 73 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 74 | #define ENABLE_LOGGER_CHECKING 1 |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 75 | |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 76 | #if ENABLE_LOGGER_CHECKING |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 77 | static void |
| 78 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 79 | { |
| 80 | (void) level; /* unused */ |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 81 | if (store) { |
| 82 | if (path && path[0]) { |
| 83 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 84 | } else { |
| 85 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 86 | } |
| 87 | if (store > 0) { |
| 88 | --store; |
| 89 | } |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 90 | } |
| 91 | } |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 92 | #endif |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 93 | |
| 94 | static int |
| 95 | logger_setup(void **state) |
| 96 | { |
| 97 | (void) state; /* unused */ |
| 98 | #if ENABLE_LOGGER_CHECKING |
| 99 | ly_set_log_clb(logger, 1); |
| 100 | #endif |
| 101 | return 0; |
| 102 | } |
| 103 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 104 | static int |
| 105 | logger_teardown(void **state) |
| 106 | { |
| 107 | (void) state; /* unused */ |
| 108 | #if ENABLE_LOGGER_CHECKING |
| 109 | if (*state) { |
| 110 | fprintf(stderr, "%s\n", logbuf); |
| 111 | } |
| 112 | #endif |
| 113 | return 0; |
| 114 | } |
| 115 | |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 116 | void |
| 117 | logbuf_clean(void) |
| 118 | { |
| 119 | logbuf[0] = '\0'; |
| 120 | } |
| 121 | |
| 122 | #if ENABLE_LOGGER_CHECKING |
| 123 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 124 | #else |
| 125 | # define logbuf_assert(str) |
| 126 | #endif |
| 127 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 128 | #define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \ |
| 129 | str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 130 | assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \ |
| 131 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \ |
| 132 | CLEANUP |
| 133 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 134 | static void |
| 135 | test_helpers(void **state) |
| 136 | { |
| 137 | (void) state; /* unused */ |
| 138 | |
| 139 | const char *str; |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 140 | char *buf, *p; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 141 | size_t len, size; |
| 142 | int prefix; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 143 | struct lys_parser_ctx ctx; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 144 | ctx.ctx = NULL; |
| 145 | ctx.line = 1; |
| 146 | |
| 147 | /* storing into buffer */ |
| 148 | str = "abcd"; |
| 149 | buf = NULL; |
| 150 | size = len = 0; |
| 151 | assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &str, 2, &buf, &size, &len)); |
| 152 | assert_int_not_equal(0, size); |
| 153 | assert_int_equal(2, len); |
| 154 | assert_string_equal("cd", str); |
| 155 | assert_false(strncmp("ab", buf, 2)); |
| 156 | free(buf); |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 157 | buf = NULL; |
| 158 | |
| 159 | /* invalid first characters */ |
| 160 | len = 0; |
| 161 | str = "2invalid"; |
| 162 | assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1)); |
| 163 | str = ".invalid"; |
| 164 | assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1)); |
| 165 | str = "-invalid"; |
| 166 | assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1)); |
| 167 | /* invalid following characters */ |
| 168 | len = 3; /* number of characters read before the str content */ |
| 169 | str = "!"; |
| 170 | assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1)); |
| 171 | str = ":"; |
| 172 | assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1)); |
| 173 | /* valid colon for prefixed identifiers */ |
| 174 | len = size = 0; |
| 175 | p = NULL; |
| 176 | str = "x:id"; |
| 177 | assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 0)); |
| 178 | assert_int_equal(1, len); |
| 179 | assert_null(buf); |
| 180 | assert_string_equal(":id", str); |
| 181 | assert_int_equal('x', p[len - 1]); |
| 182 | assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1)); |
| 183 | assert_int_equal(2, len); |
| 184 | assert_string_equal("id", str); |
| 185 | assert_int_equal(':', p[len - 1]); |
| 186 | free(buf); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 187 | |
| 188 | /* checking identifiers */ |
| 189 | assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, NULL)); |
| 190 | logbuf_assert("Invalid identifier character ':'. Line number 1."); |
| 191 | assert_int_equal(LY_EVALID, check_identifierchar(&ctx, '#', 1, NULL)); |
| 192 | logbuf_assert("Invalid identifier first character '#'. Line number 1."); |
| 193 | |
| 194 | assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'a', 1, &prefix)); |
| 195 | assert_int_equal(0, prefix); |
| 196 | assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, ':', 0, &prefix)); |
| 197 | assert_int_equal(1, prefix); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 198 | assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix)); |
| 199 | assert_int_equal(1, prefix); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 200 | assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'b', 0, &prefix)); |
| 201 | assert_int_equal(2, prefix); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 202 | /* second colon is invalid */ |
| 203 | assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix)); |
| 204 | logbuf_assert("Invalid identifier character ':'. Line number 1."); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 205 | } |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 206 | |
| 207 | static void |
| 208 | test_comments(void **state) |
| 209 | { |
| 210 | (void) state; /* unused */ |
| 211 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 212 | struct lys_parser_ctx ctx; |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 213 | const char *str, *p; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 214 | char *word, *buf; |
| 215 | size_t len; |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 216 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 217 | ctx.ctx = NULL; |
| 218 | ctx.line = 1; |
| 219 | |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 220 | str = " // this is a text of / one * line */ comment\nargument;"; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 221 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 222 | assert_string_equal("argument;", word); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 223 | assert_null(buf); |
| 224 | assert_int_equal(8, len); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 225 | |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 226 | str = "/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\";"; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 227 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 228 | assert_string_equal("argument", word); |
| 229 | assert_ptr_equal(buf, word); |
| 230 | assert_int_equal(8, len); |
| 231 | free(word); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 232 | |
| 233 | str = p = " this is one line comment on last line"; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 234 | assert_int_equal(LY_SUCCESS, skip_comment(&ctx, &str, 1)); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 235 | assert_true(str[0] == '\0'); |
| 236 | |
| 237 | str = p = " this is a not terminated comment x"; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 238 | assert_int_equal(LY_EVALID, skip_comment(&ctx, &str, 2)); |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 239 | logbuf_assert("Unexpected end-of-input, non-terminated comment. Line number 5."); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 240 | assert_true(str[0] == '\0'); |
| 241 | } |
| 242 | |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 243 | static void |
| 244 | test_arg(void **state) |
| 245 | { |
| 246 | (void) state; /* unused */ |
| 247 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 248 | struct lys_parser_ctx ctx; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 249 | const char *str; |
| 250 | char *word, *buf; |
| 251 | size_t len; |
| 252 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 253 | ctx.ctx = NULL; |
| 254 | ctx.line = 1; |
| 255 | |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 256 | /* missing argument */ |
| 257 | str = ";"; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 258 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len)); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 259 | assert_null(word); |
| 260 | |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 261 | str = "{"; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 262 | assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 263 | logbuf_assert("Invalid character sequence \"{\", expected an argument. Line number 1."); |
| 264 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 265 | /* invalid escape sequence */ |
| 266 | str = "\"\\s\""; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 267 | assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 268 | logbuf_assert("Double-quoted string unknown special character \'\\s\'. Line number 1."); |
| 269 | str = "\'\\s\'"; /* valid, since it is not an escape sequence in single quoted string */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 270 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 271 | assert_int_equal(2, len); |
| 272 | assert_string_equal("\\s\'", word); |
| 273 | assert_int_equal('\0', str[0]); /* input has been eaten */ |
| 274 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 275 | /* invalid character after the argument */ |
| 276 | str = "hello\""; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 277 | assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 278 | logbuf_assert("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1."); |
| 279 | str = "hello}"; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 280 | assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 281 | logbuf_assert("Invalid character sequence \"}\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1."); |
| 282 | |
Radek Krejci | 4e199f5 | 2019-05-28 09:09:28 +0200 | [diff] [blame] | 283 | str = "\"\";"; /* empty identifier is not allowed */ |
| 284 | assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_IDENTIF_ARG, NULL, &word, &buf, &len)); |
| 285 | logbuf_assert("Statement argument is required. Line number 1."); |
| 286 | logbuf_clean(); |
| 287 | str = "\"\";"; /* empty reference identifier is not allowed */ |
| 288 | assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len)); |
| 289 | logbuf_assert("Statement argument is required. Line number 1."); |
| 290 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 291 | str = "hello/x\t"; /* slash is not an invalid character */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 292 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 293 | assert_int_equal(7, len); |
| 294 | assert_string_equal("hello/x\t", word); |
| 295 | |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 296 | assert_null(buf); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 297 | |
| 298 | /* different quoting */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 299 | str = "hello "; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 300 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 301 | assert_null(buf); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 302 | assert_int_equal(5, len); |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 303 | assert_string_equal("hello ", word); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 304 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 305 | str = "hello/*comment*/\n"; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 306 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 307 | assert_null(buf); |
| 308 | assert_int_equal(5, len); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 309 | assert_false(strncmp("hello", word, len)); |
| 310 | |
| 311 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 312 | str = "\"hello\\n\\t\\\"\\\\\";"; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 313 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 314 | assert_null(buf); |
| 315 | assert_int_equal(9, len); |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 316 | assert_string_equal("hello\\n\\t\\\"\\\\\";", word); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 317 | |
| 318 | ctx.indent = 14; |
| 319 | str = "\"hello \t\n\t\t world!\""; |
| 320 | /* - space and tabs before newline are stripped out |
| 321 | * - space and tabs after newline (indentation) are stripped out |
| 322 | */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 323 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 324 | assert_non_null(buf); |
| 325 | assert_ptr_equal(word, buf); |
| 326 | assert_int_equal(14, len); |
| 327 | assert_string_equal("hello\n world!", word); |
| 328 | free(buf); |
| 329 | |
| 330 | ctx.indent = 14; |
| 331 | str = "\"hello\n \tworld!\""; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 332 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 333 | assert_non_null(buf); |
| 334 | assert_ptr_equal(word, buf); |
| 335 | assert_int_equal(12, len); |
| 336 | assert_string_equal("hello\nworld!", word); |
| 337 | free(buf); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 338 | |
| 339 | str = "\'hello\'"; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 340 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 341 | assert_null(buf); |
| 342 | assert_int_equal(5, len); |
| 343 | assert_false(strncmp("hello", word, 5)); |
| 344 | |
| 345 | str = "\"hel\" +\t\n\"lo\""; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 346 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 347 | assert_ptr_equal(word, buf); |
| 348 | assert_int_equal(5, len); |
| 349 | assert_string_equal("hello", word); |
| 350 | free(buf); |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 351 | str = "\"hel\" +\t\nlo"; /* unquoted the second part */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 352 | assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 353 | logbuf_assert("Both string parts divided by '+' must be quoted. Line number 5."); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 354 | |
| 355 | str = "\'he\'\t\n+ \"llo\""; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 356 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 357 | assert_ptr_equal(word, buf); |
| 358 | assert_int_equal(5, len); |
| 359 | assert_string_equal("hello", word); |
| 360 | free(buf); |
| 361 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 362 | str = " \t\n\"he\"+\'llo\'"; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 363 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 364 | assert_ptr_equal(word, buf); |
| 365 | assert_int_equal(5, len); |
| 366 | assert_string_equal("hello", word); |
| 367 | free(buf); |
| 368 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 369 | /* missing argument */ |
| 370 | str = ";"; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 371 | assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 372 | logbuf_assert("Invalid character sequence \";\", expected an argument. Line number 7."); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static void |
| 376 | test_stmts(void **state) |
| 377 | { |
| 378 | (void) state; /* unused */ |
| 379 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 380 | struct lys_parser_ctx ctx; |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 381 | const char *str, *p; |
| 382 | enum yang_keyword kw; |
| 383 | char *word; |
| 384 | size_t len; |
| 385 | |
| 386 | ctx.ctx = NULL; |
| 387 | ctx.line = 1; |
| 388 | |
| 389 | str = "\n// comment\n\tinput\t{"; |
| 390 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 391 | assert_int_equal(YANG_INPUT, kw); |
| 392 | assert_int_equal(5, len); |
| 393 | assert_string_equal("input\t{", word); |
| 394 | assert_string_equal("\t{", str); |
| 395 | |
| 396 | str = "\t /* comment */\t output\n\t{"; |
| 397 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 398 | assert_int_equal(YANG_OUTPUT, kw); |
| 399 | assert_int_equal(6, len); |
| 400 | assert_string_equal("output\n\t{", word); |
| 401 | assert_string_equal("\n\t{", str); |
Radek Krejci | abdd806 | 2019-06-11 16:44:19 +0200 | [diff] [blame] | 402 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 403 | assert_int_equal(YANG_LEFT_BRACE, kw); |
| 404 | assert_int_equal(1, len); |
| 405 | assert_string_equal("{", word); |
| 406 | assert_string_equal("", str); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 407 | |
| 408 | str = "/input { "; /* invalid slash */ |
| 409 | assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 410 | logbuf_assert("Invalid identifier first character '/'. Line number 4."); |
| 411 | |
| 412 | str = "not-a-statement-nor-extension { "; /* invalid identifier */ |
| 413 | assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 414 | logbuf_assert("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword. Line number 4."); |
| 415 | |
| 416 | str = "path;"; /* missing sep after the keyword */ |
| 417 | assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 418 | logbuf_assert("Invalid character sequence \"path;\", expected a keyword followed by a separator. Line number 4."); |
| 419 | |
| 420 | str = "action "; |
| 421 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 422 | assert_int_equal(YANG_ACTION, kw); |
| 423 | assert_int_equal(6, len); |
| 424 | str = "anydata "; |
| 425 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 426 | assert_int_equal(YANG_ANYDATA, kw); |
| 427 | assert_int_equal(7, len); |
| 428 | str = "anyxml "; |
| 429 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 430 | assert_int_equal(YANG_ANYXML, kw); |
| 431 | assert_int_equal(6, len); |
| 432 | str = "argument "; |
| 433 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 434 | assert_int_equal(YANG_ARGUMENT, kw); |
| 435 | assert_int_equal(8, len); |
| 436 | str = "augment "; |
| 437 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 438 | assert_int_equal(YANG_AUGMENT, kw); |
| 439 | assert_int_equal(7, len); |
| 440 | str = "base "; |
| 441 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 442 | assert_int_equal(YANG_BASE, kw); |
| 443 | assert_int_equal(4, len); |
| 444 | str = "belongs-to "; |
| 445 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 446 | assert_int_equal(YANG_BELONGS_TO, kw); |
| 447 | assert_int_equal(10, len); |
| 448 | str = "bit "; |
| 449 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 450 | assert_int_equal(YANG_BIT, kw); |
| 451 | assert_int_equal(3, len); |
| 452 | str = "case "; |
| 453 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 454 | assert_int_equal(YANG_CASE, kw); |
| 455 | assert_int_equal(4, len); |
| 456 | str = "choice "; |
| 457 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 458 | assert_int_equal(YANG_CHOICE, kw); |
| 459 | assert_int_equal(6, len); |
| 460 | str = "config "; |
| 461 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 462 | assert_int_equal(YANG_CONFIG, kw); |
| 463 | assert_int_equal(6, len); |
| 464 | str = "contact "; |
| 465 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 466 | assert_int_equal(YANG_CONTACT, kw); |
| 467 | assert_int_equal(7, len); |
| 468 | str = "container "; |
| 469 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 470 | assert_int_equal(YANG_CONTAINER, kw); |
| 471 | assert_int_equal(9, len); |
| 472 | str = "default "; |
| 473 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 474 | assert_int_equal(YANG_DEFAULT, kw); |
| 475 | assert_int_equal(7, len); |
| 476 | str = "description "; |
| 477 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 478 | assert_int_equal(YANG_DESCRIPTION, kw); |
| 479 | assert_int_equal(11, len); |
| 480 | str = "deviate "; |
| 481 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 482 | assert_int_equal(YANG_DEVIATE, kw); |
| 483 | assert_int_equal(7, len); |
| 484 | str = "deviation "; |
| 485 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 486 | assert_int_equal(YANG_DEVIATION, kw); |
| 487 | assert_int_equal(9, len); |
| 488 | str = "enum "; |
| 489 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 490 | assert_int_equal(YANG_ENUM, kw); |
| 491 | assert_int_equal(4, len); |
| 492 | str = "error-app-tag "; |
| 493 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 494 | assert_int_equal(YANG_ERROR_APP_TAG, kw); |
| 495 | assert_int_equal(13, len); |
| 496 | str = "error-message "; |
| 497 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 498 | assert_int_equal(YANG_ERROR_MESSAGE, kw); |
| 499 | assert_int_equal(13, len); |
| 500 | str = "extension "; |
| 501 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 502 | assert_int_equal(YANG_EXTENSION, kw); |
| 503 | assert_int_equal(9, len); |
| 504 | str = "feature "; |
| 505 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 506 | assert_int_equal(YANG_FEATURE, kw); |
| 507 | assert_int_equal(7, len); |
| 508 | str = "fraction-digits "; |
| 509 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 510 | assert_int_equal(YANG_FRACTION_DIGITS, kw); |
| 511 | assert_int_equal(15, len); |
| 512 | str = "grouping "; |
| 513 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 514 | assert_int_equal(YANG_GROUPING, kw); |
| 515 | assert_int_equal(8, len); |
| 516 | str = "identity "; |
| 517 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 518 | assert_int_equal(YANG_IDENTITY, kw); |
| 519 | assert_int_equal(8, len); |
| 520 | str = "if-feature "; |
| 521 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 522 | assert_int_equal(YANG_IF_FEATURE, kw); |
| 523 | assert_int_equal(10, len); |
| 524 | str = "import "; |
| 525 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 526 | assert_int_equal(YANG_IMPORT, kw); |
| 527 | assert_int_equal(6, len); |
| 528 | str = "include "; |
| 529 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 530 | assert_int_equal(YANG_INCLUDE, kw); |
| 531 | assert_int_equal(7, len); |
| 532 | str = "input{"; |
| 533 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 534 | assert_int_equal(YANG_INPUT, kw); |
| 535 | assert_int_equal(5, len); |
| 536 | str = "key "; |
| 537 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 538 | assert_int_equal(YANG_KEY, kw); |
| 539 | assert_int_equal(3, len); |
| 540 | str = "leaf "; |
| 541 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 542 | assert_int_equal(YANG_LEAF, kw); |
| 543 | assert_int_equal(4, len); |
| 544 | str = "leaf-list "; |
| 545 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 546 | assert_int_equal(YANG_LEAF_LIST, kw); |
| 547 | assert_int_equal(9, len); |
| 548 | str = "length "; |
| 549 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 550 | assert_int_equal(YANG_LENGTH, kw); |
| 551 | assert_int_equal(6, len); |
| 552 | str = "list "; |
| 553 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 554 | assert_int_equal(YANG_LIST, kw); |
| 555 | assert_int_equal(4, len); |
| 556 | str = "mandatory "; |
| 557 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 558 | assert_int_equal(YANG_MANDATORY, kw); |
| 559 | assert_int_equal(9, len); |
| 560 | str = "max-elements "; |
| 561 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 562 | assert_int_equal(YANG_MAX_ELEMENTS, kw); |
| 563 | assert_int_equal(12, len); |
| 564 | str = "min-elements "; |
| 565 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 566 | assert_int_equal(YANG_MIN_ELEMENTS, kw); |
| 567 | assert_int_equal(12, len); |
| 568 | str = "modifier "; |
| 569 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 570 | assert_int_equal(YANG_MODIFIER, kw); |
| 571 | assert_int_equal(8, len); |
| 572 | str = "module "; |
| 573 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 574 | assert_int_equal(YANG_MODULE, kw); |
| 575 | assert_int_equal(6, len); |
| 576 | str = "must "; |
| 577 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 578 | assert_int_equal(YANG_MUST, kw); |
| 579 | assert_int_equal(4, len); |
| 580 | str = "namespace "; |
| 581 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 582 | assert_int_equal(YANG_NAMESPACE, kw); |
| 583 | assert_int_equal(9, len); |
| 584 | str = "notification "; |
| 585 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 586 | assert_int_equal(YANG_NOTIFICATION, kw); |
| 587 | assert_int_equal(12, len); |
| 588 | str = "ordered-by "; |
| 589 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 590 | assert_int_equal(YANG_ORDERED_BY, kw); |
| 591 | assert_int_equal(10, len); |
| 592 | str = "organization "; |
| 593 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 594 | assert_int_equal(YANG_ORGANIZATION, kw); |
| 595 | assert_int_equal(12, len); |
| 596 | str = "output "; |
| 597 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 598 | assert_int_equal(YANG_OUTPUT, kw); |
| 599 | assert_int_equal(6, len); |
| 600 | str = "path "; |
| 601 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 602 | assert_int_equal(YANG_PATH, kw); |
| 603 | assert_int_equal(4, len); |
| 604 | str = "pattern "; |
| 605 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 606 | assert_int_equal(YANG_PATTERN, kw); |
| 607 | assert_int_equal(7, len); |
| 608 | str = "position "; |
| 609 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 610 | assert_int_equal(YANG_POSITION, kw); |
| 611 | assert_int_equal(8, len); |
| 612 | str = "prefix "; |
| 613 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 614 | assert_int_equal(YANG_PREFIX, kw); |
| 615 | assert_int_equal(6, len); |
| 616 | str = "presence "; |
| 617 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 618 | assert_int_equal(YANG_PRESENCE, kw); |
| 619 | assert_int_equal(8, len); |
| 620 | str = "range "; |
| 621 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 622 | assert_int_equal(YANG_RANGE, kw); |
| 623 | assert_int_equal(5, len); |
| 624 | str = "reference "; |
| 625 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 626 | assert_int_equal(YANG_REFERENCE, kw); |
| 627 | assert_int_equal(9, len); |
| 628 | str = "refine "; |
| 629 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 630 | assert_int_equal(YANG_REFINE, kw); |
| 631 | assert_int_equal(6, len); |
| 632 | str = "require-instance "; |
| 633 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 634 | assert_int_equal(YANG_REQUIRE_INSTANCE, kw); |
| 635 | assert_int_equal(16, len); |
| 636 | str = "revision "; |
| 637 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 638 | assert_int_equal(YANG_REVISION, kw); |
| 639 | assert_int_equal(8, len); |
| 640 | str = "revision-date "; |
| 641 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 642 | assert_int_equal(YANG_REVISION_DATE, kw); |
| 643 | assert_int_equal(13, len); |
| 644 | str = "rpc "; |
| 645 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 646 | assert_int_equal(YANG_RPC, kw); |
| 647 | assert_int_equal(3, len); |
| 648 | str = "status "; |
| 649 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 650 | assert_int_equal(YANG_STATUS, kw); |
| 651 | assert_int_equal(6, len); |
| 652 | str = "submodule "; |
| 653 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 654 | assert_int_equal(YANG_SUBMODULE, kw); |
| 655 | assert_int_equal(9, len); |
| 656 | str = "type "; |
| 657 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 658 | assert_int_equal(YANG_TYPE, kw); |
| 659 | assert_int_equal(4, len); |
| 660 | str = "typedef "; |
| 661 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 662 | assert_int_equal(YANG_TYPEDEF, kw); |
| 663 | assert_int_equal(7, len); |
| 664 | str = "unique "; |
| 665 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 666 | assert_int_equal(YANG_UNIQUE, kw); |
| 667 | assert_int_equal(6, len); |
| 668 | str = "units "; |
| 669 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 670 | assert_int_equal(YANG_UNITS, kw); |
| 671 | assert_int_equal(5, len); |
| 672 | str = "uses "; |
| 673 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 674 | assert_int_equal(YANG_USES, kw); |
| 675 | assert_int_equal(4, len); |
| 676 | str = "value "; |
| 677 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 678 | assert_int_equal(YANG_VALUE, kw); |
| 679 | assert_int_equal(5, len); |
| 680 | str = "when "; |
| 681 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 682 | assert_int_equal(YANG_WHEN, kw); |
| 683 | assert_int_equal(4, len); |
| 684 | str = "yang-version "; |
| 685 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 686 | assert_int_equal(YANG_YANG_VERSION, kw); |
| 687 | assert_int_equal(12, len); |
| 688 | str = "yin-element "; |
| 689 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 690 | assert_int_equal(YANG_YIN_ELEMENT, kw); |
| 691 | assert_int_equal(11, len); |
Radek Krejci | 626df48 | 2018-10-11 15:06:31 +0200 | [diff] [blame] | 692 | str = ";config false;"; |
| 693 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 694 | assert_int_equal(YANG_SEMICOLON, kw); |
| 695 | assert_int_equal(1, len); |
| 696 | assert_string_equal("config false;", str); |
| 697 | str = "{ config false;"; |
| 698 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 699 | assert_int_equal(YANG_LEFT_BRACE, kw); |
| 700 | assert_int_equal(1, len); |
| 701 | assert_string_equal(" config false;", str); |
| 702 | str = "}"; |
| 703 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 704 | assert_int_equal(YANG_RIGHT_BRACE, kw); |
| 705 | assert_int_equal(1, len); |
| 706 | assert_string_equal("", str); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 707 | |
| 708 | /* geenric extension */ |
| 709 | str = p = "nacm:default-deny-write;"; |
| 710 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 711 | assert_int_equal(YANG_CUSTOM, kw); |
| 712 | assert_int_equal(23, len); |
| 713 | assert_ptr_equal(p, word); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 714 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 715 | |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 716 | static void |
| 717 | test_minmax(void **state) |
| 718 | { |
| 719 | *state = test_minmax; |
| 720 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 721 | struct lys_parser_ctx ctx = {0}; |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 722 | uint16_t flags = 0; |
| 723 | uint32_t value = 0; |
| 724 | struct lysp_ext_instance *ext = NULL; |
| 725 | const char *str; |
| 726 | |
| 727 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 728 | assert_non_null(ctx.ctx); |
| 729 | ctx.line = 1; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 730 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 731 | |
Radek Krejci | df6cad1 | 2018-11-28 17:10:55 +0100 | [diff] [blame] | 732 | str = " 1invalid; ..."; |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 733 | assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext)); |
Radek Krejci | df6cad1 | 2018-11-28 17:10:55 +0100 | [diff] [blame] | 734 | logbuf_assert("Invalid value \"1invalid\" of \"min-elements\". Line number 1."); |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 735 | |
| 736 | flags = value = 0; |
| 737 | str = " -1; ..."; |
| 738 | assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext)); |
| 739 | logbuf_assert("Invalid value \"-1\" of \"min-elements\". Line number 1."); |
| 740 | |
Radek Krejci | df6cad1 | 2018-11-28 17:10:55 +0100 | [diff] [blame] | 741 | /* implementation limit */ |
| 742 | flags = value = 0; |
| 743 | str = " 4294967296; ..."; |
| 744 | assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext)); |
| 745 | logbuf_assert("Value \"4294967296\" is out of \"min-elements\" bounds. Line number 1."); |
| 746 | |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 747 | flags = value = 0; |
| 748 | str = " 1; ..."; |
| 749 | assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext)); |
| 750 | assert_int_equal(LYS_SET_MIN, flags); |
| 751 | assert_int_equal(1, value); |
| 752 | |
| 753 | flags = value = 0; |
| 754 | str = " 1 {m:ext;} ..."; |
| 755 | assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext)); |
| 756 | assert_int_equal(LYS_SET_MIN, flags); |
| 757 | assert_int_equal(1, value); |
| 758 | assert_non_null(ext); |
| 759 | FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free); |
| 760 | ext = NULL; |
| 761 | |
| 762 | flags = value = 0; |
| 763 | str = " 1 {config true;} ..."; |
| 764 | assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext)); |
| 765 | logbuf_assert("Invalid keyword \"config\" as a child of \"min-elements\". Line number 1."); |
| 766 | |
Radek Krejci | df6cad1 | 2018-11-28 17:10:55 +0100 | [diff] [blame] | 767 | str = " 1invalid; ..."; |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 768 | assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext)); |
Radek Krejci | df6cad1 | 2018-11-28 17:10:55 +0100 | [diff] [blame] | 769 | logbuf_assert("Invalid value \"1invalid\" of \"max-elements\". Line number 1."); |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 770 | |
| 771 | flags = value = 0; |
| 772 | str = " -1; ..."; |
| 773 | assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext)); |
| 774 | logbuf_assert("Invalid value \"-1\" of \"max-elements\". Line number 1."); |
| 775 | |
Radek Krejci | df6cad1 | 2018-11-28 17:10:55 +0100 | [diff] [blame] | 776 | /* implementation limit */ |
| 777 | flags = value = 0; |
| 778 | str = " 4294967296; ..."; |
| 779 | assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext)); |
| 780 | logbuf_assert("Value \"4294967296\" is out of \"max-elements\" bounds. Line number 1."); |
| 781 | |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 782 | flags = value = 0; |
| 783 | str = " 1; ..."; |
| 784 | assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext)); |
| 785 | assert_int_equal(LYS_SET_MAX, flags); |
| 786 | assert_int_equal(1, value); |
| 787 | |
| 788 | flags = value = 0; |
| 789 | str = " unbounded; ..."; |
| 790 | assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext)); |
| 791 | assert_int_equal(LYS_SET_MAX, flags); |
| 792 | assert_int_equal(0, value); |
| 793 | |
| 794 | flags = value = 0; |
| 795 | str = " 1 {m:ext;} ..."; |
| 796 | assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext)); |
| 797 | assert_int_equal(LYS_SET_MAX, flags); |
| 798 | assert_int_equal(1, value); |
| 799 | assert_non_null(ext); |
| 800 | FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free); |
| 801 | ext = NULL; |
| 802 | |
| 803 | flags = value = 0; |
| 804 | str = " 1 {config true;} ..."; |
| 805 | assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext)); |
| 806 | logbuf_assert("Invalid keyword \"config\" as a child of \"max-elements\". Line number 1."); |
| 807 | |
| 808 | *state = NULL; |
| 809 | ly_ctx_destroy(ctx.ctx, NULL); |
| 810 | } |
| 811 | |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 812 | static struct lysp_module * |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 813 | mod_renew(struct lys_parser_ctx *ctx) |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 814 | { |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 815 | struct lysp_module *mod_p; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 816 | static struct lys_module mod = {0}; |
| 817 | |
| 818 | lysc_module_free(mod.compiled, NULL); |
| 819 | lysp_module_free(mod.parsed); |
| 820 | FREE_STRING(mod.ctx, mod.name); |
| 821 | FREE_STRING(mod.ctx, mod.ns); |
| 822 | FREE_STRING(mod.ctx, mod.prefix); |
| 823 | FREE_STRING(mod.ctx, mod.filepath); |
| 824 | FREE_STRING(mod.ctx, mod.org); |
| 825 | FREE_STRING(mod.ctx, mod.contact); |
| 826 | FREE_STRING(mod.ctx, mod.dsc); |
| 827 | FREE_STRING(mod.ctx, mod.ref); |
| 828 | memset(&mod, 0, sizeof mod); |
| 829 | mod.ctx = ctx->ctx; |
| 830 | |
| 831 | mod_p = calloc(1, sizeof *mod_p); |
| 832 | mod.parsed = mod_p; |
| 833 | mod_p->mod = &mod; |
| 834 | assert_non_null(mod_p); |
| 835 | return mod_p; |
| 836 | } |
| 837 | |
| 838 | static struct lysp_submodule * |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 839 | submod_renew(struct lys_parser_ctx *ctx, struct lysp_submodule *submod) |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 840 | { |
| 841 | lysp_submodule_free(ctx->ctx, submod); |
| 842 | submod = calloc(1, sizeof *submod); |
| 843 | assert_non_null(submod); |
| 844 | return submod; |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 845 | } |
| 846 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 847 | static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name), |
| 848 | const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format, |
| 849 | const char **module_data, void (**free_module_data)(void *model_data, void *user_data)) |
| 850 | { |
| 851 | *module_data = user_data; |
| 852 | *format = LYS_IN_YANG; |
| 853 | *free_module_data = NULL; |
| 854 | return LY_SUCCESS; |
| 855 | } |
| 856 | |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 857 | static void |
| 858 | test_module(void **state) |
| 859 | { |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 860 | *state = test_module; |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 861 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 862 | struct lys_parser_ctx ctx; |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 863 | struct lysp_module *mod = NULL; |
| 864 | struct lysp_submodule *submod = NULL; |
| 865 | struct lys_module *m; |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 866 | const char *str; |
| 867 | |
| 868 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 869 | assert_non_null(ctx.ctx); |
| 870 | ctx.line = 1; |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 871 | ctx.indent = 0; |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 872 | |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 873 | mod = mod_renew(&ctx); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 874 | |
| 875 | /* missing mandatory substatements */ |
| 876 | str = " name {}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 877 | assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod)); |
| 878 | assert_string_equal("name", mod->mod->name); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 879 | logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1."); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 880 | mod = mod_renew(&ctx); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 881 | |
| 882 | str = " name {namespace urn:x;}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 883 | assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod)); |
| 884 | assert_string_equal("urn:x", mod->mod->ns); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 885 | logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1."); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 886 | mod = mod_renew(&ctx); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 887 | |
| 888 | str = " name {namespace urn:x;prefix \"x\";}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 889 | assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); |
| 890 | assert_string_equal("x", mod->mod->prefix); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 891 | mod = mod_renew(&ctx); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 892 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 893 | #define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";" |
| 894 | #define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";" |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 895 | #define TEST_NODE(NODETYPE, INPUT, NAME) \ |
| 896 | str = SCHEMA_BEGINNING INPUT; \ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 897 | assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \ |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 898 | assert_non_null(mod->data); \ |
| 899 | assert_int_equal(NODETYPE, mod->data->nodetype); \ |
| 900 | assert_string_equal(NAME, mod->data->name); \ |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 901 | mod = mod_renew(&ctx); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 902 | #define TEST_GENERIC(INPUT, TARGET, TEST) \ |
| 903 | str = SCHEMA_BEGINNING INPUT; \ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 904 | assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \ |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 905 | assert_non_null(TARGET); \ |
| 906 | TEST; \ |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 907 | mod = mod_renew(&ctx); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 908 | #define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 909 | TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \ |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 910 | parse_module, mod, LINE, mod = mod_renew(&ctx)) |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 911 | |
| 912 | /* duplicated namespace, prefix */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 913 | TEST_DUP("namespace", "y", "z", "1"); |
| 914 | TEST_DUP("prefix", "y", "z", "1"); |
| 915 | TEST_DUP("contact", "a", "b", "1"); |
| 916 | TEST_DUP("description", "a", "b", "1"); |
| 917 | TEST_DUP("organization", "a", "b", "1"); |
| 918 | TEST_DUP("reference", "a", "b", "1"); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 919 | |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 920 | /* not allowed in module (submodule-specific) */ |
| 921 | str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 922 | assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod)); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 923 | logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1."); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 924 | mod = mod_renew(&ctx); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 925 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 926 | /* anydata */ |
| 927 | TEST_NODE(LYS_ANYDATA, "anydata test;}", "test"); |
| 928 | /* anyxml */ |
| 929 | TEST_NODE(LYS_ANYXML, "anyxml test;}", "test"); |
| 930 | /* augment */ |
| 931 | TEST_GENERIC("augment /somepath;}", mod->augments, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 932 | assert_string_equal("/somepath", mod->augments[0].nodeid)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 933 | /* choice */ |
| 934 | TEST_NODE(LYS_CHOICE, "choice test;}", "test"); |
| 935 | /* contact 0..1 */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 936 | TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact, |
| 937 | assert_string_equal("firstname surname", mod->mod->contact)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 938 | /* container */ |
| 939 | TEST_NODE(LYS_CONTAINER, "container test;}", "test"); |
| 940 | /* description 0..1 */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 941 | TEST_GENERIC("description \'some description\';}", mod->mod->dsc, |
| 942 | assert_string_equal("some description", mod->mod->dsc)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 943 | /* deviation */ |
| 944 | TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 945 | assert_string_equal("/somepath", mod->deviations[0].nodeid)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 946 | /* extension */ |
| 947 | TEST_GENERIC("extension test;}", mod->extensions, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 948 | assert_string_equal("test", mod->extensions[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 949 | /* feature */ |
| 950 | TEST_GENERIC("feature test;}", mod->features, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 951 | assert_string_equal("test", mod->features[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 952 | /* grouping */ |
| 953 | TEST_GENERIC("grouping grp;}", mod->groupings, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 954 | assert_string_equal("grp", mod->groupings[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 955 | /* identity */ |
| 956 | TEST_GENERIC("identity test;}", mod->identities, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 957 | assert_string_equal("test", mod->identities[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 958 | /* import */ |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 959 | ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}"); |
| 960 | TEST_GENERIC("import zzz {prefix z;}}", mod->imports, |
| 961 | assert_string_equal("zzz", mod->imports[0].name)); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 962 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 963 | /* import - prefix collision */ |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 964 | str = SCHEMA_BEGINNING "import zzz {prefix x;}}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 965 | assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod)); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 966 | logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2."); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 967 | mod = mod_renew(&ctx); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 968 | str = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 969 | assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod)); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 970 | logbuf_assert("Prefix \"y\" already used to import \"zzz\" module. Line number 2."); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 971 | mod = mod_renew(&ctx); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 972 | str = "module" SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix z;}}"; |
| 973 | assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG)); |
| 974 | assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx)); |
| 975 | logbuf_assert("Single revision of the module \"zzz\" referred twice."); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 976 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 977 | /* include */ |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 978 | store = 1; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 979 | ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module xxx { namespace urn:xxx; prefix x;}"); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 980 | str = "module" SCHEMA_BEGINNING "include xxx;}"; |
| 981 | assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG)); |
| 982 | assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 983 | logbuf_assert("Input data contains module in situation when a submodule is expected."); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 984 | store = -1; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 985 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 986 | store = 1; |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 987 | ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "submodule xxx {belongs-to wrong-name {prefix w;}}"); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 988 | str = "module" SCHEMA_BEGINNING "include xxx;}"; |
| 989 | assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG)); |
| 990 | assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx)); |
| 991 | logbuf_assert("Included \"xxx\" submodule from \"name\" belongs-to a different module \"wrong-name\"."); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 992 | store = -1; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 993 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 994 | ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "submodule xxx {belongs-to name {prefix x;}}"); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 995 | TEST_GENERIC("include xxx;}", mod->includes, |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 996 | assert_string_equal("xxx", mod->includes[0].name)); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 997 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 998 | /* leaf */ |
| 999 | TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test"); |
| 1000 | /* leaf-list */ |
| 1001 | TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test"); |
| 1002 | /* list */ |
| 1003 | TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test"); |
| 1004 | /* notification */ |
| 1005 | TEST_GENERIC("notification test;}", mod->notifs, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1006 | assert_string_equal("test", mod->notifs[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 1007 | /* organization 0..1 */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1008 | TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org, |
| 1009 | assert_string_equal("CESNET a.l.e.", mod->mod->org)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 1010 | /* reference 0..1 */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1011 | TEST_GENERIC("reference RFC7950;}", mod->mod->ref, |
| 1012 | assert_string_equal("RFC7950", mod->mod->ref)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 1013 | /* revision */ |
| 1014 | TEST_GENERIC("revision 2018-10-12;}", mod->revs, |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 1015 | assert_string_equal("2018-10-12", mod->revs[0].date)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 1016 | /* rpc */ |
| 1017 | TEST_GENERIC("rpc test;}", mod->rpcs, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1018 | assert_string_equal("test", mod->rpcs[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 1019 | /* typedef */ |
| 1020 | TEST_GENERIC("typedef test{type string;}}", mod->typedefs, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1021 | assert_string_equal("test", mod->typedefs[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 1022 | /* uses */ |
| 1023 | TEST_NODE(LYS_USES, "uses test;}", "test"); |
| 1024 | /* yang-version */ |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1025 | str = SCHEMA_BEGINNING2 "\n\tyang-version 10;}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1026 | assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 1027 | logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3."); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 1028 | mod = mod_renew(&ctx); |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1029 | str = SCHEMA_BEGINNING2 "yang-version 1.0;yang-version 1.1;}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1030 | assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 1031 | logbuf_assert("Duplicate keyword \"yang-version\". Line number 3."); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 1032 | mod = mod_renew(&ctx); |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1033 | str = SCHEMA_BEGINNING2 "yang-version 1.0;}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1034 | assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); |
| 1035 | assert_int_equal(1, mod->mod->version); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 1036 | mod = mod_renew(&ctx); |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1037 | str = SCHEMA_BEGINNING2 "yang-version \"1.1\";}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1038 | assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); |
| 1039 | assert_int_equal(2, mod->mod->version); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 1040 | mod = mod_renew(&ctx); |
| 1041 | |
| 1042 | str = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}"; |
| 1043 | m = mod->mod; |
| 1044 | free(mod); |
| 1045 | m->parsed = NULL; |
| 1046 | assert_int_equal(LY_EVALID, yang_parse_module(&ctx, str, m)); |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 1047 | logbuf_assert("Trailing garbage \"module q {names...\" after module, expected end-of-input. Line number 3."); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 1048 | mod = mod_renew(&ctx); |
| 1049 | |
| 1050 | str = "prefix " SCHEMA_BEGINNING "}"; |
| 1051 | m = mod->mod; |
| 1052 | free(mod); |
| 1053 | m->parsed = NULL; |
| 1054 | assert_int_equal(LY_EVALID, yang_parse_module(&ctx, str, m)); |
| 1055 | logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 3."); |
| 1056 | mod = mod_renew(&ctx); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 1057 | |
Radek Krejci | 156ccaf | 2018-10-15 15:49:17 +0200 | [diff] [blame] | 1058 | /* extensions */ |
| 1059 | TEST_GENERIC("prefix:test;}", mod->exts, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1060 | assert_string_equal("prefix:test", mod->exts[0].name); |
| 1061 | assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt)); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 1062 | mod = mod_renew(&ctx); |
Radek Krejci | 156ccaf | 2018-10-15 15:49:17 +0200 | [diff] [blame] | 1063 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1064 | /* invalid substatement */ |
| 1065 | str = SCHEMA_BEGINNING "must false;}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1066 | assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod)); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1067 | logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3."); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 1068 | mod = mod_renew(&ctx); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1069 | |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 1070 | /* submodule */ |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 1071 | submod = submod_renew(&ctx, submod); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 1072 | |
| 1073 | /* missing mandatory substatements */ |
| 1074 | str = " subname {}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1075 | lydict_remove(ctx.ctx, submod->name); |
| 1076 | assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod)); |
| 1077 | assert_string_equal("subname", submod->name); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 1078 | logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3."); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1079 | submod = submod_renew(&ctx, submod); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 1080 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 1081 | str = " subname {belongs-to name {prefix x;}}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1082 | lydict_remove(ctx.ctx, submod->name); |
| 1083 | assert_int_equal(LY_SUCCESS, parse_submodule(&ctx, &str, submod)); |
| 1084 | assert_string_equal("name", submod->belongsto); |
| 1085 | submod = submod_renew(&ctx, submod); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 1086 | |
| 1087 | #undef SCHEMA_BEGINNING |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 1088 | #define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}" |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 1089 | |
| 1090 | /* duplicated namespace, prefix */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1091 | str = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ..."; |
| 1092 | assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod)); \ |
| 1093 | logbuf_assert("Duplicate keyword \"belongs-to\". Line number 3."); \ |
| 1094 | submod = submod_renew(&ctx, submod); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 1095 | |
| 1096 | /* not allowed in submodule (module-specific) */ |
| 1097 | str = SCHEMA_BEGINNING "namespace \"urn:z\";}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1098 | assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod)); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 1099 | logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3."); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1100 | submod = submod_renew(&ctx, submod); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 1101 | str = SCHEMA_BEGINNING "prefix m;}}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1102 | assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod)); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 1103 | logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3."); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1104 | submod = submod_renew(&ctx, submod); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 1105 | |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 1106 | str = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}"; |
| 1107 | lysp_submodule_free(ctx.ctx, submod); |
| 1108 | submod = NULL; |
| 1109 | assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx, str, &submod)); |
Radek Krejci | 0a1d0d4 | 2019-05-16 15:14:51 +0200 | [diff] [blame] | 1110 | logbuf_assert("Trailing garbage \"module q {names...\" after submodule, expected end-of-input. Line number 3."); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 1111 | |
| 1112 | str = "prefix " SCHEMA_BEGINNING "}"; |
| 1113 | assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx, str, &submod)); |
| 1114 | logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 3."); |
| 1115 | submod = submod_renew(&ctx, submod); |
| 1116 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 1117 | #undef TEST_GENERIC |
| 1118 | #undef TEST_NODE |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1119 | #undef TEST_DUP |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 1120 | #undef SCHEMA_BEGINNING |
| 1121 | |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 1122 | lysp_module_free(mod); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1123 | lysp_submodule_free(ctx.ctx, submod); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 1124 | ly_ctx_destroy(ctx.ctx, NULL); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 1125 | |
| 1126 | *state = NULL; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1127 | } |
| 1128 | |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 1129 | static void |
| 1130 | test_identity(void **state) |
| 1131 | { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1132 | *state = test_identity; |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 1133 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1134 | struct lys_parser_ctx ctx; |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 1135 | struct lysp_ident *ident = NULL; |
| 1136 | const char *str; |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 1137 | |
| 1138 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1139 | assert_non_null(ctx.ctx); |
| 1140 | ctx.line = 1; |
| 1141 | ctx.indent = 0; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1142 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 1143 | |
| 1144 | /* invalid cardinality */ |
| 1145 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1146 | TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \ |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1147 | &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL) |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 1148 | |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1149 | TEST_DUP("description", "a", "b"); |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 1150 | TEST_DUP("reference", "a", "b"); |
| 1151 | TEST_DUP("status", "current", "obsolete"); |
| 1152 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1153 | /* full content */ |
| 1154 | str = " test {base \"a\";base b; description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ..."; |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 1155 | assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident)); |
| 1156 | assert_non_null(ident); |
| 1157 | assert_string_equal(" ...", str); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1158 | FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1159 | ident = NULL; |
| 1160 | |
| 1161 | /* invalid substatement */ |
| 1162 | str = " test {organization XXX;}"; |
| 1163 | assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident)); |
| 1164 | logbuf_assert("Invalid keyword \"organization\" as a child of \"identity\". Line number 1."); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1165 | FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1166 | ident = NULL; |
| 1167 | |
| 1168 | #undef TEST_DUP |
| 1169 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1170 | *state = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1171 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1172 | } |
| 1173 | |
| 1174 | static void |
| 1175 | test_feature(void **state) |
| 1176 | { |
| 1177 | (void) state; /* unused */ |
| 1178 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1179 | struct lys_parser_ctx ctx; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1180 | struct lysp_feature *features = NULL; |
| 1181 | const char *str; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1182 | |
| 1183 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1184 | assert_non_null(ctx.ctx); |
| 1185 | ctx.line = 1; |
| 1186 | ctx.indent = 0; |
| 1187 | |
| 1188 | /* invalid cardinality */ |
| 1189 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1190 | TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \ |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1191 | &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL) |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1192 | |
| 1193 | TEST_DUP("description", "a", "b"); |
| 1194 | TEST_DUP("reference", "a", "b"); |
| 1195 | TEST_DUP("status", "current", "obsolete"); |
| 1196 | |
| 1197 | /* full content */ |
| 1198 | str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ..."; |
| 1199 | assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features)); |
| 1200 | assert_non_null(features); |
| 1201 | assert_string_equal(" ...", str); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1202 | FREE_ARRAY(ctx.ctx, features, lysp_feature_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1203 | features = NULL; |
| 1204 | |
| 1205 | /* invalid substatement */ |
| 1206 | str = " test {organization XXX;}"; |
| 1207 | assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features)); |
| 1208 | logbuf_assert("Invalid keyword \"organization\" as a child of \"feature\". Line number 1."); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1209 | FREE_ARRAY(ctx.ctx, features, lysp_feature_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1210 | features = NULL; |
| 1211 | |
| 1212 | #undef TEST_DUP |
| 1213 | |
| 1214 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1215 | } |
| 1216 | |
| 1217 | static void |
| 1218 | test_deviation(void **state) |
| 1219 | { |
| 1220 | (void) state; /* unused */ |
| 1221 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1222 | struct lys_parser_ctx ctx; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1223 | struct lysp_deviation *d = NULL; |
| 1224 | const char *str; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1225 | |
| 1226 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1227 | assert_non_null(ctx.ctx); |
| 1228 | ctx.line = 1; |
| 1229 | ctx.indent = 0; |
| 1230 | |
| 1231 | /* invalid cardinality */ |
| 1232 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1233 | TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \ |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1234 | &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL) |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1235 | |
| 1236 | TEST_DUP("description", "a", "b"); |
| 1237 | TEST_DUP("reference", "a", "b"); |
| 1238 | |
| 1239 | /* full content */ |
| 1240 | str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ..."; |
| 1241 | assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d)); |
| 1242 | assert_non_null(d); |
| 1243 | assert_string_equal(" ...", str); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1244 | FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1245 | d = NULL; |
| 1246 | |
| 1247 | /* missing mandatory substatement */ |
| 1248 | str = " test {description text;}"; |
| 1249 | assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d)); |
| 1250 | logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1."); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1251 | FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1252 | d = NULL; |
| 1253 | |
| 1254 | /* invalid substatement */ |
| 1255 | str = " test {deviate not-supported; status obsolete;}"; |
| 1256 | assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d)); |
| 1257 | logbuf_assert("Invalid keyword \"status\" as a child of \"deviation\". Line number 1."); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1258 | FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1259 | d = NULL; |
| 1260 | |
| 1261 | #undef TEST_DUP |
| 1262 | |
| 1263 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1264 | } |
| 1265 | |
| 1266 | static void |
| 1267 | test_deviate(void **state) |
| 1268 | { |
| 1269 | (void) state; /* unused */ |
| 1270 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1271 | struct lys_parser_ctx ctx; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1272 | struct lysp_deviate *d = NULL; |
| 1273 | const char *str; |
| 1274 | |
| 1275 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1276 | assert_non_null(ctx.ctx); |
| 1277 | ctx.line = 1; |
| 1278 | ctx.indent = 0; |
| 1279 | |
| 1280 | /* invalid cardinality */ |
| 1281 | #define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \ |
| 1282 | TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \ |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1283 | &d, "1", lysp_deviate_free(ctx.ctx, d); free(d); d = NULL) |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1284 | |
| 1285 | TEST_DUP("add", "config", "true", "false"); |
| 1286 | TEST_DUP("replace", "default", "int8", "uint8"); |
| 1287 | TEST_DUP("add", "mandatory", "true", "false"); |
| 1288 | TEST_DUP("add", "max-elements", "1", "2"); |
| 1289 | TEST_DUP("add", "min-elements", "1", "2"); |
| 1290 | TEST_DUP("replace", "type", "int8", "uint8"); |
| 1291 | TEST_DUP("add", "units", "kilometers", "miles"); |
| 1292 | |
| 1293 | /* full contents */ |
| 1294 | str = " not-supported {prefix:ext;} ..."; |
| 1295 | assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d)); |
| 1296 | assert_non_null(d); |
| 1297 | assert_string_equal(" ...", str); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1298 | lysp_deviate_free(ctx.ctx, d); free(d); d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1299 | str = " add {units meters; must 1; must 2; unique x; unique y; default a; default b; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ..."; |
| 1300 | assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d)); |
| 1301 | assert_non_null(d); |
| 1302 | assert_string_equal(" ...", str); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1303 | lysp_deviate_free(ctx.ctx, d); free(d); d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1304 | str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ..."; |
| 1305 | assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d)); |
| 1306 | assert_non_null(d); |
| 1307 | assert_string_equal(" ...", str); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1308 | lysp_deviate_free(ctx.ctx, d); free(d); d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1309 | str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ..."; |
| 1310 | assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d)); |
| 1311 | assert_non_null(d); |
| 1312 | assert_string_equal(" ...", str); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1313 | lysp_deviate_free(ctx.ctx, d); free(d); d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1314 | |
| 1315 | /* invalid substatements */ |
| 1316 | #define TEST_NOT_SUP(DEV, STMT, VALUE) \ |
| 1317 | str = " "DEV" {"STMT" "VALUE";}..."; \ |
| 1318 | assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \ |
| 1319 | logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \ |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1320 | lysp_deviate_free(ctx.ctx, d); free(d); d = NULL |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1321 | |
| 1322 | TEST_NOT_SUP("not-supported", "units", "meters"); |
| 1323 | TEST_NOT_SUP("not-supported", "must", "1"); |
| 1324 | TEST_NOT_SUP("not-supported", "unique", "x"); |
| 1325 | TEST_NOT_SUP("not-supported", "default", "a"); |
| 1326 | TEST_NOT_SUP("not-supported", "config", "true"); |
| 1327 | TEST_NOT_SUP("not-supported", "mandatory", "true"); |
| 1328 | TEST_NOT_SUP("not-supported", "min-elements", "1"); |
| 1329 | TEST_NOT_SUP("not-supported", "max-elements", "2"); |
| 1330 | TEST_NOT_SUP("not-supported", "type", "string"); |
| 1331 | TEST_NOT_SUP("add", "type", "string"); |
| 1332 | TEST_NOT_SUP("delete", "config", "true"); |
| 1333 | TEST_NOT_SUP("delete", "mandatory", "true"); |
| 1334 | TEST_NOT_SUP("delete", "min-elements", "1"); |
| 1335 | TEST_NOT_SUP("delete", "max-elements", "2"); |
| 1336 | TEST_NOT_SUP("delete", "type", "string"); |
| 1337 | TEST_NOT_SUP("replace", "must", "1"); |
| 1338 | TEST_NOT_SUP("replace", "unique", "a"); |
| 1339 | |
| 1340 | str = " nonsence; ..."; |
| 1341 | assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); |
| 1342 | logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1."); |
| 1343 | assert_null(d); |
| 1344 | |
| 1345 | #undef TEST_NOT_SUP |
| 1346 | #undef TEST_DUP |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 1347 | |
| 1348 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1349 | } |
| 1350 | |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1351 | static void |
| 1352 | test_container(void **state) |
| 1353 | { |
| 1354 | (void) state; /* unused */ |
| 1355 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1356 | struct lys_parser_ctx ctx = {0}; |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1357 | struct lysp_node_container *c = NULL; |
| 1358 | const char *str; |
| 1359 | |
| 1360 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1361 | assert_non_null(ctx.ctx); |
| 1362 | ctx.line = 1; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1363 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1364 | |
| 1365 | /* invalid cardinality */ |
| 1366 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1367 | str = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1368 | assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); \ |
| 1369 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1370 | lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL; |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1371 | |
| 1372 | TEST_DUP("config", "true", "false"); |
| 1373 | TEST_DUP("description", "text1", "text2"); |
| 1374 | TEST_DUP("presence", "true", "false"); |
| 1375 | TEST_DUP("reference", "1", "2"); |
| 1376 | TEST_DUP("status", "current", "obsolete"); |
| 1377 | TEST_DUP("when", "true", "false"); |
| 1378 | #undef TEST_DUP |
| 1379 | |
| 1380 | /* full content */ |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 1381 | str = "cont {action x;anydata any;anyxml anyxml; choice ch;config false;container c;description test;grouping g;if-feature f; leaf l {type string;}" |
| 1382 | "leaf-list ll {type string;} list li;must 'expr';notification not; presence true; reference test;status current;typedef t {type int8;}uses g;when true;m:ext;} ..."; |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1383 | assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); |
| 1384 | assert_non_null(c); |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1385 | assert_int_equal(LYS_CONTAINER, c->nodetype); |
| 1386 | assert_string_equal("cont", c->name); |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1387 | assert_non_null(c->actions); |
| 1388 | assert_non_null(c->child); |
| 1389 | assert_string_equal("test", c->dsc); |
| 1390 | assert_non_null(c->exts); |
| 1391 | assert_non_null(c->groupings); |
| 1392 | assert_non_null(c->iffeatures); |
| 1393 | assert_non_null(c->musts); |
| 1394 | assert_non_null(c->notifs); |
| 1395 | assert_string_equal("true", c->presence); |
| 1396 | assert_string_equal("test", c->ref); |
| 1397 | assert_non_null(c->typedefs); |
| 1398 | assert_non_null(c->when); |
| 1399 | assert_null(c->parent); |
| 1400 | assert_null(c->next); |
| 1401 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags); |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 1402 | ly_set_erase(&ctx.tpdfs_nodes, NULL); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1403 | lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL; |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1404 | |
| 1405 | /* invalid */ |
| 1406 | str = " cont {augment /root;} ..."; |
| 1407 | assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); |
| 1408 | logbuf_assert("Invalid keyword \"augment\" as a child of \"container\". Line number 1."); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1409 | lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL; |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1410 | str = " cont {nonsence true;} ..."; |
| 1411 | assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); |
| 1412 | logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1."); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1413 | lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL; |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1414 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1415 | ctx.mod_version = 1; /* simulate YANG 1.0 */ |
| 1416 | str = " cont {action x;} ..."; |
| 1417 | assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); |
| 1418 | logbuf_assert("Invalid keyword \"action\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1."); |
| 1419 | lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL; |
| 1420 | |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1421 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1422 | } |
| 1423 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1424 | static void |
| 1425 | test_leaf(void **state) |
| 1426 | { |
| 1427 | *state = test_leaf; |
| 1428 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1429 | struct lys_parser_ctx ctx = {0}; |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1430 | struct lysp_node_leaf *l = NULL; |
| 1431 | const char *str; |
| 1432 | |
| 1433 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1434 | assert_non_null(ctx.ctx); |
| 1435 | ctx.line = 1; |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1436 | //ctx.mod->version = 2; /* simulate YANG 1.1 */ |
| 1437 | |
| 1438 | /* invalid cardinality */ |
| 1439 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1440 | str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1441 | assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); \ |
| 1442 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1443 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1444 | |
| 1445 | TEST_DUP("config", "true", "false"); |
| 1446 | TEST_DUP("default", "x", "y"); |
| 1447 | TEST_DUP("description", "text1", "text2"); |
| 1448 | TEST_DUP("mandatory", "true", "false"); |
| 1449 | TEST_DUP("reference", "1", "2"); |
| 1450 | TEST_DUP("status", "current", "obsolete"); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1451 | TEST_DUP("type", "int8", "uint8"); |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1452 | TEST_DUP("units", "text1", "text2"); |
| 1453 | TEST_DUP("when", "true", "false"); |
| 1454 | #undef TEST_DUP |
| 1455 | |
| 1456 | /* full content - without mandatory which is mutual exclusive with default */ |
| 1457 | str = "l {config false;default \"xxx\";description test;if-feature f;" |
| 1458 | "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ..."; |
| 1459 | assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); |
| 1460 | assert_non_null(l); |
| 1461 | assert_int_equal(LYS_LEAF, l->nodetype); |
| 1462 | assert_string_equal("l", l->name); |
| 1463 | assert_string_equal("test", l->dsc); |
| 1464 | assert_string_equal("xxx", l->dflt); |
| 1465 | assert_string_equal("yyy", l->units); |
| 1466 | assert_string_equal("string", l->type.name); |
| 1467 | assert_non_null(l->exts); |
| 1468 | assert_non_null(l->iffeatures); |
| 1469 | assert_non_null(l->musts); |
| 1470 | assert_string_equal("test", l->ref); |
| 1471 | assert_non_null(l->when); |
| 1472 | assert_null(l->parent); |
| 1473 | assert_null(l->next); |
| 1474 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, l->flags); |
| 1475 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1476 | |
| 1477 | /* full content - now with mandatory */ |
| 1478 | str = "l {mandatory true; type string;} ..."; |
| 1479 | assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); |
| 1480 | assert_non_null(l); |
| 1481 | assert_int_equal(LYS_LEAF, l->nodetype); |
| 1482 | assert_string_equal("l", l->name); |
| 1483 | assert_string_equal("string", l->type.name); |
| 1484 | assert_int_equal(LYS_MAND_TRUE, l->flags); |
| 1485 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1486 | |
| 1487 | /* invalid */ |
| 1488 | str = " l {mandatory true; default xx; type string;} ..."; |
| 1489 | assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 1490 | logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"leaf\". Line number 1."); |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1491 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1492 | |
| 1493 | str = " l {description \"missing type\";} ..."; |
| 1494 | assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); |
| 1495 | logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf\". Line number 1."); |
| 1496 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1497 | |
| 1498 | *state = NULL; |
| 1499 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1500 | } |
| 1501 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1502 | static void |
| 1503 | test_leaflist(void **state) |
| 1504 | { |
| 1505 | *state = test_leaf; |
| 1506 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1507 | struct lys_parser_ctx ctx = {0}; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1508 | struct lysp_node_leaflist *ll = NULL; |
| 1509 | const char *str; |
| 1510 | |
| 1511 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1512 | assert_non_null(ctx.ctx); |
| 1513 | ctx.line = 1; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1514 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1515 | |
| 1516 | /* invalid cardinality */ |
| 1517 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1518 | str = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1519 | assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); \ |
| 1520 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1521 | lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL; |
| 1522 | |
| 1523 | TEST_DUP("config", "true", "false"); |
| 1524 | TEST_DUP("description", "text1", "text2"); |
| 1525 | TEST_DUP("max-elements", "10", "20"); |
| 1526 | TEST_DUP("min-elements", "10", "20"); |
| 1527 | TEST_DUP("ordered-by", "user", "system"); |
| 1528 | TEST_DUP("reference", "1", "2"); |
| 1529 | TEST_DUP("status", "current", "obsolete"); |
| 1530 | TEST_DUP("type", "int8", "uint8"); |
| 1531 | TEST_DUP("units", "text1", "text2"); |
| 1532 | TEST_DUP("when", "true", "false"); |
| 1533 | #undef TEST_DUP |
| 1534 | |
| 1535 | /* full content - without min-elements which is mutual exclusive with default */ |
| 1536 | str = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;" |
| 1537 | "max-elements 10;must 'expr';ordered-by user;reference test;" |
| 1538 | "status current;type string; units zzz;when true;m:ext;} ..."; |
| 1539 | assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); |
| 1540 | assert_non_null(ll); |
| 1541 | assert_int_equal(LYS_LEAFLIST, ll->nodetype); |
| 1542 | assert_string_equal("ll", ll->name); |
| 1543 | assert_string_equal("test", ll->dsc); |
| 1544 | assert_non_null(ll->dflts); |
| 1545 | assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts)); |
| 1546 | assert_string_equal("xxx", ll->dflts[0]); |
| 1547 | assert_string_equal("yyy", ll->dflts[1]); |
| 1548 | assert_string_equal("zzz", ll->units); |
| 1549 | assert_int_equal(10, ll->max); |
| 1550 | assert_int_equal(0, ll->min); |
| 1551 | assert_string_equal("string", ll->type.name); |
| 1552 | assert_non_null(ll->exts); |
| 1553 | assert_non_null(ll->iffeatures); |
| 1554 | assert_non_null(ll->musts); |
| 1555 | assert_string_equal("test", ll->ref); |
| 1556 | assert_non_null(ll->when); |
| 1557 | assert_null(ll->parent); |
| 1558 | assert_null(ll->next); |
| 1559 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags); |
| 1560 | lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL; |
| 1561 | |
| 1562 | /* full content - now with min-elements */ |
| 1563 | str = "ll {min-elements 10; type string;} ..."; |
| 1564 | assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); |
| 1565 | assert_non_null(ll); |
| 1566 | assert_int_equal(LYS_LEAFLIST, ll->nodetype); |
| 1567 | assert_string_equal("ll", ll->name); |
| 1568 | assert_string_equal("string", ll->type.name); |
| 1569 | assert_int_equal(0, ll->max); |
| 1570 | assert_int_equal(10, ll->min); |
| 1571 | assert_int_equal(LYS_SET_MIN, ll->flags); |
| 1572 | lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL; |
| 1573 | |
| 1574 | /* invalid */ |
| 1575 | str = " ll {min-elements 1; default xx; type string;} ..."; |
| 1576 | assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 1577 | logbuf_assert("Invalid combination of keywords \"min-elements\" and \"default\" as substatements of \"leaf-list\". Line number 1."); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1578 | lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL; |
| 1579 | |
| 1580 | str = " ll {description \"missing type\";} ..."; |
| 1581 | assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); |
| 1582 | logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf-list\". Line number 1."); |
| 1583 | lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL; |
| 1584 | |
Radek Krejci | df6cad1 | 2018-11-28 17:10:55 +0100 | [diff] [blame] | 1585 | str = " ll {type string; min-elements 10; max-elements 1;} ..."; /* invalid combination of min/max */ |
| 1586 | assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); |
| 1587 | logbuf_assert("Invalid combination of min-elements and max-elements: min value 10 is bigger than the max value 1. Line number 1."); |
| 1588 | lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL; |
| 1589 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1590 | ctx.mod_version = 1; /* simulate YANG 1.0 - default statement is not allowed */ |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1591 | str = " ll {default xx; type string;} ..."; |
| 1592 | assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); |
| 1593 | logbuf_assert("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules. Line number 1."); |
| 1594 | lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL; |
| 1595 | |
| 1596 | *state = NULL; |
| 1597 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1598 | } |
| 1599 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1600 | static void |
| 1601 | test_list(void **state) |
| 1602 | { |
| 1603 | *state = test_list; |
| 1604 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1605 | struct lys_parser_ctx ctx = {0}; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1606 | struct lysp_node_list *l = NULL; |
| 1607 | const char *str; |
| 1608 | |
| 1609 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1610 | assert_non_null(ctx.ctx); |
| 1611 | ctx.line = 1; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1612 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1613 | |
| 1614 | /* invalid cardinality */ |
| 1615 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1616 | str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1617 | assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l)); \ |
| 1618 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1619 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1620 | |
| 1621 | TEST_DUP("config", "true", "false"); |
| 1622 | TEST_DUP("description", "text1", "text2"); |
| 1623 | TEST_DUP("key", "one", "two"); |
| 1624 | TEST_DUP("max-elements", "10", "20"); |
| 1625 | TEST_DUP("min-elements", "10", "20"); |
| 1626 | TEST_DUP("ordered-by", "user", "system"); |
| 1627 | TEST_DUP("reference", "1", "2"); |
| 1628 | TEST_DUP("status", "current", "obsolete"); |
| 1629 | TEST_DUP("when", "true", "false"); |
| 1630 | #undef TEST_DUP |
| 1631 | |
| 1632 | /* full content */ |
| 1633 | str = "l {action x;anydata any;anyxml anyxml; choice ch;config false;container c;description test;grouping g;if-feature f; key l; leaf l {type string;}" |
| 1634 | "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;" |
| 1635 | "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ..."; |
| 1636 | assert_int_equal(LY_SUCCESS, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l)); |
| 1637 | assert_non_null(l); |
| 1638 | assert_int_equal(LYS_LIST, l->nodetype); |
| 1639 | assert_string_equal("l", l->name); |
| 1640 | assert_string_equal("test", l->dsc); |
| 1641 | assert_string_equal("l", l->key); |
| 1642 | assert_non_null(l->uniques); |
| 1643 | assert_int_equal(2, LY_ARRAY_SIZE(l->uniques)); |
| 1644 | assert_string_equal("xxx", l->uniques[0]); |
| 1645 | assert_string_equal("yyy", l->uniques[1]); |
| 1646 | assert_int_equal(10, l->max); |
| 1647 | assert_int_equal(1, l->min); |
| 1648 | assert_non_null(l->exts); |
| 1649 | assert_non_null(l->iffeatures); |
| 1650 | assert_non_null(l->musts); |
| 1651 | assert_string_equal("test", l->ref); |
| 1652 | assert_non_null(l->when); |
| 1653 | assert_null(l->parent); |
| 1654 | assert_null(l->next); |
| 1655 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, l->flags); |
| 1656 | ly_set_erase(&ctx.tpdfs_nodes, NULL); |
| 1657 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1658 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1659 | /* invalid content */ |
| 1660 | ctx.mod_version = 1; /* simulate YANG 1.0 */ |
| 1661 | str = "l {action x;} ..."; |
| 1662 | assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l)); |
| 1663 | logbuf_assert("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules. Line number 1."); |
| 1664 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1665 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1666 | *state = NULL; |
| 1667 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1668 | } |
| 1669 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1670 | static void |
| 1671 | test_choice(void **state) |
| 1672 | { |
| 1673 | *state = test_choice; |
| 1674 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1675 | struct lys_parser_ctx ctx = {0}; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1676 | struct lysp_node_choice *ch = NULL; |
| 1677 | const char *str; |
| 1678 | |
| 1679 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1680 | assert_non_null(ctx.ctx); |
| 1681 | ctx.line = 1; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1682 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1683 | |
| 1684 | /* invalid cardinality */ |
| 1685 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1686 | str = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1687 | assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); \ |
| 1688 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1689 | lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL; |
| 1690 | |
| 1691 | TEST_DUP("config", "true", "false"); |
| 1692 | TEST_DUP("default", "a", "b"); |
| 1693 | TEST_DUP("description", "text1", "text2"); |
| 1694 | TEST_DUP("mandatory", "true", "false"); |
| 1695 | TEST_DUP("reference", "1", "2"); |
| 1696 | TEST_DUP("status", "current", "obsolete"); |
| 1697 | TEST_DUP("when", "true", "false"); |
| 1698 | #undef TEST_DUP |
| 1699 | |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 1700 | /* full content - without default due to a collision with mandatory */ |
| 1701 | str = "ch {anydata any;anyxml anyxml; case c;choice ch;config false;container c;description test;if-feature f;leaf l {type string;}" |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1702 | "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ..."; |
| 1703 | assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); |
| 1704 | assert_non_null(ch); |
| 1705 | assert_int_equal(LYS_CHOICE, ch->nodetype); |
| 1706 | assert_string_equal("ch", ch->name); |
| 1707 | assert_string_equal("test", ch->dsc); |
| 1708 | assert_non_null(ch->exts); |
| 1709 | assert_non_null(ch->iffeatures); |
| 1710 | assert_string_equal("test", ch->ref); |
| 1711 | assert_non_null(ch->when); |
| 1712 | assert_null(ch->parent); |
| 1713 | assert_null(ch->next); |
| 1714 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, ch->flags); |
| 1715 | lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL; |
| 1716 | |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 1717 | /* full content - the default missing from the previous node */ |
| 1718 | str = "ch {default c;case c;} ..."; |
| 1719 | assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); |
| 1720 | assert_non_null(ch); |
| 1721 | assert_int_equal(LYS_CHOICE, ch->nodetype); |
| 1722 | assert_string_equal("ch", ch->name); |
| 1723 | assert_string_equal("c", ch->dflt); |
| 1724 | assert_int_equal(0, ch->flags); |
| 1725 | lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL; |
| 1726 | |
| 1727 | /* invalid content */ |
| 1728 | str = "ch {mandatory true; default c1; case c1 {leaf x{type string;}}} ..."; |
| 1729 | assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); |
| 1730 | logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"choice\". Line number 1."); |
| 1731 | lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL; |
| 1732 | |
| 1733 | *state = NULL; |
| 1734 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1735 | } |
| 1736 | |
| 1737 | static void |
| 1738 | test_case(void **state) |
| 1739 | { |
| 1740 | *state = test_case; |
| 1741 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1742 | struct lys_parser_ctx ctx = {0}; |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 1743 | struct lysp_node_case *cs = NULL; |
| 1744 | const char *str; |
| 1745 | |
| 1746 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1747 | assert_non_null(ctx.ctx); |
| 1748 | ctx.line = 1; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1749 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 1750 | |
| 1751 | /* invalid cardinality */ |
| 1752 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1753 | str = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1754 | assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs)); \ |
| 1755 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1756 | lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL; |
| 1757 | |
| 1758 | TEST_DUP("description", "text1", "text2"); |
| 1759 | TEST_DUP("reference", "1", "2"); |
| 1760 | TEST_DUP("status", "current", "obsolete"); |
| 1761 | TEST_DUP("when", "true", "false"); |
| 1762 | #undef TEST_DUP |
| 1763 | |
| 1764 | /* full content */ |
| 1765 | str = "cs {anydata any;anyxml anyxml; choice ch;container c;description test;if-feature f;leaf l {type string;}" |
| 1766 | "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ..."; |
| 1767 | assert_int_equal(LY_SUCCESS, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs)); |
| 1768 | assert_non_null(cs); |
| 1769 | assert_int_equal(LYS_CASE, cs->nodetype); |
| 1770 | assert_string_equal("cs", cs->name); |
| 1771 | assert_string_equal("test", cs->dsc); |
| 1772 | assert_non_null(cs->exts); |
| 1773 | assert_non_null(cs->iffeatures); |
| 1774 | assert_string_equal("test", cs->ref); |
| 1775 | assert_non_null(cs->when); |
| 1776 | assert_null(cs->parent); |
| 1777 | assert_null(cs->next); |
| 1778 | assert_int_equal(LYS_STATUS_CURR, cs->flags); |
| 1779 | lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL; |
| 1780 | |
| 1781 | /* invalid content */ |
| 1782 | str = "cs {config true} ..."; |
| 1783 | assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs)); |
| 1784 | logbuf_assert("Invalid keyword \"config\" as a child of \"case\". Line number 1."); |
| 1785 | lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL; |
| 1786 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1787 | *state = NULL; |
| 1788 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1789 | } |
| 1790 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1791 | static void |
| 1792 | test_any(void **state, enum yang_keyword kw) |
| 1793 | { |
| 1794 | *state = test_any; |
| 1795 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1796 | struct lys_parser_ctx ctx = {0}; |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1797 | struct lysp_node_anydata *any = NULL; |
| 1798 | const char *str; |
| 1799 | |
| 1800 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1801 | assert_non_null(ctx.ctx); |
| 1802 | ctx.line = 1; |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1803 | if (kw == YANG_ANYDATA) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1804 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1805 | } else { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1806 | ctx.mod_version = 1; /* simulate YANG 1.0 */ |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | /* invalid cardinality */ |
| 1810 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1811 | str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1812 | assert_int_equal(LY_EVALID, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any)); \ |
| 1813 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1814 | lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL; |
| 1815 | |
| 1816 | TEST_DUP("config", "true", "false"); |
| 1817 | TEST_DUP("description", "text1", "text2"); |
| 1818 | TEST_DUP("mandatory", "true", "false"); |
| 1819 | TEST_DUP("reference", "1", "2"); |
| 1820 | TEST_DUP("status", "current", "obsolete"); |
| 1821 | TEST_DUP("when", "true", "false"); |
| 1822 | #undef TEST_DUP |
| 1823 | |
| 1824 | /* full content */ |
| 1825 | str = "any {config true;description test;if-feature f;mandatory true;must 'expr';reference test;status current;when true;m:ext;} ..."; |
| 1826 | assert_int_equal(LY_SUCCESS, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any)); |
| 1827 | assert_non_null(any); |
| 1828 | assert_int_equal(kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML, any->nodetype); |
| 1829 | assert_string_equal("any", any->name); |
| 1830 | assert_string_equal("test", any->dsc); |
| 1831 | assert_non_null(any->exts); |
| 1832 | assert_non_null(any->iffeatures); |
| 1833 | assert_non_null(any->musts); |
| 1834 | assert_string_equal("test", any->ref); |
| 1835 | assert_non_null(any->when); |
| 1836 | assert_null(any->parent); |
| 1837 | assert_null(any->next); |
| 1838 | assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags); |
| 1839 | lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL; |
| 1840 | |
| 1841 | *state = NULL; |
| 1842 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1843 | } |
| 1844 | |
| 1845 | static void |
| 1846 | test_anydata(void **state) |
| 1847 | { |
| 1848 | return test_any(state, YANG_ANYDATA); |
| 1849 | } |
| 1850 | |
| 1851 | static void |
| 1852 | test_anyxml(void **state) |
| 1853 | { |
| 1854 | return test_any(state, YANG_ANYXML); |
| 1855 | } |
| 1856 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1857 | static void |
| 1858 | test_grouping(void **state) |
| 1859 | { |
| 1860 | *state = test_grouping; |
| 1861 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1862 | struct lys_parser_ctx ctx = {0}; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1863 | struct lysp_grp *grp = NULL; |
| 1864 | const char *str; |
| 1865 | |
| 1866 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1867 | assert_non_null(ctx.ctx); |
| 1868 | ctx.line = 1; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1869 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1870 | |
| 1871 | /* invalid cardinality */ |
| 1872 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1873 | str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1874 | assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp)); \ |
| 1875 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1876 | FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL; |
| 1877 | |
| 1878 | TEST_DUP("description", "text1", "text2"); |
| 1879 | TEST_DUP("reference", "1", "2"); |
| 1880 | TEST_DUP("status", "current", "obsolete"); |
| 1881 | #undef TEST_DUP |
| 1882 | |
| 1883 | /* full content */ |
| 1884 | str = "grp {action x;anydata any;anyxml anyxml; choice ch;container c;description test;grouping g;leaf l {type string;}" |
| 1885 | "leaf-list ll {type string;} list li;notification not;reference test;status current;typedef t {type int8;}uses g;m:ext;} ..."; |
| 1886 | assert_int_equal(LY_SUCCESS, parse_grouping(&ctx, &str, NULL, &grp)); |
| 1887 | assert_non_null(grp); |
| 1888 | assert_int_equal(LYS_GROUPING, grp->nodetype); |
| 1889 | assert_string_equal("grp", grp->name); |
| 1890 | assert_string_equal("test", grp->dsc); |
| 1891 | assert_non_null(grp->exts); |
| 1892 | assert_string_equal("test", grp->ref); |
| 1893 | assert_null(grp->parent); |
| 1894 | assert_int_equal( LYS_STATUS_CURR, grp->flags); |
| 1895 | ly_set_erase(&ctx.tpdfs_nodes, NULL); |
| 1896 | FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL; |
| 1897 | |
| 1898 | /* invalid content */ |
| 1899 | str = "grp {config true} ..."; |
| 1900 | assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp)); |
| 1901 | logbuf_assert("Invalid keyword \"config\" as a child of \"grouping\". Line number 1."); |
| 1902 | FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL; |
| 1903 | |
| 1904 | str = "grp {must 'expr'} ..."; |
| 1905 | assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp)); |
| 1906 | logbuf_assert("Invalid keyword \"must\" as a child of \"grouping\". Line number 1."); |
| 1907 | FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL; |
| 1908 | |
| 1909 | *state = NULL; |
| 1910 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1911 | } |
| 1912 | |
| 1913 | static void |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1914 | test_action(void **state) |
| 1915 | { |
| 1916 | *state = test_action; |
| 1917 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1918 | struct lys_parser_ctx ctx = {0}; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1919 | struct lysp_action *rpcs = NULL; |
| 1920 | struct lysp_node_container *c = NULL; |
| 1921 | const char *str; |
| 1922 | |
| 1923 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1924 | assert_non_null(ctx.ctx); |
| 1925 | ctx.line = 1; |
| 1926 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
| 1927 | |
| 1928 | /* invalid cardinality */ |
| 1929 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1930 | str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1931 | assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs)); \ |
| 1932 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1933 | FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL; |
| 1934 | |
| 1935 | TEST_DUP("description", "text1", "text2"); |
| 1936 | TEST_DUP("input", "", ""); |
| 1937 | TEST_DUP("output", "", ""); |
| 1938 | TEST_DUP("reference", "1", "2"); |
| 1939 | TEST_DUP("status", "current", "obsolete"); |
| 1940 | #undef TEST_DUP |
| 1941 | |
| 1942 | /* full content */ |
| 1943 | str = "top;"; |
| 1944 | assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); |
| 1945 | str = "func {description test;grouping grp;if-feature f;reference test;status current;typedef mytype {type int8;} m:ext;" |
| 1946 | "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}" |
| 1947 | " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}" |
| 1948 | "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}" |
| 1949 | " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ..."; |
| 1950 | assert_int_equal(LY_SUCCESS, parse_action(&ctx, &str, (struct lysp_node*)c, &rpcs)); |
| 1951 | assert_non_null(rpcs); |
| 1952 | assert_int_equal(LYS_ACTION, rpcs->nodetype); |
| 1953 | assert_string_equal("func", rpcs->name); |
| 1954 | assert_string_equal("test", rpcs->dsc); |
| 1955 | assert_non_null(rpcs->exts); |
| 1956 | assert_non_null(rpcs->iffeatures); |
| 1957 | assert_string_equal("test", rpcs->ref); |
| 1958 | assert_non_null(rpcs->groupings); |
| 1959 | assert_non_null(rpcs->typedefs); |
| 1960 | assert_int_equal(LYS_STATUS_CURR, rpcs->flags); |
| 1961 | /* input */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1962 | assert_int_equal(rpcs->input.nodetype, LYS_INPUT); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1963 | assert_non_null(rpcs->input.groupings); |
| 1964 | assert_non_null(rpcs->input.exts); |
| 1965 | assert_non_null(rpcs->input.musts); |
| 1966 | assert_non_null(rpcs->input.typedefs); |
| 1967 | assert_non_null(rpcs->input.data); |
| 1968 | /* output */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1969 | assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1970 | assert_non_null(rpcs->output.groupings); |
| 1971 | assert_non_null(rpcs->output.exts); |
| 1972 | assert_non_null(rpcs->output.musts); |
| 1973 | assert_non_null(rpcs->output.typedefs); |
| 1974 | assert_non_null(rpcs->output.data); |
| 1975 | |
| 1976 | ly_set_erase(&ctx.tpdfs_nodes, NULL); |
| 1977 | FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL; |
| 1978 | |
| 1979 | /* invalid content */ |
| 1980 | str = "func {config true} ..."; |
| 1981 | assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs)); |
| 1982 | logbuf_assert("Invalid keyword \"config\" as a child of \"rpc\". Line number 1."); |
| 1983 | FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL; |
| 1984 | |
| 1985 | *state = NULL; |
| 1986 | lysp_node_free(ctx.ctx, (struct lysp_node*)c); |
| 1987 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1988 | } |
| 1989 | |
| 1990 | static void |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 1991 | test_notification(void **state) |
| 1992 | { |
| 1993 | *state = test_notification; |
| 1994 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1995 | struct lys_parser_ctx ctx = {0}; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 1996 | struct lysp_notif *notifs = NULL; |
| 1997 | struct lysp_node_container *c = NULL; |
| 1998 | const char *str; |
| 1999 | |
| 2000 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 2001 | assert_non_null(ctx.ctx); |
| 2002 | ctx.line = 1; |
| 2003 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
| 2004 | |
| 2005 | /* invalid cardinality */ |
| 2006 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 2007 | str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 2008 | assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, ¬ifs)); \ |
| 2009 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 2010 | FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL; |
| 2011 | |
| 2012 | TEST_DUP("description", "text1", "text2"); |
| 2013 | TEST_DUP("reference", "1", "2"); |
| 2014 | TEST_DUP("status", "current", "obsolete"); |
| 2015 | #undef TEST_DUP |
| 2016 | |
| 2017 | /* full content */ |
| 2018 | str = "top;"; |
| 2019 | assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); |
| 2020 | str = "ntf {anydata a1; anyxml a2; choice ch; container c; description test; grouping grp; if-feature f; leaf l {type int8;}" |
| 2021 | "leaf-list ll {type int8;} list li; must 1; reference test; status current; typedef mytype {type int8;} uses grp; m:ext;}"; |
| 2022 | assert_int_equal(LY_SUCCESS, parse_notif(&ctx, &str, (struct lysp_node*)c, ¬ifs)); |
| 2023 | assert_non_null(notifs); |
| 2024 | assert_int_equal(LYS_NOTIF, notifs->nodetype); |
| 2025 | assert_string_equal("ntf", notifs->name); |
| 2026 | assert_string_equal("test", notifs->dsc); |
| 2027 | assert_non_null(notifs->exts); |
| 2028 | assert_non_null(notifs->iffeatures); |
| 2029 | assert_string_equal("test", notifs->ref); |
| 2030 | assert_non_null(notifs->groupings); |
| 2031 | assert_non_null(notifs->typedefs); |
| 2032 | assert_non_null(notifs->musts); |
| 2033 | assert_non_null(notifs->data); |
| 2034 | assert_int_equal(LYS_STATUS_CURR, notifs->flags); |
| 2035 | |
| 2036 | ly_set_erase(&ctx.tpdfs_nodes, NULL); |
| 2037 | FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL; |
| 2038 | |
| 2039 | /* invalid content */ |
| 2040 | str = "ntf {config true} ..."; |
| 2041 | assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, ¬ifs)); |
| 2042 | logbuf_assert("Invalid keyword \"config\" as a child of \"notification\". Line number 1."); |
| 2043 | FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL; |
| 2044 | |
| 2045 | *state = NULL; |
| 2046 | lysp_node_free(ctx.ctx, (struct lysp_node*)c); |
| 2047 | ly_ctx_destroy(ctx.ctx, NULL); |
| 2048 | } |
| 2049 | |
| 2050 | static void |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2051 | test_uses(void **state) |
| 2052 | { |
| 2053 | *state = test_uses; |
| 2054 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2055 | struct lys_parser_ctx ctx = {0}; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2056 | struct lysp_node_uses *u = NULL; |
| 2057 | const char *str; |
| 2058 | |
| 2059 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 2060 | assert_non_null(ctx.ctx); |
| 2061 | ctx.line = 1; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2062 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2063 | |
| 2064 | /* invalid cardinality */ |
| 2065 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 2066 | str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 2067 | assert_int_equal(LY_EVALID, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u)); \ |
| 2068 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 2069 | lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL; |
| 2070 | |
| 2071 | TEST_DUP("description", "text1", "text2"); |
| 2072 | TEST_DUP("reference", "1", "2"); |
| 2073 | TEST_DUP("status", "current", "obsolete"); |
| 2074 | TEST_DUP("when", "true", "false"); |
| 2075 | #undef TEST_DUP |
| 2076 | |
| 2077 | /* full content */ |
| 2078 | str = "grpref {augment some/node;description test;if-feature f;reference test;refine some/other/node;status current;when true;m:ext;} ..."; |
| 2079 | assert_int_equal(LY_SUCCESS, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u)); |
| 2080 | assert_non_null(u); |
| 2081 | assert_int_equal(LYS_USES, u->nodetype); |
| 2082 | assert_string_equal("grpref", u->name); |
| 2083 | assert_string_equal("test", u->dsc); |
| 2084 | assert_non_null(u->exts); |
| 2085 | assert_non_null(u->iffeatures); |
| 2086 | assert_string_equal("test", u->ref); |
| 2087 | assert_non_null(u->augments); |
| 2088 | assert_non_null(u->refines); |
| 2089 | assert_non_null(u->when); |
| 2090 | assert_null(u->parent); |
| 2091 | assert_null(u->next); |
| 2092 | assert_int_equal(LYS_STATUS_CURR, u->flags); |
| 2093 | lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL; |
| 2094 | |
| 2095 | *state = NULL; |
| 2096 | ly_ctx_destroy(ctx.ctx, NULL); |
| 2097 | } |
Radek Krejci | 5a7c4a5 | 2019-02-12 15:45:11 +0100 | [diff] [blame] | 2098 | |
| 2099 | |
| 2100 | static void |
| 2101 | test_augment(void **state) |
| 2102 | { |
| 2103 | *state = test_augment; |
| 2104 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 2105 | struct lys_parser_ctx ctx = {0}; |
Radek Krejci | 5a7c4a5 | 2019-02-12 15:45:11 +0100 | [diff] [blame] | 2106 | struct lysp_augment *a = NULL; |
| 2107 | const char *str; |
| 2108 | |
| 2109 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 2110 | assert_non_null(ctx.ctx); |
| 2111 | ctx.line = 1; |
Radek Krejci | b4adbf1 | 2019-02-25 13:35:22 +0100 | [diff] [blame] | 2112 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 5a7c4a5 | 2019-02-12 15:45:11 +0100 | [diff] [blame] | 2113 | |
| 2114 | /* invalid cardinality */ |
| 2115 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 2116 | str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 2117 | assert_int_equal(LY_EVALID, parse_augment(&ctx, &str, NULL, &a)); \ |
| 2118 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
Radek Krejci | b4adbf1 | 2019-02-25 13:35:22 +0100 | [diff] [blame] | 2119 | FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL; |
Radek Krejci | 5a7c4a5 | 2019-02-12 15:45:11 +0100 | [diff] [blame] | 2120 | |
| 2121 | TEST_DUP("description", "text1", "text2"); |
| 2122 | TEST_DUP("reference", "1", "2"); |
| 2123 | TEST_DUP("status", "current", "obsolete"); |
| 2124 | TEST_DUP("when", "true", "false"); |
| 2125 | #undef TEST_DUP |
| 2126 | |
| 2127 | /* full content */ |
| 2128 | str = "/target/nodeid {action x; anydata any;anyxml anyxml; case cs; choice ch;container c;description test;if-feature f;leaf l {type string;}" |
| 2129 | "leaf-list ll {type string;} list li;notification not;reference test;status current;uses g;when true;m:ext;} ..."; |
| 2130 | assert_int_equal(LY_SUCCESS, parse_augment(&ctx, &str, NULL, &a)); |
| 2131 | assert_non_null(a); |
| 2132 | assert_int_equal(LYS_AUGMENT, a->nodetype); |
| 2133 | assert_string_equal("/target/nodeid", a->nodeid); |
| 2134 | assert_string_equal("test", a->dsc); |
| 2135 | assert_non_null(a->exts); |
| 2136 | assert_non_null(a->iffeatures); |
| 2137 | assert_string_equal("test", a->ref); |
| 2138 | assert_non_null(a->when); |
| 2139 | assert_null(a->parent); |
| 2140 | assert_int_equal(LYS_STATUS_CURR, a->flags); |
Radek Krejci | b4adbf1 | 2019-02-25 13:35:22 +0100 | [diff] [blame] | 2141 | FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL; |
Radek Krejci | 5a7c4a5 | 2019-02-12 15:45:11 +0100 | [diff] [blame] | 2142 | |
| 2143 | *state = NULL; |
| 2144 | ly_ctx_destroy(ctx.ctx, NULL); |
| 2145 | } |
| 2146 | |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 2147 | int main(void) |
| 2148 | { |
| 2149 | const struct CMUnitTest tests[] = { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2150 | cmocka_unit_test_setup(test_helpers, logger_setup), |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 2151 | cmocka_unit_test_setup(test_comments, logger_setup), |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2152 | cmocka_unit_test_setup(test_arg, logger_setup), |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 2153 | cmocka_unit_test_setup(test_stmts, logger_setup), |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 2154 | cmocka_unit_test_setup_teardown(test_minmax, logger_setup, logger_teardown), |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 2155 | cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown), |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2156 | cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown), |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 2157 | cmocka_unit_test_setup(test_feature, logger_setup), |
| 2158 | cmocka_unit_test_setup(test_deviation, logger_setup), |
| 2159 | cmocka_unit_test_setup(test_deviate, logger_setup), |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 2160 | cmocka_unit_test_setup(test_container, logger_setup), |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 2161 | cmocka_unit_test_setup_teardown(test_leaf, logger_setup, logger_teardown), |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 2162 | cmocka_unit_test_setup_teardown(test_leaflist, logger_setup, logger_teardown), |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2163 | cmocka_unit_test_setup_teardown(test_list, logger_setup, logger_teardown), |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2164 | cmocka_unit_test_setup_teardown(test_choice, logger_setup, logger_teardown), |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 2165 | cmocka_unit_test_setup_teardown(test_case, logger_setup, logger_teardown), |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 2166 | cmocka_unit_test_setup_teardown(test_anydata, logger_setup, logger_teardown), |
| 2167 | cmocka_unit_test_setup_teardown(test_anyxml, logger_setup, logger_teardown), |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 2168 | cmocka_unit_test_setup_teardown(test_action, logger_setup, logger_teardown), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 2169 | cmocka_unit_test_setup_teardown(test_notification, logger_setup, logger_teardown), |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2170 | cmocka_unit_test_setup_teardown(test_grouping, logger_setup, logger_teardown), |
| 2171 | cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown), |
Radek Krejci | b4adbf1 | 2019-02-25 13:35:22 +0100 | [diff] [blame] | 2172 | cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown), |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 2173 | }; |
| 2174 | |
| 2175 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 2176 | } |