blob: 6f0908472427ab1a49eedbf6547386002c945f7e [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);
Radek Krejci33090f92020-12-17 20:12:46 +010039LY_ERR buf_store_char(struct lys_yang_parser_ctx *ctx, 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);
Radek Krejci33090f92020-12-17 20:12:46 +010041LY_ERR get_keyword(struct lys_yang_parser_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len);
42LY_ERR get_argument(struct lys_yang_parser_ctx *ctx, 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 Krejci33090f92020-12-17 20:12:46 +010044LY_ERR skip_comment(struct lys_yang_parser_ctx *ctx, uint8_t comment);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020045
Radek Krejci33090f92020-12-17 20:12:46 +010046LY_ERR parse_action(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_action **actions);
47LY_ERR parse_any(struct lys_yang_parser_ctx *ctx, enum ly_stmt kw, struct lysp_node *parent, struct lysp_node **siblings);
48LY_ERR parse_augment(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_augment **augments);
49LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
50LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
51LY_ERR parse_deviate(struct lys_yang_parser_ctx *ctx, struct lysp_deviate **deviates);
52LY_ERR parse_deviation(struct lys_yang_parser_ctx *ctx, struct lysp_deviation **deviations);
53LY_ERR parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_grp **groupings);
54LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
55LY_ERR parse_leaf(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
56LY_ERR parse_leaflist(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
57LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
58LY_ERR parse_maxelements(struct lys_yang_parser_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts);
59LY_ERR parse_minelements(struct lys_yang_parser_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts);
60LY_ERR parse_module(struct lys_yang_parser_ctx *ctx, struct lysp_module *mod);
61LY_ERR parse_notif(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_notif **notifs);
62LY_ERR parse_submodule(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod);
63LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
64LY_ERR parse_when(struct lys_yang_parser_ctx *ctx, struct lysp_when **when_p);
65LY_ERR parse_type_enum_value_pos(struct lys_yang_parser_ctx *ctx, enum ly_stmt val_kw, int64_t *value, uint16_t *flags, struct lysp_ext_instance **exts);
Radek 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
Radek Krejci33090f92020-12-17 20:12:46 +010069#define YCTX_INIT \
70 struct ly_in in = {0}; \
71 YCTX->in = &in
72
Michal Vasko5d24f6c2020-10-13 13:49:06 +020073static int
Radek Iša56ca9e42020-09-08 18:42:00 +020074setup(void **state)
Michal Vasko5d24f6c2020-10-13 13:49:06 +020075{
Radek Iša56ca9e42020-09-08 18:42:00 +020076 UTEST_SETUP;
Michal Vasko5d24f6c2020-10-13 13:49:06 +020077
Radek Iša56ca9e42020-09-08 18:42:00 +020078 /* allocate parser context */
79 YCTX = calloc(1, sizeof(*YCTX));
80 YCTX->format = LYS_IN_YANG;
81 YCTX->pos_type = LY_VLOG_LINE;
82 YCTX->line = 1;
Michal Vasko5d24f6c2020-10-13 13:49:06 +020083
Radek Iša56ca9e42020-09-08 18:42:00 +020084 /* allocate new parsed module */
85 YCTX->parsed_mod = calloc(1, sizeof *YCTX->parsed_mod);
Michal Vasko5d24f6c2020-10-13 13:49:06 +020086
Radek Iša56ca9e42020-09-08 18:42:00 +020087 /* allocate new module */
88 YCTX->parsed_mod->mod = calloc(1, sizeof *YCTX->parsed_mod->mod);
89 YCTX->parsed_mod->mod->ctx = UTEST_LYCTX;
90 YCTX->parsed_mod->mod->parsed = YCTX->parsed_mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +020091
Michal Vasko5d24f6c2020-10-13 13:49:06 +020092 return 0;
93}
94
95static int
Radek Iša56ca9e42020-09-08 18:42:00 +020096teardown(void **state)
Michal Vasko5d24f6c2020-10-13 13:49:06 +020097{
Radek Iša56ca9e42020-09-08 18:42:00 +020098 lys_module_free(YCTX->parsed_mod->mod, NULL);
99 free(YCTX);
100 YCTX = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200101
Radek Iša56ca9e42020-09-08 18:42:00 +0200102 UTEST_TEARDOWN;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200103
104 return 0;
105}
106
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200107#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200108 in.current = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +0100109 assert_int_equal(LY_EVALID, FUNC(YCTX, RESULT)); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200110 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number "LINE".");\
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200111 CLEANUP
Radek Krejci44ceedc2018-10-02 15:54:31 +0200112static void
113test_helpers(void **state)
114{
Radek Krejci404251e2018-10-09 12:06:44 +0200115 char *buf, *p;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200116 size_t len, size;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200117 uint8_t prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200118
Radek Krejci33090f92020-12-17 20:12:46 +0100119 YCTX_INIT;
120
Radek Krejci44ceedc2018-10-02 15:54:31 +0200121 /* storing into buffer */
Michal Vasko63f3d842020-07-08 10:10:14 +0200122 in.current = "abcd";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200123 buf = NULL;
124 size = len = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200125 assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &in, 2, &buf, &size, &len));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200126 assert_int_not_equal(0, size);
127 assert_int_equal(2, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200128 assert_string_equal("cd", in.current);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200129 assert_false(strncmp("ab", buf, 2));
130 free(buf);
Radek Krejci404251e2018-10-09 12:06:44 +0200131 buf = NULL;
132
133 /* invalid first characters */
134 len = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200135 in.current = "2invalid";
Radek Krejci33090f92020-12-17 20:12:46 +0100136 assert_int_equal(LY_EVALID, buf_store_char(YCTX, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Michal Vasko63f3d842020-07-08 10:10:14 +0200137 in.current = ".invalid";
Radek Krejci33090f92020-12-17 20:12:46 +0100138 assert_int_equal(LY_EVALID, buf_store_char(YCTX, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Michal Vasko63f3d842020-07-08 10:10:14 +0200139 in.current = "-invalid";
Radek Krejci33090f92020-12-17 20:12:46 +0100140 assert_int_equal(LY_EVALID, buf_store_char(YCTX, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200141 /* invalid following characters */
142 len = 3; /* number of characters read before the str content */
Michal Vasko63f3d842020-07-08 10:10:14 +0200143 in.current = "!";
Radek Krejci33090f92020-12-17 20:12:46 +0100144 assert_int_equal(LY_EVALID, buf_store_char(YCTX, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Michal Vasko63f3d842020-07-08 10:10:14 +0200145 in.current = ":";
Radek Krejci33090f92020-12-17 20:12:46 +0100146 assert_int_equal(LY_EVALID, buf_store_char(YCTX, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200147 /* valid colon for prefixed identifiers */
148 len = size = 0;
149 p = NULL;
David Sedlák40bb13b2019-07-10 14:34:18 +0200150 prefix = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200151 in.current = "x:id";
Radek Krejci33090f92020-12-17 20:12:46 +0100152 assert_int_equal(LY_SUCCESS, buf_store_char(YCTX, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 0, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200153 assert_int_equal(1, len);
154 assert_null(buf);
Michal Vasko63f3d842020-07-08 10:10:14 +0200155 assert_string_equal(":id", in.current);
Radek Krejci404251e2018-10-09 12:06:44 +0200156 assert_int_equal('x', p[len - 1]);
Radek Krejci33090f92020-12-17 20:12:46 +0100157 assert_int_equal(LY_SUCCESS, buf_store_char(YCTX, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200158 assert_int_equal(2, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200159 assert_string_equal("id", in.current);
Radek Krejci404251e2018-10-09 12:06:44 +0200160 assert_int_equal(':', p[len - 1]);
161 free(buf);
David Sedlák40bb13b2019-07-10 14:34:18 +0200162 prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200163
164 /* checking identifiers */
Radek Iša56ca9e42020-09-08 18:42:00 +0200165 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, NULL));
166 CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
167 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, '#', 1, NULL));
168 CHECK_LOG_CTX("Invalid identifier first character '#' (0x0023).", "Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200169
Radek Iša56ca9e42020-09-08 18:42:00 +0200170 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, 'a', 1, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200171 assert_int_equal(0, prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200172 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200173 assert_int_equal(1, prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200174 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, &prefix));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200175 assert_int_equal(1, prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200176 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, 'b', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200177 assert_int_equal(2, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200178 /* second colon is invalid */
Radek Iša56ca9e42020-09-08 18:42:00 +0200179 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, &prefix));
180 CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200181}
Radek Krejci80dd33e2018-09-26 15:57:18 +0200182
Radek Iša56ca9e42020-09-08 18:42:00 +0200183#define TEST_GET_ARGUMENT_SUCCESS(INPUT_TEXT, CTX, ARG_TYPE, EXPECT_WORD, EXPECT_LEN, EXPECT_CURRENT)\
184 {\
185 const char * text = INPUT_TEXT;\
186 in.current = text;\
Radek Krejci33090f92020-12-17 20:12:46 +0100187 assert_int_equal(LY_SUCCESS, get_argument(CTX, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));\
Radek Iša56ca9e42020-09-08 18:42:00 +0200188 assert_string_equal(word, EXPECT_WORD);\
189 assert_int_equal(len, EXPECT_LEN);\
190 assert_string_equal(EXPECT_CURRENT, in.current);\
191 }
192
Radek Krejci80dd33e2018-09-26 15:57:18 +0200193static void
194test_comments(void **state)
195{
Radek Krejciefd22f62018-09-27 11:47:58 +0200196 char *word, *buf;
197 size_t len;
Radek Iša56ca9e42020-09-08 18:42:00 +0200198 const char *in_text;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200199
Radek Krejci33090f92020-12-17 20:12:46 +0100200 YCTX_INIT;
201
Radek Iša56ca9e42020-09-08 18:42:00 +0200202 // in.current = " // this is a text of / one * line */ comment\nargument;";
203 in_text = " // this is a text of / one * line */ comment\nargument;";
204 TEST_GET_ARGUMENT_SUCCESS(in_text, YCTX, Y_STR_ARG, "argument;", 8, ";");
Radek Krejciefd22f62018-09-27 11:47:58 +0200205 assert_null(buf);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200206
Radek Iša56ca9e42020-09-08 18:42:00 +0200207 in_text = "/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\";";
208 TEST_GET_ARGUMENT_SUCCESS(in_text, YCTX, Y_STR_ARG, "argument", 8, ";");
Radek Krejciefd22f62018-09-27 11:47:58 +0200209 assert_ptr_equal(buf, word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200210 free(word);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200211
Michal Vasko63f3d842020-07-08 10:10:14 +0200212 in.current = " this is one line comment on last line";
Radek Krejci33090f92020-12-17 20:12:46 +0100213 assert_int_equal(LY_SUCCESS, skip_comment(YCTX, 1));
Michal Vasko63f3d842020-07-08 10:10:14 +0200214 assert_true(in.current[0] == '\0');
Radek Krejci80dd33e2018-09-26 15:57:18 +0200215
Michal Vasko63f3d842020-07-08 10:10:14 +0200216 in.current = " this is a not terminated comment x";
Radek Krejci33090f92020-12-17 20:12:46 +0100217 assert_int_equal(LY_EVALID, skip_comment(YCTX, 2));
Radek Iša56ca9e42020-09-08 18:42:00 +0200218 CHECK_LOG_CTX("Unexpected end-of-input, non-terminated comment.", "Line number 5.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200219 assert_true(in.current[0] == '\0');
Radek Krejci80dd33e2018-09-26 15:57:18 +0200220}
221
Radek Krejciefd22f62018-09-27 11:47:58 +0200222static void
223test_arg(void **state)
224{
Radek Krejciefd22f62018-09-27 11:47:58 +0200225 char *word, *buf;
226 size_t len;
227
Radek Krejci33090f92020-12-17 20:12:46 +0100228 YCTX_INIT;
229
Radek Krejciefd22f62018-09-27 11:47:58 +0200230 /* missing argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200231 in.current = ";";
Radek Krejci33090f92020-12-17 20:12:46 +0100232 assert_int_equal(LY_SUCCESS, get_argument(YCTX, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200233 assert_null(word);
234
Michal Vasko63f3d842020-07-08 10:10:14 +0200235 in.current = "{";
Radek Krejci33090f92020-12-17 20:12:46 +0100236 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200237 CHECK_LOG_CTX("Invalid character sequence \"{\", expected an argument.", "Line number 1.");
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200238
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200239 /* invalid escape sequence */
Michal Vasko63f3d842020-07-08 10:10:14 +0200240 in.current = "\"\\s\"";
Radek Krejci33090f92020-12-17 20:12:46 +0100241 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200242 CHECK_LOG_CTX("Double-quoted string unknown special character \'\\s\'.", "Line number 1.");
243
244 TEST_GET_ARGUMENT_SUCCESS("\'\\s\'", YCTX, Y_STR_ARG, "\\s\'", 2, "");
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200245
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200246 /* invalid character after the argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200247 in.current = "hello\"";
Radek Krejci33090f92020-12-17 20:12:46 +0100248 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200249 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 +0200250
Radek Iša56ca9e42020-09-08 18:42:00 +0200251 in.current = "hello}";
Radek Krejci33090f92020-12-17 20:12:46 +0100252 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200253 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 +0200254 /* invalid identifier-ref-arg-str */
Michal Vasko63f3d842020-07-08 10:10:14 +0200255 in.current = "pre:pre:value";
Radek Krejci33090f92020-12-17 20:12:46 +0100256 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200257 CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
David Sedlák40bb13b2019-07-10 14:34:18 +0200258
Michal Vasko63f3d842020-07-08 10:10:14 +0200259 in.current = "\"\";"; /* empty identifier is not allowed */
Radek Krejci33090f92020-12-17 20:12:46 +0100260 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_IDENTIF_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200261 CHECK_LOG_CTX("Statement argument is required.", "Line number 1.");
262
Michal Vasko63f3d842020-07-08 10:10:14 +0200263 in.current = "\"\";"; /* empty reference identifier is not allowed */
Radek Krejci33090f92020-12-17 20:12:46 +0100264 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200265 CHECK_LOG_CTX("Statement argument is required.", "Line number 1.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200266
Radek Iša56ca9e42020-09-08 18:42:00 +0200267 /* slash is not an invalid character */
268 TEST_GET_ARGUMENT_SUCCESS("hello/x\t", YCTX, Y_STR_ARG, "hello/x\t", 7, "\t");
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200269 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200270
271 /* different quoting */
Radek Iša56ca9e42020-09-08 18:42:00 +0200272 TEST_GET_ARGUMENT_SUCCESS("hello/x\t", YCTX, Y_STR_ARG, "hello/x\t", 7, "\t");
Radek Krejciefd22f62018-09-27 11:47:58 +0200273
Radek Iša56ca9e42020-09-08 18:42:00 +0200274 TEST_GET_ARGUMENT_SUCCESS("hello ", YCTX, Y_STR_ARG, "hello ", 5, " ");
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200275
Radek Iša56ca9e42020-09-08 18:42:00 +0200276 TEST_GET_ARGUMENT_SUCCESS("hello/*comment*/\n", YCTX, Y_STR_ARG, "hello/*comment*/\n", 5, "\n");
277
278 TEST_GET_ARGUMENT_SUCCESS("\"hello\\n\\t\\\"\\\\\";", YCTX, Y_STR_ARG, "hello\n\t\"\\", 9, ";");
fredgand49fe112019-10-21 20:51:50 +0800279 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200280
Radek Iša56ca9e42020-09-08 18:42:00 +0200281 YCTX->indent = 14;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200282 /* - space and tabs before newline are stripped out
283 * - space and tabs after newline (indentation) are stripped out
284 */
Radek Iša56ca9e42020-09-08 18:42:00 +0200285 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 +0200286 free(buf);
Radek Iša56ca9e42020-09-08 18:42:00 +0200287
288/* In contrast to previous, the backslash-escaped tabs are expanded after trimming, so they are preserved */
289 YCTX->indent = 14;
290 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 +0200291 assert_ptr_equal(word, buf);
Radek Krejciff13cd12019-10-25 15:34:24 +0200292 free(buf);
Radek Iša56ca9e42020-09-08 18:42:00 +0200293
Radek Krejciff13cd12019-10-25 15:34:24 +0200294 /* Do not handle whitespaces after backslash-escaped newline as indentation */
Radek Iša56ca9e42020-09-08 18:42:00 +0200295 YCTX->indent = 14;
296 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 +0200297 assert_ptr_equal(word, buf);
Radek Krejciff13cd12019-10-25 15:34:24 +0200298 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200299
Radek Iša56ca9e42020-09-08 18:42:00 +0200300 YCTX->indent = 14;
301 TEST_GET_ARGUMENT_SUCCESS("\"hello\n \tworld!\"", YCTX, Y_STR_ARG, "hello\nworld!", 12, "");
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200302 assert_ptr_equal(word, buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200303 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200304
Radek Iša56ca9e42020-09-08 18:42:00 +0200305 TEST_GET_ARGUMENT_SUCCESS("\'hello\'", YCTX, Y_STR_ARG, "hello'", 5, "");
Radek Krejciefd22f62018-09-27 11:47:58 +0200306
Radek Iša56ca9e42020-09-08 18:42:00 +0200307 TEST_GET_ARGUMENT_SUCCESS("\"hel\" +\t\n\"lo\"", YCTX, Y_STR_ARG, "hello", 5, "");
Radek Krejciefd22f62018-09-27 11:47:58 +0200308 assert_ptr_equal(word, buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200309 free(buf);
Radek Iša56ca9e42020-09-08 18:42:00 +0200310
Michal Vasko63f3d842020-07-08 10:10:14 +0200311 in.current = "\"hel\" +\t\nlo"; /* unquoted the second part */
Radek Krejci33090f92020-12-17 20:12:46 +0100312 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200313 CHECK_LOG_CTX("Both string parts divided by '+' must be quoted.", "Line number 6.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200314
Radek Iša56ca9e42020-09-08 18:42:00 +0200315 TEST_GET_ARGUMENT_SUCCESS("\'he\'\t\n+ \"llo\"", YCTX, Y_STR_ARG, "hello", 5, "");
Radek Krejciefd22f62018-09-27 11:47:58 +0200316 free(buf);
317
Radek Iša56ca9e42020-09-08 18:42:00 +0200318 TEST_GET_ARGUMENT_SUCCESS(" \t\n\"he\"+\'llo\'", YCTX, Y_STR_ARG, "hello", 5, "");
Radek Krejciefd22f62018-09-27 11:47:58 +0200319 free(buf);
320
Radek Krejci44ceedc2018-10-02 15:54:31 +0200321 /* missing argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200322 in.current = ";";
Radek Krejci33090f92020-12-17 20:12:46 +0100323 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200324 CHECK_LOG_CTX("Invalid character sequence \";\", expected an argument.", "Line number 8.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200325}
326
Radek Iša56ca9e42020-09-08 18:42:00 +0200327#define TEST_STMS_SUCCESS(INPUT_TEXT, CTX, ACTION, EXPECT_WORD)\
328 in.current = INPUT_TEXT;\
Radek Krejci33090f92020-12-17 20:12:46 +0100329 assert_int_equal(LY_SUCCESS, get_keyword(CTX, &kw, &word, &len));\
Radek Iša56ca9e42020-09-08 18:42:00 +0200330 assert_int_equal(ACTION, kw);\
331 assert_int_equal(strlen(EXPECT_WORD), len);\
332 assert_true(0 == strncmp(EXPECT_WORD, word, len))
333
Radek Krejcidcc7b322018-10-11 14:24:02 +0200334static void
335test_stmts(void **state)
336{
Michal Vasko63f3d842020-07-08 10:10:14 +0200337 const char *p;
Radek Krejcid6b76452019-09-03 17:03:03 +0200338 enum ly_stmt kw;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200339 char *word;
340 size_t len;
341
Radek Krejci33090f92020-12-17 20:12:46 +0100342 YCTX_INIT;
343
Michal Vasko63f3d842020-07-08 10:10:14 +0200344 in.current = "\n// comment\n\tinput\t{";
Radek Krejci33090f92020-12-17 20:12:46 +0100345 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200346 assert_int_equal(LY_STMT_INPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200347 assert_int_equal(5, len);
348 assert_string_equal("input\t{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200349 assert_string_equal("\t{", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200350
Michal Vasko63f3d842020-07-08 10:10:14 +0200351 in.current = "\t /* comment */\t output\n\t{";
Radek Krejci33090f92020-12-17 20:12:46 +0100352 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200353 assert_int_equal(LY_STMT_OUTPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200354 assert_int_equal(6, len);
355 assert_string_equal("output\n\t{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200356 assert_string_equal("\n\t{", in.current);
Radek Krejci33090f92020-12-17 20:12:46 +0100357 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200358 assert_int_equal(LY_STMT_SYNTAX_LEFT_BRACE, kw);
Radek Krejciabdd8062019-06-11 16:44:19 +0200359 assert_int_equal(1, len);
360 assert_string_equal("{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200361 assert_string_equal("", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200362
Michal Vasko63f3d842020-07-08 10:10:14 +0200363 in.current = "/input { "; /* invalid slash */
Radek Krejci33090f92020-12-17 20:12:46 +0100364 assert_int_equal(LY_EVALID, get_keyword(YCTX, &kw, &word, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200365 CHECK_LOG_CTX("Invalid identifier first character '/'.", "Line number 4.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200366
Michal Vasko63f3d842020-07-08 10:10:14 +0200367 in.current = "not-a-statement-nor-extension { "; /* invalid identifier */
Radek Krejci33090f92020-12-17 20:12:46 +0100368 assert_int_equal(LY_EVALID, get_keyword(YCTX, &kw, &word, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200369 CHECK_LOG_CTX("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword.", "Line number 4.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200370
Michal Vasko63f3d842020-07-08 10:10:14 +0200371 in.current = "path;"; /* missing sep after the keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100372 assert_int_equal(LY_EVALID, get_keyword(YCTX, &kw, &word, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200373 CHECK_LOG_CTX("Invalid character sequence \"path;\", expected a keyword followed by a separator.", "Line number 4.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200374
Radek Iša56ca9e42020-09-08 18:42:00 +0200375 TEST_STMS_SUCCESS("action ", YCTX, LY_STMT_ACTION, "action");
376
377 TEST_STMS_SUCCESS("anydata ", YCTX, LY_STMT_ANYDATA, "anydata");
378 TEST_STMS_SUCCESS("anyxml ", YCTX, LY_STMT_ANYXML, "anyxml");
379 TEST_STMS_SUCCESS("argument ", YCTX, LY_STMT_ARGUMENT, "argument");
380 TEST_STMS_SUCCESS("augment ", YCTX, LY_STMT_AUGMENT, "augment");
381 TEST_STMS_SUCCESS("base ", YCTX, LY_STMT_BASE, "base");
382 TEST_STMS_SUCCESS("belongs-to ", YCTX, LY_STMT_BELONGS_TO, "belongs-to");
383 TEST_STMS_SUCCESS("bit ", YCTX, LY_STMT_BIT, "bit");
384 TEST_STMS_SUCCESS("case ", YCTX, LY_STMT_CASE, "case");
385 TEST_STMS_SUCCESS("choice ", YCTX, LY_STMT_CHOICE, "choice");
386 TEST_STMS_SUCCESS("config ", YCTX, LY_STMT_CONFIG, "config");
387 TEST_STMS_SUCCESS("contact ", YCTX, LY_STMT_CONTACT, "contact");
388 TEST_STMS_SUCCESS("container ", YCTX, LY_STMT_CONTAINER, "container");
389 TEST_STMS_SUCCESS("default ", YCTX, LY_STMT_DEFAULT, "default");
390 TEST_STMS_SUCCESS("description ", YCTX, LY_STMT_DESCRIPTION, "description");
391 TEST_STMS_SUCCESS("deviate ", YCTX, LY_STMT_DEVIATE, "deviate");
392 TEST_STMS_SUCCESS("deviation ", YCTX, LY_STMT_DEVIATION, "deviation");
393 TEST_STMS_SUCCESS("enum ", YCTX, LY_STMT_ENUM, "enum");
394 TEST_STMS_SUCCESS("error-app-tag ", YCTX, LY_STMT_ERROR_APP_TAG, "error-app-tag");
395 TEST_STMS_SUCCESS("error-message ", YCTX, LY_STMT_ERROR_MESSAGE, "error-message");
396 TEST_STMS_SUCCESS("extension ", YCTX, LY_STMT_EXTENSION, "extension");
397 TEST_STMS_SUCCESS("feature ", YCTX, LY_STMT_FEATURE, "feature");
398 TEST_STMS_SUCCESS("fraction-digits ", YCTX, LY_STMT_FRACTION_DIGITS, "fraction-digits");
399 TEST_STMS_SUCCESS("grouping ", YCTX, LY_STMT_GROUPING, "grouping");
400 TEST_STMS_SUCCESS("identity ", YCTX, LY_STMT_IDENTITY, "identity");
401 TEST_STMS_SUCCESS("if-feature ", YCTX, LY_STMT_IF_FEATURE, "if-feature");
402 TEST_STMS_SUCCESS("import ", YCTX, LY_STMT_IMPORT, "import");
403 TEST_STMS_SUCCESS("include ", YCTX, LY_STMT_INCLUDE, "include");
404 TEST_STMS_SUCCESS("input{", YCTX, LY_STMT_INPUT, "input");
405 TEST_STMS_SUCCESS("key ", YCTX, LY_STMT_KEY, "key");
406 TEST_STMS_SUCCESS("leaf ", YCTX, LY_STMT_LEAF, "leaf");
407 TEST_STMS_SUCCESS("leaf-list ", YCTX, LY_STMT_LEAF_LIST, "leaf-list");
408 TEST_STMS_SUCCESS("length ", YCTX, LY_STMT_LENGTH, "length");
409 TEST_STMS_SUCCESS("list ", YCTX, LY_STMT_LIST, "list");
410 TEST_STMS_SUCCESS("mandatory ", YCTX, LY_STMT_MANDATORY, "mandatory");
411 TEST_STMS_SUCCESS("max-elements ", YCTX, LY_STMT_MAX_ELEMENTS, "max-elements");
412 TEST_STMS_SUCCESS("min-elements ", YCTX, LY_STMT_MIN_ELEMENTS, "min-elements");
413 TEST_STMS_SUCCESS("modifier ", YCTX, LY_STMT_MODIFIER, "modifier");
414 TEST_STMS_SUCCESS("module ", YCTX, LY_STMT_MODULE, "module");
415 TEST_STMS_SUCCESS("must ", YCTX, LY_STMT_MUST, "must");
416 TEST_STMS_SUCCESS("namespace ", YCTX, LY_STMT_NAMESPACE, "namespace");
417 TEST_STMS_SUCCESS("notification ", YCTX, LY_STMT_NOTIFICATION, "notification");
418 TEST_STMS_SUCCESS("ordered-by ", YCTX, LY_STMT_ORDERED_BY, "ordered-by");
419 TEST_STMS_SUCCESS("organization ", YCTX, LY_STMT_ORGANIZATION, "organization");
420 TEST_STMS_SUCCESS("output ", YCTX, LY_STMT_OUTPUT, "output");
421 TEST_STMS_SUCCESS("path ", YCTX, LY_STMT_PATH, "path");
422 TEST_STMS_SUCCESS("pattern ", YCTX, LY_STMT_PATTERN, "pattern");
423 TEST_STMS_SUCCESS("position ", YCTX, LY_STMT_POSITION, "position");
424 TEST_STMS_SUCCESS("prefix ", YCTX, LY_STMT_PREFIX, "prefix");
425 TEST_STMS_SUCCESS("presence ", YCTX, LY_STMT_PRESENCE, "presence");
426 TEST_STMS_SUCCESS("range ", YCTX, LY_STMT_RANGE, "range");
427 TEST_STMS_SUCCESS("reference ", YCTX, LY_STMT_REFERENCE, "reference");
428 TEST_STMS_SUCCESS("refine ", YCTX, LY_STMT_REFINE, "refine");
429 TEST_STMS_SUCCESS("require-instance ", YCTX, LY_STMT_REQUIRE_INSTANCE, "require-instance");
430 TEST_STMS_SUCCESS("revision ", YCTX, LY_STMT_REVISION, "revision");
431 TEST_STMS_SUCCESS("revision-date ", YCTX, LY_STMT_REVISION_DATE, "revision-date");
432 TEST_STMS_SUCCESS("rpc ", YCTX, LY_STMT_RPC, "rpc");
433 TEST_STMS_SUCCESS("status ", YCTX, LY_STMT_STATUS, "status");
434 TEST_STMS_SUCCESS("submodule ", YCTX, LY_STMT_SUBMODULE, "submodule");
435 TEST_STMS_SUCCESS("type ", YCTX, LY_STMT_TYPE, "type");
436 TEST_STMS_SUCCESS("typedef ", YCTX, LY_STMT_TYPEDEF, "typedef");
437 TEST_STMS_SUCCESS("unique ", YCTX, LY_STMT_UNIQUE, "unique");
438 TEST_STMS_SUCCESS("units ", YCTX, LY_STMT_UNITS, "units");
439 TEST_STMS_SUCCESS("uses ", YCTX, LY_STMT_USES, "uses");
440 TEST_STMS_SUCCESS("value ", YCTX, LY_STMT_VALUE, "value");
441 TEST_STMS_SUCCESS("when ", YCTX, LY_STMT_WHEN, "when");
442 TEST_STMS_SUCCESS("yang-version ", YCTX, LY_STMT_YANG_VERSION, "yang-version");
443 TEST_STMS_SUCCESS("yin-element ", YCTX, LY_STMT_YIN_ELEMENT, "yin-element");
444 TEST_STMS_SUCCESS(";config false;", YCTX, LY_STMT_SYNTAX_SEMICOLON, ";");
Michal Vasko63f3d842020-07-08 10:10:14 +0200445 assert_string_equal("config false;", in.current);
Radek Iša56ca9e42020-09-08 18:42:00 +0200446 TEST_STMS_SUCCESS("{ config false;", YCTX, LY_STMT_SYNTAX_LEFT_BRACE, "{");
Michal Vasko63f3d842020-07-08 10:10:14 +0200447 assert_string_equal(" config false;", in.current);
Radek Iša56ca9e42020-09-08 18:42:00 +0200448 TEST_STMS_SUCCESS("}", YCTX, LY_STMT_SYNTAX_RIGHT_BRACE, "}");
Michal Vasko63f3d842020-07-08 10:10:14 +0200449 assert_string_equal("", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200450
451 /* geenric extension */
Michal Vasko63f3d842020-07-08 10:10:14 +0200452 in.current = p = "nacm:default-deny-write;";
Radek Krejci33090f92020-12-17 20:12:46 +0100453 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200454 assert_int_equal(LY_STMT_EXTENSION_INSTANCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200455 assert_int_equal(23, len);
456 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200457}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200458
Radek Iša56ca9e42020-09-08 18:42:00 +0200459#define TEST_MINMAX_SUCCESS(INPUT_TEXT, CTX, TYPE, VALUE)\
460 in.current = INPUT_TEXT;\
461 if(TYPE == LYS_SET_MIN){\
Radek Krejci33090f92020-12-17 20:12:46 +0100462 assert_int_equal(LY_SUCCESS, parse_minelements(CTX, &value, &flags, &ext));\
Radek Iša56ca9e42020-09-08 18:42:00 +0200463 }\
464 if(TYPE == LYS_SET_MAX){\
Radek Krejci33090f92020-12-17 20:12:46 +0100465 assert_int_equal(LY_SUCCESS, parse_maxelements(CTX, &value, &flags, &ext));\
Radek Iša56ca9e42020-09-08 18:42:00 +0200466 }\
467 assert_int_equal(TYPE, flags);\
468 assert_int_equal(VALUE, value)
469
Radek Krejci05b13982018-11-28 16:22:07 +0100470static void
471test_minmax(void **state)
472{
Radek Krejci05b13982018-11-28 16:22:07 +0100473 uint16_t flags = 0;
474 uint32_t value = 0;
475 struct lysp_ext_instance *ext = NULL;
Radek Krejci05b13982018-11-28 16:22:07 +0100476
Radek Krejci33090f92020-12-17 20:12:46 +0100477 YCTX_INIT;
Radek Iša56ca9e42020-09-08 18:42:00 +0200478 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci05b13982018-11-28 16:22:07 +0100479
Michal Vasko63f3d842020-07-08 10:10:14 +0200480 in.current = " 1invalid; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100481 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200482 CHECK_LOG_CTX("Invalid value \"1invalid\" of \"min-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100483
484 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200485 in.current = " -1; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100486 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200487 CHECK_LOG_CTX("Invalid value \"-1\" of \"min-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100488
Radek Krejcidf6cad12018-11-28 17:10:55 +0100489 /* implementation limit */
490 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200491 in.current = " 4294967296; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100492 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200493 CHECK_LOG_CTX("Value \"4294967296\" is out of \"min-elements\" bounds.", "Line number 1.");
Radek Krejcidf6cad12018-11-28 17:10:55 +0100494
Radek Krejci05b13982018-11-28 16:22:07 +0100495 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200496 TEST_MINMAX_SUCCESS(" 1; ...", YCTX, LYS_SET_MIN, 1);
Radek Krejci05b13982018-11-28 16:22:07 +0100497
498 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200499 TEST_MINMAX_SUCCESS(" 1 {m:ext;} ...", YCTX, LYS_SET_MIN, 1);
Radek Krejci05b13982018-11-28 16:22:07 +0100500 assert_non_null(ext);
Radek Iša56ca9e42020-09-08 18:42:00 +0200501 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, ext, lysp_ext_instance_free);
Radek Krejci05b13982018-11-28 16:22:07 +0100502 ext = NULL;
503
504 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200505 in.current = " 1 {config true;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100506 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200507 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"min-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100508
Michal Vasko63f3d842020-07-08 10:10:14 +0200509 in.current = " 1invalid; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100510 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200511 CHECK_LOG_CTX("Invalid value \"1invalid\" of \"max-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100512
513 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200514 in.current = " -1; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100515 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200516 CHECK_LOG_CTX("Invalid value \"-1\" of \"max-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100517
Radek Krejcidf6cad12018-11-28 17:10:55 +0100518 /* implementation limit */
519 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200520 in.current = " 4294967296; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100521 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200522 CHECK_LOG_CTX("Value \"4294967296\" is out of \"max-elements\" bounds.", "Line number 1.");
Radek Krejcidf6cad12018-11-28 17:10:55 +0100523
Radek Krejci05b13982018-11-28 16:22:07 +0100524 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200525 TEST_MINMAX_SUCCESS(" 1; ...", YCTX, LYS_SET_MAX, 1);
Radek Krejci05b13982018-11-28 16:22:07 +0100526
527 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200528 TEST_MINMAX_SUCCESS(" unbounded; ...", YCTX, LYS_SET_MAX, 0);
Radek Krejci05b13982018-11-28 16:22:07 +0100529
530 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200531 TEST_MINMAX_SUCCESS(" 1 {m:ext;} ...", YCTX, LYS_SET_MAX, 1);
Radek Krejci05b13982018-11-28 16:22:07 +0100532 assert_non_null(ext);
Radek Iša56ca9e42020-09-08 18:42:00 +0200533 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, ext, lysp_ext_instance_free);
Radek Krejci05b13982018-11-28 16:22:07 +0100534 ext = NULL;
535
536 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200537 in.current = " 1 {config true;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100538 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200539 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"max-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100540}
541
Radek Krejci9fcacc12018-10-11 15:59:11 +0200542static struct lysp_module *
Michal Vaskob36053d2020-03-26 15:49:30 +0100543mod_renew(struct lys_yang_parser_ctx *ctx)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200544{
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200545 struct ly_ctx *ly_ctx = ctx->parsed_mod->mod->ctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100546
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200547 lys_module_free(ctx->parsed_mod->mod, NULL);
548 ctx->parsed_mod = calloc(1, sizeof *ctx->parsed_mod);
549 ctx->parsed_mod->mod = calloc(1, sizeof *ctx->parsed_mod->mod);
550 ctx->parsed_mod->mod->parsed = ctx->parsed_mod;
551 ctx->parsed_mod->mod->ctx = ly_ctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100552
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200553 return ctx->parsed_mod;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100554}
555
556static struct lysp_submodule *
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200557submod_renew(struct lys_yang_parser_ctx *ctx)
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100558{
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200559 struct ly_ctx *ly_ctx = ctx->parsed_mod->mod->ctx;
560
561 lys_module_free(ctx->parsed_mod->mod, NULL);
562 ctx->parsed_mod = calloc(1, sizeof(struct lysp_submodule));
563 ctx->parsed_mod->mod = calloc(1, sizeof *ctx->parsed_mod->mod);
564 lydict_insert(ly_ctx, "name", 0, &ctx->parsed_mod->mod->name);
565 ctx->parsed_mod->mod->parsed = ctx->parsed_mod;
566 ctx->parsed_mod->mod->ctx = ly_ctx;
567
568 return (struct lysp_submodule *)ctx->parsed_mod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200569}
570
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100571static LY_ERR
572test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
573 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
574 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
Radek Krejcid33273d2018-10-25 14:55:52 +0200575{
576 *module_data = user_data;
577 *format = LYS_IN_YANG;
578 *free_module_data = NULL;
579 return LY_SUCCESS;
580}
581
Radek Krejci9fcacc12018-10-11 15:59:11 +0200582static void
583test_module(void **state)
584{
Radek Krejci40544fa2019-01-11 09:38:37 +0100585 struct lysp_module *mod = NULL;
586 struct lysp_submodule *submod = NULL;
587 struct lys_module *m;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100588 struct lys_glob_unres unres = {0};
Radek Iša56ca9e42020-09-08 18:42:00 +0200589 struct lys_yang_parser_ctx *ctx_p;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200590
Radek Krejci33090f92020-12-17 20:12:46 +0100591 YCTX_INIT;
592
Radek Iša56ca9e42020-09-08 18:42:00 +0200593 mod = mod_renew(YCTX);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200594
595 /* missing mandatory substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +0200596 in.current = " name {}";
Radek Krejci33090f92020-12-17 20:12:46 +0100597 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100598 assert_string_equal("name", mod->mod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +0200599 CHECK_LOG_CTX("Missing mandatory keyword \"namespace\" as a child of \"module\".", "Line number 1.");
Radek Krejci9fcacc12018-10-11 15:59:11 +0200600
Radek Iša56ca9e42020-09-08 18:42:00 +0200601 mod = mod_renew(YCTX);
Michal Vasko63f3d842020-07-08 10:10:14 +0200602 in.current = " name {namespace urn:x;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100603 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100604 assert_string_equal("urn:x", mod->mod->ns);
Radek Iša56ca9e42020-09-08 18:42:00 +0200605 CHECK_LOG_CTX("Missing mandatory keyword \"prefix\" as a child of \"module\".", "Line number 1.");
606 mod = mod_renew(YCTX);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200607
Michal Vasko63f3d842020-07-08 10:10:14 +0200608 in.current = " name {namespace urn:x;prefix \"x\";}";
Radek Krejci33090f92020-12-17 20:12:46 +0100609 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100610 assert_string_equal("x", mod->mod->prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200611 mod = mod_renew(YCTX);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200612
Radek Krejci027d5802018-11-14 16:57:28 +0100613#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
614#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200615#define TEST_NODE(NODETYPE, INPUT, NAME) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200616 in.current = SCHEMA_BEGINNING INPUT; \
Radek Krejci33090f92020-12-17 20:12:46 +0100617 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200618 assert_non_null(mod->data); \
619 assert_int_equal(NODETYPE, mod->data->nodetype); \
620 assert_string_equal(NAME, mod->data->name); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200621 mod = mod_renew(YCTX);
Radek Krejcia042ea12018-10-13 07:52:15 +0200622#define TEST_GENERIC(INPUT, TARGET, TEST) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200623 in.current = SCHEMA_BEGINNING INPUT; \
Radek Krejci33090f92020-12-17 20:12:46 +0100624 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200625 assert_non_null(TARGET); \
626 TEST; \
Radek Iša56ca9e42020-09-08 18:42:00 +0200627 mod = mod_renew(YCTX);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100628#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200629 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
Radek Iša56ca9e42020-09-08 18:42:00 +0200630 parse_module, mod, LINE, mod = mod_renew(YCTX))
Radek Krejcia042ea12018-10-13 07:52:15 +0200631
632 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100633 TEST_DUP("namespace", "y", "z", "1");
634 TEST_DUP("prefix", "y", "z", "1");
635 TEST_DUP("contact", "a", "b", "1");
636 TEST_DUP("description", "a", "b", "1");
637 TEST_DUP("organization", "a", "b", "1");
638 TEST_DUP("reference", "a", "b", "1");
Radek Krejcia042ea12018-10-13 07:52:15 +0200639
Radek Krejci70853c52018-10-15 14:46:16 +0200640 /* not allowed in module (submodule-specific) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200641 in.current = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100642 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200643 CHECK_LOG_CTX("Invalid keyword \"belongs-to\" as a child of \"module\".", "Line number 1.");
644 mod = mod_renew(YCTX);
Radek Krejci70853c52018-10-15 14:46:16 +0200645
Radek Krejcia042ea12018-10-13 07:52:15 +0200646 /* anydata */
647 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
648 /* anyxml */
649 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
650 /* augment */
651 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100652 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200653 /* choice */
654 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
655 /* contact 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100656 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100657 assert_string_equal("firstname surname", mod->mod->contact));
Radek Krejcia042ea12018-10-13 07:52:15 +0200658 /* container */
659 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
660 /* description 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100661 TEST_GENERIC("description \'some description\';}", mod->mod->dsc,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100662 assert_string_equal("some description", mod->mod->dsc));
Radek Krejcia042ea12018-10-13 07:52:15 +0200663 /* deviation */
664 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100665 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200666 /* extension */
667 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100668 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200669 /* feature */
670 TEST_GENERIC("feature test;}", mod->features,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100671 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200672 /* grouping */
673 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100674 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200675 /* identity */
676 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100677 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200678 /* import */
Radek Iša56ca9e42020-09-08 18:42:00 +0200679 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 +0200680 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100681 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200682
Radek Krejcia042ea12018-10-13 07:52:15 +0200683 /* import - prefix collision */
Michal Vasko63f3d842020-07-08 10:10:14 +0200684 in.current = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100685 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200686 CHECK_LOG_CTX("Prefix \"x\" already used as module prefix.", "Line number 2.");
687 mod = mod_renew(YCTX);
688
Michal Vasko63f3d842020-07-08 10:10:14 +0200689 in.current = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100690 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200691 CHECK_LOG_CTX("Prefix \"y\" already used to import \"zzz\" module.", "Line number 2.");
692
693 mod = mod_renew(YCTX);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200694 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 +0200695 assert_int_equal(lys_parse_mem(YCTX->parsed_mod->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_SUCCESS);
696 CHECK_LOG_CTX("Single revision of the module \"zzz\" imported twice.", NULL);
Radek Krejci70853c52018-10-15 14:46:16 +0200697
Radek Krejcia042ea12018-10-13 07:52:15 +0200698 /* include */
Radek Iša56ca9e42020-09-08 18:42:00 +0200699 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 +0200700 in.current = "module" SCHEMA_BEGINNING "include xxx;}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200701 assert_int_equal(lys_parse_mem(YCTX->parsed_mod->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_EVALID);
702 CHECK_LOG_CTX("Including \"xxx\" submodule into \"name\" failed.", NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200703
Radek Iša56ca9e42020-09-08 18:42:00 +0200704 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 +0200705 in.current = "module" SCHEMA_BEGINNING "include xxx;}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200706 assert_int_equal(lys_parse_mem(YCTX->parsed_mod->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_EVALID);
707 CHECK_LOG_CTX("Including \"xxx\" submodule into \"name\" failed.", NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200708
Radek Iša56ca9e42020-09-08 18:42:00 +0200709 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 +0200710 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100711 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +0200712
Radek Krejcia042ea12018-10-13 07:52:15 +0200713 /* leaf */
714 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
715 /* leaf-list */
716 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
717 /* list */
718 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
719 /* notification */
720 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100721 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200722 /* organization 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100723 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100724 assert_string_equal("CESNET a.l.e.", mod->mod->org));
Radek Krejcia042ea12018-10-13 07:52:15 +0200725 /* reference 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100726 TEST_GENERIC("reference RFC7950;}", mod->mod->ref,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100727 assert_string_equal("RFC7950", mod->mod->ref));
Radek Krejcia042ea12018-10-13 07:52:15 +0200728 /* revision */
729 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100730 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +0200731 /* rpc */
732 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100733 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200734 /* typedef */
735 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100736 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200737 /* uses */
738 TEST_NODE(LYS_USES, "uses test;}", "test");
739 /* yang-version */
Michal Vasko63f3d842020-07-08 10:10:14 +0200740 in.current = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100741 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200742 CHECK_LOG_CTX("Invalid value \"10\" of \"yang-version\".", "Line number 3.");
743 mod = mod_renew(YCTX);
Radek Krejci96e48da2020-09-04 13:18:06 +0200744 in.current = SCHEMA_BEGINNING2 "yang-version 1;yang-version 1.1;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100745 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200746 CHECK_LOG_CTX("Duplicate keyword \"yang-version\".", "Line number 3.");
747 mod = mod_renew(YCTX);
Radek Krejci96e48da2020-09-04 13:18:06 +0200748 in.current = SCHEMA_BEGINNING2 "yang-version 1;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100749 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200750 assert_int_equal(1, mod->version);
Radek Iša56ca9e42020-09-08 18:42:00 +0200751 mod = mod_renew(YCTX);
Michal Vasko63f3d842020-07-08 10:10:14 +0200752 in.current = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Krejci33090f92020-12-17 20:12:46 +0100753 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200754 assert_int_equal(2, mod->version);
Radek Iša56ca9e42020-09-08 18:42:00 +0200755 mod = mod_renew(YCTX);
Radek Krejci40544fa2019-01-11 09:38:37 +0100756
Michal Vasko63f3d842020-07-08 10:10:14 +0200757 in.current = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200758 m = calloc(1, sizeof *m);
Radek Iša56ca9e42020-09-08 18:42:00 +0200759 m->ctx = YCTX->parsed_mod->mod->ctx;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100760 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m, &unres));
Radek Iša56ca9e42020-09-08 18:42:00 +0200761 CHECK_LOG_CTX("Trailing garbage \"module q {names...\" after module, expected end-of-input.", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100762 yang_parser_ctx_free(ctx_p);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200763 lys_module_free(m, NULL);
Radek Krejci40544fa2019-01-11 09:38:37 +0100764
Michal Vasko63f3d842020-07-08 10:10:14 +0200765 in.current = "prefix " SCHEMA_BEGINNING "}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200766 m = calloc(1, sizeof *m);
Radek Iša56ca9e42020-09-08 18:42:00 +0200767 m->ctx = YCTX->parsed_mod->mod->ctx;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100768 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m, &unres));
Radek Iša56ca9e42020-09-08 18:42:00 +0200769 CHECK_LOG_CTX("Invalid keyword \"prefix\", expected \"module\" or \"submodule\".", "Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200770 yang_parser_ctx_free(ctx_p);
771 lys_module_free(m, NULL);
Radek Krejci09306362018-10-15 15:26:01 +0200772
Michal Vasko63f3d842020-07-08 10:10:14 +0200773 in.current = "module " SCHEMA_BEGINNING "leaf enum {type enumeration {enum seven { position 7;}}}}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200774 m = calloc(1, sizeof *m);
Radek Iša56ca9e42020-09-08 18:42:00 +0200775 m->ctx = YCTX->parsed_mod->mod->ctx;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100776 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m, &unres));
Radek Iša56ca9e42020-09-08 18:42:00 +0200777 CHECK_LOG_CTX("Invalid keyword \"position\" as a child of \"enum\".", "Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200778 yang_parser_ctx_free(ctx_p);
779 lys_module_free(m, NULL);
David Sedlák9fb515f2019-07-11 10:33:58 +0200780
Radek Krejci156ccaf2018-10-15 15:49:17 +0200781 /* extensions */
782 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100783 assert_string_equal("prefix:test", mod->exts[0].name);
784 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
Radek Iša56ca9e42020-09-08 18:42:00 +0200785 mod = mod_renew(YCTX);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200786
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200787 /* invalid substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +0200788 in.current = SCHEMA_BEGINNING "must false;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100789 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200790 CHECK_LOG_CTX("Invalid keyword \"must\" as a child of \"module\".", "Line number 3.");
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200791
Radek Krejci09306362018-10-15 15:26:01 +0200792 /* submodule */
Radek Iša56ca9e42020-09-08 18:42:00 +0200793 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200794
795 /* missing mandatory substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +0200796 in.current = " subname {}";
Radek Krejci33090f92020-12-17 20:12:46 +0100797 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200798 CHECK_LOG_CTX("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\".", "Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100799 assert_string_equal("subname", submod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +0200800
801 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200802
Michal Vasko63f3d842020-07-08 10:10:14 +0200803 in.current = " subname {belongs-to name {prefix x;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100804 assert_int_equal(LY_SUCCESS, parse_submodule(YCTX, submod));
Michal Vaskoc3781c32020-10-06 14:04:08 +0200805 assert_string_equal("name", submod->mod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +0200806 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200807
808#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +0100809#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +0200810
811 /* duplicated namespace, prefix */
Michal Vasko63f3d842020-07-08 10:10:14 +0200812 in.current = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100813 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200814 CHECK_LOG_CTX("Duplicate keyword \"belongs-to\".", "Line number 3.");
815 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200816
817 /* not allowed in submodule (module-specific) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200818 in.current = SCHEMA_BEGINNING "namespace \"urn:z\";}";
Radek Krejci33090f92020-12-17 20:12:46 +0100819 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200820 CHECK_LOG_CTX("Invalid keyword \"namespace\" as a child of \"submodule\".", "Line number 3.");
821 submod = submod_renew(YCTX);
Michal Vasko63f3d842020-07-08 10:10:14 +0200822 in.current = SCHEMA_BEGINNING "prefix m;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100823 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200824 CHECK_LOG_CTX("Invalid keyword \"prefix\" as a child of \"submodule\".", "Line number 3.");
825 submod = submod_renew(YCTX);
Radek Krejcia042ea12018-10-13 07:52:15 +0200826
Michal Vasko63f3d842020-07-08 10:10:14 +0200827 in.current = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100828 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, YCTX->parsed_mod->mod->ctx, (struct lys_parser_ctx *)YCTX, YCTX->in, &submod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200829 CHECK_LOG_CTX("Trailing garbage \"module q {names...\" after submodule, expected end-of-input.", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100830 yang_parser_ctx_free(ctx_p);
Radek Krejci40544fa2019-01-11 09:38:37 +0100831
Michal Vasko63f3d842020-07-08 10:10:14 +0200832 in.current = "prefix " SCHEMA_BEGINNING "}";
Radek Krejci33090f92020-12-17 20:12:46 +0100833 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, YCTX->parsed_mod->mod->ctx, (struct lys_parser_ctx *)YCTX, YCTX->in, &submod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200834 CHECK_LOG_CTX("Invalid keyword \"prefix\", expected \"module\" or \"submodule\".", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100835 yang_parser_ctx_free(ctx_p);
Radek Iša56ca9e42020-09-08 18:42:00 +0200836 submod = submod_renew(YCTX);
Radek Krejci40544fa2019-01-11 09:38:37 +0100837
Radek Krejcia042ea12018-10-13 07:52:15 +0200838#undef TEST_GENERIC
839#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200840#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +0200841#undef SCHEMA_BEGINNING
Radek Krejciefd22f62018-09-27 11:47:58 +0200842}
843
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200844static void
845test_deviation(void **state)
846{
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200847 struct lysp_deviation *d = NULL;
Radek Krejci33090f92020-12-17 20:12:46 +0100848
849 YCTX_INIT;
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200850
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200851 /* invalid cardinality */
852#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
853 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Iša56ca9e42020-09-08 18:42:00 +0200854 &d, "1", FREE_ARRAY(YCTX->parsed_mod->mod->ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200855
856 TEST_DUP("description", "a", "b");
857 TEST_DUP("reference", "a", "b");
858
859 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +0200860 in.current = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100861 assert_int_equal(LY_SUCCESS, parse_deviation(YCTX, &d));
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200862 assert_non_null(d);
Michal Vasko63f3d842020-07-08 10:10:14 +0200863 assert_string_equal(" ...", in.current);
Radek Iša56ca9e42020-09-08 18:42:00 +0200864 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200865 d = NULL;
866
867 /* missing mandatory substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +0200868 in.current = " test {description text;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100869 assert_int_equal(LY_EVALID, parse_deviation(YCTX, &d));
Radek Iša56ca9e42020-09-08 18:42:00 +0200870 CHECK_LOG_CTX("Missing mandatory keyword \"deviate\" as a child of \"deviation\".", "Line number 1.");
871 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200872 d = NULL;
873
874 /* invalid substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +0200875 in.current = " test {deviate not-supported; status obsolete;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100876 assert_int_equal(LY_EVALID, parse_deviation(YCTX, &d));
Radek Iša56ca9e42020-09-08 18:42:00 +0200877 CHECK_LOG_CTX("Invalid keyword \"status\" as a child of \"deviation\".", "Line number 1.");
878 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200879 d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200880#undef TEST_DUP
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200881}
882
Radek Iša56ca9e42020-09-08 18:42:00 +0200883#define TEST_DEVIATE_SUCCESS(INPUT_TEXT, REMAIN_TEXT)\
884 in.current = INPUT_TEXT;\
Radek Krejci33090f92020-12-17 20:12:46 +0100885 assert_int_equal(LY_SUCCESS, parse_deviate(YCTX, &d));\
Radek Iša56ca9e42020-09-08 18:42:00 +0200886 assert_non_null(d);\
887 assert_string_equal(REMAIN_TEXT, in.current);\
888 lysp_deviate_free(YCTX->parsed_mod->mod->ctx, d); free(d); d = NULL
889
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200890static void
891test_deviate(void **state)
892{
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200893 struct lysp_deviate *d = NULL;
Radek Krejci33090f92020-12-17 20:12:46 +0100894
895 YCTX_INIT;
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200896
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200897 /* invalid cardinality */
898#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
899 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Iša56ca9e42020-09-08 18:42:00 +0200900 &d, "1", lysp_deviate_free(YCTX->parsed_mod->mod->ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200901
902 TEST_DUP("add", "config", "true", "false");
903 TEST_DUP("replace", "default", "int8", "uint8");
904 TEST_DUP("add", "mandatory", "true", "false");
905 TEST_DUP("add", "max-elements", "1", "2");
906 TEST_DUP("add", "min-elements", "1", "2");
907 TEST_DUP("replace", "type", "int8", "uint8");
908 TEST_DUP("add", "units", "kilometers", "miles");
909
910 /* full contents */
Radek Iša56ca9e42020-09-08 18:42:00 +0200911 TEST_DEVIATE_SUCCESS(" not-supported {prefix:ext;} ...", " ...");
912 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;} ...", " ...");
913 TEST_DEVIATE_SUCCESS(" delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...", " ...");
914 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 +0200915
916 /* invalid substatements */
917#define TEST_NOT_SUP(DEV, STMT, VALUE) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200918 in.current = " "DEV" {"STMT" "VALUE";}..."; \
Radek Krejci33090f92020-12-17 20:12:46 +0100919 assert_int_equal(LY_EVALID, parse_deviate(YCTX, &d)); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200920 CHECK_LOG_CTX("Deviate \""DEV"\" does not support keyword \""STMT"\".", "Line number 1.");\
921 lysp_deviate_free(YCTX->parsed_mod->mod->ctx, d); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200922
923 TEST_NOT_SUP("not-supported", "units", "meters");
924 TEST_NOT_SUP("not-supported", "must", "1");
925 TEST_NOT_SUP("not-supported", "unique", "x");
926 TEST_NOT_SUP("not-supported", "default", "a");
927 TEST_NOT_SUP("not-supported", "config", "true");
928 TEST_NOT_SUP("not-supported", "mandatory", "true");
929 TEST_NOT_SUP("not-supported", "min-elements", "1");
930 TEST_NOT_SUP("not-supported", "max-elements", "2");
931 TEST_NOT_SUP("not-supported", "type", "string");
932 TEST_NOT_SUP("add", "type", "string");
933 TEST_NOT_SUP("delete", "config", "true");
934 TEST_NOT_SUP("delete", "mandatory", "true");
935 TEST_NOT_SUP("delete", "min-elements", "1");
936 TEST_NOT_SUP("delete", "max-elements", "2");
937 TEST_NOT_SUP("delete", "type", "string");
938 TEST_NOT_SUP("replace", "must", "1");
939 TEST_NOT_SUP("replace", "unique", "a");
940
Michal Vasko63f3d842020-07-08 10:10:14 +0200941 in.current = " nonsence; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100942 assert_int_equal(LY_EVALID, parse_deviate(YCTX, &d));
Radek Iša56ca9e42020-09-08 18:42:00 +0200943 CHECK_LOG_CTX("Invalid value \"nonsence\" of \"deviate\".", "Line number 1.");\
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200944 assert_null(d);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200945#undef TEST_NOT_SUP
946#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200947}
948
Radek Krejci8c370832018-11-02 15:10:03 +0100949static void
950test_container(void **state)
951{
Radek Krejci8c370832018-11-02 15:10:03 +0100952 struct lysp_node_container *c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +0100953
Radek Krejci33090f92020-12-17 20:12:46 +0100954 YCTX_INIT;
Radek Iša56ca9e42020-09-08 18:42:00 +0200955 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +0100956
957 /* invalid cardinality */
958#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200959 in.current = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +0100960 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node**)&c)); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200961 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
962 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +0100963
964 TEST_DUP("config", "true", "false");
965 TEST_DUP("description", "text1", "text2");
966 TEST_DUP("presence", "true", "false");
967 TEST_DUP("reference", "1", "2");
968 TEST_DUP("status", "current", "obsolete");
969 TEST_DUP("when", "true", "false");
970#undef TEST_DUP
971
972 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +0200973 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 +0100974 "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 Krejci33090f92020-12-17 20:12:46 +0100975 assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +0200976 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 +0100977 assert_non_null(c->actions);
978 assert_non_null(c->child);
Radek Krejci8c370832018-11-02 15:10:03 +0100979 assert_non_null(c->groupings);
Radek Krejci8c370832018-11-02 15:10:03 +0100980 assert_non_null(c->musts);
981 assert_non_null(c->notifs);
982 assert_string_equal("true", c->presence);
Radek Krejci8c370832018-11-02 15:10:03 +0100983 assert_non_null(c->typedefs);
Radek Iša56ca9e42020-09-08 18:42:00 +0200984 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
985 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +0100986
987 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +0200988 in.current = " cont {augment /root;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100989 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +0200990 CHECK_LOG_CTX("Invalid keyword \"augment\" as a child of \"container\".", "Line number 1.");
991 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c); c = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200992 in.current = " cont {nonsence true;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100993 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +0200994 CHECK_LOG_CTX("Invalid character sequence \"nonsence\", expected a keyword.", "Line number 1.");
995 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +0100996
Radek Iša56ca9e42020-09-08 18:42:00 +0200997 YCTX->parsed_mod->version = 1; /* simulate YANG 1.0 */
Michal Vasko63f3d842020-07-08 10:10:14 +0200998 in.current = " cont {action x;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100999 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +02001000 CHECK_LOG_CTX("Invalid keyword \"action\" as a child of \"container\" - "
1001 "the statement is allowed only in YANG 1.1 modules.", "Line number 1.");
1002 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001003}
1004
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001005static void
1006test_leaf(void **state)
1007{
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001008 struct lysp_node_leaf *l = NULL;
Radek Krejci33090f92020-12-17 20:12:46 +01001009
1010 YCTX_INIT;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001011
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001012 /* invalid cardinality */
1013#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001014 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001015 assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node**)&l)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001016 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1017 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001018
1019 TEST_DUP("config", "true", "false");
1020 TEST_DUP("default", "x", "y");
1021 TEST_DUP("description", "text1", "text2");
1022 TEST_DUP("mandatory", "true", "false");
1023 TEST_DUP("reference", "1", "2");
1024 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001025 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001026 TEST_DUP("units", "text1", "text2");
1027 TEST_DUP("when", "true", "false");
1028#undef TEST_DUP
1029
1030 /* full content - without mandatory which is mutual exclusive with default */
Michal Vasko63f3d842020-07-08 10:10:14 +02001031 in.current = "l {config false;default \"xxx\";description test;if-feature f;"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001032 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001033 assert_int_equal(LY_SUCCESS, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001034 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 +02001035 assert_string_equal("xxx", l->dflt.str);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001036 assert_string_equal("yyy", l->units);
1037 assert_string_equal("string", l->type.name);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001038 assert_non_null(l->musts);
Radek Iša56ca9e42020-09-08 18:42:00 +02001039 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001040
1041 /* full content - now with mandatory */
Michal Vasko63f3d842020-07-08 10:10:14 +02001042 in.current = "l {mandatory true; type string;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001043 assert_int_equal(LY_SUCCESS, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001044 CHECK_LYSP_NODE(l, NULL, 0, LYS_MAND_TRUE, 0, "l", 0, LYS_LEAF, 0, NULL, 0);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001045 assert_string_equal("string", l->type.name);
Radek Iša56ca9e42020-09-08 18:42:00 +02001046 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001047
1048 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001049 in.current = " l {description \"missing type\";} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001050 assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001051 CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"leaf\".", "Line number 1.");
1052 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001053}
1054
Radek Krejci0e5d8382018-11-28 16:37:53 +01001055static void
1056test_leaflist(void **state)
1057{
Radek Krejci0e5d8382018-11-28 16:37:53 +01001058 struct lysp_node_leaflist *ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001059
Radek Krejci33090f92020-12-17 20:12:46 +01001060 YCTX_INIT;
Radek Iša56ca9e42020-09-08 18:42:00 +02001061 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001062
1063 /* invalid cardinality */
1064#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001065 in.current = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001066 assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node**)&ll)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001067 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1068 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001069
1070 TEST_DUP("config", "true", "false");
1071 TEST_DUP("description", "text1", "text2");
1072 TEST_DUP("max-elements", "10", "20");
1073 TEST_DUP("min-elements", "10", "20");
1074 TEST_DUP("ordered-by", "user", "system");
1075 TEST_DUP("reference", "1", "2");
1076 TEST_DUP("status", "current", "obsolete");
1077 TEST_DUP("type", "int8", "uint8");
1078 TEST_DUP("units", "text1", "text2");
1079 TEST_DUP("when", "true", "false");
1080#undef TEST_DUP
1081
1082 /* full content - without min-elements which is mutual exclusive with default */
Michal Vasko63f3d842020-07-08 10:10:14 +02001083 in.current = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001084 "max-elements 10;must 'expr';ordered-by user;reference test;"
1085 "status current;type string; units zzz;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001086 assert_int_equal(LY_SUCCESS, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001087 CHECK_LYSP_NODE(ll, "test", 1, 0x446, 1, "ll", 0, LYS_LEAFLIST, 0, "test", 1);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001088 assert_non_null(ll->dflts);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001089 assert_int_equal(2, LY_ARRAY_COUNT(ll->dflts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001090 assert_string_equal("xxx", ll->dflts[0].str);
1091 assert_string_equal("yyy", ll->dflts[1].str);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001092 assert_string_equal("zzz", ll->units);
1093 assert_int_equal(10, ll->max);
1094 assert_int_equal(0, ll->min);
1095 assert_string_equal("string", ll->type.name);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001096 assert_non_null(ll->musts);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001097 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 +02001098 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001099
1100 /* full content - now with min-elements */
Michal Vasko63f3d842020-07-08 10:10:14 +02001101 in.current = "ll {min-elements 10; type string;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001102 assert_int_equal(LY_SUCCESS, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001103 CHECK_LYSP_NODE(ll, NULL, 0, 0x200, 0, "ll", 0, LYS_LEAFLIST, 0, NULL, 0);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001104 assert_string_equal("string", ll->type.name);
1105 assert_int_equal(0, ll->max);
1106 assert_int_equal(10, ll->min);
1107 assert_int_equal(LYS_SET_MIN, ll->flags);
Radek Iša56ca9e42020-09-08 18:42:00 +02001108 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001109
1110 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001111 in.current = " ll {description \"missing type\";} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001112 assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001113 CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"leaf-list\".", "Line number 1.");
1114 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001115
Radek Iša56ca9e42020-09-08 18:42:00 +02001116 YCTX->parsed_mod->version = 1; /* simulate YANG 1.0 - default statement is not allowed */
Michal Vasko63f3d842020-07-08 10:10:14 +02001117 in.current = " ll {default xx; type string;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001118 assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001119 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.");
1120 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001121}
1122
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001123static void
1124test_list(void **state)
1125{
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001126 struct lysp_node_list *l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001127
Radek Krejci33090f92020-12-17 20:12:46 +01001128 YCTX_INIT;
Radek Iša56ca9e42020-09-08 18:42:00 +02001129 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001130
1131 /* invalid cardinality */
1132#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001133 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001134 assert_int_equal(LY_EVALID, parse_list(YCTX, NULL, (struct lysp_node**)&l)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001135 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1136 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001137
1138 TEST_DUP("config", "true", "false");
1139 TEST_DUP("description", "text1", "text2");
1140 TEST_DUP("key", "one", "two");
1141 TEST_DUP("max-elements", "10", "20");
1142 TEST_DUP("min-elements", "10", "20");
1143 TEST_DUP("ordered-by", "user", "system");
1144 TEST_DUP("reference", "1", "2");
1145 TEST_DUP("status", "current", "obsolete");
1146 TEST_DUP("when", "true", "false");
1147#undef TEST_DUP
1148
1149 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001150 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 +01001151 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1152 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001153 assert_int_equal(LY_SUCCESS, parse_list(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001154 CHECK_LYSP_NODE(l, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, 1, "l",
1155 0, LYS_LIST, 0, "test", 1);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001156 assert_string_equal("l", l->key);
1157 assert_non_null(l->uniques);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001158 assert_int_equal(2, LY_ARRAY_COUNT(l->uniques));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001159 assert_string_equal("xxx", l->uniques[0].str);
1160 assert_string_equal("yyy", l->uniques[1].str);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001161 assert_int_equal(10, l->max);
1162 assert_int_equal(1, l->min);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001163 assert_non_null(l->musts);
Radek Iša56ca9e42020-09-08 18:42:00 +02001164 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
1165 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001166
Radek Krejcif538ce52019-03-05 10:46:14 +01001167 /* invalid content */
Radek Iša56ca9e42020-09-08 18:42:00 +02001168 YCTX->parsed_mod->version = 1; /* simulate YANG 1.0 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001169 in.current = "l {action x;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001170 assert_int_equal(LY_EVALID, parse_list(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001171 CHECK_LOG_CTX("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules.", "Line number 1.");
1172 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001173}
1174
Radek Krejci056d0a82018-12-06 16:57:25 +01001175static void
1176test_choice(void **state)
1177{
Radek Krejci056d0a82018-12-06 16:57:25 +01001178 struct lysp_node_choice *ch = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001179
Radek Krejci33090f92020-12-17 20:12:46 +01001180 YCTX_INIT;
Radek Iša56ca9e42020-09-08 18:42:00 +02001181 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci056d0a82018-12-06 16:57:25 +01001182
1183 /* invalid cardinality */
1184#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001185 in.current = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001186 assert_int_equal(LY_EVALID, parse_choice(YCTX, NULL, (struct lysp_node**)&ch)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001187 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1188 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)ch); ch = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001189
1190 TEST_DUP("config", "true", "false");
1191 TEST_DUP("default", "a", "b");
1192 TEST_DUP("description", "text1", "text2");
1193 TEST_DUP("mandatory", "true", "false");
1194 TEST_DUP("reference", "1", "2");
1195 TEST_DUP("status", "current", "obsolete");
1196 TEST_DUP("when", "true", "false");
1197#undef TEST_DUP
1198
Radek Krejcia9026eb2018-12-12 16:04:47 +01001199 /* full content - without default due to a collision with mandatory */
Michal Vasko63f3d842020-07-08 10:10:14 +02001200 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 +01001201 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001202 assert_int_equal(LY_SUCCESS, parse_choice(YCTX, NULL, (struct lysp_node **)&ch));
Radek Iša56ca9e42020-09-08 18:42:00 +02001203 CHECK_LYSP_NODE(ch, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, 1, "ch", 0, LYS_CHOICE, 0, "test", 1);
1204 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ch); ch = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001205
Radek Krejcia9026eb2018-12-12 16:04:47 +01001206 /* full content - the default missing from the previous node */
Michal Vasko63f3d842020-07-08 10:10:14 +02001207 in.current = "ch {default c;case c;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001208 assert_int_equal(LY_SUCCESS, parse_choice(YCTX, NULL, (struct lysp_node **)&ch));
Radek Iša56ca9e42020-09-08 18:42:00 +02001209 CHECK_LYSP_NODE(ch, NULL, 0, 0, 0, "ch", 0, LYS_CHOICE, 0, NULL, 0);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001210 assert_string_equal("c", ch->dflt.str);
Radek Iša56ca9e42020-09-08 18:42:00 +02001211 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ch); ch = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001212}
1213
1214static void
1215test_case(void **state)
1216{
Radek Krejcia9026eb2018-12-12 16:04:47 +01001217 struct lysp_node_case *cs = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001218
Radek Krejci33090f92020-12-17 20:12:46 +01001219 YCTX_INIT;
Radek Iša56ca9e42020-09-08 18:42:00 +02001220 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcia9026eb2018-12-12 16:04:47 +01001221
1222 /* invalid cardinality */
1223#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001224 in.current = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001225 assert_int_equal(LY_EVALID, parse_case(YCTX, NULL, (struct lysp_node**)&cs)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001226 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1227 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)cs); cs = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001228
1229 TEST_DUP("description", "text1", "text2");
1230 TEST_DUP("reference", "1", "2");
1231 TEST_DUP("status", "current", "obsolete");
1232 TEST_DUP("when", "true", "false");
1233#undef TEST_DUP
1234
1235 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001236 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 +01001237 "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001238 assert_int_equal(LY_SUCCESS, parse_case(YCTX, NULL, (struct lysp_node **)&cs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001239 CHECK_LYSP_NODE(cs, "test", 1, LYS_STATUS_CURR, 1, "cs", 0, LYS_CASE, 0, "test", 1);
1240 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)cs); cs = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001241
1242 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001243 in.current = "cs {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001244 assert_int_equal(LY_EVALID, parse_case(YCTX, NULL, (struct lysp_node **)&cs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001245 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"case\".", "Line number 1.");
1246 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)cs); cs = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001247}
1248
Radek Krejci9800fb82018-12-13 14:26:23 +01001249static void
Radek Krejcid6b76452019-09-03 17:03:03 +02001250test_any(void **state, enum ly_stmt kw)
Radek Krejci9800fb82018-12-13 14:26:23 +01001251{
Radek Krejci9800fb82018-12-13 14:26:23 +01001252 struct lysp_node_anydata *any = NULL;
Radek Krejci9800fb82018-12-13 14:26:23 +01001253
Radek Krejci33090f92020-12-17 20:12:46 +01001254 YCTX_INIT;
Radek Krejcid6b76452019-09-03 17:03:03 +02001255 if (kw == LY_STMT_ANYDATA) {
Radek Iša56ca9e42020-09-08 18:42:00 +02001256 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001257 } else {
Radek Iša56ca9e42020-09-08 18:42:00 +02001258 YCTX->parsed_mod->version = 1; /* simulate YANG 1.0 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001259 }
1260
1261 /* invalid cardinality */
1262#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001263 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001264 assert_int_equal(LY_EVALID, parse_any(YCTX, kw, NULL, (struct lysp_node**)&any)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001265 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1266 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)any); any = NULL;
Radek Krejci9800fb82018-12-13 14:26:23 +01001267
1268 TEST_DUP("config", "true", "false");
1269 TEST_DUP("description", "text1", "text2");
1270 TEST_DUP("mandatory", "true", "false");
1271 TEST_DUP("reference", "1", "2");
1272 TEST_DUP("status", "current", "obsolete");
1273 TEST_DUP("when", "true", "false");
1274#undef TEST_DUP
1275
1276 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001277 in.current = "any {config true;description test;if-feature f;mandatory true;must 'expr';reference test;status current;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001278 assert_int_equal(LY_SUCCESS, parse_any(YCTX, kw, NULL, (struct lysp_node **)&any));
Radek Iša56ca9e42020-09-08 18:42:00 +02001279 // CHECK_LYSP_NODE(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, NEXT, TYPE, PARENT, REF, WHEN)
1280 uint16_t node_type = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
1281 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 +01001282 assert_non_null(any->musts);
Radek Iša56ca9e42020-09-08 18:42:00 +02001283 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)any); any = NULL;
Radek Krejci9800fb82018-12-13 14:26:23 +01001284}
1285
1286static void
1287test_anydata(void **state)
1288{
Radek Krejcid6b76452019-09-03 17:03:03 +02001289 return test_any(state, LY_STMT_ANYDATA);
Radek Krejci9800fb82018-12-13 14:26:23 +01001290}
1291
1292static void
1293test_anyxml(void **state)
1294{
Radek Krejcid6b76452019-09-03 17:03:03 +02001295 return test_any(state, LY_STMT_ANYXML);
Radek Krejci9800fb82018-12-13 14:26:23 +01001296}
1297
Radek Krejcie86bf772018-12-14 11:39:53 +01001298static void
1299test_grouping(void **state)
1300{
Radek Krejcie86bf772018-12-14 11:39:53 +01001301 struct lysp_grp *grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001302
Radek Krejci33090f92020-12-17 20:12:46 +01001303 YCTX_INIT;
Radek Iša56ca9e42020-09-08 18:42:00 +02001304 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001305
1306 /* invalid cardinality */
1307#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001308 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001309 assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001310 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1311 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, grp, lysp_grp_free); grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001312
1313 TEST_DUP("description", "text1", "text2");
1314 TEST_DUP("reference", "1", "2");
1315 TEST_DUP("status", "current", "obsolete");
1316#undef TEST_DUP
1317
1318 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001319 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 +01001320 "leaf-list ll {type string;} list li;notification not;reference test;status current;typedef t {type int8;}uses g;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001321 assert_int_equal(LY_SUCCESS, parse_grouping(YCTX, NULL, &grp));
Radek Krejcie86bf772018-12-14 11:39:53 +01001322 assert_non_null(grp);
1323 assert_int_equal(LYS_GROUPING, grp->nodetype);
1324 assert_string_equal("grp", grp->name);
1325 assert_string_equal("test", grp->dsc);
1326 assert_non_null(grp->exts);
1327 assert_string_equal("test", grp->ref);
1328 assert_null(grp->parent);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001329 assert_int_equal(LYS_STATUS_CURR, grp->flags);
Radek Iša56ca9e42020-09-08 18:42:00 +02001330 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
1331 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, grp, lysp_grp_free); grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001332
1333 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001334 in.current = "grp {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001335 assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp));
Radek Iša56ca9e42020-09-08 18:42:00 +02001336 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"grouping\".", "Line number 1.");
1337 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, grp, lysp_grp_free); grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001338
Michal Vasko63f3d842020-07-08 10:10:14 +02001339 in.current = "grp {must 'expr'} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001340 assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp));
Radek Iša56ca9e42020-09-08 18:42:00 +02001341 CHECK_LOG_CTX("Invalid keyword \"must\" as a child of \"grouping\".", "Line number 1.");
1342 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, grp, lysp_grp_free); grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001343}
1344
1345static void
Radek Krejcif538ce52019-03-05 10:46:14 +01001346test_action(void **state)
1347{
Radek Krejcif538ce52019-03-05 10:46:14 +01001348 struct lysp_action *rpcs = NULL;
1349 struct lysp_node_container *c = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001350
Radek Krejci33090f92020-12-17 20:12:46 +01001351 YCTX_INIT;
Radek Iša56ca9e42020-09-08 18:42:00 +02001352 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcif538ce52019-03-05 10:46:14 +01001353
1354 /* invalid cardinality */
1355#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001356 in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001357 assert_int_equal(LY_EVALID, parse_action(YCTX, NULL, &rpcs)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001358 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1359 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, rpcs, lysp_action_free); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001360
1361 TEST_DUP("description", "text1", "text2");
Michal Vaskob83af8a2020-01-06 09:49:22 +01001362 TEST_DUP("input", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
1363 TEST_DUP("output", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
Radek Krejcif538ce52019-03-05 10:46:14 +01001364 TEST_DUP("reference", "1", "2");
1365 TEST_DUP("status", "current", "obsolete");
1366#undef TEST_DUP
1367
1368 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001369 in.current = "top;";
Radek Krejci33090f92020-12-17 20:12:46 +01001370 assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Michal Vasko63f3d842020-07-08 10:10:14 +02001371 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 +01001372 "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1373 " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}"
1374 "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1375 " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001376 assert_int_equal(LY_SUCCESS, parse_action(YCTX, (struct lysp_node *)c, &rpcs));
Radek Krejcif538ce52019-03-05 10:46:14 +01001377 assert_non_null(rpcs);
1378 assert_int_equal(LYS_ACTION, rpcs->nodetype);
1379 assert_string_equal("func", rpcs->name);
1380 assert_string_equal("test", rpcs->dsc);
1381 assert_non_null(rpcs->exts);
1382 assert_non_null(rpcs->iffeatures);
1383 assert_string_equal("test", rpcs->ref);
1384 assert_non_null(rpcs->groupings);
1385 assert_non_null(rpcs->typedefs);
1386 assert_int_equal(LYS_STATUS_CURR, rpcs->flags);
1387 /* input */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001388 assert_int_equal(rpcs->input.nodetype, LYS_INPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001389 assert_non_null(rpcs->input.groupings);
1390 assert_non_null(rpcs->input.exts);
1391 assert_non_null(rpcs->input.musts);
1392 assert_non_null(rpcs->input.typedefs);
1393 assert_non_null(rpcs->input.data);
1394 /* output */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001395 assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001396 assert_non_null(rpcs->output.groupings);
1397 assert_non_null(rpcs->output.exts);
1398 assert_non_null(rpcs->output.musts);
1399 assert_non_null(rpcs->output.typedefs);
1400 assert_non_null(rpcs->output.data);
1401
Radek Iša56ca9e42020-09-08 18:42:00 +02001402 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
1403 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, rpcs, lysp_action_free); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001404
1405 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001406 in.current = "func {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001407 assert_int_equal(LY_EVALID, parse_action(YCTX, NULL, &rpcs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001408 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"rpc\".", "Line number 1.");
1409 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, rpcs, lysp_action_free); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001410
Radek Iša56ca9e42020-09-08 18:42:00 +02001411 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c);
Radek Krejcif538ce52019-03-05 10:46:14 +01001412}
1413
1414static void
Radek Krejcifc11bd72019-04-11 16:00:05 +02001415test_notification(void **state)
1416{
Radek Krejcifc11bd72019-04-11 16:00:05 +02001417 struct lysp_notif *notifs = NULL;
1418 struct lysp_node_container *c = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001419
Radek Krejci33090f92020-12-17 20:12:46 +01001420 YCTX_INIT;
Radek Iša56ca9e42020-09-08 18:42:00 +02001421 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcifc11bd72019-04-11 16:00:05 +02001422
1423 /* invalid cardinality */
1424#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001425 in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001426 assert_int_equal(LY_EVALID, parse_notif(YCTX, NULL, &notifs)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001427 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1428 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, notifs, lysp_notif_free); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001429
1430 TEST_DUP("description", "text1", "text2");
1431 TEST_DUP("reference", "1", "2");
1432 TEST_DUP("status", "current", "obsolete");
1433#undef TEST_DUP
1434
1435 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001436 in.current = "top;";
Radek Krejci33090f92020-12-17 20:12:46 +01001437 assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Michal Vasko63f3d842020-07-08 10:10:14 +02001438 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 +01001439 "leaf-list ll {type int8;} list li; must 1; reference test; status current; typedef mytype {type int8;} uses grp; m:ext;}";
Radek Krejci33090f92020-12-17 20:12:46 +01001440 assert_int_equal(LY_SUCCESS, parse_notif(YCTX, (struct lysp_node *)c, &notifs));
Radek Krejcifc11bd72019-04-11 16:00:05 +02001441 assert_non_null(notifs);
1442 assert_int_equal(LYS_NOTIF, notifs->nodetype);
1443 assert_string_equal("ntf", notifs->name);
1444 assert_string_equal("test", notifs->dsc);
1445 assert_non_null(notifs->exts);
1446 assert_non_null(notifs->iffeatures);
1447 assert_string_equal("test", notifs->ref);
1448 assert_non_null(notifs->groupings);
1449 assert_non_null(notifs->typedefs);
1450 assert_non_null(notifs->musts);
1451 assert_non_null(notifs->data);
1452 assert_int_equal(LYS_STATUS_CURR, notifs->flags);
1453
Radek Iša56ca9e42020-09-08 18:42:00 +02001454 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
1455 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, notifs, lysp_notif_free); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001456
1457 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001458 in.current = "ntf {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001459 assert_int_equal(LY_EVALID, parse_notif(YCTX, NULL, &notifs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001460 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"notification\".", "Line number 1.");
1461 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, notifs, lysp_notif_free); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001462
Radek Iša56ca9e42020-09-08 18:42:00 +02001463 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c);
Radek Krejcifc11bd72019-04-11 16:00:05 +02001464}
1465
1466static void
Radek Krejcie86bf772018-12-14 11:39:53 +01001467test_uses(void **state)
1468{
Radek Krejcie86bf772018-12-14 11:39:53 +01001469 struct lysp_node_uses *u = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001470
Radek Krejci33090f92020-12-17 20:12:46 +01001471 YCTX_INIT;
Radek Iša56ca9e42020-09-08 18:42:00 +02001472 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001473
1474 /* invalid cardinality */
1475#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001476 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001477 assert_int_equal(LY_EVALID, parse_uses(YCTX, NULL, (struct lysp_node**)&u)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001478 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1479 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)u); u = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001480
1481 TEST_DUP("description", "text1", "text2");
1482 TEST_DUP("reference", "1", "2");
1483 TEST_DUP("status", "current", "obsolete");
1484 TEST_DUP("when", "true", "false");
1485#undef TEST_DUP
1486
1487 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001488 in.current = "grpref {augment some/node;description test;if-feature f;reference test;refine some/other/node;status current;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001489 assert_int_equal(LY_SUCCESS, parse_uses(YCTX, NULL, (struct lysp_node **)&u));
Radek Iša56ca9e42020-09-08 18:42:00 +02001490 CHECK_LYSP_NODE(u, "test", 1, LYS_STATUS_CURR, 1, "grpref", 0, LYS_USES, 0, "test", 1);
Radek Krejcie86bf772018-12-14 11:39:53 +01001491 assert_non_null(u->augments);
1492 assert_non_null(u->refines);
Radek Iša56ca9e42020-09-08 18:42:00 +02001493 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)u); u = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001494}
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001495
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001496static void
1497test_augment(void **state)
1498{
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001499 struct lysp_augment *a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001500
Radek Krejci33090f92020-12-17 20:12:46 +01001501 YCTX_INIT;
Radek Iša56ca9e42020-09-08 18:42:00 +02001502 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001503
1504 /* invalid cardinality */
1505#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001506 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001507 assert_int_equal(LY_EVALID, parse_augment(YCTX, NULL, &a)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001508 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1509 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001510
1511 TEST_DUP("description", "text1", "text2");
1512 TEST_DUP("reference", "1", "2");
1513 TEST_DUP("status", "current", "obsolete");
1514 TEST_DUP("when", "true", "false");
1515#undef TEST_DUP
1516
1517 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001518 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 +01001519 "leaf-list ll {type string;} list li;notification not;reference test;status current;uses g;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001520 assert_int_equal(LY_SUCCESS, parse_augment(YCTX, NULL, &a));
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001521 assert_non_null(a);
1522 assert_int_equal(LYS_AUGMENT, a->nodetype);
1523 assert_string_equal("/target/nodeid", a->nodeid);
1524 assert_string_equal("test", a->dsc);
1525 assert_non_null(a->exts);
1526 assert_non_null(a->iffeatures);
1527 assert_string_equal("test", a->ref);
1528 assert_non_null(a->when);
1529 assert_null(a->parent);
1530 assert_int_equal(LYS_STATUS_CURR, a->flags);
Radek Iša56ca9e42020-09-08 18:42:00 +02001531 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001532}
1533
Radek Krejcif09e4e82019-06-14 15:08:11 +02001534static void
1535test_when(void **state)
1536{
Radek Krejcif09e4e82019-06-14 15:08:11 +02001537 struct lysp_when *w = NULL;
Radek Krejcif09e4e82019-06-14 15:08:11 +02001538
Radek Krejci33090f92020-12-17 20:12:46 +01001539 YCTX_INIT;
Radek Iša56ca9e42020-09-08 18:42:00 +02001540 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001541
1542 /* invalid cardinality */
1543#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001544 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001545 assert_int_equal(LY_EVALID, parse_when(YCTX, &w)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001546 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1547 FREE_MEMBER(YCTX->parsed_mod->mod->ctx, w, lysp_when_free); w = NULL;
Radek Krejcif09e4e82019-06-14 15:08:11 +02001548
1549 TEST_DUP("description", "text1", "text2");
1550 TEST_DUP("reference", "1", "2");
1551#undef TEST_DUP
1552
1553 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001554 in.current = "expression {description test;reference test;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001555 assert_int_equal(LY_SUCCESS, parse_when(YCTX, &w));
Radek Krejcif09e4e82019-06-14 15:08:11 +02001556 assert_non_null(w);
1557 assert_string_equal("expression", w->cond);
1558 assert_string_equal("test", w->dsc);
1559 assert_string_equal("test", w->ref);
1560 assert_non_null(w->exts);
Radek Iša56ca9e42020-09-08 18:42:00 +02001561 FREE_MEMBER(YCTX->parsed_mod->mod->ctx, w, lysp_when_free); w = NULL;
Radek Krejcif09e4e82019-06-14 15:08:11 +02001562
1563 /* empty condition */
Michal Vasko63f3d842020-07-08 10:10:14 +02001564 in.current = "\"\";";
Radek Krejci33090f92020-12-17 20:12:46 +01001565 assert_int_equal(LY_SUCCESS, parse_when(YCTX, &w));
Radek Iša56ca9e42020-09-08 18:42:00 +02001566 CHECK_LOG_CTX("Empty argument of when statement does not make sense.", NULL);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001567 assert_non_null(w);
1568 assert_string_equal("", w->cond);
Radek Iša56ca9e42020-09-08 18:42:00 +02001569 FREE_MEMBER(YCTX->parsed_mod->mod->ctx, w, lysp_when_free); w = NULL;
Radek Krejcif09e4e82019-06-14 15:08:11 +02001570}
1571
David Sedlákd6ce6d72019-07-16 17:30:18 +02001572static void
1573test_value(void **state)
1574{
David Sedlákd6ce6d72019-07-16 17:30:18 +02001575 int64_t val = 0;
1576 uint16_t flags = 0;
1577
Radek Krejci33090f92020-12-17 20:12:46 +01001578 YCTX_INIT;
1579
Michal Vasko63f3d842020-07-08 10:10:14 +02001580 in.current = "-0;";
Radek Krejci33090f92020-12-17 20:12:46 +01001581 assert_int_equal(parse_type_enum_value_pos(YCTX, LY_STMT_VALUE, &val, &flags, NULL), LY_SUCCESS);
David Sedlákd6ce6d72019-07-16 17:30:18 +02001582 assert_int_equal(val, 0);
1583
Michal Vasko63f3d842020-07-08 10:10:14 +02001584 in.current = "-0;";
David Sedlákd6ce6d72019-07-16 17:30:18 +02001585 flags = 0;
Radek Krejci33090f92020-12-17 20:12:46 +01001586 assert_int_equal(parse_type_enum_value_pos(YCTX, LY_STMT_POSITION, &val, &flags, NULL), LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +02001587 CHECK_LOG_CTX("Invalid value \"-0\" of \"position\".", "Line number 1.");
David Sedlákd6ce6d72019-07-16 17:30:18 +02001588}
1589
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001590int
1591main(void)
Radek Krejci80dd33e2018-09-26 15:57:18 +02001592{
1593 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +02001594 UTEST(test_helpers, setup, teardown),
1595 UTEST(test_comments, setup, teardown),
1596 UTEST(test_arg, setup, teardown),
1597 UTEST(test_stmts, setup, teardown),
1598 UTEST(test_minmax, setup, teardown),
1599 UTEST(test_module, setup, teardown),
1600 UTEST(test_deviation, setup, teardown),
1601 UTEST(test_deviate, setup, teardown),
1602 UTEST(test_container, setup, teardown),
1603 UTEST(test_leaf, setup, teardown),
1604 UTEST(test_leaflist, setup, teardown),
1605 UTEST(test_list, setup, teardown),
1606 UTEST(test_choice, setup, teardown),
1607 UTEST(test_case, setup, teardown),
1608 UTEST(test_anydata, setup, teardown),
1609 UTEST(test_anyxml, setup, teardown),
1610 UTEST(test_action, setup, teardown),
1611 UTEST(test_notification, setup, teardown),
1612 UTEST(test_grouping, setup, teardown),
1613 UTEST(test_uses, setup, teardown),
1614 UTEST(test_augment, setup, teardown),
1615 UTEST(test_when, setup, teardown),
1616 UTEST(test_value, setup, teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02001617 };
1618
1619 return cmocka_run_group_tests(tests, NULL, NULL);
1620}