blob: cb9e058d2e16bc9215f951a9452fe2b5a2b6109c [file] [log] [blame]
Radek Krejci80dd33e2018-09-26 15:57:18 +02001/*
2 * @file test_parser_yang.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from parser_yang.c
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci80dd33e2018-09-26 15:57:18 +020015#include <stdarg.h>
16#include <stddef.h>
17#include <setjmp.h>
18#include <cmocka.h>
19
20#include <stdio.h>
21#include <string.h>
22
Radek Krejci2d7a47b2019-05-16 13:34:10 +020023#include "../../src/common.h"
24#include "../../src/tree_schema.h"
25#include "../../src/tree_schema_internal.h"
26
27/* originally static functions from tree_schema_free.c and parser_yang.c */
28void lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext);
29void lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident);
30void lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat);
31void lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev);
32void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp);
33void lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action);
34void lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif);
35void lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment);
36void lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d);
37void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
Radek Krejcif09e4e82019-06-14 15:08:11 +020038void lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020039
40LY_ERR buf_add_char(struct ly_ctx *ctx, const char **input, size_t len, char **buf, size_t *buf_len, size_t *buf_used);
David Sedlák40bb13b2019-07-10 14:34:18 +020041LY_ERR buf_store_char(struct lys_parser_ctx *ctx, const char **input, enum yang_arg arg, char **word_p,
42 size_t *word_len, char **word_b, size_t *buf_len, int need_buf, int *prefix);
Radek Krejcid6b76452019-09-03 17:03:03 +020043LY_ERR get_keyword(struct lys_parser_ctx *ctx, const char **data, enum ly_stmt *kw, char **word_p, size_t *word_len);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020044LY_ERR get_argument(struct lys_parser_ctx *ctx, const char **data, enum yang_arg arg,
45 uint16_t *flags, char **word_p, char **word_b, size_t *word_len);
46LY_ERR skip_comment(struct lys_parser_ctx *ctx, const char **data, int comment);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020047
48LY_ERR parse_action(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions);
Radek Krejcid6b76452019-09-03 17:03:03 +020049LY_ERR parse_any(struct lys_parser_ctx *ctx, const char **data, enum ly_stmt kw, struct lysp_node *parent, struct lysp_node **siblings);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020050LY_ERR parse_augment(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_augment **augments);
51LY_ERR parse_case(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
52LY_ERR parse_container(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
53LY_ERR parse_deviate(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates);
54LY_ERR parse_deviation(struct lys_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations);
55LY_ERR parse_feature(struct lys_parser_ctx *ctx, const char **data, struct lysp_feature **features);
56LY_ERR parse_grouping(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings);
57LY_ERR parse_choice(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
58LY_ERR parse_identity(struct lys_parser_ctx *ctx, const char **data, struct lysp_ident **identities);
59LY_ERR parse_leaf(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
60LY_ERR parse_leaflist(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
61LY_ERR parse_list(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
62LY_ERR parse_maxelements(struct lys_parser_ctx *ctx, const char **data, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts);
63LY_ERR parse_minelements(struct lys_parser_ctx *ctx, const char **data, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts);
64LY_ERR parse_module(struct lys_parser_ctx *ctx, const char **data, struct lysp_module *mod);
65LY_ERR parse_notif(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_notif **notifs);
66LY_ERR parse_submodule(struct lys_parser_ctx *ctx, const char **data, struct lysp_submodule *submod);
67LY_ERR parse_uses(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
Radek Krejcif09e4e82019-06-14 15:08:11 +020068LY_ERR parse_when(struct lys_parser_ctx *ctx, const char **data, struct lysp_when **when_p);
Radek Krejcid6b76452019-09-03 17:03:03 +020069LY_ERR parse_type_enum_value_pos(struct lys_parser_ctx *ctx, const char **data, enum ly_stmt val_kw, int64_t *value, uint16_t *flags, struct lysp_ext_instance **exts);
Radek Krejci80dd33e2018-09-26 15:57:18 +020070
71#define BUFSIZE 1024
72char logbuf[BUFSIZE] = {0};
Radek Krejci9ed7a192018-10-31 16:23:51 +010073int store = -1; /* negative for infinite logging, positive for limited logging */
Radek Krejci80dd33e2018-09-26 15:57:18 +020074
75/* set to 0 to printing error messages to stderr instead of checking them in code */
Radek Krejci70853c52018-10-15 14:46:16 +020076#define ENABLE_LOGGER_CHECKING 1
Radek Krejci80dd33e2018-09-26 15:57:18 +020077
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020078#if ENABLE_LOGGER_CHECKING
Radek Krejci80dd33e2018-09-26 15:57:18 +020079static void
80logger(LY_LOG_LEVEL level, const char *msg, const char *path)
81{
82 (void) level; /* unused */
Radek Krejci9ed7a192018-10-31 16:23:51 +010083 if (store) {
84 if (path && path[0]) {
85 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
86 } else {
87 strncpy(logbuf, msg, BUFSIZE - 1);
88 }
89 if (store > 0) {
90 --store;
91 }
Radek Krejci80dd33e2018-09-26 15:57:18 +020092 }
93}
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020094#endif
Radek Krejci80dd33e2018-09-26 15:57:18 +020095
96static int
97logger_setup(void **state)
98{
99 (void) state; /* unused */
100#if ENABLE_LOGGER_CHECKING
101 ly_set_log_clb(logger, 1);
102#endif
103 return 0;
104}
105
Radek Krejcib1a5dcc2018-11-26 14:50:05 +0100106static int
107logger_teardown(void **state)
108{
109 (void) state; /* unused */
110#if ENABLE_LOGGER_CHECKING
111 if (*state) {
112 fprintf(stderr, "%s\n", logbuf);
113 }
114#endif
115 return 0;
116}
117
Radek Krejci80dd33e2018-09-26 15:57:18 +0200118void
119logbuf_clean(void)
120{
121 logbuf[0] = '\0';
122}
123
124#if ENABLE_LOGGER_CHECKING
125# define logbuf_assert(str) assert_string_equal(logbuf, str)
126#else
127# define logbuf_assert(str)
128#endif
129
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200130#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
131 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
132 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
133 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
134 CLEANUP
135
Radek Krejci44ceedc2018-10-02 15:54:31 +0200136static void
137test_helpers(void **state)
138{
139 (void) state; /* unused */
140
141 const char *str;
Radek Krejci404251e2018-10-09 12:06:44 +0200142 char *buf, *p;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200143 size_t len, size;
Radek Krejcie7b95092019-05-15 11:03:07 +0200144 struct lys_parser_ctx ctx;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200145 ctx.ctx = NULL;
Radek Krejci99435242019-09-05 16:19:15 +0200146 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200147 ctx.line = 1;
David Sedlák40bb13b2019-07-10 14:34:18 +0200148 int prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200149
150 /* storing into buffer */
151 str = "abcd";
152 buf = NULL;
153 size = len = 0;
154 assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &str, 2, &buf, &size, &len));
155 assert_int_not_equal(0, size);
156 assert_int_equal(2, len);
157 assert_string_equal("cd", str);
158 assert_false(strncmp("ab", buf, 2));
159 free(buf);
Radek Krejci404251e2018-10-09 12:06:44 +0200160 buf = NULL;
161
162 /* invalid first characters */
163 len = 0;
164 str = "2invalid";
David Sedlák40bb13b2019-07-10 14:34:18 +0200165 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200166 str = ".invalid";
David Sedlák40bb13b2019-07-10 14:34:18 +0200167 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200168 str = "-invalid";
David Sedlák40bb13b2019-07-10 14:34:18 +0200169 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200170 /* invalid following characters */
171 len = 3; /* number of characters read before the str content */
172 str = "!";
David Sedlák40bb13b2019-07-10 14:34:18 +0200173 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200174 str = ":";
David Sedlák40bb13b2019-07-10 14:34:18 +0200175 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200176 /* valid colon for prefixed identifiers */
177 len = size = 0;
178 p = NULL;
David Sedlák40bb13b2019-07-10 14:34:18 +0200179 prefix = 0;
Radek Krejci404251e2018-10-09 12:06:44 +0200180 str = "x:id";
David Sedlák40bb13b2019-07-10 14:34:18 +0200181 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 0, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200182 assert_int_equal(1, len);
183 assert_null(buf);
184 assert_string_equal(":id", str);
185 assert_int_equal('x', p[len - 1]);
David Sedlák40bb13b2019-07-10 14:34:18 +0200186 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200187 assert_int_equal(2, len);
188 assert_string_equal("id", str);
189 assert_int_equal(':', p[len - 1]);
190 free(buf);
David Sedlák40bb13b2019-07-10 14:34:18 +0200191 prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200192
193 /* checking identifiers */
David Sedlák4ccd7c32019-07-10 12:02:04 +0200194 assert_int_equal(LY_EVALID, lysp_check_identifierchar(&ctx, ':', 0, NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200195 logbuf_assert("Invalid identifier character ':'. Line number 1.");
David Sedlák4ccd7c32019-07-10 12:02:04 +0200196 assert_int_equal(LY_EVALID, lysp_check_identifierchar(&ctx, '#', 1, NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200197 logbuf_assert("Invalid identifier first character '#'. Line number 1.");
198
David Sedlák4ccd7c32019-07-10 12:02:04 +0200199 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar(&ctx, 'a', 1, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200200 assert_int_equal(0, prefix);
David Sedlák4ccd7c32019-07-10 12:02:04 +0200201 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar(&ctx, ':', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200202 assert_int_equal(1, prefix);
David Sedlák4ccd7c32019-07-10 12:02:04 +0200203 assert_int_equal(LY_EVALID, lysp_check_identifierchar(&ctx, ':', 0, &prefix));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200204 assert_int_equal(1, prefix);
David Sedlák4ccd7c32019-07-10 12:02:04 +0200205 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar(&ctx, 'b', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200206 assert_int_equal(2, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200207 /* second colon is invalid */
David Sedlák4ccd7c32019-07-10 12:02:04 +0200208 assert_int_equal(LY_EVALID, lysp_check_identifierchar(&ctx, ':', 0, &prefix));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200209 logbuf_assert("Invalid identifier character ':'. Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200210}
Radek Krejci80dd33e2018-09-26 15:57:18 +0200211
212static void
213test_comments(void **state)
214{
215 (void) state; /* unused */
216
Radek Krejcie7b95092019-05-15 11:03:07 +0200217 struct lys_parser_ctx ctx;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200218 const char *str, *p;
Radek Krejciefd22f62018-09-27 11:47:58 +0200219 char *word, *buf;
220 size_t len;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200221
Radek Krejci44ceedc2018-10-02 15:54:31 +0200222 ctx.ctx = NULL;
Radek Krejci99435242019-09-05 16:19:15 +0200223 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200224 ctx.line = 1;
225
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200226 str = " // this is a text of / one * line */ comment\nargument;";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200227 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200228 assert_string_equal("argument;", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200229 assert_null(buf);
230 assert_int_equal(8, len);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200231
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200232 str = "/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200233 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200234 assert_string_equal("argument", word);
235 assert_ptr_equal(buf, word);
236 assert_int_equal(8, len);
237 free(word);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200238
239 str = p = " this is one line comment on last line";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200240 assert_int_equal(LY_SUCCESS, skip_comment(&ctx, &str, 1));
Radek Krejci80dd33e2018-09-26 15:57:18 +0200241 assert_true(str[0] == '\0');
242
243 str = p = " this is a not terminated comment x";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200244 assert_int_equal(LY_EVALID, skip_comment(&ctx, &str, 2));
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200245 logbuf_assert("Unexpected end-of-input, non-terminated comment. Line number 5.");
Radek Krejci80dd33e2018-09-26 15:57:18 +0200246 assert_true(str[0] == '\0');
247}
248
Radek Krejciefd22f62018-09-27 11:47:58 +0200249static void
250test_arg(void **state)
251{
252 (void) state; /* unused */
253
Radek Krejcie7b95092019-05-15 11:03:07 +0200254 struct lys_parser_ctx ctx;
Radek Krejciefd22f62018-09-27 11:47:58 +0200255 const char *str;
256 char *word, *buf;
257 size_t len;
258
Radek Krejci44ceedc2018-10-02 15:54:31 +0200259 ctx.ctx = NULL;
Radek Krejci99435242019-09-05 16:19:15 +0200260 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200261 ctx.line = 1;
262
Radek Krejciefd22f62018-09-27 11:47:58 +0200263 /* missing argument */
264 str = ";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200265 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200266 assert_null(word);
267
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200268 str = "{";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200269 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200270 logbuf_assert("Invalid character sequence \"{\", expected an argument. Line number 1.");
271
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200272 /* invalid escape sequence */
273 str = "\"\\s\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200274 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200275 logbuf_assert("Double-quoted string unknown special character \'\\s\'. Line number 1.");
276 str = "\'\\s\'"; /* valid, since it is not an escape sequence in single quoted string */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200277 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200278 assert_int_equal(2, len);
279 assert_string_equal("\\s\'", word);
280 assert_int_equal('\0', str[0]); /* input has been eaten */
281
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200282 /* invalid character after the argument */
283 str = "hello\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200284 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200285 logbuf_assert("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
286 str = "hello}";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200287 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200288 logbuf_assert("Invalid character sequence \"}\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
289
David Sedlák40bb13b2019-07-10 14:34:18 +0200290 /* invalid identifier-ref-arg-str */
291 str = "pre:pre:value";
292 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
293
Radek Krejci4e199f52019-05-28 09:09:28 +0200294 str = "\"\";"; /* empty identifier is not allowed */
295 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_IDENTIF_ARG, NULL, &word, &buf, &len));
296 logbuf_assert("Statement argument is required. Line number 1.");
297 logbuf_clean();
298 str = "\"\";"; /* empty reference identifier is not allowed */
299 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
300 logbuf_assert("Statement argument is required. Line number 1.");
301
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200302 str = "hello/x\t"; /* slash is not an invalid character */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200303 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200304 assert_int_equal(7, len);
305 assert_string_equal("hello/x\t", word);
306
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200307 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200308
309 /* different quoting */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200310 str = "hello ";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200311 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200312 assert_null(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200313 assert_int_equal(5, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200314 assert_string_equal("hello ", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200315
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200316 str = "hello/*comment*/\n";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200317 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200318 assert_null(buf);
319 assert_int_equal(5, len);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200320 assert_false(strncmp("hello", word, len));
321
322
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200323 str = "\"hello\\n\\t\\\"\\\\\";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200324 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
fredgand49fe112019-10-21 20:51:50 +0800325 assert_non_null(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200326 assert_int_equal(9, len);
fredgand49fe112019-10-21 20:51:50 +0800327 assert_string_equal("hello\n\t\"\\", word);
328 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200329
330 ctx.indent = 14;
331 str = "\"hello \t\n\t\t world!\"";
332 /* - space and tabs before newline are stripped out
333 * - space and tabs after newline (indentation) are stripped out
334 */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200335 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200336 assert_non_null(buf);
337 assert_ptr_equal(word, buf);
338 assert_int_equal(14, len);
339 assert_string_equal("hello\n world!", word);
340 free(buf);
341
342 ctx.indent = 14;
343 str = "\"hello\n \tworld!\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200344 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200345 assert_non_null(buf);
346 assert_ptr_equal(word, buf);
347 assert_int_equal(12, len);
348 assert_string_equal("hello\nworld!", word);
349 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200350
351 str = "\'hello\'";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200352 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200353 assert_null(buf);
354 assert_int_equal(5, len);
355 assert_false(strncmp("hello", word, 5));
356
357 str = "\"hel\" +\t\n\"lo\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200358 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200359 assert_ptr_equal(word, buf);
360 assert_int_equal(5, len);
361 assert_string_equal("hello", word);
362 free(buf);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200363 str = "\"hel\" +\t\nlo"; /* unquoted the second part */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200364 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200365 logbuf_assert("Both string parts divided by '+' must be quoted. Line number 5.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200366
367 str = "\'he\'\t\n+ \"llo\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200368 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200369 assert_ptr_equal(word, buf);
370 assert_int_equal(5, len);
371 assert_string_equal("hello", word);
372 free(buf);
373
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200374 str = " \t\n\"he\"+\'llo\'";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200375 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200376 assert_ptr_equal(word, buf);
377 assert_int_equal(5, len);
378 assert_string_equal("hello", word);
379 free(buf);
380
Radek Krejci44ceedc2018-10-02 15:54:31 +0200381 /* missing argument */
382 str = ";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200383 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200384 logbuf_assert("Invalid character sequence \";\", expected an argument. Line number 7.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200385}
386
387static void
388test_stmts(void **state)
389{
390 (void) state; /* unused */
391
Radek Krejcie7b95092019-05-15 11:03:07 +0200392 struct lys_parser_ctx ctx;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200393 const char *str, *p;
Radek Krejcid6b76452019-09-03 17:03:03 +0200394 enum ly_stmt kw;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200395 char *word;
396 size_t len;
397
398 ctx.ctx = NULL;
Radek Krejci99435242019-09-05 16:19:15 +0200399 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200400 ctx.line = 1;
401
402 str = "\n// comment\n\tinput\t{";
403 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200404 assert_int_equal(LY_STMT_INPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200405 assert_int_equal(5, len);
406 assert_string_equal("input\t{", word);
407 assert_string_equal("\t{", str);
408
409 str = "\t /* comment */\t output\n\t{";
410 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200411 assert_int_equal(LY_STMT_OUTPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200412 assert_int_equal(6, len);
413 assert_string_equal("output\n\t{", word);
414 assert_string_equal("\n\t{", str);
Radek Krejciabdd8062019-06-11 16:44:19 +0200415 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200416 assert_int_equal(LY_STMT_SYNTAX_LEFT_BRACE, kw);
Radek Krejciabdd8062019-06-11 16:44:19 +0200417 assert_int_equal(1, len);
418 assert_string_equal("{", word);
419 assert_string_equal("", str);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200420
421 str = "/input { "; /* invalid slash */
422 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
423 logbuf_assert("Invalid identifier first character '/'. Line number 4.");
424
425 str = "not-a-statement-nor-extension { "; /* invalid identifier */
426 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
427 logbuf_assert("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword. Line number 4.");
428
429 str = "path;"; /* missing sep after the keyword */
430 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
431 logbuf_assert("Invalid character sequence \"path;\", expected a keyword followed by a separator. Line number 4.");
432
433 str = "action ";
434 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200435 assert_int_equal(LY_STMT_ACTION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200436 assert_int_equal(6, len);
437 str = "anydata ";
438 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200439 assert_int_equal(LY_STMT_ANYDATA, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200440 assert_int_equal(7, len);
441 str = "anyxml ";
442 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200443 assert_int_equal(LY_STMT_ANYXML, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200444 assert_int_equal(6, len);
445 str = "argument ";
446 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200447 assert_int_equal(LY_STMT_ARGUMENT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200448 assert_int_equal(8, len);
449 str = "augment ";
450 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200451 assert_int_equal(LY_STMT_AUGMENT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200452 assert_int_equal(7, len);
453 str = "base ";
454 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200455 assert_int_equal(LY_STMT_BASE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200456 assert_int_equal(4, len);
457 str = "belongs-to ";
458 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200459 assert_int_equal(LY_STMT_BELONGS_TO, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200460 assert_int_equal(10, len);
461 str = "bit ";
462 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200463 assert_int_equal(LY_STMT_BIT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200464 assert_int_equal(3, len);
465 str = "case ";
466 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200467 assert_int_equal(LY_STMT_CASE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200468 assert_int_equal(4, len);
469 str = "choice ";
470 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200471 assert_int_equal(LY_STMT_CHOICE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200472 assert_int_equal(6, len);
473 str = "config ";
474 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200475 assert_int_equal(LY_STMT_CONFIG, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200476 assert_int_equal(6, len);
477 str = "contact ";
478 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200479 assert_int_equal(LY_STMT_CONTACT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200480 assert_int_equal(7, len);
481 str = "container ";
482 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200483 assert_int_equal(LY_STMT_CONTAINER, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200484 assert_int_equal(9, len);
485 str = "default ";
486 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200487 assert_int_equal(LY_STMT_DEFAULT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200488 assert_int_equal(7, len);
489 str = "description ";
490 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200491 assert_int_equal(LY_STMT_DESCRIPTION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200492 assert_int_equal(11, len);
493 str = "deviate ";
494 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200495 assert_int_equal(LY_STMT_DEVIATE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200496 assert_int_equal(7, len);
497 str = "deviation ";
498 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200499 assert_int_equal(LY_STMT_DEVIATION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200500 assert_int_equal(9, len);
501 str = "enum ";
502 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200503 assert_int_equal(LY_STMT_ENUM, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200504 assert_int_equal(4, len);
505 str = "error-app-tag ";
506 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200507 assert_int_equal(LY_STMT_ERROR_APP_TAG, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200508 assert_int_equal(13, len);
509 str = "error-message ";
510 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200511 assert_int_equal(LY_STMT_ERROR_MESSAGE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200512 assert_int_equal(13, len);
513 str = "extension ";
514 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200515 assert_int_equal(LY_STMT_EXTENSION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200516 assert_int_equal(9, len);
517 str = "feature ";
518 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200519 assert_int_equal(LY_STMT_FEATURE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200520 assert_int_equal(7, len);
521 str = "fraction-digits ";
522 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200523 assert_int_equal(LY_STMT_FRACTION_DIGITS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200524 assert_int_equal(15, len);
525 str = "grouping ";
526 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200527 assert_int_equal(LY_STMT_GROUPING, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200528 assert_int_equal(8, len);
529 str = "identity ";
530 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200531 assert_int_equal(LY_STMT_IDENTITY, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200532 assert_int_equal(8, len);
533 str = "if-feature ";
534 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200535 assert_int_equal(LY_STMT_IF_FEATURE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200536 assert_int_equal(10, len);
537 str = "import ";
538 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200539 assert_int_equal(LY_STMT_IMPORT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200540 assert_int_equal(6, len);
541 str = "include ";
542 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200543 assert_int_equal(LY_STMT_INCLUDE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200544 assert_int_equal(7, len);
545 str = "input{";
546 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200547 assert_int_equal(LY_STMT_INPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200548 assert_int_equal(5, len);
549 str = "key ";
550 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200551 assert_int_equal(LY_STMT_KEY, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200552 assert_int_equal(3, len);
553 str = "leaf ";
554 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200555 assert_int_equal(LY_STMT_LEAF, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200556 assert_int_equal(4, len);
557 str = "leaf-list ";
558 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200559 assert_int_equal(LY_STMT_LEAF_LIST, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200560 assert_int_equal(9, len);
561 str = "length ";
562 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200563 assert_int_equal(LY_STMT_LENGTH, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200564 assert_int_equal(6, len);
565 str = "list ";
566 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200567 assert_int_equal(LY_STMT_LIST, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200568 assert_int_equal(4, len);
569 str = "mandatory ";
570 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200571 assert_int_equal(LY_STMT_MANDATORY, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200572 assert_int_equal(9, len);
573 str = "max-elements ";
574 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200575 assert_int_equal(LY_STMT_MAX_ELEMENTS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200576 assert_int_equal(12, len);
577 str = "min-elements ";
578 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200579 assert_int_equal(LY_STMT_MIN_ELEMENTS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200580 assert_int_equal(12, len);
581 str = "modifier ";
582 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200583 assert_int_equal(LY_STMT_MODIFIER, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200584 assert_int_equal(8, len);
585 str = "module ";
586 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200587 assert_int_equal(LY_STMT_MODULE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200588 assert_int_equal(6, len);
589 str = "must ";
590 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200591 assert_int_equal(LY_STMT_MUST, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200592 assert_int_equal(4, len);
593 str = "namespace ";
594 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200595 assert_int_equal(LY_STMT_NAMESPACE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200596 assert_int_equal(9, len);
597 str = "notification ";
598 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200599 assert_int_equal(LY_STMT_NOTIFICATION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200600 assert_int_equal(12, len);
601 str = "ordered-by ";
602 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200603 assert_int_equal(LY_STMT_ORDERED_BY, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200604 assert_int_equal(10, len);
605 str = "organization ";
606 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200607 assert_int_equal(LY_STMT_ORGANIZATION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200608 assert_int_equal(12, len);
609 str = "output ";
610 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200611 assert_int_equal(LY_STMT_OUTPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200612 assert_int_equal(6, len);
613 str = "path ";
614 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200615 assert_int_equal(LY_STMT_PATH, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200616 assert_int_equal(4, len);
617 str = "pattern ";
618 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200619 assert_int_equal(LY_STMT_PATTERN, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200620 assert_int_equal(7, len);
621 str = "position ";
622 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200623 assert_int_equal(LY_STMT_POSITION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200624 assert_int_equal(8, len);
625 str = "prefix ";
626 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200627 assert_int_equal(LY_STMT_PREFIX, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200628 assert_int_equal(6, len);
629 str = "presence ";
630 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200631 assert_int_equal(LY_STMT_PRESENCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200632 assert_int_equal(8, len);
633 str = "range ";
634 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200635 assert_int_equal(LY_STMT_RANGE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200636 assert_int_equal(5, len);
637 str = "reference ";
638 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200639 assert_int_equal(LY_STMT_REFERENCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200640 assert_int_equal(9, len);
641 str = "refine ";
642 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200643 assert_int_equal(LY_STMT_REFINE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200644 assert_int_equal(6, len);
645 str = "require-instance ";
646 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200647 assert_int_equal(LY_STMT_REQUIRE_INSTANCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200648 assert_int_equal(16, len);
649 str = "revision ";
650 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200651 assert_int_equal(LY_STMT_REVISION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200652 assert_int_equal(8, len);
653 str = "revision-date ";
654 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200655 assert_int_equal(LY_STMT_REVISION_DATE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200656 assert_int_equal(13, len);
657 str = "rpc ";
658 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200659 assert_int_equal(LY_STMT_RPC, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200660 assert_int_equal(3, len);
661 str = "status ";
662 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200663 assert_int_equal(LY_STMT_STATUS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200664 assert_int_equal(6, len);
665 str = "submodule ";
666 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200667 assert_int_equal(LY_STMT_SUBMODULE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200668 assert_int_equal(9, len);
669 str = "type ";
670 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200671 assert_int_equal(LY_STMT_TYPE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200672 assert_int_equal(4, len);
673 str = "typedef ";
674 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200675 assert_int_equal(LY_STMT_TYPEDEF, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200676 assert_int_equal(7, len);
677 str = "unique ";
678 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200679 assert_int_equal(LY_STMT_UNIQUE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200680 assert_int_equal(6, len);
681 str = "units ";
682 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200683 assert_int_equal(LY_STMT_UNITS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200684 assert_int_equal(5, len);
685 str = "uses ";
686 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200687 assert_int_equal(LY_STMT_USES, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200688 assert_int_equal(4, len);
689 str = "value ";
690 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200691 assert_int_equal(LY_STMT_VALUE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200692 assert_int_equal(5, len);
693 str = "when ";
694 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200695 assert_int_equal(LY_STMT_WHEN, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200696 assert_int_equal(4, len);
697 str = "yang-version ";
698 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200699 assert_int_equal(LY_STMT_YANG_VERSION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200700 assert_int_equal(12, len);
701 str = "yin-element ";
702 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200703 assert_int_equal(LY_STMT_YIN_ELEMENT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200704 assert_int_equal(11, len);
Radek Krejci626df482018-10-11 15:06:31 +0200705 str = ";config false;";
706 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200707 assert_int_equal(LY_STMT_SYNTAX_SEMICOLON, kw);
Radek Krejci626df482018-10-11 15:06:31 +0200708 assert_int_equal(1, len);
709 assert_string_equal("config false;", str);
710 str = "{ config false;";
711 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200712 assert_int_equal(LY_STMT_SYNTAX_LEFT_BRACE, kw);
Radek Krejci626df482018-10-11 15:06:31 +0200713 assert_int_equal(1, len);
714 assert_string_equal(" config false;", str);
715 str = "}";
716 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200717 assert_int_equal(LY_STMT_SYNTAX_RIGHT_BRACE, kw);
Radek Krejci626df482018-10-11 15:06:31 +0200718 assert_int_equal(1, len);
719 assert_string_equal("", str);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200720
721 /* geenric extension */
722 str = p = "nacm:default-deny-write;";
723 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200724 assert_int_equal(LY_STMT_EXTENSION_INSTANCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200725 assert_int_equal(23, len);
726 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200727}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200728
Radek Krejci05b13982018-11-28 16:22:07 +0100729static void
730test_minmax(void **state)
731{
732 *state = test_minmax;
733
Radek Krejcie7b95092019-05-15 11:03:07 +0200734 struct lys_parser_ctx ctx = {0};
Radek Krejci05b13982018-11-28 16:22:07 +0100735 uint16_t flags = 0;
736 uint32_t value = 0;
737 struct lysp_ext_instance *ext = NULL;
738 const char *str;
739
740 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
741 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +0200742 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci05b13982018-11-28 16:22:07 +0100743 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100744 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci05b13982018-11-28 16:22:07 +0100745
Radek Krejcidf6cad12018-11-28 17:10:55 +0100746 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100747 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100748 logbuf_assert("Invalid value \"1invalid\" of \"min-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100749
750 flags = value = 0;
751 str = " -1; ...";
752 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
753 logbuf_assert("Invalid value \"-1\" of \"min-elements\". Line number 1.");
754
Radek Krejcidf6cad12018-11-28 17:10:55 +0100755 /* implementation limit */
756 flags = value = 0;
757 str = " 4294967296; ...";
758 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
759 logbuf_assert("Value \"4294967296\" is out of \"min-elements\" bounds. Line number 1.");
760
Radek Krejci05b13982018-11-28 16:22:07 +0100761 flags = value = 0;
762 str = " 1; ...";
763 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
764 assert_int_equal(LYS_SET_MIN, flags);
765 assert_int_equal(1, value);
766
767 flags = value = 0;
768 str = " 1 {m:ext;} ...";
769 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
770 assert_int_equal(LYS_SET_MIN, flags);
771 assert_int_equal(1, value);
772 assert_non_null(ext);
773 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
774 ext = NULL;
775
776 flags = value = 0;
777 str = " 1 {config true;} ...";
778 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
779 logbuf_assert("Invalid keyword \"config\" as a child of \"min-elements\". Line number 1.");
780
Radek Krejcidf6cad12018-11-28 17:10:55 +0100781 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100782 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100783 logbuf_assert("Invalid value \"1invalid\" of \"max-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100784
785 flags = value = 0;
786 str = " -1; ...";
787 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
788 logbuf_assert("Invalid value \"-1\" of \"max-elements\". Line number 1.");
789
Radek Krejcidf6cad12018-11-28 17:10:55 +0100790 /* implementation limit */
791 flags = value = 0;
792 str = " 4294967296; ...";
793 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
794 logbuf_assert("Value \"4294967296\" is out of \"max-elements\" bounds. Line number 1.");
795
Radek Krejci05b13982018-11-28 16:22:07 +0100796 flags = value = 0;
797 str = " 1; ...";
798 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
799 assert_int_equal(LYS_SET_MAX, flags);
800 assert_int_equal(1, value);
801
802 flags = value = 0;
803 str = " unbounded; ...";
804 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
805 assert_int_equal(LYS_SET_MAX, flags);
806 assert_int_equal(0, value);
807
808 flags = value = 0;
809 str = " 1 {m:ext;} ...";
810 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
811 assert_int_equal(LYS_SET_MAX, flags);
812 assert_int_equal(1, value);
813 assert_non_null(ext);
814 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
815 ext = NULL;
816
817 flags = value = 0;
818 str = " 1 {config true;} ...";
819 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
820 logbuf_assert("Invalid keyword \"config\" as a child of \"max-elements\". Line number 1.");
821
822 *state = NULL;
823 ly_ctx_destroy(ctx.ctx, NULL);
824}
825
Radek Krejci9fcacc12018-10-11 15:59:11 +0200826static struct lysp_module *
Radek Krejcie7b95092019-05-15 11:03:07 +0200827mod_renew(struct lys_parser_ctx *ctx)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200828{
Radek Krejci40544fa2019-01-11 09:38:37 +0100829 struct lysp_module *mod_p;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100830 static struct lys_module mod = {0};
831
832 lysc_module_free(mod.compiled, NULL);
833 lysp_module_free(mod.parsed);
834 FREE_STRING(mod.ctx, mod.name);
835 FREE_STRING(mod.ctx, mod.ns);
836 FREE_STRING(mod.ctx, mod.prefix);
837 FREE_STRING(mod.ctx, mod.filepath);
838 FREE_STRING(mod.ctx, mod.org);
839 FREE_STRING(mod.ctx, mod.contact);
840 FREE_STRING(mod.ctx, mod.dsc);
841 FREE_STRING(mod.ctx, mod.ref);
842 memset(&mod, 0, sizeof mod);
843 mod.ctx = ctx->ctx;
844
845 mod_p = calloc(1, sizeof *mod_p);
846 mod.parsed = mod_p;
847 mod_p->mod = &mod;
848 assert_non_null(mod_p);
849 return mod_p;
850}
851
852static struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +0200853submod_renew(struct lys_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100854{
855 lysp_submodule_free(ctx->ctx, submod);
856 submod = calloc(1, sizeof *submod);
857 assert_non_null(submod);
858 return submod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200859}
860
Radek Krejcid33273d2018-10-25 14:55:52 +0200861static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
862 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
863 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
864{
865 *module_data = user_data;
866 *format = LYS_IN_YANG;
867 *free_module_data = NULL;
868 return LY_SUCCESS;
869}
870
Radek Krejci9fcacc12018-10-11 15:59:11 +0200871static void
872test_module(void **state)
873{
Radek Krejci40544fa2019-01-11 09:38:37 +0100874 *state = test_module;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200875
Radek Krejcie7b95092019-05-15 11:03:07 +0200876 struct lys_parser_ctx ctx;
Radek Krejci40544fa2019-01-11 09:38:37 +0100877 struct lysp_module *mod = NULL;
878 struct lysp_submodule *submod = NULL;
879 struct lys_module *m;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200880 const char *str;
881
882 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
883 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +0200884 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200885 ctx.line = 1;
Radek Krejcia042ea12018-10-13 07:52:15 +0200886 ctx.indent = 0;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200887
Radek Krejci40544fa2019-01-11 09:38:37 +0100888 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200889
890 /* missing mandatory substatements */
891 str = " name {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100892 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
893 assert_string_equal("name", mod->mod->name);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200894 logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100895 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200896
897 str = " name {namespace urn:x;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100898 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
899 assert_string_equal("urn:x", mod->mod->ns);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200900 logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100901 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200902
903 str = " name {namespace urn:x;prefix \"x\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100904 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
905 assert_string_equal("x", mod->mod->prefix);
Radek Krejci40544fa2019-01-11 09:38:37 +0100906 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200907
Radek Krejci027d5802018-11-14 16:57:28 +0100908#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
909#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200910#define TEST_NODE(NODETYPE, INPUT, NAME) \
911 str = SCHEMA_BEGINNING INPUT; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100912 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200913 assert_non_null(mod->data); \
914 assert_int_equal(NODETYPE, mod->data->nodetype); \
915 assert_string_equal(NAME, mod->data->name); \
Radek Krejci40544fa2019-01-11 09:38:37 +0100916 mod = mod_renew(&ctx);
Radek Krejcia042ea12018-10-13 07:52:15 +0200917#define TEST_GENERIC(INPUT, TARGET, TEST) \
918 str = SCHEMA_BEGINNING INPUT; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100919 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200920 assert_non_null(TARGET); \
921 TEST; \
Radek Krejci40544fa2019-01-11 09:38:37 +0100922 mod = mod_renew(&ctx);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100923#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200924 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
Radek Krejci40544fa2019-01-11 09:38:37 +0100925 parse_module, mod, LINE, mod = mod_renew(&ctx))
Radek Krejcia042ea12018-10-13 07:52:15 +0200926
927 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100928 TEST_DUP("namespace", "y", "z", "1");
929 TEST_DUP("prefix", "y", "z", "1");
930 TEST_DUP("contact", "a", "b", "1");
931 TEST_DUP("description", "a", "b", "1");
932 TEST_DUP("organization", "a", "b", "1");
933 TEST_DUP("reference", "a", "b", "1");
Radek Krejcia042ea12018-10-13 07:52:15 +0200934
Radek Krejci70853c52018-10-15 14:46:16 +0200935 /* not allowed in module (submodule-specific) */
936 str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100937 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200938 logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100939 mod = mod_renew(&ctx);
Radek Krejci70853c52018-10-15 14:46:16 +0200940
Radek Krejcia042ea12018-10-13 07:52:15 +0200941 /* anydata */
942 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
943 /* anyxml */
944 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
945 /* augment */
946 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200947 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200948 /* choice */
949 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
950 /* contact 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100951 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact,
952 assert_string_equal("firstname surname", mod->mod->contact));
Radek Krejcia042ea12018-10-13 07:52:15 +0200953 /* container */
954 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
955 /* description 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100956 TEST_GENERIC("description \'some description\';}", mod->mod->dsc,
957 assert_string_equal("some description", mod->mod->dsc));
Radek Krejcia042ea12018-10-13 07:52:15 +0200958 /* deviation */
959 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200960 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200961 /* extension */
962 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200963 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200964 /* feature */
965 TEST_GENERIC("feature test;}", mod->features,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200966 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200967 /* grouping */
968 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200969 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200970 /* identity */
971 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200972 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200973 /* import */
Radek Krejci086c7132018-10-26 15:29:04 +0200974 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
975 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
976 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200977
Radek Krejcia042ea12018-10-13 07:52:15 +0200978 /* import - prefix collision */
Radek Krejci086c7132018-10-26 15:29:04 +0200979 str = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100980 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200981 logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100982 mod = mod_renew(&ctx);
Radek Krejci086c7132018-10-26 15:29:04 +0200983 str = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100984 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci086c7132018-10-26 15:29:04 +0200985 logbuf_assert("Prefix \"y\" already used to import \"zzz\" module. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100986 mod = mod_renew(&ctx);
Radek Krejci086c7132018-10-26 15:29:04 +0200987 str = "module" SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix z;}}";
988 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
989 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
990 logbuf_assert("Single revision of the module \"zzz\" referred twice.");
Radek Krejci70853c52018-10-15 14:46:16 +0200991
Radek Krejcia042ea12018-10-13 07:52:15 +0200992 /* include */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100993 store = 1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200994 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 +0200995 str = "module" SCHEMA_BEGINNING "include xxx;}";
996 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
997 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100998 logbuf_assert("Input data contains module in situation when a submodule is expected.");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100999 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +02001000
Radek Krejci9ed7a192018-10-31 16:23:51 +01001001 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +01001002 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 +02001003 str = "module" SCHEMA_BEGINNING "include xxx;}";
1004 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
1005 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
1006 logbuf_assert("Included \"xxx\" submodule from \"name\" belongs-to a different module \"wrong-name\".");
Radek Krejci9ed7a192018-10-31 16:23:51 +01001007 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +02001008
Radek Krejci313d9902018-11-08 09:42:58 +01001009 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 +02001010 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejci086c7132018-10-26 15:29:04 +02001011 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +02001012
Radek Krejcia042ea12018-10-13 07:52:15 +02001013 /* leaf */
1014 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
1015 /* leaf-list */
1016 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
1017 /* list */
1018 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
1019 /* notification */
1020 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001021 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001022 /* organization 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001023 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org,
1024 assert_string_equal("CESNET a.l.e.", mod->mod->org));
Radek Krejcia042ea12018-10-13 07:52:15 +02001025 /* reference 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001026 TEST_GENERIC("reference RFC7950;}", mod->mod->ref,
1027 assert_string_equal("RFC7950", mod->mod->ref));
Radek Krejcia042ea12018-10-13 07:52:15 +02001028 /* revision */
1029 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib7db73a2018-10-24 14:18:40 +02001030 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +02001031 /* rpc */
1032 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001033 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001034 /* typedef */
1035 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001036 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001037 /* uses */
1038 TEST_NODE(LYS_USES, "uses test;}", "test");
1039 /* yang-version */
Radek Krejci027d5802018-11-14 16:57:28 +01001040 str = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001041 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +02001042 logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001043 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001044 str = SCHEMA_BEGINNING2 "yang-version 1.0;yang-version 1.1;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001045 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +02001046 logbuf_assert("Duplicate keyword \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001047 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001048 str = SCHEMA_BEGINNING2 "yang-version 1.0;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001049 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
1050 assert_int_equal(1, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +01001051 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001052 str = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001053 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
1054 assert_int_equal(2, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +01001055 mod = mod_renew(&ctx);
1056
David Sedlák1b623122019-08-05 15:27:49 +02001057 struct lys_parser_ctx *ctx_p = NULL;
Radek Krejci40544fa2019-01-11 09:38:37 +01001058 str = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
1059 m = mod->mod;
1060 free(mod);
1061 m->parsed = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02001062 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, str, m));
1063 logbuf_assert("Trailing garbage \"module q {names...\" after module, expected end-of-input. Line number 1.");
1064 lys_parser_ctx_free(ctx_p);
Radek Krejci40544fa2019-01-11 09:38:37 +01001065 mod = mod_renew(&ctx);
1066
1067 str = "prefix " SCHEMA_BEGINNING "}";
1068 m = mod->mod;
1069 free(mod);
1070 m->parsed = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02001071 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, str, m));
1072 lys_parser_ctx_free(ctx_p);
1073 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001074 mod = mod_renew(&ctx);
Radek Krejci09306362018-10-15 15:26:01 +02001075
David Sedlák9fb515f2019-07-11 10:33:58 +02001076 str = "module " SCHEMA_BEGINNING "}";
1077 str = "module " SCHEMA_BEGINNING "leaf enum {type enumeration {enum seven { position 7;}}}}";
1078 m = mod->mod;
1079 free(mod);
1080 m->parsed = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02001081 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, str, m));
1082 lys_parser_ctx_free(ctx_p);
1083 logbuf_assert("Invalid keyword \"position\" as a child of \"enum\". Line number 1.");
David Sedlák9fb515f2019-07-11 10:33:58 +02001084 mod = mod_renew(&ctx);
1085
Radek Krejci156ccaf2018-10-15 15:49:17 +02001086 /* extensions */
1087 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001088 assert_string_equal("prefix:test", mod->exts[0].name);
1089 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
Radek Krejci40544fa2019-01-11 09:38:37 +01001090 mod = mod_renew(&ctx);
Radek Krejci156ccaf2018-10-15 15:49:17 +02001091
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001092 /* invalid substatement */
1093 str = SCHEMA_BEGINNING "must false;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001094 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001095 logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001096 mod = mod_renew(&ctx);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001097
Radek Krejci09306362018-10-15 15:26:01 +02001098 /* submodule */
Radek Krejci40544fa2019-01-11 09:38:37 +01001099 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001100
1101 /* missing mandatory substatements */
1102 str = " subname {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001103 lydict_remove(ctx.ctx, submod->name);
1104 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
1105 assert_string_equal("subname", submod->name);
Radek Krejci09306362018-10-15 15:26:01 +02001106 logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001107 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001108
Radek Krejci313d9902018-11-08 09:42:58 +01001109 str = " subname {belongs-to name {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001110 lydict_remove(ctx.ctx, submod->name);
1111 assert_int_equal(LY_SUCCESS, parse_submodule(&ctx, &str, submod));
1112 assert_string_equal("name", submod->belongsto);
1113 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001114
1115#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +01001116#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +02001117
1118 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001119 str = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ...";
1120 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod)); \
1121 logbuf_assert("Duplicate keyword \"belongs-to\". Line number 3."); \
1122 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001123
1124 /* not allowed in submodule (module-specific) */
1125 str = SCHEMA_BEGINNING "namespace \"urn:z\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001126 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001127 logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001128 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001129 str = SCHEMA_BEGINNING "prefix m;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001130 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001131 logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001132 submod = submod_renew(&ctx, submod);
Radek Krejcia042ea12018-10-13 07:52:15 +02001133
Radek Krejci40544fa2019-01-11 09:38:37 +01001134 str = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
1135 lysp_submodule_free(ctx.ctx, submod);
1136 submod = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02001137 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, ctx.ctx, &ctx, str, &submod));
1138 lys_parser_ctx_free(ctx_p);
1139 logbuf_assert("Trailing garbage \"module q {names...\" after submodule, expected end-of-input. Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001140
1141 str = "prefix " SCHEMA_BEGINNING "}";
David Sedlák1b623122019-08-05 15:27:49 +02001142 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, ctx.ctx, &ctx, str, &submod));
1143 lys_parser_ctx_free(ctx_p);
1144 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001145 submod = submod_renew(&ctx, submod);
1146
Radek Krejcia042ea12018-10-13 07:52:15 +02001147#undef TEST_GENERIC
1148#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001149#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +02001150#undef SCHEMA_BEGINNING
1151
Radek Krejci9fcacc12018-10-11 15:59:11 +02001152 lysp_module_free(mod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001153 lysp_submodule_free(ctx.ctx, submod);
Radek Krejci9fcacc12018-10-11 15:59:11 +02001154 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci40544fa2019-01-11 09:38:37 +01001155
1156 *state = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02001157}
1158
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001159static void
1160test_identity(void **state)
1161{
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001162 *state = test_identity;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001163
Radek Krejcie7b95092019-05-15 11:03:07 +02001164 struct lys_parser_ctx ctx;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001165 struct lysp_ident *ident = NULL;
1166 const char *str;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001167
1168 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1169 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001170 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001171 ctx.line = 1;
1172 ctx.indent = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001173 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001174
1175 /* invalid cardinality */
1176#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001177 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001178 &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL)
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001179
Radek Krejci38222632019-02-12 16:55:05 +01001180 TEST_DUP("description", "a", "b");
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001181 TEST_DUP("reference", "a", "b");
1182 TEST_DUP("status", "current", "obsolete");
1183
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001184 /* full content */
David Sedlák40bb13b2019-07-10 14:34:18 +02001185 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 +02001186 assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident));
1187 assert_non_null(ident);
1188 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001189 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001190 ident = NULL;
1191
1192 /* invalid substatement */
1193 str = " test {organization XXX;}";
1194 assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident));
1195 logbuf_assert("Invalid keyword \"organization\" as a child of \"identity\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001196 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001197 ident = NULL;
1198
1199#undef TEST_DUP
1200
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001201 *state = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001202 ly_ctx_destroy(ctx.ctx, NULL);
1203}
1204
1205static void
1206test_feature(void **state)
1207{
1208 (void) state; /* unused */
1209
Radek Krejcie7b95092019-05-15 11:03:07 +02001210 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001211 struct lysp_feature *features = NULL;
1212 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001213
1214 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1215 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001216 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001217 ctx.line = 1;
1218 ctx.indent = 0;
1219
1220 /* invalid cardinality */
1221#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1222 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001223 &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001224
1225 TEST_DUP("description", "a", "b");
1226 TEST_DUP("reference", "a", "b");
1227 TEST_DUP("status", "current", "obsolete");
1228
1229 /* full content */
1230 str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ...";
1231 assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features));
1232 assert_non_null(features);
1233 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001234 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001235 features = NULL;
1236
1237 /* invalid substatement */
1238 str = " test {organization XXX;}";
1239 assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features));
1240 logbuf_assert("Invalid keyword \"organization\" as a child of \"feature\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001241 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001242 features = NULL;
1243
1244#undef TEST_DUP
1245
1246 ly_ctx_destroy(ctx.ctx, NULL);
1247}
1248
1249static void
1250test_deviation(void **state)
1251{
1252 (void) state; /* unused */
1253
Radek Krejcie7b95092019-05-15 11:03:07 +02001254 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001255 struct lysp_deviation *d = NULL;
1256 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001257
1258 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1259 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001260 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001261 ctx.line = 1;
1262 ctx.indent = 0;
1263
1264 /* invalid cardinality */
1265#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1266 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001267 &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001268
1269 TEST_DUP("description", "a", "b");
1270 TEST_DUP("reference", "a", "b");
1271
1272 /* full content */
1273 str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
1274 assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d));
1275 assert_non_null(d);
1276 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001277 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001278 d = NULL;
1279
1280 /* missing mandatory substatement */
1281 str = " test {description text;}";
1282 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1283 logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001284 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001285 d = NULL;
1286
1287 /* invalid substatement */
1288 str = " test {deviate not-supported; status obsolete;}";
1289 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1290 logbuf_assert("Invalid keyword \"status\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001291 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001292 d = NULL;
1293
1294#undef TEST_DUP
1295
1296 ly_ctx_destroy(ctx.ctx, NULL);
1297}
1298
1299static void
1300test_deviate(void **state)
1301{
1302 (void) state; /* unused */
1303
Radek Krejcie7b95092019-05-15 11:03:07 +02001304 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001305 struct lysp_deviate *d = NULL;
1306 const char *str;
1307
1308 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1309 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001310 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001311 ctx.line = 1;
1312 ctx.indent = 0;
1313
1314 /* invalid cardinality */
1315#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
1316 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001317 &d, "1", lysp_deviate_free(ctx.ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001318
1319 TEST_DUP("add", "config", "true", "false");
1320 TEST_DUP("replace", "default", "int8", "uint8");
1321 TEST_DUP("add", "mandatory", "true", "false");
1322 TEST_DUP("add", "max-elements", "1", "2");
1323 TEST_DUP("add", "min-elements", "1", "2");
1324 TEST_DUP("replace", "type", "int8", "uint8");
1325 TEST_DUP("add", "units", "kilometers", "miles");
1326
1327 /* full contents */
1328 str = " not-supported {prefix:ext;} ...";
1329 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1330 assert_non_null(d);
1331 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001332 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001333 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;} ...";
1334 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1335 assert_non_null(d);
1336 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001337 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001338 str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...";
1339 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1340 assert_non_null(d);
1341 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001342 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001343 str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...";
1344 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1345 assert_non_null(d);
1346 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001347 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001348
1349 /* invalid substatements */
1350#define TEST_NOT_SUP(DEV, STMT, VALUE) \
1351 str = " "DEV" {"STMT" "VALUE";}..."; \
1352 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \
1353 logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001354 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001355
1356 TEST_NOT_SUP("not-supported", "units", "meters");
1357 TEST_NOT_SUP("not-supported", "must", "1");
1358 TEST_NOT_SUP("not-supported", "unique", "x");
1359 TEST_NOT_SUP("not-supported", "default", "a");
1360 TEST_NOT_SUP("not-supported", "config", "true");
1361 TEST_NOT_SUP("not-supported", "mandatory", "true");
1362 TEST_NOT_SUP("not-supported", "min-elements", "1");
1363 TEST_NOT_SUP("not-supported", "max-elements", "2");
1364 TEST_NOT_SUP("not-supported", "type", "string");
1365 TEST_NOT_SUP("add", "type", "string");
1366 TEST_NOT_SUP("delete", "config", "true");
1367 TEST_NOT_SUP("delete", "mandatory", "true");
1368 TEST_NOT_SUP("delete", "min-elements", "1");
1369 TEST_NOT_SUP("delete", "max-elements", "2");
1370 TEST_NOT_SUP("delete", "type", "string");
1371 TEST_NOT_SUP("replace", "must", "1");
1372 TEST_NOT_SUP("replace", "unique", "a");
1373
1374 str = " nonsence; ...";
1375 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d));
1376 logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1.");
1377 assert_null(d);
1378
1379#undef TEST_NOT_SUP
1380#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001381
1382 ly_ctx_destroy(ctx.ctx, NULL);
1383}
1384
Radek Krejci8c370832018-11-02 15:10:03 +01001385static void
1386test_container(void **state)
1387{
1388 (void) state; /* unused */
1389
Radek Krejcie7b95092019-05-15 11:03:07 +02001390 struct lys_parser_ctx ctx = {0};
Radek Krejci8c370832018-11-02 15:10:03 +01001391 struct lysp_node_container *c = NULL;
1392 const char *str;
1393
1394 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1395 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001396 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci8c370832018-11-02 15:10:03 +01001397 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001398 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +01001399
1400 /* invalid cardinality */
1401#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1402 str = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1403 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); \
1404 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001405 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001406
1407 TEST_DUP("config", "true", "false");
1408 TEST_DUP("description", "text1", "text2");
1409 TEST_DUP("presence", "true", "false");
1410 TEST_DUP("reference", "1", "2");
1411 TEST_DUP("status", "current", "obsolete");
1412 TEST_DUP("when", "true", "false");
1413#undef TEST_DUP
1414
1415 /* full content */
Radek Krejci313d9902018-11-08 09:42:58 +01001416 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;}"
1417 "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 +01001418 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1419 assert_non_null(c);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001420 assert_int_equal(LYS_CONTAINER, c->nodetype);
1421 assert_string_equal("cont", c->name);
Radek Krejci8c370832018-11-02 15:10:03 +01001422 assert_non_null(c->actions);
1423 assert_non_null(c->child);
1424 assert_string_equal("test", c->dsc);
1425 assert_non_null(c->exts);
1426 assert_non_null(c->groupings);
1427 assert_non_null(c->iffeatures);
1428 assert_non_null(c->musts);
1429 assert_non_null(c->notifs);
1430 assert_string_equal("true", c->presence);
1431 assert_string_equal("test", c->ref);
1432 assert_non_null(c->typedefs);
1433 assert_non_null(c->when);
1434 assert_null(c->parent);
1435 assert_null(c->next);
1436 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags);
Radek Krejci313d9902018-11-08 09:42:58 +01001437 ly_set_erase(&ctx.tpdfs_nodes, NULL);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001438 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001439
1440 /* invalid */
1441 str = " cont {augment /root;} ...";
1442 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1443 logbuf_assert("Invalid keyword \"augment\" as a child of \"container\". Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001444 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001445 str = " cont {nonsence true;} ...";
1446 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1447 logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001448 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001449
Radek Krejcif538ce52019-03-05 10:46:14 +01001450 ctx.mod_version = 1; /* simulate YANG 1.0 */
1451 str = " cont {action x;} ...";
1452 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1453 logbuf_assert("Invalid keyword \"action\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1454 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
1455
Radek Krejci8c370832018-11-02 15:10:03 +01001456 ly_ctx_destroy(ctx.ctx, NULL);
1457}
1458
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001459static void
1460test_leaf(void **state)
1461{
1462 *state = test_leaf;
1463
Radek Krejcie7b95092019-05-15 11:03:07 +02001464 struct lys_parser_ctx ctx = {0};
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001465 struct lysp_node_leaf *l = NULL;
1466 const char *str;
1467
1468 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1469 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001470 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001471 ctx.line = 1;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001472 //ctx.mod->version = 2; /* simulate YANG 1.1 */
1473
1474 /* invalid cardinality */
1475#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1476 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1477 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1478 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1479 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1480
1481 TEST_DUP("config", "true", "false");
1482 TEST_DUP("default", "x", "y");
1483 TEST_DUP("description", "text1", "text2");
1484 TEST_DUP("mandatory", "true", "false");
1485 TEST_DUP("reference", "1", "2");
1486 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001487 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001488 TEST_DUP("units", "text1", "text2");
1489 TEST_DUP("when", "true", "false");
1490#undef TEST_DUP
1491
1492 /* full content - without mandatory which is mutual exclusive with default */
1493 str = "l {config false;default \"xxx\";description test;if-feature f;"
1494 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
1495 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1496 assert_non_null(l);
1497 assert_int_equal(LYS_LEAF, l->nodetype);
1498 assert_string_equal("l", l->name);
1499 assert_string_equal("test", l->dsc);
1500 assert_string_equal("xxx", l->dflt);
1501 assert_string_equal("yyy", l->units);
1502 assert_string_equal("string", l->type.name);
1503 assert_non_null(l->exts);
1504 assert_non_null(l->iffeatures);
1505 assert_non_null(l->musts);
1506 assert_string_equal("test", l->ref);
1507 assert_non_null(l->when);
1508 assert_null(l->parent);
1509 assert_null(l->next);
1510 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, l->flags);
1511 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1512
1513 /* full content - now with mandatory */
1514 str = "l {mandatory true; type string;} ...";
1515 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1516 assert_non_null(l);
1517 assert_int_equal(LYS_LEAF, l->nodetype);
1518 assert_string_equal("l", l->name);
1519 assert_string_equal("string", l->type.name);
1520 assert_int_equal(LYS_MAND_TRUE, l->flags);
1521 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1522
1523 /* invalid */
1524 str = " l {mandatory true; default xx; type string;} ...";
1525 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001526 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"leaf\". Line number 1.");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001527 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1528
1529 str = " l {description \"missing type\";} ...";
1530 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1531 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf\". Line number 1.");
1532 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1533
1534 *state = NULL;
1535 ly_ctx_destroy(ctx.ctx, NULL);
1536}
1537
Radek Krejci0e5d8382018-11-28 16:37:53 +01001538static void
1539test_leaflist(void **state)
1540{
1541 *state = test_leaf;
1542
Radek Krejcie7b95092019-05-15 11:03:07 +02001543 struct lys_parser_ctx ctx = {0};
Radek Krejci0e5d8382018-11-28 16:37:53 +01001544 struct lysp_node_leaflist *ll = NULL;
1545 const char *str;
1546
1547 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1548 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001549 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001550 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001551 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001552
1553 /* invalid cardinality */
1554#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1555 str = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1556 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); \
1557 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1558 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1559
1560 TEST_DUP("config", "true", "false");
1561 TEST_DUP("description", "text1", "text2");
1562 TEST_DUP("max-elements", "10", "20");
1563 TEST_DUP("min-elements", "10", "20");
1564 TEST_DUP("ordered-by", "user", "system");
1565 TEST_DUP("reference", "1", "2");
1566 TEST_DUP("status", "current", "obsolete");
1567 TEST_DUP("type", "int8", "uint8");
1568 TEST_DUP("units", "text1", "text2");
1569 TEST_DUP("when", "true", "false");
1570#undef TEST_DUP
1571
1572 /* full content - without min-elements which is mutual exclusive with default */
1573 str = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
1574 "max-elements 10;must 'expr';ordered-by user;reference test;"
1575 "status current;type string; units zzz;when true;m:ext;} ...";
1576 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1577 assert_non_null(ll);
1578 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1579 assert_string_equal("ll", ll->name);
1580 assert_string_equal("test", ll->dsc);
1581 assert_non_null(ll->dflts);
1582 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
1583 assert_string_equal("xxx", ll->dflts[0]);
1584 assert_string_equal("yyy", ll->dflts[1]);
1585 assert_string_equal("zzz", ll->units);
1586 assert_int_equal(10, ll->max);
1587 assert_int_equal(0, ll->min);
1588 assert_string_equal("string", ll->type.name);
1589 assert_non_null(ll->exts);
1590 assert_non_null(ll->iffeatures);
1591 assert_non_null(ll->musts);
1592 assert_string_equal("test", ll->ref);
1593 assert_non_null(ll->when);
1594 assert_null(ll->parent);
1595 assert_null(ll->next);
1596 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
1597 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1598
1599 /* full content - now with min-elements */
1600 str = "ll {min-elements 10; type string;} ...";
1601 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1602 assert_non_null(ll);
1603 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1604 assert_string_equal("ll", ll->name);
1605 assert_string_equal("string", ll->type.name);
1606 assert_int_equal(0, ll->max);
1607 assert_int_equal(10, ll->min);
1608 assert_int_equal(LYS_SET_MIN, ll->flags);
1609 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1610
1611 /* invalid */
1612 str = " ll {min-elements 1; default xx; type string;} ...";
1613 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001614 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 +01001615 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1616
1617 str = " ll {description \"missing type\";} ...";
1618 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1619 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf-list\". Line number 1.");
1620 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1621
Radek Krejcidf6cad12018-11-28 17:10:55 +01001622 str = " ll {type string; min-elements 10; max-elements 1;} ..."; /* invalid combination of min/max */
1623 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1624 logbuf_assert("Invalid combination of min-elements and max-elements: min value 10 is bigger than the max value 1. Line number 1.");
1625 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1626
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001627 ctx.mod_version = 1; /* simulate YANG 1.0 - default statement is not allowed */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001628 str = " ll {default xx; type string;} ...";
1629 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1630 logbuf_assert("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1631 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1632
1633 *state = NULL;
1634 ly_ctx_destroy(ctx.ctx, NULL);
1635}
1636
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001637static void
1638test_list(void **state)
1639{
1640 *state = test_list;
1641
Radek Krejcie7b95092019-05-15 11:03:07 +02001642 struct lys_parser_ctx ctx = {0};
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001643 struct lysp_node_list *l = NULL;
1644 const char *str;
1645
1646 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1647 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001648 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001649 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001650 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001651
1652 /* invalid cardinality */
1653#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1654 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1655 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1656 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1657 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1658
1659 TEST_DUP("config", "true", "false");
1660 TEST_DUP("description", "text1", "text2");
1661 TEST_DUP("key", "one", "two");
1662 TEST_DUP("max-elements", "10", "20");
1663 TEST_DUP("min-elements", "10", "20");
1664 TEST_DUP("ordered-by", "user", "system");
1665 TEST_DUP("reference", "1", "2");
1666 TEST_DUP("status", "current", "obsolete");
1667 TEST_DUP("when", "true", "false");
1668#undef TEST_DUP
1669
1670 /* full content */
1671 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;}"
1672 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1673 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
1674 assert_int_equal(LY_SUCCESS, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1675 assert_non_null(l);
1676 assert_int_equal(LYS_LIST, l->nodetype);
1677 assert_string_equal("l", l->name);
1678 assert_string_equal("test", l->dsc);
1679 assert_string_equal("l", l->key);
1680 assert_non_null(l->uniques);
1681 assert_int_equal(2, LY_ARRAY_SIZE(l->uniques));
1682 assert_string_equal("xxx", l->uniques[0]);
1683 assert_string_equal("yyy", l->uniques[1]);
1684 assert_int_equal(10, l->max);
1685 assert_int_equal(1, l->min);
1686 assert_non_null(l->exts);
1687 assert_non_null(l->iffeatures);
1688 assert_non_null(l->musts);
1689 assert_string_equal("test", l->ref);
1690 assert_non_null(l->when);
1691 assert_null(l->parent);
1692 assert_null(l->next);
1693 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, l->flags);
1694 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1695 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1696
Radek Krejcif538ce52019-03-05 10:46:14 +01001697 /* invalid content */
1698 ctx.mod_version = 1; /* simulate YANG 1.0 */
1699 str = "l {action x;} ...";
1700 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1701 logbuf_assert("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1702 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1703
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001704 *state = NULL;
1705 ly_ctx_destroy(ctx.ctx, NULL);
1706}
1707
Radek Krejci056d0a82018-12-06 16:57:25 +01001708static void
1709test_choice(void **state)
1710{
1711 *state = test_choice;
1712
Radek Krejcie7b95092019-05-15 11:03:07 +02001713 struct lys_parser_ctx ctx = {0};
Radek Krejci056d0a82018-12-06 16:57:25 +01001714 struct lysp_node_choice *ch = NULL;
1715 const char *str;
1716
1717 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1718 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001719 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci056d0a82018-12-06 16:57:25 +01001720 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001721 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci056d0a82018-12-06 16:57:25 +01001722
1723 /* invalid cardinality */
1724#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1725 str = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1726 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); \
1727 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1728 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1729
1730 TEST_DUP("config", "true", "false");
1731 TEST_DUP("default", "a", "b");
1732 TEST_DUP("description", "text1", "text2");
1733 TEST_DUP("mandatory", "true", "false");
1734 TEST_DUP("reference", "1", "2");
1735 TEST_DUP("status", "current", "obsolete");
1736 TEST_DUP("when", "true", "false");
1737#undef TEST_DUP
1738
Radek Krejcia9026eb2018-12-12 16:04:47 +01001739 /* full content - without default due to a collision with mandatory */
1740 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 +01001741 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
1742 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1743 assert_non_null(ch);
1744 assert_int_equal(LYS_CHOICE, ch->nodetype);
1745 assert_string_equal("ch", ch->name);
1746 assert_string_equal("test", ch->dsc);
1747 assert_non_null(ch->exts);
1748 assert_non_null(ch->iffeatures);
1749 assert_string_equal("test", ch->ref);
1750 assert_non_null(ch->when);
1751 assert_null(ch->parent);
1752 assert_null(ch->next);
1753 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, ch->flags);
1754 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1755
Radek Krejcia9026eb2018-12-12 16:04:47 +01001756 /* full content - the default missing from the previous node */
1757 str = "ch {default c;case c;} ...";
1758 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1759 assert_non_null(ch);
1760 assert_int_equal(LYS_CHOICE, ch->nodetype);
1761 assert_string_equal("ch", ch->name);
1762 assert_string_equal("c", ch->dflt);
1763 assert_int_equal(0, ch->flags);
1764 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1765
1766 /* invalid content */
1767 str = "ch {mandatory true; default c1; case c1 {leaf x{type string;}}} ...";
1768 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1769 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"choice\". Line number 1.");
1770 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1771
1772 *state = NULL;
1773 ly_ctx_destroy(ctx.ctx, NULL);
1774}
1775
1776static void
1777test_case(void **state)
1778{
1779 *state = test_case;
1780
Radek Krejcie7b95092019-05-15 11:03:07 +02001781 struct lys_parser_ctx ctx = {0};
Radek Krejcia9026eb2018-12-12 16:04:47 +01001782 struct lysp_node_case *cs = NULL;
1783 const char *str;
1784
1785 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1786 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001787 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001788 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001789 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcia9026eb2018-12-12 16:04:47 +01001790
1791 /* invalid cardinality */
1792#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1793 str = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1794 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs)); \
1795 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1796 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1797
1798 TEST_DUP("description", "text1", "text2");
1799 TEST_DUP("reference", "1", "2");
1800 TEST_DUP("status", "current", "obsolete");
1801 TEST_DUP("when", "true", "false");
1802#undef TEST_DUP
1803
1804 /* full content */
1805 str = "cs {anydata any;anyxml anyxml; choice ch;container c;description test;if-feature f;leaf l {type string;}"
1806 "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
1807 assert_int_equal(LY_SUCCESS, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1808 assert_non_null(cs);
1809 assert_int_equal(LYS_CASE, cs->nodetype);
1810 assert_string_equal("cs", cs->name);
1811 assert_string_equal("test", cs->dsc);
1812 assert_non_null(cs->exts);
1813 assert_non_null(cs->iffeatures);
1814 assert_string_equal("test", cs->ref);
1815 assert_non_null(cs->when);
1816 assert_null(cs->parent);
1817 assert_null(cs->next);
1818 assert_int_equal(LYS_STATUS_CURR, cs->flags);
1819 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1820
1821 /* invalid content */
1822 str = "cs {config true} ...";
1823 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1824 logbuf_assert("Invalid keyword \"config\" as a child of \"case\". Line number 1.");
1825 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1826
Radek Krejci056d0a82018-12-06 16:57:25 +01001827 *state = NULL;
1828 ly_ctx_destroy(ctx.ctx, NULL);
1829}
1830
Radek Krejci9800fb82018-12-13 14:26:23 +01001831static void
Radek Krejcid6b76452019-09-03 17:03:03 +02001832test_any(void **state, enum ly_stmt kw)
Radek Krejci9800fb82018-12-13 14:26:23 +01001833{
1834 *state = test_any;
1835
Radek Krejcie7b95092019-05-15 11:03:07 +02001836 struct lys_parser_ctx ctx = {0};
Radek Krejci9800fb82018-12-13 14:26:23 +01001837 struct lysp_node_anydata *any = NULL;
1838 const char *str;
1839
1840 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1841 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001842 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci9800fb82018-12-13 14:26:23 +01001843 ctx.line = 1;
Radek Krejcid6b76452019-09-03 17:03:03 +02001844 if (kw == LY_STMT_ANYDATA) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001845 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001846 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001847 ctx.mod_version = 1; /* simulate YANG 1.0 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001848 }
1849
1850 /* invalid cardinality */
1851#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1852 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1853 assert_int_equal(LY_EVALID, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any)); \
1854 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1855 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1856
1857 TEST_DUP("config", "true", "false");
1858 TEST_DUP("description", "text1", "text2");
1859 TEST_DUP("mandatory", "true", "false");
1860 TEST_DUP("reference", "1", "2");
1861 TEST_DUP("status", "current", "obsolete");
1862 TEST_DUP("when", "true", "false");
1863#undef TEST_DUP
1864
1865 /* full content */
1866 str = "any {config true;description test;if-feature f;mandatory true;must 'expr';reference test;status current;when true;m:ext;} ...";
1867 assert_int_equal(LY_SUCCESS, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any));
1868 assert_non_null(any);
Radek Krejcid6b76452019-09-03 17:03:03 +02001869 assert_int_equal(kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML, any->nodetype);
Radek Krejci9800fb82018-12-13 14:26:23 +01001870 assert_string_equal("any", any->name);
1871 assert_string_equal("test", any->dsc);
1872 assert_non_null(any->exts);
1873 assert_non_null(any->iffeatures);
1874 assert_non_null(any->musts);
1875 assert_string_equal("test", any->ref);
1876 assert_non_null(any->when);
1877 assert_null(any->parent);
1878 assert_null(any->next);
1879 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags);
1880 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1881
1882 *state = NULL;
1883 ly_ctx_destroy(ctx.ctx, NULL);
1884}
1885
1886static void
1887test_anydata(void **state)
1888{
Radek Krejcid6b76452019-09-03 17:03:03 +02001889 return test_any(state, LY_STMT_ANYDATA);
Radek Krejci9800fb82018-12-13 14:26:23 +01001890}
1891
1892static void
1893test_anyxml(void **state)
1894{
Radek Krejcid6b76452019-09-03 17:03:03 +02001895 return test_any(state, LY_STMT_ANYXML);
Radek Krejci9800fb82018-12-13 14:26:23 +01001896}
1897
Radek Krejcie86bf772018-12-14 11:39:53 +01001898static void
1899test_grouping(void **state)
1900{
1901 *state = test_grouping;
1902
Radek Krejcie7b95092019-05-15 11:03:07 +02001903 struct lys_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01001904 struct lysp_grp *grp = NULL;
1905 const char *str;
1906
1907 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1908 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001909 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcie86bf772018-12-14 11:39:53 +01001910 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001911 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001912
1913 /* invalid cardinality */
1914#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1915 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1916 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp)); \
1917 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1918 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1919
1920 TEST_DUP("description", "text1", "text2");
1921 TEST_DUP("reference", "1", "2");
1922 TEST_DUP("status", "current", "obsolete");
1923#undef TEST_DUP
1924
1925 /* full content */
1926 str = "grp {action x;anydata any;anyxml anyxml; choice ch;container c;description test;grouping g;leaf l {type string;}"
1927 "leaf-list ll {type string;} list li;notification not;reference test;status current;typedef t {type int8;}uses g;m:ext;} ...";
1928 assert_int_equal(LY_SUCCESS, parse_grouping(&ctx, &str, NULL, &grp));
1929 assert_non_null(grp);
1930 assert_int_equal(LYS_GROUPING, grp->nodetype);
1931 assert_string_equal("grp", grp->name);
1932 assert_string_equal("test", grp->dsc);
1933 assert_non_null(grp->exts);
1934 assert_string_equal("test", grp->ref);
1935 assert_null(grp->parent);
1936 assert_int_equal( LYS_STATUS_CURR, grp->flags);
1937 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1938 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1939
1940 /* invalid content */
1941 str = "grp {config true} ...";
1942 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1943 logbuf_assert("Invalid keyword \"config\" as a child of \"grouping\". Line number 1.");
1944 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1945
1946 str = "grp {must 'expr'} ...";
1947 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1948 logbuf_assert("Invalid keyword \"must\" as a child of \"grouping\". Line number 1.");
1949 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1950
1951 *state = NULL;
1952 ly_ctx_destroy(ctx.ctx, NULL);
1953}
1954
1955static void
Radek Krejcif538ce52019-03-05 10:46:14 +01001956test_action(void **state)
1957{
1958 *state = test_action;
1959
Radek Krejcie7b95092019-05-15 11:03:07 +02001960 struct lys_parser_ctx ctx = {0};
Radek Krejcif538ce52019-03-05 10:46:14 +01001961 struct lysp_action *rpcs = NULL;
1962 struct lysp_node_container *c = NULL;
1963 const char *str;
1964
1965 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1966 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001967 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcif538ce52019-03-05 10:46:14 +01001968 ctx.line = 1;
1969 ctx.mod_version = 2; /* simulate YANG 1.1 */
1970
1971 /* invalid cardinality */
1972#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1973 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1974 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs)); \
1975 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1976 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1977
1978 TEST_DUP("description", "text1", "text2");
1979 TEST_DUP("input", "", "");
1980 TEST_DUP("output", "", "");
1981 TEST_DUP("reference", "1", "2");
1982 TEST_DUP("status", "current", "obsolete");
1983#undef TEST_DUP
1984
1985 /* full content */
1986 str = "top;";
1987 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1988 str = "func {description test;grouping grp;if-feature f;reference test;status current;typedef mytype {type int8;} m:ext;"
1989 "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1990 " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}"
1991 "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1992 " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ...";
1993 assert_int_equal(LY_SUCCESS, parse_action(&ctx, &str, (struct lysp_node*)c, &rpcs));
1994 assert_non_null(rpcs);
1995 assert_int_equal(LYS_ACTION, rpcs->nodetype);
1996 assert_string_equal("func", rpcs->name);
1997 assert_string_equal("test", rpcs->dsc);
1998 assert_non_null(rpcs->exts);
1999 assert_non_null(rpcs->iffeatures);
2000 assert_string_equal("test", rpcs->ref);
2001 assert_non_null(rpcs->groupings);
2002 assert_non_null(rpcs->typedefs);
2003 assert_int_equal(LYS_STATUS_CURR, rpcs->flags);
2004 /* input */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002005 assert_int_equal(rpcs->input.nodetype, LYS_INPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01002006 assert_non_null(rpcs->input.groupings);
2007 assert_non_null(rpcs->input.exts);
2008 assert_non_null(rpcs->input.musts);
2009 assert_non_null(rpcs->input.typedefs);
2010 assert_non_null(rpcs->input.data);
2011 /* output */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002012 assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01002013 assert_non_null(rpcs->output.groupings);
2014 assert_non_null(rpcs->output.exts);
2015 assert_non_null(rpcs->output.musts);
2016 assert_non_null(rpcs->output.typedefs);
2017 assert_non_null(rpcs->output.data);
2018
2019 ly_set_erase(&ctx.tpdfs_nodes, NULL);
2020 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
2021
2022 /* invalid content */
2023 str = "func {config true} ...";
2024 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs));
2025 logbuf_assert("Invalid keyword \"config\" as a child of \"rpc\". Line number 1.");
2026 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
2027
2028 *state = NULL;
2029 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
2030 ly_ctx_destroy(ctx.ctx, NULL);
2031}
2032
2033static void
Radek Krejcifc11bd72019-04-11 16:00:05 +02002034test_notification(void **state)
2035{
2036 *state = test_notification;
2037
Radek Krejcie7b95092019-05-15 11:03:07 +02002038 struct lys_parser_ctx ctx = {0};
Radek Krejcifc11bd72019-04-11 16:00:05 +02002039 struct lysp_notif *notifs = NULL;
2040 struct lysp_node_container *c = NULL;
2041 const char *str;
2042
2043 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2044 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002045 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcifc11bd72019-04-11 16:00:05 +02002046 ctx.line = 1;
2047 ctx.mod_version = 2; /* simulate YANG 1.1 */
2048
2049 /* invalid cardinality */
2050#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2051 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2052 assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, &notifs)); \
2053 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2054 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2055
2056 TEST_DUP("description", "text1", "text2");
2057 TEST_DUP("reference", "1", "2");
2058 TEST_DUP("status", "current", "obsolete");
2059#undef TEST_DUP
2060
2061 /* full content */
2062 str = "top;";
2063 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
2064 str = "ntf {anydata a1; anyxml a2; choice ch; container c; description test; grouping grp; if-feature f; leaf l {type int8;}"
2065 "leaf-list ll {type int8;} list li; must 1; reference test; status current; typedef mytype {type int8;} uses grp; m:ext;}";
2066 assert_int_equal(LY_SUCCESS, parse_notif(&ctx, &str, (struct lysp_node*)c, &notifs));
2067 assert_non_null(notifs);
2068 assert_int_equal(LYS_NOTIF, notifs->nodetype);
2069 assert_string_equal("ntf", notifs->name);
2070 assert_string_equal("test", notifs->dsc);
2071 assert_non_null(notifs->exts);
2072 assert_non_null(notifs->iffeatures);
2073 assert_string_equal("test", notifs->ref);
2074 assert_non_null(notifs->groupings);
2075 assert_non_null(notifs->typedefs);
2076 assert_non_null(notifs->musts);
2077 assert_non_null(notifs->data);
2078 assert_int_equal(LYS_STATUS_CURR, notifs->flags);
2079
2080 ly_set_erase(&ctx.tpdfs_nodes, NULL);
2081 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2082
2083 /* invalid content */
2084 str = "ntf {config true} ...";
2085 assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, &notifs));
2086 logbuf_assert("Invalid keyword \"config\" as a child of \"notification\". Line number 1.");
2087 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2088
2089 *state = NULL;
2090 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
2091 ly_ctx_destroy(ctx.ctx, NULL);
2092}
2093
2094static void
Radek Krejcie86bf772018-12-14 11:39:53 +01002095test_uses(void **state)
2096{
2097 *state = test_uses;
2098
Radek Krejcie7b95092019-05-15 11:03:07 +02002099 struct lys_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01002100 struct lysp_node_uses *u = NULL;
2101 const char *str;
2102
2103 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2104 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002105 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcie86bf772018-12-14 11:39:53 +01002106 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002107 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01002108
2109 /* invalid cardinality */
2110#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2111 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2112 assert_int_equal(LY_EVALID, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u)); \
2113 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2114 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2115
2116 TEST_DUP("description", "text1", "text2");
2117 TEST_DUP("reference", "1", "2");
2118 TEST_DUP("status", "current", "obsolete");
2119 TEST_DUP("when", "true", "false");
2120#undef TEST_DUP
2121
2122 /* full content */
2123 str = "grpref {augment some/node;description test;if-feature f;reference test;refine some/other/node;status current;when true;m:ext;} ...";
2124 assert_int_equal(LY_SUCCESS, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u));
2125 assert_non_null(u);
2126 assert_int_equal(LYS_USES, u->nodetype);
2127 assert_string_equal("grpref", u->name);
2128 assert_string_equal("test", u->dsc);
2129 assert_non_null(u->exts);
2130 assert_non_null(u->iffeatures);
2131 assert_string_equal("test", u->ref);
2132 assert_non_null(u->augments);
2133 assert_non_null(u->refines);
2134 assert_non_null(u->when);
2135 assert_null(u->parent);
2136 assert_null(u->next);
2137 assert_int_equal(LYS_STATUS_CURR, u->flags);
2138 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2139
2140 *state = NULL;
2141 ly_ctx_destroy(ctx.ctx, NULL);
2142}
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002143
2144
2145static void
2146test_augment(void **state)
2147{
2148 *state = test_augment;
2149
Radek Krejcie7b95092019-05-15 11:03:07 +02002150 struct lys_parser_ctx ctx = {0};
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002151 struct lysp_augment *a = NULL;
2152 const char *str;
2153
2154 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2155 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002156 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002157 ctx.line = 1;
Radek Krejcib4adbf12019-02-25 13:35:22 +01002158 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002159
2160 /* invalid cardinality */
2161#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2162 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2163 assert_int_equal(LY_EVALID, parse_augment(&ctx, &str, NULL, &a)); \
2164 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejcib4adbf12019-02-25 13:35:22 +01002165 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002166
2167 TEST_DUP("description", "text1", "text2");
2168 TEST_DUP("reference", "1", "2");
2169 TEST_DUP("status", "current", "obsolete");
2170 TEST_DUP("when", "true", "false");
2171#undef TEST_DUP
2172
2173 /* full content */
2174 str = "/target/nodeid {action x; anydata any;anyxml anyxml; case cs; choice ch;container c;description test;if-feature f;leaf l {type string;}"
2175 "leaf-list ll {type string;} list li;notification not;reference test;status current;uses g;when true;m:ext;} ...";
2176 assert_int_equal(LY_SUCCESS, parse_augment(&ctx, &str, NULL, &a));
2177 assert_non_null(a);
2178 assert_int_equal(LYS_AUGMENT, a->nodetype);
2179 assert_string_equal("/target/nodeid", a->nodeid);
2180 assert_string_equal("test", a->dsc);
2181 assert_non_null(a->exts);
2182 assert_non_null(a->iffeatures);
2183 assert_string_equal("test", a->ref);
2184 assert_non_null(a->when);
2185 assert_null(a->parent);
2186 assert_int_equal(LYS_STATUS_CURR, a->flags);
Radek Krejcib4adbf12019-02-25 13:35:22 +01002187 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002188
2189 *state = NULL;
2190 ly_ctx_destroy(ctx.ctx, NULL);
2191}
2192
Radek Krejcif09e4e82019-06-14 15:08:11 +02002193static void
2194test_when(void **state)
2195{
2196 *state = test_when;
2197
2198 struct lys_parser_ctx ctx = {0};
2199 struct lysp_when *w = NULL;
2200 const char *str;
2201
2202 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2203 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002204 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcif09e4e82019-06-14 15:08:11 +02002205 ctx.line = 1;
2206 ctx.mod_version = 2; /* simulate YANG 1.1 */
2207
2208 /* invalid cardinality */
2209#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2210 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2211 assert_int_equal(LY_EVALID, parse_when(&ctx, &str, &w)); \
2212 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2213 FREE_MEMBER(ctx.ctx, w, lysp_when_free); w = NULL;
2214
2215 TEST_DUP("description", "text1", "text2");
2216 TEST_DUP("reference", "1", "2");
2217#undef TEST_DUP
2218
2219 /* full content */
2220 str = "expression {description test;reference test;m:ext;} ...";
2221 assert_int_equal(LY_SUCCESS, parse_when(&ctx, &str, &w));
2222 assert_non_null(w);
2223 assert_string_equal("expression", w->cond);
2224 assert_string_equal("test", w->dsc);
2225 assert_string_equal("test", w->ref);
2226 assert_non_null(w->exts);
2227 FREE_MEMBER(ctx.ctx, w, lysp_when_free); w = NULL;
2228
2229 /* empty condition */
2230 str = "\"\";";
2231 assert_int_equal(LY_SUCCESS, parse_when(&ctx, &str, &w));
2232 logbuf_assert("Empty argument of when statement does not make sense.");
2233 assert_non_null(w);
2234 assert_string_equal("", w->cond);
2235 FREE_MEMBER(ctx.ctx, w, lysp_when_free); w = NULL;
2236
2237 *state = NULL;
2238 ly_ctx_destroy(ctx.ctx, NULL);
2239}
2240
David Sedlákd6ce6d72019-07-16 17:30:18 +02002241static void
2242test_value(void **state)
2243{
2244 *state = test_value;
2245 struct lys_parser_ctx ctx;
2246
2247 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2248 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002249 ctx.pos_type = LY_VLOG_LINE;
David Sedlákd6ce6d72019-07-16 17:30:18 +02002250 ctx.line = 1;
2251 ctx.indent = 0;
2252 int64_t val = 0;
2253 uint16_t flags = 0;
2254
2255 const char *data = "-0;";
Radek Krejcid6b76452019-09-03 17:03:03 +02002256 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 +02002257 assert_int_equal(val, 0);
2258
2259 data = "-0;";
2260 flags = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02002261 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 +02002262 logbuf_assert("Invalid value \"-0\" of \"position\". Line number 1.");
2263
David Sedlák1af868e2019-07-17 17:03:14 +02002264 *state = NULL;
David Sedlákd6ce6d72019-07-16 17:30:18 +02002265 ly_ctx_destroy(ctx.ctx, NULL);
2266}
2267
Radek Krejci80dd33e2018-09-26 15:57:18 +02002268int main(void)
2269{
2270 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002271 cmocka_unit_test_setup(test_helpers, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002272 cmocka_unit_test_setup(test_comments, logger_setup),
Radek Krejciefd22f62018-09-27 11:47:58 +02002273 cmocka_unit_test_setup(test_arg, logger_setup),
Radek Krejcidcc7b322018-10-11 14:24:02 +02002274 cmocka_unit_test_setup(test_stmts, logger_setup),
Radek Krejci05b13982018-11-28 16:22:07 +01002275 cmocka_unit_test_setup_teardown(test_minmax, logger_setup, logger_teardown),
Radek Krejci40544fa2019-01-11 09:38:37 +01002276 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002277 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci2c02f3e2018-10-16 10:54:38 +02002278 cmocka_unit_test_setup(test_feature, logger_setup),
2279 cmocka_unit_test_setup(test_deviation, logger_setup),
2280 cmocka_unit_test_setup(test_deviate, logger_setup),
Radek Krejci8c370832018-11-02 15:10:03 +01002281 cmocka_unit_test_setup(test_container, logger_setup),
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002282 cmocka_unit_test_setup_teardown(test_leaf, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01002283 cmocka_unit_test_setup_teardown(test_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002284 cmocka_unit_test_setup_teardown(test_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01002285 cmocka_unit_test_setup_teardown(test_choice, logger_setup, logger_teardown),
Radek Krejcia9026eb2018-12-12 16:04:47 +01002286 cmocka_unit_test_setup_teardown(test_case, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01002287 cmocka_unit_test_setup_teardown(test_anydata, logger_setup, logger_teardown),
2288 cmocka_unit_test_setup_teardown(test_anyxml, logger_setup, logger_teardown),
Radek Krejcif538ce52019-03-05 10:46:14 +01002289 cmocka_unit_test_setup_teardown(test_action, logger_setup, logger_teardown),
Radek Krejcifc11bd72019-04-11 16:00:05 +02002290 cmocka_unit_test_setup_teardown(test_notification, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01002291 cmocka_unit_test_setup_teardown(test_grouping, logger_setup, logger_teardown),
2292 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejcib4adbf12019-02-25 13:35:22 +01002293 cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown),
Radek Krejcif09e4e82019-06-14 15:08:11 +02002294 cmocka_unit_test_setup_teardown(test_when, logger_setup, logger_teardown),
David Sedlákd6ce6d72019-07-16 17:30:18 +02002295 cmocka_unit_test_setup_teardown(test_value, logger_setup, logger_teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002296 };
2297
2298 return cmocka_run_group_tests(tests, NULL, NULL);
2299}