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 | * |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 6 | * Copyright (c) 2018-2020 CESNET, z.s.p.o. |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 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 | */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 14 | #define _UTEST_MAIN_ |
| 15 | #include "utests.h" |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 16 | |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | #include <string.h> |
| 19 | |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 20 | #include "common.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 21 | #include "in_internal.h" |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 22 | #include "parser_internal.h" |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 23 | #include "schema_compile.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 24 | #include "tree_edit.h" |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 25 | #include "tree_schema.h" |
| 26 | #include "tree_schema_internal.h" |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 27 | |
| 28 | /* originally static functions from tree_schema_free.c and parser_yang.c */ |
| 29 | void lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext); |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 30 | void lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 31 | void lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp); |
| 32 | void lysp_augment_free(struct ly_ctx *ctx, struct lysp_node_augment *augment); |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 33 | void lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d); |
| 34 | void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node); |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 35 | void lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when); |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 36 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 37 | LY_ERR buf_add_char(struct ly_ctx *ctx, struct ly_in *in, size_t len, char **buf, size_t *buf_len, size_t *buf_used); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 38 | LY_ERR buf_store_char(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, char **word_p, |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 39 | size_t *word_len, char **word_b, size_t *buf_len, uint8_t need_buf, uint8_t *prefix); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 40 | LY_ERR get_keyword(struct lys_yang_parser_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len); |
| 41 | LY_ERR get_argument(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 42 | uint16_t *flags, char **word_p, char **word_b, size_t *word_len); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 43 | LY_ERR skip_comment(struct lys_yang_parser_ctx *ctx, uint8_t comment); |
Radek Krejci | 2d7a47b | 2019-05-16 13:34:10 +0200 | [diff] [blame] | 44 | |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 45 | LY_ERR parse_action(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 46 | LY_ERR parse_any(struct lys_yang_parser_ctx *ctx, enum ly_stmt kw, struct lysp_node *parent, struct lysp_node **siblings); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 47 | LY_ERR parse_augment(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 48 | LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings); |
| 49 | LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings); |
| 50 | LY_ERR parse_deviate(struct lys_yang_parser_ctx *ctx, struct lysp_deviate **deviates); |
| 51 | LY_ERR parse_deviation(struct lys_yang_parser_ctx *ctx, struct lysp_deviation **deviations); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 52 | LY_ERR parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 53 | LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings); |
| 54 | LY_ERR parse_leaf(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings); |
| 55 | LY_ERR parse_leaflist(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings); |
| 56 | LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings); |
| 57 | LY_ERR parse_maxelements(struct lys_yang_parser_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts); |
| 58 | LY_ERR parse_minelements(struct lys_yang_parser_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts); |
| 59 | LY_ERR parse_module(struct lys_yang_parser_ctx *ctx, struct lysp_module *mod); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 60 | LY_ERR parse_notif(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 61 | LY_ERR parse_submodule(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod); |
| 62 | LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings); |
| 63 | LY_ERR parse_when(struct lys_yang_parser_ctx *ctx, struct lysp_when **when_p); |
| 64 | LY_ERR parse_type_enum_value_pos(struct lys_yang_parser_ctx *ctx, 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] | 65 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 66 | struct lys_yang_parser_ctx *YCTX; |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 67 | |
Radek Krejci | 1640e80 | 2021-01-08 11:01:30 +0100 | [diff] [blame] | 68 | struct ly_in in = {0}; |
| 69 | |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 70 | #define YCTX_INIT \ |
Radek Krejci | d54412f | 2020-12-17 20:25:35 +0100 | [diff] [blame] | 71 | in.line = 1; \ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 72 | YCTX->in = ∈ \ |
| 73 | LOG_LOCINIT(UTEST_LYCTX, NULL, NULL, NULL, &in) |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 74 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 75 | static int |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 76 | setup(void **state) |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 77 | { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 78 | UTEST_SETUP; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 79 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 80 | /* allocate parser context */ |
| 81 | YCTX = calloc(1, sizeof(*YCTX)); |
| 82 | YCTX->format = LYS_IN_YANG; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 83 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 84 | /* allocate new parsed module */ |
| 85 | YCTX->parsed_mod = calloc(1, sizeof *YCTX->parsed_mod); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 86 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 87 | /* allocate new module */ |
| 88 | YCTX->parsed_mod->mod = calloc(1, sizeof *YCTX->parsed_mod->mod); |
| 89 | YCTX->parsed_mod->mod->ctx = UTEST_LYCTX; |
| 90 | YCTX->parsed_mod->mod->parsed = YCTX->parsed_mod; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 91 | |
Radek Krejci | 1640e80 | 2021-01-08 11:01:30 +0100 | [diff] [blame] | 92 | /* initilize and use the global easily available and customizable input handler */ |
| 93 | in.line = 1; |
| 94 | YCTX->in = ∈ |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 95 | LOG_LOCINIT(NULL, NULL, NULL, &in); |
Radek Krejci | 1640e80 | 2021-01-08 11:01:30 +0100 | [diff] [blame] | 96 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static int |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 101 | teardown(void **state) |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 102 | { |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 103 | lys_module_free(YCTX->parsed_mod->mod); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 104 | LOG_LOCBACK(0, 0, 0, 1); |
Radek Krejci | 1640e80 | 2021-01-08 11:01:30 +0100 | [diff] [blame] | 105 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 106 | free(YCTX); |
| 107 | YCTX = NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 108 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 109 | UTEST_TEARDOWN; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 114 | #define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 115 | in.current = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 116 | assert_int_equal(LY_EVALID, FUNC(YCTX, RESULT)); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 117 | CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number "LINE".");\ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 118 | CLEANUP |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 119 | static void |
| 120 | test_helpers(void **state) |
| 121 | { |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 122 | char *buf, *p; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 123 | size_t len, size; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 124 | uint8_t prefix = 0; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 125 | |
| 126 | /* storing into buffer */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 127 | in.current = "abcd"; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 128 | buf = NULL; |
| 129 | size = len = 0; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 130 | assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &in, 2, &buf, &size, &len)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 131 | assert_int_not_equal(0, size); |
| 132 | assert_int_equal(2, len); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 133 | assert_string_equal("cd", in.current); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 134 | assert_false(strncmp("ab", buf, 2)); |
| 135 | free(buf); |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 136 | buf = NULL; |
| 137 | |
| 138 | /* invalid first characters */ |
| 139 | len = 0; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 140 | in.current = "2invalid"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 141 | assert_int_equal(LY_EVALID, buf_store_char(YCTX, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 142 | in.current = ".invalid"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 143 | assert_int_equal(LY_EVALID, buf_store_char(YCTX, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 144 | in.current = "-invalid"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 145 | assert_int_equal(LY_EVALID, buf_store_char(YCTX, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix)); |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 146 | /* invalid following characters */ |
| 147 | len = 3; /* number of characters read before the str content */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 148 | in.current = "!"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 149 | assert_int_equal(LY_EVALID, buf_store_char(YCTX, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 150 | in.current = ":"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 151 | assert_int_equal(LY_EVALID, buf_store_char(YCTX, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix)); |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 152 | /* valid colon for prefixed identifiers */ |
| 153 | len = size = 0; |
| 154 | p = NULL; |
David Sedlák | 40bb13b | 2019-07-10 14:34:18 +0200 | [diff] [blame] | 155 | prefix = 0; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 156 | in.current = "x:id"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 157 | assert_int_equal(LY_SUCCESS, buf_store_char(YCTX, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 0, &prefix)); |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 158 | assert_int_equal(1, len); |
| 159 | assert_null(buf); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 160 | assert_string_equal(":id", in.current); |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 161 | assert_int_equal('x', p[len - 1]); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 162 | assert_int_equal(LY_SUCCESS, buf_store_char(YCTX, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix)); |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 163 | assert_int_equal(2, len); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 164 | assert_string_equal("id", in.current); |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 165 | assert_int_equal(':', p[len - 1]); |
| 166 | free(buf); |
David Sedlák | 40bb13b | 2019-07-10 14:34:18 +0200 | [diff] [blame] | 167 | prefix = 0; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 168 | |
| 169 | /* checking identifiers */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 170 | assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, NULL)); |
| 171 | CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1."); |
| 172 | assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, '#', 1, NULL)); |
| 173 | CHECK_LOG_CTX("Invalid identifier first character '#' (0x0023).", "Line number 1."); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 174 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 175 | assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, 'a', 1, &prefix)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 176 | assert_int_equal(0, prefix); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 177 | assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, &prefix)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 178 | assert_int_equal(1, prefix); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 179 | assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, &prefix)); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 180 | assert_int_equal(1, prefix); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 181 | assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, 'b', 0, &prefix)); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 182 | assert_int_equal(2, prefix); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 183 | /* second colon is invalid */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 184 | assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, &prefix)); |
| 185 | CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1."); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 186 | } |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 187 | |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 188 | #define TEST_GET_ARGUMENT_SUCCESS(INPUT_TEXT, CTX, ARG_TYPE, EXPECT_WORD, EXPECT_LEN, EXPECT_CURRENT, EXPECT_LINE)\ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 189 | {\ |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 190 | const char * text = INPUT_TEXT;\ |
| 191 | in.line = 1;\ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 192 | in.current = text;\ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 193 | assert_int_equal(LY_SUCCESS, get_argument(CTX, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));\ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 194 | assert_string_equal(word, EXPECT_WORD);\ |
| 195 | assert_int_equal(len, EXPECT_LEN);\ |
| 196 | assert_string_equal(EXPECT_CURRENT, in.current);\ |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 197 | assert_int_equal(EXPECT_LINE, in.line);\ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 198 | } |
| 199 | |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 200 | static void |
| 201 | test_comments(void **state) |
| 202 | { |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 203 | char *word, *buf; |
| 204 | size_t len; |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 205 | |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 206 | TEST_GET_ARGUMENT_SUCCESS(" // this is a text of / one * line */ comment\nargument;", |
| 207 | YCTX, Y_STR_ARG, "argument;", 8, ";", 2); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 208 | assert_null(buf); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 209 | |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 210 | TEST_GET_ARGUMENT_SUCCESS("/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\";", |
| 211 | YCTX, Y_STR_ARG, "argument", 8, ";", 4); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 212 | assert_ptr_equal(buf, word); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 213 | free(word); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 214 | |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 215 | in.line = 1; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 216 | in.current = " this is one line comment on last line"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 217 | assert_int_equal(LY_SUCCESS, skip_comment(YCTX, 1)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 218 | assert_true(in.current[0] == '\0'); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 219 | |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 220 | in.line = 1; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 221 | in.current = " this is a not terminated comment x"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 222 | assert_int_equal(LY_EVALID, skip_comment(YCTX, 2)); |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 223 | CHECK_LOG_CTX("Unexpected end-of-input, non-terminated comment.", "Line number 1."); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 224 | assert_true(in.current[0] == '\0'); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 225 | } |
| 226 | |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 227 | static void |
| 228 | test_arg(void **state) |
| 229 | { |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 230 | char *word, *buf; |
| 231 | size_t len; |
| 232 | |
| 233 | /* missing argument */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 234 | in.current = ";"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 235 | assert_int_equal(LY_SUCCESS, get_argument(YCTX, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len)); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 236 | assert_null(word); |
| 237 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 238 | in.current = "{"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 239 | assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 240 | CHECK_LOG_CTX("Invalid character sequence \"{\", expected an argument.", "Line number 1."); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 241 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 242 | /* invalid escape sequence */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 243 | in.current = "\"\\s\""; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 244 | assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 245 | CHECK_LOG_CTX("Double-quoted string unknown special character \'\\s\'.", "Line number 1."); |
| 246 | |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 247 | TEST_GET_ARGUMENT_SUCCESS("\'\\s\'", YCTX, Y_STR_ARG, "\\s\'", 2, "", 1); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 248 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 249 | /* invalid character after the argument */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 250 | in.current = "hello\""; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 251 | assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 252 | CHECK_LOG_CTX("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace.", "Line number 1."); |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 253 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 254 | in.current = "hello}"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 255 | assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 256 | CHECK_LOG_CTX("Invalid character sequence \"}\", expected unquoted string character, optsep, semicolon or opening brace.", "Line number 1."); |
David Sedlák | 40bb13b | 2019-07-10 14:34:18 +0200 | [diff] [blame] | 257 | /* invalid identifier-ref-arg-str */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 258 | in.current = "pre:pre:value"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 259 | assert_int_equal(LY_EVALID, get_argument(YCTX, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 260 | CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1."); |
David Sedlák | 40bb13b | 2019-07-10 14:34:18 +0200 | [diff] [blame] | 261 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 262 | in.current = "\"\";"; /* empty identifier is not allowed */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 263 | assert_int_equal(LY_EVALID, get_argument(YCTX, Y_IDENTIF_ARG, NULL, &word, &buf, &len)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 264 | CHECK_LOG_CTX("Statement argument is required.", "Line number 1."); |
| 265 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 266 | in.current = "\"\";"; /* empty reference identifier is not allowed */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 267 | assert_int_equal(LY_EVALID, get_argument(YCTX, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 268 | CHECK_LOG_CTX("Statement argument is required.", "Line number 1."); |
Radek Krejci | 4e199f5 | 2019-05-28 09:09:28 +0200 | [diff] [blame] | 269 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 270 | /* slash is not an invalid character */ |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 271 | TEST_GET_ARGUMENT_SUCCESS("hello/x\t", YCTX, Y_STR_ARG, "hello/x\t", 7, "\t", 1); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 272 | assert_null(buf); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 273 | |
| 274 | /* different quoting */ |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 275 | TEST_GET_ARGUMENT_SUCCESS("hello/x\t", YCTX, Y_STR_ARG, "hello/x\t", 7, "\t", 1); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 276 | |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 277 | TEST_GET_ARGUMENT_SUCCESS("hello ", YCTX, Y_STR_ARG, "hello ", 5, " ", 1); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 278 | |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 279 | TEST_GET_ARGUMENT_SUCCESS("hello/*comment*/\n", YCTX, Y_STR_ARG, "hello/*comment*/\n", 5, "\n", 1); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 280 | |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 281 | TEST_GET_ARGUMENT_SUCCESS("\"hello\\n\\t\\\"\\\\\";", YCTX, Y_STR_ARG, "hello\n\t\"\\", 9, ";", 1); |
fredgan | d49fe11 | 2019-10-21 20:51:50 +0800 | [diff] [blame] | 282 | free(buf); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 283 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 284 | YCTX->indent = 14; |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 285 | /* - space and tabs before newline are stripped out |
| 286 | * - space and tabs after newline (indentation) are stripped out |
| 287 | */ |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 288 | TEST_GET_ARGUMENT_SUCCESS("\"hello \t\n\t\t world!\"", YCTX, Y_STR_ARG, "hello\n world!", 14, "", 2); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 289 | free(buf); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 290 | |
| 291 | /* In contrast to previous, the backslash-escaped tabs are expanded after trimming, so they are preserved */ |
| 292 | YCTX->indent = 14; |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 293 | TEST_GET_ARGUMENT_SUCCESS("\"hello \\t\n\t\\t world!\"", YCTX, Y_STR_ARG, "hello \t\n\t world!", 16, "", 2); |
Radek Krejci | ff13cd1 | 2019-10-25 15:34:24 +0200 | [diff] [blame] | 294 | assert_ptr_equal(word, buf); |
Radek Krejci | ff13cd1 | 2019-10-25 15:34:24 +0200 | [diff] [blame] | 295 | free(buf); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 296 | |
Radek Krejci | ff13cd1 | 2019-10-25 15:34:24 +0200 | [diff] [blame] | 297 | /* Do not handle whitespaces after backslash-escaped newline as indentation */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 298 | YCTX->indent = 14; |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 299 | TEST_GET_ARGUMENT_SUCCESS("\"hello\\n\t\t world!\"", YCTX, Y_STR_ARG, "hello\n\t\t world!", 15, "", 1); |
Radek Krejci | ff13cd1 | 2019-10-25 15:34:24 +0200 | [diff] [blame] | 300 | assert_ptr_equal(word, buf); |
Radek Krejci | ff13cd1 | 2019-10-25 15:34:24 +0200 | [diff] [blame] | 301 | free(buf); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 302 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 303 | YCTX->indent = 14; |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 304 | TEST_GET_ARGUMENT_SUCCESS("\"hello\n \tworld!\"", YCTX, Y_STR_ARG, "hello\nworld!", 12, "", 2); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 305 | assert_ptr_equal(word, buf); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 306 | free(buf); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 307 | |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 308 | TEST_GET_ARGUMENT_SUCCESS("\'hello\'", YCTX, Y_STR_ARG, "hello'", 5, "", 1); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 309 | |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 310 | TEST_GET_ARGUMENT_SUCCESS("\"hel\" +\t\n\"lo\"", YCTX, Y_STR_ARG, "hello", 5, "", 2); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 311 | assert_ptr_equal(word, buf); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 312 | free(buf); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 313 | |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 314 | in.line = 1; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 315 | in.current = "\"hel\" +\t\nlo"; /* unquoted the second part */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 316 | assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 317 | CHECK_LOG_CTX("Both string parts divided by '+' must be quoted.", "Line number 2."); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 318 | |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 319 | TEST_GET_ARGUMENT_SUCCESS("\'he\'\t\n+ \"llo\"", YCTX, Y_STR_ARG, "hello", 5, "", 2); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 320 | free(buf); |
| 321 | |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 322 | TEST_GET_ARGUMENT_SUCCESS(" \t\n\"he\"+\'llo\'", YCTX, Y_STR_ARG, "hello", 5, "", 2); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 323 | free(buf); |
| 324 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 325 | /* missing argument */ |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 326 | in.line = 1; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 327 | in.current = ";"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 328 | assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len)); |
Radek Krejci | dd713ce | 2021-01-04 23:12:12 +0100 | [diff] [blame] | 329 | CHECK_LOG_CTX("Invalid character sequence \";\", expected an argument.", "Line number 1."); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 330 | } |
| 331 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 332 | #define TEST_STMS_SUCCESS(INPUT_TEXT, CTX, ACTION, EXPECT_WORD)\ |
| 333 | in.current = INPUT_TEXT;\ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 334 | assert_int_equal(LY_SUCCESS, get_keyword(CTX, &kw, &word, &len));\ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 335 | assert_int_equal(ACTION, kw);\ |
| 336 | assert_int_equal(strlen(EXPECT_WORD), len);\ |
| 337 | assert_true(0 == strncmp(EXPECT_WORD, word, len)) |
| 338 | |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 339 | static void |
| 340 | test_stmts(void **state) |
| 341 | { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 342 | const char *p; |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 343 | enum ly_stmt kw; |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 344 | char *word; |
| 345 | size_t len; |
| 346 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 347 | in.current = "\n// comment\n\tinput\t{"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 348 | assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len)); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 349 | assert_int_equal(LY_STMT_INPUT, kw); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 350 | assert_int_equal(5, len); |
| 351 | assert_string_equal("input\t{", word); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 352 | assert_string_equal("\t{", in.current); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 353 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 354 | in.current = "\t /* comment */\t output\n\t{"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 355 | assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len)); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 356 | assert_int_equal(LY_STMT_OUTPUT, kw); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 357 | assert_int_equal(6, len); |
| 358 | assert_string_equal("output\n\t{", word); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 359 | assert_string_equal("\n\t{", in.current); |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 360 | assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len)); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 361 | assert_int_equal(LY_STMT_SYNTAX_LEFT_BRACE, kw); |
Radek Krejci | abdd806 | 2019-06-11 16:44:19 +0200 | [diff] [blame] | 362 | assert_int_equal(1, len); |
| 363 | assert_string_equal("{", word); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 364 | assert_string_equal("", in.current); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 365 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 366 | in.current = "/input { "; /* invalid slash */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 367 | assert_int_equal(LY_EVALID, get_keyword(YCTX, &kw, &word, &len)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 368 | CHECK_LOG_CTX("Invalid identifier first character '/'.", "Line number 4."); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 369 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 370 | in.current = "not-a-statement-nor-extension { "; /* invalid identifier */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 371 | assert_int_equal(LY_EVALID, get_keyword(YCTX, &kw, &word, &len)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 372 | CHECK_LOG_CTX("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword.", "Line number 4."); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 373 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 374 | in.current = "path;"; /* missing sep after the keyword */ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 375 | assert_int_equal(LY_EVALID, get_keyword(YCTX, &kw, &word, &len)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 376 | CHECK_LOG_CTX("Invalid character sequence \"path;\", expected a keyword followed by a separator.", "Line number 4."); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 377 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 378 | TEST_STMS_SUCCESS("action ", YCTX, LY_STMT_ACTION, "action"); |
| 379 | |
| 380 | TEST_STMS_SUCCESS("anydata ", YCTX, LY_STMT_ANYDATA, "anydata"); |
| 381 | TEST_STMS_SUCCESS("anyxml ", YCTX, LY_STMT_ANYXML, "anyxml"); |
| 382 | TEST_STMS_SUCCESS("argument ", YCTX, LY_STMT_ARGUMENT, "argument"); |
| 383 | TEST_STMS_SUCCESS("augment ", YCTX, LY_STMT_AUGMENT, "augment"); |
| 384 | TEST_STMS_SUCCESS("base ", YCTX, LY_STMT_BASE, "base"); |
| 385 | TEST_STMS_SUCCESS("belongs-to ", YCTX, LY_STMT_BELONGS_TO, "belongs-to"); |
| 386 | TEST_STMS_SUCCESS("bit ", YCTX, LY_STMT_BIT, "bit"); |
| 387 | TEST_STMS_SUCCESS("case ", YCTX, LY_STMT_CASE, "case"); |
| 388 | TEST_STMS_SUCCESS("choice ", YCTX, LY_STMT_CHOICE, "choice"); |
| 389 | TEST_STMS_SUCCESS("config ", YCTX, LY_STMT_CONFIG, "config"); |
| 390 | TEST_STMS_SUCCESS("contact ", YCTX, LY_STMT_CONTACT, "contact"); |
| 391 | TEST_STMS_SUCCESS("container ", YCTX, LY_STMT_CONTAINER, "container"); |
| 392 | TEST_STMS_SUCCESS("default ", YCTX, LY_STMT_DEFAULT, "default"); |
| 393 | TEST_STMS_SUCCESS("description ", YCTX, LY_STMT_DESCRIPTION, "description"); |
| 394 | TEST_STMS_SUCCESS("deviate ", YCTX, LY_STMT_DEVIATE, "deviate"); |
| 395 | TEST_STMS_SUCCESS("deviation ", YCTX, LY_STMT_DEVIATION, "deviation"); |
| 396 | TEST_STMS_SUCCESS("enum ", YCTX, LY_STMT_ENUM, "enum"); |
| 397 | TEST_STMS_SUCCESS("error-app-tag ", YCTX, LY_STMT_ERROR_APP_TAG, "error-app-tag"); |
| 398 | TEST_STMS_SUCCESS("error-message ", YCTX, LY_STMT_ERROR_MESSAGE, "error-message"); |
| 399 | TEST_STMS_SUCCESS("extension ", YCTX, LY_STMT_EXTENSION, "extension"); |
| 400 | TEST_STMS_SUCCESS("feature ", YCTX, LY_STMT_FEATURE, "feature"); |
| 401 | TEST_STMS_SUCCESS("fraction-digits ", YCTX, LY_STMT_FRACTION_DIGITS, "fraction-digits"); |
| 402 | TEST_STMS_SUCCESS("grouping ", YCTX, LY_STMT_GROUPING, "grouping"); |
| 403 | TEST_STMS_SUCCESS("identity ", YCTX, LY_STMT_IDENTITY, "identity"); |
| 404 | TEST_STMS_SUCCESS("if-feature ", YCTX, LY_STMT_IF_FEATURE, "if-feature"); |
| 405 | TEST_STMS_SUCCESS("import ", YCTX, LY_STMT_IMPORT, "import"); |
| 406 | TEST_STMS_SUCCESS("include ", YCTX, LY_STMT_INCLUDE, "include"); |
| 407 | TEST_STMS_SUCCESS("input{", YCTX, LY_STMT_INPUT, "input"); |
| 408 | TEST_STMS_SUCCESS("key ", YCTX, LY_STMT_KEY, "key"); |
| 409 | TEST_STMS_SUCCESS("leaf ", YCTX, LY_STMT_LEAF, "leaf"); |
| 410 | TEST_STMS_SUCCESS("leaf-list ", YCTX, LY_STMT_LEAF_LIST, "leaf-list"); |
| 411 | TEST_STMS_SUCCESS("length ", YCTX, LY_STMT_LENGTH, "length"); |
| 412 | TEST_STMS_SUCCESS("list ", YCTX, LY_STMT_LIST, "list"); |
| 413 | TEST_STMS_SUCCESS("mandatory ", YCTX, LY_STMT_MANDATORY, "mandatory"); |
| 414 | TEST_STMS_SUCCESS("max-elements ", YCTX, LY_STMT_MAX_ELEMENTS, "max-elements"); |
| 415 | TEST_STMS_SUCCESS("min-elements ", YCTX, LY_STMT_MIN_ELEMENTS, "min-elements"); |
| 416 | TEST_STMS_SUCCESS("modifier ", YCTX, LY_STMT_MODIFIER, "modifier"); |
| 417 | TEST_STMS_SUCCESS("module ", YCTX, LY_STMT_MODULE, "module"); |
| 418 | TEST_STMS_SUCCESS("must ", YCTX, LY_STMT_MUST, "must"); |
| 419 | TEST_STMS_SUCCESS("namespace ", YCTX, LY_STMT_NAMESPACE, "namespace"); |
| 420 | TEST_STMS_SUCCESS("notification ", YCTX, LY_STMT_NOTIFICATION, "notification"); |
| 421 | TEST_STMS_SUCCESS("ordered-by ", YCTX, LY_STMT_ORDERED_BY, "ordered-by"); |
| 422 | TEST_STMS_SUCCESS("organization ", YCTX, LY_STMT_ORGANIZATION, "organization"); |
| 423 | TEST_STMS_SUCCESS("output ", YCTX, LY_STMT_OUTPUT, "output"); |
| 424 | TEST_STMS_SUCCESS("path ", YCTX, LY_STMT_PATH, "path"); |
| 425 | TEST_STMS_SUCCESS("pattern ", YCTX, LY_STMT_PATTERN, "pattern"); |
| 426 | TEST_STMS_SUCCESS("position ", YCTX, LY_STMT_POSITION, "position"); |
| 427 | TEST_STMS_SUCCESS("prefix ", YCTX, LY_STMT_PREFIX, "prefix"); |
| 428 | TEST_STMS_SUCCESS("presence ", YCTX, LY_STMT_PRESENCE, "presence"); |
| 429 | TEST_STMS_SUCCESS("range ", YCTX, LY_STMT_RANGE, "range"); |
| 430 | TEST_STMS_SUCCESS("reference ", YCTX, LY_STMT_REFERENCE, "reference"); |
| 431 | TEST_STMS_SUCCESS("refine ", YCTX, LY_STMT_REFINE, "refine"); |
| 432 | TEST_STMS_SUCCESS("require-instance ", YCTX, LY_STMT_REQUIRE_INSTANCE, "require-instance"); |
| 433 | TEST_STMS_SUCCESS("revision ", YCTX, LY_STMT_REVISION, "revision"); |
| 434 | TEST_STMS_SUCCESS("revision-date ", YCTX, LY_STMT_REVISION_DATE, "revision-date"); |
| 435 | TEST_STMS_SUCCESS("rpc ", YCTX, LY_STMT_RPC, "rpc"); |
| 436 | TEST_STMS_SUCCESS("status ", YCTX, LY_STMT_STATUS, "status"); |
| 437 | TEST_STMS_SUCCESS("submodule ", YCTX, LY_STMT_SUBMODULE, "submodule"); |
| 438 | TEST_STMS_SUCCESS("type ", YCTX, LY_STMT_TYPE, "type"); |
| 439 | TEST_STMS_SUCCESS("typedef ", YCTX, LY_STMT_TYPEDEF, "typedef"); |
| 440 | TEST_STMS_SUCCESS("unique ", YCTX, LY_STMT_UNIQUE, "unique"); |
| 441 | TEST_STMS_SUCCESS("units ", YCTX, LY_STMT_UNITS, "units"); |
| 442 | TEST_STMS_SUCCESS("uses ", YCTX, LY_STMT_USES, "uses"); |
| 443 | TEST_STMS_SUCCESS("value ", YCTX, LY_STMT_VALUE, "value"); |
| 444 | TEST_STMS_SUCCESS("when ", YCTX, LY_STMT_WHEN, "when"); |
| 445 | TEST_STMS_SUCCESS("yang-version ", YCTX, LY_STMT_YANG_VERSION, "yang-version"); |
| 446 | TEST_STMS_SUCCESS("yin-element ", YCTX, LY_STMT_YIN_ELEMENT, "yin-element"); |
| 447 | TEST_STMS_SUCCESS(";config false;", YCTX, LY_STMT_SYNTAX_SEMICOLON, ";"); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 448 | assert_string_equal("config false;", in.current); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 449 | TEST_STMS_SUCCESS("{ config false;", YCTX, LY_STMT_SYNTAX_LEFT_BRACE, "{"); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 450 | assert_string_equal(" config false;", in.current); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 451 | TEST_STMS_SUCCESS("}", YCTX, LY_STMT_SYNTAX_RIGHT_BRACE, "}"); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 452 | assert_string_equal("", in.current); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 453 | |
| 454 | /* geenric extension */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 455 | in.current = p = "nacm:default-deny-write;"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 456 | assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len)); |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 457 | assert_int_equal(LY_STMT_EXTENSION_INSTANCE, kw); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 458 | assert_int_equal(23, len); |
| 459 | assert_ptr_equal(p, word); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 460 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 461 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 462 | #define TEST_MINMAX_SUCCESS(INPUT_TEXT, CTX, TYPE, VALUE)\ |
| 463 | in.current = INPUT_TEXT;\ |
| 464 | if(TYPE == LYS_SET_MIN){\ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 465 | assert_int_equal(LY_SUCCESS, parse_minelements(CTX, &value, &flags, &ext));\ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 466 | }\ |
| 467 | if(TYPE == LYS_SET_MAX){\ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 468 | assert_int_equal(LY_SUCCESS, parse_maxelements(CTX, &value, &flags, &ext));\ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 469 | }\ |
| 470 | assert_int_equal(TYPE, flags);\ |
| 471 | assert_int_equal(VALUE, value) |
| 472 | |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 473 | static void |
| 474 | test_minmax(void **state) |
| 475 | { |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 476 | uint16_t flags = 0; |
| 477 | uint32_t value = 0; |
| 478 | struct lysp_ext_instance *ext = NULL; |
Radek Krejci | df54913 | 2021-01-21 10:32:32 +0100 | [diff] [blame] | 479 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 480 | YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 481 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 482 | in.current = " 1invalid; ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 483 | assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 484 | CHECK_LOG_CTX("Invalid value \"1invalid\" of \"min-elements\".", "Line number 1."); |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 485 | |
| 486 | flags = value = 0; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 487 | in.current = " -1; ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 488 | assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 489 | CHECK_LOG_CTX("Invalid value \"-1\" of \"min-elements\".", "Line number 1."); |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 490 | |
Radek Krejci | df6cad1 | 2018-11-28 17:10:55 +0100 | [diff] [blame] | 491 | /* implementation limit */ |
| 492 | flags = value = 0; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 493 | in.current = " 4294967296; ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 494 | assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 495 | CHECK_LOG_CTX("Value \"4294967296\" is out of \"min-elements\" bounds.", "Line number 1."); |
Radek Krejci | df6cad1 | 2018-11-28 17:10:55 +0100 | [diff] [blame] | 496 | |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 497 | flags = value = 0; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 498 | TEST_MINMAX_SUCCESS(" 1; ...", YCTX, LYS_SET_MIN, 1); |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 499 | |
| 500 | flags = value = 0; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 501 | TEST_MINMAX_SUCCESS(" 1 {m:ext;} ...", YCTX, LYS_SET_MIN, 1); |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 502 | assert_non_null(ext); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 503 | FREE_ARRAY(YCTX->parsed_mod->mod->ctx, ext, lysp_ext_instance_free); |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 504 | ext = NULL; |
| 505 | |
| 506 | flags = value = 0; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 507 | in.current = " 1 {config true;} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 508 | assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 509 | CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"min-elements\".", "Line number 1."); |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 510 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 511 | in.current = " 1invalid; ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 512 | assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 513 | CHECK_LOG_CTX("Invalid value \"1invalid\" of \"max-elements\".", "Line number 1."); |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 514 | |
| 515 | flags = value = 0; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 516 | in.current = " -1; ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 517 | assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 518 | CHECK_LOG_CTX("Invalid value \"-1\" of \"max-elements\".", "Line number 1."); |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 519 | |
Radek Krejci | df6cad1 | 2018-11-28 17:10:55 +0100 | [diff] [blame] | 520 | /* implementation limit */ |
| 521 | flags = value = 0; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 522 | in.current = " 4294967296; ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 523 | assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 524 | CHECK_LOG_CTX("Value \"4294967296\" is out of \"max-elements\" bounds.", "Line number 1."); |
Radek Krejci | df6cad1 | 2018-11-28 17:10:55 +0100 | [diff] [blame] | 525 | |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 526 | flags = value = 0; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 527 | TEST_MINMAX_SUCCESS(" 1; ...", YCTX, LYS_SET_MAX, 1); |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 528 | |
| 529 | flags = value = 0; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 530 | TEST_MINMAX_SUCCESS(" unbounded; ...", YCTX, LYS_SET_MAX, 0); |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 531 | |
| 532 | flags = value = 0; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 533 | TEST_MINMAX_SUCCESS(" 1 {m:ext;} ...", YCTX, LYS_SET_MAX, 1); |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 534 | assert_non_null(ext); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 535 | FREE_ARRAY(YCTX->parsed_mod->mod->ctx, ext, lysp_ext_instance_free); |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 536 | ext = NULL; |
| 537 | |
| 538 | flags = value = 0; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 539 | in.current = " 1 {config true;} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 540 | assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 541 | CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"max-elements\".", "Line number 1."); |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 542 | } |
| 543 | |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 544 | static struct lysp_module * |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 545 | mod_renew(struct lys_yang_parser_ctx *ctx) |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 546 | { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 547 | struct ly_ctx *ly_ctx = ctx->parsed_mod->mod->ctx; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 548 | |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 549 | lys_module_free(ctx->parsed_mod->mod); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 550 | ctx->parsed_mod = calloc(1, sizeof *ctx->parsed_mod); |
| 551 | ctx->parsed_mod->mod = calloc(1, sizeof *ctx->parsed_mod->mod); |
| 552 | ctx->parsed_mod->mod->parsed = ctx->parsed_mod; |
| 553 | ctx->parsed_mod->mod->ctx = ly_ctx; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 554 | |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 555 | ctx->in->line = 1; |
| 556 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 557 | return ctx->parsed_mod; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | static struct lysp_submodule * |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 561 | submod_renew(struct lys_yang_parser_ctx *ctx) |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 562 | { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 563 | struct ly_ctx *ly_ctx = ctx->parsed_mod->mod->ctx; |
| 564 | |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 565 | lys_module_free(ctx->parsed_mod->mod); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 566 | ctx->parsed_mod = calloc(1, sizeof(struct lysp_submodule)); |
| 567 | ctx->parsed_mod->mod = calloc(1, sizeof *ctx->parsed_mod->mod); |
| 568 | lydict_insert(ly_ctx, "name", 0, &ctx->parsed_mod->mod->name); |
| 569 | ctx->parsed_mod->mod->parsed = ctx->parsed_mod; |
| 570 | ctx->parsed_mod->mod->ctx = ly_ctx; |
| 571 | |
| 572 | return (struct lysp_submodule *)ctx->parsed_mod; |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 573 | } |
| 574 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 575 | static LY_ERR |
| 576 | test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name), |
| 577 | const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format, |
| 578 | const char **module_data, void (**free_module_data)(void *model_data, void *user_data)) |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 579 | { |
| 580 | *module_data = user_data; |
| 581 | *format = LYS_IN_YANG; |
| 582 | *free_module_data = NULL; |
| 583 | return LY_SUCCESS; |
| 584 | } |
| 585 | |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 586 | static void |
| 587 | test_module(void **state) |
| 588 | { |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 589 | struct lysp_module *mod = NULL; |
| 590 | struct lysp_submodule *submod = NULL; |
| 591 | struct lys_module *m; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 592 | struct lys_yang_parser_ctx *ctx_p; |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 593 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 594 | mod = mod_renew(YCTX); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 595 | |
| 596 | /* missing mandatory substatements */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 597 | in.current = " name {}"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 598 | assert_int_equal(LY_EVALID, parse_module(YCTX, mod)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 599 | assert_string_equal("name", mod->mod->name); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 600 | CHECK_LOG_CTX("Missing mandatory keyword \"namespace\" as a child of \"module\".", "Line number 1."); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 601 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 602 | mod = mod_renew(YCTX); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 603 | in.current = " name {namespace urn:x;}"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 604 | assert_int_equal(LY_EVALID, parse_module(YCTX, mod)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 605 | assert_string_equal("urn:x", mod->mod->ns); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 606 | CHECK_LOG_CTX("Missing mandatory keyword \"prefix\" as a child of \"module\".", "Line number 1."); |
| 607 | mod = mod_renew(YCTX); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 608 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 609 | in.current = " name {namespace urn:x;prefix \"x\";}"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 610 | assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 611 | assert_string_equal("x", mod->mod->prefix); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 612 | mod = mod_renew(YCTX); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 613 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 614 | #define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";" |
| 615 | #define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";" |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 616 | #define TEST_NODE(NODETYPE, INPUT, NAME) \ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 617 | in.current = SCHEMA_BEGINNING INPUT; \ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 618 | assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod)); \ |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 619 | assert_non_null(mod->data); \ |
| 620 | assert_int_equal(NODETYPE, mod->data->nodetype); \ |
| 621 | assert_string_equal(NAME, mod->data->name); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 622 | mod = mod_renew(YCTX); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 623 | #define TEST_GENERIC(INPUT, TARGET, TEST) \ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 624 | in.current = SCHEMA_BEGINNING INPUT; \ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 625 | assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod)); \ |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 626 | assert_non_null(TARGET); \ |
| 627 | TEST; \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 628 | mod = mod_renew(YCTX); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 629 | #define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 630 | TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 631 | parse_module, mod, LINE, mod = mod_renew(YCTX)) |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 632 | |
| 633 | /* duplicated namespace, prefix */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 634 | TEST_DUP("namespace", "y", "z", "1"); |
| 635 | TEST_DUP("prefix", "y", "z", "1"); |
| 636 | TEST_DUP("contact", "a", "b", "1"); |
| 637 | TEST_DUP("description", "a", "b", "1"); |
| 638 | TEST_DUP("organization", "a", "b", "1"); |
| 639 | TEST_DUP("reference", "a", "b", "1"); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 640 | |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 641 | /* not allowed in module (submodule-specific) */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 642 | in.current = SCHEMA_BEGINNING "belongs-to master {prefix m;}}"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 643 | assert_int_equal(LY_EVALID, parse_module(YCTX, mod)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 644 | CHECK_LOG_CTX("Invalid keyword \"belongs-to\" as a child of \"module\".", "Line number 1."); |
| 645 | mod = mod_renew(YCTX); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 646 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 647 | /* anydata */ |
| 648 | TEST_NODE(LYS_ANYDATA, "anydata test;}", "test"); |
| 649 | /* anyxml */ |
| 650 | TEST_NODE(LYS_ANYXML, "anyxml test;}", "test"); |
| 651 | /* augment */ |
| 652 | TEST_GENERIC("augment /somepath;}", mod->augments, |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 653 | assert_string_equal("/somepath", mod->augments[0].nodeid)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 654 | /* choice */ |
| 655 | TEST_NODE(LYS_CHOICE, "choice test;}", "test"); |
| 656 | /* contact 0..1 */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 657 | TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact, |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 658 | assert_string_equal("firstname surname", mod->mod->contact)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 659 | /* container */ |
| 660 | TEST_NODE(LYS_CONTAINER, "container test;}", "test"); |
| 661 | /* description 0..1 */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 662 | TEST_GENERIC("description \'some description\';}", mod->mod->dsc, |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 663 | assert_string_equal("some description", mod->mod->dsc)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 664 | /* deviation */ |
| 665 | TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations, |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 666 | assert_string_equal("/somepath", mod->deviations[0].nodeid)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 667 | /* extension */ |
| 668 | TEST_GENERIC("extension test;}", mod->extensions, |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 669 | assert_string_equal("test", mod->extensions[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 670 | /* feature */ |
| 671 | TEST_GENERIC("feature test;}", mod->features, |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 672 | assert_string_equal("test", mod->features[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 673 | /* grouping */ |
| 674 | TEST_GENERIC("grouping grp;}", mod->groupings, |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 675 | assert_string_equal("grp", mod->groupings[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 676 | /* identity */ |
| 677 | TEST_GENERIC("identity test;}", mod->identities, |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 678 | assert_string_equal("test", mod->identities[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 679 | /* import */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 680 | ly_ctx_set_module_imp_clb(YCTX->parsed_mod->mod->ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}"); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 681 | TEST_GENERIC("import zzz {prefix z;}}", mod->imports, |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 682 | assert_string_equal("zzz", mod->imports[0].name)); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 683 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 684 | /* import - prefix collision */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 685 | in.current = SCHEMA_BEGINNING "import zzz {prefix x;}}"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 686 | assert_int_equal(LY_EVALID, parse_module(YCTX, mod)); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 687 | CHECK_LOG_CTX("Prefix \"x\" already used as module prefix.", "Line number 1."); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 688 | mod = mod_renew(YCTX); |
| 689 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 690 | in.current = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 691 | assert_int_equal(LY_EVALID, parse_module(YCTX, mod)); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 692 | CHECK_LOG_CTX("Prefix \"y\" already used to import \"zzz\" module.", "Line number 1."); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 693 | |
| 694 | mod = mod_renew(YCTX); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 695 | LOG_LOCBACK(0, 0, 0, 1); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 696 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 697 | in.current = "module name10 {yang-version 1.1;namespace urn:x;prefix \"x\";import zzz {prefix y;}import zzz {prefix z;}}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 698 | assert_int_equal(lys_parse_mem(YCTX->parsed_mod->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_SUCCESS); |
| 699 | CHECK_LOG_CTX("Single revision of the module \"zzz\" imported twice.", NULL); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 700 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 701 | /* include */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 702 | ly_ctx_set_module_imp_clb(YCTX->parsed_mod->mod->ctx, test_imp_clb, "module xxx { namespace urn:xxx; prefix x;}"); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 703 | in.current = "module" SCHEMA_BEGINNING "include xxx;}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 704 | assert_int_equal(lys_parse_mem(YCTX->parsed_mod->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 8297b79 | 2020-08-16 14:49:05 +0200 | [diff] [blame] | 705 | CHECK_LOG_CTX("Parsing module \"name\" failed.", NULL, "Including \"xxx\" submodule into \"name\" failed.", NULL); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 706 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 707 | ly_ctx_set_module_imp_clb(YCTX->parsed_mod->mod->ctx, test_imp_clb, "submodule xxx {belongs-to wrong-name {prefix w;}}"); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 708 | in.current = "module" SCHEMA_BEGINNING "include xxx;}"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 709 | assert_int_equal(lys_parse_mem(YCTX->parsed_mod->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_EVALID); |
Radek Krejci | 8297b79 | 2020-08-16 14:49:05 +0200 | [diff] [blame] | 710 | CHECK_LOG_CTX("Parsing module \"name\" failed.", NULL, "Including \"xxx\" submodule into \"name\" failed.", NULL); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 711 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 712 | ly_ctx_set_module_imp_clb(YCTX->parsed_mod->mod->ctx, test_imp_clb, "submodule xxx {belongs-to name {prefix x;}}"); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 713 | TEST_GENERIC("include xxx;}", mod->includes, |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 714 | assert_string_equal("xxx", mod->includes[0].name)); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 715 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 716 | /* leaf */ |
| 717 | TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test"); |
| 718 | /* leaf-list */ |
| 719 | TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test"); |
| 720 | /* list */ |
| 721 | TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test"); |
| 722 | /* notification */ |
| 723 | TEST_GENERIC("notification test;}", mod->notifs, |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 724 | assert_string_equal("test", mod->notifs[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 725 | /* organization 0..1 */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 726 | TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org, |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 727 | assert_string_equal("CESNET a.l.e.", mod->mod->org)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 728 | /* reference 0..1 */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 729 | TEST_GENERIC("reference RFC7950;}", mod->mod->ref, |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 730 | assert_string_equal("RFC7950", mod->mod->ref)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 731 | /* revision */ |
| 732 | TEST_GENERIC("revision 2018-10-12;}", mod->revs, |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 733 | assert_string_equal("2018-10-12", mod->revs[0].date)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 734 | /* rpc */ |
| 735 | TEST_GENERIC("rpc test;}", mod->rpcs, |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 736 | assert_string_equal("test", mod->rpcs[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 737 | /* typedef */ |
| 738 | TEST_GENERIC("typedef test{type string;}}", mod->typedefs, |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 739 | assert_string_equal("test", mod->typedefs[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 740 | /* uses */ |
| 741 | TEST_NODE(LYS_USES, "uses test;}", "test"); |
| 742 | /* yang-version */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 743 | in.current = SCHEMA_BEGINNING2 "\n\tyang-version 10;}"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 744 | assert_int_equal(LY_EVALID, parse_module(YCTX, mod)); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 745 | CHECK_LOG_CTX("Invalid value \"10\" of \"yang-version\".", NULL); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 746 | mod = mod_renew(YCTX); |
Radek Krejci | 96e48da | 2020-09-04 13:18:06 +0200 | [diff] [blame] | 747 | in.current = SCHEMA_BEGINNING2 "yang-version 1;yang-version 1.1;}"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 748 | assert_int_equal(LY_EVALID, parse_module(YCTX, mod)); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 749 | CHECK_LOG_CTX("Duplicate keyword \"yang-version\".", NULL); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 750 | mod = mod_renew(YCTX); |
Radek Krejci | 96e48da | 2020-09-04 13:18:06 +0200 | [diff] [blame] | 751 | in.current = SCHEMA_BEGINNING2 "yang-version 1;}"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 752 | assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod)); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 753 | assert_int_equal(1, mod->version); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 754 | mod = mod_renew(YCTX); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 755 | in.current = SCHEMA_BEGINNING2 "yang-version \"1.1\";}"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 756 | assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod)); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 757 | assert_int_equal(2, mod->version); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 758 | mod = mod_renew(YCTX); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 759 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 760 | in.current = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}"; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 761 | m = calloc(1, sizeof *m); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 762 | m->ctx = YCTX->parsed_mod->mod->ctx; |
aPiecek | c3e2614 | 2021-06-22 14:25:49 +0200 | [diff] [blame] | 763 | assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 764 | CHECK_LOG_CTX("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] | 765 | yang_parser_ctx_free(ctx_p); |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 766 | lys_module_free(m); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 767 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 768 | in.current = "prefix " SCHEMA_BEGINNING "}"; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 769 | m = calloc(1, sizeof *m); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 770 | m->ctx = YCTX->parsed_mod->mod->ctx; |
aPiecek | c3e2614 | 2021-06-22 14:25:49 +0200 | [diff] [blame] | 771 | assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 772 | CHECK_LOG_CTX("Invalid keyword \"prefix\", expected \"module\" or \"submodule\".", "Line number 1."); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 773 | yang_parser_ctx_free(ctx_p); |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 774 | lys_module_free(m); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 775 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 776 | in.current = "module " SCHEMA_BEGINNING "leaf enum {type enumeration {enum seven { position 7;}}}}"; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 777 | m = calloc(1, sizeof *m); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 778 | m->ctx = YCTX->parsed_mod->mod->ctx; |
aPiecek | c3e2614 | 2021-06-22 14:25:49 +0200 | [diff] [blame] | 779 | assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 780 | CHECK_LOG_CTX("Invalid keyword \"position\" as a child of \"enum\".", "Line number 1."); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 781 | yang_parser_ctx_free(ctx_p); |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 782 | lys_module_free(m); |
David Sedlák | 9fb515f | 2019-07-11 10:33:58 +0200 | [diff] [blame] | 783 | |
Radek Krejci | 156ccaf | 2018-10-15 15:49:17 +0200 | [diff] [blame] | 784 | /* extensions */ |
| 785 | TEST_GENERIC("prefix:test;}", mod->exts, |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 786 | assert_string_equal("prefix:test", mod->exts[0].name); |
Radek Krejci | ab43086 | 2021-03-02 20:13:40 +0100 | [diff] [blame] | 787 | assert_int_equal(LY_STMT_MODULE, mod->exts[0].parent_stmt)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 788 | mod = mod_renew(YCTX); |
Radek Krejci | 156ccaf | 2018-10-15 15:49:17 +0200 | [diff] [blame] | 789 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 790 | /* invalid substatement */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 791 | in.current = SCHEMA_BEGINNING "must false;}"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 792 | assert_int_equal(LY_EVALID, parse_module(YCTX, mod)); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 793 | CHECK_LOG_CTX("Invalid keyword \"must\" as a child of \"module\".", NULL); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 794 | |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 795 | /* submodule */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 796 | submod = submod_renew(YCTX); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 797 | |
| 798 | /* missing mandatory substatements */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 799 | in.current = " subname {}"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 800 | assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod)); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 801 | CHECK_LOG_CTX("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\".", NULL); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 802 | assert_string_equal("subname", submod->name); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 803 | |
| 804 | submod = submod_renew(YCTX); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 805 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 806 | in.current = " subname {belongs-to name {prefix x;}}"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 807 | assert_int_equal(LY_SUCCESS, parse_submodule(YCTX, submod)); |
Michal Vasko | c3781c3 | 2020-10-06 14:04:08 +0200 | [diff] [blame] | 808 | assert_string_equal("name", submod->mod->name); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 809 | submod = submod_renew(YCTX); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 810 | |
| 811 | #undef SCHEMA_BEGINNING |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 812 | #define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}" |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 813 | |
| 814 | /* duplicated namespace, prefix */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 815 | in.current = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 816 | assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod)); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 817 | CHECK_LOG_CTX("Duplicate keyword \"belongs-to\".", NULL); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 818 | submod = submod_renew(YCTX); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 819 | |
| 820 | /* not allowed in submodule (module-specific) */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 821 | in.current = SCHEMA_BEGINNING "namespace \"urn:z\";}"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 822 | assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod)); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 823 | CHECK_LOG_CTX("Invalid keyword \"namespace\" as a child of \"submodule\".", NULL); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 824 | submod = submod_renew(YCTX); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 825 | in.current = SCHEMA_BEGINNING "prefix m;}}"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 826 | assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod)); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 827 | CHECK_LOG_CTX("Invalid keyword \"prefix\" as a child of \"submodule\".", NULL); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 828 | submod = submod_renew(YCTX); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 829 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 830 | in.current = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 831 | assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, YCTX->parsed_mod->mod->ctx, (struct lys_parser_ctx *)YCTX, YCTX->in, &submod)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 832 | CHECK_LOG_CTX("Trailing garbage \"module q {names...\" after submodule, expected end-of-input.", "Line number 1."); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 833 | yang_parser_ctx_free(ctx_p); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 834 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 835 | in.current = "prefix " SCHEMA_BEGINNING "}"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 836 | assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, YCTX->parsed_mod->mod->ctx, (struct lys_parser_ctx *)YCTX, YCTX->in, &submod)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 837 | CHECK_LOG_CTX("Invalid keyword \"prefix\", expected \"module\" or \"submodule\".", "Line number 1."); |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 838 | yang_parser_ctx_free(ctx_p); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 839 | submod = submod_renew(YCTX); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 840 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 841 | #undef TEST_GENERIC |
| 842 | #undef TEST_NODE |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 843 | #undef TEST_DUP |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 844 | #undef SCHEMA_BEGINNING |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 845 | } |
| 846 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 847 | static void |
| 848 | test_deviation(void **state) |
| 849 | { |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 850 | struct lysp_deviation *d = NULL; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 851 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 852 | /* invalid cardinality */ |
| 853 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 854 | TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 855 | &d, "1", FREE_ARRAY(YCTX->parsed_mod->mod->ctx, d, lysp_deviation_free); d = NULL) |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 856 | |
| 857 | TEST_DUP("description", "a", "b"); |
| 858 | TEST_DUP("reference", "a", "b"); |
| 859 | |
| 860 | /* full content */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 861 | in.current = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 862 | assert_int_equal(LY_SUCCESS, parse_deviation(YCTX, &d)); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 863 | assert_non_null(d); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 864 | assert_string_equal(" ...", in.current); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 865 | FREE_ARRAY(YCTX->parsed_mod->mod->ctx, d, lysp_deviation_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 866 | d = NULL; |
| 867 | |
| 868 | /* missing mandatory substatement */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 869 | in.current = " test {description text;}"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 870 | assert_int_equal(LY_EVALID, parse_deviation(YCTX, &d)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 871 | CHECK_LOG_CTX("Missing mandatory keyword \"deviate\" as a child of \"deviation\".", "Line number 1."); |
| 872 | FREE_ARRAY(YCTX->parsed_mod->mod->ctx, d, lysp_deviation_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 873 | d = NULL; |
| 874 | |
| 875 | /* invalid substatement */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 876 | in.current = " test {deviate not-supported; status obsolete;}"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 877 | assert_int_equal(LY_EVALID, parse_deviation(YCTX, &d)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 878 | CHECK_LOG_CTX("Invalid keyword \"status\" as a child of \"deviation\".", "Line number 1."); |
| 879 | FREE_ARRAY(YCTX->parsed_mod->mod->ctx, d, lysp_deviation_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 880 | d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 881 | #undef TEST_DUP |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 882 | } |
| 883 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 884 | #define TEST_DEVIATE_SUCCESS(INPUT_TEXT, REMAIN_TEXT)\ |
| 885 | in.current = INPUT_TEXT;\ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 886 | assert_int_equal(LY_SUCCESS, parse_deviate(YCTX, &d));\ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 887 | assert_non_null(d);\ |
| 888 | assert_string_equal(REMAIN_TEXT, in.current);\ |
| 889 | lysp_deviate_free(YCTX->parsed_mod->mod->ctx, d); free(d); d = NULL |
| 890 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 891 | static void |
| 892 | test_deviate(void **state) |
| 893 | { |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 894 | struct lysp_deviate *d = NULL; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 895 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 896 | /* invalid cardinality */ |
| 897 | #define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \ |
| 898 | TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 899 | &d, "1", lysp_deviate_free(YCTX->parsed_mod->mod->ctx, d); free(d); d = NULL) |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 900 | |
| 901 | TEST_DUP("add", "config", "true", "false"); |
Radek Iša | 0776795 | 2021-01-31 20:54:24 +0100 | [diff] [blame] | 902 | #if 0 |
| 903 | /*test on int8 now is in file tests/utest/types/int8.c */ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 904 | TEST_DUP("replace", "default", "int8", "uint8"); |
Radek Iša | 0776795 | 2021-01-31 20:54:24 +0100 | [diff] [blame] | 905 | #endif |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 906 | TEST_DUP("add", "mandatory", "true", "false"); |
| 907 | TEST_DUP("add", "max-elements", "1", "2"); |
| 908 | TEST_DUP("add", "min-elements", "1", "2"); |
Radek Iša | 0776795 | 2021-01-31 20:54:24 +0100 | [diff] [blame] | 909 | #if 0 |
| 910 | /*test on int8 now is in file tests/utest/types/int8.c*/ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 911 | TEST_DUP("replace", "type", "int8", "uint8"); |
Radek Iša | 0776795 | 2021-01-31 20:54:24 +0100 | [diff] [blame] | 912 | #endif |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 913 | TEST_DUP("add", "units", "kilometers", "miles"); |
| 914 | |
| 915 | /* full contents */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 916 | TEST_DEVIATE_SUCCESS(" not-supported {prefix:ext;} ...", " ..."); |
| 917 | TEST_DEVIATE_SUCCESS(" 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;} ...", " ..."); |
| 918 | TEST_DEVIATE_SUCCESS(" delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...", " ..."); |
| 919 | TEST_DEVIATE_SUCCESS(" replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...", " ..."); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 920 | |
| 921 | /* invalid substatements */ |
| 922 | #define TEST_NOT_SUP(DEV, STMT, VALUE) \ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 923 | in.current = " "DEV" {"STMT" "VALUE";}..."; \ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 924 | assert_int_equal(LY_EVALID, parse_deviate(YCTX, &d)); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 925 | CHECK_LOG_CTX("Deviate \""DEV"\" does not support keyword \""STMT"\".", "Line number 1.");\ |
| 926 | lysp_deviate_free(YCTX->parsed_mod->mod->ctx, d); free(d); d = NULL |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 927 | |
| 928 | TEST_NOT_SUP("not-supported", "units", "meters"); |
| 929 | TEST_NOT_SUP("not-supported", "must", "1"); |
| 930 | TEST_NOT_SUP("not-supported", "unique", "x"); |
| 931 | TEST_NOT_SUP("not-supported", "default", "a"); |
| 932 | TEST_NOT_SUP("not-supported", "config", "true"); |
| 933 | TEST_NOT_SUP("not-supported", "mandatory", "true"); |
| 934 | TEST_NOT_SUP("not-supported", "min-elements", "1"); |
| 935 | TEST_NOT_SUP("not-supported", "max-elements", "2"); |
| 936 | TEST_NOT_SUP("not-supported", "type", "string"); |
| 937 | TEST_NOT_SUP("add", "type", "string"); |
| 938 | TEST_NOT_SUP("delete", "config", "true"); |
| 939 | TEST_NOT_SUP("delete", "mandatory", "true"); |
| 940 | TEST_NOT_SUP("delete", "min-elements", "1"); |
| 941 | TEST_NOT_SUP("delete", "max-elements", "2"); |
| 942 | TEST_NOT_SUP("delete", "type", "string"); |
| 943 | TEST_NOT_SUP("replace", "must", "1"); |
| 944 | TEST_NOT_SUP("replace", "unique", "a"); |
| 945 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 946 | in.current = " nonsence; ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 947 | assert_int_equal(LY_EVALID, parse_deviate(YCTX, &d)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 948 | CHECK_LOG_CTX("Invalid value \"nonsence\" of \"deviate\".", "Line number 1.");\ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 949 | assert_null(d); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 950 | #undef TEST_NOT_SUP |
| 951 | #undef TEST_DUP |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 952 | } |
| 953 | |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 954 | static void |
| 955 | test_container(void **state) |
| 956 | { |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 957 | struct lysp_node_container *c = NULL; |
Radek Krejci | df54913 | 2021-01-21 10:32:32 +0100 | [diff] [blame] | 958 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 959 | YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */ |
aPiecek | 8d4e75d | 2021-06-24 14:47:06 +0200 | [diff] [blame] | 960 | YCTX->main_ctx = (struct lys_parser_ctx *)YCTX; |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 961 | |
| 962 | /* invalid cardinality */ |
| 963 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 964 | in.current = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 965 | assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node**)&c)); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 966 | CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \ |
| 967 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)c); c = NULL; |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 968 | |
| 969 | TEST_DUP("config", "true", "false"); |
| 970 | TEST_DUP("description", "text1", "text2"); |
| 971 | TEST_DUP("presence", "true", "false"); |
| 972 | TEST_DUP("reference", "1", "2"); |
| 973 | TEST_DUP("status", "current", "obsolete"); |
| 974 | TEST_DUP("when", "true", "false"); |
| 975 | #undef TEST_DUP |
| 976 | |
| 977 | /* full content */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 978 | in.current = "cont {action x;anydata any;anyxml anyxml; choice ch;config false;container c;description test;grouping g;if-feature f; leaf l {type string;}" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 979 | "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 | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 980 | assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 981 | CHECK_LYSP_NODE(c, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR, 1, "cont", 0, LYS_CONTAINER, 0, "test", 1); |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 982 | assert_non_null(c->actions); |
| 983 | assert_non_null(c->child); |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 984 | assert_non_null(c->groupings); |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 985 | assert_non_null(c->musts); |
| 986 | assert_non_null(c->notifs); |
| 987 | assert_string_equal("true", c->presence); |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 988 | assert_non_null(c->typedefs); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 989 | ly_set_erase(&YCTX->tpdfs_nodes, NULL); |
aPiecek | 63e080d | 2021-06-29 13:53:28 +0200 | [diff] [blame] | 990 | ly_set_erase(&YCTX->grps_nodes, NULL); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 991 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c); c = NULL; |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 992 | |
| 993 | /* invalid */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 994 | in.current = " cont {augment /root;} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 995 | assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 996 | CHECK_LOG_CTX("Invalid keyword \"augment\" as a child of \"container\".", "Line number 1."); |
| 997 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c); c = NULL; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 998 | in.current = " cont {nonsence true;} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 999 | assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1000 | CHECK_LOG_CTX("Invalid character sequence \"nonsence\", expected a keyword.", "Line number 1."); |
| 1001 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c); c = NULL; |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1002 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1003 | YCTX->parsed_mod->version = 1; /* simulate YANG 1.0 */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1004 | in.current = " cont {action x;} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1005 | assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1006 | CHECK_LOG_CTX("Invalid keyword \"action\" as a child of \"container\" - " |
| 1007 | "the statement is allowed only in YANG 1.1 modules.", "Line number 1."); |
| 1008 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c); c = NULL; |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1009 | } |
| 1010 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1011 | static void |
| 1012 | test_leaf(void **state) |
| 1013 | { |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1014 | struct lysp_node_leaf *l = NULL; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1015 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1016 | /* invalid cardinality */ |
| 1017 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1018 | in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1019 | assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node**)&l)); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1020 | CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \ |
| 1021 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)l); l = NULL; |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1022 | |
| 1023 | TEST_DUP("config", "true", "false"); |
| 1024 | TEST_DUP("default", "x", "y"); |
| 1025 | TEST_DUP("description", "text1", "text2"); |
| 1026 | TEST_DUP("mandatory", "true", "false"); |
| 1027 | TEST_DUP("reference", "1", "2"); |
| 1028 | TEST_DUP("status", "current", "obsolete"); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1029 | TEST_DUP("type", "int8", "uint8"); |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1030 | TEST_DUP("units", "text1", "text2"); |
| 1031 | TEST_DUP("when", "true", "false"); |
| 1032 | #undef TEST_DUP |
| 1033 | |
| 1034 | /* full content - without mandatory which is mutual exclusive with default */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1035 | in.current = "l {config false;default \"xxx\";description test;if-feature f;" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 1036 | "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1037 | assert_int_equal(LY_SUCCESS, parse_leaf(YCTX, NULL, (struct lysp_node **)&l)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1038 | CHECK_LYSP_NODE(l, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR, 1, "l", 0, LYS_LEAF, 0, "test", 1); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1039 | assert_string_equal("xxx", l->dflt.str); |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1040 | assert_string_equal("yyy", l->units); |
| 1041 | assert_string_equal("string", l->type.name); |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1042 | assert_non_null(l->musts); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1043 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL; |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1044 | |
| 1045 | /* full content - now with mandatory */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1046 | in.current = "l {mandatory true; type string;} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1047 | assert_int_equal(LY_SUCCESS, parse_leaf(YCTX, NULL, (struct lysp_node **)&l)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1048 | CHECK_LYSP_NODE(l, NULL, 0, LYS_MAND_TRUE, 0, "l", 0, LYS_LEAF, 0, NULL, 0); |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1049 | assert_string_equal("string", l->type.name); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1050 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL; |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1051 | |
| 1052 | /* invalid */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1053 | in.current = " l {description \"missing type\";} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1054 | assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node **)&l)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1055 | CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"leaf\".", "Line number 1."); |
| 1056 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL; |
aPiecek | 4bb1e37 | 2021-05-07 11:01:00 +0200 | [diff] [blame] | 1057 | |
| 1058 | in.current = "l { type iid { path qpud wrong {"; |
| 1059 | assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node **)&l)); |
| 1060 | CHECK_LOG_CTX("Invalid character sequence \"wrong\", expected a keyword.", "Line number 1."); |
| 1061 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL; |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1062 | } |
| 1063 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1064 | static void |
| 1065 | test_leaflist(void **state) |
| 1066 | { |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1067 | struct lysp_node_leaflist *ll = NULL; |
Radek Krejci | df54913 | 2021-01-21 10:32:32 +0100 | [diff] [blame] | 1068 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1069 | YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1070 | |
| 1071 | /* invalid cardinality */ |
| 1072 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1073 | in.current = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1074 | assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node**)&ll)); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1075 | CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \ |
| 1076 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)ll); ll = NULL; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1077 | |
| 1078 | TEST_DUP("config", "true", "false"); |
| 1079 | TEST_DUP("description", "text1", "text2"); |
| 1080 | TEST_DUP("max-elements", "10", "20"); |
| 1081 | TEST_DUP("min-elements", "10", "20"); |
| 1082 | TEST_DUP("ordered-by", "user", "system"); |
| 1083 | TEST_DUP("reference", "1", "2"); |
| 1084 | TEST_DUP("status", "current", "obsolete"); |
| 1085 | TEST_DUP("type", "int8", "uint8"); |
| 1086 | TEST_DUP("units", "text1", "text2"); |
| 1087 | TEST_DUP("when", "true", "false"); |
| 1088 | #undef TEST_DUP |
| 1089 | |
| 1090 | /* full content - without min-elements which is mutual exclusive with default */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1091 | in.current = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 1092 | "max-elements 10;must 'expr';ordered-by user;reference test;" |
| 1093 | "status current;type string; units zzz;when true;m:ext;} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1094 | assert_int_equal(LY_SUCCESS, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1095 | CHECK_LYSP_NODE(ll, "test", 1, 0x446, 1, "ll", 0, LYS_LEAFLIST, 0, "test", 1); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1096 | assert_non_null(ll->dflts); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1097 | assert_int_equal(2, LY_ARRAY_COUNT(ll->dflts)); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1098 | assert_string_equal("xxx", ll->dflts[0].str); |
| 1099 | assert_string_equal("yyy", ll->dflts[1].str); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1100 | assert_string_equal("zzz", ll->units); |
| 1101 | assert_int_equal(10, ll->max); |
| 1102 | assert_int_equal(0, ll->min); |
| 1103 | assert_string_equal("string", ll->type.name); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1104 | assert_non_null(ll->musts); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1105 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1106 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ll); ll = NULL; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1107 | |
| 1108 | /* full content - now with min-elements */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1109 | in.current = "ll {min-elements 10; type string;} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1110 | assert_int_equal(LY_SUCCESS, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1111 | CHECK_LYSP_NODE(ll, NULL, 0, 0x200, 0, "ll", 0, LYS_LEAFLIST, 0, NULL, 0); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1112 | assert_string_equal("string", ll->type.name); |
| 1113 | assert_int_equal(0, ll->max); |
| 1114 | assert_int_equal(10, ll->min); |
| 1115 | assert_int_equal(LYS_SET_MIN, ll->flags); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1116 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ll); ll = NULL; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1117 | |
| 1118 | /* invalid */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1119 | in.current = " ll {description \"missing type\";} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1120 | assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1121 | CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"leaf-list\".", "Line number 1."); |
| 1122 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ll); ll = NULL; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1123 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1124 | YCTX->parsed_mod->version = 1; /* simulate YANG 1.0 - default statement is not allowed */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1125 | in.current = " ll {default xx; type string;} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1126 | assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1127 | CHECK_LOG_CTX("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules.", "Line number 1."); |
| 1128 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ll); ll = NULL; |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1129 | } |
| 1130 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1131 | static void |
| 1132 | test_list(void **state) |
| 1133 | { |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1134 | struct lysp_node_list *l = NULL; |
Radek Krejci | df54913 | 2021-01-21 10:32:32 +0100 | [diff] [blame] | 1135 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1136 | YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */ |
aPiecek | 8d4e75d | 2021-06-24 14:47:06 +0200 | [diff] [blame] | 1137 | YCTX->main_ctx = (struct lys_parser_ctx *)YCTX; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1138 | |
| 1139 | /* invalid cardinality */ |
| 1140 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1141 | in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1142 | assert_int_equal(LY_EVALID, parse_list(YCTX, NULL, (struct lysp_node**)&l)); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1143 | CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \ |
| 1144 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)l); l = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1145 | |
| 1146 | TEST_DUP("config", "true", "false"); |
| 1147 | TEST_DUP("description", "text1", "text2"); |
| 1148 | TEST_DUP("key", "one", "two"); |
| 1149 | TEST_DUP("max-elements", "10", "20"); |
| 1150 | TEST_DUP("min-elements", "10", "20"); |
| 1151 | TEST_DUP("ordered-by", "user", "system"); |
| 1152 | TEST_DUP("reference", "1", "2"); |
| 1153 | TEST_DUP("status", "current", "obsolete"); |
| 1154 | TEST_DUP("when", "true", "false"); |
| 1155 | #undef TEST_DUP |
| 1156 | |
| 1157 | /* full content */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1158 | in.current = "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;}" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 1159 | "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;" |
| 1160 | "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1161 | assert_int_equal(LY_SUCCESS, parse_list(YCTX, NULL, (struct lysp_node **)&l)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1162 | CHECK_LYSP_NODE(l, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, 1, "l", |
| 1163 | 0, LYS_LIST, 0, "test", 1); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1164 | assert_string_equal("l", l->key); |
| 1165 | assert_non_null(l->uniques); |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1166 | assert_int_equal(2, LY_ARRAY_COUNT(l->uniques)); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1167 | assert_string_equal("xxx", l->uniques[0].str); |
| 1168 | assert_string_equal("yyy", l->uniques[1].str); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1169 | assert_int_equal(10, l->max); |
| 1170 | assert_int_equal(1, l->min); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1171 | assert_non_null(l->musts); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1172 | ly_set_erase(&YCTX->tpdfs_nodes, NULL); |
aPiecek | 63e080d | 2021-06-29 13:53:28 +0200 | [diff] [blame] | 1173 | ly_set_erase(&YCTX->grps_nodes, NULL); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1174 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1175 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1176 | /* invalid content */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1177 | YCTX->parsed_mod->version = 1; /* simulate YANG 1.0 */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1178 | in.current = "l {action x;} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1179 | assert_int_equal(LY_EVALID, parse_list(YCTX, NULL, (struct lysp_node **)&l)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1180 | CHECK_LOG_CTX("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules.", "Line number 1."); |
| 1181 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL; |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1182 | } |
| 1183 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1184 | static void |
| 1185 | test_choice(void **state) |
| 1186 | { |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1187 | struct lysp_node_choice *ch = NULL; |
Radek Krejci | df54913 | 2021-01-21 10:32:32 +0100 | [diff] [blame] | 1188 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1189 | YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1190 | |
| 1191 | /* invalid cardinality */ |
| 1192 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1193 | in.current = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1194 | assert_int_equal(LY_EVALID, parse_choice(YCTX, NULL, (struct lysp_node**)&ch)); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1195 | CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \ |
| 1196 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)ch); ch = NULL; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1197 | |
| 1198 | TEST_DUP("config", "true", "false"); |
| 1199 | TEST_DUP("default", "a", "b"); |
| 1200 | TEST_DUP("description", "text1", "text2"); |
| 1201 | TEST_DUP("mandatory", "true", "false"); |
| 1202 | TEST_DUP("reference", "1", "2"); |
| 1203 | TEST_DUP("status", "current", "obsolete"); |
| 1204 | TEST_DUP("when", "true", "false"); |
| 1205 | #undef TEST_DUP |
| 1206 | |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 1207 | /* full content - without default due to a collision with mandatory */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1208 | in.current = "ch {anydata any;anyxml anyxml; case c;choice ch;config false;container c;description test;if-feature f;leaf l {type string;}" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 1209 | "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1210 | assert_int_equal(LY_SUCCESS, parse_choice(YCTX, NULL, (struct lysp_node **)&ch)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1211 | CHECK_LYSP_NODE(ch, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, 1, "ch", 0, LYS_CHOICE, 0, "test", 1); |
| 1212 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ch); ch = NULL; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1213 | |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 1214 | /* full content - the default missing from the previous node */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1215 | in.current = "ch {default c;case c;} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1216 | assert_int_equal(LY_SUCCESS, parse_choice(YCTX, NULL, (struct lysp_node **)&ch)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1217 | CHECK_LYSP_NODE(ch, NULL, 0, 0, 0, "ch", 0, LYS_CHOICE, 0, NULL, 0); |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 1218 | assert_string_equal("c", ch->dflt.str); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1219 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ch); ch = NULL; |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | static void |
| 1223 | test_case(void **state) |
| 1224 | { |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 1225 | struct lysp_node_case *cs = NULL; |
Radek Krejci | df54913 | 2021-01-21 10:32:32 +0100 | [diff] [blame] | 1226 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1227 | YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 1228 | |
| 1229 | /* invalid cardinality */ |
| 1230 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1231 | in.current = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1232 | assert_int_equal(LY_EVALID, parse_case(YCTX, NULL, (struct lysp_node**)&cs)); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1233 | CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \ |
| 1234 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)cs); cs = NULL; |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 1235 | |
| 1236 | TEST_DUP("description", "text1", "text2"); |
| 1237 | TEST_DUP("reference", "1", "2"); |
| 1238 | TEST_DUP("status", "current", "obsolete"); |
| 1239 | TEST_DUP("when", "true", "false"); |
| 1240 | #undef TEST_DUP |
| 1241 | |
| 1242 | /* full content */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1243 | in.current = "cs {anydata any;anyxml anyxml; choice ch;container c;description test;if-feature f;leaf l {type string;}" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 1244 | "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1245 | assert_int_equal(LY_SUCCESS, parse_case(YCTX, NULL, (struct lysp_node **)&cs)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1246 | CHECK_LYSP_NODE(cs, "test", 1, LYS_STATUS_CURR, 1, "cs", 0, LYS_CASE, 0, "test", 1); |
| 1247 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)cs); cs = NULL; |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 1248 | |
| 1249 | /* invalid content */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1250 | in.current = "cs {config true} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1251 | assert_int_equal(LY_EVALID, parse_case(YCTX, NULL, (struct lysp_node **)&cs)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1252 | CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"case\".", "Line number 1."); |
| 1253 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)cs); cs = NULL; |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1254 | } |
| 1255 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1256 | static void |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1257 | test_any(void **state, enum ly_stmt kw) |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1258 | { |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1259 | struct lysp_node_anydata *any = NULL; |
Radek Krejci | df54913 | 2021-01-21 10:32:32 +0100 | [diff] [blame] | 1260 | |
Radek Krejci | d6b7645 | 2019-09-03 17:03:03 +0200 | [diff] [blame] | 1261 | if (kw == LY_STMT_ANYDATA) { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1262 | YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1263 | } else { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1264 | YCTX->parsed_mod->version = 1; /* simulate YANG 1.0 */ |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | /* invalid cardinality */ |
| 1268 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1269 | in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1270 | assert_int_equal(LY_EVALID, parse_any(YCTX, kw, NULL, (struct lysp_node**)&any)); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1271 | CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \ |
| 1272 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)any); any = NULL; |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1273 | |
| 1274 | TEST_DUP("config", "true", "false"); |
| 1275 | TEST_DUP("description", "text1", "text2"); |
| 1276 | TEST_DUP("mandatory", "true", "false"); |
| 1277 | TEST_DUP("reference", "1", "2"); |
| 1278 | TEST_DUP("status", "current", "obsolete"); |
| 1279 | TEST_DUP("when", "true", "false"); |
| 1280 | #undef TEST_DUP |
| 1281 | |
| 1282 | /* full content */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1283 | in.current = "any {config true;description test;if-feature f;mandatory true;must 'expr';reference test;status current;when true;m:ext;} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1284 | assert_int_equal(LY_SUCCESS, parse_any(YCTX, kw, NULL, (struct lysp_node **)&any)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1285 | // CHECK_LYSP_NODE(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, NEXT, TYPE, PARENT, REF, WHEN) |
| 1286 | uint16_t node_type = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML; |
| 1287 | CHECK_LYSP_NODE(any, "test", 1, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, 1, "any", 0, node_type, 0, "test", 1); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1288 | assert_non_null(any->musts); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1289 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)any); any = NULL; |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | static void |
| 1293 | test_anydata(void **state) |
| 1294 | { |
Michal Vasko | a0a498b | 2021-09-22 12:17:48 +0200 | [diff] [blame] | 1295 | test_any(state, LY_STMT_ANYDATA); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | static void |
| 1299 | test_anyxml(void **state) |
| 1300 | { |
Michal Vasko | a0a498b | 2021-09-22 12:17:48 +0200 | [diff] [blame] | 1301 | test_any(state, LY_STMT_ANYXML); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1302 | } |
| 1303 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1304 | static void |
| 1305 | test_grouping(void **state) |
| 1306 | { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1307 | struct lysp_node_grp *grp = NULL; |
Radek Krejci | df54913 | 2021-01-21 10:32:32 +0100 | [diff] [blame] | 1308 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1309 | YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */ |
aPiecek | 8d4e75d | 2021-06-24 14:47:06 +0200 | [diff] [blame] | 1310 | YCTX->main_ctx = (struct lys_parser_ctx *)YCTX; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1311 | |
| 1312 | /* invalid cardinality */ |
| 1313 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1314 | in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1315 | assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp)); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1316 | CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \ |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1317 | lysp_node_free(YCTX->parsed_mod->mod->ctx, &grp->node); grp = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1318 | |
| 1319 | TEST_DUP("description", "text1", "text2"); |
| 1320 | TEST_DUP("reference", "1", "2"); |
| 1321 | TEST_DUP("status", "current", "obsolete"); |
| 1322 | #undef TEST_DUP |
| 1323 | |
| 1324 | /* full content */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1325 | in.current = "grp {action x;anydata any;anyxml anyxml; choice ch;container c;description test;grouping g;leaf l {type string;}" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 1326 | "leaf-list ll {type string;} list li;notification not;reference test;status current;typedef t {type int8;}uses g;m:ext;} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1327 | assert_int_equal(LY_SUCCESS, parse_grouping(YCTX, NULL, &grp)); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1328 | assert_non_null(grp); |
| 1329 | assert_int_equal(LYS_GROUPING, grp->nodetype); |
| 1330 | assert_string_equal("grp", grp->name); |
| 1331 | assert_string_equal("test", grp->dsc); |
| 1332 | assert_non_null(grp->exts); |
| 1333 | assert_string_equal("test", grp->ref); |
| 1334 | assert_null(grp->parent); |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 1335 | assert_int_equal(LYS_STATUS_CURR, grp->flags); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1336 | ly_set_erase(&YCTX->tpdfs_nodes, NULL); |
aPiecek | 63e080d | 2021-06-29 13:53:28 +0200 | [diff] [blame] | 1337 | ly_set_erase(&YCTX->grps_nodes, NULL); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1338 | lysp_node_free(YCTX->parsed_mod->mod->ctx, &grp->node); |
| 1339 | grp = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1340 | |
| 1341 | /* invalid content */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1342 | in.current = "grp {config true} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1343 | assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1344 | CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"grouping\".", "Line number 1."); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1345 | lysp_node_free(YCTX->parsed_mod->mod->ctx, &grp->node); |
| 1346 | grp = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1347 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1348 | in.current = "grp {must 'expr'} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1349 | assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1350 | CHECK_LOG_CTX("Invalid keyword \"must\" as a child of \"grouping\".", "Line number 1."); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1351 | lysp_node_free(YCTX->parsed_mod->mod->ctx, &grp->node); |
| 1352 | grp = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | static void |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1356 | test_action(void **state) |
| 1357 | { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1358 | struct lysp_node_action *rpcs = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1359 | struct lysp_node_container *c = NULL; |
Radek Krejci | df54913 | 2021-01-21 10:32:32 +0100 | [diff] [blame] | 1360 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1361 | YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */ |
aPiecek | 8d4e75d | 2021-06-24 14:47:06 +0200 | [diff] [blame] | 1362 | YCTX->main_ctx = (struct lys_parser_ctx *)YCTX; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1363 | |
| 1364 | /* invalid cardinality */ |
| 1365 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1366 | in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1367 | assert_int_equal(LY_EVALID, parse_action(YCTX, NULL, &rpcs)); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1368 | CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \ |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1369 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)rpcs); rpcs = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1370 | |
| 1371 | TEST_DUP("description", "text1", "text2"); |
Michal Vasko | b83af8a | 2020-01-06 09:49:22 +0100 | [diff] [blame] | 1372 | TEST_DUP("input", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a"); |
| 1373 | 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] | 1374 | TEST_DUP("reference", "1", "2"); |
| 1375 | TEST_DUP("status", "current", "obsolete"); |
| 1376 | #undef TEST_DUP |
| 1377 | |
| 1378 | /* full content */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1379 | in.current = "top;"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1380 | assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1381 | in.current = "func {description test;grouping grp;if-feature f;reference test;status current;typedef mytype {type int8;} m:ext;" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 1382 | "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}" |
| 1383 | " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}" |
| 1384 | "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}" |
| 1385 | " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1386 | assert_int_equal(LY_SUCCESS, parse_action(YCTX, (struct lysp_node *)c, &rpcs)); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1387 | assert_non_null(rpcs); |
| 1388 | assert_int_equal(LYS_ACTION, rpcs->nodetype); |
| 1389 | assert_string_equal("func", rpcs->name); |
| 1390 | assert_string_equal("test", rpcs->dsc); |
| 1391 | assert_non_null(rpcs->exts); |
| 1392 | assert_non_null(rpcs->iffeatures); |
| 1393 | assert_string_equal("test", rpcs->ref); |
| 1394 | assert_non_null(rpcs->groupings); |
| 1395 | assert_non_null(rpcs->typedefs); |
| 1396 | assert_int_equal(LYS_STATUS_CURR, rpcs->flags); |
| 1397 | /* input */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1398 | assert_int_equal(rpcs->input.nodetype, LYS_INPUT); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1399 | assert_non_null(rpcs->input.groupings); |
| 1400 | assert_non_null(rpcs->input.exts); |
| 1401 | assert_non_null(rpcs->input.musts); |
| 1402 | assert_non_null(rpcs->input.typedefs); |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 1403 | assert_non_null(rpcs->input.child); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1404 | /* output */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1405 | assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1406 | assert_non_null(rpcs->output.groupings); |
| 1407 | assert_non_null(rpcs->output.exts); |
| 1408 | assert_non_null(rpcs->output.musts); |
| 1409 | assert_non_null(rpcs->output.typedefs); |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 1410 | assert_non_null(rpcs->output.child); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1411 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1412 | ly_set_erase(&YCTX->tpdfs_nodes, NULL); |
aPiecek | 63e080d | 2021-06-29 13:53:28 +0200 | [diff] [blame] | 1413 | ly_set_erase(&YCTX->grps_nodes, NULL); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1414 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)rpcs); rpcs = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1415 | |
| 1416 | /* invalid content */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1417 | in.current = "func {config true} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1418 | assert_int_equal(LY_EVALID, parse_action(YCTX, NULL, &rpcs)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1419 | CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"rpc\".", "Line number 1."); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1420 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)rpcs); rpcs = NULL; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1421 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1422 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | static void |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 1426 | test_notification(void **state) |
| 1427 | { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1428 | struct lysp_node_notif *notifs = NULL; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 1429 | struct lysp_node_container *c = NULL; |
Radek Krejci | df54913 | 2021-01-21 10:32:32 +0100 | [diff] [blame] | 1430 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1431 | YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */ |
aPiecek | 8d4e75d | 2021-06-24 14:47:06 +0200 | [diff] [blame] | 1432 | YCTX->main_ctx = (struct lys_parser_ctx *)YCTX; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 1433 | |
| 1434 | /* invalid cardinality */ |
| 1435 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1436 | in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1437 | assert_int_equal(LY_EVALID, parse_notif(YCTX, NULL, ¬ifs)); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1438 | CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \ |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1439 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)notifs); notifs = NULL; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 1440 | |
| 1441 | TEST_DUP("description", "text1", "text2"); |
| 1442 | TEST_DUP("reference", "1", "2"); |
| 1443 | TEST_DUP("status", "current", "obsolete"); |
| 1444 | #undef TEST_DUP |
| 1445 | |
| 1446 | /* full content */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1447 | in.current = "top;"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1448 | assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1449 | in.current = "ntf {anydata a1; anyxml a2; choice ch; container c; description test; grouping grp; if-feature f; leaf l {type int8;}" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 1450 | "leaf-list ll {type int8;} list li; must 1; reference test; status current; typedef mytype {type int8;} uses grp; m:ext;}"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1451 | assert_int_equal(LY_SUCCESS, parse_notif(YCTX, (struct lysp_node *)c, ¬ifs)); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 1452 | assert_non_null(notifs); |
| 1453 | assert_int_equal(LYS_NOTIF, notifs->nodetype); |
| 1454 | assert_string_equal("ntf", notifs->name); |
| 1455 | assert_string_equal("test", notifs->dsc); |
| 1456 | assert_non_null(notifs->exts); |
| 1457 | assert_non_null(notifs->iffeatures); |
| 1458 | assert_string_equal("test", notifs->ref); |
| 1459 | assert_non_null(notifs->groupings); |
| 1460 | assert_non_null(notifs->typedefs); |
| 1461 | assert_non_null(notifs->musts); |
Radek Krejci | 01180ac | 2021-01-27 08:48:22 +0100 | [diff] [blame] | 1462 | assert_non_null(notifs->child); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 1463 | assert_int_equal(LYS_STATUS_CURR, notifs->flags); |
| 1464 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1465 | ly_set_erase(&YCTX->tpdfs_nodes, NULL); |
aPiecek | 63e080d | 2021-06-29 13:53:28 +0200 | [diff] [blame] | 1466 | ly_set_erase(&YCTX->grps_nodes, NULL); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1467 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)notifs); notifs = NULL; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 1468 | |
| 1469 | /* invalid content */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1470 | in.current = "ntf {config true} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1471 | assert_int_equal(LY_EVALID, parse_notif(YCTX, NULL, ¬ifs)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1472 | CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"notification\".", "Line number 1."); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1473 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)notifs); notifs = NULL; |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 1474 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1475 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | static void |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1479 | test_uses(void **state) |
| 1480 | { |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1481 | struct lysp_node_uses *u = NULL; |
Radek Krejci | df54913 | 2021-01-21 10:32:32 +0100 | [diff] [blame] | 1482 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1483 | YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1484 | |
| 1485 | /* invalid cardinality */ |
| 1486 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1487 | in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1488 | assert_int_equal(LY_EVALID, parse_uses(YCTX, NULL, (struct lysp_node**)&u)); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1489 | CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \ |
| 1490 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)u); u = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1491 | |
| 1492 | TEST_DUP("description", "text1", "text2"); |
| 1493 | TEST_DUP("reference", "1", "2"); |
| 1494 | TEST_DUP("status", "current", "obsolete"); |
| 1495 | TEST_DUP("when", "true", "false"); |
| 1496 | #undef TEST_DUP |
| 1497 | |
| 1498 | /* full content */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1499 | in.current = "grpref {augment some/node;description test;if-feature f;reference test;refine some/other/node;status current;when true;m:ext;} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1500 | assert_int_equal(LY_SUCCESS, parse_uses(YCTX, NULL, (struct lysp_node **)&u)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1501 | CHECK_LYSP_NODE(u, "test", 1, LYS_STATUS_CURR, 1, "grpref", 0, LYS_USES, 0, "test", 1); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1502 | assert_non_null(u->augments); |
| 1503 | assert_non_null(u->refines); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1504 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)u); u = NULL; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1505 | } |
Radek Krejci | 5a7c4a5 | 2019-02-12 15:45:11 +0100 | [diff] [blame] | 1506 | |
Radek Krejci | 5a7c4a5 | 2019-02-12 15:45:11 +0100 | [diff] [blame] | 1507 | static void |
| 1508 | test_augment(void **state) |
| 1509 | { |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1510 | struct lysp_node_augment *a = NULL; |
Radek Krejci | df54913 | 2021-01-21 10:32:32 +0100 | [diff] [blame] | 1511 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1512 | YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 5a7c4a5 | 2019-02-12 15:45:11 +0100 | [diff] [blame] | 1513 | |
| 1514 | /* invalid cardinality */ |
| 1515 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1516 | in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1517 | assert_int_equal(LY_EVALID, parse_augment(YCTX, NULL, &a)); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1518 | CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \ |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1519 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)a); a = NULL; |
Radek Krejci | 5a7c4a5 | 2019-02-12 15:45:11 +0100 | [diff] [blame] | 1520 | |
| 1521 | TEST_DUP("description", "text1", "text2"); |
| 1522 | TEST_DUP("reference", "1", "2"); |
| 1523 | TEST_DUP("status", "current", "obsolete"); |
| 1524 | TEST_DUP("when", "true", "false"); |
| 1525 | #undef TEST_DUP |
| 1526 | |
| 1527 | /* full content */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1528 | in.current = "/target/nodeid {action x; anydata any;anyxml anyxml; case cs; choice ch;container c;description test;if-feature f;leaf l {type string;}" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 1529 | "leaf-list ll {type string;} list li;notification not;reference test;status current;uses g;when true;m:ext;} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1530 | assert_int_equal(LY_SUCCESS, parse_augment(YCTX, NULL, &a)); |
Radek Krejci | 5a7c4a5 | 2019-02-12 15:45:11 +0100 | [diff] [blame] | 1531 | assert_non_null(a); |
| 1532 | assert_int_equal(LYS_AUGMENT, a->nodetype); |
| 1533 | assert_string_equal("/target/nodeid", a->nodeid); |
| 1534 | assert_string_equal("test", a->dsc); |
| 1535 | assert_non_null(a->exts); |
| 1536 | assert_non_null(a->iffeatures); |
| 1537 | assert_string_equal("test", a->ref); |
| 1538 | assert_non_null(a->when); |
| 1539 | assert_null(a->parent); |
| 1540 | assert_int_equal(LYS_STATUS_CURR, a->flags); |
Radek Krejci | 2a9fc65 | 2021-01-22 17:44:34 +0100 | [diff] [blame] | 1541 | lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)a); a = NULL; |
Radek Krejci | 5a7c4a5 | 2019-02-12 15:45:11 +0100 | [diff] [blame] | 1542 | } |
| 1543 | |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 1544 | static void |
| 1545 | test_when(void **state) |
| 1546 | { |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 1547 | struct lysp_when *w = NULL; |
Radek Krejci | df54913 | 2021-01-21 10:32:32 +0100 | [diff] [blame] | 1548 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1549 | YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 1550 | |
| 1551 | /* invalid cardinality */ |
| 1552 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1553 | in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1554 | assert_int_equal(LY_EVALID, parse_when(YCTX, &w)); \ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1555 | CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \ |
| 1556 | FREE_MEMBER(YCTX->parsed_mod->mod->ctx, w, lysp_when_free); w = NULL; |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 1557 | |
| 1558 | TEST_DUP("description", "text1", "text2"); |
| 1559 | TEST_DUP("reference", "1", "2"); |
| 1560 | #undef TEST_DUP |
| 1561 | |
| 1562 | /* full content */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1563 | in.current = "expression {description test;reference test;m:ext;} ..."; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1564 | assert_int_equal(LY_SUCCESS, parse_when(YCTX, &w)); |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 1565 | assert_non_null(w); |
| 1566 | assert_string_equal("expression", w->cond); |
| 1567 | assert_string_equal("test", w->dsc); |
| 1568 | assert_string_equal("test", w->ref); |
| 1569 | assert_non_null(w->exts); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1570 | FREE_MEMBER(YCTX->parsed_mod->mod->ctx, w, lysp_when_free); w = NULL; |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 1571 | |
| 1572 | /* empty condition */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1573 | in.current = "\"\";"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1574 | assert_int_equal(LY_SUCCESS, parse_when(YCTX, &w)); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1575 | CHECK_LOG_CTX("Empty argument of when statement does not make sense.", NULL); |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 1576 | assert_non_null(w); |
| 1577 | assert_string_equal("", w->cond); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1578 | FREE_MEMBER(YCTX->parsed_mod->mod->ctx, w, lysp_when_free); w = NULL; |
Radek Krejci | f09e4e8 | 2019-06-14 15:08:11 +0200 | [diff] [blame] | 1579 | } |
| 1580 | |
David Sedlák | d6ce6d7 | 2019-07-16 17:30:18 +0200 | [diff] [blame] | 1581 | static void |
| 1582 | test_value(void **state) |
| 1583 | { |
David Sedlák | d6ce6d7 | 2019-07-16 17:30:18 +0200 | [diff] [blame] | 1584 | int64_t val = 0; |
| 1585 | uint16_t flags = 0; |
| 1586 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1587 | in.current = "-0;"; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1588 | assert_int_equal(parse_type_enum_value_pos(YCTX, LY_STMT_VALUE, &val, &flags, NULL), LY_SUCCESS); |
David Sedlák | d6ce6d7 | 2019-07-16 17:30:18 +0200 | [diff] [blame] | 1589 | assert_int_equal(val, 0); |
| 1590 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1591 | in.current = "-0;"; |
David Sedlák | d6ce6d7 | 2019-07-16 17:30:18 +0200 | [diff] [blame] | 1592 | flags = 0; |
Radek Krejci | 33090f9 | 2020-12-17 20:12:46 +0100 | [diff] [blame] | 1593 | assert_int_equal(parse_type_enum_value_pos(YCTX, LY_STMT_POSITION, &val, &flags, NULL), LY_EVALID); |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1594 | CHECK_LOG_CTX("Invalid value \"-0\" of \"position\".", "Line number 1."); |
David Sedlák | d6ce6d7 | 2019-07-16 17:30:18 +0200 | [diff] [blame] | 1595 | } |
| 1596 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 1597 | int |
| 1598 | main(void) |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 1599 | { |
| 1600 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 1601 | UTEST(test_helpers, setup, teardown), |
| 1602 | UTEST(test_comments, setup, teardown), |
| 1603 | UTEST(test_arg, setup, teardown), |
| 1604 | UTEST(test_stmts, setup, teardown), |
| 1605 | UTEST(test_minmax, setup, teardown), |
| 1606 | UTEST(test_module, setup, teardown), |
| 1607 | UTEST(test_deviation, setup, teardown), |
| 1608 | UTEST(test_deviate, setup, teardown), |
| 1609 | UTEST(test_container, setup, teardown), |
| 1610 | UTEST(test_leaf, setup, teardown), |
| 1611 | UTEST(test_leaflist, setup, teardown), |
| 1612 | UTEST(test_list, setup, teardown), |
| 1613 | UTEST(test_choice, setup, teardown), |
| 1614 | UTEST(test_case, setup, teardown), |
| 1615 | UTEST(test_anydata, setup, teardown), |
| 1616 | UTEST(test_anyxml, setup, teardown), |
| 1617 | UTEST(test_action, setup, teardown), |
| 1618 | UTEST(test_notification, setup, teardown), |
| 1619 | UTEST(test_grouping, setup, teardown), |
| 1620 | UTEST(test_uses, setup, teardown), |
| 1621 | UTEST(test_augment, setup, teardown), |
| 1622 | UTEST(test_when, setup, teardown), |
| 1623 | UTEST(test_value, setup, teardown), |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 1624 | }; |
| 1625 | |
| 1626 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 1627 | } |