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