blob: 15dcd8c33fd6ed4d8188bc1ce404c559421d75d8 [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);
38
39LY_ERR buf_add_char(struct ly_ctx *ctx, const char **input, size_t len, char **buf, size_t *buf_len, size_t *buf_used);
40LY_ERR buf_store_char(struct lys_parser_ctx *ctx, const char **input, enum yang_arg arg,
41 char **word_p, size_t *word_len, char **word_b, size_t *buf_len, int need_buf);
42LY_ERR get_keyword(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword *kw, char **word_p, size_t *word_len);
43LY_ERR get_argument(struct lys_parser_ctx *ctx, const char **data, enum yang_arg arg,
44 uint16_t *flags, char **word_p, char **word_b, size_t *word_len);
45LY_ERR skip_comment(struct lys_parser_ctx *ctx, const char **data, int comment);
46LY_ERR check_identifierchar(struct lys_parser_ctx *ctx, unsigned int c, int first, int *prefix);
47
48LY_ERR parse_action(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions);
49LY_ERR parse_any(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword kw, struct lysp_node *parent, struct lysp_node **siblings);
50LY_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 Krejci80dd33e2018-09-26 15:57:18 +020068
69#define BUFSIZE 1024
70char logbuf[BUFSIZE] = {0};
Radek Krejci9ed7a192018-10-31 16:23:51 +010071int store = -1; /* negative for infinite logging, positive for limited logging */
Radek Krejci80dd33e2018-09-26 15:57:18 +020072
73/* set to 0 to printing error messages to stderr instead of checking them in code */
Radek Krejci70853c52018-10-15 14:46:16 +020074#define ENABLE_LOGGER_CHECKING 1
Radek Krejci80dd33e2018-09-26 15:57:18 +020075
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020076#if ENABLE_LOGGER_CHECKING
Radek Krejci80dd33e2018-09-26 15:57:18 +020077static void
78logger(LY_LOG_LEVEL level, const char *msg, const char *path)
79{
80 (void) level; /* unused */
Radek Krejci9ed7a192018-10-31 16:23:51 +010081 if (store) {
82 if (path && path[0]) {
83 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
84 } else {
85 strncpy(logbuf, msg, BUFSIZE - 1);
86 }
87 if (store > 0) {
88 --store;
89 }
Radek Krejci80dd33e2018-09-26 15:57:18 +020090 }
91}
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020092#endif
Radek Krejci80dd33e2018-09-26 15:57:18 +020093
94static int
95logger_setup(void **state)
96{
97 (void) state; /* unused */
98#if ENABLE_LOGGER_CHECKING
99 ly_set_log_clb(logger, 1);
100#endif
101 return 0;
102}
103
Radek Krejcib1a5dcc2018-11-26 14:50:05 +0100104static int
105logger_teardown(void **state)
106{
107 (void) state; /* unused */
108#if ENABLE_LOGGER_CHECKING
109 if (*state) {
110 fprintf(stderr, "%s\n", logbuf);
111 }
112#endif
113 return 0;
114}
115
Radek Krejci80dd33e2018-09-26 15:57:18 +0200116void
117logbuf_clean(void)
118{
119 logbuf[0] = '\0';
120}
121
122#if ENABLE_LOGGER_CHECKING
123# define logbuf_assert(str) assert_string_equal(logbuf, str)
124#else
125# define logbuf_assert(str)
126#endif
127
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200128#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
129 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
130 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
131 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
132 CLEANUP
133
Radek Krejci44ceedc2018-10-02 15:54:31 +0200134static void
135test_helpers(void **state)
136{
137 (void) state; /* unused */
138
139 const char *str;
Radek Krejci404251e2018-10-09 12:06:44 +0200140 char *buf, *p;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200141 size_t len, size;
142 int prefix;
Radek Krejcie7b95092019-05-15 11:03:07 +0200143 struct lys_parser_ctx ctx;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200144 ctx.ctx = NULL;
145 ctx.line = 1;
146
147 /* storing into buffer */
148 str = "abcd";
149 buf = NULL;
150 size = len = 0;
151 assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &str, 2, &buf, &size, &len));
152 assert_int_not_equal(0, size);
153 assert_int_equal(2, len);
154 assert_string_equal("cd", str);
155 assert_false(strncmp("ab", buf, 2));
156 free(buf);
Radek Krejci404251e2018-10-09 12:06:44 +0200157 buf = NULL;
158
159 /* invalid first characters */
160 len = 0;
161 str = "2invalid";
162 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
163 str = ".invalid";
164 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
165 str = "-invalid";
166 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
167 /* invalid following characters */
168 len = 3; /* number of characters read before the str content */
169 str = "!";
170 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
171 str = ":";
172 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
173 /* valid colon for prefixed identifiers */
174 len = size = 0;
175 p = NULL;
176 str = "x:id";
177 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 0));
178 assert_int_equal(1, len);
179 assert_null(buf);
180 assert_string_equal(":id", str);
181 assert_int_equal('x', p[len - 1]);
182 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1));
183 assert_int_equal(2, len);
184 assert_string_equal("id", str);
185 assert_int_equal(':', p[len - 1]);
186 free(buf);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200187
188 /* checking identifiers */
189 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, NULL));
190 logbuf_assert("Invalid identifier character ':'. Line number 1.");
191 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, '#', 1, NULL));
192 logbuf_assert("Invalid identifier first character '#'. Line number 1.");
193
194 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'a', 1, &prefix));
195 assert_int_equal(0, prefix);
196 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, ':', 0, &prefix));
197 assert_int_equal(1, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200198 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix));
199 assert_int_equal(1, prefix);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200200 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'b', 0, &prefix));
201 assert_int_equal(2, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200202 /* second colon is invalid */
203 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix));
204 logbuf_assert("Invalid identifier character ':'. Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200205}
Radek Krejci80dd33e2018-09-26 15:57:18 +0200206
207static void
208test_comments(void **state)
209{
210 (void) state; /* unused */
211
Radek Krejcie7b95092019-05-15 11:03:07 +0200212 struct lys_parser_ctx ctx;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200213 const char *str, *p;
Radek Krejciefd22f62018-09-27 11:47:58 +0200214 char *word, *buf;
215 size_t len;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200216
Radek Krejci44ceedc2018-10-02 15:54:31 +0200217 ctx.ctx = NULL;
218 ctx.line = 1;
219
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200220 str = " // this is a text of / one * line */ comment\nargument;";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200221 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200222 assert_string_equal("argument;", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200223 assert_null(buf);
224 assert_int_equal(8, len);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200225
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200226 str = "/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\";";
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 Krejciefd22f62018-09-27 11:47:58 +0200228 assert_string_equal("argument", word);
229 assert_ptr_equal(buf, word);
230 assert_int_equal(8, len);
231 free(word);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200232
233 str = p = " this is one line comment on last line";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200234 assert_int_equal(LY_SUCCESS, skip_comment(&ctx, &str, 1));
Radek Krejci80dd33e2018-09-26 15:57:18 +0200235 assert_true(str[0] == '\0');
236
237 str = p = " this is a not terminated comment x";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200238 assert_int_equal(LY_EVALID, skip_comment(&ctx, &str, 2));
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200239 logbuf_assert("Unexpected end-of-input, non-terminated comment. Line number 5.");
Radek Krejci80dd33e2018-09-26 15:57:18 +0200240 assert_true(str[0] == '\0');
241}
242
Radek Krejciefd22f62018-09-27 11:47:58 +0200243static void
244test_arg(void **state)
245{
246 (void) state; /* unused */
247
Radek Krejcie7b95092019-05-15 11:03:07 +0200248 struct lys_parser_ctx ctx;
Radek Krejciefd22f62018-09-27 11:47:58 +0200249 const char *str;
250 char *word, *buf;
251 size_t len;
252
Radek Krejci44ceedc2018-10-02 15:54:31 +0200253 ctx.ctx = NULL;
254 ctx.line = 1;
255
Radek Krejciefd22f62018-09-27 11:47:58 +0200256 /* missing argument */
257 str = ";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200258 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200259 assert_null(word);
260
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200261 str = "{";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200262 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200263 logbuf_assert("Invalid character sequence \"{\", expected an argument. Line number 1.");
264
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200265 /* invalid escape sequence */
266 str = "\"\\s\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200267 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200268 logbuf_assert("Double-quoted string unknown special character \'\\s\'. Line number 1.");
269 str = "\'\\s\'"; /* valid, since it is not an escape sequence in single quoted string */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200270 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200271 assert_int_equal(2, len);
272 assert_string_equal("\\s\'", word);
273 assert_int_equal('\0', str[0]); /* input has been eaten */
274
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200275 /* invalid character after the argument */
276 str = "hello\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200277 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200278 logbuf_assert("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
279 str = "hello}";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200280 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200281 logbuf_assert("Invalid character sequence \"}\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
282
Radek Krejci4e199f52019-05-28 09:09:28 +0200283 str = "\"\";"; /* empty identifier is not allowed */
284 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_IDENTIF_ARG, NULL, &word, &buf, &len));
285 logbuf_assert("Statement argument is required. Line number 1.");
286 logbuf_clean();
287 str = "\"\";"; /* empty reference identifier is not allowed */
288 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
289 logbuf_assert("Statement argument is required. Line number 1.");
290
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200291 str = "hello/x\t"; /* slash is not an invalid character */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200292 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200293 assert_int_equal(7, len);
294 assert_string_equal("hello/x\t", word);
295
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200296 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200297
298 /* different quoting */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200299 str = "hello ";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200300 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200301 assert_null(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200302 assert_int_equal(5, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200303 assert_string_equal("hello ", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200304
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200305 str = "hello/*comment*/\n";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200306 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200307 assert_null(buf);
308 assert_int_equal(5, len);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200309 assert_false(strncmp("hello", word, len));
310
311
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200312 str = "\"hello\\n\\t\\\"\\\\\";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200313 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200314 assert_null(buf);
315 assert_int_equal(9, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200316 assert_string_equal("hello\\n\\t\\\"\\\\\";", word);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200317
318 ctx.indent = 14;
319 str = "\"hello \t\n\t\t world!\"";
320 /* - space and tabs before newline are stripped out
321 * - space and tabs after newline (indentation) are stripped out
322 */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200323 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200324 assert_non_null(buf);
325 assert_ptr_equal(word, buf);
326 assert_int_equal(14, len);
327 assert_string_equal("hello\n world!", word);
328 free(buf);
329
330 ctx.indent = 14;
331 str = "\"hello\n \tworld!\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200332 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200333 assert_non_null(buf);
334 assert_ptr_equal(word, buf);
335 assert_int_equal(12, len);
336 assert_string_equal("hello\nworld!", word);
337 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200338
339 str = "\'hello\'";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200340 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200341 assert_null(buf);
342 assert_int_equal(5, len);
343 assert_false(strncmp("hello", word, 5));
344
345 str = "\"hel\" +\t\n\"lo\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200346 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200347 assert_ptr_equal(word, buf);
348 assert_int_equal(5, len);
349 assert_string_equal("hello", word);
350 free(buf);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200351 str = "\"hel\" +\t\nlo"; /* unquoted the second part */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200352 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200353 logbuf_assert("Both string parts divided by '+' must be quoted. Line number 5.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200354
355 str = "\'he\'\t\n+ \"llo\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200356 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200357 assert_ptr_equal(word, buf);
358 assert_int_equal(5, len);
359 assert_string_equal("hello", word);
360 free(buf);
361
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200362 str = " \t\n\"he\"+\'llo\'";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200363 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200364 assert_ptr_equal(word, buf);
365 assert_int_equal(5, len);
366 assert_string_equal("hello", word);
367 free(buf);
368
Radek Krejci44ceedc2018-10-02 15:54:31 +0200369 /* missing argument */
370 str = ";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200371 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200372 logbuf_assert("Invalid character sequence \";\", expected an argument. Line number 7.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200373}
374
375static void
376test_stmts(void **state)
377{
378 (void) state; /* unused */
379
Radek Krejcie7b95092019-05-15 11:03:07 +0200380 struct lys_parser_ctx ctx;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200381 const char *str, *p;
382 enum yang_keyword kw;
383 char *word;
384 size_t len;
385
386 ctx.ctx = NULL;
387 ctx.line = 1;
388
389 str = "\n// comment\n\tinput\t{";
390 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
391 assert_int_equal(YANG_INPUT, kw);
392 assert_int_equal(5, len);
393 assert_string_equal("input\t{", word);
394 assert_string_equal("\t{", str);
395
396 str = "\t /* comment */\t output\n\t{";
397 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
398 assert_int_equal(YANG_OUTPUT, kw);
399 assert_int_equal(6, len);
400 assert_string_equal("output\n\t{", word);
401 assert_string_equal("\n\t{", str);
402
403 str = "/input { "; /* invalid slash */
404 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
405 logbuf_assert("Invalid identifier first character '/'. Line number 4.");
406
407 str = "not-a-statement-nor-extension { "; /* invalid identifier */
408 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
409 logbuf_assert("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword. Line number 4.");
410
411 str = "path;"; /* missing sep after the keyword */
412 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
413 logbuf_assert("Invalid character sequence \"path;\", expected a keyword followed by a separator. Line number 4.");
414
415 str = "action ";
416 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
417 assert_int_equal(YANG_ACTION, kw);
418 assert_int_equal(6, len);
419 str = "anydata ";
420 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
421 assert_int_equal(YANG_ANYDATA, kw);
422 assert_int_equal(7, len);
423 str = "anyxml ";
424 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
425 assert_int_equal(YANG_ANYXML, kw);
426 assert_int_equal(6, len);
427 str = "argument ";
428 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
429 assert_int_equal(YANG_ARGUMENT, kw);
430 assert_int_equal(8, len);
431 str = "augment ";
432 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
433 assert_int_equal(YANG_AUGMENT, kw);
434 assert_int_equal(7, len);
435 str = "base ";
436 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
437 assert_int_equal(YANG_BASE, kw);
438 assert_int_equal(4, len);
439 str = "belongs-to ";
440 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
441 assert_int_equal(YANG_BELONGS_TO, kw);
442 assert_int_equal(10, len);
443 str = "bit ";
444 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
445 assert_int_equal(YANG_BIT, kw);
446 assert_int_equal(3, len);
447 str = "case ";
448 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
449 assert_int_equal(YANG_CASE, kw);
450 assert_int_equal(4, len);
451 str = "choice ";
452 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
453 assert_int_equal(YANG_CHOICE, kw);
454 assert_int_equal(6, len);
455 str = "config ";
456 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
457 assert_int_equal(YANG_CONFIG, kw);
458 assert_int_equal(6, len);
459 str = "contact ";
460 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
461 assert_int_equal(YANG_CONTACT, kw);
462 assert_int_equal(7, len);
463 str = "container ";
464 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
465 assert_int_equal(YANG_CONTAINER, kw);
466 assert_int_equal(9, len);
467 str = "default ";
468 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
469 assert_int_equal(YANG_DEFAULT, kw);
470 assert_int_equal(7, len);
471 str = "description ";
472 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
473 assert_int_equal(YANG_DESCRIPTION, kw);
474 assert_int_equal(11, len);
475 str = "deviate ";
476 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
477 assert_int_equal(YANG_DEVIATE, kw);
478 assert_int_equal(7, len);
479 str = "deviation ";
480 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
481 assert_int_equal(YANG_DEVIATION, kw);
482 assert_int_equal(9, len);
483 str = "enum ";
484 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
485 assert_int_equal(YANG_ENUM, kw);
486 assert_int_equal(4, len);
487 str = "error-app-tag ";
488 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
489 assert_int_equal(YANG_ERROR_APP_TAG, kw);
490 assert_int_equal(13, len);
491 str = "error-message ";
492 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
493 assert_int_equal(YANG_ERROR_MESSAGE, kw);
494 assert_int_equal(13, len);
495 str = "extension ";
496 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
497 assert_int_equal(YANG_EXTENSION, kw);
498 assert_int_equal(9, len);
499 str = "feature ";
500 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
501 assert_int_equal(YANG_FEATURE, kw);
502 assert_int_equal(7, len);
503 str = "fraction-digits ";
504 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
505 assert_int_equal(YANG_FRACTION_DIGITS, kw);
506 assert_int_equal(15, len);
507 str = "grouping ";
508 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
509 assert_int_equal(YANG_GROUPING, kw);
510 assert_int_equal(8, len);
511 str = "identity ";
512 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
513 assert_int_equal(YANG_IDENTITY, kw);
514 assert_int_equal(8, len);
515 str = "if-feature ";
516 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
517 assert_int_equal(YANG_IF_FEATURE, kw);
518 assert_int_equal(10, len);
519 str = "import ";
520 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
521 assert_int_equal(YANG_IMPORT, kw);
522 assert_int_equal(6, len);
523 str = "include ";
524 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
525 assert_int_equal(YANG_INCLUDE, kw);
526 assert_int_equal(7, len);
527 str = "input{";
528 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
529 assert_int_equal(YANG_INPUT, kw);
530 assert_int_equal(5, len);
531 str = "key ";
532 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
533 assert_int_equal(YANG_KEY, kw);
534 assert_int_equal(3, len);
535 str = "leaf ";
536 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
537 assert_int_equal(YANG_LEAF, kw);
538 assert_int_equal(4, len);
539 str = "leaf-list ";
540 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
541 assert_int_equal(YANG_LEAF_LIST, kw);
542 assert_int_equal(9, len);
543 str = "length ";
544 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
545 assert_int_equal(YANG_LENGTH, kw);
546 assert_int_equal(6, len);
547 str = "list ";
548 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
549 assert_int_equal(YANG_LIST, kw);
550 assert_int_equal(4, len);
551 str = "mandatory ";
552 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
553 assert_int_equal(YANG_MANDATORY, kw);
554 assert_int_equal(9, len);
555 str = "max-elements ";
556 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
557 assert_int_equal(YANG_MAX_ELEMENTS, kw);
558 assert_int_equal(12, len);
559 str = "min-elements ";
560 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
561 assert_int_equal(YANG_MIN_ELEMENTS, kw);
562 assert_int_equal(12, len);
563 str = "modifier ";
564 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
565 assert_int_equal(YANG_MODIFIER, kw);
566 assert_int_equal(8, len);
567 str = "module ";
568 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
569 assert_int_equal(YANG_MODULE, kw);
570 assert_int_equal(6, len);
571 str = "must ";
572 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
573 assert_int_equal(YANG_MUST, kw);
574 assert_int_equal(4, len);
575 str = "namespace ";
576 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
577 assert_int_equal(YANG_NAMESPACE, kw);
578 assert_int_equal(9, len);
579 str = "notification ";
580 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
581 assert_int_equal(YANG_NOTIFICATION, kw);
582 assert_int_equal(12, len);
583 str = "ordered-by ";
584 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
585 assert_int_equal(YANG_ORDERED_BY, kw);
586 assert_int_equal(10, len);
587 str = "organization ";
588 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
589 assert_int_equal(YANG_ORGANIZATION, kw);
590 assert_int_equal(12, len);
591 str = "output ";
592 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
593 assert_int_equal(YANG_OUTPUT, kw);
594 assert_int_equal(6, len);
595 str = "path ";
596 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
597 assert_int_equal(YANG_PATH, kw);
598 assert_int_equal(4, len);
599 str = "pattern ";
600 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
601 assert_int_equal(YANG_PATTERN, kw);
602 assert_int_equal(7, len);
603 str = "position ";
604 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
605 assert_int_equal(YANG_POSITION, kw);
606 assert_int_equal(8, len);
607 str = "prefix ";
608 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
609 assert_int_equal(YANG_PREFIX, kw);
610 assert_int_equal(6, len);
611 str = "presence ";
612 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
613 assert_int_equal(YANG_PRESENCE, kw);
614 assert_int_equal(8, len);
615 str = "range ";
616 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
617 assert_int_equal(YANG_RANGE, kw);
618 assert_int_equal(5, len);
619 str = "reference ";
620 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
621 assert_int_equal(YANG_REFERENCE, kw);
622 assert_int_equal(9, len);
623 str = "refine ";
624 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
625 assert_int_equal(YANG_REFINE, kw);
626 assert_int_equal(6, len);
627 str = "require-instance ";
628 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
629 assert_int_equal(YANG_REQUIRE_INSTANCE, kw);
630 assert_int_equal(16, len);
631 str = "revision ";
632 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
633 assert_int_equal(YANG_REVISION, kw);
634 assert_int_equal(8, len);
635 str = "revision-date ";
636 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
637 assert_int_equal(YANG_REVISION_DATE, kw);
638 assert_int_equal(13, len);
639 str = "rpc ";
640 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
641 assert_int_equal(YANG_RPC, kw);
642 assert_int_equal(3, len);
643 str = "status ";
644 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
645 assert_int_equal(YANG_STATUS, kw);
646 assert_int_equal(6, len);
647 str = "submodule ";
648 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
649 assert_int_equal(YANG_SUBMODULE, kw);
650 assert_int_equal(9, len);
651 str = "type ";
652 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
653 assert_int_equal(YANG_TYPE, kw);
654 assert_int_equal(4, len);
655 str = "typedef ";
656 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
657 assert_int_equal(YANG_TYPEDEF, kw);
658 assert_int_equal(7, len);
659 str = "unique ";
660 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
661 assert_int_equal(YANG_UNIQUE, kw);
662 assert_int_equal(6, len);
663 str = "units ";
664 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
665 assert_int_equal(YANG_UNITS, kw);
666 assert_int_equal(5, len);
667 str = "uses ";
668 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
669 assert_int_equal(YANG_USES, kw);
670 assert_int_equal(4, len);
671 str = "value ";
672 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
673 assert_int_equal(YANG_VALUE, kw);
674 assert_int_equal(5, len);
675 str = "when ";
676 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
677 assert_int_equal(YANG_WHEN, kw);
678 assert_int_equal(4, len);
679 str = "yang-version ";
680 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
681 assert_int_equal(YANG_YANG_VERSION, kw);
682 assert_int_equal(12, len);
683 str = "yin-element ";
684 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
685 assert_int_equal(YANG_YIN_ELEMENT, kw);
686 assert_int_equal(11, len);
Radek Krejci626df482018-10-11 15:06:31 +0200687 str = ";config false;";
688 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
689 assert_int_equal(YANG_SEMICOLON, kw);
690 assert_int_equal(1, len);
691 assert_string_equal("config false;", str);
692 str = "{ config false;";
693 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
694 assert_int_equal(YANG_LEFT_BRACE, kw);
695 assert_int_equal(1, len);
696 assert_string_equal(" config false;", str);
697 str = "}";
698 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
699 assert_int_equal(YANG_RIGHT_BRACE, kw);
700 assert_int_equal(1, len);
701 assert_string_equal("", str);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200702
703 /* geenric extension */
704 str = p = "nacm:default-deny-write;";
705 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
706 assert_int_equal(YANG_CUSTOM, kw);
707 assert_int_equal(23, len);
708 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200709}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200710
Radek Krejci05b13982018-11-28 16:22:07 +0100711static void
712test_minmax(void **state)
713{
714 *state = test_minmax;
715
Radek Krejcie7b95092019-05-15 11:03:07 +0200716 struct lys_parser_ctx ctx = {0};
Radek Krejci05b13982018-11-28 16:22:07 +0100717 uint16_t flags = 0;
718 uint32_t value = 0;
719 struct lysp_ext_instance *ext = NULL;
720 const char *str;
721
722 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
723 assert_non_null(ctx.ctx);
724 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100725 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci05b13982018-11-28 16:22:07 +0100726
Radek Krejcidf6cad12018-11-28 17:10:55 +0100727 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100728 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100729 logbuf_assert("Invalid value \"1invalid\" of \"min-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100730
731 flags = value = 0;
732 str = " -1; ...";
733 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
734 logbuf_assert("Invalid value \"-1\" of \"min-elements\". Line number 1.");
735
Radek Krejcidf6cad12018-11-28 17:10:55 +0100736 /* implementation limit */
737 flags = value = 0;
738 str = " 4294967296; ...";
739 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
740 logbuf_assert("Value \"4294967296\" is out of \"min-elements\" bounds. Line number 1.");
741
Radek Krejci05b13982018-11-28 16:22:07 +0100742 flags = value = 0;
743 str = " 1; ...";
744 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
745 assert_int_equal(LYS_SET_MIN, flags);
746 assert_int_equal(1, value);
747
748 flags = value = 0;
749 str = " 1 {m:ext;} ...";
750 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
751 assert_int_equal(LYS_SET_MIN, flags);
752 assert_int_equal(1, value);
753 assert_non_null(ext);
754 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
755 ext = NULL;
756
757 flags = value = 0;
758 str = " 1 {config true;} ...";
759 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
760 logbuf_assert("Invalid keyword \"config\" as a child of \"min-elements\". Line number 1.");
761
Radek Krejcidf6cad12018-11-28 17:10:55 +0100762 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100763 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100764 logbuf_assert("Invalid value \"1invalid\" of \"max-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100765
766 flags = value = 0;
767 str = " -1; ...";
768 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
769 logbuf_assert("Invalid value \"-1\" of \"max-elements\". Line number 1.");
770
Radek Krejcidf6cad12018-11-28 17:10:55 +0100771 /* implementation limit */
772 flags = value = 0;
773 str = " 4294967296; ...";
774 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
775 logbuf_assert("Value \"4294967296\" is out of \"max-elements\" bounds. Line number 1.");
776
Radek Krejci05b13982018-11-28 16:22:07 +0100777 flags = value = 0;
778 str = " 1; ...";
779 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
780 assert_int_equal(LYS_SET_MAX, flags);
781 assert_int_equal(1, value);
782
783 flags = value = 0;
784 str = " unbounded; ...";
785 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
786 assert_int_equal(LYS_SET_MAX, flags);
787 assert_int_equal(0, value);
788
789 flags = value = 0;
790 str = " 1 {m:ext;} ...";
791 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
792 assert_int_equal(LYS_SET_MAX, flags);
793 assert_int_equal(1, value);
794 assert_non_null(ext);
795 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
796 ext = NULL;
797
798 flags = value = 0;
799 str = " 1 {config true;} ...";
800 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
801 logbuf_assert("Invalid keyword \"config\" as a child of \"max-elements\". Line number 1.");
802
803 *state = NULL;
804 ly_ctx_destroy(ctx.ctx, NULL);
805}
806
Radek Krejci9fcacc12018-10-11 15:59:11 +0200807static struct lysp_module *
Radek Krejcie7b95092019-05-15 11:03:07 +0200808mod_renew(struct lys_parser_ctx *ctx)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200809{
Radek Krejci40544fa2019-01-11 09:38:37 +0100810 struct lysp_module *mod_p;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100811 static struct lys_module mod = {0};
812
813 lysc_module_free(mod.compiled, NULL);
814 lysp_module_free(mod.parsed);
815 FREE_STRING(mod.ctx, mod.name);
816 FREE_STRING(mod.ctx, mod.ns);
817 FREE_STRING(mod.ctx, mod.prefix);
818 FREE_STRING(mod.ctx, mod.filepath);
819 FREE_STRING(mod.ctx, mod.org);
820 FREE_STRING(mod.ctx, mod.contact);
821 FREE_STRING(mod.ctx, mod.dsc);
822 FREE_STRING(mod.ctx, mod.ref);
823 memset(&mod, 0, sizeof mod);
824 mod.ctx = ctx->ctx;
825
826 mod_p = calloc(1, sizeof *mod_p);
827 mod.parsed = mod_p;
828 mod_p->mod = &mod;
829 assert_non_null(mod_p);
830 return mod_p;
831}
832
833static struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +0200834submod_renew(struct lys_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100835{
836 lysp_submodule_free(ctx->ctx, submod);
837 submod = calloc(1, sizeof *submod);
838 assert_non_null(submod);
839 return submod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200840}
841
Radek Krejcid33273d2018-10-25 14:55:52 +0200842static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
843 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
844 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
845{
846 *module_data = user_data;
847 *format = LYS_IN_YANG;
848 *free_module_data = NULL;
849 return LY_SUCCESS;
850}
851
Radek Krejci9fcacc12018-10-11 15:59:11 +0200852static void
853test_module(void **state)
854{
Radek Krejci40544fa2019-01-11 09:38:37 +0100855 *state = test_module;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200856
Radek Krejcie7b95092019-05-15 11:03:07 +0200857 struct lys_parser_ctx ctx;
Radek Krejci40544fa2019-01-11 09:38:37 +0100858 struct lysp_module *mod = NULL;
859 struct lysp_submodule *submod = NULL;
860 struct lys_module *m;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200861 const char *str;
862
863 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
864 assert_non_null(ctx.ctx);
865 ctx.line = 1;
Radek Krejcia042ea12018-10-13 07:52:15 +0200866 ctx.indent = 0;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200867
Radek Krejci40544fa2019-01-11 09:38:37 +0100868 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200869
870 /* missing mandatory substatements */
871 str = " name {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100872 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
873 assert_string_equal("name", mod->mod->name);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200874 logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100875 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200876
877 str = " name {namespace urn:x;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100878 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
879 assert_string_equal("urn:x", mod->mod->ns);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200880 logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100881 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200882
883 str = " name {namespace urn:x;prefix \"x\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100884 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
885 assert_string_equal("x", mod->mod->prefix);
Radek Krejci40544fa2019-01-11 09:38:37 +0100886 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200887
Radek Krejci027d5802018-11-14 16:57:28 +0100888#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
889#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200890#define TEST_NODE(NODETYPE, INPUT, NAME) \
891 str = SCHEMA_BEGINNING INPUT; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100892 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200893 assert_non_null(mod->data); \
894 assert_int_equal(NODETYPE, mod->data->nodetype); \
895 assert_string_equal(NAME, mod->data->name); \
Radek Krejci40544fa2019-01-11 09:38:37 +0100896 mod = mod_renew(&ctx);
Radek Krejcia042ea12018-10-13 07:52:15 +0200897#define TEST_GENERIC(INPUT, TARGET, TEST) \
898 str = SCHEMA_BEGINNING INPUT; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100899 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200900 assert_non_null(TARGET); \
901 TEST; \
Radek Krejci40544fa2019-01-11 09:38:37 +0100902 mod = mod_renew(&ctx);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100903#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200904 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
Radek Krejci40544fa2019-01-11 09:38:37 +0100905 parse_module, mod, LINE, mod = mod_renew(&ctx))
Radek Krejcia042ea12018-10-13 07:52:15 +0200906
907 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100908 TEST_DUP("namespace", "y", "z", "1");
909 TEST_DUP("prefix", "y", "z", "1");
910 TEST_DUP("contact", "a", "b", "1");
911 TEST_DUP("description", "a", "b", "1");
912 TEST_DUP("organization", "a", "b", "1");
913 TEST_DUP("reference", "a", "b", "1");
Radek Krejcia042ea12018-10-13 07:52:15 +0200914
Radek Krejci70853c52018-10-15 14:46:16 +0200915 /* not allowed in module (submodule-specific) */
916 str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100917 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200918 logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100919 mod = mod_renew(&ctx);
Radek Krejci70853c52018-10-15 14:46:16 +0200920
Radek Krejcia042ea12018-10-13 07:52:15 +0200921 /* anydata */
922 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
923 /* anyxml */
924 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
925 /* augment */
926 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200927 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200928 /* choice */
929 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
930 /* contact 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100931 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact,
932 assert_string_equal("firstname surname", mod->mod->contact));
Radek Krejcia042ea12018-10-13 07:52:15 +0200933 /* container */
934 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
935 /* description 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100936 TEST_GENERIC("description \'some description\';}", mod->mod->dsc,
937 assert_string_equal("some description", mod->mod->dsc));
Radek Krejcia042ea12018-10-13 07:52:15 +0200938 /* deviation */
939 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200940 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200941 /* extension */
942 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200943 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200944 /* feature */
945 TEST_GENERIC("feature test;}", mod->features,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200946 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200947 /* grouping */
948 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200949 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200950 /* identity */
951 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200952 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200953 /* import */
Radek Krejci086c7132018-10-26 15:29:04 +0200954 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
955 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
956 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200957
Radek Krejcia042ea12018-10-13 07:52:15 +0200958 /* import - prefix collision */
Radek Krejci086c7132018-10-26 15:29:04 +0200959 str = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100960 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200961 logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100962 mod = mod_renew(&ctx);
Radek Krejci086c7132018-10-26 15:29:04 +0200963 str = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100964 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci086c7132018-10-26 15:29:04 +0200965 logbuf_assert("Prefix \"y\" already used to import \"zzz\" module. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100966 mod = mod_renew(&ctx);
Radek Krejci086c7132018-10-26 15:29:04 +0200967 str = "module" SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix z;}}";
968 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
969 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
970 logbuf_assert("Single revision of the module \"zzz\" referred twice.");
Radek Krejci70853c52018-10-15 14:46:16 +0200971
Radek Krejcia042ea12018-10-13 07:52:15 +0200972 /* include */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100973 store = 1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200974 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 +0200975 str = "module" SCHEMA_BEGINNING "include xxx;}";
976 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
977 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100978 logbuf_assert("Input data contains module in situation when a submodule is expected.");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100979 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200980
Radek Krejci9ed7a192018-10-31 16:23:51 +0100981 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +0100982 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 +0200983 str = "module" SCHEMA_BEGINNING "include xxx;}";
984 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
985 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
986 logbuf_assert("Included \"xxx\" submodule from \"name\" belongs-to a different module \"wrong-name\".");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100987 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200988
Radek Krejci313d9902018-11-08 09:42:58 +0100989 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 +0200990 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejci086c7132018-10-26 15:29:04 +0200991 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +0200992
Radek Krejcia042ea12018-10-13 07:52:15 +0200993 /* leaf */
994 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
995 /* leaf-list */
996 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
997 /* list */
998 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
999 /* notification */
1000 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001001 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001002 /* organization 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001003 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org,
1004 assert_string_equal("CESNET a.l.e.", mod->mod->org));
Radek Krejcia042ea12018-10-13 07:52:15 +02001005 /* reference 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001006 TEST_GENERIC("reference RFC7950;}", mod->mod->ref,
1007 assert_string_equal("RFC7950", mod->mod->ref));
Radek Krejcia042ea12018-10-13 07:52:15 +02001008 /* revision */
1009 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib7db73a2018-10-24 14:18:40 +02001010 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +02001011 /* rpc */
1012 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001013 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001014 /* typedef */
1015 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001016 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001017 /* uses */
1018 TEST_NODE(LYS_USES, "uses test;}", "test");
1019 /* yang-version */
Radek Krejci027d5802018-11-14 16:57:28 +01001020 str = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001021 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +02001022 logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001023 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001024 str = SCHEMA_BEGINNING2 "yang-version 1.0;yang-version 1.1;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001025 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +02001026 logbuf_assert("Duplicate keyword \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001027 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001028 str = SCHEMA_BEGINNING2 "yang-version 1.0;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001029 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
1030 assert_int_equal(1, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +01001031 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001032 str = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001033 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
1034 assert_int_equal(2, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +01001035 mod = mod_renew(&ctx);
1036
1037 str = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
1038 m = mod->mod;
1039 free(mod);
1040 m->parsed = NULL;
1041 assert_int_equal(LY_EVALID, yang_parse_module(&ctx, str, m));
Radek Krejci0a1d0d42019-05-16 15:14:51 +02001042 logbuf_assert("Trailing garbage \"module q {names...\" after module, expected end-of-input. Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001043 mod = mod_renew(&ctx);
1044
1045 str = "prefix " SCHEMA_BEGINNING "}";
1046 m = mod->mod;
1047 free(mod);
1048 m->parsed = NULL;
1049 assert_int_equal(LY_EVALID, yang_parse_module(&ctx, str, m));
1050 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 3.");
1051 mod = mod_renew(&ctx);
Radek Krejci09306362018-10-15 15:26:01 +02001052
Radek Krejci156ccaf2018-10-15 15:49:17 +02001053 /* extensions */
1054 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001055 assert_string_equal("prefix:test", mod->exts[0].name);
1056 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
Radek Krejci40544fa2019-01-11 09:38:37 +01001057 mod = mod_renew(&ctx);
Radek Krejci156ccaf2018-10-15 15:49:17 +02001058
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001059 /* invalid substatement */
1060 str = SCHEMA_BEGINNING "must false;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001061 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001062 logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001063 mod = mod_renew(&ctx);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001064
Radek Krejci09306362018-10-15 15:26:01 +02001065 /* submodule */
Radek Krejci40544fa2019-01-11 09:38:37 +01001066 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001067
1068 /* missing mandatory substatements */
1069 str = " subname {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001070 lydict_remove(ctx.ctx, submod->name);
1071 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
1072 assert_string_equal("subname", submod->name);
Radek Krejci09306362018-10-15 15:26:01 +02001073 logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001074 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001075
Radek Krejci313d9902018-11-08 09:42:58 +01001076 str = " subname {belongs-to name {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001077 lydict_remove(ctx.ctx, submod->name);
1078 assert_int_equal(LY_SUCCESS, parse_submodule(&ctx, &str, submod));
1079 assert_string_equal("name", submod->belongsto);
1080 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001081
1082#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +01001083#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +02001084
1085 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001086 str = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ...";
1087 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod)); \
1088 logbuf_assert("Duplicate keyword \"belongs-to\". Line number 3."); \
1089 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001090
1091 /* not allowed in submodule (module-specific) */
1092 str = SCHEMA_BEGINNING "namespace \"urn:z\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001093 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001094 logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001095 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001096 str = SCHEMA_BEGINNING "prefix m;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001097 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001098 logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001099 submod = submod_renew(&ctx, submod);
Radek Krejcia042ea12018-10-13 07:52:15 +02001100
Radek Krejci40544fa2019-01-11 09:38:37 +01001101 str = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
1102 lysp_submodule_free(ctx.ctx, submod);
1103 submod = NULL;
1104 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx, str, &submod));
Radek Krejci0a1d0d42019-05-16 15:14:51 +02001105 logbuf_assert("Trailing garbage \"module q {names...\" after submodule, expected end-of-input. Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001106
1107 str = "prefix " SCHEMA_BEGINNING "}";
1108 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx, str, &submod));
1109 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 3.");
1110 submod = submod_renew(&ctx, submod);
1111
Radek Krejcia042ea12018-10-13 07:52:15 +02001112#undef TEST_GENERIC
1113#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001114#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +02001115#undef SCHEMA_BEGINNING
1116
Radek Krejci9fcacc12018-10-11 15:59:11 +02001117 lysp_module_free(mod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001118 lysp_submodule_free(ctx.ctx, submod);
Radek Krejci9fcacc12018-10-11 15:59:11 +02001119 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci40544fa2019-01-11 09:38:37 +01001120
1121 *state = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02001122}
1123
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001124static void
1125test_identity(void **state)
1126{
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001127 *state = test_identity;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001128
Radek Krejcie7b95092019-05-15 11:03:07 +02001129 struct lys_parser_ctx ctx;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001130 struct lysp_ident *ident = NULL;
1131 const char *str;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001132
1133 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1134 assert_non_null(ctx.ctx);
1135 ctx.line = 1;
1136 ctx.indent = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001137 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001138
1139 /* invalid cardinality */
1140#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001141 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001142 &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL)
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001143
Radek Krejci38222632019-02-12 16:55:05 +01001144 TEST_DUP("description", "a", "b");
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001145 TEST_DUP("reference", "a", "b");
1146 TEST_DUP("status", "current", "obsolete");
1147
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001148 /* full content */
1149 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 +02001150 assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident));
1151 assert_non_null(ident);
1152 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001153 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001154 ident = NULL;
1155
1156 /* invalid substatement */
1157 str = " test {organization XXX;}";
1158 assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident));
1159 logbuf_assert("Invalid keyword \"organization\" as a child of \"identity\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001160 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001161 ident = NULL;
1162
1163#undef TEST_DUP
1164
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001165 *state = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001166 ly_ctx_destroy(ctx.ctx, NULL);
1167}
1168
1169static void
1170test_feature(void **state)
1171{
1172 (void) state; /* unused */
1173
Radek Krejcie7b95092019-05-15 11:03:07 +02001174 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001175 struct lysp_feature *features = NULL;
1176 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001177
1178 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1179 assert_non_null(ctx.ctx);
1180 ctx.line = 1;
1181 ctx.indent = 0;
1182
1183 /* invalid cardinality */
1184#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1185 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001186 &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001187
1188 TEST_DUP("description", "a", "b");
1189 TEST_DUP("reference", "a", "b");
1190 TEST_DUP("status", "current", "obsolete");
1191
1192 /* full content */
1193 str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ...";
1194 assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features));
1195 assert_non_null(features);
1196 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001197 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001198 features = NULL;
1199
1200 /* invalid substatement */
1201 str = " test {organization XXX;}";
1202 assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features));
1203 logbuf_assert("Invalid keyword \"organization\" as a child of \"feature\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001204 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001205 features = NULL;
1206
1207#undef TEST_DUP
1208
1209 ly_ctx_destroy(ctx.ctx, NULL);
1210}
1211
1212static void
1213test_deviation(void **state)
1214{
1215 (void) state; /* unused */
1216
Radek Krejcie7b95092019-05-15 11:03:07 +02001217 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001218 struct lysp_deviation *d = NULL;
1219 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001220
1221 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1222 assert_non_null(ctx.ctx);
1223 ctx.line = 1;
1224 ctx.indent = 0;
1225
1226 /* invalid cardinality */
1227#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1228 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001229 &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001230
1231 TEST_DUP("description", "a", "b");
1232 TEST_DUP("reference", "a", "b");
1233
1234 /* full content */
1235 str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
1236 assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d));
1237 assert_non_null(d);
1238 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001239 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001240 d = NULL;
1241
1242 /* missing mandatory substatement */
1243 str = " test {description text;}";
1244 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1245 logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001246 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001247 d = NULL;
1248
1249 /* invalid substatement */
1250 str = " test {deviate not-supported; status obsolete;}";
1251 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1252 logbuf_assert("Invalid keyword \"status\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001253 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001254 d = NULL;
1255
1256#undef TEST_DUP
1257
1258 ly_ctx_destroy(ctx.ctx, NULL);
1259}
1260
1261static void
1262test_deviate(void **state)
1263{
1264 (void) state; /* unused */
1265
Radek Krejcie7b95092019-05-15 11:03:07 +02001266 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001267 struct lysp_deviate *d = NULL;
1268 const char *str;
1269
1270 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1271 assert_non_null(ctx.ctx);
1272 ctx.line = 1;
1273 ctx.indent = 0;
1274
1275 /* invalid cardinality */
1276#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
1277 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001278 &d, "1", lysp_deviate_free(ctx.ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001279
1280 TEST_DUP("add", "config", "true", "false");
1281 TEST_DUP("replace", "default", "int8", "uint8");
1282 TEST_DUP("add", "mandatory", "true", "false");
1283 TEST_DUP("add", "max-elements", "1", "2");
1284 TEST_DUP("add", "min-elements", "1", "2");
1285 TEST_DUP("replace", "type", "int8", "uint8");
1286 TEST_DUP("add", "units", "kilometers", "miles");
1287
1288 /* full contents */
1289 str = " not-supported {prefix:ext;} ...";
1290 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1291 assert_non_null(d);
1292 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001293 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001294 str = " 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;} ...";
1295 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1296 assert_non_null(d);
1297 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001298 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001299 str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...";
1300 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1301 assert_non_null(d);
1302 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001303 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001304 str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...";
1305 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1306 assert_non_null(d);
1307 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001308 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001309
1310 /* invalid substatements */
1311#define TEST_NOT_SUP(DEV, STMT, VALUE) \
1312 str = " "DEV" {"STMT" "VALUE";}..."; \
1313 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \
1314 logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001315 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001316
1317 TEST_NOT_SUP("not-supported", "units", "meters");
1318 TEST_NOT_SUP("not-supported", "must", "1");
1319 TEST_NOT_SUP("not-supported", "unique", "x");
1320 TEST_NOT_SUP("not-supported", "default", "a");
1321 TEST_NOT_SUP("not-supported", "config", "true");
1322 TEST_NOT_SUP("not-supported", "mandatory", "true");
1323 TEST_NOT_SUP("not-supported", "min-elements", "1");
1324 TEST_NOT_SUP("not-supported", "max-elements", "2");
1325 TEST_NOT_SUP("not-supported", "type", "string");
1326 TEST_NOT_SUP("add", "type", "string");
1327 TEST_NOT_SUP("delete", "config", "true");
1328 TEST_NOT_SUP("delete", "mandatory", "true");
1329 TEST_NOT_SUP("delete", "min-elements", "1");
1330 TEST_NOT_SUP("delete", "max-elements", "2");
1331 TEST_NOT_SUP("delete", "type", "string");
1332 TEST_NOT_SUP("replace", "must", "1");
1333 TEST_NOT_SUP("replace", "unique", "a");
1334
1335 str = " nonsence; ...";
1336 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d));
1337 logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1.");
1338 assert_null(d);
1339
1340#undef TEST_NOT_SUP
1341#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001342
1343 ly_ctx_destroy(ctx.ctx, NULL);
1344}
1345
Radek Krejci8c370832018-11-02 15:10:03 +01001346static void
1347test_container(void **state)
1348{
1349 (void) state; /* unused */
1350
Radek Krejcie7b95092019-05-15 11:03:07 +02001351 struct lys_parser_ctx ctx = {0};
Radek Krejci8c370832018-11-02 15:10:03 +01001352 struct lysp_node_container *c = NULL;
1353 const char *str;
1354
1355 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1356 assert_non_null(ctx.ctx);
1357 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001358 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +01001359
1360 /* invalid cardinality */
1361#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1362 str = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1363 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); \
1364 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001365 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001366
1367 TEST_DUP("config", "true", "false");
1368 TEST_DUP("description", "text1", "text2");
1369 TEST_DUP("presence", "true", "false");
1370 TEST_DUP("reference", "1", "2");
1371 TEST_DUP("status", "current", "obsolete");
1372 TEST_DUP("when", "true", "false");
1373#undef TEST_DUP
1374
1375 /* full content */
Radek Krejci313d9902018-11-08 09:42:58 +01001376 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;}"
1377 "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 +01001378 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1379 assert_non_null(c);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001380 assert_int_equal(LYS_CONTAINER, c->nodetype);
1381 assert_string_equal("cont", c->name);
Radek Krejci8c370832018-11-02 15:10:03 +01001382 assert_non_null(c->actions);
1383 assert_non_null(c->child);
1384 assert_string_equal("test", c->dsc);
1385 assert_non_null(c->exts);
1386 assert_non_null(c->groupings);
1387 assert_non_null(c->iffeatures);
1388 assert_non_null(c->musts);
1389 assert_non_null(c->notifs);
1390 assert_string_equal("true", c->presence);
1391 assert_string_equal("test", c->ref);
1392 assert_non_null(c->typedefs);
1393 assert_non_null(c->when);
1394 assert_null(c->parent);
1395 assert_null(c->next);
1396 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags);
Radek Krejci313d9902018-11-08 09:42:58 +01001397 ly_set_erase(&ctx.tpdfs_nodes, NULL);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001398 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001399
1400 /* invalid */
1401 str = " cont {augment /root;} ...";
1402 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1403 logbuf_assert("Invalid keyword \"augment\" as a child of \"container\". Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001404 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001405 str = " cont {nonsence true;} ...";
1406 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1407 logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001408 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001409
Radek Krejcif538ce52019-03-05 10:46:14 +01001410 ctx.mod_version = 1; /* simulate YANG 1.0 */
1411 str = " cont {action x;} ...";
1412 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1413 logbuf_assert("Invalid keyword \"action\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1414 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
1415
Radek Krejci8c370832018-11-02 15:10:03 +01001416 ly_ctx_destroy(ctx.ctx, NULL);
1417}
1418
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001419static void
1420test_leaf(void **state)
1421{
1422 *state = test_leaf;
1423
Radek Krejcie7b95092019-05-15 11:03:07 +02001424 struct lys_parser_ctx ctx = {0};
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001425 struct lysp_node_leaf *l = NULL;
1426 const char *str;
1427
1428 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1429 assert_non_null(ctx.ctx);
1430 ctx.line = 1;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001431 //ctx.mod->version = 2; /* simulate YANG 1.1 */
1432
1433 /* invalid cardinality */
1434#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1435 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1436 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1437 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1438 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1439
1440 TEST_DUP("config", "true", "false");
1441 TEST_DUP("default", "x", "y");
1442 TEST_DUP("description", "text1", "text2");
1443 TEST_DUP("mandatory", "true", "false");
1444 TEST_DUP("reference", "1", "2");
1445 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001446 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001447 TEST_DUP("units", "text1", "text2");
1448 TEST_DUP("when", "true", "false");
1449#undef TEST_DUP
1450
1451 /* full content - without mandatory which is mutual exclusive with default */
1452 str = "l {config false;default \"xxx\";description test;if-feature f;"
1453 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
1454 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1455 assert_non_null(l);
1456 assert_int_equal(LYS_LEAF, l->nodetype);
1457 assert_string_equal("l", l->name);
1458 assert_string_equal("test", l->dsc);
1459 assert_string_equal("xxx", l->dflt);
1460 assert_string_equal("yyy", l->units);
1461 assert_string_equal("string", l->type.name);
1462 assert_non_null(l->exts);
1463 assert_non_null(l->iffeatures);
1464 assert_non_null(l->musts);
1465 assert_string_equal("test", l->ref);
1466 assert_non_null(l->when);
1467 assert_null(l->parent);
1468 assert_null(l->next);
1469 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, l->flags);
1470 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1471
1472 /* full content - now with mandatory */
1473 str = "l {mandatory true; type string;} ...";
1474 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1475 assert_non_null(l);
1476 assert_int_equal(LYS_LEAF, l->nodetype);
1477 assert_string_equal("l", l->name);
1478 assert_string_equal("string", l->type.name);
1479 assert_int_equal(LYS_MAND_TRUE, l->flags);
1480 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1481
1482 /* invalid */
1483 str = " l {mandatory true; default xx; type string;} ...";
1484 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001485 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"leaf\". Line number 1.");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001486 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1487
1488 str = " l {description \"missing type\";} ...";
1489 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1490 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf\". Line number 1.");
1491 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1492
1493 *state = NULL;
1494 ly_ctx_destroy(ctx.ctx, NULL);
1495}
1496
Radek Krejci0e5d8382018-11-28 16:37:53 +01001497static void
1498test_leaflist(void **state)
1499{
1500 *state = test_leaf;
1501
Radek Krejcie7b95092019-05-15 11:03:07 +02001502 struct lys_parser_ctx ctx = {0};
Radek Krejci0e5d8382018-11-28 16:37:53 +01001503 struct lysp_node_leaflist *ll = NULL;
1504 const char *str;
1505
1506 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1507 assert_non_null(ctx.ctx);
1508 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001509 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001510
1511 /* invalid cardinality */
1512#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1513 str = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1514 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); \
1515 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1516 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1517
1518 TEST_DUP("config", "true", "false");
1519 TEST_DUP("description", "text1", "text2");
1520 TEST_DUP("max-elements", "10", "20");
1521 TEST_DUP("min-elements", "10", "20");
1522 TEST_DUP("ordered-by", "user", "system");
1523 TEST_DUP("reference", "1", "2");
1524 TEST_DUP("status", "current", "obsolete");
1525 TEST_DUP("type", "int8", "uint8");
1526 TEST_DUP("units", "text1", "text2");
1527 TEST_DUP("when", "true", "false");
1528#undef TEST_DUP
1529
1530 /* full content - without min-elements which is mutual exclusive with default */
1531 str = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
1532 "max-elements 10;must 'expr';ordered-by user;reference test;"
1533 "status current;type string; units zzz;when true;m:ext;} ...";
1534 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1535 assert_non_null(ll);
1536 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1537 assert_string_equal("ll", ll->name);
1538 assert_string_equal("test", ll->dsc);
1539 assert_non_null(ll->dflts);
1540 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
1541 assert_string_equal("xxx", ll->dflts[0]);
1542 assert_string_equal("yyy", ll->dflts[1]);
1543 assert_string_equal("zzz", ll->units);
1544 assert_int_equal(10, ll->max);
1545 assert_int_equal(0, ll->min);
1546 assert_string_equal("string", ll->type.name);
1547 assert_non_null(ll->exts);
1548 assert_non_null(ll->iffeatures);
1549 assert_non_null(ll->musts);
1550 assert_string_equal("test", ll->ref);
1551 assert_non_null(ll->when);
1552 assert_null(ll->parent);
1553 assert_null(ll->next);
1554 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
1555 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1556
1557 /* full content - now with min-elements */
1558 str = "ll {min-elements 10; type string;} ...";
1559 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1560 assert_non_null(ll);
1561 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1562 assert_string_equal("ll", ll->name);
1563 assert_string_equal("string", ll->type.name);
1564 assert_int_equal(0, ll->max);
1565 assert_int_equal(10, ll->min);
1566 assert_int_equal(LYS_SET_MIN, ll->flags);
1567 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1568
1569 /* invalid */
1570 str = " ll {min-elements 1; default xx; type string;} ...";
1571 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001572 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 +01001573 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1574
1575 str = " ll {description \"missing type\";} ...";
1576 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1577 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf-list\". Line number 1.");
1578 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1579
Radek Krejcidf6cad12018-11-28 17:10:55 +01001580 str = " ll {type string; min-elements 10; max-elements 1;} ..."; /* invalid combination of min/max */
1581 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1582 logbuf_assert("Invalid combination of min-elements and max-elements: min value 10 is bigger than the max value 1. Line number 1.");
1583 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1584
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001585 ctx.mod_version = 1; /* simulate YANG 1.0 - default statement is not allowed */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001586 str = " ll {default xx; type string;} ...";
1587 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1588 logbuf_assert("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1589 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1590
1591 *state = NULL;
1592 ly_ctx_destroy(ctx.ctx, NULL);
1593}
1594
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001595static void
1596test_list(void **state)
1597{
1598 *state = test_list;
1599
Radek Krejcie7b95092019-05-15 11:03:07 +02001600 struct lys_parser_ctx ctx = {0};
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001601 struct lysp_node_list *l = NULL;
1602 const char *str;
1603
1604 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1605 assert_non_null(ctx.ctx);
1606 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001607 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001608
1609 /* invalid cardinality */
1610#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1611 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1612 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1613 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1614 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1615
1616 TEST_DUP("config", "true", "false");
1617 TEST_DUP("description", "text1", "text2");
1618 TEST_DUP("key", "one", "two");
1619 TEST_DUP("max-elements", "10", "20");
1620 TEST_DUP("min-elements", "10", "20");
1621 TEST_DUP("ordered-by", "user", "system");
1622 TEST_DUP("reference", "1", "2");
1623 TEST_DUP("status", "current", "obsolete");
1624 TEST_DUP("when", "true", "false");
1625#undef TEST_DUP
1626
1627 /* full content */
1628 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;}"
1629 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1630 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
1631 assert_int_equal(LY_SUCCESS, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1632 assert_non_null(l);
1633 assert_int_equal(LYS_LIST, l->nodetype);
1634 assert_string_equal("l", l->name);
1635 assert_string_equal("test", l->dsc);
1636 assert_string_equal("l", l->key);
1637 assert_non_null(l->uniques);
1638 assert_int_equal(2, LY_ARRAY_SIZE(l->uniques));
1639 assert_string_equal("xxx", l->uniques[0]);
1640 assert_string_equal("yyy", l->uniques[1]);
1641 assert_int_equal(10, l->max);
1642 assert_int_equal(1, l->min);
1643 assert_non_null(l->exts);
1644 assert_non_null(l->iffeatures);
1645 assert_non_null(l->musts);
1646 assert_string_equal("test", l->ref);
1647 assert_non_null(l->when);
1648 assert_null(l->parent);
1649 assert_null(l->next);
1650 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, l->flags);
1651 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1652 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1653
Radek Krejcif538ce52019-03-05 10:46:14 +01001654 /* invalid content */
1655 ctx.mod_version = 1; /* simulate YANG 1.0 */
1656 str = "l {action x;} ...";
1657 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1658 logbuf_assert("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1659 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1660
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001661 *state = NULL;
1662 ly_ctx_destroy(ctx.ctx, NULL);
1663}
1664
Radek Krejci056d0a82018-12-06 16:57:25 +01001665static void
1666test_choice(void **state)
1667{
1668 *state = test_choice;
1669
Radek Krejcie7b95092019-05-15 11:03:07 +02001670 struct lys_parser_ctx ctx = {0};
Radek Krejci056d0a82018-12-06 16:57:25 +01001671 struct lysp_node_choice *ch = NULL;
1672 const char *str;
1673
1674 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1675 assert_non_null(ctx.ctx);
1676 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001677 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci056d0a82018-12-06 16:57:25 +01001678
1679 /* invalid cardinality */
1680#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1681 str = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1682 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); \
1683 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1684 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1685
1686 TEST_DUP("config", "true", "false");
1687 TEST_DUP("default", "a", "b");
1688 TEST_DUP("description", "text1", "text2");
1689 TEST_DUP("mandatory", "true", "false");
1690 TEST_DUP("reference", "1", "2");
1691 TEST_DUP("status", "current", "obsolete");
1692 TEST_DUP("when", "true", "false");
1693#undef TEST_DUP
1694
Radek Krejcia9026eb2018-12-12 16:04:47 +01001695 /* full content - without default due to a collision with mandatory */
1696 str = "ch {anydata any;anyxml anyxml; case c;choice ch;config false;container c;description test;if-feature f;leaf l {type string;}"
Radek Krejci056d0a82018-12-06 16:57:25 +01001697 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
1698 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1699 assert_non_null(ch);
1700 assert_int_equal(LYS_CHOICE, ch->nodetype);
1701 assert_string_equal("ch", ch->name);
1702 assert_string_equal("test", ch->dsc);
1703 assert_non_null(ch->exts);
1704 assert_non_null(ch->iffeatures);
1705 assert_string_equal("test", ch->ref);
1706 assert_non_null(ch->when);
1707 assert_null(ch->parent);
1708 assert_null(ch->next);
1709 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, ch->flags);
1710 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1711
Radek Krejcia9026eb2018-12-12 16:04:47 +01001712 /* full content - the default missing from the previous node */
1713 str = "ch {default c;case c;} ...";
1714 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1715 assert_non_null(ch);
1716 assert_int_equal(LYS_CHOICE, ch->nodetype);
1717 assert_string_equal("ch", ch->name);
1718 assert_string_equal("c", ch->dflt);
1719 assert_int_equal(0, ch->flags);
1720 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1721
1722 /* invalid content */
1723 str = "ch {mandatory true; default c1; case c1 {leaf x{type string;}}} ...";
1724 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1725 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"choice\". Line number 1.");
1726 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1727
1728 *state = NULL;
1729 ly_ctx_destroy(ctx.ctx, NULL);
1730}
1731
1732static void
1733test_case(void **state)
1734{
1735 *state = test_case;
1736
Radek Krejcie7b95092019-05-15 11:03:07 +02001737 struct lys_parser_ctx ctx = {0};
Radek Krejcia9026eb2018-12-12 16:04:47 +01001738 struct lysp_node_case *cs = NULL;
1739 const char *str;
1740
1741 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1742 assert_non_null(ctx.ctx);
1743 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001744 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcia9026eb2018-12-12 16:04:47 +01001745
1746 /* invalid cardinality */
1747#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1748 str = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1749 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs)); \
1750 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1751 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1752
1753 TEST_DUP("description", "text1", "text2");
1754 TEST_DUP("reference", "1", "2");
1755 TEST_DUP("status", "current", "obsolete");
1756 TEST_DUP("when", "true", "false");
1757#undef TEST_DUP
1758
1759 /* full content */
1760 str = "cs {anydata any;anyxml anyxml; choice ch;container c;description test;if-feature f;leaf l {type string;}"
1761 "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
1762 assert_int_equal(LY_SUCCESS, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1763 assert_non_null(cs);
1764 assert_int_equal(LYS_CASE, cs->nodetype);
1765 assert_string_equal("cs", cs->name);
1766 assert_string_equal("test", cs->dsc);
1767 assert_non_null(cs->exts);
1768 assert_non_null(cs->iffeatures);
1769 assert_string_equal("test", cs->ref);
1770 assert_non_null(cs->when);
1771 assert_null(cs->parent);
1772 assert_null(cs->next);
1773 assert_int_equal(LYS_STATUS_CURR, cs->flags);
1774 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1775
1776 /* invalid content */
1777 str = "cs {config true} ...";
1778 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1779 logbuf_assert("Invalid keyword \"config\" as a child of \"case\". Line number 1.");
1780 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1781
Radek Krejci056d0a82018-12-06 16:57:25 +01001782 *state = NULL;
1783 ly_ctx_destroy(ctx.ctx, NULL);
1784}
1785
Radek Krejci9800fb82018-12-13 14:26:23 +01001786static void
1787test_any(void **state, enum yang_keyword kw)
1788{
1789 *state = test_any;
1790
Radek Krejcie7b95092019-05-15 11:03:07 +02001791 struct lys_parser_ctx ctx = {0};
Radek Krejci9800fb82018-12-13 14:26:23 +01001792 struct lysp_node_anydata *any = NULL;
1793 const char *str;
1794
1795 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1796 assert_non_null(ctx.ctx);
1797 ctx.line = 1;
Radek Krejci9800fb82018-12-13 14:26:23 +01001798 if (kw == YANG_ANYDATA) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001799 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001800 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001801 ctx.mod_version = 1; /* simulate YANG 1.0 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001802 }
1803
1804 /* invalid cardinality */
1805#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1806 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1807 assert_int_equal(LY_EVALID, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any)); \
1808 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1809 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1810
1811 TEST_DUP("config", "true", "false");
1812 TEST_DUP("description", "text1", "text2");
1813 TEST_DUP("mandatory", "true", "false");
1814 TEST_DUP("reference", "1", "2");
1815 TEST_DUP("status", "current", "obsolete");
1816 TEST_DUP("when", "true", "false");
1817#undef TEST_DUP
1818
1819 /* full content */
1820 str = "any {config true;description test;if-feature f;mandatory true;must 'expr';reference test;status current;when true;m:ext;} ...";
1821 assert_int_equal(LY_SUCCESS, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any));
1822 assert_non_null(any);
1823 assert_int_equal(kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML, any->nodetype);
1824 assert_string_equal("any", any->name);
1825 assert_string_equal("test", any->dsc);
1826 assert_non_null(any->exts);
1827 assert_non_null(any->iffeatures);
1828 assert_non_null(any->musts);
1829 assert_string_equal("test", any->ref);
1830 assert_non_null(any->when);
1831 assert_null(any->parent);
1832 assert_null(any->next);
1833 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags);
1834 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1835
1836 *state = NULL;
1837 ly_ctx_destroy(ctx.ctx, NULL);
1838}
1839
1840static void
1841test_anydata(void **state)
1842{
1843 return test_any(state, YANG_ANYDATA);
1844}
1845
1846static void
1847test_anyxml(void **state)
1848{
1849 return test_any(state, YANG_ANYXML);
1850}
1851
Radek Krejcie86bf772018-12-14 11:39:53 +01001852static void
1853test_grouping(void **state)
1854{
1855 *state = test_grouping;
1856
Radek Krejcie7b95092019-05-15 11:03:07 +02001857 struct lys_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01001858 struct lysp_grp *grp = NULL;
1859 const char *str;
1860
1861 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1862 assert_non_null(ctx.ctx);
1863 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001864 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001865
1866 /* invalid cardinality */
1867#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1868 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1869 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp)); \
1870 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1871 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1872
1873 TEST_DUP("description", "text1", "text2");
1874 TEST_DUP("reference", "1", "2");
1875 TEST_DUP("status", "current", "obsolete");
1876#undef TEST_DUP
1877
1878 /* full content */
1879 str = "grp {action x;anydata any;anyxml anyxml; choice ch;container c;description test;grouping g;leaf l {type string;}"
1880 "leaf-list ll {type string;} list li;notification not;reference test;status current;typedef t {type int8;}uses g;m:ext;} ...";
1881 assert_int_equal(LY_SUCCESS, parse_grouping(&ctx, &str, NULL, &grp));
1882 assert_non_null(grp);
1883 assert_int_equal(LYS_GROUPING, grp->nodetype);
1884 assert_string_equal("grp", grp->name);
1885 assert_string_equal("test", grp->dsc);
1886 assert_non_null(grp->exts);
1887 assert_string_equal("test", grp->ref);
1888 assert_null(grp->parent);
1889 assert_int_equal( LYS_STATUS_CURR, grp->flags);
1890 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1891 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1892
1893 /* invalid content */
1894 str = "grp {config true} ...";
1895 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1896 logbuf_assert("Invalid keyword \"config\" as a child of \"grouping\". Line number 1.");
1897 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1898
1899 str = "grp {must 'expr'} ...";
1900 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1901 logbuf_assert("Invalid keyword \"must\" as a child of \"grouping\". Line number 1.");
1902 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1903
1904 *state = NULL;
1905 ly_ctx_destroy(ctx.ctx, NULL);
1906}
1907
1908static void
Radek Krejcif538ce52019-03-05 10:46:14 +01001909test_action(void **state)
1910{
1911 *state = test_action;
1912
Radek Krejcie7b95092019-05-15 11:03:07 +02001913 struct lys_parser_ctx ctx = {0};
Radek Krejcif538ce52019-03-05 10:46:14 +01001914 struct lysp_action *rpcs = NULL;
1915 struct lysp_node_container *c = NULL;
1916 const char *str;
1917
1918 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1919 assert_non_null(ctx.ctx);
1920 ctx.line = 1;
1921 ctx.mod_version = 2; /* simulate YANG 1.1 */
1922
1923 /* invalid cardinality */
1924#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1925 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1926 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs)); \
1927 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1928 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1929
1930 TEST_DUP("description", "text1", "text2");
1931 TEST_DUP("input", "", "");
1932 TEST_DUP("output", "", "");
1933 TEST_DUP("reference", "1", "2");
1934 TEST_DUP("status", "current", "obsolete");
1935#undef TEST_DUP
1936
1937 /* full content */
1938 str = "top;";
1939 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1940 str = "func {description test;grouping grp;if-feature f;reference test;status current;typedef mytype {type int8;} m:ext;"
1941 "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1942 " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}"
1943 "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1944 " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ...";
1945 assert_int_equal(LY_SUCCESS, parse_action(&ctx, &str, (struct lysp_node*)c, &rpcs));
1946 assert_non_null(rpcs);
1947 assert_int_equal(LYS_ACTION, rpcs->nodetype);
1948 assert_string_equal("func", rpcs->name);
1949 assert_string_equal("test", rpcs->dsc);
1950 assert_non_null(rpcs->exts);
1951 assert_non_null(rpcs->iffeatures);
1952 assert_string_equal("test", rpcs->ref);
1953 assert_non_null(rpcs->groupings);
1954 assert_non_null(rpcs->typedefs);
1955 assert_int_equal(LYS_STATUS_CURR, rpcs->flags);
1956 /* input */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001957 assert_int_equal(rpcs->input.nodetype, LYS_INPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001958 assert_non_null(rpcs->input.groupings);
1959 assert_non_null(rpcs->input.exts);
1960 assert_non_null(rpcs->input.musts);
1961 assert_non_null(rpcs->input.typedefs);
1962 assert_non_null(rpcs->input.data);
1963 /* output */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001964 assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001965 assert_non_null(rpcs->output.groupings);
1966 assert_non_null(rpcs->output.exts);
1967 assert_non_null(rpcs->output.musts);
1968 assert_non_null(rpcs->output.typedefs);
1969 assert_non_null(rpcs->output.data);
1970
1971 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1972 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1973
1974 /* invalid content */
1975 str = "func {config true} ...";
1976 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs));
1977 logbuf_assert("Invalid keyword \"config\" as a child of \"rpc\". Line number 1.");
1978 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1979
1980 *state = NULL;
1981 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
1982 ly_ctx_destroy(ctx.ctx, NULL);
1983}
1984
1985static void
Radek Krejcifc11bd72019-04-11 16:00:05 +02001986test_notification(void **state)
1987{
1988 *state = test_notification;
1989
Radek Krejcie7b95092019-05-15 11:03:07 +02001990 struct lys_parser_ctx ctx = {0};
Radek Krejcifc11bd72019-04-11 16:00:05 +02001991 struct lysp_notif *notifs = NULL;
1992 struct lysp_node_container *c = NULL;
1993 const char *str;
1994
1995 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1996 assert_non_null(ctx.ctx);
1997 ctx.line = 1;
1998 ctx.mod_version = 2; /* simulate YANG 1.1 */
1999
2000 /* invalid cardinality */
2001#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2002 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2003 assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, &notifs)); \
2004 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2005 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2006
2007 TEST_DUP("description", "text1", "text2");
2008 TEST_DUP("reference", "1", "2");
2009 TEST_DUP("status", "current", "obsolete");
2010#undef TEST_DUP
2011
2012 /* full content */
2013 str = "top;";
2014 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
2015 str = "ntf {anydata a1; anyxml a2; choice ch; container c; description test; grouping grp; if-feature f; leaf l {type int8;}"
2016 "leaf-list ll {type int8;} list li; must 1; reference test; status current; typedef mytype {type int8;} uses grp; m:ext;}";
2017 assert_int_equal(LY_SUCCESS, parse_notif(&ctx, &str, (struct lysp_node*)c, &notifs));
2018 assert_non_null(notifs);
2019 assert_int_equal(LYS_NOTIF, notifs->nodetype);
2020 assert_string_equal("ntf", notifs->name);
2021 assert_string_equal("test", notifs->dsc);
2022 assert_non_null(notifs->exts);
2023 assert_non_null(notifs->iffeatures);
2024 assert_string_equal("test", notifs->ref);
2025 assert_non_null(notifs->groupings);
2026 assert_non_null(notifs->typedefs);
2027 assert_non_null(notifs->musts);
2028 assert_non_null(notifs->data);
2029 assert_int_equal(LYS_STATUS_CURR, notifs->flags);
2030
2031 ly_set_erase(&ctx.tpdfs_nodes, NULL);
2032 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2033
2034 /* invalid content */
2035 str = "ntf {config true} ...";
2036 assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, &notifs));
2037 logbuf_assert("Invalid keyword \"config\" as a child of \"notification\". Line number 1.");
2038 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2039
2040 *state = NULL;
2041 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
2042 ly_ctx_destroy(ctx.ctx, NULL);
2043}
2044
2045static void
Radek Krejcie86bf772018-12-14 11:39:53 +01002046test_uses(void **state)
2047{
2048 *state = test_uses;
2049
Radek Krejcie7b95092019-05-15 11:03:07 +02002050 struct lys_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01002051 struct lysp_node_uses *u = NULL;
2052 const char *str;
2053
2054 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2055 assert_non_null(ctx.ctx);
2056 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002057 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01002058
2059 /* invalid cardinality */
2060#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2061 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2062 assert_int_equal(LY_EVALID, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u)); \
2063 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2064 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2065
2066 TEST_DUP("description", "text1", "text2");
2067 TEST_DUP("reference", "1", "2");
2068 TEST_DUP("status", "current", "obsolete");
2069 TEST_DUP("when", "true", "false");
2070#undef TEST_DUP
2071
2072 /* full content */
2073 str = "grpref {augment some/node;description test;if-feature f;reference test;refine some/other/node;status current;when true;m:ext;} ...";
2074 assert_int_equal(LY_SUCCESS, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u));
2075 assert_non_null(u);
2076 assert_int_equal(LYS_USES, u->nodetype);
2077 assert_string_equal("grpref", u->name);
2078 assert_string_equal("test", u->dsc);
2079 assert_non_null(u->exts);
2080 assert_non_null(u->iffeatures);
2081 assert_string_equal("test", u->ref);
2082 assert_non_null(u->augments);
2083 assert_non_null(u->refines);
2084 assert_non_null(u->when);
2085 assert_null(u->parent);
2086 assert_null(u->next);
2087 assert_int_equal(LYS_STATUS_CURR, u->flags);
2088 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2089
2090 *state = NULL;
2091 ly_ctx_destroy(ctx.ctx, NULL);
2092}
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002093
2094
2095static void
2096test_augment(void **state)
2097{
2098 *state = test_augment;
2099
Radek Krejcie7b95092019-05-15 11:03:07 +02002100 struct lys_parser_ctx ctx = {0};
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002101 struct lysp_augment *a = NULL;
2102 const char *str;
2103
2104 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2105 assert_non_null(ctx.ctx);
2106 ctx.line = 1;
Radek Krejcib4adbf12019-02-25 13:35:22 +01002107 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci5a7c4a52019-02-12 15:45:11 +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_augment(&ctx, &str, NULL, &a)); \
2113 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejcib4adbf12019-02-25 13:35:22 +01002114 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002115
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 = "/target/nodeid {action x; anydata any;anyxml anyxml; case cs; choice ch;container c;description test;if-feature f;leaf l {type string;}"
2124 "leaf-list ll {type string;} list li;notification not;reference test;status current;uses g;when true;m:ext;} ...";
2125 assert_int_equal(LY_SUCCESS, parse_augment(&ctx, &str, NULL, &a));
2126 assert_non_null(a);
2127 assert_int_equal(LYS_AUGMENT, a->nodetype);
2128 assert_string_equal("/target/nodeid", a->nodeid);
2129 assert_string_equal("test", a->dsc);
2130 assert_non_null(a->exts);
2131 assert_non_null(a->iffeatures);
2132 assert_string_equal("test", a->ref);
2133 assert_non_null(a->when);
2134 assert_null(a->parent);
2135 assert_int_equal(LYS_STATUS_CURR, a->flags);
Radek Krejcib4adbf12019-02-25 13:35:22 +01002136 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002137
2138 *state = NULL;
2139 ly_ctx_destroy(ctx.ctx, NULL);
2140}
2141
Radek Krejci80dd33e2018-09-26 15:57:18 +02002142int main(void)
2143{
2144 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002145 cmocka_unit_test_setup(test_helpers, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002146 cmocka_unit_test_setup(test_comments, logger_setup),
Radek Krejciefd22f62018-09-27 11:47:58 +02002147 cmocka_unit_test_setup(test_arg, logger_setup),
Radek Krejcidcc7b322018-10-11 14:24:02 +02002148 cmocka_unit_test_setup(test_stmts, logger_setup),
Radek Krejci05b13982018-11-28 16:22:07 +01002149 cmocka_unit_test_setup_teardown(test_minmax, logger_setup, logger_teardown),
Radek Krejci40544fa2019-01-11 09:38:37 +01002150 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002151 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci2c02f3e2018-10-16 10:54:38 +02002152 cmocka_unit_test_setup(test_feature, logger_setup),
2153 cmocka_unit_test_setup(test_deviation, logger_setup),
2154 cmocka_unit_test_setup(test_deviate, logger_setup),
Radek Krejci8c370832018-11-02 15:10:03 +01002155 cmocka_unit_test_setup(test_container, logger_setup),
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002156 cmocka_unit_test_setup_teardown(test_leaf, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01002157 cmocka_unit_test_setup_teardown(test_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002158 cmocka_unit_test_setup_teardown(test_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01002159 cmocka_unit_test_setup_teardown(test_choice, logger_setup, logger_teardown),
Radek Krejcia9026eb2018-12-12 16:04:47 +01002160 cmocka_unit_test_setup_teardown(test_case, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01002161 cmocka_unit_test_setup_teardown(test_anydata, logger_setup, logger_teardown),
2162 cmocka_unit_test_setup_teardown(test_anyxml, logger_setup, logger_teardown),
Radek Krejcif538ce52019-03-05 10:46:14 +01002163 cmocka_unit_test_setup_teardown(test_action, logger_setup, logger_teardown),
Radek Krejcifc11bd72019-04-11 16:00:05 +02002164 cmocka_unit_test_setup_teardown(test_notification, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01002165 cmocka_unit_test_setup_teardown(test_grouping, logger_setup, logger_teardown),
2166 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejcib4adbf12019-02-25 13:35:22 +01002167 cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002168 };
2169
2170 return cmocka_run_group_tests(tests, NULL, NULL);
2171}