blob: d0595ff96ccd39061e6e7ae3a0b3766add7aafbe [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));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200325 assert_null(buf);
326 assert_int_equal(9, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200327 assert_string_equal("hello\\n\\t\\\"\\\\\";", word);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200328
329 ctx.indent = 14;
330 str = "\"hello \t\n\t\t world!\"";
331 /* - space and tabs before newline are stripped out
332 * - space and tabs after newline (indentation) are stripped out
333 */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200334 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200335 assert_non_null(buf);
336 assert_ptr_equal(word, buf);
337 assert_int_equal(14, len);
338 assert_string_equal("hello\n world!", word);
339 free(buf);
340
341 ctx.indent = 14;
342 str = "\"hello\n \tworld!\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200343 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200344 assert_non_null(buf);
345 assert_ptr_equal(word, buf);
346 assert_int_equal(12, len);
347 assert_string_equal("hello\nworld!", word);
348 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200349
350 str = "\'hello\'";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200351 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200352 assert_null(buf);
353 assert_int_equal(5, len);
354 assert_false(strncmp("hello", word, 5));
355
356 str = "\"hel\" +\t\n\"lo\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200357 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200358 assert_ptr_equal(word, buf);
359 assert_int_equal(5, len);
360 assert_string_equal("hello", word);
361 free(buf);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200362 str = "\"hel\" +\t\nlo"; /* unquoted the second part */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200363 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200364 logbuf_assert("Both string parts divided by '+' must be quoted. Line number 5.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200365
366 str = "\'he\'\t\n+ \"llo\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200367 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200368 assert_ptr_equal(word, buf);
369 assert_int_equal(5, len);
370 assert_string_equal("hello", word);
371 free(buf);
372
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200373 str = " \t\n\"he\"+\'llo\'";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200374 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200375 assert_ptr_equal(word, buf);
376 assert_int_equal(5, len);
377 assert_string_equal("hello", word);
378 free(buf);
379
Radek Krejci44ceedc2018-10-02 15:54:31 +0200380 /* missing argument */
381 str = ";";
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 Krejcifc62d7e2018-10-11 12:56:42 +0200383 logbuf_assert("Invalid character sequence \";\", expected an argument. Line number 7.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200384}
385
386static void
387test_stmts(void **state)
388{
389 (void) state; /* unused */
390
Radek Krejcie7b95092019-05-15 11:03:07 +0200391 struct lys_parser_ctx ctx;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200392 const char *str, *p;
Radek Krejcid6b76452019-09-03 17:03:03 +0200393 enum ly_stmt kw;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200394 char *word;
395 size_t len;
396
397 ctx.ctx = NULL;
Radek Krejci99435242019-09-05 16:19:15 +0200398 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200399 ctx.line = 1;
400
401 str = "\n// comment\n\tinput\t{";
402 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200403 assert_int_equal(LY_STMT_INPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200404 assert_int_equal(5, len);
405 assert_string_equal("input\t{", word);
406 assert_string_equal("\t{", str);
407
408 str = "\t /* comment */\t output\n\t{";
409 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200410 assert_int_equal(LY_STMT_OUTPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200411 assert_int_equal(6, len);
412 assert_string_equal("output\n\t{", word);
413 assert_string_equal("\n\t{", str);
Radek Krejciabdd8062019-06-11 16:44:19 +0200414 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200415 assert_int_equal(LY_STMT_SYNTAX_LEFT_BRACE, kw);
Radek Krejciabdd8062019-06-11 16:44:19 +0200416 assert_int_equal(1, len);
417 assert_string_equal("{", word);
418 assert_string_equal("", str);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200419
420 str = "/input { "; /* invalid slash */
421 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
422 logbuf_assert("Invalid identifier first character '/'. Line number 4.");
423
424 str = "not-a-statement-nor-extension { "; /* invalid identifier */
425 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
426 logbuf_assert("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword. Line number 4.");
427
428 str = "path;"; /* missing sep after the keyword */
429 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
430 logbuf_assert("Invalid character sequence \"path;\", expected a keyword followed by a separator. Line number 4.");
431
432 str = "action ";
433 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_ACTION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200435 assert_int_equal(6, len);
436 str = "anydata ";
437 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200438 assert_int_equal(LY_STMT_ANYDATA, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200439 assert_int_equal(7, len);
440 str = "anyxml ";
441 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200442 assert_int_equal(LY_STMT_ANYXML, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200443 assert_int_equal(6, len);
444 str = "argument ";
445 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200446 assert_int_equal(LY_STMT_ARGUMENT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200447 assert_int_equal(8, len);
448 str = "augment ";
449 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200450 assert_int_equal(LY_STMT_AUGMENT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200451 assert_int_equal(7, len);
452 str = "base ";
453 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200454 assert_int_equal(LY_STMT_BASE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200455 assert_int_equal(4, len);
456 str = "belongs-to ";
457 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200458 assert_int_equal(LY_STMT_BELONGS_TO, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200459 assert_int_equal(10, len);
460 str = "bit ";
461 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200462 assert_int_equal(LY_STMT_BIT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200463 assert_int_equal(3, len);
464 str = "case ";
465 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200466 assert_int_equal(LY_STMT_CASE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200467 assert_int_equal(4, len);
468 str = "choice ";
469 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200470 assert_int_equal(LY_STMT_CHOICE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200471 assert_int_equal(6, len);
472 str = "config ";
473 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200474 assert_int_equal(LY_STMT_CONFIG, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200475 assert_int_equal(6, len);
476 str = "contact ";
477 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200478 assert_int_equal(LY_STMT_CONTACT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200479 assert_int_equal(7, len);
480 str = "container ";
481 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200482 assert_int_equal(LY_STMT_CONTAINER, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200483 assert_int_equal(9, len);
484 str = "default ";
485 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200486 assert_int_equal(LY_STMT_DEFAULT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200487 assert_int_equal(7, len);
488 str = "description ";
489 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200490 assert_int_equal(LY_STMT_DESCRIPTION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200491 assert_int_equal(11, len);
492 str = "deviate ";
493 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200494 assert_int_equal(LY_STMT_DEVIATE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200495 assert_int_equal(7, len);
496 str = "deviation ";
497 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200498 assert_int_equal(LY_STMT_DEVIATION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200499 assert_int_equal(9, len);
500 str = "enum ";
501 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200502 assert_int_equal(LY_STMT_ENUM, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200503 assert_int_equal(4, len);
504 str = "error-app-tag ";
505 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200506 assert_int_equal(LY_STMT_ERROR_APP_TAG, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200507 assert_int_equal(13, len);
508 str = "error-message ";
509 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200510 assert_int_equal(LY_STMT_ERROR_MESSAGE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200511 assert_int_equal(13, len);
512 str = "extension ";
513 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200514 assert_int_equal(LY_STMT_EXTENSION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200515 assert_int_equal(9, len);
516 str = "feature ";
517 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200518 assert_int_equal(LY_STMT_FEATURE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200519 assert_int_equal(7, len);
520 str = "fraction-digits ";
521 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200522 assert_int_equal(LY_STMT_FRACTION_DIGITS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200523 assert_int_equal(15, len);
524 str = "grouping ";
525 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200526 assert_int_equal(LY_STMT_GROUPING, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200527 assert_int_equal(8, len);
528 str = "identity ";
529 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200530 assert_int_equal(LY_STMT_IDENTITY, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200531 assert_int_equal(8, len);
532 str = "if-feature ";
533 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200534 assert_int_equal(LY_STMT_IF_FEATURE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200535 assert_int_equal(10, len);
536 str = "import ";
537 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200538 assert_int_equal(LY_STMT_IMPORT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200539 assert_int_equal(6, len);
540 str = "include ";
541 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200542 assert_int_equal(LY_STMT_INCLUDE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200543 assert_int_equal(7, len);
544 str = "input{";
545 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200546 assert_int_equal(LY_STMT_INPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200547 assert_int_equal(5, len);
548 str = "key ";
549 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200550 assert_int_equal(LY_STMT_KEY, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200551 assert_int_equal(3, len);
552 str = "leaf ";
553 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200554 assert_int_equal(LY_STMT_LEAF, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200555 assert_int_equal(4, len);
556 str = "leaf-list ";
557 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200558 assert_int_equal(LY_STMT_LEAF_LIST, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200559 assert_int_equal(9, len);
560 str = "length ";
561 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200562 assert_int_equal(LY_STMT_LENGTH, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200563 assert_int_equal(6, len);
564 str = "list ";
565 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200566 assert_int_equal(LY_STMT_LIST, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200567 assert_int_equal(4, len);
568 str = "mandatory ";
569 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200570 assert_int_equal(LY_STMT_MANDATORY, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200571 assert_int_equal(9, len);
572 str = "max-elements ";
573 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200574 assert_int_equal(LY_STMT_MAX_ELEMENTS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200575 assert_int_equal(12, len);
576 str = "min-elements ";
577 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200578 assert_int_equal(LY_STMT_MIN_ELEMENTS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200579 assert_int_equal(12, len);
580 str = "modifier ";
581 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200582 assert_int_equal(LY_STMT_MODIFIER, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200583 assert_int_equal(8, len);
584 str = "module ";
585 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200586 assert_int_equal(LY_STMT_MODULE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200587 assert_int_equal(6, len);
588 str = "must ";
589 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200590 assert_int_equal(LY_STMT_MUST, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200591 assert_int_equal(4, len);
592 str = "namespace ";
593 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200594 assert_int_equal(LY_STMT_NAMESPACE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200595 assert_int_equal(9, len);
596 str = "notification ";
597 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200598 assert_int_equal(LY_STMT_NOTIFICATION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200599 assert_int_equal(12, len);
600 str = "ordered-by ";
601 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200602 assert_int_equal(LY_STMT_ORDERED_BY, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200603 assert_int_equal(10, len);
604 str = "organization ";
605 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200606 assert_int_equal(LY_STMT_ORGANIZATION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200607 assert_int_equal(12, len);
608 str = "output ";
609 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200610 assert_int_equal(LY_STMT_OUTPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200611 assert_int_equal(6, len);
612 str = "path ";
613 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200614 assert_int_equal(LY_STMT_PATH, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200615 assert_int_equal(4, len);
616 str = "pattern ";
617 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200618 assert_int_equal(LY_STMT_PATTERN, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200619 assert_int_equal(7, len);
620 str = "position ";
621 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200622 assert_int_equal(LY_STMT_POSITION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200623 assert_int_equal(8, len);
624 str = "prefix ";
625 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200626 assert_int_equal(LY_STMT_PREFIX, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200627 assert_int_equal(6, len);
628 str = "presence ";
629 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200630 assert_int_equal(LY_STMT_PRESENCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200631 assert_int_equal(8, len);
632 str = "range ";
633 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200634 assert_int_equal(LY_STMT_RANGE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200635 assert_int_equal(5, len);
636 str = "reference ";
637 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200638 assert_int_equal(LY_STMT_REFERENCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200639 assert_int_equal(9, len);
640 str = "refine ";
641 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200642 assert_int_equal(LY_STMT_REFINE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200643 assert_int_equal(6, len);
644 str = "require-instance ";
645 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200646 assert_int_equal(LY_STMT_REQUIRE_INSTANCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200647 assert_int_equal(16, len);
648 str = "revision ";
649 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200650 assert_int_equal(LY_STMT_REVISION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200651 assert_int_equal(8, len);
652 str = "revision-date ";
653 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200654 assert_int_equal(LY_STMT_REVISION_DATE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200655 assert_int_equal(13, len);
656 str = "rpc ";
657 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200658 assert_int_equal(LY_STMT_RPC, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200659 assert_int_equal(3, len);
660 str = "status ";
661 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200662 assert_int_equal(LY_STMT_STATUS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200663 assert_int_equal(6, len);
664 str = "submodule ";
665 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200666 assert_int_equal(LY_STMT_SUBMODULE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200667 assert_int_equal(9, len);
668 str = "type ";
669 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200670 assert_int_equal(LY_STMT_TYPE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200671 assert_int_equal(4, len);
672 str = "typedef ";
673 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200674 assert_int_equal(LY_STMT_TYPEDEF, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200675 assert_int_equal(7, len);
676 str = "unique ";
677 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200678 assert_int_equal(LY_STMT_UNIQUE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200679 assert_int_equal(6, len);
680 str = "units ";
681 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200682 assert_int_equal(LY_STMT_UNITS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200683 assert_int_equal(5, len);
684 str = "uses ";
685 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200686 assert_int_equal(LY_STMT_USES, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200687 assert_int_equal(4, len);
688 str = "value ";
689 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200690 assert_int_equal(LY_STMT_VALUE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200691 assert_int_equal(5, len);
692 str = "when ";
693 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200694 assert_int_equal(LY_STMT_WHEN, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200695 assert_int_equal(4, len);
696 str = "yang-version ";
697 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200698 assert_int_equal(LY_STMT_YANG_VERSION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200699 assert_int_equal(12, len);
700 str = "yin-element ";
701 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200702 assert_int_equal(LY_STMT_YIN_ELEMENT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200703 assert_int_equal(11, len);
Radek Krejci626df482018-10-11 15:06:31 +0200704 str = ";config false;";
705 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200706 assert_int_equal(LY_STMT_SYNTAX_SEMICOLON, kw);
Radek Krejci626df482018-10-11 15:06:31 +0200707 assert_int_equal(1, len);
708 assert_string_equal("config false;", str);
709 str = "{ config false;";
710 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200711 assert_int_equal(LY_STMT_SYNTAX_LEFT_BRACE, kw);
Radek Krejci626df482018-10-11 15:06:31 +0200712 assert_int_equal(1, len);
713 assert_string_equal(" config false;", str);
714 str = "}";
715 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200716 assert_int_equal(LY_STMT_SYNTAX_RIGHT_BRACE, kw);
Radek Krejci626df482018-10-11 15:06:31 +0200717 assert_int_equal(1, len);
718 assert_string_equal("", str);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200719
720 /* geenric extension */
721 str = p = "nacm:default-deny-write;";
722 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200723 assert_int_equal(LY_STMT_EXTENSION_INSTANCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200724 assert_int_equal(23, len);
725 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200726}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200727
Radek Krejci05b13982018-11-28 16:22:07 +0100728static void
729test_minmax(void **state)
730{
731 *state = test_minmax;
732
Radek Krejcie7b95092019-05-15 11:03:07 +0200733 struct lys_parser_ctx ctx = {0};
Radek Krejci05b13982018-11-28 16:22:07 +0100734 uint16_t flags = 0;
735 uint32_t value = 0;
736 struct lysp_ext_instance *ext = NULL;
737 const char *str;
738
739 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
740 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +0200741 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci05b13982018-11-28 16:22:07 +0100742 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100743 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci05b13982018-11-28 16:22:07 +0100744
Radek Krejcidf6cad12018-11-28 17:10:55 +0100745 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100746 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100747 logbuf_assert("Invalid value \"1invalid\" of \"min-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100748
749 flags = value = 0;
750 str = " -1; ...";
751 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
752 logbuf_assert("Invalid value \"-1\" of \"min-elements\". Line number 1.");
753
Radek Krejcidf6cad12018-11-28 17:10:55 +0100754 /* implementation limit */
755 flags = value = 0;
756 str = " 4294967296; ...";
757 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
758 logbuf_assert("Value \"4294967296\" is out of \"min-elements\" bounds. Line number 1.");
759
Radek Krejci05b13982018-11-28 16:22:07 +0100760 flags = value = 0;
761 str = " 1; ...";
762 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
763 assert_int_equal(LYS_SET_MIN, flags);
764 assert_int_equal(1, value);
765
766 flags = value = 0;
767 str = " 1 {m:ext;} ...";
768 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
769 assert_int_equal(LYS_SET_MIN, flags);
770 assert_int_equal(1, value);
771 assert_non_null(ext);
772 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
773 ext = NULL;
774
775 flags = value = 0;
776 str = " 1 {config true;} ...";
777 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
778 logbuf_assert("Invalid keyword \"config\" as a child of \"min-elements\". Line number 1.");
779
Radek Krejcidf6cad12018-11-28 17:10:55 +0100780 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100781 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100782 logbuf_assert("Invalid value \"1invalid\" of \"max-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100783
784 flags = value = 0;
785 str = " -1; ...";
786 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
787 logbuf_assert("Invalid value \"-1\" of \"max-elements\". Line number 1.");
788
Radek Krejcidf6cad12018-11-28 17:10:55 +0100789 /* implementation limit */
790 flags = value = 0;
791 str = " 4294967296; ...";
792 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
793 logbuf_assert("Value \"4294967296\" is out of \"max-elements\" bounds. Line number 1.");
794
Radek Krejci05b13982018-11-28 16:22:07 +0100795 flags = value = 0;
796 str = " 1; ...";
797 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
798 assert_int_equal(LYS_SET_MAX, flags);
799 assert_int_equal(1, value);
800
801 flags = value = 0;
802 str = " unbounded; ...";
803 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
804 assert_int_equal(LYS_SET_MAX, flags);
805 assert_int_equal(0, value);
806
807 flags = value = 0;
808 str = " 1 {m:ext;} ...";
809 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
810 assert_int_equal(LYS_SET_MAX, flags);
811 assert_int_equal(1, value);
812 assert_non_null(ext);
813 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
814 ext = NULL;
815
816 flags = value = 0;
817 str = " 1 {config true;} ...";
818 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
819 logbuf_assert("Invalid keyword \"config\" as a child of \"max-elements\". Line number 1.");
820
821 *state = NULL;
822 ly_ctx_destroy(ctx.ctx, NULL);
823}
824
Radek Krejci9fcacc12018-10-11 15:59:11 +0200825static struct lysp_module *
Radek Krejcie7b95092019-05-15 11:03:07 +0200826mod_renew(struct lys_parser_ctx *ctx)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200827{
Radek Krejci40544fa2019-01-11 09:38:37 +0100828 struct lysp_module *mod_p;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100829 static struct lys_module mod = {0};
830
831 lysc_module_free(mod.compiled, NULL);
832 lysp_module_free(mod.parsed);
833 FREE_STRING(mod.ctx, mod.name);
834 FREE_STRING(mod.ctx, mod.ns);
835 FREE_STRING(mod.ctx, mod.prefix);
836 FREE_STRING(mod.ctx, mod.filepath);
837 FREE_STRING(mod.ctx, mod.org);
838 FREE_STRING(mod.ctx, mod.contact);
839 FREE_STRING(mod.ctx, mod.dsc);
840 FREE_STRING(mod.ctx, mod.ref);
841 memset(&mod, 0, sizeof mod);
842 mod.ctx = ctx->ctx;
843
844 mod_p = calloc(1, sizeof *mod_p);
845 mod.parsed = mod_p;
846 mod_p->mod = &mod;
847 assert_non_null(mod_p);
848 return mod_p;
849}
850
851static struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +0200852submod_renew(struct lys_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100853{
854 lysp_submodule_free(ctx->ctx, submod);
855 submod = calloc(1, sizeof *submod);
856 assert_non_null(submod);
857 return submod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200858}
859
Radek Krejcid33273d2018-10-25 14:55:52 +0200860static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
861 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
862 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
863{
864 *module_data = user_data;
865 *format = LYS_IN_YANG;
866 *free_module_data = NULL;
867 return LY_SUCCESS;
868}
869
Radek Krejci9fcacc12018-10-11 15:59:11 +0200870static void
871test_module(void **state)
872{
Radek Krejci40544fa2019-01-11 09:38:37 +0100873 *state = test_module;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200874
Radek Krejcie7b95092019-05-15 11:03:07 +0200875 struct lys_parser_ctx ctx;
Radek Krejci40544fa2019-01-11 09:38:37 +0100876 struct lysp_module *mod = NULL;
877 struct lysp_submodule *submod = NULL;
878 struct lys_module *m;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200879 const char *str;
880
881 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
882 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +0200883 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200884 ctx.line = 1;
Radek Krejcia042ea12018-10-13 07:52:15 +0200885 ctx.indent = 0;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200886
Radek Krejci40544fa2019-01-11 09:38:37 +0100887 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200888
889 /* missing mandatory substatements */
890 str = " name {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100891 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
892 assert_string_equal("name", mod->mod->name);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200893 logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100894 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200895
896 str = " name {namespace urn:x;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100897 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
898 assert_string_equal("urn:x", mod->mod->ns);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200899 logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100900 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200901
902 str = " name {namespace urn:x;prefix \"x\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100903 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
904 assert_string_equal("x", mod->mod->prefix);
Radek Krejci40544fa2019-01-11 09:38:37 +0100905 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200906
Radek Krejci027d5802018-11-14 16:57:28 +0100907#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
908#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200909#define TEST_NODE(NODETYPE, INPUT, NAME) \
910 str = SCHEMA_BEGINNING INPUT; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100911 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200912 assert_non_null(mod->data); \
913 assert_int_equal(NODETYPE, mod->data->nodetype); \
914 assert_string_equal(NAME, mod->data->name); \
Radek Krejci40544fa2019-01-11 09:38:37 +0100915 mod = mod_renew(&ctx);
Radek Krejcia042ea12018-10-13 07:52:15 +0200916#define TEST_GENERIC(INPUT, TARGET, TEST) \
917 str = SCHEMA_BEGINNING INPUT; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100918 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200919 assert_non_null(TARGET); \
920 TEST; \
Radek Krejci40544fa2019-01-11 09:38:37 +0100921 mod = mod_renew(&ctx);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100922#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200923 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
Radek Krejci40544fa2019-01-11 09:38:37 +0100924 parse_module, mod, LINE, mod = mod_renew(&ctx))
Radek Krejcia042ea12018-10-13 07:52:15 +0200925
926 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100927 TEST_DUP("namespace", "y", "z", "1");
928 TEST_DUP("prefix", "y", "z", "1");
929 TEST_DUP("contact", "a", "b", "1");
930 TEST_DUP("description", "a", "b", "1");
931 TEST_DUP("organization", "a", "b", "1");
932 TEST_DUP("reference", "a", "b", "1");
Radek Krejcia042ea12018-10-13 07:52:15 +0200933
Radek Krejci70853c52018-10-15 14:46:16 +0200934 /* not allowed in module (submodule-specific) */
935 str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100936 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200937 logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100938 mod = mod_renew(&ctx);
Radek Krejci70853c52018-10-15 14:46:16 +0200939
Radek Krejcia042ea12018-10-13 07:52:15 +0200940 /* anydata */
941 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
942 /* anyxml */
943 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
944 /* augment */
945 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200946 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200947 /* choice */
948 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
949 /* contact 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100950 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact,
951 assert_string_equal("firstname surname", mod->mod->contact));
Radek Krejcia042ea12018-10-13 07:52:15 +0200952 /* container */
953 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
954 /* description 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100955 TEST_GENERIC("description \'some description\';}", mod->mod->dsc,
956 assert_string_equal("some description", mod->mod->dsc));
Radek Krejcia042ea12018-10-13 07:52:15 +0200957 /* deviation */
958 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200959 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200960 /* extension */
961 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200962 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200963 /* feature */
964 TEST_GENERIC("feature test;}", mod->features,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200965 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200966 /* grouping */
967 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200968 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200969 /* identity */
970 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200971 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200972 /* import */
Radek Krejci086c7132018-10-26 15:29:04 +0200973 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
974 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
975 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200976
Radek Krejcia042ea12018-10-13 07:52:15 +0200977 /* import - prefix collision */
Radek Krejci086c7132018-10-26 15:29:04 +0200978 str = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100979 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200980 logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100981 mod = mod_renew(&ctx);
Radek Krejci086c7132018-10-26 15:29:04 +0200982 str = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100983 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci086c7132018-10-26 15:29:04 +0200984 logbuf_assert("Prefix \"y\" already used to import \"zzz\" module. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100985 mod = mod_renew(&ctx);
Radek Krejci086c7132018-10-26 15:29:04 +0200986 str = "module" SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix z;}}";
987 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
988 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
989 logbuf_assert("Single revision of the module \"zzz\" referred twice.");
Radek Krejci70853c52018-10-15 14:46:16 +0200990
Radek Krejcia042ea12018-10-13 07:52:15 +0200991 /* include */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100992 store = 1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200993 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 +0200994 str = "module" SCHEMA_BEGINNING "include xxx;}";
995 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
996 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100997 logbuf_assert("Input data contains module in situation when a submodule is expected.");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100998 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200999
Radek Krejci9ed7a192018-10-31 16:23:51 +01001000 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +01001001 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 +02001002 str = "module" SCHEMA_BEGINNING "include xxx;}";
1003 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
1004 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
1005 logbuf_assert("Included \"xxx\" submodule from \"name\" belongs-to a different module \"wrong-name\".");
Radek Krejci9ed7a192018-10-31 16:23:51 +01001006 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +02001007
Radek Krejci313d9902018-11-08 09:42:58 +01001008 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 +02001009 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejci086c7132018-10-26 15:29:04 +02001010 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +02001011
Radek Krejcia042ea12018-10-13 07:52:15 +02001012 /* leaf */
1013 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
1014 /* leaf-list */
1015 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
1016 /* list */
1017 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
1018 /* notification */
1019 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001020 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001021 /* organization 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001022 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org,
1023 assert_string_equal("CESNET a.l.e.", mod->mod->org));
Radek Krejcia042ea12018-10-13 07:52:15 +02001024 /* reference 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001025 TEST_GENERIC("reference RFC7950;}", mod->mod->ref,
1026 assert_string_equal("RFC7950", mod->mod->ref));
Radek Krejcia042ea12018-10-13 07:52:15 +02001027 /* revision */
1028 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib7db73a2018-10-24 14:18:40 +02001029 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +02001030 /* rpc */
1031 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001032 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001033 /* typedef */
1034 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001035 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001036 /* uses */
1037 TEST_NODE(LYS_USES, "uses test;}", "test");
1038 /* yang-version */
Radek Krejci027d5802018-11-14 16:57:28 +01001039 str = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001040 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +02001041 logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001042 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001043 str = SCHEMA_BEGINNING2 "yang-version 1.0;yang-version 1.1;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001044 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +02001045 logbuf_assert("Duplicate keyword \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001046 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001047 str = SCHEMA_BEGINNING2 "yang-version 1.0;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001048 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
1049 assert_int_equal(1, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +01001050 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001051 str = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001052 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
1053 assert_int_equal(2, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +01001054 mod = mod_renew(&ctx);
1055
David Sedlák1b623122019-08-05 15:27:49 +02001056 struct lys_parser_ctx *ctx_p = NULL;
Radek Krejci40544fa2019-01-11 09:38:37 +01001057 str = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
1058 m = mod->mod;
1059 free(mod);
1060 m->parsed = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02001061 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, str, m));
1062 logbuf_assert("Trailing garbage \"module q {names...\" after module, expected end-of-input. Line number 1.");
1063 lys_parser_ctx_free(ctx_p);
Radek Krejci40544fa2019-01-11 09:38:37 +01001064 mod = mod_renew(&ctx);
1065
1066 str = "prefix " SCHEMA_BEGINNING "}";
1067 m = mod->mod;
1068 free(mod);
1069 m->parsed = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02001070 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, str, m));
1071 lys_parser_ctx_free(ctx_p);
1072 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001073 mod = mod_renew(&ctx);
Radek Krejci09306362018-10-15 15:26:01 +02001074
David Sedlák9fb515f2019-07-11 10:33:58 +02001075 str = "module " SCHEMA_BEGINNING "}";
1076 str = "module " SCHEMA_BEGINNING "leaf enum {type enumeration {enum seven { position 7;}}}}";
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 lys_parser_ctx_free(ctx_p);
1082 logbuf_assert("Invalid keyword \"position\" as a child of \"enum\". Line number 1.");
David Sedlák9fb515f2019-07-11 10:33:58 +02001083 mod = mod_renew(&ctx);
1084
Radek Krejci156ccaf2018-10-15 15:49:17 +02001085 /* extensions */
1086 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001087 assert_string_equal("prefix:test", mod->exts[0].name);
1088 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
Radek Krejci40544fa2019-01-11 09:38:37 +01001089 mod = mod_renew(&ctx);
Radek Krejci156ccaf2018-10-15 15:49:17 +02001090
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001091 /* invalid substatement */
1092 str = SCHEMA_BEGINNING "must false;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001093 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001094 logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001095 mod = mod_renew(&ctx);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001096
Radek Krejci09306362018-10-15 15:26:01 +02001097 /* submodule */
Radek Krejci40544fa2019-01-11 09:38:37 +01001098 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001099
1100 /* missing mandatory substatements */
1101 str = " subname {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001102 lydict_remove(ctx.ctx, submod->name);
1103 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
1104 assert_string_equal("subname", submod->name);
Radek Krejci09306362018-10-15 15:26:01 +02001105 logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001106 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001107
Radek Krejci313d9902018-11-08 09:42:58 +01001108 str = " subname {belongs-to name {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001109 lydict_remove(ctx.ctx, submod->name);
1110 assert_int_equal(LY_SUCCESS, parse_submodule(&ctx, &str, submod));
1111 assert_string_equal("name", submod->belongsto);
1112 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001113
1114#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +01001115#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +02001116
1117 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001118 str = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ...";
1119 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod)); \
1120 logbuf_assert("Duplicate keyword \"belongs-to\". Line number 3."); \
1121 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001122
1123 /* not allowed in submodule (module-specific) */
1124 str = SCHEMA_BEGINNING "namespace \"urn:z\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001125 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001126 logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001127 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001128 str = SCHEMA_BEGINNING "prefix m;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001129 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001130 logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001131 submod = submod_renew(&ctx, submod);
Radek Krejcia042ea12018-10-13 07:52:15 +02001132
Radek Krejci40544fa2019-01-11 09:38:37 +01001133 str = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
1134 lysp_submodule_free(ctx.ctx, submod);
1135 submod = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02001136 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, ctx.ctx, &ctx, str, &submod));
1137 lys_parser_ctx_free(ctx_p);
1138 logbuf_assert("Trailing garbage \"module q {names...\" after submodule, expected end-of-input. Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001139
1140 str = "prefix " SCHEMA_BEGINNING "}";
David Sedlák1b623122019-08-05 15:27:49 +02001141 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, ctx.ctx, &ctx, str, &submod));
1142 lys_parser_ctx_free(ctx_p);
1143 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001144 submod = submod_renew(&ctx, submod);
1145
Radek Krejcia042ea12018-10-13 07:52:15 +02001146#undef TEST_GENERIC
1147#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001148#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +02001149#undef SCHEMA_BEGINNING
1150
Radek Krejci9fcacc12018-10-11 15:59:11 +02001151 lysp_module_free(mod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001152 lysp_submodule_free(ctx.ctx, submod);
Radek Krejci9fcacc12018-10-11 15:59:11 +02001153 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci40544fa2019-01-11 09:38:37 +01001154
1155 *state = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02001156}
1157
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001158static void
1159test_identity(void **state)
1160{
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001161 *state = test_identity;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001162
Radek Krejcie7b95092019-05-15 11:03:07 +02001163 struct lys_parser_ctx ctx;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001164 struct lysp_ident *ident = NULL;
1165 const char *str;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001166
1167 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1168 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001169 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001170 ctx.line = 1;
1171 ctx.indent = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001172 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001173
1174 /* invalid cardinality */
1175#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001176 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001177 &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL)
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001178
Radek Krejci38222632019-02-12 16:55:05 +01001179 TEST_DUP("description", "a", "b");
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001180 TEST_DUP("reference", "a", "b");
1181 TEST_DUP("status", "current", "obsolete");
1182
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001183 /* full content */
David Sedlák40bb13b2019-07-10 14:34:18 +02001184 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 +02001185 assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident));
1186 assert_non_null(ident);
1187 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001188 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001189 ident = NULL;
1190
1191 /* invalid substatement */
1192 str = " test {organization XXX;}";
1193 assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident));
1194 logbuf_assert("Invalid keyword \"organization\" as a child of \"identity\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001195 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001196 ident = NULL;
1197
1198#undef TEST_DUP
1199
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001200 *state = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001201 ly_ctx_destroy(ctx.ctx, NULL);
1202}
1203
1204static void
1205test_feature(void **state)
1206{
1207 (void) state; /* unused */
1208
Radek Krejcie7b95092019-05-15 11:03:07 +02001209 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001210 struct lysp_feature *features = NULL;
1211 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001212
1213 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1214 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001215 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001216 ctx.line = 1;
1217 ctx.indent = 0;
1218
1219 /* invalid cardinality */
1220#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1221 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001222 &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001223
1224 TEST_DUP("description", "a", "b");
1225 TEST_DUP("reference", "a", "b");
1226 TEST_DUP("status", "current", "obsolete");
1227
1228 /* full content */
1229 str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ...";
1230 assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features));
1231 assert_non_null(features);
1232 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001233 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001234 features = NULL;
1235
1236 /* invalid substatement */
1237 str = " test {organization XXX;}";
1238 assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features));
1239 logbuf_assert("Invalid keyword \"organization\" as a child of \"feature\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001240 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001241 features = NULL;
1242
1243#undef TEST_DUP
1244
1245 ly_ctx_destroy(ctx.ctx, NULL);
1246}
1247
1248static void
1249test_deviation(void **state)
1250{
1251 (void) state; /* unused */
1252
Radek Krejcie7b95092019-05-15 11:03:07 +02001253 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001254 struct lysp_deviation *d = NULL;
1255 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001256
1257 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1258 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001259 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001260 ctx.line = 1;
1261 ctx.indent = 0;
1262
1263 /* invalid cardinality */
1264#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1265 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001266 &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001267
1268 TEST_DUP("description", "a", "b");
1269 TEST_DUP("reference", "a", "b");
1270
1271 /* full content */
1272 str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
1273 assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d));
1274 assert_non_null(d);
1275 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001276 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001277 d = NULL;
1278
1279 /* missing mandatory substatement */
1280 str = " test {description text;}";
1281 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1282 logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001283 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001284 d = NULL;
1285
1286 /* invalid substatement */
1287 str = " test {deviate not-supported; status obsolete;}";
1288 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1289 logbuf_assert("Invalid keyword \"status\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001290 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001291 d = NULL;
1292
1293#undef TEST_DUP
1294
1295 ly_ctx_destroy(ctx.ctx, NULL);
1296}
1297
1298static void
1299test_deviate(void **state)
1300{
1301 (void) state; /* unused */
1302
Radek Krejcie7b95092019-05-15 11:03:07 +02001303 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001304 struct lysp_deviate *d = NULL;
1305 const char *str;
1306
1307 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1308 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001309 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001310 ctx.line = 1;
1311 ctx.indent = 0;
1312
1313 /* invalid cardinality */
1314#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
1315 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001316 &d, "1", lysp_deviate_free(ctx.ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001317
1318 TEST_DUP("add", "config", "true", "false");
1319 TEST_DUP("replace", "default", "int8", "uint8");
1320 TEST_DUP("add", "mandatory", "true", "false");
1321 TEST_DUP("add", "max-elements", "1", "2");
1322 TEST_DUP("add", "min-elements", "1", "2");
1323 TEST_DUP("replace", "type", "int8", "uint8");
1324 TEST_DUP("add", "units", "kilometers", "miles");
1325
1326 /* full contents */
1327 str = " not-supported {prefix:ext;} ...";
1328 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1329 assert_non_null(d);
1330 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001331 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001332 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;} ...";
1333 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1334 assert_non_null(d);
1335 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001336 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001337 str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...";
1338 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1339 assert_non_null(d);
1340 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001341 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001342 str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...";
1343 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1344 assert_non_null(d);
1345 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001346 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001347
1348 /* invalid substatements */
1349#define TEST_NOT_SUP(DEV, STMT, VALUE) \
1350 str = " "DEV" {"STMT" "VALUE";}..."; \
1351 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \
1352 logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001353 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001354
1355 TEST_NOT_SUP("not-supported", "units", "meters");
1356 TEST_NOT_SUP("not-supported", "must", "1");
1357 TEST_NOT_SUP("not-supported", "unique", "x");
1358 TEST_NOT_SUP("not-supported", "default", "a");
1359 TEST_NOT_SUP("not-supported", "config", "true");
1360 TEST_NOT_SUP("not-supported", "mandatory", "true");
1361 TEST_NOT_SUP("not-supported", "min-elements", "1");
1362 TEST_NOT_SUP("not-supported", "max-elements", "2");
1363 TEST_NOT_SUP("not-supported", "type", "string");
1364 TEST_NOT_SUP("add", "type", "string");
1365 TEST_NOT_SUP("delete", "config", "true");
1366 TEST_NOT_SUP("delete", "mandatory", "true");
1367 TEST_NOT_SUP("delete", "min-elements", "1");
1368 TEST_NOT_SUP("delete", "max-elements", "2");
1369 TEST_NOT_SUP("delete", "type", "string");
1370 TEST_NOT_SUP("replace", "must", "1");
1371 TEST_NOT_SUP("replace", "unique", "a");
1372
1373 str = " nonsence; ...";
1374 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d));
1375 logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1.");
1376 assert_null(d);
1377
1378#undef TEST_NOT_SUP
1379#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001380
1381 ly_ctx_destroy(ctx.ctx, NULL);
1382}
1383
Radek Krejci8c370832018-11-02 15:10:03 +01001384static void
1385test_container(void **state)
1386{
1387 (void) state; /* unused */
1388
Radek Krejcie7b95092019-05-15 11:03:07 +02001389 struct lys_parser_ctx ctx = {0};
Radek Krejci8c370832018-11-02 15:10:03 +01001390 struct lysp_node_container *c = NULL;
1391 const char *str;
1392
1393 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1394 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001395 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci8c370832018-11-02 15:10:03 +01001396 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001397 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +01001398
1399 /* invalid cardinality */
1400#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1401 str = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1402 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); \
1403 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001404 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001405
1406 TEST_DUP("config", "true", "false");
1407 TEST_DUP("description", "text1", "text2");
1408 TEST_DUP("presence", "true", "false");
1409 TEST_DUP("reference", "1", "2");
1410 TEST_DUP("status", "current", "obsolete");
1411 TEST_DUP("when", "true", "false");
1412#undef TEST_DUP
1413
1414 /* full content */
Radek Krejci313d9902018-11-08 09:42:58 +01001415 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;}"
1416 "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 +01001417 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1418 assert_non_null(c);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001419 assert_int_equal(LYS_CONTAINER, c->nodetype);
1420 assert_string_equal("cont", c->name);
Radek Krejci8c370832018-11-02 15:10:03 +01001421 assert_non_null(c->actions);
1422 assert_non_null(c->child);
1423 assert_string_equal("test", c->dsc);
1424 assert_non_null(c->exts);
1425 assert_non_null(c->groupings);
1426 assert_non_null(c->iffeatures);
1427 assert_non_null(c->musts);
1428 assert_non_null(c->notifs);
1429 assert_string_equal("true", c->presence);
1430 assert_string_equal("test", c->ref);
1431 assert_non_null(c->typedefs);
1432 assert_non_null(c->when);
1433 assert_null(c->parent);
1434 assert_null(c->next);
1435 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags);
Radek Krejci313d9902018-11-08 09:42:58 +01001436 ly_set_erase(&ctx.tpdfs_nodes, NULL);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001437 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001438
1439 /* invalid */
1440 str = " cont {augment /root;} ...";
1441 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1442 logbuf_assert("Invalid keyword \"augment\" as a child of \"container\". Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001443 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001444 str = " cont {nonsence true;} ...";
1445 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1446 logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001447 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001448
Radek Krejcif538ce52019-03-05 10:46:14 +01001449 ctx.mod_version = 1; /* simulate YANG 1.0 */
1450 str = " cont {action x;} ...";
1451 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1452 logbuf_assert("Invalid keyword \"action\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1453 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
1454
Radek Krejci8c370832018-11-02 15:10:03 +01001455 ly_ctx_destroy(ctx.ctx, NULL);
1456}
1457
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001458static void
1459test_leaf(void **state)
1460{
1461 *state = test_leaf;
1462
Radek Krejcie7b95092019-05-15 11:03:07 +02001463 struct lys_parser_ctx ctx = {0};
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001464 struct lysp_node_leaf *l = NULL;
1465 const char *str;
1466
1467 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1468 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001469 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001470 ctx.line = 1;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001471 //ctx.mod->version = 2; /* simulate YANG 1.1 */
1472
1473 /* invalid cardinality */
1474#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1475 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1476 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1477 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1478 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1479
1480 TEST_DUP("config", "true", "false");
1481 TEST_DUP("default", "x", "y");
1482 TEST_DUP("description", "text1", "text2");
1483 TEST_DUP("mandatory", "true", "false");
1484 TEST_DUP("reference", "1", "2");
1485 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001486 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001487 TEST_DUP("units", "text1", "text2");
1488 TEST_DUP("when", "true", "false");
1489#undef TEST_DUP
1490
1491 /* full content - without mandatory which is mutual exclusive with default */
1492 str = "l {config false;default \"xxx\";description test;if-feature f;"
1493 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
1494 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1495 assert_non_null(l);
1496 assert_int_equal(LYS_LEAF, l->nodetype);
1497 assert_string_equal("l", l->name);
1498 assert_string_equal("test", l->dsc);
1499 assert_string_equal("xxx", l->dflt);
1500 assert_string_equal("yyy", l->units);
1501 assert_string_equal("string", l->type.name);
1502 assert_non_null(l->exts);
1503 assert_non_null(l->iffeatures);
1504 assert_non_null(l->musts);
1505 assert_string_equal("test", l->ref);
1506 assert_non_null(l->when);
1507 assert_null(l->parent);
1508 assert_null(l->next);
1509 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, l->flags);
1510 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1511
1512 /* full content - now with mandatory */
1513 str = "l {mandatory true; type string;} ...";
1514 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1515 assert_non_null(l);
1516 assert_int_equal(LYS_LEAF, l->nodetype);
1517 assert_string_equal("l", l->name);
1518 assert_string_equal("string", l->type.name);
1519 assert_int_equal(LYS_MAND_TRUE, l->flags);
1520 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1521
1522 /* invalid */
1523 str = " l {mandatory true; default xx; type string;} ...";
1524 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001525 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"leaf\". Line number 1.");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001526 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1527
1528 str = " l {description \"missing type\";} ...";
1529 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1530 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf\". Line number 1.");
1531 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1532
1533 *state = NULL;
1534 ly_ctx_destroy(ctx.ctx, NULL);
1535}
1536
Radek Krejci0e5d8382018-11-28 16:37:53 +01001537static void
1538test_leaflist(void **state)
1539{
1540 *state = test_leaf;
1541
Radek Krejcie7b95092019-05-15 11:03:07 +02001542 struct lys_parser_ctx ctx = {0};
Radek Krejci0e5d8382018-11-28 16:37:53 +01001543 struct lysp_node_leaflist *ll = NULL;
1544 const char *str;
1545
1546 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1547 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001548 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001549 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001550 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001551
1552 /* invalid cardinality */
1553#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1554 str = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1555 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); \
1556 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1557 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1558
1559 TEST_DUP("config", "true", "false");
1560 TEST_DUP("description", "text1", "text2");
1561 TEST_DUP("max-elements", "10", "20");
1562 TEST_DUP("min-elements", "10", "20");
1563 TEST_DUP("ordered-by", "user", "system");
1564 TEST_DUP("reference", "1", "2");
1565 TEST_DUP("status", "current", "obsolete");
1566 TEST_DUP("type", "int8", "uint8");
1567 TEST_DUP("units", "text1", "text2");
1568 TEST_DUP("when", "true", "false");
1569#undef TEST_DUP
1570
1571 /* full content - without min-elements which is mutual exclusive with default */
1572 str = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
1573 "max-elements 10;must 'expr';ordered-by user;reference test;"
1574 "status current;type string; units zzz;when true;m:ext;} ...";
1575 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1576 assert_non_null(ll);
1577 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1578 assert_string_equal("ll", ll->name);
1579 assert_string_equal("test", ll->dsc);
1580 assert_non_null(ll->dflts);
1581 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
1582 assert_string_equal("xxx", ll->dflts[0]);
1583 assert_string_equal("yyy", ll->dflts[1]);
1584 assert_string_equal("zzz", ll->units);
1585 assert_int_equal(10, ll->max);
1586 assert_int_equal(0, ll->min);
1587 assert_string_equal("string", ll->type.name);
1588 assert_non_null(ll->exts);
1589 assert_non_null(ll->iffeatures);
1590 assert_non_null(ll->musts);
1591 assert_string_equal("test", ll->ref);
1592 assert_non_null(ll->when);
1593 assert_null(ll->parent);
1594 assert_null(ll->next);
1595 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
1596 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1597
1598 /* full content - now with min-elements */
1599 str = "ll {min-elements 10; type string;} ...";
1600 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1601 assert_non_null(ll);
1602 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1603 assert_string_equal("ll", ll->name);
1604 assert_string_equal("string", ll->type.name);
1605 assert_int_equal(0, ll->max);
1606 assert_int_equal(10, ll->min);
1607 assert_int_equal(LYS_SET_MIN, ll->flags);
1608 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1609
1610 /* invalid */
1611 str = " ll {min-elements 1; default xx; type string;} ...";
1612 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001613 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 +01001614 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1615
1616 str = " ll {description \"missing type\";} ...";
1617 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1618 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf-list\". Line number 1.");
1619 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1620
Radek Krejcidf6cad12018-11-28 17:10:55 +01001621 str = " ll {type string; min-elements 10; max-elements 1;} ..."; /* invalid combination of min/max */
1622 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1623 logbuf_assert("Invalid combination of min-elements and max-elements: min value 10 is bigger than the max value 1. Line number 1.");
1624 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1625
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001626 ctx.mod_version = 1; /* simulate YANG 1.0 - default statement is not allowed */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001627 str = " ll {default xx; type string;} ...";
1628 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1629 logbuf_assert("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1630 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1631
1632 *state = NULL;
1633 ly_ctx_destroy(ctx.ctx, NULL);
1634}
1635
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001636static void
1637test_list(void **state)
1638{
1639 *state = test_list;
1640
Radek Krejcie7b95092019-05-15 11:03:07 +02001641 struct lys_parser_ctx ctx = {0};
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001642 struct lysp_node_list *l = NULL;
1643 const char *str;
1644
1645 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1646 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001647 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001648 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001649 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001650
1651 /* invalid cardinality */
1652#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1653 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1654 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1655 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1656 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1657
1658 TEST_DUP("config", "true", "false");
1659 TEST_DUP("description", "text1", "text2");
1660 TEST_DUP("key", "one", "two");
1661 TEST_DUP("max-elements", "10", "20");
1662 TEST_DUP("min-elements", "10", "20");
1663 TEST_DUP("ordered-by", "user", "system");
1664 TEST_DUP("reference", "1", "2");
1665 TEST_DUP("status", "current", "obsolete");
1666 TEST_DUP("when", "true", "false");
1667#undef TEST_DUP
1668
1669 /* full content */
1670 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;}"
1671 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1672 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
1673 assert_int_equal(LY_SUCCESS, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1674 assert_non_null(l);
1675 assert_int_equal(LYS_LIST, l->nodetype);
1676 assert_string_equal("l", l->name);
1677 assert_string_equal("test", l->dsc);
1678 assert_string_equal("l", l->key);
1679 assert_non_null(l->uniques);
1680 assert_int_equal(2, LY_ARRAY_SIZE(l->uniques));
1681 assert_string_equal("xxx", l->uniques[0]);
1682 assert_string_equal("yyy", l->uniques[1]);
1683 assert_int_equal(10, l->max);
1684 assert_int_equal(1, l->min);
1685 assert_non_null(l->exts);
1686 assert_non_null(l->iffeatures);
1687 assert_non_null(l->musts);
1688 assert_string_equal("test", l->ref);
1689 assert_non_null(l->when);
1690 assert_null(l->parent);
1691 assert_null(l->next);
1692 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, l->flags);
1693 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1694 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1695
Radek Krejcif538ce52019-03-05 10:46:14 +01001696 /* invalid content */
1697 ctx.mod_version = 1; /* simulate YANG 1.0 */
1698 str = "l {action x;} ...";
1699 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1700 logbuf_assert("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1701 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1702
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001703 *state = NULL;
1704 ly_ctx_destroy(ctx.ctx, NULL);
1705}
1706
Radek Krejci056d0a82018-12-06 16:57:25 +01001707static void
1708test_choice(void **state)
1709{
1710 *state = test_choice;
1711
Radek Krejcie7b95092019-05-15 11:03:07 +02001712 struct lys_parser_ctx ctx = {0};
Radek Krejci056d0a82018-12-06 16:57:25 +01001713 struct lysp_node_choice *ch = NULL;
1714 const char *str;
1715
1716 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1717 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001718 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci056d0a82018-12-06 16:57:25 +01001719 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001720 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci056d0a82018-12-06 16:57:25 +01001721
1722 /* invalid cardinality */
1723#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1724 str = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1725 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); \
1726 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1727 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1728
1729 TEST_DUP("config", "true", "false");
1730 TEST_DUP("default", "a", "b");
1731 TEST_DUP("description", "text1", "text2");
1732 TEST_DUP("mandatory", "true", "false");
1733 TEST_DUP("reference", "1", "2");
1734 TEST_DUP("status", "current", "obsolete");
1735 TEST_DUP("when", "true", "false");
1736#undef TEST_DUP
1737
Radek Krejcia9026eb2018-12-12 16:04:47 +01001738 /* full content - without default due to a collision with mandatory */
1739 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 +01001740 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
1741 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1742 assert_non_null(ch);
1743 assert_int_equal(LYS_CHOICE, ch->nodetype);
1744 assert_string_equal("ch", ch->name);
1745 assert_string_equal("test", ch->dsc);
1746 assert_non_null(ch->exts);
1747 assert_non_null(ch->iffeatures);
1748 assert_string_equal("test", ch->ref);
1749 assert_non_null(ch->when);
1750 assert_null(ch->parent);
1751 assert_null(ch->next);
1752 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, ch->flags);
1753 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1754
Radek Krejcia9026eb2018-12-12 16:04:47 +01001755 /* full content - the default missing from the previous node */
1756 str = "ch {default c;case c;} ...";
1757 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1758 assert_non_null(ch);
1759 assert_int_equal(LYS_CHOICE, ch->nodetype);
1760 assert_string_equal("ch", ch->name);
1761 assert_string_equal("c", ch->dflt);
1762 assert_int_equal(0, ch->flags);
1763 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1764
1765 /* invalid content */
1766 str = "ch {mandatory true; default c1; case c1 {leaf x{type string;}}} ...";
1767 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1768 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"choice\". Line number 1.");
1769 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1770
1771 *state = NULL;
1772 ly_ctx_destroy(ctx.ctx, NULL);
1773}
1774
1775static void
1776test_case(void **state)
1777{
1778 *state = test_case;
1779
Radek Krejcie7b95092019-05-15 11:03:07 +02001780 struct lys_parser_ctx ctx = {0};
Radek Krejcia9026eb2018-12-12 16:04:47 +01001781 struct lysp_node_case *cs = NULL;
1782 const char *str;
1783
1784 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1785 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001786 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001787 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001788 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcia9026eb2018-12-12 16:04:47 +01001789
1790 /* invalid cardinality */
1791#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1792 str = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1793 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs)); \
1794 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1795 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1796
1797 TEST_DUP("description", "text1", "text2");
1798 TEST_DUP("reference", "1", "2");
1799 TEST_DUP("status", "current", "obsolete");
1800 TEST_DUP("when", "true", "false");
1801#undef TEST_DUP
1802
1803 /* full content */
1804 str = "cs {anydata any;anyxml anyxml; choice ch;container c;description test;if-feature f;leaf l {type string;}"
1805 "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
1806 assert_int_equal(LY_SUCCESS, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1807 assert_non_null(cs);
1808 assert_int_equal(LYS_CASE, cs->nodetype);
1809 assert_string_equal("cs", cs->name);
1810 assert_string_equal("test", cs->dsc);
1811 assert_non_null(cs->exts);
1812 assert_non_null(cs->iffeatures);
1813 assert_string_equal("test", cs->ref);
1814 assert_non_null(cs->when);
1815 assert_null(cs->parent);
1816 assert_null(cs->next);
1817 assert_int_equal(LYS_STATUS_CURR, cs->flags);
1818 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1819
1820 /* invalid content */
1821 str = "cs {config true} ...";
1822 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1823 logbuf_assert("Invalid keyword \"config\" as a child of \"case\". Line number 1.");
1824 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1825
Radek Krejci056d0a82018-12-06 16:57:25 +01001826 *state = NULL;
1827 ly_ctx_destroy(ctx.ctx, NULL);
1828}
1829
Radek Krejci9800fb82018-12-13 14:26:23 +01001830static void
Radek Krejcid6b76452019-09-03 17:03:03 +02001831test_any(void **state, enum ly_stmt kw)
Radek Krejci9800fb82018-12-13 14:26:23 +01001832{
1833 *state = test_any;
1834
Radek Krejcie7b95092019-05-15 11:03:07 +02001835 struct lys_parser_ctx ctx = {0};
Radek Krejci9800fb82018-12-13 14:26:23 +01001836 struct lysp_node_anydata *any = NULL;
1837 const char *str;
1838
1839 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1840 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001841 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci9800fb82018-12-13 14:26:23 +01001842 ctx.line = 1;
Radek Krejcid6b76452019-09-03 17:03:03 +02001843 if (kw == LY_STMT_ANYDATA) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001844 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001845 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001846 ctx.mod_version = 1; /* simulate YANG 1.0 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001847 }
1848
1849 /* invalid cardinality */
1850#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1851 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1852 assert_int_equal(LY_EVALID, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any)); \
1853 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1854 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1855
1856 TEST_DUP("config", "true", "false");
1857 TEST_DUP("description", "text1", "text2");
1858 TEST_DUP("mandatory", "true", "false");
1859 TEST_DUP("reference", "1", "2");
1860 TEST_DUP("status", "current", "obsolete");
1861 TEST_DUP("when", "true", "false");
1862#undef TEST_DUP
1863
1864 /* full content */
1865 str = "any {config true;description test;if-feature f;mandatory true;must 'expr';reference test;status current;when true;m:ext;} ...";
1866 assert_int_equal(LY_SUCCESS, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any));
1867 assert_non_null(any);
Radek Krejcid6b76452019-09-03 17:03:03 +02001868 assert_int_equal(kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML, any->nodetype);
Radek Krejci9800fb82018-12-13 14:26:23 +01001869 assert_string_equal("any", any->name);
1870 assert_string_equal("test", any->dsc);
1871 assert_non_null(any->exts);
1872 assert_non_null(any->iffeatures);
1873 assert_non_null(any->musts);
1874 assert_string_equal("test", any->ref);
1875 assert_non_null(any->when);
1876 assert_null(any->parent);
1877 assert_null(any->next);
1878 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags);
1879 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1880
1881 *state = NULL;
1882 ly_ctx_destroy(ctx.ctx, NULL);
1883}
1884
1885static void
1886test_anydata(void **state)
1887{
Radek Krejcid6b76452019-09-03 17:03:03 +02001888 return test_any(state, LY_STMT_ANYDATA);
Radek Krejci9800fb82018-12-13 14:26:23 +01001889}
1890
1891static void
1892test_anyxml(void **state)
1893{
Radek Krejcid6b76452019-09-03 17:03:03 +02001894 return test_any(state, LY_STMT_ANYXML);
Radek Krejci9800fb82018-12-13 14:26:23 +01001895}
1896
Radek Krejcie86bf772018-12-14 11:39:53 +01001897static void
1898test_grouping(void **state)
1899{
1900 *state = test_grouping;
1901
Radek Krejcie7b95092019-05-15 11:03:07 +02001902 struct lys_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01001903 struct lysp_grp *grp = NULL;
1904 const char *str;
1905
1906 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1907 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001908 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcie86bf772018-12-14 11:39:53 +01001909 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001910 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001911
1912 /* invalid cardinality */
1913#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1914 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1915 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp)); \
1916 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1917 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1918
1919 TEST_DUP("description", "text1", "text2");
1920 TEST_DUP("reference", "1", "2");
1921 TEST_DUP("status", "current", "obsolete");
1922#undef TEST_DUP
1923
1924 /* full content */
1925 str = "grp {action x;anydata any;anyxml anyxml; choice ch;container c;description test;grouping g;leaf l {type string;}"
1926 "leaf-list ll {type string;} list li;notification not;reference test;status current;typedef t {type int8;}uses g;m:ext;} ...";
1927 assert_int_equal(LY_SUCCESS, parse_grouping(&ctx, &str, NULL, &grp));
1928 assert_non_null(grp);
1929 assert_int_equal(LYS_GROUPING, grp->nodetype);
1930 assert_string_equal("grp", grp->name);
1931 assert_string_equal("test", grp->dsc);
1932 assert_non_null(grp->exts);
1933 assert_string_equal("test", grp->ref);
1934 assert_null(grp->parent);
1935 assert_int_equal( LYS_STATUS_CURR, grp->flags);
1936 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1937 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1938
1939 /* invalid content */
1940 str = "grp {config true} ...";
1941 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1942 logbuf_assert("Invalid keyword \"config\" as a child of \"grouping\". Line number 1.");
1943 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1944
1945 str = "grp {must 'expr'} ...";
1946 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1947 logbuf_assert("Invalid keyword \"must\" as a child of \"grouping\". Line number 1.");
1948 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1949
1950 *state = NULL;
1951 ly_ctx_destroy(ctx.ctx, NULL);
1952}
1953
1954static void
Radek Krejcif538ce52019-03-05 10:46:14 +01001955test_action(void **state)
1956{
1957 *state = test_action;
1958
Radek Krejcie7b95092019-05-15 11:03:07 +02001959 struct lys_parser_ctx ctx = {0};
Radek Krejcif538ce52019-03-05 10:46:14 +01001960 struct lysp_action *rpcs = NULL;
1961 struct lysp_node_container *c = NULL;
1962 const char *str;
1963
1964 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1965 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001966 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcif538ce52019-03-05 10:46:14 +01001967 ctx.line = 1;
1968 ctx.mod_version = 2; /* simulate YANG 1.1 */
1969
1970 /* invalid cardinality */
1971#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1972 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1973 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs)); \
1974 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1975 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1976
1977 TEST_DUP("description", "text1", "text2");
1978 TEST_DUP("input", "", "");
1979 TEST_DUP("output", "", "");
1980 TEST_DUP("reference", "1", "2");
1981 TEST_DUP("status", "current", "obsolete");
1982#undef TEST_DUP
1983
1984 /* full content */
1985 str = "top;";
1986 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1987 str = "func {description test;grouping grp;if-feature f;reference test;status current;typedef mytype {type int8;} m:ext;"
1988 "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1989 " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}"
1990 "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1991 " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ...";
1992 assert_int_equal(LY_SUCCESS, parse_action(&ctx, &str, (struct lysp_node*)c, &rpcs));
1993 assert_non_null(rpcs);
1994 assert_int_equal(LYS_ACTION, rpcs->nodetype);
1995 assert_string_equal("func", rpcs->name);
1996 assert_string_equal("test", rpcs->dsc);
1997 assert_non_null(rpcs->exts);
1998 assert_non_null(rpcs->iffeatures);
1999 assert_string_equal("test", rpcs->ref);
2000 assert_non_null(rpcs->groupings);
2001 assert_non_null(rpcs->typedefs);
2002 assert_int_equal(LYS_STATUS_CURR, rpcs->flags);
2003 /* input */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002004 assert_int_equal(rpcs->input.nodetype, LYS_INPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01002005 assert_non_null(rpcs->input.groupings);
2006 assert_non_null(rpcs->input.exts);
2007 assert_non_null(rpcs->input.musts);
2008 assert_non_null(rpcs->input.typedefs);
2009 assert_non_null(rpcs->input.data);
2010 /* output */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002011 assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01002012 assert_non_null(rpcs->output.groupings);
2013 assert_non_null(rpcs->output.exts);
2014 assert_non_null(rpcs->output.musts);
2015 assert_non_null(rpcs->output.typedefs);
2016 assert_non_null(rpcs->output.data);
2017
2018 ly_set_erase(&ctx.tpdfs_nodes, NULL);
2019 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
2020
2021 /* invalid content */
2022 str = "func {config true} ...";
2023 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs));
2024 logbuf_assert("Invalid keyword \"config\" as a child of \"rpc\". Line number 1.");
2025 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
2026
2027 *state = NULL;
2028 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
2029 ly_ctx_destroy(ctx.ctx, NULL);
2030}
2031
2032static void
Radek Krejcifc11bd72019-04-11 16:00:05 +02002033test_notification(void **state)
2034{
2035 *state = test_notification;
2036
Radek Krejcie7b95092019-05-15 11:03:07 +02002037 struct lys_parser_ctx ctx = {0};
Radek Krejcifc11bd72019-04-11 16:00:05 +02002038 struct lysp_notif *notifs = NULL;
2039 struct lysp_node_container *c = NULL;
2040 const char *str;
2041
2042 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2043 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002044 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcifc11bd72019-04-11 16:00:05 +02002045 ctx.line = 1;
2046 ctx.mod_version = 2; /* simulate YANG 1.1 */
2047
2048 /* invalid cardinality */
2049#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2050 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2051 assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, &notifs)); \
2052 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2053 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2054
2055 TEST_DUP("description", "text1", "text2");
2056 TEST_DUP("reference", "1", "2");
2057 TEST_DUP("status", "current", "obsolete");
2058#undef TEST_DUP
2059
2060 /* full content */
2061 str = "top;";
2062 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
2063 str = "ntf {anydata a1; anyxml a2; choice ch; container c; description test; grouping grp; if-feature f; leaf l {type int8;}"
2064 "leaf-list ll {type int8;} list li; must 1; reference test; status current; typedef mytype {type int8;} uses grp; m:ext;}";
2065 assert_int_equal(LY_SUCCESS, parse_notif(&ctx, &str, (struct lysp_node*)c, &notifs));
2066 assert_non_null(notifs);
2067 assert_int_equal(LYS_NOTIF, notifs->nodetype);
2068 assert_string_equal("ntf", notifs->name);
2069 assert_string_equal("test", notifs->dsc);
2070 assert_non_null(notifs->exts);
2071 assert_non_null(notifs->iffeatures);
2072 assert_string_equal("test", notifs->ref);
2073 assert_non_null(notifs->groupings);
2074 assert_non_null(notifs->typedefs);
2075 assert_non_null(notifs->musts);
2076 assert_non_null(notifs->data);
2077 assert_int_equal(LYS_STATUS_CURR, notifs->flags);
2078
2079 ly_set_erase(&ctx.tpdfs_nodes, NULL);
2080 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2081
2082 /* invalid content */
2083 str = "ntf {config true} ...";
2084 assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, &notifs));
2085 logbuf_assert("Invalid keyword \"config\" as a child of \"notification\". Line number 1.");
2086 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2087
2088 *state = NULL;
2089 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
2090 ly_ctx_destroy(ctx.ctx, NULL);
2091}
2092
2093static void
Radek Krejcie86bf772018-12-14 11:39:53 +01002094test_uses(void **state)
2095{
2096 *state = test_uses;
2097
Radek Krejcie7b95092019-05-15 11:03:07 +02002098 struct lys_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01002099 struct lysp_node_uses *u = NULL;
2100 const char *str;
2101
2102 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2103 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002104 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcie86bf772018-12-14 11:39:53 +01002105 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002106 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01002107
2108 /* invalid cardinality */
2109#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2110 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2111 assert_int_equal(LY_EVALID, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u)); \
2112 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2113 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2114
2115 TEST_DUP("description", "text1", "text2");
2116 TEST_DUP("reference", "1", "2");
2117 TEST_DUP("status", "current", "obsolete");
2118 TEST_DUP("when", "true", "false");
2119#undef TEST_DUP
2120
2121 /* full content */
2122 str = "grpref {augment some/node;description test;if-feature f;reference test;refine some/other/node;status current;when true;m:ext;} ...";
2123 assert_int_equal(LY_SUCCESS, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u));
2124 assert_non_null(u);
2125 assert_int_equal(LYS_USES, u->nodetype);
2126 assert_string_equal("grpref", u->name);
2127 assert_string_equal("test", u->dsc);
2128 assert_non_null(u->exts);
2129 assert_non_null(u->iffeatures);
2130 assert_string_equal("test", u->ref);
2131 assert_non_null(u->augments);
2132 assert_non_null(u->refines);
2133 assert_non_null(u->when);
2134 assert_null(u->parent);
2135 assert_null(u->next);
2136 assert_int_equal(LYS_STATUS_CURR, u->flags);
2137 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2138
2139 *state = NULL;
2140 ly_ctx_destroy(ctx.ctx, NULL);
2141}
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002142
2143
2144static void
2145test_augment(void **state)
2146{
2147 *state = test_augment;
2148
Radek Krejcie7b95092019-05-15 11:03:07 +02002149 struct lys_parser_ctx ctx = {0};
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002150 struct lysp_augment *a = NULL;
2151 const char *str;
2152
2153 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2154 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002155 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002156 ctx.line = 1;
Radek Krejcib4adbf12019-02-25 13:35:22 +01002157 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002158
2159 /* invalid cardinality */
2160#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2161 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2162 assert_int_equal(LY_EVALID, parse_augment(&ctx, &str, NULL, &a)); \
2163 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejcib4adbf12019-02-25 13:35:22 +01002164 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002165
2166 TEST_DUP("description", "text1", "text2");
2167 TEST_DUP("reference", "1", "2");
2168 TEST_DUP("status", "current", "obsolete");
2169 TEST_DUP("when", "true", "false");
2170#undef TEST_DUP
2171
2172 /* full content */
2173 str = "/target/nodeid {action x; anydata any;anyxml anyxml; case cs; choice ch;container c;description test;if-feature f;leaf l {type string;}"
2174 "leaf-list ll {type string;} list li;notification not;reference test;status current;uses g;when true;m:ext;} ...";
2175 assert_int_equal(LY_SUCCESS, parse_augment(&ctx, &str, NULL, &a));
2176 assert_non_null(a);
2177 assert_int_equal(LYS_AUGMENT, a->nodetype);
2178 assert_string_equal("/target/nodeid", a->nodeid);
2179 assert_string_equal("test", a->dsc);
2180 assert_non_null(a->exts);
2181 assert_non_null(a->iffeatures);
2182 assert_string_equal("test", a->ref);
2183 assert_non_null(a->when);
2184 assert_null(a->parent);
2185 assert_int_equal(LYS_STATUS_CURR, a->flags);
Radek Krejcib4adbf12019-02-25 13:35:22 +01002186 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002187
2188 *state = NULL;
2189 ly_ctx_destroy(ctx.ctx, NULL);
2190}
2191
Radek Krejcif09e4e82019-06-14 15:08:11 +02002192static void
2193test_when(void **state)
2194{
2195 *state = test_when;
2196
2197 struct lys_parser_ctx ctx = {0};
2198 struct lysp_when *w = NULL;
2199 const char *str;
2200
2201 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2202 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002203 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcif09e4e82019-06-14 15:08:11 +02002204 ctx.line = 1;
2205 ctx.mod_version = 2; /* simulate YANG 1.1 */
2206
2207 /* invalid cardinality */
2208#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2209 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2210 assert_int_equal(LY_EVALID, parse_when(&ctx, &str, &w)); \
2211 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2212 FREE_MEMBER(ctx.ctx, w, lysp_when_free); w = NULL;
2213
2214 TEST_DUP("description", "text1", "text2");
2215 TEST_DUP("reference", "1", "2");
2216#undef TEST_DUP
2217
2218 /* full content */
2219 str = "expression {description test;reference test;m:ext;} ...";
2220 assert_int_equal(LY_SUCCESS, parse_when(&ctx, &str, &w));
2221 assert_non_null(w);
2222 assert_string_equal("expression", w->cond);
2223 assert_string_equal("test", w->dsc);
2224 assert_string_equal("test", w->ref);
2225 assert_non_null(w->exts);
2226 FREE_MEMBER(ctx.ctx, w, lysp_when_free); w = NULL;
2227
2228 /* empty condition */
2229 str = "\"\";";
2230 assert_int_equal(LY_SUCCESS, parse_when(&ctx, &str, &w));
2231 logbuf_assert("Empty argument of when statement does not make sense.");
2232 assert_non_null(w);
2233 assert_string_equal("", w->cond);
2234 FREE_MEMBER(ctx.ctx, w, lysp_when_free); w = NULL;
2235
2236 *state = NULL;
2237 ly_ctx_destroy(ctx.ctx, NULL);
2238}
2239
David Sedlákd6ce6d72019-07-16 17:30:18 +02002240static void
2241test_value(void **state)
2242{
2243 *state = test_value;
2244 struct lys_parser_ctx ctx;
2245
2246 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2247 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002248 ctx.pos_type = LY_VLOG_LINE;
David Sedlákd6ce6d72019-07-16 17:30:18 +02002249 ctx.line = 1;
2250 ctx.indent = 0;
2251 int64_t val = 0;
2252 uint16_t flags = 0;
2253
2254 const char *data = "-0;";
Radek Krejcid6b76452019-09-03 17:03:03 +02002255 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 +02002256 assert_int_equal(val, 0);
2257
2258 data = "-0;";
2259 flags = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02002260 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 +02002261 logbuf_assert("Invalid value \"-0\" of \"position\". Line number 1.");
2262
David Sedlák1af868e2019-07-17 17:03:14 +02002263 *state = NULL;
David Sedlákd6ce6d72019-07-16 17:30:18 +02002264 ly_ctx_destroy(ctx.ctx, NULL);
2265}
2266
Radek Krejci80dd33e2018-09-26 15:57:18 +02002267int main(void)
2268{
2269 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002270 cmocka_unit_test_setup(test_helpers, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002271 cmocka_unit_test_setup(test_comments, logger_setup),
Radek Krejciefd22f62018-09-27 11:47:58 +02002272 cmocka_unit_test_setup(test_arg, logger_setup),
Radek Krejcidcc7b322018-10-11 14:24:02 +02002273 cmocka_unit_test_setup(test_stmts, logger_setup),
Radek Krejci05b13982018-11-28 16:22:07 +01002274 cmocka_unit_test_setup_teardown(test_minmax, logger_setup, logger_teardown),
Radek Krejci40544fa2019-01-11 09:38:37 +01002275 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002276 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci2c02f3e2018-10-16 10:54:38 +02002277 cmocka_unit_test_setup(test_feature, logger_setup),
2278 cmocka_unit_test_setup(test_deviation, logger_setup),
2279 cmocka_unit_test_setup(test_deviate, logger_setup),
Radek Krejci8c370832018-11-02 15:10:03 +01002280 cmocka_unit_test_setup(test_container, logger_setup),
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002281 cmocka_unit_test_setup_teardown(test_leaf, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01002282 cmocka_unit_test_setup_teardown(test_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002283 cmocka_unit_test_setup_teardown(test_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01002284 cmocka_unit_test_setup_teardown(test_choice, logger_setup, logger_teardown),
Radek Krejcia9026eb2018-12-12 16:04:47 +01002285 cmocka_unit_test_setup_teardown(test_case, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01002286 cmocka_unit_test_setup_teardown(test_anydata, logger_setup, logger_teardown),
2287 cmocka_unit_test_setup_teardown(test_anyxml, logger_setup, logger_teardown),
Radek Krejcif538ce52019-03-05 10:46:14 +01002288 cmocka_unit_test_setup_teardown(test_action, logger_setup, logger_teardown),
Radek Krejcifc11bd72019-04-11 16:00:05 +02002289 cmocka_unit_test_setup_teardown(test_notification, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01002290 cmocka_unit_test_setup_teardown(test_grouping, logger_setup, logger_teardown),
2291 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejcib4adbf12019-02-25 13:35:22 +01002292 cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown),
Radek Krejcif09e4e82019-06-14 15:08:11 +02002293 cmocka_unit_test_setup_teardown(test_when, logger_setup, logger_teardown),
David Sedlákd6ce6d72019-07-16 17:30:18 +02002294 cmocka_unit_test_setup_teardown(test_value, logger_setup, logger_teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002295 };
2296
2297 return cmocka_run_group_tests(tests, NULL, NULL);
2298}