blob: b279b16e5f19f46e4f4991c310d4b4df7b4ad99d [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);
Radek Krejciabdd8062019-06-11 16:44:19 +0200402 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
403 assert_int_equal(YANG_LEFT_BRACE, kw);
404 assert_int_equal(1, len);
405 assert_string_equal("{", word);
406 assert_string_equal("", str);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200407
408 str = "/input { "; /* invalid slash */
409 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
410 logbuf_assert("Invalid identifier first character '/'. Line number 4.");
411
412 str = "not-a-statement-nor-extension { "; /* invalid identifier */
413 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
414 logbuf_assert("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword. Line number 4.");
415
416 str = "path;"; /* missing sep after the keyword */
417 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
418 logbuf_assert("Invalid character sequence \"path;\", expected a keyword followed by a separator. Line number 4.");
419
420 str = "action ";
421 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
422 assert_int_equal(YANG_ACTION, kw);
423 assert_int_equal(6, len);
424 str = "anydata ";
425 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
426 assert_int_equal(YANG_ANYDATA, kw);
427 assert_int_equal(7, len);
428 str = "anyxml ";
429 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
430 assert_int_equal(YANG_ANYXML, kw);
431 assert_int_equal(6, len);
432 str = "argument ";
433 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
434 assert_int_equal(YANG_ARGUMENT, kw);
435 assert_int_equal(8, len);
436 str = "augment ";
437 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
438 assert_int_equal(YANG_AUGMENT, kw);
439 assert_int_equal(7, len);
440 str = "base ";
441 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
442 assert_int_equal(YANG_BASE, kw);
443 assert_int_equal(4, len);
444 str = "belongs-to ";
445 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
446 assert_int_equal(YANG_BELONGS_TO, kw);
447 assert_int_equal(10, len);
448 str = "bit ";
449 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
450 assert_int_equal(YANG_BIT, kw);
451 assert_int_equal(3, len);
452 str = "case ";
453 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
454 assert_int_equal(YANG_CASE, kw);
455 assert_int_equal(4, len);
456 str = "choice ";
457 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
458 assert_int_equal(YANG_CHOICE, kw);
459 assert_int_equal(6, len);
460 str = "config ";
461 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
462 assert_int_equal(YANG_CONFIG, kw);
463 assert_int_equal(6, len);
464 str = "contact ";
465 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
466 assert_int_equal(YANG_CONTACT, kw);
467 assert_int_equal(7, len);
468 str = "container ";
469 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
470 assert_int_equal(YANG_CONTAINER, kw);
471 assert_int_equal(9, len);
472 str = "default ";
473 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
474 assert_int_equal(YANG_DEFAULT, kw);
475 assert_int_equal(7, len);
476 str = "description ";
477 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
478 assert_int_equal(YANG_DESCRIPTION, kw);
479 assert_int_equal(11, len);
480 str = "deviate ";
481 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
482 assert_int_equal(YANG_DEVIATE, kw);
483 assert_int_equal(7, len);
484 str = "deviation ";
485 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
486 assert_int_equal(YANG_DEVIATION, kw);
487 assert_int_equal(9, len);
488 str = "enum ";
489 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
490 assert_int_equal(YANG_ENUM, kw);
491 assert_int_equal(4, len);
492 str = "error-app-tag ";
493 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
494 assert_int_equal(YANG_ERROR_APP_TAG, kw);
495 assert_int_equal(13, len);
496 str = "error-message ";
497 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
498 assert_int_equal(YANG_ERROR_MESSAGE, kw);
499 assert_int_equal(13, len);
500 str = "extension ";
501 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
502 assert_int_equal(YANG_EXTENSION, kw);
503 assert_int_equal(9, len);
504 str = "feature ";
505 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
506 assert_int_equal(YANG_FEATURE, kw);
507 assert_int_equal(7, len);
508 str = "fraction-digits ";
509 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
510 assert_int_equal(YANG_FRACTION_DIGITS, kw);
511 assert_int_equal(15, len);
512 str = "grouping ";
513 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
514 assert_int_equal(YANG_GROUPING, kw);
515 assert_int_equal(8, len);
516 str = "identity ";
517 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
518 assert_int_equal(YANG_IDENTITY, kw);
519 assert_int_equal(8, len);
520 str = "if-feature ";
521 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
522 assert_int_equal(YANG_IF_FEATURE, kw);
523 assert_int_equal(10, len);
524 str = "import ";
525 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
526 assert_int_equal(YANG_IMPORT, kw);
527 assert_int_equal(6, len);
528 str = "include ";
529 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
530 assert_int_equal(YANG_INCLUDE, kw);
531 assert_int_equal(7, len);
532 str = "input{";
533 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
534 assert_int_equal(YANG_INPUT, kw);
535 assert_int_equal(5, len);
536 str = "key ";
537 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
538 assert_int_equal(YANG_KEY, kw);
539 assert_int_equal(3, len);
540 str = "leaf ";
541 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
542 assert_int_equal(YANG_LEAF, kw);
543 assert_int_equal(4, len);
544 str = "leaf-list ";
545 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
546 assert_int_equal(YANG_LEAF_LIST, kw);
547 assert_int_equal(9, len);
548 str = "length ";
549 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
550 assert_int_equal(YANG_LENGTH, kw);
551 assert_int_equal(6, len);
552 str = "list ";
553 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
554 assert_int_equal(YANG_LIST, kw);
555 assert_int_equal(4, len);
556 str = "mandatory ";
557 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
558 assert_int_equal(YANG_MANDATORY, kw);
559 assert_int_equal(9, len);
560 str = "max-elements ";
561 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
562 assert_int_equal(YANG_MAX_ELEMENTS, kw);
563 assert_int_equal(12, len);
564 str = "min-elements ";
565 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
566 assert_int_equal(YANG_MIN_ELEMENTS, kw);
567 assert_int_equal(12, len);
568 str = "modifier ";
569 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
570 assert_int_equal(YANG_MODIFIER, kw);
571 assert_int_equal(8, len);
572 str = "module ";
573 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
574 assert_int_equal(YANG_MODULE, kw);
575 assert_int_equal(6, len);
576 str = "must ";
577 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
578 assert_int_equal(YANG_MUST, kw);
579 assert_int_equal(4, len);
580 str = "namespace ";
581 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
582 assert_int_equal(YANG_NAMESPACE, kw);
583 assert_int_equal(9, len);
584 str = "notification ";
585 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
586 assert_int_equal(YANG_NOTIFICATION, kw);
587 assert_int_equal(12, len);
588 str = "ordered-by ";
589 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
590 assert_int_equal(YANG_ORDERED_BY, kw);
591 assert_int_equal(10, len);
592 str = "organization ";
593 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
594 assert_int_equal(YANG_ORGANIZATION, kw);
595 assert_int_equal(12, len);
596 str = "output ";
597 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
598 assert_int_equal(YANG_OUTPUT, kw);
599 assert_int_equal(6, len);
600 str = "path ";
601 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
602 assert_int_equal(YANG_PATH, kw);
603 assert_int_equal(4, len);
604 str = "pattern ";
605 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
606 assert_int_equal(YANG_PATTERN, kw);
607 assert_int_equal(7, len);
608 str = "position ";
609 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
610 assert_int_equal(YANG_POSITION, kw);
611 assert_int_equal(8, len);
612 str = "prefix ";
613 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
614 assert_int_equal(YANG_PREFIX, kw);
615 assert_int_equal(6, len);
616 str = "presence ";
617 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
618 assert_int_equal(YANG_PRESENCE, kw);
619 assert_int_equal(8, len);
620 str = "range ";
621 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
622 assert_int_equal(YANG_RANGE, kw);
623 assert_int_equal(5, len);
624 str = "reference ";
625 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
626 assert_int_equal(YANG_REFERENCE, kw);
627 assert_int_equal(9, len);
628 str = "refine ";
629 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
630 assert_int_equal(YANG_REFINE, kw);
631 assert_int_equal(6, len);
632 str = "require-instance ";
633 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
634 assert_int_equal(YANG_REQUIRE_INSTANCE, kw);
635 assert_int_equal(16, len);
636 str = "revision ";
637 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
638 assert_int_equal(YANG_REVISION, kw);
639 assert_int_equal(8, len);
640 str = "revision-date ";
641 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
642 assert_int_equal(YANG_REVISION_DATE, kw);
643 assert_int_equal(13, len);
644 str = "rpc ";
645 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
646 assert_int_equal(YANG_RPC, kw);
647 assert_int_equal(3, len);
648 str = "status ";
649 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
650 assert_int_equal(YANG_STATUS, kw);
651 assert_int_equal(6, len);
652 str = "submodule ";
653 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
654 assert_int_equal(YANG_SUBMODULE, kw);
655 assert_int_equal(9, len);
656 str = "type ";
657 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
658 assert_int_equal(YANG_TYPE, kw);
659 assert_int_equal(4, len);
660 str = "typedef ";
661 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
662 assert_int_equal(YANG_TYPEDEF, kw);
663 assert_int_equal(7, len);
664 str = "unique ";
665 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
666 assert_int_equal(YANG_UNIQUE, kw);
667 assert_int_equal(6, len);
668 str = "units ";
669 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
670 assert_int_equal(YANG_UNITS, kw);
671 assert_int_equal(5, len);
672 str = "uses ";
673 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
674 assert_int_equal(YANG_USES, kw);
675 assert_int_equal(4, len);
676 str = "value ";
677 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
678 assert_int_equal(YANG_VALUE, kw);
679 assert_int_equal(5, len);
680 str = "when ";
681 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
682 assert_int_equal(YANG_WHEN, kw);
683 assert_int_equal(4, len);
684 str = "yang-version ";
685 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
686 assert_int_equal(YANG_YANG_VERSION, kw);
687 assert_int_equal(12, len);
688 str = "yin-element ";
689 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
690 assert_int_equal(YANG_YIN_ELEMENT, kw);
691 assert_int_equal(11, len);
Radek Krejci626df482018-10-11 15:06:31 +0200692 str = ";config false;";
693 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
694 assert_int_equal(YANG_SEMICOLON, kw);
695 assert_int_equal(1, len);
696 assert_string_equal("config false;", str);
697 str = "{ config false;";
698 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
699 assert_int_equal(YANG_LEFT_BRACE, kw);
700 assert_int_equal(1, len);
701 assert_string_equal(" config false;", str);
702 str = "}";
703 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
704 assert_int_equal(YANG_RIGHT_BRACE, kw);
705 assert_int_equal(1, len);
706 assert_string_equal("", str);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200707
708 /* geenric extension */
709 str = p = "nacm:default-deny-write;";
710 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
711 assert_int_equal(YANG_CUSTOM, kw);
712 assert_int_equal(23, len);
713 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200714}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200715
Radek Krejci05b13982018-11-28 16:22:07 +0100716static void
717test_minmax(void **state)
718{
719 *state = test_minmax;
720
Radek Krejcie7b95092019-05-15 11:03:07 +0200721 struct lys_parser_ctx ctx = {0};
Radek Krejci05b13982018-11-28 16:22:07 +0100722 uint16_t flags = 0;
723 uint32_t value = 0;
724 struct lysp_ext_instance *ext = NULL;
725 const char *str;
726
727 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
728 assert_non_null(ctx.ctx);
729 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100730 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci05b13982018-11-28 16:22:07 +0100731
Radek Krejcidf6cad12018-11-28 17:10:55 +0100732 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100733 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100734 logbuf_assert("Invalid value \"1invalid\" of \"min-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100735
736 flags = value = 0;
737 str = " -1; ...";
738 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
739 logbuf_assert("Invalid value \"-1\" of \"min-elements\". Line number 1.");
740
Radek Krejcidf6cad12018-11-28 17:10:55 +0100741 /* implementation limit */
742 flags = value = 0;
743 str = " 4294967296; ...";
744 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
745 logbuf_assert("Value \"4294967296\" is out of \"min-elements\" bounds. Line number 1.");
746
Radek Krejci05b13982018-11-28 16:22:07 +0100747 flags = value = 0;
748 str = " 1; ...";
749 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
750 assert_int_equal(LYS_SET_MIN, flags);
751 assert_int_equal(1, value);
752
753 flags = value = 0;
754 str = " 1 {m:ext;} ...";
755 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
756 assert_int_equal(LYS_SET_MIN, flags);
757 assert_int_equal(1, value);
758 assert_non_null(ext);
759 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
760 ext = NULL;
761
762 flags = value = 0;
763 str = " 1 {config true;} ...";
764 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
765 logbuf_assert("Invalid keyword \"config\" as a child of \"min-elements\". Line number 1.");
766
Radek Krejcidf6cad12018-11-28 17:10:55 +0100767 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100768 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100769 logbuf_assert("Invalid value \"1invalid\" of \"max-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100770
771 flags = value = 0;
772 str = " -1; ...";
773 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
774 logbuf_assert("Invalid value \"-1\" of \"max-elements\". Line number 1.");
775
Radek Krejcidf6cad12018-11-28 17:10:55 +0100776 /* implementation limit */
777 flags = value = 0;
778 str = " 4294967296; ...";
779 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
780 logbuf_assert("Value \"4294967296\" is out of \"max-elements\" bounds. Line number 1.");
781
Radek Krejci05b13982018-11-28 16:22:07 +0100782 flags = value = 0;
783 str = " 1; ...";
784 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
785 assert_int_equal(LYS_SET_MAX, flags);
786 assert_int_equal(1, value);
787
788 flags = value = 0;
789 str = " unbounded; ...";
790 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
791 assert_int_equal(LYS_SET_MAX, flags);
792 assert_int_equal(0, value);
793
794 flags = value = 0;
795 str = " 1 {m:ext;} ...";
796 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
797 assert_int_equal(LYS_SET_MAX, flags);
798 assert_int_equal(1, value);
799 assert_non_null(ext);
800 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
801 ext = NULL;
802
803 flags = value = 0;
804 str = " 1 {config true;} ...";
805 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
806 logbuf_assert("Invalid keyword \"config\" as a child of \"max-elements\". Line number 1.");
807
808 *state = NULL;
809 ly_ctx_destroy(ctx.ctx, NULL);
810}
811
Radek Krejci9fcacc12018-10-11 15:59:11 +0200812static struct lysp_module *
Radek Krejcie7b95092019-05-15 11:03:07 +0200813mod_renew(struct lys_parser_ctx *ctx)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200814{
Radek Krejci40544fa2019-01-11 09:38:37 +0100815 struct lysp_module *mod_p;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100816 static struct lys_module mod = {0};
817
818 lysc_module_free(mod.compiled, NULL);
819 lysp_module_free(mod.parsed);
820 FREE_STRING(mod.ctx, mod.name);
821 FREE_STRING(mod.ctx, mod.ns);
822 FREE_STRING(mod.ctx, mod.prefix);
823 FREE_STRING(mod.ctx, mod.filepath);
824 FREE_STRING(mod.ctx, mod.org);
825 FREE_STRING(mod.ctx, mod.contact);
826 FREE_STRING(mod.ctx, mod.dsc);
827 FREE_STRING(mod.ctx, mod.ref);
828 memset(&mod, 0, sizeof mod);
829 mod.ctx = ctx->ctx;
830
831 mod_p = calloc(1, sizeof *mod_p);
832 mod.parsed = mod_p;
833 mod_p->mod = &mod;
834 assert_non_null(mod_p);
835 return mod_p;
836}
837
838static struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +0200839submod_renew(struct lys_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100840{
841 lysp_submodule_free(ctx->ctx, submod);
842 submod = calloc(1, sizeof *submod);
843 assert_non_null(submod);
844 return submod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200845}
846
Radek Krejcid33273d2018-10-25 14:55:52 +0200847static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
848 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
849 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
850{
851 *module_data = user_data;
852 *format = LYS_IN_YANG;
853 *free_module_data = NULL;
854 return LY_SUCCESS;
855}
856
Radek Krejci9fcacc12018-10-11 15:59:11 +0200857static void
858test_module(void **state)
859{
Radek Krejci40544fa2019-01-11 09:38:37 +0100860 *state = test_module;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200861
Radek Krejcie7b95092019-05-15 11:03:07 +0200862 struct lys_parser_ctx ctx;
Radek Krejci40544fa2019-01-11 09:38:37 +0100863 struct lysp_module *mod = NULL;
864 struct lysp_submodule *submod = NULL;
865 struct lys_module *m;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200866 const char *str;
867
868 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
869 assert_non_null(ctx.ctx);
870 ctx.line = 1;
Radek Krejcia042ea12018-10-13 07:52:15 +0200871 ctx.indent = 0;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200872
Radek Krejci40544fa2019-01-11 09:38:37 +0100873 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200874
875 /* missing mandatory substatements */
876 str = " name {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100877 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
878 assert_string_equal("name", mod->mod->name);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200879 logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100880 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200881
882 str = " name {namespace urn:x;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100883 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
884 assert_string_equal("urn:x", mod->mod->ns);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200885 logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100886 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200887
888 str = " name {namespace urn:x;prefix \"x\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100889 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
890 assert_string_equal("x", mod->mod->prefix);
Radek Krejci40544fa2019-01-11 09:38:37 +0100891 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200892
Radek Krejci027d5802018-11-14 16:57:28 +0100893#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
894#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200895#define TEST_NODE(NODETYPE, INPUT, NAME) \
896 str = SCHEMA_BEGINNING INPUT; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100897 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200898 assert_non_null(mod->data); \
899 assert_int_equal(NODETYPE, mod->data->nodetype); \
900 assert_string_equal(NAME, mod->data->name); \
Radek Krejci40544fa2019-01-11 09:38:37 +0100901 mod = mod_renew(&ctx);
Radek Krejcia042ea12018-10-13 07:52:15 +0200902#define TEST_GENERIC(INPUT, TARGET, TEST) \
903 str = SCHEMA_BEGINNING INPUT; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100904 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200905 assert_non_null(TARGET); \
906 TEST; \
Radek Krejci40544fa2019-01-11 09:38:37 +0100907 mod = mod_renew(&ctx);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100908#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200909 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
Radek Krejci40544fa2019-01-11 09:38:37 +0100910 parse_module, mod, LINE, mod = mod_renew(&ctx))
Radek Krejcia042ea12018-10-13 07:52:15 +0200911
912 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100913 TEST_DUP("namespace", "y", "z", "1");
914 TEST_DUP("prefix", "y", "z", "1");
915 TEST_DUP("contact", "a", "b", "1");
916 TEST_DUP("description", "a", "b", "1");
917 TEST_DUP("organization", "a", "b", "1");
918 TEST_DUP("reference", "a", "b", "1");
Radek Krejcia042ea12018-10-13 07:52:15 +0200919
Radek Krejci70853c52018-10-15 14:46:16 +0200920 /* not allowed in module (submodule-specific) */
921 str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100922 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200923 logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100924 mod = mod_renew(&ctx);
Radek Krejci70853c52018-10-15 14:46:16 +0200925
Radek Krejcia042ea12018-10-13 07:52:15 +0200926 /* anydata */
927 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
928 /* anyxml */
929 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
930 /* augment */
931 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200932 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200933 /* choice */
934 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
935 /* contact 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100936 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact,
937 assert_string_equal("firstname surname", mod->mod->contact));
Radek Krejcia042ea12018-10-13 07:52:15 +0200938 /* container */
939 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
940 /* description 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100941 TEST_GENERIC("description \'some description\';}", mod->mod->dsc,
942 assert_string_equal("some description", mod->mod->dsc));
Radek Krejcia042ea12018-10-13 07:52:15 +0200943 /* deviation */
944 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200945 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200946 /* extension */
947 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200948 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200949 /* feature */
950 TEST_GENERIC("feature test;}", mod->features,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200951 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200952 /* grouping */
953 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200954 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200955 /* identity */
956 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200957 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200958 /* import */
Radek Krejci086c7132018-10-26 15:29:04 +0200959 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
960 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
961 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200962
Radek Krejcia042ea12018-10-13 07:52:15 +0200963 /* import - prefix collision */
Radek Krejci086c7132018-10-26 15:29:04 +0200964 str = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100965 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200966 logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100967 mod = mod_renew(&ctx);
Radek Krejci086c7132018-10-26 15:29:04 +0200968 str = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100969 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci086c7132018-10-26 15:29:04 +0200970 logbuf_assert("Prefix \"y\" already used to import \"zzz\" module. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100971 mod = mod_renew(&ctx);
Radek Krejci086c7132018-10-26 15:29:04 +0200972 str = "module" SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix z;}}";
973 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
974 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
975 logbuf_assert("Single revision of the module \"zzz\" referred twice.");
Radek Krejci70853c52018-10-15 14:46:16 +0200976
Radek Krejcia042ea12018-10-13 07:52:15 +0200977 /* include */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100978 store = 1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200979 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 +0200980 str = "module" SCHEMA_BEGINNING "include xxx;}";
981 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
982 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100983 logbuf_assert("Input data contains module in situation when a submodule is expected.");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100984 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200985
Radek Krejci9ed7a192018-10-31 16:23:51 +0100986 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +0100987 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 +0200988 str = "module" SCHEMA_BEGINNING "include xxx;}";
989 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
990 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
991 logbuf_assert("Included \"xxx\" submodule from \"name\" belongs-to a different module \"wrong-name\".");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100992 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200993
Radek Krejci313d9902018-11-08 09:42:58 +0100994 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 +0200995 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejci086c7132018-10-26 15:29:04 +0200996 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +0200997
Radek Krejcia042ea12018-10-13 07:52:15 +0200998 /* leaf */
999 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
1000 /* leaf-list */
1001 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
1002 /* list */
1003 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
1004 /* notification */
1005 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001006 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001007 /* organization 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001008 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org,
1009 assert_string_equal("CESNET a.l.e.", mod->mod->org));
Radek Krejcia042ea12018-10-13 07:52:15 +02001010 /* reference 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001011 TEST_GENERIC("reference RFC7950;}", mod->mod->ref,
1012 assert_string_equal("RFC7950", mod->mod->ref));
Radek Krejcia042ea12018-10-13 07:52:15 +02001013 /* revision */
1014 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib7db73a2018-10-24 14:18:40 +02001015 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +02001016 /* rpc */
1017 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001018 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001019 /* typedef */
1020 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001021 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001022 /* uses */
1023 TEST_NODE(LYS_USES, "uses test;}", "test");
1024 /* yang-version */
Radek Krejci027d5802018-11-14 16:57:28 +01001025 str = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001026 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +02001027 logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001028 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001029 str = SCHEMA_BEGINNING2 "yang-version 1.0;yang-version 1.1;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001030 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +02001031 logbuf_assert("Duplicate keyword \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001032 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001033 str = SCHEMA_BEGINNING2 "yang-version 1.0;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001034 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
1035 assert_int_equal(1, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +01001036 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001037 str = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001038 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
1039 assert_int_equal(2, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +01001040 mod = mod_renew(&ctx);
1041
1042 str = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
1043 m = mod->mod;
1044 free(mod);
1045 m->parsed = NULL;
1046 assert_int_equal(LY_EVALID, yang_parse_module(&ctx, str, m));
Radek Krejci0a1d0d42019-05-16 15:14:51 +02001047 logbuf_assert("Trailing garbage \"module q {names...\" after module, expected end-of-input. Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001048 mod = mod_renew(&ctx);
1049
1050 str = "prefix " SCHEMA_BEGINNING "}";
1051 m = mod->mod;
1052 free(mod);
1053 m->parsed = NULL;
1054 assert_int_equal(LY_EVALID, yang_parse_module(&ctx, str, m));
1055 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 3.");
1056 mod = mod_renew(&ctx);
Radek Krejci09306362018-10-15 15:26:01 +02001057
Radek Krejci156ccaf2018-10-15 15:49:17 +02001058 /* extensions */
1059 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001060 assert_string_equal("prefix:test", mod->exts[0].name);
1061 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
Radek Krejci40544fa2019-01-11 09:38:37 +01001062 mod = mod_renew(&ctx);
Radek Krejci156ccaf2018-10-15 15:49:17 +02001063
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001064 /* invalid substatement */
1065 str = SCHEMA_BEGINNING "must false;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001066 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001067 logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001068 mod = mod_renew(&ctx);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001069
Radek Krejci09306362018-10-15 15:26:01 +02001070 /* submodule */
Radek Krejci40544fa2019-01-11 09:38:37 +01001071 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001072
1073 /* missing mandatory substatements */
1074 str = " subname {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001075 lydict_remove(ctx.ctx, submod->name);
1076 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
1077 assert_string_equal("subname", submod->name);
Radek Krejci09306362018-10-15 15:26:01 +02001078 logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001079 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001080
Radek Krejci313d9902018-11-08 09:42:58 +01001081 str = " subname {belongs-to name {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001082 lydict_remove(ctx.ctx, submod->name);
1083 assert_int_equal(LY_SUCCESS, parse_submodule(&ctx, &str, submod));
1084 assert_string_equal("name", submod->belongsto);
1085 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001086
1087#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +01001088#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +02001089
1090 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001091 str = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ...";
1092 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod)); \
1093 logbuf_assert("Duplicate keyword \"belongs-to\". Line number 3."); \
1094 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001095
1096 /* not allowed in submodule (module-specific) */
1097 str = SCHEMA_BEGINNING "namespace \"urn:z\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001098 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001099 logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001100 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001101 str = SCHEMA_BEGINNING "prefix m;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001102 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001103 logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001104 submod = submod_renew(&ctx, submod);
Radek Krejcia042ea12018-10-13 07:52:15 +02001105
Radek Krejci40544fa2019-01-11 09:38:37 +01001106 str = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
1107 lysp_submodule_free(ctx.ctx, submod);
1108 submod = NULL;
1109 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx, str, &submod));
Radek Krejci0a1d0d42019-05-16 15:14:51 +02001110 logbuf_assert("Trailing garbage \"module q {names...\" after submodule, expected end-of-input. Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001111
1112 str = "prefix " SCHEMA_BEGINNING "}";
1113 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx, str, &submod));
1114 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 3.");
1115 submod = submod_renew(&ctx, submod);
1116
Radek Krejcia042ea12018-10-13 07:52:15 +02001117#undef TEST_GENERIC
1118#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001119#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +02001120#undef SCHEMA_BEGINNING
1121
Radek Krejci9fcacc12018-10-11 15:59:11 +02001122 lysp_module_free(mod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001123 lysp_submodule_free(ctx.ctx, submod);
Radek Krejci9fcacc12018-10-11 15:59:11 +02001124 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci40544fa2019-01-11 09:38:37 +01001125
1126 *state = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02001127}
1128
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001129static void
1130test_identity(void **state)
1131{
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001132 *state = test_identity;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001133
Radek Krejcie7b95092019-05-15 11:03:07 +02001134 struct lys_parser_ctx ctx;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001135 struct lysp_ident *ident = NULL;
1136 const char *str;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001137
1138 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1139 assert_non_null(ctx.ctx);
1140 ctx.line = 1;
1141 ctx.indent = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001142 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001143
1144 /* invalid cardinality */
1145#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001146 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001147 &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL)
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001148
Radek Krejci38222632019-02-12 16:55:05 +01001149 TEST_DUP("description", "a", "b");
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001150 TEST_DUP("reference", "a", "b");
1151 TEST_DUP("status", "current", "obsolete");
1152
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001153 /* full content */
1154 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 +02001155 assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident));
1156 assert_non_null(ident);
1157 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001158 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001159 ident = NULL;
1160
1161 /* invalid substatement */
1162 str = " test {organization XXX;}";
1163 assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident));
1164 logbuf_assert("Invalid keyword \"organization\" as a child of \"identity\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001165 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001166 ident = NULL;
1167
1168#undef TEST_DUP
1169
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001170 *state = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001171 ly_ctx_destroy(ctx.ctx, NULL);
1172}
1173
1174static void
1175test_feature(void **state)
1176{
1177 (void) state; /* unused */
1178
Radek Krejcie7b95092019-05-15 11:03:07 +02001179 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001180 struct lysp_feature *features = NULL;
1181 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001182
1183 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1184 assert_non_null(ctx.ctx);
1185 ctx.line = 1;
1186 ctx.indent = 0;
1187
1188 /* invalid cardinality */
1189#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1190 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001191 &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001192
1193 TEST_DUP("description", "a", "b");
1194 TEST_DUP("reference", "a", "b");
1195 TEST_DUP("status", "current", "obsolete");
1196
1197 /* full content */
1198 str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ...";
1199 assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features));
1200 assert_non_null(features);
1201 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001202 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001203 features = NULL;
1204
1205 /* invalid substatement */
1206 str = " test {organization XXX;}";
1207 assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features));
1208 logbuf_assert("Invalid keyword \"organization\" as a child of \"feature\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001209 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001210 features = NULL;
1211
1212#undef TEST_DUP
1213
1214 ly_ctx_destroy(ctx.ctx, NULL);
1215}
1216
1217static void
1218test_deviation(void **state)
1219{
1220 (void) state; /* unused */
1221
Radek Krejcie7b95092019-05-15 11:03:07 +02001222 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001223 struct lysp_deviation *d = NULL;
1224 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001225
1226 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1227 assert_non_null(ctx.ctx);
1228 ctx.line = 1;
1229 ctx.indent = 0;
1230
1231 /* invalid cardinality */
1232#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1233 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001234 &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001235
1236 TEST_DUP("description", "a", "b");
1237 TEST_DUP("reference", "a", "b");
1238
1239 /* full content */
1240 str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
1241 assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d));
1242 assert_non_null(d);
1243 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001244 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001245 d = NULL;
1246
1247 /* missing mandatory substatement */
1248 str = " test {description text;}";
1249 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1250 logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001251 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001252 d = NULL;
1253
1254 /* invalid substatement */
1255 str = " test {deviate not-supported; status obsolete;}";
1256 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1257 logbuf_assert("Invalid keyword \"status\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001258 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001259 d = NULL;
1260
1261#undef TEST_DUP
1262
1263 ly_ctx_destroy(ctx.ctx, NULL);
1264}
1265
1266static void
1267test_deviate(void **state)
1268{
1269 (void) state; /* unused */
1270
Radek Krejcie7b95092019-05-15 11:03:07 +02001271 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001272 struct lysp_deviate *d = NULL;
1273 const char *str;
1274
1275 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1276 assert_non_null(ctx.ctx);
1277 ctx.line = 1;
1278 ctx.indent = 0;
1279
1280 /* invalid cardinality */
1281#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
1282 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001283 &d, "1", lysp_deviate_free(ctx.ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001284
1285 TEST_DUP("add", "config", "true", "false");
1286 TEST_DUP("replace", "default", "int8", "uint8");
1287 TEST_DUP("add", "mandatory", "true", "false");
1288 TEST_DUP("add", "max-elements", "1", "2");
1289 TEST_DUP("add", "min-elements", "1", "2");
1290 TEST_DUP("replace", "type", "int8", "uint8");
1291 TEST_DUP("add", "units", "kilometers", "miles");
1292
1293 /* full contents */
1294 str = " not-supported {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 = " 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;} ...";
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 = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; 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 str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...";
1310 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1311 assert_non_null(d);
1312 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001313 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001314
1315 /* invalid substatements */
1316#define TEST_NOT_SUP(DEV, STMT, VALUE) \
1317 str = " "DEV" {"STMT" "VALUE";}..."; \
1318 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \
1319 logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001320 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001321
1322 TEST_NOT_SUP("not-supported", "units", "meters");
1323 TEST_NOT_SUP("not-supported", "must", "1");
1324 TEST_NOT_SUP("not-supported", "unique", "x");
1325 TEST_NOT_SUP("not-supported", "default", "a");
1326 TEST_NOT_SUP("not-supported", "config", "true");
1327 TEST_NOT_SUP("not-supported", "mandatory", "true");
1328 TEST_NOT_SUP("not-supported", "min-elements", "1");
1329 TEST_NOT_SUP("not-supported", "max-elements", "2");
1330 TEST_NOT_SUP("not-supported", "type", "string");
1331 TEST_NOT_SUP("add", "type", "string");
1332 TEST_NOT_SUP("delete", "config", "true");
1333 TEST_NOT_SUP("delete", "mandatory", "true");
1334 TEST_NOT_SUP("delete", "min-elements", "1");
1335 TEST_NOT_SUP("delete", "max-elements", "2");
1336 TEST_NOT_SUP("delete", "type", "string");
1337 TEST_NOT_SUP("replace", "must", "1");
1338 TEST_NOT_SUP("replace", "unique", "a");
1339
1340 str = " nonsence; ...";
1341 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d));
1342 logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1.");
1343 assert_null(d);
1344
1345#undef TEST_NOT_SUP
1346#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001347
1348 ly_ctx_destroy(ctx.ctx, NULL);
1349}
1350
Radek Krejci8c370832018-11-02 15:10:03 +01001351static void
1352test_container(void **state)
1353{
1354 (void) state; /* unused */
1355
Radek Krejcie7b95092019-05-15 11:03:07 +02001356 struct lys_parser_ctx ctx = {0};
Radek Krejci8c370832018-11-02 15:10:03 +01001357 struct lysp_node_container *c = NULL;
1358 const char *str;
1359
1360 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1361 assert_non_null(ctx.ctx);
1362 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001363 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +01001364
1365 /* invalid cardinality */
1366#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1367 str = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1368 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); \
1369 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001370 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001371
1372 TEST_DUP("config", "true", "false");
1373 TEST_DUP("description", "text1", "text2");
1374 TEST_DUP("presence", "true", "false");
1375 TEST_DUP("reference", "1", "2");
1376 TEST_DUP("status", "current", "obsolete");
1377 TEST_DUP("when", "true", "false");
1378#undef TEST_DUP
1379
1380 /* full content */
Radek Krejci313d9902018-11-08 09:42:58 +01001381 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;}"
1382 "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 +01001383 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1384 assert_non_null(c);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001385 assert_int_equal(LYS_CONTAINER, c->nodetype);
1386 assert_string_equal("cont", c->name);
Radek Krejci8c370832018-11-02 15:10:03 +01001387 assert_non_null(c->actions);
1388 assert_non_null(c->child);
1389 assert_string_equal("test", c->dsc);
1390 assert_non_null(c->exts);
1391 assert_non_null(c->groupings);
1392 assert_non_null(c->iffeatures);
1393 assert_non_null(c->musts);
1394 assert_non_null(c->notifs);
1395 assert_string_equal("true", c->presence);
1396 assert_string_equal("test", c->ref);
1397 assert_non_null(c->typedefs);
1398 assert_non_null(c->when);
1399 assert_null(c->parent);
1400 assert_null(c->next);
1401 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags);
Radek Krejci313d9902018-11-08 09:42:58 +01001402 ly_set_erase(&ctx.tpdfs_nodes, NULL);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001403 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001404
1405 /* invalid */
1406 str = " cont {augment /root;} ...";
1407 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1408 logbuf_assert("Invalid keyword \"augment\" as a child of \"container\". Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001409 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001410 str = " cont {nonsence true;} ...";
1411 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1412 logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001413 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001414
Radek Krejcif538ce52019-03-05 10:46:14 +01001415 ctx.mod_version = 1; /* simulate YANG 1.0 */
1416 str = " cont {action x;} ...";
1417 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1418 logbuf_assert("Invalid keyword \"action\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1419 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
1420
Radek Krejci8c370832018-11-02 15:10:03 +01001421 ly_ctx_destroy(ctx.ctx, NULL);
1422}
1423
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001424static void
1425test_leaf(void **state)
1426{
1427 *state = test_leaf;
1428
Radek Krejcie7b95092019-05-15 11:03:07 +02001429 struct lys_parser_ctx ctx = {0};
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001430 struct lysp_node_leaf *l = NULL;
1431 const char *str;
1432
1433 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1434 assert_non_null(ctx.ctx);
1435 ctx.line = 1;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001436 //ctx.mod->version = 2; /* simulate YANG 1.1 */
1437
1438 /* invalid cardinality */
1439#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1440 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1441 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1442 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1443 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1444
1445 TEST_DUP("config", "true", "false");
1446 TEST_DUP("default", "x", "y");
1447 TEST_DUP("description", "text1", "text2");
1448 TEST_DUP("mandatory", "true", "false");
1449 TEST_DUP("reference", "1", "2");
1450 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001451 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001452 TEST_DUP("units", "text1", "text2");
1453 TEST_DUP("when", "true", "false");
1454#undef TEST_DUP
1455
1456 /* full content - without mandatory which is mutual exclusive with default */
1457 str = "l {config false;default \"xxx\";description test;if-feature f;"
1458 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
1459 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1460 assert_non_null(l);
1461 assert_int_equal(LYS_LEAF, l->nodetype);
1462 assert_string_equal("l", l->name);
1463 assert_string_equal("test", l->dsc);
1464 assert_string_equal("xxx", l->dflt);
1465 assert_string_equal("yyy", l->units);
1466 assert_string_equal("string", l->type.name);
1467 assert_non_null(l->exts);
1468 assert_non_null(l->iffeatures);
1469 assert_non_null(l->musts);
1470 assert_string_equal("test", l->ref);
1471 assert_non_null(l->when);
1472 assert_null(l->parent);
1473 assert_null(l->next);
1474 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, l->flags);
1475 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1476
1477 /* full content - now with mandatory */
1478 str = "l {mandatory true; type string;} ...";
1479 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1480 assert_non_null(l);
1481 assert_int_equal(LYS_LEAF, l->nodetype);
1482 assert_string_equal("l", l->name);
1483 assert_string_equal("string", l->type.name);
1484 assert_int_equal(LYS_MAND_TRUE, l->flags);
1485 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1486
1487 /* invalid */
1488 str = " l {mandatory true; default xx; type string;} ...";
1489 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001490 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"leaf\". Line number 1.");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001491 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1492
1493 str = " l {description \"missing type\";} ...";
1494 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1495 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf\". Line number 1.");
1496 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1497
1498 *state = NULL;
1499 ly_ctx_destroy(ctx.ctx, NULL);
1500}
1501
Radek Krejci0e5d8382018-11-28 16:37:53 +01001502static void
1503test_leaflist(void **state)
1504{
1505 *state = test_leaf;
1506
Radek Krejcie7b95092019-05-15 11:03:07 +02001507 struct lys_parser_ctx ctx = {0};
Radek Krejci0e5d8382018-11-28 16:37:53 +01001508 struct lysp_node_leaflist *ll = NULL;
1509 const char *str;
1510
1511 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1512 assert_non_null(ctx.ctx);
1513 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001514 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001515
1516 /* invalid cardinality */
1517#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1518 str = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1519 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); \
1520 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1521 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1522
1523 TEST_DUP("config", "true", "false");
1524 TEST_DUP("description", "text1", "text2");
1525 TEST_DUP("max-elements", "10", "20");
1526 TEST_DUP("min-elements", "10", "20");
1527 TEST_DUP("ordered-by", "user", "system");
1528 TEST_DUP("reference", "1", "2");
1529 TEST_DUP("status", "current", "obsolete");
1530 TEST_DUP("type", "int8", "uint8");
1531 TEST_DUP("units", "text1", "text2");
1532 TEST_DUP("when", "true", "false");
1533#undef TEST_DUP
1534
1535 /* full content - without min-elements which is mutual exclusive with default */
1536 str = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
1537 "max-elements 10;must 'expr';ordered-by user;reference test;"
1538 "status current;type string; units zzz;when true;m:ext;} ...";
1539 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1540 assert_non_null(ll);
1541 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1542 assert_string_equal("ll", ll->name);
1543 assert_string_equal("test", ll->dsc);
1544 assert_non_null(ll->dflts);
1545 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
1546 assert_string_equal("xxx", ll->dflts[0]);
1547 assert_string_equal("yyy", ll->dflts[1]);
1548 assert_string_equal("zzz", ll->units);
1549 assert_int_equal(10, ll->max);
1550 assert_int_equal(0, ll->min);
1551 assert_string_equal("string", ll->type.name);
1552 assert_non_null(ll->exts);
1553 assert_non_null(ll->iffeatures);
1554 assert_non_null(ll->musts);
1555 assert_string_equal("test", ll->ref);
1556 assert_non_null(ll->when);
1557 assert_null(ll->parent);
1558 assert_null(ll->next);
1559 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
1560 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1561
1562 /* full content - now with min-elements */
1563 str = "ll {min-elements 10; type string;} ...";
1564 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1565 assert_non_null(ll);
1566 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1567 assert_string_equal("ll", ll->name);
1568 assert_string_equal("string", ll->type.name);
1569 assert_int_equal(0, ll->max);
1570 assert_int_equal(10, ll->min);
1571 assert_int_equal(LYS_SET_MIN, ll->flags);
1572 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1573
1574 /* invalid */
1575 str = " ll {min-elements 1; default xx; type string;} ...";
1576 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001577 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 +01001578 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1579
1580 str = " ll {description \"missing type\";} ...";
1581 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1582 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf-list\". Line number 1.");
1583 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1584
Radek Krejcidf6cad12018-11-28 17:10:55 +01001585 str = " ll {type string; min-elements 10; max-elements 1;} ..."; /* invalid combination of min/max */
1586 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1587 logbuf_assert("Invalid combination of min-elements and max-elements: min value 10 is bigger than the max value 1. Line number 1.");
1588 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1589
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001590 ctx.mod_version = 1; /* simulate YANG 1.0 - default statement is not allowed */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001591 str = " ll {default xx; type string;} ...";
1592 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1593 logbuf_assert("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1594 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1595
1596 *state = NULL;
1597 ly_ctx_destroy(ctx.ctx, NULL);
1598}
1599
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001600static void
1601test_list(void **state)
1602{
1603 *state = test_list;
1604
Radek Krejcie7b95092019-05-15 11:03:07 +02001605 struct lys_parser_ctx ctx = {0};
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001606 struct lysp_node_list *l = NULL;
1607 const char *str;
1608
1609 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1610 assert_non_null(ctx.ctx);
1611 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001612 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001613
1614 /* invalid cardinality */
1615#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1616 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1617 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1618 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1619 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1620
1621 TEST_DUP("config", "true", "false");
1622 TEST_DUP("description", "text1", "text2");
1623 TEST_DUP("key", "one", "two");
1624 TEST_DUP("max-elements", "10", "20");
1625 TEST_DUP("min-elements", "10", "20");
1626 TEST_DUP("ordered-by", "user", "system");
1627 TEST_DUP("reference", "1", "2");
1628 TEST_DUP("status", "current", "obsolete");
1629 TEST_DUP("when", "true", "false");
1630#undef TEST_DUP
1631
1632 /* full content */
1633 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;}"
1634 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1635 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
1636 assert_int_equal(LY_SUCCESS, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1637 assert_non_null(l);
1638 assert_int_equal(LYS_LIST, l->nodetype);
1639 assert_string_equal("l", l->name);
1640 assert_string_equal("test", l->dsc);
1641 assert_string_equal("l", l->key);
1642 assert_non_null(l->uniques);
1643 assert_int_equal(2, LY_ARRAY_SIZE(l->uniques));
1644 assert_string_equal("xxx", l->uniques[0]);
1645 assert_string_equal("yyy", l->uniques[1]);
1646 assert_int_equal(10, l->max);
1647 assert_int_equal(1, l->min);
1648 assert_non_null(l->exts);
1649 assert_non_null(l->iffeatures);
1650 assert_non_null(l->musts);
1651 assert_string_equal("test", l->ref);
1652 assert_non_null(l->when);
1653 assert_null(l->parent);
1654 assert_null(l->next);
1655 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, l->flags);
1656 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1657 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1658
Radek Krejcif538ce52019-03-05 10:46:14 +01001659 /* invalid content */
1660 ctx.mod_version = 1; /* simulate YANG 1.0 */
1661 str = "l {action x;} ...";
1662 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1663 logbuf_assert("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1664 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1665
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001666 *state = NULL;
1667 ly_ctx_destroy(ctx.ctx, NULL);
1668}
1669
Radek Krejci056d0a82018-12-06 16:57:25 +01001670static void
1671test_choice(void **state)
1672{
1673 *state = test_choice;
1674
Radek Krejcie7b95092019-05-15 11:03:07 +02001675 struct lys_parser_ctx ctx = {0};
Radek Krejci056d0a82018-12-06 16:57:25 +01001676 struct lysp_node_choice *ch = NULL;
1677 const char *str;
1678
1679 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1680 assert_non_null(ctx.ctx);
1681 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001682 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci056d0a82018-12-06 16:57:25 +01001683
1684 /* invalid cardinality */
1685#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1686 str = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1687 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); \
1688 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1689 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1690
1691 TEST_DUP("config", "true", "false");
1692 TEST_DUP("default", "a", "b");
1693 TEST_DUP("description", "text1", "text2");
1694 TEST_DUP("mandatory", "true", "false");
1695 TEST_DUP("reference", "1", "2");
1696 TEST_DUP("status", "current", "obsolete");
1697 TEST_DUP("when", "true", "false");
1698#undef TEST_DUP
1699
Radek Krejcia9026eb2018-12-12 16:04:47 +01001700 /* full content - without default due to a collision with mandatory */
1701 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 +01001702 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
1703 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1704 assert_non_null(ch);
1705 assert_int_equal(LYS_CHOICE, ch->nodetype);
1706 assert_string_equal("ch", ch->name);
1707 assert_string_equal("test", ch->dsc);
1708 assert_non_null(ch->exts);
1709 assert_non_null(ch->iffeatures);
1710 assert_string_equal("test", ch->ref);
1711 assert_non_null(ch->when);
1712 assert_null(ch->parent);
1713 assert_null(ch->next);
1714 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, ch->flags);
1715 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1716
Radek Krejcia9026eb2018-12-12 16:04:47 +01001717 /* full content - the default missing from the previous node */
1718 str = "ch {default c;case c;} ...";
1719 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1720 assert_non_null(ch);
1721 assert_int_equal(LYS_CHOICE, ch->nodetype);
1722 assert_string_equal("ch", ch->name);
1723 assert_string_equal("c", ch->dflt);
1724 assert_int_equal(0, ch->flags);
1725 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1726
1727 /* invalid content */
1728 str = "ch {mandatory true; default c1; case c1 {leaf x{type string;}}} ...";
1729 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1730 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"choice\". Line number 1.");
1731 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1732
1733 *state = NULL;
1734 ly_ctx_destroy(ctx.ctx, NULL);
1735}
1736
1737static void
1738test_case(void **state)
1739{
1740 *state = test_case;
1741
Radek Krejcie7b95092019-05-15 11:03:07 +02001742 struct lys_parser_ctx ctx = {0};
Radek Krejcia9026eb2018-12-12 16:04:47 +01001743 struct lysp_node_case *cs = NULL;
1744 const char *str;
1745
1746 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1747 assert_non_null(ctx.ctx);
1748 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001749 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcia9026eb2018-12-12 16:04:47 +01001750
1751 /* invalid cardinality */
1752#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1753 str = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1754 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs)); \
1755 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1756 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1757
1758 TEST_DUP("description", "text1", "text2");
1759 TEST_DUP("reference", "1", "2");
1760 TEST_DUP("status", "current", "obsolete");
1761 TEST_DUP("when", "true", "false");
1762#undef TEST_DUP
1763
1764 /* full content */
1765 str = "cs {anydata any;anyxml anyxml; choice ch;container c;description test;if-feature f;leaf l {type string;}"
1766 "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
1767 assert_int_equal(LY_SUCCESS, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1768 assert_non_null(cs);
1769 assert_int_equal(LYS_CASE, cs->nodetype);
1770 assert_string_equal("cs", cs->name);
1771 assert_string_equal("test", cs->dsc);
1772 assert_non_null(cs->exts);
1773 assert_non_null(cs->iffeatures);
1774 assert_string_equal("test", cs->ref);
1775 assert_non_null(cs->when);
1776 assert_null(cs->parent);
1777 assert_null(cs->next);
1778 assert_int_equal(LYS_STATUS_CURR, cs->flags);
1779 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1780
1781 /* invalid content */
1782 str = "cs {config true} ...";
1783 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1784 logbuf_assert("Invalid keyword \"config\" as a child of \"case\". Line number 1.");
1785 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1786
Radek Krejci056d0a82018-12-06 16:57:25 +01001787 *state = NULL;
1788 ly_ctx_destroy(ctx.ctx, NULL);
1789}
1790
Radek Krejci9800fb82018-12-13 14:26:23 +01001791static void
1792test_any(void **state, enum yang_keyword kw)
1793{
1794 *state = test_any;
1795
Radek Krejcie7b95092019-05-15 11:03:07 +02001796 struct lys_parser_ctx ctx = {0};
Radek Krejci9800fb82018-12-13 14:26:23 +01001797 struct lysp_node_anydata *any = NULL;
1798 const char *str;
1799
1800 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1801 assert_non_null(ctx.ctx);
1802 ctx.line = 1;
Radek Krejci9800fb82018-12-13 14:26:23 +01001803 if (kw == YANG_ANYDATA) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001804 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001805 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001806 ctx.mod_version = 1; /* simulate YANG 1.0 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001807 }
1808
1809 /* invalid cardinality */
1810#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1811 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1812 assert_int_equal(LY_EVALID, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any)); \
1813 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1814 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1815
1816 TEST_DUP("config", "true", "false");
1817 TEST_DUP("description", "text1", "text2");
1818 TEST_DUP("mandatory", "true", "false");
1819 TEST_DUP("reference", "1", "2");
1820 TEST_DUP("status", "current", "obsolete");
1821 TEST_DUP("when", "true", "false");
1822#undef TEST_DUP
1823
1824 /* full content */
1825 str = "any {config true;description test;if-feature f;mandatory true;must 'expr';reference test;status current;when true;m:ext;} ...";
1826 assert_int_equal(LY_SUCCESS, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any));
1827 assert_non_null(any);
1828 assert_int_equal(kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML, any->nodetype);
1829 assert_string_equal("any", any->name);
1830 assert_string_equal("test", any->dsc);
1831 assert_non_null(any->exts);
1832 assert_non_null(any->iffeatures);
1833 assert_non_null(any->musts);
1834 assert_string_equal("test", any->ref);
1835 assert_non_null(any->when);
1836 assert_null(any->parent);
1837 assert_null(any->next);
1838 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags);
1839 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1840
1841 *state = NULL;
1842 ly_ctx_destroy(ctx.ctx, NULL);
1843}
1844
1845static void
1846test_anydata(void **state)
1847{
1848 return test_any(state, YANG_ANYDATA);
1849}
1850
1851static void
1852test_anyxml(void **state)
1853{
1854 return test_any(state, YANG_ANYXML);
1855}
1856
Radek Krejcie86bf772018-12-14 11:39:53 +01001857static void
1858test_grouping(void **state)
1859{
1860 *state = test_grouping;
1861
Radek Krejcie7b95092019-05-15 11:03:07 +02001862 struct lys_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01001863 struct lysp_grp *grp = NULL;
1864 const char *str;
1865
1866 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1867 assert_non_null(ctx.ctx);
1868 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001869 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001870
1871 /* invalid cardinality */
1872#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1873 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1874 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp)); \
1875 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1876 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1877
1878 TEST_DUP("description", "text1", "text2");
1879 TEST_DUP("reference", "1", "2");
1880 TEST_DUP("status", "current", "obsolete");
1881#undef TEST_DUP
1882
1883 /* full content */
1884 str = "grp {action x;anydata any;anyxml anyxml; choice ch;container c;description test;grouping g;leaf l {type string;}"
1885 "leaf-list ll {type string;} list li;notification not;reference test;status current;typedef t {type int8;}uses g;m:ext;} ...";
1886 assert_int_equal(LY_SUCCESS, parse_grouping(&ctx, &str, NULL, &grp));
1887 assert_non_null(grp);
1888 assert_int_equal(LYS_GROUPING, grp->nodetype);
1889 assert_string_equal("grp", grp->name);
1890 assert_string_equal("test", grp->dsc);
1891 assert_non_null(grp->exts);
1892 assert_string_equal("test", grp->ref);
1893 assert_null(grp->parent);
1894 assert_int_equal( LYS_STATUS_CURR, grp->flags);
1895 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1896 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1897
1898 /* invalid content */
1899 str = "grp {config true} ...";
1900 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1901 logbuf_assert("Invalid keyword \"config\" as a child of \"grouping\". Line number 1.");
1902 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1903
1904 str = "grp {must 'expr'} ...";
1905 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1906 logbuf_assert("Invalid keyword \"must\" as a child of \"grouping\". Line number 1.");
1907 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1908
1909 *state = NULL;
1910 ly_ctx_destroy(ctx.ctx, NULL);
1911}
1912
1913static void
Radek Krejcif538ce52019-03-05 10:46:14 +01001914test_action(void **state)
1915{
1916 *state = test_action;
1917
Radek Krejcie7b95092019-05-15 11:03:07 +02001918 struct lys_parser_ctx ctx = {0};
Radek Krejcif538ce52019-03-05 10:46:14 +01001919 struct lysp_action *rpcs = NULL;
1920 struct lysp_node_container *c = NULL;
1921 const char *str;
1922
1923 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1924 assert_non_null(ctx.ctx);
1925 ctx.line = 1;
1926 ctx.mod_version = 2; /* simulate YANG 1.1 */
1927
1928 /* invalid cardinality */
1929#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1930 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1931 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs)); \
1932 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1933 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1934
1935 TEST_DUP("description", "text1", "text2");
1936 TEST_DUP("input", "", "");
1937 TEST_DUP("output", "", "");
1938 TEST_DUP("reference", "1", "2");
1939 TEST_DUP("status", "current", "obsolete");
1940#undef TEST_DUP
1941
1942 /* full content */
1943 str = "top;";
1944 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1945 str = "func {description test;grouping grp;if-feature f;reference test;status current;typedef mytype {type int8;} m:ext;"
1946 "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1947 " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}"
1948 "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1949 " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ...";
1950 assert_int_equal(LY_SUCCESS, parse_action(&ctx, &str, (struct lysp_node*)c, &rpcs));
1951 assert_non_null(rpcs);
1952 assert_int_equal(LYS_ACTION, rpcs->nodetype);
1953 assert_string_equal("func", rpcs->name);
1954 assert_string_equal("test", rpcs->dsc);
1955 assert_non_null(rpcs->exts);
1956 assert_non_null(rpcs->iffeatures);
1957 assert_string_equal("test", rpcs->ref);
1958 assert_non_null(rpcs->groupings);
1959 assert_non_null(rpcs->typedefs);
1960 assert_int_equal(LYS_STATUS_CURR, rpcs->flags);
1961 /* input */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001962 assert_int_equal(rpcs->input.nodetype, LYS_INPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001963 assert_non_null(rpcs->input.groupings);
1964 assert_non_null(rpcs->input.exts);
1965 assert_non_null(rpcs->input.musts);
1966 assert_non_null(rpcs->input.typedefs);
1967 assert_non_null(rpcs->input.data);
1968 /* output */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001969 assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001970 assert_non_null(rpcs->output.groupings);
1971 assert_non_null(rpcs->output.exts);
1972 assert_non_null(rpcs->output.musts);
1973 assert_non_null(rpcs->output.typedefs);
1974 assert_non_null(rpcs->output.data);
1975
1976 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1977 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1978
1979 /* invalid content */
1980 str = "func {config true} ...";
1981 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs));
1982 logbuf_assert("Invalid keyword \"config\" as a child of \"rpc\". Line number 1.");
1983 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1984
1985 *state = NULL;
1986 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
1987 ly_ctx_destroy(ctx.ctx, NULL);
1988}
1989
1990static void
Radek Krejcifc11bd72019-04-11 16:00:05 +02001991test_notification(void **state)
1992{
1993 *state = test_notification;
1994
Radek Krejcie7b95092019-05-15 11:03:07 +02001995 struct lys_parser_ctx ctx = {0};
Radek Krejcifc11bd72019-04-11 16:00:05 +02001996 struct lysp_notif *notifs = NULL;
1997 struct lysp_node_container *c = NULL;
1998 const char *str;
1999
2000 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2001 assert_non_null(ctx.ctx);
2002 ctx.line = 1;
2003 ctx.mod_version = 2; /* simulate YANG 1.1 */
2004
2005 /* invalid cardinality */
2006#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2007 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2008 assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, &notifs)); \
2009 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2010 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2011
2012 TEST_DUP("description", "text1", "text2");
2013 TEST_DUP("reference", "1", "2");
2014 TEST_DUP("status", "current", "obsolete");
2015#undef TEST_DUP
2016
2017 /* full content */
2018 str = "top;";
2019 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
2020 str = "ntf {anydata a1; anyxml a2; choice ch; container c; description test; grouping grp; if-feature f; leaf l {type int8;}"
2021 "leaf-list ll {type int8;} list li; must 1; reference test; status current; typedef mytype {type int8;} uses grp; m:ext;}";
2022 assert_int_equal(LY_SUCCESS, parse_notif(&ctx, &str, (struct lysp_node*)c, &notifs));
2023 assert_non_null(notifs);
2024 assert_int_equal(LYS_NOTIF, notifs->nodetype);
2025 assert_string_equal("ntf", notifs->name);
2026 assert_string_equal("test", notifs->dsc);
2027 assert_non_null(notifs->exts);
2028 assert_non_null(notifs->iffeatures);
2029 assert_string_equal("test", notifs->ref);
2030 assert_non_null(notifs->groupings);
2031 assert_non_null(notifs->typedefs);
2032 assert_non_null(notifs->musts);
2033 assert_non_null(notifs->data);
2034 assert_int_equal(LYS_STATUS_CURR, notifs->flags);
2035
2036 ly_set_erase(&ctx.tpdfs_nodes, NULL);
2037 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2038
2039 /* invalid content */
2040 str = "ntf {config true} ...";
2041 assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, &notifs));
2042 logbuf_assert("Invalid keyword \"config\" as a child of \"notification\". Line number 1.");
2043 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2044
2045 *state = NULL;
2046 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
2047 ly_ctx_destroy(ctx.ctx, NULL);
2048}
2049
2050static void
Radek Krejcie86bf772018-12-14 11:39:53 +01002051test_uses(void **state)
2052{
2053 *state = test_uses;
2054
Radek Krejcie7b95092019-05-15 11:03:07 +02002055 struct lys_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01002056 struct lysp_node_uses *u = NULL;
2057 const char *str;
2058
2059 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2060 assert_non_null(ctx.ctx);
2061 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002062 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01002063
2064 /* invalid cardinality */
2065#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2066 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2067 assert_int_equal(LY_EVALID, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u)); \
2068 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2069 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2070
2071 TEST_DUP("description", "text1", "text2");
2072 TEST_DUP("reference", "1", "2");
2073 TEST_DUP("status", "current", "obsolete");
2074 TEST_DUP("when", "true", "false");
2075#undef TEST_DUP
2076
2077 /* full content */
2078 str = "grpref {augment some/node;description test;if-feature f;reference test;refine some/other/node;status current;when true;m:ext;} ...";
2079 assert_int_equal(LY_SUCCESS, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u));
2080 assert_non_null(u);
2081 assert_int_equal(LYS_USES, u->nodetype);
2082 assert_string_equal("grpref", u->name);
2083 assert_string_equal("test", u->dsc);
2084 assert_non_null(u->exts);
2085 assert_non_null(u->iffeatures);
2086 assert_string_equal("test", u->ref);
2087 assert_non_null(u->augments);
2088 assert_non_null(u->refines);
2089 assert_non_null(u->when);
2090 assert_null(u->parent);
2091 assert_null(u->next);
2092 assert_int_equal(LYS_STATUS_CURR, u->flags);
2093 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2094
2095 *state = NULL;
2096 ly_ctx_destroy(ctx.ctx, NULL);
2097}
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002098
2099
2100static void
2101test_augment(void **state)
2102{
2103 *state = test_augment;
2104
Radek Krejcie7b95092019-05-15 11:03:07 +02002105 struct lys_parser_ctx ctx = {0};
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002106 struct lysp_augment *a = NULL;
2107 const char *str;
2108
2109 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2110 assert_non_null(ctx.ctx);
2111 ctx.line = 1;
Radek Krejcib4adbf12019-02-25 13:35:22 +01002112 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002113
2114 /* invalid cardinality */
2115#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2116 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2117 assert_int_equal(LY_EVALID, parse_augment(&ctx, &str, NULL, &a)); \
2118 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejcib4adbf12019-02-25 13:35:22 +01002119 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002120
2121 TEST_DUP("description", "text1", "text2");
2122 TEST_DUP("reference", "1", "2");
2123 TEST_DUP("status", "current", "obsolete");
2124 TEST_DUP("when", "true", "false");
2125#undef TEST_DUP
2126
2127 /* full content */
2128 str = "/target/nodeid {action x; anydata any;anyxml anyxml; case cs; choice ch;container c;description test;if-feature f;leaf l {type string;}"
2129 "leaf-list ll {type string;} list li;notification not;reference test;status current;uses g;when true;m:ext;} ...";
2130 assert_int_equal(LY_SUCCESS, parse_augment(&ctx, &str, NULL, &a));
2131 assert_non_null(a);
2132 assert_int_equal(LYS_AUGMENT, a->nodetype);
2133 assert_string_equal("/target/nodeid", a->nodeid);
2134 assert_string_equal("test", a->dsc);
2135 assert_non_null(a->exts);
2136 assert_non_null(a->iffeatures);
2137 assert_string_equal("test", a->ref);
2138 assert_non_null(a->when);
2139 assert_null(a->parent);
2140 assert_int_equal(LYS_STATUS_CURR, a->flags);
Radek Krejcib4adbf12019-02-25 13:35:22 +01002141 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002142
2143 *state = NULL;
2144 ly_ctx_destroy(ctx.ctx, NULL);
2145}
2146
Radek Krejci80dd33e2018-09-26 15:57:18 +02002147int main(void)
2148{
2149 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002150 cmocka_unit_test_setup(test_helpers, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002151 cmocka_unit_test_setup(test_comments, logger_setup),
Radek Krejciefd22f62018-09-27 11:47:58 +02002152 cmocka_unit_test_setup(test_arg, logger_setup),
Radek Krejcidcc7b322018-10-11 14:24:02 +02002153 cmocka_unit_test_setup(test_stmts, logger_setup),
Radek Krejci05b13982018-11-28 16:22:07 +01002154 cmocka_unit_test_setup_teardown(test_minmax, logger_setup, logger_teardown),
Radek Krejci40544fa2019-01-11 09:38:37 +01002155 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002156 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci2c02f3e2018-10-16 10:54:38 +02002157 cmocka_unit_test_setup(test_feature, logger_setup),
2158 cmocka_unit_test_setup(test_deviation, logger_setup),
2159 cmocka_unit_test_setup(test_deviate, logger_setup),
Radek Krejci8c370832018-11-02 15:10:03 +01002160 cmocka_unit_test_setup(test_container, logger_setup),
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002161 cmocka_unit_test_setup_teardown(test_leaf, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01002162 cmocka_unit_test_setup_teardown(test_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002163 cmocka_unit_test_setup_teardown(test_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01002164 cmocka_unit_test_setup_teardown(test_choice, logger_setup, logger_teardown),
Radek Krejcia9026eb2018-12-12 16:04:47 +01002165 cmocka_unit_test_setup_teardown(test_case, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01002166 cmocka_unit_test_setup_teardown(test_anydata, logger_setup, logger_teardown),
2167 cmocka_unit_test_setup_teardown(test_anyxml, logger_setup, logger_teardown),
Radek Krejcif538ce52019-03-05 10:46:14 +01002168 cmocka_unit_test_setup_teardown(test_action, logger_setup, logger_teardown),
Radek Krejcifc11bd72019-04-11 16:00:05 +02002169 cmocka_unit_test_setup_teardown(test_notification, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01002170 cmocka_unit_test_setup_teardown(test_grouping, logger_setup, logger_teardown),
2171 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejcib4adbf12019-02-25 13:35:22 +01002172 cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002173 };
2174
2175 return cmocka_run_group_tests(tests, NULL, NULL);
2176}