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