blob: 6311dfc7afb44592a3b345876b769649b707568d [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 Krejci3a4889a2020-05-19 17:01:58 +020023#include "../../../src/common.h"
24#include "../../../src/tree_schema.h"
25#include "../../../src/tree_schema_internal.h"
Radek Krejci2d7a47b2019-05-16 13:34:10 +020026
27/* originally static functions from tree_schema_free.c and parser_yang.c */
28void lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020029void lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev);
30void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp);
31void lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action);
32void lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif);
33void lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment);
34void lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d);
35void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
Radek Krejcif09e4e82019-06-14 15:08:11 +020036void lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020037
38LY_ERR buf_add_char(struct ly_ctx *ctx, const char **input, size_t len, char **buf, size_t *buf_len, size_t *buf_used);
Michal Vaskob36053d2020-03-26 15:49:30 +010039LY_ERR buf_store_char(struct lys_yang_parser_ctx *ctx, const char **input, enum yang_arg arg, char **word_p,
David Sedlák40bb13b2019-07-10 14:34:18 +020040 size_t *word_len, char **word_b, size_t *buf_len, int need_buf, int *prefix);
Michal Vaskob36053d2020-03-26 15:49:30 +010041LY_ERR get_keyword(struct lys_yang_parser_ctx *ctx, const char **data, enum ly_stmt *kw, char **word_p, size_t *word_len);
42LY_ERR get_argument(struct lys_yang_parser_ctx *ctx, const char **data, enum yang_arg arg,
Radek Krejci2d7a47b2019-05-16 13:34:10 +020043 uint16_t *flags, char **word_p, char **word_b, size_t *word_len);
Michal Vaskob36053d2020-03-26 15:49:30 +010044LY_ERR skip_comment(struct lys_yang_parser_ctx *ctx, const char **data, int comment);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020045
Michal Vaskob36053d2020-03-26 15:49:30 +010046LY_ERR parse_action(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions);
47LY_ERR parse_any(struct lys_yang_parser_ctx *ctx, const char **data, enum ly_stmt kw, struct lysp_node *parent, struct lysp_node **siblings);
48LY_ERR parse_augment(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_augment **augments);
49LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
50LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
51LY_ERR parse_deviate(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates);
52LY_ERR parse_deviation(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations);
Michal Vaskob36053d2020-03-26 15:49:30 +010053LY_ERR parse_grouping(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings);
54LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
Michal Vaskob36053d2020-03-26 15:49:30 +010055LY_ERR parse_leaf(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
56LY_ERR parse_leaflist(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
57LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
58LY_ERR parse_maxelements(struct lys_yang_parser_ctx *ctx, const char **data, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts);
59LY_ERR parse_minelements(struct lys_yang_parser_ctx *ctx, const char **data, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts);
60LY_ERR parse_module(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_module *mod);
61LY_ERR parse_notif(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_notif **notifs);
62LY_ERR parse_submodule(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_submodule *submod);
63LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
64LY_ERR parse_when(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_when **when_p);
65LY_ERR parse_type_enum_value_pos(struct lys_yang_parser_ctx *ctx, const char **data, enum ly_stmt val_kw, int64_t *value, uint16_t *flags, struct lysp_ext_instance **exts);
Radek Krejci80dd33e2018-09-26 15:57:18 +020066
67#define BUFSIZE 1024
68char logbuf[BUFSIZE] = {0};
Radek Krejci9ed7a192018-10-31 16:23:51 +010069int store = -1; /* negative for infinite logging, positive for limited logging */
Radek Krejci80dd33e2018-09-26 15:57:18 +020070
71/* set to 0 to printing error messages to stderr instead of checking them in code */
Radek Krejci70853c52018-10-15 14:46:16 +020072#define ENABLE_LOGGER_CHECKING 1
Radek Krejci80dd33e2018-09-26 15:57:18 +020073
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020074#if ENABLE_LOGGER_CHECKING
Radek Krejci80dd33e2018-09-26 15:57:18 +020075static void
76logger(LY_LOG_LEVEL level, const char *msg, const char *path)
77{
78 (void) level; /* unused */
Radek Krejci9ed7a192018-10-31 16:23:51 +010079 if (store) {
80 if (path && path[0]) {
81 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
82 } else {
83 strncpy(logbuf, msg, BUFSIZE - 1);
84 }
85 if (store > 0) {
86 --store;
87 }
Radek Krejci80dd33e2018-09-26 15:57:18 +020088 }
89}
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020090#endif
Radek Krejci80dd33e2018-09-26 15:57:18 +020091
92static int
93logger_setup(void **state)
94{
95 (void) state; /* unused */
96#if ENABLE_LOGGER_CHECKING
97 ly_set_log_clb(logger, 1);
98#endif
99 return 0;
100}
101
Radek Krejcib1a5dcc2018-11-26 14:50:05 +0100102static int
103logger_teardown(void **state)
104{
105 (void) state; /* unused */
106#if ENABLE_LOGGER_CHECKING
107 if (*state) {
108 fprintf(stderr, "%s\n", logbuf);
109 }
110#endif
111 return 0;
112}
113
Radek Krejci80dd33e2018-09-26 15:57:18 +0200114void
115logbuf_clean(void)
116{
117 logbuf[0] = '\0';
118}
119
120#if ENABLE_LOGGER_CHECKING
121# define logbuf_assert(str) assert_string_equal(logbuf, str)
122#else
123# define logbuf_assert(str)
124#endif
125
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200126#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
127 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
128 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
129 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
130 CLEANUP
131
Radek Krejci44ceedc2018-10-02 15:54:31 +0200132static void
133test_helpers(void **state)
134{
135 (void) state; /* unused */
136
137 const char *str;
Radek Krejci404251e2018-10-09 12:06:44 +0200138 char *buf, *p;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200139 size_t len, size;
Michal Vaskob36053d2020-03-26 15:49:30 +0100140 struct lys_yang_parser_ctx ctx;
141 ctx.format = LYS_IN_YANG;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200142 ctx.ctx = NULL;
Radek Krejci99435242019-09-05 16:19:15 +0200143 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200144 ctx.line = 1;
David Sedlák40bb13b2019-07-10 14:34:18 +0200145 int prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200146
147 /* storing into buffer */
148 str = "abcd";
149 buf = NULL;
150 size = len = 0;
151 assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &str, 2, &buf, &size, &len));
152 assert_int_not_equal(0, size);
153 assert_int_equal(2, len);
154 assert_string_equal("cd", str);
155 assert_false(strncmp("ab", buf, 2));
156 free(buf);
Radek Krejci404251e2018-10-09 12:06:44 +0200157 buf = NULL;
158
159 /* invalid first characters */
160 len = 0;
161 str = "2invalid";
David Sedlák40bb13b2019-07-10 14:34:18 +0200162 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200163 str = ".invalid";
David Sedlák40bb13b2019-07-10 14:34:18 +0200164 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200165 str = "-invalid";
David Sedlák40bb13b2019-07-10 14:34:18 +0200166 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200167 /* invalid following characters */
168 len = 3; /* number of characters read before the str content */
169 str = "!";
David Sedlák40bb13b2019-07-10 14:34:18 +0200170 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200171 str = ":";
David Sedlák40bb13b2019-07-10 14:34:18 +0200172 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200173 /* valid colon for prefixed identifiers */
174 len = size = 0;
175 p = NULL;
David Sedlák40bb13b2019-07-10 14:34:18 +0200176 prefix = 0;
Radek Krejci404251e2018-10-09 12:06:44 +0200177 str = "x:id";
David Sedlák40bb13b2019-07-10 14:34:18 +0200178 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 0, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200179 assert_int_equal(1, len);
180 assert_null(buf);
181 assert_string_equal(":id", str);
182 assert_int_equal('x', p[len - 1]);
David Sedlák40bb13b2019-07-10 14:34:18 +0200183 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200184 assert_int_equal(2, len);
185 assert_string_equal("id", str);
186 assert_int_equal(':', p[len - 1]);
187 free(buf);
David Sedlák40bb13b2019-07-10 14:34:18 +0200188 prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200189
190 /* checking identifiers */
Michal Vaskob36053d2020-03-26 15:49:30 +0100191 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)&ctx, ':', 0, NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200192 logbuf_assert("Invalid identifier character ':'. Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100193 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)&ctx, '#', 1, NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200194 logbuf_assert("Invalid identifier first character '#'. Line number 1.");
195
Michal Vaskob36053d2020-03-26 15:49:30 +0100196 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)&ctx, 'a', 1, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200197 assert_int_equal(0, prefix);
Michal Vaskob36053d2020-03-26 15:49:30 +0100198 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)&ctx, ':', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200199 assert_int_equal(1, prefix);
Michal Vaskob36053d2020-03-26 15:49:30 +0100200 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)&ctx, ':', 0, &prefix));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200201 assert_int_equal(1, prefix);
Michal Vaskob36053d2020-03-26 15:49:30 +0100202 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)&ctx, 'b', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200203 assert_int_equal(2, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200204 /* second colon is invalid */
Michal Vaskob36053d2020-03-26 15:49:30 +0100205 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)&ctx, ':', 0, &prefix));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200206 logbuf_assert("Invalid identifier character ':'. Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200207}
Radek Krejci80dd33e2018-09-26 15:57:18 +0200208
209static void
210test_comments(void **state)
211{
212 (void) state; /* unused */
213
Michal Vaskob36053d2020-03-26 15:49:30 +0100214 struct lys_yang_parser_ctx ctx;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200215 const char *str, *p;
Radek Krejciefd22f62018-09-27 11:47:58 +0200216 char *word, *buf;
217 size_t len;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200218
Michal Vaskob36053d2020-03-26 15:49:30 +0100219 ctx.format = LYS_IN_YANG;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200220 ctx.ctx = NULL;
Radek Krejci99435242019-09-05 16:19:15 +0200221 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200222 ctx.line = 1;
223
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200224 str = " // this is a text of / one * line */ comment\nargument;";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200225 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200226 assert_string_equal("argument;", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200227 assert_null(buf);
228 assert_int_equal(8, len);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200229
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200230 str = "/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200231 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200232 assert_string_equal("argument", word);
233 assert_ptr_equal(buf, word);
234 assert_int_equal(8, len);
235 free(word);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200236
237 str = p = " this is one line comment on last line";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200238 assert_int_equal(LY_SUCCESS, skip_comment(&ctx, &str, 1));
Radek Krejci80dd33e2018-09-26 15:57:18 +0200239 assert_true(str[0] == '\0');
240
241 str = p = " this is a not terminated comment x";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200242 assert_int_equal(LY_EVALID, skip_comment(&ctx, &str, 2));
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200243 logbuf_assert("Unexpected end-of-input, non-terminated comment. Line number 5.");
Radek Krejci80dd33e2018-09-26 15:57:18 +0200244 assert_true(str[0] == '\0');
245}
246
Radek Krejciefd22f62018-09-27 11:47:58 +0200247static void
248test_arg(void **state)
249{
250 (void) state; /* unused */
251
Michal Vaskob36053d2020-03-26 15:49:30 +0100252 struct lys_yang_parser_ctx ctx;
Radek Krejciefd22f62018-09-27 11:47:58 +0200253 const char *str;
254 char *word, *buf;
255 size_t len;
256
Michal Vaskob36053d2020-03-26 15:49:30 +0100257 ctx.format = LYS_IN_YANG;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200258 ctx.ctx = NULL;
Radek Krejci99435242019-09-05 16:19:15 +0200259 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200260 ctx.line = 1;
261
Radek Krejciefd22f62018-09-27 11:47:58 +0200262 /* missing argument */
263 str = ";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200264 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200265 assert_null(word);
266
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200267 str = "{";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200268 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200269 logbuf_assert("Invalid character sequence \"{\", expected an argument. Line number 1.");
270
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200271 /* invalid escape sequence */
272 str = "\"\\s\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200273 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200274 logbuf_assert("Double-quoted string unknown special character \'\\s\'. Line number 1.");
275 str = "\'\\s\'"; /* valid, since it is not an escape sequence in single quoted string */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200276 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200277 assert_int_equal(2, len);
278 assert_string_equal("\\s\'", word);
279 assert_int_equal('\0', str[0]); /* input has been eaten */
280
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200281 /* invalid character after the argument */
282 str = "hello\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200283 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200284 logbuf_assert("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
285 str = "hello}";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200286 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200287 logbuf_assert("Invalid character sequence \"}\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
288
David Sedlák40bb13b2019-07-10 14:34:18 +0200289 /* invalid identifier-ref-arg-str */
290 str = "pre:pre:value";
291 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
292
Radek Krejci4e199f52019-05-28 09:09:28 +0200293 str = "\"\";"; /* empty identifier is not allowed */
294 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_IDENTIF_ARG, NULL, &word, &buf, &len));
295 logbuf_assert("Statement argument is required. Line number 1.");
296 logbuf_clean();
297 str = "\"\";"; /* empty reference identifier is not allowed */
298 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
299 logbuf_assert("Statement argument is required. Line number 1.");
300
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200301 str = "hello/x\t"; /* slash is not an invalid character */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200302 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200303 assert_int_equal(7, len);
304 assert_string_equal("hello/x\t", word);
305
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200306 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200307
308 /* different quoting */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200309 str = "hello ";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200310 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200311 assert_null(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200312 assert_int_equal(5, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200313 assert_string_equal("hello ", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200314
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200315 str = "hello/*comment*/\n";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200316 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200317 assert_null(buf);
318 assert_int_equal(5, len);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200319 assert_false(strncmp("hello", word, len));
320
321
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200322 str = "\"hello\\n\\t\\\"\\\\\";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200323 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
fredgand49fe112019-10-21 20:51:50 +0800324 assert_non_null(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200325 assert_int_equal(9, len);
fredgand49fe112019-10-21 20:51:50 +0800326 assert_string_equal("hello\n\t\"\\", word);
327 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200328
329 ctx.indent = 14;
330 str = "\"hello \t\n\t\t world!\"";
331 /* - space and tabs before newline are stripped out
332 * - space and tabs after newline (indentation) are stripped out
333 */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200334 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200335 assert_non_null(buf);
336 assert_ptr_equal(word, buf);
337 assert_int_equal(14, len);
338 assert_string_equal("hello\n world!", word);
339 free(buf);
Radek Krejciff13cd12019-10-25 15:34:24 +0200340 /* In contrast to previous, the backslash-escaped tabs are expanded after trimming, so they are preserved */
341 ctx.indent = 14;
342 str = "\"hello \\t\n\t\\t world!\"";
343 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
344 assert_non_null(buf);
345 assert_ptr_equal(word, buf);
346 assert_int_equal(16, len);
347 assert_string_equal("hello \t\n\t world!", word);
348 free(buf);
349 /* Do not handle whitespaces after backslash-escaped newline as indentation */
350 ctx.indent = 14;
351 str = "\"hello\\n\t\t world!\"";
352 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
353 assert_non_null(buf);
354 assert_ptr_equal(word, buf);
355 assert_int_equal(15, len);
356 assert_string_equal("hello\n\t\t world!", word);
357 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200358
359 ctx.indent = 14;
360 str = "\"hello\n \tworld!\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200361 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200362 assert_non_null(buf);
363 assert_ptr_equal(word, buf);
364 assert_int_equal(12, len);
365 assert_string_equal("hello\nworld!", word);
366 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200367
368 str = "\'hello\'";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200369 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200370 assert_null(buf);
371 assert_int_equal(5, len);
372 assert_false(strncmp("hello", word, 5));
373
374 str = "\"hel\" +\t\n\"lo\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200375 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200376 assert_ptr_equal(word, buf);
377 assert_int_equal(5, len);
378 assert_string_equal("hello", word);
379 free(buf);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200380 str = "\"hel\" +\t\nlo"; /* unquoted the second part */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200381 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciff13cd12019-10-25 15:34:24 +0200382 logbuf_assert("Both string parts divided by '+' must be quoted. Line number 6.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200383
384 str = "\'he\'\t\n+ \"llo\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200385 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200386 assert_ptr_equal(word, buf);
387 assert_int_equal(5, len);
388 assert_string_equal("hello", word);
389 free(buf);
390
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200391 str = " \t\n\"he\"+\'llo\'";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200392 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200393 assert_ptr_equal(word, buf);
394 assert_int_equal(5, len);
395 assert_string_equal("hello", word);
396 free(buf);
397
Radek Krejci44ceedc2018-10-02 15:54:31 +0200398 /* missing argument */
399 str = ";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200400 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciff13cd12019-10-25 15:34:24 +0200401 logbuf_assert("Invalid character sequence \";\", expected an argument. Line number 8.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200402}
403
404static void
405test_stmts(void **state)
406{
407 (void) state; /* unused */
408
Michal Vaskob36053d2020-03-26 15:49:30 +0100409 struct lys_yang_parser_ctx ctx;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200410 const char *str, *p;
Radek Krejcid6b76452019-09-03 17:03:03 +0200411 enum ly_stmt kw;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200412 char *word;
413 size_t len;
414
Michal Vaskob36053d2020-03-26 15:49:30 +0100415 ctx.format = LYS_IN_YANG;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200416 ctx.ctx = NULL;
Radek Krejci99435242019-09-05 16:19:15 +0200417 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200418 ctx.line = 1;
419
420 str = "\n// comment\n\tinput\t{";
421 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200422 assert_int_equal(LY_STMT_INPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200423 assert_int_equal(5, len);
424 assert_string_equal("input\t{", word);
425 assert_string_equal("\t{", str);
426
427 str = "\t /* comment */\t output\n\t{";
428 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200429 assert_int_equal(LY_STMT_OUTPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200430 assert_int_equal(6, len);
431 assert_string_equal("output\n\t{", word);
432 assert_string_equal("\n\t{", str);
Radek Krejciabdd8062019-06-11 16:44:19 +0200433 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200434 assert_int_equal(LY_STMT_SYNTAX_LEFT_BRACE, kw);
Radek Krejciabdd8062019-06-11 16:44:19 +0200435 assert_int_equal(1, len);
436 assert_string_equal("{", word);
437 assert_string_equal("", str);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200438
439 str = "/input { "; /* invalid slash */
440 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
441 logbuf_assert("Invalid identifier first character '/'. Line number 4.");
442
443 str = "not-a-statement-nor-extension { "; /* invalid identifier */
444 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
445 logbuf_assert("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword. Line number 4.");
446
447 str = "path;"; /* missing sep after the keyword */
448 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
449 logbuf_assert("Invalid character sequence \"path;\", expected a keyword followed by a separator. Line number 4.");
450
451 str = "action ";
452 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200453 assert_int_equal(LY_STMT_ACTION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200454 assert_int_equal(6, len);
455 str = "anydata ";
456 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200457 assert_int_equal(LY_STMT_ANYDATA, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200458 assert_int_equal(7, len);
459 str = "anyxml ";
460 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200461 assert_int_equal(LY_STMT_ANYXML, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200462 assert_int_equal(6, len);
463 str = "argument ";
464 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200465 assert_int_equal(LY_STMT_ARGUMENT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200466 assert_int_equal(8, len);
467 str = "augment ";
468 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200469 assert_int_equal(LY_STMT_AUGMENT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200470 assert_int_equal(7, len);
471 str = "base ";
472 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200473 assert_int_equal(LY_STMT_BASE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200474 assert_int_equal(4, len);
475 str = "belongs-to ";
476 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200477 assert_int_equal(LY_STMT_BELONGS_TO, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200478 assert_int_equal(10, len);
479 str = "bit ";
480 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200481 assert_int_equal(LY_STMT_BIT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200482 assert_int_equal(3, len);
483 str = "case ";
484 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200485 assert_int_equal(LY_STMT_CASE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200486 assert_int_equal(4, len);
487 str = "choice ";
488 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200489 assert_int_equal(LY_STMT_CHOICE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200490 assert_int_equal(6, len);
491 str = "config ";
492 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200493 assert_int_equal(LY_STMT_CONFIG, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200494 assert_int_equal(6, len);
495 str = "contact ";
496 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200497 assert_int_equal(LY_STMT_CONTACT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200498 assert_int_equal(7, len);
499 str = "container ";
500 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200501 assert_int_equal(LY_STMT_CONTAINER, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200502 assert_int_equal(9, len);
503 str = "default ";
504 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200505 assert_int_equal(LY_STMT_DEFAULT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200506 assert_int_equal(7, len);
507 str = "description ";
508 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200509 assert_int_equal(LY_STMT_DESCRIPTION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200510 assert_int_equal(11, len);
511 str = "deviate ";
512 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200513 assert_int_equal(LY_STMT_DEVIATE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200514 assert_int_equal(7, len);
515 str = "deviation ";
516 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200517 assert_int_equal(LY_STMT_DEVIATION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200518 assert_int_equal(9, len);
519 str = "enum ";
520 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200521 assert_int_equal(LY_STMT_ENUM, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200522 assert_int_equal(4, len);
523 str = "error-app-tag ";
524 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200525 assert_int_equal(LY_STMT_ERROR_APP_TAG, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200526 assert_int_equal(13, len);
527 str = "error-message ";
528 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200529 assert_int_equal(LY_STMT_ERROR_MESSAGE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200530 assert_int_equal(13, len);
531 str = "extension ";
532 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200533 assert_int_equal(LY_STMT_EXTENSION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200534 assert_int_equal(9, len);
535 str = "feature ";
536 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200537 assert_int_equal(LY_STMT_FEATURE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200538 assert_int_equal(7, len);
539 str = "fraction-digits ";
540 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200541 assert_int_equal(LY_STMT_FRACTION_DIGITS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200542 assert_int_equal(15, len);
543 str = "grouping ";
544 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200545 assert_int_equal(LY_STMT_GROUPING, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200546 assert_int_equal(8, len);
547 str = "identity ";
548 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200549 assert_int_equal(LY_STMT_IDENTITY, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200550 assert_int_equal(8, len);
551 str = "if-feature ";
552 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200553 assert_int_equal(LY_STMT_IF_FEATURE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200554 assert_int_equal(10, len);
555 str = "import ";
556 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200557 assert_int_equal(LY_STMT_IMPORT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200558 assert_int_equal(6, len);
559 str = "include ";
560 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200561 assert_int_equal(LY_STMT_INCLUDE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200562 assert_int_equal(7, len);
563 str = "input{";
564 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200565 assert_int_equal(LY_STMT_INPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200566 assert_int_equal(5, len);
567 str = "key ";
568 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200569 assert_int_equal(LY_STMT_KEY, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200570 assert_int_equal(3, len);
571 str = "leaf ";
572 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200573 assert_int_equal(LY_STMT_LEAF, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200574 assert_int_equal(4, len);
575 str = "leaf-list ";
576 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200577 assert_int_equal(LY_STMT_LEAF_LIST, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200578 assert_int_equal(9, len);
579 str = "length ";
580 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200581 assert_int_equal(LY_STMT_LENGTH, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200582 assert_int_equal(6, len);
583 str = "list ";
584 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200585 assert_int_equal(LY_STMT_LIST, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200586 assert_int_equal(4, len);
587 str = "mandatory ";
588 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200589 assert_int_equal(LY_STMT_MANDATORY, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200590 assert_int_equal(9, len);
591 str = "max-elements ";
592 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200593 assert_int_equal(LY_STMT_MAX_ELEMENTS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200594 assert_int_equal(12, len);
595 str = "min-elements ";
596 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200597 assert_int_equal(LY_STMT_MIN_ELEMENTS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200598 assert_int_equal(12, len);
599 str = "modifier ";
600 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200601 assert_int_equal(LY_STMT_MODIFIER, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200602 assert_int_equal(8, len);
603 str = "module ";
604 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200605 assert_int_equal(LY_STMT_MODULE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200606 assert_int_equal(6, len);
607 str = "must ";
608 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200609 assert_int_equal(LY_STMT_MUST, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200610 assert_int_equal(4, len);
611 str = "namespace ";
612 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200613 assert_int_equal(LY_STMT_NAMESPACE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200614 assert_int_equal(9, len);
615 str = "notification ";
616 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200617 assert_int_equal(LY_STMT_NOTIFICATION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200618 assert_int_equal(12, len);
619 str = "ordered-by ";
620 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200621 assert_int_equal(LY_STMT_ORDERED_BY, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200622 assert_int_equal(10, len);
623 str = "organization ";
624 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200625 assert_int_equal(LY_STMT_ORGANIZATION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200626 assert_int_equal(12, len);
627 str = "output ";
628 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200629 assert_int_equal(LY_STMT_OUTPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200630 assert_int_equal(6, len);
631 str = "path ";
632 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200633 assert_int_equal(LY_STMT_PATH, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200634 assert_int_equal(4, len);
635 str = "pattern ";
636 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200637 assert_int_equal(LY_STMT_PATTERN, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200638 assert_int_equal(7, len);
639 str = "position ";
640 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200641 assert_int_equal(LY_STMT_POSITION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200642 assert_int_equal(8, len);
643 str = "prefix ";
644 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200645 assert_int_equal(LY_STMT_PREFIX, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200646 assert_int_equal(6, len);
647 str = "presence ";
648 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200649 assert_int_equal(LY_STMT_PRESENCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200650 assert_int_equal(8, len);
651 str = "range ";
652 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200653 assert_int_equal(LY_STMT_RANGE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200654 assert_int_equal(5, len);
655 str = "reference ";
656 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200657 assert_int_equal(LY_STMT_REFERENCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200658 assert_int_equal(9, len);
659 str = "refine ";
660 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200661 assert_int_equal(LY_STMT_REFINE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200662 assert_int_equal(6, len);
663 str = "require-instance ";
664 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200665 assert_int_equal(LY_STMT_REQUIRE_INSTANCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200666 assert_int_equal(16, len);
667 str = "revision ";
668 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200669 assert_int_equal(LY_STMT_REVISION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200670 assert_int_equal(8, len);
671 str = "revision-date ";
672 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200673 assert_int_equal(LY_STMT_REVISION_DATE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200674 assert_int_equal(13, len);
675 str = "rpc ";
676 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200677 assert_int_equal(LY_STMT_RPC, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200678 assert_int_equal(3, len);
679 str = "status ";
680 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200681 assert_int_equal(LY_STMT_STATUS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200682 assert_int_equal(6, len);
683 str = "submodule ";
684 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200685 assert_int_equal(LY_STMT_SUBMODULE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200686 assert_int_equal(9, len);
687 str = "type ";
688 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200689 assert_int_equal(LY_STMT_TYPE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200690 assert_int_equal(4, len);
691 str = "typedef ";
692 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200693 assert_int_equal(LY_STMT_TYPEDEF, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200694 assert_int_equal(7, len);
695 str = "unique ";
696 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200697 assert_int_equal(LY_STMT_UNIQUE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200698 assert_int_equal(6, len);
699 str = "units ";
700 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200701 assert_int_equal(LY_STMT_UNITS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200702 assert_int_equal(5, len);
703 str = "uses ";
704 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200705 assert_int_equal(LY_STMT_USES, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200706 assert_int_equal(4, len);
707 str = "value ";
708 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200709 assert_int_equal(LY_STMT_VALUE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200710 assert_int_equal(5, len);
711 str = "when ";
712 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200713 assert_int_equal(LY_STMT_WHEN, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200714 assert_int_equal(4, len);
715 str = "yang-version ";
716 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200717 assert_int_equal(LY_STMT_YANG_VERSION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200718 assert_int_equal(12, len);
719 str = "yin-element ";
720 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200721 assert_int_equal(LY_STMT_YIN_ELEMENT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200722 assert_int_equal(11, len);
Radek Krejci626df482018-10-11 15:06:31 +0200723 str = ";config false;";
724 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200725 assert_int_equal(LY_STMT_SYNTAX_SEMICOLON, kw);
Radek Krejci626df482018-10-11 15:06:31 +0200726 assert_int_equal(1, len);
727 assert_string_equal("config false;", str);
728 str = "{ config false;";
729 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200730 assert_int_equal(LY_STMT_SYNTAX_LEFT_BRACE, kw);
Radek Krejci626df482018-10-11 15:06:31 +0200731 assert_int_equal(1, len);
732 assert_string_equal(" config false;", str);
733 str = "}";
734 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200735 assert_int_equal(LY_STMT_SYNTAX_RIGHT_BRACE, kw);
Radek Krejci626df482018-10-11 15:06:31 +0200736 assert_int_equal(1, len);
737 assert_string_equal("", str);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200738
739 /* geenric extension */
740 str = p = "nacm:default-deny-write;";
741 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200742 assert_int_equal(LY_STMT_EXTENSION_INSTANCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200743 assert_int_equal(23, len);
744 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200745}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200746
Radek Krejci05b13982018-11-28 16:22:07 +0100747static void
748test_minmax(void **state)
749{
750 *state = test_minmax;
751
Michal Vaskob36053d2020-03-26 15:49:30 +0100752 struct lys_yang_parser_ctx ctx = {0};
Radek Krejci05b13982018-11-28 16:22:07 +0100753 uint16_t flags = 0;
754 uint32_t value = 0;
755 struct lysp_ext_instance *ext = NULL;
756 const char *str;
757
Michal Vaskob36053d2020-03-26 15:49:30 +0100758 ctx.format = LYS_IN_YANG;
Radek Krejci05b13982018-11-28 16:22:07 +0100759 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
760 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +0200761 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci05b13982018-11-28 16:22:07 +0100762 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100763 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci05b13982018-11-28 16:22:07 +0100764
Radek Krejcidf6cad12018-11-28 17:10:55 +0100765 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100766 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100767 logbuf_assert("Invalid value \"1invalid\" of \"min-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100768
769 flags = value = 0;
770 str = " -1; ...";
771 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
772 logbuf_assert("Invalid value \"-1\" of \"min-elements\". Line number 1.");
773
Radek Krejcidf6cad12018-11-28 17:10:55 +0100774 /* implementation limit */
775 flags = value = 0;
776 str = " 4294967296; ...";
777 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
778 logbuf_assert("Value \"4294967296\" is out of \"min-elements\" bounds. Line number 1.");
779
Radek Krejci05b13982018-11-28 16:22:07 +0100780 flags = value = 0;
781 str = " 1; ...";
782 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
783 assert_int_equal(LYS_SET_MIN, flags);
784 assert_int_equal(1, value);
785
786 flags = value = 0;
787 str = " 1 {m:ext;} ...";
788 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
789 assert_int_equal(LYS_SET_MIN, flags);
790 assert_int_equal(1, value);
791 assert_non_null(ext);
792 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
793 ext = NULL;
794
795 flags = value = 0;
796 str = " 1 {config true;} ...";
797 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
798 logbuf_assert("Invalid keyword \"config\" as a child of \"min-elements\". Line number 1.");
799
Radek Krejcidf6cad12018-11-28 17:10:55 +0100800 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100801 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100802 logbuf_assert("Invalid value \"1invalid\" of \"max-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100803
804 flags = value = 0;
805 str = " -1; ...";
806 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
807 logbuf_assert("Invalid value \"-1\" of \"max-elements\". Line number 1.");
808
Radek Krejcidf6cad12018-11-28 17:10:55 +0100809 /* implementation limit */
810 flags = value = 0;
811 str = " 4294967296; ...";
812 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
813 logbuf_assert("Value \"4294967296\" is out of \"max-elements\" bounds. Line number 1.");
814
Radek Krejci05b13982018-11-28 16:22:07 +0100815 flags = value = 0;
816 str = " 1; ...";
817 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
818 assert_int_equal(LYS_SET_MAX, flags);
819 assert_int_equal(1, value);
820
821 flags = value = 0;
822 str = " unbounded; ...";
823 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
824 assert_int_equal(LYS_SET_MAX, flags);
825 assert_int_equal(0, value);
826
827 flags = value = 0;
828 str = " 1 {m:ext;} ...";
829 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
830 assert_int_equal(LYS_SET_MAX, flags);
831 assert_int_equal(1, value);
832 assert_non_null(ext);
833 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
834 ext = NULL;
835
836 flags = value = 0;
837 str = " 1 {config true;} ...";
838 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
839 logbuf_assert("Invalid keyword \"config\" as a child of \"max-elements\". Line number 1.");
840
841 *state = NULL;
842 ly_ctx_destroy(ctx.ctx, NULL);
843}
844
Radek Krejci9fcacc12018-10-11 15:59:11 +0200845static struct lysp_module *
Michal Vaskob36053d2020-03-26 15:49:30 +0100846mod_renew(struct lys_yang_parser_ctx *ctx)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200847{
Radek Krejci40544fa2019-01-11 09:38:37 +0100848 struct lysp_module *mod_p;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100849 static struct lys_module mod = {0};
850
851 lysc_module_free(mod.compiled, NULL);
852 lysp_module_free(mod.parsed);
853 FREE_STRING(mod.ctx, mod.name);
854 FREE_STRING(mod.ctx, mod.ns);
855 FREE_STRING(mod.ctx, mod.prefix);
856 FREE_STRING(mod.ctx, mod.filepath);
857 FREE_STRING(mod.ctx, mod.org);
858 FREE_STRING(mod.ctx, mod.contact);
859 FREE_STRING(mod.ctx, mod.dsc);
860 FREE_STRING(mod.ctx, mod.ref);
861 memset(&mod, 0, sizeof mod);
862 mod.ctx = ctx->ctx;
863
864 mod_p = calloc(1, sizeof *mod_p);
865 mod.parsed = mod_p;
866 mod_p->mod = &mod;
867 assert_non_null(mod_p);
868 return mod_p;
869}
870
871static struct lysp_submodule *
Michal Vaskob36053d2020-03-26 15:49:30 +0100872submod_renew(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100873{
874 lysp_submodule_free(ctx->ctx, submod);
875 submod = calloc(1, sizeof *submod);
876 assert_non_null(submod);
877 return submod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200878}
879
Radek Krejcid33273d2018-10-25 14:55:52 +0200880static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
881 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
882 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
883{
884 *module_data = user_data;
885 *format = LYS_IN_YANG;
886 *free_module_data = NULL;
887 return LY_SUCCESS;
888}
889
Radek Krejci9fcacc12018-10-11 15:59:11 +0200890static void
891test_module(void **state)
892{
Radek Krejci40544fa2019-01-11 09:38:37 +0100893 *state = test_module;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200894
Michal Vaskob36053d2020-03-26 15:49:30 +0100895 struct lys_yang_parser_ctx ctx;
Radek Krejci40544fa2019-01-11 09:38:37 +0100896 struct lysp_module *mod = NULL;
897 struct lysp_submodule *submod = NULL;
898 struct lys_module *m;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200899 const char *str;
900
Michal Vaskob36053d2020-03-26 15:49:30 +0100901 ctx.format = LYS_IN_YANG;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200902 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
903 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +0200904 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200905 ctx.line = 1;
Radek Krejcia042ea12018-10-13 07:52:15 +0200906 ctx.indent = 0;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200907
Radek Krejci40544fa2019-01-11 09:38:37 +0100908 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200909
910 /* missing mandatory substatements */
911 str = " name {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100912 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
913 assert_string_equal("name", mod->mod->name);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200914 logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100915 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200916
917 str = " name {namespace urn:x;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100918 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
919 assert_string_equal("urn:x", mod->mod->ns);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200920 logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100921 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200922
923 str = " name {namespace urn:x;prefix \"x\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100924 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
925 assert_string_equal("x", mod->mod->prefix);
Radek Krejci40544fa2019-01-11 09:38:37 +0100926 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200927
Radek Krejci027d5802018-11-14 16:57:28 +0100928#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
929#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200930#define TEST_NODE(NODETYPE, INPUT, NAME) \
931 str = SCHEMA_BEGINNING INPUT; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100932 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200933 assert_non_null(mod->data); \
934 assert_int_equal(NODETYPE, mod->data->nodetype); \
935 assert_string_equal(NAME, mod->data->name); \
Radek Krejci40544fa2019-01-11 09:38:37 +0100936 mod = mod_renew(&ctx);
Radek Krejcia042ea12018-10-13 07:52:15 +0200937#define TEST_GENERIC(INPUT, TARGET, TEST) \
938 str = SCHEMA_BEGINNING INPUT; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100939 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200940 assert_non_null(TARGET); \
941 TEST; \
Radek Krejci40544fa2019-01-11 09:38:37 +0100942 mod = mod_renew(&ctx);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100943#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200944 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
Radek Krejci40544fa2019-01-11 09:38:37 +0100945 parse_module, mod, LINE, mod = mod_renew(&ctx))
Radek Krejcia042ea12018-10-13 07:52:15 +0200946
947 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100948 TEST_DUP("namespace", "y", "z", "1");
949 TEST_DUP("prefix", "y", "z", "1");
950 TEST_DUP("contact", "a", "b", "1");
951 TEST_DUP("description", "a", "b", "1");
952 TEST_DUP("organization", "a", "b", "1");
953 TEST_DUP("reference", "a", "b", "1");
Radek Krejcia042ea12018-10-13 07:52:15 +0200954
Radek Krejci70853c52018-10-15 14:46:16 +0200955 /* not allowed in module (submodule-specific) */
956 str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100957 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200958 logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100959 mod = mod_renew(&ctx);
Radek Krejci70853c52018-10-15 14:46:16 +0200960
Radek Krejcia042ea12018-10-13 07:52:15 +0200961 /* anydata */
962 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
963 /* anyxml */
964 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
965 /* augment */
966 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200967 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200968 /* choice */
969 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
970 /* contact 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100971 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact,
972 assert_string_equal("firstname surname", mod->mod->contact));
Radek Krejcia042ea12018-10-13 07:52:15 +0200973 /* container */
974 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
975 /* description 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100976 TEST_GENERIC("description \'some description\';}", mod->mod->dsc,
977 assert_string_equal("some description", mod->mod->dsc));
Radek Krejcia042ea12018-10-13 07:52:15 +0200978 /* deviation */
979 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200980 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200981 /* extension */
982 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200983 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200984 /* feature */
985 TEST_GENERIC("feature test;}", mod->features,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200986 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200987 /* grouping */
988 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200989 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200990 /* identity */
991 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200992 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200993 /* import */
Radek Krejci086c7132018-10-26 15:29:04 +0200994 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
995 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
996 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200997
Radek Krejcia042ea12018-10-13 07:52:15 +0200998 /* import - prefix collision */
Radek Krejci086c7132018-10-26 15:29:04 +0200999 str = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001000 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +02001001 logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001002 mod = mod_renew(&ctx);
Radek Krejci086c7132018-10-26 15:29:04 +02001003 str = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001004 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci086c7132018-10-26 15:29:04 +02001005 logbuf_assert("Prefix \"y\" already used to import \"zzz\" module. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001006 mod = mod_renew(&ctx);
Radek Krejci086c7132018-10-26 15:29:04 +02001007 str = "module" SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix z;}}";
1008 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
1009 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
1010 logbuf_assert("Single revision of the module \"zzz\" referred twice.");
Radek Krejci70853c52018-10-15 14:46:16 +02001011
Radek Krejcia042ea12018-10-13 07:52:15 +02001012 /* include */
Radek Krejci9ed7a192018-10-31 16:23:51 +01001013 store = 1;
Radek Krejcid33273d2018-10-25 14:55:52 +02001014 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module xxx { namespace urn:xxx; prefix x;}");
Radek Krejci086c7132018-10-26 15:29:04 +02001015 str = "module" SCHEMA_BEGINNING "include xxx;}";
1016 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
1017 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001018 logbuf_assert("Input data contains module in situation when a submodule is expected.");
Radek Krejci9ed7a192018-10-31 16:23:51 +01001019 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +02001020
Radek Krejci9ed7a192018-10-31 16:23:51 +01001021 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +01001022 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "submodule xxx {belongs-to wrong-name {prefix w;}}");
Radek Krejci086c7132018-10-26 15:29:04 +02001023 str = "module" SCHEMA_BEGINNING "include xxx;}";
1024 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
1025 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
1026 logbuf_assert("Included \"xxx\" submodule from \"name\" belongs-to a different module \"wrong-name\".");
Radek Krejci9ed7a192018-10-31 16:23:51 +01001027 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +02001028
Radek Krejci313d9902018-11-08 09:42:58 +01001029 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "submodule xxx {belongs-to name {prefix x;}}");
Radek Krejcid33273d2018-10-25 14:55:52 +02001030 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejci086c7132018-10-26 15:29:04 +02001031 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +02001032
Radek Krejcia042ea12018-10-13 07:52:15 +02001033 /* leaf */
1034 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
1035 /* leaf-list */
1036 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
1037 /* list */
1038 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
1039 /* notification */
1040 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001041 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001042 /* organization 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001043 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org,
1044 assert_string_equal("CESNET a.l.e.", mod->mod->org));
Radek Krejcia042ea12018-10-13 07:52:15 +02001045 /* reference 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001046 TEST_GENERIC("reference RFC7950;}", mod->mod->ref,
1047 assert_string_equal("RFC7950", mod->mod->ref));
Radek Krejcia042ea12018-10-13 07:52:15 +02001048 /* revision */
1049 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib7db73a2018-10-24 14:18:40 +02001050 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +02001051 /* rpc */
1052 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001053 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001054 /* typedef */
1055 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001056 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001057 /* uses */
1058 TEST_NODE(LYS_USES, "uses test;}", "test");
1059 /* yang-version */
Radek Krejci027d5802018-11-14 16:57:28 +01001060 str = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001061 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +02001062 logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001063 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001064 str = SCHEMA_BEGINNING2 "yang-version 1.0;yang-version 1.1;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001065 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +02001066 logbuf_assert("Duplicate keyword \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001067 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001068 str = SCHEMA_BEGINNING2 "yang-version 1.0;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001069 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
1070 assert_int_equal(1, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +01001071 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001072 str = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001073 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
1074 assert_int_equal(2, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +01001075 mod = mod_renew(&ctx);
1076
Michal Vaskob36053d2020-03-26 15:49:30 +01001077 struct lys_yang_parser_ctx *ctx_p = NULL;
Radek Krejci40544fa2019-01-11 09:38:37 +01001078 str = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
1079 m = mod->mod;
1080 free(mod);
1081 m->parsed = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02001082 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, str, m));
1083 logbuf_assert("Trailing garbage \"module q {names...\" after module, expected end-of-input. Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +01001084 yang_parser_ctx_free(ctx_p);
Radek Krejci40544fa2019-01-11 09:38:37 +01001085 mod = mod_renew(&ctx);
1086
1087 str = "prefix " SCHEMA_BEGINNING "}";
1088 m = mod->mod;
1089 free(mod);
1090 m->parsed = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02001091 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, str, m));
Michal Vaskob36053d2020-03-26 15:49:30 +01001092 yang_parser_ctx_free(ctx_p);
David Sedlák1b623122019-08-05 15:27:49 +02001093 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001094 mod = mod_renew(&ctx);
Radek Krejci09306362018-10-15 15:26:01 +02001095
David Sedlák9fb515f2019-07-11 10:33:58 +02001096 str = "module " SCHEMA_BEGINNING "}";
1097 str = "module " SCHEMA_BEGINNING "leaf enum {type enumeration {enum seven { position 7;}}}}";
1098 m = mod->mod;
1099 free(mod);
1100 m->parsed = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02001101 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, str, m));
Michal Vaskob36053d2020-03-26 15:49:30 +01001102 yang_parser_ctx_free(ctx_p);
David Sedlák1b623122019-08-05 15:27:49 +02001103 logbuf_assert("Invalid keyword \"position\" as a child of \"enum\". Line number 1.");
David Sedlák9fb515f2019-07-11 10:33:58 +02001104 mod = mod_renew(&ctx);
1105
Radek Krejci156ccaf2018-10-15 15:49:17 +02001106 /* extensions */
1107 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001108 assert_string_equal("prefix:test", mod->exts[0].name);
1109 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
Radek Krejci40544fa2019-01-11 09:38:37 +01001110 mod = mod_renew(&ctx);
Radek Krejci156ccaf2018-10-15 15:49:17 +02001111
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001112 /* invalid substatement */
1113 str = SCHEMA_BEGINNING "must false;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001114 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001115 logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001116 mod = mod_renew(&ctx);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001117
Radek Krejci09306362018-10-15 15:26:01 +02001118 /* submodule */
Radek Krejci40544fa2019-01-11 09:38:37 +01001119 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001120
1121 /* missing mandatory substatements */
1122 str = " subname {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001123 lydict_remove(ctx.ctx, submod->name);
1124 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
1125 assert_string_equal("subname", submod->name);
Radek Krejci09306362018-10-15 15:26:01 +02001126 logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001127 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001128
Radek Krejci313d9902018-11-08 09:42:58 +01001129 str = " subname {belongs-to name {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001130 lydict_remove(ctx.ctx, submod->name);
1131 assert_int_equal(LY_SUCCESS, parse_submodule(&ctx, &str, submod));
1132 assert_string_equal("name", submod->belongsto);
1133 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001134
1135#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +01001136#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +02001137
1138 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001139 str = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ...";
1140 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod)); \
1141 logbuf_assert("Duplicate keyword \"belongs-to\". Line number 3."); \
1142 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001143
1144 /* not allowed in submodule (module-specific) */
1145 str = SCHEMA_BEGINNING "namespace \"urn:z\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001146 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001147 logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001148 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001149 str = SCHEMA_BEGINNING "prefix m;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001150 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001151 logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001152 submod = submod_renew(&ctx, submod);
Radek Krejcia042ea12018-10-13 07:52:15 +02001153
Radek Krejci40544fa2019-01-11 09:38:37 +01001154 str = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
1155 lysp_submodule_free(ctx.ctx, submod);
1156 submod = NULL;
Michal Vaskob36053d2020-03-26 15:49:30 +01001157 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, ctx.ctx, (struct lys_parser_ctx *)&ctx, str, &submod));
1158 yang_parser_ctx_free(ctx_p);
David Sedlák1b623122019-08-05 15:27:49 +02001159 logbuf_assert("Trailing garbage \"module q {names...\" after submodule, expected end-of-input. Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001160
1161 str = "prefix " SCHEMA_BEGINNING "}";
Michal Vaskob36053d2020-03-26 15:49:30 +01001162 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, ctx.ctx, (struct lys_parser_ctx *)&ctx, str, &submod));
1163 yang_parser_ctx_free(ctx_p);
David Sedlák1b623122019-08-05 15:27:49 +02001164 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001165 submod = submod_renew(&ctx, submod);
1166
Radek Krejcia042ea12018-10-13 07:52:15 +02001167#undef TEST_GENERIC
1168#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001169#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +02001170#undef SCHEMA_BEGINNING
1171
Radek Krejci9fcacc12018-10-11 15:59:11 +02001172 lysp_module_free(mod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001173 lysp_submodule_free(ctx.ctx, submod);
Radek Krejci9fcacc12018-10-11 15:59:11 +02001174 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci40544fa2019-01-11 09:38:37 +01001175
1176 *state = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02001177}
1178
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001179
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001180
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001181
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001182
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001183
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001184
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001185
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001186
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001187
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001188
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001189
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001190
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001191
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001192
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001193
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001194
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001195
Radek Krejci3a4889a2020-05-19 17:01:58 +02001196
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001197
1198static void
1199test_deviation(void **state)
1200{
1201 (void) state; /* unused */
1202
Michal Vaskob36053d2020-03-26 15:49:30 +01001203 struct lys_yang_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001204 struct lysp_deviation *d = NULL;
1205 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001206
Michal Vaskob36053d2020-03-26 15:49:30 +01001207 ctx.format = LYS_IN_YANG;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001208 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1209 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001210 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001211 ctx.line = 1;
1212 ctx.indent = 0;
1213
1214 /* invalid cardinality */
1215#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1216 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001217 &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001218
1219 TEST_DUP("description", "a", "b");
1220 TEST_DUP("reference", "a", "b");
1221
1222 /* full content */
1223 str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
1224 assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d));
1225 assert_non_null(d);
1226 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001227 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001228 d = NULL;
1229
1230 /* missing mandatory substatement */
1231 str = " test {description text;}";
1232 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1233 logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001234 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001235 d = NULL;
1236
1237 /* invalid substatement */
1238 str = " test {deviate not-supported; status obsolete;}";
1239 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1240 logbuf_assert("Invalid keyword \"status\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001241 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001242 d = NULL;
1243
1244#undef TEST_DUP
1245
1246 ly_ctx_destroy(ctx.ctx, NULL);
1247}
1248
1249static void
1250test_deviate(void **state)
1251{
1252 (void) state; /* unused */
1253
Michal Vaskob36053d2020-03-26 15:49:30 +01001254 struct lys_yang_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001255 struct lysp_deviate *d = NULL;
1256 const char *str;
1257
Michal Vaskob36053d2020-03-26 15:49:30 +01001258 ctx.format = LYS_IN_YANG;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001259 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1260 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001261 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001262 ctx.line = 1;
1263 ctx.indent = 0;
1264
1265 /* invalid cardinality */
1266#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
1267 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001268 &d, "1", lysp_deviate_free(ctx.ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001269
1270 TEST_DUP("add", "config", "true", "false");
1271 TEST_DUP("replace", "default", "int8", "uint8");
1272 TEST_DUP("add", "mandatory", "true", "false");
1273 TEST_DUP("add", "max-elements", "1", "2");
1274 TEST_DUP("add", "min-elements", "1", "2");
1275 TEST_DUP("replace", "type", "int8", "uint8");
1276 TEST_DUP("add", "units", "kilometers", "miles");
1277
1278 /* full contents */
1279 str = " not-supported {prefix:ext;} ...";
1280 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1281 assert_non_null(d);
1282 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001283 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001284 str = " 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;} ...";
1285 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1286 assert_non_null(d);
1287 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001288 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001289 str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...";
1290 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1291 assert_non_null(d);
1292 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001293 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001294 str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...";
1295 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1296 assert_non_null(d);
1297 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001298 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001299
1300 /* invalid substatements */
1301#define TEST_NOT_SUP(DEV, STMT, VALUE) \
1302 str = " "DEV" {"STMT" "VALUE";}..."; \
1303 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \
1304 logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001305 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001306
1307 TEST_NOT_SUP("not-supported", "units", "meters");
1308 TEST_NOT_SUP("not-supported", "must", "1");
1309 TEST_NOT_SUP("not-supported", "unique", "x");
1310 TEST_NOT_SUP("not-supported", "default", "a");
1311 TEST_NOT_SUP("not-supported", "config", "true");
1312 TEST_NOT_SUP("not-supported", "mandatory", "true");
1313 TEST_NOT_SUP("not-supported", "min-elements", "1");
1314 TEST_NOT_SUP("not-supported", "max-elements", "2");
1315 TEST_NOT_SUP("not-supported", "type", "string");
1316 TEST_NOT_SUP("add", "type", "string");
1317 TEST_NOT_SUP("delete", "config", "true");
1318 TEST_NOT_SUP("delete", "mandatory", "true");
1319 TEST_NOT_SUP("delete", "min-elements", "1");
1320 TEST_NOT_SUP("delete", "max-elements", "2");
1321 TEST_NOT_SUP("delete", "type", "string");
1322 TEST_NOT_SUP("replace", "must", "1");
1323 TEST_NOT_SUP("replace", "unique", "a");
1324
1325 str = " nonsence; ...";
1326 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d));
1327 logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1.");
1328 assert_null(d);
1329
1330#undef TEST_NOT_SUP
1331#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001332
1333 ly_ctx_destroy(ctx.ctx, NULL);
1334}
1335
Radek Krejci8c370832018-11-02 15:10:03 +01001336static void
1337test_container(void **state)
1338{
1339 (void) state; /* unused */
1340
Michal Vaskob36053d2020-03-26 15:49:30 +01001341 struct lys_yang_parser_ctx ctx = {0};
Radek Krejci8c370832018-11-02 15:10:03 +01001342 struct lysp_node_container *c = NULL;
1343 const char *str;
1344
Michal Vaskob36053d2020-03-26 15:49:30 +01001345 ctx.format = LYS_IN_YANG;
Radek Krejci8c370832018-11-02 15:10:03 +01001346 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1347 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001348 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci8c370832018-11-02 15:10:03 +01001349 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001350 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +01001351
1352 /* invalid cardinality */
1353#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1354 str = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1355 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); \
1356 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001357 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001358
1359 TEST_DUP("config", "true", "false");
1360 TEST_DUP("description", "text1", "text2");
1361 TEST_DUP("presence", "true", "false");
1362 TEST_DUP("reference", "1", "2");
1363 TEST_DUP("status", "current", "obsolete");
1364 TEST_DUP("when", "true", "false");
1365#undef TEST_DUP
1366
1367 /* full content */
Radek Krejci313d9902018-11-08 09:42:58 +01001368 str = "cont {action x;anydata any;anyxml anyxml; choice ch;config false;container c;description test;grouping g;if-feature f; leaf l {type string;}"
1369 "leaf-list ll {type string;} list li;must 'expr';notification not; presence true; reference test;status current;typedef t {type int8;}uses g;when true;m:ext;} ...";
Radek Krejci8c370832018-11-02 15:10:03 +01001370 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1371 assert_non_null(c);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001372 assert_int_equal(LYS_CONTAINER, c->nodetype);
1373 assert_string_equal("cont", c->name);
Radek Krejci8c370832018-11-02 15:10:03 +01001374 assert_non_null(c->actions);
1375 assert_non_null(c->child);
1376 assert_string_equal("test", c->dsc);
1377 assert_non_null(c->exts);
1378 assert_non_null(c->groupings);
1379 assert_non_null(c->iffeatures);
1380 assert_non_null(c->musts);
1381 assert_non_null(c->notifs);
1382 assert_string_equal("true", c->presence);
1383 assert_string_equal("test", c->ref);
1384 assert_non_null(c->typedefs);
1385 assert_non_null(c->when);
1386 assert_null(c->parent);
1387 assert_null(c->next);
1388 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags);
Radek Krejci313d9902018-11-08 09:42:58 +01001389 ly_set_erase(&ctx.tpdfs_nodes, NULL);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001390 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001391
1392 /* invalid */
1393 str = " cont {augment /root;} ...";
1394 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1395 logbuf_assert("Invalid keyword \"augment\" as a child of \"container\". Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001396 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001397 str = " cont {nonsence true;} ...";
1398 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1399 logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001400 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001401
Radek Krejcif538ce52019-03-05 10:46:14 +01001402 ctx.mod_version = 1; /* simulate YANG 1.0 */
1403 str = " cont {action x;} ...";
1404 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1405 logbuf_assert("Invalid keyword \"action\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1406 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
1407
Radek Krejci8c370832018-11-02 15:10:03 +01001408 ly_ctx_destroy(ctx.ctx, NULL);
1409}
1410
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001411static void
1412test_leaf(void **state)
1413{
1414 *state = test_leaf;
1415
Michal Vaskob36053d2020-03-26 15:49:30 +01001416 struct lys_yang_parser_ctx ctx = {0};
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001417 struct lysp_node_leaf *l = NULL;
1418 const char *str;
1419
Michal Vaskob36053d2020-03-26 15:49:30 +01001420 ctx.format = LYS_IN_YANG;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001421 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1422 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001423 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001424 ctx.line = 1;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001425 //ctx.mod->version = 2; /* simulate YANG 1.1 */
1426
1427 /* invalid cardinality */
1428#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1429 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1430 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1431 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1432 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1433
1434 TEST_DUP("config", "true", "false");
1435 TEST_DUP("default", "x", "y");
1436 TEST_DUP("description", "text1", "text2");
1437 TEST_DUP("mandatory", "true", "false");
1438 TEST_DUP("reference", "1", "2");
1439 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001440 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001441 TEST_DUP("units", "text1", "text2");
1442 TEST_DUP("when", "true", "false");
1443#undef TEST_DUP
1444
1445 /* full content - without mandatory which is mutual exclusive with default */
1446 str = "l {config false;default \"xxx\";description test;if-feature f;"
1447 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
1448 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1449 assert_non_null(l);
1450 assert_int_equal(LYS_LEAF, l->nodetype);
1451 assert_string_equal("l", l->name);
1452 assert_string_equal("test", l->dsc);
1453 assert_string_equal("xxx", l->dflt);
1454 assert_string_equal("yyy", l->units);
1455 assert_string_equal("string", l->type.name);
1456 assert_non_null(l->exts);
1457 assert_non_null(l->iffeatures);
1458 assert_non_null(l->musts);
1459 assert_string_equal("test", l->ref);
1460 assert_non_null(l->when);
1461 assert_null(l->parent);
1462 assert_null(l->next);
1463 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, l->flags);
1464 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1465
1466 /* full content - now with mandatory */
1467 str = "l {mandatory true; type string;} ...";
1468 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1469 assert_non_null(l);
1470 assert_int_equal(LYS_LEAF, l->nodetype);
1471 assert_string_equal("l", l->name);
1472 assert_string_equal("string", l->type.name);
1473 assert_int_equal(LYS_MAND_TRUE, l->flags);
1474 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1475
1476 /* invalid */
1477 str = " l {mandatory true; default xx; type string;} ...";
1478 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001479 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"leaf\". Line number 1.");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001480 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1481
1482 str = " l {description \"missing type\";} ...";
1483 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1484 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf\". Line number 1.");
1485 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1486
1487 *state = NULL;
1488 ly_ctx_destroy(ctx.ctx, NULL);
1489}
1490
Radek Krejci0e5d8382018-11-28 16:37:53 +01001491static void
1492test_leaflist(void **state)
1493{
1494 *state = test_leaf;
1495
Michal Vaskob36053d2020-03-26 15:49:30 +01001496 struct lys_yang_parser_ctx ctx = {0};
Radek Krejci0e5d8382018-11-28 16:37:53 +01001497 struct lysp_node_leaflist *ll = NULL;
1498 const char *str;
1499
Michal Vaskob36053d2020-03-26 15:49:30 +01001500 ctx.format = LYS_IN_YANG;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001501 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1502 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001503 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001504 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001505 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001506
1507 /* invalid cardinality */
1508#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1509 str = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1510 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); \
1511 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1512 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1513
1514 TEST_DUP("config", "true", "false");
1515 TEST_DUP("description", "text1", "text2");
1516 TEST_DUP("max-elements", "10", "20");
1517 TEST_DUP("min-elements", "10", "20");
1518 TEST_DUP("ordered-by", "user", "system");
1519 TEST_DUP("reference", "1", "2");
1520 TEST_DUP("status", "current", "obsolete");
1521 TEST_DUP("type", "int8", "uint8");
1522 TEST_DUP("units", "text1", "text2");
1523 TEST_DUP("when", "true", "false");
1524#undef TEST_DUP
1525
1526 /* full content - without min-elements which is mutual exclusive with default */
1527 str = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
1528 "max-elements 10;must 'expr';ordered-by user;reference test;"
1529 "status current;type string; units zzz;when true;m:ext;} ...";
1530 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1531 assert_non_null(ll);
1532 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1533 assert_string_equal("ll", ll->name);
1534 assert_string_equal("test", ll->dsc);
1535 assert_non_null(ll->dflts);
1536 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
1537 assert_string_equal("xxx", ll->dflts[0]);
1538 assert_string_equal("yyy", ll->dflts[1]);
1539 assert_string_equal("zzz", ll->units);
1540 assert_int_equal(10, ll->max);
1541 assert_int_equal(0, ll->min);
1542 assert_string_equal("string", ll->type.name);
1543 assert_non_null(ll->exts);
1544 assert_non_null(ll->iffeatures);
1545 assert_non_null(ll->musts);
1546 assert_string_equal("test", ll->ref);
1547 assert_non_null(ll->when);
1548 assert_null(ll->parent);
1549 assert_null(ll->next);
1550 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
1551 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1552
1553 /* full content - now with min-elements */
1554 str = "ll {min-elements 10; type string;} ...";
1555 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1556 assert_non_null(ll);
1557 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1558 assert_string_equal("ll", ll->name);
1559 assert_string_equal("string", ll->type.name);
1560 assert_int_equal(0, ll->max);
1561 assert_int_equal(10, ll->min);
1562 assert_int_equal(LYS_SET_MIN, ll->flags);
1563 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1564
1565 /* invalid */
1566 str = " ll {min-elements 1; default xx; type string;} ...";
1567 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001568 logbuf_assert("Invalid combination of keywords \"min-elements\" and \"default\" as substatements of \"leaf-list\". Line number 1.");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001569 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1570
1571 str = " ll {description \"missing type\";} ...";
1572 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1573 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf-list\". Line number 1.");
1574 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1575
Radek Krejcidf6cad12018-11-28 17:10:55 +01001576 str = " ll {type string; min-elements 10; max-elements 1;} ..."; /* invalid combination of min/max */
1577 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1578 logbuf_assert("Invalid combination of min-elements and max-elements: min value 10 is bigger than the max value 1. Line number 1.");
1579 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1580
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001581 ctx.mod_version = 1; /* simulate YANG 1.0 - default statement is not allowed */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001582 str = " ll {default xx; type string;} ...";
1583 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1584 logbuf_assert("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1585 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1586
1587 *state = NULL;
1588 ly_ctx_destroy(ctx.ctx, NULL);
1589}
1590
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001591static void
1592test_list(void **state)
1593{
1594 *state = test_list;
1595
Michal Vaskob36053d2020-03-26 15:49:30 +01001596 struct lys_yang_parser_ctx ctx = {0};
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001597 struct lysp_node_list *l = NULL;
1598 const char *str;
1599
Michal Vaskob36053d2020-03-26 15:49:30 +01001600 ctx.format = LYS_IN_YANG;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001601 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1602 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001603 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001604 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001605 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001606
1607 /* invalid cardinality */
1608#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1609 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1610 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1611 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1612 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1613
1614 TEST_DUP("config", "true", "false");
1615 TEST_DUP("description", "text1", "text2");
1616 TEST_DUP("key", "one", "two");
1617 TEST_DUP("max-elements", "10", "20");
1618 TEST_DUP("min-elements", "10", "20");
1619 TEST_DUP("ordered-by", "user", "system");
1620 TEST_DUP("reference", "1", "2");
1621 TEST_DUP("status", "current", "obsolete");
1622 TEST_DUP("when", "true", "false");
1623#undef TEST_DUP
1624
1625 /* full content */
1626 str = "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;}"
1627 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1628 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
1629 assert_int_equal(LY_SUCCESS, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1630 assert_non_null(l);
1631 assert_int_equal(LYS_LIST, l->nodetype);
1632 assert_string_equal("l", l->name);
1633 assert_string_equal("test", l->dsc);
1634 assert_string_equal("l", l->key);
1635 assert_non_null(l->uniques);
1636 assert_int_equal(2, LY_ARRAY_SIZE(l->uniques));
1637 assert_string_equal("xxx", l->uniques[0]);
1638 assert_string_equal("yyy", l->uniques[1]);
1639 assert_int_equal(10, l->max);
1640 assert_int_equal(1, l->min);
1641 assert_non_null(l->exts);
1642 assert_non_null(l->iffeatures);
1643 assert_non_null(l->musts);
1644 assert_string_equal("test", l->ref);
1645 assert_non_null(l->when);
1646 assert_null(l->parent);
1647 assert_null(l->next);
1648 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, l->flags);
1649 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1650 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1651
Radek Krejcif538ce52019-03-05 10:46:14 +01001652 /* invalid content */
1653 ctx.mod_version = 1; /* simulate YANG 1.0 */
1654 str = "l {action x;} ...";
1655 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1656 logbuf_assert("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1657 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1658
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001659 *state = NULL;
1660 ly_ctx_destroy(ctx.ctx, NULL);
1661}
1662
Radek Krejci056d0a82018-12-06 16:57:25 +01001663static void
1664test_choice(void **state)
1665{
1666 *state = test_choice;
1667
Michal Vaskob36053d2020-03-26 15:49:30 +01001668 struct lys_yang_parser_ctx ctx = {0};
Radek Krejci056d0a82018-12-06 16:57:25 +01001669 struct lysp_node_choice *ch = NULL;
1670 const char *str;
1671
Michal Vaskob36053d2020-03-26 15:49:30 +01001672 ctx.format = LYS_IN_YANG;
Radek Krejci056d0a82018-12-06 16:57:25 +01001673 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1674 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001675 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci056d0a82018-12-06 16:57:25 +01001676 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001677 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci056d0a82018-12-06 16:57:25 +01001678
1679 /* invalid cardinality */
1680#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1681 str = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1682 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); \
1683 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1684 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1685
1686 TEST_DUP("config", "true", "false");
1687 TEST_DUP("default", "a", "b");
1688 TEST_DUP("description", "text1", "text2");
1689 TEST_DUP("mandatory", "true", "false");
1690 TEST_DUP("reference", "1", "2");
1691 TEST_DUP("status", "current", "obsolete");
1692 TEST_DUP("when", "true", "false");
1693#undef TEST_DUP
1694
Radek Krejcia9026eb2018-12-12 16:04:47 +01001695 /* full content - without default due to a collision with mandatory */
1696 str = "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 +01001697 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
1698 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1699 assert_non_null(ch);
1700 assert_int_equal(LYS_CHOICE, ch->nodetype);
1701 assert_string_equal("ch", ch->name);
1702 assert_string_equal("test", ch->dsc);
1703 assert_non_null(ch->exts);
1704 assert_non_null(ch->iffeatures);
1705 assert_string_equal("test", ch->ref);
1706 assert_non_null(ch->when);
1707 assert_null(ch->parent);
1708 assert_null(ch->next);
1709 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, ch->flags);
1710 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1711
Radek Krejcia9026eb2018-12-12 16:04:47 +01001712 /* full content - the default missing from the previous node */
1713 str = "ch {default c;case c;} ...";
1714 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1715 assert_non_null(ch);
1716 assert_int_equal(LYS_CHOICE, ch->nodetype);
1717 assert_string_equal("ch", ch->name);
1718 assert_string_equal("c", ch->dflt);
1719 assert_int_equal(0, ch->flags);
1720 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1721
1722 /* invalid content */
1723 str = "ch {mandatory true; default c1; case c1 {leaf x{type string;}}} ...";
1724 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1725 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"choice\". Line number 1.");
1726 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1727
1728 *state = NULL;
1729 ly_ctx_destroy(ctx.ctx, NULL);
1730}
1731
1732static void
1733test_case(void **state)
1734{
1735 *state = test_case;
1736
Michal Vaskob36053d2020-03-26 15:49:30 +01001737 struct lys_yang_parser_ctx ctx = {0};
Radek Krejcia9026eb2018-12-12 16:04:47 +01001738 struct lysp_node_case *cs = NULL;
1739 const char *str;
1740
Michal Vaskob36053d2020-03-26 15:49:30 +01001741 ctx.format = LYS_IN_YANG;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001742 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1743 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001744 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001745 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001746 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcia9026eb2018-12-12 16:04:47 +01001747
1748 /* invalid cardinality */
1749#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1750 str = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1751 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs)); \
1752 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1753 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1754
1755 TEST_DUP("description", "text1", "text2");
1756 TEST_DUP("reference", "1", "2");
1757 TEST_DUP("status", "current", "obsolete");
1758 TEST_DUP("when", "true", "false");
1759#undef TEST_DUP
1760
1761 /* full content */
1762 str = "cs {anydata any;anyxml anyxml; choice ch;container c;description test;if-feature f;leaf l {type string;}"
1763 "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
1764 assert_int_equal(LY_SUCCESS, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1765 assert_non_null(cs);
1766 assert_int_equal(LYS_CASE, cs->nodetype);
1767 assert_string_equal("cs", cs->name);
1768 assert_string_equal("test", cs->dsc);
1769 assert_non_null(cs->exts);
1770 assert_non_null(cs->iffeatures);
1771 assert_string_equal("test", cs->ref);
1772 assert_non_null(cs->when);
1773 assert_null(cs->parent);
1774 assert_null(cs->next);
1775 assert_int_equal(LYS_STATUS_CURR, cs->flags);
1776 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1777
1778 /* invalid content */
1779 str = "cs {config true} ...";
1780 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1781 logbuf_assert("Invalid keyword \"config\" as a child of \"case\". Line number 1.");
1782 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1783
Radek Krejci056d0a82018-12-06 16:57:25 +01001784 *state = NULL;
1785 ly_ctx_destroy(ctx.ctx, NULL);
1786}
1787
Radek Krejci9800fb82018-12-13 14:26:23 +01001788static void
Radek Krejcid6b76452019-09-03 17:03:03 +02001789test_any(void **state, enum ly_stmt kw)
Radek Krejci9800fb82018-12-13 14:26:23 +01001790{
1791 *state = test_any;
1792
Michal Vaskob36053d2020-03-26 15:49:30 +01001793 struct lys_yang_parser_ctx ctx = {0};
Radek Krejci9800fb82018-12-13 14:26:23 +01001794 struct lysp_node_anydata *any = NULL;
1795 const char *str;
1796
Michal Vaskob36053d2020-03-26 15:49:30 +01001797 ctx.format = LYS_IN_YANG;
Radek Krejci9800fb82018-12-13 14:26:23 +01001798 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1799 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001800 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci9800fb82018-12-13 14:26:23 +01001801 ctx.line = 1;
Radek Krejcid6b76452019-09-03 17:03:03 +02001802 if (kw == LY_STMT_ANYDATA) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001803 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001804 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001805 ctx.mod_version = 1; /* simulate YANG 1.0 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001806 }
1807
1808 /* invalid cardinality */
1809#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1810 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1811 assert_int_equal(LY_EVALID, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any)); \
1812 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1813 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1814
1815 TEST_DUP("config", "true", "false");
1816 TEST_DUP("description", "text1", "text2");
1817 TEST_DUP("mandatory", "true", "false");
1818 TEST_DUP("reference", "1", "2");
1819 TEST_DUP("status", "current", "obsolete");
1820 TEST_DUP("when", "true", "false");
1821#undef TEST_DUP
1822
1823 /* full content */
1824 str = "any {config true;description test;if-feature f;mandatory true;must 'expr';reference test;status current;when true;m:ext;} ...";
1825 assert_int_equal(LY_SUCCESS, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any));
1826 assert_non_null(any);
Radek Krejcid6b76452019-09-03 17:03:03 +02001827 assert_int_equal(kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML, any->nodetype);
Radek Krejci9800fb82018-12-13 14:26:23 +01001828 assert_string_equal("any", any->name);
1829 assert_string_equal("test", any->dsc);
1830 assert_non_null(any->exts);
1831 assert_non_null(any->iffeatures);
1832 assert_non_null(any->musts);
1833 assert_string_equal("test", any->ref);
1834 assert_non_null(any->when);
1835 assert_null(any->parent);
1836 assert_null(any->next);
1837 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags);
1838 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1839
1840 *state = NULL;
1841 ly_ctx_destroy(ctx.ctx, NULL);
1842}
1843
1844static void
1845test_anydata(void **state)
1846{
Radek Krejcid6b76452019-09-03 17:03:03 +02001847 return test_any(state, LY_STMT_ANYDATA);
Radek Krejci9800fb82018-12-13 14:26:23 +01001848}
1849
1850static void
1851test_anyxml(void **state)
1852{
Radek Krejcid6b76452019-09-03 17:03:03 +02001853 return test_any(state, LY_STMT_ANYXML);
Radek Krejci9800fb82018-12-13 14:26:23 +01001854}
1855
Radek Krejcie86bf772018-12-14 11:39:53 +01001856static void
1857test_grouping(void **state)
1858{
1859 *state = test_grouping;
1860
Michal Vaskob36053d2020-03-26 15:49:30 +01001861 struct lys_yang_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01001862 struct lysp_grp *grp = NULL;
1863 const char *str;
1864
Michal Vaskob36053d2020-03-26 15:49:30 +01001865 ctx.format = LYS_IN_YANG;
Radek Krejcie86bf772018-12-14 11:39:53 +01001866 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1867 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001868 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcie86bf772018-12-14 11:39:53 +01001869 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001870 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001871
1872 /* invalid cardinality */
1873#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1874 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1875 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp)); \
1876 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1877 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1878
1879 TEST_DUP("description", "text1", "text2");
1880 TEST_DUP("reference", "1", "2");
1881 TEST_DUP("status", "current", "obsolete");
1882#undef TEST_DUP
1883
1884 /* full content */
1885 str = "grp {action x;anydata any;anyxml anyxml; choice ch;container c;description test;grouping g;leaf l {type string;}"
1886 "leaf-list ll {type string;} list li;notification not;reference test;status current;typedef t {type int8;}uses g;m:ext;} ...";
1887 assert_int_equal(LY_SUCCESS, parse_grouping(&ctx, &str, NULL, &grp));
1888 assert_non_null(grp);
1889 assert_int_equal(LYS_GROUPING, grp->nodetype);
1890 assert_string_equal("grp", grp->name);
1891 assert_string_equal("test", grp->dsc);
1892 assert_non_null(grp->exts);
1893 assert_string_equal("test", grp->ref);
1894 assert_null(grp->parent);
1895 assert_int_equal( LYS_STATUS_CURR, grp->flags);
1896 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1897 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1898
1899 /* invalid content */
1900 str = "grp {config true} ...";
1901 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1902 logbuf_assert("Invalid keyword \"config\" as a child of \"grouping\". Line number 1.");
1903 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1904
1905 str = "grp {must 'expr'} ...";
1906 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1907 logbuf_assert("Invalid keyword \"must\" as a child of \"grouping\". Line number 1.");
1908 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1909
1910 *state = NULL;
1911 ly_ctx_destroy(ctx.ctx, NULL);
1912}
1913
1914static void
Radek Krejcif538ce52019-03-05 10:46:14 +01001915test_action(void **state)
1916{
1917 *state = test_action;
1918
Michal Vaskob36053d2020-03-26 15:49:30 +01001919 struct lys_yang_parser_ctx ctx = {0};
Radek Krejcif538ce52019-03-05 10:46:14 +01001920 struct lysp_action *rpcs = NULL;
1921 struct lysp_node_container *c = NULL;
1922 const char *str;
1923
Michal Vaskob36053d2020-03-26 15:49:30 +01001924 ctx.format = LYS_IN_YANG;
Radek Krejcif538ce52019-03-05 10:46:14 +01001925 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1926 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001927 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcif538ce52019-03-05 10:46:14 +01001928 ctx.line = 1;
1929 ctx.mod_version = 2; /* simulate YANG 1.1 */
1930
1931 /* invalid cardinality */
1932#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1933 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1934 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs)); \
1935 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1936 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1937
1938 TEST_DUP("description", "text1", "text2");
Michal Vaskob83af8a2020-01-06 09:49:22 +01001939 TEST_DUP("input", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
1940 TEST_DUP("output", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
Radek Krejcif538ce52019-03-05 10:46:14 +01001941 TEST_DUP("reference", "1", "2");
1942 TEST_DUP("status", "current", "obsolete");
1943#undef TEST_DUP
1944
1945 /* full content */
1946 str = "top;";
1947 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1948 str = "func {description test;grouping grp;if-feature f;reference test;status current;typedef mytype {type int8;} m:ext;"
1949 "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1950 " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}"
1951 "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1952 " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ...";
1953 assert_int_equal(LY_SUCCESS, parse_action(&ctx, &str, (struct lysp_node*)c, &rpcs));
1954 assert_non_null(rpcs);
1955 assert_int_equal(LYS_ACTION, rpcs->nodetype);
1956 assert_string_equal("func", rpcs->name);
1957 assert_string_equal("test", rpcs->dsc);
1958 assert_non_null(rpcs->exts);
1959 assert_non_null(rpcs->iffeatures);
1960 assert_string_equal("test", rpcs->ref);
1961 assert_non_null(rpcs->groupings);
1962 assert_non_null(rpcs->typedefs);
1963 assert_int_equal(LYS_STATUS_CURR, rpcs->flags);
1964 /* input */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001965 assert_int_equal(rpcs->input.nodetype, LYS_INPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001966 assert_non_null(rpcs->input.groupings);
1967 assert_non_null(rpcs->input.exts);
1968 assert_non_null(rpcs->input.musts);
1969 assert_non_null(rpcs->input.typedefs);
1970 assert_non_null(rpcs->input.data);
1971 /* output */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001972 assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001973 assert_non_null(rpcs->output.groupings);
1974 assert_non_null(rpcs->output.exts);
1975 assert_non_null(rpcs->output.musts);
1976 assert_non_null(rpcs->output.typedefs);
1977 assert_non_null(rpcs->output.data);
1978
1979 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1980 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1981
1982 /* invalid content */
1983 str = "func {config true} ...";
1984 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs));
1985 logbuf_assert("Invalid keyword \"config\" as a child of \"rpc\". Line number 1.");
1986 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1987
1988 *state = NULL;
1989 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
1990 ly_ctx_destroy(ctx.ctx, NULL);
1991}
1992
1993static void
Radek Krejcifc11bd72019-04-11 16:00:05 +02001994test_notification(void **state)
1995{
1996 *state = test_notification;
1997
Michal Vaskob36053d2020-03-26 15:49:30 +01001998 struct lys_yang_parser_ctx ctx = {0};
Radek Krejcifc11bd72019-04-11 16:00:05 +02001999 struct lysp_notif *notifs = NULL;
2000 struct lysp_node_container *c = NULL;
2001 const char *str;
2002
Michal Vaskob36053d2020-03-26 15:49:30 +01002003 ctx.format = LYS_IN_YANG;
Radek Krejcifc11bd72019-04-11 16:00:05 +02002004 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2005 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002006 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcifc11bd72019-04-11 16:00:05 +02002007 ctx.line = 1;
2008 ctx.mod_version = 2; /* simulate YANG 1.1 */
2009
2010 /* invalid cardinality */
2011#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2012 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2013 assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, &notifs)); \
2014 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2015 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2016
2017 TEST_DUP("description", "text1", "text2");
2018 TEST_DUP("reference", "1", "2");
2019 TEST_DUP("status", "current", "obsolete");
2020#undef TEST_DUP
2021
2022 /* full content */
2023 str = "top;";
2024 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
2025 str = "ntf {anydata a1; anyxml a2; choice ch; container c; description test; grouping grp; if-feature f; leaf l {type int8;}"
2026 "leaf-list ll {type int8;} list li; must 1; reference test; status current; typedef mytype {type int8;} uses grp; m:ext;}";
2027 assert_int_equal(LY_SUCCESS, parse_notif(&ctx, &str, (struct lysp_node*)c, &notifs));
2028 assert_non_null(notifs);
2029 assert_int_equal(LYS_NOTIF, notifs->nodetype);
2030 assert_string_equal("ntf", notifs->name);
2031 assert_string_equal("test", notifs->dsc);
2032 assert_non_null(notifs->exts);
2033 assert_non_null(notifs->iffeatures);
2034 assert_string_equal("test", notifs->ref);
2035 assert_non_null(notifs->groupings);
2036 assert_non_null(notifs->typedefs);
2037 assert_non_null(notifs->musts);
2038 assert_non_null(notifs->data);
2039 assert_int_equal(LYS_STATUS_CURR, notifs->flags);
2040
2041 ly_set_erase(&ctx.tpdfs_nodes, NULL);
2042 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2043
2044 /* invalid content */
2045 str = "ntf {config true} ...";
2046 assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, &notifs));
2047 logbuf_assert("Invalid keyword \"config\" as a child of \"notification\". Line number 1.");
2048 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2049
2050 *state = NULL;
2051 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
2052 ly_ctx_destroy(ctx.ctx, NULL);
2053}
2054
2055static void
Radek Krejcie86bf772018-12-14 11:39:53 +01002056test_uses(void **state)
2057{
2058 *state = test_uses;
2059
Michal Vaskob36053d2020-03-26 15:49:30 +01002060 struct lys_yang_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01002061 struct lysp_node_uses *u = NULL;
2062 const char *str;
2063
Michal Vaskob36053d2020-03-26 15:49:30 +01002064 ctx.format = LYS_IN_YANG;
Radek Krejcie86bf772018-12-14 11:39:53 +01002065 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2066 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002067 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcie86bf772018-12-14 11:39:53 +01002068 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002069 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01002070
2071 /* invalid cardinality */
2072#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2073 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2074 assert_int_equal(LY_EVALID, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u)); \
2075 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2076 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2077
2078 TEST_DUP("description", "text1", "text2");
2079 TEST_DUP("reference", "1", "2");
2080 TEST_DUP("status", "current", "obsolete");
2081 TEST_DUP("when", "true", "false");
2082#undef TEST_DUP
2083
2084 /* full content */
2085 str = "grpref {augment some/node;description test;if-feature f;reference test;refine some/other/node;status current;when true;m:ext;} ...";
2086 assert_int_equal(LY_SUCCESS, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u));
2087 assert_non_null(u);
2088 assert_int_equal(LYS_USES, u->nodetype);
2089 assert_string_equal("grpref", u->name);
2090 assert_string_equal("test", u->dsc);
2091 assert_non_null(u->exts);
2092 assert_non_null(u->iffeatures);
2093 assert_string_equal("test", u->ref);
2094 assert_non_null(u->augments);
2095 assert_non_null(u->refines);
2096 assert_non_null(u->when);
2097 assert_null(u->parent);
2098 assert_null(u->next);
2099 assert_int_equal(LYS_STATUS_CURR, u->flags);
2100 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2101
2102 *state = NULL;
2103 ly_ctx_destroy(ctx.ctx, NULL);
2104}
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002105
2106
2107static void
2108test_augment(void **state)
2109{
2110 *state = test_augment;
2111
Michal Vaskob36053d2020-03-26 15:49:30 +01002112 struct lys_yang_parser_ctx ctx = {0};
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002113 struct lysp_augment *a = NULL;
2114 const char *str;
2115
Michal Vaskob36053d2020-03-26 15:49:30 +01002116 ctx.format = LYS_IN_YANG;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002117 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2118 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002119 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002120 ctx.line = 1;
Radek Krejcib4adbf12019-02-25 13:35:22 +01002121 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002122
2123 /* invalid cardinality */
2124#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2125 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2126 assert_int_equal(LY_EVALID, parse_augment(&ctx, &str, NULL, &a)); \
2127 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejcib4adbf12019-02-25 13:35:22 +01002128 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002129
2130 TEST_DUP("description", "text1", "text2");
2131 TEST_DUP("reference", "1", "2");
2132 TEST_DUP("status", "current", "obsolete");
2133 TEST_DUP("when", "true", "false");
2134#undef TEST_DUP
2135
2136 /* full content */
2137 str = "/target/nodeid {action x; anydata any;anyxml anyxml; case cs; choice ch;container c;description test;if-feature f;leaf l {type string;}"
2138 "leaf-list ll {type string;} list li;notification not;reference test;status current;uses g;when true;m:ext;} ...";
2139 assert_int_equal(LY_SUCCESS, parse_augment(&ctx, &str, NULL, &a));
2140 assert_non_null(a);
2141 assert_int_equal(LYS_AUGMENT, a->nodetype);
2142 assert_string_equal("/target/nodeid", a->nodeid);
2143 assert_string_equal("test", a->dsc);
2144 assert_non_null(a->exts);
2145 assert_non_null(a->iffeatures);
2146 assert_string_equal("test", a->ref);
2147 assert_non_null(a->when);
2148 assert_null(a->parent);
2149 assert_int_equal(LYS_STATUS_CURR, a->flags);
Radek Krejcib4adbf12019-02-25 13:35:22 +01002150 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002151
2152 *state = NULL;
2153 ly_ctx_destroy(ctx.ctx, NULL);
2154}
2155
Radek Krejcif09e4e82019-06-14 15:08:11 +02002156static void
2157test_when(void **state)
2158{
2159 *state = test_when;
2160
Michal Vaskob36053d2020-03-26 15:49:30 +01002161 struct lys_yang_parser_ctx ctx = {0};
Radek Krejcif09e4e82019-06-14 15:08:11 +02002162 struct lysp_when *w = NULL;
2163 const char *str;
2164
Michal Vaskob36053d2020-03-26 15:49:30 +01002165 ctx.format = LYS_IN_YANG;
Radek Krejcif09e4e82019-06-14 15:08:11 +02002166 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2167 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002168 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcif09e4e82019-06-14 15:08:11 +02002169 ctx.line = 1;
2170 ctx.mod_version = 2; /* simulate YANG 1.1 */
2171
2172 /* invalid cardinality */
2173#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2174 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2175 assert_int_equal(LY_EVALID, parse_when(&ctx, &str, &w)); \
2176 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2177 FREE_MEMBER(ctx.ctx, w, lysp_when_free); w = NULL;
2178
2179 TEST_DUP("description", "text1", "text2");
2180 TEST_DUP("reference", "1", "2");
2181#undef TEST_DUP
2182
2183 /* full content */
2184 str = "expression {description test;reference test;m:ext;} ...";
2185 assert_int_equal(LY_SUCCESS, parse_when(&ctx, &str, &w));
2186 assert_non_null(w);
2187 assert_string_equal("expression", w->cond);
2188 assert_string_equal("test", w->dsc);
2189 assert_string_equal("test", w->ref);
2190 assert_non_null(w->exts);
2191 FREE_MEMBER(ctx.ctx, w, lysp_when_free); w = NULL;
2192
2193 /* empty condition */
2194 str = "\"\";";
2195 assert_int_equal(LY_SUCCESS, parse_when(&ctx, &str, &w));
2196 logbuf_assert("Empty argument of when statement does not make sense.");
2197 assert_non_null(w);
2198 assert_string_equal("", w->cond);
2199 FREE_MEMBER(ctx.ctx, w, lysp_when_free); w = NULL;
2200
2201 *state = NULL;
2202 ly_ctx_destroy(ctx.ctx, NULL);
2203}
2204
David Sedlákd6ce6d72019-07-16 17:30:18 +02002205static void
2206test_value(void **state)
2207{
2208 *state = test_value;
Michal Vaskob36053d2020-03-26 15:49:30 +01002209 struct lys_yang_parser_ctx ctx;
David Sedlákd6ce6d72019-07-16 17:30:18 +02002210
Michal Vaskob36053d2020-03-26 15:49:30 +01002211 ctx.format = LYS_IN_YANG;
David Sedlákd6ce6d72019-07-16 17:30:18 +02002212 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2213 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002214 ctx.pos_type = LY_VLOG_LINE;
David Sedlákd6ce6d72019-07-16 17:30:18 +02002215 ctx.line = 1;
2216 ctx.indent = 0;
2217 int64_t val = 0;
2218 uint16_t flags = 0;
2219
2220 const char *data = "-0;";
Radek Krejcid6b76452019-09-03 17:03:03 +02002221 assert_int_equal(parse_type_enum_value_pos(&ctx, &data, LY_STMT_VALUE, &val, &flags, NULL), LY_SUCCESS);
David Sedlákd6ce6d72019-07-16 17:30:18 +02002222 assert_int_equal(val, 0);
2223
2224 data = "-0;";
2225 flags = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02002226 assert_int_equal(parse_type_enum_value_pos(&ctx, &data, LY_STMT_POSITION, &val, &flags, NULL), LY_EVALID);
David Sedlákd6ce6d72019-07-16 17:30:18 +02002227 logbuf_assert("Invalid value \"-0\" of \"position\". Line number 1.");
2228
David Sedlák1af868e2019-07-17 17:03:14 +02002229 *state = NULL;
David Sedlákd6ce6d72019-07-16 17:30:18 +02002230 ly_ctx_destroy(ctx.ctx, NULL);
2231}
2232
Radek Krejci80dd33e2018-09-26 15:57:18 +02002233int main(void)
2234{
2235 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002236 cmocka_unit_test_setup(test_helpers, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002237 cmocka_unit_test_setup(test_comments, logger_setup),
Radek Krejciefd22f62018-09-27 11:47:58 +02002238 cmocka_unit_test_setup(test_arg, logger_setup),
Radek Krejcidcc7b322018-10-11 14:24:02 +02002239 cmocka_unit_test_setup(test_stmts, logger_setup),
Radek Krejci05b13982018-11-28 16:22:07 +01002240 cmocka_unit_test_setup_teardown(test_minmax, logger_setup, logger_teardown),
Radek Krejci40544fa2019-01-11 09:38:37 +01002241 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
Radek Krejci2c02f3e2018-10-16 10:54:38 +02002242 cmocka_unit_test_setup(test_deviation, logger_setup),
2243 cmocka_unit_test_setup(test_deviate, logger_setup),
Radek Krejci8c370832018-11-02 15:10:03 +01002244 cmocka_unit_test_setup(test_container, logger_setup),
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002245 cmocka_unit_test_setup_teardown(test_leaf, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01002246 cmocka_unit_test_setup_teardown(test_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002247 cmocka_unit_test_setup_teardown(test_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01002248 cmocka_unit_test_setup_teardown(test_choice, logger_setup, logger_teardown),
Radek Krejcia9026eb2018-12-12 16:04:47 +01002249 cmocka_unit_test_setup_teardown(test_case, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01002250 cmocka_unit_test_setup_teardown(test_anydata, logger_setup, logger_teardown),
2251 cmocka_unit_test_setup_teardown(test_anyxml, logger_setup, logger_teardown),
Radek Krejcif538ce52019-03-05 10:46:14 +01002252 cmocka_unit_test_setup_teardown(test_action, logger_setup, logger_teardown),
Radek Krejcifc11bd72019-04-11 16:00:05 +02002253 cmocka_unit_test_setup_teardown(test_notification, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01002254 cmocka_unit_test_setup_teardown(test_grouping, logger_setup, logger_teardown),
2255 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejcib4adbf12019-02-25 13:35:22 +01002256 cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown),
Radek Krejcif09e4e82019-06-14 15:08:11 +02002257 cmocka_unit_test_setup_teardown(test_when, logger_setup, logger_teardown),
David Sedlákd6ce6d72019-07-16 17:30:18 +02002258 cmocka_unit_test_setup_teardown(test_value, logger_setup, logger_teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002259 };
2260
2261 return cmocka_run_group_tests(tests, NULL, NULL);
2262}