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