blob: 3b9c81e78ce8bd27d6647d3f94f2ca9cee7eda33 [file] [log] [blame]
Radek Krejci80dd33e2018-09-26 15:57:18 +02001/*
2 * @file test_parser_yang.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from parser_yang.c
5 *
Radek Iša56ca9e42020-09-08 18:42:00 +02006 * Copyright (c) 2018-2020 CESNET, z.s.p.o.
Radek Krejci80dd33e2018-09-26 15:57:18 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Iša56ca9e42020-09-08 18:42:00 +020014#define _UTEST_MAIN_
15#include "utests.h"
Radek Krejci80dd33e2018-09-26 15:57:18 +020016
Radek Krejci80dd33e2018-09-26 15:57:18 +020017#include <stdio.h>
18#include <string.h>
19
Radek Krejci70593c12020-06-13 20:48:09 +020020#include "common.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020021#include "in_internal.h"
Radek Krejci70593c12020-06-13 20:48:09 +020022#include "parser_internal.h"
Michal Vasko405cc9e2020-12-01 12:01:27 +010023#include "schema_compile.h"
Radek Krejci70593c12020-06-13 20:48:09 +020024#include "tree_schema.h"
25#include "tree_schema_internal.h"
Radek Krejci2d7a47b2019-05-16 13:34:10 +020026
27/* originally static functions from tree_schema_free.c and parser_yang.c */
28void lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020029void lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev);
30void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp);
31void lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action);
32void lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif);
33void lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment);
34void lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d);
35void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
Radek Krejcif09e4e82019-06-14 15:08:11 +020036void lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020037
Michal Vasko63f3d842020-07-08 10:10:14 +020038LY_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);
39LY_ERR buf_store_char(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum yang_arg arg, char **word_p,
Radek Krejcib4ac5a92020-11-23 17:54:33 +010040 size_t *word_len, char **word_b, size_t *buf_len, uint8_t need_buf, uint8_t *prefix);
Michal Vasko63f3d842020-07-08 10:10:14 +020041LY_ERR get_keyword(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt *kw, char **word_p, size_t *word_len);
42LY_ERR get_argument(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum yang_arg arg,
Radek Krejcib4ac5a92020-11-23 17:54:33 +010043 uint16_t *flags, char **word_p, char **word_b, size_t *word_len);
Radek Krejci1deb5be2020-08-26 16:43:36 +020044LY_ERR skip_comment(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint8_t comment);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020045
Michal Vasko63f3d842020-07-08 10:10:14 +020046LY_ERR parse_action(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_action **actions);
47LY_ERR parse_any(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt kw, struct lysp_node *parent, struct lysp_node **siblings);
48LY_ERR parse_augment(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_augment **augments);
49LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
50LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
51LY_ERR parse_deviate(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_deviate **deviates);
52LY_ERR parse_deviation(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_deviation **deviations);
53LY_ERR parse_grouping(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_grp **groupings);
54LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
55LY_ERR parse_leaf(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
56LY_ERR parse_leaflist(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
57LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
58LY_ERR parse_maxelements(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts);
59LY_ERR parse_minelements(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts);
60LY_ERR parse_module(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_module *mod);
61LY_ERR parse_notif(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_notif **notifs);
62LY_ERR parse_submodule(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_submodule *submod);
63LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
64LY_ERR parse_when(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_when **when_p);
65LY_ERR parse_type_enum_value_pos(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt val_kw, int64_t *value, uint16_t *flags, struct lysp_ext_instance **exts);
Radek Krejci80dd33e2018-09-26 15:57:18 +020066
Radek Iša56ca9e42020-09-08 18:42:00 +020067struct lys_yang_parser_ctx *YCTX;
Radek Krejci80dd33e2018-09-26 15:57:18 +020068
Michal Vasko5d24f6c2020-10-13 13:49:06 +020069static int
Radek Iša56ca9e42020-09-08 18:42:00 +020070setup(void **state)
Michal Vasko5d24f6c2020-10-13 13:49:06 +020071{
Radek Iša56ca9e42020-09-08 18:42:00 +020072 UTEST_SETUP;
Michal Vasko5d24f6c2020-10-13 13:49:06 +020073
Radek Iša56ca9e42020-09-08 18:42:00 +020074 /* allocate parser context */
75 YCTX = calloc(1, sizeof(*YCTX));
76 YCTX->format = LYS_IN_YANG;
77 YCTX->pos_type = LY_VLOG_LINE;
78 YCTX->line = 1;
Michal Vasko5d24f6c2020-10-13 13:49:06 +020079
Radek Iša56ca9e42020-09-08 18:42:00 +020080 /* allocate new parsed module */
81 YCTX->parsed_mod = calloc(1, sizeof *YCTX->parsed_mod);
Michal Vasko5d24f6c2020-10-13 13:49:06 +020082
Radek Iša56ca9e42020-09-08 18:42:00 +020083 /* allocate new module */
84 YCTX->parsed_mod->mod = calloc(1, sizeof *YCTX->parsed_mod->mod);
85 YCTX->parsed_mod->mod->ctx = UTEST_LYCTX;
86 YCTX->parsed_mod->mod->parsed = YCTX->parsed_mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +020087
Michal Vasko5d24f6c2020-10-13 13:49:06 +020088 return 0;
89}
90
91static int
Radek Iša56ca9e42020-09-08 18:42:00 +020092teardown(void **state)
Michal Vasko5d24f6c2020-10-13 13:49:06 +020093{
Radek Iša56ca9e42020-09-08 18:42:00 +020094 lys_module_free(YCTX->parsed_mod->mod, NULL);
95 free(YCTX);
96 YCTX = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +020097
Radek Iša56ca9e42020-09-08 18:42:00 +020098 UTEST_TEARDOWN;
Michal Vasko5d24f6c2020-10-13 13:49:06 +020099
100 return 0;
101}
102
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200103#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200104 in.current = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Iša56ca9e42020-09-08 18:42:00 +0200105 assert_int_equal(LY_EVALID, FUNC(YCTX, &in, RESULT)); \
106 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number "LINE".");\
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200107 CLEANUP
Radek Krejci44ceedc2018-10-02 15:54:31 +0200108static void
109test_helpers(void **state)
110{
Michal Vasko63f3d842020-07-08 10:10:14 +0200111 struct ly_in in = {0};
Radek Krejci404251e2018-10-09 12:06:44 +0200112 char *buf, *p;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200113 size_t len, size;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200114 uint8_t prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200115
116 /* storing into buffer */
Michal Vasko63f3d842020-07-08 10:10:14 +0200117 in.current = "abcd";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200118 buf = NULL;
119 size = len = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200120 assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &in, 2, &buf, &size, &len));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200121 assert_int_not_equal(0, size);
122 assert_int_equal(2, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200123 assert_string_equal("cd", in.current);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200124 assert_false(strncmp("ab", buf, 2));
125 free(buf);
Radek Krejci404251e2018-10-09 12:06:44 +0200126 buf = NULL;
127
128 /* invalid first characters */
129 len = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200130 in.current = "2invalid";
Radek Iša56ca9e42020-09-08 18:42:00 +0200131 assert_int_equal(LY_EVALID, buf_store_char(YCTX, &in, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Michal Vasko63f3d842020-07-08 10:10:14 +0200132 in.current = ".invalid";
Radek Iša56ca9e42020-09-08 18:42:00 +0200133 assert_int_equal(LY_EVALID, buf_store_char(YCTX, &in, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Michal Vasko63f3d842020-07-08 10:10:14 +0200134 in.current = "-invalid";
Radek Iša56ca9e42020-09-08 18:42:00 +0200135 assert_int_equal(LY_EVALID, buf_store_char(YCTX, &in, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200136 /* invalid following characters */
137 len = 3; /* number of characters read before the str content */
Michal Vasko63f3d842020-07-08 10:10:14 +0200138 in.current = "!";
Radek Iša56ca9e42020-09-08 18:42:00 +0200139 assert_int_equal(LY_EVALID, buf_store_char(YCTX, &in, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Michal Vasko63f3d842020-07-08 10:10:14 +0200140 in.current = ":";
Radek Iša56ca9e42020-09-08 18:42:00 +0200141 assert_int_equal(LY_EVALID, buf_store_char(YCTX, &in, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200142 /* valid colon for prefixed identifiers */
143 len = size = 0;
144 p = NULL;
David Sedlák40bb13b2019-07-10 14:34:18 +0200145 prefix = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200146 in.current = "x:id";
Radek Iša56ca9e42020-09-08 18:42:00 +0200147 assert_int_equal(LY_SUCCESS, buf_store_char(YCTX, &in, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 0, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200148 assert_int_equal(1, len);
149 assert_null(buf);
Michal Vasko63f3d842020-07-08 10:10:14 +0200150 assert_string_equal(":id", in.current);
Radek Krejci404251e2018-10-09 12:06:44 +0200151 assert_int_equal('x', p[len - 1]);
Radek Iša56ca9e42020-09-08 18:42:00 +0200152 assert_int_equal(LY_SUCCESS, buf_store_char(YCTX, &in, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200153 assert_int_equal(2, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200154 assert_string_equal("id", in.current);
Radek Krejci404251e2018-10-09 12:06:44 +0200155 assert_int_equal(':', p[len - 1]);
156 free(buf);
David Sedlák40bb13b2019-07-10 14:34:18 +0200157 prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200158
159 /* checking identifiers */
Radek Iša56ca9e42020-09-08 18:42:00 +0200160 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, NULL));
161 CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
162 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, '#', 1, NULL));
163 CHECK_LOG_CTX("Invalid identifier first character '#' (0x0023).", "Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200164
Radek Iša56ca9e42020-09-08 18:42:00 +0200165 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, 'a', 1, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200166 assert_int_equal(0, prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200167 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200168 assert_int_equal(1, prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200169 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, &prefix));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200170 assert_int_equal(1, prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200171 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, 'b', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200172 assert_int_equal(2, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200173 /* second colon is invalid */
Radek Iša56ca9e42020-09-08 18:42:00 +0200174 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, &prefix));
175 CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200176}
Radek Krejci80dd33e2018-09-26 15:57:18 +0200177
Radek Iša56ca9e42020-09-08 18:42:00 +0200178#define TEST_GET_ARGUMENT_SUCCESS(INPUT_TEXT, CTX, ARG_TYPE, EXPECT_WORD, EXPECT_LEN, EXPECT_CURRENT)\
179 {\
180 const char * text = INPUT_TEXT;\
181 in.current = text;\
182 assert_int_equal(LY_SUCCESS, get_argument(CTX, &in, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));\
183 assert_string_equal(word, EXPECT_WORD);\
184 assert_int_equal(len, EXPECT_LEN);\
185 assert_string_equal(EXPECT_CURRENT, in.current);\
186 }
187
Radek Krejci80dd33e2018-09-26 15:57:18 +0200188static void
189test_comments(void **state)
190{
Michal Vasko63f3d842020-07-08 10:10:14 +0200191 struct ly_in in = {0};
Radek Krejciefd22f62018-09-27 11:47:58 +0200192 char *word, *buf;
193 size_t len;
Radek Iša56ca9e42020-09-08 18:42:00 +0200194 const char *in_text;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200195
Radek Iša56ca9e42020-09-08 18:42:00 +0200196 // in.current = " // this is a text of / one * line */ comment\nargument;";
197 in_text = " // this is a text of / one * line */ comment\nargument;";
198 TEST_GET_ARGUMENT_SUCCESS(in_text, YCTX, Y_STR_ARG, "argument;", 8, ";");
Radek Krejciefd22f62018-09-27 11:47:58 +0200199 assert_null(buf);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200200
Radek Iša56ca9e42020-09-08 18:42:00 +0200201 in_text = "/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\";";
202 TEST_GET_ARGUMENT_SUCCESS(in_text, YCTX, Y_STR_ARG, "argument", 8, ";");
Radek Krejciefd22f62018-09-27 11:47:58 +0200203 assert_ptr_equal(buf, word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200204 free(word);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200205
Michal Vasko63f3d842020-07-08 10:10:14 +0200206 in.current = " this is one line comment on last line";
Radek Iša56ca9e42020-09-08 18:42:00 +0200207 assert_int_equal(LY_SUCCESS, skip_comment(YCTX, &in, 1));
Michal Vasko63f3d842020-07-08 10:10:14 +0200208 assert_true(in.current[0] == '\0');
Radek Krejci80dd33e2018-09-26 15:57:18 +0200209
Michal Vasko63f3d842020-07-08 10:10:14 +0200210 in.current = " this is a not terminated comment x";
Radek Iša56ca9e42020-09-08 18:42:00 +0200211 assert_int_equal(LY_EVALID, skip_comment(YCTX, &in, 2));
212 CHECK_LOG_CTX("Unexpected end-of-input, non-terminated comment.", "Line number 5.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200213 assert_true(in.current[0] == '\0');
Radek Krejci80dd33e2018-09-26 15:57:18 +0200214}
215
Radek Krejciefd22f62018-09-27 11:47:58 +0200216static void
217test_arg(void **state)
218{
Michal Vasko63f3d842020-07-08 10:10:14 +0200219 struct ly_in in = {0};
Radek Krejciefd22f62018-09-27 11:47:58 +0200220 char *word, *buf;
221 size_t len;
222
223 /* missing argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200224 in.current = ";";
Radek Iša56ca9e42020-09-08 18:42:00 +0200225 assert_int_equal(LY_SUCCESS, get_argument(YCTX, &in, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200226 assert_null(word);
227
Michal Vasko63f3d842020-07-08 10:10:14 +0200228 in.current = "{";
Radek Iša56ca9e42020-09-08 18:42:00 +0200229 assert_int_equal(LY_EVALID, get_argument(YCTX, &in, Y_STR_ARG, NULL, &word, &buf, &len));
230 CHECK_LOG_CTX("Invalid character sequence \"{\", expected an argument.", "Line number 1.");
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200231
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200232 /* invalid escape sequence */
Michal Vasko63f3d842020-07-08 10:10:14 +0200233 in.current = "\"\\s\"";
Radek Iša56ca9e42020-09-08 18:42:00 +0200234 assert_int_equal(LY_EVALID, get_argument(YCTX, &in, Y_STR_ARG, NULL, &word, &buf, &len));
235 CHECK_LOG_CTX("Double-quoted string unknown special character \'\\s\'.", "Line number 1.");
236
237 TEST_GET_ARGUMENT_SUCCESS("\'\\s\'", YCTX, Y_STR_ARG, "\\s\'", 2, "");
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200238
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200239 /* invalid character after the argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200240 in.current = "hello\"";
Radek Iša56ca9e42020-09-08 18:42:00 +0200241 assert_int_equal(LY_EVALID, get_argument(YCTX, &in, Y_STR_ARG, NULL, &word, &buf, &len));
242 CHECK_LOG_CTX("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace.", "Line number 1.");
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200243
Radek Iša56ca9e42020-09-08 18:42:00 +0200244 in.current = "hello}";
245 assert_int_equal(LY_EVALID, get_argument(YCTX, &in, Y_STR_ARG, NULL, &word, &buf, &len));
246 CHECK_LOG_CTX("Invalid character sequence \"}\", expected unquoted string character, optsep, semicolon or opening brace.", "Line number 1.");
David Sedlák40bb13b2019-07-10 14:34:18 +0200247 /* invalid identifier-ref-arg-str */
Michal Vasko63f3d842020-07-08 10:10:14 +0200248 in.current = "pre:pre:value";
Radek Iša56ca9e42020-09-08 18:42:00 +0200249 assert_int_equal(LY_EVALID, get_argument(YCTX, &in, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
250 CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
David Sedlák40bb13b2019-07-10 14:34:18 +0200251
Michal Vasko63f3d842020-07-08 10:10:14 +0200252 in.current = "\"\";"; /* empty identifier is not allowed */
Radek Iša56ca9e42020-09-08 18:42:00 +0200253 assert_int_equal(LY_EVALID, get_argument(YCTX, &in, Y_IDENTIF_ARG, NULL, &word, &buf, &len));
254 CHECK_LOG_CTX("Statement argument is required.", "Line number 1.");
255
Michal Vasko63f3d842020-07-08 10:10:14 +0200256 in.current = "\"\";"; /* empty reference identifier is not allowed */
Radek Iša56ca9e42020-09-08 18:42:00 +0200257 assert_int_equal(LY_EVALID, get_argument(YCTX, &in, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
258 CHECK_LOG_CTX("Statement argument is required.", "Line number 1.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200259
Radek Iša56ca9e42020-09-08 18:42:00 +0200260 /* slash is not an invalid character */
261 TEST_GET_ARGUMENT_SUCCESS("hello/x\t", YCTX, Y_STR_ARG, "hello/x\t", 7, "\t");
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200262 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200263
264 /* different quoting */
Radek Iša56ca9e42020-09-08 18:42:00 +0200265 TEST_GET_ARGUMENT_SUCCESS("hello/x\t", YCTX, Y_STR_ARG, "hello/x\t", 7, "\t");
Radek Krejciefd22f62018-09-27 11:47:58 +0200266
Radek Iša56ca9e42020-09-08 18:42:00 +0200267 TEST_GET_ARGUMENT_SUCCESS("hello ", YCTX, Y_STR_ARG, "hello ", 5, " ");
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200268
Radek Iša56ca9e42020-09-08 18:42:00 +0200269 TEST_GET_ARGUMENT_SUCCESS("hello/*comment*/\n", YCTX, Y_STR_ARG, "hello/*comment*/\n", 5, "\n");
270
271 TEST_GET_ARGUMENT_SUCCESS("\"hello\\n\\t\\\"\\\\\";", YCTX, Y_STR_ARG, "hello\n\t\"\\", 9, ";");
fredgand49fe112019-10-21 20:51:50 +0800272 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200273
Radek Iša56ca9e42020-09-08 18:42:00 +0200274 YCTX->indent = 14;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200275 /* - space and tabs before newline are stripped out
276 * - space and tabs after newline (indentation) are stripped out
277 */
Radek Iša56ca9e42020-09-08 18:42:00 +0200278 TEST_GET_ARGUMENT_SUCCESS("\"hello \t\n\t\t world!\"", YCTX, Y_STR_ARG, "hello\n world!", 14, "");
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200279 free(buf);
Radek Iša56ca9e42020-09-08 18:42:00 +0200280
281/* In contrast to previous, the backslash-escaped tabs are expanded after trimming, so they are preserved */
282 YCTX->indent = 14;
283 TEST_GET_ARGUMENT_SUCCESS("\"hello \\t\n\t\\t world!\"", YCTX, Y_STR_ARG, "hello \t\n\t world!", 16, "");
Radek Krejciff13cd12019-10-25 15:34:24 +0200284 assert_ptr_equal(word, buf);
Radek Krejciff13cd12019-10-25 15:34:24 +0200285 free(buf);
Radek Iša56ca9e42020-09-08 18:42:00 +0200286
Radek Krejciff13cd12019-10-25 15:34:24 +0200287 /* Do not handle whitespaces after backslash-escaped newline as indentation */
Radek Iša56ca9e42020-09-08 18:42:00 +0200288 YCTX->indent = 14;
289 TEST_GET_ARGUMENT_SUCCESS("\"hello\\n\t\t world!\"", YCTX, Y_STR_ARG, "hello\n\t\t world!", 15, "");
Radek Krejciff13cd12019-10-25 15:34:24 +0200290 assert_ptr_equal(word, buf);
Radek Krejciff13cd12019-10-25 15:34:24 +0200291 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200292
Radek Iša56ca9e42020-09-08 18:42:00 +0200293 YCTX->indent = 14;
294 TEST_GET_ARGUMENT_SUCCESS("\"hello\n \tworld!\"", YCTX, Y_STR_ARG, "hello\nworld!", 12, "");
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200295 assert_ptr_equal(word, buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200296 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200297
Radek Iša56ca9e42020-09-08 18:42:00 +0200298 TEST_GET_ARGUMENT_SUCCESS("\'hello\'", YCTX, Y_STR_ARG, "hello'", 5, "");
Radek Krejciefd22f62018-09-27 11:47:58 +0200299
Radek Iša56ca9e42020-09-08 18:42:00 +0200300 TEST_GET_ARGUMENT_SUCCESS("\"hel\" +\t\n\"lo\"", YCTX, Y_STR_ARG, "hello", 5, "");
Radek Krejciefd22f62018-09-27 11:47:58 +0200301 assert_ptr_equal(word, buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200302 free(buf);
Radek Iša56ca9e42020-09-08 18:42:00 +0200303
Michal Vasko63f3d842020-07-08 10:10:14 +0200304 in.current = "\"hel\" +\t\nlo"; /* unquoted the second part */
Radek Iša56ca9e42020-09-08 18:42:00 +0200305 assert_int_equal(LY_EVALID, get_argument(YCTX, &in, Y_STR_ARG, NULL, &word, &buf, &len));
306 CHECK_LOG_CTX("Both string parts divided by '+' must be quoted.", "Line number 6.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200307
Radek Iša56ca9e42020-09-08 18:42:00 +0200308 TEST_GET_ARGUMENT_SUCCESS("\'he\'\t\n+ \"llo\"", YCTX, Y_STR_ARG, "hello", 5, "");
Radek Krejciefd22f62018-09-27 11:47:58 +0200309 free(buf);
310
Radek Iša56ca9e42020-09-08 18:42:00 +0200311 TEST_GET_ARGUMENT_SUCCESS(" \t\n\"he\"+\'llo\'", YCTX, Y_STR_ARG, "hello", 5, "");
Radek Krejciefd22f62018-09-27 11:47:58 +0200312 free(buf);
313
Radek Krejci44ceedc2018-10-02 15:54:31 +0200314 /* missing argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200315 in.current = ";";
Radek Iša56ca9e42020-09-08 18:42:00 +0200316 assert_int_equal(LY_EVALID, get_argument(YCTX, &in, Y_STR_ARG, NULL, &word, &buf, &len));
317 CHECK_LOG_CTX("Invalid character sequence \";\", expected an argument.", "Line number 8.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200318}
319
Radek Iša56ca9e42020-09-08 18:42:00 +0200320#define TEST_STMS_SUCCESS(INPUT_TEXT, CTX, ACTION, EXPECT_WORD)\
321 in.current = INPUT_TEXT;\
322 assert_int_equal(LY_SUCCESS, get_keyword(CTX, &in, &kw, &word, &len));\
323 assert_int_equal(ACTION, kw);\
324 assert_int_equal(strlen(EXPECT_WORD), len);\
325 assert_true(0 == strncmp(EXPECT_WORD, word, len))
326
Radek Krejcidcc7b322018-10-11 14:24:02 +0200327static void
328test_stmts(void **state)
329{
Michal Vasko63f3d842020-07-08 10:10:14 +0200330 struct ly_in in = {0};
331 const char *p;
Radek Krejcid6b76452019-09-03 17:03:03 +0200332 enum ly_stmt kw;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200333 char *word;
334 size_t len;
335
Michal Vasko63f3d842020-07-08 10:10:14 +0200336 in.current = "\n// comment\n\tinput\t{";
Radek Iša56ca9e42020-09-08 18:42:00 +0200337 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200338 assert_int_equal(LY_STMT_INPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200339 assert_int_equal(5, len);
340 assert_string_equal("input\t{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200341 assert_string_equal("\t{", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200342
Michal Vasko63f3d842020-07-08 10:10:14 +0200343 in.current = "\t /* comment */\t output\n\t{";
Radek Iša56ca9e42020-09-08 18:42:00 +0200344 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200345 assert_int_equal(LY_STMT_OUTPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200346 assert_int_equal(6, len);
347 assert_string_equal("output\n\t{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200348 assert_string_equal("\n\t{", in.current);
Radek Iša56ca9e42020-09-08 18:42:00 +0200349 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200350 assert_int_equal(LY_STMT_SYNTAX_LEFT_BRACE, kw);
Radek Krejciabdd8062019-06-11 16:44:19 +0200351 assert_int_equal(1, len);
352 assert_string_equal("{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200353 assert_string_equal("", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200354
Michal Vasko63f3d842020-07-08 10:10:14 +0200355 in.current = "/input { "; /* invalid slash */
Radek Iša56ca9e42020-09-08 18:42:00 +0200356 assert_int_equal(LY_EVALID, get_keyword(YCTX, &in, &kw, &word, &len));
357 CHECK_LOG_CTX("Invalid identifier first character '/'.", "Line number 4.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200358
Michal Vasko63f3d842020-07-08 10:10:14 +0200359 in.current = "not-a-statement-nor-extension { "; /* invalid identifier */
Radek Iša56ca9e42020-09-08 18:42:00 +0200360 assert_int_equal(LY_EVALID, get_keyword(YCTX, &in, &kw, &word, &len));
361 CHECK_LOG_CTX("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword.", "Line number 4.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200362
Michal Vasko63f3d842020-07-08 10:10:14 +0200363 in.current = "path;"; /* missing sep after the keyword */
Radek Iša56ca9e42020-09-08 18:42:00 +0200364 assert_int_equal(LY_EVALID, get_keyword(YCTX, &in, &kw, &word, &len));
365 CHECK_LOG_CTX("Invalid character sequence \"path;\", expected a keyword followed by a separator.", "Line number 4.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200366
Radek Iša56ca9e42020-09-08 18:42:00 +0200367 TEST_STMS_SUCCESS("action ", YCTX, LY_STMT_ACTION, "action");
368
369 TEST_STMS_SUCCESS("anydata ", YCTX, LY_STMT_ANYDATA, "anydata");
370 TEST_STMS_SUCCESS("anyxml ", YCTX, LY_STMT_ANYXML, "anyxml");
371 TEST_STMS_SUCCESS("argument ", YCTX, LY_STMT_ARGUMENT, "argument");
372 TEST_STMS_SUCCESS("augment ", YCTX, LY_STMT_AUGMENT, "augment");
373 TEST_STMS_SUCCESS("base ", YCTX, LY_STMT_BASE, "base");
374 TEST_STMS_SUCCESS("belongs-to ", YCTX, LY_STMT_BELONGS_TO, "belongs-to");
375 TEST_STMS_SUCCESS("bit ", YCTX, LY_STMT_BIT, "bit");
376 TEST_STMS_SUCCESS("case ", YCTX, LY_STMT_CASE, "case");
377 TEST_STMS_SUCCESS("choice ", YCTX, LY_STMT_CHOICE, "choice");
378 TEST_STMS_SUCCESS("config ", YCTX, LY_STMT_CONFIG, "config");
379 TEST_STMS_SUCCESS("contact ", YCTX, LY_STMT_CONTACT, "contact");
380 TEST_STMS_SUCCESS("container ", YCTX, LY_STMT_CONTAINER, "container");
381 TEST_STMS_SUCCESS("default ", YCTX, LY_STMT_DEFAULT, "default");
382 TEST_STMS_SUCCESS("description ", YCTX, LY_STMT_DESCRIPTION, "description");
383 TEST_STMS_SUCCESS("deviate ", YCTX, LY_STMT_DEVIATE, "deviate");
384 TEST_STMS_SUCCESS("deviation ", YCTX, LY_STMT_DEVIATION, "deviation");
385 TEST_STMS_SUCCESS("enum ", YCTX, LY_STMT_ENUM, "enum");
386 TEST_STMS_SUCCESS("error-app-tag ", YCTX, LY_STMT_ERROR_APP_TAG, "error-app-tag");
387 TEST_STMS_SUCCESS("error-message ", YCTX, LY_STMT_ERROR_MESSAGE, "error-message");
388 TEST_STMS_SUCCESS("extension ", YCTX, LY_STMT_EXTENSION, "extension");
389 TEST_STMS_SUCCESS("feature ", YCTX, LY_STMT_FEATURE, "feature");
390 TEST_STMS_SUCCESS("fraction-digits ", YCTX, LY_STMT_FRACTION_DIGITS, "fraction-digits");
391 TEST_STMS_SUCCESS("grouping ", YCTX, LY_STMT_GROUPING, "grouping");
392 TEST_STMS_SUCCESS("identity ", YCTX, LY_STMT_IDENTITY, "identity");
393 TEST_STMS_SUCCESS("if-feature ", YCTX, LY_STMT_IF_FEATURE, "if-feature");
394 TEST_STMS_SUCCESS("import ", YCTX, LY_STMT_IMPORT, "import");
395 TEST_STMS_SUCCESS("include ", YCTX, LY_STMT_INCLUDE, "include");
396 TEST_STMS_SUCCESS("input{", YCTX, LY_STMT_INPUT, "input");
397 TEST_STMS_SUCCESS("key ", YCTX, LY_STMT_KEY, "key");
398 TEST_STMS_SUCCESS("leaf ", YCTX, LY_STMT_LEAF, "leaf");
399 TEST_STMS_SUCCESS("leaf-list ", YCTX, LY_STMT_LEAF_LIST, "leaf-list");
400 TEST_STMS_SUCCESS("length ", YCTX, LY_STMT_LENGTH, "length");
401 TEST_STMS_SUCCESS("list ", YCTX, LY_STMT_LIST, "list");
402 TEST_STMS_SUCCESS("mandatory ", YCTX, LY_STMT_MANDATORY, "mandatory");
403 TEST_STMS_SUCCESS("max-elements ", YCTX, LY_STMT_MAX_ELEMENTS, "max-elements");
404 TEST_STMS_SUCCESS("min-elements ", YCTX, LY_STMT_MIN_ELEMENTS, "min-elements");
405 TEST_STMS_SUCCESS("modifier ", YCTX, LY_STMT_MODIFIER, "modifier");
406 TEST_STMS_SUCCESS("module ", YCTX, LY_STMT_MODULE, "module");
407 TEST_STMS_SUCCESS("must ", YCTX, LY_STMT_MUST, "must");
408 TEST_STMS_SUCCESS("namespace ", YCTX, LY_STMT_NAMESPACE, "namespace");
409 TEST_STMS_SUCCESS("notification ", YCTX, LY_STMT_NOTIFICATION, "notification");
410 TEST_STMS_SUCCESS("ordered-by ", YCTX, LY_STMT_ORDERED_BY, "ordered-by");
411 TEST_STMS_SUCCESS("organization ", YCTX, LY_STMT_ORGANIZATION, "organization");
412 TEST_STMS_SUCCESS("output ", YCTX, LY_STMT_OUTPUT, "output");
413 TEST_STMS_SUCCESS("path ", YCTX, LY_STMT_PATH, "path");
414 TEST_STMS_SUCCESS("pattern ", YCTX, LY_STMT_PATTERN, "pattern");
415 TEST_STMS_SUCCESS("position ", YCTX, LY_STMT_POSITION, "position");
416 TEST_STMS_SUCCESS("prefix ", YCTX, LY_STMT_PREFIX, "prefix");
417 TEST_STMS_SUCCESS("presence ", YCTX, LY_STMT_PRESENCE, "presence");
418 TEST_STMS_SUCCESS("range ", YCTX, LY_STMT_RANGE, "range");
419 TEST_STMS_SUCCESS("reference ", YCTX, LY_STMT_REFERENCE, "reference");
420 TEST_STMS_SUCCESS("refine ", YCTX, LY_STMT_REFINE, "refine");
421 TEST_STMS_SUCCESS("require-instance ", YCTX, LY_STMT_REQUIRE_INSTANCE, "require-instance");
422 TEST_STMS_SUCCESS("revision ", YCTX, LY_STMT_REVISION, "revision");
423 TEST_STMS_SUCCESS("revision-date ", YCTX, LY_STMT_REVISION_DATE, "revision-date");
424 TEST_STMS_SUCCESS("rpc ", YCTX, LY_STMT_RPC, "rpc");
425 TEST_STMS_SUCCESS("status ", YCTX, LY_STMT_STATUS, "status");
426 TEST_STMS_SUCCESS("submodule ", YCTX, LY_STMT_SUBMODULE, "submodule");
427 TEST_STMS_SUCCESS("type ", YCTX, LY_STMT_TYPE, "type");
428 TEST_STMS_SUCCESS("typedef ", YCTX, LY_STMT_TYPEDEF, "typedef");
429 TEST_STMS_SUCCESS("unique ", YCTX, LY_STMT_UNIQUE, "unique");
430 TEST_STMS_SUCCESS("units ", YCTX, LY_STMT_UNITS, "units");
431 TEST_STMS_SUCCESS("uses ", YCTX, LY_STMT_USES, "uses");
432 TEST_STMS_SUCCESS("value ", YCTX, LY_STMT_VALUE, "value");
433 TEST_STMS_SUCCESS("when ", YCTX, LY_STMT_WHEN, "when");
434 TEST_STMS_SUCCESS("yang-version ", YCTX, LY_STMT_YANG_VERSION, "yang-version");
435 TEST_STMS_SUCCESS("yin-element ", YCTX, LY_STMT_YIN_ELEMENT, "yin-element");
436 TEST_STMS_SUCCESS(";config false;", YCTX, LY_STMT_SYNTAX_SEMICOLON, ";");
Michal Vasko63f3d842020-07-08 10:10:14 +0200437 assert_string_equal("config false;", in.current);
Radek Iša56ca9e42020-09-08 18:42:00 +0200438 TEST_STMS_SUCCESS("{ config false;", YCTX, LY_STMT_SYNTAX_LEFT_BRACE, "{");
Michal Vasko63f3d842020-07-08 10:10:14 +0200439 assert_string_equal(" config false;", in.current);
Radek Iša56ca9e42020-09-08 18:42:00 +0200440 TEST_STMS_SUCCESS("}", YCTX, LY_STMT_SYNTAX_RIGHT_BRACE, "}");
Michal Vasko63f3d842020-07-08 10:10:14 +0200441 assert_string_equal("", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200442
443 /* geenric extension */
Michal Vasko63f3d842020-07-08 10:10:14 +0200444 in.current = p = "nacm:default-deny-write;";
Radek Iša56ca9e42020-09-08 18:42:00 +0200445 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200446 assert_int_equal(LY_STMT_EXTENSION_INSTANCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200447 assert_int_equal(23, len);
448 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200449}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200450
Radek Iša56ca9e42020-09-08 18:42:00 +0200451#define TEST_MINMAX_SUCCESS(INPUT_TEXT, CTX, TYPE, VALUE)\
452 in.current = INPUT_TEXT;\
453 if(TYPE == LYS_SET_MIN){\
454 assert_int_equal(LY_SUCCESS, parse_minelements(CTX, &in, &value, &flags, &ext));\
455 }\
456 if(TYPE == LYS_SET_MAX){\
457 assert_int_equal(LY_SUCCESS, parse_maxelements(CTX, &in, &value, &flags, &ext));\
458 }\
459 assert_int_equal(TYPE, flags);\
460 assert_int_equal(VALUE, value)
461
Radek Krejci05b13982018-11-28 16:22:07 +0100462static void
463test_minmax(void **state)
464{
Radek Krejci05b13982018-11-28 16:22:07 +0100465 uint16_t flags = 0;
466 uint32_t value = 0;
467 struct lysp_ext_instance *ext = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200468 struct ly_in in = {0};
Radek Krejci05b13982018-11-28 16:22:07 +0100469
Radek Iša56ca9e42020-09-08 18:42:00 +0200470 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci05b13982018-11-28 16:22:07 +0100471
Michal Vasko63f3d842020-07-08 10:10:14 +0200472 in.current = " 1invalid; ...";
Radek Iša56ca9e42020-09-08 18:42:00 +0200473 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &in, &value, &flags, &ext));
474 CHECK_LOG_CTX("Invalid value \"1invalid\" of \"min-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100475
476 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200477 in.current = " -1; ...";
Radek Iša56ca9e42020-09-08 18:42:00 +0200478 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &in, &value, &flags, &ext));
479 CHECK_LOG_CTX("Invalid value \"-1\" of \"min-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100480
Radek Krejcidf6cad12018-11-28 17:10:55 +0100481 /* implementation limit */
482 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200483 in.current = " 4294967296; ...";
Radek Iša56ca9e42020-09-08 18:42:00 +0200484 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &in, &value, &flags, &ext));
485 CHECK_LOG_CTX("Value \"4294967296\" is out of \"min-elements\" bounds.", "Line number 1.");
Radek Krejcidf6cad12018-11-28 17:10:55 +0100486
Radek Krejci05b13982018-11-28 16:22:07 +0100487 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200488 TEST_MINMAX_SUCCESS(" 1; ...", YCTX, LYS_SET_MIN, 1);
Radek Krejci05b13982018-11-28 16:22:07 +0100489
490 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200491 TEST_MINMAX_SUCCESS(" 1 {m:ext;} ...", YCTX, LYS_SET_MIN, 1);
Radek Krejci05b13982018-11-28 16:22:07 +0100492 assert_non_null(ext);
Radek Iša56ca9e42020-09-08 18:42:00 +0200493 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, ext, lysp_ext_instance_free);
Radek Krejci05b13982018-11-28 16:22:07 +0100494 ext = NULL;
495
496 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200497 in.current = " 1 {config true;} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +0200498 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &in, &value, &flags, &ext));
499 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"min-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100500
Michal Vasko63f3d842020-07-08 10:10:14 +0200501 in.current = " 1invalid; ...";
Radek Iša56ca9e42020-09-08 18:42:00 +0200502 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &in, &value, &flags, &ext));
503 CHECK_LOG_CTX("Invalid value \"1invalid\" of \"max-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100504
505 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200506 in.current = " -1; ...";
Radek Iša56ca9e42020-09-08 18:42:00 +0200507 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &in, &value, &flags, &ext));
508 CHECK_LOG_CTX("Invalid value \"-1\" of \"max-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100509
Radek Krejcidf6cad12018-11-28 17:10:55 +0100510 /* implementation limit */
511 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200512 in.current = " 4294967296; ...";
Radek Iša56ca9e42020-09-08 18:42:00 +0200513 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &in, &value, &flags, &ext));
514 CHECK_LOG_CTX("Value \"4294967296\" is out of \"max-elements\" bounds.", "Line number 1.");
Radek Krejcidf6cad12018-11-28 17:10:55 +0100515
Radek Krejci05b13982018-11-28 16:22:07 +0100516 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200517 TEST_MINMAX_SUCCESS(" 1; ...", YCTX, LYS_SET_MAX, 1);
Radek Krejci05b13982018-11-28 16:22:07 +0100518
519 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200520 TEST_MINMAX_SUCCESS(" unbounded; ...", YCTX, LYS_SET_MAX, 0);
Radek Krejci05b13982018-11-28 16:22:07 +0100521
522 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200523 TEST_MINMAX_SUCCESS(" 1 {m:ext;} ...", YCTX, LYS_SET_MAX, 1);
Radek Krejci05b13982018-11-28 16:22:07 +0100524 assert_non_null(ext);
Radek Iša56ca9e42020-09-08 18:42:00 +0200525 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, ext, lysp_ext_instance_free);
Radek Krejci05b13982018-11-28 16:22:07 +0100526 ext = NULL;
527
528 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200529 in.current = " 1 {config true;} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +0200530 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &in, &value, &flags, &ext));
531 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"max-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100532}
533
Radek Krejci9fcacc12018-10-11 15:59:11 +0200534static struct lysp_module *
Michal Vaskob36053d2020-03-26 15:49:30 +0100535mod_renew(struct lys_yang_parser_ctx *ctx)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200536{
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200537 struct ly_ctx *ly_ctx = ctx->parsed_mod->mod->ctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100538
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200539 lys_module_free(ctx->parsed_mod->mod, NULL);
540 ctx->parsed_mod = calloc(1, sizeof *ctx->parsed_mod);
541 ctx->parsed_mod->mod = calloc(1, sizeof *ctx->parsed_mod->mod);
542 ctx->parsed_mod->mod->parsed = ctx->parsed_mod;
543 ctx->parsed_mod->mod->ctx = ly_ctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100544
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200545 return ctx->parsed_mod;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100546}
547
548static struct lysp_submodule *
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200549submod_renew(struct lys_yang_parser_ctx *ctx)
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100550{
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200551 struct ly_ctx *ly_ctx = ctx->parsed_mod->mod->ctx;
552
553 lys_module_free(ctx->parsed_mod->mod, NULL);
554 ctx->parsed_mod = calloc(1, sizeof(struct lysp_submodule));
555 ctx->parsed_mod->mod = calloc(1, sizeof *ctx->parsed_mod->mod);
556 lydict_insert(ly_ctx, "name", 0, &ctx->parsed_mod->mod->name);
557 ctx->parsed_mod->mod->parsed = ctx->parsed_mod;
558 ctx->parsed_mod->mod->ctx = ly_ctx;
559
560 return (struct lysp_submodule *)ctx->parsed_mod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200561}
562
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100563static LY_ERR
564test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
565 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
566 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
Radek Krejcid33273d2018-10-25 14:55:52 +0200567{
568 *module_data = user_data;
569 *format = LYS_IN_YANG;
570 *free_module_data = NULL;
571 return LY_SUCCESS;
572}
573
Radek Krejci9fcacc12018-10-11 15:59:11 +0200574static void
575test_module(void **state)
576{
Radek Krejci40544fa2019-01-11 09:38:37 +0100577 struct lysp_module *mod = NULL;
578 struct lysp_submodule *submod = NULL;
579 struct lys_module *m;
Michal Vasko63f3d842020-07-08 10:10:14 +0200580 struct ly_in in = {0};
Michal Vasko405cc9e2020-12-01 12:01:27 +0100581 struct lys_glob_unres unres = {0};
Radek Iša56ca9e42020-09-08 18:42:00 +0200582 struct lys_yang_parser_ctx *ctx_p;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200583
Radek Iša56ca9e42020-09-08 18:42:00 +0200584 mod = mod_renew(YCTX);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200585
586 /* missing mandatory substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +0200587 in.current = " name {}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200588 assert_int_equal(LY_EVALID, parse_module(YCTX, &in, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100589 assert_string_equal("name", mod->mod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +0200590 CHECK_LOG_CTX("Missing mandatory keyword \"namespace\" as a child of \"module\".", "Line number 1.");
Radek Krejci9fcacc12018-10-11 15:59:11 +0200591
Radek Iša56ca9e42020-09-08 18:42:00 +0200592 mod = mod_renew(YCTX);
Michal Vasko63f3d842020-07-08 10:10:14 +0200593 in.current = " name {namespace urn:x;}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200594 assert_int_equal(LY_EVALID, parse_module(YCTX, &in, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100595 assert_string_equal("urn:x", mod->mod->ns);
Radek Iša56ca9e42020-09-08 18:42:00 +0200596 CHECK_LOG_CTX("Missing mandatory keyword \"prefix\" as a child of \"module\".", "Line number 1.");
597 mod = mod_renew(YCTX);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200598
Michal Vasko63f3d842020-07-08 10:10:14 +0200599 in.current = " name {namespace urn:x;prefix \"x\";}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200600 assert_int_equal(LY_SUCCESS, parse_module(YCTX, &in, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100601 assert_string_equal("x", mod->mod->prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200602 mod = mod_renew(YCTX);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200603
Radek Krejci027d5802018-11-14 16:57:28 +0100604#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
605#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200606#define TEST_NODE(NODETYPE, INPUT, NAME) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200607 in.current = SCHEMA_BEGINNING INPUT; \
Radek Iša56ca9e42020-09-08 18:42:00 +0200608 assert_int_equal(LY_SUCCESS, parse_module(YCTX, &in, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200609 assert_non_null(mod->data); \
610 assert_int_equal(NODETYPE, mod->data->nodetype); \
611 assert_string_equal(NAME, mod->data->name); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200612 mod = mod_renew(YCTX);
Radek Krejcia042ea12018-10-13 07:52:15 +0200613#define TEST_GENERIC(INPUT, TARGET, TEST) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200614 in.current = SCHEMA_BEGINNING INPUT; \
Radek Iša56ca9e42020-09-08 18:42:00 +0200615 assert_int_equal(LY_SUCCESS, parse_module(YCTX, &in, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200616 assert_non_null(TARGET); \
617 TEST; \
Radek Iša56ca9e42020-09-08 18:42:00 +0200618 mod = mod_renew(YCTX);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100619#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200620 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
Radek Iša56ca9e42020-09-08 18:42:00 +0200621 parse_module, mod, LINE, mod = mod_renew(YCTX))
Radek Krejcia042ea12018-10-13 07:52:15 +0200622
623 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100624 TEST_DUP("namespace", "y", "z", "1");
625 TEST_DUP("prefix", "y", "z", "1");
626 TEST_DUP("contact", "a", "b", "1");
627 TEST_DUP("description", "a", "b", "1");
628 TEST_DUP("organization", "a", "b", "1");
629 TEST_DUP("reference", "a", "b", "1");
Radek Krejcia042ea12018-10-13 07:52:15 +0200630
Radek Krejci70853c52018-10-15 14:46:16 +0200631 /* not allowed in module (submodule-specific) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200632 in.current = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200633 assert_int_equal(LY_EVALID, parse_module(YCTX, &in, mod));
634 CHECK_LOG_CTX("Invalid keyword \"belongs-to\" as a child of \"module\".", "Line number 1.");
635 mod = mod_renew(YCTX);
Radek Krejci70853c52018-10-15 14:46:16 +0200636
Radek Krejcia042ea12018-10-13 07:52:15 +0200637 /* anydata */
638 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
639 /* anyxml */
640 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
641 /* augment */
642 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100643 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200644 /* choice */
645 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
646 /* contact 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100647 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100648 assert_string_equal("firstname surname", mod->mod->contact));
Radek Krejcia042ea12018-10-13 07:52:15 +0200649 /* container */
650 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
651 /* description 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100652 TEST_GENERIC("description \'some description\';}", mod->mod->dsc,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100653 assert_string_equal("some description", mod->mod->dsc));
Radek Krejcia042ea12018-10-13 07:52:15 +0200654 /* deviation */
655 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100656 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200657 /* extension */
658 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100659 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200660 /* feature */
661 TEST_GENERIC("feature test;}", mod->features,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100662 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200663 /* grouping */
664 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100665 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200666 /* identity */
667 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100668 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200669 /* import */
Radek Iša56ca9e42020-09-08 18:42:00 +0200670 ly_ctx_set_module_imp_clb(YCTX->parsed_mod->mod->ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
Radek Krejci086c7132018-10-26 15:29:04 +0200671 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100672 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200673
Radek Krejcia042ea12018-10-13 07:52:15 +0200674 /* import - prefix collision */
Michal Vasko63f3d842020-07-08 10:10:14 +0200675 in.current = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200676 assert_int_equal(LY_EVALID, parse_module(YCTX, &in, mod));
677 CHECK_LOG_CTX("Prefix \"x\" already used as module prefix.", "Line number 2.");
678 mod = mod_renew(YCTX);
679
Michal Vasko63f3d842020-07-08 10:10:14 +0200680 in.current = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200681 assert_int_equal(LY_EVALID, parse_module(YCTX, &in, mod));
682 CHECK_LOG_CTX("Prefix \"y\" already used to import \"zzz\" module.", "Line number 2.");
683
684 mod = mod_renew(YCTX);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200685 in.current = "module name10 {yang-version 1.1;namespace urn:x;prefix \"x\";import zzz {prefix y;}import zzz {prefix z;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200686 assert_int_equal(lys_parse_mem(YCTX->parsed_mod->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_SUCCESS);
687 CHECK_LOG_CTX("Single revision of the module \"zzz\" imported twice.", NULL);
Radek Krejci70853c52018-10-15 14:46:16 +0200688
Radek Krejcia042ea12018-10-13 07:52:15 +0200689 /* include */
Radek Iša56ca9e42020-09-08 18:42:00 +0200690 ly_ctx_set_module_imp_clb(YCTX->parsed_mod->mod->ctx, test_imp_clb, "module xxx { namespace urn:xxx; prefix x;}");
Michal Vasko63f3d842020-07-08 10:10:14 +0200691 in.current = "module" SCHEMA_BEGINNING "include xxx;}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200692 assert_int_equal(lys_parse_mem(YCTX->parsed_mod->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_EVALID);
693 CHECK_LOG_CTX("Including \"xxx\" submodule into \"name\" failed.", NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200694
Radek Iša56ca9e42020-09-08 18:42:00 +0200695 ly_ctx_set_module_imp_clb(YCTX->parsed_mod->mod->ctx, test_imp_clb, "submodule xxx {belongs-to wrong-name {prefix w;}}");
Michal Vasko63f3d842020-07-08 10:10:14 +0200696 in.current = "module" SCHEMA_BEGINNING "include xxx;}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200697 assert_int_equal(lys_parse_mem(YCTX->parsed_mod->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_EVALID);
698 CHECK_LOG_CTX("Including \"xxx\" submodule into \"name\" failed.", NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200699
Radek Iša56ca9e42020-09-08 18:42:00 +0200700 ly_ctx_set_module_imp_clb(YCTX->parsed_mod->mod->ctx, test_imp_clb, "submodule xxx {belongs-to name {prefix x;}}");
Radek Krejcid33273d2018-10-25 14:55:52 +0200701 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100702 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +0200703
Radek Krejcia042ea12018-10-13 07:52:15 +0200704 /* leaf */
705 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
706 /* leaf-list */
707 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
708 /* list */
709 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
710 /* notification */
711 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100712 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200713 /* organization 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100714 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100715 assert_string_equal("CESNET a.l.e.", mod->mod->org));
Radek Krejcia042ea12018-10-13 07:52:15 +0200716 /* reference 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100717 TEST_GENERIC("reference RFC7950;}", mod->mod->ref,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100718 assert_string_equal("RFC7950", mod->mod->ref));
Radek Krejcia042ea12018-10-13 07:52:15 +0200719 /* revision */
720 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100721 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +0200722 /* rpc */
723 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100724 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200725 /* typedef */
726 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100727 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200728 /* uses */
729 TEST_NODE(LYS_USES, "uses test;}", "test");
730 /* yang-version */
Michal Vasko63f3d842020-07-08 10:10:14 +0200731 in.current = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200732 assert_int_equal(LY_EVALID, parse_module(YCTX, &in, mod));
733 CHECK_LOG_CTX("Invalid value \"10\" of \"yang-version\".", "Line number 3.");
734 mod = mod_renew(YCTX);
Radek Krejci96e48da2020-09-04 13:18:06 +0200735 in.current = SCHEMA_BEGINNING2 "yang-version 1;yang-version 1.1;}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200736 assert_int_equal(LY_EVALID, parse_module(YCTX, &in, mod));
737 CHECK_LOG_CTX("Duplicate keyword \"yang-version\".", "Line number 3.");
738 mod = mod_renew(YCTX);
Radek Krejci96e48da2020-09-04 13:18:06 +0200739 in.current = SCHEMA_BEGINNING2 "yang-version 1;}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200740 assert_int_equal(LY_SUCCESS, parse_module(YCTX, &in, mod));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200741 assert_int_equal(1, mod->version);
Radek Iša56ca9e42020-09-08 18:42:00 +0200742 mod = mod_renew(YCTX);
Michal Vasko63f3d842020-07-08 10:10:14 +0200743 in.current = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200744 assert_int_equal(LY_SUCCESS, parse_module(YCTX, &in, mod));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200745 assert_int_equal(2, mod->version);
Radek Iša56ca9e42020-09-08 18:42:00 +0200746 mod = mod_renew(YCTX);
Radek Krejci40544fa2019-01-11 09:38:37 +0100747
Michal Vasko63f3d842020-07-08 10:10:14 +0200748 in.current = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200749 m = calloc(1, sizeof *m);
Radek Iša56ca9e42020-09-08 18:42:00 +0200750 m->ctx = YCTX->parsed_mod->mod->ctx;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100751 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m, &unres));
Radek Iša56ca9e42020-09-08 18:42:00 +0200752 CHECK_LOG_CTX("Trailing garbage \"module q {names...\" after module, expected end-of-input.", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100753 yang_parser_ctx_free(ctx_p);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200754 lys_module_free(m, NULL);
Radek Krejci40544fa2019-01-11 09:38:37 +0100755
Michal Vasko63f3d842020-07-08 10:10:14 +0200756 in.current = "prefix " SCHEMA_BEGINNING "}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200757 m = calloc(1, sizeof *m);
Radek Iša56ca9e42020-09-08 18:42:00 +0200758 m->ctx = YCTX->parsed_mod->mod->ctx;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100759 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m, &unres));
Radek Iša56ca9e42020-09-08 18:42:00 +0200760 CHECK_LOG_CTX("Invalid keyword \"prefix\", expected \"module\" or \"submodule\".", "Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200761 yang_parser_ctx_free(ctx_p);
762 lys_module_free(m, NULL);
Radek Krejci09306362018-10-15 15:26:01 +0200763
Michal Vasko63f3d842020-07-08 10:10:14 +0200764 in.current = "module " SCHEMA_BEGINNING "leaf enum {type enumeration {enum seven { position 7;}}}}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200765 m = calloc(1, sizeof *m);
Radek Iša56ca9e42020-09-08 18:42:00 +0200766 m->ctx = YCTX->parsed_mod->mod->ctx;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100767 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m, &unres));
Radek Iša56ca9e42020-09-08 18:42:00 +0200768 CHECK_LOG_CTX("Invalid keyword \"position\" as a child of \"enum\".", "Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200769 yang_parser_ctx_free(ctx_p);
770 lys_module_free(m, NULL);
David Sedlák9fb515f2019-07-11 10:33:58 +0200771
Radek Krejci156ccaf2018-10-15 15:49:17 +0200772 /* extensions */
773 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100774 assert_string_equal("prefix:test", mod->exts[0].name);
775 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
Radek Iša56ca9e42020-09-08 18:42:00 +0200776 mod = mod_renew(YCTX);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200777
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200778 /* invalid substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +0200779 in.current = SCHEMA_BEGINNING "must false;}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200780 assert_int_equal(LY_EVALID, parse_module(YCTX, &in, mod));
781 CHECK_LOG_CTX("Invalid keyword \"must\" as a child of \"module\".", "Line number 3.");
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200782
Radek Krejci09306362018-10-15 15:26:01 +0200783 /* submodule */
Radek Iša56ca9e42020-09-08 18:42:00 +0200784 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200785
786 /* missing mandatory substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +0200787 in.current = " subname {}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200788 assert_int_equal(LY_EVALID, parse_submodule(YCTX, &in, submod));
789 CHECK_LOG_CTX("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\".", "Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100790 assert_string_equal("subname", submod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +0200791
792 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200793
Michal Vasko63f3d842020-07-08 10:10:14 +0200794 in.current = " subname {belongs-to name {prefix x;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200795 assert_int_equal(LY_SUCCESS, parse_submodule(YCTX, &in, submod));
Michal Vaskoc3781c32020-10-06 14:04:08 +0200796 assert_string_equal("name", submod->mod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +0200797 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200798
799#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +0100800#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +0200801
802 /* duplicated namespace, prefix */
Michal Vasko63f3d842020-07-08 10:10:14 +0200803 in.current = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +0200804 assert_int_equal(LY_EVALID, parse_submodule(YCTX, &in, submod));
805 CHECK_LOG_CTX("Duplicate keyword \"belongs-to\".", "Line number 3.");
806 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200807
808 /* not allowed in submodule (module-specific) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200809 in.current = SCHEMA_BEGINNING "namespace \"urn:z\";}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200810 assert_int_equal(LY_EVALID, parse_submodule(YCTX, &in, submod));
811 CHECK_LOG_CTX("Invalid keyword \"namespace\" as a child of \"submodule\".", "Line number 3.");
812 submod = submod_renew(YCTX);
Michal Vasko63f3d842020-07-08 10:10:14 +0200813 in.current = SCHEMA_BEGINNING "prefix m;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200814 assert_int_equal(LY_EVALID, parse_submodule(YCTX, &in, submod));
815 CHECK_LOG_CTX("Invalid keyword \"prefix\" as a child of \"submodule\".", "Line number 3.");
816 submod = submod_renew(YCTX);
Radek Krejcia042ea12018-10-13 07:52:15 +0200817
Michal Vasko63f3d842020-07-08 10:10:14 +0200818 in.current = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200819 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, YCTX->parsed_mod->mod->ctx, (struct lys_parser_ctx *)YCTX, &in, &submod));
820 CHECK_LOG_CTX("Trailing garbage \"module q {names...\" after submodule, expected end-of-input.", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100821 yang_parser_ctx_free(ctx_p);
Radek Krejci40544fa2019-01-11 09:38:37 +0100822
Michal Vasko63f3d842020-07-08 10:10:14 +0200823 in.current = "prefix " SCHEMA_BEGINNING "}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200824 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, YCTX->parsed_mod->mod->ctx, (struct lys_parser_ctx *)YCTX, &in, &submod));
825 CHECK_LOG_CTX("Invalid keyword \"prefix\", expected \"module\" or \"submodule\".", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100826 yang_parser_ctx_free(ctx_p);
Radek Iša56ca9e42020-09-08 18:42:00 +0200827 submod = submod_renew(YCTX);
Radek Krejci40544fa2019-01-11 09:38:37 +0100828
Radek Krejcia042ea12018-10-13 07:52:15 +0200829#undef TEST_GENERIC
830#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200831#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +0200832#undef SCHEMA_BEGINNING
Radek Krejciefd22f62018-09-27 11:47:58 +0200833}
834
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200835static void
836test_deviation(void **state)
837{
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200838 struct lysp_deviation *d = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200839 struct ly_in in = {0};
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200840
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200841 /* invalid cardinality */
842#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
843 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Iša56ca9e42020-09-08 18:42:00 +0200844 &d, "1", FREE_ARRAY(YCTX->parsed_mod->mod->ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200845
846 TEST_DUP("description", "a", "b");
847 TEST_DUP("reference", "a", "b");
848
849 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +0200850 in.current = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +0200851 assert_int_equal(LY_SUCCESS, parse_deviation(YCTX, &in, &d));
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200852 assert_non_null(d);
Michal Vasko63f3d842020-07-08 10:10:14 +0200853 assert_string_equal(" ...", in.current);
Radek Iša56ca9e42020-09-08 18:42:00 +0200854 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200855 d = NULL;
856
857 /* missing mandatory substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +0200858 in.current = " test {description text;}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200859 assert_int_equal(LY_EVALID, parse_deviation(YCTX, &in, &d));
860 CHECK_LOG_CTX("Missing mandatory keyword \"deviate\" as a child of \"deviation\".", "Line number 1.");
861 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200862 d = NULL;
863
864 /* invalid substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +0200865 in.current = " test {deviate not-supported; status obsolete;}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200866 assert_int_equal(LY_EVALID, parse_deviation(YCTX, &in, &d));
867 CHECK_LOG_CTX("Invalid keyword \"status\" as a child of \"deviation\".", "Line number 1.");
868 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200869 d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200870#undef TEST_DUP
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200871}
872
Radek Iša56ca9e42020-09-08 18:42:00 +0200873#define TEST_DEVIATE_SUCCESS(INPUT_TEXT, REMAIN_TEXT)\
874 in.current = INPUT_TEXT;\
875 assert_int_equal(LY_SUCCESS, parse_deviate(YCTX, &in, &d));\
876 assert_non_null(d);\
877 assert_string_equal(REMAIN_TEXT, in.current);\
878 lysp_deviate_free(YCTX->parsed_mod->mod->ctx, d); free(d); d = NULL
879
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200880static void
881test_deviate(void **state)
882{
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200883 struct lysp_deviate *d = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200884 struct ly_in in = {0};
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200885
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200886 /* invalid cardinality */
887#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
888 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Iša56ca9e42020-09-08 18:42:00 +0200889 &d, "1", lysp_deviate_free(YCTX->parsed_mod->mod->ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200890
891 TEST_DUP("add", "config", "true", "false");
892 TEST_DUP("replace", "default", "int8", "uint8");
893 TEST_DUP("add", "mandatory", "true", "false");
894 TEST_DUP("add", "max-elements", "1", "2");
895 TEST_DUP("add", "min-elements", "1", "2");
896 TEST_DUP("replace", "type", "int8", "uint8");
897 TEST_DUP("add", "units", "kilometers", "miles");
898
899 /* full contents */
Radek Iša56ca9e42020-09-08 18:42:00 +0200900 TEST_DEVIATE_SUCCESS(" not-supported {prefix:ext;} ...", " ...");
901 TEST_DEVIATE_SUCCESS(" add {units meters; must 1; must 2; unique x; unique y; default a; default b; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...", " ...");
902 TEST_DEVIATE_SUCCESS(" delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...", " ...");
903 TEST_DEVIATE_SUCCESS(" replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...", " ...");
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200904
905 /* invalid substatements */
906#define TEST_NOT_SUP(DEV, STMT, VALUE) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200907 in.current = " "DEV" {"STMT" "VALUE";}..."; \
Radek Iša56ca9e42020-09-08 18:42:00 +0200908 assert_int_equal(LY_EVALID, parse_deviate(YCTX, &in, &d)); \
909 CHECK_LOG_CTX("Deviate \""DEV"\" does not support keyword \""STMT"\".", "Line number 1.");\
910 lysp_deviate_free(YCTX->parsed_mod->mod->ctx, d); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200911
912 TEST_NOT_SUP("not-supported", "units", "meters");
913 TEST_NOT_SUP("not-supported", "must", "1");
914 TEST_NOT_SUP("not-supported", "unique", "x");
915 TEST_NOT_SUP("not-supported", "default", "a");
916 TEST_NOT_SUP("not-supported", "config", "true");
917 TEST_NOT_SUP("not-supported", "mandatory", "true");
918 TEST_NOT_SUP("not-supported", "min-elements", "1");
919 TEST_NOT_SUP("not-supported", "max-elements", "2");
920 TEST_NOT_SUP("not-supported", "type", "string");
921 TEST_NOT_SUP("add", "type", "string");
922 TEST_NOT_SUP("delete", "config", "true");
923 TEST_NOT_SUP("delete", "mandatory", "true");
924 TEST_NOT_SUP("delete", "min-elements", "1");
925 TEST_NOT_SUP("delete", "max-elements", "2");
926 TEST_NOT_SUP("delete", "type", "string");
927 TEST_NOT_SUP("replace", "must", "1");
928 TEST_NOT_SUP("replace", "unique", "a");
929
Michal Vasko63f3d842020-07-08 10:10:14 +0200930 in.current = " nonsence; ...";
Radek Iša56ca9e42020-09-08 18:42:00 +0200931 assert_int_equal(LY_EVALID, parse_deviate(YCTX, &in, &d));
932 CHECK_LOG_CTX("Invalid value \"nonsence\" of \"deviate\".", "Line number 1.");\
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200933 assert_null(d);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200934#undef TEST_NOT_SUP
935#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200936}
937
Radek Krejci8c370832018-11-02 15:10:03 +0100938static void
939test_container(void **state)
940{
Radek Krejci8c370832018-11-02 15:10:03 +0100941 struct lysp_node_container *c = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200942 struct ly_in in = {0};
Radek Krejci8c370832018-11-02 15:10:03 +0100943
Radek Iša56ca9e42020-09-08 18:42:00 +0200944 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +0100945
946 /* invalid cardinality */
947#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200948 in.current = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Iša56ca9e42020-09-08 18:42:00 +0200949 assert_int_equal(LY_EVALID, parse_container(YCTX, &in, NULL, (struct lysp_node**)&c)); \
950 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
951 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +0100952
953 TEST_DUP("config", "true", "false");
954 TEST_DUP("description", "text1", "text2");
955 TEST_DUP("presence", "true", "false");
956 TEST_DUP("reference", "1", "2");
957 TEST_DUP("status", "current", "obsolete");
958 TEST_DUP("when", "true", "false");
959#undef TEST_DUP
960
961 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +0200962 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 Krejcib4ac5a92020-11-23 17:54:33 +0100963 "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 Iša56ca9e42020-09-08 18:42:00 +0200964 assert_int_equal(LY_SUCCESS, parse_container(YCTX, &in, NULL, (struct lysp_node **)&c));
965 CHECK_LYSP_NODE(c, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR, 1, "cont", 0, LYS_CONTAINER, 0, "test", 1);
Radek Krejci8c370832018-11-02 15:10:03 +0100966 assert_non_null(c->actions);
967 assert_non_null(c->child);
Radek Krejci8c370832018-11-02 15:10:03 +0100968 assert_non_null(c->groupings);
Radek Krejci8c370832018-11-02 15:10:03 +0100969 assert_non_null(c->musts);
970 assert_non_null(c->notifs);
971 assert_string_equal("true", c->presence);
Radek Krejci8c370832018-11-02 15:10:03 +0100972 assert_non_null(c->typedefs);
Radek Iša56ca9e42020-09-08 18:42:00 +0200973 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
974 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +0100975
976 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +0200977 in.current = " cont {augment /root;} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +0200978 assert_int_equal(LY_EVALID, parse_container(YCTX, &in, NULL, (struct lysp_node **)&c));
979 CHECK_LOG_CTX("Invalid keyword \"augment\" as a child of \"container\".", "Line number 1.");
980 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c); c = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200981 in.current = " cont {nonsence true;} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +0200982 assert_int_equal(LY_EVALID, parse_container(YCTX, &in, NULL, (struct lysp_node **)&c));
983 CHECK_LOG_CTX("Invalid character sequence \"nonsence\", expected a keyword.", "Line number 1.");
984 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +0100985
Radek Iša56ca9e42020-09-08 18:42:00 +0200986 YCTX->parsed_mod->version = 1; /* simulate YANG 1.0 */
Michal Vasko63f3d842020-07-08 10:10:14 +0200987 in.current = " cont {action x;} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +0200988 assert_int_equal(LY_EVALID, parse_container(YCTX, &in, NULL, (struct lysp_node **)&c));
989 CHECK_LOG_CTX("Invalid keyword \"action\" as a child of \"container\" - "
990 "the statement is allowed only in YANG 1.1 modules.", "Line number 1.");
991 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +0100992}
993
Radek Krejcib1a5dcc2018-11-26 14:50:05 +0100994static void
995test_leaf(void **state)
996{
Radek Krejcib1a5dcc2018-11-26 14:50:05 +0100997 struct lysp_node_leaf *l = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200998 struct ly_in in = {0};
Radek Krejcib1a5dcc2018-11-26 14:50:05 +0100999
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001000 /* invalid cardinality */
1001#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001002 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Iša56ca9e42020-09-08 18:42:00 +02001003 assert_int_equal(LY_EVALID, parse_leaf(YCTX, &in, NULL, (struct lysp_node**)&l)); \
1004 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1005 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001006
1007 TEST_DUP("config", "true", "false");
1008 TEST_DUP("default", "x", "y");
1009 TEST_DUP("description", "text1", "text2");
1010 TEST_DUP("mandatory", "true", "false");
1011 TEST_DUP("reference", "1", "2");
1012 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001013 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001014 TEST_DUP("units", "text1", "text2");
1015 TEST_DUP("when", "true", "false");
1016#undef TEST_DUP
1017
1018 /* full content - without mandatory which is mutual exclusive with default */
Michal Vasko63f3d842020-07-08 10:10:14 +02001019 in.current = "l {config false;default \"xxx\";description test;if-feature f;"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001020 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +02001021 assert_int_equal(LY_SUCCESS, parse_leaf(YCTX, &in, NULL, (struct lysp_node **)&l));
1022 CHECK_LYSP_NODE(l, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR, 1, "l", 0, LYS_LEAF, 0, "test", 1);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001023 assert_string_equal("xxx", l->dflt.str);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001024 assert_string_equal("yyy", l->units);
1025 assert_string_equal("string", l->type.name);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001026 assert_non_null(l->musts);
Radek Iša56ca9e42020-09-08 18:42:00 +02001027 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001028
1029 /* full content - now with mandatory */
Michal Vasko63f3d842020-07-08 10:10:14 +02001030 in.current = "l {mandatory true; type string;} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +02001031 assert_int_equal(LY_SUCCESS, parse_leaf(YCTX, &in, NULL, (struct lysp_node **)&l));
1032 CHECK_LYSP_NODE(l, NULL, 0, LYS_MAND_TRUE, 0, "l", 0, LYS_LEAF, 0, NULL, 0);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001033 assert_string_equal("string", l->type.name);
Radek Iša56ca9e42020-09-08 18:42:00 +02001034 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001035
1036 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001037 in.current = " l {description \"missing type\";} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +02001038 assert_int_equal(LY_EVALID, parse_leaf(YCTX, &in, NULL, (struct lysp_node **)&l));
1039 CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"leaf\".", "Line number 1.");
1040 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001041}
1042
Radek Krejci0e5d8382018-11-28 16:37:53 +01001043static void
1044test_leaflist(void **state)
1045{
Radek Krejci0e5d8382018-11-28 16:37:53 +01001046 struct lysp_node_leaflist *ll = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001047 struct ly_in in = {0};
Radek Krejci0e5d8382018-11-28 16:37:53 +01001048
Radek Iša56ca9e42020-09-08 18:42:00 +02001049 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001050
1051 /* invalid cardinality */
1052#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001053 in.current = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Iša56ca9e42020-09-08 18:42:00 +02001054 assert_int_equal(LY_EVALID, parse_leaflist(YCTX, &in, NULL, (struct lysp_node**)&ll)); \
1055 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1056 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001057
1058 TEST_DUP("config", "true", "false");
1059 TEST_DUP("description", "text1", "text2");
1060 TEST_DUP("max-elements", "10", "20");
1061 TEST_DUP("min-elements", "10", "20");
1062 TEST_DUP("ordered-by", "user", "system");
1063 TEST_DUP("reference", "1", "2");
1064 TEST_DUP("status", "current", "obsolete");
1065 TEST_DUP("type", "int8", "uint8");
1066 TEST_DUP("units", "text1", "text2");
1067 TEST_DUP("when", "true", "false");
1068#undef TEST_DUP
1069
1070 /* full content - without min-elements which is mutual exclusive with default */
Michal Vasko63f3d842020-07-08 10:10:14 +02001071 in.current = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001072 "max-elements 10;must 'expr';ordered-by user;reference test;"
1073 "status current;type string; units zzz;when true;m:ext;} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +02001074 assert_int_equal(LY_SUCCESS, parse_leaflist(YCTX, &in, NULL, (struct lysp_node **)&ll));
1075 CHECK_LYSP_NODE(ll, "test", 1, 0x446, 1, "ll", 0, LYS_LEAFLIST, 0, "test", 1);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001076 assert_non_null(ll->dflts);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001077 assert_int_equal(2, LY_ARRAY_COUNT(ll->dflts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001078 assert_string_equal("xxx", ll->dflts[0].str);
1079 assert_string_equal("yyy", ll->dflts[1].str);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001080 assert_string_equal("zzz", ll->units);
1081 assert_int_equal(10, ll->max);
1082 assert_int_equal(0, ll->min);
1083 assert_string_equal("string", ll->type.name);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001084 assert_non_null(ll->musts);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001085 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
Radek Iša56ca9e42020-09-08 18:42:00 +02001086 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001087
1088 /* full content - now with min-elements */
Michal Vasko63f3d842020-07-08 10:10:14 +02001089 in.current = "ll {min-elements 10; type string;} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +02001090 assert_int_equal(LY_SUCCESS, parse_leaflist(YCTX, &in, NULL, (struct lysp_node **)&ll));
1091 CHECK_LYSP_NODE(ll, NULL, 0, 0x200, 0, "ll", 0, LYS_LEAFLIST, 0, NULL, 0);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001092 assert_string_equal("string", ll->type.name);
1093 assert_int_equal(0, ll->max);
1094 assert_int_equal(10, ll->min);
1095 assert_int_equal(LYS_SET_MIN, ll->flags);
Radek Iša56ca9e42020-09-08 18:42:00 +02001096 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001097
1098 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001099 in.current = " ll {description \"missing type\";} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +02001100 assert_int_equal(LY_EVALID, parse_leaflist(YCTX, &in, NULL, (struct lysp_node **)&ll));
1101 CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"leaf-list\".", "Line number 1.");
1102 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001103
Radek Iša56ca9e42020-09-08 18:42:00 +02001104 YCTX->parsed_mod->version = 1; /* simulate YANG 1.0 - default statement is not allowed */
Michal Vasko63f3d842020-07-08 10:10:14 +02001105 in.current = " ll {default xx; type string;} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +02001106 assert_int_equal(LY_EVALID, parse_leaflist(YCTX, &in, NULL, (struct lysp_node **)&ll));
1107 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.");
1108 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001109}
1110
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001111static void
1112test_list(void **state)
1113{
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001114 struct lysp_node_list *l = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001115 struct ly_in in = {0};
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001116
Radek Iša56ca9e42020-09-08 18:42:00 +02001117 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001118
1119 /* invalid cardinality */
1120#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001121 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Iša56ca9e42020-09-08 18:42:00 +02001122 assert_int_equal(LY_EVALID, parse_list(YCTX, &in, NULL, (struct lysp_node**)&l)); \
1123 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1124 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001125
1126 TEST_DUP("config", "true", "false");
1127 TEST_DUP("description", "text1", "text2");
1128 TEST_DUP("key", "one", "two");
1129 TEST_DUP("max-elements", "10", "20");
1130 TEST_DUP("min-elements", "10", "20");
1131 TEST_DUP("ordered-by", "user", "system");
1132 TEST_DUP("reference", "1", "2");
1133 TEST_DUP("status", "current", "obsolete");
1134 TEST_DUP("when", "true", "false");
1135#undef TEST_DUP
1136
1137 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001138 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 Krejcib4ac5a92020-11-23 17:54:33 +01001139 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1140 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +02001141 assert_int_equal(LY_SUCCESS, parse_list(YCTX, &in, NULL, (struct lysp_node **)&l));
1142 CHECK_LYSP_NODE(l, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, 1, "l",
1143 0, LYS_LIST, 0, "test", 1);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001144 assert_string_equal("l", l->key);
1145 assert_non_null(l->uniques);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001146 assert_int_equal(2, LY_ARRAY_COUNT(l->uniques));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001147 assert_string_equal("xxx", l->uniques[0].str);
1148 assert_string_equal("yyy", l->uniques[1].str);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001149 assert_int_equal(10, l->max);
1150 assert_int_equal(1, l->min);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001151 assert_non_null(l->musts);
Radek Iša56ca9e42020-09-08 18:42:00 +02001152 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
1153 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001154
Radek Krejcif538ce52019-03-05 10:46:14 +01001155 /* invalid content */
Radek Iša56ca9e42020-09-08 18:42:00 +02001156 YCTX->parsed_mod->version = 1; /* simulate YANG 1.0 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001157 in.current = "l {action x;} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +02001158 assert_int_equal(LY_EVALID, parse_list(YCTX, &in, NULL, (struct lysp_node **)&l));
1159 CHECK_LOG_CTX("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules.", "Line number 1.");
1160 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001161}
1162
Radek Krejci056d0a82018-12-06 16:57:25 +01001163static void
1164test_choice(void **state)
1165{
Radek Krejci056d0a82018-12-06 16:57:25 +01001166 struct lysp_node_choice *ch = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001167 struct ly_in in = {0};
Radek Krejci056d0a82018-12-06 16:57:25 +01001168
Radek Iša56ca9e42020-09-08 18:42:00 +02001169 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci056d0a82018-12-06 16:57:25 +01001170
1171 /* invalid cardinality */
1172#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001173 in.current = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Iša56ca9e42020-09-08 18:42:00 +02001174 assert_int_equal(LY_EVALID, parse_choice(YCTX, &in, NULL, (struct lysp_node**)&ch)); \
1175 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1176 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)ch); ch = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001177
1178 TEST_DUP("config", "true", "false");
1179 TEST_DUP("default", "a", "b");
1180 TEST_DUP("description", "text1", "text2");
1181 TEST_DUP("mandatory", "true", "false");
1182 TEST_DUP("reference", "1", "2");
1183 TEST_DUP("status", "current", "obsolete");
1184 TEST_DUP("when", "true", "false");
1185#undef TEST_DUP
1186
Radek Krejcia9026eb2018-12-12 16:04:47 +01001187 /* full content - without default due to a collision with mandatory */
Michal Vasko63f3d842020-07-08 10:10:14 +02001188 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 Krejcib4ac5a92020-11-23 17:54:33 +01001189 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +02001190 assert_int_equal(LY_SUCCESS, parse_choice(YCTX, &in, NULL, (struct lysp_node **)&ch));
1191 CHECK_LYSP_NODE(ch, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, 1, "ch", 0, LYS_CHOICE, 0, "test", 1);
1192 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ch); ch = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001193
Radek Krejcia9026eb2018-12-12 16:04:47 +01001194 /* full content - the default missing from the previous node */
Michal Vasko63f3d842020-07-08 10:10:14 +02001195 in.current = "ch {default c;case c;} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +02001196 assert_int_equal(LY_SUCCESS, parse_choice(YCTX, &in, NULL, (struct lysp_node **)&ch));
1197 CHECK_LYSP_NODE(ch, NULL, 0, 0, 0, "ch", 0, LYS_CHOICE, 0, NULL, 0);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001198 assert_string_equal("c", ch->dflt.str);
Radek Iša56ca9e42020-09-08 18:42:00 +02001199 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ch); ch = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001200}
1201
1202static void
1203test_case(void **state)
1204{
Radek Krejcia9026eb2018-12-12 16:04:47 +01001205 struct lysp_node_case *cs = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001206 struct ly_in in = {0};
Radek Krejcia9026eb2018-12-12 16:04:47 +01001207
Radek Iša56ca9e42020-09-08 18:42:00 +02001208 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcia9026eb2018-12-12 16:04:47 +01001209
1210 /* invalid cardinality */
1211#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001212 in.current = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Iša56ca9e42020-09-08 18:42:00 +02001213 assert_int_equal(LY_EVALID, parse_case(YCTX, &in, NULL, (struct lysp_node**)&cs)); \
1214 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1215 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)cs); cs = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001216
1217 TEST_DUP("description", "text1", "text2");
1218 TEST_DUP("reference", "1", "2");
1219 TEST_DUP("status", "current", "obsolete");
1220 TEST_DUP("when", "true", "false");
1221#undef TEST_DUP
1222
1223 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001224 in.current = "cs {anydata any;anyxml anyxml; choice ch;container c;description test;if-feature f;leaf l {type string;}"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001225 "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +02001226 assert_int_equal(LY_SUCCESS, parse_case(YCTX, &in, NULL, (struct lysp_node **)&cs));
1227 CHECK_LYSP_NODE(cs, "test", 1, LYS_STATUS_CURR, 1, "cs", 0, LYS_CASE, 0, "test", 1);
1228 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)cs); cs = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001229
1230 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001231 in.current = "cs {config true} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +02001232 assert_int_equal(LY_EVALID, parse_case(YCTX, &in, NULL, (struct lysp_node **)&cs));
1233 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"case\".", "Line number 1.");
1234 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)cs); cs = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001235}
1236
Radek Krejci9800fb82018-12-13 14:26:23 +01001237static void
Radek Krejcid6b76452019-09-03 17:03:03 +02001238test_any(void **state, enum ly_stmt kw)
Radek Krejci9800fb82018-12-13 14:26:23 +01001239{
Radek Krejci9800fb82018-12-13 14:26:23 +01001240 struct lysp_node_anydata *any = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001241 struct ly_in in = {0};
Radek Krejci9800fb82018-12-13 14:26:23 +01001242
Radek Krejcid6b76452019-09-03 17:03:03 +02001243 if (kw == LY_STMT_ANYDATA) {
Radek Iša56ca9e42020-09-08 18:42:00 +02001244 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001245 } else {
Radek Iša56ca9e42020-09-08 18:42:00 +02001246 YCTX->parsed_mod->version = 1; /* simulate YANG 1.0 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001247 }
1248
1249 /* invalid cardinality */
1250#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001251 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Iša56ca9e42020-09-08 18:42:00 +02001252 assert_int_equal(LY_EVALID, parse_any(YCTX, &in, kw, NULL, (struct lysp_node**)&any)); \
1253 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1254 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)any); any = NULL;
Radek Krejci9800fb82018-12-13 14:26:23 +01001255
1256 TEST_DUP("config", "true", "false");
1257 TEST_DUP("description", "text1", "text2");
1258 TEST_DUP("mandatory", "true", "false");
1259 TEST_DUP("reference", "1", "2");
1260 TEST_DUP("status", "current", "obsolete");
1261 TEST_DUP("when", "true", "false");
1262#undef TEST_DUP
1263
1264 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001265 in.current = "any {config true;description test;if-feature f;mandatory true;must 'expr';reference test;status current;when true;m:ext;} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +02001266 assert_int_equal(LY_SUCCESS, parse_any(YCTX, &in, kw, NULL, (struct lysp_node **)&any));
1267 // CHECK_LYSP_NODE(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, NEXT, TYPE, PARENT, REF, WHEN)
1268 uint16_t node_type = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
1269 CHECK_LYSP_NODE(any, "test", 1, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, 1, "any", 0, node_type, 0, "test", 1);
Radek Krejci9800fb82018-12-13 14:26:23 +01001270 assert_non_null(any->musts);
Radek Iša56ca9e42020-09-08 18:42:00 +02001271 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)any); any = NULL;
Radek Krejci9800fb82018-12-13 14:26:23 +01001272}
1273
1274static void
1275test_anydata(void **state)
1276{
Radek Krejcid6b76452019-09-03 17:03:03 +02001277 return test_any(state, LY_STMT_ANYDATA);
Radek Krejci9800fb82018-12-13 14:26:23 +01001278}
1279
1280static void
1281test_anyxml(void **state)
1282{
Radek Krejcid6b76452019-09-03 17:03:03 +02001283 return test_any(state, LY_STMT_ANYXML);
Radek Krejci9800fb82018-12-13 14:26:23 +01001284}
1285
Radek Krejcie86bf772018-12-14 11:39:53 +01001286static void
1287test_grouping(void **state)
1288{
Radek Krejcie86bf772018-12-14 11:39:53 +01001289 struct lysp_grp *grp = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001290 struct ly_in in = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01001291
Radek Iša56ca9e42020-09-08 18:42:00 +02001292 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001293
1294 /* invalid cardinality */
1295#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001296 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Iša56ca9e42020-09-08 18:42:00 +02001297 assert_int_equal(LY_EVALID, parse_grouping(YCTX, &in, NULL, &grp)); \
1298 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1299 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, grp, lysp_grp_free); grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001300
1301 TEST_DUP("description", "text1", "text2");
1302 TEST_DUP("reference", "1", "2");
1303 TEST_DUP("status", "current", "obsolete");
1304#undef TEST_DUP
1305
1306 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001307 in.current = "grp {action x;anydata any;anyxml anyxml; choice ch;container c;description test;grouping g;leaf l {type string;}"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001308 "leaf-list ll {type string;} list li;notification not;reference test;status current;typedef t {type int8;}uses g;m:ext;} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +02001309 assert_int_equal(LY_SUCCESS, parse_grouping(YCTX, &in, NULL, &grp));
Radek Krejcie86bf772018-12-14 11:39:53 +01001310 assert_non_null(grp);
1311 assert_int_equal(LYS_GROUPING, grp->nodetype);
1312 assert_string_equal("grp", grp->name);
1313 assert_string_equal("test", grp->dsc);
1314 assert_non_null(grp->exts);
1315 assert_string_equal("test", grp->ref);
1316 assert_null(grp->parent);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001317 assert_int_equal(LYS_STATUS_CURR, grp->flags);
Radek Iša56ca9e42020-09-08 18:42:00 +02001318 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
1319 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, grp, lysp_grp_free); grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001320
1321 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001322 in.current = "grp {config true} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +02001323 assert_int_equal(LY_EVALID, parse_grouping(YCTX, &in, NULL, &grp));
1324 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"grouping\".", "Line number 1.");
1325 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, grp, lysp_grp_free); grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001326
Michal Vasko63f3d842020-07-08 10:10:14 +02001327 in.current = "grp {must 'expr'} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +02001328 assert_int_equal(LY_EVALID, parse_grouping(YCTX, &in, NULL, &grp));
1329 CHECK_LOG_CTX("Invalid keyword \"must\" as a child of \"grouping\".", "Line number 1.");
1330 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, grp, lysp_grp_free); grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001331}
1332
1333static void
Radek Krejcif538ce52019-03-05 10:46:14 +01001334test_action(void **state)
1335{
Radek Krejcif538ce52019-03-05 10:46:14 +01001336 struct lysp_action *rpcs = NULL;
1337 struct lysp_node_container *c = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001338 struct ly_in in = {0};
Radek Krejcif538ce52019-03-05 10:46:14 +01001339
Radek Iša56ca9e42020-09-08 18:42:00 +02001340 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcif538ce52019-03-05 10:46:14 +01001341
1342 /* invalid cardinality */
1343#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001344 in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Iša56ca9e42020-09-08 18:42:00 +02001345 assert_int_equal(LY_EVALID, parse_action(YCTX, &in, NULL, &rpcs)); \
1346 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1347 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, rpcs, lysp_action_free); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001348
1349 TEST_DUP("description", "text1", "text2");
Michal Vaskob83af8a2020-01-06 09:49:22 +01001350 TEST_DUP("input", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
1351 TEST_DUP("output", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
Radek Krejcif538ce52019-03-05 10:46:14 +01001352 TEST_DUP("reference", "1", "2");
1353 TEST_DUP("status", "current", "obsolete");
1354#undef TEST_DUP
1355
1356 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001357 in.current = "top;";
Radek Iša56ca9e42020-09-08 18:42:00 +02001358 assert_int_equal(LY_SUCCESS, parse_container(YCTX, &in, NULL, (struct lysp_node **)&c));
Michal Vasko63f3d842020-07-08 10:10:14 +02001359 in.current = "func {description test;grouping grp;if-feature f;reference test;status current;typedef mytype {type int8;} m:ext;"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001360 "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1361 " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}"
1362 "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1363 " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +02001364 assert_int_equal(LY_SUCCESS, parse_action(YCTX, &in, (struct lysp_node *)c, &rpcs));
Radek Krejcif538ce52019-03-05 10:46:14 +01001365 assert_non_null(rpcs);
1366 assert_int_equal(LYS_ACTION, rpcs->nodetype);
1367 assert_string_equal("func", rpcs->name);
1368 assert_string_equal("test", rpcs->dsc);
1369 assert_non_null(rpcs->exts);
1370 assert_non_null(rpcs->iffeatures);
1371 assert_string_equal("test", rpcs->ref);
1372 assert_non_null(rpcs->groupings);
1373 assert_non_null(rpcs->typedefs);
1374 assert_int_equal(LYS_STATUS_CURR, rpcs->flags);
1375 /* input */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001376 assert_int_equal(rpcs->input.nodetype, LYS_INPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001377 assert_non_null(rpcs->input.groupings);
1378 assert_non_null(rpcs->input.exts);
1379 assert_non_null(rpcs->input.musts);
1380 assert_non_null(rpcs->input.typedefs);
1381 assert_non_null(rpcs->input.data);
1382 /* output */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001383 assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001384 assert_non_null(rpcs->output.groupings);
1385 assert_non_null(rpcs->output.exts);
1386 assert_non_null(rpcs->output.musts);
1387 assert_non_null(rpcs->output.typedefs);
1388 assert_non_null(rpcs->output.data);
1389
Radek Iša56ca9e42020-09-08 18:42:00 +02001390 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
1391 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, rpcs, lysp_action_free); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001392
1393 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001394 in.current = "func {config true} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +02001395 assert_int_equal(LY_EVALID, parse_action(YCTX, &in, NULL, &rpcs));
1396 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"rpc\".", "Line number 1.");
1397 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, rpcs, lysp_action_free); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001398
Radek Iša56ca9e42020-09-08 18:42:00 +02001399 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c);
Radek Krejcif538ce52019-03-05 10:46:14 +01001400}
1401
1402static void
Radek Krejcifc11bd72019-04-11 16:00:05 +02001403test_notification(void **state)
1404{
Radek Krejcifc11bd72019-04-11 16:00:05 +02001405 struct lysp_notif *notifs = NULL;
1406 struct lysp_node_container *c = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001407 struct ly_in in = {0};
Radek Krejcifc11bd72019-04-11 16:00:05 +02001408
Radek Iša56ca9e42020-09-08 18:42:00 +02001409 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcifc11bd72019-04-11 16:00:05 +02001410
1411 /* invalid cardinality */
1412#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001413 in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Iša56ca9e42020-09-08 18:42:00 +02001414 assert_int_equal(LY_EVALID, parse_notif(YCTX, &in, NULL, &notifs)); \
1415 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1416 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, notifs, lysp_notif_free); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001417
1418 TEST_DUP("description", "text1", "text2");
1419 TEST_DUP("reference", "1", "2");
1420 TEST_DUP("status", "current", "obsolete");
1421#undef TEST_DUP
1422
1423 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001424 in.current = "top;";
Radek Iša56ca9e42020-09-08 18:42:00 +02001425 assert_int_equal(LY_SUCCESS, parse_container(YCTX, &in, NULL, (struct lysp_node **)&c));
Michal Vasko63f3d842020-07-08 10:10:14 +02001426 in.current = "ntf {anydata a1; anyxml a2; choice ch; container c; description test; grouping grp; if-feature f; leaf l {type int8;}"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001427 "leaf-list ll {type int8;} list li; must 1; reference test; status current; typedef mytype {type int8;} uses grp; m:ext;}";
Radek Iša56ca9e42020-09-08 18:42:00 +02001428 assert_int_equal(LY_SUCCESS, parse_notif(YCTX, &in, (struct lysp_node *)c, &notifs));
Radek Krejcifc11bd72019-04-11 16:00:05 +02001429 assert_non_null(notifs);
1430 assert_int_equal(LYS_NOTIF, notifs->nodetype);
1431 assert_string_equal("ntf", notifs->name);
1432 assert_string_equal("test", notifs->dsc);
1433 assert_non_null(notifs->exts);
1434 assert_non_null(notifs->iffeatures);
1435 assert_string_equal("test", notifs->ref);
1436 assert_non_null(notifs->groupings);
1437 assert_non_null(notifs->typedefs);
1438 assert_non_null(notifs->musts);
1439 assert_non_null(notifs->data);
1440 assert_int_equal(LYS_STATUS_CURR, notifs->flags);
1441
Radek Iša56ca9e42020-09-08 18:42:00 +02001442 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
1443 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, notifs, lysp_notif_free); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001444
1445 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001446 in.current = "ntf {config true} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +02001447 assert_int_equal(LY_EVALID, parse_notif(YCTX, &in, NULL, &notifs));
1448 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"notification\".", "Line number 1.");
1449 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, notifs, lysp_notif_free); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001450
Radek Iša56ca9e42020-09-08 18:42:00 +02001451 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c);
Radek Krejcifc11bd72019-04-11 16:00:05 +02001452}
1453
1454static void
Radek Krejcie86bf772018-12-14 11:39:53 +01001455test_uses(void **state)
1456{
Radek Krejcie86bf772018-12-14 11:39:53 +01001457 struct lysp_node_uses *u = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001458 struct ly_in in = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01001459
Radek Iša56ca9e42020-09-08 18:42:00 +02001460 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001461
1462 /* invalid cardinality */
1463#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001464 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Iša56ca9e42020-09-08 18:42:00 +02001465 assert_int_equal(LY_EVALID, parse_uses(YCTX, &in, NULL, (struct lysp_node**)&u)); \
1466 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1467 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)u); u = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001468
1469 TEST_DUP("description", "text1", "text2");
1470 TEST_DUP("reference", "1", "2");
1471 TEST_DUP("status", "current", "obsolete");
1472 TEST_DUP("when", "true", "false");
1473#undef TEST_DUP
1474
1475 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001476 in.current = "grpref {augment some/node;description test;if-feature f;reference test;refine some/other/node;status current;when true;m:ext;} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +02001477 assert_int_equal(LY_SUCCESS, parse_uses(YCTX, &in, NULL, (struct lysp_node **)&u));
1478 CHECK_LYSP_NODE(u, "test", 1, LYS_STATUS_CURR, 1, "grpref", 0, LYS_USES, 0, "test", 1);
Radek Krejcie86bf772018-12-14 11:39:53 +01001479 assert_non_null(u->augments);
1480 assert_non_null(u->refines);
Radek Iša56ca9e42020-09-08 18:42:00 +02001481 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)u); u = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001482}
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001483
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001484static void
1485test_augment(void **state)
1486{
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001487 struct lysp_augment *a = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001488 struct ly_in in = {0};
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001489
Radek Iša56ca9e42020-09-08 18:42:00 +02001490 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001491
1492 /* invalid cardinality */
1493#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001494 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Iša56ca9e42020-09-08 18:42:00 +02001495 assert_int_equal(LY_EVALID, parse_augment(YCTX, &in, NULL, &a)); \
1496 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1497 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001498
1499 TEST_DUP("description", "text1", "text2");
1500 TEST_DUP("reference", "1", "2");
1501 TEST_DUP("status", "current", "obsolete");
1502 TEST_DUP("when", "true", "false");
1503#undef TEST_DUP
1504
1505 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001506 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 Krejcib4ac5a92020-11-23 17:54:33 +01001507 "leaf-list ll {type string;} list li;notification not;reference test;status current;uses g;when true;m:ext;} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +02001508 assert_int_equal(LY_SUCCESS, parse_augment(YCTX, &in, NULL, &a));
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001509 assert_non_null(a);
1510 assert_int_equal(LYS_AUGMENT, a->nodetype);
1511 assert_string_equal("/target/nodeid", a->nodeid);
1512 assert_string_equal("test", a->dsc);
1513 assert_non_null(a->exts);
1514 assert_non_null(a->iffeatures);
1515 assert_string_equal("test", a->ref);
1516 assert_non_null(a->when);
1517 assert_null(a->parent);
1518 assert_int_equal(LYS_STATUS_CURR, a->flags);
Radek Iša56ca9e42020-09-08 18:42:00 +02001519 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001520}
1521
Radek Krejcif09e4e82019-06-14 15:08:11 +02001522static void
1523test_when(void **state)
1524{
Radek Krejcif09e4e82019-06-14 15:08:11 +02001525 struct lysp_when *w = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001526 struct ly_in in = {0};
Radek Krejcif09e4e82019-06-14 15:08:11 +02001527
Radek Iša56ca9e42020-09-08 18:42:00 +02001528 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001529
1530 /* invalid cardinality */
1531#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001532 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Iša56ca9e42020-09-08 18:42:00 +02001533 assert_int_equal(LY_EVALID, parse_when(YCTX, &in, &w)); \
1534 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1535 FREE_MEMBER(YCTX->parsed_mod->mod->ctx, w, lysp_when_free); w = NULL;
Radek Krejcif09e4e82019-06-14 15:08:11 +02001536
1537 TEST_DUP("description", "text1", "text2");
1538 TEST_DUP("reference", "1", "2");
1539#undef TEST_DUP
1540
1541 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001542 in.current = "expression {description test;reference test;m:ext;} ...";
Radek Iša56ca9e42020-09-08 18:42:00 +02001543 assert_int_equal(LY_SUCCESS, parse_when(YCTX, &in, &w));
Radek Krejcif09e4e82019-06-14 15:08:11 +02001544 assert_non_null(w);
1545 assert_string_equal("expression", w->cond);
1546 assert_string_equal("test", w->dsc);
1547 assert_string_equal("test", w->ref);
1548 assert_non_null(w->exts);
Radek Iša56ca9e42020-09-08 18:42:00 +02001549 FREE_MEMBER(YCTX->parsed_mod->mod->ctx, w, lysp_when_free); w = NULL;
Radek Krejcif09e4e82019-06-14 15:08:11 +02001550
1551 /* empty condition */
Michal Vasko63f3d842020-07-08 10:10:14 +02001552 in.current = "\"\";";
Radek Iša56ca9e42020-09-08 18:42:00 +02001553 assert_int_equal(LY_SUCCESS, parse_when(YCTX, &in, &w));
1554 CHECK_LOG_CTX("Empty argument of when statement does not make sense.", NULL);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001555 assert_non_null(w);
1556 assert_string_equal("", w->cond);
Radek Iša56ca9e42020-09-08 18:42:00 +02001557 FREE_MEMBER(YCTX->parsed_mod->mod->ctx, w, lysp_when_free); w = NULL;
Radek Krejcif09e4e82019-06-14 15:08:11 +02001558}
1559
David Sedlákd6ce6d72019-07-16 17:30:18 +02001560static void
1561test_value(void **state)
1562{
Michal Vasko63f3d842020-07-08 10:10:14 +02001563 struct ly_in in = {0};
David Sedlákd6ce6d72019-07-16 17:30:18 +02001564 int64_t val = 0;
1565 uint16_t flags = 0;
1566
Michal Vasko63f3d842020-07-08 10:10:14 +02001567 in.current = "-0;";
Radek Iša56ca9e42020-09-08 18:42:00 +02001568 assert_int_equal(parse_type_enum_value_pos(YCTX, &in, LY_STMT_VALUE, &val, &flags, NULL), LY_SUCCESS);
David Sedlákd6ce6d72019-07-16 17:30:18 +02001569 assert_int_equal(val, 0);
1570
Michal Vasko63f3d842020-07-08 10:10:14 +02001571 in.current = "-0;";
David Sedlákd6ce6d72019-07-16 17:30:18 +02001572 flags = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +02001573 assert_int_equal(parse_type_enum_value_pos(YCTX, &in, LY_STMT_POSITION, &val, &flags, NULL), LY_EVALID);
1574 CHECK_LOG_CTX("Invalid value \"-0\" of \"position\".", "Line number 1.");
David Sedlákd6ce6d72019-07-16 17:30:18 +02001575}
1576
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001577int
1578main(void)
Radek Krejci80dd33e2018-09-26 15:57:18 +02001579{
1580 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +02001581 UTEST(test_helpers, setup, teardown),
1582 UTEST(test_comments, setup, teardown),
1583 UTEST(test_arg, setup, teardown),
1584 UTEST(test_stmts, setup, teardown),
1585 UTEST(test_minmax, setup, teardown),
1586 UTEST(test_module, setup, teardown),
1587 UTEST(test_deviation, setup, teardown),
1588 UTEST(test_deviate, setup, teardown),
1589 UTEST(test_container, setup, teardown),
1590 UTEST(test_leaf, setup, teardown),
1591 UTEST(test_leaflist, setup, teardown),
1592 UTEST(test_list, setup, teardown),
1593 UTEST(test_choice, setup, teardown),
1594 UTEST(test_case, setup, teardown),
1595 UTEST(test_anydata, setup, teardown),
1596 UTEST(test_anyxml, setup, teardown),
1597 UTEST(test_action, setup, teardown),
1598 UTEST(test_notification, setup, teardown),
1599 UTEST(test_grouping, setup, teardown),
1600 UTEST(test_uses, setup, teardown),
1601 UTEST(test_augment, setup, teardown),
1602 UTEST(test_when, setup, teardown),
1603 UTEST(test_value, setup, teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02001604 };
1605
1606 return cmocka_run_group_tests(tests, NULL, NULL);
1607}