blob: 8931fbabe74671955fd5f1d89dd37ba777ca6785 [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 Krejci2d7a47b2019-05-16 13:34:10 +020023#include "../../src/common.h"
24#include "../../src/tree_schema.h"
25#include "../../src/tree_schema_internal.h"
26
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);
29void lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident);
30void lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat);
31void lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev);
32void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp);
33void lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action);
34void lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif);
35void lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment);
36void lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d);
37void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
Radek Krejcif09e4e82019-06-14 15:08:11 +020038void lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020039
40LY_ERR buf_add_char(struct ly_ctx *ctx, const char **input, size_t len, char **buf, size_t *buf_len, size_t *buf_used);
David Sedlák40bb13b2019-07-10 14:34:18 +020041LY_ERR buf_store_char(struct lys_parser_ctx *ctx, const char **input, enum yang_arg arg, char **word_p,
42 size_t *word_len, char **word_b, size_t *buf_len, int need_buf, int *prefix);
Radek Krejcid6b76452019-09-03 17:03:03 +020043LY_ERR get_keyword(struct lys_parser_ctx *ctx, const char **data, enum ly_stmt *kw, char **word_p, size_t *word_len);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020044LY_ERR get_argument(struct lys_parser_ctx *ctx, const char **data, enum yang_arg arg,
45 uint16_t *flags, char **word_p, char **word_b, size_t *word_len);
46LY_ERR skip_comment(struct lys_parser_ctx *ctx, const char **data, int comment);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020047
48LY_ERR parse_action(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions);
Radek Krejcid6b76452019-09-03 17:03:03 +020049LY_ERR parse_any(struct lys_parser_ctx *ctx, const char **data, enum ly_stmt kw, struct lysp_node *parent, struct lysp_node **siblings);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020050LY_ERR parse_augment(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_augment **augments);
51LY_ERR parse_case(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
52LY_ERR parse_container(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
53LY_ERR parse_deviate(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates);
54LY_ERR parse_deviation(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations);
55LY_ERR parse_feature(struct lys_parser_ctx *ctx, const char **data, struct lysp_feature **features);
56LY_ERR parse_grouping(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings);
57LY_ERR parse_choice(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
58LY_ERR parse_identity(struct lys_parser_ctx *ctx, const char **data, struct lysp_ident **identities);
59LY_ERR parse_leaf(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
60LY_ERR parse_leaflist(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
61LY_ERR parse_list(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
62LY_ERR parse_maxelements(struct lys_parser_ctx *ctx, const char **data, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts);
63LY_ERR parse_minelements(struct lys_parser_ctx *ctx, const char **data, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts);
64LY_ERR parse_module(struct lys_parser_ctx *ctx, const char **data, struct lysp_module *mod);
65LY_ERR parse_notif(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_notif **notifs);
66LY_ERR parse_submodule(struct lys_parser_ctx *ctx, const char **data, struct lysp_submodule *submod);
67LY_ERR parse_uses(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
Radek Krejcif09e4e82019-06-14 15:08:11 +020068LY_ERR parse_when(struct lys_parser_ctx *ctx, const char **data, struct lysp_when **when_p);
Radek Krejcid6b76452019-09-03 17:03:03 +020069LY_ERR parse_type_enum_value_pos(struct lys_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 +020070
71#define BUFSIZE 1024
72char logbuf[BUFSIZE] = {0};
Radek Krejci9ed7a192018-10-31 16:23:51 +010073int store = -1; /* negative for infinite logging, positive for limited logging */
Radek Krejci80dd33e2018-09-26 15:57:18 +020074
75/* set to 0 to printing error messages to stderr instead of checking them in code */
Radek Krejci70853c52018-10-15 14:46:16 +020076#define ENABLE_LOGGER_CHECKING 1
Radek Krejci80dd33e2018-09-26 15:57:18 +020077
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020078#if ENABLE_LOGGER_CHECKING
Radek Krejci80dd33e2018-09-26 15:57:18 +020079static void
80logger(LY_LOG_LEVEL level, const char *msg, const char *path)
81{
82 (void) level; /* unused */
Radek Krejci9ed7a192018-10-31 16:23:51 +010083 if (store) {
84 if (path && path[0]) {
85 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
86 } else {
87 strncpy(logbuf, msg, BUFSIZE - 1);
88 }
89 if (store > 0) {
90 --store;
91 }
Radek Krejci80dd33e2018-09-26 15:57:18 +020092 }
93}
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020094#endif
Radek Krejci80dd33e2018-09-26 15:57:18 +020095
96static int
97logger_setup(void **state)
98{
99 (void) state; /* unused */
100#if ENABLE_LOGGER_CHECKING
101 ly_set_log_clb(logger, 1);
102#endif
103 return 0;
104}
105
Radek Krejcib1a5dcc2018-11-26 14:50:05 +0100106static int
107logger_teardown(void **state)
108{
109 (void) state; /* unused */
110#if ENABLE_LOGGER_CHECKING
111 if (*state) {
112 fprintf(stderr, "%s\n", logbuf);
113 }
114#endif
115 return 0;
116}
117
Radek Krejci80dd33e2018-09-26 15:57:18 +0200118void
119logbuf_clean(void)
120{
121 logbuf[0] = '\0';
122}
123
124#if ENABLE_LOGGER_CHECKING
125# define logbuf_assert(str) assert_string_equal(logbuf, str)
126#else
127# define logbuf_assert(str)
128#endif
129
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200130#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
131 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
132 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
133 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
134 CLEANUP
135
Radek Krejci44ceedc2018-10-02 15:54:31 +0200136static void
137test_helpers(void **state)
138{
139 (void) state; /* unused */
140
141 const char *str;
Radek Krejci404251e2018-10-09 12:06:44 +0200142 char *buf, *p;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200143 size_t len, size;
Radek Krejcie7b95092019-05-15 11:03:07 +0200144 struct lys_parser_ctx ctx;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200145 ctx.ctx = NULL;
Radek Krejci99435242019-09-05 16:19:15 +0200146 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200147 ctx.line = 1;
David Sedlák40bb13b2019-07-10 14:34:18 +0200148 int prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200149
150 /* storing into buffer */
151 str = "abcd";
152 buf = NULL;
153 size = len = 0;
154 assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &str, 2, &buf, &size, &len));
155 assert_int_not_equal(0, size);
156 assert_int_equal(2, len);
157 assert_string_equal("cd", str);
158 assert_false(strncmp("ab", buf, 2));
159 free(buf);
Radek Krejci404251e2018-10-09 12:06:44 +0200160 buf = NULL;
161
162 /* invalid first characters */
163 len = 0;
164 str = "2invalid";
David Sedlák40bb13b2019-07-10 14:34:18 +0200165 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 +0200166 str = ".invalid";
David Sedlák40bb13b2019-07-10 14:34:18 +0200167 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 +0200168 str = "-invalid";
David Sedlák40bb13b2019-07-10 14:34:18 +0200169 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 +0200170 /* invalid following characters */
171 len = 3; /* number of characters read before the str content */
172 str = "!";
David Sedlák40bb13b2019-07-10 14:34:18 +0200173 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 +0200174 str = ":";
David Sedlák40bb13b2019-07-10 14:34:18 +0200175 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 +0200176 /* valid colon for prefixed identifiers */
177 len = size = 0;
178 p = NULL;
David Sedlák40bb13b2019-07-10 14:34:18 +0200179 prefix = 0;
Radek Krejci404251e2018-10-09 12:06:44 +0200180 str = "x:id";
David Sedlák40bb13b2019-07-10 14:34:18 +0200181 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 +0200182 assert_int_equal(1, len);
183 assert_null(buf);
184 assert_string_equal(":id", str);
185 assert_int_equal('x', p[len - 1]);
David Sedlák40bb13b2019-07-10 14:34:18 +0200186 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 +0200187 assert_int_equal(2, len);
188 assert_string_equal("id", str);
189 assert_int_equal(':', p[len - 1]);
190 free(buf);
David Sedlák40bb13b2019-07-10 14:34:18 +0200191 prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200192
193 /* checking identifiers */
David Sedlák4ccd7c32019-07-10 12:02:04 +0200194 assert_int_equal(LY_EVALID, lysp_check_identifierchar(&ctx, ':', 0, NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200195 logbuf_assert("Invalid identifier character ':'. Line number 1.");
David Sedlák4ccd7c32019-07-10 12:02:04 +0200196 assert_int_equal(LY_EVALID, lysp_check_identifierchar(&ctx, '#', 1, NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200197 logbuf_assert("Invalid identifier first character '#'. Line number 1.");
198
David Sedlák4ccd7c32019-07-10 12:02:04 +0200199 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar(&ctx, 'a', 1, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200200 assert_int_equal(0, prefix);
David Sedlák4ccd7c32019-07-10 12:02:04 +0200201 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar(&ctx, ':', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200202 assert_int_equal(1, prefix);
David Sedlák4ccd7c32019-07-10 12:02:04 +0200203 assert_int_equal(LY_EVALID, lysp_check_identifierchar(&ctx, ':', 0, &prefix));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200204 assert_int_equal(1, prefix);
David Sedlák4ccd7c32019-07-10 12:02:04 +0200205 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar(&ctx, 'b', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200206 assert_int_equal(2, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200207 /* second colon is invalid */
David Sedlák4ccd7c32019-07-10 12:02:04 +0200208 assert_int_equal(LY_EVALID, lysp_check_identifierchar(&ctx, ':', 0, &prefix));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200209 logbuf_assert("Invalid identifier character ':'. Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200210}
Radek Krejci80dd33e2018-09-26 15:57:18 +0200211
212static void
213test_comments(void **state)
214{
215 (void) state; /* unused */
216
Radek Krejcie7b95092019-05-15 11:03:07 +0200217 struct lys_parser_ctx ctx;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200218 const char *str, *p;
Radek Krejciefd22f62018-09-27 11:47:58 +0200219 char *word, *buf;
220 size_t len;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200221
Radek Krejci44ceedc2018-10-02 15:54:31 +0200222 ctx.ctx = NULL;
Radek Krejci99435242019-09-05 16:19:15 +0200223 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200224 ctx.line = 1;
225
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200226 str = " // this is a text of / one * line */ comment\nargument;";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200227 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200228 assert_string_equal("argument;", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200229 assert_null(buf);
230 assert_int_equal(8, len);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200231
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200232 str = "/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200233 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200234 assert_string_equal("argument", word);
235 assert_ptr_equal(buf, word);
236 assert_int_equal(8, len);
237 free(word);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200238
239 str = p = " this is one line comment on last line";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200240 assert_int_equal(LY_SUCCESS, skip_comment(&ctx, &str, 1));
Radek Krejci80dd33e2018-09-26 15:57:18 +0200241 assert_true(str[0] == '\0');
242
243 str = p = " this is a not terminated comment x";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200244 assert_int_equal(LY_EVALID, skip_comment(&ctx, &str, 2));
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200245 logbuf_assert("Unexpected end-of-input, non-terminated comment. Line number 5.");
Radek Krejci80dd33e2018-09-26 15:57:18 +0200246 assert_true(str[0] == '\0');
247}
248
Radek Krejciefd22f62018-09-27 11:47:58 +0200249static void
250test_arg(void **state)
251{
252 (void) state; /* unused */
253
Radek Krejcie7b95092019-05-15 11:03:07 +0200254 struct lys_parser_ctx ctx;
Radek Krejciefd22f62018-09-27 11:47:58 +0200255 const char *str;
256 char *word, *buf;
257 size_t len;
258
Radek Krejci44ceedc2018-10-02 15:54:31 +0200259 ctx.ctx = NULL;
Radek Krejci99435242019-09-05 16:19:15 +0200260 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200261 ctx.line = 1;
262
Radek Krejciefd22f62018-09-27 11:47:58 +0200263 /* missing argument */
264 str = ";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200265 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200266 assert_null(word);
267
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200268 str = "{";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200269 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200270 logbuf_assert("Invalid character sequence \"{\", expected an argument. Line number 1.");
271
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200272 /* invalid escape sequence */
273 str = "\"\\s\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200274 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200275 logbuf_assert("Double-quoted string unknown special character \'\\s\'. Line number 1.");
276 str = "\'\\s\'"; /* valid, since it is not an escape sequence in single quoted string */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200277 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200278 assert_int_equal(2, len);
279 assert_string_equal("\\s\'", word);
280 assert_int_equal('\0', str[0]); /* input has been eaten */
281
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200282 /* invalid character after the argument */
283 str = "hello\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200284 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200285 logbuf_assert("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
286 str = "hello}";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200287 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200288 logbuf_assert("Invalid character sequence \"}\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
289
David Sedlák40bb13b2019-07-10 14:34:18 +0200290 /* invalid identifier-ref-arg-str */
291 str = "pre:pre:value";
292 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
293
Radek Krejci4e199f52019-05-28 09:09:28 +0200294 str = "\"\";"; /* empty identifier is not allowed */
295 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_IDENTIF_ARG, NULL, &word, &buf, &len));
296 logbuf_assert("Statement argument is required. Line number 1.");
297 logbuf_clean();
298 str = "\"\";"; /* empty reference identifier is not allowed */
299 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
300 logbuf_assert("Statement argument is required. Line number 1.");
301
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200302 str = "hello/x\t"; /* slash is not an invalid character */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200303 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200304 assert_int_equal(7, len);
305 assert_string_equal("hello/x\t", word);
306
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200307 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200308
309 /* different quoting */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200310 str = "hello ";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200311 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200312 assert_null(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200313 assert_int_equal(5, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200314 assert_string_equal("hello ", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200315
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200316 str = "hello/*comment*/\n";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200317 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200318 assert_null(buf);
319 assert_int_equal(5, len);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200320 assert_false(strncmp("hello", word, len));
321
322
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200323 str = "\"hello\\n\\t\\\"\\\\\";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200324 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
fredgand49fe112019-10-21 20:51:50 +0800325 assert_non_null(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200326 assert_int_equal(9, len);
fredgand49fe112019-10-21 20:51:50 +0800327 assert_string_equal("hello\n\t\"\\", word);
328 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200329
330 ctx.indent = 14;
331 str = "\"hello \t\n\t\t world!\"";
332 /* - space and tabs before newline are stripped out
333 * - space and tabs after newline (indentation) are stripped out
334 */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200335 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200336 assert_non_null(buf);
337 assert_ptr_equal(word, buf);
338 assert_int_equal(14, len);
339 assert_string_equal("hello\n world!", word);
340 free(buf);
Radek Krejciff13cd12019-10-25 15:34:24 +0200341 /* In contrast to previous, the backslash-escaped tabs are expanded after trimming, so they are preserved */
342 ctx.indent = 14;
343 str = "\"hello \\t\n\t\\t world!\"";
344 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
345 assert_non_null(buf);
346 assert_ptr_equal(word, buf);
347 assert_int_equal(16, len);
348 assert_string_equal("hello \t\n\t world!", word);
349 free(buf);
350 /* Do not handle whitespaces after backslash-escaped newline as indentation */
351 ctx.indent = 14;
352 str = "\"hello\\n\t\t world!\"";
353 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
354 assert_non_null(buf);
355 assert_ptr_equal(word, buf);
356 assert_int_equal(15, len);
357 assert_string_equal("hello\n\t\t world!", word);
358 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200359
360 ctx.indent = 14;
361 str = "\"hello\n \tworld!\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200362 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200363 assert_non_null(buf);
364 assert_ptr_equal(word, buf);
365 assert_int_equal(12, len);
366 assert_string_equal("hello\nworld!", word);
367 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200368
369 str = "\'hello\'";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200370 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200371 assert_null(buf);
372 assert_int_equal(5, len);
373 assert_false(strncmp("hello", word, 5));
374
375 str = "\"hel\" +\t\n\"lo\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200376 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200377 assert_ptr_equal(word, buf);
378 assert_int_equal(5, len);
379 assert_string_equal("hello", word);
380 free(buf);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200381 str = "\"hel\" +\t\nlo"; /* unquoted the second part */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200382 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciff13cd12019-10-25 15:34:24 +0200383 logbuf_assert("Both string parts divided by '+' must be quoted. Line number 6.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200384
385 str = "\'he\'\t\n+ \"llo\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200386 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200387 assert_ptr_equal(word, buf);
388 assert_int_equal(5, len);
389 assert_string_equal("hello", word);
390 free(buf);
391
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200392 str = " \t\n\"he\"+\'llo\'";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200393 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200394 assert_ptr_equal(word, buf);
395 assert_int_equal(5, len);
396 assert_string_equal("hello", word);
397 free(buf);
398
Radek Krejci44ceedc2018-10-02 15:54:31 +0200399 /* missing argument */
400 str = ";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200401 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciff13cd12019-10-25 15:34:24 +0200402 logbuf_assert("Invalid character sequence \";\", expected an argument. Line number 8.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200403}
404
405static void
406test_stmts(void **state)
407{
408 (void) state; /* unused */
409
Radek Krejcie7b95092019-05-15 11:03:07 +0200410 struct lys_parser_ctx ctx;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200411 const char *str, *p;
Radek Krejcid6b76452019-09-03 17:03:03 +0200412 enum ly_stmt kw;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200413 char *word;
414 size_t len;
415
416 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
Radek Krejcie7b95092019-05-15 11:03:07 +0200752 struct lys_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
758 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
759 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +0200760 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci05b13982018-11-28 16:22:07 +0100761 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100762 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci05b13982018-11-28 16:22:07 +0100763
Radek Krejcidf6cad12018-11-28 17:10:55 +0100764 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100765 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100766 logbuf_assert("Invalid value \"1invalid\" of \"min-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100767
768 flags = value = 0;
769 str = " -1; ...";
770 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
771 logbuf_assert("Invalid value \"-1\" of \"min-elements\". Line number 1.");
772
Radek Krejcidf6cad12018-11-28 17:10:55 +0100773 /* implementation limit */
774 flags = value = 0;
775 str = " 4294967296; ...";
776 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
777 logbuf_assert("Value \"4294967296\" is out of \"min-elements\" bounds. Line number 1.");
778
Radek Krejci05b13982018-11-28 16:22:07 +0100779 flags = value = 0;
780 str = " 1; ...";
781 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
782 assert_int_equal(LYS_SET_MIN, flags);
783 assert_int_equal(1, value);
784
785 flags = value = 0;
786 str = " 1 {m:ext;} ...";
787 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
788 assert_int_equal(LYS_SET_MIN, flags);
789 assert_int_equal(1, value);
790 assert_non_null(ext);
791 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
792 ext = NULL;
793
794 flags = value = 0;
795 str = " 1 {config true;} ...";
796 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
797 logbuf_assert("Invalid keyword \"config\" as a child of \"min-elements\". Line number 1.");
798
Radek Krejcidf6cad12018-11-28 17:10:55 +0100799 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100800 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100801 logbuf_assert("Invalid value \"1invalid\" of \"max-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100802
803 flags = value = 0;
804 str = " -1; ...";
805 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
806 logbuf_assert("Invalid value \"-1\" of \"max-elements\". Line number 1.");
807
Radek Krejcidf6cad12018-11-28 17:10:55 +0100808 /* implementation limit */
809 flags = value = 0;
810 str = " 4294967296; ...";
811 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
812 logbuf_assert("Value \"4294967296\" is out of \"max-elements\" bounds. Line number 1.");
813
Radek Krejci05b13982018-11-28 16:22:07 +0100814 flags = value = 0;
815 str = " 1; ...";
816 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
817 assert_int_equal(LYS_SET_MAX, flags);
818 assert_int_equal(1, value);
819
820 flags = value = 0;
821 str = " unbounded; ...";
822 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
823 assert_int_equal(LYS_SET_MAX, flags);
824 assert_int_equal(0, value);
825
826 flags = value = 0;
827 str = " 1 {m:ext;} ...";
828 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
829 assert_int_equal(LYS_SET_MAX, flags);
830 assert_int_equal(1, value);
831 assert_non_null(ext);
832 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
833 ext = NULL;
834
835 flags = value = 0;
836 str = " 1 {config true;} ...";
837 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
838 logbuf_assert("Invalid keyword \"config\" as a child of \"max-elements\". Line number 1.");
839
840 *state = NULL;
841 ly_ctx_destroy(ctx.ctx, NULL);
842}
843
Radek Krejci9fcacc12018-10-11 15:59:11 +0200844static struct lysp_module *
Radek Krejcie7b95092019-05-15 11:03:07 +0200845mod_renew(struct lys_parser_ctx *ctx)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200846{
Radek Krejci40544fa2019-01-11 09:38:37 +0100847 struct lysp_module *mod_p;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100848 static struct lys_module mod = {0};
849
850 lysc_module_free(mod.compiled, NULL);
851 lysp_module_free(mod.parsed);
852 FREE_STRING(mod.ctx, mod.name);
853 FREE_STRING(mod.ctx, mod.ns);
854 FREE_STRING(mod.ctx, mod.prefix);
855 FREE_STRING(mod.ctx, mod.filepath);
856 FREE_STRING(mod.ctx, mod.org);
857 FREE_STRING(mod.ctx, mod.contact);
858 FREE_STRING(mod.ctx, mod.dsc);
859 FREE_STRING(mod.ctx, mod.ref);
860 memset(&mod, 0, sizeof mod);
861 mod.ctx = ctx->ctx;
862
863 mod_p = calloc(1, sizeof *mod_p);
864 mod.parsed = mod_p;
865 mod_p->mod = &mod;
866 assert_non_null(mod_p);
867 return mod_p;
868}
869
870static struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +0200871submod_renew(struct lys_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100872{
873 lysp_submodule_free(ctx->ctx, submod);
874 submod = calloc(1, sizeof *submod);
875 assert_non_null(submod);
876 return submod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200877}
878
Radek Krejcid33273d2018-10-25 14:55:52 +0200879static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
880 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
881 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
882{
883 *module_data = user_data;
884 *format = LYS_IN_YANG;
885 *free_module_data = NULL;
886 return LY_SUCCESS;
887}
888
Radek Krejci9fcacc12018-10-11 15:59:11 +0200889static void
890test_module(void **state)
891{
Radek Krejci40544fa2019-01-11 09:38:37 +0100892 *state = test_module;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200893
Radek Krejcie7b95092019-05-15 11:03:07 +0200894 struct lys_parser_ctx ctx;
Radek Krejci40544fa2019-01-11 09:38:37 +0100895 struct lysp_module *mod = NULL;
896 struct lysp_submodule *submod = NULL;
897 struct lys_module *m;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200898 const char *str;
899
900 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
901 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +0200902 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200903 ctx.line = 1;
Radek Krejcia042ea12018-10-13 07:52:15 +0200904 ctx.indent = 0;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200905
Radek Krejci40544fa2019-01-11 09:38:37 +0100906 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200907
908 /* missing mandatory substatements */
909 str = " name {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100910 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
911 assert_string_equal("name", mod->mod->name);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200912 logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100913 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200914
915 str = " name {namespace urn:x;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100916 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
917 assert_string_equal("urn:x", mod->mod->ns);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200918 logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100919 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200920
921 str = " name {namespace urn:x;prefix \"x\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100922 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
923 assert_string_equal("x", mod->mod->prefix);
Radek Krejci40544fa2019-01-11 09:38:37 +0100924 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200925
Radek Krejci027d5802018-11-14 16:57:28 +0100926#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
927#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200928#define TEST_NODE(NODETYPE, INPUT, NAME) \
929 str = SCHEMA_BEGINNING INPUT; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100930 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200931 assert_non_null(mod->data); \
932 assert_int_equal(NODETYPE, mod->data->nodetype); \
933 assert_string_equal(NAME, mod->data->name); \
Radek Krejci40544fa2019-01-11 09:38:37 +0100934 mod = mod_renew(&ctx);
Radek Krejcia042ea12018-10-13 07:52:15 +0200935#define TEST_GENERIC(INPUT, TARGET, TEST) \
936 str = SCHEMA_BEGINNING INPUT; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100937 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200938 assert_non_null(TARGET); \
939 TEST; \
Radek Krejci40544fa2019-01-11 09:38:37 +0100940 mod = mod_renew(&ctx);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100941#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200942 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
Radek Krejci40544fa2019-01-11 09:38:37 +0100943 parse_module, mod, LINE, mod = mod_renew(&ctx))
Radek Krejcia042ea12018-10-13 07:52:15 +0200944
945 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100946 TEST_DUP("namespace", "y", "z", "1");
947 TEST_DUP("prefix", "y", "z", "1");
948 TEST_DUP("contact", "a", "b", "1");
949 TEST_DUP("description", "a", "b", "1");
950 TEST_DUP("organization", "a", "b", "1");
951 TEST_DUP("reference", "a", "b", "1");
Radek Krejcia042ea12018-10-13 07:52:15 +0200952
Radek Krejci70853c52018-10-15 14:46:16 +0200953 /* not allowed in module (submodule-specific) */
954 str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100955 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200956 logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100957 mod = mod_renew(&ctx);
Radek Krejci70853c52018-10-15 14:46:16 +0200958
Radek Krejcia042ea12018-10-13 07:52:15 +0200959 /* anydata */
960 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
961 /* anyxml */
962 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
963 /* augment */
964 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200965 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200966 /* choice */
967 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
968 /* contact 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100969 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact,
970 assert_string_equal("firstname surname", mod->mod->contact));
Radek Krejcia042ea12018-10-13 07:52:15 +0200971 /* container */
972 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
973 /* description 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100974 TEST_GENERIC("description \'some description\';}", mod->mod->dsc,
975 assert_string_equal("some description", mod->mod->dsc));
Radek Krejcia042ea12018-10-13 07:52:15 +0200976 /* deviation */
977 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200978 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200979 /* extension */
980 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200981 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200982 /* feature */
983 TEST_GENERIC("feature test;}", mod->features,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200984 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200985 /* grouping */
986 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200987 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200988 /* identity */
989 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200990 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200991 /* import */
Radek Krejci086c7132018-10-26 15:29:04 +0200992 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
993 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
994 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200995
Radek Krejcia042ea12018-10-13 07:52:15 +0200996 /* import - prefix collision */
Radek Krejci086c7132018-10-26 15:29:04 +0200997 str = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100998 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200999 logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001000 mod = mod_renew(&ctx);
Radek Krejci086c7132018-10-26 15:29:04 +02001001 str = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001002 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci086c7132018-10-26 15:29:04 +02001003 logbuf_assert("Prefix \"y\" already used to import \"zzz\" module. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001004 mod = mod_renew(&ctx);
Radek Krejci086c7132018-10-26 15:29:04 +02001005 str = "module" SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix z;}}";
1006 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
1007 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
1008 logbuf_assert("Single revision of the module \"zzz\" referred twice.");
Radek Krejci70853c52018-10-15 14:46:16 +02001009
Radek Krejcia042ea12018-10-13 07:52:15 +02001010 /* include */
Radek Krejci9ed7a192018-10-31 16:23:51 +01001011 store = 1;
Radek Krejcid33273d2018-10-25 14:55:52 +02001012 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 +02001013 str = "module" SCHEMA_BEGINNING "include xxx;}";
1014 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
1015 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001016 logbuf_assert("Input data contains module in situation when a submodule is expected.");
Radek Krejci9ed7a192018-10-31 16:23:51 +01001017 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +02001018
Radek Krejci9ed7a192018-10-31 16:23:51 +01001019 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +01001020 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 +02001021 str = "module" SCHEMA_BEGINNING "include xxx;}";
1022 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
1023 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
1024 logbuf_assert("Included \"xxx\" submodule from \"name\" belongs-to a different module \"wrong-name\".");
Radek Krejci9ed7a192018-10-31 16:23:51 +01001025 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +02001026
Radek Krejci313d9902018-11-08 09:42:58 +01001027 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 +02001028 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejci086c7132018-10-26 15:29:04 +02001029 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +02001030
Radek Krejcia042ea12018-10-13 07:52:15 +02001031 /* leaf */
1032 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
1033 /* leaf-list */
1034 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
1035 /* list */
1036 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
1037 /* notification */
1038 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001039 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001040 /* organization 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001041 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org,
1042 assert_string_equal("CESNET a.l.e.", mod->mod->org));
Radek Krejcia042ea12018-10-13 07:52:15 +02001043 /* reference 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001044 TEST_GENERIC("reference RFC7950;}", mod->mod->ref,
1045 assert_string_equal("RFC7950", mod->mod->ref));
Radek Krejcia042ea12018-10-13 07:52:15 +02001046 /* revision */
1047 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib7db73a2018-10-24 14:18:40 +02001048 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +02001049 /* rpc */
1050 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001051 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001052 /* typedef */
1053 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001054 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001055 /* uses */
1056 TEST_NODE(LYS_USES, "uses test;}", "test");
1057 /* yang-version */
Radek Krejci027d5802018-11-14 16:57:28 +01001058 str = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001059 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +02001060 logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001061 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001062 str = SCHEMA_BEGINNING2 "yang-version 1.0;yang-version 1.1;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001063 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +02001064 logbuf_assert("Duplicate keyword \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001065 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001066 str = SCHEMA_BEGINNING2 "yang-version 1.0;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001067 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
1068 assert_int_equal(1, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +01001069 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001070 str = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001071 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
1072 assert_int_equal(2, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +01001073 mod = mod_renew(&ctx);
1074
David Sedlák1b623122019-08-05 15:27:49 +02001075 struct lys_parser_ctx *ctx_p = NULL;
Radek Krejci40544fa2019-01-11 09:38:37 +01001076 str = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
1077 m = mod->mod;
1078 free(mod);
1079 m->parsed = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02001080 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, str, m));
1081 logbuf_assert("Trailing garbage \"module q {names...\" after module, expected end-of-input. Line number 1.");
1082 lys_parser_ctx_free(ctx_p);
Radek Krejci40544fa2019-01-11 09:38:37 +01001083 mod = mod_renew(&ctx);
1084
1085 str = "prefix " SCHEMA_BEGINNING "}";
1086 m = mod->mod;
1087 free(mod);
1088 m->parsed = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02001089 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, str, m));
1090 lys_parser_ctx_free(ctx_p);
1091 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001092 mod = mod_renew(&ctx);
Radek Krejci09306362018-10-15 15:26:01 +02001093
David Sedlák9fb515f2019-07-11 10:33:58 +02001094 str = "module " SCHEMA_BEGINNING "}";
1095 str = "module " SCHEMA_BEGINNING "leaf enum {type enumeration {enum seven { position 7;}}}}";
1096 m = mod->mod;
1097 free(mod);
1098 m->parsed = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02001099 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, str, m));
1100 lys_parser_ctx_free(ctx_p);
1101 logbuf_assert("Invalid keyword \"position\" as a child of \"enum\". Line number 1.");
David Sedlák9fb515f2019-07-11 10:33:58 +02001102 mod = mod_renew(&ctx);
1103
Radek Krejci156ccaf2018-10-15 15:49:17 +02001104 /* extensions */
1105 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001106 assert_string_equal("prefix:test", mod->exts[0].name);
1107 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
Radek Krejci40544fa2019-01-11 09:38:37 +01001108 mod = mod_renew(&ctx);
Radek Krejci156ccaf2018-10-15 15:49:17 +02001109
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001110 /* invalid substatement */
1111 str = SCHEMA_BEGINNING "must false;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001112 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001113 logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001114 mod = mod_renew(&ctx);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001115
Radek Krejci09306362018-10-15 15:26:01 +02001116 /* submodule */
Radek Krejci40544fa2019-01-11 09:38:37 +01001117 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001118
1119 /* missing mandatory substatements */
1120 str = " subname {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001121 lydict_remove(ctx.ctx, submod->name);
1122 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
1123 assert_string_equal("subname", submod->name);
Radek Krejci09306362018-10-15 15:26:01 +02001124 logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001125 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001126
Radek Krejci313d9902018-11-08 09:42:58 +01001127 str = " subname {belongs-to name {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001128 lydict_remove(ctx.ctx, submod->name);
1129 assert_int_equal(LY_SUCCESS, parse_submodule(&ctx, &str, submod));
1130 assert_string_equal("name", submod->belongsto);
1131 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001132
1133#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +01001134#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +02001135
1136 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001137 str = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ...";
1138 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod)); \
1139 logbuf_assert("Duplicate keyword \"belongs-to\". Line number 3."); \
1140 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001141
1142 /* not allowed in submodule (module-specific) */
1143 str = SCHEMA_BEGINNING "namespace \"urn:z\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001144 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001145 logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001146 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001147 str = SCHEMA_BEGINNING "prefix m;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001148 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001149 logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001150 submod = submod_renew(&ctx, submod);
Radek Krejcia042ea12018-10-13 07:52:15 +02001151
Radek Krejci40544fa2019-01-11 09:38:37 +01001152 str = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
1153 lysp_submodule_free(ctx.ctx, submod);
1154 submod = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02001155 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, ctx.ctx, &ctx, str, &submod));
1156 lys_parser_ctx_free(ctx_p);
1157 logbuf_assert("Trailing garbage \"module q {names...\" after submodule, expected end-of-input. Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001158
1159 str = "prefix " SCHEMA_BEGINNING "}";
David Sedlák1b623122019-08-05 15:27:49 +02001160 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, ctx.ctx, &ctx, str, &submod));
1161 lys_parser_ctx_free(ctx_p);
1162 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001163 submod = submod_renew(&ctx, submod);
1164
Radek Krejcia042ea12018-10-13 07:52:15 +02001165#undef TEST_GENERIC
1166#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001167#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +02001168#undef SCHEMA_BEGINNING
1169
Radek Krejci9fcacc12018-10-11 15:59:11 +02001170 lysp_module_free(mod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001171 lysp_submodule_free(ctx.ctx, submod);
Radek Krejci9fcacc12018-10-11 15:59:11 +02001172 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci40544fa2019-01-11 09:38:37 +01001173
1174 *state = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02001175}
1176
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001177static void
1178test_identity(void **state)
1179{
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001180 *state = test_identity;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001181
Radek Krejcie7b95092019-05-15 11:03:07 +02001182 struct lys_parser_ctx ctx;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001183 struct lysp_ident *ident = NULL;
1184 const char *str;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001185
1186 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1187 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001188 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001189 ctx.line = 1;
1190 ctx.indent = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001191 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001192
1193 /* invalid cardinality */
1194#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001195 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001196 &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL)
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001197
Radek Krejci38222632019-02-12 16:55:05 +01001198 TEST_DUP("description", "a", "b");
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001199 TEST_DUP("reference", "a", "b");
1200 TEST_DUP("status", "current", "obsolete");
1201
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001202 /* full content */
David Sedlák40bb13b2019-07-10 14:34:18 +02001203 str = " test {base \"a\";base b; description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ...";
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001204 assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident));
1205 assert_non_null(ident);
1206 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001207 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001208 ident = NULL;
1209
1210 /* invalid substatement */
1211 str = " test {organization XXX;}";
1212 assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident));
1213 logbuf_assert("Invalid keyword \"organization\" as a child of \"identity\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001214 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001215 ident = NULL;
1216
1217#undef TEST_DUP
1218
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001219 *state = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001220 ly_ctx_destroy(ctx.ctx, NULL);
1221}
1222
1223static void
1224test_feature(void **state)
1225{
1226 (void) state; /* unused */
1227
Radek Krejcie7b95092019-05-15 11:03:07 +02001228 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001229 struct lysp_feature *features = NULL;
1230 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001231
1232 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1233 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001234 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001235 ctx.line = 1;
1236 ctx.indent = 0;
1237
1238 /* invalid cardinality */
1239#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1240 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001241 &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001242
1243 TEST_DUP("description", "a", "b");
1244 TEST_DUP("reference", "a", "b");
1245 TEST_DUP("status", "current", "obsolete");
1246
1247 /* full content */
1248 str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ...";
1249 assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features));
1250 assert_non_null(features);
1251 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001252 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001253 features = NULL;
1254
1255 /* invalid substatement */
1256 str = " test {organization XXX;}";
1257 assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features));
1258 logbuf_assert("Invalid keyword \"organization\" as a child of \"feature\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001259 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001260 features = NULL;
1261
1262#undef TEST_DUP
1263
1264 ly_ctx_destroy(ctx.ctx, NULL);
1265}
1266
1267static void
1268test_deviation(void **state)
1269{
1270 (void) state; /* unused */
1271
Radek Krejcie7b95092019-05-15 11:03:07 +02001272 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001273 struct lysp_deviation *d = NULL;
1274 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001275
1276 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1277 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001278 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001279 ctx.line = 1;
1280 ctx.indent = 0;
1281
1282 /* invalid cardinality */
1283#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1284 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001285 &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001286
1287 TEST_DUP("description", "a", "b");
1288 TEST_DUP("reference", "a", "b");
1289
1290 /* full content */
1291 str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
1292 assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d));
1293 assert_non_null(d);
1294 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001295 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001296 d = NULL;
1297
1298 /* missing mandatory substatement */
1299 str = " test {description text;}";
1300 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1301 logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001302 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001303 d = NULL;
1304
1305 /* invalid substatement */
1306 str = " test {deviate not-supported; status obsolete;}";
1307 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1308 logbuf_assert("Invalid keyword \"status\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001309 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001310 d = NULL;
1311
1312#undef TEST_DUP
1313
1314 ly_ctx_destroy(ctx.ctx, NULL);
1315}
1316
1317static void
1318test_deviate(void **state)
1319{
1320 (void) state; /* unused */
1321
Radek Krejcie7b95092019-05-15 11:03:07 +02001322 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001323 struct lysp_deviate *d = NULL;
1324 const char *str;
1325
1326 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1327 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001328 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001329 ctx.line = 1;
1330 ctx.indent = 0;
1331
1332 /* invalid cardinality */
1333#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
1334 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001335 &d, "1", lysp_deviate_free(ctx.ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001336
1337 TEST_DUP("add", "config", "true", "false");
1338 TEST_DUP("replace", "default", "int8", "uint8");
1339 TEST_DUP("add", "mandatory", "true", "false");
1340 TEST_DUP("add", "max-elements", "1", "2");
1341 TEST_DUP("add", "min-elements", "1", "2");
1342 TEST_DUP("replace", "type", "int8", "uint8");
1343 TEST_DUP("add", "units", "kilometers", "miles");
1344
1345 /* full contents */
1346 str = " not-supported {prefix:ext;} ...";
1347 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1348 assert_non_null(d);
1349 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001350 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001351 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;} ...";
1352 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1353 assert_non_null(d);
1354 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001355 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001356 str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...";
1357 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1358 assert_non_null(d);
1359 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001360 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001361 str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...";
1362 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1363 assert_non_null(d);
1364 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001365 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001366
1367 /* invalid substatements */
1368#define TEST_NOT_SUP(DEV, STMT, VALUE) \
1369 str = " "DEV" {"STMT" "VALUE";}..."; \
1370 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \
1371 logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001372 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001373
1374 TEST_NOT_SUP("not-supported", "units", "meters");
1375 TEST_NOT_SUP("not-supported", "must", "1");
1376 TEST_NOT_SUP("not-supported", "unique", "x");
1377 TEST_NOT_SUP("not-supported", "default", "a");
1378 TEST_NOT_SUP("not-supported", "config", "true");
1379 TEST_NOT_SUP("not-supported", "mandatory", "true");
1380 TEST_NOT_SUP("not-supported", "min-elements", "1");
1381 TEST_NOT_SUP("not-supported", "max-elements", "2");
1382 TEST_NOT_SUP("not-supported", "type", "string");
1383 TEST_NOT_SUP("add", "type", "string");
1384 TEST_NOT_SUP("delete", "config", "true");
1385 TEST_NOT_SUP("delete", "mandatory", "true");
1386 TEST_NOT_SUP("delete", "min-elements", "1");
1387 TEST_NOT_SUP("delete", "max-elements", "2");
1388 TEST_NOT_SUP("delete", "type", "string");
1389 TEST_NOT_SUP("replace", "must", "1");
1390 TEST_NOT_SUP("replace", "unique", "a");
1391
1392 str = " nonsence; ...";
1393 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d));
1394 logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1.");
1395 assert_null(d);
1396
1397#undef TEST_NOT_SUP
1398#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001399
1400 ly_ctx_destroy(ctx.ctx, NULL);
1401}
1402
Radek Krejci8c370832018-11-02 15:10:03 +01001403static void
1404test_container(void **state)
1405{
1406 (void) state; /* unused */
1407
Radek Krejcie7b95092019-05-15 11:03:07 +02001408 struct lys_parser_ctx ctx = {0};
Radek Krejci8c370832018-11-02 15:10:03 +01001409 struct lysp_node_container *c = NULL;
1410 const char *str;
1411
1412 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1413 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001414 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci8c370832018-11-02 15:10:03 +01001415 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001416 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +01001417
1418 /* invalid cardinality */
1419#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1420 str = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1421 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); \
1422 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001423 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001424
1425 TEST_DUP("config", "true", "false");
1426 TEST_DUP("description", "text1", "text2");
1427 TEST_DUP("presence", "true", "false");
1428 TEST_DUP("reference", "1", "2");
1429 TEST_DUP("status", "current", "obsolete");
1430 TEST_DUP("when", "true", "false");
1431#undef TEST_DUP
1432
1433 /* full content */
Radek Krejci313d9902018-11-08 09:42:58 +01001434 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;}"
1435 "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 +01001436 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1437 assert_non_null(c);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001438 assert_int_equal(LYS_CONTAINER, c->nodetype);
1439 assert_string_equal("cont", c->name);
Radek Krejci8c370832018-11-02 15:10:03 +01001440 assert_non_null(c->actions);
1441 assert_non_null(c->child);
1442 assert_string_equal("test", c->dsc);
1443 assert_non_null(c->exts);
1444 assert_non_null(c->groupings);
1445 assert_non_null(c->iffeatures);
1446 assert_non_null(c->musts);
1447 assert_non_null(c->notifs);
1448 assert_string_equal("true", c->presence);
1449 assert_string_equal("test", c->ref);
1450 assert_non_null(c->typedefs);
1451 assert_non_null(c->when);
1452 assert_null(c->parent);
1453 assert_null(c->next);
1454 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags);
Radek Krejci313d9902018-11-08 09:42:58 +01001455 ly_set_erase(&ctx.tpdfs_nodes, NULL);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001456 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001457
1458 /* invalid */
1459 str = " cont {augment /root;} ...";
1460 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1461 logbuf_assert("Invalid keyword \"augment\" as a child of \"container\". Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001462 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001463 str = " cont {nonsence true;} ...";
1464 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1465 logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001466 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001467
Radek Krejcif538ce52019-03-05 10:46:14 +01001468 ctx.mod_version = 1; /* simulate YANG 1.0 */
1469 str = " cont {action x;} ...";
1470 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1471 logbuf_assert("Invalid keyword \"action\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1472 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
1473
Radek Krejci8c370832018-11-02 15:10:03 +01001474 ly_ctx_destroy(ctx.ctx, NULL);
1475}
1476
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001477static void
1478test_leaf(void **state)
1479{
1480 *state = test_leaf;
1481
Radek Krejcie7b95092019-05-15 11:03:07 +02001482 struct lys_parser_ctx ctx = {0};
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001483 struct lysp_node_leaf *l = NULL;
1484 const char *str;
1485
1486 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1487 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001488 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001489 ctx.line = 1;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001490 //ctx.mod->version = 2; /* simulate YANG 1.1 */
1491
1492 /* invalid cardinality */
1493#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1494 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1495 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1496 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1497 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1498
1499 TEST_DUP("config", "true", "false");
1500 TEST_DUP("default", "x", "y");
1501 TEST_DUP("description", "text1", "text2");
1502 TEST_DUP("mandatory", "true", "false");
1503 TEST_DUP("reference", "1", "2");
1504 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001505 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001506 TEST_DUP("units", "text1", "text2");
1507 TEST_DUP("when", "true", "false");
1508#undef TEST_DUP
1509
1510 /* full content - without mandatory which is mutual exclusive with default */
1511 str = "l {config false;default \"xxx\";description test;if-feature f;"
1512 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
1513 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1514 assert_non_null(l);
1515 assert_int_equal(LYS_LEAF, l->nodetype);
1516 assert_string_equal("l", l->name);
1517 assert_string_equal("test", l->dsc);
1518 assert_string_equal("xxx", l->dflt);
1519 assert_string_equal("yyy", l->units);
1520 assert_string_equal("string", l->type.name);
1521 assert_non_null(l->exts);
1522 assert_non_null(l->iffeatures);
1523 assert_non_null(l->musts);
1524 assert_string_equal("test", l->ref);
1525 assert_non_null(l->when);
1526 assert_null(l->parent);
1527 assert_null(l->next);
1528 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, l->flags);
1529 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1530
1531 /* full content - now with mandatory */
1532 str = "l {mandatory true; type string;} ...";
1533 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1534 assert_non_null(l);
1535 assert_int_equal(LYS_LEAF, l->nodetype);
1536 assert_string_equal("l", l->name);
1537 assert_string_equal("string", l->type.name);
1538 assert_int_equal(LYS_MAND_TRUE, l->flags);
1539 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1540
1541 /* invalid */
1542 str = " l {mandatory true; default xx; type string;} ...";
1543 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001544 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"leaf\". Line number 1.");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001545 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1546
1547 str = " l {description \"missing type\";} ...";
1548 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1549 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf\". Line number 1.");
1550 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1551
1552 *state = NULL;
1553 ly_ctx_destroy(ctx.ctx, NULL);
1554}
1555
Radek Krejci0e5d8382018-11-28 16:37:53 +01001556static void
1557test_leaflist(void **state)
1558{
1559 *state = test_leaf;
1560
Radek Krejcie7b95092019-05-15 11:03:07 +02001561 struct lys_parser_ctx ctx = {0};
Radek Krejci0e5d8382018-11-28 16:37:53 +01001562 struct lysp_node_leaflist *ll = NULL;
1563 const char *str;
1564
1565 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1566 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001567 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001568 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001569 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001570
1571 /* invalid cardinality */
1572#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1573 str = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1574 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); \
1575 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1576 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1577
1578 TEST_DUP("config", "true", "false");
1579 TEST_DUP("description", "text1", "text2");
1580 TEST_DUP("max-elements", "10", "20");
1581 TEST_DUP("min-elements", "10", "20");
1582 TEST_DUP("ordered-by", "user", "system");
1583 TEST_DUP("reference", "1", "2");
1584 TEST_DUP("status", "current", "obsolete");
1585 TEST_DUP("type", "int8", "uint8");
1586 TEST_DUP("units", "text1", "text2");
1587 TEST_DUP("when", "true", "false");
1588#undef TEST_DUP
1589
1590 /* full content - without min-elements which is mutual exclusive with default */
1591 str = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
1592 "max-elements 10;must 'expr';ordered-by user;reference test;"
1593 "status current;type string; units zzz;when true;m:ext;} ...";
1594 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1595 assert_non_null(ll);
1596 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1597 assert_string_equal("ll", ll->name);
1598 assert_string_equal("test", ll->dsc);
1599 assert_non_null(ll->dflts);
1600 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
1601 assert_string_equal("xxx", ll->dflts[0]);
1602 assert_string_equal("yyy", ll->dflts[1]);
1603 assert_string_equal("zzz", ll->units);
1604 assert_int_equal(10, ll->max);
1605 assert_int_equal(0, ll->min);
1606 assert_string_equal("string", ll->type.name);
1607 assert_non_null(ll->exts);
1608 assert_non_null(ll->iffeatures);
1609 assert_non_null(ll->musts);
1610 assert_string_equal("test", ll->ref);
1611 assert_non_null(ll->when);
1612 assert_null(ll->parent);
1613 assert_null(ll->next);
1614 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
1615 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1616
1617 /* full content - now with min-elements */
1618 str = "ll {min-elements 10; type string;} ...";
1619 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1620 assert_non_null(ll);
1621 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1622 assert_string_equal("ll", ll->name);
1623 assert_string_equal("string", ll->type.name);
1624 assert_int_equal(0, ll->max);
1625 assert_int_equal(10, ll->min);
1626 assert_int_equal(LYS_SET_MIN, ll->flags);
1627 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1628
1629 /* invalid */
1630 str = " ll {min-elements 1; default xx; type string;} ...";
1631 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001632 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 +01001633 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1634
1635 str = " ll {description \"missing type\";} ...";
1636 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1637 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf-list\". Line number 1.");
1638 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1639
Radek Krejcidf6cad12018-11-28 17:10:55 +01001640 str = " ll {type string; min-elements 10; max-elements 1;} ..."; /* invalid combination of min/max */
1641 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1642 logbuf_assert("Invalid combination of min-elements and max-elements: min value 10 is bigger than the max value 1. Line number 1.");
1643 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1644
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001645 ctx.mod_version = 1; /* simulate YANG 1.0 - default statement is not allowed */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001646 str = " ll {default xx; type string;} ...";
1647 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1648 logbuf_assert("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1649 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1650
1651 *state = NULL;
1652 ly_ctx_destroy(ctx.ctx, NULL);
1653}
1654
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001655static void
1656test_list(void **state)
1657{
1658 *state = test_list;
1659
Radek Krejcie7b95092019-05-15 11:03:07 +02001660 struct lys_parser_ctx ctx = {0};
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001661 struct lysp_node_list *l = NULL;
1662 const char *str;
1663
1664 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1665 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001666 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001667 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001668 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001669
1670 /* invalid cardinality */
1671#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1672 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1673 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1674 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1675 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1676
1677 TEST_DUP("config", "true", "false");
1678 TEST_DUP("description", "text1", "text2");
1679 TEST_DUP("key", "one", "two");
1680 TEST_DUP("max-elements", "10", "20");
1681 TEST_DUP("min-elements", "10", "20");
1682 TEST_DUP("ordered-by", "user", "system");
1683 TEST_DUP("reference", "1", "2");
1684 TEST_DUP("status", "current", "obsolete");
1685 TEST_DUP("when", "true", "false");
1686#undef TEST_DUP
1687
1688 /* full content */
1689 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;}"
1690 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1691 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
1692 assert_int_equal(LY_SUCCESS, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1693 assert_non_null(l);
1694 assert_int_equal(LYS_LIST, l->nodetype);
1695 assert_string_equal("l", l->name);
1696 assert_string_equal("test", l->dsc);
1697 assert_string_equal("l", l->key);
1698 assert_non_null(l->uniques);
1699 assert_int_equal(2, LY_ARRAY_SIZE(l->uniques));
1700 assert_string_equal("xxx", l->uniques[0]);
1701 assert_string_equal("yyy", l->uniques[1]);
1702 assert_int_equal(10, l->max);
1703 assert_int_equal(1, l->min);
1704 assert_non_null(l->exts);
1705 assert_non_null(l->iffeatures);
1706 assert_non_null(l->musts);
1707 assert_string_equal("test", l->ref);
1708 assert_non_null(l->when);
1709 assert_null(l->parent);
1710 assert_null(l->next);
1711 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, l->flags);
1712 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1713 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1714
Radek Krejcif538ce52019-03-05 10:46:14 +01001715 /* invalid content */
1716 ctx.mod_version = 1; /* simulate YANG 1.0 */
1717 str = "l {action x;} ...";
1718 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1719 logbuf_assert("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1720 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1721
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001722 *state = NULL;
1723 ly_ctx_destroy(ctx.ctx, NULL);
1724}
1725
Radek Krejci056d0a82018-12-06 16:57:25 +01001726static void
1727test_choice(void **state)
1728{
1729 *state = test_choice;
1730
Radek Krejcie7b95092019-05-15 11:03:07 +02001731 struct lys_parser_ctx ctx = {0};
Radek Krejci056d0a82018-12-06 16:57:25 +01001732 struct lysp_node_choice *ch = NULL;
1733 const char *str;
1734
1735 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1736 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001737 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci056d0a82018-12-06 16:57:25 +01001738 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001739 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci056d0a82018-12-06 16:57:25 +01001740
1741 /* invalid cardinality */
1742#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1743 str = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1744 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); \
1745 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1746 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1747
1748 TEST_DUP("config", "true", "false");
1749 TEST_DUP("default", "a", "b");
1750 TEST_DUP("description", "text1", "text2");
1751 TEST_DUP("mandatory", "true", "false");
1752 TEST_DUP("reference", "1", "2");
1753 TEST_DUP("status", "current", "obsolete");
1754 TEST_DUP("when", "true", "false");
1755#undef TEST_DUP
1756
Radek Krejcia9026eb2018-12-12 16:04:47 +01001757 /* full content - without default due to a collision with mandatory */
1758 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 +01001759 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
1760 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1761 assert_non_null(ch);
1762 assert_int_equal(LYS_CHOICE, ch->nodetype);
1763 assert_string_equal("ch", ch->name);
1764 assert_string_equal("test", ch->dsc);
1765 assert_non_null(ch->exts);
1766 assert_non_null(ch->iffeatures);
1767 assert_string_equal("test", ch->ref);
1768 assert_non_null(ch->when);
1769 assert_null(ch->parent);
1770 assert_null(ch->next);
1771 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, ch->flags);
1772 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1773
Radek Krejcia9026eb2018-12-12 16:04:47 +01001774 /* full content - the default missing from the previous node */
1775 str = "ch {default c;case c;} ...";
1776 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1777 assert_non_null(ch);
1778 assert_int_equal(LYS_CHOICE, ch->nodetype);
1779 assert_string_equal("ch", ch->name);
1780 assert_string_equal("c", ch->dflt);
1781 assert_int_equal(0, ch->flags);
1782 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1783
1784 /* invalid content */
1785 str = "ch {mandatory true; default c1; case c1 {leaf x{type string;}}} ...";
1786 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1787 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"choice\". Line number 1.");
1788 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1789
1790 *state = NULL;
1791 ly_ctx_destroy(ctx.ctx, NULL);
1792}
1793
1794static void
1795test_case(void **state)
1796{
1797 *state = test_case;
1798
Radek Krejcie7b95092019-05-15 11:03:07 +02001799 struct lys_parser_ctx ctx = {0};
Radek Krejcia9026eb2018-12-12 16:04:47 +01001800 struct lysp_node_case *cs = NULL;
1801 const char *str;
1802
1803 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1804 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001805 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001806 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001807 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcia9026eb2018-12-12 16:04:47 +01001808
1809 /* invalid cardinality */
1810#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1811 str = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1812 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs)); \
1813 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1814 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1815
1816 TEST_DUP("description", "text1", "text2");
1817 TEST_DUP("reference", "1", "2");
1818 TEST_DUP("status", "current", "obsolete");
1819 TEST_DUP("when", "true", "false");
1820#undef TEST_DUP
1821
1822 /* full content */
1823 str = "cs {anydata any;anyxml anyxml; choice ch;container c;description test;if-feature f;leaf l {type string;}"
1824 "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
1825 assert_int_equal(LY_SUCCESS, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1826 assert_non_null(cs);
1827 assert_int_equal(LYS_CASE, cs->nodetype);
1828 assert_string_equal("cs", cs->name);
1829 assert_string_equal("test", cs->dsc);
1830 assert_non_null(cs->exts);
1831 assert_non_null(cs->iffeatures);
1832 assert_string_equal("test", cs->ref);
1833 assert_non_null(cs->when);
1834 assert_null(cs->parent);
1835 assert_null(cs->next);
1836 assert_int_equal(LYS_STATUS_CURR, cs->flags);
1837 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1838
1839 /* invalid content */
1840 str = "cs {config true} ...";
1841 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1842 logbuf_assert("Invalid keyword \"config\" as a child of \"case\". Line number 1.");
1843 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1844
Radek Krejci056d0a82018-12-06 16:57:25 +01001845 *state = NULL;
1846 ly_ctx_destroy(ctx.ctx, NULL);
1847}
1848
Radek Krejci9800fb82018-12-13 14:26:23 +01001849static void
Radek Krejcid6b76452019-09-03 17:03:03 +02001850test_any(void **state, enum ly_stmt kw)
Radek Krejci9800fb82018-12-13 14:26:23 +01001851{
1852 *state = test_any;
1853
Radek Krejcie7b95092019-05-15 11:03:07 +02001854 struct lys_parser_ctx ctx = {0};
Radek Krejci9800fb82018-12-13 14:26:23 +01001855 struct lysp_node_anydata *any = NULL;
1856 const char *str;
1857
1858 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1859 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001860 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci9800fb82018-12-13 14:26:23 +01001861 ctx.line = 1;
Radek Krejcid6b76452019-09-03 17:03:03 +02001862 if (kw == LY_STMT_ANYDATA) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001863 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001864 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001865 ctx.mod_version = 1; /* simulate YANG 1.0 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001866 }
1867
1868 /* invalid cardinality */
1869#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1870 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1871 assert_int_equal(LY_EVALID, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any)); \
1872 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1873 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1874
1875 TEST_DUP("config", "true", "false");
1876 TEST_DUP("description", "text1", "text2");
1877 TEST_DUP("mandatory", "true", "false");
1878 TEST_DUP("reference", "1", "2");
1879 TEST_DUP("status", "current", "obsolete");
1880 TEST_DUP("when", "true", "false");
1881#undef TEST_DUP
1882
1883 /* full content */
1884 str = "any {config true;description test;if-feature f;mandatory true;must 'expr';reference test;status current;when true;m:ext;} ...";
1885 assert_int_equal(LY_SUCCESS, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any));
1886 assert_non_null(any);
Radek Krejcid6b76452019-09-03 17:03:03 +02001887 assert_int_equal(kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML, any->nodetype);
Radek Krejci9800fb82018-12-13 14:26:23 +01001888 assert_string_equal("any", any->name);
1889 assert_string_equal("test", any->dsc);
1890 assert_non_null(any->exts);
1891 assert_non_null(any->iffeatures);
1892 assert_non_null(any->musts);
1893 assert_string_equal("test", any->ref);
1894 assert_non_null(any->when);
1895 assert_null(any->parent);
1896 assert_null(any->next);
1897 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags);
1898 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1899
1900 *state = NULL;
1901 ly_ctx_destroy(ctx.ctx, NULL);
1902}
1903
1904static void
1905test_anydata(void **state)
1906{
Radek Krejcid6b76452019-09-03 17:03:03 +02001907 return test_any(state, LY_STMT_ANYDATA);
Radek Krejci9800fb82018-12-13 14:26:23 +01001908}
1909
1910static void
1911test_anyxml(void **state)
1912{
Radek Krejcid6b76452019-09-03 17:03:03 +02001913 return test_any(state, LY_STMT_ANYXML);
Radek Krejci9800fb82018-12-13 14:26:23 +01001914}
1915
Radek Krejcie86bf772018-12-14 11:39:53 +01001916static void
1917test_grouping(void **state)
1918{
1919 *state = test_grouping;
1920
Radek Krejcie7b95092019-05-15 11:03:07 +02001921 struct lys_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01001922 struct lysp_grp *grp = NULL;
1923 const char *str;
1924
1925 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 Krejcie86bf772018-12-14 11:39:53 +01001928 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001929 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001930
1931 /* invalid cardinality */
1932#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1933 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1934 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp)); \
1935 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1936 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1937
1938 TEST_DUP("description", "text1", "text2");
1939 TEST_DUP("reference", "1", "2");
1940 TEST_DUP("status", "current", "obsolete");
1941#undef TEST_DUP
1942
1943 /* full content */
1944 str = "grp {action x;anydata any;anyxml anyxml; choice ch;container c;description test;grouping g;leaf l {type string;}"
1945 "leaf-list ll {type string;} list li;notification not;reference test;status current;typedef t {type int8;}uses g;m:ext;} ...";
1946 assert_int_equal(LY_SUCCESS, parse_grouping(&ctx, &str, NULL, &grp));
1947 assert_non_null(grp);
1948 assert_int_equal(LYS_GROUPING, grp->nodetype);
1949 assert_string_equal("grp", grp->name);
1950 assert_string_equal("test", grp->dsc);
1951 assert_non_null(grp->exts);
1952 assert_string_equal("test", grp->ref);
1953 assert_null(grp->parent);
1954 assert_int_equal( LYS_STATUS_CURR, grp->flags);
1955 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1956 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1957
1958 /* invalid content */
1959 str = "grp {config true} ...";
1960 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1961 logbuf_assert("Invalid keyword \"config\" as a child of \"grouping\". Line number 1.");
1962 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1963
1964 str = "grp {must 'expr'} ...";
1965 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1966 logbuf_assert("Invalid keyword \"must\" as a child of \"grouping\". Line number 1.");
1967 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1968
1969 *state = NULL;
1970 ly_ctx_destroy(ctx.ctx, NULL);
1971}
1972
1973static void
Radek Krejcif538ce52019-03-05 10:46:14 +01001974test_action(void **state)
1975{
1976 *state = test_action;
1977
Radek Krejcie7b95092019-05-15 11:03:07 +02001978 struct lys_parser_ctx ctx = {0};
Radek Krejcif538ce52019-03-05 10:46:14 +01001979 struct lysp_action *rpcs = NULL;
1980 struct lysp_node_container *c = NULL;
1981 const char *str;
1982
1983 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1984 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001985 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcif538ce52019-03-05 10:46:14 +01001986 ctx.line = 1;
1987 ctx.mod_version = 2; /* simulate YANG 1.1 */
1988
1989 /* invalid cardinality */
1990#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1991 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1992 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs)); \
1993 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1994 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1995
1996 TEST_DUP("description", "text1", "text2");
1997 TEST_DUP("input", "", "");
1998 TEST_DUP("output", "", "");
1999 TEST_DUP("reference", "1", "2");
2000 TEST_DUP("status", "current", "obsolete");
2001#undef TEST_DUP
2002
2003 /* full content */
2004 str = "top;";
2005 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
2006 str = "func {description test;grouping grp;if-feature f;reference test;status current;typedef mytype {type int8;} m:ext;"
2007 "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
2008 " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}"
2009 "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
2010 " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ...";
2011 assert_int_equal(LY_SUCCESS, parse_action(&ctx, &str, (struct lysp_node*)c, &rpcs));
2012 assert_non_null(rpcs);
2013 assert_int_equal(LYS_ACTION, rpcs->nodetype);
2014 assert_string_equal("func", rpcs->name);
2015 assert_string_equal("test", rpcs->dsc);
2016 assert_non_null(rpcs->exts);
2017 assert_non_null(rpcs->iffeatures);
2018 assert_string_equal("test", rpcs->ref);
2019 assert_non_null(rpcs->groupings);
2020 assert_non_null(rpcs->typedefs);
2021 assert_int_equal(LYS_STATUS_CURR, rpcs->flags);
2022 /* input */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002023 assert_int_equal(rpcs->input.nodetype, LYS_INPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01002024 assert_non_null(rpcs->input.groupings);
2025 assert_non_null(rpcs->input.exts);
2026 assert_non_null(rpcs->input.musts);
2027 assert_non_null(rpcs->input.typedefs);
2028 assert_non_null(rpcs->input.data);
2029 /* output */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002030 assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01002031 assert_non_null(rpcs->output.groupings);
2032 assert_non_null(rpcs->output.exts);
2033 assert_non_null(rpcs->output.musts);
2034 assert_non_null(rpcs->output.typedefs);
2035 assert_non_null(rpcs->output.data);
2036
2037 ly_set_erase(&ctx.tpdfs_nodes, NULL);
2038 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
2039
2040 /* invalid content */
2041 str = "func {config true} ...";
2042 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs));
2043 logbuf_assert("Invalid keyword \"config\" as a child of \"rpc\". Line number 1.");
2044 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
2045
2046 *state = NULL;
2047 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
2048 ly_ctx_destroy(ctx.ctx, NULL);
2049}
2050
2051static void
Radek Krejcifc11bd72019-04-11 16:00:05 +02002052test_notification(void **state)
2053{
2054 *state = test_notification;
2055
Radek Krejcie7b95092019-05-15 11:03:07 +02002056 struct lys_parser_ctx ctx = {0};
Radek Krejcifc11bd72019-04-11 16:00:05 +02002057 struct lysp_notif *notifs = NULL;
2058 struct lysp_node_container *c = NULL;
2059 const char *str;
2060
2061 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2062 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002063 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcifc11bd72019-04-11 16:00:05 +02002064 ctx.line = 1;
2065 ctx.mod_version = 2; /* simulate YANG 1.1 */
2066
2067 /* invalid cardinality */
2068#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2069 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2070 assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, &notifs)); \
2071 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2072 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2073
2074 TEST_DUP("description", "text1", "text2");
2075 TEST_DUP("reference", "1", "2");
2076 TEST_DUP("status", "current", "obsolete");
2077#undef TEST_DUP
2078
2079 /* full content */
2080 str = "top;";
2081 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
2082 str = "ntf {anydata a1; anyxml a2; choice ch; container c; description test; grouping grp; if-feature f; leaf l {type int8;}"
2083 "leaf-list ll {type int8;} list li; must 1; reference test; status current; typedef mytype {type int8;} uses grp; m:ext;}";
2084 assert_int_equal(LY_SUCCESS, parse_notif(&ctx, &str, (struct lysp_node*)c, &notifs));
2085 assert_non_null(notifs);
2086 assert_int_equal(LYS_NOTIF, notifs->nodetype);
2087 assert_string_equal("ntf", notifs->name);
2088 assert_string_equal("test", notifs->dsc);
2089 assert_non_null(notifs->exts);
2090 assert_non_null(notifs->iffeatures);
2091 assert_string_equal("test", notifs->ref);
2092 assert_non_null(notifs->groupings);
2093 assert_non_null(notifs->typedefs);
2094 assert_non_null(notifs->musts);
2095 assert_non_null(notifs->data);
2096 assert_int_equal(LYS_STATUS_CURR, notifs->flags);
2097
2098 ly_set_erase(&ctx.tpdfs_nodes, NULL);
2099 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2100
2101 /* invalid content */
2102 str = "ntf {config true} ...";
2103 assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, &notifs));
2104 logbuf_assert("Invalid keyword \"config\" as a child of \"notification\". Line number 1.");
2105 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2106
2107 *state = NULL;
2108 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
2109 ly_ctx_destroy(ctx.ctx, NULL);
2110}
2111
2112static void
Radek Krejcie86bf772018-12-14 11:39:53 +01002113test_uses(void **state)
2114{
2115 *state = test_uses;
2116
Radek Krejcie7b95092019-05-15 11:03:07 +02002117 struct lys_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01002118 struct lysp_node_uses *u = NULL;
2119 const char *str;
2120
2121 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2122 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002123 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcie86bf772018-12-14 11:39:53 +01002124 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002125 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01002126
2127 /* invalid cardinality */
2128#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2129 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2130 assert_int_equal(LY_EVALID, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u)); \
2131 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2132 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2133
2134 TEST_DUP("description", "text1", "text2");
2135 TEST_DUP("reference", "1", "2");
2136 TEST_DUP("status", "current", "obsolete");
2137 TEST_DUP("when", "true", "false");
2138#undef TEST_DUP
2139
2140 /* full content */
2141 str = "grpref {augment some/node;description test;if-feature f;reference test;refine some/other/node;status current;when true;m:ext;} ...";
2142 assert_int_equal(LY_SUCCESS, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u));
2143 assert_non_null(u);
2144 assert_int_equal(LYS_USES, u->nodetype);
2145 assert_string_equal("grpref", u->name);
2146 assert_string_equal("test", u->dsc);
2147 assert_non_null(u->exts);
2148 assert_non_null(u->iffeatures);
2149 assert_string_equal("test", u->ref);
2150 assert_non_null(u->augments);
2151 assert_non_null(u->refines);
2152 assert_non_null(u->when);
2153 assert_null(u->parent);
2154 assert_null(u->next);
2155 assert_int_equal(LYS_STATUS_CURR, u->flags);
2156 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2157
2158 *state = NULL;
2159 ly_ctx_destroy(ctx.ctx, NULL);
2160}
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002161
2162
2163static void
2164test_augment(void **state)
2165{
2166 *state = test_augment;
2167
Radek Krejcie7b95092019-05-15 11:03:07 +02002168 struct lys_parser_ctx ctx = {0};
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002169 struct lysp_augment *a = NULL;
2170 const char *str;
2171
2172 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2173 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002174 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002175 ctx.line = 1;
Radek Krejcib4adbf12019-02-25 13:35:22 +01002176 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002177
2178 /* invalid cardinality */
2179#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2180 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2181 assert_int_equal(LY_EVALID, parse_augment(&ctx, &str, NULL, &a)); \
2182 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejcib4adbf12019-02-25 13:35:22 +01002183 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002184
2185 TEST_DUP("description", "text1", "text2");
2186 TEST_DUP("reference", "1", "2");
2187 TEST_DUP("status", "current", "obsolete");
2188 TEST_DUP("when", "true", "false");
2189#undef TEST_DUP
2190
2191 /* full content */
2192 str = "/target/nodeid {action x; anydata any;anyxml anyxml; case cs; choice ch;container c;description test;if-feature f;leaf l {type string;}"
2193 "leaf-list ll {type string;} list li;notification not;reference test;status current;uses g;when true;m:ext;} ...";
2194 assert_int_equal(LY_SUCCESS, parse_augment(&ctx, &str, NULL, &a));
2195 assert_non_null(a);
2196 assert_int_equal(LYS_AUGMENT, a->nodetype);
2197 assert_string_equal("/target/nodeid", a->nodeid);
2198 assert_string_equal("test", a->dsc);
2199 assert_non_null(a->exts);
2200 assert_non_null(a->iffeatures);
2201 assert_string_equal("test", a->ref);
2202 assert_non_null(a->when);
2203 assert_null(a->parent);
2204 assert_int_equal(LYS_STATUS_CURR, a->flags);
Radek Krejcib4adbf12019-02-25 13:35:22 +01002205 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002206
2207 *state = NULL;
2208 ly_ctx_destroy(ctx.ctx, NULL);
2209}
2210
Radek Krejcif09e4e82019-06-14 15:08:11 +02002211static void
2212test_when(void **state)
2213{
2214 *state = test_when;
2215
2216 struct lys_parser_ctx ctx = {0};
2217 struct lysp_when *w = NULL;
2218 const char *str;
2219
2220 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2221 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002222 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcif09e4e82019-06-14 15:08:11 +02002223 ctx.line = 1;
2224 ctx.mod_version = 2; /* simulate YANG 1.1 */
2225
2226 /* invalid cardinality */
2227#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2228 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2229 assert_int_equal(LY_EVALID, parse_when(&ctx, &str, &w)); \
2230 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2231 FREE_MEMBER(ctx.ctx, w, lysp_when_free); w = NULL;
2232
2233 TEST_DUP("description", "text1", "text2");
2234 TEST_DUP("reference", "1", "2");
2235#undef TEST_DUP
2236
2237 /* full content */
2238 str = "expression {description test;reference test;m:ext;} ...";
2239 assert_int_equal(LY_SUCCESS, parse_when(&ctx, &str, &w));
2240 assert_non_null(w);
2241 assert_string_equal("expression", w->cond);
2242 assert_string_equal("test", w->dsc);
2243 assert_string_equal("test", w->ref);
2244 assert_non_null(w->exts);
2245 FREE_MEMBER(ctx.ctx, w, lysp_when_free); w = NULL;
2246
2247 /* empty condition */
2248 str = "\"\";";
2249 assert_int_equal(LY_SUCCESS, parse_when(&ctx, &str, &w));
2250 logbuf_assert("Empty argument of when statement does not make sense.");
2251 assert_non_null(w);
2252 assert_string_equal("", w->cond);
2253 FREE_MEMBER(ctx.ctx, w, lysp_when_free); w = NULL;
2254
2255 *state = NULL;
2256 ly_ctx_destroy(ctx.ctx, NULL);
2257}
2258
David Sedlákd6ce6d72019-07-16 17:30:18 +02002259static void
2260test_value(void **state)
2261{
2262 *state = test_value;
2263 struct lys_parser_ctx ctx;
2264
2265 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2266 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002267 ctx.pos_type = LY_VLOG_LINE;
David Sedlákd6ce6d72019-07-16 17:30:18 +02002268 ctx.line = 1;
2269 ctx.indent = 0;
2270 int64_t val = 0;
2271 uint16_t flags = 0;
2272
2273 const char *data = "-0;";
Radek Krejcid6b76452019-09-03 17:03:03 +02002274 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 +02002275 assert_int_equal(val, 0);
2276
2277 data = "-0;";
2278 flags = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02002279 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 +02002280 logbuf_assert("Invalid value \"-0\" of \"position\". Line number 1.");
2281
David Sedlák1af868e2019-07-17 17:03:14 +02002282 *state = NULL;
David Sedlákd6ce6d72019-07-16 17:30:18 +02002283 ly_ctx_destroy(ctx.ctx, NULL);
2284}
2285
Radek Krejci80dd33e2018-09-26 15:57:18 +02002286int main(void)
2287{
2288 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002289 cmocka_unit_test_setup(test_helpers, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002290 cmocka_unit_test_setup(test_comments, logger_setup),
Radek Krejciefd22f62018-09-27 11:47:58 +02002291 cmocka_unit_test_setup(test_arg, logger_setup),
Radek Krejcidcc7b322018-10-11 14:24:02 +02002292 cmocka_unit_test_setup(test_stmts, logger_setup),
Radek Krejci05b13982018-11-28 16:22:07 +01002293 cmocka_unit_test_setup_teardown(test_minmax, logger_setup, logger_teardown),
Radek Krejci40544fa2019-01-11 09:38:37 +01002294 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002295 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci2c02f3e2018-10-16 10:54:38 +02002296 cmocka_unit_test_setup(test_feature, logger_setup),
2297 cmocka_unit_test_setup(test_deviation, logger_setup),
2298 cmocka_unit_test_setup(test_deviate, logger_setup),
Radek Krejci8c370832018-11-02 15:10:03 +01002299 cmocka_unit_test_setup(test_container, logger_setup),
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002300 cmocka_unit_test_setup_teardown(test_leaf, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01002301 cmocka_unit_test_setup_teardown(test_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002302 cmocka_unit_test_setup_teardown(test_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01002303 cmocka_unit_test_setup_teardown(test_choice, logger_setup, logger_teardown),
Radek Krejcia9026eb2018-12-12 16:04:47 +01002304 cmocka_unit_test_setup_teardown(test_case, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01002305 cmocka_unit_test_setup_teardown(test_anydata, logger_setup, logger_teardown),
2306 cmocka_unit_test_setup_teardown(test_anyxml, logger_setup, logger_teardown),
Radek Krejcif538ce52019-03-05 10:46:14 +01002307 cmocka_unit_test_setup_teardown(test_action, logger_setup, logger_teardown),
Radek Krejcifc11bd72019-04-11 16:00:05 +02002308 cmocka_unit_test_setup_teardown(test_notification, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01002309 cmocka_unit_test_setup_teardown(test_grouping, logger_setup, logger_teardown),
2310 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejcib4adbf12019-02-25 13:35:22 +01002311 cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown),
Radek Krejcif09e4e82019-06-14 15:08:11 +02002312 cmocka_unit_test_setup_teardown(test_when, logger_setup, logger_teardown),
David Sedlákd6ce6d72019-07-16 17:30:18 +02002313 cmocka_unit_test_setup_teardown(test_value, logger_setup, logger_teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002314 };
2315
2316 return cmocka_run_group_tests(tests, NULL, NULL);
2317}