blob: 87dc277a706519cd1a8710906acdd637191740e0 [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 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci80dd33e2018-09-26 15:57:18 +020015#include <stdarg.h>
16#include <stddef.h>
17#include <setjmp.h>
18#include <cmocka.h>
19
20#include <stdio.h>
21#include <string.h>
22
Radek Krejci70593c12020-06-13 20:48:09 +020023#include "common.h"
24#include "parser_internal.h"
25#include "tree_schema.h"
26#include "tree_schema_internal.h"
Radek Krejci2d7a47b2019-05-16 13:34:10 +020027
28/* originally static functions from tree_schema_free.c and parser_yang.c */
29void lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020030void lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev);
31void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp);
32void lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action);
33void lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif);
34void lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment);
35void lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d);
36void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
Radek Krejcif09e4e82019-06-14 15:08:11 +020037void lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020038
Michal Vasko63f3d842020-07-08 10:10:14 +020039LY_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);
40LY_ERR buf_store_char(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum yang_arg arg, char **word_p,
Radek Krejci1deb5be2020-08-26 16:43:36 +020041 size_t *word_len, char **word_b, size_t *buf_len, uint8_t need_buf, uint8_t *prefix);
Michal Vasko63f3d842020-07-08 10:10:14 +020042LY_ERR get_keyword(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt *kw, char **word_p, size_t *word_len);
43LY_ERR get_argument(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum yang_arg arg,
Radek Krejci2d7a47b2019-05-16 13:34:10 +020044 uint16_t *flags, char **word_p, char **word_b, size_t *word_len);
Radek Krejci1deb5be2020-08-26 16:43:36 +020045LY_ERR skip_comment(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint8_t comment);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020046
Michal Vasko63f3d842020-07-08 10:10:14 +020047LY_ERR parse_action(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_action **actions);
48LY_ERR parse_any(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt kw, struct lysp_node *parent, struct lysp_node **siblings);
49LY_ERR parse_augment(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_augment **augments);
50LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
51LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
52LY_ERR parse_deviate(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_deviate **deviates);
53LY_ERR parse_deviation(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_deviation **deviations);
54LY_ERR parse_grouping(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_grp **groupings);
55LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
56LY_ERR parse_leaf(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
57LY_ERR parse_leaflist(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
58LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
59LY_ERR parse_maxelements(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts);
60LY_ERR parse_minelements(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts);
61LY_ERR parse_module(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_module *mod);
62LY_ERR parse_notif(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_notif **notifs);
63LY_ERR parse_submodule(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_submodule *submod);
64LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
65LY_ERR parse_when(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_when **when_p);
66LY_ERR parse_type_enum_value_pos(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt val_kw, int64_t *value, uint16_t *flags, struct lysp_ext_instance **exts);
Radek Krejci80dd33e2018-09-26 15:57:18 +020067
68#define BUFSIZE 1024
69char logbuf[BUFSIZE] = {0};
Radek Krejci9ed7a192018-10-31 16:23:51 +010070int store = -1; /* negative for infinite logging, positive for limited logging */
Radek Krejci80dd33e2018-09-26 15:57:18 +020071
72/* set to 0 to printing error messages to stderr instead of checking them in code */
Radek Krejci70853c52018-10-15 14:46:16 +020073#define ENABLE_LOGGER_CHECKING 1
Radek Krejci80dd33e2018-09-26 15:57:18 +020074
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020075#if ENABLE_LOGGER_CHECKING
Radek Krejci80dd33e2018-09-26 15:57:18 +020076static void
77logger(LY_LOG_LEVEL level, const char *msg, const char *path)
78{
79 (void) level; /* unused */
Radek Krejci9ed7a192018-10-31 16:23:51 +010080 if (store) {
81 if (path && path[0]) {
82 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
83 } else {
84 strncpy(logbuf, msg, BUFSIZE - 1);
85 }
86 if (store > 0) {
87 --store;
88 }
Radek Krejci80dd33e2018-09-26 15:57:18 +020089 }
90}
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020091#endif
Radek Krejci80dd33e2018-09-26 15:57:18 +020092
Michal Vasko5d24f6c2020-10-13 13:49:06 +020093static void
94logger_setup(void)
Radek Krejci80dd33e2018-09-26 15:57:18 +020095{
Radek Krejci80dd33e2018-09-26 15:57:18 +020096#if ENABLE_LOGGER_CHECKING
97 ly_set_log_clb(logger, 1);
98#endif
Radek Krejcib1a5dcc2018-11-26 14:50:05 +010099}
100
Radek Krejci80dd33e2018-09-26 15:57:18 +0200101void
102logbuf_clean(void)
103{
104 logbuf[0] = '\0';
105}
106
107#if ENABLE_LOGGER_CHECKING
108# define logbuf_assert(str) assert_string_equal(logbuf, str)
109#else
110# define logbuf_assert(str)
111#endif
112
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200113static int
114setup_f(void **state)
115{
116 struct lys_yang_parser_ctx *pctx;
117 LY_ERR ret;
118
119 logger_setup();
120
121 pctx = calloc(1, sizeof *pctx);
122 pctx->format = LYS_IN_YANG;
123 pctx->pos_type = LY_VLOG_LINE;
124 pctx->line = 1;
125
126 pctx->parsed_mod = calloc(1, sizeof *pctx->parsed_mod);
127 pctx->parsed_mod->mod = calloc(1, sizeof *pctx->parsed_mod->mod);
128 pctx->parsed_mod->mod->parsed = pctx->parsed_mod;
129 ret = ly_ctx_new(NULL, 0, &pctx->parsed_mod->mod->ctx);
130 if (ret) {
131 return ret;
132 }
133
134 *state = pctx;
135 return 0;
136}
137
138static int
139teardown_f(void **state)
140{
141 struct lys_yang_parser_ctx *pctx = *state;
142 struct ly_ctx *ctx = pctx->parsed_mod->mod->ctx;
143
144 lys_module_free(pctx->parsed_mod->mod, NULL);
145 ly_ctx_destroy(ctx, NULL);
146 free(pctx);
147
148 return 0;
149}
150
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200151#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200152 in.current = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200153 assert_int_equal(LY_EVALID, FUNC(ctx, &in, RESULT)); \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200154 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
155 CLEANUP
156
Radek Krejci44ceedc2018-10-02 15:54:31 +0200157static void
158test_helpers(void **state)
159{
Michal Vasko63f3d842020-07-08 10:10:14 +0200160 struct ly_in in = {0};
Radek Krejci404251e2018-10-09 12:06:44 +0200161 char *buf, *p;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200162 size_t len, size;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200163 struct lys_yang_parser_ctx *ctx = *state;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200164 uint8_t prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200165
166 /* storing into buffer */
Michal Vasko63f3d842020-07-08 10:10:14 +0200167 in.current = "abcd";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200168 buf = NULL;
169 size = len = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200170 assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &in, 2, &buf, &size, &len));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200171 assert_int_not_equal(0, size);
172 assert_int_equal(2, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200173 assert_string_equal("cd", in.current);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200174 assert_false(strncmp("ab", buf, 2));
175 free(buf);
Radek Krejci404251e2018-10-09 12:06:44 +0200176 buf = NULL;
177
178 /* invalid first characters */
179 len = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200180 in.current = "2invalid";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200181 assert_int_equal(LY_EVALID, buf_store_char(ctx, &in, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Michal Vasko63f3d842020-07-08 10:10:14 +0200182 in.current = ".invalid";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200183 assert_int_equal(LY_EVALID, buf_store_char(ctx, &in, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Michal Vasko63f3d842020-07-08 10:10:14 +0200184 in.current = "-invalid";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200185 assert_int_equal(LY_EVALID, buf_store_char(ctx, &in, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200186 /* invalid following characters */
187 len = 3; /* number of characters read before the str content */
Michal Vasko63f3d842020-07-08 10:10:14 +0200188 in.current = "!";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200189 assert_int_equal(LY_EVALID, buf_store_char(ctx, &in, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Michal Vasko63f3d842020-07-08 10:10:14 +0200190 in.current = ":";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200191 assert_int_equal(LY_EVALID, buf_store_char(ctx, &in, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200192 /* valid colon for prefixed identifiers */
193 len = size = 0;
194 p = NULL;
David Sedlák40bb13b2019-07-10 14:34:18 +0200195 prefix = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200196 in.current = "x:id";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200197 assert_int_equal(LY_SUCCESS, buf_store_char(ctx, &in, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 0, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200198 assert_int_equal(1, len);
199 assert_null(buf);
Michal Vasko63f3d842020-07-08 10:10:14 +0200200 assert_string_equal(":id", in.current);
Radek Krejci404251e2018-10-09 12:06:44 +0200201 assert_int_equal('x', p[len - 1]);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200202 assert_int_equal(LY_SUCCESS, buf_store_char(ctx, &in, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200203 assert_int_equal(2, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200204 assert_string_equal("id", in.current);
Radek Krejci404251e2018-10-09 12:06:44 +0200205 assert_int_equal(':', p[len - 1]);
206 free(buf);
David Sedlák40bb13b2019-07-10 14:34:18 +0200207 prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200208
209 /* checking identifiers */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200210 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)ctx, ':', 0, NULL));
Radek Krejci1deb5be2020-08-26 16:43:36 +0200211 logbuf_assert("Invalid identifier character ':' (0x003a). Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200212 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)ctx, '#', 1, NULL));
Radek Krejci1deb5be2020-08-26 16:43:36 +0200213 logbuf_assert("Invalid identifier first character '#' (0x0023). Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200214
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200215 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)ctx, 'a', 1, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200216 assert_int_equal(0, prefix);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200217 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)ctx, ':', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200218 assert_int_equal(1, prefix);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200219 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)ctx, ':', 0, &prefix));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200220 assert_int_equal(1, prefix);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200221 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)ctx, 'b', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200222 assert_int_equal(2, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200223 /* second colon is invalid */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200224 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)ctx, ':', 0, &prefix));
Radek Krejci1deb5be2020-08-26 16:43:36 +0200225 logbuf_assert("Invalid identifier character ':' (0x003a). Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200226}
Radek Krejci80dd33e2018-09-26 15:57:18 +0200227
228static void
229test_comments(void **state)
230{
Michal Vasko63f3d842020-07-08 10:10:14 +0200231 struct ly_in in = {0};
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200232 struct lys_yang_parser_ctx *ctx = *state;
Radek Krejciefd22f62018-09-27 11:47:58 +0200233 char *word, *buf;
234 size_t len;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200235
Michal Vasko63f3d842020-07-08 10:10:14 +0200236 in.current = " // this is a text of / one * line */ comment\nargument;";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200237 assert_int_equal(LY_SUCCESS, get_argument(ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200238 assert_string_equal("argument;", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200239 assert_null(buf);
240 assert_int_equal(8, len);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200241
Michal Vasko63f3d842020-07-08 10:10:14 +0200242 in.current = "/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\";";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200243 assert_int_equal(LY_SUCCESS, get_argument(ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200244 assert_string_equal("argument", word);
245 assert_ptr_equal(buf, word);
246 assert_int_equal(8, len);
247 free(word);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200248
Michal Vasko63f3d842020-07-08 10:10:14 +0200249 in.current = " this is one line comment on last line";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200250 assert_int_equal(LY_SUCCESS, skip_comment(ctx, &in, 1));
Michal Vasko63f3d842020-07-08 10:10:14 +0200251 assert_true(in.current[0] == '\0');
Radek Krejci80dd33e2018-09-26 15:57:18 +0200252
Michal Vasko63f3d842020-07-08 10:10:14 +0200253 in.current = " this is a not terminated comment x";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200254 assert_int_equal(LY_EVALID, skip_comment(ctx, &in, 2));
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200255 logbuf_assert("Unexpected end-of-input, non-terminated comment. Line number 5.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200256 assert_true(in.current[0] == '\0');
Radek Krejci80dd33e2018-09-26 15:57:18 +0200257}
258
Radek Krejciefd22f62018-09-27 11:47:58 +0200259static void
260test_arg(void **state)
261{
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200262 struct lys_yang_parser_ctx *ctx = *state;
Michal Vasko63f3d842020-07-08 10:10:14 +0200263 struct ly_in in = {0};
Radek Krejciefd22f62018-09-27 11:47:58 +0200264 char *word, *buf;
265 size_t len;
266
267 /* missing argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200268 in.current = ";";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200269 assert_int_equal(LY_SUCCESS, get_argument(ctx, &in, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200270 assert_null(word);
271
Michal Vasko63f3d842020-07-08 10:10:14 +0200272 in.current = "{";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200273 assert_int_equal(LY_EVALID, get_argument(ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200274 logbuf_assert("Invalid character sequence \"{\", expected an argument. Line number 1.");
275
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200276 /* invalid escape sequence */
Michal Vasko63f3d842020-07-08 10:10:14 +0200277 in.current = "\"\\s\"";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200278 assert_int_equal(LY_EVALID, get_argument(ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200279 logbuf_assert("Double-quoted string unknown special character \'\\s\'. Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200280 in.current = "\'\\s\'"; /* valid, since it is not an escape sequence in single quoted string */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200281 assert_int_equal(LY_SUCCESS, get_argument(ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200282 assert_int_equal(2, len);
283 assert_string_equal("\\s\'", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200284 assert_int_equal('\0', in.current[0]); /* input has been eaten */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200285
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200286 /* invalid character after the argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200287 in.current = "hello\"";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200288 assert_int_equal(LY_EVALID, get_argument(ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200289 logbuf_assert("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200290 in.current = "hello}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200291 assert_int_equal(LY_EVALID, get_argument(ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200292 logbuf_assert("Invalid character sequence \"}\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
293
David Sedlák40bb13b2019-07-10 14:34:18 +0200294 /* invalid identifier-ref-arg-str */
Michal Vasko63f3d842020-07-08 10:10:14 +0200295 in.current = "pre:pre:value";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200296 assert_int_equal(LY_EVALID, get_argument(ctx, &in, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
David Sedlák40bb13b2019-07-10 14:34:18 +0200297
Michal Vasko63f3d842020-07-08 10:10:14 +0200298 in.current = "\"\";"; /* empty identifier is not allowed */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200299 assert_int_equal(LY_EVALID, get_argument(ctx, &in, Y_IDENTIF_ARG, NULL, &word, &buf, &len));
Radek Krejci4e199f52019-05-28 09:09:28 +0200300 logbuf_assert("Statement argument is required. Line number 1.");
301 logbuf_clean();
Michal Vasko63f3d842020-07-08 10:10:14 +0200302 in.current = "\"\";"; /* empty reference identifier is not allowed */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200303 assert_int_equal(LY_EVALID, get_argument(ctx, &in, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
Radek Krejci4e199f52019-05-28 09:09:28 +0200304 logbuf_assert("Statement argument is required. Line number 1.");
305
Michal Vasko63f3d842020-07-08 10:10:14 +0200306 in.current = "hello/x\t"; /* slash is not an invalid character */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200307 assert_int_equal(LY_SUCCESS, get_argument(ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200308 assert_int_equal(7, len);
309 assert_string_equal("hello/x\t", word);
310
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200311 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200312
313 /* different quoting */
Michal Vasko63f3d842020-07-08 10:10:14 +0200314 in.current = "hello ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200315 assert_int_equal(LY_SUCCESS, get_argument(ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200316 assert_null(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200317 assert_int_equal(5, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200318 assert_string_equal("hello ", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200319
Michal Vasko63f3d842020-07-08 10:10:14 +0200320 in.current = "hello/*comment*/\n";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200321 assert_int_equal(LY_SUCCESS, get_argument(ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200322 assert_null(buf);
323 assert_int_equal(5, len);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200324 assert_false(strncmp("hello", word, len));
325
326
Michal Vasko63f3d842020-07-08 10:10:14 +0200327 in.current = "\"hello\\n\\t\\\"\\\\\";";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200328 assert_int_equal(LY_SUCCESS, get_argument(ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
fredgand49fe112019-10-21 20:51:50 +0800329 assert_non_null(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200330 assert_int_equal(9, len);
fredgand49fe112019-10-21 20:51:50 +0800331 assert_string_equal("hello\n\t\"\\", word);
332 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200333
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200334 ctx->indent = 14;
Michal Vasko63f3d842020-07-08 10:10:14 +0200335 in.current = "\"hello \t\n\t\t world!\"";
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200336 /* - space and tabs before newline are stripped out
337 * - space and tabs after newline (indentation) are stripped out
338 */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200339 assert_int_equal(LY_SUCCESS, get_argument(ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200340 assert_non_null(buf);
341 assert_ptr_equal(word, buf);
342 assert_int_equal(14, len);
343 assert_string_equal("hello\n world!", word);
344 free(buf);
Radek Krejciff13cd12019-10-25 15:34:24 +0200345 /* In contrast to previous, the backslash-escaped tabs are expanded after trimming, so they are preserved */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200346 ctx->indent = 14;
Michal Vasko63f3d842020-07-08 10:10:14 +0200347 in.current = "\"hello \\t\n\t\\t world!\"";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200348 assert_int_equal(LY_SUCCESS, get_argument(ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciff13cd12019-10-25 15:34:24 +0200349 assert_non_null(buf);
350 assert_ptr_equal(word, buf);
351 assert_int_equal(16, len);
352 assert_string_equal("hello \t\n\t world!", word);
353 free(buf);
354 /* Do not handle whitespaces after backslash-escaped newline as indentation */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200355 ctx->indent = 14;
Michal Vasko63f3d842020-07-08 10:10:14 +0200356 in.current = "\"hello\\n\t\t world!\"";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200357 assert_int_equal(LY_SUCCESS, get_argument(ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciff13cd12019-10-25 15:34:24 +0200358 assert_non_null(buf);
359 assert_ptr_equal(word, buf);
360 assert_int_equal(15, len);
361 assert_string_equal("hello\n\t\t world!", word);
362 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200363
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200364 ctx->indent = 14;
Michal Vasko63f3d842020-07-08 10:10:14 +0200365 in.current = "\"hello\n \tworld!\"";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200366 assert_int_equal(LY_SUCCESS, get_argument(ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200367 assert_non_null(buf);
368 assert_ptr_equal(word, buf);
369 assert_int_equal(12, len);
370 assert_string_equal("hello\nworld!", word);
371 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200372
Michal Vasko63f3d842020-07-08 10:10:14 +0200373 in.current = "\'hello\'";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200374 assert_int_equal(LY_SUCCESS, get_argument(ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200375 assert_null(buf);
376 assert_int_equal(5, len);
377 assert_false(strncmp("hello", word, 5));
378
Michal Vasko63f3d842020-07-08 10:10:14 +0200379 in.current = "\"hel\" +\t\n\"lo\"";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200380 assert_int_equal(LY_SUCCESS, get_argument(ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200381 assert_ptr_equal(word, buf);
382 assert_int_equal(5, len);
383 assert_string_equal("hello", word);
384 free(buf);
Michal Vasko63f3d842020-07-08 10:10:14 +0200385 in.current = "\"hel\" +\t\nlo"; /* unquoted the second part */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200386 assert_int_equal(LY_EVALID, get_argument(ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciff13cd12019-10-25 15:34:24 +0200387 logbuf_assert("Both string parts divided by '+' must be quoted. Line number 6.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200388
Michal Vasko63f3d842020-07-08 10:10:14 +0200389 in.current = "\'he\'\t\n+ \"llo\"";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200390 assert_int_equal(LY_SUCCESS, get_argument(ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200391 assert_ptr_equal(word, buf);
392 assert_int_equal(5, len);
393 assert_string_equal("hello", word);
394 free(buf);
395
Michal Vasko63f3d842020-07-08 10:10:14 +0200396 in.current = " \t\n\"he\"+\'llo\'";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200397 assert_int_equal(LY_SUCCESS, get_argument(ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200398 assert_ptr_equal(word, buf);
399 assert_int_equal(5, len);
400 assert_string_equal("hello", word);
401 free(buf);
402
Radek Krejci44ceedc2018-10-02 15:54:31 +0200403 /* missing argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200404 in.current = ";";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200405 assert_int_equal(LY_EVALID, get_argument(ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciff13cd12019-10-25 15:34:24 +0200406 logbuf_assert("Invalid character sequence \";\", expected an argument. Line number 8.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200407}
408
409static void
410test_stmts(void **state)
411{
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200412 struct lys_yang_parser_ctx *ctx = *state;
Michal Vasko63f3d842020-07-08 10:10:14 +0200413 struct ly_in in = {0};
414 const char *p;
Radek Krejcid6b76452019-09-03 17:03:03 +0200415 enum ly_stmt kw;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200416 char *word;
417 size_t len;
418
Michal Vasko63f3d842020-07-08 10:10:14 +0200419 in.current = "\n// comment\n\tinput\t{";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200420 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200421 assert_int_equal(LY_STMT_INPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200422 assert_int_equal(5, len);
423 assert_string_equal("input\t{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200424 assert_string_equal("\t{", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200425
Michal Vasko63f3d842020-07-08 10:10:14 +0200426 in.current = "\t /* comment */\t output\n\t{";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200427 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200428 assert_int_equal(LY_STMT_OUTPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200429 assert_int_equal(6, len);
430 assert_string_equal("output\n\t{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200431 assert_string_equal("\n\t{", in.current);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200432 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200433 assert_int_equal(LY_STMT_SYNTAX_LEFT_BRACE, kw);
Radek Krejciabdd8062019-06-11 16:44:19 +0200434 assert_int_equal(1, len);
435 assert_string_equal("{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200436 assert_string_equal("", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200437
Michal Vasko63f3d842020-07-08 10:10:14 +0200438 in.current = "/input { "; /* invalid slash */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200439 assert_int_equal(LY_EVALID, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200440 logbuf_assert("Invalid identifier first character '/'. Line number 4.");
441
Michal Vasko63f3d842020-07-08 10:10:14 +0200442 in.current = "not-a-statement-nor-extension { "; /* invalid identifier */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200443 assert_int_equal(LY_EVALID, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200444 logbuf_assert("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword. Line number 4.");
445
Michal Vasko63f3d842020-07-08 10:10:14 +0200446 in.current = "path;"; /* missing sep after the keyword */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200447 assert_int_equal(LY_EVALID, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200448 logbuf_assert("Invalid character sequence \"path;\", expected a keyword followed by a separator. Line number 4.");
449
Michal Vasko63f3d842020-07-08 10:10:14 +0200450 in.current = "action ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200451 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200452 assert_int_equal(LY_STMT_ACTION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200453 assert_int_equal(6, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200454 in.current = "anydata ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200455 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200456 assert_int_equal(LY_STMT_ANYDATA, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200457 assert_int_equal(7, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200458 in.current = "anyxml ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200459 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200460 assert_int_equal(LY_STMT_ANYXML, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200461 assert_int_equal(6, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200462 in.current = "argument ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200463 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200464 assert_int_equal(LY_STMT_ARGUMENT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200465 assert_int_equal(8, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200466 in.current = "augment ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200467 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200468 assert_int_equal(LY_STMT_AUGMENT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200469 assert_int_equal(7, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200470 in.current = "base ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200471 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200472 assert_int_equal(LY_STMT_BASE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200473 assert_int_equal(4, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200474 in.current = "belongs-to ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200475 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200476 assert_int_equal(LY_STMT_BELONGS_TO, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200477 assert_int_equal(10, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200478 in.current = "bit ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200479 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200480 assert_int_equal(LY_STMT_BIT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200481 assert_int_equal(3, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200482 in.current = "case ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200483 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200484 assert_int_equal(LY_STMT_CASE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200485 assert_int_equal(4, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200486 in.current = "choice ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200487 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200488 assert_int_equal(LY_STMT_CHOICE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200489 assert_int_equal(6, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200490 in.current = "config ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200491 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200492 assert_int_equal(LY_STMT_CONFIG, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200493 assert_int_equal(6, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200494 in.current = "contact ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200495 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200496 assert_int_equal(LY_STMT_CONTACT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200497 assert_int_equal(7, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200498 in.current = "container ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200499 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200500 assert_int_equal(LY_STMT_CONTAINER, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200501 assert_int_equal(9, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200502 in.current = "default ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200503 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200504 assert_int_equal(LY_STMT_DEFAULT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200505 assert_int_equal(7, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200506 in.current = "description ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200507 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200508 assert_int_equal(LY_STMT_DESCRIPTION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200509 assert_int_equal(11, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200510 in.current = "deviate ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200511 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200512 assert_int_equal(LY_STMT_DEVIATE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200513 assert_int_equal(7, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200514 in.current = "deviation ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200515 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200516 assert_int_equal(LY_STMT_DEVIATION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200517 assert_int_equal(9, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200518 in.current = "enum ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200519 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200520 assert_int_equal(LY_STMT_ENUM, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200521 assert_int_equal(4, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200522 in.current = "error-app-tag ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200523 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200524 assert_int_equal(LY_STMT_ERROR_APP_TAG, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200525 assert_int_equal(13, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200526 in.current = "error-message ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200527 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200528 assert_int_equal(LY_STMT_ERROR_MESSAGE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200529 assert_int_equal(13, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200530 in.current = "extension ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200531 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200532 assert_int_equal(LY_STMT_EXTENSION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200533 assert_int_equal(9, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200534 in.current = "feature ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200535 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200536 assert_int_equal(LY_STMT_FEATURE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200537 assert_int_equal(7, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200538 in.current = "fraction-digits ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200539 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200540 assert_int_equal(LY_STMT_FRACTION_DIGITS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200541 assert_int_equal(15, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200542 in.current = "grouping ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200543 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200544 assert_int_equal(LY_STMT_GROUPING, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200545 assert_int_equal(8, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200546 in.current = "identity ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200547 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200548 assert_int_equal(LY_STMT_IDENTITY, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200549 assert_int_equal(8, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200550 in.current = "if-feature ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200551 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200552 assert_int_equal(LY_STMT_IF_FEATURE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200553 assert_int_equal(10, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200554 in.current = "import ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200555 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200556 assert_int_equal(LY_STMT_IMPORT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200557 assert_int_equal(6, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200558 in.current = "include ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200559 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200560 assert_int_equal(LY_STMT_INCLUDE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200561 assert_int_equal(7, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200562 in.current = "input{";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200563 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200564 assert_int_equal(LY_STMT_INPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200565 assert_int_equal(5, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200566 in.current = "key ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200567 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200568 assert_int_equal(LY_STMT_KEY, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200569 assert_int_equal(3, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200570 in.current = "leaf ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200571 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200572 assert_int_equal(LY_STMT_LEAF, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200573 assert_int_equal(4, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200574 in.current = "leaf-list ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200575 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200576 assert_int_equal(LY_STMT_LEAF_LIST, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200577 assert_int_equal(9, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200578 in.current = "length ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200579 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200580 assert_int_equal(LY_STMT_LENGTH, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200581 assert_int_equal(6, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200582 in.current = "list ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200583 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200584 assert_int_equal(LY_STMT_LIST, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200585 assert_int_equal(4, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200586 in.current = "mandatory ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200587 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200588 assert_int_equal(LY_STMT_MANDATORY, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200589 assert_int_equal(9, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200590 in.current = "max-elements ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200591 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200592 assert_int_equal(LY_STMT_MAX_ELEMENTS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200593 assert_int_equal(12, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200594 in.current = "min-elements ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200595 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200596 assert_int_equal(LY_STMT_MIN_ELEMENTS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200597 assert_int_equal(12, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200598 in.current = "modifier ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200599 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200600 assert_int_equal(LY_STMT_MODIFIER, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200601 assert_int_equal(8, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200602 in.current = "module ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200603 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200604 assert_int_equal(LY_STMT_MODULE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200605 assert_int_equal(6, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200606 in.current = "must ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200607 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200608 assert_int_equal(LY_STMT_MUST, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200609 assert_int_equal(4, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200610 in.current = "namespace ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200611 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200612 assert_int_equal(LY_STMT_NAMESPACE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200613 assert_int_equal(9, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200614 in.current = "notification ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200615 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200616 assert_int_equal(LY_STMT_NOTIFICATION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200617 assert_int_equal(12, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200618 in.current = "ordered-by ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200619 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200620 assert_int_equal(LY_STMT_ORDERED_BY, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200621 assert_int_equal(10, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200622 in.current = "organization ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200623 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200624 assert_int_equal(LY_STMT_ORGANIZATION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200625 assert_int_equal(12, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200626 in.current = "output ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200627 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200628 assert_int_equal(LY_STMT_OUTPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200629 assert_int_equal(6, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200630 in.current = "path ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200631 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200632 assert_int_equal(LY_STMT_PATH, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200633 assert_int_equal(4, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200634 in.current = "pattern ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200635 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200636 assert_int_equal(LY_STMT_PATTERN, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200637 assert_int_equal(7, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200638 in.current = "position ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200639 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200640 assert_int_equal(LY_STMT_POSITION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200641 assert_int_equal(8, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200642 in.current = "prefix ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200643 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200644 assert_int_equal(LY_STMT_PREFIX, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200645 assert_int_equal(6, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200646 in.current = "presence ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200647 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200648 assert_int_equal(LY_STMT_PRESENCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200649 assert_int_equal(8, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200650 in.current = "range ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200651 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200652 assert_int_equal(LY_STMT_RANGE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200653 assert_int_equal(5, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200654 in.current = "reference ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200655 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200656 assert_int_equal(LY_STMT_REFERENCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200657 assert_int_equal(9, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200658 in.current = "refine ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200659 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200660 assert_int_equal(LY_STMT_REFINE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200661 assert_int_equal(6, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200662 in.current = "require-instance ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200663 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200664 assert_int_equal(LY_STMT_REQUIRE_INSTANCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200665 assert_int_equal(16, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200666 in.current = "revision ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200667 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200668 assert_int_equal(LY_STMT_REVISION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200669 assert_int_equal(8, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200670 in.current = "revision-date ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200671 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200672 assert_int_equal(LY_STMT_REVISION_DATE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200673 assert_int_equal(13, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200674 in.current = "rpc ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200675 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200676 assert_int_equal(LY_STMT_RPC, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200677 assert_int_equal(3, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200678 in.current = "status ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200679 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200680 assert_int_equal(LY_STMT_STATUS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200681 assert_int_equal(6, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200682 in.current = "submodule ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200683 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200684 assert_int_equal(LY_STMT_SUBMODULE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200685 assert_int_equal(9, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200686 in.current = "type ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200687 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200688 assert_int_equal(LY_STMT_TYPE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200689 assert_int_equal(4, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200690 in.current = "typedef ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200691 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200692 assert_int_equal(LY_STMT_TYPEDEF, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200693 assert_int_equal(7, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200694 in.current = "unique ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200695 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200696 assert_int_equal(LY_STMT_UNIQUE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200697 assert_int_equal(6, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200698 in.current = "units ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200699 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200700 assert_int_equal(LY_STMT_UNITS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200701 assert_int_equal(5, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200702 in.current = "uses ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200703 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200704 assert_int_equal(LY_STMT_USES, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200705 assert_int_equal(4, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200706 in.current = "value ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200707 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200708 assert_int_equal(LY_STMT_VALUE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200709 assert_int_equal(5, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200710 in.current = "when ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200711 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200712 assert_int_equal(LY_STMT_WHEN, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200713 assert_int_equal(4, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200714 in.current = "yang-version ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200715 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200716 assert_int_equal(LY_STMT_YANG_VERSION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200717 assert_int_equal(12, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200718 in.current = "yin-element ";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200719 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200720 assert_int_equal(LY_STMT_YIN_ELEMENT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200721 assert_int_equal(11, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200722 in.current = ";config false;";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200723 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200724 assert_int_equal(LY_STMT_SYNTAX_SEMICOLON, kw);
Radek Krejci626df482018-10-11 15:06:31 +0200725 assert_int_equal(1, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200726 assert_string_equal("config false;", in.current);
727 in.current = "{ config false;";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200728 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200729 assert_int_equal(LY_STMT_SYNTAX_LEFT_BRACE, kw);
Radek Krejci626df482018-10-11 15:06:31 +0200730 assert_int_equal(1, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200731 assert_string_equal(" config false;", in.current);
732 in.current = "}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200733 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200734 assert_int_equal(LY_STMT_SYNTAX_RIGHT_BRACE, kw);
Radek Krejci626df482018-10-11 15:06:31 +0200735 assert_int_equal(1, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200736 assert_string_equal("", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200737
738 /* geenric extension */
Michal Vasko63f3d842020-07-08 10:10:14 +0200739 in.current = p = "nacm:default-deny-write;";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200740 assert_int_equal(LY_SUCCESS, get_keyword(ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200741 assert_int_equal(LY_STMT_EXTENSION_INSTANCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200742 assert_int_equal(23, len);
743 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200744}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200745
Radek Krejci05b13982018-11-28 16:22:07 +0100746static void
747test_minmax(void **state)
748{
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200749 struct lys_yang_parser_ctx *ctx = *state;
Radek Krejci05b13982018-11-28 16:22:07 +0100750 uint16_t flags = 0;
751 uint32_t value = 0;
752 struct lysp_ext_instance *ext = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200753 struct ly_in in = {0};
Radek Krejci05b13982018-11-28 16:22:07 +0100754
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200755 ctx->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci05b13982018-11-28 16:22:07 +0100756
Michal Vasko63f3d842020-07-08 10:10:14 +0200757 in.current = " 1invalid; ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200758 assert_int_equal(LY_EVALID, parse_minelements(ctx, &in, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100759 logbuf_assert("Invalid value \"1invalid\" of \"min-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100760
761 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200762 in.current = " -1; ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200763 assert_int_equal(LY_EVALID, parse_minelements(ctx, &in, &value, &flags, &ext));
Radek Krejci05b13982018-11-28 16:22:07 +0100764 logbuf_assert("Invalid value \"-1\" of \"min-elements\". Line number 1.");
765
Radek Krejcidf6cad12018-11-28 17:10:55 +0100766 /* implementation limit */
767 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200768 in.current = " 4294967296; ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200769 assert_int_equal(LY_EVALID, parse_minelements(ctx, &in, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100770 logbuf_assert("Value \"4294967296\" is out of \"min-elements\" bounds. Line number 1.");
771
Radek Krejci05b13982018-11-28 16:22:07 +0100772 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200773 in.current = " 1; ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200774 assert_int_equal(LY_SUCCESS, parse_minelements(ctx, &in, &value, &flags, &ext));
Radek Krejci05b13982018-11-28 16:22:07 +0100775 assert_int_equal(LYS_SET_MIN, flags);
776 assert_int_equal(1, value);
777
778 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200779 in.current = " 1 {m:ext;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200780 assert_int_equal(LY_SUCCESS, parse_minelements(ctx, &in, &value, &flags, &ext));
Radek Krejci05b13982018-11-28 16:22:07 +0100781 assert_int_equal(LYS_SET_MIN, flags);
782 assert_int_equal(1, value);
783 assert_non_null(ext);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200784 FREE_ARRAY(ctx->parsed_mod->mod->ctx, ext, lysp_ext_instance_free);
Radek Krejci05b13982018-11-28 16:22:07 +0100785 ext = NULL;
786
787 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200788 in.current = " 1 {config true;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200789 assert_int_equal(LY_EVALID, parse_minelements(ctx, &in, &value, &flags, &ext));
Radek Krejci05b13982018-11-28 16:22:07 +0100790 logbuf_assert("Invalid keyword \"config\" as a child of \"min-elements\". Line number 1.");
791
Michal Vasko63f3d842020-07-08 10:10:14 +0200792 in.current = " 1invalid; ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200793 assert_int_equal(LY_EVALID, parse_maxelements(ctx, &in, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100794 logbuf_assert("Invalid value \"1invalid\" of \"max-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100795
796 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200797 in.current = " -1; ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200798 assert_int_equal(LY_EVALID, parse_maxelements(ctx, &in, &value, &flags, &ext));
Radek Krejci05b13982018-11-28 16:22:07 +0100799 logbuf_assert("Invalid value \"-1\" of \"max-elements\". Line number 1.");
800
Radek Krejcidf6cad12018-11-28 17:10:55 +0100801 /* implementation limit */
802 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200803 in.current = " 4294967296; ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200804 assert_int_equal(LY_EVALID, parse_maxelements(ctx, &in, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100805 logbuf_assert("Value \"4294967296\" is out of \"max-elements\" bounds. Line number 1.");
806
Radek Krejci05b13982018-11-28 16:22:07 +0100807 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200808 in.current = " 1; ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200809 assert_int_equal(LY_SUCCESS, parse_maxelements(ctx, &in, &value, &flags, &ext));
Radek Krejci05b13982018-11-28 16:22:07 +0100810 assert_int_equal(LYS_SET_MAX, flags);
811 assert_int_equal(1, value);
812
813 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200814 in.current = " unbounded; ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200815 assert_int_equal(LY_SUCCESS, parse_maxelements(ctx, &in, &value, &flags, &ext));
Radek Krejci05b13982018-11-28 16:22:07 +0100816 assert_int_equal(LYS_SET_MAX, flags);
817 assert_int_equal(0, value);
818
819 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200820 in.current = " 1 {m:ext;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200821 assert_int_equal(LY_SUCCESS, parse_maxelements(ctx, &in, &value, &flags, &ext));
Radek Krejci05b13982018-11-28 16:22:07 +0100822 assert_int_equal(LYS_SET_MAX, flags);
823 assert_int_equal(1, value);
824 assert_non_null(ext);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200825 FREE_ARRAY(ctx->parsed_mod->mod->ctx, ext, lysp_ext_instance_free);
Radek Krejci05b13982018-11-28 16:22:07 +0100826 ext = NULL;
827
828 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200829 in.current = " 1 {config true;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200830 assert_int_equal(LY_EVALID, parse_maxelements(ctx, &in, &value, &flags, &ext));
Radek Krejci05b13982018-11-28 16:22:07 +0100831 logbuf_assert("Invalid keyword \"config\" as a child of \"max-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100832}
833
Radek Krejci9fcacc12018-10-11 15:59:11 +0200834static struct lysp_module *
Michal Vaskob36053d2020-03-26 15:49:30 +0100835mod_renew(struct lys_yang_parser_ctx *ctx)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200836{
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200837 struct ly_ctx *ly_ctx = ctx->parsed_mod->mod->ctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100838
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200839 lys_module_free(ctx->parsed_mod->mod, NULL);
840 ctx->parsed_mod = calloc(1, sizeof *ctx->parsed_mod);
841 ctx->parsed_mod->mod = calloc(1, sizeof *ctx->parsed_mod->mod);
842 ctx->parsed_mod->mod->parsed = ctx->parsed_mod;
843 ctx->parsed_mod->mod->ctx = ly_ctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100844
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200845 return ctx->parsed_mod;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100846}
847
848static struct lysp_submodule *
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200849submod_renew(struct lys_yang_parser_ctx *ctx)
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100850{
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200851 struct ly_ctx *ly_ctx = ctx->parsed_mod->mod->ctx;
852
853 lys_module_free(ctx->parsed_mod->mod, NULL);
854 ctx->parsed_mod = calloc(1, sizeof(struct lysp_submodule));
855 ctx->parsed_mod->mod = calloc(1, sizeof *ctx->parsed_mod->mod);
856 lydict_insert(ly_ctx, "name", 0, &ctx->parsed_mod->mod->name);
857 ctx->parsed_mod->mod->parsed = ctx->parsed_mod;
858 ctx->parsed_mod->mod->ctx = ly_ctx;
859
860 return (struct lysp_submodule *)ctx->parsed_mod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200861}
862
Radek Krejcid33273d2018-10-25 14:55:52 +0200863static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
864 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
865 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
866{
867 *module_data = user_data;
868 *format = LYS_IN_YANG;
869 *free_module_data = NULL;
870 return LY_SUCCESS;
871}
872
Radek Krejci9fcacc12018-10-11 15:59:11 +0200873static void
874test_module(void **state)
875{
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200876 struct lys_yang_parser_ctx *ctx = *state;
Radek Krejci40544fa2019-01-11 09:38:37 +0100877 struct lysp_module *mod = NULL;
878 struct lysp_submodule *submod = NULL;
879 struct lys_module *m;
Michal Vasko63f3d842020-07-08 10:10:14 +0200880 struct ly_in in = {0};
Radek Krejci9fcacc12018-10-11 15:59:11 +0200881
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200882 mod = mod_renew(ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200883
884 /* missing mandatory substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +0200885 in.current = " name {}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200886 assert_int_equal(LY_EVALID, parse_module(ctx, &in, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100887 assert_string_equal("name", mod->mod->name);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200888 logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200889 mod = mod_renew(ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200890
Michal Vasko63f3d842020-07-08 10:10:14 +0200891 in.current = " name {namespace urn:x;}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200892 assert_int_equal(LY_EVALID, parse_module(ctx, &in, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100893 assert_string_equal("urn:x", mod->mod->ns);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200894 logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200895 mod = mod_renew(ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200896
Michal Vasko63f3d842020-07-08 10:10:14 +0200897 in.current = " name {namespace urn:x;prefix \"x\";}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200898 assert_int_equal(LY_SUCCESS, parse_module(ctx, &in, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100899 assert_string_equal("x", mod->mod->prefix);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200900 mod = mod_renew(ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200901
Radek Krejci027d5802018-11-14 16:57:28 +0100902#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
903#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200904#define TEST_NODE(NODETYPE, INPUT, NAME) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200905 in.current = SCHEMA_BEGINNING INPUT; \
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200906 assert_int_equal(LY_SUCCESS, parse_module(ctx, &in, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200907 assert_non_null(mod->data); \
908 assert_int_equal(NODETYPE, mod->data->nodetype); \
909 assert_string_equal(NAME, mod->data->name); \
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200910 mod = mod_renew(ctx);
Radek Krejcia042ea12018-10-13 07:52:15 +0200911#define TEST_GENERIC(INPUT, TARGET, TEST) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200912 in.current = SCHEMA_BEGINNING INPUT; \
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200913 assert_int_equal(LY_SUCCESS, parse_module(ctx, &in, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200914 assert_non_null(TARGET); \
915 TEST; \
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200916 mod = mod_renew(ctx);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100917#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200918 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200919 parse_module, mod, LINE, mod = mod_renew(ctx))
Radek Krejcia042ea12018-10-13 07:52:15 +0200920
921 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100922 TEST_DUP("namespace", "y", "z", "1");
923 TEST_DUP("prefix", "y", "z", "1");
924 TEST_DUP("contact", "a", "b", "1");
925 TEST_DUP("description", "a", "b", "1");
926 TEST_DUP("organization", "a", "b", "1");
927 TEST_DUP("reference", "a", "b", "1");
Radek Krejcia042ea12018-10-13 07:52:15 +0200928
Radek Krejci70853c52018-10-15 14:46:16 +0200929 /* not allowed in module (submodule-specific) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200930 in.current = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200931 assert_int_equal(LY_EVALID, parse_module(ctx, &in, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200932 logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200933 mod = mod_renew(ctx);
Radek Krejci70853c52018-10-15 14:46:16 +0200934
Radek Krejcia042ea12018-10-13 07:52:15 +0200935 /* anydata */
936 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
937 /* anyxml */
938 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
939 /* augment */
940 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200941 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200942 /* choice */
943 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
944 /* contact 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100945 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact,
946 assert_string_equal("firstname surname", mod->mod->contact));
Radek Krejcia042ea12018-10-13 07:52:15 +0200947 /* container */
948 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
949 /* description 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100950 TEST_GENERIC("description \'some description\';}", mod->mod->dsc,
951 assert_string_equal("some description", mod->mod->dsc));
Radek Krejcia042ea12018-10-13 07:52:15 +0200952 /* deviation */
953 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200954 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200955 /* extension */
956 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200957 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200958 /* feature */
959 TEST_GENERIC("feature test;}", mod->features,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200960 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200961 /* grouping */
962 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200963 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200964 /* identity */
965 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200966 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200967 /* import */
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200968 ly_ctx_set_module_imp_clb(ctx->parsed_mod->mod->ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
Radek Krejci086c7132018-10-26 15:29:04 +0200969 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
970 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200971
Radek Krejcia042ea12018-10-13 07:52:15 +0200972 /* import - prefix collision */
Michal Vasko63f3d842020-07-08 10:10:14 +0200973 in.current = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200974 assert_int_equal(LY_EVALID, parse_module(ctx, &in, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200975 logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200976 mod = mod_renew(ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +0200977 in.current = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200978 assert_int_equal(LY_EVALID, parse_module(ctx, &in, mod));
Radek Krejci086c7132018-10-26 15:29:04 +0200979 logbuf_assert("Prefix \"y\" already used to import \"zzz\" module. Line number 2.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200980 mod = mod_renew(ctx);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200981 in.current = "module name10 {yang-version 1.1;namespace urn:x;prefix \"x\";import zzz {prefix y;}import zzz {prefix z;}}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200982 assert_int_equal(lys_parse_mem(ctx->parsed_mod->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_SUCCESS);
Michal Vasko7c8439f2020-08-05 13:25:19 +0200983 logbuf_assert("Single revision of the module \"zzz\" imported twice.");
Radek Krejci70853c52018-10-15 14:46:16 +0200984
Radek Krejcia042ea12018-10-13 07:52:15 +0200985 /* include */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100986 store = 1;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200987 ly_ctx_set_module_imp_clb(ctx->parsed_mod->mod->ctx, test_imp_clb, "module xxx { namespace urn:xxx; prefix x;}");
Michal Vasko63f3d842020-07-08 10:10:14 +0200988 in.current = "module" SCHEMA_BEGINNING "include xxx;}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200989 assert_int_equal(lys_parse_mem(ctx->parsed_mod->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100990 logbuf_assert("Input data contains module in situation when a submodule is expected.");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100991 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200992
Radek Krejci9ed7a192018-10-31 16:23:51 +0100993 store = 1;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200994 ly_ctx_set_module_imp_clb(ctx->parsed_mod->mod->ctx, test_imp_clb, "submodule xxx {belongs-to wrong-name {prefix w;}}");
Michal Vasko63f3d842020-07-08 10:10:14 +0200995 in.current = "module" SCHEMA_BEGINNING "include xxx;}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200996 assert_int_equal(lys_parse_mem(ctx->parsed_mod->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vaskoc3781c32020-10-06 14:04:08 +0200997 logbuf_assert("Submodule \"belongs-to\" value \"wrong-name\" does not match its module name \"name\". Line number 1.");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100998 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200999
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001000 ly_ctx_set_module_imp_clb(ctx->parsed_mod->mod->ctx, test_imp_clb, "submodule xxx {belongs-to name {prefix x;}}");
Radek Krejcid33273d2018-10-25 14:55:52 +02001001 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejci086c7132018-10-26 15:29:04 +02001002 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +02001003
Radek Krejcia042ea12018-10-13 07:52:15 +02001004 /* leaf */
1005 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
1006 /* leaf-list */
1007 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
1008 /* list */
1009 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
1010 /* notification */
1011 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001012 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001013 /* organization 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001014 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org,
1015 assert_string_equal("CESNET a.l.e.", mod->mod->org));
Radek Krejcia042ea12018-10-13 07:52:15 +02001016 /* reference 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001017 TEST_GENERIC("reference RFC7950;}", mod->mod->ref,
1018 assert_string_equal("RFC7950", mod->mod->ref));
Radek Krejcia042ea12018-10-13 07:52:15 +02001019 /* revision */
1020 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib7db73a2018-10-24 14:18:40 +02001021 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +02001022 /* rpc */
1023 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001024 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001025 /* typedef */
1026 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001027 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001028 /* uses */
1029 TEST_NODE(LYS_USES, "uses test;}", "test");
1030 /* yang-version */
Michal Vasko63f3d842020-07-08 10:10:14 +02001031 in.current = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001032 assert_int_equal(LY_EVALID, parse_module(ctx, &in, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +02001033 logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001034 mod = mod_renew(ctx);
Radek Krejci96e48da2020-09-04 13:18:06 +02001035 in.current = SCHEMA_BEGINNING2 "yang-version 1;yang-version 1.1;}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001036 assert_int_equal(LY_EVALID, parse_module(ctx, &in, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +02001037 logbuf_assert("Duplicate keyword \"yang-version\". Line number 3.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001038 mod = mod_renew(ctx);
Radek Krejci96e48da2020-09-04 13:18:06 +02001039 in.current = SCHEMA_BEGINNING2 "yang-version 1;}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001040 assert_int_equal(LY_SUCCESS, parse_module(ctx, &in, mod));
1041 assert_int_equal(1, mod->version);
1042 mod = mod_renew(ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02001043 in.current = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001044 assert_int_equal(LY_SUCCESS, parse_module(ctx, &in, mod));
1045 assert_int_equal(2, mod->version);
1046 mod = mod_renew(ctx);
Radek Krejci40544fa2019-01-11 09:38:37 +01001047
Michal Vaskob36053d2020-03-26 15:49:30 +01001048 struct lys_yang_parser_ctx *ctx_p = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001049 in.current = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001050 m = calloc(1, sizeof *m);
1051 m->ctx = ctx->parsed_mod->mod->ctx;
Michal Vasko63f3d842020-07-08 10:10:14 +02001052 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m));
David Sedlák1b623122019-08-05 15:27:49 +02001053 logbuf_assert("Trailing garbage \"module q {names...\" after module, expected end-of-input. Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +01001054 yang_parser_ctx_free(ctx_p);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001055 lys_module_free(m, NULL);
Radek Krejci40544fa2019-01-11 09:38:37 +01001056
Michal Vasko63f3d842020-07-08 10:10:14 +02001057 in.current = "prefix " SCHEMA_BEGINNING "}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001058 m = calloc(1, sizeof *m);
1059 m->ctx = ctx->parsed_mod->mod->ctx;
Michal Vasko63f3d842020-07-08 10:10:14 +02001060 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m));
David Sedlák1b623122019-08-05 15:27:49 +02001061 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001062 yang_parser_ctx_free(ctx_p);
1063 lys_module_free(m, NULL);
Radek Krejci09306362018-10-15 15:26:01 +02001064
Michal Vasko63f3d842020-07-08 10:10:14 +02001065 in.current = "module " SCHEMA_BEGINNING "leaf enum {type enumeration {enum seven { position 7;}}}}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001066 m = calloc(1, sizeof *m);
1067 m->ctx = ctx->parsed_mod->mod->ctx;
Michal Vasko63f3d842020-07-08 10:10:14 +02001068 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m));
David Sedlák1b623122019-08-05 15:27:49 +02001069 logbuf_assert("Invalid keyword \"position\" as a child of \"enum\". Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001070 yang_parser_ctx_free(ctx_p);
1071 lys_module_free(m, NULL);
David Sedlák9fb515f2019-07-11 10:33:58 +02001072
Radek Krejci156ccaf2018-10-15 15:49:17 +02001073 /* extensions */
1074 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001075 assert_string_equal("prefix:test", mod->exts[0].name);
1076 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001077 mod = mod_renew(ctx);
Radek Krejci156ccaf2018-10-15 15:49:17 +02001078
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001079 /* invalid substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +02001080 in.current = SCHEMA_BEGINNING "must false;}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001081 assert_int_equal(LY_EVALID, parse_module(ctx, &in, mod));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001082 logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3.");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001083
Radek Krejci09306362018-10-15 15:26:01 +02001084 /* submodule */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001085 submod = submod_renew(ctx);
Radek Krejci09306362018-10-15 15:26:01 +02001086
1087 /* missing mandatory substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02001088 in.current = " subname {}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001089 assert_int_equal(LY_EVALID, parse_submodule(ctx, &in, submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001090 assert_string_equal("subname", submod->name);
Radek Krejci09306362018-10-15 15:26:01 +02001091 logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001092 submod = submod_renew(ctx);
Radek Krejci09306362018-10-15 15:26:01 +02001093
Michal Vasko63f3d842020-07-08 10:10:14 +02001094 in.current = " subname {belongs-to name {prefix x;}}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001095 assert_int_equal(LY_SUCCESS, parse_submodule(ctx, &in, submod));
Michal Vaskoc3781c32020-10-06 14:04:08 +02001096 assert_string_equal("name", submod->mod->name);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001097 submod = submod_renew(ctx);
Radek Krejci09306362018-10-15 15:26:01 +02001098
1099#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +01001100#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +02001101
1102 /* duplicated namespace, prefix */
Michal Vasko63f3d842020-07-08 10:10:14 +02001103 in.current = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001104 assert_int_equal(LY_EVALID, parse_submodule(ctx, &in, submod)); \
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001105 logbuf_assert("Duplicate keyword \"belongs-to\". Line number 3."); \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001106 submod = submod_renew(ctx);
Radek Krejci09306362018-10-15 15:26:01 +02001107
1108 /* not allowed in submodule (module-specific) */
Michal Vasko63f3d842020-07-08 10:10:14 +02001109 in.current = SCHEMA_BEGINNING "namespace \"urn:z\";}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001110 assert_int_equal(LY_EVALID, parse_submodule(ctx, &in, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001111 logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001112 submod = submod_renew(ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02001113 in.current = SCHEMA_BEGINNING "prefix m;}}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001114 assert_int_equal(LY_EVALID, parse_submodule(ctx, &in, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001115 logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001116 submod = submod_renew(ctx);
Radek Krejcia042ea12018-10-13 07:52:15 +02001117
Michal Vasko63f3d842020-07-08 10:10:14 +02001118 in.current = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001119 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, ctx->parsed_mod->mod->ctx, (struct lys_parser_ctx *)ctx, &in, &submod));
Michal Vaskob36053d2020-03-26 15:49:30 +01001120 yang_parser_ctx_free(ctx_p);
David Sedlák1b623122019-08-05 15:27:49 +02001121 logbuf_assert("Trailing garbage \"module q {names...\" after submodule, expected end-of-input. Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001122
Michal Vasko63f3d842020-07-08 10:10:14 +02001123 in.current = "prefix " SCHEMA_BEGINNING "}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001124 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, ctx->parsed_mod->mod->ctx, (struct lys_parser_ctx *)ctx, &in, &submod));
Michal Vaskob36053d2020-03-26 15:49:30 +01001125 yang_parser_ctx_free(ctx_p);
David Sedlák1b623122019-08-05 15:27:49 +02001126 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001127 submod = submod_renew(ctx);
Radek Krejci40544fa2019-01-11 09:38:37 +01001128
Radek Krejcia042ea12018-10-13 07:52:15 +02001129#undef TEST_GENERIC
1130#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001131#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +02001132#undef SCHEMA_BEGINNING
Radek Krejciefd22f62018-09-27 11:47:58 +02001133}
1134
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001135static void
1136test_deviation(void **state)
1137{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001138 struct lys_yang_parser_ctx *ctx = *state;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001139 struct lysp_deviation *d = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001140 struct ly_in in = {0};
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001141
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001142 /* invalid cardinality */
1143#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1144 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001145 &d, "1", FREE_ARRAY(ctx->parsed_mod->mod->ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001146
1147 TEST_DUP("description", "a", "b");
1148 TEST_DUP("reference", "a", "b");
1149
1150 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001151 in.current = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001152 assert_int_equal(LY_SUCCESS, parse_deviation(ctx, &in, &d));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001153 assert_non_null(d);
Michal Vasko63f3d842020-07-08 10:10:14 +02001154 assert_string_equal(" ...", in.current);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001155 FREE_ARRAY(ctx->parsed_mod->mod->ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001156 d = NULL;
1157
1158 /* missing mandatory substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +02001159 in.current = " test {description text;}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001160 assert_int_equal(LY_EVALID, parse_deviation(ctx, &in, &d));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001161 logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001162 FREE_ARRAY(ctx->parsed_mod->mod->ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001163 d = NULL;
1164
1165 /* invalid substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +02001166 in.current = " test {deviate not-supported; status obsolete;}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001167 assert_int_equal(LY_EVALID, parse_deviation(ctx, &in, &d));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001168 logbuf_assert("Invalid keyword \"status\" as a child of \"deviation\". Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001169 FREE_ARRAY(ctx->parsed_mod->mod->ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001170 d = NULL;
1171
1172#undef TEST_DUP
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001173}
1174
1175static void
1176test_deviate(void **state)
1177{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001178 struct lys_yang_parser_ctx *ctx = *state;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001179 struct lysp_deviate *d = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001180 struct ly_in in = {0};
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001181
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001182 /* invalid cardinality */
1183#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
1184 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001185 &d, "1", lysp_deviate_free(ctx->parsed_mod->mod->ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001186
1187 TEST_DUP("add", "config", "true", "false");
1188 TEST_DUP("replace", "default", "int8", "uint8");
1189 TEST_DUP("add", "mandatory", "true", "false");
1190 TEST_DUP("add", "max-elements", "1", "2");
1191 TEST_DUP("add", "min-elements", "1", "2");
1192 TEST_DUP("replace", "type", "int8", "uint8");
1193 TEST_DUP("add", "units", "kilometers", "miles");
1194
1195 /* full contents */
Michal Vasko63f3d842020-07-08 10:10:14 +02001196 in.current = " not-supported {prefix:ext;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001197 assert_int_equal(LY_SUCCESS, parse_deviate(ctx, &in, &d));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001198 assert_non_null(d);
Michal Vasko63f3d842020-07-08 10:10:14 +02001199 assert_string_equal(" ...", in.current);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001200 lysp_deviate_free(ctx->parsed_mod->mod->ctx, d); free(d); d = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001201 in.current = " 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;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001202 assert_int_equal(LY_SUCCESS, parse_deviate(ctx, &in, &d));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001203 assert_non_null(d);
Michal Vasko63f3d842020-07-08 10:10:14 +02001204 assert_string_equal(" ...", in.current);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001205 lysp_deviate_free(ctx->parsed_mod->mod->ctx, d); free(d); d = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001206 in.current = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001207 assert_int_equal(LY_SUCCESS, parse_deviate(ctx, &in, &d));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001208 assert_non_null(d);
Michal Vasko63f3d842020-07-08 10:10:14 +02001209 assert_string_equal(" ...", in.current);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001210 lysp_deviate_free(ctx->parsed_mod->mod->ctx, d); free(d); d = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001211 in.current = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001212 assert_int_equal(LY_SUCCESS, parse_deviate(ctx, &in, &d));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001213 assert_non_null(d);
Michal Vasko63f3d842020-07-08 10:10:14 +02001214 assert_string_equal(" ...", in.current);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001215 lysp_deviate_free(ctx->parsed_mod->mod->ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001216
1217 /* invalid substatements */
1218#define TEST_NOT_SUP(DEV, STMT, VALUE) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001219 in.current = " "DEV" {"STMT" "VALUE";}..."; \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001220 assert_int_equal(LY_EVALID, parse_deviate(ctx, &in, &d)); \
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001221 logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001222 lysp_deviate_free(ctx->parsed_mod->mod->ctx, d); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001223
1224 TEST_NOT_SUP("not-supported", "units", "meters");
1225 TEST_NOT_SUP("not-supported", "must", "1");
1226 TEST_NOT_SUP("not-supported", "unique", "x");
1227 TEST_NOT_SUP("not-supported", "default", "a");
1228 TEST_NOT_SUP("not-supported", "config", "true");
1229 TEST_NOT_SUP("not-supported", "mandatory", "true");
1230 TEST_NOT_SUP("not-supported", "min-elements", "1");
1231 TEST_NOT_SUP("not-supported", "max-elements", "2");
1232 TEST_NOT_SUP("not-supported", "type", "string");
1233 TEST_NOT_SUP("add", "type", "string");
1234 TEST_NOT_SUP("delete", "config", "true");
1235 TEST_NOT_SUP("delete", "mandatory", "true");
1236 TEST_NOT_SUP("delete", "min-elements", "1");
1237 TEST_NOT_SUP("delete", "max-elements", "2");
1238 TEST_NOT_SUP("delete", "type", "string");
1239 TEST_NOT_SUP("replace", "must", "1");
1240 TEST_NOT_SUP("replace", "unique", "a");
1241
Michal Vasko63f3d842020-07-08 10:10:14 +02001242 in.current = " nonsence; ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001243 assert_int_equal(LY_EVALID, parse_deviate(ctx, &in, &d));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001244 logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1.");
1245 assert_null(d);
1246
1247#undef TEST_NOT_SUP
1248#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001249}
1250
Radek Krejci8c370832018-11-02 15:10:03 +01001251static void
1252test_container(void **state)
1253{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001254 struct lys_yang_parser_ctx *ctx = *state;
Radek Krejci8c370832018-11-02 15:10:03 +01001255 struct lysp_node_container *c = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001256 struct ly_in in = {0};
Radek Krejci8c370832018-11-02 15:10:03 +01001257
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001258 ctx->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +01001259
1260 /* invalid cardinality */
1261#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001262 in.current = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001263 assert_int_equal(LY_EVALID, parse_container(ctx, &in, NULL, (struct lysp_node**)&c)); \
Radek Krejci8c370832018-11-02 15:10:03 +01001264 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001265 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001266
1267 TEST_DUP("config", "true", "false");
1268 TEST_DUP("description", "text1", "text2");
1269 TEST_DUP("presence", "true", "false");
1270 TEST_DUP("reference", "1", "2");
1271 TEST_DUP("status", "current", "obsolete");
1272 TEST_DUP("when", "true", "false");
1273#undef TEST_DUP
1274
1275 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001276 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 Krejci313d9902018-11-08 09:42:58 +01001277 "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;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001278 assert_int_equal(LY_SUCCESS, parse_container(ctx, &in, NULL, (struct lysp_node**)&c));
Radek Krejci8c370832018-11-02 15:10:03 +01001279 assert_non_null(c);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001280 assert_int_equal(LYS_CONTAINER, c->nodetype);
1281 assert_string_equal("cont", c->name);
Radek Krejci8c370832018-11-02 15:10:03 +01001282 assert_non_null(c->actions);
1283 assert_non_null(c->child);
1284 assert_string_equal("test", c->dsc);
1285 assert_non_null(c->exts);
1286 assert_non_null(c->groupings);
1287 assert_non_null(c->iffeatures);
1288 assert_non_null(c->musts);
1289 assert_non_null(c->notifs);
1290 assert_string_equal("true", c->presence);
1291 assert_string_equal("test", c->ref);
1292 assert_non_null(c->typedefs);
1293 assert_non_null(c->when);
1294 assert_null(c->parent);
1295 assert_null(c->next);
1296 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001297 ly_set_erase(&ctx->tpdfs_nodes, NULL);
1298 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001299
1300 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001301 in.current = " cont {augment /root;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001302 assert_int_equal(LY_EVALID, parse_container(ctx, &in, NULL, (struct lysp_node**)&c));
Radek Krejci8c370832018-11-02 15:10:03 +01001303 logbuf_assert("Invalid keyword \"augment\" as a child of \"container\". Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001304 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)c); c = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001305 in.current = " cont {nonsence true;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001306 assert_int_equal(LY_EVALID, parse_container(ctx, &in, NULL, (struct lysp_node**)&c));
Radek Krejci8c370832018-11-02 15:10:03 +01001307 logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001308 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001309
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001310 ctx->parsed_mod->version = 1; /* simulate YANG 1.0 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001311 in.current = " cont {action x;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001312 assert_int_equal(LY_EVALID, parse_container(ctx, &in, NULL, (struct lysp_node**)&c));
Radek Krejcif538ce52019-03-05 10:46:14 +01001313 logbuf_assert("Invalid keyword \"action\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001314 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001315}
1316
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001317static void
1318test_leaf(void **state)
1319{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001320 struct lys_yang_parser_ctx *ctx = *state;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001321 struct lysp_node_leaf *l = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001322 struct ly_in in = {0};
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001323
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001324 /* invalid cardinality */
1325#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001326 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001327 assert_int_equal(LY_EVALID, parse_leaf(ctx, &in, NULL, (struct lysp_node**)&l)); \
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001328 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001329 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001330
1331 TEST_DUP("config", "true", "false");
1332 TEST_DUP("default", "x", "y");
1333 TEST_DUP("description", "text1", "text2");
1334 TEST_DUP("mandatory", "true", "false");
1335 TEST_DUP("reference", "1", "2");
1336 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001337 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001338 TEST_DUP("units", "text1", "text2");
1339 TEST_DUP("when", "true", "false");
1340#undef TEST_DUP
1341
1342 /* full content - without mandatory which is mutual exclusive with default */
Michal Vasko63f3d842020-07-08 10:10:14 +02001343 in.current = "l {config false;default \"xxx\";description test;if-feature f;"
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001344 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001345 assert_int_equal(LY_SUCCESS, parse_leaf(ctx, &in, NULL, (struct lysp_node**)&l));
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001346 assert_non_null(l);
1347 assert_int_equal(LYS_LEAF, l->nodetype);
1348 assert_string_equal("l", l->name);
1349 assert_string_equal("test", l->dsc);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001350 assert_string_equal("xxx", l->dflt.str);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001351 assert_string_equal("yyy", l->units);
1352 assert_string_equal("string", l->type.name);
1353 assert_non_null(l->exts);
1354 assert_non_null(l->iffeatures);
1355 assert_non_null(l->musts);
1356 assert_string_equal("test", l->ref);
1357 assert_non_null(l->when);
1358 assert_null(l->parent);
1359 assert_null(l->next);
1360 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, l->flags);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001361 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001362
1363 /* full content - now with mandatory */
Michal Vasko63f3d842020-07-08 10:10:14 +02001364 in.current = "l {mandatory true; type string;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001365 assert_int_equal(LY_SUCCESS, parse_leaf(ctx, &in, NULL, (struct lysp_node**)&l));
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001366 assert_non_null(l);
1367 assert_int_equal(LYS_LEAF, l->nodetype);
1368 assert_string_equal("l", l->name);
1369 assert_string_equal("string", l->type.name);
1370 assert_int_equal(LYS_MAND_TRUE, l->flags);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001371 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001372
1373 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001374 in.current = " l {description \"missing type\";} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001375 assert_int_equal(LY_EVALID, parse_leaf(ctx, &in, NULL, (struct lysp_node**)&l));
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001376 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf\". Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001377 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001378}
1379
Radek Krejci0e5d8382018-11-28 16:37:53 +01001380static void
1381test_leaflist(void **state)
1382{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001383 struct lys_yang_parser_ctx *ctx = *state;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001384 struct lysp_node_leaflist *ll = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001385 struct ly_in in = {0};
Radek Krejci0e5d8382018-11-28 16:37:53 +01001386
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001387 ctx->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001388
1389 /* invalid cardinality */
1390#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001391 in.current = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001392 assert_int_equal(LY_EVALID, parse_leaflist(ctx, &in, NULL, (struct lysp_node**)&ll)); \
Radek Krejci0e5d8382018-11-28 16:37:53 +01001393 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001394 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001395
1396 TEST_DUP("config", "true", "false");
1397 TEST_DUP("description", "text1", "text2");
1398 TEST_DUP("max-elements", "10", "20");
1399 TEST_DUP("min-elements", "10", "20");
1400 TEST_DUP("ordered-by", "user", "system");
1401 TEST_DUP("reference", "1", "2");
1402 TEST_DUP("status", "current", "obsolete");
1403 TEST_DUP("type", "int8", "uint8");
1404 TEST_DUP("units", "text1", "text2");
1405 TEST_DUP("when", "true", "false");
1406#undef TEST_DUP
1407
1408 /* full content - without min-elements which is mutual exclusive with default */
Michal Vasko63f3d842020-07-08 10:10:14 +02001409 in.current = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
Radek Krejci0e5d8382018-11-28 16:37:53 +01001410 "max-elements 10;must 'expr';ordered-by user;reference test;"
1411 "status current;type string; units zzz;when true;m:ext;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001412 assert_int_equal(LY_SUCCESS, parse_leaflist(ctx, &in, NULL, (struct lysp_node**)&ll));
Radek Krejci0e5d8382018-11-28 16:37:53 +01001413 assert_non_null(ll);
1414 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1415 assert_string_equal("ll", ll->name);
1416 assert_string_equal("test", ll->dsc);
1417 assert_non_null(ll->dflts);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001418 assert_int_equal(2, LY_ARRAY_COUNT(ll->dflts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001419 assert_string_equal("xxx", ll->dflts[0].str);
1420 assert_string_equal("yyy", ll->dflts[1].str);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001421 assert_string_equal("zzz", ll->units);
1422 assert_int_equal(10, ll->max);
1423 assert_int_equal(0, ll->min);
1424 assert_string_equal("string", ll->type.name);
1425 assert_non_null(ll->exts);
1426 assert_non_null(ll->iffeatures);
1427 assert_non_null(ll->musts);
1428 assert_string_equal("test", ll->ref);
1429 assert_non_null(ll->when);
1430 assert_null(ll->parent);
1431 assert_null(ll->next);
1432 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001433 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001434
1435 /* full content - now with min-elements */
Michal Vasko63f3d842020-07-08 10:10:14 +02001436 in.current = "ll {min-elements 10; type string;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001437 assert_int_equal(LY_SUCCESS, parse_leaflist(ctx, &in, NULL, (struct lysp_node**)&ll));
Radek Krejci0e5d8382018-11-28 16:37:53 +01001438 assert_non_null(ll);
1439 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1440 assert_string_equal("ll", ll->name);
1441 assert_string_equal("string", ll->type.name);
1442 assert_int_equal(0, ll->max);
1443 assert_int_equal(10, ll->min);
1444 assert_int_equal(LYS_SET_MIN, ll->flags);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001445 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001446
1447 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001448 in.current = " ll {description \"missing type\";} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001449 assert_int_equal(LY_EVALID, parse_leaflist(ctx, &in, NULL, (struct lysp_node**)&ll));
Radek Krejci0e5d8382018-11-28 16:37:53 +01001450 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf-list\". Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001451 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001452
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001453 ctx->parsed_mod->version = 1; /* simulate YANG 1.0 - default statement is not allowed */
Michal Vasko63f3d842020-07-08 10:10:14 +02001454 in.current = " ll {default xx; type string;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001455 assert_int_equal(LY_EVALID, parse_leaflist(ctx, &in, NULL, (struct lysp_node**)&ll));
Radek Krejci0e5d8382018-11-28 16:37:53 +01001456 logbuf_assert("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001457 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001458}
1459
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001460static void
1461test_list(void **state)
1462{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001463 struct lys_yang_parser_ctx *ctx = *state;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001464 struct lysp_node_list *l = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001465 struct ly_in in = {0};
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001466
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001467 ctx->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001468
1469 /* invalid cardinality */
1470#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001471 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001472 assert_int_equal(LY_EVALID, parse_list(ctx, &in, NULL, (struct lysp_node**)&l)); \
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001473 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001474 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001475
1476 TEST_DUP("config", "true", "false");
1477 TEST_DUP("description", "text1", "text2");
1478 TEST_DUP("key", "one", "two");
1479 TEST_DUP("max-elements", "10", "20");
1480 TEST_DUP("min-elements", "10", "20");
1481 TEST_DUP("ordered-by", "user", "system");
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 = "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 Krejci9bb94eb2018-12-04 16:48:35 +01001489 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1490 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001491 assert_int_equal(LY_SUCCESS, parse_list(ctx, &in, NULL, (struct lysp_node**)&l));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001492 assert_non_null(l);
1493 assert_int_equal(LYS_LIST, l->nodetype);
1494 assert_string_equal("l", l->name);
1495 assert_string_equal("test", l->dsc);
1496 assert_string_equal("l", l->key);
1497 assert_non_null(l->uniques);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001498 assert_int_equal(2, LY_ARRAY_COUNT(l->uniques));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001499 assert_string_equal("xxx", l->uniques[0].str);
1500 assert_string_equal("yyy", l->uniques[1].str);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001501 assert_int_equal(10, l->max);
1502 assert_int_equal(1, l->min);
1503 assert_non_null(l->exts);
1504 assert_non_null(l->iffeatures);
1505 assert_non_null(l->musts);
1506 assert_string_equal("test", l->ref);
1507 assert_non_null(l->when);
1508 assert_null(l->parent);
1509 assert_null(l->next);
1510 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, l->flags);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001511 ly_set_erase(&ctx->tpdfs_nodes, NULL);
1512 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001513
Radek Krejcif538ce52019-03-05 10:46:14 +01001514 /* invalid content */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001515 ctx->parsed_mod->version = 1; /* simulate YANG 1.0 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001516 in.current = "l {action x;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001517 assert_int_equal(LY_EVALID, parse_list(ctx, &in, NULL, (struct lysp_node**)&l));
Radek Krejcif538ce52019-03-05 10:46:14 +01001518 logbuf_assert("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001519 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001520}
1521
Radek Krejci056d0a82018-12-06 16:57:25 +01001522static void
1523test_choice(void **state)
1524{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001525 struct lys_yang_parser_ctx *ctx = *state;
Radek Krejci056d0a82018-12-06 16:57:25 +01001526 struct lysp_node_choice *ch = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001527 struct ly_in in = {0};
Radek Krejci056d0a82018-12-06 16:57:25 +01001528
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001529 ctx->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci056d0a82018-12-06 16:57:25 +01001530
1531 /* invalid cardinality */
1532#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001533 in.current = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001534 assert_int_equal(LY_EVALID, parse_choice(ctx, &in, NULL, (struct lysp_node**)&ch)); \
Radek Krejci056d0a82018-12-06 16:57:25 +01001535 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001536 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)ch); ch = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001537
1538 TEST_DUP("config", "true", "false");
1539 TEST_DUP("default", "a", "b");
1540 TEST_DUP("description", "text1", "text2");
1541 TEST_DUP("mandatory", "true", "false");
1542 TEST_DUP("reference", "1", "2");
1543 TEST_DUP("status", "current", "obsolete");
1544 TEST_DUP("when", "true", "false");
1545#undef TEST_DUP
1546
Radek Krejcia9026eb2018-12-12 16:04:47 +01001547 /* full content - without default due to a collision with mandatory */
Michal Vasko63f3d842020-07-08 10:10:14 +02001548 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 Krejci056d0a82018-12-06 16:57:25 +01001549 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001550 assert_int_equal(LY_SUCCESS, parse_choice(ctx, &in, NULL, (struct lysp_node**)&ch));
Radek Krejci056d0a82018-12-06 16:57:25 +01001551 assert_non_null(ch);
1552 assert_int_equal(LYS_CHOICE, ch->nodetype);
1553 assert_string_equal("ch", ch->name);
1554 assert_string_equal("test", ch->dsc);
1555 assert_non_null(ch->exts);
1556 assert_non_null(ch->iffeatures);
1557 assert_string_equal("test", ch->ref);
1558 assert_non_null(ch->when);
1559 assert_null(ch->parent);
1560 assert_null(ch->next);
1561 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, ch->flags);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001562 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)ch); ch = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001563
Radek Krejcia9026eb2018-12-12 16:04:47 +01001564 /* full content - the default missing from the previous node */
Michal Vasko63f3d842020-07-08 10:10:14 +02001565 in.current = "ch {default c;case c;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001566 assert_int_equal(LY_SUCCESS, parse_choice(ctx, &in, NULL, (struct lysp_node**)&ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001567 assert_non_null(ch);
1568 assert_int_equal(LYS_CHOICE, ch->nodetype);
1569 assert_string_equal("ch", ch->name);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001570 assert_string_equal("c", ch->dflt.str);
Radek Krejcia9026eb2018-12-12 16:04:47 +01001571 assert_int_equal(0, ch->flags);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001572 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)ch); ch = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001573}
1574
1575static void
1576test_case(void **state)
1577{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001578 struct lys_yang_parser_ctx *ctx = *state;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001579 struct lysp_node_case *cs = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001580 struct ly_in in = {0};
Radek Krejcia9026eb2018-12-12 16:04:47 +01001581
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001582 ctx->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcia9026eb2018-12-12 16:04:47 +01001583
1584 /* invalid cardinality */
1585#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001586 in.current = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001587 assert_int_equal(LY_EVALID, parse_case(ctx, &in, NULL, (struct lysp_node**)&cs)); \
Radek Krejcia9026eb2018-12-12 16:04:47 +01001588 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001589 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)cs); cs = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001590
1591 TEST_DUP("description", "text1", "text2");
1592 TEST_DUP("reference", "1", "2");
1593 TEST_DUP("status", "current", "obsolete");
1594 TEST_DUP("when", "true", "false");
1595#undef TEST_DUP
1596
1597 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001598 in.current = "cs {anydata any;anyxml anyxml; choice ch;container c;description test;if-feature f;leaf l {type string;}"
Radek Krejcia9026eb2018-12-12 16:04:47 +01001599 "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001600 assert_int_equal(LY_SUCCESS, parse_case(ctx, &in, NULL, (struct lysp_node**)&cs));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001601 assert_non_null(cs);
1602 assert_int_equal(LYS_CASE, cs->nodetype);
1603 assert_string_equal("cs", cs->name);
1604 assert_string_equal("test", cs->dsc);
1605 assert_non_null(cs->exts);
1606 assert_non_null(cs->iffeatures);
1607 assert_string_equal("test", cs->ref);
1608 assert_non_null(cs->when);
1609 assert_null(cs->parent);
1610 assert_null(cs->next);
1611 assert_int_equal(LYS_STATUS_CURR, cs->flags);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001612 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)cs); cs = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001613
1614 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001615 in.current = "cs {config true} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001616 assert_int_equal(LY_EVALID, parse_case(ctx, &in, NULL, (struct lysp_node**)&cs));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001617 logbuf_assert("Invalid keyword \"config\" as a child of \"case\". Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001618 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)cs); cs = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001619}
1620
Radek Krejci9800fb82018-12-13 14:26:23 +01001621static void
Radek Krejcid6b76452019-09-03 17:03:03 +02001622test_any(void **state, enum ly_stmt kw)
Radek Krejci9800fb82018-12-13 14:26:23 +01001623{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001624 struct lys_yang_parser_ctx *ctx = *state;
Radek Krejci9800fb82018-12-13 14:26:23 +01001625 struct lysp_node_anydata *any = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001626 struct ly_in in = {0};
Radek Krejci9800fb82018-12-13 14:26:23 +01001627
Radek Krejcid6b76452019-09-03 17:03:03 +02001628 if (kw == LY_STMT_ANYDATA) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001629 ctx->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001630 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001631 ctx->parsed_mod->version = 1; /* simulate YANG 1.0 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001632 }
1633
1634 /* invalid cardinality */
1635#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001636 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001637 assert_int_equal(LY_EVALID, parse_any(ctx, &in, kw, NULL, (struct lysp_node**)&any)); \
Radek Krejci9800fb82018-12-13 14:26:23 +01001638 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001639 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)any); any = NULL;
Radek Krejci9800fb82018-12-13 14:26:23 +01001640
1641 TEST_DUP("config", "true", "false");
1642 TEST_DUP("description", "text1", "text2");
1643 TEST_DUP("mandatory", "true", "false");
1644 TEST_DUP("reference", "1", "2");
1645 TEST_DUP("status", "current", "obsolete");
1646 TEST_DUP("when", "true", "false");
1647#undef TEST_DUP
1648
1649 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001650 in.current = "any {config true;description test;if-feature f;mandatory true;must 'expr';reference test;status current;when true;m:ext;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001651 assert_int_equal(LY_SUCCESS, parse_any(ctx, &in, kw, NULL, (struct lysp_node**)&any));
Radek Krejci9800fb82018-12-13 14:26:23 +01001652 assert_non_null(any);
Radek Krejcid6b76452019-09-03 17:03:03 +02001653 assert_int_equal(kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML, any->nodetype);
Radek Krejci9800fb82018-12-13 14:26:23 +01001654 assert_string_equal("any", any->name);
1655 assert_string_equal("test", any->dsc);
1656 assert_non_null(any->exts);
1657 assert_non_null(any->iffeatures);
1658 assert_non_null(any->musts);
1659 assert_string_equal("test", any->ref);
1660 assert_non_null(any->when);
1661 assert_null(any->parent);
1662 assert_null(any->next);
1663 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001664 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)any); any = NULL;
Radek Krejci9800fb82018-12-13 14:26:23 +01001665}
1666
1667static void
1668test_anydata(void **state)
1669{
Radek Krejcid6b76452019-09-03 17:03:03 +02001670 return test_any(state, LY_STMT_ANYDATA);
Radek Krejci9800fb82018-12-13 14:26:23 +01001671}
1672
1673static void
1674test_anyxml(void **state)
1675{
Radek Krejcid6b76452019-09-03 17:03:03 +02001676 return test_any(state, LY_STMT_ANYXML);
Radek Krejci9800fb82018-12-13 14:26:23 +01001677}
1678
Radek Krejcie86bf772018-12-14 11:39:53 +01001679static void
1680test_grouping(void **state)
1681{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001682 struct lys_yang_parser_ctx *ctx = *state;
Radek Krejcie86bf772018-12-14 11:39:53 +01001683 struct lysp_grp *grp = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001684 struct ly_in in = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01001685
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001686 ctx->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001687
1688 /* invalid cardinality */
1689#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001690 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001691 assert_int_equal(LY_EVALID, parse_grouping(ctx, &in, NULL, &grp)); \
Radek Krejcie86bf772018-12-14 11:39:53 +01001692 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001693 FREE_ARRAY(ctx->parsed_mod->mod->ctx, grp, lysp_grp_free); grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001694
1695 TEST_DUP("description", "text1", "text2");
1696 TEST_DUP("reference", "1", "2");
1697 TEST_DUP("status", "current", "obsolete");
1698#undef TEST_DUP
1699
1700 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001701 in.current = "grp {action x;anydata any;anyxml anyxml; choice ch;container c;description test;grouping g;leaf l {type string;}"
Radek Krejcie86bf772018-12-14 11:39:53 +01001702 "leaf-list ll {type string;} list li;notification not;reference test;status current;typedef t {type int8;}uses g;m:ext;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001703 assert_int_equal(LY_SUCCESS, parse_grouping(ctx, &in, NULL, &grp));
Radek Krejcie86bf772018-12-14 11:39:53 +01001704 assert_non_null(grp);
1705 assert_int_equal(LYS_GROUPING, grp->nodetype);
1706 assert_string_equal("grp", grp->name);
1707 assert_string_equal("test", grp->dsc);
1708 assert_non_null(grp->exts);
1709 assert_string_equal("test", grp->ref);
1710 assert_null(grp->parent);
1711 assert_int_equal( LYS_STATUS_CURR, grp->flags);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001712 ly_set_erase(&ctx->tpdfs_nodes, NULL);
1713 FREE_ARRAY(ctx->parsed_mod->mod->ctx, grp, lysp_grp_free); grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001714
1715 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001716 in.current = "grp {config true} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001717 assert_int_equal(LY_EVALID, parse_grouping(ctx, &in, NULL, &grp));
Radek Krejcie86bf772018-12-14 11:39:53 +01001718 logbuf_assert("Invalid keyword \"config\" as a child of \"grouping\". Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001719 FREE_ARRAY(ctx->parsed_mod->mod->ctx, grp, lysp_grp_free); grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001720
Michal Vasko63f3d842020-07-08 10:10:14 +02001721 in.current = "grp {must 'expr'} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001722 assert_int_equal(LY_EVALID, parse_grouping(ctx, &in, NULL, &grp));
Radek Krejcie86bf772018-12-14 11:39:53 +01001723 logbuf_assert("Invalid keyword \"must\" as a child of \"grouping\". Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001724 FREE_ARRAY(ctx->parsed_mod->mod->ctx, grp, lysp_grp_free); grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001725}
1726
1727static void
Radek Krejcif538ce52019-03-05 10:46:14 +01001728test_action(void **state)
1729{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001730 struct lys_yang_parser_ctx *ctx = *state;
Radek Krejcif538ce52019-03-05 10:46:14 +01001731 struct lysp_action *rpcs = NULL;
1732 struct lysp_node_container *c = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001733 struct ly_in in = {0};
Radek Krejcif538ce52019-03-05 10:46:14 +01001734
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001735 ctx->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcif538ce52019-03-05 10:46:14 +01001736
1737 /* invalid cardinality */
1738#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001739 in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001740 assert_int_equal(LY_EVALID, parse_action(ctx, &in, NULL, &rpcs)); \
Radek Krejcif538ce52019-03-05 10:46:14 +01001741 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001742 FREE_ARRAY(ctx->parsed_mod->mod->ctx, rpcs, lysp_action_free); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001743
1744 TEST_DUP("description", "text1", "text2");
Michal Vaskob83af8a2020-01-06 09:49:22 +01001745 TEST_DUP("input", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
1746 TEST_DUP("output", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
Radek Krejcif538ce52019-03-05 10:46:14 +01001747 TEST_DUP("reference", "1", "2");
1748 TEST_DUP("status", "current", "obsolete");
1749#undef TEST_DUP
1750
1751 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001752 in.current = "top;";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001753 assert_int_equal(LY_SUCCESS, parse_container(ctx, &in, NULL, (struct lysp_node**)&c));
Michal Vasko63f3d842020-07-08 10:10:14 +02001754 in.current = "func {description test;grouping grp;if-feature f;reference test;status current;typedef mytype {type int8;} m:ext;"
Radek Krejcif538ce52019-03-05 10:46:14 +01001755 "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1756 " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}"
1757 "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1758 " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001759 assert_int_equal(LY_SUCCESS, parse_action(ctx, &in, (struct lysp_node*)c, &rpcs));
Radek Krejcif538ce52019-03-05 10:46:14 +01001760 assert_non_null(rpcs);
1761 assert_int_equal(LYS_ACTION, rpcs->nodetype);
1762 assert_string_equal("func", rpcs->name);
1763 assert_string_equal("test", rpcs->dsc);
1764 assert_non_null(rpcs->exts);
1765 assert_non_null(rpcs->iffeatures);
1766 assert_string_equal("test", rpcs->ref);
1767 assert_non_null(rpcs->groupings);
1768 assert_non_null(rpcs->typedefs);
1769 assert_int_equal(LYS_STATUS_CURR, rpcs->flags);
1770 /* input */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001771 assert_int_equal(rpcs->input.nodetype, LYS_INPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001772 assert_non_null(rpcs->input.groupings);
1773 assert_non_null(rpcs->input.exts);
1774 assert_non_null(rpcs->input.musts);
1775 assert_non_null(rpcs->input.typedefs);
1776 assert_non_null(rpcs->input.data);
1777 /* output */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001778 assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001779 assert_non_null(rpcs->output.groupings);
1780 assert_non_null(rpcs->output.exts);
1781 assert_non_null(rpcs->output.musts);
1782 assert_non_null(rpcs->output.typedefs);
1783 assert_non_null(rpcs->output.data);
1784
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001785 ly_set_erase(&ctx->tpdfs_nodes, NULL);
1786 FREE_ARRAY(ctx->parsed_mod->mod->ctx, rpcs, lysp_action_free); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001787
1788 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001789 in.current = "func {config true} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001790 assert_int_equal(LY_EVALID, parse_action(ctx, &in, NULL, &rpcs));
Radek Krejcif538ce52019-03-05 10:46:14 +01001791 logbuf_assert("Invalid keyword \"config\" as a child of \"rpc\". Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001792 FREE_ARRAY(ctx->parsed_mod->mod->ctx, rpcs, lysp_action_free); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001793
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001794 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)c);
Radek Krejcif538ce52019-03-05 10:46:14 +01001795}
1796
1797static void
Radek Krejcifc11bd72019-04-11 16:00:05 +02001798test_notification(void **state)
1799{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001800 struct lys_yang_parser_ctx *ctx = *state;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001801 struct lysp_notif *notifs = NULL;
1802 struct lysp_node_container *c = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001803 struct ly_in in = {0};
Radek Krejcifc11bd72019-04-11 16:00:05 +02001804
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001805 ctx->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcifc11bd72019-04-11 16:00:05 +02001806
1807 /* invalid cardinality */
1808#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001809 in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001810 assert_int_equal(LY_EVALID, parse_notif(ctx, &in, NULL, &notifs)); \
Radek Krejcifc11bd72019-04-11 16:00:05 +02001811 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001812 FREE_ARRAY(ctx->parsed_mod->mod->ctx, notifs, lysp_notif_free); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001813
1814 TEST_DUP("description", "text1", "text2");
1815 TEST_DUP("reference", "1", "2");
1816 TEST_DUP("status", "current", "obsolete");
1817#undef TEST_DUP
1818
1819 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001820 in.current = "top;";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001821 assert_int_equal(LY_SUCCESS, parse_container(ctx, &in, NULL, (struct lysp_node**)&c));
Michal Vasko63f3d842020-07-08 10:10:14 +02001822 in.current = "ntf {anydata a1; anyxml a2; choice ch; container c; description test; grouping grp; if-feature f; leaf l {type int8;}"
Radek Krejcifc11bd72019-04-11 16:00:05 +02001823 "leaf-list ll {type int8;} list li; must 1; reference test; status current; typedef mytype {type int8;} uses grp; m:ext;}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001824 assert_int_equal(LY_SUCCESS, parse_notif(ctx, &in, (struct lysp_node*)c, &notifs));
Radek Krejcifc11bd72019-04-11 16:00:05 +02001825 assert_non_null(notifs);
1826 assert_int_equal(LYS_NOTIF, notifs->nodetype);
1827 assert_string_equal("ntf", notifs->name);
1828 assert_string_equal("test", notifs->dsc);
1829 assert_non_null(notifs->exts);
1830 assert_non_null(notifs->iffeatures);
1831 assert_string_equal("test", notifs->ref);
1832 assert_non_null(notifs->groupings);
1833 assert_non_null(notifs->typedefs);
1834 assert_non_null(notifs->musts);
1835 assert_non_null(notifs->data);
1836 assert_int_equal(LYS_STATUS_CURR, notifs->flags);
1837
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001838 ly_set_erase(&ctx->tpdfs_nodes, NULL);
1839 FREE_ARRAY(ctx->parsed_mod->mod->ctx, notifs, lysp_notif_free); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001840
1841 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001842 in.current = "ntf {config true} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001843 assert_int_equal(LY_EVALID, parse_notif(ctx, &in, NULL, &notifs));
Radek Krejcifc11bd72019-04-11 16:00:05 +02001844 logbuf_assert("Invalid keyword \"config\" as a child of \"notification\". Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001845 FREE_ARRAY(ctx->parsed_mod->mod->ctx, notifs, lysp_notif_free); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001846
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001847 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)c);
Radek Krejcifc11bd72019-04-11 16:00:05 +02001848}
1849
1850static void
Radek Krejcie86bf772018-12-14 11:39:53 +01001851test_uses(void **state)
1852{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001853 struct lys_yang_parser_ctx *ctx = *state;
Radek Krejcie86bf772018-12-14 11:39:53 +01001854 struct lysp_node_uses *u = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001855 struct ly_in in = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01001856
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001857 ctx->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001858
1859 /* invalid cardinality */
1860#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001861 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001862 assert_int_equal(LY_EVALID, parse_uses(ctx, &in, NULL, (struct lysp_node**)&u)); \
Radek Krejcie86bf772018-12-14 11:39:53 +01001863 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001864 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)u); u = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001865
1866 TEST_DUP("description", "text1", "text2");
1867 TEST_DUP("reference", "1", "2");
1868 TEST_DUP("status", "current", "obsolete");
1869 TEST_DUP("when", "true", "false");
1870#undef TEST_DUP
1871
1872 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001873 in.current = "grpref {augment some/node;description test;if-feature f;reference test;refine some/other/node;status current;when true;m:ext;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001874 assert_int_equal(LY_SUCCESS, parse_uses(ctx, &in, NULL, (struct lysp_node**)&u));
Radek Krejcie86bf772018-12-14 11:39:53 +01001875 assert_non_null(u);
1876 assert_int_equal(LYS_USES, u->nodetype);
1877 assert_string_equal("grpref", u->name);
1878 assert_string_equal("test", u->dsc);
1879 assert_non_null(u->exts);
1880 assert_non_null(u->iffeatures);
1881 assert_string_equal("test", u->ref);
1882 assert_non_null(u->augments);
1883 assert_non_null(u->refines);
1884 assert_non_null(u->when);
1885 assert_null(u->parent);
1886 assert_null(u->next);
1887 assert_int_equal(LYS_STATUS_CURR, u->flags);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001888 lysp_node_free(ctx->parsed_mod->mod->ctx, (struct lysp_node*)u); u = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001889}
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001890
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001891static void
1892test_augment(void **state)
1893{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001894 struct lys_yang_parser_ctx *ctx = *state;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001895 struct lysp_augment *a = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001896 struct ly_in in = {0};
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001897
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001898 ctx->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001899
1900 /* invalid cardinality */
1901#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001902 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001903 assert_int_equal(LY_EVALID, parse_augment(ctx, &in, NULL, &a)); \
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001904 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001905 FREE_ARRAY(ctx->parsed_mod->mod->ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001906
1907 TEST_DUP("description", "text1", "text2");
1908 TEST_DUP("reference", "1", "2");
1909 TEST_DUP("status", "current", "obsolete");
1910 TEST_DUP("when", "true", "false");
1911#undef TEST_DUP
1912
1913 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001914 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 Krejci5a7c4a52019-02-12 15:45:11 +01001915 "leaf-list ll {type string;} list li;notification not;reference test;status current;uses g;when true;m:ext;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001916 assert_int_equal(LY_SUCCESS, parse_augment(ctx, &in, NULL, &a));
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001917 assert_non_null(a);
1918 assert_int_equal(LYS_AUGMENT, a->nodetype);
1919 assert_string_equal("/target/nodeid", a->nodeid);
1920 assert_string_equal("test", a->dsc);
1921 assert_non_null(a->exts);
1922 assert_non_null(a->iffeatures);
1923 assert_string_equal("test", a->ref);
1924 assert_non_null(a->when);
1925 assert_null(a->parent);
1926 assert_int_equal(LYS_STATUS_CURR, a->flags);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001927 FREE_ARRAY(ctx->parsed_mod->mod->ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001928}
1929
Radek Krejcif09e4e82019-06-14 15:08:11 +02001930static void
1931test_when(void **state)
1932{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001933 struct lys_yang_parser_ctx *ctx = *state;
Radek Krejcif09e4e82019-06-14 15:08:11 +02001934 struct lysp_when *w = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001935 struct ly_in in = {0};
Radek Krejcif09e4e82019-06-14 15:08:11 +02001936
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001937 ctx->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001938
1939 /* invalid cardinality */
1940#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001941 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001942 assert_int_equal(LY_EVALID, parse_when(ctx, &in, &w)); \
Radek Krejcif09e4e82019-06-14 15:08:11 +02001943 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001944 FREE_MEMBER(ctx->parsed_mod->mod->ctx, w, lysp_when_free); w = NULL;
Radek Krejcif09e4e82019-06-14 15:08:11 +02001945
1946 TEST_DUP("description", "text1", "text2");
1947 TEST_DUP("reference", "1", "2");
1948#undef TEST_DUP
1949
1950 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001951 in.current = "expression {description test;reference test;m:ext;} ...";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001952 assert_int_equal(LY_SUCCESS, parse_when(ctx, &in, &w));
Radek Krejcif09e4e82019-06-14 15:08:11 +02001953 assert_non_null(w);
1954 assert_string_equal("expression", w->cond);
1955 assert_string_equal("test", w->dsc);
1956 assert_string_equal("test", w->ref);
1957 assert_non_null(w->exts);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001958 FREE_MEMBER(ctx->parsed_mod->mod->ctx, w, lysp_when_free); w = NULL;
Radek Krejcif09e4e82019-06-14 15:08:11 +02001959
1960 /* empty condition */
Michal Vasko63f3d842020-07-08 10:10:14 +02001961 in.current = "\"\";";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001962 assert_int_equal(LY_SUCCESS, parse_when(ctx, &in, &w));
Radek Krejcif09e4e82019-06-14 15:08:11 +02001963 logbuf_assert("Empty argument of when statement does not make sense.");
1964 assert_non_null(w);
1965 assert_string_equal("", w->cond);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001966 FREE_MEMBER(ctx->parsed_mod->mod->ctx, w, lysp_when_free); w = NULL;
Radek Krejcif09e4e82019-06-14 15:08:11 +02001967}
1968
David Sedlákd6ce6d72019-07-16 17:30:18 +02001969static void
1970test_value(void **state)
1971{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001972 struct lys_yang_parser_ctx *ctx = *state;
Michal Vasko63f3d842020-07-08 10:10:14 +02001973 struct ly_in in = {0};
David Sedlákd6ce6d72019-07-16 17:30:18 +02001974 int64_t val = 0;
1975 uint16_t flags = 0;
1976
Michal Vasko63f3d842020-07-08 10:10:14 +02001977 in.current = "-0;";
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001978 assert_int_equal(parse_type_enum_value_pos(ctx, &in, LY_STMT_VALUE, &val, &flags, NULL), LY_SUCCESS);
David Sedlákd6ce6d72019-07-16 17:30:18 +02001979 assert_int_equal(val, 0);
1980
Michal Vasko63f3d842020-07-08 10:10:14 +02001981 in.current = "-0;";
David Sedlákd6ce6d72019-07-16 17:30:18 +02001982 flags = 0;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001983 assert_int_equal(parse_type_enum_value_pos(ctx, &in, LY_STMT_POSITION, &val, &flags, NULL), LY_EVALID);
David Sedlákd6ce6d72019-07-16 17:30:18 +02001984 logbuf_assert("Invalid value \"-0\" of \"position\". Line number 1.");
David Sedlákd6ce6d72019-07-16 17:30:18 +02001985}
1986
Radek Krejci80dd33e2018-09-26 15:57:18 +02001987int main(void)
1988{
1989 const struct CMUnitTest tests[] = {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001990 cmocka_unit_test_setup_teardown(test_helpers, setup_f, teardown_f),
1991 cmocka_unit_test_setup_teardown(test_comments, setup_f, teardown_f),
1992 cmocka_unit_test_setup_teardown(test_arg, setup_f, teardown_f),
1993 cmocka_unit_test_setup_teardown(test_stmts, setup_f, teardown_f),
1994 cmocka_unit_test_setup_teardown(test_minmax, setup_f, teardown_f),
1995 cmocka_unit_test_setup_teardown(test_module, setup_f, teardown_f),
1996 cmocka_unit_test_setup_teardown(test_deviation, setup_f, teardown_f),
1997 cmocka_unit_test_setup_teardown(test_deviate, setup_f, teardown_f),
1998 cmocka_unit_test_setup_teardown(test_container, setup_f, teardown_f),
1999 cmocka_unit_test_setup_teardown(test_leaf, setup_f, teardown_f),
2000 cmocka_unit_test_setup_teardown(test_leaflist, setup_f, teardown_f),
2001 cmocka_unit_test_setup_teardown(test_list, setup_f, teardown_f),
2002 cmocka_unit_test_setup_teardown(test_choice, setup_f, teardown_f),
2003 cmocka_unit_test_setup_teardown(test_case, setup_f, teardown_f),
2004 cmocka_unit_test_setup_teardown(test_anydata, setup_f, teardown_f),
2005 cmocka_unit_test_setup_teardown(test_anyxml, setup_f, teardown_f),
2006 cmocka_unit_test_setup_teardown(test_action, setup_f, teardown_f),
2007 cmocka_unit_test_setup_teardown(test_notification, setup_f, teardown_f),
2008 cmocka_unit_test_setup_teardown(test_grouping, setup_f, teardown_f),
2009 cmocka_unit_test_setup_teardown(test_uses, setup_f, teardown_f),
2010 cmocka_unit_test_setup_teardown(test_augment, setup_f, teardown_f),
2011 cmocka_unit_test_setup_teardown(test_when, setup_f, teardown_f),
2012 cmocka_unit_test_setup_teardown(test_value, setup_f, teardown_f),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002013 };
2014
2015 return cmocka_run_group_tests(tests, NULL, NULL);
2016}