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